From 06305c4e35cc8f1f96bd4f44e50e5e3dedf3a9e0 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Sat, 24 Aug 2024 15:38:18 -0400 Subject: [PATCH 1/7] WebGPURenderer: Support for access previous frame textures using pass() --- types/three/src/nodes/display/PassNode.d.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/types/three/src/nodes/display/PassNode.d.ts b/types/three/src/nodes/display/PassNode.d.ts index 44aaac186..9b385818f 100644 --- a/types/three/src/nodes/display/PassNode.d.ts +++ b/types/three/src/nodes/display/PassNode.d.ts @@ -16,8 +16,11 @@ declare class PassTextureNode extends TextureNode { declare class PassMultipleTextureNode extends PassTextureNode { textureName: string; + previousTexture: boolean; - constructor(passNode: PassNode, textureName: string); + constructor(passNode: PassNode, textureName: string, previousTexture?: boolean); + + updateTexture(): void; } declare class PassNode extends TempNode { @@ -37,8 +40,14 @@ declare class PassNode extends TempNode { getTexture(name: string): Texture; + getPreviousTexture(name: string): Texture; + + toggleTexture(name: string): void; + getTextureNode(name?: string): ShaderNodeObject; + getPreviousTextureNode(name?: string): ShaderNodeObject; + getViewZNode(name?: string): ShaderNodeObject; getLinearDepthNode(name?: string): ShaderNodeObject; From 5fa008d2332517bc9dbc31991659680dfe17c90d Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Sat, 24 Aug 2024 15:38:35 -0400 Subject: [PATCH 2/7] Update three.js --- three.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/three.js b/three.js index bf5710ada..cec6ded9a 160000 --- a/three.js +++ b/three.js @@ -1 +1 @@ -Subproject commit bf5710ada0314800823bf42dc0f41c004dc4ba4f +Subproject commit cec6ded9aff17859c939ba15c7cb537fa3389190 From 7bfb9ef653b82174fdf12adba6dc1d289238df03 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Sat, 24 Aug 2024 15:38:51 -0400 Subject: [PATCH 3/7] Add src --- src-testing/src/Three.Legacy.d.ts | 20 + src-testing/src/Three.WebGPU.d.ts | 195 +++ src-testing/src/Three.d.ts | 213 +++ .../src/animation/AnimationAction.d.ts | 86 + src-testing/src/animation/AnimationClip.d.ts | 59 + src-testing/src/animation/AnimationMixer.d.ts | 39 + .../src/animation/AnimationObjectGroup.d.ts | 17 + src-testing/src/animation/AnimationUtils.d.ts | 60 + src-testing/src/animation/KeyframeTrack.d.ts | 55 + .../src/animation/PropertyBinding.d.ts | 43 + src-testing/src/animation/PropertyMixer.d.ts | 17 + .../tracks/BooleanKeyframeTrack.d.ts | 10 + .../animation/tracks/ColorKeyframeTrack.d.ts | 11 + .../animation/tracks/NumberKeyframeTrack.d.ts | 11 + .../tracks/QuaternionKeyframeTrack.d.ts | 11 + .../animation/tracks/StringKeyframeTrack.d.ts | 10 + .../animation/tracks/VectorKeyframeTrack.d.ts | 11 + src-testing/src/audio/Audio.d.ts | 270 ++++ src-testing/src/audio/AudioAnalyser.d.ts | 58 + src-testing/src/audio/AudioContext.d.ts | 19 + src-testing/src/audio/AudioListener.d.ts | 96 ++ src-testing/src/audio/PositionalAudio.d.ts | 101 ++ src-testing/src/cameras/ArrayCamera.d.ts | 32 + src-testing/src/cameras/Camera.d.ts | 74 + src-testing/src/cameras/CubeCamera.d.ts | 68 + .../src/cameras/OrthographicCamera.d.ts | 174 ++ .../src/cameras/PerspectiveCamera.d.ts | 254 +++ src-testing/src/cameras/StereoCamera.d.ts | 50 + src-testing/src/constants.d.ts | 926 +++++++++++ src-testing/src/core/BufferAttribute.d.ts | 637 ++++++++ src-testing/src/core/BufferGeometry.d.ts | 422 +++++ src-testing/src/core/Clock.d.ts | 72 + src-testing/src/core/EventDispatcher.d.ts | 85 + src-testing/src/core/GLBufferAttribute.d.ts | 121 ++ .../src/core/InstancedBufferAttribute.d.ts | 32 + .../src/core/InstancedBufferGeometry.d.ts | 37 + .../src/core/InstancedInterleavedBuffer.d.ts | 22 + src-testing/src/core/InterleavedBuffer.d.ts | 162 ++ .../src/core/InterleavedBufferAttribute.d.ts | 201 +++ src-testing/src/core/Layers.d.ts | 72 + src-testing/src/core/Object3D.d.ts | 672 ++++++++ src-testing/src/core/Raycaster.d.ts | 207 +++ src-testing/src/core/RenderTarget.d.ts | 96 ++ src-testing/src/core/Uniform.d.ts | 38 + src-testing/src/core/UniformsGroup.d.ts | 33 + src-testing/src/extras/DataUtils.d.ts | 22 + src-testing/src/extras/Earcut.d.ts | 15 + src-testing/src/extras/ImageUtils.d.ts | 32 + src-testing/src/extras/PMREMGenerator.d.ts | 82 + src-testing/src/extras/ShapeUtils.d.ts | 25 + src-testing/src/extras/TextureUtils.d.ts | 42 + src-testing/src/extras/core/Curve.d.ts | 162 ++ src-testing/src/extras/core/CurvePath.d.ts | 77 + .../src/extras/core/Interpolations.d.ts | 36 + src-testing/src/extras/core/Path.d.ts | 166 ++ src-testing/src/extras/core/Shape.d.ts | 86 + src-testing/src/extras/core/ShapePath.d.ts | 98 ++ src-testing/src/extras/curves/ArcCurve.d.ts | 41 + .../src/extras/curves/CatmullRomCurve3.d.ts | 77 + .../src/extras/curves/CubicBezierCurve.d.ts | 72 + .../src/extras/curves/CubicBezierCurve3.d.ts | 72 + src-testing/src/extras/curves/Curves.d.ts | 10 + .../src/extras/curves/EllipseCurve.d.ts | 115 ++ src-testing/src/extras/curves/LineCurve.d.ts | 42 + src-testing/src/extras/curves/LineCurve3.d.ts | 42 + .../extras/curves/QuadraticBezierCurve.d.ts | 64 + .../extras/curves/QuadraticBezierCurve3.d.ts | 64 + .../src/extras/curves/SplineCurve.d.ts | 52 + src-testing/src/geometries/BoxGeometry.d.ts | 59 + .../src/geometries/CapsuleGeometry.d.ts | 48 + .../src/geometries/CircleGeometry.d.ts | 51 + src-testing/src/geometries/ConeGeometry.d.ts | 64 + .../src/geometries/CylinderGeometry.d.ts | 64 + .../src/geometries/DodecahedronGeometry.d.ts | 25 + src-testing/src/geometries/EdgesGeometry.d.ts | 41 + .../src/geometries/ExtrudeGeometry.d.ts | 152 ++ src-testing/src/geometries/Geometries.d.ts | 21 + .../src/geometries/IcosahedronGeometry.d.ts | 26 + src-testing/src/geometries/LatheGeometry.d.ts | 55 + .../src/geometries/OctahedronGeometry.d.ts | 25 + src-testing/src/geometries/PlaneGeometry.d.ts | 48 + .../src/geometries/PolyhedronGeometry.d.ts | 54 + src-testing/src/geometries/RingGeometry.d.ts | 59 + src-testing/src/geometries/ShapeGeometry.d.ts | 53 + .../src/geometries/SphereGeometry.d.ts | 67 + .../src/geometries/TetrahedronGeometry.d.ts | 25 + src-testing/src/geometries/TorusGeometry.d.ts | 49 + .../src/geometries/TorusKnotGeometry.d.ts | 59 + src-testing/src/geometries/TubeGeometry.d.ts | 86 + .../src/geometries/WireframeGeometry.d.ts | 40 + src-testing/src/helpers/ArrowHelper.d.ts | 93 ++ src-testing/src/helpers/AxesHelper.d.ts | 50 + src-testing/src/helpers/Box3Helper.d.ts | 44 + src-testing/src/helpers/BoxHelper.d.ts | 64 + src-testing/src/helpers/CameraHelper.d.ts | 80 + .../src/helpers/DirectionalLightHelper.d.ts | 81 + src-testing/src/helpers/GridHelper.d.ts | 47 + .../src/helpers/HemisphereLightHelper.d.ts | 72 + src-testing/src/helpers/PlaneHelper.d.ts | 50 + src-testing/src/helpers/PointLightHelper.d.ts | 73 + src-testing/src/helpers/PolarGridHelper.d.ts | 55 + src-testing/src/helpers/SkeletonHelper.d.ts | 78 + src-testing/src/helpers/SpotLightHelper.d.ts | 77 + src-testing/src/lights/AmbientLight.d.ts | 36 + src-testing/src/lights/DirectionalLight.d.ts | 103 ++ .../src/lights/DirectionalLightShadow.d.ts | 73 + src-testing/src/lights/HemisphereLight.d.ts | 61 + src-testing/src/lights/Light.d.ts | 82 + src-testing/src/lights/LightProbe.d.ts | 47 + src-testing/src/lights/LightShadow.d.ts | 169 ++ src-testing/src/lights/PointLight.d.ts | 102 ++ src-testing/src/lights/PointLightShadow.d.ts | 22 + src-testing/src/lights/RectAreaLight.d.ts | 82 + src-testing/src/lights/SpotLight.d.ts | 164 ++ src-testing/src/lights/SpotLightShadow.d.ts | 72 + .../src/lights/webgpu/IESSpotLight.d.ts | 6 + src-testing/src/loaders/AnimationLoader.d.ts | 9 + src-testing/src/loaders/AudioLoader.d.ts | 6 + .../src/loaders/BufferGeometryLoader.d.ts | 10 + src-testing/src/loaders/Cache.d.ts | 21 + .../src/loaders/CompressedTextureLoader.d.ts | 14 + .../src/loaders/CubeTextureLoader.d.ts | 14 + .../src/loaders/DataTextureLoader.d.ts | 14 + src-testing/src/loaders/FileLoader.d.ts | 19 + .../src/loaders/ImageBitmapLoader.d.ts | 22 + src-testing/src/loaders/ImageLoader.d.ts | 17 + src-testing/src/loaders/Loader.d.ts | 50 + src-testing/src/loaders/LoaderUtils.d.ts | 10 + src-testing/src/loaders/LoadingManager.d.ts | 69 + src-testing/src/loaders/MaterialLoader.d.ts | 19 + src-testing/src/loaders/ObjectLoader.d.ts | 35 + src-testing/src/loaders/TextureLoader.d.ts | 18 + .../src/materials/LineBasicMaterial.d.ts | 60 + .../src/materials/LineDashedMaterial.d.ts | 40 + src-testing/src/materials/Material.d.ts | 631 ++++++++ src-testing/src/materials/Materials.d.ts | 18 + .../src/materials/MeshBasicMaterial.d.ts | 139 ++ .../src/materials/MeshDepthMaterial.d.ts | 76 + .../src/materials/MeshDistanceMaterial.d.ts | 62 + .../src/materials/MeshLambertMaterial.d.ts | 204 +++ .../src/materials/MeshMatcapMaterial.d.ts | 117 ++ .../src/materials/MeshNormalMaterial.d.ts | 93 ++ .../src/materials/MeshPhongMaterial.d.ts | 228 +++ .../src/materials/MeshPhysicalMaterial.d.ts | 236 +++ .../src/materials/MeshStandardMaterial.d.ts | 218 +++ .../src/materials/MeshToonMaterial.d.ts | 178 +++ src-testing/src/materials/PointsMaterial.d.ts | 61 + .../src/materials/RawShaderMaterial.d.ts | 14 + src-testing/src/materials/ShaderMaterial.d.ts | 167 ++ src-testing/src/materials/ShadowMaterial.d.ts | 39 + src-testing/src/materials/SpriteMaterial.d.ts | 66 + src-testing/src/math/Box2.d.ts | 48 + src-testing/src/math/Box3.d.ts | 66 + src-testing/src/math/Color.d.ts | 358 +++++ src-testing/src/math/ColorManagement.d.ts | 49 + src-testing/src/math/Cylindrical.d.ts | 26 + src-testing/src/math/Euler.d.ts | 50 + src-testing/src/math/Frustum.d.ts | 30 + src-testing/src/math/Interpolant.d.ts | 10 + src-testing/src/math/Line3.d.ts | 29 + src-testing/src/math/MathUtils.d.ts | 137 ++ src-testing/src/math/Matrix2.d.ts | 53 + src-testing/src/math/Matrix3.d.ts | 184 +++ src-testing/src/math/Matrix4.d.ts | 284 ++++ src-testing/src/math/Plane.d.ts | 47 + src-testing/src/math/Quaternion.d.ts | 188 +++ src-testing/src/math/Ray.d.ts | 60 + src-testing/src/math/Sphere.d.ts | 47 + src-testing/src/math/Spherical.d.ts | 27 + src-testing/src/math/SphericalHarmonics3.d.ts | 50 + src-testing/src/math/Triangle.d.ts | 86 + src-testing/src/math/Vector2.d.ts | 321 ++++ src-testing/src/math/Vector3.d.ts | 301 ++++ src-testing/src/math/Vector4.d.ts | 239 +++ .../math/interpolants/CubicInterpolant.d.ts | 7 + .../interpolants/DiscreteInterpolant.d.ts | 7 + .../math/interpolants/LinearInterpolant.d.ts | 7 + .../QuaternionLinearInterpolant.d.ts | 7 + src-testing/src/nodes/Nodes.ts | 435 +++++ .../src/nodes/accessors/AccessorsUtils.d.ts | 9 + .../src/nodes/accessors/BatchNode.d.ts | 14 + .../src/nodes/accessors/BitangentNode.d.ts | 9 + .../nodes/accessors/BufferAttributeNode.ts | 134 ++ .../src/nodes/accessors/BufferNode.d.ts | 17 + .../src/nodes/accessors/CameraNode.d.ts | 15 + .../src/nodes/accessors/ClippingNode.d.ts | 16 + .../src/nodes/accessors/CubeTextureNode.d.ts | 34 + .../src/nodes/accessors/InstanceNode.d.ts | 13 + .../src/nodes/accessors/MaterialNode.d.ts | 130 ++ .../nodes/accessors/MaterialProperties.d.ts | 4 + .../accessors/MaterialReferenceNode.d.ts | 15 + .../src/nodes/accessors/ModelNode.d.ts | 20 + .../accessors/ModelViewProjectionNode.d.ts | 8 + .../src/nodes/accessors/NormalNode.d.ts | 12 + .../src/nodes/accessors/Object3DNode.d.ts | 26 + .../src/nodes/accessors/PointUVNode.d.ts | 10 + .../src/nodes/accessors/PositionNode.d.ts | 9 + .../src/nodes/accessors/ReferenceNode.d.ts | 27 + .../nodes/accessors/ReflectVectorNode.d.ts | 9 + .../accessors/RendererReferenceNode.d.ts | 15 + .../src/nodes/accessors/SkinningNode.d.ts | 20 + .../nodes/accessors/StorageBufferNode.d.ts | 38 + .../nodes/accessors/StorageTextureNode.d.ts | 40 + .../src/nodes/accessors/TangentNode.d.ts | 12 + .../src/nodes/accessors/Texture3DNode.d.ts | 17 + .../nodes/accessors/TextureBicubicNode.d.ts | 18 + .../src/nodes/accessors/TextureNode.ts | 369 +++++ src-testing/src/nodes/accessors/UVNode.d.ts | 4 + .../src/nodes/accessors/UniformArrayNode.d.ts | 30 + .../src/nodes/accessors/UserDataNode.d.ts | 15 + .../src/nodes/accessors/VertexColorNode.d.ts | 12 + src-testing/src/nodes/code/CodeNode.ts | 66 + .../src/nodes/code/ExpressionNode.d.ts | 9 + .../src/nodes/code/FunctionCallNode.d.ts | 25 + src-testing/src/nodes/code/FunctionNode.ts | 100 ++ src-testing/src/nodes/core/AssignNode.d.ts | 18 + src-testing/src/nodes/core/AttributeNode.d.ts | 19 + src-testing/src/nodes/core/BypassNode.d.ts | 18 + src-testing/src/nodes/core/CacheNode.d.ts | 20 + src-testing/src/nodes/core/ConstNode.d.ts | 9 + src-testing/src/nodes/core/ContextNode.ts | 55 + src-testing/src/nodes/core/IndexNode.d.ts | 20 + src-testing/src/nodes/core/InputNode.ts | 65 + src-testing/src/nodes/core/LightingModel.d.ts | 46 + src-testing/src/nodes/core/MRTNode.d.ts | 19 + src-testing/src/nodes/core/Node.ts | 417 +++++ src-testing/src/nodes/core/NodeAttribute.ts | 11 + src-testing/src/nodes/core/NodeBuilder.ts | 1128 +++++++++++++ src-testing/src/nodes/core/NodeCache.ts | 26 + src-testing/src/nodes/core/NodeCode.ts | 11 + src-testing/src/nodes/core/NodeFrame.ts | 127 ++ src-testing/src/nodes/core/NodeFunction.ts | 16 + .../src/nodes/core/NodeFunctionInput.d.ts | 7 + src-testing/src/nodes/core/NodeKeywords.ts | 58 + src-testing/src/nodes/core/NodeParser.ts | 7 + src-testing/src/nodes/core/NodeUniform.ts | 27 + src-testing/src/nodes/core/NodeUtils.ts | 137 ++ src-testing/src/nodes/core/NodeVar.ts | 10 + src-testing/src/nodes/core/NodeVarying.ts | 13 + .../src/nodes/core/OutputStructNode.d.ts | 12 + src-testing/src/nodes/core/PropertyNode.d.ts | 43 + src-testing/src/nodes/core/StackNode.ts | 87 + src-testing/src/nodes/core/StructTypeNode.ts | 18 + src-testing/src/nodes/core/TempNode.d.ts | 10 + src-testing/src/nodes/core/UniformGroup.d.ts | 7 + .../src/nodes/core/UniformGroupNode.ts | 46 + src-testing/src/nodes/core/UniformNode.ts | 90 ++ src-testing/src/nodes/core/VarNode.d.ts | 20 + src-testing/src/nodes/core/VaryingNode.d.ts | 21 + src-testing/src/nodes/core/constants.ts | 28 + .../src/nodes/display/AfterImageNode.d.ts | 25 + .../src/nodes/display/AnamorphicNode.d.ts | 31 + .../src/nodes/display/BleachBypassNode.d.ts | 10 + .../src/nodes/display/BlendModeNode.d.ts | 47 + src-testing/src/nodes/display/BloomNode.d.ts | 35 + .../nodes/display/ColorAdjustmentNode.d.ts | 47 + .../src/nodes/display/ColorSpaceNode.d.ts | 35 + .../src/nodes/display/DenoiseNode.d.ts | 38 + .../src/nodes/display/DepthOfFieldNode.d.ts | 30 + .../src/nodes/display/DotScreenNode.d.ts | 27 + src-testing/src/nodes/display/FXAANode.d.ts | 19 + src-testing/src/nodes/display/FilmNode.d.ts | 25 + .../src/nodes/display/FrontFacingNode.d.ts | 10 + src-testing/src/nodes/display/GTAONode.d.ts | 46 + .../src/nodes/display/GaussianBlurNode.d.ts | 31 + src-testing/src/nodes/display/Lut3DNode.d.ts | 30 + .../src/nodes/display/NormalMapNode.d.ts | 21 + src-testing/src/nodes/display/PassNode.d.ts | 71 + .../src/nodes/display/PixelationPassNode.d.ts | 67 + .../src/nodes/display/PosterizeNode.d.ts | 20 + .../src/nodes/display/RGBShiftNode.d.ts | 24 + .../src/nodes/display/RenderOutputNode.d.ts | 28 + src-testing/src/nodes/display/SepiaNode.d.ts | 10 + .../src/nodes/display/SobelOperatorNode.d.ts | 17 + .../src/nodes/display/ToneMappingNode.d.ts | 33 + .../src/nodes/display/TransitionNode.d.ts | 41 + .../src/nodes/display/ViewportDepthNode.d.ts | 30 + .../display/ViewportDepthTextureNode.d.ts | 12 + .../src/nodes/display/ViewportNode.d.ts | 31 + .../display/ViewportSharedTextureNode.d.ts | 18 + .../nodes/display/ViewportTextureNode.d.ts | 33 + src-testing/src/nodes/fog/FogExp2Node.d.ts | 18 + src-testing/src/nodes/fog/FogNode.ts | 38 + src-testing/src/nodes/fog/FogRangeNode.d.ts | 23 + .../src/nodes/functions/BSDF/BRDF_GGX.d.ts | 15 + .../nodes/functions/BSDF/BRDF_Lambert.d.ts | 7 + .../src/nodes/functions/BSDF/BRDF_Sheen.d.ts | 7 + .../src/nodes/functions/BSDF/DFGApprox.d.ts | 11 + .../src/nodes/functions/BSDF/D_GGX.d.ts | 10 + .../functions/BSDF/D_GGX_Anisotropic.d.ts | 10 + .../src/nodes/functions/BSDF/F_Schlick.d.ts | 7 + src-testing/src/nodes/functions/BSDF/LTC.d.ts | 9 + .../functions/BSDF/V_GGX_SmithCorrelated.d.ts | 11 + .../V_GGX_SmithCorrelated_Anisotropic.d.ts | 16 + .../nodes/functions/BasicLightingModel.d.ts | 7 + .../nodes/functions/PhongLightingModel.d.ts | 7 + .../functions/PhysicalLightingModel.d.ts | 30 + .../src/nodes/functions/ShadowMaskModel.d.ts | 9 + .../nodes/functions/ToonLightingModel.d.ts | 4 + .../material/getGeometryRoughness.d.ts | 6 + .../functions/material/getRoughness.d.ts | 7 + src-testing/src/nodes/geometry/RangeNode.d.ts | 19 + src-testing/src/nodes/gpgpu/ComputeNode.ts | 67 + src-testing/src/nodes/lighting/AONode.d.ts | 8 + .../src/nodes/lighting/AnalyticLightNode.d.ts | 8 + .../nodes/lighting/BasicEnvironmentNode.d.ts | 10 + .../src/nodes/lighting/BasicLightMapNode.d.ts | 8 + .../src/nodes/lighting/EnvironmentNode.ts | 121 ++ .../nodes/lighting/HemisphereLightNode.d.ts | 13 + .../src/nodes/lighting/IrradianceNode.d.ts | 8 + .../src/nodes/lighting/LightUtils.d.ts | 9 + .../src/nodes/lighting/LightingContextNode.ts | 58 + .../src/nodes/lighting/LightingNode.d.ts | 7 + src-testing/src/nodes/lighting/LightsNode.ts | 168 ++ .../src/nodes/lighting/PointLightNode.d.ts | 10 + .../src/nodes/lighting/RectAreaLightNode.d.ts | 21 + .../src/nodes/lighting/SpotLightNode.d.ts | 15 + src-testing/src/nodes/loaders/NodeLoader.d.ts | 16 + .../src/nodes/loaders/NodeMaterialLoader.d.ts | 8 + .../src/nodes/loaders/NodeObjectLoader.d.ts | 10 + .../nodes/materials/Line2NodeMaterial.d.ts | 53 + .../materials/LineBasicNodeMaterial.d.ts | 22 + .../src/nodes/materials/Materials.d.ts | 15 + .../materials/MeshBasicNodeMaterial.d.ts | 36 + .../materials/MeshMatcapNodeMaterial.d.ts | 32 + .../materials/MeshNormalNodeMaterial.d.ts | 28 + .../materials/MeshPhongNodeMaterial.d.ts | 56 + .../materials/MeshPhysicalNodeMaterial.d.ts | 90 ++ .../nodes/materials/MeshSSSNodeMaterial.d.ts | 16 + .../materials/MeshStandardNodeMaterial.d.ts | 56 + .../nodes/materials/MeshToonNodeMaterial.d.ts | 42 + .../src/nodes/materials/NodeMaterial.ts | 554 +++++++ .../nodes/materials/PointsNodeMaterial.d.ts | 22 + .../nodes/materials/ShadowNodeMaterial.d.ts | 17 + .../nodes/materials/SpriteNodeMaterial.d.ts | 26 + .../nodes/materials/VolumeNodeMaterial.d.ts | 10 + .../src/nodes/materialx/MaterialXNodes.d.ts | 107 ++ .../src/nodes/materialx/lib/mx_hsv.d.ts | 6 + .../src/nodes/materialx/lib/mx_noise.d.ts | 359 +++++ .../materialx/lib/mx_transform_color.d.ts | 4 + src-testing/src/nodes/math/CondNode.d.ts | 37 + src-testing/src/nodes/math/HashNode.d.ts | 16 + src-testing/src/nodes/math/MathNode.d.ts | 273 ++++ src-testing/src/nodes/math/MathUtils.d.ts | 16 + src-testing/src/nodes/math/OperatorNode.d.ts | 83 + src-testing/src/nodes/math/TriNoise3D.d.ts | 12 + src-testing/src/nodes/pmrem/PMREMNode.d.ts | 20 + .../src/nodes/procedural/CheckerNode.d.ts | 15 + .../src/nodes/shadernode/ShaderNode.ts | 538 +++++++ .../src/nodes/utils/ArrayElementNode.d.ts | 9 + src-testing/src/nodes/utils/ConvertNode.d.ts | 7 + src-testing/src/nodes/utils/DiscardNode.d.ts | 17 + .../src/nodes/utils/EquirectUVNode.d.ts | 8 + src-testing/src/nodes/utils/JoinNode.d.ts | 10 + src-testing/src/nodes/utils/LoopNode.d.ts | 22 + src-testing/src/nodes/utils/MatcapUVNode.d.ts | 8 + .../src/nodes/utils/MaxMipLevelNode.d.ts | 14 + src-testing/src/nodes/utils/OscNode.d.ts | 25 + src-testing/src/nodes/utils/PackingNode.d.ts | 24 + src-testing/src/nodes/utils/RTTNode.d.ts | 45 + .../src/nodes/utils/ReflectorNode.d.ts | 32 + src-testing/src/nodes/utils/RemapNode.d.ts | 36 + src-testing/src/nodes/utils/RotateNode.d.ts | 21 + src-testing/src/nodes/utils/SplitNode.d.ts | 15 + .../src/nodes/utils/SpriteSheetUVNode.d.ts | 16 + src-testing/src/nodes/utils/SpriteUtils.d.ts | 6 + .../nodes/utils/StoargeArrayElementNode.d.ts | 25 + src-testing/src/nodes/utils/TimerNode.d.ts | 25 + .../nodes/utils/TriplanarTexturesNode.d.ts | 42 + src-testing/src/nodes/utils/UVUtils.d.ts | 21 + .../src/nodes/utils/ViewportUtils.d.ts | 4 + src-testing/src/objects/BatchedMesh.d.ts | 197 +++ src-testing/src/objects/Bone.d.ts | 36 + src-testing/src/objects/Group.d.ts | 43 + src-testing/src/objects/InstancedMesh.d.ts | 179 +++ src-testing/src/objects/LOD.d.ts | 104 ++ src-testing/src/objects/Line.d.ts | 87 + src-testing/src/objects/LineLoop.d.ts | 40 + src-testing/src/objects/LineSegments.d.ts | 41 + src-testing/src/objects/Mesh.d.ts | 94 ++ src-testing/src/objects/Points.d.ts | 66 + src-testing/src/objects/Skeleton.d.ts | 120 ++ src-testing/src/objects/SkinnedMesh.d.ts | 160 ++ src-testing/src/objects/Sprite.d.ts | 65 + .../src/renderers/WebGL3DRenderTarget.d.ts | 29 + .../src/renderers/WebGLArrayRenderTarget.d.ts | 29 + .../src/renderers/WebGLCubeRenderTarget.d.ts | 18 + .../src/renderers/WebGLRenderTarget.d.ts | 8 + src-testing/src/renderers/WebGLRenderer.d.ts | 562 +++++++ src-testing/src/renderers/common/Animation.ts | 38 + .../src/renderers/common/Attributes.ts | 54 + src-testing/src/renderers/common/Backend.ts | 170 ++ .../src/renderers/common/Background.ts | 125 ++ src-testing/src/renderers/common/BindGroup.ts | 13 + src-testing/src/renderers/common/Binding.ts | 17 + src-testing/src/renderers/common/Bindings.ts | 161 ++ src-testing/src/renderers/common/Buffer.ts | 28 + .../src/renderers/common/BufferUtils.ts | 23 + src-testing/src/renderers/common/ChainMap.ts | 43 + .../src/renderers/common/ClippingContext.ts | 130 ++ src-testing/src/renderers/common/Color4.ts | 27 + .../src/renderers/common/ComputePipeline.ts | 13 + src-testing/src/renderers/common/Constants.ts | 14 + .../src/renderers/common/CubeRenderTarget.ts | 65 + src-testing/src/renderers/common/DataMap.ts | 38 + .../src/renderers/common/Geometries.ts | 183 +++ src-testing/src/renderers/common/Info.ts | 95 ++ src-testing/src/renderers/common/Pipeline.ts | 9 + src-testing/src/renderers/common/Pipelines.ts | 270 ++++ .../src/renderers/common/PostProcessing.d.ts | 21 + .../src/renderers/common/ProgrammableStage.ts | 16 + .../src/renderers/common/QuadMesh.d.ts | 14 + .../src/renderers/common/RenderBundle.ts | 12 + .../src/renderers/common/RenderBundles.ts | 28 + .../src/renderers/common/RenderContext.ts | 57 + .../src/renderers/common/RenderContexts.ts | 47 + .../src/renderers/common/RenderList.ts | 145 ++ .../src/renderers/common/RenderLists.ts | 28 + .../src/renderers/common/RenderObject.ts | 231 +++ .../src/renderers/common/RenderObjects.ts | 100 ++ .../src/renderers/common/RenderPipeline.ts | 12 + src-testing/src/renderers/common/Renderer.ts | 1399 +++++++++++++++++ .../src/renderers/common/SampledTexture.ts | 61 + src-testing/src/renderers/common/Sampler.ts | 14 + .../src/renderers/common/StorageBuffer.ts | 13 + .../common/StorageBufferAttribute.d.ts | 7 + .../StorageInstancedBufferAttribute.d.ts | 8 + .../src/renderers/common/StorageTexture.d.ts | 5 + src-testing/src/renderers/common/Textures.ts | 290 ++++ src-testing/src/renderers/common/Uniform.ts | 105 ++ .../src/renderers/common/UniformBuffer.ts | 11 + .../src/renderers/common/UniformsGroup.ts | 277 ++++ .../renderers/common/extras/PMREMGenerator.ts | 660 ++++++++ .../common/nodes/NodeBuilderState.ts | 55 + .../common/nodes/NodeSampledTexture.d.ts | 29 + .../renderers/common/nodes/NodeSampler.d.ts | 12 + .../src/renderers/common/nodes/NodeUniform.ts | 103 ++ .../common/nodes/NodeUniformsGroup.ts | 30 + .../src/renderers/common/nodes/Nodes.ts | 399 +++++ .../src/renderers/shaders/ShaderChunk.d.ts | 143 ++ .../src/renderers/shaders/ShaderLib.d.ts | 29 + .../src/renderers/shaders/UniformsLib.d.ts | 189 +++ .../src/renderers/shaders/UniformsUtils.d.ts | 14 + .../renderers/webgl-fallback/WebGLBackend.ts | 1280 +++++++++++++++ .../webgl-fallback/nodes/GLSLNodeBuilder.ts | 794 ++++++++++ .../src/renderers/webgl/WebGLAttributes.d.ts | 21 + .../renderers/webgl/WebGLBindingStates.d.ts | 26 + .../renderers/webgl/WebGLBufferRenderer.d.ts | 21 + .../renderers/webgl/WebGLCapabilities.d.ts | 35 + .../src/renderers/webgl/WebGLClipping.d.ts | 26 + .../src/renderers/webgl/WebGLCubeMaps.d.ts | 8 + .../src/renderers/webgl/WebGLCubeUVMaps.d.ts | 9 + .../src/renderers/webgl/WebGLExtensions.d.ts | 7 + .../src/renderers/webgl/WebGLGeometries.d.ts | 13 + .../webgl/WebGLIndexedBufferRenderer.d.ts | 15 + .../src/renderers/webgl/WebGLInfo.d.ts | 39 + .../src/renderers/webgl/WebGLLights.d.ts | 49 + .../src/renderers/webgl/WebGLObjects.d.ts | 6 + .../src/renderers/webgl/WebGLProgram.d.ts | 30 + .../src/renderers/webgl/WebGLPrograms.d.ts | 239 +++ .../src/renderers/webgl/WebGLProperties.d.ts | 9 + .../src/renderers/webgl/WebGLRenderLists.d.ts | 66 + .../src/renderers/webgl/WebGLShader.d.ts | 1 + .../src/renderers/webgl/WebGLShadowMap.d.ts | 38 + .../src/renderers/webgl/WebGLState.d.ts | 116 ++ .../src/renderers/webgl/WebGLTextures.d.ts | 30 + .../src/renderers/webgl/WebGLUniforms.d.ts | 12 + .../renderers/webgl/WebGLUniformsGroups.d.ts | 17 + .../src/renderers/webgl/WebGLUtils.d.ts | 11 + .../src/renderers/webgpu/WebGPUBackend.ts | 1249 +++++++++++++++ .../src/renderers/webgpu/WebGPURenderer.ts | 43 + .../renderers/webgpu/nodes/WGSLNodeBuilder.ts | 1114 +++++++++++++ .../webgpu/nodes/WGSLNodeFunction.ts | 140 ++ .../renderers/webgpu/nodes/WGSLNodeParser.ts | 10 + .../webgpu/utils/WebGPUConstants.d.ts | 328 ++++ .../src/renderers/webxr/WebXRController.d.ts | 63 + .../renderers/webxr/WebXRDepthSensing.d.ts | 22 + .../src/renderers/webxr/WebXRManager.d.ts | 85 + src-testing/src/scenes/Fog.d.ts | 77 + src-testing/src/scenes/FogExp2.d.ts | 66 + src-testing/src/scenes/Scene.d.ts | 118 ++ src-testing/src/textures/CanvasTexture.d.ts | 51 + .../src/textures/CompressedArrayTexture.d.ts | 68 + .../src/textures/CompressedCubeTexture.d.ts | 13 + .../src/textures/CompressedTexture.d.ts | 95 ++ src-testing/src/textures/CubeTexture.d.ts | 90 ++ src-testing/src/textures/Data3DTexture.d.ts | 96 ++ .../src/textures/DataArrayTexture.d.ts | 123 ++ src-testing/src/textures/DataTexture.d.ts | 113 ++ src-testing/src/textures/DepthTexture.d.ts | 104 ++ .../src/textures/FramebufferTexture.d.ts | 62 + src-testing/src/textures/Source.d.ts | 75 + src-testing/src/textures/Texture.d.ts | 477 ++++++ src-testing/src/textures/VideoTexture.d.ts | 90 ++ src-testing/src/textures/types.d.ts | 9 + src-testing/src/utils.d.ts | 3 + 496 files changed, 41012 insertions(+) create mode 100644 src-testing/src/Three.Legacy.d.ts create mode 100644 src-testing/src/Three.WebGPU.d.ts create mode 100644 src-testing/src/Three.d.ts create mode 100644 src-testing/src/animation/AnimationAction.d.ts create mode 100644 src-testing/src/animation/AnimationClip.d.ts create mode 100644 src-testing/src/animation/AnimationMixer.d.ts create mode 100644 src-testing/src/animation/AnimationObjectGroup.d.ts create mode 100644 src-testing/src/animation/AnimationUtils.d.ts create mode 100644 src-testing/src/animation/KeyframeTrack.d.ts create mode 100644 src-testing/src/animation/PropertyBinding.d.ts create mode 100644 src-testing/src/animation/PropertyMixer.d.ts create mode 100644 src-testing/src/animation/tracks/BooleanKeyframeTrack.d.ts create mode 100644 src-testing/src/animation/tracks/ColorKeyframeTrack.d.ts create mode 100644 src-testing/src/animation/tracks/NumberKeyframeTrack.d.ts create mode 100644 src-testing/src/animation/tracks/QuaternionKeyframeTrack.d.ts create mode 100644 src-testing/src/animation/tracks/StringKeyframeTrack.d.ts create mode 100644 src-testing/src/animation/tracks/VectorKeyframeTrack.d.ts create mode 100644 src-testing/src/audio/Audio.d.ts create mode 100644 src-testing/src/audio/AudioAnalyser.d.ts create mode 100644 src-testing/src/audio/AudioContext.d.ts create mode 100644 src-testing/src/audio/AudioListener.d.ts create mode 100644 src-testing/src/audio/PositionalAudio.d.ts create mode 100644 src-testing/src/cameras/ArrayCamera.d.ts create mode 100644 src-testing/src/cameras/Camera.d.ts create mode 100644 src-testing/src/cameras/CubeCamera.d.ts create mode 100644 src-testing/src/cameras/OrthographicCamera.d.ts create mode 100644 src-testing/src/cameras/PerspectiveCamera.d.ts create mode 100644 src-testing/src/cameras/StereoCamera.d.ts create mode 100644 src-testing/src/constants.d.ts create mode 100644 src-testing/src/core/BufferAttribute.d.ts create mode 100644 src-testing/src/core/BufferGeometry.d.ts create mode 100644 src-testing/src/core/Clock.d.ts create mode 100644 src-testing/src/core/EventDispatcher.d.ts create mode 100644 src-testing/src/core/GLBufferAttribute.d.ts create mode 100644 src-testing/src/core/InstancedBufferAttribute.d.ts create mode 100644 src-testing/src/core/InstancedBufferGeometry.d.ts create mode 100644 src-testing/src/core/InstancedInterleavedBuffer.d.ts create mode 100644 src-testing/src/core/InterleavedBuffer.d.ts create mode 100644 src-testing/src/core/InterleavedBufferAttribute.d.ts create mode 100644 src-testing/src/core/Layers.d.ts create mode 100644 src-testing/src/core/Object3D.d.ts create mode 100644 src-testing/src/core/Raycaster.d.ts create mode 100644 src-testing/src/core/RenderTarget.d.ts create mode 100644 src-testing/src/core/Uniform.d.ts create mode 100644 src-testing/src/core/UniformsGroup.d.ts create mode 100644 src-testing/src/extras/DataUtils.d.ts create mode 100644 src-testing/src/extras/Earcut.d.ts create mode 100644 src-testing/src/extras/ImageUtils.d.ts create mode 100644 src-testing/src/extras/PMREMGenerator.d.ts create mode 100644 src-testing/src/extras/ShapeUtils.d.ts create mode 100644 src-testing/src/extras/TextureUtils.d.ts create mode 100644 src-testing/src/extras/core/Curve.d.ts create mode 100644 src-testing/src/extras/core/CurvePath.d.ts create mode 100644 src-testing/src/extras/core/Interpolations.d.ts create mode 100644 src-testing/src/extras/core/Path.d.ts create mode 100644 src-testing/src/extras/core/Shape.d.ts create mode 100644 src-testing/src/extras/core/ShapePath.d.ts create mode 100644 src-testing/src/extras/curves/ArcCurve.d.ts create mode 100644 src-testing/src/extras/curves/CatmullRomCurve3.d.ts create mode 100644 src-testing/src/extras/curves/CubicBezierCurve.d.ts create mode 100644 src-testing/src/extras/curves/CubicBezierCurve3.d.ts create mode 100644 src-testing/src/extras/curves/Curves.d.ts create mode 100644 src-testing/src/extras/curves/EllipseCurve.d.ts create mode 100644 src-testing/src/extras/curves/LineCurve.d.ts create mode 100644 src-testing/src/extras/curves/LineCurve3.d.ts create mode 100644 src-testing/src/extras/curves/QuadraticBezierCurve.d.ts create mode 100644 src-testing/src/extras/curves/QuadraticBezierCurve3.d.ts create mode 100644 src-testing/src/extras/curves/SplineCurve.d.ts create mode 100644 src-testing/src/geometries/BoxGeometry.d.ts create mode 100644 src-testing/src/geometries/CapsuleGeometry.d.ts create mode 100644 src-testing/src/geometries/CircleGeometry.d.ts create mode 100644 src-testing/src/geometries/ConeGeometry.d.ts create mode 100644 src-testing/src/geometries/CylinderGeometry.d.ts create mode 100644 src-testing/src/geometries/DodecahedronGeometry.d.ts create mode 100644 src-testing/src/geometries/EdgesGeometry.d.ts create mode 100644 src-testing/src/geometries/ExtrudeGeometry.d.ts create mode 100644 src-testing/src/geometries/Geometries.d.ts create mode 100644 src-testing/src/geometries/IcosahedronGeometry.d.ts create mode 100644 src-testing/src/geometries/LatheGeometry.d.ts create mode 100644 src-testing/src/geometries/OctahedronGeometry.d.ts create mode 100644 src-testing/src/geometries/PlaneGeometry.d.ts create mode 100644 src-testing/src/geometries/PolyhedronGeometry.d.ts create mode 100644 src-testing/src/geometries/RingGeometry.d.ts create mode 100644 src-testing/src/geometries/ShapeGeometry.d.ts create mode 100644 src-testing/src/geometries/SphereGeometry.d.ts create mode 100644 src-testing/src/geometries/TetrahedronGeometry.d.ts create mode 100644 src-testing/src/geometries/TorusGeometry.d.ts create mode 100644 src-testing/src/geometries/TorusKnotGeometry.d.ts create mode 100644 src-testing/src/geometries/TubeGeometry.d.ts create mode 100644 src-testing/src/geometries/WireframeGeometry.d.ts create mode 100644 src-testing/src/helpers/ArrowHelper.d.ts create mode 100644 src-testing/src/helpers/AxesHelper.d.ts create mode 100644 src-testing/src/helpers/Box3Helper.d.ts create mode 100644 src-testing/src/helpers/BoxHelper.d.ts create mode 100644 src-testing/src/helpers/CameraHelper.d.ts create mode 100644 src-testing/src/helpers/DirectionalLightHelper.d.ts create mode 100644 src-testing/src/helpers/GridHelper.d.ts create mode 100644 src-testing/src/helpers/HemisphereLightHelper.d.ts create mode 100644 src-testing/src/helpers/PlaneHelper.d.ts create mode 100644 src-testing/src/helpers/PointLightHelper.d.ts create mode 100644 src-testing/src/helpers/PolarGridHelper.d.ts create mode 100644 src-testing/src/helpers/SkeletonHelper.d.ts create mode 100644 src-testing/src/helpers/SpotLightHelper.d.ts create mode 100644 src-testing/src/lights/AmbientLight.d.ts create mode 100644 src-testing/src/lights/DirectionalLight.d.ts create mode 100644 src-testing/src/lights/DirectionalLightShadow.d.ts create mode 100644 src-testing/src/lights/HemisphereLight.d.ts create mode 100644 src-testing/src/lights/Light.d.ts create mode 100644 src-testing/src/lights/LightProbe.d.ts create mode 100644 src-testing/src/lights/LightShadow.d.ts create mode 100644 src-testing/src/lights/PointLight.d.ts create mode 100644 src-testing/src/lights/PointLightShadow.d.ts create mode 100644 src-testing/src/lights/RectAreaLight.d.ts create mode 100644 src-testing/src/lights/SpotLight.d.ts create mode 100644 src-testing/src/lights/SpotLightShadow.d.ts create mode 100644 src-testing/src/lights/webgpu/IESSpotLight.d.ts create mode 100644 src-testing/src/loaders/AnimationLoader.d.ts create mode 100644 src-testing/src/loaders/AudioLoader.d.ts create mode 100644 src-testing/src/loaders/BufferGeometryLoader.d.ts create mode 100644 src-testing/src/loaders/Cache.d.ts create mode 100644 src-testing/src/loaders/CompressedTextureLoader.d.ts create mode 100644 src-testing/src/loaders/CubeTextureLoader.d.ts create mode 100644 src-testing/src/loaders/DataTextureLoader.d.ts create mode 100644 src-testing/src/loaders/FileLoader.d.ts create mode 100644 src-testing/src/loaders/ImageBitmapLoader.d.ts create mode 100644 src-testing/src/loaders/ImageLoader.d.ts create mode 100644 src-testing/src/loaders/Loader.d.ts create mode 100644 src-testing/src/loaders/LoaderUtils.d.ts create mode 100644 src-testing/src/loaders/LoadingManager.d.ts create mode 100644 src-testing/src/loaders/MaterialLoader.d.ts create mode 100644 src-testing/src/loaders/ObjectLoader.d.ts create mode 100644 src-testing/src/loaders/TextureLoader.d.ts create mode 100644 src-testing/src/materials/LineBasicMaterial.d.ts create mode 100644 src-testing/src/materials/LineDashedMaterial.d.ts create mode 100644 src-testing/src/materials/Material.d.ts create mode 100644 src-testing/src/materials/Materials.d.ts create mode 100644 src-testing/src/materials/MeshBasicMaterial.d.ts create mode 100644 src-testing/src/materials/MeshDepthMaterial.d.ts create mode 100644 src-testing/src/materials/MeshDistanceMaterial.d.ts create mode 100644 src-testing/src/materials/MeshLambertMaterial.d.ts create mode 100644 src-testing/src/materials/MeshMatcapMaterial.d.ts create mode 100644 src-testing/src/materials/MeshNormalMaterial.d.ts create mode 100644 src-testing/src/materials/MeshPhongMaterial.d.ts create mode 100644 src-testing/src/materials/MeshPhysicalMaterial.d.ts create mode 100644 src-testing/src/materials/MeshStandardMaterial.d.ts create mode 100644 src-testing/src/materials/MeshToonMaterial.d.ts create mode 100644 src-testing/src/materials/PointsMaterial.d.ts create mode 100644 src-testing/src/materials/RawShaderMaterial.d.ts create mode 100644 src-testing/src/materials/ShaderMaterial.d.ts create mode 100644 src-testing/src/materials/ShadowMaterial.d.ts create mode 100644 src-testing/src/materials/SpriteMaterial.d.ts create mode 100644 src-testing/src/math/Box2.d.ts create mode 100644 src-testing/src/math/Box3.d.ts create mode 100644 src-testing/src/math/Color.d.ts create mode 100644 src-testing/src/math/ColorManagement.d.ts create mode 100644 src-testing/src/math/Cylindrical.d.ts create mode 100644 src-testing/src/math/Euler.d.ts create mode 100644 src-testing/src/math/Frustum.d.ts create mode 100644 src-testing/src/math/Interpolant.d.ts create mode 100644 src-testing/src/math/Line3.d.ts create mode 100644 src-testing/src/math/MathUtils.d.ts create mode 100644 src-testing/src/math/Matrix2.d.ts create mode 100644 src-testing/src/math/Matrix3.d.ts create mode 100644 src-testing/src/math/Matrix4.d.ts create mode 100644 src-testing/src/math/Plane.d.ts create mode 100644 src-testing/src/math/Quaternion.d.ts create mode 100644 src-testing/src/math/Ray.d.ts create mode 100644 src-testing/src/math/Sphere.d.ts create mode 100644 src-testing/src/math/Spherical.d.ts create mode 100644 src-testing/src/math/SphericalHarmonics3.d.ts create mode 100644 src-testing/src/math/Triangle.d.ts create mode 100644 src-testing/src/math/Vector2.d.ts create mode 100644 src-testing/src/math/Vector3.d.ts create mode 100644 src-testing/src/math/Vector4.d.ts create mode 100644 src-testing/src/math/interpolants/CubicInterpolant.d.ts create mode 100644 src-testing/src/math/interpolants/DiscreteInterpolant.d.ts create mode 100644 src-testing/src/math/interpolants/LinearInterpolant.d.ts create mode 100644 src-testing/src/math/interpolants/QuaternionLinearInterpolant.d.ts create mode 100644 src-testing/src/nodes/Nodes.ts create mode 100644 src-testing/src/nodes/accessors/AccessorsUtils.d.ts create mode 100644 src-testing/src/nodes/accessors/BatchNode.d.ts create mode 100644 src-testing/src/nodes/accessors/BitangentNode.d.ts create mode 100644 src-testing/src/nodes/accessors/BufferAttributeNode.ts create mode 100644 src-testing/src/nodes/accessors/BufferNode.d.ts create mode 100644 src-testing/src/nodes/accessors/CameraNode.d.ts create mode 100644 src-testing/src/nodes/accessors/ClippingNode.d.ts create mode 100644 src-testing/src/nodes/accessors/CubeTextureNode.d.ts create mode 100644 src-testing/src/nodes/accessors/InstanceNode.d.ts create mode 100644 src-testing/src/nodes/accessors/MaterialNode.d.ts create mode 100644 src-testing/src/nodes/accessors/MaterialProperties.d.ts create mode 100644 src-testing/src/nodes/accessors/MaterialReferenceNode.d.ts create mode 100644 src-testing/src/nodes/accessors/ModelNode.d.ts create mode 100644 src-testing/src/nodes/accessors/ModelViewProjectionNode.d.ts create mode 100644 src-testing/src/nodes/accessors/NormalNode.d.ts create mode 100644 src-testing/src/nodes/accessors/Object3DNode.d.ts create mode 100644 src-testing/src/nodes/accessors/PointUVNode.d.ts create mode 100644 src-testing/src/nodes/accessors/PositionNode.d.ts create mode 100644 src-testing/src/nodes/accessors/ReferenceNode.d.ts create mode 100644 src-testing/src/nodes/accessors/ReflectVectorNode.d.ts create mode 100644 src-testing/src/nodes/accessors/RendererReferenceNode.d.ts create mode 100644 src-testing/src/nodes/accessors/SkinningNode.d.ts create mode 100644 src-testing/src/nodes/accessors/StorageBufferNode.d.ts create mode 100644 src-testing/src/nodes/accessors/StorageTextureNode.d.ts create mode 100644 src-testing/src/nodes/accessors/TangentNode.d.ts create mode 100644 src-testing/src/nodes/accessors/Texture3DNode.d.ts create mode 100644 src-testing/src/nodes/accessors/TextureBicubicNode.d.ts create mode 100644 src-testing/src/nodes/accessors/TextureNode.ts create mode 100644 src-testing/src/nodes/accessors/UVNode.d.ts create mode 100644 src-testing/src/nodes/accessors/UniformArrayNode.d.ts create mode 100644 src-testing/src/nodes/accessors/UserDataNode.d.ts create mode 100644 src-testing/src/nodes/accessors/VertexColorNode.d.ts create mode 100644 src-testing/src/nodes/code/CodeNode.ts create mode 100644 src-testing/src/nodes/code/ExpressionNode.d.ts create mode 100644 src-testing/src/nodes/code/FunctionCallNode.d.ts create mode 100644 src-testing/src/nodes/code/FunctionNode.ts create mode 100644 src-testing/src/nodes/core/AssignNode.d.ts create mode 100644 src-testing/src/nodes/core/AttributeNode.d.ts create mode 100644 src-testing/src/nodes/core/BypassNode.d.ts create mode 100644 src-testing/src/nodes/core/CacheNode.d.ts create mode 100644 src-testing/src/nodes/core/ConstNode.d.ts create mode 100644 src-testing/src/nodes/core/ContextNode.ts create mode 100644 src-testing/src/nodes/core/IndexNode.d.ts create mode 100644 src-testing/src/nodes/core/InputNode.ts create mode 100644 src-testing/src/nodes/core/LightingModel.d.ts create mode 100644 src-testing/src/nodes/core/MRTNode.d.ts create mode 100644 src-testing/src/nodes/core/Node.ts create mode 100644 src-testing/src/nodes/core/NodeAttribute.ts create mode 100644 src-testing/src/nodes/core/NodeBuilder.ts create mode 100644 src-testing/src/nodes/core/NodeCache.ts create mode 100644 src-testing/src/nodes/core/NodeCode.ts create mode 100644 src-testing/src/nodes/core/NodeFrame.ts create mode 100644 src-testing/src/nodes/core/NodeFunction.ts create mode 100644 src-testing/src/nodes/core/NodeFunctionInput.d.ts create mode 100644 src-testing/src/nodes/core/NodeKeywords.ts create mode 100644 src-testing/src/nodes/core/NodeParser.ts create mode 100644 src-testing/src/nodes/core/NodeUniform.ts create mode 100644 src-testing/src/nodes/core/NodeUtils.ts create mode 100644 src-testing/src/nodes/core/NodeVar.ts create mode 100644 src-testing/src/nodes/core/NodeVarying.ts create mode 100644 src-testing/src/nodes/core/OutputStructNode.d.ts create mode 100644 src-testing/src/nodes/core/PropertyNode.d.ts create mode 100644 src-testing/src/nodes/core/StackNode.ts create mode 100644 src-testing/src/nodes/core/StructTypeNode.ts create mode 100644 src-testing/src/nodes/core/TempNode.d.ts create mode 100644 src-testing/src/nodes/core/UniformGroup.d.ts create mode 100644 src-testing/src/nodes/core/UniformGroupNode.ts create mode 100644 src-testing/src/nodes/core/UniformNode.ts create mode 100644 src-testing/src/nodes/core/VarNode.d.ts create mode 100644 src-testing/src/nodes/core/VaryingNode.d.ts create mode 100644 src-testing/src/nodes/core/constants.ts create mode 100644 src-testing/src/nodes/display/AfterImageNode.d.ts create mode 100644 src-testing/src/nodes/display/AnamorphicNode.d.ts create mode 100644 src-testing/src/nodes/display/BleachBypassNode.d.ts create mode 100644 src-testing/src/nodes/display/BlendModeNode.d.ts create mode 100644 src-testing/src/nodes/display/BloomNode.d.ts create mode 100644 src-testing/src/nodes/display/ColorAdjustmentNode.d.ts create mode 100644 src-testing/src/nodes/display/ColorSpaceNode.d.ts create mode 100644 src-testing/src/nodes/display/DenoiseNode.d.ts create mode 100644 src-testing/src/nodes/display/DepthOfFieldNode.d.ts create mode 100644 src-testing/src/nodes/display/DotScreenNode.d.ts create mode 100644 src-testing/src/nodes/display/FXAANode.d.ts create mode 100644 src-testing/src/nodes/display/FilmNode.d.ts create mode 100644 src-testing/src/nodes/display/FrontFacingNode.d.ts create mode 100644 src-testing/src/nodes/display/GTAONode.d.ts create mode 100644 src-testing/src/nodes/display/GaussianBlurNode.d.ts create mode 100644 src-testing/src/nodes/display/Lut3DNode.d.ts create mode 100644 src-testing/src/nodes/display/NormalMapNode.d.ts create mode 100644 src-testing/src/nodes/display/PassNode.d.ts create mode 100644 src-testing/src/nodes/display/PixelationPassNode.d.ts create mode 100644 src-testing/src/nodes/display/PosterizeNode.d.ts create mode 100644 src-testing/src/nodes/display/RGBShiftNode.d.ts create mode 100644 src-testing/src/nodes/display/RenderOutputNode.d.ts create mode 100644 src-testing/src/nodes/display/SepiaNode.d.ts create mode 100644 src-testing/src/nodes/display/SobelOperatorNode.d.ts create mode 100644 src-testing/src/nodes/display/ToneMappingNode.d.ts create mode 100644 src-testing/src/nodes/display/TransitionNode.d.ts create mode 100644 src-testing/src/nodes/display/ViewportDepthNode.d.ts create mode 100644 src-testing/src/nodes/display/ViewportDepthTextureNode.d.ts create mode 100644 src-testing/src/nodes/display/ViewportNode.d.ts create mode 100644 src-testing/src/nodes/display/ViewportSharedTextureNode.d.ts create mode 100644 src-testing/src/nodes/display/ViewportTextureNode.d.ts create mode 100644 src-testing/src/nodes/fog/FogExp2Node.d.ts create mode 100644 src-testing/src/nodes/fog/FogNode.ts create mode 100644 src-testing/src/nodes/fog/FogRangeNode.d.ts create mode 100644 src-testing/src/nodes/functions/BSDF/BRDF_GGX.d.ts create mode 100644 src-testing/src/nodes/functions/BSDF/BRDF_Lambert.d.ts create mode 100644 src-testing/src/nodes/functions/BSDF/BRDF_Sheen.d.ts create mode 100644 src-testing/src/nodes/functions/BSDF/DFGApprox.d.ts create mode 100644 src-testing/src/nodes/functions/BSDF/D_GGX.d.ts create mode 100644 src-testing/src/nodes/functions/BSDF/D_GGX_Anisotropic.d.ts create mode 100644 src-testing/src/nodes/functions/BSDF/F_Schlick.d.ts create mode 100644 src-testing/src/nodes/functions/BSDF/LTC.d.ts create mode 100644 src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated.d.ts create mode 100644 src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated_Anisotropic.d.ts create mode 100644 src-testing/src/nodes/functions/BasicLightingModel.d.ts create mode 100644 src-testing/src/nodes/functions/PhongLightingModel.d.ts create mode 100644 src-testing/src/nodes/functions/PhysicalLightingModel.d.ts create mode 100644 src-testing/src/nodes/functions/ShadowMaskModel.d.ts create mode 100644 src-testing/src/nodes/functions/ToonLightingModel.d.ts create mode 100644 src-testing/src/nodes/functions/material/getGeometryRoughness.d.ts create mode 100644 src-testing/src/nodes/functions/material/getRoughness.d.ts create mode 100644 src-testing/src/nodes/geometry/RangeNode.d.ts create mode 100644 src-testing/src/nodes/gpgpu/ComputeNode.ts create mode 100644 src-testing/src/nodes/lighting/AONode.d.ts create mode 100644 src-testing/src/nodes/lighting/AnalyticLightNode.d.ts create mode 100644 src-testing/src/nodes/lighting/BasicEnvironmentNode.d.ts create mode 100644 src-testing/src/nodes/lighting/BasicLightMapNode.d.ts create mode 100644 src-testing/src/nodes/lighting/EnvironmentNode.ts create mode 100644 src-testing/src/nodes/lighting/HemisphereLightNode.d.ts create mode 100644 src-testing/src/nodes/lighting/IrradianceNode.d.ts create mode 100644 src-testing/src/nodes/lighting/LightUtils.d.ts create mode 100644 src-testing/src/nodes/lighting/LightingContextNode.ts create mode 100644 src-testing/src/nodes/lighting/LightingNode.d.ts create mode 100644 src-testing/src/nodes/lighting/LightsNode.ts create mode 100644 src-testing/src/nodes/lighting/PointLightNode.d.ts create mode 100644 src-testing/src/nodes/lighting/RectAreaLightNode.d.ts create mode 100644 src-testing/src/nodes/lighting/SpotLightNode.d.ts create mode 100644 src-testing/src/nodes/loaders/NodeLoader.d.ts create mode 100644 src-testing/src/nodes/loaders/NodeMaterialLoader.d.ts create mode 100644 src-testing/src/nodes/loaders/NodeObjectLoader.d.ts create mode 100644 src-testing/src/nodes/materials/Line2NodeMaterial.d.ts create mode 100644 src-testing/src/nodes/materials/LineBasicNodeMaterial.d.ts create mode 100644 src-testing/src/nodes/materials/Materials.d.ts create mode 100644 src-testing/src/nodes/materials/MeshBasicNodeMaterial.d.ts create mode 100644 src-testing/src/nodes/materials/MeshMatcapNodeMaterial.d.ts create mode 100644 src-testing/src/nodes/materials/MeshNormalNodeMaterial.d.ts create mode 100644 src-testing/src/nodes/materials/MeshPhongNodeMaterial.d.ts create mode 100644 src-testing/src/nodes/materials/MeshPhysicalNodeMaterial.d.ts create mode 100644 src-testing/src/nodes/materials/MeshSSSNodeMaterial.d.ts create mode 100644 src-testing/src/nodes/materials/MeshStandardNodeMaterial.d.ts create mode 100644 src-testing/src/nodes/materials/MeshToonNodeMaterial.d.ts create mode 100644 src-testing/src/nodes/materials/NodeMaterial.ts create mode 100644 src-testing/src/nodes/materials/PointsNodeMaterial.d.ts create mode 100644 src-testing/src/nodes/materials/ShadowNodeMaterial.d.ts create mode 100644 src-testing/src/nodes/materials/SpriteNodeMaterial.d.ts create mode 100644 src-testing/src/nodes/materials/VolumeNodeMaterial.d.ts create mode 100644 src-testing/src/nodes/materialx/MaterialXNodes.d.ts create mode 100644 src-testing/src/nodes/materialx/lib/mx_hsv.d.ts create mode 100644 src-testing/src/nodes/materialx/lib/mx_noise.d.ts create mode 100644 src-testing/src/nodes/materialx/lib/mx_transform_color.d.ts create mode 100644 src-testing/src/nodes/math/CondNode.d.ts create mode 100644 src-testing/src/nodes/math/HashNode.d.ts create mode 100644 src-testing/src/nodes/math/MathNode.d.ts create mode 100644 src-testing/src/nodes/math/MathUtils.d.ts create mode 100644 src-testing/src/nodes/math/OperatorNode.d.ts create mode 100644 src-testing/src/nodes/math/TriNoise3D.d.ts create mode 100644 src-testing/src/nodes/pmrem/PMREMNode.d.ts create mode 100644 src-testing/src/nodes/procedural/CheckerNode.d.ts create mode 100644 src-testing/src/nodes/shadernode/ShaderNode.ts create mode 100644 src-testing/src/nodes/utils/ArrayElementNode.d.ts create mode 100644 src-testing/src/nodes/utils/ConvertNode.d.ts create mode 100644 src-testing/src/nodes/utils/DiscardNode.d.ts create mode 100644 src-testing/src/nodes/utils/EquirectUVNode.d.ts create mode 100644 src-testing/src/nodes/utils/JoinNode.d.ts create mode 100644 src-testing/src/nodes/utils/LoopNode.d.ts create mode 100644 src-testing/src/nodes/utils/MatcapUVNode.d.ts create mode 100644 src-testing/src/nodes/utils/MaxMipLevelNode.d.ts create mode 100644 src-testing/src/nodes/utils/OscNode.d.ts create mode 100644 src-testing/src/nodes/utils/PackingNode.d.ts create mode 100644 src-testing/src/nodes/utils/RTTNode.d.ts create mode 100644 src-testing/src/nodes/utils/ReflectorNode.d.ts create mode 100644 src-testing/src/nodes/utils/RemapNode.d.ts create mode 100644 src-testing/src/nodes/utils/RotateNode.d.ts create mode 100644 src-testing/src/nodes/utils/SplitNode.d.ts create mode 100644 src-testing/src/nodes/utils/SpriteSheetUVNode.d.ts create mode 100644 src-testing/src/nodes/utils/SpriteUtils.d.ts create mode 100644 src-testing/src/nodes/utils/StoargeArrayElementNode.d.ts create mode 100644 src-testing/src/nodes/utils/TimerNode.d.ts create mode 100644 src-testing/src/nodes/utils/TriplanarTexturesNode.d.ts create mode 100644 src-testing/src/nodes/utils/UVUtils.d.ts create mode 100644 src-testing/src/nodes/utils/ViewportUtils.d.ts create mode 100644 src-testing/src/objects/BatchedMesh.d.ts create mode 100644 src-testing/src/objects/Bone.d.ts create mode 100644 src-testing/src/objects/Group.d.ts create mode 100644 src-testing/src/objects/InstancedMesh.d.ts create mode 100644 src-testing/src/objects/LOD.d.ts create mode 100644 src-testing/src/objects/Line.d.ts create mode 100644 src-testing/src/objects/LineLoop.d.ts create mode 100644 src-testing/src/objects/LineSegments.d.ts create mode 100644 src-testing/src/objects/Mesh.d.ts create mode 100644 src-testing/src/objects/Points.d.ts create mode 100644 src-testing/src/objects/Skeleton.d.ts create mode 100644 src-testing/src/objects/SkinnedMesh.d.ts create mode 100644 src-testing/src/objects/Sprite.d.ts create mode 100644 src-testing/src/renderers/WebGL3DRenderTarget.d.ts create mode 100644 src-testing/src/renderers/WebGLArrayRenderTarget.d.ts create mode 100644 src-testing/src/renderers/WebGLCubeRenderTarget.d.ts create mode 100644 src-testing/src/renderers/WebGLRenderTarget.d.ts create mode 100644 src-testing/src/renderers/WebGLRenderer.d.ts create mode 100644 src-testing/src/renderers/common/Animation.ts create mode 100644 src-testing/src/renderers/common/Attributes.ts create mode 100644 src-testing/src/renderers/common/Backend.ts create mode 100644 src-testing/src/renderers/common/Background.ts create mode 100644 src-testing/src/renderers/common/BindGroup.ts create mode 100644 src-testing/src/renderers/common/Binding.ts create mode 100644 src-testing/src/renderers/common/Bindings.ts create mode 100644 src-testing/src/renderers/common/Buffer.ts create mode 100644 src-testing/src/renderers/common/BufferUtils.ts create mode 100644 src-testing/src/renderers/common/ChainMap.ts create mode 100644 src-testing/src/renderers/common/ClippingContext.ts create mode 100644 src-testing/src/renderers/common/Color4.ts create mode 100644 src-testing/src/renderers/common/ComputePipeline.ts create mode 100644 src-testing/src/renderers/common/Constants.ts create mode 100644 src-testing/src/renderers/common/CubeRenderTarget.ts create mode 100644 src-testing/src/renderers/common/DataMap.ts create mode 100644 src-testing/src/renderers/common/Geometries.ts create mode 100644 src-testing/src/renderers/common/Info.ts create mode 100644 src-testing/src/renderers/common/Pipeline.ts create mode 100644 src-testing/src/renderers/common/Pipelines.ts create mode 100644 src-testing/src/renderers/common/PostProcessing.d.ts create mode 100644 src-testing/src/renderers/common/ProgrammableStage.ts create mode 100644 src-testing/src/renderers/common/QuadMesh.d.ts create mode 100644 src-testing/src/renderers/common/RenderBundle.ts create mode 100644 src-testing/src/renderers/common/RenderBundles.ts create mode 100644 src-testing/src/renderers/common/RenderContext.ts create mode 100644 src-testing/src/renderers/common/RenderContexts.ts create mode 100644 src-testing/src/renderers/common/RenderList.ts create mode 100644 src-testing/src/renderers/common/RenderLists.ts create mode 100644 src-testing/src/renderers/common/RenderObject.ts create mode 100644 src-testing/src/renderers/common/RenderObjects.ts create mode 100644 src-testing/src/renderers/common/RenderPipeline.ts create mode 100644 src-testing/src/renderers/common/Renderer.ts create mode 100644 src-testing/src/renderers/common/SampledTexture.ts create mode 100644 src-testing/src/renderers/common/Sampler.ts create mode 100644 src-testing/src/renderers/common/StorageBuffer.ts create mode 100644 src-testing/src/renderers/common/StorageBufferAttribute.d.ts create mode 100644 src-testing/src/renderers/common/StorageInstancedBufferAttribute.d.ts create mode 100644 src-testing/src/renderers/common/StorageTexture.d.ts create mode 100644 src-testing/src/renderers/common/Textures.ts create mode 100644 src-testing/src/renderers/common/Uniform.ts create mode 100644 src-testing/src/renderers/common/UniformBuffer.ts create mode 100644 src-testing/src/renderers/common/UniformsGroup.ts create mode 100644 src-testing/src/renderers/common/extras/PMREMGenerator.ts create mode 100644 src-testing/src/renderers/common/nodes/NodeBuilderState.ts create mode 100644 src-testing/src/renderers/common/nodes/NodeSampledTexture.d.ts create mode 100644 src-testing/src/renderers/common/nodes/NodeSampler.d.ts create mode 100644 src-testing/src/renderers/common/nodes/NodeUniform.ts create mode 100644 src-testing/src/renderers/common/nodes/NodeUniformsGroup.ts create mode 100644 src-testing/src/renderers/common/nodes/Nodes.ts create mode 100644 src-testing/src/renderers/shaders/ShaderChunk.d.ts create mode 100644 src-testing/src/renderers/shaders/ShaderLib.d.ts create mode 100644 src-testing/src/renderers/shaders/UniformsLib.d.ts create mode 100644 src-testing/src/renderers/shaders/UniformsUtils.d.ts create mode 100644 src-testing/src/renderers/webgl-fallback/WebGLBackend.ts create mode 100644 src-testing/src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.ts create mode 100644 src-testing/src/renderers/webgl/WebGLAttributes.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLBindingStates.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLBufferRenderer.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLCapabilities.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLClipping.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLCubeMaps.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLCubeUVMaps.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLExtensions.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLGeometries.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLIndexedBufferRenderer.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLInfo.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLLights.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLObjects.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLProgram.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLPrograms.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLProperties.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLRenderLists.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLShader.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLShadowMap.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLState.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLTextures.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLUniforms.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLUniformsGroups.d.ts create mode 100644 src-testing/src/renderers/webgl/WebGLUtils.d.ts create mode 100644 src-testing/src/renderers/webgpu/WebGPUBackend.ts create mode 100644 src-testing/src/renderers/webgpu/WebGPURenderer.ts create mode 100644 src-testing/src/renderers/webgpu/nodes/WGSLNodeBuilder.ts create mode 100644 src-testing/src/renderers/webgpu/nodes/WGSLNodeFunction.ts create mode 100644 src-testing/src/renderers/webgpu/nodes/WGSLNodeParser.ts create mode 100644 src-testing/src/renderers/webgpu/utils/WebGPUConstants.d.ts create mode 100644 src-testing/src/renderers/webxr/WebXRController.d.ts create mode 100644 src-testing/src/renderers/webxr/WebXRDepthSensing.d.ts create mode 100644 src-testing/src/renderers/webxr/WebXRManager.d.ts create mode 100644 src-testing/src/scenes/Fog.d.ts create mode 100644 src-testing/src/scenes/FogExp2.d.ts create mode 100644 src-testing/src/scenes/Scene.d.ts create mode 100644 src-testing/src/textures/CanvasTexture.d.ts create mode 100644 src-testing/src/textures/CompressedArrayTexture.d.ts create mode 100644 src-testing/src/textures/CompressedCubeTexture.d.ts create mode 100644 src-testing/src/textures/CompressedTexture.d.ts create mode 100644 src-testing/src/textures/CubeTexture.d.ts create mode 100644 src-testing/src/textures/Data3DTexture.d.ts create mode 100644 src-testing/src/textures/DataArrayTexture.d.ts create mode 100644 src-testing/src/textures/DataTexture.d.ts create mode 100644 src-testing/src/textures/DepthTexture.d.ts create mode 100644 src-testing/src/textures/FramebufferTexture.d.ts create mode 100644 src-testing/src/textures/Source.d.ts create mode 100644 src-testing/src/textures/Texture.d.ts create mode 100644 src-testing/src/textures/VideoTexture.d.ts create mode 100644 src-testing/src/textures/types.d.ts create mode 100644 src-testing/src/utils.d.ts diff --git a/src-testing/src/Three.Legacy.d.ts b/src-testing/src/Three.Legacy.d.ts new file mode 100644 index 000000000..07f4743b9 --- /dev/null +++ b/src-testing/src/Three.Legacy.d.ts @@ -0,0 +1,20 @@ +import { RenderTargetOptions } from "./core/RenderTarget.js"; +import { WebGLRenderTarget } from "./renderers/WebGLRenderTarget.js"; +import { Texture } from "./textures/Texture.js"; + +/** + * @deprecated THREE.WebGLMultipleRenderTargets has been deprecated and will be removed in r172. Use THREE.WebGLRenderTarget and set the "count" parameter to enable MRT. + */ +export class WebGLMultipleRenderTargets extends WebGLRenderTarget { + readonly isWebGLMultipleRenderTargets: true; + + /** + * @deprecated THREE.WebGLMultipleRenderTargets has been deprecated and will be removed in r172. Use THREE.WebGLRenderTarget and set the "count" parameter to enable MRT. + * @param width The width of the render target. + * @param height The height of the render target. + * @param count The number of render targets. + * @param options object that holds texture parameters for an auto-generated target texture and depthBuffer/stencilBuffer booleans. + * For an explanation of the texture parameters see {@link Texture}. + */ + constructor(width?: number, height?: number, count?: number, options?: RenderTargetOptions); +} diff --git a/src-testing/src/Three.WebGPU.d.ts b/src-testing/src/Three.WebGPU.d.ts new file mode 100644 index 000000000..708281771 --- /dev/null +++ b/src-testing/src/Three.WebGPU.d.ts @@ -0,0 +1,195 @@ +export * from "./animation/AnimationAction.js"; +export * from "./animation/AnimationClip.js"; +export * from "./animation/AnimationMixer.js"; +export * from "./animation/AnimationObjectGroup.js"; +export { AnimationUtils } from "./animation/AnimationUtils.js"; +export * from "./animation/KeyframeTrack.js"; +export * from "./animation/PropertyBinding.js"; +export * from "./animation/PropertyMixer.js"; +export * from "./animation/tracks/BooleanKeyframeTrack.js"; +export * from "./animation/tracks/ColorKeyframeTrack.js"; +export * from "./animation/tracks/NumberKeyframeTrack.js"; +export * from "./animation/tracks/QuaternionKeyframeTrack.js"; +export * from "./animation/tracks/StringKeyframeTrack.js"; +export * from "./animation/tracks/VectorKeyframeTrack.js"; +export * from "./audio/Audio.js"; +export * from "./audio/AudioAnalyser.js"; +export * from "./audio/AudioContext.js"; +export * from "./audio/AudioListener.js"; +export * from "./audio/PositionalAudio.js"; +export * from "./cameras/ArrayCamera.js"; +export * from "./cameras/Camera.js"; +export * from "./cameras/CubeCamera.js"; +export * from "./cameras/OrthographicCamera.js"; +export * from "./cameras/PerspectiveCamera.js"; +export * from "./cameras/StereoCamera.js"; +export * from "./constants.js"; +export * from "./core/BufferAttribute.js"; +export * from "./core/BufferGeometry.js"; +export * from "./core/Clock.js"; +export * from "./core/EventDispatcher.js"; +export * from "./core/GLBufferAttribute.js"; +export * from "./core/InstancedBufferAttribute.js"; +export * from "./core/InstancedBufferGeometry.js"; +export * from "./core/InstancedInterleavedBuffer.js"; +export * from "./core/InterleavedBuffer.js"; +export * from "./core/InterleavedBufferAttribute.js"; +export * from "./core/Layers.js"; +export * from "./core/Object3D.js"; +export * from "./core/Raycaster.js"; +export * from "./core/RenderTarget.js"; +export * from "./core/Uniform.js"; +export * from "./core/UniformsGroup.js"; +export * from "./extras/core/Curve.js"; +export * from "./extras/core/CurvePath.js"; +export * from "./extras/core/Path.js"; +export * from "./extras/core/Shape.js"; +export * from "./extras/core/ShapePath.js"; +export * from "./extras/curves/Curves.js"; +export { DataUtils } from "./extras/DataUtils.js"; +export * from "./extras/ImageUtils.js"; +// export * from "./extras/PMREMGenerator.js"; +export * from "./extras/ShapeUtils.js"; +export { TextureUtils } from "./extras/TextureUtils.js"; +export * from "./geometries/Geometries.js"; +export * from "./helpers/ArrowHelper.js"; +export * from "./helpers/AxesHelper.js"; +export * from "./helpers/Box3Helper.js"; +export * from "./helpers/BoxHelper.js"; +export * from "./helpers/CameraHelper.js"; +export * from "./helpers/DirectionalLightHelper.js"; +export * from "./helpers/GridHelper.js"; +export * from "./helpers/HemisphereLightHelper.js"; +export * from "./helpers/PlaneHelper.js"; +export * from "./helpers/PointLightHelper.js"; +export * from "./helpers/PolarGridHelper.js"; +export * from "./helpers/SkeletonHelper.js"; +export * from "./helpers/SpotLightHelper.js"; +export * from "./lights/AmbientLight.js"; +export * from "./lights/DirectionalLight.js"; +export type { DirectionalLightShadow } from "./lights/DirectionalLightShadow.js"; +export * from "./lights/HemisphereLight.js"; +export * from "./lights/Light.js"; +export * from "./lights/LightProbe.js"; +export type { LightShadow, LightShadowJSON } from "./lights/LightShadow.js"; +export * from "./lights/PointLight.js"; +export type { PointLightShadow } from "./lights/PointLightShadow.js"; +export * from "./lights/RectAreaLight.js"; +export * from "./lights/SpotLight.js"; +export type { SpotLightShadow } from "./lights/SpotLightShadow.js"; +export * from "./loaders/AnimationLoader.js"; +export * from "./loaders/AudioLoader.js"; +export * from "./loaders/BufferGeometryLoader.js"; +export * from "./loaders/Cache.js"; +export * from "./loaders/CompressedTextureLoader.js"; +export * from "./loaders/CubeTextureLoader.js"; +export * from "./loaders/DataTextureLoader.js"; +export * from "./loaders/FileLoader.js"; +export * from "./loaders/ImageBitmapLoader.js"; +export * from "./loaders/ImageLoader.js"; +export * from "./loaders/Loader.js"; +export * from "./loaders/LoaderUtils.js"; +export * from "./loaders/LoadingManager.js"; +export * from "./loaders/MaterialLoader.js"; +export * from "./loaders/ObjectLoader.js"; +export * from "./loaders/TextureLoader.js"; +export * from "./materials/Materials.js"; +export * from "./math/Box2.js"; +export * from "./math/Box3.js"; +export * from "./math/Color.js"; +export { ColorManagement, DefinedColorSpace, WorkingColorSpace } from "./math/ColorManagement.js"; +export * from "./math/Cylindrical.js"; +export * from "./math/Euler.js"; +export * from "./math/Frustum.js"; +export * from "./math/Interpolant.js"; +export * from "./math/interpolants/CubicInterpolant.js"; +export * from "./math/interpolants/DiscreteInterpolant.js"; +export * from "./math/interpolants/LinearInterpolant.js"; +export * from "./math/interpolants/QuaternionLinearInterpolant.js"; +export * from "./math/Line3.js"; +export { MathUtils } from "./math/MathUtils.js"; +export * from "./math/Matrix2.js"; +export * from "./math/Matrix3.js"; +export * from "./math/Matrix4.js"; +export * from "./math/Plane.js"; +export * from "./math/Quaternion.js"; +export * from "./math/Ray.js"; +export * from "./math/Sphere.js"; +export * from "./math/Spherical.js"; +export * from "./math/SphericalHarmonics3.js"; +export * from "./math/Triangle.js"; +export * from "./math/Vector2.js"; +export * from "./math/Vector3.js"; +export * from "./math/Vector4.js"; +export * from "./objects/BatchedMesh.js"; +export * from "./objects/Bone.js"; +export * from "./objects/Group.js"; +export * from "./objects/InstancedMesh.js"; +export * from "./objects/Line.js"; +export * from "./objects/LineLoop.js"; +export * from "./objects/LineSegments.js"; +export * from "./objects/LOD.js"; +export * from "./objects/Mesh.js"; +export * from "./objects/Points.js"; +export * from "./objects/Skeleton.js"; +export * from "./objects/SkinnedMesh.js"; +export * from "./objects/Sprite.js"; +// export * from "./renderers/shaders/ShaderChunk.js"; +// export * from "./renderers/shaders/ShaderLib.js"; +// export * from "./renderers/shaders/UniformsLib.js"; +// export { UniformsUtils } from './renderers/shaders/UniformsUtils.js'; +export type { WebGLProgramParameters, WebGLProgramParametersWithUniforms } from "./renderers/webgl/WebGLPrograms.js"; +export type { WebGLShadowMap } from "./renderers/webgl/WebGLShadowMap.js"; +// export * from "./renderers/webgl/WebGLUtils.js"; +export * from "./renderers/WebGL3DRenderTarget.js"; +export * from "./renderers/WebGLArrayRenderTarget.js"; +export * from "./renderers/WebGLCubeRenderTarget.js"; +// export * from "./renderers/WebGLRenderer.js"; +export * from "./renderers/WebGLRenderTarget.js"; +export type { + WebXRController, + WebXRSpaceEventMap, + XRControllerEventType, + XRGripSpace, + XRHandInputState, + XRHandJoints, + XRHandSpace, + XRJointSpace, + XRTargetRaySpace, +} from "./renderers/webxr/WebXRController.js"; +export type { WebXRDepthSensing } from "./renderers/webxr/WebXRDepthSensing.js"; +export type { + WebXRArrayCamera, + WebXRCamera, + WebXRManager, + WebXRManagerEventMap, +} from "./renderers/webxr/WebXRManager.js"; +export * from "./scenes/Fog.js"; +export * from "./scenes/FogExp2.js"; +export * from "./scenes/Scene.js"; +export * from "./textures/CanvasTexture.js"; +export * from "./textures/CompressedArrayTexture.js"; +export * from "./textures/CompressedCubeTexture.js"; +export * from "./textures/CompressedTexture.js"; +export * from "./textures/CubeTexture.js"; +export * from "./textures/Data3DTexture.js"; +export * from "./textures/DataArrayTexture.js"; +export * from "./textures/DataTexture.js"; +export * from "./textures/DepthTexture.js"; +export * from "./textures/FramebufferTexture.js"; +export * from "./textures/Source.js"; +export * from "./textures/Texture.js"; +export * from "./textures/VideoTexture.js"; +export * from "./Three.Legacy.js"; +export { createCanvasElement } from "./utils.js"; + +export { default as IESSpotLight } from "./lights/webgpu/IESSpotLight.js"; +export * from "./nodes/Nodes.js"; +export { default as PMREMGenerator } from "./renderers/common/extras/PMREMGenerator.js"; +export { default as PostProcessing } from "./renderers/common/PostProcessing.js"; +export { default as QuadMesh } from "./renderers/common/QuadMesh.js"; +export type { default as Renderer } from "./renderers/common/Renderer.js"; +export { default as StorageBufferAttribute } from "./renderers/common/StorageBufferAttribute.js"; +export { default as StorageInstancedBufferAttribute } from "./renderers/common/StorageInstancedBufferAttribute.js"; +export { default as StorageTexture } from "./renderers/common/StorageTexture.js"; +export { default as WebGPURenderer } from "./renderers/webgpu/WebGPURenderer.js"; diff --git a/src-testing/src/Three.d.ts b/src-testing/src/Three.d.ts new file mode 100644 index 000000000..2b2ef3cb7 --- /dev/null +++ b/src-testing/src/Three.d.ts @@ -0,0 +1,213 @@ +export * from "./animation/AnimationAction.js"; +export * from "./animation/AnimationClip.js"; +export * from "./animation/AnimationMixer.js"; +export * from "./animation/AnimationObjectGroup.js"; +export { AnimationUtils } from "./animation/AnimationUtils.js"; +export * from "./animation/KeyframeTrack.js"; +export * from "./animation/PropertyBinding.js"; +export * from "./animation/PropertyMixer.js"; +export * from "./animation/tracks/BooleanKeyframeTrack.js"; +export * from "./animation/tracks/ColorKeyframeTrack.js"; +export * from "./animation/tracks/NumberKeyframeTrack.js"; +export * from "./animation/tracks/QuaternionKeyframeTrack.js"; +export * from "./animation/tracks/StringKeyframeTrack.js"; +export * from "./animation/tracks/VectorKeyframeTrack.js"; +export * from "./audio/Audio.js"; +export * from "./audio/AudioAnalyser.js"; +export * from "./audio/AudioContext.js"; +export * from "./audio/AudioListener.js"; +export * from "./audio/PositionalAudio.js"; +export * from "./cameras/ArrayCamera.js"; +export * from "./cameras/Camera.js"; +export * from "./cameras/CubeCamera.js"; +export * from "./cameras/OrthographicCamera.js"; +export * from "./cameras/PerspectiveCamera.js"; +export * from "./cameras/StereoCamera.js"; +export * from "./constants.js"; +export * from "./core/BufferAttribute.js"; +export * from "./core/BufferGeometry.js"; +export * from "./core/Clock.js"; +export * from "./core/EventDispatcher.js"; +export * from "./core/GLBufferAttribute.js"; +export * from "./core/InstancedBufferAttribute.js"; +export * from "./core/InstancedBufferGeometry.js"; +export * from "./core/InstancedInterleavedBuffer.js"; +export * from "./core/InterleavedBuffer.js"; +export * from "./core/InterleavedBufferAttribute.js"; +export * from "./core/Layers.js"; +export * from "./core/Object3D.js"; +export * from "./core/Raycaster.js"; +export * from "./core/RenderTarget.js"; +export * from "./core/Uniform.js"; +export * from "./core/UniformsGroup.js"; +export * from "./extras/core/Curve.js"; +export * from "./extras/core/CurvePath.js"; +export * from "./extras/core/Path.js"; +export * from "./extras/core/Shape.js"; +export * from "./extras/core/ShapePath.js"; +export * from "./extras/curves/Curves.js"; +export { DataUtils } from "./extras/DataUtils.js"; +export * from "./extras/ImageUtils.js"; +export * from "./extras/PMREMGenerator.js"; +export * from "./extras/ShapeUtils.js"; +export { TextureUtils } from "./extras/TextureUtils.js"; +export * from "./geometries/Geometries.js"; +export * from "./helpers/ArrowHelper.js"; +export * from "./helpers/AxesHelper.js"; +export * from "./helpers/Box3Helper.js"; +export * from "./helpers/BoxHelper.js"; +export * from "./helpers/CameraHelper.js"; +export * from "./helpers/DirectionalLightHelper.js"; +export * from "./helpers/GridHelper.js"; +export * from "./helpers/HemisphereLightHelper.js"; +export * from "./helpers/PlaneHelper.js"; +export * from "./helpers/PointLightHelper.js"; +export * from "./helpers/PolarGridHelper.js"; +export * from "./helpers/SkeletonHelper.js"; +export * from "./helpers/SpotLightHelper.js"; +export * from "./lights/AmbientLight.js"; +export * from "./lights/DirectionalLight.js"; +export type { DirectionalLightShadow } from "./lights/DirectionalLightShadow.js"; +export * from "./lights/HemisphereLight.js"; +export * from "./lights/Light.js"; +export * from "./lights/LightProbe.js"; +export type { LightShadow, LightShadowJSON } from "./lights/LightShadow.js"; +export * from "./lights/PointLight.js"; +export type { PointLightShadow } from "./lights/PointLightShadow.js"; +export * from "./lights/RectAreaLight.js"; +export * from "./lights/SpotLight.js"; +export type { SpotLightShadow } from "./lights/SpotLightShadow.js"; +export * from "./loaders/AnimationLoader.js"; +export * from "./loaders/AudioLoader.js"; +export * from "./loaders/BufferGeometryLoader.js"; +export * from "./loaders/Cache.js"; +export * from "./loaders/CompressedTextureLoader.js"; +export * from "./loaders/CubeTextureLoader.js"; +export * from "./loaders/DataTextureLoader.js"; +export * from "./loaders/FileLoader.js"; +export * from "./loaders/ImageBitmapLoader.js"; +export * from "./loaders/ImageLoader.js"; +export * from "./loaders/Loader.js"; +export * from "./loaders/LoaderUtils.js"; +export * from "./loaders/LoadingManager.js"; +export * from "./loaders/MaterialLoader.js"; +export * from "./loaders/ObjectLoader.js"; +export * from "./loaders/TextureLoader.js"; +export * from "./materials/Materials.js"; +export * from "./math/Box2.js"; +export * from "./math/Box3.js"; +export * from "./math/Color.js"; +export { ColorManagement, DefinedColorSpace, WorkingColorSpace } from "./math/ColorManagement.js"; +export * from "./math/Cylindrical.js"; +export * from "./math/Euler.js"; +export * from "./math/Frustum.js"; +export * from "./math/Interpolant.js"; +export * from "./math/interpolants/CubicInterpolant.js"; +export * from "./math/interpolants/DiscreteInterpolant.js"; +export * from "./math/interpolants/LinearInterpolant.js"; +export * from "./math/interpolants/QuaternionLinearInterpolant.js"; +export * from "./math/Line3.js"; +export { MathUtils } from "./math/MathUtils.js"; +export * from "./math/Matrix2.js"; +export * from "./math/Matrix3.js"; +export * from "./math/Matrix4.js"; +export * from "./math/Plane.js"; +export * from "./math/Quaternion.js"; +export * from "./math/Ray.js"; +export * from "./math/Sphere.js"; +export * from "./math/Spherical.js"; +export * from "./math/SphericalHarmonics3.js"; +export * from "./math/Triangle.js"; +export * from "./math/Vector2.js"; +export * from "./math/Vector3.js"; +export * from "./math/Vector4.js"; +export * from "./objects/BatchedMesh.js"; +export * from "./objects/Bone.js"; +export * from "./objects/Group.js"; +export * from "./objects/InstancedMesh.js"; +export * from "./objects/Line.js"; +export * from "./objects/LineLoop.js"; +export * from "./objects/LineSegments.js"; +export * from "./objects/LOD.js"; +export * from "./objects/Mesh.js"; +export * from "./objects/Points.js"; +export * from "./objects/Skeleton.js"; +export * from "./objects/SkinnedMesh.js"; +export * from "./objects/Sprite.js"; +export * from "./renderers/shaders/ShaderChunk.js"; +export * from "./renderers/shaders/ShaderLib.js"; +export * from "./renderers/shaders/UniformsLib.js"; +export { UniformsUtils } from "./renderers/shaders/UniformsUtils.js"; +export type { WebGLAttributes } from "./renderers/webgl/WebGLAttributes.js"; +export type { WebGLBindingStates } from "./renderers/webgl/WebGLBindingStates.js"; +export type { WebGLBufferRenderer } from "./renderers/webgl/WebGLBufferRenderer.js"; +export type { WebGLCapabilities, WebGLCapabilitiesParameters } from "./renderers/webgl/WebGLCapabilities.js"; +export type { WebGLClipping } from "./renderers/webgl/WebGLClipping.js"; +export type { WebGLCubeMaps } from "./renderers/webgl/WebGLCubeMaps.js"; +export type { WebGLCubeUVMaps } from "./renderers/webgl/WebGLCubeUVMaps.js"; +export type { WebGLExtensions } from "./renderers/webgl/WebGLExtensions.js"; +export type { WebGLGeometries } from "./renderers/webgl/WebGLGeometries.js"; +export type { WebGLIndexedBufferRenderer } from "./renderers/webgl/WebGLIndexedBufferRenderer.js"; +export type { WebGLInfo } from "./renderers/webgl/WebGLInfo.js"; +export type { WebGLLights, WebGLLightsState } from "./renderers/webgl/WebGLLights.js"; +export type { WebGLObjects } from "./renderers/webgl/WebGLObjects.js"; +export type { WebGLProgram } from "./renderers/webgl/WebGLProgram.js"; +export type { + WebGLProgramParameters, + WebGLProgramParametersWithUniforms, + WebGLPrograms, +} from "./renderers/webgl/WebGLPrograms.js"; +export type { WebGLProperties } from "./renderers/webgl/WebGLProperties.js"; +export type { RenderItem, WebGLRenderList, WebGLRenderLists } from "./renderers/webgl/WebGLRenderLists.js"; +export type { WebGLShader } from "./renderers/webgl/WebGLShader.js"; +export type { WebGLShadowMap } from "./renderers/webgl/WebGLShadowMap.js"; +export type { + WebGLColorBuffer, + WebGLDepthBuffer, + WebGLState, + WebGLStencilBuffer, +} from "./renderers/webgl/WebGLState.js"; +export type { WebGLTextures } from "./renderers/webgl/WebGLTextures.js"; +export type { WebGLUniforms } from "./renderers/webgl/WebGLUniforms.js"; +export * from "./renderers/webgl/WebGLUtils.js"; +export * from "./renderers/WebGL3DRenderTarget.js"; +export * from "./renderers/WebGLArrayRenderTarget.js"; +export * from "./renderers/WebGLCubeRenderTarget.js"; +export * from "./renderers/WebGLRenderer.js"; +export * from "./renderers/WebGLRenderTarget.js"; +export type { + WebXRController, + WebXRSpaceEventMap, + XRControllerEventType, + XRGripSpace, + XRHandInputState, + XRHandJoints, + XRHandSpace, + XRJointSpace, + XRTargetRaySpace, +} from "./renderers/webxr/WebXRController.js"; +export type { WebXRDepthSensing } from "./renderers/webxr/WebXRDepthSensing.js"; +export type { + WebXRArrayCamera, + WebXRCamera, + WebXRManager, + WebXRManagerEventMap, +} from "./renderers/webxr/WebXRManager.js"; +export * from "./scenes/Fog.js"; +export * from "./scenes/FogExp2.js"; +export * from "./scenes/Scene.js"; +export * from "./textures/CanvasTexture.js"; +export * from "./textures/CompressedArrayTexture.js"; +export * from "./textures/CompressedCubeTexture.js"; +export * from "./textures/CompressedTexture.js"; +export * from "./textures/CubeTexture.js"; +export * from "./textures/Data3DTexture.js"; +export * from "./textures/DataArrayTexture.js"; +export * from "./textures/DataTexture.js"; +export * from "./textures/DepthTexture.js"; +export * from "./textures/FramebufferTexture.js"; +export * from "./textures/Source.js"; +export * from "./textures/Texture.js"; +export * from "./textures/VideoTexture.js"; +export * from "./Three.Legacy.js"; +export { createCanvasElement } from "./utils.js"; diff --git a/src-testing/src/animation/AnimationAction.d.ts b/src-testing/src/animation/AnimationAction.d.ts new file mode 100644 index 000000000..3fa3852a2 --- /dev/null +++ b/src-testing/src/animation/AnimationAction.d.ts @@ -0,0 +1,86 @@ +import { AnimationActionLoopStyles, AnimationBlendMode } from "../constants.js"; +import { Object3D } from "../core/Object3D.js"; +import { AnimationClip } from "./AnimationClip.js"; +import { AnimationMixer } from "./AnimationMixer.js"; +// Animation //////////////////////////////////////////////////////////////////////////////////////// + +export class AnimationAction { + constructor(mixer: AnimationMixer, clip: AnimationClip, localRoot?: Object3D, blendMode?: AnimationBlendMode); + + blendMode: AnimationBlendMode; + + /** + * @default THREE.LoopRepeat + */ + loop: AnimationActionLoopStyles; + + /** + * @default 0 + */ + time: number; + + /** + * @default 1 + */ + timeScale: number; + + /** + * @default 1 + */ + weight: number; + + /** + * @default Infinity + */ + repetitions: number; + + /** + * @default false + */ + paused: boolean; + + /** + * @default true + */ + enabled: boolean; + + /** + * @default false + */ + clampWhenFinished: boolean; + + /** + * @default true + */ + zeroSlopeAtStart: boolean; + + /** + * @default true + */ + zeroSlopeAtEnd: boolean; + + play(): AnimationAction; + stop(): AnimationAction; + reset(): AnimationAction; + isRunning(): boolean; + isScheduled(): boolean; + startAt(time: number): AnimationAction; + setLoop(mode: AnimationActionLoopStyles, repetitions: number): AnimationAction; + setEffectiveWeight(weight: number): AnimationAction; + getEffectiveWeight(): number; + fadeIn(duration: number): AnimationAction; + fadeOut(duration: number): AnimationAction; + crossFadeFrom(fadeOutAction: AnimationAction, duration: number, warp: boolean): AnimationAction; + crossFadeTo(fadeInAction: AnimationAction, duration: number, warp: boolean): AnimationAction; + stopFading(): AnimationAction; + setEffectiveTimeScale(timeScale: number): AnimationAction; + getEffectiveTimeScale(): number; + setDuration(duration: number): AnimationAction; + syncWith(action: AnimationAction): AnimationAction; + halt(duration: number): AnimationAction; + warp(statTimeScale: number, endTimeScale: number, duration: number): AnimationAction; + stopWarping(): AnimationAction; + getMixer(): AnimationMixer; + getClip(): AnimationClip; + getRoot(): Object3D; +} diff --git a/src-testing/src/animation/AnimationClip.d.ts b/src-testing/src/animation/AnimationClip.d.ts new file mode 100644 index 000000000..4efc3df72 --- /dev/null +++ b/src-testing/src/animation/AnimationClip.d.ts @@ -0,0 +1,59 @@ +import { AnimationBlendMode } from "../constants.js"; +import { Vector3 } from "../math/Vector3.js"; +import { Bone } from "../objects/Bone.js"; +import { KeyframeTrack, KeyframeTrackJSON } from "./KeyframeTrack.js"; + +export interface AnimationClipJSON { + name: string; + duration: number; + tracks: KeyframeTrackJSON[]; + uuid: string; + blendMode: AnimationBlendMode; +} + +export interface MorphTarget { + name: string; + vertices: Vector3[]; +} + +export class AnimationClip { + constructor(name?: string, duration?: number, tracks?: KeyframeTrack[], blendMode?: AnimationBlendMode); + + name: string; + tracks: KeyframeTrack[]; + + /** + * @default THREE.NormalAnimationBlendMode + */ + blendMode: AnimationBlendMode; + + /** + * @default -1 + */ + duration: number; + uuid: string; + results: any[]; + + resetDuration(): AnimationClip; + trim(): AnimationClip; + validate(): boolean; + optimize(): AnimationClip; + clone(): this; + toJSON(clip: AnimationClip): any; + + static CreateFromMorphTargetSequence( + name: string, + morphTargetSequence: MorphTarget[], + fps: number, + noLoop: boolean, + ): AnimationClip; + static findByName(clipArray: AnimationClip[], name: string): AnimationClip; + static CreateClipsFromMorphTargetSequences( + morphTargets: MorphTarget[], + fps: number, + noLoop: boolean, + ): AnimationClip[]; + static parse(json: AnimationClipJSON): AnimationClip; + static parseAnimation(animation: AnimationClipJSON, bones: Bone[]): AnimationClip; + static toJSON(clip: AnimationClip): AnimationClipJSON; +} diff --git a/src-testing/src/animation/AnimationMixer.d.ts b/src-testing/src/animation/AnimationMixer.d.ts new file mode 100644 index 000000000..95712d893 --- /dev/null +++ b/src-testing/src/animation/AnimationMixer.d.ts @@ -0,0 +1,39 @@ +import { AnimationBlendMode } from "../constants.js"; +import { EventDispatcher } from "../core/EventDispatcher.js"; +import { Object3D } from "../core/Object3D.js"; +import { AnimationAction } from "./AnimationAction.js"; +import { AnimationClip } from "./AnimationClip.js"; +import { AnimationObjectGroup } from "./AnimationObjectGroup.js"; + +export interface AnimationMixerEventMap { + loop: { action: AnimationAction; loopDelta: number }; + finished: { action: AnimationAction; direction: number }; +} + +export class AnimationMixer extends EventDispatcher { + constructor(root: Object3D | AnimationObjectGroup); + + /** + * @default 0 + */ + time: number; + + /** + * @default 1.0 + */ + timeScale: number; + + clipAction( + clip: AnimationClip, + root?: Object3D | AnimationObjectGroup, + blendMode?: AnimationBlendMode, + ): AnimationAction; + existingAction(clip: AnimationClip, root?: Object3D | AnimationObjectGroup): AnimationAction | null; + stopAllAction(): AnimationMixer; + update(deltaTime: number): AnimationMixer; + setTime(timeInSeconds: number): AnimationMixer; + getRoot(): Object3D | AnimationObjectGroup; + uncacheClip(clip: AnimationClip): void; + uncacheRoot(root: Object3D | AnimationObjectGroup): void; + uncacheAction(clip: AnimationClip, root?: Object3D | AnimationObjectGroup): void; +} diff --git a/src-testing/src/animation/AnimationObjectGroup.d.ts b/src-testing/src/animation/AnimationObjectGroup.d.ts new file mode 100644 index 000000000..9d4f29ca9 --- /dev/null +++ b/src-testing/src/animation/AnimationObjectGroup.d.ts @@ -0,0 +1,17 @@ +export class AnimationObjectGroup { + constructor(...args: any[]); + + uuid: string; + stats: { + bindingsPerObject: number; + objects: { + total: number; + inUse: number; + }; + }; + readonly isAnimationObjectGroup: true; + + add(...args: any[]): void; + remove(...args: any[]): void; + uncache(...args: any[]): void; +} diff --git a/src-testing/src/animation/AnimationUtils.d.ts b/src-testing/src/animation/AnimationUtils.d.ts new file mode 100644 index 000000000..4a712ed30 --- /dev/null +++ b/src-testing/src/animation/AnimationUtils.d.ts @@ -0,0 +1,60 @@ +import { AnimationClip } from "./AnimationClip.js"; + +declare function convertArray(array: any, type: any, forceClone: boolean): any; + +declare function isTypedArray(object: any): boolean; + +declare function getKeyframeOrder(times: number[]): number[]; + +declare function sortedArray(values: any[], stride: number, order: number[]): any[]; + +declare function flattenJSON(jsonKeys: string[], times: any[], values: any[], valuePropertyName: string): void; + +/** + * @param sourceClip + * @param name + * @param startFrame + * @param endFrame + * @param [fps=30] + */ +declare function subclip( + sourceClip: AnimationClip, + name: string, + startFrame: number, + endFrame: number, + fps?: number, +): AnimationClip; + +/** + * @param targetClip + * @param [referenceFrame=0] + * @param [referenceClip=targetClip] + * @param [fps=30] + */ +declare function makeClipAdditive( + targetClip: AnimationClip, + referenceFrame?: number, + referenceClip?: AnimationClip, + fps?: number, +): AnimationClip; + +declare const AnimationUtils: { + convertArray: typeof convertArray; + isTypedArray: typeof isTypedArray; + getKeyframeOrder: typeof getKeyframeOrder; + sortedArray: typeof sortedArray; + flattenJSON: typeof flattenJSON; + subclip: typeof subclip; + makeClipAdditive: typeof makeClipAdditive; +}; + +export { + AnimationUtils, + convertArray, + flattenJSON, + getKeyframeOrder, + isTypedArray, + makeClipAdditive, + sortedArray, + subclip, +}; diff --git a/src-testing/src/animation/KeyframeTrack.d.ts b/src-testing/src/animation/KeyframeTrack.d.ts new file mode 100644 index 000000000..2a48e2651 --- /dev/null +++ b/src-testing/src/animation/KeyframeTrack.d.ts @@ -0,0 +1,55 @@ +import { InterpolationModes } from "../constants.js"; +import { Interpolant } from "../math/Interpolant.js"; +import { CubicInterpolant } from "../math/interpolants/CubicInterpolant.js"; +import { DiscreteInterpolant } from "../math/interpolants/DiscreteInterpolant.js"; +import { LinearInterpolant } from "../math/interpolants/LinearInterpolant.js"; + +export interface KeyframeTrackJSON { + name: string; + times: number[]; + values: number[]; + interpolation?: InterpolationModes; + type: string; +} + +export class KeyframeTrack { + /** + * @param name + * @param times + * @param values + * @param [interpolation=THREE.InterpolateLinear] + */ + constructor(name: string, times: ArrayLike, values: ArrayLike, interpolation?: InterpolationModes); + + name: string; + times: Float32Array; + values: Float32Array; + + ValueTypeName: string; + TimeBufferType: Float32Array; + ValueBufferType: Float32Array; + + /** + * @default THREE.InterpolateLinear + */ + DefaultInterpolation: InterpolationModes; + + InterpolantFactoryMethodDiscrete(result: any): DiscreteInterpolant; + InterpolantFactoryMethodLinear(result: any): LinearInterpolant; + InterpolantFactoryMethodSmooth(result: any): CubicInterpolant; + + setInterpolation(interpolation: InterpolationModes): KeyframeTrack; + getInterpolation(): InterpolationModes; + createInterpolant(): Interpolant; + + getValueSize(): number; + + shift(timeOffset: number): KeyframeTrack; + scale(timeScale: number): KeyframeTrack; + trim(startTime: number, endTime: number): KeyframeTrack; + validate(): boolean; + optimize(): KeyframeTrack; + clone(): this; + + static toJSON(track: KeyframeTrack): KeyframeTrackJSON; +} diff --git a/src-testing/src/animation/PropertyBinding.d.ts b/src-testing/src/animation/PropertyBinding.d.ts new file mode 100644 index 000000000..fec777634 --- /dev/null +++ b/src-testing/src/animation/PropertyBinding.d.ts @@ -0,0 +1,43 @@ +export interface ParseTrackNameResults { + nodeName: string; + objectName: string; + objectIndex: string; + propertyName: string; + propertyIndex: string; +} + +declare class Composite { + constructor(targetGroup: any, path: any, parsedPath?: any); + + getValue(array: any, offset: number): any; + setValue(array: any, offset: number): void; + bind(): void; + unbind(): void; +} + +declare class PropertyBinding { + constructor(rootNode: any, path: string, parsedPath?: any); + + path: string; + parsedPath: any; + node: any; + rootNode: any; + + getValue(targetArray: any, offset: number): any; + setValue(sourceArray: any, offset: number): void; + bind(): void; + unbind(): void; + + BindingType: { [bindingType: string]: number }; + Versioning: { [versioning: string]: number }; + + GetterByBindingType: Array<() => void>; + SetterByBindingTypeAndVersioning: Array void>>; + + static create(root: any, path: any, parsedPath?: any): PropertyBinding | Composite; + static sanitizeNodeName(name: string): string; + static parseTrackName(trackName: string): ParseTrackNameResults; + static findNode(root: any, nodeName: string): any; +} + +export { PropertyBinding }; diff --git a/src-testing/src/animation/PropertyMixer.d.ts b/src-testing/src/animation/PropertyMixer.d.ts new file mode 100644 index 000000000..f413291fa --- /dev/null +++ b/src-testing/src/animation/PropertyMixer.d.ts @@ -0,0 +1,17 @@ +export class PropertyMixer { + constructor(binding: any, typeName: string, valueSize: number); + + binding: any; + valueSize: number; + buffer: any; + cumulativeWeight: number; + cumulativeWeightAdditive: number; + useCount: number; + referenceCount: number; + + accumulate(accuIndex: number, weight: number): void; + accumulateAdditive(weight: number): void; + apply(accuIndex: number): void; + saveOriginalState(): void; + restoreOriginalState(): void; +} diff --git a/src-testing/src/animation/tracks/BooleanKeyframeTrack.d.ts b/src-testing/src/animation/tracks/BooleanKeyframeTrack.d.ts new file mode 100644 index 000000000..45b65448a --- /dev/null +++ b/src-testing/src/animation/tracks/BooleanKeyframeTrack.d.ts @@ -0,0 +1,10 @@ +import { KeyframeTrack } from "../KeyframeTrack.js"; + +export class BooleanKeyframeTrack extends KeyframeTrack { + constructor(name: string, times: ArrayLike, values: ArrayLike); + + /** + * @default 'bool' + */ + ValueTypeName: string; +} diff --git a/src-testing/src/animation/tracks/ColorKeyframeTrack.d.ts b/src-testing/src/animation/tracks/ColorKeyframeTrack.d.ts new file mode 100644 index 000000000..60d0fe9f7 --- /dev/null +++ b/src-testing/src/animation/tracks/ColorKeyframeTrack.d.ts @@ -0,0 +1,11 @@ +import { InterpolationModes } from "../../constants.js"; +import { KeyframeTrack } from "../KeyframeTrack.js"; + +export class ColorKeyframeTrack extends KeyframeTrack { + constructor(name: string, times: ArrayLike, values: ArrayLike, interpolation?: InterpolationModes); + + /** + * @default 'color' + */ + ValueTypeName: string; +} diff --git a/src-testing/src/animation/tracks/NumberKeyframeTrack.d.ts b/src-testing/src/animation/tracks/NumberKeyframeTrack.d.ts new file mode 100644 index 000000000..e27ff0337 --- /dev/null +++ b/src-testing/src/animation/tracks/NumberKeyframeTrack.d.ts @@ -0,0 +1,11 @@ +import { InterpolationModes } from "../../constants.js"; +import { KeyframeTrack } from "../KeyframeTrack.js"; + +export class NumberKeyframeTrack extends KeyframeTrack { + constructor(name: string, times: ArrayLike, values: ArrayLike, interpolation?: InterpolationModes); + + /** + * @default 'number' + */ + ValueTypeName: string; +} diff --git a/src-testing/src/animation/tracks/QuaternionKeyframeTrack.d.ts b/src-testing/src/animation/tracks/QuaternionKeyframeTrack.d.ts new file mode 100644 index 000000000..29942bad5 --- /dev/null +++ b/src-testing/src/animation/tracks/QuaternionKeyframeTrack.d.ts @@ -0,0 +1,11 @@ +import { InterpolationModes } from "../../constants.js"; +import { KeyframeTrack } from "../KeyframeTrack.js"; + +export class QuaternionKeyframeTrack extends KeyframeTrack { + constructor(name: string, times: ArrayLike, values: ArrayLike, interpolation?: InterpolationModes); + + /** + * @default 'quaternion' + */ + ValueTypeName: string; +} diff --git a/src-testing/src/animation/tracks/StringKeyframeTrack.d.ts b/src-testing/src/animation/tracks/StringKeyframeTrack.d.ts new file mode 100644 index 000000000..9bbfd2027 --- /dev/null +++ b/src-testing/src/animation/tracks/StringKeyframeTrack.d.ts @@ -0,0 +1,10 @@ +import { KeyframeTrack } from "../KeyframeTrack.js"; + +export class StringKeyframeTrack extends KeyframeTrack { + constructor(name: string, times: ArrayLike, values: ArrayLike); + + /** + * @default 'string' + */ + ValueTypeName: string; +} diff --git a/src-testing/src/animation/tracks/VectorKeyframeTrack.d.ts b/src-testing/src/animation/tracks/VectorKeyframeTrack.d.ts new file mode 100644 index 000000000..0909d4493 --- /dev/null +++ b/src-testing/src/animation/tracks/VectorKeyframeTrack.d.ts @@ -0,0 +1,11 @@ +import { InterpolationModes } from "../../constants.js"; +import { KeyframeTrack } from "../KeyframeTrack.js"; + +export class VectorKeyframeTrack extends KeyframeTrack { + constructor(name: string, times: ArrayLike, values: ArrayLike, interpolation?: InterpolationModes); + + /** + * @default 'vector' + */ + ValueTypeName: string; +} diff --git a/src-testing/src/audio/Audio.d.ts b/src-testing/src/audio/Audio.d.ts new file mode 100644 index 000000000..434ff7fef --- /dev/null +++ b/src-testing/src/audio/Audio.d.ts @@ -0,0 +1,270 @@ +import { Object3D } from "../core/Object3D.js"; +import { AudioContext } from "./AudioContext.js"; +import { AudioListener } from "./AudioListener.js"; + +// Extras / Audio ///////////////////////////////////////////////////////////////////// + +/** + * Create a non-positional ( global ) {@link Audio} object. + * This uses the {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API | Web {@link Audio} API}. + * @example + * ```typescript + * // create an AudioListener and add it to the camera + * const listener = new THREE.AudioListener(); + * camera.add(listener); + * // create a global {@link Audio} source + * const sound = new THREE.Audio(listener); + * // load a sound and set it as the {@link Audio} object's buffer + * const audioLoader = new THREE.AudioLoader(); + * audioLoader.load('sounds/ambient.ogg', function (buffer) { + * sound.setBuffer(buffer); + * sound.setLoop(true); + * sound.setVolume(0.5); + * sound.play(); + * }); + * ``` + * @see Example: {@link https://threejs.org/examples/#webaudio_sandbox | webaudio / sandbox } + * @see Example: {@link https://threejs.org/examples/#webaudio_visualizer | webaudio / visualizer } + * @see {@link https://threejs.org/docs/index.html#api/en/audio/Audio | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/audio/Audio.js | Source} + */ +export class Audio extends Object3D { + /** + * Create a new instance of {@link Audio} + * @param listener (required) {@link AudioListener | AudioListener} instance. + */ + constructor(listener: AudioListener); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `Audio` + */ + readonly type: string | "Audio"; + + /** + * A reference to the listener object of this audio. + */ + listener: AudioListener; + + /** + * The {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext | AudioContext} of the {@link AudioListener | listener} given in the constructor. + */ + context: AudioContext; + + /** + * A {@link https://developer.mozilla.org/en-US/docs/Web/API/GainNode | GainNode} created using + * {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createGain | AudioContext.createGain}(). + */ + gain: GainNode; + + /** + * Whether to start playback automatically. + * @defaultValue `false` + */ + autoplay: boolean; + + buffer: AudioBuffer | null; + + /** + * Modify pitch, measured in cents. +/- 100 is a semitone. +/- 1200 is an octave. + * @defaultValue `0` + */ + detune: number; + + /** + * @default false + */ + loop: boolean; + + /** + * @default 0 + */ + loopStart: number; + + /** + * @default 0 + */ + loopEnd: number; + + /** + * An offset to the time within the {@link Audio} buffer that playback should begin. + * Same as the {@link Audio.offset | offset} parameter of {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start | AudioBufferSourceNode.start()}. + * @defaultValue `0` + */ + offset: number; + + /** + * Overrides the duration of the audio. Same as the {@link Audio.duration | duration} parameter of + * {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start | AudioBufferSourceNode.start()}. + * @defaultValue `undefined` _to play the whole buffer_. + */ + duration: number | undefined; + + /** + * Speed of playback. + * @defaultValue `1` + */ + playbackRate: number; + + /** + * Whether the {@link Audio} is currently playing. + * @defaultValue `false` + */ + isPlaying: boolean; + + /** + * Whether playback can be controlled using the {@link Audio.play | play}(), {@link Audio.pause | pause}() etc. methods. + * @defaultValue `true` + */ + hasPlaybackControl: boolean; + + /** + * Type of the {@link Audio} source. + * @defaultValue 'empty'. + */ + sourceType: string; + + /** + * An {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode | AudioBufferSourceNode} created using + * {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createBufferSource | AudioContext.createBufferSource()}. + */ + source: AudioScheduledSourceNode | null; + + /** + * Represents an array of {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioNode | AudioNodes}. + * Can be used to apply a variety of low-order filters to create more complex sound effects. + * In most cases, the array contains instances of {@link https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode | BiquadFilterNodes}. + * Filters are set via {@link THREE.Audio.setFilter | Audio.setFilter} or {@link THREE.Audio.setFilters | Audio.setFilters}. + * @defaultValue `[]` + */ + filters: AudioNode[]; + + /** + * Return the {@link Audio.gain | gainNode}. + */ + getOutput(): NodeType; + + /** + * Setup the {@link Audio.source | source} to the audioBuffer, and sets {@link Audio.sourceType | sourceType} to 'audioNode'. + * @remarks Also sets {@link Audio.hasPlaybackControl | hasPlaybackControl} to false. + */ + setNodeSource(audioNode: AudioScheduledSourceNode): this; + + /** + * Applies the given object of type {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement | HTMLMediaElement} as the source of this audio. + * @remarks Also sets {@link Audio.hasPlaybackControl | hasPlaybackControl} to false. + */ + setMediaElementSource(mediaElement: HTMLMediaElement): this; + + /** + * Applies the given object of type {@link https://developer.mozilla.org/en-US/docs/Web/API/MediaStream | MediaStream} as the source of this audio. + * @remarks Also sets {@link Audio.hasPlaybackControl | hasPlaybackControl} to false. + */ + setMediaStreamSource(mediaStream: MediaStream): this; + + /** + * Setup the {@link Audio.source | source} to the audioBuffer, and sets {@link Audio.sourceType | sourceType} to 'buffer'. + * @remarks If {@link Audio.autoplay | autoplay}, also starts playback. + */ + setBuffer(audioBuffer: AudioBuffer): this; + + /** + * If {@link Audio.hasPlaybackControl | hasPlaybackControl} is true, starts playback. + */ + play(delay?: number): this; + /** + * If {@link Audio.hasPlaybackControl | hasPlaybackControl} is true, pauses playback. + */ + pause(): this; + /** + * If {@link Audio.hasPlaybackControl | hasPlaybackControl} is enabled, stops playback. + */ + stop(): this; + + /** + * Called automatically when playback finished. + */ + onEnded(): void; + + /** + * Connect to the {@link THREE.Audio.source | Audio.source} + * @remarks This is used internally on initialisation and when setting / removing filters. + */ + connect(): this; + /** + * Disconnect from the {@link THREE.Audio.source | Audio.source} + * @remarks This is used internally when setting / removing filters. + */ + disconnect(): this; + + /** + * Returns the detuning of oscillation in cents. + */ + getDetune(): number; + /** + * Defines the detuning of oscillation in cents. + * @param value Expects a `Float` + */ + setDetune(value: number): this; + + /** + * Returns the first element of the {@link Audio.filters | filters} array. + */ + getFilter(): AudioNode; + /** + * Applies a single filter node to the audio. + */ + setFilter(filter: AudioNode): this; + + /** + * Returns the {@link Audio.filters | filters} array. + */ + getFilters(): AudioNode[]; + /** + * Applies an array of filter nodes to the audio. + * @param value Arrays of filters. + */ + setFilters(value: AudioNode[]): this; + + /** + * Return the value of {@link Audio.playbackRate | playbackRate}. + */ + getPlaybackRate(): number; + /** + * If {@link Audio.hasPlaybackControl | hasPlaybackControl} is enabled, set the {@link Audio.playbackRate | playbackRate} to `value`. + * @param value Expects a `Float` + */ + setPlaybackRate(value: number): this; + + /** + * Return the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop | source.loop} (whether playback should loop). + */ + getLoop(): boolean; + /** + * Set {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop | source.loop} to `value` (whether playback should loop). + * @param value + */ + setLoop(value: boolean): this; + + /** + * Set {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopStart | source.loopStart} to `value`. + * @param value Expects a `Float` + */ + setLoopStart(value: number): this; + /** + * Set {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopEnd | source.loopEnd} to `value`. + * @param value Expects a `Float` + */ + setLoopEnd(value: number): this; + + /** + * Return the current volume. + */ + getVolume(): number; + /** + * Set the volume. + * @param value Expects a `Float` + */ + setVolume(value: number): this; +} diff --git a/src-testing/src/audio/AudioAnalyser.d.ts b/src-testing/src/audio/AudioAnalyser.d.ts new file mode 100644 index 000000000..474583ec7 --- /dev/null +++ b/src-testing/src/audio/AudioAnalyser.d.ts @@ -0,0 +1,58 @@ +import { Audio } from "./Audio.js"; + +/** + * Create a {@link AudioAnalyser} object, which uses an {@link https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode | AnalyserNode} to analyse audio data. + * This uses the {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API | Web Audio API}. + * @example + * ```typescript + * // create an AudioListener and add it to the camera + * const listener = new THREE.AudioListener(); + * camera.add(listener); + * // create an Audio source + * const sound = new THREE.Audio(listener); + * // load a sound and set it as the Audio object's buffer + * const audioLoader = new THREE.AudioLoader(); + * audioLoader.load('sounds/ambient.ogg', function (buffer) { + * sound.setBuffer(buffer); + * sound.setLoop(true); + * sound.setVolume(0.5); + * sound.play(); + * }); + * // create an AudioAnalyser, passing in the sound and desired fftSize + * const analyser = new THREE.AudioAnalyser(sound, 32); + * // get the average frequency of the sound + * const data = analyser.getAverageFrequency(); + * ``` + * @see Example: {@link https://threejs.org/examples/#webaudio_sandbox | webaudio / sandbox } + * @see Example: {@link https://threejs.org/examples/#webaudio_visualizer | webaudio / visualizer } + * @see {@link https://threejs.org/docs/index.html#api/en/audio/AudioAnalyser | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/audio/AudioAnalyser.js | Source} + */ +export class AudioAnalyser { + /** + * Create a new {@link {@link AudioAnalyser} | AudioAnalyser}. + * @param audio + * @param fftSize See {@link https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/fftSize | AnalyserNode.fftSize }. Expects a `unsigned integer`. Default `2048`. + */ + constructor(audio: Audio, fftSize?: number); + + /** + * An {@link https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode | AnalyserNode} used to analyze audio. + */ + analyser: AnalyserNode; + + /** + * A Uint8Array with size determined by {@link https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/frequencyBinCount | analyser.frequencyBinCount} used to hold analysis data. + */ + data: Uint8Array; + + /** + * Uses the Web Audio's {@link https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/getByteFrequencyData | getByteFrequencyData} method + */ + getFrequencyData(): Uint8Array; + + /** + * Get the average of the frequencies returned by the {@link AudioAnalyser.getFrequencyData | getFrequencyData} method. + */ + getAverageFrequency(): number; +} diff --git a/src-testing/src/audio/AudioContext.d.ts b/src-testing/src/audio/AudioContext.d.ts new file mode 100644 index 000000000..50a7f3d6e --- /dev/null +++ b/src-testing/src/audio/AudioContext.d.ts @@ -0,0 +1,19 @@ +/** + * This contains methods for setting up an {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext | AudioContext}. + * Used internally by the {@link AudioListener | AudioListener} and {@link AudioLoader | AudioLoader} classes. + * This uses the {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API | Web Audio API}. + * @see {@link https://threejs.org/docs/index.html#api/en/audio/AudioContext | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/audio/AudioContext.js | Source} + */ +export namespace AudioContext { + /** + * Return the value of the variable `context` in the outer scope, if defined, otherwise set it to a new {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext | AudioContext}. + */ + function getContext(): AudioContext; + + /** + * Set the variable `context` in the outer scope to `value`. + * @param value + */ + function setContext(context: AudioContext): void; +} diff --git a/src-testing/src/audio/AudioListener.d.ts b/src-testing/src/audio/AudioListener.d.ts new file mode 100644 index 000000000..a49e4d5c7 --- /dev/null +++ b/src-testing/src/audio/AudioListener.d.ts @@ -0,0 +1,96 @@ +import { Object3D } from "../core/Object3D.js"; +import { AudioContext } from "./AudioContext.js"; + +/** + * The {@link AudioListener} represents a virtual {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioListener | listener} of the all positional and non-positional audio effects in the scene. + * A three.js application usually creates a single instance of {@link AudioListener} * @remarks + * It is a mandatory construtor parameter for audios entities like {@link Audio | Audio} and {@link PositionalAudio | PositionalAudio}. + * In most cases, the listener object is a child of the camera + * So the 3D transformation of the camera represents the 3D transformation of the listener. + * @example + * ```typescript + * // create an {@link AudioListener} and add it to the camera + * const listener = new THREE.AudioListener(); + * camera.add(listener); + * // create a global audio source + * const sound = new THREE.Audio(listener); + * // load a sound and set it as the Audio object's buffer + * const audioLoader = new THREE.AudioLoader(); + * audioLoader.load('sounds/ambient.ogg', function (buffer) { + * sound.setBuffer(buffer); + * sound.setLoop(true); + * sound.setVolume(0.5); + * sound.play(); + * }); + * ``` + * @see Example: {@link https://threejs.org/examples/#webaudio_sandbox | webaudio / sandbox } + * @see Example: {@link https://threejs.org/examples/#webaudio_timing | webaudio / timing } + * @see Example: {@link https://threejs.org/examples/#webaudio_visualizer | webaudio / visualizer } + * @see {@link https://threejs.org/docs/index.html#api/en/audio/AudioListener | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/audio/AudioListener.js | Source} + */ +export class AudioListener extends Object3D { + /** + * Create a new AudioListener. + */ + constructor(); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `AudioListener` + */ + readonly type: string | "AudioListener"; + + /** + * The {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext | AudioContext} of the {@link {@link AudioListener} | listener} given in the constructor. + */ + context: AudioContext; + + /** + * A {@link https://developer.mozilla.org/en-US/docs/Web/API/GainNode | GainNode} created using + * {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createGain | AudioContext.createGain()}. + */ + gain: GainNode; + + /** + * @defaultValue `null` + */ + filter: AudioNode; + + /** + * Time delta value for audio entities. Use in context of {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioParam/linearRampToValueAtTime | AudioParam.linearRampToValueAtTimeDefault()}. + * @defaultValue `0` + */ + timeDelta: number; + + /** + * Return the {@link AudioListener.gain | gainNode}. + */ + getInput(): GainNode; + /** + * Set the {@link AudioListener.filter | filter} property to `null`. + */ + removeFilter(): this; + + /** + * Returns the value of the {@link AudioListener.filter | filter} property. + */ + getFilter(): AudioNode; + /** + * Set the {@link AudioListener.filter | filter} property to `value`. + * @param value + */ + setFilter(value: AudioNode): this; + + /** + * Return the volume. + */ + getMasterVolume(): number; + + /** + * Set the volume. + * @param value + */ + setMasterVolume(value: number): this; +} diff --git a/src-testing/src/audio/PositionalAudio.d.ts b/src-testing/src/audio/PositionalAudio.d.ts new file mode 100644 index 000000000..d72d10674 --- /dev/null +++ b/src-testing/src/audio/PositionalAudio.d.ts @@ -0,0 +1,101 @@ +import { Audio } from "./Audio.js"; +import { AudioListener } from "./AudioListener.js"; + +/** + * Create a positional audio object. + * This uses the {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API | Web Audio API}. + * @example + * ```typescript + * // create an AudioListener and add it to the camera + * const listener = new THREE.AudioListener(); + * camera.add(listener); + * // create the {@link PositionalAudio} object (passing in the listener) + * const sound = new THREE.PositionalAudio(listener); + * // load a sound and set it as the {@link PositionalAudio} object's buffer + * const audioLoader = new THREE.AudioLoader(); + * audioLoader.load('sounds/song.ogg', function (buffer) { + * sound.setBuffer(buffer); + * sound.setRefDistance(20); + * sound.play(); + * }); + * // create an object for the sound to play from + * const sphere = new THREE.SphereGeometry(20, 32, 16); + * const material = new THREE.MeshPhongMaterial({ + * color: 0xff2200 + * }); + * const mesh = new THREE.Mesh(sphere, material); + * scene.add(mesh); + * // finally add the sound to the mesh + * mesh.add(sound); + * ``` + * @see Example: {@link https://threejs.org/examples/#webaudio_orientation | webaudio / orientation } + * @see Example: {@link https://threejs.org/examples/#webaudio_sandbox | webaudio / sandbox } + * @see Example: {@link https://threejs.org/examples/#webaudio_timing | webaudio / timing } + * @see {@link https://threejs.org/docs/index.html#api/en/audio/PositionalAudio | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/audio/PositionalAudio.js | Source} + */ +export class PositionalAudio extends Audio { + /** + * Create a new instance of {@link PositionalAudio} + * @param listener (required) {@link AudioListener | AudioListener} instance. + */ + constructor(listener: AudioListener); + + /** + * The PositionalAudio's {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode | PannerNode}. + */ + panner: PannerNode; + + /** + * Returns the {@link PositionalAudio.panner | panner}. + */ + getOutput(): PannerNode; + + /** + * Returns the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/refDistance | panner.refDistance}. + */ + getRefDistance(): number; + /** + * Sets the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/refDistance | panner.refDistance}. + * @param value Expects a `Float` + */ + setRefDistance(value: number): this; + + /** + * Returns the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/rolloffFactor | panner.rolloffFactor}. + */ + getRolloffFactor(): number; + /** + * Sets the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/rolloffFactor | panner.rolloffFactor}. + * @param value Expects a `Float` + */ + setRolloffFactor(value: number): this; + + /** + * Returns the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/distanceModel | panner.distanceModel}. + */ + getDistanceModel(): string; + /** + * Sets the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/distanceModel | panner.distanceModel}. + * @param value + */ + setDistanceModel(value: string): this; + + /** + * Returns the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/maxDistance | panner.maxDistance}. + */ + getMaxDistance(): number; + /** + * Sets the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/maxDistance | panner.maxDistance}. + * @param value Expects a `Float` + */ + setMaxDistance(value: number): this; + + /** + * This method can be used in order to transform an omnidirectional sound into a {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode | directional sound}. + * @param coneInnerAngle Expects a `Float` + * @param coneOuterAngle Expects a `Float` + * @param coneOuterGain Expects a `Float` + */ + setDirectionalCone(coneInnerAngle: number, coneOuterAngle: number, coneOuterGain: number): this; +} diff --git a/src-testing/src/cameras/ArrayCamera.d.ts b/src-testing/src/cameras/ArrayCamera.d.ts new file mode 100644 index 000000000..e9e9a221d --- /dev/null +++ b/src-testing/src/cameras/ArrayCamera.d.ts @@ -0,0 +1,32 @@ +import { PerspectiveCamera } from "./PerspectiveCamera.js"; + +/** + * {@link ArrayCamera} can be used in order to efficiently render a scene with a predefined set of cameras + * @remarks + * This is an important performance aspect for rendering VR scenes. + * An instance of {@link ArrayCamera} always has an array of sub cameras + * It's mandatory to define for each sub camera the `viewport` property which determines the part of the viewport that is rendered with this camera. + * @see Example: {@link https://threejs.org/examples/#webgl_camera_array | camera / array } + * @see {@link https://threejs.org/docs/index.html#api/en/cameras/ArrayCamera | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/cameras/ArrayCamera.js | Source} + */ +export class ArrayCamera extends PerspectiveCamera { + /** + * An array of cameras. + * @param array. Default `[]`. + */ + constructor(cameras?: PerspectiveCamera[]); + + /** + * Read-only flag to check if a given object is of type {@link ArrayCamera}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isArrayCamera: true; + + /** + * An array of cameras. + * @defaultValue `[]` + */ + cameras: PerspectiveCamera[]; +} diff --git a/src-testing/src/cameras/Camera.d.ts b/src-testing/src/cameras/Camera.d.ts new file mode 100644 index 000000000..b49add52e --- /dev/null +++ b/src-testing/src/cameras/Camera.d.ts @@ -0,0 +1,74 @@ +import { CoordinateSystem } from "../constants.js"; +import { Layers } from "../core/Layers.js"; +import { Object3D } from "../core/Object3D.js"; +import { Matrix4 } from "../math/Matrix4.js"; +import { Vector3 } from "../math/Vector3.js"; +import { Vector4 } from "../math/Vector4.js"; + +/** + * Abstract base class for cameras + * @remarks + * This class should always be inherited when you build a new camera. + * @see {@link https://threejs.org/docs/index.html#api/en/cameras/Camera | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/cameras/Camera.js | Source} + */ +export class Camera extends Object3D { + /** + * @remarks + * Note that this class is not intended to be called directly; you probably want a + * {@link THREE.PerspectiveCamera | PerspectiveCamera} or + * {@link THREE.OrthographicCamera | OrthographicCamera} instead. + */ + constructor(); + + /** + * Read-only flag to check if a given object is of type {@link Camera}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isCamera: true; + + /** + * @override + * @defaultValue `Camera` + */ + override readonly type: string | "Camera"; + + /** + * @override + * The {@link THREE.Layers | layers} that the {@link Camera} is a member of. + * @remarks Objects must share at least one layer with the {@link Camera} to be n when the camera's viewpoint is rendered. + * @defaultValue `new THREE.Layers()` + */ + override layers: Layers; + + /** + * This is the inverse of matrixWorld. + * @remarks MatrixWorld contains the Matrix which has the world transform of the {@link Camera} . + * @defaultValue {@link THREE.Matrix4 | `new THREE.Matrix4()`} + */ + matrixWorldInverse: Matrix4; + + /** + * This is the matrix which contains the projection. + * @defaultValue {@link THREE.Matrix4 | `new THREE.Matrix4()`} + */ + projectionMatrix: Matrix4; + + /** + * This is the inverse of projectionMatrix. + * @defaultValue {@link THREE.Matrix4 | `new THREE.Matrix4()`} + */ + projectionMatrixInverse: Matrix4; + + coordinateSystem: CoordinateSystem; + + viewport?: Vector4; + + /** + * Returns a {@link THREE.Vector3 | Vector3} representing the world space direction in which the {@link Camera} is looking. + * @remarks Note: A {@link Camera} looks down its local, negative z-axis. + * @param target The result will be copied into this Vector3. + */ + getWorldDirection(target: Vector3): Vector3; +} diff --git a/src-testing/src/cameras/CubeCamera.d.ts b/src-testing/src/cameras/CubeCamera.d.ts new file mode 100644 index 000000000..e6e82fb19 --- /dev/null +++ b/src-testing/src/cameras/CubeCamera.d.ts @@ -0,0 +1,68 @@ +import { CoordinateSystem } from "../constants.js"; +import { Object3D } from "../core/Object3D.js"; +import { WebGLCubeRenderTarget } from "../renderers/WebGLCubeRenderTarget.js"; +import { WebGLRenderer } from "../renderers/WebGLRenderer.js"; + +/** + * Creates **6** {@link THREE.PerspectiveCamera | cameras} that render to a {@link THREE.WebGLCubeRenderTarget | WebGLCubeRenderTarget}. + * @remarks The cameras are added to the {@link children} array. + * @example + * ```typescript + * // Create cube render target + * const cubeRenderTarget = new THREE.WebGLCubeRenderTarget( 128, { generateMipmaps: true, minFilter: THREE.LinearMipmapLinearFilter } ); + * + * // Create cube camera + * const cubeCamera = new THREE.CubeCamera( 1, 100000, cubeRenderTarget ); + * scene.add( cubeCamera ); + * + * // Create car + * const chromeMaterial = new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: cubeRenderTarget.texture } ); + * const car = new THREE.Mesh( carGeometry, chromeMaterial ); + * scene.add( car ); + * + * // Update the render target cube + * car.visible = false; + * cubeCamera.position.copy( car.position ); + * cubeCamera.update( renderer, scene ); + * + * // Render the scene + * car.visible = true; + * renderer.render( scene, camera ); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_materials_cubemap_dynamic | materials / cubemap / dynamic } + * @see {@link https://threejs.org/docs/index.html#api/en/cameras/CubeCamera | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/cameras/CubeCamera.js | Source} + */ +export class CubeCamera extends Object3D { + /** + * Constructs a {@link CubeCamera} that contains 6 {@link PerspectiveCamera | PerspectiveCameras} that render to a {@link THREE.WebGLCubeRenderTarget | WebGLCubeRenderTarget}. + * @param near The near clipping distance. + * @param far The far clipping distance. + * @param renderTarget The destination cube render target. + */ + constructor(near: number, far: number, renderTarget: WebGLCubeRenderTarget); + + /** + * @override + * @defaultValue `CubeCamera` + */ + override readonly type: string | "CubeCamera"; + + /** + * The destination cube render target. + */ + renderTarget: WebGLCubeRenderTarget; + + coordinateSystem: CoordinateSystem; + + activeMipmapLevel: number; + + updateCoordinateSystem(): void; + + /** + * Call this to update the {@link CubeCamera.renderTarget | renderTarget}. + * @param renderer The current WebGL renderer + * @param scene The current scene + */ + update(renderer: WebGLRenderer, scene: Object3D): void; +} diff --git a/src-testing/src/cameras/OrthographicCamera.d.ts b/src-testing/src/cameras/OrthographicCamera.d.ts new file mode 100644 index 000000000..745d11416 --- /dev/null +++ b/src-testing/src/cameras/OrthographicCamera.d.ts @@ -0,0 +1,174 @@ +import { JSONMeta, Object3DJSON, Object3DJSONObject } from "../core/Object3D.js"; +import { Camera } from "./Camera.js"; + +export interface OrthographicCameraJSONObject extends Object3DJSONObject { + zoom: number; + left: number; + right: number; + top: number; + bottom: number; + near: number; + far: number; + + view?: { + enabled: boolean; + fullWidth: number; + fullHeight: number; + offsetX: number; + offsetY: number; + width: number; + height: number; + }; +} + +export interface OrthographicCameraJSON extends Object3DJSON { + object: OrthographicCameraJSONObject; +} + +/** + * Camera that uses {@link https://en.wikipedia.org/wiki/Orthographic_projection | orthographic projection}. + * In this projection mode, an object's size in the rendered image stays constant regardless of its distance from the camera. + * This can be useful for rendering 2D scenes and UI elements, amongst other things. + * @example + * ```typescript + * const camera = new THREE.OrthographicCamera(width / -2, width / 2, height / 2, height / -2, 1, 1000); + * scene.add(camera); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_camera | camera } + * @see Example: {@link https://threejs.org/examples/#webgl_interactive_cubes_ortho | interactive / cubes / ortho } + * @see Example: {@link https://threejs.org/examples/#webgl_materials_cubemap_dynamic | materials / cubemap / dynamic } + * @see Example: {@link https://threejs.org/examples/#webgl_postprocessing_advanced | postprocessing / advanced } + * @see Example: {@link https://threejs.org/examples/#webgl_postprocessing_dof2 | postprocessing / dof2 } + * @see Example: {@link https://threejs.org/examples/#webgl_postprocessing_godrays | postprocessing / godrays } + * @see Example: {@link https://threejs.org/examples/#webgl_rtt | rtt } + * @see Example: {@link https://threejs.org/examples/#webgl_shaders_tonemapping | shaders / tonemapping } + * @see Example: {@link https://threejs.org/examples/#webgl_shadowmap | shadowmap } + * @see {@link https://threejs.org/docs/index.html#api/en/cameras/OrthographicCamera | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/cameras/OrthographicCamera.js | Source} + */ +export class OrthographicCamera extends Camera { + /** + * Creates a new {@link OrthographicCamera}. + * @remarks Together these define the camera's {@link https://en.wikipedia.org/wiki/Viewing_frustum | viewing frustum}. + * @param left Camera frustum left plane. Default `-1`. + * @param right Camera frustum right plane. Default `1`. + * @param top Camera frustum top plane. Default `1`. + * @param bottom Camera frustum bottom plane. Default `-1`. + * @param near Camera frustum near plane. Default `0.1`. + * @param far Camera frustum far plane. Default `2000`. + */ + constructor(left?: number, right?: number, top?: number, bottom?: number, near?: number, far?: number); + + /** + * Read-only flag to check if a given object is of type {@link OrthographicCamera}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isOrthographicCamera: true; + + /** + * @override + * @defaultValue `OrthographicCamera` + */ + override readonly type: string | "OrthographicCamera"; + + /** + * Gets or sets the zoom factor of the camera. + * @defaultValue `1` + */ + zoom: number; + + /** + * Set by {@link setViewOffset | .setViewOffset()}. + * @defaultValue `null` + */ + view: null | { + enabled: boolean; + fullWidth: number; + fullHeight: number; + offsetX: number; + offsetY: number; + width: number; + height: number; + }; + + /** + * Camera frustum left plane. + * @remarks Expects a `Float` + * @defaultValue `-1` + */ + left: number; + + /** + * Camera frustum right plane. + * @remarks Expects a `Float` + * @defaultValue `1` + */ + right: number; + + /** + * Camera frustum top plane. + * @remarks Expects a `Float` + * @defaultValue `1` + */ + top: number; + + /** + * Camera frustum bottom plane. + * @remarks Expects a `Float`. + * @defaultValue `-1` + */ + bottom: number; + + /** + * Camera frustum near plane.`. + * @remarks The valid range is between `0` and the current value of the {@link far | .far} plane. + * @remarks Note that, unlike for the {@link THREE.PerspectiveCamera | PerspectiveCamera}, `0` is a valid value for an {@link THREE.OrthographicCamera | OrthographicCamera's} near plane. + * @remarks Expects a `Float` + * @defaultValue `0.1` + */ + near: number; + + /** + * Camera frustum far plane. + * @remarks Must be greater than the current value of {@link near | .near} plane. + * @remarks Expects a `Float` + * @defaultValue `2000` + */ + far: number; + + /** + * Updates the camera projection matrix + * @remarks Must be called after any change of parameters. + */ + updateProjectionMatrix(): void; + + /** + * Sets an offset in a larger {@link https://en.wikipedia.org/wiki/Viewing_frustum | viewing frustum} + * @remarks + * This is useful for multi-window or multi-monitor/multi-machine setups + * For an example on how to use it see {@link PerspectiveCamera.setViewOffset | PerspectiveCamera}. + * @see {@link THREE.PerspectiveCamera.setViewOffset | PerspectiveCamera}. + * @param fullWidth Full width of multiview setup Expects a `Float`. + * @param fullHeight Full height of multiview setup Expects a `Float`. + * @param x Horizontal offset of subcamera Expects a `Float`. + * @param y Vertical offset of subcamera Expects a `Float`. + * @param width Width of subcamera Expects a `Float`. + * @param height Height of subcamera Expects a `Float`. + */ + setViewOffset( + fullWidth: number, + fullHeight: number, + offsetX: number, + offsetY: number, + width: number, + height: number, + ): void; + + /** + * Removes any offset set by the {@link setViewOffset | .setViewOffset} method. + */ + clearViewOffset(): void; + + toJSON(meta?: JSONMeta): OrthographicCameraJSON; +} diff --git a/src-testing/src/cameras/PerspectiveCamera.d.ts b/src-testing/src/cameras/PerspectiveCamera.d.ts new file mode 100644 index 000000000..60567105f --- /dev/null +++ b/src-testing/src/cameras/PerspectiveCamera.d.ts @@ -0,0 +1,254 @@ +import { JSONMeta, Object3DJSON, Object3DJSONObject } from "../core/Object3D.js"; +import { Vector2 } from "../math/Vector2.js"; +import { Camera } from "./Camera.js"; + +export interface PerspectiveCameraJSONObject extends Object3DJSONObject { + fov: number; + zoom: number; + + near: number; + far: number; + focus: number; + + aspect: number; + + view?: { + enabled: boolean; + fullWidth: number; + fullHeight: number; + offsetX: number; + offsetY: number; + width: number; + height: number; + }; + + filmGauge: number; + filmOffset: number; +} + +export interface PerspectiveCameraJSON extends Object3DJSON { + object: PerspectiveCameraJSONObject; +} + +/** + * Camera that uses {@link https://en.wikipedia.org/wiki/Perspective_(graphical) | perspective projection}. + * This projection mode is designed to mimic the way the human eye sees + * @remarks + * It is the most common projection mode used for rendering a 3D scene. + * @example + * ```typescript + * const camera = new THREE.PerspectiveCamera(45, width / height, 1, 1000); + * scene.add(camera); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_animation_skinning_blending | animation / skinning / blending } + * @see Example: {@link https://threejs.org/examples/#webgl_animation_skinning_morph | animation / skinning / morph } + * @see Example: {@link https://threejs.org/examples/#webgl_effects_stereo | effects / stereo } + * @see Example: {@link https://threejs.org/examples/#webgl_interactive_cubes | interactive / cubes } + * @see Example: {@link https://threejs.org/examples/#webgl_loader_collada_skinning | loader / collada / skinning } + * @see {@link https://threejs.org/docs/index.html#api/en/cameras/PerspectiveCamera | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/cameras/PerspectiveCamera.js | Source} + */ +export class PerspectiveCamera extends Camera { + /** + * Creates a new {@link PerspectiveCamera}. + * @remarks Together these define the camera's {@link https://en.wikipedia.org/wiki/Viewing_frustum | viewing frustum}. + * @param fov Camera frustum vertical field of view. Default `50`. + * @param aspect Camera frustum aspect ratio. Default `1`. + * @param near Camera frustum near plane. Default `0.1`. + * @param far Camera frustum far plane. Default `2000`. + */ + constructor(fov?: number, aspect?: number, near?: number, far?: number); + + /** + * Read-only flag to check if a given object is of type {@link Camera}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isPerspectiveCamera: true; + + /** + * @override + * @defaultValue `PerspectiveCamera` + */ + override readonly type: string | "PerspectiveCamera"; + + /** + * Gets or sets the zoom factor of the camera. + * @defaultValue `1` + */ + zoom: number; + + /** + * Camera frustum vertical field of view, from bottom to top of view, in degrees. + * @remarks Expects a `Float` + * @defaultValue `50` + */ + fov: number; + + /** + * Camera frustum aspect ratio, usually the canvas width / canvas height. + * @remarks Expects a `Float` + * @defaultValue `1`, _(square canvas)_. + */ + aspect: number; + + /** + * Camera frustum near plane. + * @remarks The valid range is greater than `0` and less than the current value of the {@link far | .far} plane. + * @remarks Note that, unlike for the {@link THREE.OrthographicCamera | OrthographicCamera}, `0` is **not** a valid value for a {@link PerspectiveCamera |PerspectiveCamera's}. near plane. + * @defaultValue `0.1` + * @remarks Expects a `Float` + */ + near: number; + + /** + * Camera frustum far plane. + * @remarks Must be greater than the current value of {@link near | .near} plane. + * @remarks Expects a `Float` + * @defaultValue `2000` + */ + far: number; + + /** + * Object distance used for stereoscopy and depth-of-field effects. + * @remarks This parameter does not influence the projection matrix unless a {@link THREE.StereoCamera | StereoCamera} is being used. + * @remarks Expects a `Float` + * @defaultValue `10` + */ + focus: number; + + /** + * Frustum window specification or null. + * This is set using the {@link setViewOffset | .setViewOffset} method and cleared using {@link clearViewOffset | .clearViewOffset}. + * @defaultValue `null` + */ + view: null | { + enabled: boolean; + fullWidth: number; + fullHeight: number; + offsetX: number; + offsetY: number; + width: number; + height: number; + }; + + /** + * Film size used for the larger axis. + * This parameter does not influence the projection matrix unless {@link filmOffset | .filmOffset} is set to a nonzero value. + * @remarks Expects a `Float` + * @defaultValue `35`, _millimeters_. + */ + filmGauge: number; + + /** + * Horizontal off-center offset in the same unit as {@link filmGauge | .filmGauge}. + * @remarks Expects a `Float` + * @defaultValue `0` + */ + filmOffset: number; + + /** + * Returns the focal length of the current {@link .fov | fov} in respect to {@link filmGauge | .filmGauge}. + */ + getFocalLength(): number; + + /** + * Sets the FOV by focal length in respect to the current {@link filmGauge | .filmGauge}. + * @remarks By default, the focal length is specified for a `35mm` (full frame) camera. + * @param focalLength Expects a `Float` + */ + setFocalLength(focalLength: number): void; + + /** + * Returns the current vertical field of view angle in degrees considering {@link zoom | .zoom}. + */ + getEffectiveFOV(): number; + + /** + * Returns the width of the image on the film + * @remarks + * If {@link aspect | .aspect}. is greater than or equal to one (landscape format), the result equals {@link filmGauge | .filmGauge}. + */ + getFilmWidth(): number; + + /** + * Returns the height of the image on the film + * @remarks + * If {@link aspect | .aspect}. is less than or equal to one (portrait format), the result equals {@link filmGauge | .filmGauge}. + */ + getFilmHeight(): number; + + /** + * Computes the 2D bounds of the camera's viewable rectangle at a given distance along the viewing direction. + * Sets minTarget and maxTarget to the coordinates of the lower-left and upper-right corners of the view rectangle. + */ + getViewBounds(distance: number, minTarget: Vector2, maxTarget: Vector2): void; + + /** + * Computes the width and height of the camera's viewable rectangle at a given distance along the viewing direction. + * Copies the result into the target Vector2, where x is width and y is height. + */ + getViewSize(distance: number, target: Vector2): Vector2; + + /** + * Sets an offset in a larger frustum. + * @remarks + * This is useful for multi-window or multi-monitor/multi-machine setups. + * + * For example, if you have 3x2 monitors and each monitor is _1920x1080_ and + * the monitors are in grid like this + * ``` + * ┌───┬───┬───┐ + * │ A │ B │ C │ + * ├───┼───┼───┤ + * │ D │ E │ F │ + * └───┴───┴───┘ + * ``` + * then for each monitor you would call it like this + * ```typescript + * const w = 1920; + * const h = 1080; + * const fullWidth = w * 3; + * const fullHeight = h * 2; + * + * // Monitor - A + * camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 0, w, h ); + * // Monitor - B + * camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 0, w, h ); + * // Monitor - C + * camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 0, w, h ); + * // Monitor - D + * camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 1, w, h ); + * // Monitor - E + * camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 1, w, h ); + * // Monitor - F + * camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 1, w, h ); + * ``` + * Note there is no reason monitors have to be the same size or in a grid. + * @param fullWidth Full width of multiview setup Expects a `Float`. + * @param fullHeight Full height of multiview setup Expects a `Float`. + * @param x Horizontal offset of subcamera Expects a `Float`. + * @param y Vertical offset of subcamera Expects a `Float`. + * @param width Width of subcamera Expects a `Float`. + * @param height Height of subcamera Expects a `Float`. + */ + setViewOffset(fullWidth: number, fullHeight: number, x: number, y: number, width: number, height: number): void; + + /** + * Removes any offset set by the {@link setViewOffset | .setViewOffset} method. + */ + clearViewOffset(): void; + + /** + * Updates the camera projection matrix + * @remarks Must be called after any change of parameters. + */ + updateProjectionMatrix(): void; + + /** + * @deprecated Use {@link PerspectiveCamera.setFocalLength | .setFocalLength()} and {@link PerspectiveCamera.filmGauge | .filmGauge} instead. + */ + setLens(focalLength: number, frameHeight?: number): void; + + toJSON(meta?: JSONMeta): PerspectiveCameraJSON; +} diff --git a/src-testing/src/cameras/StereoCamera.d.ts b/src-testing/src/cameras/StereoCamera.d.ts new file mode 100644 index 000000000..3f359e3e0 --- /dev/null +++ b/src-testing/src/cameras/StereoCamera.d.ts @@ -0,0 +1,50 @@ +import { Camera } from "./Camera.js"; +import { PerspectiveCamera } from "./PerspectiveCamera.js"; + +/** + * Dual {@link PerspectiveCamera | PerspectiveCamera}s used for effects such as + * {@link https://en.wikipedia.org/wiki/Anaglyph_3D | 3D Anaglyph} or + * {@link https://en.wikipedia.org/wiki/parallax_barrier | Parallax Barrier}. + * @see Example: {@link https://threejs.org/examples/#webgl_effects_anaglyph | effects / anaglyph } + * @see Example: {@link https://threejs.org/examples/#webgl_effects_parallaxbarrier | effects / parallaxbarrier } + * @see Example: {@link https://threejs.org/examples/#webgl_effects_stereo | effects / stereo } + * @see {@link https://threejs.org/docs/index.html#api/en/cameras/StereoCamera | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/cameras/StereoCamera.js | Source} + */ +export class StereoCamera extends Camera { + constructor(); + + type: "StereoCamera"; + + /** + * @remarks Expects a `Float` + * @defaultValue `1` + */ + aspect: number; + + /** + * @remarks Expects a `Float` + * @defaultValue `0.064` + */ + eyeSep: number; + + /** + * The Left camera. + * A {@link PerspectiveCamera } added to {@link THREE.PerspectiveCamera.layers | layer 1} + * @remarks Objects to be rendered by the **left** camera must also be added to this layer. + */ + cameraL: PerspectiveCamera; + + /** + * The Right camera. + * A {@link PerspectiveCamera } added to {@link THREE.PerspectiveCamera.layers | layer 2} + * @remarks Objects to be rendered by the **right** camera must also be added to this layer. + */ + cameraR: PerspectiveCamera; + + /** + * Update the stereo cameras based on the camera passed in. + * @param camera + */ + update(camera: PerspectiveCamera): void; +} diff --git a/src-testing/src/constants.d.ts b/src-testing/src/constants.d.ts new file mode 100644 index 000000000..26ee6e2a5 --- /dev/null +++ b/src-testing/src/constants.d.ts @@ -0,0 +1,926 @@ +export const REVISION: string; + +// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent.button +export enum MOUSE { + LEFT = 0, + MIDDLE = 1, + RIGHT = 2, + ROTATE = 0, + DOLLY = 1, + PAN = 2, +} + +export enum TOUCH { + ROTATE = 0, + PAN = 1, + DOLLY_PAN = 2, + DOLLY_ROTATE = 3, +} + +// GL STATE CONSTANTS +export const CullFaceNone: 0; +export const CullFaceBack: 1; +export const CullFaceFront: 2; +export const CullFaceFrontBack: 3; +export type CullFace = typeof CullFaceNone | typeof CullFaceBack | typeof CullFaceFront | typeof CullFaceFrontBack; + +// Shadowing Type +export const BasicShadowMap: 0; +export const PCFShadowMap: 1; +export const PCFSoftShadowMap: 2; +export const VSMShadowMap: 3; +export type ShadowMapType = typeof BasicShadowMap | typeof PCFShadowMap | typeof PCFSoftShadowMap | typeof VSMShadowMap; + +// MATERIAL CONSTANTS + +// side +export const FrontSide: 0; +export const BackSide: 1; +export const DoubleSide: 2; +/** + * Defines which side of faces will be rendered - front, back or both. + * Default is {@link FrontSide}. + */ +export type Side = typeof FrontSide | typeof BackSide | typeof DoubleSide; + +// blending modes +export const NoBlending: 0; +export const NormalBlending: 1; +export const AdditiveBlending: 2; +export const SubtractiveBlending: 3; +export const MultiplyBlending: 4; +export const CustomBlending: 5; +export type Blending = + | typeof NoBlending + | typeof NormalBlending + | typeof AdditiveBlending + | typeof SubtractiveBlending + | typeof MultiplyBlending + | typeof CustomBlending; + +// custom blending equations +// (numbers start from 100 not to clash with other +// mappings to OpenGL constants defined in Texture.js) +export const AddEquation: 100; +export const SubtractEquation: 101; +export const ReverseSubtractEquation: 102; +export const MinEquation: 103; +export const MaxEquation: 104; +export type BlendingEquation = + | typeof AddEquation + | typeof SubtractEquation + | typeof ReverseSubtractEquation + | typeof MinEquation + | typeof MaxEquation; + +// custom blending factors +export const ZeroFactor: 200; +export const OneFactor: 201; +export const SrcColorFactor: 202; +export const OneMinusSrcColorFactor: 203; +export const SrcAlphaFactor: 204; +export const OneMinusSrcAlphaFactor: 205; +export const DstAlphaFactor: 206; +export const OneMinusDstAlphaFactor: 207; +export const DstColorFactor: 208; +export const OneMinusDstColorFactor: 209; +export const SrcAlphaSaturateFactor: 210; +export const ConstantColorFactor: 211; +export const OneMinusConstantColorFactor: 212; +export const ConstantAlphaFactor: 213; +export const OneMinusConstantAlphaFactor: 214; +export type BlendingDstFactor = + | typeof ZeroFactor + | typeof OneFactor + | typeof SrcColorFactor + | typeof OneMinusSrcColorFactor + | typeof SrcAlphaFactor + | typeof OneMinusSrcAlphaFactor + | typeof DstAlphaFactor + | typeof OneMinusDstAlphaFactor + | typeof DstColorFactor + | typeof OneMinusDstColorFactor + | typeof ConstantColorFactor + | typeof OneMinusConstantColorFactor + | typeof ConstantAlphaFactor + | typeof OneMinusConstantAlphaFactor; +export type BlendingSrcFactor = BlendingDstFactor | typeof SrcAlphaSaturateFactor; + +// depth modes +export const NeverDepth: 0; +export const AlwaysDepth: 1; +export const LessDepth: 2; +export const LessEqualDepth: 3; +export const EqualDepth: 4; +export const GreaterEqualDepth: 5; +export const GreaterDepth: 6; +export const NotEqualDepth: 7; +export type DepthModes = + | typeof NeverDepth + | typeof AlwaysDepth + | typeof LessDepth + | typeof LessEqualDepth + | typeof EqualDepth + | typeof GreaterEqualDepth + | typeof GreaterDepth + | typeof NotEqualDepth; + +// TEXTURE CONSTANTS +// Operations +export const MultiplyOperation: 0; +export const MixOperation: 1; +export const AddOperation: 2; +export type Combine = typeof MultiplyOperation | typeof MixOperation | typeof AddOperation; + +// Tone Mapping modes +export const NoToneMapping: 0; +export const LinearToneMapping: 1; +export const ReinhardToneMapping: 2; +export const CineonToneMapping: 3; +export const ACESFilmicToneMapping: 4; +export const CustomToneMapping: 5; +export const AgXToneMapping: 6; +export const NeutralToneMapping: 7; +export type ToneMapping = + | typeof NoToneMapping + | typeof LinearToneMapping + | typeof ReinhardToneMapping + | typeof CineonToneMapping + | typeof ACESFilmicToneMapping + | typeof CustomToneMapping + | typeof AgXToneMapping + | typeof NeutralToneMapping; + +// Bind modes +export const AttachedBindMode: "attached"; +export const DetachedBindMode: "detached"; +export type BindMode = typeof AttachedBindMode | typeof DetachedBindMode; + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +// Mapping modes + +/** + * Maps the texture using the mesh's UV coordinates. + * @remarks This is the _default_ value and behaver for Texture Mapping. + */ +export const UVMapping: 300; + +/** + * @remarks This is the _default_ value and behaver for Cube Texture Mapping. + */ +export const CubeReflectionMapping: 301; +export const CubeRefractionMapping: 302; +export const CubeUVReflectionMapping: 306; + +export const EquirectangularReflectionMapping: 303; +export const EquirectangularRefractionMapping: 304; + +/** + * Texture Mapping Modes for non-cube Textures + * @remarks {@link UVMapping} is the _default_ value and behaver for Texture Mapping. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + */ +export type Mapping = + | typeof UVMapping + | typeof EquirectangularReflectionMapping + | typeof EquirectangularRefractionMapping; + +/** + * Texture Mapping Modes for cube Textures + * @remarks {@link CubeReflectionMapping} is the _default_ value and behaver for Cube Texture Mapping. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + */ +export type CubeTextureMapping = + | typeof CubeReflectionMapping + | typeof CubeRefractionMapping + | typeof CubeUVReflectionMapping; + +/** + * Texture Mapping Modes for any type of Textures + * @see {@link Mapping} and {@link CubeTextureMapping} + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + */ +export type AnyMapping = Mapping | CubeTextureMapping; + +/////////////////////////////////////////////////////////////////////////////// +// Wrapping modes + +/** With {@link RepeatWrapping} the texture will simply repeat to infinity. */ +export const RepeatWrapping: 1000; +/** + * With {@link ClampToEdgeWrapping} the last pixel of the texture stretches to the edge of the mesh. + * @remarks This is the _default_ value and behaver for Wrapping Mapping. + */ +export const ClampToEdgeWrapping: 1001; +/** With {@link MirroredRepeatWrapping} the texture will repeats to infinity, mirroring on each repeat. */ +export const MirroredRepeatWrapping: 1002; + +/** + * Texture Wrapping Modes + * @remarks {@link ClampToEdgeWrapping} is the _default_ value and behaver for Wrapping Mapping. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + */ +export type Wrapping = typeof RepeatWrapping | typeof ClampToEdgeWrapping | typeof MirroredRepeatWrapping; + +/////////////////////////////////////////////////////////////////////////////// +// Filters + +/** {@link NearestFilter} returns the value of the texture element that is nearest (in Manhattan distance) to the specified texture coordinates. */ +export const NearestFilter: 1003; + +/** + * {@link NearestMipmapNearestFilter} chooses the mipmap that most closely matches the size of the pixel being textured + * and uses the {@link NearestFilter} criterion (the texel nearest to the center of the pixel) to produce a texture value. + */ +export const NearestMipmapNearestFilter: 1004; +/** + * {@link NearestMipmapNearestFilter} chooses the mipmap that most closely matches the size of the pixel being textured + * and uses the {@link NearestFilter} criterion (the texel nearest to the center of the pixel) to produce a texture value. + */ +export const NearestMipMapNearestFilter: 1004; + +/** + * {@link NearestMipmapLinearFilter} chooses the two mipmaps that most closely match the size of the pixel being textured + * and uses the {@link NearestFilter} criterion to produce a texture value from each mipmap. + * The final texture value is a weighted average of those two values. + */ +export const NearestMipmapLinearFilter: 1005; +/** + * {@link NearestMipMapLinearFilter} chooses the two mipmaps that most closely match the size of the pixel being textured + * and uses the {@link NearestFilter} criterion to produce a texture value from each mipmap. + * The final texture value is a weighted average of those two values. + */ +export const NearestMipMapLinearFilter: 1005; + +/** + * {@link LinearFilter} returns the weighted average of the four texture elements that are closest to the specified texture coordinates, + * and can include items wrapped or repeated from other parts of a texture, + * depending on the values of {@link THREE.Texture.wrapS | wrapS} and {@link THREE.Texture.wrapT | wrapT}, and on the exact mapping. + */ +export const LinearFilter: 1006; + +/** + * {@link LinearMipmapNearestFilter} chooses the mipmap that most closely matches the size of the pixel being textured and + * uses the {@link LinearFilter} criterion (a weighted average of the four texels that are closest to the center of the pixel) to produce a texture value. + */ +export const LinearMipmapNearestFilter: 1007; +/** + * {@link LinearMipMapNearestFilter} chooses the mipmap that most closely matches the size of the pixel being textured and + * uses the {@link LinearFilter} criterion (a weighted average of the four texels that are closest to the center of the pixel) to produce a texture value. + */ +export const LinearMipMapNearestFilter: 1007; + +/** + * {@link LinearMipmapLinearFilter} is the default and chooses the two mipmaps that most closely match the size of the pixel being textured and + * uses the {@link LinearFilter} criterion to produce a texture value from each mipmap. + * The final texture value is a weighted average of those two values. + */ +export const LinearMipmapLinearFilter: 1008; + +/** + * {@link LinearMipMapLinearFilter} is the default and chooses the two mipmaps that most closely match the size of the pixel being textured and + * uses the {@link LinearFilter} criterion to produce a texture value from each mipmap. + * The final texture value is a weighted average of those two values. + */ +export const LinearMipMapLinearFilter: 1008; + +/** + * Texture Magnification Filter Modes. + * For use with a texture's {@link THREE.Texture.magFilter | magFilter} property, + * these define the texture magnification function to be used when the pixel being textured maps to an area less than or equal to one texture element (texel). + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + * @see {@link https://sbcode.net/threejs/mipmaps/ | Texture Mipmaps (non-official)} + */ +export type MagnificationTextureFilter = typeof NearestFilter | typeof LinearFilter; + +/** + * Texture Minification Filter Modes. + * For use with a texture's {@link THREE.Texture.minFilter | minFilter} property, + * these define the texture minifying function that is used whenever the pixel being textured maps to an area greater than one texture element (texel). + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + * @see {@link https://sbcode.net/threejs/mipmaps/ | Texture Mipmaps (non-official)} + */ +export type MinificationTextureFilter = + | typeof NearestFilter + | typeof NearestMipmapNearestFilter + | typeof NearestMipMapNearestFilter + | typeof NearestMipmapLinearFilter + | typeof NearestMipMapLinearFilter + | typeof LinearFilter + | typeof LinearMipmapNearestFilter + | typeof LinearMipMapNearestFilter + | typeof LinearMipmapLinearFilter + | typeof LinearMipMapLinearFilter; + +/** + * Texture all Magnification and Minification Filter Modes. + * @see {@link MagnificationTextureFilter} and {@link MinificationTextureFilter} + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + * @see {@link https://sbcode.net/threejs/mipmaps/ | Texture Mipmaps (non-official)} + */ +export type TextureFilter = MagnificationTextureFilter | MinificationTextureFilter; + +/////////////////////////////////////////////////////////////////////////////// +// Data types + +export const UnsignedByteType: 1009; +export const ByteType: 1010; +export const ShortType: 1011; +export const UnsignedShortType: 1012; +export const IntType: 1013; +export const UnsignedIntType: 1014; +export const FloatType: 1015; +export const HalfFloatType: 1016; +export const UnsignedShort4444Type: 1017; +export const UnsignedShort5551Type: 1018; +export const UnsignedInt248Type: 1020; +export const UnsignedInt5999Type: 35902; + +export type AttributeGPUType = typeof FloatType | typeof IntType; + +/** + * Texture Types. + * @remarks Must correspond to the correct {@link PixelFormat | format}. + * @see {@link THREE.Texture.type} + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + */ +export type TextureDataType = + | typeof UnsignedByteType + | typeof ByteType + | typeof ShortType + | typeof UnsignedShortType + | typeof IntType + | typeof UnsignedIntType + | typeof FloatType + | typeof HalfFloatType + | typeof UnsignedShort4444Type + | typeof UnsignedShort5551Type + | typeof UnsignedInt248Type + | typeof UnsignedInt5999Type; + +/////////////////////////////////////////////////////////////////////////////// +// Pixel formats + +/** {@link AlphaFormat} discards the red, green and blue components and reads just the alpha component. */ +export const AlphaFormat: 1021; + +export const RGBFormat: 1022; + +/** {@link RGBAFormat} is the default and reads the red, green, blue and alpha components. */ +export const RGBAFormat: 1023; + +/** + * {@link LuminanceFormat} reads each element as a single luminance component. + * This is then converted to a floating point, clamped to the range `[0,1]`, and then assembled into an RGBA element by + * placing the luminance value in the red, green and blue channels, and attaching `1.0` to the alpha channel. + */ +export const LuminanceFormat: 1024; + +/** + * {@link LuminanceAlphaFormat} reads each element as a luminance/alpha double. + * The same process occurs as for the {@link LuminanceFormat}, except that the alpha channel may have values other than `1.0`. + */ +export const LuminanceAlphaFormat: 1025; + +/** + * {@link DepthFormat} reads each element as a single depth value, converts it to floating point, and clamps to the range `[0,1]`. + * @remarks This is the default for {@link THREE.DepthTexture}. + */ +export const DepthFormat: 1026; + +/** + * {@link DepthStencilFormat} reads each element is a pair of depth and stencil values. + * The depth component of the pair is interpreted as in {@link DepthFormat}. + * The stencil component is interpreted based on the depth + stencil internal format. + */ +export const DepthStencilFormat: 1027; + +/** + * {@link RedFormat} discards the green and blue components and reads just the red component. + */ +export const RedFormat: 1028; + +/** + * {@link RedIntegerFormat} discards the green and blue components and reads just the red component. + * The texels are read as integers instead of floating point. + */ +export const RedIntegerFormat: 1029; + +/** + * {@link RGFormat} discards the alpha, and blue components and reads the red, and green components. + */ +export const RGFormat: 1030; + +/** + * {@link RGIntegerFormat} discards the alpha, and blue components and reads the red, and green components. + * The texels are read as integers instead of floating point. + */ +export const RGIntegerFormat: 1031; + +/** + * {@link RGBIntegerFormat} discrads the alpha components and reads the red, green, and blue components. + */ +export const RGBIntegerFormat: 1032; + +/** + * {@link RGBAIntegerFormat} reads the red, green, blue and alpha component + * @remarks This is the default for {@link THREE.Texture}. + */ +export const RGBAIntegerFormat: 1033; + +/** + * All Texture Pixel Formats Modes. + * @remarks Note that the texture must have the correct {@link THREE.Texture.type} set, as described in {@link TextureDataType}. + * @see {@link WebGLRenderingContext.texImage2D} for details. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + */ +export type PixelFormat = + | typeof AlphaFormat + | typeof RGBFormat + | typeof RGBAFormat + | typeof LuminanceFormat + | typeof LuminanceAlphaFormat + | typeof DepthFormat + | typeof DepthStencilFormat + | typeof RedFormat + | typeof RedIntegerFormat + | typeof RGFormat + | typeof RGIntegerFormat + | typeof RGBIntegerFormat + | typeof RGBAIntegerFormat; + +/** + * All Texture Pixel Formats Modes for {@link THREE.DepthTexture}. + * @see {@link WebGLRenderingContext.texImage2D} for details. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + */ +export type DepthTexturePixelFormat = typeof DepthFormat | typeof DepthStencilFormat; + +/////////////////////////////////////////////////////////////////////////////// +// Compressed texture formats +// DDS / ST3C Compressed texture formats + +/** + * A DXT1-compressed image in an RGB image format. + * @remarks Require support for the _WEBGL_compressed_texture_s3tc_ WebGL extension. + */ +export const RGB_S3TC_DXT1_Format: 33776; +/** + * A DXT1-compressed image in an RGB image format with a simple on/off alpha value. + * @remarks Require support for the _WEBGL_compressed_texture_s3tc_ WebGL extension. + */ +export const RGBA_S3TC_DXT1_Format: 33777; +/** + * A DXT3-compressed image in an RGBA image format. Compared to a 32-bit RGBA texture, it offers 4:1 compression. + * @remarks Require support for the _WEBGL_compressed_texture_s3tc_ WebGL extension. + */ +export const RGBA_S3TC_DXT3_Format: 33778; +/** + * A DXT5-compressed image in an RGBA image format. It also provides a 4:1 compression, but differs to the DXT3 compression in how the alpha compression is done. + * @remarks Require support for the _WEBGL_compressed_texture_s3tc_ WebGL extension. + */ +export const RGBA_S3TC_DXT5_Format: 33779; + +// PVRTC compressed './texture formats + +/** + * RGB compression in 4-bit mode. One block for each 4×4 pixels. + * @remarks Require support for the _WEBGL_compressed_texture_pvrtc_ WebGL extension. + */ +export const RGB_PVRTC_4BPPV1_Format: 35840; +/** + * RGB compression in 2-bit mode. One block for each 8×4 pixels. + * @remarks Require support for the _WEBGL_compressed_texture_pvrtc_ WebGL extension. + */ +export const RGB_PVRTC_2BPPV1_Format: 35841; +/** + * RGBA compression in 4-bit mode. One block for each 4×4 pixels. + * @remarks Require support for the _WEBGL_compressed_texture_pvrtc_ WebGL extension. + */ +export const RGBA_PVRTC_4BPPV1_Format: 35842; +/** + * RGBA compression in 2-bit mode. One block for each 8×4 pixels. + * @remarks Require support for the _WEBGL_compressed_texture_pvrtc_ WebGL extension. + */ +export const RGBA_PVRTC_2BPPV1_Format: 35843; + +// ETC compressed texture formats + +/** + * @remarks Require support for the _WEBGL_compressed_texture_etc1_ (ETC1) or _WEBGL_compressed_texture_etc_ (ETC2) WebGL extension. + */ +export const RGB_ETC1_Format: 36196; +/** + * @remarks Require support for the _WEBGL_compressed_texture_etc1_ (ETC1) or _WEBGL_compressed_texture_etc_ (ETC2) WebGL extension. + */ +export const RGB_ETC2_Format: 37492; +/** + * @remarks Require support for the _WEBGL_compressed_texture_etc1_ (ETC1) or _WEBGL_compressed_texture_etc_ (ETC2) WebGL extension. + */ +export const RGBA_ETC2_EAC_Format: 37496; + +// ASTC compressed texture formats + +/** + * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. + */ +export const RGBA_ASTC_4x4_Format: 37808; +/** + * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. + */ +export const RGBA_ASTC_5x4_Format: 37809; +/** + * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. + */ +export const RGBA_ASTC_5x5_Format: 37810; +/** + * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. + */ +export const RGBA_ASTC_6x5_Format: 37811; +/** + * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. + */ +export const RGBA_ASTC_6x6_Format: 37812; +/** + * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. + */ +export const RGBA_ASTC_8x5_Format: 37813; +/** + * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. + */ +export const RGBA_ASTC_8x6_Format: 37814; +/** + * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. + */ +export const RGBA_ASTC_8x8_Format: 37815; +/** + * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. + */ +export const RGBA_ASTC_10x5_Format: 37816; +/** + * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. + */ +export const RGBA_ASTC_10x6_Format: 37817; +/** + * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. + */ +export const RGBA_ASTC_10x8_Format: 37818; +/** + * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. + */ +export const RGBA_ASTC_10x10_Format: 37819; +/** + * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. + */ +export const RGBA_ASTC_12x10_Format: 37820; +/** + * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. + */ +export const RGBA_ASTC_12x12_Format: 37821; + +// BPTC compressed texture formats + +/** + * @remarks Require support for the _EXT_texture_compression_bptc_ WebGL extension. + */ +export const RGBA_BPTC_Format: 36492; +export const RGB_BPTC_SIGNED_Format = 36494; +export const RGB_BPTC_UNSIGNED_Format = 36495; + +// RGTC compressed texture formats +export const RED_RGTC1_Format: 36283; +export const SIGNED_RED_RGTC1_Format: 36284; +export const RED_GREEN_RGTC2_Format: 36285; +export const SIGNED_RED_GREEN_RGTC2_Format: 36286; + +/** + * For use with a {@link THREE.CompressedTexture}'s {@link THREE.CompressedTexture.format | .format} property. + * @remarks Compressed Require support for correct WebGL extension. + */ +export type CompressedPixelFormat = + | typeof RGB_S3TC_DXT1_Format + | typeof RGBA_S3TC_DXT1_Format + | typeof RGBA_S3TC_DXT3_Format + | typeof RGBA_S3TC_DXT5_Format + | typeof RGB_PVRTC_4BPPV1_Format + | typeof RGB_PVRTC_2BPPV1_Format + | typeof RGBA_PVRTC_4BPPV1_Format + | typeof RGBA_PVRTC_2BPPV1_Format + | typeof RGB_ETC1_Format + | typeof RGB_ETC2_Format + | typeof RGBA_ETC2_EAC_Format + | typeof RGBA_ASTC_4x4_Format + | typeof RGBA_ASTC_5x4_Format + | typeof RGBA_ASTC_5x5_Format + | typeof RGBA_ASTC_6x5_Format + | typeof RGBA_ASTC_6x6_Format + | typeof RGBA_ASTC_8x5_Format + | typeof RGBA_ASTC_8x6_Format + | typeof RGBA_ASTC_8x8_Format + | typeof RGBA_ASTC_10x5_Format + | typeof RGBA_ASTC_10x6_Format + | typeof RGBA_ASTC_10x8_Format + | typeof RGBA_ASTC_10x10_Format + | typeof RGBA_ASTC_12x10_Format + | typeof RGBA_ASTC_12x12_Format + | typeof RGBA_BPTC_Format + | typeof RGB_BPTC_SIGNED_Format + | typeof RGB_BPTC_UNSIGNED_Format + | typeof RED_RGTC1_Format + | typeof SIGNED_RED_RGTC1_Format + | typeof RED_GREEN_RGTC2_Format + | typeof SIGNED_RED_GREEN_RGTC2_Format; + +/////////////////////////////////////////////////////////////////////////////// + +/** + * All Possible Texture Pixel Formats Modes. For any Type or SubType of Textures. + * @remarks Note that the texture must have the correct {@link THREE.Texture.type} set, as described in {@link TextureDataType}. + * @see {@link WebGLRenderingContext.texImage2D} for details. + * @see {@link PixelFormat} and {@link DepthTexturePixelFormat} and {@link CompressedPixelFormat} + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + */ +export type AnyPixelFormat = PixelFormat | DepthTexturePixelFormat | CompressedPixelFormat; + +/////////////////////////////////////////////////////////////////////////////// +// Loop styles for AnimationAction +export const LoopOnce: 2200; +export const LoopRepeat: 2201; +export const LoopPingPong: 2202; +export type AnimationActionLoopStyles = typeof LoopOnce | typeof LoopRepeat | typeof LoopPingPong; + +// Interpolation +export const InterpolateDiscrete: 2300; +export const InterpolateLinear: 2301; +export const InterpolateSmooth: 2302; +export type InterpolationModes = typeof InterpolateDiscrete | typeof InterpolateLinear | typeof InterpolateSmooth; + +// Interpolant ending modes +export const ZeroCurvatureEnding: 2400; +export const ZeroSlopeEnding: 2401; +export const WrapAroundEnding: 2402; +export type InterpolationEndingModes = typeof ZeroCurvatureEnding | typeof ZeroSlopeEnding | typeof WrapAroundEnding; + +// Animation blending modes +export const NormalAnimationBlendMode: 2500; +export const AdditiveAnimationBlendMode: 2501; +export type AnimationBlendMode = typeof NormalAnimationBlendMode | typeof AdditiveAnimationBlendMode; + +// Triangle Draw modes +export const TrianglesDrawMode: 0; +export const TriangleStripDrawMode: 1; +export const TriangleFanDrawMode: 2; +export type TrianglesDrawModes = typeof TrianglesDrawMode | typeof TriangleStripDrawMode | typeof TriangleFanDrawMode; + +/////////////////////////////////////////////////////////////////////////////// +// Depth packing strategies + +export const BasicDepthPacking: 3200; +export const RGBADepthPacking: 3201; +export const RGBDepthPacking: 3202; +export const RGDepthPacking: 3203; +export type DepthPackingStrategies = + | typeof BasicDepthPacking + | typeof RGBADepthPacking + | typeof RGBDepthPacking + | typeof RGDepthPacking; + +/////////////////////////////////////////////////////////////////////////////// +// Normal Map types + +export const TangentSpaceNormalMap: 0; +export const ObjectSpaceNormalMap: 1; +export type NormalMapTypes = typeof TangentSpaceNormalMap | typeof ObjectSpaceNormalMap; + +export const NoColorSpace: ""; +export const SRGBColorSpace: "srgb"; +export const LinearSRGBColorSpace: "srgb-linear"; +export const DisplayP3ColorSpace: "display-p3"; +export const LinearDisplayP3ColorSpace = "display-p3-linear"; +export type ColorSpace = + | typeof NoColorSpace + | typeof SRGBColorSpace + | typeof LinearSRGBColorSpace + | typeof DisplayP3ColorSpace + | typeof LinearDisplayP3ColorSpace; + +export const LinearTransfer: "linear"; +export const SRGBTransfer: "srgb"; +export type ColorSpaceTransfer = typeof LinearTransfer | typeof SRGBTransfer; + +export const Rec709Primaries: "rec709"; +export const P3Primaries: "p3"; +export type ColorSpacePrimaries = typeof Rec709Primaries | typeof P3Primaries; + +// Stencil Op types +export const ZeroStencilOp: 0; +export const KeepStencilOp: 7680; +export const ReplaceStencilOp: 7681; +export const IncrementStencilOp: 7682; +export const DecrementStencilOp: 7283; +export const IncrementWrapStencilOp: 34055; +export const DecrementWrapStencilOp: 34056; +export const InvertStencilOp: 5386; +export type StencilOp = + | typeof ZeroStencilOp + | typeof KeepStencilOp + | typeof ReplaceStencilOp + | typeof IncrementStencilOp + | typeof DecrementStencilOp + | typeof IncrementWrapStencilOp + | typeof DecrementWrapStencilOp + | typeof InvertStencilOp; + +// Stencil Func types +export const NeverStencilFunc: 512; +export const LessStencilFunc: 513; +export const EqualStencilFunc: 514; +export const LessEqualStencilFunc: 515; +export const GreaterStencilFunc: 516; +export const NotEqualStencilFunc: 517; +export const GreaterEqualStencilFunc: 518; +export const AlwaysStencilFunc: 519; +export type StencilFunc = + | typeof NeverStencilFunc + | typeof LessStencilFunc + | typeof EqualStencilFunc + | typeof LessEqualStencilFunc + | typeof GreaterStencilFunc + | typeof NotEqualStencilFunc + | typeof GreaterEqualStencilFunc + | typeof AlwaysStencilFunc; + +export const NeverCompare: 512; +export const LessCompare: 513; +export const EqualCompare: 514; +export const LessEqualCompare: 515; +export const GreaterCompare: 516; +export const NotEqualCompare: 517; +export const GreaterEqualCompare: 518; +export const AlwaysCompare: 519; +export type TextureComparisonFunction = + | typeof NeverCompare + | typeof LessCompare + | typeof EqualCompare + | typeof LessEqualCompare + | typeof GreaterCompare + | typeof NotEqualCompare + | typeof GreaterEqualCompare + | typeof AlwaysCompare; + +// usage types +export const StaticDrawUsage: 35044; +export const DynamicDrawUsage: 35048; +export const StreamDrawUsage: 35040; +export const StaticReadUsage: 35045; +export const DynamicReadUsage: 35049; +export const StreamReadUsage: 35041; +export const StaticCopyUsage: 35046; +export const DynamicCopyUsage: 35050; +export const StreamCopyUsage: 35042; +export type Usage = + | typeof StaticDrawUsage + | typeof DynamicDrawUsage + | typeof StreamDrawUsage + | typeof StaticReadUsage + | typeof DynamicReadUsage + | typeof StreamReadUsage + | typeof StaticCopyUsage + | typeof DynamicCopyUsage + | typeof StreamCopyUsage; + +export const GLSL1: "100"; +export const GLSL3: "300 es"; +export type GLSLVersion = typeof GLSL1 | typeof GLSL3; + +export const WebGLCoordinateSystem: 2000; +export const WebGPUCoordinateSystem: 2001; +export type CoordinateSystem = typeof WebGLCoordinateSystem | typeof WebGPUCoordinateSystem; + +/////////////////////////////////////////////////////////////////////////////// +// Texture - Internal Pixel Formats + +/** + * For use with a texture's {@link THREE.Texture.internalFormat} property, these define how elements of a {@link THREE.Texture}, or texels, are stored on the GPU. + * - `R8` stores the red component on 8 bits. + * - `R8_SNORM` stores the red component on 8 bits. The component is stored as normalized. + * - `R8I` stores the red component on 8 bits. The component is stored as an integer. + * - `R8UI` stores the red component on 8 bits. The component is stored as an unsigned integer. + * - `R16I` stores the red component on 16 bits. The component is stored as an integer. + * - `R16UI` stores the red component on 16 bits. The component is stored as an unsigned integer. + * - `R16F` stores the red component on 16 bits. The component is stored as floating point. + * - `R32I` stores the red component on 32 bits. The component is stored as an integer. + * - `R32UI` stores the red component on 32 bits. The component is stored as an unsigned integer. + * - `R32F` stores the red component on 32 bits. The component is stored as floating point. + * - `RG8` stores the red and green components on 8 bits each. + * - `RG8_SNORM` stores the red and green components on 8 bits each. Every component is stored as normalized. + * - `RG8I` stores the red and green components on 8 bits each. Every component is stored as an integer. + * - `RG8UI` stores the red and green components on 8 bits each. Every component is stored as an unsigned integer. + * - `RG16I` stores the red and green components on 16 bits each. Every component is stored as an integer. + * - `RG16UI` stores the red and green components on 16 bits each. Every component is stored as an unsigned integer. + * - `RG16F` stores the red and green components on 16 bits each. Every component is stored as floating point. + * - `RG32I` stores the red and green components on 32 bits each. Every component is stored as an integer. + * - `RG32UI` stores the red and green components on 32 bits. Every component is stored as an unsigned integer. + * - `RG32F` stores the red and green components on 32 bits. Every component is stored as floating point. + * - `RGB8` stores the red, green, and blue components on 8 bits each. RGB8_SNORM` stores the red, green, and blue components on 8 bits each. Every component is stored as normalized. + * - `RGB8I` stores the red, green, and blue components on 8 bits each. Every component is stored as an integer. + * - `RGB8UI` stores the red, green, and blue components on 8 bits each. Every component is stored as an unsigned integer. + * - `RGB16I` stores the red, green, and blue components on 16 bits each. Every component is stored as an integer. + * - `RGB16UI` stores the red, green, and blue components on 16 bits each. Every component is stored as an unsigned integer. + * - `RGB16F` stores the red, green, and blue components on 16 bits each. Every component is stored as floating point + * - `RGB32I` stores the red, green, and blue components on 32 bits each. Every component is stored as an integer. + * - `RGB32UI` stores the red, green, and blue components on 32 bits each. Every component is stored as an unsigned integer. + * - `RGB32F` stores the red, green, and blue components on 32 bits each. Every component is stored as floating point + * - `R11F_G11F_B10F` stores the red, green, and blue components respectively on 11 bits, 11 bits, and 10bits. Every component is stored as floating point. + * - `RGB565` stores the red, green, and blue components respectively on 5 bits, 6 bits, and 5 bits. + * - `RGB9_E5` stores the red, green, and blue components on 9 bits each. + * - `RGBA8` stores the red, green, blue, and alpha components on 8 bits each. + * - `RGBA8_SNORM` stores the red, green, blue, and alpha components on 8 bits. Every component is stored as normalized. + * - `RGBA8I` stores the red, green, blue, and alpha components on 8 bits each. Every component is stored as an integer. + * - `RGBA8UI` stores the red, green, blue, and alpha components on 8 bits. Every component is stored as an unsigned integer. + * - `RGBA16I` stores the red, green, blue, and alpha components on 16 bits. Every component is stored as an integer. + * - `RGBA16UI` stores the red, green, blue, and alpha components on 16 bits. Every component is stored as an unsigned integer. + * - `RGBA16F` stores the red, green, blue, and alpha components on 16 bits. Every component is stored as floating point. + * - `RGBA32I` stores the red, green, blue, and alpha components on 32 bits. Every component is stored as an integer. + * - `RGBA32UI` stores the red, green, blue, and alpha components on 32 bits. Every component is stored as an unsigned integer. + * - `RGBA32F` stores the red, green, blue, and alpha components on 32 bits. Every component is stored as floating point. + * - `RGB5_A1` stores the red, green, blue, and alpha components respectively on 5 bits, 5 bits, 5 bits, and 1 bit. + * - `RGB10_A2` stores the red, green, blue, and alpha components respectively on 10 bits, 10 bits, 10 bits and 2 bits. + * - `RGB10_A2UI` stores the red, green, blue, and alpha components respectively on 10 bits, 10 bits, 10 bits and 2 bits. Every component is stored as an unsigned integer. + * - `SRGB8` stores the red, green, and blue components on 8 bits each. + * - `SRGB8_ALPHA8` stores the red, green, blue, and alpha components on 8 bits each. + * - `DEPTH_COMPONENT16` stores the depth component on 16bits. + * - `DEPTH_COMPONENT24` stores the depth component on 24bits. + * - `DEPTH_COMPONENT32F` stores the depth component on 32bits. The component is stored as floating point. + * - `DEPTH24_STENCIL8` stores the depth, and stencil components respectively on 24 bits and 8 bits. The stencil component is stored as an unsigned integer. + * - `DEPTH32F_STENCIL8` stores the depth, and stencil components respectively on 32 bits and 8 bits. The depth component is stored as floating point, and the stencil component as an unsigned integer. + * @remark Note that the texture must have the correct {@link THREE.Texture.type} set, as well as the correct {@link THREE.Texture.format}. + * @see {@link WebGLRenderingContext.texImage2D} and {@link WebGLRenderingContext.texImage3D} for more details regarding the possible combination + * of {@link THREE.Texture.format}, {@link THREE.Texture.internalFormat}, and {@link THREE.Texture.type}. + * @see {@link https://registry.khronos.org/webgl/specs/latest/2.0/ | WebGL2 Specification} and + * {@link https://registry.khronos.org/OpenGL/specs/es/3.0/es_spec_3.0.pdf | OpenGL ES 3.0 Specification} For more in-depth information regarding internal formats. + */ +export type PixelFormatGPU = + | "ALPHA" + | "RGB" + | "RGBA" + | "LUMINANCE" + | "LUMINANCE_ALPHA" + | "RED_INTEGER" + | "R8" + | "R8_SNORM" + | "R8I" + | "R8UI" + | "R16I" + | "R16UI" + | "R16F" + | "R32I" + | "R32UI" + | "R32F" + | "RG8" + | "RG8_SNORM" + | "RG8I" + | "RG8UI" + | "RG16I" + | "RG16UI" + | "RG16F" + | "RG32I" + | "RG32UI" + | "RG32F" + | "RGB565" + | "RGB8" + | "RGB8_SNORM" + | "RGB8I" + | "RGB8UI" + | "RGB16I" + | "RGB16UI" + | "RGB16F" + | "RGB32I" + | "RGB32UI" + | "RGB32F" + | "RGB9_E5" + | "SRGB8" + | "R11F_G11F_B10F" + | "RGBA4" + | "RGBA8" + | "RGBA8_SNORM" + | "RGBA8I" + | "RGBA8UI" + | "RGBA16I" + | "RGBA16UI" + | "RGBA16F" + | "RGBA32I" + | "RGBA32UI" + | "RGBA32F" + | "RGB5_A1" + | "RGB10_A2" + | "RGB10_A2UI" + | "SRGB8_ALPHA8" + | "SRGB8" + | "DEPTH_COMPONENT16" + | "DEPTH_COMPONENT24" + | "DEPTH_COMPONENT32F" + | "DEPTH24_STENCIL8" + | "DEPTH32F_STENCIL8"; diff --git a/src-testing/src/core/BufferAttribute.d.ts b/src-testing/src/core/BufferAttribute.d.ts new file mode 100644 index 000000000..c0dbba8e0 --- /dev/null +++ b/src-testing/src/core/BufferAttribute.d.ts @@ -0,0 +1,637 @@ +import { AttributeGPUType, Usage } from "../constants.js"; +import { Matrix3 } from "../math/Matrix3.js"; +import { Matrix4 } from "../math/Matrix4.js"; + +export type TypedArray = + | Int8Array + | Uint8Array + | Uint8ClampedArray + | Int16Array + | Uint16Array + | Int32Array + | Uint32Array + | Float32Array + | Float64Array; + +export interface BufferAttributeJSON { + itemSize: number; + type: string; + array: number[]; + normalized: boolean; + + name?: string; + usage?: Usage; +} + +/** + * This class stores data for an attribute (such as vertex positions, face indices, normals, colors, UVs, and any custom attributes ) + * associated with a {@link THREE.BufferGeometry | BufferGeometry}, which allows for more efficient passing of data to the GPU + * @remarks + * When working with _vector-like_ data, the _`.fromBufferAttribute( attribute, index )`_ helper methods on + * {@link THREE.Vector2.fromBufferAttribute | Vector2}, + * {@link THREE.Vector3.fromBufferAttribute | Vector3}, + * {@link THREE.Vector4.fromBufferAttribute | Vector4}, and + * {@link THREE.Color.fromBufferAttribute | Color} classes may be helpful. + * @see {@link THREE.BufferGeometry | BufferGeometry} for details and a usage examples. + * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry | WebGL / BufferGeometry - Clean up Memory} + * @see {@link https://threejs.org/docs/index.html#api/en/core/BufferAttribute | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} + */ +export class BufferAttribute { + /** + * This creates a new {@link THREE.GLBufferAttribute | GLBufferAttribute} object. + * @param array Must be a `TypedArray`. Used to instantiate the buffer. + * This array should have `itemSize * numVertices` elements, where numVertices is the number of vertices in the associated {@link THREE.BufferGeometry | BufferGeometry}. + * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. + * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), + * then itemSize should be `3`. + * @param normalized Applies to integer data only. + * Indicates how the underlying data in the buffer maps to the values in the GLSL code. + * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, + * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. + * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. + * If normalized is false, the values will be converted to floats unmodified, + * i.e. `32767` becomes `32767.0f`. + * Default `false`. + * @throws `TypeError` When the {@link array} is not a `TypedArray`; + */ + constructor(array: TypedArray, itemSize: number, normalized?: boolean); + + /** + * Optional name for this attribute instance. + * @defaultValue '' + */ + name: string; + + /** + * The {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray | TypedArray} holding data stored in the buffer. + * @returns `TypedArray` + */ + array: TypedArray; + + /** + * The length of vectors that are being stored in the {@link BufferAttribute.array | array}. + * @remarks Expects a `Integer` + */ + itemSize: number; + + /** + * Defines the intended usage pattern of the data store for optimization purposes. + * Corresponds to the {@link BufferAttribute.usage | usage} parameter of + * {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferData | WebGLRenderingContext.bufferData}. + * @remarks + * After the initial use of a buffer, its usage cannot be changed. Instead, instantiate a new one and set the desired usage before the next render. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/BufferAttributeUsage | Buffer Attribute Usage Constants} for all possible values. + * @see {@link BufferAttribute.setUsage | setUsage} + * @defaultValue {@link THREE.StaticDrawUsage | THREE.StaticDrawUsage}. + */ + usage: Usage; + + /** + * Configures the bound GPU type for use in shaders. Either {@link FloatType} or {@link IntType}, default is {@link FloatType}. + * + * Note: this only has an effect for integer arrays and is not configurable for float arrays. For lower precision + * float types, see https://threejs.org/docs/#api/en/core/bufferAttributeTypes/BufferAttributeTypes. + */ + gpuType: AttributeGPUType; + + /** + * This can be used to only update some components of stored vectors (for example, just the component related to color). + * @defaultValue `{ offset: number = 0; count: number = -1 }` + * @deprecated Will be removed in r169. Use "addUpdateRange()" instead. + */ + updateRange: { + /** + * Position at which to start update. + * @defaultValue `0` + */ + offset: number; + /** @defaultValue `-1`, which means don't use update ranges. */ + count: number; + }; + + /** + * This can be used to only update some components of stored vectors (for example, just the component related to + * color). Use the {@link .addUpdateRange} function to add ranges to this array. + */ + updateRanges: Array<{ + /** + * Position at which to start update. + */ + start: number; + /** + * The number of components to update. + */ + count: number; + }>; + + /** + * A version number, incremented every time the {@link BufferAttribute.needsUpdate | needsUpdate} property is set to true. + * @remarks Expects a `Integer` + * @defaultValue `0` + */ + version: number; + + /** + * Indicates how the underlying data in the buffer maps to the values in the GLSL shader code. + * @see `constructor` above for details. + * @defaultValue `false` + */ + normalized: boolean; + + /** + * Represents the number of items this buffer attribute stores. It is internally computed by dividing the + * {@link BufferAttribute.array | array}'s length by the {@link BufferAttribute.itemSize | itemSize}. Read-only + * property. + */ + readonly count: number; + + /** + * Flag to indicate that this attribute has changed and should be re-sent to the GPU. + * Set this to true when you modify the value of the array. + * @remarks Setting this to true also increments the {@link BufferAttribute.version | version}. + * @remarks _set-only property_. + */ + set needsUpdate(value: boolean); + + /** + * Read-only flag to check if a given object is of type {@link BufferAttribute}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isBufferAttribute: true; + + /** + * A callback function that is executed after the Renderer has transferred the attribute array data to the GPU. + */ + onUploadCallback: () => void; + + /** + * Sets the value of the {@link onUploadCallback} property. + * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry | WebGL / BufferGeometry} this is used to free memory after the buffer has been transferred to the GPU. + * @see {@link onUploadCallback} + * @param callback function that is executed after the Renderer has transferred the attribute array data to the GPU. + */ + onUpload(callback: () => void): this; + + /** + * Set {@link BufferAttribute.usage | usage} + * @remarks + * After the initial use of a buffer, its usage cannot be changed. Instead, instantiate a new one and set the desired usage before the next render. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/BufferAttributeUsage | Buffer Attribute Usage Constants} for all possible values. + * @see {@link BufferAttribute.usage | usage} + * @param value Corresponds to the {@link BufferAttribute.usage | usage} parameter of + * {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferData | WebGLRenderingContext.bufferData}. + */ + setUsage(usage: Usage): this; + + /** + * Adds a range of data in the data array to be updated on the GPU. Adds an object describing the range to the + * {@link .updateRanges} array. + */ + addUpdateRange(start: number, count: number): void; + + /** + * Clears the {@link .updateRanges} array. + */ + clearUpdateRanges(): void; + + /** + * @returns a copy of this {@link BufferAttribute}. + */ + clone(): BufferAttribute; + + /** + * Copies another {@link BufferAttribute} to this {@link BufferAttribute}. + * @param bufferAttribute + */ + copy(source: BufferAttribute): this; + + /** + * Copy a vector from bufferAttribute[index2] to {@link BufferAttribute.array | array}[index1]. + * @param index1 + * @param bufferAttribute + * @param index2 + */ + copyAt(index1: number, attribute: BufferAttribute, index2: number): this; + + /** + * Copy the array given here (which can be a normal array or `TypedArray`) into {@link BufferAttribute.array | array}. + * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set | TypedArray.set} for notes on requirements if copying a `TypedArray`. + */ + copyArray(array: ArrayLike): this; + + /** + * Applies matrix {@link Matrix3 | m} to every Vector3 element of this {@link BufferAttribute}. + * @param m + */ + applyMatrix3(m: Matrix3): this; + + /** + * Applies matrix {@link Matrix4 | m} to every Vector3 element of this {@link BufferAttribute}. + * @param m + */ + applyMatrix4(m: Matrix4): this; + + /** + * Applies normal matrix {@link Matrix3 | m} to every Vector3 element of this {@link BufferAttribute}. + * @param m + */ + applyNormalMatrix(m: Matrix3): this; + + /** + * Applies matrix {@link Matrix4 | m} to every Vector3 element of this {@link BufferAttribute}, interpreting the elements as a direction vectors. + * @param m + */ + transformDirection(m: Matrix4): this; + + /** + * Calls {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set | TypedArray.set}( {@link value}, {@link offset} ) + * on the {@link BufferAttribute.array | array}. + * @param value {@link Array | Array} or `TypedArray` from which to copy values. + * @param offset index of the {@link BufferAttribute.array | array} at which to start copying. Expects a `Integer`. Default `0`. + * @throws `RangeError` When {@link offset} is negative or is too large. + */ + set(value: ArrayLike | ArrayBufferView, offset?: number): this; + + /** + * Returns the given component of the vector at the given index. + */ + getComponent(index: number, component: number): number; + + /** + * Sets the given component of the vector at the given index. + */ + setComponent(index: number, component: number, value: number): void; + + /** + * Returns the x component of the vector at the given index. + * @param index Expects a `Integer` + */ + getX(index: number): number; + + /** + * Sets the x component of the vector at the given index. + * @param index Expects a `Integer` + * @param x + */ + setX(index: number, x: number): this; + + /** + * Returns the y component of the vector at the given index. + * @param index Expects a `Integer` + */ + getY(index: number): number; + + /** + * Sets the y component of the vector at the given index. + * @param index Expects a `Integer` + * @param y + */ + setY(index: number, y: number): this; + + /** + * Returns the z component of the vector at the given index. + * @param index Expects a `Integer` + */ + getZ(index: number): number; + + /** + * Sets the z component of the vector at the given index. + * @param index Expects a `Integer` + * @param z + */ + setZ(index: number, z: number): this; + + /** + * Returns the w component of the vector at the given index. + * @param index Expects a `Integer` + */ + getW(index: number): number; + + /** + * Sets the w component of the vector at the given index. + * @param index Expects a `Integer` + * @param w + */ + setW(index: number, z: number): this; + + /** + * Sets the x and y components of the vector at the given index. + * @param index Expects a `Integer` + * @param x + * @param y + */ + setXY(index: number, x: number, y: number): this; + + /** + * Sets the x, y and z components of the vector at the given index. + * @param index Expects a `Integer` + * @param x + * @param y + * @param z + */ + setXYZ(index: number, x: number, y: number, z: number): this; + + /** + * Sets the x, y, z and w components of the vector at the given index. + * @param index Expects a `Integer` + * @param x + * @param y + * @param z + * @param w + */ + setXYZW(index: number, x: number, y: number, z: number, w: number): this; + + /** + * Convert this object to three.js to the `data.attributes` part of {@link https://github.com/mrdoob/three.js/wiki/JSON-Geometry-format-4 | JSON Geometry format v4}, + */ + toJSON(): BufferAttributeJSON; +} + +/** + * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array: Int8Array} + * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} + * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. + * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} + */ +export class Int8BufferAttribute extends BufferAttribute { + /** + * This creates a new {@link THREE.Int8BufferAttribute | Int8BufferAttribute} object. + * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Int8Array`. + * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. + * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. + * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), + * then itemSize should be `3`. + * @param normalized Applies to integer data only. + * Indicates how the underlying data in the buffer maps to the values in the GLSL code. + * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, + * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. + * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. + * If normalized is false, the values will be converted to floats unmodified, + * i.e. `32767` becomes `32767.0f`. + * Default `false`. + */ + constructor( + array: Iterable | ArrayLike | ArrayBuffer | number, + itemSize: number, + normalized?: boolean, + ); +} + +/** + * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array: Uint8Array} + * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} + * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. + * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} + */ +export class Uint8BufferAttribute extends BufferAttribute { + /** + * This creates a new {@link THREE.Uint8BufferAttribute | Uint8BufferAttribute} object. + * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Uint8Array`. + * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. + * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. + * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), + * then itemSize should be `3`. + * @param normalized Applies to integer data only. + * Indicates how the underlying data in the buffer maps to the values in the GLSL code. + * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, + * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. + * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. + * If normalized is false, the values will be converted to floats unmodified, + * i.e. `32767` becomes `32767.0f`. + * Default `false`. + * @see {@link THREE.BufferAttribute | BufferAttribute} + */ + constructor( + array: Iterable | ArrayLike | ArrayBuffer | number, + itemSize: number, + normalized?: boolean, + ); +} + +/** + * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray: Uint8ClampedArray} + * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} + * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. + * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} + */ +export class Uint8ClampedBufferAttribute extends BufferAttribute { + /** + * This creates a new {@link THREE.Uint8ClampedBufferAttribute | Uint8ClampedBufferAttribute} object. + * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Uint8ClampedArray`. + * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. + * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. + * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), + * then itemSize should be `3`. + * @param normalized Applies to integer data only. + * Indicates how the underlying data in the buffer maps to the values in the GLSL code. + * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, + * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. + * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. + * If normalized is false, the values will be converted to floats unmodified, + * i.e. `32767` becomes `32767.0f`. + * Default `false`. + * @see {@link THREE.BufferAttribute | BufferAttribute} + */ + constructor( + array: Iterable | ArrayLike | ArrayBuffer | number, + itemSize: number, + normalized?: boolean, + ); +} + +/** + * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int16Array: Int16Array} + * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} + * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. + * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} + */ +export class Int16BufferAttribute extends BufferAttribute { + /** + * This creates a new {@link THREE.Int16BufferAttribute | Int16BufferAttribute} object. + * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Int16Array`. + * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. + * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. + * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), + * then itemSize should be `3`. + * @param normalized Applies to integer data only. + * Indicates how the underlying data in the buffer maps to the values in the GLSL code. + * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, + * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. + * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. + * If normalized is false, the values will be converted to floats unmodified, + * i.e. `32767` becomes `32767.0f`. + * Default `false`. + * @see {@link THREE.BufferAttribute | BufferAttribute} + */ + constructor( + array: Iterable | ArrayLike | ArrayBuffer | number, + itemSize: number, + normalized?: boolean, + ); +} + +/** + * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array: Uint16Array} + * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} + * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. + * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} + */ +export class Uint16BufferAttribute extends BufferAttribute { + /** + * This creates a new {@link THREE.Uint16BufferAttribute | Uint16BufferAttribute} object. + * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Uint16Array`. + * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. + * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. + * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), + * then itemSize should be `3`. + * @param normalized Applies to integer data only. + * Indicates how the underlying data in the buffer maps to the values in the GLSL code. + * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, + * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. + * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. + * If normalized is false, the values will be converted to floats unmodified, + * i.e. `32767` becomes `32767.0f`. + * Default `false`. + * @see {@link THREE.BufferAttribute | BufferAttribute} + */ + constructor( + array: Iterable | ArrayLike | ArrayBuffer | number, + itemSize: number, + normalized?: boolean, + ); +} + +/** + * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array: Int32Array} + * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} + * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. + * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} + */ +export class Int32BufferAttribute extends BufferAttribute { + /** + * This creates a new {@link THREE.Int32BufferAttribute | Int32BufferAttribute} object. + * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Int32Array`. + * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. + * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. + * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), + * then itemSize should be `3`. + * @param normalized Applies to integer data only. + * Indicates how the underlying data in the buffer maps to the values in the GLSL code. + * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, + * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. + * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. + * If normalized is false, the values will be converted to floats unmodified, + * i.e. `32767` becomes `32767.0f`. + * Default `false`. + * @see {@link THREE.BufferAttribute | BufferAttribute} + */ + constructor( + array: Iterable | ArrayLike | ArrayBuffer | number, + itemSize: number, + normalized?: boolean, + ); +} + +/** + * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array: Uint32Array} + * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} + * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. + * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} + */ +export class Uint32BufferAttribute extends BufferAttribute { + /** + * This creates a new {@link THREE.Uint32BufferAttribute | Uint32BufferAttribute} object. + * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Uint32Array`. + * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. + * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. + * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), + * then itemSize should be `3`. + * @param normalized Applies to integer data only. + * Indicates how the underlying data in the buffer maps to the values in the GLSL code. + * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, + * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. + * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. + * If normalized is false, the values will be converted to floats unmodified, + * i.e. `32767` becomes `32767.0f`. + * Default `false`. + * @see {@link THREE.BufferAttribute | BufferAttribute} + */ + constructor( + array: Iterable | ArrayLike | ArrayBuffer | number, + itemSize: number, + normalized?: boolean, + ); +} + +/** + * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array: Uint16Array} + * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} + * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. + * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} + */ +export class Float16BufferAttribute extends BufferAttribute { + /** + * This creates a new {@link THREE.Float16BufferAttribute | Float16BufferAttribute} object. + * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Uint16Array`. + * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. + * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. + * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), + * then itemSize should be `3`. + * @param normalized Applies to integer data only. + * Indicates how the underlying data in the buffer maps to the values in the GLSL code. + * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, + * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. + * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. + * If normalized is false, the values will be converted to floats unmodified, + * i.e. `32767` becomes `32767.0f`. + * Default `false`. + * @see {@link THREE.BufferAttribute | BufferAttribute} + */ + constructor( + array: Iterable | ArrayLike | ArrayBuffer | number, + itemSize: number, + normalized?: boolean, + ); +} + +/** + * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array: Float32Array} + * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} + * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. + * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} + */ +export class Float32BufferAttribute extends BufferAttribute { + /** + * This creates a new {@link THREE.Float32BufferAttribute | Float32BufferAttribute} object. + * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Float32Array`. + * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. + * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. + * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), + * then itemSize should be `3`. + * @param normalized Applies to integer data only. + * Indicates how the underlying data in the buffer maps to the values in the GLSL code. + * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, + * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. + * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. + * If normalized is false, the values will be converted to floats unmodified, + * i.e. `32767` becomes `32767.0f`. + * Default `false`. + * @see {@link THREE.BufferAttribute | BufferAttribute} + */ + constructor( + array: Iterable | ArrayLike | ArrayBuffer | number, + itemSize: number, + normalized?: boolean, + ); +} diff --git a/src-testing/src/core/BufferGeometry.d.ts b/src-testing/src/core/BufferGeometry.d.ts new file mode 100644 index 000000000..82cf8ad03 --- /dev/null +++ b/src-testing/src/core/BufferGeometry.d.ts @@ -0,0 +1,422 @@ +import { Box3 } from "../math/Box3.js"; +import { Matrix4 } from "../math/Matrix4.js"; +import { Quaternion } from "../math/Quaternion.js"; +import { Sphere } from "../math/Sphere.js"; +import { Vector2 } from "../math/Vector2.js"; +import { Vector3, Vector3Tuple } from "../math/Vector3.js"; +import { BufferAttribute, BufferAttributeJSON } from "./BufferAttribute.js"; +import { EventDispatcher } from "./EventDispatcher.js"; +import { GLBufferAttribute } from "./GLBufferAttribute.js"; +import { InterleavedBufferAttribute } from "./InterleavedBufferAttribute.js"; + +export type NormalBufferAttributes = Record; +export type NormalOrGLBufferAttributes = Record< + string, + BufferAttribute | InterleavedBufferAttribute | GLBufferAttribute +>; + +export interface BufferGeometryJSON { + metadata?: { version: number; type: string; generator: string }; + + uuid: string; + type: string; + + name?: string; + userData?: Record; + + data?: { + attributes: Record; + + index?: { type: string; array: number[] }; + + morphAttributes?: Record; + morphTargetsRelative?: boolean; + + groups?: GeometryGroup[]; + + boundingSphere?: { center: Vector3Tuple; radius: number }; + }; +} + +export interface GeometryGroup { + /** + * Specifies the first element in this draw call – the first vertex for non-indexed geometry, otherwise the first triangle index. + * @remarks Expects a `Integer` + */ + start: number; + /** + * Specifies how many vertices (or indices) are included. + * @remarks Expects a `Integer` + */ + count: number; + /** + * Specifies the material array index to use. + * @remarks Expects a `Integer` + */ + materialIndex?: number | undefined; +} + +/** + * A representation of mesh, line, or point geometry + * Includes vertex positions, face indices, normals, colors, UVs, and custom attributes within buffers, reducing the cost of passing all this data to the GPU. + * @remarks + * To read and edit data in BufferGeometry attributes, see {@link THREE.BufferAttribute | BufferAttribute} documentation. + * @example + * ```typescript + * const geometry = new THREE.BufferGeometry(); + * + * // create a simple square shape. We duplicate the top left and bottom right + * // vertices because each vertex needs to appear once per triangle. + * const vertices = new Float32Array( [ + * -1.0, -1.0, 1.0, // v0 + * 1.0, -1.0, 1.0, // v1 + * 1.0, 1.0, 1.0, // v2 + * + * 1.0, 1.0, 1.0, // v3 + * -1.0, 1.0, 1.0, // v4 + * -1.0, -1.0, 1.0 // v5 + * ] ); + * + * // itemSize = 3 because there are 3 values (components) per vertex + * geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) ); + * const material = new THREE.MeshBasicMaterial( { color: 0xff0000 } ); + * const mesh = new THREE.Mesh( geometry, material ); + * ``` + * @example + * ```typescript + * const geometry = new THREE.BufferGeometry(); + * + * const vertices = new Float32Array( [ + * -1.0, -1.0, 1.0, // v0 + * 1.0, -1.0, 1.0, // v1 + * 1.0, 1.0, 1.0, // v2 + * -1.0, 1.0, 1.0, // v3 + * ] ); + * geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) ); + * + * const indices = [ + * 0, 1, 2, + * 2, 3, 0, + * ]; + * + * geometry.setIndex( indices ); + * geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) ); + * + * const material = new THREE.MeshBasicMaterial( { color: 0xff0000 } ); + * const mesh = new THREE.Mesh( geometry, material ); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry | Mesh with non-indexed faces} + * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_indexed | Mesh with indexed faces} + * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_lines | Lines} + * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_lines_indexed | Indexed Lines} + * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_custom_attributes_particles | Particles} + * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_rawshader | Raw Shaders} + * @see {@link https://threejs.org/docs/index.html#api/en/core/BufferGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferGeometry.js | Source} + */ +export class BufferGeometry< + Attributes extends NormalOrGLBufferAttributes = NormalBufferAttributes, +> extends EventDispatcher<{ dispose: {} }> { + /** + * This creates a new {@link THREE.BufferGeometry | BufferGeometry} object. + */ + constructor(); + + /** + * Unique number for this {@link THREE.BufferGeometry | BufferGeometry} instance. + * @remarks Expects a `Integer` + */ + id: number; + + /** + * {@link http://en.wikipedia.org/wiki/Universally_unique_identifier | UUID} of this object instance. + * @remarks This gets automatically assigned and shouldn't be edited. + */ + uuid: string; + + /** + * Optional name for this {@link THREE.BufferGeometry | BufferGeometry} instance. + * @defaultValue `''` + */ + name: string; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `BufferGeometry` + */ + readonly type: string | "BufferGeometry"; + + /** + * Allows for vertices to be re-used across multiple triangles; this is called using "indexed triangles". + * Each triangle is associated with the indices of three vertices. This attribute therefore stores the index of each vertex for each triangular face. + * If this attribute is not set, the {@link THREE.WebGLRenderer | renderer} assumes that each three contiguous positions represent a single triangle. + * @defaultValue `null` + */ + index: BufferAttribute | null; + + /** + * This hashmap has as id the name of the attribute to be set and as value the {@link THREE.BufferAttribute | buffer} to set it to. Rather than accessing this property directly, + * use {@link setAttribute | .setAttribute} and {@link getAttribute | .getAttribute} to access attributes of this geometry. + * @defaultValue `{}` + */ + attributes: Attributes; + + /** + * Hashmap of {@link THREE.BufferAttribute | BufferAttributes} holding details of the geometry's morph targets. + * @remarks + * Once the geometry has been rendered, the morph attribute data cannot be changed. + * You will have to call {@link dispose | .dispose}(), and create a new instance of {@link THREE.BufferGeometry | BufferGeometry}. + * @defaultValue `{}` + */ + morphAttributes: { + [name: string]: Array; // TODO Replace for 'Record<>' + }; + + /** + * Used to control the morph target behavior; when set to true, the morph target data is treated as relative offsets, rather than as absolute positions/normals. + * @defaultValue `false` + */ + morphTargetsRelative: boolean; + + /** + * Split the geometry into groups, each of which will be rendered in a separate WebGL draw call. This allows an array of materials to be used with the geometry. + * @remarks Every vertex and index must belong to exactly one group — groups must not share vertices or indices, and must not leave vertices or indices unused. + * @remarks Use {@link addGroup | .addGroup} to add groups, rather than modifying this array directly. + * @defaultValue `[]` + */ + groups: GeometryGroup[]; + + /** + * Bounding box for the {@link THREE.BufferGeometry | BufferGeometry}, which can be calculated with {@link computeBoundingBox | .computeBoundingBox()}. + * @remarks Bounding boxes aren't computed by default. They need to be explicitly computed, otherwise they are `null`. + * @defaultValue `null` + */ + boundingBox: Box3 | null; + + /** + * Bounding sphere for the {@link THREE.BufferGeometry | BufferGeometry}, which can be calculated with {@link computeBoundingSphere | .computeBoundingSphere()}. + * @remarks bounding spheres aren't computed by default. They need to be explicitly computed, otherwise they are `null`. + * @defaultValue `null` + */ + boundingSphere: Sphere | null; + + /** + * Determines the part of the geometry to render. This should not be set directly, instead use {@link setDrawRange | .setDrawRange(...)}. + * @remarks For non-indexed {@link THREE.BufferGeometry | BufferGeometry}, count is the number of vertices to render. + * @remarks For indexed {@link THREE.BufferGeometry | BufferGeometry}, count is the number of indices to render. + * @defaultValue `{ start: 0, count: Infinity }` + */ + drawRange: { start: number; count: number }; + + /** + * An object that can be used to store custom data about the BufferGeometry. It should not hold references to functions as these will not be cloned. + * @defaultValue `{}` + */ + userData: Record; + + /** + * Read-only flag to check if a given object is of type {@link BufferGeometry}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isBufferGeometry: true; + + /** + * Return the {@link index | .index} buffer. + */ + getIndex(): BufferAttribute | null; + + /** + * Set the {@link THREE.BufferGeometry.index | .index} buffer. + * @param index + */ + setIndex(index: BufferAttribute | number[] | null): this; + + /** + * Sets an {@link attributes | attribute} to this geometry with the specified name. + * @remarks + * Use this rather than the attributes property, because an internal hashmap of {@link attributes | .attributes} is maintained to speed up iterating over attributes. + * @param name + * @param attribute + */ + setAttribute(name: K, attribute: Attributes[K]): this; + + /** + * Returns the {@link attributes | attribute} with the specified name. + * @param name + */ + getAttribute(name: K): Attributes[K]; + + /** + * Deletes the {@link attributes | attribute} with the specified name. + * @param name + */ + deleteAttribute(name: keyof Attributes): this; + + /** + * Returns true if the {@link attributes | attribute} with the specified name exists. + * @param name + */ + hasAttribute(name: keyof Attributes): boolean; + + /** + * Adds a group to this geometry + * @see the {@link BufferGeometry.groups | groups} property for details. + * @param start + * @param count + * @param materialIndex + */ + addGroup(start: number, count: number, materialIndex?: number): void; + + /** + * Clears all groups. + */ + clearGroups(): void; + + /** + * Set the {@link drawRange | .drawRange} property + * @remarks For non-indexed BufferGeometry, count is the number of vertices to render + * @remarks For indexed BufferGeometry, count is the number of indices to render. + * @param start + * @param count is the number of vertices or indices to render. Expects a `Integer` + */ + setDrawRange(start: number, count: number): void; + + /** + * Applies the matrix transform to the geometry. + * @param matrix + */ + applyMatrix4(matrix: Matrix4): this; + + /** + * Applies the rotation represented by the quaternion to the geometry. + * @param quaternion + */ + applyQuaternion(quaternion: Quaternion): this; + + /** + * Rotate the geometry about the X axis. This is typically done as a one time operation, and not during a loop. + * @remarks Use {@link THREE.Object3D.rotation | Object3D.rotation} for typical real-time mesh rotation. + * @param angle radians. Expects a `Float` + */ + rotateX(angle: number): this; + + /** + * Rotate the geometry about the Y axis. + * @remarks This is typically done as a one time operation, and not during a loop. + * @remarks Use {@link THREE.Object3D.rotation | Object3D.rotation} for typical real-time mesh rotation. + * @param angle radians. Expects a `Float` + */ + rotateY(angle: number): this; + + /** + * Rotate the geometry about the Z axis. + * @remarks This is typically done as a one time operation, and not during a loop. + * @remarks Use {@link THREE.Object3D.rotation | Object3D.rotation} for typical real-time mesh rotation. + * @param angle radians. Expects a `Float` + */ + rotateZ(angle: number): this; + + /** + * Translate the geometry. + * @remarks This is typically done as a one time operation, and not during a loop. + * @remarks Use {@link THREE.Object3D.position | Object3D.position} for typical real-time mesh rotation. + * @param x Expects a `Float` + * @param y Expects a `Float` + * @param z Expects a `Float` + */ + translate(x: number, y: number, z: number): this; + + /** + * Scale the geometry data. + * @remarks This is typically done as a one time operation, and not during a loop. + * @remarks Use {@link THREE.Object3D.scale | Object3D.scale} for typical real-time mesh scaling. + * @param x Expects a `Float` + * @param y Expects a `Float` + * @param z Expects a `Float` + */ + scale(x: number, y: number, z: number): this; + + /** + * Rotates the geometry to face a point in space. + * @remarks This is typically done as a one time operation, and not during a loop. + * @remarks Use {@link THREE.Object3D.lookAt | Object3D.lookAt} for typical real-time mesh usage. + * @param vector A world vector to look at. + */ + lookAt(vector: Vector3): this; + + /** + * Center the geometry based on the bounding box. + */ + center(): this; + + /** + * Sets the attributes for this BufferGeometry from an array of points. + * @param points + */ + setFromPoints(points: Vector3[] | Vector2[]): this; + + /** + * Computes the bounding box of the geometry, and updates the {@link .boundingBox} attribute. The bounding box is + * not computed by the engine; it must be computed by your app. You may need to recompute the bounding box if the + * geometry vertices are modified. + */ + computeBoundingBox(): void; + + /** + * Computes the bounding sphere of the geometry, and updates the {@link .boundingSphere} attribute. The engine + * automatically computes the bounding sphere when it is needed, e.g., for ray casting or view frustum culling. You + * may need to recompute the bounding sphere if the geometry vertices are modified. + */ + computeBoundingSphere(): void; + + /** + * Calculates and adds a tangent attribute to this geometry. + * The computation is only supported for indexed geometries and if position, normal, and uv attributes are defined + * @remarks + * When using a tangent space normal map, prefer the MikkTSpace algorithm provided by + * {@link BufferGeometryUtils.computeMikkTSpaceTangents} instead. + */ + computeTangents(): void; + + /** + * Computes vertex normals for the given vertex data. For indexed geometries, the method sets each vertex normal to + * be the average of the face normals of the faces that share that vertex. For non-indexed geometries, vertices are + * not shared, and the method sets each vertex normal to be the same as the face normal. + */ + computeVertexNormals(): void; + + /** + * Every normal vector in a geometry will have a magnitude of 1 + * @remarks This will correct lighting on the geometry surfaces. + */ + normalizeNormals(): void; + + /** + * Return a non-index version of an indexed BufferGeometry. + */ + toNonIndexed(): BufferGeometry; + + /** + * Convert the buffer geometry to three.js {@link https://github.com/mrdoob/three.js/wiki/JSON-Object-Scene-format-4 | JSON Object/Scene format}. + */ + toJSON(): BufferGeometryJSON; + + /** + * Creates a clone of this BufferGeometry + */ + clone(): this; + + /** + * Copies another BufferGeometry to this BufferGeometry. + * @param source + */ + copy(source: BufferGeometry): this; + + /** + * Frees the GPU-related resources allocated by this instance. + * @remarks Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/core/Clock.d.ts b/src-testing/src/core/Clock.d.ts new file mode 100644 index 000000000..05307083c --- /dev/null +++ b/src-testing/src/core/Clock.d.ts @@ -0,0 +1,72 @@ +/** + * Object for keeping track of time + * @remarks + * This uses {@link https://developer.mozilla.org/en-US/docs/Web/API/Performance/now | performance.now} if it is available, + * otherwise it reverts to the less accurate {@link https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/now | Date.now}. + * @see {@link https://threejs.org/docs/index.html#api/en/core/Clock | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/Clock.js | Source} + */ +export class Clock { + /** + * Create a new instance of {@link THREE.Clock | Clock} + * @param autoStart - Whether to automatically start the clock when {@link getDelta | .getDelta()} is called for the first time. Default `true` + */ + constructor(autoStart?: boolean); + + /** + * If set, starts the clock automatically when {@link getDelta | .getDelta()} is called for the first time. + * @defaultValue `true` + */ + autoStart: boolean; + + /** + * Holds the time at which the clock's {@link start | .start()} method was last called. + * @defaultValue `0` + */ + startTime: number; + + /** + * Holds the time at which the clock's {@link start | .start()}, {@link getElapsedTime | .getElapsedTime()} or {@link getDelta | .getDelta()} methods were last called. + * @defaultValue `0` + */ + oldTime: number; + + /** + * Keeps track of the total time that the clock has been running. + * @defaultValue `0` + */ + elapsedTime: number; + + /** + * Whether the clock is running or not. + * @defaultValue `false` + */ + running: boolean; + + /** + * Starts clock. + * @remarks + * Also sets the {@link startTime | .startTime} and {@link oldTime | .oldTime} to the current time, + * sets {@link elapsedTime | .elapsedTime} to `0` and {@link running | .running} to `true`. + */ + start(): void; + + /** + * Stops clock and sets {@link oldTime | oldTime} to the current time. + */ + stop(): void; + + /** + * Get the seconds passed since the clock started and sets {@link oldTime | .oldTime} to the current time. + * @remarks + * If {@link autoStart | .autoStart} is `true` and the clock is not running, also starts the clock. + */ + getElapsedTime(): number; + + /** + * Get the seconds passed since the time {@link oldTime | .oldTime} was set and sets {@link oldTime | .oldTime} to the current time. + * @remarks + * If {@link autoStart | .autoStart} is `true` and the clock is not running, also starts the clock. + */ + getDelta(): number; +} diff --git a/src-testing/src/core/EventDispatcher.d.ts b/src-testing/src/core/EventDispatcher.d.ts new file mode 100644 index 000000000..fa8048a96 --- /dev/null +++ b/src-testing/src/core/EventDispatcher.d.ts @@ -0,0 +1,85 @@ +/** + * The minimal basic Event that can be dispatched by a {@link EventDispatcher<>}. + */ +export interface BaseEvent { + readonly type: TEventType; +} + +/** + * The minimal expected contract of a fired Event that was dispatched by a {@link EventDispatcher<>}. + */ +export interface Event { + readonly type: TEventType; + readonly target: TTarget; +} + +export type EventListener = ( + event: TEventData & Event, +) => void; + +/** + * JavaScript events for custom objects + * @example + * ```typescript + * // Adding events to a custom object + * class Car extends EventDispatcher { + * start() { + * this.dispatchEvent( { type: 'start', message: 'vroom vroom!' } ); + * } + * }; + * // Using events with the custom object + * const car = new Car(); + * car.addEventListener( 'start', ( event ) => { + * alert( event.message ); + * } ); + * car.start(); + * ``` + * @see {@link https://github.com/mrdoob/eventdispatcher.js | mrdoob EventDispatcher on GitHub} + * @see {@link https://threejs.org/docs/index.html#api/en/core/EventDispatcher | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/EventDispatcher.js | Source} + */ +export class EventDispatcher { + /** + * Creates {@link THREE.EventDispatcher | EventDispatcher} object. + */ + constructor(); + + /** + * Adds a listener to an event type. + * @param type The type of event to listen to. + * @param listener The function that gets called when the event is fired. + */ + addEventListener>( + type: T, + listener: EventListener, + ): void; + addEventListener(type: T, listener: EventListener<{}, T, this>): void; + + /** + * Checks if listener is added to an event type. + * @param type The type of event to listen to. + * @param listener The function that gets called when the event is fired. + */ + hasEventListener>( + type: T, + listener: EventListener, + ): boolean; + hasEventListener(type: T, listener: EventListener<{}, T, this>): boolean; + + /** + * Removes a listener from an event type. + * @param type The type of the listener that gets removed. + * @param listener The listener function that gets removed. + */ + removeEventListener>( + type: T, + listener: EventListener, + ): void; + removeEventListener(type: T, listener: EventListener<{}, T, this>): void; + + /** + * Fire an event type. + * @param event The event that gets fired. + */ + dispatchEvent>(event: BaseEvent & TEventMap[T]): void; +} diff --git a/src-testing/src/core/GLBufferAttribute.d.ts b/src-testing/src/core/GLBufferAttribute.d.ts new file mode 100644 index 000000000..c549518dd --- /dev/null +++ b/src-testing/src/core/GLBufferAttribute.d.ts @@ -0,0 +1,121 @@ +/** + * This buffer attribute class does not construct a VBO. + * Instead, it uses whatever VBO is passed in constructor and can later be altered via the {@link buffer | .buffer} property. + * @remarks + * It is required to pass additional params alongside the VBO + * Those are: the GL context, the GL data type, the number of components per vertex, the number of bytes per component, and the number of vertices. + * @remarks + * The most common use case for this class is when some kind of GPGPU calculation interferes or even produces the VBOs in question. + * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_glbufferattribute | WebGL / buffergeometry / glbufferattribute} + * @see {@link https://threejs.org/docs/index.html#api/en/core/GLBufferAttribute | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/GLBufferAttribute.js | Source} + */ +export class GLBufferAttribute { + /** + * This creates a new GLBufferAttribute object. + * @param buffer Must be a {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLBuffer | WebGLBuffer}. See {@link GLBufferAttribute.buffer | .buffer} + * @param type One of {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Constants#Data_types | WebGL Data Types}. See {@link GLBufferAttribute.type | .type} + * @param itemSize How many values make up each item (vertex). See {@link GLBufferAttribute.itemSize | .itemSize} + * @param elementSize `1`, `2` or `4`. The corresponding size (in bytes) for the given {@link type} param. See {@link GLBufferAttribute.elementSize | .elementSize} + * @param count The expected number of vertices in VBO. See {@link GLBufferAttribute.count | .count} + */ + constructor(buffer: WebGLBuffer, type: GLenum, itemSize: number, elementSize: 1 | 2 | 4, count: number); + + /** + * Read-only flag to check if a given object is of type {@link GLBufferAttribute}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isGLBufferAttribute: true; + + /** + * Optional name for this attribute instance. + * @defaultValue `""` + */ + name: string; + + /** + * The current {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLBuffer | WebGLBuffer} instance. + */ + buffer: WebGLBuffer; + + /** + * A {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Constants#Data_types | WebGL Data Type} describing the underlying VBO contents. + * + * #### WebGL Data Type (`GLenum`) + * - gl.BYTE: 0x1400 + * - gl.UNSIGNED_BYTE: 0x1401 + * - gl.SHORT: 0x1402 + * - gl.UNSIGNED_SHORT: 0x1403 + * - gl.INT: 0x1404 + * - gl.UNSIGNED_INT: 0x1405 + * - gl.FLOAT: 0x1406 + * @remarks Set this property together with {@link elementSize | .elementSize}. The recommended way is using the {@link setType | .setType()} method. + * @remarks Expects a `DataType` `GLenum` _possible values:_ `0x1400` `0x1401` `0x1402` `0x1403` `0x1404` `0x1405` `0x1406` + */ + type: GLenum; + + /** + * How many values make up each item (vertex). + * @remarks The number of values of the array that should be associated with a particular vertex. + * For instance, if this attribute is storing a 3-component vector (such as a position, normal, or color), then itemSize should be 3. + * @remarks Expects a `Integer` + */ + itemSize: number; + + /** + * Stores the corresponding size in bytes for the current {@link type | .type} property value. + * + * The corresponding size (_in bytes_) for the given "type" param. + * #### WebGL Data Type (`GLenum`) + * - gl.BYTE: 1 + * - gl.UNSIGNED_BYTE: 1 + * - gl.SHORT: 2 + * - gl.UNSIGNED_SHORT: 2 + * - gl.INT: 4 + * - gl.UNSIGNED_INT: 4 + * - gl.FLOAT: 4 + * @remarks Set this property together with {@link type | .type}. The recommended way is using the {@link setType | .setType} method. + * @see `constructor`` for a list of known type sizes. + * @remarks Expects a `1`, `2` or `4` + */ + elementSize: 1 | 2 | 4; + + /** + * The expected number of vertices in VBO. + * @remarks Expects a `Integer` + */ + count: number; + + /** + * A version number, incremented every time the needsUpdate property is set to true. + * @remarks Expects a `Integer` + */ + version: number; + + /** + * Setting this to true increments {@link version | .version}. + * @remarks _set-only property_. + */ + set needsUpdate(value: boolean); + + /** + * Sets the {@link buffer | .buffer} property. + */ + setBuffer(buffer: WebGLBuffer): this; + + /** + * Sets the both {@link GLBufferAttribute.type | type} and {@link GLBufferAttribute.elementSize | elementSize} properties. + */ + setType(type: GLenum, elementSize: 1 | 2 | 4): this; + + /** + * Sets the {@link GLBufferAttribute.itemSize | itemSize} property. + */ + setItemSize(itemSize: number): this; + + /** + * Sets the {@link GLBufferAttribute.count | count} property. + */ + setCount(count: number): this; +} diff --git a/src-testing/src/core/InstancedBufferAttribute.d.ts b/src-testing/src/core/InstancedBufferAttribute.d.ts new file mode 100644 index 000000000..fde083b81 --- /dev/null +++ b/src-testing/src/core/InstancedBufferAttribute.d.ts @@ -0,0 +1,32 @@ +import { BufferAttribute, TypedArray } from "./BufferAttribute.js"; + +/** + * An instanced version of {@link THREE.BufferAttribute | BufferAttribute}. + * @see {@link https://threejs.org/docs/index.html#api/en/core/InstancedBufferAttribute | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/InstancedBufferAttribute.js | Source} + */ +export class InstancedBufferAttribute extends BufferAttribute { + /** + * Create a new instance of {@link THREE.InstancedBufferAttribute | InstancedBufferAttribute} + * @param array + * @param itemSize + * @param normalized + * @param meshPerAttribute + */ + constructor(array: TypedArray, itemSize: number, normalized?: boolean, meshPerAttribute?: number); + + /** + * Defines how often a value of this buffer attribute should be repeated. + * A value of one means that each value of the instanced attribute is used for a single instance. + * A value of two means that each value is used for two consecutive instances (and so on). + * @defaultValue `1` + */ + meshPerAttribute: number; + + /** + * Read-only flag to check if a given object is of type {@link InstancedBufferAttribute}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isInstancedBufferAttribute: true; +} diff --git a/src-testing/src/core/InstancedBufferGeometry.d.ts b/src-testing/src/core/InstancedBufferGeometry.d.ts new file mode 100644 index 000000000..421294c49 --- /dev/null +++ b/src-testing/src/core/InstancedBufferGeometry.d.ts @@ -0,0 +1,37 @@ +import { BufferGeometry } from "./BufferGeometry.js"; + +/** + * An instanced version of {@link THREE.BufferGeometry | BufferGeometry}. + * @see {@link https://threejs.org/docs/index.html#api/en/core/InstancedBufferGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/InstancedBufferGeometry.js | Source} + */ +export class InstancedBufferGeometry extends BufferGeometry { + /** + * Create a new instance of {@link InstancedBufferGeometry} + */ + constructor(); + + /** + * @defaultValue `InstancedBufferGeometry` + */ + type: string; + + /** + * Read-only flag to check if a given object is of type {@link InstancedBufferGeometry}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isInstancedBufferGeometry: true; + + /** + * @defaultValue `Infinity` + */ + instanceCount: number; + + /** + * Copies the given {@link InstancedBufferGeometry} to this instance. + * @param source + * @override + */ + copy(source: InstancedBufferGeometry): this; +} diff --git a/src-testing/src/core/InstancedInterleavedBuffer.d.ts b/src-testing/src/core/InstancedInterleavedBuffer.d.ts new file mode 100644 index 000000000..3c78beae4 --- /dev/null +++ b/src-testing/src/core/InstancedInterleavedBuffer.d.ts @@ -0,0 +1,22 @@ +import { TypedArray } from "./BufferAttribute.js"; +import { InterleavedBuffer } from "./InterleavedBuffer.js"; + +/** + * An instanced version of {@link THREE.InterleavedBuffer | InterleavedBuffer}. + * @see {@link https://threejs.org/docs/index.html#api/en/core/InstancedInterleavedBuffer | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/InstancedInterleavedBuffer.js | Source} + */ +export class InstancedInterleavedBuffer extends InterleavedBuffer { + /** + * Create a new instance of {@link InstancedInterleavedBuffer} + * @param array + * @param itemSize + * @param meshPerAttribute + */ + constructor(array: TypedArray, stride: number, meshPerAttribute?: number); + + /** + * @defaultValue `1` + */ + meshPerAttribute: number; +} diff --git a/src-testing/src/core/InterleavedBuffer.d.ts b/src-testing/src/core/InterleavedBuffer.d.ts new file mode 100644 index 000000000..7b01ef5bf --- /dev/null +++ b/src-testing/src/core/InterleavedBuffer.d.ts @@ -0,0 +1,162 @@ +import { Usage } from "../constants.js"; +import { TypedArray } from "./BufferAttribute.js"; +import { InterleavedBufferAttribute } from "./InterleavedBufferAttribute.js"; + +/** + * **"Interleaved"** means that multiple attributes, possibly of different types, (e.g., _position, normal, uv, color_) are packed into a single array buffer. + * An introduction into interleaved arrays can be found here: {@link https://blog.tojicode.com/2011/05/interleaved-array-basics.html | Interleaved array basics} + * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_points_interleaved | webgl / buffergeometry / points / interleaved} + * @see {@link https://threejs.org/docs/index.html#api/en/core/InterleavedBuffer | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/InterleavedBuffer.js | Source} + */ +export class InterleavedBuffer { + readonly isInterleavedBuffer: true; + + /** + * Create a new instance of {@link InterleavedBuffer} + * @param array A {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray | TypedArray} with a shared buffer. Stores the geometry data. + * @param stride The number of typed-array elements per vertex. Expects a `Integer` + */ + constructor(array: TypedArray, stride: number); + + /** + * A {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray | TypedArray} with a shared buffer. Stores the geometry data. + */ + array: TypedArray; + + /** + * The number of {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray | TypedArray} elements per vertex. + * @remarks Expects a `Integer` + */ + stride: number; + + /** + * Defines the intended usage pattern of the data store for optimization purposes. + * Corresponds to the {@link BufferAttribute.usage | usage} parameter of + * {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferData | WebGLRenderingContext.bufferData}. + * @remarks + * After the initial use of a buffer, its usage cannot be changed. Instead, instantiate a new one and set the desired usage before the next render. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/BufferAttributeUsage | Buffer Attribute Usage Constants} for all possible values. + * @see {@link BufferAttribute.setUsage | setUsage} + * @defaultValue {@link THREE.StaticDrawUsage | THREE.StaticDrawUsage}. + */ + usage: Usage; + + /** + * Object containing offset and count. + * @defaultValue `{ offset: number = 0; count: number = -1 }` + * @deprecated Will be removed in r169. Use "addUpdateRange()" instead. + */ + updateRange: { + /** @defaultValue `0` */ + offset: number; + /** @defaultValue `-1` */ + count: number; + }; + + /** + * This can be used to only update some components of stored data. Use the {@link .addUpdateRange} function to add + * ranges to this array. + */ + updateRanges: Array<{ + /** + * Position at which to start update. + */ + start: number; + /** + * The number of components to update. + */ + count: number; + }>; + + /** + * A version number, incremented every time the {@link BufferAttribute.needsUpdate | needsUpdate} property is set to true. + * @remarks Expects a `Integer` + * @defaultValue `0` + */ + version: number; + + /** + * Gives the total number of elements in the array. + * @remarks Expects a `Integer` + * @defaultValue 0 + */ + count: number; + + /** + * Flag to indicate that this attribute has changed and should be re-sent to the GPU. + * Set this to true when you modify the value of the array. + * @remarks Setting this to true also increments the {@link BufferAttribute.version | version}. + * @remarks _set-only property_. + */ + set needsUpdate(value: boolean); + + /** + * {@link http://en.wikipedia.org/wiki/Universally_unique_identifier | UUID} of this object instance. + * @remarks This gets automatically assigned and shouldn't be edited. + */ + uuid: string; + + /** + * Calls {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set | TypedArray.set}( {@link value}, {@link offset} ) + * on the {@link BufferAttribute.array | array}. + * @param value The source `TypedArray`. + * @param offset index of the {@link BufferAttribute.array | array} at which to start copying. Expects a `Integer`. Default `0`. + * @throws `RangeError` When {@link offset} is negative or is too large. + */ + set(value: ArrayLike, offset: number): this; + + /** + * Set {@link BufferAttribute.usage | usage} + * @remarks + * After the initial use of a buffer, its usage cannot be changed. Instead, instantiate a new one and set the desired usage before the next render. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/BufferAttributeUsage | Buffer Attribute Usage Constants} for all possible values. + * @see {@link BufferAttribute.usage | usage} + * @param value Corresponds to the {@link BufferAttribute.usage | usage} parameter of + * {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferData | WebGLRenderingContext.bufferData}. + */ + setUsage(value: Usage): this; + + /** + * Adds a range of data in the data array to be updated on the GPU. Adds an object describing the range to the + * {@link .updateRanges} array. + */ + addUpdateRange(start: number, count: number): void; + + /** + * Clears the {@link .updateRanges} array. + */ + clearUpdateRanges(): void; + + /** + * Copies another {@link InterleavedBuffer} to this {@link InterleavedBuffer} instance. + * @param source + */ + copy(source: InterleavedBuffer): this; + + /** + * Copies data from {@link attribute}[{@link index2}] to {@link InterleavedBuffer.array | array}[{@link index1}]. + * @param index1 Expects a `Integer` + * @param attribute + * @param index2 Expects a `Integer` + */ + copyAt(index1: number, attribute: InterleavedBufferAttribute, index2: number): this; + + /** + * Creates a clone of this {@link InterleavedBuffer}. + * @param data This object holds shared array buffers required for properly cloning geometries with interleaved attributes. + */ + clone(data: {}): InterleavedBuffer; + + /** + * Serializes this {@link InterleavedBuffer}. + * Converting to {@link https://github.com/mrdoob/three.js/wiki/JSON-Geometry-format-4 | JSON Geometry format v4}, + * @param data This object holds shared array buffers required for properly serializing geometries with interleaved attributes. + */ + toJSON(data: {}): { + uuid: string; + buffer: string; + type: string; + stride: number; + }; +} diff --git a/src-testing/src/core/InterleavedBufferAttribute.d.ts b/src-testing/src/core/InterleavedBufferAttribute.d.ts new file mode 100644 index 000000000..955049938 --- /dev/null +++ b/src-testing/src/core/InterleavedBufferAttribute.d.ts @@ -0,0 +1,201 @@ +import { Matrix3 } from "../math/Matrix3.js"; +import { Matrix4 } from "../math/Matrix4.js"; +import { BufferAttribute, TypedArray } from "./BufferAttribute.js"; +import { InterleavedBuffer } from "./InterleavedBuffer.js"; + +/** + * @see {@link https://threejs.org/docs/index.html#api/en/core/InterleavedBufferAttribute | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/InterleavedBufferAttribute.js | Source} + */ +export class InterleavedBufferAttribute { + /** + * Create a new instance of {@link THREE.InterleavedBufferAttribute | InterleavedBufferAttribute}. + * @param interleavedBuffer + * @param itemSize + * @param offset + * @param normalized Default `false`. + */ + constructor(interleavedBuffer: InterleavedBuffer, itemSize: number, offset: number, normalized?: boolean); + + /** + * Optional name for this attribute instance. + * @defaultValue `''` + */ + name: string; + + /** + * The {@link InterleavedBuffer | InterleavedBuffer} instance passed in the constructor. + */ + data: InterleavedBuffer; + + /** + * How many values make up each item. + * @remarks Expects a `Integer` + */ + itemSize: number; + + /** + * The offset in the underlying array buffer where an item starts. + * @remarks Expects a `Integer` + */ + offset: number; + + /** + * @defaultValue `false` + */ + normalized: boolean; + + /** + * The value of {@link data | .data}.{@link InterleavedBuffer.count | count}. + * If the buffer is storing a 3-component item (such as a _position, normal, or color_), then this will count the number of such items stored. + * @remarks _get-only property_. + * @remarks Expects a `Integer` + */ + get count(): number; + + /** + * The value of {@link InterleavedBufferAttribute.data | data}.{@link InterleavedBuffer.array | array}. + * @remarks _get-only property_. + */ + get array(): TypedArray; + + /** + * Flag to indicate that the {@link data | .data} ({@link InterleavedBuffer}) attribute has changed and should be re-sent to the GPU. + * @remarks Setting this to have the same result of setting true also increments the {@link InterleavedBuffer.needsUpdate | InterleavedBuffer.needsUpdate} of {@link data | .data}. + * @remarks Setting this to true also increments the {@link InterleavedBuffer.version | InterleavedBuffer.version}. + * @remarks _set-only property_. + */ + set needsUpdate(value: boolean); + + /** + * Read-only flag to check if a given object is of type {@link InterleavedBufferAttribute}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isInterleavedBufferAttribute: true; + + /** + * Applies matrix {@link Matrix4 | m} to every Vector3 element of this InterleavedBufferAttribute. + * @param m + */ + applyMatrix4(m: Matrix4): this; + + /** + * Applies normal matrix {@link Matrix3 | m} to every Vector3 element of this InterleavedBufferAttribute. + * @param m + */ + applyNormalMatrix(m: Matrix3): this; + + /** + * Applies matrix {@link Matrix4 | m} to every Vector3 element of this InterleavedBufferAttribute, interpreting the elements as a direction vectors. + * @param m + */ + transformDirection(m: Matrix4): this; + + /** + * Returns the given component of the vector at the given index. + */ + getComponent(index: number, component: number): number; + + /** + * Sets the given component of the vector at the given index. + */ + setComponent(index: number, component: number, value: number): this; + + /** + * Returns the x component of the item at the given index. + * @param index Expects a `Integer` + */ + getX(index: number): number; + + /** + * Sets the x component of the item at the given index. + * @param index Expects a `Integer` + * @param x Expects a `Float` + */ + setX(index: number, x: number): this; + + /** + * Returns the y component of the item at the given index. + * @param index Expects a `Integer` + */ + getY(index: number): number; + + /** + * Sets the y component of the item at the given index. + * @param index Expects a `Integer` + * @param y Expects a `Float` + */ + setY(index: number, y: number): this; + + /** + * Returns the z component of the item at the given index. + * @param index Expects a `Integer` + */ + getZ(index: number): number; + + /** + * Sets the z component of the item at the given index. + * @param index Expects a `Integer` + * @param z Expects a `Float` + */ + setZ(index: number, z: number): this; + + /** + * Returns the w component of the item at the given index. + * @param index Expects a `Integer` + */ + getW(index: number): number; + + /** + * Sets the w component of the item at the given index. + * @param index Expects a `Integer` + * @param w Expects a `Float` + */ + setW(index: number, z: number): this; + + /** + * Sets the x and y components of the item at the given index. + * @param index Expects a `Integer` + * @param x Expects a `Float` + * @param y Expects a `Float` + */ + setXY(index: number, x: number, y: number): this; + /** + * Sets the x, y and z components of the item at the given index. + * @param index Expects a `Integer` + * @param x Expects a `Float` + * @param y Expects a `Float` + * @param z Expects a `Float` + */ + setXYZ(index: number, x: number, y: number, z: number): this; + + /** + * Sets the x, y, z and w components of the item at the given index. + * @param index Expects a `Integer` + * @param x Expects a `Float` + * @param y Expects a `Float` + * @param z Expects a `Float` + * @param w Expects a `Float` + */ + setXYZW(index: number, x: number, y: number, z: number, w: number): this; + + /** + * Creates a clone of this {@link InterleavedBufferAttribute}. + * @param data This object holds shared array buffers required for properly cloning geometries with interleaved attributes. + */ + clone(data?: {}): BufferAttribute; + + /** + * Serializes this {@link InterleavedBufferAttribute}. + * Converting to {@link https://github.com/mrdoob/three.js/wiki/JSON-Geometry-format-4 | JSON Geometry format v4}, + * @param data This object holds shared array buffers required for properly serializing geometries with interleaved attributes. + */ + toJSON(data?: {}): { + isInterleavedBufferAttribute: true; + itemSize: number; + data: string; + offset: number; + normalized: boolean; + }; +} diff --git a/src-testing/src/core/Layers.d.ts b/src-testing/src/core/Layers.d.ts new file mode 100644 index 000000000..ad95f892d --- /dev/null +++ b/src-testing/src/core/Layers.d.ts @@ -0,0 +1,72 @@ +/** + * A {@link THREE.Layers | Layers} object assigns an {@link THREE.Object3D | Object3D} to 1 or more of 32 layers numbered `0` to `31` - internally the + * layers are stored as a {@link https://en.wikipedia.org/wiki/Mask_(computing) | bit mask}, and + * by default all Object3Ds are a member of layer `0`. + * @remarks + * This can be used to control visibility - an object must share a layer with a {@link Camera | camera} to be visible when that camera's view is rendered. + * @remarks + * All classes that inherit from {@link THREE.Object3D | Object3D} have an {@link THREE.Object3D.layers | Object3D.layers} property which is an instance of this class. + * @see Example: {@link https://threejs.org/examples/#webgl_layers | WebGL / layers} + * @see Example: {@link https://threejs.org/examples/#webxr_vr_layers | Webxr / vr / layers} + * @see {@link https://threejs.org/docs/index.html#api/en/core/Layers | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/Layers.js | Source} + */ +export class Layers { + /** + * Create a new Layers object, with membership initially set to layer 0. + */ + constructor(); + + /** + * A bit mask storing which of the 32 layers this layers object is currently a member of. + * @defaultValue `1 | 0` + * @remarks Expects a `Integer` + */ + mask: number; + + /** + * Set membership to `layer`, and remove membership all other layers. + * @param layer An integer from 0 to 31. + */ + set(layer: number): void; + + /** + * Add membership of this `layer`. + * @param layer An integer from 0 to 31. + */ + enable(layer: number): void; + + /** + * Add membership to all layers. + */ + enableAll(): void; + + /** + * Toggle membership of `layer`. + * @param layer An integer from 0 to 31. + */ + toggle(layer: number): void; + + /** + * Remove membership of this `layer`. + * @param layer An integer from 0 to 31. + */ + disable(layer: number): void; + + /** + * Remove membership from all layers. + */ + disableAll(): void; + + /** + * Returns true if this and the passed `layers` object have at least one layer in common. + * @param layers A Layers object + */ + test(layers: Layers): boolean; + + /** + * Returns true if the given layer is enabled. + * @param layer An integer from 0 to 31. + */ + isEnabled(layer: number): boolean; +} diff --git a/src-testing/src/core/Object3D.d.ts b/src-testing/src/core/Object3D.d.ts new file mode 100644 index 000000000..1c35b6e09 --- /dev/null +++ b/src-testing/src/core/Object3D.d.ts @@ -0,0 +1,672 @@ +import { AnimationClip, AnimationClipJSON } from "../animation/AnimationClip.js"; +import { Camera } from "../cameras/Camera.js"; +import { ShapeJSON } from "../extras/core/Shape.js"; +import { Material, MaterialJSON } from "../materials/Material.js"; +import { Euler } from "../math/Euler.js"; +import { Matrix3 } from "../math/Matrix3.js"; +import { Matrix4, Matrix4Tuple } from "../math/Matrix4.js"; +import { Quaternion } from "../math/Quaternion.js"; +import { Vector3, Vector3Tuple } from "../math/Vector3.js"; +import { Group } from "../objects/Group.js"; +import { SkeletonJSON } from "../objects/Skeleton.js"; +import { WebGLRenderer } from "../renderers/WebGLRenderer.js"; +import { Scene } from "../scenes/Scene.js"; +import { SourceJSON } from "../textures/Source.js"; +import { TextureJSON } from "../textures/Texture.js"; +import { BufferGeometry, BufferGeometryJSON } from "./BufferGeometry.js"; +import { EventDispatcher } from "./EventDispatcher.js"; +import { Layers } from "./Layers.js"; +import { Intersection, Raycaster } from "./Raycaster.js"; + +export interface Object3DJSONObject { + uuid: string; + type: string; + + name?: string; + castShadow?: boolean; + receiveShadow?: boolean; + visible?: boolean; + frustumCulled?: boolean; + renderOrder?: number; + userData?: Record; + + layers: number; + matrix: Matrix4Tuple; + up: Vector3Tuple; + + matrixAutoUpdate?: boolean; + + material?: string | string[]; + + children?: string[]; + + animations?: string[]; +} + +export interface Object3DJSON { + metadata?: { version: number; type: string; generator: string }; + object: Object3DJSONObject; +} + +export interface JSONMeta { + geometries: Record; + materials: Record; + textures: Record; + images: Record; + shapes: Record; + skeletons: Record; + animations: Record; + nodes: Record; +} + +export interface Object3DEventMap { + /** + * Fires when the object has been added to its parent object. + */ + added: {}; + + /** + * Fires when the object has been removed from its parent object. + */ + removed: {}; + + /** + * Fires when a new child object has been added. + */ + childadded: { child: Object3D }; + + /** + * Fires when a new child object has been removed. + */ + childremoved: { child: Object3D }; +} + +/** + * This is the base class for most objects in three.js and provides a set of properties and methods for manipulating objects in 3D space. + * @remarks Note that this can be used for grouping objects via the {@link THREE.Object3D.add | .add()} method which adds the object as a child, + * however it is better to use {@link THREE.Group | Group} for this. + * @see {@link https://threejs.org/docs/index.html#api/en/core/Object3D | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/Object3D.js | Source} + */ +export class Object3D extends EventDispatcher { + /** + * This creates a new {@link Object3D} object. + */ + constructor(); + + /** + * Flag to check if a given object is of type {@link Object3D}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isObject3D: true; + + /** + * Unique number for this {@link Object3D} instance. + * @remarks Note that ids are assigned in chronological order: 1, 2, 3, ..., incrementing by one for each new object. + * Expects a `Integer` + */ + readonly id: number; + + /** + * {@link http://en.wikipedia.org/wiki/Universally_unique_identifier | UUID} of this object instance. + * @remarks This gets automatically assigned and shouldn't be edited. + */ + uuid: string; + + /** + * Optional name of the object + * @remarks _(doesn't need to be unique)_. + * @defaultValue `""` + */ + name: string; + + /** + * A Read-only _string_ to check `this` object type. + * @remarks This can be used to find a specific type of Object3D in a scene. + * Sub-classes will update this value. + * @defaultValue `Object3D` + */ + readonly type: string | "Object3D"; + + /** + * Object's parent in the {@link https://en.wikipedia.org/wiki/Scene_graph | scene graph}. + * @remarks An object can have at most one parent. + * @defaultValue `null` + */ + parent: Object3D | null; + + /** + * Array with object's children. + * @see {@link THREE.Object3DGroup | Group} for info on manually grouping objects. + * @defaultValue `[]` + */ + + children: Object3D[]; + + /** + * This is used by the {@link lookAt | lookAt} method, for example, to determine the orientation of the result. + * @defaultValue {@link DEFAULT_UP | Object3D.DEFAULT_UP} - that is `(0, 1, 0)`. + */ + up: Vector3; + + /** + * Object's local position. + * @defaultValue `new THREE.Vector3()` - that is `(0, 0, 0)`. + */ + readonly position: Vector3; + + /** + * Object's local rotation ({@link https://en.wikipedia.org/wiki/Euler_angles | Euler angles}), in radians. + * @defaultValue `new THREE.Euler()` - that is `(0, 0, 0, Euler.DEFAULT_ORDER)`. + */ + readonly rotation: Euler; + + /** + * Object's local rotation as a {@link THREE.Quaternion | Quaternion}. + * @defaultValue `new THREE.Quaternion()` - that is `(0, 0, 0, 1)`. + */ + readonly quaternion: Quaternion; + + /** + * The object's local scale. + * @defaultValue `new THREE.Vector3( 1, 1, 1 )` + */ + readonly scale: Vector3; + + /** + * @defaultValue `new THREE.Matrix4()` + */ + readonly modelViewMatrix: Matrix4; + + /** + * @defaultValue `new THREE.Matrix3()` + */ + readonly normalMatrix: Matrix3; + + /** + * The local transform matrix. + * @defaultValue `new THREE.Matrix4()` + */ + matrix: Matrix4; + + /** + * The global transform of the object. + * @remarks If the {@link Object3D} has no parent, then it's identical to the local transform {@link THREE.Object3D.matrix | .matrix}. + * @defaultValue `new THREE.Matrix4()` + */ + matrixWorld: Matrix4; + + /** + * When this is set, it calculates the matrix of position, (rotation or quaternion) and + * scale every frame and also recalculates the matrixWorld property. + * @defaultValue {@link DEFAULT_MATRIX_AUTO_UPDATE} - that is `(true)`. + */ + matrixAutoUpdate: boolean; + + /** + * If set, then the renderer checks every frame if the object and its children need matrix updates. + * When it isn't, then you have to maintain all matrices in the object and its children yourself. + * @defaultValue {@link DEFAULT_MATRIX_WORLD_AUTO_UPDATE} - that is `(true)`. + */ + matrixWorldAutoUpdate: boolean; + + /** + * When this is set, it calculates the matrixWorld in that frame and resets this property to false. + * @defaultValue `false` + */ + matrixWorldNeedsUpdate: boolean; + + /** + * The layer membership of the object. + * @remarks The object is only visible if it has at least one layer in common with the {@link THREE.Object3DCamera | Camera} in use. + * This property can also be used to filter out unwanted objects in ray-intersection tests when using {@link THREE.Raycaster | Raycaster}. + * @defaultValue `new THREE.Layers()` + */ + layers: Layers; + + /** + * Object gets rendered if `true`. + * @defaultValue `true` + */ + visible: boolean; + + /** + * Whether the object gets rendered into shadow map. + * @defaultValue `false` + */ + castShadow: boolean; + + /** + * Whether the material receives shadows. + * @defaultValue `false` + */ + receiveShadow: boolean; + + /** + * When this is set, it checks every frame if the object is in the frustum of the camera before rendering the object. + * If set to `false` the object gets rendered every frame even if it is not in the frustum of the camera. + * @defaultValue `true` + */ + frustumCulled: boolean; + + /** + * This value allows the default rendering order of {@link https://en.wikipedia.org/wiki/Scene_graph | scene graph} + * objects to be overridden although opaque and transparent objects remain sorted independently. + * @remarks When this property is set for an instance of {@link Group | Group}, all descendants objects will be sorted and rendered together. + * Sorting is from lowest to highest renderOrder. + * @defaultValue `0` + */ + renderOrder: number; + + /** + * Array with object's animation clips. + * @defaultValue `[]` + */ + animations: AnimationClip[]; + + /** + * An object that can be used to store custom data about the {@link Object3D}. + * @remarks It should not hold references to _functions_ as these **will not** be cloned. + * @default `{}` + */ + userData: Record; + + /** + * Custom depth material to be used when rendering to the depth map. + * @remarks Can only be used in context of meshes. + * When shadow-casting with a {@link THREE.DirectionalLight | DirectionalLight} or {@link THREE.SpotLight | SpotLight}, + * if you are modifying vertex positions in the vertex shader you must specify a customDepthMaterial for proper shadows. + * @defaultValue `undefined` + */ + customDepthMaterial?: Material | undefined; + + /** + * Same as {@link customDepthMaterial}, but used with {@link THREE.Object3DPointLight | PointLight}. + * @defaultValue `undefined` + */ + customDistanceMaterial?: Material | undefined; + + /** + * An optional callback that is executed immediately before a 3D object is rendered to a shadow map. + * @remarks This function is called with the following parameters: renderer, scene, camera, shadowCamera, geometry, + * depthMaterial, group. + * Please notice that this callback is only executed for `renderable` 3D objects. Meaning 3D objects which + * define their visual appearance with geometries and materials like instances of {@link Mesh}, {@link Line}, + * {@link Points} or {@link Sprite}. Instances of {@link Object3D}, {@link Group} or {@link Bone} are not renderable + * and thus this callback is not executed for such objects. + */ + onBeforeShadow( + renderer: WebGLRenderer, + scene: Scene, + shadowCamera: Camera, + geometry: BufferGeometry, + depthMaterial: Material, + group: Group, + ): void; + + /** + * An optional callback that is executed immediately after a 3D object is rendered to a shadow map. + * @remarks This function is called with the following parameters: renderer, scene, camera, shadowCamera, geometry, + * depthMaterial, group. + * Please notice that this callback is only executed for `renderable` 3D objects. Meaning 3D objects which + * define their visual appearance with geometries and materials like instances of {@link Mesh}, {@link Line}, + * {@link Points} or {@link Sprite}. Instances of {@link Object3D}, {@link Group} or {@link Bone} are not renderable + * and thus this callback is not executed for such objects. + */ + onAfterShadow( + renderer: WebGLRenderer, + scene: Scene, + shadowCamera: Camera, + geometry: BufferGeometry, + depthMaterial: Material, + group: Group, + ): void; + + /** + * An optional callback that is executed immediately before a 3D object is rendered. + * @remarks This function is called with the following parameters: renderer, scene, camera, geometry, material, group. + * Please notice that this callback is only executed for `renderable` 3D objects. Meaning 3D objects which + * define their visual appearance with geometries and materials like instances of {@link Mesh}, {@link Line}, + * {@link Points} or {@link Sprite}. Instances of {@link Object3D}, {@link Group} or {@link Bone} are not renderable + * and thus this callback is not executed for such objects. + */ + onBeforeRender( + renderer: WebGLRenderer, + scene: Scene, + camera: Camera, + geometry: BufferGeometry, + material: Material, + group: Group, + ): void; + + /** + * An optional callback that is executed immediately after a 3D object is rendered. + * @remarks This function is called with the following parameters: renderer, scene, camera, geometry, material, group. + * Please notice that this callback is only executed for `renderable` 3D objects. Meaning 3D objects which + * define their visual appearance with geometries and materials like instances of {@link Mesh}, {@link Line}, + * {@link Points} or {@link Sprite}. Instances of {@link Object3D}, {@link Group} or {@link Bone} are not renderable + * and thus this callback is not executed for such objects. + */ + onAfterRender( + renderer: WebGLRenderer, + scene: Scene, + camera: Camera, + geometry: BufferGeometry, + material: Material, + group: Group, + ): void; + + /** + * The default {@link up} direction for objects, also used as the default position for {@link THREE.DirectionalLight | DirectionalLight}, + * {@link THREE.HemisphereLight | HemisphereLight} and {@link THREE.Spotlight | Spotlight} (which creates lights shining from the top down). + * @defaultValue `new THREE.Vector3( 0, 1, 0)` + */ + static DEFAULT_UP: Vector3; + + /** + * The default setting for {@link matrixAutoUpdate} for newly created Object3Ds. + * @defaultValue `true` + */ + static DEFAULT_MATRIX_AUTO_UPDATE: boolean; + + /** + * The default setting for {@link matrixWorldAutoUpdate} for newly created Object3Ds. + * @defaultValue `true` + */ + static DEFAULT_MATRIX_WORLD_AUTO_UPDATE: boolean; + + /** + * Applies the matrix transform to the object and updates the object's position, rotation and scale. + * @param matrix + */ + applyMatrix4(matrix: Matrix4): void; + + /** + * Applies the rotation represented by the quaternion to the object. + * @param quaternion + */ + applyQuaternion(quaternion: Quaternion): this; + + /** + * Calls {@link THREE.Quaternion.setFromAxisAngle | setFromAxisAngle}({@link axis}, {@link angle}) on the {@link quaternion | .quaternion}. + * @param axis A normalized vector in object space. + * @param angle Angle in radians. Expects a `Float` + */ + setRotationFromAxisAngle(axis: Vector3, angle: number): void; + + /** + * Calls {@link THREE.Quaternion.setFromEuler | setFromEuler}({@link euler}) on the {@link quaternion | .quaternion}. + * @param euler Euler angle specifying rotation amount. + */ + setRotationFromEuler(euler: Euler): void; + + /** + * Calls {@link THREE.Quaternion.setFromRotationMatrix | setFromRotationMatrix}({@link m}) on the {@link quaternion | .quaternion}. + * @remarks Note that this assumes that the upper 3x3 of m is a pure rotation matrix (i.e, unscaled). + * @param m Rotate the quaternion by the rotation component of the matrix. + */ + setRotationFromMatrix(m: Matrix4): void; + + /** + * Copy the given {@link THREE.Quaternion | Quaternion} into {@link quaternion | .quaternion}. + * @param q Normalized Quaternion. + */ + setRotationFromQuaternion(q: Quaternion): void; + + /** + * Rotate an object along an axis in object space. + * @remarks The axis is assumed to be normalized. + * @param axis A normalized vector in object space. + * @param angle The angle in radians. Expects a `Float` + */ + rotateOnAxis(axis: Vector3, angle: number): this; + + /** + * Rotate an object along an axis in world space. + * @remarks The axis is assumed to be normalized + * Method Assumes no rotated parent. + * @param axis A normalized vector in world space. + * @param angle The angle in radians. Expects a `Float` + */ + rotateOnWorldAxis(axis: Vector3, angle: number): this; + + /** + * Rotates the object around _x_ axis in local space. + * @param rad The angle to rotate in radians. Expects a `Float` + */ + rotateX(angle: number): this; + + /** + * Rotates the object around _y_ axis in local space. + * @param rad The angle to rotate in radians. Expects a `Float` + */ + rotateY(angle: number): this; + + /** + * Rotates the object around _z_ axis in local space. + * @param rad The angle to rotate in radians. Expects a `Float` + */ + rotateZ(angle: number): this; + + /** + * Translate an object by distance along an axis in object space + * @remarks The axis is assumed to be normalized. + * @param axis A normalized vector in object space. + * @param distance The distance to translate. Expects a `Float` + */ + translateOnAxis(axis: Vector3, distance: number): this; + + /** + * Translates object along x axis in object space by {@link distance} units. + * @param distance Expects a `Float` + */ + translateX(distance: number): this; + + /** + * Translates object along _y_ axis in object space by {@link distance} units. + * @param distance Expects a `Float` + */ + translateY(distance: number): this; + + /** + * Translates object along _z_ axis in object space by {@link distance} units. + * @param distance Expects a `Float` + */ + translateZ(distance: number): this; + + /** + * Converts the vector from this object's local space to world space. + * @param vector A vector representing a position in this object's local space. + */ + localToWorld(vector: Vector3): Vector3; + + /** + * Converts the vector from world space to this object's local space. + * @param vector A vector representing a position in world space. + */ + worldToLocal(vector: Vector3): Vector3; + + /** + * Rotates the object to face a point in world space. + * @remarks This method does not support objects having non-uniformly-scaled parent(s). + * @param vector A vector representing a position in world space to look at. + */ + lookAt(vector: Vector3): void; + /** + * Rotates the object to face a point in world space. + * @remarks This method does not support objects having non-uniformly-scaled parent(s). + * @param x Expects a `Float` + * @param y Expects a `Float` + * @param z Expects a `Float` + */ + lookAt(x: number, y: number, z: number): void; + + /** + * Adds another {@link Object3D} as child of this {@link Object3D}. + * @remarks An arbitrary number of objects may be added + * Any current parent on an {@link object} passed in here will be removed, since an {@link Object3D} can have at most one parent. + * @see {@link attach} + * @see {@link THREE.Group | Group} for info on manually grouping objects. + * @param object + */ + add(...object: Object3D[]): this; + + /** + * Removes a {@link Object3D} as child of this {@link Object3D}. + * @remarks An arbitrary number of objects may be removed. + * @see {@link THREE.Group | Group} for info on manually grouping objects. + * @param object + */ + remove(...object: Object3D[]): this; + + /** + * Removes this object from its current parent. + */ + removeFromParent(): this; + + /** + * Removes all child objects. + */ + clear(): this; + + /** + * Adds a {@link Object3D} as a child of this, while maintaining the object's world transform. + * @remarks Note: This method does not support scene graphs having non-uniformly-scaled nodes(s). + * @see {@link add} + * @param object + */ + attach(object: Object3D): this; + + /** + * Searches through an object and its children, starting with the object itself, and returns the first with a matching id. + * @remarks Note that ids are assigned in chronological order: 1, 2, 3, ..., incrementing by one for each new object. + * @see {@link id} + * @param id Unique number of the object instance. Expects a `Integer` + */ + getObjectById(id: number): Object3D | undefined; + + /** + * Searches through an object and its children, starting with the object itself, and returns the first with a matching name. + * @remarks Note that for most objects the name is an empty string by default + * You will have to set it manually to make use of this method. + * @param name String to match to the children's Object3D.name property. + */ + getObjectByName(name: string): Object3D | undefined; + + /** + * Searches through an object and its children, starting with the object itself, + * and returns the first with a property that matches the value given. + * + * @param name - the property name to search for. + * @param value - value of the given property. + */ + getObjectByProperty(name: string, value: any): Object3D | undefined; + + /** + * Searches through an object and its children, starting with the object itself, + * and returns the first with a property that matches the value given. + * @param name The property name to search for. + * @param value Value of the given property. + * @param optionalTarget target to set the result. Otherwise a new Array is instantiated. If set, you must clear + * this array prior to each call (i.e., array.length = 0;). + */ + getObjectsByProperty(name: string, value: any, optionalTarget?: Object3D[]): Object3D[]; + + /** + * Returns a vector representing the position of the object in world space. + * @param target The result will be copied into this Vector3. + */ + getWorldPosition(target: Vector3): Vector3; + + /** + * Returns a quaternion representing the rotation of the object in world space. + * @param target The result will be copied into this Quaternion. + */ + getWorldQuaternion(target: Quaternion): Quaternion; + + /** + * Returns a vector of the scaling factors applied to the object for each axis in world space. + * @param target The result will be copied into this Vector3. + */ + getWorldScale(target: Vector3): Vector3; + + /** + * Returns a vector representing the direction of object's positive z-axis in world space. + * @param target The result will be copied into this Vector3. + */ + getWorldDirection(target: Vector3): Vector3; + + /** + * Abstract (empty) method to get intersections between a casted ray and this object + * @remarks Subclasses such as {@link THREE.Mesh | Mesh}, {@link THREE.Line | Line}, and {@link THREE.Points | Points} implement this method in order to use raycasting. + * @see {@link THREE.Raycaster | Raycaster} + * @param raycaster + * @param intersects + * @defaultValue `() => {}` + */ + raycast(raycaster: Raycaster, intersects: Intersection[]): void; + + /** + * Executes the callback on this object and all descendants. + * @remarks Note: Modifying the scene graph inside the callback is discouraged. + * @param callback A function with as first argument an {@link Object3D} object. + */ + traverse(callback: (object: Object3D) => any): void; + + /** + * Like traverse, but the callback will only be executed for visible objects + * @remarks Descendants of invisible objects are not traversed. + * Note: Modifying the scene graph inside the callback is discouraged. + * @param callback A function with as first argument an {@link Object3D} object. + */ + traverseVisible(callback: (object: Object3D) => any): void; + + /** + * Executes the callback on all ancestors. + * @remarks Note: Modifying the scene graph inside the callback is discouraged. + * @param callback A function with as first argument an {@link Object3D} object. + */ + traverseAncestors(callback: (object: Object3D) => any): void; + + /** + * Updates local transform. + */ + updateMatrix(): void; + + /** + * Updates the global transform of the object. + * And will update the object descendants if {@link matrixWorldNeedsUpdate | .matrixWorldNeedsUpdate} is set to true or if the {@link force} parameter is set to `true`. + * @param force A boolean that can be used to bypass {@link matrixWorldAutoUpdate | .matrixWorldAutoUpdate}, to recalculate the world matrix of the object and descendants on the current frame. + * Useful if you cannot wait for the renderer to update it on the next frame, assuming {@link matrixWorldAutoUpdate | .matrixWorldAutoUpdate} set to `true`. + */ + updateMatrixWorld(force?: boolean): void; + + /** + * Updates the global transform of the object. + * @param updateParents Recursively updates global transform of ancestors. + * @param updateChildren Recursively updates global transform of descendants. + */ + updateWorldMatrix(updateParents: boolean, updateChildren: boolean): void; + + /** + * Convert the object to three.js {@link https://github.com/mrdoob/three.js/wiki/JSON-Object-Scene-format-4 | JSON Object/Scene format}. + * @param meta Object containing metadata such as materials, textures or images for the object. + */ + toJSON(meta?: JSONMeta): Object3DJSON; + + /** + * Returns a clone of `this` object and optionally all descendants. + * @param recursive If true, descendants of the object are also cloned. Default `true` + */ + clone(recursive?: boolean): this; + + /** + * Copies the given object into this object. + * @remarks Event listeners and user-defined callbacks ({@link .onAfterRender} and {@link .onBeforeRender}) are not copied. + * @param object + * @param recursive If set to `true`, descendants of the object are copied next to the existing ones. If set to + * `false`, descendants are left unchanged. Default is `true`. + */ + copy(object: Object3D, recursive?: boolean): this; +} diff --git a/src-testing/src/core/Raycaster.d.ts b/src-testing/src/core/Raycaster.d.ts new file mode 100644 index 000000000..f3c4d44a0 --- /dev/null +++ b/src-testing/src/core/Raycaster.d.ts @@ -0,0 +1,207 @@ +import { Camera } from "../cameras/Camera.js"; +import { Ray } from "../math/Ray.js"; +import { Vector2 } from "../math/Vector2.js"; +import { Vector3 } from "../math/Vector3.js"; +import { XRTargetRaySpace } from "../renderers/webxr/WebXRController.js"; +import { Layers } from "./Layers.js"; +import { Object3D } from "./Object3D.js"; + +export interface Face { + a: number; + b: number; + c: number; + normal: Vector3; + materialIndex: number; +} + +export interface Intersection { + /** Distance between the origin of the ray and the intersection */ + distance: number; + distanceToRay?: number | undefined; + /** Point of intersection, in world coordinates */ + point: Vector3; + index?: number | undefined; + /** Intersected face */ + face?: Face | null | undefined; + /** Index of the intersected face */ + faceIndex?: number | undefined; + /** The intersected object */ + object: TIntersected; + uv?: Vector2 | undefined; + uv1?: Vector2 | undefined; + normal?: Vector3; + /** The index number of the instance where the ray intersects the {@link THREE.InstancedMesh | InstancedMesh } */ + instanceId?: number | undefined; + pointOnLine?: Vector3; + batchId?: number; +} + +export interface RaycasterParameters { + Mesh: any; + Line: { threshold: number }; + Line2?: { threshold: number }; + LOD: any; + Points: { threshold: number }; + Sprite: any; +} + +/** + * This class is designed to assist with {@link https://en.wikipedia.org/wiki/Ray_casting | raycasting} + * @remarks + * Raycasting is used for mouse picking (working out what objects in the 3d space the mouse is over) amongst other things. + * @example + * ```typescript + * const raycaster = new THREE.Raycaster(); + * const pointer = new THREE.Vector2(); + * + * function onPointerMove(event) { + * // calculate pointer position in normalized device coordinates (-1 to +1) for both components + * pointer.x = (event.clientX / window.innerWidth) * 2 - 1; + * pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; + * } + * + * function render() { + * // update the picking ray with the camera and pointer position + * raycaster.setFromCamera(pointer, camera); + * // calculate objects intersecting the picking ray + * const intersects = raycaster.intersectObjects(scene.children); + * for (let i = 0; i & lt; intersects.length; i++) { + * intersects[i].object.material.color.set(0xff0000); + * } + * renderer.render(scene, camera); + * } + * window.addEventListener('pointermove', onPointerMove); + * window.requestAnimationFrame(render); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_interactive_cubes | Raycasting to a Mesh} + * @see Example: {@link https://threejs.org/examples/#webgl_interactive_cubes_ortho | Raycasting to a Mesh in using an OrthographicCamera} + * @see Example: {@link https://threejs.org/examples/#webgl_interactive_buffergeometry | Raycasting to a Mesh with BufferGeometry} + * @see Example: {@link https://threejs.org/examples/#webgl_instancing_raycast | Raycasting to a InstancedMesh} + * @see Example: {@link https://threejs.org/examples/#webgl_interactive_lines | Raycasting to a Line} + * @see Example: {@link https://threejs.org/examples/#webgl_interactive_raycasting_points | Raycasting to Points} + * @see Example: {@link https://threejs.org/examples/#webgl_geometry_terrain_raycast | Terrain raycasting} + * @see Example: {@link https://threejs.org/examples/#webgl_interactive_voxelpainter | Raycasting to paint voxels} + * @see Example: {@link https://threejs.org/examples/#webgl_raycaster_texture | Raycast to a Texture} + * @see {@link https://threejs.org/docs/index.html#api/en/core/Raycaster | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/Raycaster.js | Source} + */ +export class Raycaster { + /** + * This creates a new {@link Raycaster} object. + * @param origin The origin vector where the ray casts from. Default `new Vector3()` + * @param direction The direction vector that gives direction to the ray. Should be normalized. Default `new Vector3(0, 0, -1)` + * @param near All results returned are further away than near. Near can't be negative. Expects a `Float`. Default `0` + * @param far All results returned are closer than far. Far can't be lower than near. Expects a `Float`. Default `Infinity` + */ + constructor(origin?: Vector3, direction?: Vector3, near?: number, far?: number); + + /** + * The {@link THREE.RaycasterRay | Ray} used for the raycasting. + */ + ray: Ray; + + /** + * The near factor of the raycaster. This value indicates which objects can be discarded based on the distance. + * This value shouldn't be negative and should be smaller than the far property. + * @remarks Expects a `Float` + * @defaultValue `0` + */ + near: number; + + /** + * The far factor of the raycaster. This value indicates which objects can be discarded based on the distance. + * This value shouldn't be negative and should be larger than the near property. + * @remarks Expects a `Float` + * @defaultValue `Infinity` + */ + far: number; + + /** + * The camera to use when raycasting against view-dependent objects such as billboarded objects like {@link THREE.Sprites | Sprites}. + * This field can be set manually or is set when calling {@link setFromCamera}. + * @defaultValue `null` + */ + camera: Camera; + + /** + * Used by {@link Raycaster} to selectively ignore 3D objects when performing intersection tests. + * The following code example ensures that only 3D objects on layer `1` will be honored by the instance of Raycaster. + * ``` + * raycaster.layers.set( 1 ); + * object.layers.enable( 1 ); + * ``` + * @defaultValue `new THREE.Layers()` - See {@link THREE.Layers | Layers}. + */ + layers: Layers; + + /** + * An data object where threshold is the precision of the {@link Raycaster} when intersecting objects, in world units. + * @defaultValue `{ Mesh: {}, Line: { threshold: 1 }, LOD: {}, Points: { threshold: 1 }, Sprite: {} }` + */ + params: RaycasterParameters; + + /** + * Updates the ray with a new origin and direction + * @remarks + * Please note that this method only copies the values from the arguments. + * @param origin The origin vector where the ray casts from. + * @param direction The normalized direction vector that gives direction to the ray. + */ + set(origin: Vector3, direction: Vector3): void; + + /** + * Updates the ray with a new origin and direction. + * @param coords 2D coordinates of the mouse, in normalized device coordinates (NDC)---X and Y components should be between -1 and 1. + * @param camera camera from which the ray should originate + */ + setFromCamera(coords: Vector2, camera: Camera): void; + + /** + * Updates the ray with a new origin and direction. + * @param controller The controller to copy the position and direction from. + */ + setFromXRController(controller: XRTargetRaySpace): this; + + /** + * Checks all intersection between the ray and the object with or without the descendants + * @remarks Intersections are returned sorted by distance, closest first + * @remarks {@link Raycaster} delegates to the {@link Object3D.raycast | raycast} method of the passed object, when evaluating whether the ray intersects the object or not + * This allows {@link THREE.Mesh | meshes} to respond differently to ray casting than {@link THREE.Line | lines} and {@link THREE.Points | pointclouds}. + * **Note** that for meshes, faces must be pointed towards the origin of the {@link Raycaster.ray | ray} in order to be detected; + * intersections of the ray passing through the back of a face will not be detected + * To raycast against both faces of an object, you'll want to set the {@link Mesh.material | material}'s {@link Material.side | side} property to `THREE.DoubleSide`. + * @see {@link intersectObjects | .intersectObjects()}. + * @param object The object to check for intersection with the ray. + * @param recursive If true, it also checks all descendants. Otherwise it only checks intersection with the object. Default `true` + * @param optionalTarget Target to set the result. Otherwise a new {@link Array | Array} is instantiated. + * If set, you must clear this array prior to each call (i.e., array.length = 0;). Default `[]` + * @returns An array of intersections is returned. + */ + intersectObject( + object: Object3D, + recursive?: boolean, + optionalTarget?: Array>, + ): Array>; + + /** + * Checks all intersection between the ray and the objects with or without the descendants + * @remarks Intersections are returned sorted by distance, closest first + * @remarks Intersections are of the same form as those returned by {@link intersectObject | .intersectObject()}. + * @remarks {@link Raycaster} delegates to the {@link Object3D.raycast | raycast} method of the passed object, when evaluating whether the ray intersects the object or not + * This allows {@link THREE.Mesh | meshes} to respond differently to ray casting than {@link THREE.Line | lines} and {@link THREE.Points | pointclouds}. + * **Note** that for meshes, faces must be pointed towards the origin of the {@link Raycaster.ray | ray} in order to be detected; + * intersections of the ray passing through the back of a face will not be detected + * To raycast against both faces of an object, you'll want to set the {@link Mesh.material | material}'s {@link Material.side | side} property to `THREE.DoubleSide`. + * @see {@link intersectObject | .intersectObject()}. + * @param objects The objects to check for intersection with the ray. + * @param recursive If true, it also checks all descendants of the objects. Otherwise it only checks intersection with the objects. Default `true` + * @param optionalTarget Target to set the result. Otherwise a new {@link Array | Array} is instantiated. + * If set, you must clear this array prior to each call (i.e., array.length = 0;). Default `[]` + * @returns An array of intersections is returned. + */ + intersectObjects( + objects: Object3D[], + recursive?: boolean, + optionalTarget?: Array>, + ): Array>; +} diff --git a/src-testing/src/core/RenderTarget.d.ts b/src-testing/src/core/RenderTarget.d.ts new file mode 100644 index 000000000..5268ae370 --- /dev/null +++ b/src-testing/src/core/RenderTarget.d.ts @@ -0,0 +1,96 @@ +import { + ColorSpace, + MagnificationTextureFilter, + MinificationTextureFilter, + PixelFormatGPU, + TextureDataType, + Wrapping, +} from "../constants.js"; +import { Vector4 } from "../math/Vector4.js"; +import { DepthTexture } from "../textures/DepthTexture.js"; +import { Texture } from "../textures/Texture.js"; +import { EventDispatcher } from "./EventDispatcher.js"; + +export interface RenderTargetOptions { + wrapS?: Wrapping | undefined; + wrapT?: Wrapping | undefined; + magFilter?: MagnificationTextureFilter | undefined; + minFilter?: MinificationTextureFilter | undefined; + generateMipmaps?: boolean | undefined; // true + format?: number | undefined; // RGBAFormat + type?: TextureDataType | undefined; // UnsignedByteType + anisotropy?: number | undefined; // 1 + colorSpace?: ColorSpace | undefined; + internalFormat?: PixelFormatGPU | null | undefined; // null + depthBuffer?: boolean | undefined; // true + stencilBuffer?: boolean | undefined; // false + resolveDepthBuffer?: boolean | undefined; // true + resolveStencilBuffer?: boolean | undefined; // true + depthTexture?: DepthTexture | null | undefined; // null + /** + * Defines the count of MSAA samples. Can only be used with WebGL 2. Default is **0**. + * @default 0 + */ + samples?: number | undefined; + count?: number | undefined; +} + +export class RenderTarget extends EventDispatcher<{ dispose: {} }> { + readonly isRenderTarget: true; + + width: number; + height: number; + depth: number; + + scissor: Vector4; + /** + * @default false + */ + scissorTest: boolean; + viewport: Vector4; + textures: TTexture[]; + + /** + * @default true + */ + depthBuffer: boolean; + + /** + * @default false + */ + stencilBuffer: boolean; + + /** + * Defines whether the depth buffer should be resolved when rendering into a multisampled render target. + * @default true + */ + resolveDepthBuffer: boolean; + + /** + * Defines whether the stencil buffer should be resolved when rendering into a multisampled render target. + * This property has no effect when {@link .resolveDepthBuffer} is set to `false`. + * @default true + */ + resolveStencilBuffer: boolean; + + /** + * @default null + */ + depthTexture: DepthTexture | null; + + /** + * Defines the count of MSAA samples. Can only be used with WebGL 2. Default is **0**. + * @default 0 + */ + samples: number; + + constructor(width?: number, height?: number, options?: RenderTargetOptions); + + get texture(): TTexture; + set texture(value: TTexture); + + setSize(width: number, height: number, depth?: number): void; + clone(): this; + copy(source: RenderTarget): this; + dispose(): void; +} diff --git a/src-testing/src/core/Uniform.d.ts b/src-testing/src/core/Uniform.d.ts new file mode 100644 index 000000000..851ae2bf9 --- /dev/null +++ b/src-testing/src/core/Uniform.d.ts @@ -0,0 +1,38 @@ +/** + * Uniforms are global GLSL variables. + * They are passed to shader programs. + * @example + * When declaring a uniform of a {@link THREE.ShaderMaterial | ShaderMaterial}, it is declared by value or by object. + * ```typescript + * uniforms: { + * time: { + * value: 1.0 + * }, + * resolution: new Uniform(new Vector2()) + * }; + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_nodes_materials_instance_uniform | WebGL2 / nodes / materials / instance / uniform} + * @see Example: {@link https://threejs.org/examples/#webgpu_instance_uniform| WebGPU / instance / uniform} + * @see {@link https://threejs.org/docs/index.html#api/en/core/Uniform | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/Uniform.js | Source} + */ +export class Uniform { + /** + * Create a new instance of {@link THREE.Uniform | Uniform} + * @param value An object containing the value to set up the uniform. It's type must be one of the Uniform Types described above. + */ + constructor(value: T); + + /** + * Current value of the uniform. + */ + value: T; + + /** + * Returns a clone of this uniform. + * @remarks + * If the uniform's {@link value} property is an {@link Object | Object} with a `clone()` method, this is used, + * otherwise the value is copied by assignment Array values are **shared** between cloned {@link THREE.UniformUniform | Uniform}s. + */ + clone(): Uniform; +} diff --git a/src-testing/src/core/UniformsGroup.d.ts b/src-testing/src/core/UniformsGroup.d.ts new file mode 100644 index 000000000..4fb30b2c1 --- /dev/null +++ b/src-testing/src/core/UniformsGroup.d.ts @@ -0,0 +1,33 @@ +import { Usage } from "../constants.js"; +import { EventDispatcher } from "./EventDispatcher.js"; +import { Uniform } from "./Uniform.js"; + +/** + * @see Example: {@link https://threejs.org/examples/#webgl2_ubo | WebGL2 / UBO} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/UniformsGroup.js | Source} + */ +export class UniformsGroup extends EventDispatcher<{ dispose: {} }> { + constructor(); + + readonly isUniformsGroup: true; + + id: number; + + usage: Usage; + + uniforms: Array; + + add(uniform: Uniform | Uniform[]): this; + + remove(uniform: Uniform | Uniform[]): this; + + setName(name: string): this; + + setUsage(value: Usage): this; + + dispose(): this; + + copy(source: UniformsGroup): this; + + clone(): UniformsGroup; +} diff --git a/src-testing/src/extras/DataUtils.d.ts b/src-testing/src/extras/DataUtils.d.ts new file mode 100644 index 000000000..fd678dbf4 --- /dev/null +++ b/src-testing/src/extras/DataUtils.d.ts @@ -0,0 +1,22 @@ +/** + * Returns a half precision floating point value from the given single precision floating point value. + * @param val A single precision floating point value. + * @see {@link https://threejs.org/docs/index.html#api/en/extras/DataUtils | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/DataUtils.js | Source} + */ +declare function toHalfFloat(val: number): number; + +/** + * Returns a single precision floating point value from the given half precision floating point value. + * @param val A half precision floating point value. + * @see {@link https://threejs.org/docs/index.html#api/en/extras/DataUtils | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/DataUtils.js | Source} + */ +declare function fromHalfFloat(val: number): number; + +declare const DataUtils: { + toHalfFloat: typeof toHalfFloat; + fromHalfFloat: typeof fromHalfFloat; +}; + +export { DataUtils, fromHalfFloat, toHalfFloat }; diff --git a/src-testing/src/extras/Earcut.d.ts b/src-testing/src/extras/Earcut.d.ts new file mode 100644 index 000000000..623b8ee82 --- /dev/null +++ b/src-testing/src/extras/Earcut.d.ts @@ -0,0 +1,15 @@ +/** + * An implementation of the {@link Earcut} polygon triangulation algorithm + * @remarks + * The code is a port of {@link https://github.com/mapbox/earcut | mapbox/earcut}. + * @see {@link https://threejs.org/docs/index.html#api/en/extras/Earcut | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/Earcut.js | Source} + */ +export const Earcut: { + /** + * Triangulates the given shape definition by returning an array of triangles + * @remarks + * A triangle is defined by three consecutive integers representing vertex indices. + */ + triangulate(data: number[], holeIndices?: number[], dim?: number): number[]; +}; diff --git a/src-testing/src/extras/ImageUtils.d.ts b/src-testing/src/extras/ImageUtils.d.ts new file mode 100644 index 000000000..798fc3e90 --- /dev/null +++ b/src-testing/src/extras/ImageUtils.d.ts @@ -0,0 +1,32 @@ +/** + * A class containing utility functions for images. + * @see {@link https://threejs.org/docs/index.html#api/en/extras/ImageUtils | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/ImageUtils.js | Source} + */ +declare class ImageUtils { + /** + * Returns a data URI containing a representation of the given image. + * @param image The image object. + */ + static getDataURL( + image: HTMLImageElement | HTMLCanvasElement | CanvasImageSource | ImageBitmap | ImageData, + ): string; + + /** + * Converts the given sRGB image data to linear color space. + * @param image + */ + static sRGBToLinear(image: HTMLImageElement | HTMLCanvasElement | ImageBitmap): HTMLCanvasElement; + + /** + * Converts the given sRGB image data to linear color space. + * @param image + */ + static sRGBToLinear(image: ImageData): { + data: ImageData["data"]; + width: ImageData["width"]; + height: ImageData["height"]; + }; +} + +export { ImageUtils }; diff --git a/src-testing/src/extras/PMREMGenerator.d.ts b/src-testing/src/extras/PMREMGenerator.d.ts new file mode 100644 index 000000000..3eb4a15e1 --- /dev/null +++ b/src-testing/src/extras/PMREMGenerator.d.ts @@ -0,0 +1,82 @@ +import { WebGLRenderer } from "../renderers/WebGLRenderer.js"; +import { WebGLRenderTarget } from "../renderers/WebGLRenderTarget.js"; +import { Scene } from "../scenes/Scene.js"; +import { CubeTexture } from "../textures/CubeTexture.js"; +import { Texture } from "../textures/Texture.js"; + +/** + * This class generates a Prefiltered, Mipmapped Radiance Environment Map (PMREM) from a cubeMap environment texture. + * @remarks + * This allows different levels of blur to be quickly accessed based on material roughness + * Unlike a traditional mipmap chain, it only goes down to the LOD_MIN level (above), and then creates extra even more filtered 'mips' at the same LOD_MIN resolution, + * associated with higher roughness levels + * In this way we maintain resolution to smoothly interpolate diffuse lighting while limiting sampling computation. + * @remarks + * Note: The minimum {@link THREE.MeshStandardMaterial | MeshStandardMaterial}'s roughness depends on the size of the provided texture + * If your render has small dimensions or the shiny parts have a lot of curvature, you may still be able to get away with a smaller texture size. + * + * | texture size | minimum roughness | + * |--------------|--------------------| + * | 16 | 0.21 | + * | 32 | 0.15 | + * | 64 | 0.11 | + * | 128 | 0.076 | + * | 256 | 0.054 | + * | 512 | 0.038 | + * | 1024 | 0.027 | + * + * @see {@link https://threejs.org/docs/index.html#api/en/extras/PMREMGenerator | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/PMREMGenerator.js | Source} + */ +export class PMREMGenerator { + /** + * This constructor creates a new PMREMGenerator. + * @param renderer + */ + constructor(renderer: WebGLRenderer); + + /** + * Generates a PMREM from a supplied Scene, which can be faster than using an image if networking bandwidth is low + * @remarks + * Optional near and far planes ensure the scene is rendered in its entirety (the cubeCamera is placed at the origin). + * @param scene The given scene. + * @param sigma Specifies a blur radius in radians to be applied to the scene before PMREM generation. Default `0`. + * @param near The near plane value. Default `0.1`. + * @param far The far plane value. Default `100`. + */ + fromScene(scene: Scene, sigma?: number, near?: number, far?: number): WebGLRenderTarget; + + /** + * Generates a PMREM from an equirectangular texture, which can be either LDR or HDR. The ideal input image size is + * 1k (1024 x 512), as this matches best with the 256 x 256 cubemap output. The smallest supported equirectangular + * image size is 64 x 32. + */ + fromEquirectangular(equirectangular: Texture, renderTarget?: WebGLRenderTarget | null): WebGLRenderTarget; + + /** + * Generates a PMREM from an cubemap texture, which can be either LDR or HDR. The ideal input cube size is + * 256 x 256, as this matches best with the 256 x 256 cubemap output. The smallest supported cube size is 16 x 16. + */ + fromCubemap(cubemap: CubeTexture, renderTarget?: WebGLRenderTarget | null): WebGLRenderTarget; + + /** + * Pre-compiles the cubemap shader + * @remarks + * You can get faster start-up by invoking this method during your texture's network fetch for increased concurrency. + */ + compileCubemapShader(): void; + + /** + * Pre-compiles the equirectangular shader + * @remarks + * You can get faster start-up by invoking this method during your texture's network fetch for increased concurrency. + */ + compileEquirectangularShader(): void; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/extras/ShapeUtils.d.ts b/src-testing/src/extras/ShapeUtils.d.ts new file mode 100644 index 000000000..60fe8aaf9 --- /dev/null +++ b/src-testing/src/extras/ShapeUtils.d.ts @@ -0,0 +1,25 @@ +import { Vector2Like } from "../math/Vector2.js"; + +/** + * A class containing utility functions for shapes. + * @remarks Note that these are all linear functions so it is necessary to calculate separately for x, y (and z, w if present) components of a vector. + * @see {@link https://threejs.org/docs/index.html#api/en/extras/ShapeUtils | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/ShapeUtils.js | Source} + */ +export class ShapeUtils { + /** + * Calculate area of a ( 2D ) contour polygon. + */ + static area(contour: readonly Vector2Like[]): number; + + /** + * Note that this is a linear function so it is necessary to calculate separately for x, y components of a polygon. + * @remarks Used internally by {@link THREE.Path | Path}, {@link THREE.ExtrudeGeometry | ExtrudeGeometry} and {@link THREE.ShapeGeometry | ShapeGeometry}. + */ + static isClockWise(pts: readonly Vector2Like[]): boolean; + + /** + * Used internally by {@link THREE.ExtrudeGeometry | ExtrudeGeometry} and {@link THREE.ShapeGeometry | ShapeGeometry} to calculate faces in shapes with holes. + */ + static triangulateShape(contour: Vector2Like[], holes: Vector2Like[][]): number[][]; +} diff --git a/src-testing/src/extras/TextureUtils.d.ts b/src-testing/src/extras/TextureUtils.d.ts new file mode 100644 index 000000000..254458b43 --- /dev/null +++ b/src-testing/src/extras/TextureUtils.d.ts @@ -0,0 +1,42 @@ +import { CompressedPixelFormat, PixelFormat, TextureDataType } from "../constants.js"; +import { Texture } from "../textures/Texture.js"; + +/** + * Scales the texture as large as possible within its surface without cropping or stretching the texture. The method + * preserves the original aspect ratio of the texture. Akin to CSS `object-fit: contain`. + */ +declare function contain(texture: Texture, aspect: number): Texture; + +/** + * Scales the texture to the smallest possible size to fill the surface, leaving no empty space. The method preserves + * the original aspect ratio of the texture. Akin to CSS `object-fit: cover`. + */ +declare function cover(texture: Texture, aspect: number): Texture; + +/** + * Configures the texture to the default transformation. Akin to CSS `object-fit: fill`. + */ +declare function fill(texture: Texture): Texture; + +/** + * Given the width, height, format, and type of a texture. Determines how many bytes must be used to represent the + * texture. + */ +declare function getByteLength( + width: number, + height: number, + format: PixelFormat | CompressedPixelFormat, + type: TextureDataType, +): number; + +/** + * A class containing utility functions for textures. + */ +declare const TextureUtils: { + contain: typeof contain; + cover: typeof cover; + fill: typeof fill; + getByteLength: typeof getByteLength; +}; + +export { contain, cover, fill, getByteLength, TextureUtils }; diff --git a/src-testing/src/extras/core/Curve.d.ts b/src-testing/src/extras/core/Curve.d.ts new file mode 100644 index 000000000..9e3610903 --- /dev/null +++ b/src-testing/src/extras/core/Curve.d.ts @@ -0,0 +1,162 @@ +import { Vector2 } from "../../math/Vector2.js"; +import { Vector3 } from "../../math/Vector3.js"; + +export interface CurveJSON { + metadata: { version: number; type: string; generator: string }; + arcLengthDivisions: number; + type: string; +} + +/** + * An abstract base class for creating a {@link Curve} object that contains methods for interpolation + * @remarks + * For an array of Curves see {@link THREE.CurvePath | CurvePath}. + * @remarks + * This following curves inherit from THREE.Curve: + * + * **2D curves** + * - {@link THREE.ArcCurve} + * - {@link THREE.CubicBezierCurve} + * - {@link THREE.EllipseCurve} + * - {@link THREE.LineCurve} + * - {@link THREE.QuadraticBezierCurve} + * - {@link THREE.SplineCurve} + * + * **3D curves** + * - {@link THREE.CatmullRomCurve3} + * - {@link THREE.CubicBezierCurve3} + * - {@link THREE.LineCurve3} + * - {@link THREE.QuadraticBezierCurve3} + * + * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/Curve | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/Curve.js | Source} + */ +export abstract class Curve { + protected constructor(); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `Curve` + */ + readonly type: string | "Curve"; + + /** + * This value determines the amount of divisions when calculating the cumulative segment lengths of a {@link Curve} + * via {@link .getLengths}. + * To ensure precision when using methods like {@link .getSpacedPoints}, it is recommended to increase {@link .arcLengthDivisions} if the {@link Curve} is very large. + * @defaultValue `200` + * @remarks Expects a `Integer` + */ + arcLengthDivisions: number; + + /** + * Returns a vector for a given position on the curve. + * @param t A position on the curve. Must be in the range `[ 0, 1 ]`. Expects a `Float` + * @param optionalTarget If specified, the result will be copied into this Vector, otherwise a new Vector will be created. Default `new T`. + */ + getPoint(t: number, optionalTarget?: TVector): TVector; + + /** + * Returns a vector for a given position on the {@link Curve} according to the arc length. + * @param u A position on the {@link Curve} according to the arc length. Must be in the range `[ 0, 1 ]`. Expects a `Float` + * @param optionalTarget If specified, the result will be copied into this Vector, otherwise a new Vector will be created. Default `new T`. + */ + getPointAt(u: number, optionalTarget?: TVector): TVector; + + /** + * Returns a set of divisions `+1` points using {@link .getPoint | getPoint(t)}. + * @param divisions Number of pieces to divide the {@link Curve} into. Expects a `Integer`. Default `5` + */ + getPoints(divisions?: number): TVector[]; + + /** + * Returns a set of divisions `+1` equi-spaced points using {@link .getPointAt | getPointAt(u)}. + * @param divisions Number of pieces to divide the {@link Curve} into. Expects a `Integer`. Default `5` + */ + getSpacedPoints(divisions?: number): TVector[]; + + /** + * Get total {@link Curve} arc length. + */ + getLength(): number; + + /** + * Get list of cumulative segment lengths. + * @param divisions Expects a `Integer` + */ + getLengths(divisions?: number): number[]; + + /** + * Update the cumulative segment distance cache + * @remarks + * The method must be called every time {@link Curve} parameters are changed + * If an updated {@link Curve} is part of a composed {@link Curve} like {@link THREE.CurvePath | CurvePath}, + * {@link .updateArcLengths}() must be called on the composed curve, too. + */ + updateArcLengths(): void; + + /** + * Given u in the range `[ 0, 1 ]`, + * @remarks + * `u` and `t` can then be used to give you points which are equidistant from the ends of the curve, using {@link .getPoint}. + * @param u Expects a `Float` + * @param distance Expects a `Float` + * @returns `t` also in the range `[ 0, 1 ]`. Expects a `Float`. + */ + getUtoTmapping(u: number, distance: number): number; + + /** + * Returns a unit vector tangent at t + * @remarks + * If the derived {@link Curve} does not implement its tangent derivation, two points a small delta apart will be used to find its gradient which seems to give a reasonable approximation. + * @param t A position on the curve. Must be in the range `[ 0, 1 ]`. Expects a `Float` + * @param optionalTarget If specified, the result will be copied into this Vector, otherwise a new Vector will be created. + */ + getTangent(t: number, optionalTarget?: TVector): TVector; + + /** + * Returns tangent at a point which is equidistant to the ends of the {@link Curve} from the point given in {@link .getTangent}. + * @param u A position on the {@link Curve} according to the arc length. Must be in the range `[ 0, 1 ]`. Expects a `Float` + * @param optionalTarget If specified, the result will be copied into this Vector, otherwise a new Vector will be created. + */ + getTangentAt(u: number, optionalTarget?: TVector): TVector; + + /** + * Generates the Frenet Frames + * @remarks + * Requires a {@link Curve} definition in 3D space + * Used in geometries like {@link THREE.TubeGeometry | TubeGeometry} or {@link THREE.ExtrudeGeometry | ExtrudeGeometry}. + * @param segments Expects a `Integer` + * @param closed + */ + computeFrenetFrames( + segments: number, + closed?: boolean, + ): { + tangents: Vector3[]; + normals: Vector3[]; + binormals: Vector3[]; + }; + + /** + * Creates a clone of this instance. + */ + clone(): this; + /** + * Copies another {@link Curve} object to this instance. + * @param source + */ + copy(source: Curve): this; + + /** + * Returns a JSON object representation of this instance. + */ + toJSON(): CurveJSON; + + /** + * Copies the data from the given JSON object to this instance. + * @param json + */ + fromJSON(json: CurveJSON): this; +} diff --git a/src-testing/src/extras/core/CurvePath.d.ts b/src-testing/src/extras/core/CurvePath.d.ts new file mode 100644 index 000000000..3a8577fd1 --- /dev/null +++ b/src-testing/src/extras/core/CurvePath.d.ts @@ -0,0 +1,77 @@ +import { Vector2 } from "../../math/Vector2.js"; +import { Vector3 } from "../../math/Vector3.js"; +import { Curve, CurveJSON } from "./Curve.js"; + +export interface CurvePathJSON extends CurveJSON { + autoClose: boolean; + curves: CurveJSON[]; +} + +/** + * Curved Path - a curve path is simply a array of connected curves, but retains the api of a curve. + * @remarks + * A {@link CurvePath} is simply an array of connected curves, but retains the api of a curve. + * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/CurvePath | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/CurvePath.js | Source} + */ +export class CurvePath extends Curve { + /** + * The constructor take no parameters. + */ + constructor(); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `CurvePath` + */ + override readonly type: string | "CurvePath"; + + /** + * The array of {@link Curve | Curves}. + * @defaultValue `[]` + */ + curves: Array>; + + /** + * Whether or not to automatically close the path. + * @defaultValue false + */ + autoClose: boolean; + + /** + * Add a curve to the {@link .curves} array. + * @param curve + */ + add(curve: Curve): void; + /** + * Adds a {@link LineCurve | lineCurve} to close the path. + */ + closePath(): this; + + getPoint(t: number, optionalTarget?: TVector): TVector; + + /** + * Get list of cumulative curve lengths of the curves in the {@link .curves} array. + */ + getCurveLengths(): number[]; + + /** + * Returns an array of points representing a sequence of curves + * @remarks + * The `division` parameter defines the number of pieces each curve is divided into + * However, for optimization and quality purposes, the actual sampling resolution for each curve depends on its type + * For example, for a {@link THREE.LineCurve | LineCurve}, the returned number of points is always just 2. + * @param divisions Number of pieces to divide the curve into. Expects a `Integer`. Default `12` + */ + override getPoints(divisions?: number): TVector[]; + + /** + * Returns a set of divisions `+1` equi-spaced points using {@link .getPointAt | getPointAt(u)}. + * @param divisions Number of pieces to divide the curve into. Expects a `Integer`. Default `40` + */ + override getSpacedPoints(divisions?: number): TVector[]; + + toJSON(): CurvePathJSON; + fromJSON(json: CurvePathJSON): this; +} diff --git a/src-testing/src/extras/core/Interpolations.d.ts b/src-testing/src/extras/core/Interpolations.d.ts new file mode 100644 index 000000000..9f5ed3784 --- /dev/null +++ b/src-testing/src/extras/core/Interpolations.d.ts @@ -0,0 +1,36 @@ +/** + * Used internally by {@link THREE.SplineCurve | SplineCurve}. + * @param t Interpolation weight. Expects a `Float` + * @param p0 Expects a `Float` + * @param p1 Expects a `Float` + * @param p2 Expects a `Float` + * @param p3 P0, p1, p2, the points defining the spline curve. Expects a `Float` + * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/Interpolations | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/Interpolations.js | Source} + */ +declare function CatmullRom(t: number, p0: number, p1: number, p2: number, p3: number): number; + +/** + * Used internally by {@link THREE.QuadraticBezierCurve3 | QuadraticBezierCurve3} and {@link THREE.QuadraticBezierCurve | QuadraticBezierCurve}. + * @param t Interpolation weight. Expects a `Float` + * @param p0 Expects a `Float` + * @param p1 Expects a `Float` + * @param p2 P0, p1, the starting, control and end points defining the curve. Expects a `Float` + * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/Interpolations | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/Interpolations.js | Source} + */ +declare function QuadraticBezier(t: number, p0: number, p1: number, p2: number): number; + +/** + * Used internally by {@link THREE.CubicBezierCurve3 | CubicBezierCurve3} and {@link THREE.CubicBezierCurve | CubicBezierCurve}. + * @param t Interpolation weight. Expects a `Float` + * @param p0 Expects a `Float` + * @param p1 Expects a `Float` + * @param p2 Expects a `Float` + * @param p3 P0, p1, p2, the starting, control(twice) and end points defining the curve. Expects a `Float` + * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/Interpolations | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/Interpolations.js | Source} + */ +declare function CubicBezier(t: number, p0: number, p1: number, p2: number, p3: number): number; + +export { CatmullRom, CubicBezier, QuadraticBezier }; diff --git a/src-testing/src/extras/core/Path.d.ts b/src-testing/src/extras/core/Path.d.ts new file mode 100644 index 000000000..28be15b9c --- /dev/null +++ b/src-testing/src/extras/core/Path.d.ts @@ -0,0 +1,166 @@ +import { Vector2, Vector2Tuple } from "../../math/Vector2.js"; +import { CurvePath, CurvePathJSON } from "./CurvePath.js"; + +export interface PathJSON extends CurvePathJSON { + currentPoint: Vector2Tuple; +} + +/** + * A 2D {@link Path} representation. + * @remarks + * The class provides methods for creating paths and contours of 2D shapes similar to the 2D Canvas API. + * @example + * ```typescript + * const {@link Path} = new THREE.Path(); + * path.lineTo(0, 0.8); + * path.quadraticCurveTo(0, 1, 0.2, 1); + * path.lineTo(1, 1); + * const points = path.getPoints(); + * const geometry = new THREE.BufferGeometry().setFromPoints(points); + * const material = new THREE.LineBasicMaterial({ + * color: 0xffffff + * }); + * const line = new THREE.Line(geometry, material); + * scene.add(line); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/Path | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/Path.js | Source} + */ +export class Path extends CurvePath { + /** + * Creates a {@link Path} from the points + * @remarks + * The first point defines the offset, then successive points are added to the {@link CurvePath.curves | curves} array as {@link LineCurve | LineCurves}. + * If no points are specified, an empty {@link Path} is created and the {@link .currentPoint} is set to the origin. + * @param points Array of {@link Vector2 | Vector2s}. + */ + constructor(points?: Vector2[]); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `Path` + */ + override readonly type: string | "Path"; + + /** + * The current offset of the path. Any new {@link THREE.Curve | Curve} added will start here. + * @defaultValue `new THREE.Vector2()` + */ + currentPoint: Vector2; + + /** + * Adds an absolutely positioned {@link THREE.EllipseCurve | EllipseCurve} to the path. + * @param x Expects a `Float` + * @param y X, The absolute center of the arc. Expects a `Float` + * @param radius The radius of the arc. Expects a `Float` + * @param startAngle The start angle in radians. Expects a `Float` + * @param endAngle The end angle in radians. Expects a `Float` + * @param clockwise Sweep the arc clockwise. Default `false` + */ + absarc(aX: number, aY: number, aRadius: number, aStartAngle: number, aEndAngle: number, aClockwise?: boolean): this; + + /** + * Adds an absolutely positioned {@link THREE.EllipseCurve | EllipseCurve} to the path. + * @param x Expects a `Float` + * @param y X, The absolute center of the ellipse. Expects a `Float` + * @param xRadius The radius of the ellipse in the x axis. Expects a `Float` + * @param yRadius The radius of the ellipse in the y axis. Expects a `Float` + * @param startAngle The start angle in radians. Expects a `Float` + * @param endAngle The end angle in radians. Expects a `Float` + * @param clockwise Sweep the ellipse clockwise. Default `false` + * @param rotation The rotation angle of the ellipse in radians, counterclockwise from the positive X axis. Optional, Expects a `Float`. Default `0` + */ + absellipse( + aX: number, + aY: number, + xRadius: number, + yRadius: number, + aStartAngle: number, + aEndAngle: number, + aClockwise?: boolean, + aRotation?: number, + ): this; + + /** + * Adds an {@link THREE.EllipseCurve | EllipseCurve} to the path, positioned relative to {@link .currentPoint}. + * @param x Expects a `Float` + * @param y X, The center of the arc offset from the last call. Expects a `Float` + * @param radius The radius of the arc. Expects a `Float` + * @param startAngle The start angle in radians. Expects a `Float` + * @param endAngle The end angle in radians. Expects a `Float` + * @param clockwise Sweep the arc clockwise. Default `false` + */ + arc(aX: number, aY: number, aRadius: number, aStartAngle: number, aEndAngle: number, aClockwise?: boolean): this; + + /** + * This creates a bezier curve from {@link .currentPoint} with (cp1X, cp1Y) and (cp2X, cp2Y) as control points and updates {@link .currentPoint} to x and y. + * @param cp1X Expects a `Float` + * @param cp1Y Expects a `Float` + * @param cp2X Expects a `Float` + * @param cp2Y Expects a `Float` + * @param x Expects a `Float` + * @param y Expects a `Float` + */ + bezierCurveTo(aCP1x: number, aCP1y: number, aCP2x: number, aCP2y: number, aX: number, aY: number): this; + + /** + * Adds an {@link THREE.EllipseCurve | EllipseCurve} to the path, positioned relative to {@link .currentPoint}. + * @param x Expects a `Float` + * @param y X, The center of the ellipse offset from the last call. Expects a `Float` + * @param xRadius The radius of the ellipse in the x axis. Expects a `Float` + * @param yRadius The radius of the ellipse in the y axis. Expects a `Float` + * @param startAngle The start angle in radians. Expects a `Float` + * @param endAngle The end angle in radians. Expects a `Float` + * @param clockwise Sweep the ellipse clockwise. Default `false` + * @param rotation The rotation angle of the ellipse in radians, counterclockwise from the positive X axis. Optional, Expects a `Float`. Default `0` + */ + ellipse( + aX: number, + aY: number, + xRadius: number, + yRadius: number, + aStartAngle: number, + aEndAngle: number, + aClockwise?: boolean, + aRotation?: number, + ): this; + + /** + * Connects a {@link THREE.LineCurve | LineCurve} from {@link .currentPoint} to x, y onto the path. + * @param x Expects a `Float` + * @param y Expects a `Float` + */ + lineTo(x: number, y: number): this; + + /** + * Move the {@link .currentPoint} to x, y. + * @param x Expects a `Float` + * @param y Expects a `Float` + */ + moveTo(x: number, y: number): this; + + /** + * Creates a quadratic curve from {@link .currentPoint} with cpX and cpY as control point and updates {@link .currentPoint} to x and y. + * @param cpX Expects a `Float` + * @param cpY Expects a `Float` + * @param x Expects a `Float` + * @param y Expects a `Float` + */ + quadraticCurveTo(aCPx: number, aCPy: number, aX: number, aY: number): this; + + /** + * Points are added to the {@link CurvePath.curves | curves} array as {@link THREE.LineCurve | LineCurves}. + * @param vector2s + */ + setFromPoints(vectors: Vector2[]): this; + + /** + * Connects a new {@link THREE.SplineCurve | SplineCurve} onto the path. + * @param points An array of {@link Vector2 | Vector2's} + */ + splineThru(pts: Vector2[]): this; + + toJSON(): PathJSON; + fromJSON(json: PathJSON): this; +} diff --git a/src-testing/src/extras/core/Shape.d.ts b/src-testing/src/extras/core/Shape.d.ts new file mode 100644 index 000000000..84e5ff37f --- /dev/null +++ b/src-testing/src/extras/core/Shape.d.ts @@ -0,0 +1,86 @@ +import { Vector2 } from "../../math/Vector2.js"; +import { Path, PathJSON } from "./Path.js"; + +export interface ShapeJSON extends PathJSON { + uuid: string; + holes: PathJSON[]; +} + +/** + * Defines an arbitrary 2d {@link Shape} plane using paths with optional holes + * @remarks + * It can be used with {@link THREE.ExtrudeGeometry | ExtrudeGeometry}, {@link THREE.ShapeGeometry | ShapeGeometry}, to get points, or to get triangulated faces. + * @example + * ```typescript + * const heartShape = new THREE.Shape(); + * heartShape.moveTo(25, 25); + * heartShape.bezierCurveTo(25, 25, 20, 0, 0, 0); + * heartShape.bezierCurveTo(-30, 0, -30, 35, -30, 35); + * heartShape.bezierCurveTo(-30, 55, -10, 77, 25, 95); + * heartShape.bezierCurveTo(60, 77, 80, 55, 80, 35); + * heartShape.bezierCurveTo(80, 35, 80, 0, 50, 0); + * heartShape.bezierCurveTo(35, 0, 25, 25, 25, 25); + * const extrudeSettings = { + * depth: 8, + * bevelEnabled: true, + * bevelSegments: 2, + * steps: 2, + * bevelSize: 1, + * bevelThickness: 1 + * }; + * const geometry = new THREE.ExtrudeGeometry(heartShape, extrudeSettings); + * const mesh = new THREE.Mesh(geometry, new THREE.MeshPhongMaterial()); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_geometry_shapes | geometry / shapes } + * @see Example: {@link https://threejs.org/examples/#webgl_geometry_extrude_shapes | geometry / extrude / shapes } + * @see Example: {@link https://threejs.org/examples/#webgl_geometry_extrude_shapes2 | geometry / extrude / shapes2 } + * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/Shape | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/Shape.js | Source} + */ +export class Shape extends Path { + /** + * Creates a {@link Shape} from the points + * @remarks + * The first point defines the offset, then successive points are added to the {@link CurvePath.curves | curves} array as {@link THREE.LineCurve | LineCurves}. + * If no points are specified, an empty {@link Shape} is created and the {@link .currentPoint} is set to the origin. + * @param points Array of {@link Vector2 | Vector2s}. + */ + constructor(points?: Vector2[]); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `Shape` + */ + override readonly type: string | "Shape"; + + /** + * {@link http://en.wikipedia.org/wiki/Universally_unique_identifier | UUID} of this object instance. + * @remarks This gets automatically assigned and shouldn't be edited. + */ + uuid: string; + + /** + * An array of {@link Path | paths} that define the holes in the shape. + * @defaultValue `[]` + */ + holes: Path[]; + + /** + * Call {@link THREE.Curve.getPoints | getPoints} on the {@link Shape} and the {@link holes} array + * @param divisions The fineness of the result. Expects a `Integer` + */ + extractPoints(divisions: number): { + shape: Vector2[]; + holes: Vector2[][]; + }; + + /** + * Get an array of {@link Vector2 | Vector2's} that represent the holes in the shape. + * @param divisions The fineness of the result. Expects a `Integer` + */ + getPointsHoles(divisions: number): Vector2[][]; + + toJSON(): ShapeJSON; + fromJSON(json: ShapeJSON): this; +} diff --git a/src-testing/src/extras/core/ShapePath.d.ts b/src-testing/src/extras/core/ShapePath.d.ts new file mode 100644 index 000000000..0fcd88312 --- /dev/null +++ b/src-testing/src/extras/core/ShapePath.d.ts @@ -0,0 +1,98 @@ +import { Color } from "../../math/Color.js"; +import { Vector2 } from "../../math/Vector2.js"; +import { Path } from "./Path.js"; +import { Shape } from "./Shape.js"; + +/** + * This class is used to convert a series of shapes to an array of {@link THREE.Path | Path's}, + * for example an SVG shape to a path. + * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/ShapePath | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/ShapePath.js | Source} + */ +export class ShapePath { + /** + * Creates a new {@link ShapePath} + * @remarks + * Unlike a {@link THREE.Path | Path}, no points are passed in as the {@link ShapePath} is designed to be generated after creation. + */ + constructor(); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `ShapePath` + */ + readonly type: "ShapePath"; + + /** + * Array of {@link THREE.Path | Path's}s. + * @defaultValue `[]` + */ + subPaths: Path[]; + + /** + * The current {@link THREE.Path | Path} that is being generated. + * @defaultValue `null` + */ + readonly currentPath: Path | null; + + /** + * {@link THREE.Color | Color} of the shape, by default set to white _(0xffffff)_. + * @defaultValue `new THREE.Color()` + */ + color: Color; + + /** + * Starts a new {@link THREE.Path | Path} and calls {@link THREE.Path.moveTo | Path.moveTo}( x, y ) on that {@link THREE.Path | Path} + * @remarks + * Also points {@link ShapePath.currentPath | currentPath} to that {@link THREE.Path | Path}. + * @param x Expects a `Float` + * @param y Expects a `Float` + */ + moveTo(x: number, y: number): this; + + /** + * This creates a line from the {@link ShapePath.currentPath | currentPath}'s offset to X and Y and updates the offset to X and Y. + * @param x Expects a `Float` + * @param y Expects a `Float` + */ + lineTo(x: number, y: number): this; + + /** + * This creates a quadratic curve from the {@link ShapePath.currentPath | currentPath}'s + * offset to _x_ and _y_ with _cpX_ and _cpY_ as control point and updates the {@link ShapePath.currentPath | currentPath}'s offset to _x_ and _y_. + * @param cpX Expects a `Float` + * @param cpY Expects a `Float` + * @param x Expects a `Float` + * @param y Expects a `Float` + */ + quadraticCurveTo(aCPx: number, aCPy: number, aX: number, aY: number): this; + + /** + * This creates a bezier curve from the {@link ShapePath.currentPath | currentPath}'s + * offset to _x_ and _y_ with _cp1X_, _cp1Y_ and _cp2X_, _cp2Y_ as control points and + * updates the {@link ShapePath.currentPath | currentPath}'s offset to _x_ and _y_. + * @param cp1X Expects a `Float` + * @param cp1Y Expects a `Float` + * @param cp2X Expects a `Float` + * @param cp2Y Expects a `Float` + * @param x Expects a `Float` + * @param y Expects a `Float` + */ + bezierCurveTo(aCP1x: number, aCP1y: number, aCP2x: number, aCP2y: number, aX: number, aY: number): this; + + /** + * Connects a new {@link THREE.SplineCurve | SplineCurve} onto the {@link ShapePath.currentPath | currentPath}. + * @param points An array of {@link THREE.Vector2 | Vector2}s + */ + splineThru(pts: Vector2[]): this; + + /** + * Converts the {@link ShapePath.subPaths | subPaths} array into an array of Shapes + * @remarks + * By default solid shapes are defined clockwise (CW) and holes are defined counterclockwise (CCW) + * If isCCW is set to true, then those are flipped. + * @param isCCW Changes how solids and holes are generated + */ + toShapes(isCCW: boolean): Shape[]; +} diff --git a/src-testing/src/extras/curves/ArcCurve.d.ts b/src-testing/src/extras/curves/ArcCurve.d.ts new file mode 100644 index 000000000..0932f7ffb --- /dev/null +++ b/src-testing/src/extras/curves/ArcCurve.d.ts @@ -0,0 +1,41 @@ +import { EllipseCurve } from "./EllipseCurve.js"; + +/** + * Alias for {@link THREE.EllipseCurve | EllipseCurve}. + * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/ArcCurve | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/ArcCurve.js | Source} + */ +export class ArcCurve extends EllipseCurve { + /** + * This constructor creates a new {@link ArcCurve}. + * @param aX The X center of the ellipse. Expects a `Float`. Default is `0`. + * @param aY The Y center of the ellipse. Expects a `Float`. Default is `0`. + * @param xRadius The radius of the ellipse in the x direction. Expects a `Float`. Default is `1`. + * @param yRadius The radius of the ellipse in the y direction. Expects a `Float`. Default is `1`. + * @param aStartAngle The start angle of the curve in radians starting from the positive X axis. Default is `0`. + * @param aEndAngle The end angle of the curve in radians starting from the positive X axis. Default is `2 x Math.PI`. + * @param aClockwise Whether the ellipse is drawn clockwise. Default is `false`. + */ + constructor( + aX?: number, + aY?: number, + aRadius?: number, + aStartAngle?: number, + aEndAngle?: number, + aClockwise?: boolean, + ); + + /** + * Read-only flag to check if a given object is of type {@link ArcCurve}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isArcCurve = true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `ArcCurve` + */ + override readonly type: string | "ArcCurve"; +} diff --git a/src-testing/src/extras/curves/CatmullRomCurve3.d.ts b/src-testing/src/extras/curves/CatmullRomCurve3.d.ts new file mode 100644 index 000000000..55088e412 --- /dev/null +++ b/src-testing/src/extras/curves/CatmullRomCurve3.d.ts @@ -0,0 +1,77 @@ +import { Vector3 } from "../../math/Vector3.js"; +import { Curve } from "../core/Curve.js"; + +export type CurveType = "centripetal" | "chordal" | "catmullrom"; + +/** + * Create a smooth **3D** spline curve from a series of points using the {@link https://en.wikipedia.org/wiki/Centripetal_Catmull-Rom_spline | Catmull-Rom} algorithm. + * @example + * ```typescript + * //Create a closed wavey loop + * const curve = new THREE.CatmullRomCurve3([ + * new THREE.Vector3(-10, 0, 10), + * new THREE.Vector3(-5, 5, 5), + * new THREE.Vector3(0, 0, 0), + * new THREE.Vector3(5, -5, 5), + * new THREE.Vector3(10, 0, 10)]); + * const points = curve.getPoints(50); + * const geometry = new THREE.BufferGeometry().setFromPoints(points); + * const material = new THREE.LineBasicMaterial({ + * color: 0xff0000 + * }); + * // Create the final object to add to the scene + * const curveObject = new THREE.Line(geometry, material); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_geometry_extrude_splines | WebGL / geometry / extrude / splines} + * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/CatmullRomCurve3 | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/CatmullRomCurve3.js | Source} + */ +export class CatmullRomCurve3 extends Curve { + /** + * This constructor creates a new {@link CatmullRomCurve3}. + * @param points An array of {@link THREE.Vector3 | Vector3} points + * @param closed Whether the curve is closed. Default `false` + * @param curveType Type of the curve. Default `centripetal` + * @param tension Tension of the curve. Expects a `Float`. Default `0.5` + */ + constructor(points?: Vector3[], closed?: boolean, curveType?: CurveType, tension?: number); + + /** + * Read-only flag to check if a given object is of type {@link CatmullRomCurve3}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isCatmullRomCurve3 = true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `CatmullRomCurve3` + */ + override readonly type: string | "CatmullRomCurve3"; + + /** + * The curve will loop back onto itself when this is true. + * @defaultValue `false` + */ + closed: boolean; + + /** + * The array of {@link THREE.Vector3 | Vector3} points that define the curve. + * @remarks It needs at least two entries. + * @defaultValue `[]` + */ + points: Vector3[]; + + /** + * Possible values are `centripetal`, `chordal` and `catmullrom`. + * @defaultValue `centripetal` + */ + curveType: CurveType; + + /** + * When {@link .curveType} is `catmullrom`, defines catmullrom's tension. + * @remarks Expects a `Float` + */ + tension: number; +} diff --git a/src-testing/src/extras/curves/CubicBezierCurve.d.ts b/src-testing/src/extras/curves/CubicBezierCurve.d.ts new file mode 100644 index 000000000..ae4518312 --- /dev/null +++ b/src-testing/src/extras/curves/CubicBezierCurve.d.ts @@ -0,0 +1,72 @@ +import { Vector2 } from "../../math/Vector2.js"; +import { Curve } from "../core/Curve.js"; + +/** + * Create a smooth **2D** {@link http://en.wikipedia.org/wiki/B%C3%A9zier_curve#mediaviewer/File:Bezier_curve.svg | cubic bezier curve}, + * defined by a start point, endpoint and two control points. + * @example + * ```typescript + * const curve = new THREE.CubicBezierCurve( + * new THREE.Vector2(-10, 0), + * new THREE.Vector2(-5, 15), + * new THREE.Vector2(20, 15), + * new THREE.Vector2(10, 0)); + * const points = curve.getPoints(50); + * const geometry = new THREE.BufferGeometry().setFromPoints(points); + * const material = new THREE.LineBasicMaterial({ + * color: 0xff0000 + * }); + * // Create the final object to add to the scene + * const curveObject = new THREE.Line(geometry, material); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/CubicBezierCurve | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/CubicBezierCurve.js | Source} + */ +export class CubicBezierCurve extends Curve { + /** + * This constructor creates a new {@link CubicBezierCurve}. + * @param v0 The starting point. Default is `new THREE.Vector2()`. + * @param v1 The first control point. Default is `new THREE.Vector2()`. + * @param v2 The second control point. Default is `new THREE.Vector2()`. + * @param v3 The ending point. Default is `new THREE.Vector2()`. + */ + constructor(v0?: Vector2, v1?: Vector2, v2?: Vector2, v3?: Vector2); + + /** + * Read-only flag to check if a given object is of type {@link CubicBezierCurve}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isCubicBezierCurve = true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `CubicBezierCurve` + */ + override readonly type: string | "CubicBezierCurve"; + + /** + * The starting point. + * @defaultValue `new THREE.Vector2()` + */ + v0: Vector2; + + /** + * The first control point. + * @defaultValue `new THREE.Vector2()` + */ + v1: Vector2; + + /** + * The second control point. + * @defaultValue `new THREE.Vector2()` + */ + v2: Vector2; + + /** + * The ending point. + * @defaultValue `new THREE.Vector2()` + */ + v3: Vector2; +} diff --git a/src-testing/src/extras/curves/CubicBezierCurve3.d.ts b/src-testing/src/extras/curves/CubicBezierCurve3.d.ts new file mode 100644 index 000000000..ad8edb356 --- /dev/null +++ b/src-testing/src/extras/curves/CubicBezierCurve3.d.ts @@ -0,0 +1,72 @@ +import { Vector3 } from "../../math/Vector3.js"; +import { Curve } from "../core/Curve.js"; + +/** + * Create a smooth **3D** {@link http://en.wikipedia.org/wiki/B%C3%A9zier_curve#mediaviewer/File:Bezier_curve.svg | cubic bezier curve}, + * defined by a start point, endpoint and two control points. + * @example + * ```typescript + * const curve = new THREE.CubicBezierCurve( + * new THREE.Vector2(-10, 0), + * new THREE.Vector2(-5, 15), + * new THREE.Vector2(20, 15), + * new THREE.Vector2(10, 0)); + * const points = curve.getPoints(50); + * const geometry = new THREE.BufferGeometry().setFromPoints(points); + * const material = new THREE.LineBasicMaterial({ + * color: 0xff0000 + * }); + * // Create the final object to add to the scene + * const curveObject = new THREE.Line(geometry, material); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/CubicBezierCurve | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/CubicBezierCurve.js | Source} + */ +export class CubicBezierCurve3 extends Curve { + /** + * This constructor creates a new {@link CubicBezierCurve3}. + * @param v0 The starting point. Default is `new THREE.Vector3()`. + * @param v1 The first control point. Default is `new THREE.Vector3()`. + * @param v2 The second control point. Default is `new THREE.Vector3()`. + * @param v3 The ending point. Default is `new THREE.Vector3()`. + */ + constructor(v0?: Vector3, v1?: Vector3, v2?: Vector3, v3?: Vector3); + + /** + * Read-only flag to check if a given object is of type {@link CubicBezierCurve3}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isCubicBezierCurve3 = true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `CubicBezierCurve3` + */ + override readonly type: string | "CubicBezierCurve3"; + + /** + * The starting point. + * @defaultValue `new THREE.Vector3()`. + */ + v0: Vector3; + + /** + * The first control point. + * @defaultValue `new THREE.Vector3()`. + */ + v1: Vector3; + + /** + * The second control point. + * @defaultValue `new THREE.Vector3()`. + */ + v2: Vector3; + + /** + * The ending point. + * @defaultValue `new THREE.Vector3()`. + */ + v3: Vector3; +} diff --git a/src-testing/src/extras/curves/Curves.d.ts b/src-testing/src/extras/curves/Curves.d.ts new file mode 100644 index 000000000..996021d72 --- /dev/null +++ b/src-testing/src/extras/curves/Curves.d.ts @@ -0,0 +1,10 @@ +export * from "./ArcCurve.js"; +export * from "./CatmullRomCurve3.js"; +export * from "./CubicBezierCurve.js"; +export * from "./CubicBezierCurve3.js"; +export * from "./EllipseCurve.js"; +export * from "./LineCurve.js"; +export * from "./LineCurve3.js"; +export * from "./QuadraticBezierCurve.js"; +export * from "./QuadraticBezierCurve3.js"; +export * from "./SplineCurve.js"; diff --git a/src-testing/src/extras/curves/EllipseCurve.d.ts b/src-testing/src/extras/curves/EllipseCurve.d.ts new file mode 100644 index 000000000..4d15ca8c2 --- /dev/null +++ b/src-testing/src/extras/curves/EllipseCurve.d.ts @@ -0,0 +1,115 @@ +import { Vector2 } from "../../math/Vector2.js"; +import { Curve } from "../core/Curve.js"; + +/** + * Creates a 2d curve in the shape of an ellipse + * @remarks + * Setting the {@link xRadius} equal to the {@link yRadius} will result in a circle. + * @example + * ```typescript + * const curve = new THREE.EllipseCurve( + * 0, 0, // ax, aY + * 10, 10, // xRadius, yRadius + * 0, 2 * Math.PI, // aStartAngle, aEndAngle + * false, // aClockwise + * 0 // aRotation + * ); + * const points = curve.getPoints(50); + * const geometry = new THREE.BufferGeometry().setFromPoints(points); + * const material = new THREE.LineBasicMaterial({ color: 0xff0000 }); + * // Create the final object to add to the scene + * const ellipse = new THREE.Line(geometry, material); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/EllipseCurve | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/EllipseCurve.js | Source} + */ +export class EllipseCurve extends Curve { + /** + * This constructor creates a new {@link EllipseCurve}. + * @param aX The X center of the ellipse. Expects a `Float`. Default is `0`. + * @param aY The Y center of the ellipse. Expects a `Float`. Default is `0`. + * @param xRadius The radius of the ellipse in the x direction. Expects a `Float`. Default is `1`. + * @param yRadius The radius of the ellipse in the y direction. Expects a `Float`. Default is `1`. + * @param aStartAngle The start angle of the curve in radians starting from the positive X axis. Default is `0`. + * @param aEndAngle The end angle of the curve in radians starting from the positive X axis. Default is `2 x Math.PI`. + * @param aClockwise Whether the ellipse is drawn clockwise. Default is `false`. + * @param aRotation The rotation angle of the ellipse in radians, counterclockwise from the positive X axis. Default is `0`. + */ + constructor( + aX?: number, + aY?: number, + xRadius?: number, + yRadius?: number, + aStartAngle?: number, + aEndAngle?: number, + aClockwise?: boolean, + aRotation?: number, + ); + + /** + * Read-only flag to check if a given object is of type {@link EllipseCurve}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isEllipseCurve = true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `EllipseCurve` + */ + override readonly type: string | "EllipseCurve"; + + /** + * The X center of the ellipse. + * @remarks Expects a `Float` + * @defaultValue `0` + */ + aX: number; + + /** + * The Y center of the ellipse. + * @remarks Expects a `Float` + * @defaultValue `0` + */ + aY: number; + + /** + * The radius of the ellipse in the x direction. + * @defaultValue `1` + */ + xRadius: number; + + /** + * The radius of the ellipse in the y direction. + * @defaultValue `1` + */ + yRadius: number; + + /** + * The start angle of the curve in radians starting from the middle right side. + * @remarks Expects a `Float` + * @defaultValue `0` + */ + aStartAngle: number; + + /** + * The end angle of the curve in radians starting from the middle right side. + * @remarks Expects a `Float` + * @defaultValue `2 * Math.PI` + */ + aEndAngle: number; + + /** + * Whether the ellipse is drawn clockwise. + * @defaultValue `false`` + */ + aClockwise: boolean; + + /** + * The rotation angle of the ellipse in radians, counterclockwise from the positive X axis (optional). + * @remarks Expects a `Float` + * @defaultValue `0` + */ + aRotation: number; +} diff --git a/src-testing/src/extras/curves/LineCurve.d.ts b/src-testing/src/extras/curves/LineCurve.d.ts new file mode 100644 index 000000000..3bcb000d3 --- /dev/null +++ b/src-testing/src/extras/curves/LineCurve.d.ts @@ -0,0 +1,42 @@ +import { Vector2 } from "../../math/Vector2.js"; +import { Curve } from "../core/Curve.js"; + +/** + * A curve representing a **2D** line segment. + * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/LineCurve | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/LineCurve.js | Source} + */ +export class LineCurve extends Curve { + /** + * This constructor creates a new {@link LineCurve}. + * @param v1 The start point. Default is `new THREE.Vector2()`. + * @param v2 The end point. Default is `new THREE.Vector2()`. + */ + constructor(v1?: Vector2, v2?: Vector2); + + /** + * Read-only flag to check if a given object is of type {@link LineCurve}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isLineCurve = true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `LineCurve` + */ + override readonly type: string | "LineCurve"; + + /** + * The start point. + * @defaultValue `new THREE.Vector2()` + */ + v1: Vector2; + + /** + * The end point + * @defaultValue `new THREE.Vector2()` + */ + v2: Vector2; +} diff --git a/src-testing/src/extras/curves/LineCurve3.d.ts b/src-testing/src/extras/curves/LineCurve3.d.ts new file mode 100644 index 000000000..c7ae7c301 --- /dev/null +++ b/src-testing/src/extras/curves/LineCurve3.d.ts @@ -0,0 +1,42 @@ +import { Vector3 } from "../../math/Vector3.js"; +import { Curve } from "../core/Curve.js"; + +/** + * A curve representing a **3D** line segment. + * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/LineCurve3 | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/LineCurve3.js | Source} + */ +export class LineCurve3 extends Curve { + /** + * This constructor creates a new {@link LineCurve3}. + * @param v1 The start point. Default is `new THREE.Vector3()`. + * @param v2 The end point. Default is `new THREE.Vector3()`. + */ + constructor(v1?: Vector3, v2?: Vector3); + + /** + * Read-only flag to check if a given object is of type {@link LineCurve3}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isLineCurve3 = true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `LineCurve3` + */ + override readonly type: string | "LineCurve3"; + + /** + * The start point. + * @defaultValue `new THREE.Vector3()`. + */ + v1: Vector3; + + /** + * The end point. + * @defaultValue `new THREE.Vector3()`. + */ + v2: Vector3; +} diff --git a/src-testing/src/extras/curves/QuadraticBezierCurve.d.ts b/src-testing/src/extras/curves/QuadraticBezierCurve.d.ts new file mode 100644 index 000000000..8ae4c3213 --- /dev/null +++ b/src-testing/src/extras/curves/QuadraticBezierCurve.d.ts @@ -0,0 +1,64 @@ +import { Vector2 } from "../../math/Vector2.js"; +import { Curve } from "../core/Curve.js"; + +/** + * Create a smooth **2D** {@link http://en.wikipedia.org/wiki/B%C3%A9zier_curve#mediaviewer/File:B%C3%A9zier_2_big.gif | quadratic bezier curve}, + * defined by a start point, end point and a single control point. + * @example + * ```typescript + * const curve = new THREE.QuadraticBezierCurve( + * new THREE.Vector2(-10, 0), + * new THREE.Vector2(20, 15), + * new THREE.Vector2(10, 0)); + * const points = curve.getPoints(50); + * const geometry = new THREE.BufferGeometry().setFromPoints(points); + * const material = new THREE.LineBasicMaterial({ + * color: 0xff0000 + * }); + * // Create the final object to add to the scene + * const curveObject = new THREE.Line(geometry, material); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/QuadraticBezierCurve | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/QuadraticBezierCurve.js | Source} + */ +export class QuadraticBezierCurve extends Curve { + /** + * This constructor creates a new {@link QuadraticBezierCurve}. + * @param v0 The start point. Default is `new THREE.Vector2()`. + * @param v1 The control point. Default is `new THREE.Vector2()`. + * @param v2 The end point. Default is `new THREE.Vector2()`. + */ + constructor(v0?: Vector2, v1?: Vector2, v2?: Vector2); + + /** + * Read-only flag to check if a given object is of type {@link LineCurve3}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isQuadraticBezierCurve = true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `QuadraticBezierCurve` + */ + override readonly type: string | "QuadraticBezierCurve"; + + /** + * The start point. + * @defaultValue `new THREE.Vector2()` + */ + v0: Vector2; + + /** + * The control point. + * @defaultValue `new THREE.Vector2()` + */ + v1: Vector2; + + /** + * The end point. + * @defaultValue `new THREE.Vector2()` + */ + v2: Vector2; +} diff --git a/src-testing/src/extras/curves/QuadraticBezierCurve3.d.ts b/src-testing/src/extras/curves/QuadraticBezierCurve3.d.ts new file mode 100644 index 000000000..3964af820 --- /dev/null +++ b/src-testing/src/extras/curves/QuadraticBezierCurve3.d.ts @@ -0,0 +1,64 @@ +import { Vector3 } from "../../math/Vector3.js"; +import { Curve } from "../core/Curve.js"; + +/** + * Create a smooth **3D** {@link http://en.wikipedia.org/wiki/B%C3%A9zier_curve#mediaviewer/File:B%C3%A9zier_2_big.gif | quadratic bezier curve}, + * defined by a start point, end point and a single control point. + * @example + * ```typescript + * const curve = new THREE.QuadraticBezierCurve3( + * new THREE.Vector3(-10, 0, 0), + * new THREE.Vector3(20, 15, 0), + * new THREE.Vector3(10, 0, 0)); + * const points = curve.getPoints(50); + * const geometry = new THREE.BufferGeometry().setFromPoints(points); + * const material = new THREE.LineBasicMaterial({ + * color: 0xff0000 + * }); + * // Create the final object to add to the scene + * const curveObject = new THREE.Line(geometry, material); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/QuadraticBezierCurve3 | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/QuadraticBezierCurve3.js | Source} + */ +export class QuadraticBezierCurve3 extends Curve { + /** + * This constructor creates a new {@link QuadraticBezierCurve}. + * @param v0 The start point. Default is `new THREE.Vector3()`. + * @param v1 The control point. Default is `new THREE.Vector3()`. + * @param v2 The end point. Default is `new THREE.Vector3()`. + */ + constructor(v0?: Vector3, v1?: Vector3, v2?: Vector3); + + /** + * Read-only flag to check if a given object is of type {@link QuadraticBezierCurve3}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isQuadraticBezierCurve3 = true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `QuadraticBezierCurve3` + */ + override readonly type: string | "QuadraticBezierCurve3"; + + /** + * The start point. + * @defaultValue `new THREE.Vector3()` + */ + v0: Vector3; + + /** + * The control point. + * @defaultValue `new THREE.Vector3()` + */ + v1: Vector3; + + /** + * The end point. + * @defaultValue `new THREE.Vector3()` + */ + v2: Vector3; +} diff --git a/src-testing/src/extras/curves/SplineCurve.d.ts b/src-testing/src/extras/curves/SplineCurve.d.ts new file mode 100644 index 000000000..c3c56210a --- /dev/null +++ b/src-testing/src/extras/curves/SplineCurve.d.ts @@ -0,0 +1,52 @@ +import { Vector2 } from "../../math/Vector2.js"; +import { Curve } from "../core/Curve.js"; + +/** + * Create a smooth **2D** spline curve from a series of points. + * @example + * ```typescript + * // Create a sine-like wave + * const curve = new THREE.SplineCurve([ + * new THREE.Vector2(-10, 0), + * new THREE.Vector2(-5, 5), + * new THREE.Vector2(0, 0), + * new THREE.Vector2(5, -5), + * new THREE.Vector2(10, 0)]); + * const points = curve.getPoints(50); + * const geometry = new THREE.BufferGeometry().setFromPoints(points); + * const material = new THREE.LineBasicMaterial({ + * color: 0xff0000 + * }); + * // Create the final object to add to the scene + * const splineObject = new THREE.Line(geometry, material); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/SplineCurve | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/SplineCurve.js | Source} + */ +export class SplineCurve extends Curve { + /** + * This constructor creates a new {@link SplineCurve}. + * @param points An array of {@link THREE.Vector2 | Vector2} points that define the curve. Default `[]` + */ + constructor(points?: Vector2[]); + + /** + * Read-only flag to check if a given object is of type {@link SplineCurve}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isSplineCurve = true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `SplineCurve` + */ + override readonly type: string | "SplineCurve"; + + /** + * The array of {@link THREE.Vector2 | Vector2} points that define the curve. + * @defaultValue `[]` + */ + points: Vector2[]; +} diff --git a/src-testing/src/geometries/BoxGeometry.d.ts b/src-testing/src/geometries/BoxGeometry.d.ts new file mode 100644 index 000000000..7a756ae56 --- /dev/null +++ b/src-testing/src/geometries/BoxGeometry.d.ts @@ -0,0 +1,59 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; + +/** + * {@link BoxGeometry} is a geometry class for a rectangular cuboid with a given 'width', 'height', and 'depth' + * @remarks On creation, the cuboid is centred on the origin, with each edge parallel to one of the axes. + * @example + * ```typescript + * const geometry = new THREE.BoxGeometry(1, 1, 1); + * const material = new THREE.MeshBasicMaterial({ + * color: 0x00ff00 + * }); + * const cube = new THREE.Mesh(geometry, material); + * scene.add(cube); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/BoxGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/BoxGeometry.js | Source} + */ +export class BoxGeometry extends BufferGeometry { + /** + * Create a new instance of {@link BoxGeometry} + * @param width Width; that is, the length of the edges parallel to the X axis. Optional; Expects a `Float`. Default `1` + * @param height Height; that is, the length of the edges parallel to the Y axis. Optional; Expects a `Float`. Default `1` + * @param depth Depth; that is, the length of the edges parallel to the Z axis. Optional; Expects a `Float`. Default `1` + * @param widthSegments Number of segmented rectangular faces along the width of the sides. Optional; Expects a `Integer`. Default `1` + * @param heightSegments Number of segmented rectangular faces along the height of the sides. Optional; Expects a `Integer`. Default `1` + * @param depthSegments Number of segmented rectangular faces along the depth of the sides. Optional; Expects a `Integer`. Default `1` + */ + constructor( + width?: number, + height?: number, + depth?: number, + widthSegments?: number, + heightSegments?: number, + depthSegments?: number, + ); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `BoxGeometry` + */ + override readonly type: string | "BoxGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly width: number; + readonly height: number; + readonly depth: number; + readonly widthSegments: number; + readonly heightSegments: number; + readonly depthSegments: number; + }; + + /** @internal */ + static fromJSON(data: {}): BoxGeometry; +} diff --git a/src-testing/src/geometries/CapsuleGeometry.d.ts b/src-testing/src/geometries/CapsuleGeometry.d.ts new file mode 100644 index 000000000..b20616035 --- /dev/null +++ b/src-testing/src/geometries/CapsuleGeometry.d.ts @@ -0,0 +1,48 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; + +/** + * {@link CapsuleGeometry} is a geometry class for a capsule with given radii and height + * @remarks It is constructed using a lathe. + * @example + * ```typescript + * const geometry = new THREE.CapsuleGeometry(1, 1, 4, 8); + * const material = new THREE.MeshBasicMaterial({ + * color: 0x00ff00 + * }); + * const capsule = new THREE.Mesh(geometry, material); + * scene.add(capsule); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/CapsuleGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/CapsuleGeometry.js | Source} + */ +export class CapsuleGeometry extends BufferGeometry { + /** + * Create a new instance of {@link CapsuleGeometry} + * @param radius Radius of the capsule. Expects a `Float`. Default `1` + * @param length Length of the middle section. Expects a `Float`. Default `1` + * @param capSegments Number of curve segments used to build the caps. Expects a `Integer`. Default `4` + * @param radialSegments Number of segmented faces around the circumference of the capsule. Expects a `Integer`. Default `8` + */ + constructor(radius?: number, length?: number, capSegments?: number, radialSegments?: number); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `CapsuleGeometry` + */ + override readonly type: string | "CapsuleGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly radius: number; + readonly length: number; + readonly capSegments: number; + readonly radialSegments: number; + }; + + /** @internal */ + static fromJSON(data: {}): CapsuleGeometry; +} diff --git a/src-testing/src/geometries/CircleGeometry.d.ts b/src-testing/src/geometries/CircleGeometry.d.ts new file mode 100644 index 000000000..b512e4866 --- /dev/null +++ b/src-testing/src/geometries/CircleGeometry.d.ts @@ -0,0 +1,51 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; + +/** + * {@link CircleGeometry} is a simple shape of Euclidean geometry + * @remarks + * It is constructed from a number of triangular segments that are oriented around a central point and extend as far out as a given radius + * It is built counter-clockwise from a start angle and a given central angle + * It can also be used to create regular polygons, where the number of segments determines the number of sides. + * @example + * ```typescript + * const geometry = new THREE.CircleGeometry(5, 32); + * const material = new THREE.MeshBasicMaterial({ + * color: 0xffff00 + * }); + * const circle = new THREE.Mesh(geometry, material); + * scene.add(circle); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/CircleGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/CircleGeometry.js | Source} + */ +export class CircleGeometry extends BufferGeometry { + /** + * Create a new instance of {@link CircleGeometry} + * @param radius Radius of the circle. Expects a `Float`. Default `1` + * @param segments Number of segments (triangles). Expects a `Integer`. Minimum `3`. Default `32` + * @param thetaStart Start angle for first segment. Expects a `Float`. Default `0`, _(three o'clock position)_. + * @param thetaLength The central angle, often called theta, of the circular sector. Expects a `Float`. Default `Math.PI * 2`, _which makes for a complete circle_. + */ + constructor(radius?: number, segments?: number, thetaStart?: number, thetaLength?: number); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `CircleGeometry` + */ + override readonly type: string | "CircleGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly radius: number; + readonly segments: number; + readonly thetaStart: number; + readonly thetaLength: number; + }; + + /** @internal */ + static fromJSON(data: {}): CircleGeometry; +} diff --git a/src-testing/src/geometries/ConeGeometry.d.ts b/src-testing/src/geometries/ConeGeometry.d.ts new file mode 100644 index 000000000..683376429 --- /dev/null +++ b/src-testing/src/geometries/ConeGeometry.d.ts @@ -0,0 +1,64 @@ +import { CylinderGeometry } from "./CylinderGeometry.js"; + +/** + * A class for generating cone geometries. + * @example + * ```typescript + * const geometry = new THREE.ConeGeometry(5, 20, 32); + * const material = new THREE.MeshBasicMaterial({ + * color: 0xffff00 + * }); + * const cone = new THREE.Mesh(geometry, material); + * scene.add(cone); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/ConeGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/ConeGeometry.js | Source} + */ +export class ConeGeometry extends CylinderGeometry { + /** + * Create a new instance of {@link ConeGeometry} + * @param radius Radius of the cone base. Expects a `Float`. Default `1` + * @param height Height of the cone. Expects a `Float`. Default `1` + * @param radialSegments Number of segmented faces around the circumference of the cone. Expects a `Integer`. Default `32` + * @param heightSegments Number of rows of faces along the height of the cone. Expects a `Integer`. Default `1` + * @param openEnded A Boolean indicating whether the base of the cone is open or capped. Default `false`, _meaning capped_. + * @param thetaStart Start angle for first segment. Expects a `Float`. Default `0`, _(three o'clock position)_. + * @param thetaLength The central angle, often called theta, of the circular sector. Expects a `Float`. Default `Math.PI * 2`, _which makes for a complete cone_. + */ + constructor( + radius?: number, + height?: number, + radialSegments?: number, + heightSegments?: number, + openEnded?: boolean, + thetaStart?: number, + thetaLength?: number, + ); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `ConeGeometry` + */ + override readonly type: string | "ConeGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks {@link radiusTop} and {@link radiusBottom} are from base {@link THREE.CylinderGeometry} class. + * @remarks Any modification after instantiation does not change the geometry. + */ + override readonly parameters: { + readonly radius: number; + readonly radiusTop: number; + readonly radiusBottom: number; + readonly height: number; + readonly radialSegments: number; + readonly heightSegments: number; + readonly openEnded: boolean; + readonly thetaStart: number; + readonly thetaLength: number; + }; + + /** @internal */ + static fromJSON(data: {}): ConeGeometry; +} diff --git a/src-testing/src/geometries/CylinderGeometry.d.ts b/src-testing/src/geometries/CylinderGeometry.d.ts new file mode 100644 index 000000000..90f1b06f3 --- /dev/null +++ b/src-testing/src/geometries/CylinderGeometry.d.ts @@ -0,0 +1,64 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; + +/** + * A class for generating cylinder geometries. + * @example + * ```typescript + * const geometry = new THREE.CylinderGeometry(5, 5, 20, 32); + * const material = new THREE.MeshBasicMaterial({ + * color: 0xffff00 + * }); + * const cylinder = new THREE.Mesh(geometry, material); + * scene.add(cylinder); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/CylinderGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/CylinderGeometry.js | Source} + */ +export class CylinderGeometry extends BufferGeometry { + /** + * Create a new instance of {@link CylinderGeometry} + * @param radiusTop Radius of the cylinder at the top. Default `1` + * @param radiusBottom Radius of the cylinder at the bottom. Default `1` + * @param height Height of the cylinder. Default `1` + * @param radialSegments Number of segmented faces around the circumference of the cylinder. Default `32` + * @param heightSegments Number of rows of faces along the height of the cylinder. Expects a `Integer`. Default `1` + * @param openEnded A Boolean indicating whether the ends of the cylinder are open or capped. Default `false`, _meaning capped_. + * @param thetaStart Start angle for first segment. Default `0`, _(three o'clock position)_. + * @param thetaLength The central angle, often called theta, of the circular sector. Default `Math.PI * 2`, _which makes for a complete cylinder. + */ + constructor( + radiusTop?: number, + radiusBottom?: number, + height?: number, + radialSegments?: number, + heightSegments?: number, + openEnded?: boolean, + thetaStart?: number, + thetaLength?: number, + ); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `CylinderGeometry` + */ + override readonly type: string | "CylinderGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly radiusTop: number; + readonly radiusBottom: number; + readonly height: number; + readonly radialSegments: number; + readonly heightSegments: number; + readonly openEnded: boolean; + readonly thetaStart: number; + readonly thetaLength: number; + }; + + /** @internal */ + static fromJSON(data: any): CylinderGeometry; +} diff --git a/src-testing/src/geometries/DodecahedronGeometry.d.ts b/src-testing/src/geometries/DodecahedronGeometry.d.ts new file mode 100644 index 000000000..e79e5a895 --- /dev/null +++ b/src-testing/src/geometries/DodecahedronGeometry.d.ts @@ -0,0 +1,25 @@ +import { PolyhedronGeometry } from "./PolyhedronGeometry.js"; + +/** + * A class for generating a dodecahedron geometries. + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/DodecahedronGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/DodecahedronGeometry.js | Source} + */ +export class DodecahedronGeometry extends PolyhedronGeometry { + /** + * Create a new instance of {@link DodecahedronGeometry} + * @param radius Radius of the dodecahedron. Expects a `Float`. Default `1` + * @param detail Setting this to a value greater than 0 adds vertices making it no longer a dodecahedron. Expects a `Integer`. Default `0` + */ + constructor(radius?: number, detail?: number); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `DodecahedronGeometry` + */ + override readonly type: string | "DodecahedronGeometry"; + + /** @internal */ + static fromJSON(data: {}): DodecahedronGeometry; +} diff --git a/src-testing/src/geometries/EdgesGeometry.d.ts b/src-testing/src/geometries/EdgesGeometry.d.ts new file mode 100644 index 000000000..b901b10d7 --- /dev/null +++ b/src-testing/src/geometries/EdgesGeometry.d.ts @@ -0,0 +1,41 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; + +/** + * This can be used as a helper object to view the edges of a {@link THREE.BufferGeometry | geometry}. + * @example + * ```typescript + * const geometry = new THREE.BoxGeometry(100, 100, 100); + * const edges = new THREE.EdgesGeometry(geometry); + * const line = new THREE.LineSegments(edges, new THREE.LineBasicMaterial({ + * color: 0xffffff + * })); + * scene.add(line); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_helpers | helpers} + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/EdgesGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/EdgesGeometry.js | Source} + */ +export class EdgesGeometry extends BufferGeometry { + /** + * Create a new instance of {@link EdgesGeometry} + * @param geometry Any geometry object. Default `null`. + * @param thresholdAngle An edge is only rendered if the angle (in degrees) between the face normals of the adjoining faces exceeds this value. Expects a `Integer`. Default `1` _degree_. + */ + constructor(geometry?: TBufferGeometry | null, thresholdAngle?: number); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `EdgesGeometry` + */ + override readonly type: string | "EdgesGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly geometry: TBufferGeometry | null; + readonly thresholdAngle: number; + }; +} diff --git a/src-testing/src/geometries/ExtrudeGeometry.d.ts b/src-testing/src/geometries/ExtrudeGeometry.d.ts new file mode 100644 index 000000000..d872b7c21 --- /dev/null +++ b/src-testing/src/geometries/ExtrudeGeometry.d.ts @@ -0,0 +1,152 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { Curve } from "../extras/core/Curve.js"; +import { Shape } from "../extras/core/Shape.js"; +import { Vector2 } from "../math/Vector2.js"; +import { Vector3 } from "../math/Vector3.js"; + +export interface ExtrudeGeometryOptions { + /** + * Number of points on the curves. + * Expects a `Integer`. + * @defaultValue `12` + */ + curveSegments?: number | undefined; + + /** + * Number of points used for subdividing segments along the depth of the extruded spline. + * @defaultValue `1` + */ + steps?: number | undefined; + + /** + * Depth to extrude the shape. + * @defaultValue `1` + */ + depth?: number | undefined; + + /** + * Turn on bevel. Applying beveling to the shape. + * @defaultValue `true` + */ + bevelEnabled?: boolean | undefined; + + /** + * How deep into the original shape the bevel goes. + * Expects a `Float`. + * @defaultValue `0.2` + */ + bevelThickness?: number | undefined; + + /** + * Distance from the shape outline that the bevel extends + * Expects a `Float`. + * @defaultValue `bevelThickness - 0.1` + */ + bevelSize?: number | undefined; + + /** + * Distance from the shape outline that the bevel starts. + * Expects a `Float`. + * @defaultValue `0` + */ + bevelOffset?: number | undefined; + + /** + * Number of bevel layers/segments. + * Expects a `Integer`. + * @defaultValue `3` + */ + bevelSegments?: number | undefined; + + /** + * A 3D spline path along which the shape should be extruded. + * @remarks Bevels not supported for path extrusion. + */ + extrudePath?: Curve | undefined; + + /** + * A object that provides UV generator functions. + */ + UVGenerator?: UVGenerator | undefined; +} + +export interface UVGenerator { + generateTopUV( + geometry: ExtrudeGeometry, + vertices: number[], + indexA: number, + indexB: number, + indexC: number, + ): Vector2[]; + generateSideWallUV( + geometry: ExtrudeGeometry, + vertices: number[], + indexA: number, + indexB: number, + indexC: number, + indexD: number, + ): Vector2[]; +} + +/** + * Creates extruded geometry from a path shape. + * @remarks This object extrudes a 2D shape to a 3D geometry. + * @remarks When creating a Mesh with this geometry, if you'd like to have a separate material used for its face and its extruded sides, you can use an array of materials + * @remarks The first material will be applied to the face; the second material will be applied to the sides. + * @example + * ```typescript + * const length = 12, width = 8; + * const shape = new THREE.Shape(); + * shape.moveTo(0, 0); + * shape.lineTo(0, width); + * shape.lineTo(length, width); + * shape.lineTo(length, 0); + * shape.lineTo(0, 0); + * const extrudeSettings = { + * steps: 2, + * depth: 16, + * bevelEnabled: true, + * bevelThickness: 1, + * bevelSize: 1, + * bevelOffset: 0, + * bevelSegments: 1 + * }; + * const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings); + * const material = new THREE.MeshBasicMaterial({ + * color: 0x00ff00 + * }); + * const mesh = new THREE.Mesh(geometry, material); + * scene.add(mesh); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/ExtrudeGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/ExtrudeGeometry.js | Source} + */ +export class ExtrudeGeometry extends BufferGeometry { + /** + * Create a new instance of {@link ExtrudeGeometry} + * @param shapes Shape or an array of shapes. Default `new Shape([new Vector2(0.5, 0.5), new Vector2(-0.5, 0.5), new Vector2(-0.5, -0.5), new Vector2(0.5, -0.5)])`. + * @param options Object that can contain the following parameters. @see {@link ExtrudeGeometryOptions} for defaults. + */ + constructor(shapes?: Shape | Shape[], options?: ExtrudeGeometryOptions); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `ExtrudeGeometry` + */ + override readonly type: string | "ExtrudeGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly shapes: Shape | Shape[]; + readonly options: ExtrudeGeometryOptions; + }; + + addShape(shape: Shape): void; + + /** @internal */ + static fromJSON(data: {}, shapes: unknown): ExtrudeGeometry; +} diff --git a/src-testing/src/geometries/Geometries.d.ts b/src-testing/src/geometries/Geometries.d.ts new file mode 100644 index 000000000..b1be46c46 --- /dev/null +++ b/src-testing/src/geometries/Geometries.d.ts @@ -0,0 +1,21 @@ +export * from "./BoxGeometry.js"; +export * from "./CapsuleGeometry.js"; +export * from "./CircleGeometry.js"; +export * from "./ConeGeometry.js"; +export * from "./CylinderGeometry.js"; +export * from "./DodecahedronGeometry.js"; +export * from "./EdgesGeometry.js"; +export * from "./ExtrudeGeometry.js"; +export * from "./IcosahedronGeometry.js"; +export * from "./LatheGeometry.js"; +export * from "./OctahedronGeometry.js"; +export * from "./PlaneGeometry.js"; +export * from "./PolyhedronGeometry.js"; +export * from "./RingGeometry.js"; +export * from "./ShapeGeometry.js"; +export * from "./SphereGeometry.js"; +export * from "./TetrahedronGeometry.js"; +export * from "./TorusGeometry.js"; +export * from "./TorusKnotGeometry.js"; +export * from "./TubeGeometry.js"; +export * from "./WireframeGeometry.js"; diff --git a/src-testing/src/geometries/IcosahedronGeometry.d.ts b/src-testing/src/geometries/IcosahedronGeometry.d.ts new file mode 100644 index 000000000..4c05b54de --- /dev/null +++ b/src-testing/src/geometries/IcosahedronGeometry.d.ts @@ -0,0 +1,26 @@ +import { PolyhedronGeometry } from "./PolyhedronGeometry.js"; + +/** + * A class for generating an icosahedron geometry. + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/IcosahedronGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/IcosahedronGeometry.js | Source} + */ +export class IcosahedronGeometry extends PolyhedronGeometry { + /** + * Create a new instance of {@link IcosahedronGeometry} + * @param radius Expects a `Float`. Default `1` + * @param detail Setting this to a value greater than 0 adds more vertices making it no longer an icosahedron. + * When detail is greater than 1, it's effectively a sphere. Expects a `Integer`. Default `0` + */ + constructor(radius?: number, detail?: number); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `IcosahedronGeometry` + */ + override readonly type: string | "IcosahedronGeometry"; + + /** @internal */ + static fromJSON(data: {}): IcosahedronGeometry; +} diff --git a/src-testing/src/geometries/LatheGeometry.d.ts b/src-testing/src/geometries/LatheGeometry.d.ts new file mode 100644 index 000000000..f4194e514 --- /dev/null +++ b/src-testing/src/geometries/LatheGeometry.d.ts @@ -0,0 +1,55 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { Vector2 } from "../math/Vector2.js"; + +/** + * Creates meshes with axial symmetry like vases + * @remarks + * The lathe rotates around the Y axis. + * @example + * ```typescript + * const points = []; + * for (let i = 0; i & lt; 10; i++) { + * points.push(new THREE.Vector2(Math.sin(i * 0.2) * 10 + 5, (i - 5) * 2)); + * } + * const geometry = new THREE.LatheGeometry(points); + * const material = new THREE.MeshBasicMaterial({ + * color: 0xffff00 + * }); + * const lathe = new THREE.Mesh(geometry, material); + * scene.add(lathe); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/LatheGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/LatheGeometry.js | Source} + */ +export class LatheGeometry extends BufferGeometry { + /** + * This creates a {@link LatheGeometry} based on the parameters. + * @param points Array of Vector2s. The x-coordinate of each point must be greater than zero. + * Default `[new Vector2(0, -0.5), new Vector2(0.5, 0), new Vector2(0, 0.5)]` _which creates a simple diamond shape_. + * @param segments The number of circumference segments to generate. Expects a `Integer`. Default `12`. + * @param phiStart The starting angle in radians. Expects a `Float`. Default `0`. + * @param phiLength The radian (0 to 2*PI) range of the lathed section 2*PI is a closed lathe, less than 2PI is a portion. Expects a `Float`. Default `Math.PI * 2`. + */ + constructor(points?: Vector2[], segments?: number, phiStart?: number, phiLength?: number); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `LatheGeometry` + */ + override readonly type: string | "LatheGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly points: Vector2[]; + readonly segments: number; + readonly phiStart: number; + readonly phiLength: number; + }; + + /** @internal */ + static fromJSON(data: {}): LatheGeometry; +} diff --git a/src-testing/src/geometries/OctahedronGeometry.d.ts b/src-testing/src/geometries/OctahedronGeometry.d.ts new file mode 100644 index 000000000..09ba0b1e7 --- /dev/null +++ b/src-testing/src/geometries/OctahedronGeometry.d.ts @@ -0,0 +1,25 @@ +import { PolyhedronGeometry } from "./PolyhedronGeometry.js"; + +/** + * A class for generating an octahedron geometry. + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/OctahedronGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/OctahedronGeometry.js | Source} + */ +export class OctahedronGeometry extends PolyhedronGeometry { + /** + * Create a new instance of {@link OctahedronGeometry} + * @param radius Radius of the octahedron. Expects a `Float`. Default `1` + * @param detail Setting this to a value greater than zero add vertices making it no longer an octahedron. Expects a `Integer`. Default `0` + */ + constructor(radius?: number, detail?: number); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `OctahedronGeometry` + */ + override readonly type: string | "OctahedronGeometry"; + + /** @internal */ + static fromJSON(data: {}): OctahedronGeometry; +} diff --git a/src-testing/src/geometries/PlaneGeometry.d.ts b/src-testing/src/geometries/PlaneGeometry.d.ts new file mode 100644 index 000000000..fda5f4908 --- /dev/null +++ b/src-testing/src/geometries/PlaneGeometry.d.ts @@ -0,0 +1,48 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; + +/** + * A class for generating plane geometries. + * @example + * ```typescript + * const geometry = new THREE.PlaneGeometry(1, 1); + * const material = new THREE.MeshBasicMaterial({ + * color: 0xffff00, + * side: THREE.DoubleSide + * }); + * const plane = new THREE.Mesh(geometry, material); + * scene.add(plane); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/PlaneGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/PlaneGeometry.js | Source} + */ +export class PlaneGeometry extends BufferGeometry { + /** + * Create a new instance of {@link PlaneGeometry} + * @param width Width along the X axis. Expects a `Float`. Default `1` + * @param height Height along the Y axis. Expects a `Float`. Default `1` + * @param widthSegments Number of segmented faces along the width of the sides. Expects a `Integer`. Default `1` + * @param heightSegments Number of segmented faces along the height of the sides. Expects a `Integer`. Default `1` + */ + constructor(width?: number, height?: number, widthSegments?: number, heightSegments?: number); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `PlaneGeometry` + */ + override readonly type: string | "PlaneGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly width: number; + readonly height: number; + readonly widthSegments: number; + readonly heightSegments: number; + }; + + /** @internal */ + static fromJSON(data: {}): PlaneGeometry; +} diff --git a/src-testing/src/geometries/PolyhedronGeometry.d.ts b/src-testing/src/geometries/PolyhedronGeometry.d.ts new file mode 100644 index 000000000..f4c07772b --- /dev/null +++ b/src-testing/src/geometries/PolyhedronGeometry.d.ts @@ -0,0 +1,54 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; + +/** + * A polyhedron is a solid in three dimensions with flat faces + * @remarks + * This class will take an array of vertices, project them onto a sphere, and then divide them up to the desired level of detail + * This class is used by {@link THREE.DodecahedronGeometry | DodecahedronGeometry}, {@link THREE.IcosahedronGeometry | IcosahedronGeometry}, + * {@link THREE.OctahedronGeometry | OctahedronGeometry}, and {@link THREE.TetrahedronGeometry | TetrahedronGeometry} to generate their respective geometries. + * @example + * ```typescript + * const verticesOfCube = [-1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, ]; + * const indicesOfFaces = [ + * 2, 1, 0, 0, 3, 2, + * 0, 4, 7, 7, 3, 0, + * 0, 1, 5, 5, 4, 0, + * 1, 2, 6, 6, 5, 1, + * 2, 3, 7, 7, 6, 2, + * 4, 5, 6, 6, 7, 4]; + * const geometry = new THREE.PolyhedronGeometry(verticesOfCube, indicesOfFaces, 6, 2); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/PolyhedronGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/PolyhedronGeometry.js | Source} + */ +export class PolyhedronGeometry extends BufferGeometry { + /** + * Create a new instance of {@link PolyhedronGeometry} + * @param vertices Array of points of the form [1,1,1, -1,-1,-1, ... ]. Default `[]`. + * @param indices Array of indices that make up the faces of the form [0,1,2, 2,3,0, ... ]. Default `[]`. + * @param radius [page:The radius of the final shape Expects a `Float`. Default `1` + * @param detail [page:How many levels to subdivide the geometry. The more detail, the smoother the shape. Expects a `Integer`. Default `0` + */ + constructor(vertices?: number[], indices?: number[], radius?: number, detail?: number); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `PolyhedronGeometry` + */ + override readonly type: string | "PolyhedronGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly vertices: number[]; + readonly indices: number[]; + readonly radius: number; + readonly detail: number; + }; + + /** @internal */ + static fromJSON(data: {}): PolyhedronGeometry; +} diff --git a/src-testing/src/geometries/RingGeometry.d.ts b/src-testing/src/geometries/RingGeometry.d.ts new file mode 100644 index 000000000..2cbc63ef4 --- /dev/null +++ b/src-testing/src/geometries/RingGeometry.d.ts @@ -0,0 +1,59 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; + +/** + * A class for generating a two-dimensional ring geometry. + * @example + * ```typescript + * const geometry = new THREE.RingGeometry(1, 5, 32); + * const material = new THREE.MeshBasicMaterial({ + * color: 0xffff00, + * side: THREE.DoubleSide + * }); + * const mesh = new THREE.Mesh(geometry, material); + * scene.add(mesh); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/RingGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/RingGeometry.js | Source} + */ +export class RingGeometry extends BufferGeometry { + /** + * Create a new instance of {@link RingGeometry} + * @param innerRadius Expects a `Float`. Default `0.5`. + * @param outerRadius Expects a `Float`. Default `1`. + * @param thetaSegments Number of segments. A higher number means the ring will be more round. Minimum is 3. Expects a `Integer`. Default `32`. + * @param phiSegments Number of segments per ring segment. Minimum is `1`. Expects a `Integer`. Default `1`. + * @param thetaStart Starting angle. Expects a `Float`. Default `0`. + * @param thetaLength Central angle. Expects a `Float`. Default `Math.PI * 2`. + */ + constructor( + innerRadius?: number, + outerRadius?: number, + thetaSegments?: number, + phiSegments?: number, + thetaStart?: number, + thetaLength?: number, + ); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `RingGeometry` + */ + override readonly type: string | "RingGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly innerRadius: number; + readonly outerRadius: number; + readonly thetaSegments: number; + readonly phiSegments: number; + readonly thetaStart: number; + readonly thetaLength: number; + }; + + /** @internal */ + static fromJSON(data: {}): RingGeometry; +} diff --git a/src-testing/src/geometries/ShapeGeometry.d.ts b/src-testing/src/geometries/ShapeGeometry.d.ts new file mode 100644 index 000000000..a3089a645 --- /dev/null +++ b/src-testing/src/geometries/ShapeGeometry.d.ts @@ -0,0 +1,53 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { Shape } from "../extras/core/Shape.js"; + +/** + * Creates an one-sided polygonal geometry from one or more path shapes. + * @example + * ```typescript + * const x = 0, y = 0; + * const heartShape = new THREE.Shape(); + * heartShape.moveTo(x + 5, y + 5); + * heartShape.bezierCurveTo(x + 5, y + 5, x + 4, y, x, y); + * heartShape.bezierCurveTo(x - 6, y, x - 6, y + 7, x - 6, y + 7); + * heartShape.bezierCurveTo(x - 6, y + 11, x - 3, y + 15.4, x + 5, y + 19); + * heartShape.bezierCurveTo(x + 12, y + 15.4, x + 16, y + 11, x + 16, y + 7); + * heartShape.bezierCurveTo(x + 16, y + 7, x + 16, y, x + 10, y); + * heartShape.bezierCurveTo(x + 7, y, x + 5, y + 5, x + 5, y + 5); + * const geometry = new THREE.ShapeGeometry(heartShape); + * const material = new THREE.MeshBasicMaterial({ + * color: 0x00ff00 + * }); + * const mesh = new THREE.Mesh(geometry, material); + * scene.add(mesh); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/ShapeGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/ShapeGeometry.js | Source} + */ +export class ShapeGeometry extends BufferGeometry { + /** + * Create a new instance of {@link ShapeGeometry} + * @param shapes Array of shapes or a single {@link THREE.Shape | Shape}. Default `new Shape([new Vector2(0, 0.5), new Vector2(-0.5, -0.5), new Vector2(0.5, -0.5)])`, _a single triangle shape_. + * @param curveSegments Number of segments per shape. Expects a `Integer`. Default `12` + */ + constructor(shapes?: Shape | Shape[], curveSegments?: number); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `ShapeGeometry` + */ + override readonly type: string | "ShapeGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly shapes: Shape | Shape[]; + readonly curveSegments: number; + }; + + /** @internal */ + static fromJSON(data: {}): ShapeGeometry; +} diff --git a/src-testing/src/geometries/SphereGeometry.d.ts b/src-testing/src/geometries/SphereGeometry.d.ts new file mode 100644 index 000000000..b597bb26c --- /dev/null +++ b/src-testing/src/geometries/SphereGeometry.d.ts @@ -0,0 +1,67 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; + +/** + * A class for generating sphere geometries. + * @example + * ```typescript + * const geometry = new THREE.SphereGeometry(15, 32, 16); + * const material = new THREE.MeshBasicMaterial({ + * color: 0xffff00 + * }); + * const sphere = new THREE.Mesh(geometry, material); + * scene.add(sphere); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/SphereGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/SphereGeometry.js | Source} + */ +export class SphereGeometry extends BufferGeometry { + /** + * Create a new instance of {@link SphereGeometry} + * @remarks + * The geometry is created by sweeping and calculating vertexes + * around the **Y** axis (horizontal sweep) and the **Z** axis (vertical sweep) + * Thus, incomplete spheres (akin to `'sphere slices'`) can be created + * through the use of different values of {@link phiStart}, {@link phiLength}, {@link thetaStart} and {@link thetaLength}, + * in order to define the points in which we start (or end) calculating those vertices. + * @param radius Sphere radius. Expects a `Float`. Default `1` + * @param widthSegments Number of horizontal segments. Minimum value is 3, and the Expects a `Integer`. Default `32` + * @param heightSegments Number of vertical segments. Minimum value is 2, and the Expects a `Integer`. Default `16` + * @param phiStart Specify horizontal starting angle. Expects a `Float`. Default `0` + * @param phiLength Specify horizontal sweep angle size. Expects a `Float`. Default `Math.PI * 2` + * @param thetaStart Specify vertical starting angle. Expects a `Float`. Default `0` + * @param thetaLength Specify vertical sweep angle size. Expects a `Float`. Default `Math.PI` + */ + constructor( + radius?: number, + widthSegments?: number, + heightSegments?: number, + phiStart?: number, + phiLength?: number, + thetaStart?: number, + thetaLength?: number, + ); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `SphereGeometry` + */ + override readonly type: string | "SphereGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly radius: number; + readonly widthSegments: number; + readonly heightSegments: number; + readonly phiStart: number; + readonly phiLength: number; + readonly thetaStart: number; + readonly thetaLength: number; + }; + + /** @internal */ + static fromJSON(data: {}): SphereGeometry; +} diff --git a/src-testing/src/geometries/TetrahedronGeometry.d.ts b/src-testing/src/geometries/TetrahedronGeometry.d.ts new file mode 100644 index 000000000..2dd0fe5b6 --- /dev/null +++ b/src-testing/src/geometries/TetrahedronGeometry.d.ts @@ -0,0 +1,25 @@ +import { PolyhedronGeometry } from "./PolyhedronGeometry.js"; + +/** + * A class for generating a tetrahedron geometries. + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/TetrahedronGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/TetrahedronGeometry.js | Source} + */ +export class TetrahedronGeometry extends PolyhedronGeometry { + /** + * Create a new instance of {@link TetrahedronGeometry} + * @param radius Radius of the tetrahedron. Expects a `Float`. Default `1` + * @param detail Setting this to a value greater than 0 adds vertices making it no longer a tetrahedron. Expects a `Integer`. Default `0` + */ + constructor(radius?: number, detail?: number); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `TetrahedronGeometry` + */ + override readonly type: string | "TetrahedronGeometry"; + + /** @internal */ + static fromJSON(data: {}): TetrahedronGeometry; +} diff --git a/src-testing/src/geometries/TorusGeometry.d.ts b/src-testing/src/geometries/TorusGeometry.d.ts new file mode 100644 index 000000000..47a70ceea --- /dev/null +++ b/src-testing/src/geometries/TorusGeometry.d.ts @@ -0,0 +1,49 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; + +/** + * A class for generating torus geometries. + * @example + * ```typescript + * const geometry = new THREE.TorusGeometry(10, 3, 16, 100); + * const material = new THREE.MeshBasicMaterial({ + * color: 0xffff00 + * }); + * const torus = new THREE.Mesh(geometry, material); + * scene.add(torus); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/TorusGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/TorusGeometry.js | Source} + */ +export class TorusGeometry extends BufferGeometry { + /** + * Create a new instance of {@link TorusGeometry} + * @param radius Radius of the torus, from the center of the torus to the center of the tube. Expects a `Float`. Default `1`. + * @param tube Radius of the tube. Expects a `Float`. Default `0.4`. + * @param radialSegments Expects a `Integer`.Default is `12`. + * @param tubularSegments Expects a `Integer`. Default `48`. + * @param arc Central angle. Expects a `Float`. Default `Math.PI * 2` + */ + constructor(radius?: number, tube?: number, radialSegments?: number, tubularSegments?: number, arc?: number); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `TorusGeometry` + */ + override readonly type: string | "TorusGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly radius: number; + readonly tube: number; + readonly radialSegments: number; + readonly tubularSegments: number; + readonly arc: number; + }; + + /** @internal */ + static fromJSON(data: any): TorusGeometry; +} diff --git a/src-testing/src/geometries/TorusKnotGeometry.d.ts b/src-testing/src/geometries/TorusKnotGeometry.d.ts new file mode 100644 index 000000000..103b6916e --- /dev/null +++ b/src-testing/src/geometries/TorusKnotGeometry.d.ts @@ -0,0 +1,59 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; + +/** + * Creates a torus knot, the particular shape of which is defined by a pair of coprime integers, p and q + * If p and q are not coprime, the result will be a torus link. + * @example + * ```typescript + * const geometry = new THREE.TorusKnotGeometry(10, 3, 100, 16); + * const material = new THREE.MeshBasicMaterial({ + * color: 0xffff00 + * }); + * const torusKnot = new THREE.Mesh(geometry, material); + * scene.add(torusKnot); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/TorusKnotGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/TorusKnotGeometry.js | Source} + */ +export class TorusKnotGeometry extends BufferGeometry { + /** + * Create a new instance of {@link TorusKnotGeometry} + * @param radius Radius of the torus.. Default `1`. + * @param tube Expects a `Float`. Default `0.4`. + * @param tubularSegments Expects a `Integer`. Default `64`. + * @param radialSegments Expects a `Integer`. Default `8`. + * @param p This value determines, how many times the geometry winds around its axis of rotational symmetry. Expects a `Integer`. Default `2`. + * @param q This value determines, how many times the geometry winds around a circle in the interior of the torus. Expects a `Integer`. Default `3`. + */ + constructor( + radius?: number, + tube?: number, + tubularSegments?: number, + radialSegments?: number, + p?: number, + q?: number, + ); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `TorusKnotGeometry` + */ + override readonly type: string | "TorusKnotGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly radius: number; + readonly tube: number; + readonly tubularSegments: number; + readonly radialSegments: number; + readonly p: number; + readonly q: number; + }; + + /** @internal */ + static fromJSON(data: {}): TorusKnotGeometry; +} diff --git a/src-testing/src/geometries/TubeGeometry.d.ts b/src-testing/src/geometries/TubeGeometry.d.ts new file mode 100644 index 000000000..37e7129ad --- /dev/null +++ b/src-testing/src/geometries/TubeGeometry.d.ts @@ -0,0 +1,86 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { Curve } from "../extras/core/Curve.js"; +import { Vector3 } from "../math/Vector3.js"; + +/** + * Creates a tube that extrudes along a 3d curve. + * @example + * ```typescript + * class CustomSinCurve extends THREE.Curve { + * constructor(scale = 1) { + * super(); + * this.scale = scale; + * } + * getPoint(t, optionalTarget = new THREE.Vector3()) { + * const tx = t * 3 - 1.5; + * const ty = Math.sin(2 * Math.PI * t); + * const tz = 0; + * return optionalTarget.set(tx, ty, tz).multiplyScalar(this.scale); + * } + * } + * const path = new CustomSinCurve(10); + * const geometry = new THREE.TubeGeometry(path, 20, 2, 8, false); + * const material = new THREE.MeshBasicMaterial({ + * color: 0x00ff00 + * }); + * const mesh = new THREE.Mesh(geometry, material); + * scene.add(mesh); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/TubeGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/TubeGeometry.js | Source} + */ +export class TubeGeometry extends BufferGeometry { + /** + * Create a new instance of {@link TubeGeometry} + * @param path A 3D path that inherits from the {@link THREE.Curve | Curve} base class. + * Default {@link THREE.QuadraticBezierCurve3 | new THREE.QuadraticBezierCurve3(new Vector3(-1, -1, 0 ), new Vector3(-1, 1, 0), new Vector3(1, 1, 0))}. + * @param tubularSegments The number of segments that make up the tube. Expects a `Integer`. Default `64`. + * @param radius The radius of the tube. Expects a `Float`. Default `1`. + * @param radialSegments The number of segments that make up the cross-section. Expects a `Integer`. Default `8`. + * @param closed Is the tube open or closed. Default `false`. + */ + constructor( + path?: Curve, + tubularSegments?: number, + radius?: number, + radialSegments?: number, + closed?: boolean, + ); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `TubeGeometry` + */ + override readonly type: string | "TubeGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly path: Curve; + readonly tubularSegments: number; + readonly radius: number; + readonly radialSegments: number; + readonly closed: boolean; + }; + + /** + * An array of {@link THREE.Vector3 | Vector3} tangents + */ + tangents: Vector3[]; + + /** + * An array of {@link THREE.Vector3 | Vector3} normals + */ + normals: Vector3[]; + + /** + * An array of {@link THREE.Vector3 | Vector3} binormals + */ + binormals: Vector3[]; + + /** @internal */ + static fromJSON(data: {}): TubeGeometry; +} diff --git a/src-testing/src/geometries/WireframeGeometry.d.ts b/src-testing/src/geometries/WireframeGeometry.d.ts new file mode 100644 index 000000000..6263316a6 --- /dev/null +++ b/src-testing/src/geometries/WireframeGeometry.d.ts @@ -0,0 +1,40 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; + +/** + * This can be used as a helper object to view a {@link BufferGeometry | geometry} as a wireframe. + * @example + * ```typescript + * const geometry = new THREE.SphereGeometry(100, 100, 100); + * const wireframe = new THREE.WireframeGeometry(geometry); + * const line = new THREE.LineSegments(wireframe); + * line.material.depthTest = false; + * line.material.opacity = 0.25; + * line.material.transparent = true; + * scene.add(line); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_helpers | helpers} + * @see {@link https://threejs.org/docs/index.html#api/en/geometries/WireframeGeometry | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/WireframeGeometry.js | Source} + */ +export class WireframeGeometry extends BufferGeometry { + /** + * Create a new instance of {@link WireframeGeometry} + * @param geometry Any geometry object. Default `null`. + */ + constructor(geometry?: TBufferGeometry); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `WireframeGeometry` + */ + override readonly type: string | "WireframeGeometry"; + + /** + * An object with a property for each of the constructor parameters. + * @remarks Any modification after instantiation does not change the geometry. + */ + readonly parameters: { + readonly geometry: TBufferGeometry; + }; +} diff --git a/src-testing/src/helpers/ArrowHelper.d.ts b/src-testing/src/helpers/ArrowHelper.d.ts new file mode 100644 index 000000000..94896123c --- /dev/null +++ b/src-testing/src/helpers/ArrowHelper.d.ts @@ -0,0 +1,93 @@ +import { Object3D } from "../core/Object3D.js"; +import { ColorRepresentation } from "../math/Color.js"; +import { Vector3 } from "../math/Vector3.js"; +import { Line } from "../objects/Line.js"; +import { Mesh } from "../objects/Mesh.js"; + +/** + * An 3D arrow object for visualizing directions. + * @example + * ```typescript + * const dir = new THREE.Vector3(1, 2, 0); + * //normalize the direction vector (convert to vector of length 1) + * dir.normalize(); + * const origin = new THREE.Vector3(0, 0, 0); + * const length = 1; + * const hex = 0xffff00; + * const {@link ArrowHelper} = new THREE.ArrowHelper(dir, origin, length, hex); + * scene.add(arrowHelper); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_shadowmesh | WebGL / shadowmesh} + * @see {@link https://threejs.org/docs/index.html#api/en/helpers/ArrowHelper | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/ArrowHelper.js | Source} + */ +export class ArrowHelper extends Object3D { + /** + * Create a new instance of {@link ArrowHelper} + * @param dir Direction from origin. Must be a unit vector. Default `new THREE.Vector3(0, 0, 1)` + * @param origin Point at which the arrow starts. Default `new THREE.Vector3(0, 0, 0)` + * @param length Length of the arrow. Default `1` + * @param hex Hexadecimal value to define color. Default `0xffff00` + * @param headLength The length of the head of the arrow. Default `0.2 * length` + * @param headWidth The width of the head of the arrow. Default `0.2 * headLength` + */ + constructor( + dir?: Vector3, + origin?: Vector3, + length?: number, + color?: ColorRepresentation, + headLength?: number, + headWidth?: number, + ); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @override + * @defaultValue `ArrowHelper` + */ + override readonly type: string | "ArrowHelper"; + + /** + * Contains the line part of the arrowHelper. + */ + line: Line; + + /** + * Contains the cone part of the arrowHelper. + */ + cone: Mesh; + + /** + * Sets the color of the arrowHelper. + * @param color The desired color. + */ + setColor(color: ColorRepresentation): void; + + /** + * @param dir The desired direction. Must be a unit vector. + */ + setDirection(dir: Vector3): void; + + /** + * Sets the length of the arrowhelper. + * @param length The desired length. + * @param headLength The length of the head of the arrow. Default `0.2 * length` + * @param headWidth The width of the head of the arrow. Default `0.2 * headLength` + */ + setLength(length: number, headLength?: number, headWidth?: number): void; + + /** + * Copy the given object into this object + * @remarks Note: event listeners and user-defined callbacks ({@link onAfterRender | .onAfterRender} and {@link onBeforeRender | .onBeforeRender}) are not copied. + * @param source + */ + override copy(source: this): this; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/helpers/AxesHelper.d.ts b/src-testing/src/helpers/AxesHelper.d.ts new file mode 100644 index 000000000..c0633c102 --- /dev/null +++ b/src-testing/src/helpers/AxesHelper.d.ts @@ -0,0 +1,50 @@ +import { ColorRepresentation } from "../math/Color.js"; +import { LineSegments } from "../objects/LineSegments.js"; + +/** + * An axis object to visualize the 3 axes in a simple way. + * @remarks + * The X axis is red + * The Y axis is green + * The Z axis is blue. + * @example + * ```typescript + * const {@link AxesHelper} = new THREE.AxesHelper(5); + * scene.add(axesHelper); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_compression | WebGL / buffergeometry / compression} + * @see Example: {@link https://threejs.org/examples/#webgl_geometry_convex | WebGL / geometry / convex} + * @see Example: {@link https://threejs.org/examples/#webgl_loader_nrrd | WebGL / loader / nrrd} + * @see {@link https://threejs.org/docs/index.html#api/en/helpers/AxesHelper | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/AxesHelper.js | Source} + */ +export class AxesHelper extends LineSegments { + /** + * Create a new instance of {@link AxesHelper} + * @param size Size of the lines representing the axes. Default `1` + */ + constructor(size?: number); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @override + * @defaultValue `AxesHelper` + */ + override readonly type: string | "AxesHelper"; + + /** + * Sets the axes colors to {@link Color | xAxisColor}, {@link Color | yAxisColor}, {@link Color | zAxisColor}. + * @param xAxisColor + * @param yAxisColor + * @param zAxisColor + */ + setColors(xAxisColor: ColorRepresentation, yAxisColor: ColorRepresentation, zAxisColor: ColorRepresentation): this; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/helpers/Box3Helper.d.ts b/src-testing/src/helpers/Box3Helper.d.ts new file mode 100644 index 000000000..78e1c6f82 --- /dev/null +++ b/src-testing/src/helpers/Box3Helper.d.ts @@ -0,0 +1,44 @@ +import { Box3 } from "../math/Box3.js"; +import { ColorRepresentation } from "../math/Color.js"; +import { LineSegments } from "../objects/LineSegments.js"; + +/** + * Helper object to visualize a {@link THREE.Box3 | Box3}. + * @example + * ```typescript + * const box = new THREE.Box3(); + * box.setFromCenterAndSize(new THREE.Vector3(1, 1, 1), new THREE.Vector3(2, 1, 3)); + * const helper = new THREE.Box3Helper(box, 0xffff00); + * scene.add(helper); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/helpers/Box3Helper | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/Box3Helper.js | Source} + */ +export class Box3Helper extends LineSegments { + /** + * Creates a new wireframe box that represents the passed Box3. + * @param box The Box3 to show. + * @param color The box's color. Default `0xffff00` + */ + constructor(box: Box3, color?: ColorRepresentation); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @override + * @defaultValue `Box3Helper` + */ + override readonly type: string | "Box3Helper"; + + /** + * The Box3 being visualized. + */ + box: Box3; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/helpers/BoxHelper.d.ts b/src-testing/src/helpers/BoxHelper.d.ts new file mode 100644 index 000000000..d049a5b72 --- /dev/null +++ b/src-testing/src/helpers/BoxHelper.d.ts @@ -0,0 +1,64 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { Object3D } from "../core/Object3D.js"; +import { LineBasicMaterial } from "../materials/LineBasicMaterial.js"; +import { ColorRepresentation } from "../math/Color.js"; +import { LineSegments } from "../objects/LineSegments.js"; + +/** + * Helper object to graphically show the world-axis-aligned bounding box around an object + * @remarks + * The actual bounding box is handled with {@link THREE.Box3 | Box3}, this is just a visual helper for debugging + * It can be automatically resized with the {@link THREE.BoxHelper.update | BoxHelper.update} method when the object it's created from is transformed + * Note that the object must have a {@link THREE.BufferGeometry | BufferGeometry} for this to work, so it won't work with {@link Sprite | Sprites}. + * @example + * ```typescript + * const sphere = new THREE.SphereGeometry(); + * const object = new THREE.Mesh(sphere, new THREE.MeshBasicMaterial(0xff0000)); + * const box = new THREE.BoxHelper(object, 0xffff00); + * scene.add(box); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_helpers | WebGL / helpers} + * @see Example: {@link https://threejs.org/examples/#webgl_loader_nrrd | WebGL / loader / nrrd} + * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_drawrange | WebGL / buffergeometry / drawrange} + * @see {@link https://threejs.org/docs/index.html#api/en/helpers/BoxHelper | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/BoxHelper.js | Source} + */ +export class BoxHelper extends LineSegments { + /** + * Creates a new wireframe box that bounds the passed object + * @remarks + * Internally this uses {@link THREE.Box3.setFromObject | Box3.setFromObject} to calculate the dimensions + * Note that this includes any children. + * @param object The object3D to show the world-axis-aligned bounding box. + * @param color Hexadecimal value that defines the box's color. Default `0xffff00` + */ + constructor(object: Object3D, color?: ColorRepresentation); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @override + * @defaultValue `BoxHelper` + */ + override readonly type: string | "BoxHelper"; + + /** + * Updates the helper's geometry to match the dimensions of the object, including any children + * @remarks + * See {@link THREE.Box3.setFromObject | Box3.setFromObject}. + */ + update(object?: Object3D): void; + + /** + * Updates the wireframe box for the passed object. + * @param object {@link THREE.Object3D | Object3D} to create the helper of. + */ + setFromObject(object: Object3D): this; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/helpers/CameraHelper.d.ts b/src-testing/src/helpers/CameraHelper.d.ts new file mode 100644 index 000000000..469dcaa0d --- /dev/null +++ b/src-testing/src/helpers/CameraHelper.d.ts @@ -0,0 +1,80 @@ +import { Camera } from "../cameras/Camera.js"; +import { Color } from "../math/Color.js"; +import { Matrix4 } from "../math/Matrix4.js"; +import { LineSegments } from "../objects/LineSegments.js"; + +/** + * This helps with visualizing what a camera contains in its frustum + * @remarks + * It visualizes the frustum of a camera using a {@link THREE.LineSegments | LineSegments}. + * @remarks {@link CameraHelper} must be a child of the scene. + * @example + * ```typescript + * const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); + * const helper = new THREE.CameraHelper(camera); + * scene.add(helper); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_camera | WebGL / camera} + * @see Example: {@link https://threejs.org/examples/#webgl_geometry_extrude_splines | WebGL / extrude / splines} + * @see {@link https://threejs.org/docs/index.html#api/en/helpers/CameraHelper | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/CameraHelper.js | Source} + */ +export class CameraHelper extends LineSegments { + /** + * This create a new {@link CameraHelper} for the specified camera. + * @param camera The camera to visualize. + */ + constructor(camera: Camera); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @override + * @defaultValue `CameraHelper` + */ + override readonly type: string | "CameraHelper"; + + /** + * The camera being visualized. + */ + camera: Camera; + + /** + * This contains the points used to visualize the camera. + */ + pointMap: { [id: string]: number[] }; + + /** + * Reference to the {@link THREE.Camera.matrixWorld | camera.matrixWorld}. + */ + matrix: Matrix4; + + /** + * Is set to `false`, as the helper is using the {@link THREE.Camera.matrixWorld | camera.matrixWorld}. + * @see {@link THREE.Object3D.matrixAutoUpdate | Object3D.matrixAutoUpdate}. + * @defaultValue `false`. + */ + override matrixAutoUpdate: boolean; + + /** + * Defines the colors of the helper. + * @param frustum + * @param cone + * @param up + * @param target + * @param cross + */ + setColors(frustum: Color, cone: Color, up: Color, target: Color, cross: Color): this; + + /** + * Updates the helper based on the projectionMatrix of the camera. + */ + update(): void; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/helpers/DirectionalLightHelper.d.ts b/src-testing/src/helpers/DirectionalLightHelper.d.ts new file mode 100644 index 000000000..729eccedd --- /dev/null +++ b/src-testing/src/helpers/DirectionalLightHelper.d.ts @@ -0,0 +1,81 @@ +import { Object3D } from "../core/Object3D.js"; +import { DirectionalLight } from "../lights/DirectionalLight.js"; +import { ColorRepresentation } from "../math/Color.js"; +import { Matrix4 } from "../math/Matrix4.js"; +import { Line } from "../objects/Line.js"; + +/** + * Helper object to assist with visualizing a {@link THREE.DirectionalLight | DirectionalLight}'s effect on the scene + * @remarks + * This consists of plane and a line representing the light's position and direction. + * @example + * ```typescript + * const light = new THREE.DirectionalLight(0xFFFFFF); + * scene.add(light); + * + * const helper = new THREE.DirectionalLightHelper(light, 5); + * scene.add(helper); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/helpers/DirectionalLightHelper | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/DirectionalLightHelper.js | Source} + */ +export class DirectionalLightHelper extends Object3D { + /** + * Create a new instance of {@link DirectionalLightHelper} + * @param light The light to be visualized. + * @param size Dimensions of the plane. Default `1` + * @param color If this is not the set the helper will take the color of the light. Default `light.color` + */ + constructor(light: DirectionalLight, size?: number, color?: ColorRepresentation); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @override + * @defaultValue `DirectionalLightHelper` + */ + override readonly type: string | "DirectionalLightHelper"; + + /** + * Contains the line mesh showing the location of the directional light. + */ + lightPlane: Line; + + /** + * Reference to the {@link THREE.DirectionalLight | directionalLight} being visualized. + */ + light: DirectionalLight; + + /** + * Reference to the {@link THREE.DirectionalLight.matrixWorld | light.matrixWorld}. + */ + matrix: Matrix4; + + /** + * Is set to `false`, as the helper is using the {@link THREE.DirectionalLight.matrixWorld | light.matrixWorld}. + * @see {@link THREE.Object3D.matrixAutoUpdate | Object3D.matrixAutoUpdate}. + * @defaultValue `false`. + */ + override matrixAutoUpdate: boolean; + + /** + * The color parameter passed in the constructor. + * @remarks If this is changed, the helper's color will update the next time {@link update} is called. + * @defaultValue `undefined` + */ + color: ColorRepresentation | undefined; + + targetLine: Line; // TODO: Double check if this need to be exposed or not. + + /** + * Updates the helper to match the position and direction of the {@link light | DirectionalLight} being visualized. + */ + update(): void; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/helpers/GridHelper.d.ts b/src-testing/src/helpers/GridHelper.d.ts new file mode 100644 index 000000000..0b786b992 --- /dev/null +++ b/src-testing/src/helpers/GridHelper.d.ts @@ -0,0 +1,47 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { LineBasicMaterial } from "../materials/LineBasicMaterial.js"; +import { ColorRepresentation } from "../math/Color.js"; +import { LineSegments } from "../objects/LineSegments.js"; + +/** + * The {@link GridHelper} is an object to define grids + * @remarks + * Grids are two-dimensional arrays of lines. + * @example + * ```typescript + * const size = 10; + * const divisions = 10; + * const {@link GridHelper} = new THREE.GridHelper(size, divisions); + * scene.add(gridHelper); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_helpers | WebGL / helpers} + * @see {@link https://threejs.org/docs/index.html#api/en/helpers/GridHelper | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/GridHelper.js | Source} + */ +export class GridHelper extends LineSegments { + /** + * Creates a new {@link GridHelper} of size 'size' and divided into 'divisions' segments per side + * @remarks + * Colors are optional. + * @param size The size of the grid. Default `10` + * @param divisions The number of divisions across the grid. Default `10` + * @param colorCenterLine The color of the centerline. This can be a {@link THREE.Color | Color}, a hexadecimal value and an CSS-Color name. Default `0x444444` + * @param colorGrid The color of the lines of the grid. This can be a {@link THREE.Color | Color}, a hexadecimal value and an CSS-Color name. Default `0x888888` + */ + constructor(size?: number, divisions?: number, color1?: ColorRepresentation, color2?: ColorRepresentation); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @override + * @defaultValue `GridHelper` + */ + override readonly type: string | "GridHelper"; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/helpers/HemisphereLightHelper.d.ts b/src-testing/src/helpers/HemisphereLightHelper.d.ts new file mode 100644 index 000000000..80366b63b --- /dev/null +++ b/src-testing/src/helpers/HemisphereLightHelper.d.ts @@ -0,0 +1,72 @@ +import { Object3D } from "../core/Object3D.js"; +import { HemisphereLight } from "../lights/HemisphereLight.js"; +import { MeshBasicMaterial } from "../materials/MeshBasicMaterial.js"; +import { ColorRepresentation } from "../math/Color.js"; +import { Matrix4 } from "../math/Matrix4.js"; + +/** + * Creates a visual aid consisting of a spherical {@link THREE.Mesh | Mesh} for a {@link THREE.HemisphereLight | HemisphereLight}. + * @example + * ```typescript + * const light = new THREE.HemisphereLight(0xffffbb, 0x080820, 1); + * const helper = new THREE.HemisphereLightHelper(light, 5); + * scene.add(helper); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/helpers/HemisphereLightHelper | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/HemisphereLightHelper.js | Source} + */ +export class HemisphereLightHelper extends Object3D { + /** + * Create a new instance of {@link HemisphereLightHelper} + * @param light The light being visualized. + * @param size Thr sphere size + * @param color If this is not the set the helper will take the color of the light. + */ + constructor(light: HemisphereLight, size: number, color?: ColorRepresentation); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @override + * @defaultValue `HemisphereLightHelper` + */ + override readonly type: string | "HemisphereLightHelper"; + + /** + * Reference to the HemisphereLight being visualized. + */ + light: HemisphereLight; + + /** + * Reference to the {@link THREE.HemisphereLight.matrixWorld | light.matrixWorld}. + */ + matrix: Matrix4; + + /** + * Is set to `false`, as the helper is using the {@link THREE.HemisphereLight.matrixWorld | light.matrixWorld}. + * @see {@link THREE.Object3D.matrixAutoUpdate | Object3D.matrixAutoUpdate}. + * @defaultValue `false`. + */ + override matrixAutoUpdate: boolean; + + material: MeshBasicMaterial; // TODO: Double check if this need to be exposed or not. + + /** + * The color parameter passed in the constructor. + * @remarks If this is changed, the helper's color will update the next time {@link update} is called. + * @defaultValue `undefined` + */ + color: ColorRepresentation | undefined; + + /** + * Updates the helper to match the position and direction of the {@link .light | HemisphereLight}. + */ + update(): void; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/helpers/PlaneHelper.d.ts b/src-testing/src/helpers/PlaneHelper.d.ts new file mode 100644 index 000000000..43c9821cb --- /dev/null +++ b/src-testing/src/helpers/PlaneHelper.d.ts @@ -0,0 +1,50 @@ +import { Plane } from "../math/Plane.js"; +import { LineSegments } from "../objects/LineSegments.js"; + +/** + * Helper object to visualize a {@link THREE.Plane | Plane}. + * @example + * ```typescript + * const plane = new THREE.Plane(new THREE.Vector3(1, 1, 0.2), 3); + * const helper = new THREE.PlaneHelper(plane, 1, 0xffff00); + * scene.add(helper); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/helpers/PlaneHelper | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/PlaneHelper.js | Source} + */ +export class PlaneHelper extends LineSegments { + /** + * Creates a new wireframe representation of the passed plane. + * @param plane The plane to visualize. + * @param size Side length of plane helper. Expects a `Float`. Default `1` + * @param hex Color. Default `0xffff00` + */ + constructor(plane: Plane, size?: number, hex?: number); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @override + * @defaultValue `PlaneHelper` + */ + override readonly type: string | "PlaneHelper"; + + /** + * The {@link Plane | plane} being visualized. + */ + plane: Plane; + + /** + * The side lengths of plane helper. + * @remarks Expects a `Float` + * @defaultValue `1` + */ + size: number; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/helpers/PointLightHelper.d.ts b/src-testing/src/helpers/PointLightHelper.d.ts new file mode 100644 index 000000000..7d61da5e3 --- /dev/null +++ b/src-testing/src/helpers/PointLightHelper.d.ts @@ -0,0 +1,73 @@ +import { Object3D } from "../core/Object3D.js"; +import { PointLight } from "../lights/PointLight.js"; +import { ColorRepresentation } from "../math/Color.js"; +import { Matrix4 } from "../math/Matrix4.js"; + +/** + * This displays a helper object consisting of a spherical {@link THREE.Mesh | Mesh} for visualizing a {@link THREE.PointLight | PointLight}. + * @example + * ```typescript + * const pointLight = new THREE.PointLight(0xff0000, 1, 100); + * pointLight.position.set(10, 10, 10); + * scene.add(pointLight); + * const sphereSize = 1; + * const {@link PointLightHelper} = new THREE.PointLightHelper(pointLight, sphereSize); + * scene.add(pointLightHelper); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_helpers | WebGL / helpers} + * @see {@link https://threejs.org/docs/index.html#api/en/helpers/PointLightHelper | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/PointLightHelper.js | Source} + */ +export class PointLightHelper extends Object3D { + /** + * Create a new instance of {@link PointLightHelper} + * @param light The light to be visualized. + * @param sphereSize The size of the sphere helper. Expects a `Float`. Default `1` + * @param color If this is not the set the helper will take the color of the light. + */ + constructor(light: PointLight, sphereSize?: number, color?: ColorRepresentation); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @override + * @defaultValue `PointLightHelper` + */ + override readonly type: string | "PointLightHelper"; + + /** + * The {@link THREE.PointLight | PointLight} that is being visualized. + */ + light: PointLight; + + /** + * Reference to the {@link THREE.PointLight.matrixWorld | light.matrixWorld}. + */ + matrix: Matrix4; + + /** + * The color parameter passed in the constructor. + * @remarks If this is changed, the helper's color will update the next time {@link update} is called. + * @defaultValue `undefined` + */ + color: ColorRepresentation | undefined; + + /** + * Is set to `false`, as the helper is using the {@link THREE.PointLight.matrixWorld | light.matrixWorld}. + * @see {@link THREE.Object3D.matrixAutoUpdate | Object3D.matrixAutoUpdate}. + * @defaultValue `false`. + */ + override matrixAutoUpdate: boolean; + + /** + * Updates the helper to match the position of the {@link THREE..light | .light}. + */ + update(): void; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/helpers/PolarGridHelper.d.ts b/src-testing/src/helpers/PolarGridHelper.d.ts new file mode 100644 index 000000000..994d71c94 --- /dev/null +++ b/src-testing/src/helpers/PolarGridHelper.d.ts @@ -0,0 +1,55 @@ +import { ColorRepresentation } from "../math/Color.js"; +import { LineSegments } from "../objects/LineSegments.js"; + +/** + * The {@link PolarGridHelper} is an object to define polar grids + * @remarks + * Grids are two-dimensional arrays of lines. + * @example + * ```typescript + * const radius = 10; + * const sectors = 16; + * const rings = 8; + * const divisions = 64; + * const helper = new THREE.PolarGridHelper(radius, sectors, rings, divisions); + * scene.add(helper); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_helpers | WebGL / helpers} + * @see {@link https://threejs.org/docs/index.html#api/en/helpers/PolarGridHelper | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/PolarGridHelper.js | Source} + */ +export class PolarGridHelper extends LineSegments { + /** + * Creates a new {@link PolarGridHelper} of radius 'radius' with 'sectors' number of sectors and 'rings' number of rings, where each circle is smoothed into 'divisions' number of line segments. + * @remarks Colors are optional. + * @param radius The radius of the polar grid. This can be any positive number. Default `10`. + * @param sectors The number of sectors the grid will be divided into. This can be any positive integer. Default `16`. + * @param rings The number of rings. This can be any positive integer. Default `8`. + * @param divisions The number of line segments used for each circle. This can be any positive integer that is 3 or greater. Default `64`. + * @param color1 The first color used for grid elements. This can be a {@link THREE.Color | Color}, a hexadecimal value and an CSS-Color name. Default `0x444444`. + * @param color2 The second color used for grid elements. This can be a {@link THREE.Color | Color}, a hexadecimal value and an CSS-Color name. Default `0x888888`. + */ + constructor( + radius?: number, + radials?: number, + circles?: number, + divisions?: number, + color1?: ColorRepresentation, + color2?: ColorRepresentation, + ); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @override + * @defaultValue `PolarGridHelper` + */ + override readonly type: string | "PolarGridHelper"; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/helpers/SkeletonHelper.d.ts b/src-testing/src/helpers/SkeletonHelper.d.ts new file mode 100644 index 000000000..772ebf30e --- /dev/null +++ b/src-testing/src/helpers/SkeletonHelper.d.ts @@ -0,0 +1,78 @@ +import { Object3D } from "../core/Object3D.js"; +import { Matrix4 } from "../math/Matrix4.js"; +import { Bone } from "../objects/Bone.js"; +import { LineSegments } from "../objects/LineSegments.js"; +import { SkinnedMesh } from "../objects/SkinnedMesh.js"; + +/** + * A helper object to assist with visualizing a {@link Skeleton | Skeleton} + * @remarks + * The helper is rendered using a {@link LineBasicMaterial | LineBasicMaterial}. + * @example + * ```typescript + * const helper = new THREE.SkeletonHelper(skinnedMesh); + * scene.add(helper); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_animation_skinning_blending | WebGL / animation / skinning / blending} + * @see Example: {@link https://threejs.org/examples/#webgl_animation_skinning_morph | WebGL / animation / skinning / morph} + * @see Example: {@link https://threejs.org/examples/#webgl_loader_bvh | WebGL / loader / bvh } + * @see {@link https://threejs.org/docs/index.html#api/en/helpers/SkeletonHelper | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/SkeletonHelper.js | Source} + */ +export class SkeletonHelper extends LineSegments { + /** + * Create a new instance of {@link SkeletonHelper} + * @param object Usually an instance of {@link THREE.SkinnedMesh | SkinnedMesh}. + * However, any instance of {@link THREE.Object3D | Object3D} can be used if it represents a hierarchy of {@link Bone | Bone}s (via {@link THREE.Object3D.children | Object3D.children}). + */ + constructor(object: SkinnedMesh | Object3D); + + /** + * Read-only flag to check if a given object is of type {@link SkeletonHelper}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isSkeletonHelper = true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @override + * @defaultValue `SkeletonHelper` + */ + override readonly type: string | "SkeletonHelper"; + + /** + * The list of bones that the helper renders as {@link Line | Lines}. + */ + bones: Bone[]; + + /** + * The object passed in the constructor. + */ + root: SkinnedMesh | Object3D; + + /** + * Reference to the {@link THREE.Object3D.matrixWorld | root.matrixWorld}. + */ + matrix: Matrix4; + + /** + * Is set to `false`, as the helper is using the {@link THREE.Object3D.matrixWorld | root.matrixWorld}. + * @see {@link THREE.Object3D.matrixAutoUpdate | Object3D.matrixAutoUpdate}. + * @defaultValue `false`. + */ + override matrixAutoUpdate: boolean; + + /** + * Updates the helper. + */ + update(): void; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/helpers/SpotLightHelper.d.ts b/src-testing/src/helpers/SpotLightHelper.d.ts new file mode 100644 index 000000000..f620ed990 --- /dev/null +++ b/src-testing/src/helpers/SpotLightHelper.d.ts @@ -0,0 +1,77 @@ +import { Object3D } from "../core/Object3D.js"; +import { Light } from "../lights/Light.js"; +import { ColorRepresentation } from "../math/Color.js"; +import { Matrix4 } from "../math/Matrix4.js"; +import { LineSegments } from "../objects/LineSegments.js"; + +/** + * This displays a cone shaped helper object for a {@link THREE.SpotLight | SpotLight}. + * @example + * ```typescript + * const spotLight = new THREE.SpotLight(0xffffff); + * spotLight.position.set(10, 10, 10); + * scene.add(spotLight); + * const {@link SpotLightHelper} = new THREE.SpotLightHelper(spotLight); + * scene.add(spotLightHelper); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_lights_spotlights | WebGL/ lights / spotlights } + * @see {@link https://threejs.org/docs/index.html#api/en/helpers/SpotLightHelper | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/SpotLightHelper.js | Source} + */ +export class SpotLightHelper extends Object3D { + /** + * Create a new instance of {@link SpotLightHelper} + * @param light The {@link THREE.SpotLight | SpotLight} to be visualized. + * @param color If this is not the set the helper will take the color of the light. Default `light.color` + */ + constructor(light: Light, color?: ColorRepresentation); + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @override + * @defaultValue `SpotLightHelper` + */ + override readonly type: string | "SpotLightHelper"; + + /** + * {@link THREE.LineSegments | LineSegments} used to visualize the light. + */ + cone: LineSegments; + + /** + * Reference to the {@link THREE.SpotLight | SpotLight} being visualized. + */ + light: Light; + + /** + * Reference to the spotLight's {@link Object3D.matrixWorld | matrixWorld}. + */ + matrix: Matrix4; + + /** + * The color parameter passed in the constructor. + * If this is changed, the helper's color will update the next time {@link SpotLightHelper.update | update} is called. + * @defaultValue `undefined` + */ + color: ColorRepresentation | undefined; + + /** + * Is set to `false`, as the helper is using the {@link THREE.Light.matrixWorld | light.matrixWorld}. + * @see {@link THREE.Object3D.matrixAutoUpdate | Object3D.matrixAutoUpdate}. + * @defaultValue `false`. + */ + override matrixAutoUpdate: boolean; + + /** + * Updates the light helper. + */ + update(): void; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/lights/AmbientLight.d.ts b/src-testing/src/lights/AmbientLight.d.ts new file mode 100644 index 000000000..7000a37e7 --- /dev/null +++ b/src-testing/src/lights/AmbientLight.d.ts @@ -0,0 +1,36 @@ +import { ColorRepresentation } from "../math/Color.js"; +import { Light } from "./Light.js"; + +/** + * This light globally illuminates all objects in the scene equally. + * @remarks This light cannot be used to cast shadows as it does not have a direction. + * @example + * ```typescript + * const light = new THREE.AmbientLight(0x404040); // soft white light + * scene.add(light); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/lights/AmbientLight | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/AmbientLight.js | Source} + */ +export class AmbientLight extends Light { + /** + * Creates a new {@link AmbientLight}. + * @param color Numeric value of the RGB component of the color. Default `0xffffff` + * @param intensity Numeric value of the light's strength/intensity. Expects a `Float`. Default `1` + */ + constructor(color?: ColorRepresentation, intensity?: number); + + /** + * Read-only flag to check if a given object is of type {@link AmbientLight}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isAmbientLight: true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `AmbientLight` + */ + override readonly type: string | "AmbientLight"; +} diff --git a/src-testing/src/lights/DirectionalLight.d.ts b/src-testing/src/lights/DirectionalLight.d.ts new file mode 100644 index 000000000..3d43b7d89 --- /dev/null +++ b/src-testing/src/lights/DirectionalLight.d.ts @@ -0,0 +1,103 @@ +import { Object3D } from "../core/Object3D.js"; +import { ColorRepresentation } from "../math/Color.js"; +import { Vector3 } from "../math/Vector3.js"; +import { DirectionalLightShadow } from "./DirectionalLightShadow.js"; +import { Light } from "./Light.js"; + +/** + * A light that gets emitted in a specific direction + * @remarks + * This light will behave as though it is infinitely far away and the rays produced from it are all parallel + * The common use case for this is to simulate daylight; the sun is far enough away that its position can be considered to be infinite, and all light rays coming from it are parallel. + * A common point of confusion for directional lights is that setting the rotation has no effect + * @remarks + * This is because three.js's {@link DirectionalLight} is the equivalent to what is often called a 'Target Direct Light' in other applications. + * This means that its direction is calculated as pointing from the light's {@link THREE.Object3D.position | position} to the {@link THREE.DirectionalLight.target | target}'s + * position (as opposed to a 'Free Direct Light' that just has a rotation component). + * See the {@link THREE.DirectionalLight.target | target} property below for details on updating the target. + * @example + * ```typescript + * // White directional light at half intensity shining from the top. + * const {@link DirectionalLight} = new THREE.DirectionalLight(0xffffff, 0.5); + * scene.add(directionalLight); + * ``` + * @see Example: {@link https://threejs.org/examples/#misc_controls_fly | controls / fly } + * @see Example: {@link https://threejs.org/examples/#webgl_effects_parallaxbarrier | effects / parallaxbarrier } + * @see Example: {@link https://threejs.org/examples/#webgl_effects_stereo | effects / stereo } + * @see Example: {@link https://threejs.org/examples/#webgl_geometry_extrude_splines | geometry / extrude / splines } + * @see Example: {@link https://threejs.org/examples/#webgl_materials_bumpmap | materials / bumpmap } + * @see {@link https://threejs.org/docs/index.html#api/en/lights/DirectionalLight | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/DirectionalLight.js | Source} + */ +export class DirectionalLight extends Light { + /** + * Creates a new {@link DirectionalLight}. + * @param color Hexadecimal color of the light. Default `0xffffff` _(white)_. + * @param intensity Numeric value of the light's strength/intensity. Expects a `Float`. Default `1` + */ + constructor(color?: ColorRepresentation, intensity?: number); + + /** + * Read-only flag to check if a given object is of type {@link DirectionalLight}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isDirectionalLight: true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `DirectionalLight` + */ + override readonly type: string | "DirectionalLight"; + + /** + * Whether the object gets rendered into shadow map. + * @remarks + * If set to `true` light will cast dynamic shadows. + * **Warning**: This is expensive and requires tweaking to get shadows looking right. + * @see {@link THREE.DirectionalLightShadow | DirectionalLightShadow} for details. + * @defaultValue `false` + */ + override castShadow: boolean; + + /** + * This is set equal to {@link THREE.Object3D.DEFAULT_UP}, so that the light shines from the top down. + * @defaultValue {@link Object3D.DEFAULT_UP} _(0, 1, 0)_ + */ + override readonly position: Vector3; + + /** + * A {@link THREE.DirectionalLightShadow | DirectionalLightShadow} used to calculate shadows for this light. + * @defaultValue `new THREE.DirectionalLightShadow()` + */ + shadow: DirectionalLightShadow; + + /** + * The {@link DirectionalLight} points from its {@link DirectionalLight.position | position} to target.position. + * @remarks **Note**: For the target's position to be changed to anything other than the default, + * it must be added to the {@link THREE.Scene | scene} using + * ```typescript + * Scene.add( light.target ); + * ``` + * This is so that the target's {@link THREE.Object3D.matrixWorld | matrixWorld} gets automatically updated each frame. + * + * It is also possible to set the target to be another object in the scene (anything with a {@link THREE.Object3D.position | position} property), + * like so: + * ```typescript + * const targetObject = new THREE.Object3D(); + * scene.add(targetObject); + * light.target = targetObject; + * ``` + * The {@link DirectionalLight} will now track the target object. + * @defaultValue `new THREE.Object3D()` at _(0, 0, 0)_ + */ + target: Object3D; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/lights/DirectionalLightShadow.d.ts b/src-testing/src/lights/DirectionalLightShadow.d.ts new file mode 100644 index 000000000..805e4fa0b --- /dev/null +++ b/src-testing/src/lights/DirectionalLightShadow.d.ts @@ -0,0 +1,73 @@ +import { OrthographicCamera } from "../cameras/OrthographicCamera.js"; +import { LightShadow } from "./LightShadow.js"; + +/** + * This is used internally by {@link DirectionalLight | DirectionalLights} for calculating shadows. + * Unlike the other shadow classes, this uses an {@link THREE.OrthographicCamera | OrthographicCamera} to calculate the shadows, + * rather than a {@link THREE.PerspectiveCamera | PerspectiveCamera} + * @remarks + * This is because light rays from a {@link THREE.DirectionalLight | DirectionalLight} are parallel. + * @example + * ```typescript + * //Create a WebGLRenderer and turn on shadows in the renderer + * const renderer = new THREE.WebGLRenderer(); + * renderer.shadowMap.enabled = true; + * renderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap + * //Create a DirectionalLight and turn on shadows for the light + * const light = new THREE.DirectionalLight(0xffffff, 1); + * light.position.set(0, 1, 0); //default; light shining from top + * light.castShadow = true; // default false + * scene.add(light); + * //Set up shadow properties for the light + * light.shadow.mapSize.width = 512; // default + * light.shadow.mapSize.height = 512; // default + * light.shadow.camera.near = 0.5; // default + * light.shadow.camera.far = 500; // default + * //Create a sphere that cast shadows (but does not receive them) + * const sphereGeometry = new THREE.SphereGeometry(5, 32, 32); + * const sphereMaterial = new THREE.MeshStandardMaterial({ + * color: 0xff0000 + * }); + * const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); + * sphere.castShadow = true; //default is false + * sphere.receiveShadow = false; //default + * scene.add(sphere); + * //Create a plane that receives shadows (but does not cast them) + * const planeGeometry = new THREE.PlaneGeometry(20, 20, 32, 32); + * const planeMaterial = new THREE.MeshStandardMaterial({ + * color: 0x00ff00 + * }) + * const plane = new THREE.Mesh(planeGeometry, planeMaterial); + * plane.receiveShadow = true; + * scene.add(plane); + * //Create a helper for the shadow camera (optional) + * const helper = new THREE.CameraHelper(light.shadow.camera); + * scene.add(helper); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/lights/shadows/DirectionalLightShadow | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/DirectionalLightShadow.js | Source} + */ +export class DirectionalLightShadow extends LightShadow { + /** + * Create a new instance of {@link DirectionalLightShadow} + */ + constructor(); + + /** + * Read-only flag to check if a given object is of type {@link DirectionalLightShadow}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isDirectionalLightShadow: true; + + /** + * The light's view of the world. + * @remarks This is used to generate a depth map of the scene; objects behind other objects from the light's perspective will be in shadow. + * @defaultValue is an {@link THREE.OrthographicCamera | OrthographicCamera} with + * {@link OrthographicCamera.left | left} and {@link OrthographicCamera.bottom | bottom} set to -5, + * {@link OrthographicCamera.right | right} and {@link OrthographicCamera.top | top} set to 5, + * the {@link OrthographicCamera.near | near} clipping plane at 0.5 and + * the {@link OrthographicCamera.far | far} clipping plane at 500. + */ + camera: OrthographicCamera; +} diff --git a/src-testing/src/lights/HemisphereLight.d.ts b/src-testing/src/lights/HemisphereLight.d.ts new file mode 100644 index 000000000..1a796dfc8 --- /dev/null +++ b/src-testing/src/lights/HemisphereLight.d.ts @@ -0,0 +1,61 @@ +import { Color, ColorRepresentation } from "../math/Color.js"; +import { Vector3 } from "../math/Vector3.js"; +import { Light } from "./Light.js"; + +/** + * A light source positioned directly above the scene, with color fading from the sky color to the ground color. + * @remarks This light cannot be used to cast shadows. + * @example + * ```typescript + * const light = new THREE.HemisphereLight(0xffffbb, 0x080820, 1); + * scene.add(light); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_animation_skinning_blending | animation / skinning / blending } + * @see Example: {@link https://threejs.org/examples/#webgl_lights_hemisphere | lights / hemisphere } + * @see Example: {@link https://threejs.org/examples/#misc_controls_pointerlock | controls / pointerlock } + * @see Example: {@link https://threejs.org/examples/#webgl_loader_collada_kinematics | loader / collada / kinematics } + * @see Example: {@link https://threejs.org/examples/#webgl_loader_stl | loader / stl } + * @see {@link https://threejs.org/docs/index.html#api/en/lights/HemisphereLight | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/HemisphereLight.js | Source} + */ +export class HemisphereLight extends Light { + /** + * Creates a new {@link HemisphereLight}. + * @param skyColor Hexadecimal color of the sky. Expects a `Integer`. Default `0xffffff` _(white)_. + * @param groundColor Hexadecimal color of the ground. Expects a `Integer`. Default `0xffffff` _(white)_. + * @param intensity Numeric value of the light's strength/intensity. Expects a `Float`. Default `1`. + */ + constructor(skyColor?: ColorRepresentation, groundColor?: ColorRepresentation, intensity?: number); + + /** + * Read-only flag to check if a given object is of type {@link HemisphereLight}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isHemisphereLight: true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `HemisphereLight` + */ + override readonly type: string | "HemisphereLight"; + + /** + * This is set equal to {@link THREE.Object3D.DEFAULT_UP}, so that the light shines from the top down. + * @defaultValue {@link Object3D.DEFAULT_UP} _(0, 1, 0)_ + */ + override readonly position: Vector3; + + /** + * The light's sky color, as passed in the constructor. + * @defaultValue `new THREE.Color()` set to white _(0xffffff)_. + */ + override color: Color; + + /** + * The light's ground color, as passed in the constructor. + * @defaultValue `new THREE.Color()` set to white _(0xffffff)_. + */ + groundColor: Color; +} diff --git a/src-testing/src/lights/Light.d.ts b/src-testing/src/lights/Light.d.ts new file mode 100644 index 000000000..3ae757e3b --- /dev/null +++ b/src-testing/src/lights/Light.d.ts @@ -0,0 +1,82 @@ +import { JSONMeta, Object3D, Object3DJSON } from "../core/Object3D.js"; +import { Color, ColorRepresentation } from "../math/Color.js"; +import { LightShadow, LightShadowJSON } from "./LightShadow.js"; + +export interface LightJSON extends Object3DJSON { + color: number; + intensity: number; + + groundColor?: number; + + distance?: number; + angle?: number; + decay?: number; + penumbra?: number; + + shadow?: LightShadowJSON; + target?: string; +} + +/** + * Abstract base class for lights. + * @remarks All other light types inherit the properties and methods described here. + */ +export abstract class Light extends Object3D { + /** + * Creates a new {@link Light} + * @remarks + * **Note** that this is not intended to be called directly (use one of derived classes instead). + * @param color Hexadecimal color of the light. Default `0xffffff` _(white)_. + * @param intensity Numeric value of the light's strength/intensity. Expects a `Float`. Default `1`. + */ + constructor(color?: ColorRepresentation, intensity?: number); + + /** + * Read-only flag to check if a given object is of type {@link HemisphereLight}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isLight: true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `Light` + */ + override readonly type: string | "Light"; + + /** + * Color of the light. \ + * @defaultValue `new THREE.Color(0xffffff)` _(white)_. + */ + color: Color; + + /** + * The light's intensity, or strength. + * The units of intensity depend on the type of light. + * @defaultValue `1` + */ + intensity: number; + + /** + * A {@link THREE.LightShadow | LightShadow} used to calculate shadows for this light. + * @remarks Available only on Light's that support shadows. + */ + shadow: TShadowSupport; + + /** + * Copies value of all the properties from the {@link Light | source} to this instance. + * @param source + * @param recursive + */ + copy(source: this, recursive?: boolean): this; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; + + toJSON(meta?: JSONMeta): LightJSON; +} diff --git a/src-testing/src/lights/LightProbe.d.ts b/src-testing/src/lights/LightProbe.d.ts new file mode 100644 index 000000000..a63ffdc57 --- /dev/null +++ b/src-testing/src/lights/LightProbe.d.ts @@ -0,0 +1,47 @@ +import { SphericalHarmonics3 } from "../math/SphericalHarmonics3.js"; +import { Light } from "./Light.js"; + +/** + * Light probes are an alternative way of adding light to a 3D scene. + * @remarks + * Unlike classical light sources (e.g + * directional, point or spot lights), light probes do not emit light + * Instead they store information about light passing through 3D space + * During rendering, the light that hits a 3D object is approximated by using the data from the light probe. + * Light probes are usually created from (radiance) environment maps + * The class {@link THREE.LightProbeGenerator | LightProbeGenerator} can be used to create light probes from + * instances of {@link THREE.CubeTexture | CubeTexture} or {@link THREE.WebGLCubeRenderTarget | WebGLCubeRenderTarget} + * However, light estimation data could also be provided in other forms e.g + * by WebXR + * This enables the rendering of augmented reality content that reacts to real world lighting. + * The current probe implementation in three.js supports so-called diffuse light probes + * This type of light probe is functionally equivalent to an irradiance environment map. + * @see Example: {@link https://threejs.org/examples/#webgl_lightprobe | WebGL / light probe } + * @see Example: {@link https://threejs.org/examples/#webgl_lightprobe_cubecamera | WebGL / light probe / cube camera } + * @see {@link https://threejs.org/docs/index.html#api/en/lights/LightProbe | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/LightProbe.js | Source} + */ +export class LightProbe extends Light { + /** + * Creates a new LightProbe. + * @param sh An instance of {@link THREE.SphericalHarmonics3 | SphericalHarmonics3}. Default `new THREE.SphericalHarmonics3()``. + * @param intensity Numeric value of the light probe's intensity. Expects a `Float`. Default `1`. + */ + constructor(sh?: SphericalHarmonics3, intensity?: number); + + /** + * Read-only flag to check if a given object is of type {@link DirectionalLight}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isLightProbe: true; + + /** + * A light probe uses spherical harmonics to encode lighting information. + * @defaultValue `new THREE.SphericalHarmonics3()` + */ + sh: SphericalHarmonics3; + + /** @internal */ + fromJSON(json: {}): LightProbe; +} diff --git a/src-testing/src/lights/LightShadow.d.ts b/src-testing/src/lights/LightShadow.d.ts new file mode 100644 index 000000000..53c163c3e --- /dev/null +++ b/src-testing/src/lights/LightShadow.d.ts @@ -0,0 +1,169 @@ +import { Camera } from "../cameras/Camera.js"; +import { Object3DJSONObject } from "../core/Object3D.js"; +import { Frustum } from "../math/Frustum.js"; +import { Matrix4 } from "../math/Matrix4.js"; +import { Vector2, Vector2Tuple } from "../math/Vector2.js"; +import { Vector4 } from "../math/Vector4.js"; +import { WebGLRenderTarget } from "../renderers/WebGLRenderTarget.js"; +import { Light } from "./Light.js"; + +export interface LightShadowJSON { + intensity?: number; + bias?: number; + normalBias?: number; + radius?: number; + mapSize?: Vector2Tuple; + + camera: Omit; +} + +/** + * Serves as a base class for the other shadow classes. + * @see {@link https://threejs.org/docs/index.html#api/en/lights/shadows/LightShadow | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/LightShadow.js | Source} + */ +export class LightShadow { + /** + * Create a new instance of {@link LightShadow} + * @param camera The light's view of the world. + */ + constructor(camera: TCamera); + + /** + * The light's view of the world. + * @remark This is used to generate a depth map of the scene; objects behind other objects from the light's perspective will be in shadow. + */ + camera: TCamera; + + /** + * The intensity of the shadow. The default is `1`. Valid values are in the range `[0, 1]`. + */ + intensity: number; + + /** + * Shadow map bias, how much to add or subtract from the normalized depth when deciding whether a surface is in shadow. + * @remark The Very tiny adjustments here (in the order of 0.0001) may help reduce artifacts in shadows. + * @remarks Expects a `Float` + * @defaultValue `0` + */ + bias: number; + + /** + * Defines how much the position used to query the shadow map is offset along the object normal. + * @remark The Increasing this value can be used to reduce shadow acne especially in large scenes where light shines onto geometry at a shallow angle. + * @remark The cost is that shadows may appear distorted. + * @remarks Expects a `Float` + * @defaultValue `0` + */ + normalBias: number; + + /** + * Setting this to values greater than 1 will blur the edges of the shadow.toi + * @remark High values will cause unwanted banding effects in the shadows - a greater {@link LightShadow.mapSize | mapSize + * will allow for a higher value to be used here before these effects become visible. + * @remark If {@link THREE.WebGLRenderer.shadowMap.type | WebGLRenderer.shadowMap.type} is set to {@link Renderer | PCFSoftShadowMap}, + * radius has no effect and it is recommended to increase softness by decreasing {@link LightShadow.mapSize | mapSize} instead. + * @remark Note that this has no effect if the {@link THREE.WebGLRenderer.shadowMap | WebGLRenderer.shadowMap}.{@link THREE.WebGLShadowMap.type | type} + * is set to {@link THREE.BasicShadowMap | BasicShadowMap}. + * @remarks Expects a `Float` + * @defaultValue `1` + */ + radius: number; + + /** + * The amount of samples to use when blurring a VSM shadow map. + * @remarks Expects a `Integer` + * @defaultValue `8` + */ + blurSamples: number; + + /** + * A {@link THREE.Vector2 | Vector2} defining the width and height of the shadow map. + * @remarks Higher values give better quality shadows at the cost of computation time. + * @remarks Values must be powers of 2, up to the {@link THREE.WebGLRenderer.capabilities | WebGLRenderer.capabilities}.maxTextureSize for a given device, + * although the width and height don't have to be the same (so, for example, (512, 1024) is valid). + * @defaultValue `new THREE.Vector2(512, 512)` + */ + mapSize: Vector2; + + /** + * The depth map generated using the internal camera; a location beyond a pixel's depth is in shadow. Computed internally during rendering. + * @defaultValue null + */ + map: WebGLRenderTarget | null; + + /** + * The distribution map generated using the internal camera; an occlusion is calculated based on the distribution of depths. Computed internally during rendering. + * @defaultValue null + */ + mapPass: WebGLRenderTarget | null; + + /** + * Model to shadow camera space, to compute location and depth in shadow map. + * Stored in a {@link Matrix4 | Matrix4}. + * @remarks This is computed internally during rendering. + * @defaultValue new THREE.Matrix4() + */ + matrix: Matrix4; + + /** + * Enables automatic updates of the light's shadow. If you do not require dynamic lighting / shadows, you may set this to `false`. + * @defaultValue `true` + */ + autoUpdate: boolean; + + /** + * When set to `true`, shadow maps will be updated in the next `render` call. + * If you have set {@link autoUpdate} to `false`, you will need to set this property to `true` and then make a render call to update the light's shadow. + * @defaultValue `false` + */ + needsUpdate: boolean; + + /** + * Used internally by the renderer to get the number of viewports that need to be rendered for this shadow. + */ + getViewportCount(): number; + + /** + * Copies value of all the properties from the {@link {@link LightShadow} | source} to this Light. + * @param source + */ + copy(source: LightShadow): this; + + /** + * Creates a new {@link LightShadow} with the same properties as this one. + */ + clone(recursive?: boolean): this; + + /** + * Serialize this LightShadow. + */ + toJSON(): LightShadowJSON; + + /** + * Gets the shadow cameras frustum + * @remarks + * Used internally by the renderer to cull objects. + */ + getFrustum(): Frustum; + + /** + * Update the matrices for the camera and shadow, used internally by the renderer. + * @param light The light for which the shadow is being rendered. + */ + updateMatrices(light: Light): void; + + getViewport(viewportIndex: number): Vector4; + + /** + * Used internally by the renderer to extend the shadow map to contain all viewports + */ + getFrameExtents(): Vector2; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/lights/PointLight.d.ts b/src-testing/src/lights/PointLight.d.ts new file mode 100644 index 000000000..c13044e12 --- /dev/null +++ b/src-testing/src/lights/PointLight.d.ts @@ -0,0 +1,102 @@ +import { ColorRepresentation } from "../math/Color.js"; +import { Light } from "./Light.js"; +import { PointLightShadow } from "./PointLightShadow.js"; + +/** + * A light that gets emitted from a single point in all directions + * @remarks + * A common use case for this is to replicate the light emitted from a bare lightbulb. + * @example + * ```typescript + * const light = new THREE.PointLight(0xff0000, 1, 100); + * light.position.set(50, 50, 50); + * scene.add(light); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_lights_pointlights | lights / pointlights } + * @see Example: {@link https://threejs.org/examples/#webgl_effects_anaglyph | effects / anaglyph } + * @see Example: {@link https://threejs.org/examples/#webgl_geometry_text | geometry / text } + * @see Example: {@link https://threejs.org/examples/#webgl_lensflares | lensflares } + * @see {@link https://threejs.org/docs/index.html#api/en/lights/PointLight | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/PointLight.js | Source} + */ +export class PointLight extends Light { + /** + * Creates a new PointLight. + * @param color Hexadecimal color of the light. Default is 0xffffff (white). Expects a `Integer` + * @param intensity Numeric value of the light's strength/intensity. Expects a `Float`. Default `1` + * @param distance Maximum range of the light. Default is 0 (no limit). + * @param decay The amount the light dims along the distance of the light. Expects a `Float`. Default `2` + */ + constructor(color?: ColorRepresentation, intensity?: number, distance?: number, decay?: number); + + /** + * Read-only flag to check if a given object is of type {@link PointLight}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isPointLight: true; + + /** + * @default 'PointLight' + */ + type: string; + + /** + * The light's intensity. + * + * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — intensity is the luminous intensity of the light measured in candela (cd). + * @remarks Changing the intensity will also change the light's power. + * @remarks Expects a `Float` + * @defaultValue `1` + */ + intensity: number; + + /** + * When **Default mode** — When distance is zero, light does not attenuate. When distance is non-zero, + * light will attenuate linearly from maximum intensity at the light's position down to zero at this distance from the light. + * + * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — When distance is zero, + * light will attenuate according to inverse-square law to infinite distance. + * When distance is non-zero, light will attenuate according to inverse-square law until near the distance cutoff, + * where it will then attenuate quickly and smoothly to 0. Inherently, cutoffs are not physically correct. + * + * @defaultValue `0.0` + * @remarks Expects a `Float` + */ + distance: number; + + /** + * If set to `true` light will cast dynamic shadows. + * **Warning**: This is expensive and requires tweaking to get shadows looking right. + * @see {@link THREE.PointLightShadow | PointLightShadow} for details. + * @defaultValue `false` + */ + castShadow: boolean; + + /** + * The amount the light dims along the distance of the light. + * In context of physically-correct rendering the default value should not be changed. + * @remarks Expects a `Float` + * @defaultValue `2` + */ + decay: number; + + /** + * A {@link THREE.PointLightShadow | PointLightShadow} used to calculate shadows for this light. + * The lightShadow's {@link LightShadow.camera | camera} is set to + * a {@link THREE.PerspectiveCamera | PerspectiveCamera} with {@link PerspectiveCamera.fov | fov} of 90, + * {@link PerspectiveCamera.aspect | aspect} of 1, + * {@link PerspectiveCamera.near | near} clipping plane at 0.5 + * and {@link PerspectiveCamera.far | far} clipping plane at 500. + * @defaultValue new THREE.PointLightShadow() + */ + shadow: PointLightShadow; + + /** + * The light's power. + * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — power is the luminous power of the light measured in lumens (lm). + * @remarks Changing the power will also change the light's intensity. + * @remarks Expects a `Float` + */ + power: number; +} diff --git a/src-testing/src/lights/PointLightShadow.d.ts b/src-testing/src/lights/PointLightShadow.d.ts new file mode 100644 index 000000000..1d0e7e4af --- /dev/null +++ b/src-testing/src/lights/PointLightShadow.d.ts @@ -0,0 +1,22 @@ +import { PerspectiveCamera } from "../cameras/PerspectiveCamera.js"; +import { Light } from "./Light.js"; +import { LightShadow } from "./LightShadow.js"; + +/** + * Shadow for {@link THREE.PointLight | PointLight} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/PointLightShadow.js | Source} + */ +export class PointLightShadow extends LightShadow { + /** + * Read-only flag to check if a given object is of type {@link PointLightShadow}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isPointLightShadow = true; + + /** + * Update the matrices for the camera and shadow, used internally by the renderer. + * @param light The light for which the shadow is being rendered. + */ + override updateMatrices(light: Light, viewportIndex?: number): void; +} diff --git a/src-testing/src/lights/RectAreaLight.d.ts b/src-testing/src/lights/RectAreaLight.d.ts new file mode 100644 index 000000000..2861e9794 --- /dev/null +++ b/src-testing/src/lights/RectAreaLight.d.ts @@ -0,0 +1,82 @@ +import { ColorRepresentation } from "../math/Color.js"; +import { Light } from "./Light.js"; + +/** + * {@link RectAreaLight} emits light uniformly across the face a rectangular plane + * @remarks + * This light type can be used to simulate light sources such as bright windows or strip lighting. + * Important Notes: + * - There is no shadow support. + * - Only {@link MeshStandardMaterial | MeshStandardMaterial} and {@link MeshPhysicalMaterial | MeshPhysicalMaterial} are supported. + * - You have to include {@link https://threejs.org/examples/jsm/lights/RectAreaLightUniformsLib.js | RectAreaLightUniformsLib} into your scene and call `init()`. + * @example + * ```typescript + * const width = 10; + * const height = 10; + * const intensity = 1; + * const rectLight = new THREE.RectAreaLight(0xffffff, intensity, width, height); + * rectLight.position.set(5, 5, 0); + * rectLight.lookAt(0, 0, 0); + * scene.add(rectLight) + * const rectLightHelper = new RectAreaLightHelper(rectLight); + * rectLight.add(rectLightHelper); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_lights_rectarealight | WebGL / {@link RectAreaLight} } + * @see {@link https://threejs.org/docs/index.html#api/en/lights/RectAreaLight | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/RectAreaLight.js | Source} + */ +export class RectAreaLight extends Light { + /** + * Creates a new {@link RectAreaLight}. + * @param color Hexadecimal color of the light. Default `0xffffff` _(white)_. + * @param intensity The light's intensity, or brightness. Expects a `Float`. Default `1` + * @param width Width of the light. Expects a `Float`. Default `10` + * @param height Height of the light. Expects a `Float`. Default `10` + */ + constructor(color?: ColorRepresentation, intensity?: number, width?: number, height?: number); + + /** + * Read-only flag to check if a given object is of type {@link RectAreaLight}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isRectAreaLight: true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `RectAreaLight` + */ + override readonly type: string | "RectAreaLight"; + + /** + * The width of the light. + * @remarks Expects a `Float` + * @defaultValue `10` + */ + width: number; + + /** + * The height of the light. + * @remarks Expects a `Float` + * @defaultValue `10` + */ + height: number; + + /** + * The light's intensity. + * @remarks Changing the intensity will also change the light's power. + * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — intensity is the luminance (brightness) of the light measured in nits (cd/m^2). + * @remarks Expects a `Float` + * @defaultValue `1` + */ + intensity: number; + + /** + * The light's power. + * @remarks Changing the power will also change the light's intensity. + * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — power is the luminous power of the light measured in lumens (lm). + * @remarks Expects a `Float` + */ + power: number; +} diff --git a/src-testing/src/lights/SpotLight.d.ts b/src-testing/src/lights/SpotLight.d.ts new file mode 100644 index 000000000..7f42488a8 --- /dev/null +++ b/src-testing/src/lights/SpotLight.d.ts @@ -0,0 +1,164 @@ +import { Object3D } from "../core/Object3D.js"; +import { ColorRepresentation } from "../math/Color.js"; +import { Vector3 } from "../math/Vector3.js"; +import { Texture } from "../textures/Texture.js"; +import { Light } from "./Light.js"; +import { SpotLightShadow } from "./SpotLightShadow.js"; + +/** + * This light gets emitted from a single point in one direction, along a cone that increases in size the further from the light it gets. + * @example + * ```typescript + * // white {@link SpotLight} shining from the side, modulated by a texture, casting a shadow + * const {@link SpotLight} = new THREE.SpotLight(0xffffff); + * spotLight.position.set(100, 1000, 100); + * spotLight.map = new THREE.TextureLoader().load(url); + * spotLight.castShadow = true; + * spotLight.shadow.mapSize.width = 1024; + * spotLight.shadow.mapSize.height = 1024; + * spotLight.shadow.camera.near = 500; + * spotLight.shadow.camera.far = 4000; + * spotLight.shadow.camera.fov = 30; + * scene.add(spotLight); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_lights_spotlight | lights / {@link SpotLight} } + * @see Example: {@link https://threejs.org/examples/#webgl_lights_spotlights | lights / spotlights } + * @see {@link https://threejs.org/docs/index.html#api/en/lights/SpotLight | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/SpotLight.js | Source} + */ +export class SpotLight extends Light { + /** + * Creates a new SpotLight. + * @param color Hexadecimal color of the light. Default `0xffffff` _(white)_. + * @param intensity Numeric value of the light's strength/intensity. Expects a `Float`. Default `1`. + * @param distance Maximum range of the light. Default is 0 (no limit). Expects a `Float`. + * @param angle Maximum angle of light dispersion from its direction whose upper bound is Math.PI/2. + * @param penumbra Percent of the {@link SpotLight} cone that is attenuated due to penumbra. Takes values between zero and 1. Expects a `Float`. Default `0`. + * @param decay The amount the light dims along the distance of the light. Expects a `Float`. Default `2`. + */ + constructor( + color?: ColorRepresentation, + intensity?: number, + distance?: number, + angle?: number, + penumbra?: number, + decay?: number, + ); + + /** + * Read-only flag to check if a given object is of type {@link SpotLight}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isSpotLight: true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @defaultValue `SpotLight` + */ + override readonly type: string | "SpotLight"; + + /** + * This is set equal to {@link THREE.Object3D.DEFAULT_UP | Object3D.DEFAULT_UP} (0, 1, 0), so that the light shines from the top down. + * @defaultValue `{@link Object3D.DEFAULT_UP}` + */ + readonly position: Vector3; + + /** + * The {@link SpotLight} points from its {@link SpotLight.position | position} to target.position. + * @remarks + * **Note**: For the target's position to be changed to anything other than the default, + * it must be added to the {@link Scene | scene} using + * + * ```typescript + * scene.add( light.target ); + * ``` + * + * This is so that the target's {@link Object3D.matrixWorld | matrixWorld} gets automatically updated each frame. + * It is also possible to set the target to be another object in the scene (anything with a {@link THREE.Object3D.position | position} property), like so: + * ```typescript + * const targetObject = new THREE.Object3D(); + * scene.add(targetObject); + * light.target = targetObject; + * ``` + * The {@link SpotLight} will now track the target object. + * @defaultValue `new THREE.Object3D()` _The default position of the target is *(0, 0, 0)*._ + */ + target: Object3D; + + /** + * If set to `true` light will cast dynamic shadows. + * @remarks **Warning**: This is expensive and requires tweaking to get shadows looking right. the {@link THREE.SpotLightShadow | SpotLightShadow} for details. + * @defaultValue `false` + */ + override castShadow: boolean; + + /** + * The light's intensity. + * @remarks Changing the intensity will also change the light's power. + * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — intensity is the luminous intensity of the light measured in candela (cd). + * @remarks Expects a `Float` + * @defaultValue `1` + */ + intensity: number; + + /** + * When **Default mode** — When distance is zero, light does not attenuate. When distance is non-zero, + * light will attenuate linearly from maximum intensity at the light's position down to zero at this distance from the light. + * + * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — When distance is zero, + * light will attenuate according to inverse-square law to infinite distance. + * When distance is non-zero, light will attenuate according to inverse-square law until near the distance cutoff, + * where it will then attenuate quickly and smoothly to `0`. Inherently, cutoffs are not physically correct. + * @remarks Expects a `Float` + * @defaultValue `0.0` + */ + distance: number; + + /** + * Maximum extent of the spotlight, in radians, from its direction. + * @remarks Should be no more than `Math.PI/2`. + * @remarks Expects a `Float` + * @defaultValue `Math.PI / 3` + */ + angle: number; + + /** + * The amount the light dims along the distance of the light. + * In context of physically-correct rendering the default value should not be changed. + * @remarks Expects a `Float` + * @defaultValue `2` + */ + decay: number; + + /** + * A {@link THREE.SpotLightShadow | SpotLightShadow} used to calculate shadows for this light. + * @defaultValue `new THREE.SpotLightShadow()` + */ + shadow: SpotLightShadow; + + /** + * The light's power. + * @remarks Changing the power will also change the light's intensity. + * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — power is the luminous power of the light measured in lumens (lm). + * @remarks Expects a `Float` + */ + power: number; + + /** + * Percent of the {@link SpotLight} cone that is attenuated due to penumbra. + * @remarks Takes values between zero and 1. + * @remarks Expects a `Float` + * @defaultValue `0.0` + */ + penumbra: number; + + /** + * A {@link THREE.Texture | Texture} used to modulate the color of the light. + * The spot light color is mixed with the _RGB_ value of this texture, with a ratio corresponding to its alpha value. + * The cookie-like masking effect is reproduced using pixel values (0, 0, 0, 1-cookie_value). + * @remarks **Warning**: {@link SpotLight.map} is disabled if {@link SpotLight.castShadow} is `false`. + */ + map: Texture | null; +} diff --git a/src-testing/src/lights/SpotLightShadow.d.ts b/src-testing/src/lights/SpotLightShadow.d.ts new file mode 100644 index 000000000..77f075c44 --- /dev/null +++ b/src-testing/src/lights/SpotLightShadow.d.ts @@ -0,0 +1,72 @@ +import { PerspectiveCamera } from "../cameras/PerspectiveCamera.js"; +import { LightShadow } from "./LightShadow.js"; + +/** + * This is used internally by {@link SpotLight | SpotLights} for calculating shadows. + * @example + * ```typescript + * //Create a WebGLRenderer and turn on shadows in the renderer + * const renderer = new THREE.WebGLRenderer(); + * renderer.shadowMap.enabled = true; + * renderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap + * //Create a SpotLight and turn on shadows for the light + * const light = new THREE.SpotLight(0xffffff); + * light.castShadow = true; // default false + * scene.add(light); + * //Set up shadow properties for the light + * light.shadow.mapSize.width = 512; // default + * light.shadow.mapSize.height = 512; // default + * light.shadow.camera.near = 0.5; // default + * light.shadow.camera.far = 500; // default + * light.shadow.focus = 1; // default + * //Create a sphere that cast shadows (but does not receive them) + * const sphereGeometry = new THREE.SphereGeometry(5, 32, 32); + * const sphereMaterial = new THREE.MeshStandardMaterial({ + * color: 0xff0000 + * }); + * const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); + * sphere.castShadow = true; //default is false + * sphere.receiveShadow = false; //default + * scene.add(sphere); + * //Create a plane that receives shadows (but does not cast them) + * const planeGeometry = new THREE.PlaneGeometry(20, 20, 32, 32); + * const planeMaterial = new THREE.MeshStandardMaterial({ + * color: 0x00ff00 + * }) + * const plane = new THREE.Mesh(planeGeometry, planeMaterial); + * plane.receiveShadow = true; + * scene.add(plane); + * //Create a helper for the shadow camera (optional) + * const helper = new THREE.CameraHelper(light.shadow.camera); + * scene.add(helper); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/lights/shadows/SpotLightShadow | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/SpotLightShadow.js | Source} + */ +export class SpotLightShadow extends LightShadow { + /** + * Read-only flag to check if a given object is of type {@link SpotLightShadow}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isSpotLightShadow: true; + + /** + * The light's view of the world. + * @remarks This is used to generate a depth map of the scene; objects behind other objects from the light's perspective will be in shadow. + * @remarks + * The {@link THREE.PerspectiveCamera.fov | fov} will track the {@link THREE.SpotLight.angle | angle} property + * of the owning {@link SpotLight | SpotLight} via the {@link SpotLightShadow.update | update} method. + * Similarly, the {@link THREE.PerspectiveCamera.aspect | aspect} property will track the aspect of the {@link LightShadow.mapSize | mapSize}. + * If the {@link SpotLight.distance | distance} property of the light is set, the {@link THREE.PerspectiveCamera.far | far} clipping plane will track that, otherwise it defaults to `500`. + * @defaultValue is a {@link THREE.PerspectiveCamera | PerspectiveCamera} with {@link THREE.PerspectiveCamera.near | near} clipping plane at `0.5`. + */ + camera: PerspectiveCamera; + + /** + * Used to focus the shadow camera. + * @remarks The camera's field of view is set as a percentage of the spotlight's field-of-view. Range is `[0, 1]`. 0`. + * @defaultValue `1` + */ + focus: number; +} diff --git a/src-testing/src/lights/webgpu/IESSpotLight.d.ts b/src-testing/src/lights/webgpu/IESSpotLight.d.ts new file mode 100644 index 000000000..bf1b66006 --- /dev/null +++ b/src-testing/src/lights/webgpu/IESSpotLight.d.ts @@ -0,0 +1,6 @@ +import { Texture } from "../../textures/Texture.js"; +import { SpotLight } from "../SpotLight.js"; + +export default class IESSpotLight extends SpotLight { + iesMap: Texture | null; +} diff --git a/src-testing/src/loaders/AnimationLoader.d.ts b/src-testing/src/loaders/AnimationLoader.d.ts new file mode 100644 index 000000000..567f30f30 --- /dev/null +++ b/src-testing/src/loaders/AnimationLoader.d.ts @@ -0,0 +1,9 @@ +import { AnimationClip } from "../animation/AnimationClip.js"; +import { Loader } from "./Loader.js"; +import { LoadingManager } from "./LoadingManager.js"; + +export class AnimationLoader extends Loader { + constructor(manager?: LoadingManager); + + parse(json: readonly unknown[]): AnimationClip[]; +} diff --git a/src-testing/src/loaders/AudioLoader.d.ts b/src-testing/src/loaders/AudioLoader.d.ts new file mode 100644 index 000000000..0204bef47 --- /dev/null +++ b/src-testing/src/loaders/AudioLoader.d.ts @@ -0,0 +1,6 @@ +import { Loader } from "./Loader.js"; +import { LoadingManager } from "./LoadingManager.js"; + +export class AudioLoader extends Loader { + constructor(manager?: LoadingManager); +} diff --git a/src-testing/src/loaders/BufferGeometryLoader.d.ts b/src-testing/src/loaders/BufferGeometryLoader.d.ts new file mode 100644 index 000000000..0aa994011 --- /dev/null +++ b/src-testing/src/loaders/BufferGeometryLoader.d.ts @@ -0,0 +1,10 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { InstancedBufferGeometry } from "../core/InstancedBufferGeometry.js"; +import { Loader } from "./Loader.js"; +import { LoadingManager } from "./LoadingManager.js"; + +export class BufferGeometryLoader extends Loader { + constructor(manager?: LoadingManager); + + parse(json: unknown): InstancedBufferGeometry | BufferGeometry; +} diff --git a/src-testing/src/loaders/Cache.d.ts b/src-testing/src/loaders/Cache.d.ts new file mode 100644 index 000000000..0742af8f5 --- /dev/null +++ b/src-testing/src/loaders/Cache.d.ts @@ -0,0 +1,21 @@ +declare const Cache: { + /** + * @default false + */ + enabled: boolean; + + /** + * @default {} + */ + files: any; + + add(key: string, file: any): void; + + get(key: string): any; + + remove(key: string): void; + + clear(): void; +}; + +export { Cache }; diff --git a/src-testing/src/loaders/CompressedTextureLoader.d.ts b/src-testing/src/loaders/CompressedTextureLoader.d.ts new file mode 100644 index 000000000..eeca01ded --- /dev/null +++ b/src-testing/src/loaders/CompressedTextureLoader.d.ts @@ -0,0 +1,14 @@ +import { CompressedTexture } from "../textures/CompressedTexture.js"; +import { Loader } from "./Loader.js"; +import { LoadingManager } from "./LoadingManager.js"; + +export class CompressedTextureLoader extends Loader { + constructor(manager?: LoadingManager); + + load( + url: string, + onLoad?: (data: CompressedTexture) => void, + onProgress?: (event: ProgressEvent) => void, + onError?: (err: unknown) => void, + ): CompressedTexture; +} diff --git a/src-testing/src/loaders/CubeTextureLoader.d.ts b/src-testing/src/loaders/CubeTextureLoader.d.ts new file mode 100644 index 000000000..f6cd285c4 --- /dev/null +++ b/src-testing/src/loaders/CubeTextureLoader.d.ts @@ -0,0 +1,14 @@ +import { CubeTexture } from "../textures/CubeTexture.js"; +import { Loader } from "./Loader.js"; +import { LoadingManager } from "./LoadingManager.js"; + +export class CubeTextureLoader extends Loader { + constructor(manager?: LoadingManager); + + load( + url: readonly string[], + onLoad?: (data: CubeTexture) => void, + onProgress?: (event: ProgressEvent) => void, + onError?: (err: unknown) => void, + ): CubeTexture; +} diff --git a/src-testing/src/loaders/DataTextureLoader.d.ts b/src-testing/src/loaders/DataTextureLoader.d.ts new file mode 100644 index 000000000..0cc8d7475 --- /dev/null +++ b/src-testing/src/loaders/DataTextureLoader.d.ts @@ -0,0 +1,14 @@ +import { DataTexture } from "../textures/DataTexture.js"; +import { Loader } from "./Loader.js"; +import { LoadingManager } from "./LoadingManager.js"; + +export class DataTextureLoader extends Loader { + constructor(manager?: LoadingManager); + + load( + url: string, + onLoad?: (data: DataTexture, texData: object) => void, + onProgress?: (event: ProgressEvent) => void, + onError?: (err: unknown) => void, + ): DataTexture; +} diff --git a/src-testing/src/loaders/FileLoader.d.ts b/src-testing/src/loaders/FileLoader.d.ts new file mode 100644 index 000000000..a07ca5152 --- /dev/null +++ b/src-testing/src/loaders/FileLoader.d.ts @@ -0,0 +1,19 @@ +import { Loader } from "./Loader.js"; +import { LoadingManager } from "./LoadingManager.js"; + +export class FileLoader extends Loader { + constructor(manager?: LoadingManager); + + load( + url: string, + onLoad?: (data: string | ArrayBuffer) => void, + onProgress?: (event: ProgressEvent) => void, + onError?: (err: unknown) => void, + ): void; + + mimeType: undefined | MimeType; + responseType: undefined | string; + + setMimeType(mimeType: MimeType): FileLoader; + setResponseType(responseType: string): FileLoader; +} diff --git a/src-testing/src/loaders/ImageBitmapLoader.d.ts b/src-testing/src/loaders/ImageBitmapLoader.d.ts new file mode 100644 index 000000000..f182d326a --- /dev/null +++ b/src-testing/src/loaders/ImageBitmapLoader.d.ts @@ -0,0 +1,22 @@ +import { Loader } from "./Loader.js"; +import { LoadingManager } from "./LoadingManager.js"; + +export class ImageBitmapLoader extends Loader { + constructor(manager?: LoadingManager); + + load( + url: string, + onLoad?: (data: ImageBitmap) => void, + onProgress?: (event: ProgressEvent) => void, + onError?: (err: unknown) => void, + ): void; + + /** + * @default { premultiplyAlpha: 'none' } + */ + options: undefined | object; + + readonly isImageBitmapLoader: true; + + setOptions(options: object): ImageBitmapLoader; +} diff --git a/src-testing/src/loaders/ImageLoader.d.ts b/src-testing/src/loaders/ImageLoader.d.ts new file mode 100644 index 000000000..2189198e4 --- /dev/null +++ b/src-testing/src/loaders/ImageLoader.d.ts @@ -0,0 +1,17 @@ +import { Loader } from "./Loader.js"; +import { LoadingManager } from "./LoadingManager.js"; + +/** + * A loader for loading an image. + * Unlike other loaders, this one emits events instead of using predefined callbacks. So if you're interested in getting notified when things happen, you need to add listeners to the object. + */ +export class ImageLoader extends Loader { + constructor(manager?: LoadingManager); + + load( + url: string, + onLoad?: (data: HTMLImageElement) => void, + onProgress?: (event: ProgressEvent) => void, + onError?: (err: unknown) => void, + ): HTMLImageElement; +} diff --git a/src-testing/src/loaders/Loader.d.ts b/src-testing/src/loaders/Loader.d.ts new file mode 100644 index 000000000..0f65e66f3 --- /dev/null +++ b/src-testing/src/loaders/Loader.d.ts @@ -0,0 +1,50 @@ +import { LoadingManager } from "./LoadingManager.js"; + +/** + * Base class for implementing loaders. + */ +export class Loader { + constructor(manager?: LoadingManager); + + /** + * @default 'anonymous' + */ + crossOrigin: string; + + /** + * @default false + */ + withCredentials: boolean; + + /** + * @default '' + */ + path: string; + + /** + * @default '' + */ + resourcePath: string; + manager: LoadingManager; + + /** + * @default {} + */ + requestHeader: { [header: string]: string }; + + load( + url: TUrl, + onLoad: (data: TData) => void, + onProgress?: (event: ProgressEvent) => void, + onError?: (err: unknown) => void, + ): void; + loadAsync(url: TUrl, onProgress?: (event: ProgressEvent) => void): Promise; + + setCrossOrigin(crossOrigin: string): this; + setWithCredentials(value: boolean): this; + setPath(path: string): this; + setResourcePath(resourcePath: string): this; + setRequestHeader(requestHeader: { [header: string]: string }): this; + + static DEFAULT_MATERIAL_NAME: string; +} diff --git a/src-testing/src/loaders/LoaderUtils.d.ts b/src-testing/src/loaders/LoaderUtils.d.ts new file mode 100644 index 000000000..2f00baeff --- /dev/null +++ b/src-testing/src/loaders/LoaderUtils.d.ts @@ -0,0 +1,10 @@ +export class LoaderUtils { + /** + * @deprecated decodeText() has been deprecated with r165 and will be removed with r175. Use TextDecoder instead. + */ + static decodeText(array: BufferSource): string; + + static extractUrlBase(url: string): string; + + static resolveURL(url: string, path: string): string; +} diff --git a/src-testing/src/loaders/LoadingManager.d.ts b/src-testing/src/loaders/LoadingManager.d.ts new file mode 100644 index 000000000..ab5b546cd --- /dev/null +++ b/src-testing/src/loaders/LoadingManager.d.ts @@ -0,0 +1,69 @@ +import { Loader } from "./Loader.js"; + +export const DefaultLoadingManager: LoadingManager; + +/** + * Handles and keeps track of loaded and pending data. + */ +export class LoadingManager { + constructor( + onLoad?: () => void, + onProgress?: (url: string, loaded: number, total: number) => void, + onError?: (url: string) => void, + ); + + /** + * Will be called when loading of an item starts. + * @param url The url of the item that started loading. + * @param loaded The number of items already loaded so far. + * @param total The total amount of items to be loaded. + */ + onStart?: ((url: string, loaded: number, total: number) => void) | undefined; + + /** + * Will be called when all items finish loading. + * The default is a function with empty body. + */ + onLoad: () => void; + + /** + * Will be called for each loaded item. + * The default is a function with empty body. + * @param url The url of the item just loaded. + * @param loaded The number of items already loaded so far. + * @param total The total amount of items to be loaded. + */ + onProgress: (url: string, loaded: number, total: number) => void; + + /** + * Will be called when item loading fails. + * The default is a function with empty body. + * @param url The url of the item that errored. + */ + onError: (url: string) => void; + + /** + * If provided, the callback will be passed each resource URL before a request is sent. + * The callback may return the original URL, or a new URL to override loading behavior. + * This behavior can be used to load assets from .ZIP files, drag-and-drop APIs, and Data URIs. + * @param callback URL modifier callback. Called with url argument, and must return resolvedURL. + */ + setURLModifier(callback?: (url: string) => string): this; + + /** + * Given a URL, uses the URL modifier callback (if any) and returns a resolved URL. + * If no URL modifier is set, returns the original URL. + * @param url the url to load + */ + resolveURL(url: string): string; + + itemStart(url: string): void; + itemEnd(url: string): void; + itemError(url: string): void; + + // handlers + + addHandler(regex: RegExp, loader: Loader): this; + removeHandler(regex: RegExp): this; + getHandler(file: string): Loader | null; +} diff --git a/src-testing/src/loaders/MaterialLoader.d.ts b/src-testing/src/loaders/MaterialLoader.d.ts new file mode 100644 index 000000000..67db71113 --- /dev/null +++ b/src-testing/src/loaders/MaterialLoader.d.ts @@ -0,0 +1,19 @@ +import { Material } from "../materials/Material.js"; +import { Texture } from "../textures/Texture.js"; +import { Loader } from "./Loader.js"; +import { LoadingManager } from "./LoadingManager.js"; + +export class MaterialLoader extends Loader { + /** + * @default {} + */ + textures: { [key: string]: Texture }; + + constructor(manager?: LoadingManager); + + parse(json: unknown): Material; + + setTextures(textures: { [key: string]: Texture }): this; + + static createMaterialFromType(type: string): Material; +} diff --git a/src-testing/src/loaders/ObjectLoader.d.ts b/src-testing/src/loaders/ObjectLoader.d.ts new file mode 100644 index 000000000..306c75700 --- /dev/null +++ b/src-testing/src/loaders/ObjectLoader.d.ts @@ -0,0 +1,35 @@ +import { AnimationClip } from "../animation/AnimationClip.js"; +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { InstancedBufferGeometry } from "../core/InstancedBufferGeometry.js"; +import { Object3D } from "../core/Object3D.js"; +import { Material } from "../materials/Material.js"; +import { Source } from "../textures/Source.js"; +import { Texture } from "../textures/Texture.js"; +import { Loader } from "./Loader.js"; +import { LoadingManager } from "./LoadingManager.js"; + +export class ObjectLoader extends Loader { + constructor(manager?: LoadingManager); + + load( + url: string, + onLoad?: (data: Object3D) => void, + onProgress?: (event: ProgressEvent) => void, + onError?: (err: unknown) => void, + ): void; + + parse(json: unknown, onLoad?: (object: Object3D) => void): Object3D; + parseAsync(json: unknown): Promise; + parseGeometries(json: unknown): { [key: string]: InstancedBufferGeometry | BufferGeometry }; + parseMaterials(json: unknown, textures: { [key: string]: Texture }): { [key: string]: Material }; + parseAnimations(json: unknown): { [key: string]: AnimationClip }; + parseImages(json: unknown, onLoad?: () => void): { [key: string]: Source }; + parseImagesAsync(json: unknown): Promise<{ [key: string]: Source }>; + parseTextures(json: unknown, images: { [key: string]: Source }): { [key: string]: Texture }; + parseObject( + data: unknown, + geometries: { [key: string]: InstancedBufferGeometry | BufferGeometry }, + materials: { [key: string]: Material }, + animations: { [key: string]: AnimationClip }, + ): Object3D; +} diff --git a/src-testing/src/loaders/TextureLoader.d.ts b/src-testing/src/loaders/TextureLoader.d.ts new file mode 100644 index 000000000..3cc07f5c3 --- /dev/null +++ b/src-testing/src/loaders/TextureLoader.d.ts @@ -0,0 +1,18 @@ +import { Texture } from "../textures/Texture.js"; +import { Loader } from "./Loader.js"; +import { LoadingManager } from "./LoadingManager.js"; + +/** + * Class for loading a texture. + * Unlike other loaders, this one emits events instead of using predefined callbacks. So if you're interested in getting notified when things happen, you need to add listeners to the object. + */ +export class TextureLoader extends Loader { + constructor(manager?: LoadingManager); + + load( + url: string, + onLoad?: (data: Texture) => void, + onProgress?: (event: ProgressEvent) => void, + onError?: (err: unknown) => void, + ): Texture; +} diff --git a/src-testing/src/materials/LineBasicMaterial.d.ts b/src-testing/src/materials/LineBasicMaterial.d.ts new file mode 100644 index 000000000..791193eee --- /dev/null +++ b/src-testing/src/materials/LineBasicMaterial.d.ts @@ -0,0 +1,60 @@ +import { Color, ColorRepresentation } from "../math/Color.js"; +import { Texture } from "../textures/Texture.js"; +import { Material, MaterialParameters } from "./Material.js"; + +export interface LineBasicMaterialParameters extends MaterialParameters { + color?: ColorRepresentation | undefined; + fog?: boolean | undefined; + linewidth?: number | undefined; + linecap?: string | undefined; + linejoin?: string | undefined; +} + +export class LineBasicMaterial extends Material { + constructor(parameters?: LineBasicMaterialParameters); + + /** + * Read-only flag to check if a given object is of type {@link LineBasicMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isLineBasicMaterial: true; + + /** + * @default 'LineBasicMaterial' + */ + type: string; + + /** + * @default 0xffffff + */ + color: Color; + + /** + * Whether the material is affected by fog. Default is true. + * @default true + */ + fog: boolean; + + /** + * @default 1 + */ + linewidth: number; + + /** + * @default 'round' + */ + linecap: string; + + /** + * @default 'round' + */ + linejoin: string; + + /** + * Sets the color of the lines using data from a {@link Texture}. + */ + map: Texture | null; + + setValues(parameters: LineBasicMaterialParameters): void; +} diff --git a/src-testing/src/materials/LineDashedMaterial.d.ts b/src-testing/src/materials/LineDashedMaterial.d.ts new file mode 100644 index 000000000..fd5ee9ae6 --- /dev/null +++ b/src-testing/src/materials/LineDashedMaterial.d.ts @@ -0,0 +1,40 @@ +import { LineBasicMaterial, LineBasicMaterialParameters } from "./LineBasicMaterial.js"; + +export interface LineDashedMaterialParameters extends LineBasicMaterialParameters { + scale?: number | undefined; + dashSize?: number | undefined; + gapSize?: number | undefined; +} + +export class LineDashedMaterial extends LineBasicMaterial { + constructor(parameters?: LineDashedMaterialParameters); + + /** + * Read-only flag to check if a given object is of type {@link LineDashedMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isLineDashedMaterial: true; + + /** + * @default 'LineDashedMaterial' + */ + type: string; + + /** + * @default 1 + */ + scale: number; + + /** + * @default 1 + */ + dashSize: number; + + /** + * @default 1 + */ + gapSize: number; + + setValues(parameters: LineDashedMaterialParameters): void; +} diff --git a/src-testing/src/materials/Material.d.ts b/src-testing/src/materials/Material.d.ts new file mode 100644 index 000000000..9abe3ab50 --- /dev/null +++ b/src-testing/src/materials/Material.d.ts @@ -0,0 +1,631 @@ +import { Camera } from "../cameras/Camera.js"; +import { + Blending, + BlendingDstFactor, + BlendingEquation, + BlendingSrcFactor, + Combine, + DepthModes, + NormalMapTypes, + PixelFormat, + Side, + StencilFunc, + StencilOp, +} from "../constants.js"; +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { EventDispatcher } from "../core/EventDispatcher.js"; +import { JSONMeta, Object3D } from "../core/Object3D.js"; +import { Color, ColorRepresentation } from "../math/Color.js"; +import { Plane } from "../math/Plane.js"; +import { Group } from "../objects/Group.js"; +import { WebGLProgramParametersWithUniforms } from "../renderers/webgl/WebGLPrograms.js"; +import { WebGLRenderer } from "../renderers/WebGLRenderer.js"; +import { Scene } from "../scenes/Scene.js"; +import { EulerTuple, SourceJSON, TextureJSON, Vector2Tuple } from "../Three.js"; + +export interface MaterialParameters { + alphaHash?: boolean | undefined; + alphaTest?: number | undefined; + alphaToCoverage?: boolean | undefined; + blendAlpha?: number | undefined; + blendColor?: ColorRepresentation | undefined; + blendDst?: BlendingDstFactor | undefined; + blendDstAlpha?: number | undefined; + blendEquation?: BlendingEquation | undefined; + blendEquationAlpha?: number | undefined; + blending?: Blending | undefined; + blendSrc?: BlendingSrcFactor | BlendingDstFactor | undefined; + blendSrcAlpha?: number | undefined; + clipIntersection?: boolean | undefined; + clippingPlanes?: Plane[] | undefined; + clipShadows?: boolean | undefined; + colorWrite?: boolean | undefined; + defines?: any; + depthFunc?: DepthModes | undefined; + depthTest?: boolean | undefined; + depthWrite?: boolean | undefined; + name?: string | undefined; + opacity?: number | undefined; + polygonOffset?: boolean | undefined; + polygonOffsetFactor?: number | undefined; + polygonOffsetUnits?: number | undefined; + precision?: "highp" | "mediump" | "lowp" | null | undefined; + premultipliedAlpha?: boolean | undefined; + forceSinglePass?: boolean | undefined; + dithering?: boolean | undefined; + side?: Side | undefined; + shadowSide?: Side | undefined; + toneMapped?: boolean | undefined; + transparent?: boolean | undefined; + vertexColors?: boolean | undefined; + visible?: boolean | undefined; + format?: PixelFormat | undefined; + stencilWrite?: boolean | undefined; + stencilFunc?: StencilFunc | undefined; + stencilRef?: number | undefined; + stencilWriteMask?: number | undefined; + stencilFuncMask?: number | undefined; + stencilFail?: StencilOp | undefined; + stencilZFail?: StencilOp | undefined; + stencilZPass?: StencilOp | undefined; + userData?: Record | undefined; +} + +export interface MaterialJSON { + metadata: { version: number; type: string; generator: string }; + + uuid: string; + type: string; + + name?: string; + + color?: number; + roughness?: number; + metalness?: number; + + sheen?: number; + sheenColor?: number; + sheenRoughness?: number; + emissive?: number; + emissiveIntensity?: number; + + specular?: number; + specularIntensity?: number; + specularColor?: number; + shininess?: number; + clearcoat?: number; + clearcoatRoughness?: number; + clearcoatMap?: string; + clearcoatRoughnessMap?: string; + clearcoatNormalMap?: string; + clearcoatNormalScale?: Vector2Tuple; + + dispersion?: number; + + iridescence?: number; + iridescenceIOR?: number; + iridescenceThicknessRange?: number; + iridescenceMap?: string; + iridescenceThicknessMap?: string; + + anisotropy?: number; + anisotropyRotation?: number; + anisotropyMap?: string; + + map?: string; + matcap?: string; + alphaMap?: string; + + lightMap?: string; + lightMapIntensity?: number; + + aoMap?: string; + aoMapIntensity?: number; + + bumpMap?: string; + bumpScale?: number; + + normalMap?: string; + normalMapType?: NormalMapTypes; + normalScale?: Vector2Tuple; + + displacementMap?: string; + displacementScale?: number; + displacementBias?: number; + + roughnessMap?: string; + metalnessMap?: string; + + emissiveMap?: string; + specularMap?: string; + specularIntensityMap?: string; + specularColorMap?: string; + + envMap?: string; + combine?: Combine; + + envMapRotation?: EulerTuple; + envMapIntensity?: number; + reflectivity?: number; + refractionRatio?: number; + + gradientMap?: string; + + transmission?: number; + transmissionMap?: string; + thickness?: number; + thicknessMap?: string; + attenuationDistance?: number; + attenuationColor?: number; + + size?: number; + shadowSide?: number; + sizeAttenuation?: boolean; + + blending?: Blending; + side?: Side; + vertexColors?: boolean; + + opacity?: number; + transparent?: boolean; + + blendSrc?: BlendingSrcFactor; + blendDst?: BlendingDstFactor; + blendEquation?: BlendingEquation; + blendSrcAlpha?: number | null; + blendDstAlpha?: number | null; + blendEquationAlpha?: number | null; + blendColor?: number; + blendAlpha?: number; + + depthFunc?: DepthModes; + depthTest?: boolean; + depthWrite?: boolean; + colorWrite?: boolean; + + stencilWriteMask?: number; + stencilFunc?: StencilFunc; + stencilRef?: number; + stencilFuncMask?: number; + stencilFail?: StencilOp; + stencilZFail?: StencilOp; + stencilZPass?: StencilOp; + stencilWrite?: boolean; + + rotation?: number; + + polygonOffset?: boolean; + polygonOffsetFactor?: number; + polygonOffsetUnits?: number; + + linewidth?: number; + dashSize?: number; + gapSize?: number; + scale?: number; + + dithering?: boolean; + + alphaTest?: number; + alphaHash?: boolean; + alphaToCoverage?: boolean; + premultipliedAlpha?: boolean; + forceSinglePass?: boolean; + + wireframe?: boolean; + wireframeLinewidth?: number; + wireframeLinecap?: string; + wireframeLinejoin?: string; + + flatShading?: boolean; + + visible?: boolean; + + toneMapped?: boolean; + + fog?: boolean; + + userData?: Record; + + textures?: Array>; + images?: SourceJSON[]; +} + +/** + * Materials describe the appearance of objects. They are defined in a (mostly) renderer-independent way, so you don't have to rewrite materials if you decide to use a different renderer. + */ +export class Material extends EventDispatcher<{ dispose: {} }> { + constructor(); + + /** + * Read-only flag to check if a given object is of type {@link Material}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isMaterial: true; + + /** + * Enables alpha hashed transparency, an alternative to {@link .transparent} or {@link .alphaTest}. The material + * will not be rendered if opacity is lower than a random threshold. Randomization introduces some grain or noise, + * but approximates alpha blending without the associated problems of sorting. Using TAARenderPass can reduce the + * resulting noise. + */ + alphaHash: boolean; + + /** + * Enables alpha to coverage. Can only be used with MSAA-enabled rendering contexts (meaning when the renderer was + * created with *antialias* parameter set to `true`). Enabling this will smooth aliasing on clip plane edges and + * alphaTest-clipped edges. + * @default false + */ + alphaToCoverage: boolean; + + /** + * Represents the alpha value of the constant blend color. This property has only an effect when using custom + * blending with {@link ConstantAlphaFactor} or {@link OneMinusConstantAlphaFactor}. + * @default 0 + */ + blendAlpha: number; + + /** + * Represent the RGB values of the constant blend color. This property has only an effect when using custom + * blending with {@link ConstantColorFactor} or {@link OneMinusConstantColorFactor}. + * @default 0x000000 + */ + blendColor: Color; + + /** + * Blending destination. It's one of the blending mode constants defined in Three.js. Default is {@link OneMinusSrcAlphaFactor}. + * @default THREE.OneMinusSrcAlphaFactor + */ + blendDst: BlendingDstFactor; + + /** + * The tranparency of the .blendDst. Default is null. + * @default null + */ + blendDstAlpha: number | null; + + /** + * Blending equation to use when applying blending. It's one of the constants defined in Three.js. Default is {@link AddEquation}. + * @default THREE.AddEquation + */ + blendEquation: BlendingEquation; + + /** + * The tranparency of the .blendEquation. Default is null. + * @default null + */ + blendEquationAlpha: number | null; + + /** + * Which blending to use when displaying objects with this material. Default is {@link NormalBlending}. + * @default THREE.NormalBlending + */ + blending: Blending; + + /** + * Blending source. It's one of the blending mode constants defined in Three.js. Default is {@link SrcAlphaFactor}. + * @default THREE.SrcAlphaFactor + */ + blendSrc: BlendingSrcFactor | BlendingDstFactor; + + /** + * The tranparency of the .blendSrc. Default is null. + * @default null + */ + blendSrcAlpha: number | null; + + /** + * Changes the behavior of clipping planes so that only their intersection is clipped, rather than their union. Default is false. + * @default false + */ + clipIntersection: boolean; + + /** + * User-defined clipping planes specified as THREE.Plane objects in world space. + * These planes apply to the objects this material is attached to. + * Points in space whose signed distance to the plane is negative are clipped (not rendered). + * See the WebGL / clipping /intersection example. Default is null. + * @default null + */ + clippingPlanes: Plane[] | null; + + /** + * Defines whether to clip shadows according to the clipping planes specified on this material. Default is false. + * @default false + */ + clipShadows: boolean; + + /** + * Whether to render the material's color. This can be used in conjunction with a mesh's .renderOrder property to create invisible objects that occlude other objects. Default is true. + * @default true + */ + colorWrite: boolean; + + /** + * Custom defines to be injected into the shader. These are passed in form of an object literal, with key/value pairs. { MY_CUSTOM_DEFINE: '' , PI2: Math.PI * 2 }. + * The pairs are defined in both vertex and fragment shaders. Default is undefined. + * @default undefined + */ + defines: undefined | { [key: string]: any }; + + /** + * Which depth function to use. Default is {@link LessEqualDepth}. See the depth mode constants for all possible values. + * @default THREE.LessEqualDepth + */ + depthFunc: DepthModes; + + /** + * Whether to have depth test enabled when rendering this material. When the depth test is disabled, the depth write + * will also be implicitly disabled. + * @default true + */ + depthTest: boolean; + + /** + * Whether rendering this material has any effect on the depth buffer. Default is true. + * When drawing 2D overlays it can be useful to disable the depth writing in order to layer several things together without creating z-index artifacts. + * @default true + */ + depthWrite: boolean; + + /** + * Unique number of this material instance. + */ + id: number; + + /** + * Whether rendering this material has any effect on the stencil buffer. Default is *false*. + * @default false + */ + stencilWrite: boolean; + + /** + * The stencil comparison function to use. Default is {@link AlwaysStencilFunc}. See stencil operation constants for all possible values. + * @default THREE.AlwaysStencilFunc + */ + stencilFunc: StencilFunc; + + /** + * The value to use when performing stencil comparisons or stencil operations. Default is *0*. + * @default 0 + */ + stencilRef: number; + + /** + * The bit mask to use when writing to the stencil buffer. Default is *0xFF*. + * @default 0xff + */ + stencilWriteMask: number; + + /** + * The bit mask to use when comparing against the stencil buffer. Default is *0xFF*. + * @default 0xff + */ + stencilFuncMask: number; + + /** + * Which stencil operation to perform when the comparison function returns false. Default is {@link KeepStencilOp}. See the stencil operation constants for all possible values. + * @default THREE.KeepStencilOp + */ + stencilFail: StencilOp; + + /** + * Which stencil operation to perform when the comparison function returns true but the depth test fails. + * Default is {@link KeepStencilOp}. + * See the stencil operation constants for all possible values. + * @default THREE.KeepStencilOp + */ + stencilZFail: StencilOp; + + /** + * Which stencil operation to perform when the comparison function returns true and the depth test passes. + * Default is {@link KeepStencilOp}. + * See the stencil operation constants for all possible values. + * @default THREE.KeepStencilOp + */ + stencilZPass: StencilOp; + + /** + * Material name. Default is an empty string. + * @default '' + */ + name: string; + + /** + * Opacity. Default is 1. + * @default 1 + */ + opacity: number; + + /** + * Whether to use polygon offset. Default is false. This corresponds to the POLYGON_OFFSET_FILL WebGL feature. + * @default false + */ + polygonOffset: boolean; + + /** + * Sets the polygon offset factor. Default is 0. + * @default 0 + */ + polygonOffsetFactor: number; + + /** + * Sets the polygon offset units. Default is 0. + * @default 0 + */ + polygonOffsetUnits: number; + + /** + * Override the renderer's default precision for this material. Can be "highp", "mediump" or "lowp". Defaults is null. + * @default null + */ + precision: "highp" | "mediump" | "lowp" | null; + + /** + * Whether to premultiply the alpha (transparency) value. See WebGL / Materials / Transparency for an example of the difference. Default is false. + * @default false + */ + premultipliedAlpha: boolean; + + /** + * @default false + */ + forceSinglePass: boolean; + + /** + * Whether to apply dithering to the color to remove the appearance of banding. Default is false. + * @default false + */ + dithering: boolean; + + /** + * Defines which of the face sides will be rendered - front, back or both. + * Default is {@link THREE.FrontSide}. Other options are {@link THREE.BackSide} and {@link THREE.DoubleSide}. + * + * @default {@link THREE.FrontSide} + */ + side: Side; + + /** + * Defines which of the face sides will cast shadows. Default is *null*. + * If *null*, the value is opposite that of side, above. + * @default null + */ + shadowSide: Side | null; + + /** + * Defines whether this material is tone mapped according to the renderer's + * {@link WebGLRenderer.toneMapping toneMapping} setting. It is ignored when rendering to a render target or using + * post processing. + * @default true + */ + toneMapped: boolean; + + /** + * Defines whether this material is transparent. This has an effect on rendering as transparent objects need special treatment and are rendered after non-transparent objects. + * When set to true, the extent to which the material is transparent is controlled by setting it's .opacity property. + * @default false + */ + transparent: boolean; + + /** + * Value is the string 'Material'. This shouldn't be changed, and can be used to find all objects of this type in a scene. + * @default 'Material' + */ + type: string; + + /** + * UUID of this material instance. This gets automatically assigned, so this shouldn't be edited. + */ + uuid: string; + + /** + * Defines whether vertex coloring is used. Default is false. + * @default false + */ + vertexColors: boolean; + + /** + * Defines whether this material is visible. Default is true. + * @default true + */ + visible: boolean; + + /** + * An object that can be used to store custom data about the Material. It should not hold references to functions as these will not be cloned. + * @default {} + */ + userData: Record; + + /** + * This starts at 0 and counts how many times .needsUpdate is set to true. + * @default 0 + */ + version: number; + + /** + * Gets the alpha value to be used when running an alpha test. Default is 0. + * @default 0 + */ + get alphaTest(): number; + + /** + * Sets the alpha value to be used when running an alpha test. Default is 0. + * @default 0 + */ + set alphaTest(value: number); + + /** + * An optional callback that is executed immediately before the material is used to render a 3D object. + * Unlike properties, the callback is not supported by {@link .clone()}, {@link .copy()} and {@link .toJSON()}. + * This callback is only supported in `WebGLRenderer` (not `WebGPURenderer`). + */ + onBeforeRender( + renderer: WebGLRenderer, + scene: Scene, + camera: Camera, + geometry: BufferGeometry, + object: Object3D, + group: Group, + ): void; + + /** + * An optional callback that is executed immediately before the shader program is compiled. + * This function is called with the shader source code as a parameter. + * Useful for the modification of built-in materials. + * Unlike properties, the callback is not supported by {@link .clone()}, {@link .copy()} and {@link .toJSON()}. + * This callback is only supported in `WebGLRenderer` (not `WebGPURenderer`). + * @param parameters WebGL program parameters + * @param renderer WebGLRenderer context that is initializing the material + */ + onBeforeCompile(parameters: WebGLProgramParametersWithUniforms, renderer: WebGLRenderer): void; + + /** + * In case onBeforeCompile is used, this callback can be used to identify values of settings used in onBeforeCompile, so three.js can reuse a cached shader or recompile the shader as needed. + */ + customProgramCacheKey(): string; + + /** + * Sets the properties based on the values. + * @param values A container with parameters. + */ + setValues(values: MaterialParameters): void; + + /** + * Convert the material to three.js JSON format. + * @param meta Object containing metadata such as textures or images for the material. + */ + toJSON(meta?: JSONMeta): MaterialJSON; + + /** + * Return a new material with the same parameters as this material. + */ + clone(): this; + + /** + * Copy the parameters from the passed material into this material. + * @param material + */ + copy(material: Material): this; + + /** + * Frees the GPU-related resources allocated by this instance. Call this method whenever this instance is no longer + * used in your app. + * + * Material textures must be disposed of by the dispose() method of {@link Texture}. + */ + dispose(): void; + + /** + * Specifies that the material needs to be updated, WebGL wise. Set it to true if you made changes that need to be reflected in WebGL. + * This property is automatically set to true when instancing a new material. + * @default false + */ + set needsUpdate(value: boolean); + + /** + * @deprecated onBuild() has been removed. + */ + onBuild(object: Object3D, parameters: WebGLProgramParametersWithUniforms, renderer: WebGLRenderer): void; +} diff --git a/src-testing/src/materials/Materials.d.ts b/src-testing/src/materials/Materials.d.ts new file mode 100644 index 000000000..dbca5e5b7 --- /dev/null +++ b/src-testing/src/materials/Materials.d.ts @@ -0,0 +1,18 @@ +export * from "./LineBasicMaterial.js"; +export * from "./LineDashedMaterial.js"; +export * from "./Material.js"; +export * from "./MeshBasicMaterial.js"; +export * from "./MeshDepthMaterial.js"; +export * from "./MeshDistanceMaterial.js"; +export * from "./MeshLambertMaterial.js"; +export * from "./MeshMatcapMaterial.js"; +export * from "./MeshNormalMaterial.js"; +export * from "./MeshPhongMaterial.js"; +export * from "./MeshPhysicalMaterial.js"; +export * from "./MeshStandardMaterial.js"; +export * from "./MeshToonMaterial.js"; +export * from "./PointsMaterial.js"; +export * from "./RawShaderMaterial.js"; +export * from "./ShaderMaterial.js"; +export * from "./ShadowMaterial.js"; +export * from "./SpriteMaterial.js"; diff --git a/src-testing/src/materials/MeshBasicMaterial.d.ts b/src-testing/src/materials/MeshBasicMaterial.d.ts new file mode 100644 index 000000000..10fab3cfa --- /dev/null +++ b/src-testing/src/materials/MeshBasicMaterial.d.ts @@ -0,0 +1,139 @@ +import { Combine } from "../constants.js"; +import { Color, ColorRepresentation } from "../math/Color.js"; +import { Euler } from "../math/Euler.js"; +import { Texture } from "../textures/Texture.js"; +import { Material, MaterialParameters } from "./Material.js"; + +/** + * parameters is an object with one or more properties defining the material's appearance. + */ +export interface MeshBasicMaterialParameters extends MaterialParameters { + color?: ColorRepresentation | undefined; + opacity?: number | undefined; + map?: Texture | null | undefined; + lightMap?: Texture | null; + lightMapIntensity?: number | undefined; + aoMap?: Texture | null | undefined; + aoMapIntensity?: number | undefined; + specularMap?: Texture | null | undefined; + alphaMap?: Texture | null | undefined; + fog?: boolean | undefined; + envMap?: Texture | null | undefined; + envMapRotation?: Euler | undefined; + combine?: Combine | undefined; + reflectivity?: number | undefined; + refractionRatio?: number | undefined; + wireframe?: boolean | undefined; + wireframeLinewidth?: number | undefined; + wireframeLinecap?: string | undefined; + wireframeLinejoin?: string | undefined; +} + +export class MeshBasicMaterial extends Material { + constructor(parameters?: MeshBasicMaterialParameters); + + /** + * Read-only flag to check if a given object is of type {@link MeshBasicMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isMeshBasicMaterial: true; + + /** + * @default 'MeshBasicMaterial' + */ + type: string; + + /** + * @default new THREE.Color( 0xffffff ) + */ + color: Color; + + /** + * @default null + */ + map: Texture | null; + + /** + * @default null + */ + lightMap: Texture | null; + + /** + * @default 1 + */ + lightMapIntensity: number; + + /** + * @default null + */ + aoMap: Texture | null; + + /** + * @default 1 + */ + aoMapIntensity: number; + + /** + * @default null + */ + specularMap: Texture | null; + + /** + * @default null + */ + alphaMap: Texture | null; + + /** + * @default null + */ + envMap: Texture | null; + + /** + * The rotation of the environment map in radians. Default is `(0,0,0)`. + */ + envMapRotation: Euler; + + /** + * @default THREE.MultiplyOperation + */ + combine: Combine; + + /** + * @default 1 + */ + reflectivity: number; + + /** + * @default 0.98 + */ + refractionRatio: number; + + /** + * @default false + */ + wireframe: boolean; + + /** + * @default 1 + */ + wireframeLinewidth: number; + + /** + * @default 'round' + */ + wireframeLinecap: string; + + /** + * @default 'round' + */ + wireframeLinejoin: string; + + /** + * Whether the material is affected by fog. Default is true. + * @default fog + */ + fog: boolean; + + setValues(parameters: MeshBasicMaterialParameters): void; +} diff --git a/src-testing/src/materials/MeshDepthMaterial.d.ts b/src-testing/src/materials/MeshDepthMaterial.d.ts new file mode 100644 index 000000000..86be99b6c --- /dev/null +++ b/src-testing/src/materials/MeshDepthMaterial.d.ts @@ -0,0 +1,76 @@ +import { DepthPackingStrategies } from "../constants.js"; +import { Texture } from "../textures/Texture.js"; +import { Material, MaterialParameters } from "./Material.js"; + +export interface MeshDepthMaterialParameters extends MaterialParameters { + map?: Texture | null | undefined; + alphaMap?: Texture | null | undefined; + depthPacking?: DepthPackingStrategies | undefined; + displacementMap?: Texture | null | undefined; + displacementScale?: number | undefined; + displacementBias?: number | undefined; + wireframe?: boolean | undefined; + wireframeLinewidth?: number | undefined; +} + +export class MeshDepthMaterial extends Material { + constructor(parameters?: MeshDepthMaterialParameters); + /** + * Read-only flag to check if a given object is of type {@link MeshDepthMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isMeshDepthMaterial: true; + + /** + * @default 'MeshDepthMaterial' + */ + type: string; + + /** + * @default null + */ + map: Texture | null; + + /** + * @default null + */ + alphaMap: Texture | null; + + /** + * @default THREE.BasicDepthPacking + */ + depthPacking: DepthPackingStrategies; + + /** + * @default null + */ + displacementMap: Texture | null; + + /** + * @default 1 + */ + displacementScale: number; + + /** + * @default 0 + */ + displacementBias: number; + + /** + * @default false + */ + wireframe: boolean; + + /** + * @default 1 + */ + wireframeLinewidth: number; + + /** + * @default false + */ + fog: boolean; + + setValues(parameters: MeshDepthMaterialParameters): void; +} diff --git a/src-testing/src/materials/MeshDistanceMaterial.d.ts b/src-testing/src/materials/MeshDistanceMaterial.d.ts new file mode 100644 index 000000000..8a27c00ee --- /dev/null +++ b/src-testing/src/materials/MeshDistanceMaterial.d.ts @@ -0,0 +1,62 @@ +import { Vector3 } from "../math/Vector3.js"; +import { Texture } from "../textures/Texture.js"; +import { Material, MaterialParameters } from "./Material.js"; + +export interface MeshDistanceMaterialParameters extends MaterialParameters { + map?: Texture | null | undefined; + alphaMap?: Texture | null | undefined; + displacementMap?: Texture | null | undefined; + displacementScale?: number | undefined; + displacementBias?: number | undefined; + farDistance?: number | undefined; + nearDistance?: number | undefined; + referencePosition?: Vector3 | undefined; +} + +export class MeshDistanceMaterial extends Material { + constructor(parameters?: MeshDistanceMaterialParameters); + + /** + * Read-only flag to check if a given object is of type {@link MeshDistanceMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isMeshDistanceMaterial: true; + + /** + * @default 'MeshDistanceMaterial' + */ + type: string; + + /** + * @default null + */ + map: Texture | null; + + /** + * @default null + */ + alphaMap: Texture | null; + + /** + * @default null + */ + displacementMap: Texture | null; + + /** + * @default 1 + */ + displacementScale: number; + + /** + * @default 0 + */ + displacementBias: number; + + /** + * @default false + */ + fog: boolean; + + setValues(parameters: MeshDistanceMaterialParameters): void; +} diff --git a/src-testing/src/materials/MeshLambertMaterial.d.ts b/src-testing/src/materials/MeshLambertMaterial.d.ts new file mode 100644 index 000000000..47467d100 --- /dev/null +++ b/src-testing/src/materials/MeshLambertMaterial.d.ts @@ -0,0 +1,204 @@ +import { Combine, NormalMapTypes } from "../constants.js"; +import { Color, ColorRepresentation } from "../math/Color.js"; +import { Euler } from "../math/Euler.js"; +import { Vector2 } from "../math/Vector2.js"; +import { Texture } from "../textures/Texture.js"; +import { Material, MaterialParameters } from "./Material.js"; + +export interface MeshLambertMaterialParameters extends MaterialParameters { + bumpMap?: Texture | undefined; + bumpScale?: number | undefined; + color?: ColorRepresentation | undefined; + displacementMap?: Texture | undefined; + displacementScale?: number | undefined; + displacementBias?: number | undefined; + emissive?: ColorRepresentation | undefined; + emissiveIntensity?: number | undefined; + emissiveMap?: Texture | null | undefined; + flatShading?: boolean | undefined; + map?: Texture | null | undefined; + lightMap?: Texture | null | undefined; + lightMapIntensity?: number | undefined; + normalMap?: Texture | undefined; + normalScale?: Vector2 | undefined; + aoMap?: Texture | null | undefined; + aoMapIntensity?: number | undefined; + specularMap?: Texture | null | undefined; + alphaMap?: Texture | null | undefined; + envMap?: Texture | null | undefined; + envMapRotation?: Euler | undefined; + combine?: Combine | undefined; + reflectivity?: number | undefined; + refractionRatio?: number | undefined; + wireframe?: boolean | undefined; + wireframeLinewidth?: number | undefined; + wireframeLinecap?: string | undefined; + wireframeLinejoin?: string | undefined; + fog?: boolean | undefined; +} + +export class MeshLambertMaterial extends Material { + constructor(parameters?: MeshLambertMaterialParameters); + + /** + * Read-only flag to check if a given object is of type {@link MeshLambertMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isMeshLambertMaterial: true; + + /** + * @default 'MeshLambertMaterial' + */ + type: string; + + /** + * @default new THREE.Color( 0xffffff ) + */ + color: Color; + + /** + * @default null + */ + bumpMap: Texture | null; + + /** + * @default 1 + */ + bumpScale: number; + + /** + * @default null + */ + displacementMap: Texture | null; + + /** + * @default 1 + */ + displacementScale: number; + + /** + * @default 0 + */ + displacementBias: number; + + /** + * @default new THREE.Color( 0x000000 ) + */ + emissive: Color; + + /** + * @default 1 + */ + emissiveIntensity: number; + + /** + * @default null + */ + emissiveMap: Texture | null; + + /** + * @default false + */ + flatShading: boolean; + + /** + * @default null + */ + map: Texture | null; + + /** + * @default null + */ + lightMap: Texture | null; + + /** + * @default 1 + */ + lightMapIntensity: number; + + /** + * @default null + */ + normalMap: Texture | null; + + normalMapType: NormalMapTypes; + + /** + * @default new THREE.Vector2( 1, 1 ) + */ + normalScale: Vector2; + + /** + * @default null + */ + aoMap: Texture | null; + + /** + * @default 1 + */ + aoMapIntensity: number; + + /** + * @default null + */ + specularMap: Texture | null; + + /** + * @default null + */ + alphaMap: Texture | null; + + /** + * @default null + */ + envMap: Texture | null; + + /** + * The rotation of the environment map in radians. Default is `(0,0,0)`. + */ + envMapRotation: Euler; + + /** + * @default THREE.MultiplyOperation + */ + combine: Combine; + + /** + * @default 1 + */ + reflectivity: number; + + /** + * @default 0.98 + */ + refractionRatio: number; + + /** + * @default false + */ + wireframe: boolean; + + /** + * @default 1 + */ + wireframeLinewidth: number; + + /** + * @default 'round' + */ + wireframeLinecap: string; + + /** + * @default 'round' + */ + wireframeLinejoin: string; + + /** + * Whether the material is affected by fog. Default is true. + * @default fog + */ + fog: boolean; + + setValues(parameters: MeshLambertMaterialParameters): void; +} diff --git a/src-testing/src/materials/MeshMatcapMaterial.d.ts b/src-testing/src/materials/MeshMatcapMaterial.d.ts new file mode 100644 index 000000000..51429464b --- /dev/null +++ b/src-testing/src/materials/MeshMatcapMaterial.d.ts @@ -0,0 +1,117 @@ +import { NormalMapTypes } from "../constants.js"; +import { Color, ColorRepresentation } from "../math/Color.js"; +import { Vector2 } from "../math/Vector2.js"; +import { Texture } from "../textures/Texture.js"; +import { Material, MaterialParameters } from "./Material.js"; + +export interface MeshMatcapMaterialParameters extends MaterialParameters { + color?: ColorRepresentation | undefined; + matcap?: Texture | null | undefined; + map?: Texture | null | undefined; + bumpMap?: Texture | null | undefined; + bumpScale?: number | undefined; + normalMap?: Texture | null | undefined; + normalMapType?: NormalMapTypes | undefined; + normalScale?: Vector2 | undefined; + displacementMap?: Texture | null | undefined; + displacementScale?: number | undefined; + displacementBias?: number | undefined; + alphaMap?: Texture | null | undefined; + fog?: boolean | undefined; + flatShading?: boolean | undefined; +} + +export class MeshMatcapMaterial extends Material { + constructor(parameters?: MeshMatcapMaterialParameters); + + /** + * Read-only flag to check if a given object is of type {@link MeshMatcapMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isMeshMatcapMaterial: true; + + /** + * @default 'MeshMatcapMaterial' + */ + type: string; + + /** + * @default { 'MATCAP': '' } + */ + defines: { [key: string]: any }; + + /** + * @default new THREE.Color( 0xffffff ) + */ + color: Color; + + /** + * @default null + */ + matcap: Texture | null; + + /** + * @default null + */ + map: Texture | null; + + /** + * @default null + */ + bumpMap: Texture | null; + + /** + * @default 1 + */ + bumpScale: number; + + /** + * @default null + */ + normalMap: Texture | null; + + /** + * @default THREE.TangentSpaceNormalMap + */ + normalMapType: NormalMapTypes; + + /** + * @default new Vector2( 1, 1 ) + */ + normalScale: Vector2; + + /** + * @default null + */ + displacementMap: Texture | null; + + /** + * @default 1 + */ + displacementScale: number; + + /** + * @default 0 + */ + displacementBias: number; + + /** + * @default null + */ + alphaMap: Texture | null; + + /** + * Define whether the material is rendered with flat shading. Default is false. + * @default false + */ + flatShading: boolean; + + /** + * Whether the material is affected by fog. Default is true. + * @default fog + */ + fog: boolean; + + setValues(parameters: MeshMatcapMaterialParameters): void; +} diff --git a/src-testing/src/materials/MeshNormalMaterial.d.ts b/src-testing/src/materials/MeshNormalMaterial.d.ts new file mode 100644 index 000000000..b779f5e29 --- /dev/null +++ b/src-testing/src/materials/MeshNormalMaterial.d.ts @@ -0,0 +1,93 @@ +import { NormalMapTypes } from "../constants.js"; +import { Vector2 } from "../math/Vector2.js"; +import { Texture } from "../textures/Texture.js"; +import { Material, MaterialParameters } from "./Material.js"; + +export interface MeshNormalMaterialParameters extends MaterialParameters { + bumpMap?: Texture | null | undefined; + bumpScale?: number | undefined; + normalMap?: Texture | null | undefined; + normalMapType?: NormalMapTypes | undefined; + normalScale?: Vector2 | undefined; + displacementMap?: Texture | null | undefined; + displacementScale?: number | undefined; + displacementBias?: number | undefined; + wireframe?: boolean | undefined; + wireframeLinewidth?: number | undefined; + + flatShading?: boolean | undefined; +} + +export class MeshNormalMaterial extends Material { + constructor(parameters?: MeshNormalMaterialParameters); + + /** + * Read-only flag to check if a given object is of type {@link MeshNormalMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isMeshNormalMaterial: true; + + /** + * @default 'MeshNormalMaterial' + */ + type: string; + + /** + * @default null + */ + bumpMap: Texture | null; + + /** + * @default 1 + */ + bumpScale: number; + + /** + * @default null + */ + normalMap: Texture | null; + + /** + * @default THREE.TangentSpaceNormalMap + */ + normalMapType: NormalMapTypes; + + /** + * @default new THREE.Vector2( 1, 1 ) + */ + normalScale: Vector2; + + /** + * @default null + */ + displacementMap: Texture | null; + + /** + * @default 1 + */ + displacementScale: number; + + /** + * @default 0 + */ + displacementBias: number; + + /** + * @default false + */ + wireframe: boolean; + + /** + * @default 1 + */ + wireframeLinewidth: number; + + /** + * Define whether the material is rendered with flat shading. Default is false. + * @default false + */ + flatShading: boolean; + + setValues(parameters: MeshNormalMaterialParameters): void; +} diff --git a/src-testing/src/materials/MeshPhongMaterial.d.ts b/src-testing/src/materials/MeshPhongMaterial.d.ts new file mode 100644 index 000000000..fb6bce256 --- /dev/null +++ b/src-testing/src/materials/MeshPhongMaterial.d.ts @@ -0,0 +1,228 @@ +import { Combine, NormalMapTypes } from "../constants.js"; +import { Color, ColorRepresentation } from "../math/Color.js"; +import { Euler } from "../math/Euler.js"; +import { Vector2 } from "../math/Vector2.js"; +import { Texture } from "../textures/Texture.js"; +import { Material, MaterialParameters } from "./Material.js"; + +export interface MeshPhongMaterialParameters extends MaterialParameters { + /** geometry color in hexadecimal. Default is 0xffffff. */ + color?: ColorRepresentation | undefined; + specular?: ColorRepresentation | undefined; + shininess?: number | undefined; + opacity?: number | undefined; + map?: Texture | null | undefined; + lightMap?: Texture | null | undefined; + lightMapIntensity?: number | undefined; + aoMap?: Texture | null | undefined; + aoMapIntensity?: number | undefined; + emissive?: ColorRepresentation | undefined; + emissiveIntensity?: number | undefined; + emissiveMap?: Texture | null | undefined; + bumpMap?: Texture | null | undefined; + bumpScale?: number | undefined; + normalMap?: Texture | null | undefined; + normalMapType?: NormalMapTypes | undefined; + normalScale?: Vector2 | undefined; + displacementMap?: Texture | null | undefined; + displacementScale?: number | undefined; + displacementBias?: number | undefined; + specularMap?: Texture | null | undefined; + alphaMap?: Texture | null | undefined; + envMap?: Texture | null | undefined; + envMapRotation?: Euler | undefined; + combine?: Combine | undefined; + reflectivity?: number | undefined; + refractionRatio?: number | undefined; + wireframe?: boolean | undefined; + wireframeLinewidth?: number | undefined; + wireframeLinecap?: string | undefined; + wireframeLinejoin?: string | undefined; + fog?: boolean | undefined; + flatShading?: boolean | undefined; +} + +export class MeshPhongMaterial extends Material { + constructor(parameters?: MeshPhongMaterialParameters); + + /** + * Read-only flag to check if a given object is of type {@link MeshPhongMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isMeshPhongMaterial: true; + + /** + * @default 'MeshNormalMaterial' + */ + type: string; + + /** + * @default new THREE.Color( 0xffffff ) + */ + color: Color; + + /** + * @default new THREE.Color( 0x111111 ) + */ + specular: Color; + + /** + * @default 30 + */ + shininess: number; + + /** + * @default null + */ + map: Texture | null; + + /** + * @default null + */ + lightMap: Texture | null; + + /** + * @default null + */ + lightMapIntensity: number; + + /** + * @default null + */ + aoMap: Texture | null; + + /** + * @default null + */ + aoMapIntensity: number; + + /** + * @default new THREE.Color( 0x000000 ) + */ + emissive: Color; + + /** + * @default 1 + */ + emissiveIntensity: number; + + /** + * @default null + */ + emissiveMap: Texture | null; + + /** + * @default null + */ + bumpMap: Texture | null; + + /** + * @default 1 + */ + bumpScale: number; + + /** + * @default null + */ + normalMap: Texture | null; + + /** + * @default THREE.TangentSpaceNormalMap + */ + normalMapType: NormalMapTypes; + + /** + * @default new Vector2( 1, 1 ) + */ + normalScale: Vector2; + + /** + * @default null + */ + displacementMap: Texture | null; + + /** + * @default 1 + */ + displacementScale: number; + + /** + * @default 0 + */ + displacementBias: number; + + /** + * @default null + */ + specularMap: Texture | null; + + /** + * @default null + */ + alphaMap: Texture | null; + + /** + * @default null + */ + envMap: Texture | null; + + /** + * The rotation of the environment map in radians. Default is `(0,0,0)`. + */ + envMapRotation: Euler; + + /** + * @default THREE.MultiplyOperation + */ + combine: Combine; + + /** + * @default 1 + */ + reflectivity: number; + + /** + * @default 0.98 + */ + refractionRatio: number; + + /** + * @default false + */ + wireframe: boolean; + + /** + * @default 1 + */ + wireframeLinewidth: number; + + /** + * @default 'round' + */ + wireframeLinecap: string; + + /** + * @default 'round' + */ + wireframeLinejoin: string; + + /** + * Define whether the material is rendered with flat shading. Default is false. + * @default false + */ + flatShading: boolean; + + /** + * @deprecated Use {@link MeshStandardMaterial THREE.MeshStandardMaterial} instead. + */ + metal: boolean; + + /** + * Whether the material is affected by fog. Default is true. + * @default fog + */ + fog: boolean; + + setValues(parameters: MeshPhongMaterialParameters): void; +} diff --git a/src-testing/src/materials/MeshPhysicalMaterial.d.ts b/src-testing/src/materials/MeshPhysicalMaterial.d.ts new file mode 100644 index 000000000..503fe6a6b --- /dev/null +++ b/src-testing/src/materials/MeshPhysicalMaterial.d.ts @@ -0,0 +1,236 @@ +import { Color, ColorRepresentation } from "../math/Color.js"; +import { Vector2 } from "../math/Vector2.js"; +import { Texture } from "../textures/Texture.js"; +import { MeshStandardMaterial, MeshStandardMaterialParameters } from "./MeshStandardMaterial.js"; + +export interface MeshPhysicalMaterialParameters extends MeshStandardMaterialParameters { + anisotropyRotation?: number | undefined; + anisotropyMap?: Texture | null | undefined; + + clearcoatMap?: Texture | null | undefined; + clearcoatRoughness?: number | undefined; + clearcoatRoughnessMap?: Texture | null | undefined; + clearcoatNormalScale?: Vector2 | undefined; + clearcoatNormalMap?: Texture | null | undefined; + + ior?: number | undefined; + + reflectivity?: number | undefined; + + iridescenceMap?: Texture | null | undefined; + iridescenceIOR?: number | undefined; + iridescenceThicknessRange?: [number, number] | undefined; + iridescenceThicknessMap?: Texture | null | undefined; + + sheenColor?: ColorRepresentation | undefined; + sheenColorMap?: Texture | null | undefined; + sheenRoughness?: number | undefined; + sheenRoughnessMap?: Texture | null | undefined; + + transmissionMap?: Texture | null | undefined; + + thickness?: number | undefined; + thicknessMap?: Texture | null | undefined; + attenuationDistance?: number | undefined; + attenuationColor?: ColorRepresentation | undefined; + + specularIntensity?: number | undefined; + specularIntensityMap?: Texture | null | undefined; + specularColor?: ColorRepresentation | undefined; + specularColorMap?: Texture | null | undefined; + + anisotropy?: number | undefined; + clearcoat?: number | undefined; + iridescence?: number | undefined; + dispersion?: number | undefined; + sheen?: number | undefined; + transmission?: number | undefined; +} + +export class MeshPhysicalMaterial extends MeshStandardMaterial { + constructor(parameters?: MeshPhysicalMaterialParameters); + + /** + * Read-only flag to check if a given object is of type {@link MeshPhysicalMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isMeshPhysicalMaterial: true; + + /** + * @default { 'STANDARD': '', 'PHYSICAL': '' } + */ + defines: { [key: string]: any }; + + /** + * @default 'MeshPhysicalMaterial' + */ + type: string; + + /** + * @default 0 + */ + anisotropyRotation?: number; + + /** + * @default null + */ + anisotropyMap?: Texture | null; + + /** + * @default null + */ + clearcoatMap: Texture | null; + + /** + * @default 0 + */ + clearcoatRoughness: number; + + /** + * @default null + */ + clearcoatRoughnessMap: Texture | null; + + /** + * @default new THREE.Vector2( 1, 1 ) + */ + clearcoatNormalScale: Vector2; + + /** + * @default null + */ + clearcoatNormalMap: Texture | null; + + /** + * @default 1.5 + */ + ior: number; + + /** + * @default 0.5 + */ + get reflectivity(): number; + set reflectivity(reflectivity: number); + + /** + * @default null + */ + iridescenceMap: Texture | null; + + /** + * @default 1.3 + */ + iridescenceIOR: number; + + /** + * @default [100, 400] + */ + iridescenceThicknessRange: [number, number]; + + /** + * @default null + */ + iridescenceThicknessMap: Texture | null; + + /** + * @default Color( 0x000000 ) + */ + sheenColor: Color; + + /** + * @default null + */ + sheenColorMap: Texture | null; + + /** + * @default 1.0 + */ + sheenRoughness: number; + + /** + * @default null + */ + sheenRoughnessMap: Texture | null; + + /** + * @default null + */ + transmissionMap: Texture | null; + + /** + * @default 0.01 + */ + thickness: number; + + /** + * @default null + */ + thicknessMap: Texture | null; + + /** + * @default 0.0 + */ + attenuationDistance: number; + + /** + * @default Color( 1, 1, 1 ) + */ + attenuationColor: Color; + + /** + * @default 1.0 + */ + specularIntensity: number; + + /** + * @default null + */ + specularIntensityMap: Texture | null; + + /** + * @default Color(1, 1, 1) + */ + specularColor: Color; + + /** + * @default null + */ + specularColorMap: Texture | null; + + /** + * @default 0 + */ + get anisotropy(): number; + set anisotropy(value: number); + + /** + * @default 0 + */ + get clearcoat(): number; + set clearcoat(value: number); + + /** + * @default 0 + */ + get iridescence(): number; + set iridescence(value: number); + + /** + * @default 0 + */ + get dispersion(): number; + set dispersion(value: number); + + /** + * @default 0.0 + */ + get sheen(): number; + set sheen(value: number); + + /** + * @default 0 + */ + get transmission(): number; + set transmission(value: number); +} diff --git a/src-testing/src/materials/MeshStandardMaterial.d.ts b/src-testing/src/materials/MeshStandardMaterial.d.ts new file mode 100644 index 000000000..ddde9cfba --- /dev/null +++ b/src-testing/src/materials/MeshStandardMaterial.d.ts @@ -0,0 +1,218 @@ +import { NormalMapTypes } from "../constants.js"; +import { Color, ColorRepresentation } from "../math/Color.js"; +import { Euler } from "../math/Euler.js"; +import { Vector2 } from "../math/Vector2.js"; +import { Texture } from "../textures/Texture.js"; +import { Material, MaterialParameters } from "./Material.js"; + +export interface MeshStandardMaterialParameters extends MaterialParameters { + color?: ColorRepresentation | undefined; + roughness?: number | undefined; + metalness?: number | undefined; + map?: Texture | null | undefined; + lightMap?: Texture | null | undefined; + lightMapIntensity?: number | undefined; + aoMap?: Texture | null | undefined; + aoMapIntensity?: number | undefined; + emissive?: ColorRepresentation | undefined; + emissiveIntensity?: number | undefined; + emissiveMap?: Texture | null | undefined; + bumpMap?: Texture | null | undefined; + bumpScale?: number | undefined; + normalMap?: Texture | null | undefined; + normalMapType?: NormalMapTypes | undefined; + normalScale?: Vector2 | undefined; + displacementMap?: Texture | null | undefined; + displacementScale?: number | undefined; + displacementBias?: number | undefined; + roughnessMap?: Texture | null | undefined; + metalnessMap?: Texture | null | undefined; + alphaMap?: Texture | null | undefined; + envMap?: Texture | null | undefined; + envMapRotation?: Euler | undefined; + envMapIntensity?: number | undefined; + wireframe?: boolean | undefined; + wireframeLinewidth?: number | undefined; + fog?: boolean | undefined; + flatShading?: boolean | undefined; +} + +export class MeshStandardMaterial extends Material { + constructor(parameters?: MeshStandardMaterialParameters); + + /** + * Read-only flag to check if a given object is of type {@link MeshStandardMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isMeshStandardMaterial: true; + + /** + * @default 'MeshStandardMaterial' + */ + type: string; + + /** + * @default { 'STANDARD': '' } + */ + defines: { [key: string]: any }; + + /** + * @default new THREE.Color( 0xffffff ) + */ + color: Color; + + /** + * @default 1 + */ + roughness: number; + + /** + * @default 0 + */ + metalness: number; + + /** + * @default null + */ + map: Texture | null; + + /** + * @default null + */ + lightMap: Texture | null; + + /** + * @default 1 + */ + lightMapIntensity: number; + + /** + * @default null + */ + aoMap: Texture | null; + + /** + * @default 1 + */ + aoMapIntensity: number; + + /** + * @default new THREE.Color( 0x000000 ) + */ + emissive: Color; + + /** + * @default 1 + */ + emissiveIntensity: number; + + /** + * @default null + */ + emissiveMap: Texture | null; + + /** + * @default null + */ + bumpMap: Texture | null; + + /** + * @default 1 + */ + bumpScale: number; + + /** + * @default null + */ + normalMap: Texture | null; + + /** + * @default THREE.TangentSpaceNormalMap + */ + normalMapType: NormalMapTypes; + + /** + * @default new THREE.Vector2( 1, 1 ) + */ + normalScale: Vector2; + + /** + * @default null + */ + displacementMap: Texture | null; + + /** + * @default 1 + */ + displacementScale: number; + + /** + * @default 0 + */ + displacementBias: number; + + /** + * @default null + */ + roughnessMap: Texture | null; + + /** + * @default null + */ + metalnessMap: Texture | null; + + /** + * @default null + */ + alphaMap: Texture | null; + + /** + * @default null + */ + envMap: Texture | null; + + /** + * The rotation of the environment map in radians. Default is `(0,0,0)`. + */ + envMapRotation: Euler; + + /** + * @default 1 + */ + envMapIntensity: number; + + /** + * @default false + */ + wireframe: boolean; + + /** + * @default 1 + */ + wireframeLinewidth: number; + + /** + * @default 'round' + */ + wireframeLinecap: string; + + /** + * @default 'round' + */ + wireframeLinejoin: string; + + /** + * Define whether the material is rendered with flat shading. Default is false. + * @default false + */ + flatShading: boolean; + + /** + * Whether the material is affected by fog. Default is true. + * @default fog + */ + fog: boolean; + + setValues(parameters: MeshStandardMaterialParameters): void; +} diff --git a/src-testing/src/materials/MeshToonMaterial.d.ts b/src-testing/src/materials/MeshToonMaterial.d.ts new file mode 100644 index 000000000..0f0c6f61d --- /dev/null +++ b/src-testing/src/materials/MeshToonMaterial.d.ts @@ -0,0 +1,178 @@ +import { NormalMapTypes } from "../constants.js"; +import { Color, ColorRepresentation } from "../math/Color.js"; +import { Vector2 } from "../math/Vector2.js"; +import { Texture } from "../textures/Texture.js"; +import { Material, MaterialParameters } from "./Material.js"; + +export interface MeshToonMaterialParameters extends MaterialParameters { + /** geometry color in hexadecimal. Default is 0xffffff. */ + color?: ColorRepresentation | undefined; + opacity?: number | undefined; + gradientMap?: Texture | null | undefined; + map?: Texture | null | undefined; + lightMap?: Texture | null | undefined; + lightMapIntensity?: number | undefined; + aoMap?: Texture | null | undefined; + aoMapIntensity?: number | undefined; + emissive?: ColorRepresentation | undefined; + emissiveIntensity?: number | undefined; + emissiveMap?: Texture | null | undefined; + bumpMap?: Texture | null | undefined; + bumpScale?: number | undefined; + normalMap?: Texture | null | undefined; + normalMapType?: NormalMapTypes | undefined; + normalScale?: Vector2 | undefined; + displacementMap?: Texture | null | undefined; + displacementScale?: number | undefined; + displacementBias?: number | undefined; + alphaMap?: Texture | null | undefined; + wireframe?: boolean | undefined; + wireframeLinewidth?: number | undefined; + wireframeLinecap?: string | undefined; + wireframeLinejoin?: string | undefined; + fog?: boolean | undefined; +} + +export class MeshToonMaterial extends Material { + constructor(parameters?: MeshToonMaterialParameters); + + /** + * Read-only flag to check if a given object is of type {@link MeshToonMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isMeshToonMaterial: true; + + /** + * @default 'MeshToonMaterial' + */ + type: string; + + /** + * @default { 'TOON': '' } + */ + defines: { [key: string]: any }; + + /** + * @default new THREE.Color( 0xffffff ) + */ + color: Color; + + /** + * @default null + */ + gradientMap: Texture | null; + + /** + * @default null + */ + map: Texture | null; + + /** + * @default null + */ + lightMap: Texture | null; + + /** + * @default 1 + */ + lightMapIntensity: number; + + /** + * @default null + */ + aoMap: Texture | null; + + /** + * @default 1 + */ + aoMapIntensity: number; + + /** + * @default new THREE.Color( 0x000000 ) + */ + emissive: Color; + + /** + * @default 1 + */ + emissiveIntensity: number; + + /** + * @default null + */ + emissiveMap: Texture | null; + + /** + * @default null + */ + bumpMap: Texture | null; + + /** + * @default 1 + */ + bumpScale: number; + + /** + * @default null + */ + normalMap: Texture | null; + + /** + * @default THREE.TangentSpaceNormalMap + */ + normalMapType: NormalMapTypes; + + /** + * @default new THREE.Vector2( 1, 1 ) + */ + normalScale: Vector2; + + /** + * @default null + */ + displacementMap: Texture | null; + + /** + * @default 1 + */ + displacementScale: number; + + /** + * @default 0 + */ + displacementBias: number; + + /** + * @default null + */ + alphaMap: Texture | null; + + /** + * @default false + */ + wireframe: boolean; + + /** + * @default 1 + */ + wireframeLinewidth: number; + + /** + * @default 'round' + */ + wireframeLinecap: string; + + /** + * @default 'round' + */ + wireframeLinejoin: string; + + /** + * Whether the material is affected by fog. Default is true. + * @default fog + */ + fog: boolean; + + setValues(parameters: MeshToonMaterialParameters): void; +} diff --git a/src-testing/src/materials/PointsMaterial.d.ts b/src-testing/src/materials/PointsMaterial.d.ts new file mode 100644 index 000000000..58e32868c --- /dev/null +++ b/src-testing/src/materials/PointsMaterial.d.ts @@ -0,0 +1,61 @@ +import { Color, ColorRepresentation } from "../math/Color.js"; +import { Texture } from "../textures/Texture.js"; +import { Material, MaterialParameters } from "./Material.js"; + +export interface PointsMaterialParameters extends MaterialParameters { + color?: ColorRepresentation | undefined; + map?: Texture | null | undefined; + alphaMap?: Texture | null | undefined; + size?: number | undefined; + sizeAttenuation?: boolean | undefined; + fog?: boolean | undefined; +} + +export class PointsMaterial extends Material { + constructor(parameters?: PointsMaterialParameters); + + /** + * Read-only flag to check if a given object is of type {@link PointsMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isPointsMaterial: true; + + /** + * @default 'PointsMaterial' + */ + type: string; + + /** + * @default new THREE.Color( 0xffffff ) + */ + color: Color; + + /** + * @default null + */ + map: Texture | null; + + /** + * @default null + */ + alphaMap: Texture | null; + + /** + * @default 1 + */ + size: number; + + /** + * @default true + */ + sizeAttenuation: boolean; + + /** + * Whether the material is affected by fog. Default is true. + * @default fog + */ + fog: boolean; + + setValues(parameters: PointsMaterialParameters): void; +} diff --git a/src-testing/src/materials/RawShaderMaterial.d.ts b/src-testing/src/materials/RawShaderMaterial.d.ts new file mode 100644 index 000000000..f033fc6b0 --- /dev/null +++ b/src-testing/src/materials/RawShaderMaterial.d.ts @@ -0,0 +1,14 @@ +import { ShaderMaterial, ShaderMaterialParameters } from "./ShaderMaterial.js"; + +export class RawShaderMaterial extends ShaderMaterial { + constructor(parameters?: ShaderMaterialParameters); + + /** + * Read-only flag to check if a given object is of type {@link RawShaderMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isRawShaderMaterial: true; + + override readonly type: "RawShaderMaterial"; +} diff --git a/src-testing/src/materials/ShaderMaterial.d.ts b/src-testing/src/materials/ShaderMaterial.d.ts new file mode 100644 index 000000000..671b3e2c3 --- /dev/null +++ b/src-testing/src/materials/ShaderMaterial.d.ts @@ -0,0 +1,167 @@ +import { GLSLVersion } from "../constants.js"; +import { JSONMeta } from "../core/Object3D.js"; +import { UniformsGroup } from "../core/UniformsGroup.js"; +import { Matrix3, Matrix3Tuple } from "../math/Matrix3.js"; +import { Matrix4, Matrix4Tuple } from "../math/Matrix4.js"; +import { Vector2Tuple } from "../math/Vector2.js"; +import { Vector3Tuple } from "../math/Vector3.js"; +import { Vector4Tuple } from "../math/Vector4.js"; +import { IUniform } from "../renderers/shaders/UniformsLib.js"; +import { Material, MaterialJSON, MaterialParameters } from "./Material.js"; + +export interface ShaderMaterialParameters extends MaterialParameters { + uniforms?: { [uniform: string]: IUniform } | undefined; + uniformsGroups?: UniformsGroup[] | undefined; + vertexShader?: string | undefined; + fragmentShader?: string | undefined; + linewidth?: number | undefined; + wireframe?: boolean | undefined; + wireframeLinewidth?: number | undefined; + lights?: boolean | undefined; + clipping?: boolean | undefined; + fog?: boolean | undefined; + extensions?: + | { + clipCullDistance?: boolean | undefined; + multiDraw?: boolean | undefined; + } + | undefined; + glslVersion?: GLSLVersion | undefined; +} + +export type ShaderMaterialUniformJSON = { + type: "t"; + value: string; +} | { + type: "c"; + value: number; +} | { + type: "v2"; + value: Vector2Tuple; +} | { + type: "v3"; + value: Vector3Tuple; +} | { + type: "v4"; + value: Vector4Tuple; +} | { + type: "m3"; + value: Matrix3Tuple; +} | { + type: "m4"; + value: Matrix4Tuple; +} | { + value: unknown; +}; + +export interface ShaderMaterialJSON extends MaterialJSON { + glslVersion: number | null; + uniforms: Record; + + defines?: Record; + + vertexShader: string; + ragmentShader: string; + + lights: boolean; + clipping: boolean; + + extensions?: Record; +} + +export class ShaderMaterial extends Material { + constructor(parameters?: ShaderMaterialParameters); + + /** + * Read-only flag to check if a given object is of type {@link ShaderMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isShaderMaterial: true; + + /** + * @default 'ShaderMaterial' + */ + type: string; + + /** + * @default {} + */ + defines: { [key: string]: any }; + + /** + * @default {} + */ + uniforms: { [uniform: string]: IUniform }; + + uniformsGroups: UniformsGroup[]; + + vertexShader: string; + + fragmentShader: string; + + /** + * @default 1 + */ + linewidth: number; + + /** + * @default false + */ + wireframe: boolean; + + /** + * @default 1 + */ + wireframeLinewidth: number; + + /** + * @default false + */ + fog: boolean; + + /** + * @default false + */ + lights: boolean; + + /** + * @default false + */ + clipping: boolean; + + /** + * @default { + * clipCullDistance: false, + * multiDraw: false + * } + */ + extensions: { + clipCullDistance: boolean; + multiDraw: boolean; + }; + + /** + * @default { 'color': [ 1, 1, 1 ], 'uv': [ 0, 0 ], 'uv1': [ 0, 0 ] } + */ + defaultAttributeValues: any; + + /** + * @default undefined + */ + index0AttributeName: string | undefined; + + /** + * @default false + */ + uniformsNeedUpdate: boolean; + + /** + * @default null + */ + glslVersion: GLSLVersion | null; + + setValues(parameters: ShaderMaterialParameters): void; + + toJSON(meta?: JSONMeta): ShaderMaterialJSON; +} diff --git a/src-testing/src/materials/ShadowMaterial.d.ts b/src-testing/src/materials/ShadowMaterial.d.ts new file mode 100644 index 000000000..46c8741d8 --- /dev/null +++ b/src-testing/src/materials/ShadowMaterial.d.ts @@ -0,0 +1,39 @@ +import { Color, ColorRepresentation } from "../math/Color.js"; +import { Material, MaterialParameters } from "./Material.js"; + +export interface ShadowMaterialParameters extends MaterialParameters { + color?: ColorRepresentation | undefined; + fog?: boolean | undefined; +} + +export class ShadowMaterial extends Material { + constructor(parameters?: ShadowMaterialParameters); + + /** + * Read-only flag to check if a given object is of type {@link ShadowMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isShadowMaterial: true; + + /** + * @default 'ShadowMaterial' + */ + type: string; + + /** + * @default new THREE.Color( 0x000000 ) + */ + color: Color; + + /** + * @default true + */ + transparent: boolean; + + /** + * Whether the material is affected by fog. Default is true. + * @default fog + */ + fog: boolean; +} diff --git a/src-testing/src/materials/SpriteMaterial.d.ts b/src-testing/src/materials/SpriteMaterial.d.ts new file mode 100644 index 000000000..8dc4d9738 --- /dev/null +++ b/src-testing/src/materials/SpriteMaterial.d.ts @@ -0,0 +1,66 @@ +import { Color, ColorRepresentation } from "../math/Color.js"; +import { Texture } from "../textures/Texture.js"; +import { Material, MaterialParameters } from "./Material.js"; + +export interface SpriteMaterialParameters extends MaterialParameters { + color?: ColorRepresentation | undefined; + map?: Texture | null | undefined; + alphaMap?: Texture | null | undefined; + rotation?: number | undefined; + sizeAttenuation?: boolean | undefined; + fog?: boolean | undefined; +} + +export class SpriteMaterial extends Material { + constructor(parameters?: SpriteMaterialParameters); + /** + * Read-only flag to check if a given object is of type {@link SpriteMaterial}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isSpriteMaterial: true; + + /** + * @default 'SpriteMaterial' + */ + type: string; + + /** + * @default new THREE.Color( 0xffffff ) + */ + color: Color; + + /** + * @default null + */ + map: Texture | null; + + /** + * @default null + */ + alphaMap: Texture | null; + + /** + * @default 0 + */ + rotation: number; + + /** + * @default true + */ + sizeAttenuation: boolean; + + /** + * @default true + */ + transparent: boolean; + + /** + * Whether the material is affected by fog. Default is true. + * @default fog + */ + fog: boolean; + + setValues(parameters: SpriteMaterialParameters): void; + copy(source: SpriteMaterial): this; +} diff --git a/src-testing/src/math/Box2.d.ts b/src-testing/src/math/Box2.d.ts new file mode 100644 index 000000000..de05083d0 --- /dev/null +++ b/src-testing/src/math/Box2.d.ts @@ -0,0 +1,48 @@ +import { Vector2 } from "./Vector2.js"; + +// Math ////////////////////////////////////////////////////////////////////////////////// + +export class Box2 { + constructor(min?: Vector2, max?: Vector2); + + /** + * @default new THREE.Vector2( + Infinity, + Infinity ) + */ + min: Vector2; + + /** + * @default new THREE.Vector2( - Infinity, - Infinity ) + */ + max: Vector2; + + set(min: Vector2, max: Vector2): Box2; + setFromPoints(points: Vector2[]): Box2; + setFromCenterAndSize(center: Vector2, size: Vector2): Box2; + clone(): this; + copy(box: Box2): this; + makeEmpty(): Box2; + isEmpty(): boolean; + getCenter(target: Vector2): Vector2; + getSize(target: Vector2): Vector2; + expandByPoint(point: Vector2): Box2; + expandByVector(vector: Vector2): Box2; + expandByScalar(scalar: number): Box2; + containsPoint(point: Vector2): boolean; + containsBox(box: Box2): boolean; + getParameter(point: Vector2, target: Vector2): Vector2; + intersectsBox(box: Box2): boolean; + clampPoint(point: Vector2, target: Vector2): Vector2; + distanceToPoint(point: Vector2): number; + intersect(box: Box2): Box2; + union(box: Box2): Box2; + translate(offset: Vector2): Box2; + equals(box: Box2): boolean; + /** + * @deprecated Use {@link Box2#isEmpty .isEmpty()} instead. + */ + empty(): any; + /** + * @deprecated Use {@link Box2#intersectsBox .intersectsBox()} instead. + */ + isIntersectionBox(b: any): any; +} diff --git a/src-testing/src/math/Box3.d.ts b/src-testing/src/math/Box3.d.ts new file mode 100644 index 000000000..2e7b11bc9 --- /dev/null +++ b/src-testing/src/math/Box3.d.ts @@ -0,0 +1,66 @@ +import { BufferAttribute } from "../core/BufferAttribute.js"; +import { Object3D } from "../core/Object3D.js"; +import { Matrix4 } from "./Matrix4.js"; +import { Plane } from "./Plane.js"; +import { Sphere } from "./Sphere.js"; +import { Triangle } from "./Triangle.js"; +import { Vector3 } from "./Vector3.js"; + +export class Box3 { + constructor(min?: Vector3, max?: Vector3); + + /** + * @default new THREE.Vector3( + Infinity, + Infinity, + Infinity ) + */ + min: Vector3; + + /** + * @default new THREE.Vector3( - Infinity, - Infinity, - Infinity ) + */ + max: Vector3; + readonly isBox3: true; + + set(min: Vector3, max: Vector3): this; + setFromArray(array: ArrayLike): this; + setFromBufferAttribute(bufferAttribute: BufferAttribute): this; + setFromPoints(points: Vector3[]): this; + setFromCenterAndSize(center: Vector3, size: Vector3): this; + setFromObject(object: Object3D, precise?: boolean): this; + clone(): this; + copy(box: Box3): this; + makeEmpty(): this; + isEmpty(): boolean; + getCenter(target: Vector3): Vector3; + getSize(target: Vector3): Vector3; + expandByPoint(point: Vector3): this; + expandByVector(vector: Vector3): this; + expandByScalar(scalar: number): this; + expandByObject(object: Object3D, precise?: boolean): this; + containsPoint(point: Vector3): boolean; + containsBox(box: Box3): boolean; + getParameter(point: Vector3, target: Vector3): Vector3; + intersectsBox(box: Box3): boolean; + intersectsSphere(sphere: Sphere): boolean; + intersectsPlane(plane: Plane): boolean; + intersectsTriangle(triangle: Triangle): boolean; + clampPoint(point: Vector3, target: Vector3): Vector3; + distanceToPoint(point: Vector3): number; + getBoundingSphere(target: Sphere): Sphere; + intersect(box: Box3): this; + union(box: Box3): this; + applyMatrix4(matrix: Matrix4): this; + translate(offset: Vector3): this; + equals(box: Box3): boolean; + /** + * @deprecated Use {@link Box3#isEmpty .isEmpty()} instead. + */ + empty(): any; + /** + * @deprecated Use {@link Box3#intersectsBox .intersectsBox()} instead. + */ + isIntersectionBox(b: any): any; + /** + * @deprecated Use {@link Box3#intersectsSphere .intersectsSphere()} instead. + */ + isIntersectionSphere(s: any): any; +} diff --git a/src-testing/src/math/Color.d.ts b/src-testing/src/math/Color.d.ts new file mode 100644 index 000000000..487d4c57a --- /dev/null +++ b/src-testing/src/math/Color.d.ts @@ -0,0 +1,358 @@ +import { ColorSpace } from "../constants.js"; +import { Matrix3 } from "./Matrix3.js"; +import { Vector3 } from "./Vector3.js"; + +import { BufferAttribute } from "../core/BufferAttribute.js"; +import { InterleavedBufferAttribute } from "../core/InterleavedBufferAttribute.js"; + +export { SRGBToLinear } from "./ColorManagement.js"; + +declare const _colorKeywords: { + aliceblue: 0xf0f8ff; + antiquewhite: 0xfaebd7; + aqua: 0x00ffff; + aquamarine: 0x7fffd4; + azure: 0xf0ffff; + beige: 0xf5f5dc; + bisque: 0xffe4c4; + black: 0x000000; + blanchedalmond: 0xffebcd; + blue: 0x0000ff; + blueviolet: 0x8a2be2; + brown: 0xa52a2a; + burlywood: 0xdeb887; + cadetblue: 0x5f9ea0; + chartreuse: 0x7fff00; + chocolate: 0xd2691e; + coral: 0xff7f50; + cornflowerblue: 0x6495ed; + cornsilk: 0xfff8dc; + crimson: 0xdc143c; + cyan: 0x00ffff; + darkblue: 0x00008b; + darkcyan: 0x008b8b; + darkgoldenrod: 0xb8860b; + darkgray: 0xa9a9a9; + darkgreen: 0x006400; + darkgrey: 0xa9a9a9; + darkkhaki: 0xbdb76b; + darkmagenta: 0x8b008b; + darkolivegreen: 0x556b2f; + darkorange: 0xff8c00; + darkorchid: 0x9932cc; + darkred: 0x8b0000; + darksalmon: 0xe9967a; + darkseagreen: 0x8fbc8f; + darkslateblue: 0x483d8b; + darkslategray: 0x2f4f4f; + darkslategrey: 0x2f4f4f; + darkturquoise: 0x00ced1; + darkviolet: 0x9400d3; + deeppink: 0xff1493; + deepskyblue: 0x00bfff; + dimgray: 0x696969; + dimgrey: 0x696969; + dodgerblue: 0x1e90ff; + firebrick: 0xb22222; + floralwhite: 0xfffaf0; + forestgreen: 0x228b22; + fuchsia: 0xff00ff; + gainsboro: 0xdcdcdc; + ghostwhite: 0xf8f8ff; + gold: 0xffd700; + goldenrod: 0xdaa520; + gray: 0x808080; + green: 0x008000; + greenyellow: 0xadff2f; + grey: 0x808080; + honeydew: 0xf0fff0; + hotpink: 0xff69b4; + indianred: 0xcd5c5c; + indigo: 0x4b0082; + ivory: 0xfffff0; + khaki: 0xf0e68c; + lavender: 0xe6e6fa; + lavenderblush: 0xfff0f5; + lawngreen: 0x7cfc00; + lemonchiffon: 0xfffacd; + lightblue: 0xadd8e6; + lightcoral: 0xf08080; + lightcyan: 0xe0ffff; + lightgoldenrodyellow: 0xfafad2; + lightgray: 0xd3d3d3; + lightgreen: 0x90ee90; + lightgrey: 0xd3d3d3; + lightpink: 0xffb6c1; + lightsalmon: 0xffa07a; + lightseagreen: 0x20b2aa; + lightskyblue: 0x87cefa; + lightslategray: 0x778899; + lightslategrey: 0x778899; + lightsteelblue: 0xb0c4de; + lightyellow: 0xffffe0; + lime: 0x00ff00; + limegreen: 0x32cd32; + linen: 0xfaf0e6; + magenta: 0xff00ff; + maroon: 0x800000; + mediumaquamarine: 0x66cdaa; + mediumblue: 0x0000cd; + mediumorchid: 0xba55d3; + mediumpurple: 0x9370db; + mediumseagreen: 0x3cb371; + mediumslateblue: 0x7b68ee; + mediumspringgreen: 0x00fa9a; + mediumturquoise: 0x48d1cc; + mediumvioletred: 0xc71585; + midnightblue: 0x191970; + mintcream: 0xf5fffa; + mistyrose: 0xffe4e1; + moccasin: 0xffe4b5; + navajowhite: 0xffdead; + navy: 0x000080; + oldlace: 0xfdf5e6; + olive: 0x808000; + olivedrab: 0x6b8e23; + orange: 0xffa500; + orangered: 0xff4500; + orchid: 0xda70d6; + palegoldenrod: 0xeee8aa; + palegreen: 0x98fb98; + paleturquoise: 0xafeeee; + palevioletred: 0xdb7093; + papayawhip: 0xffefd5; + peachpuff: 0xffdab9; + peru: 0xcd853f; + pink: 0xffc0cb; + plum: 0xdda0dd; + powderblue: 0xb0e0e6; + purple: 0x800080; + rebeccapurple: 0x663399; + red: 0xff0000; + rosybrown: 0xbc8f8f; + royalblue: 0x4169e1; + saddlebrown: 0x8b4513; + salmon: 0xfa8072; + sandybrown: 0xf4a460; + seagreen: 0x2e8b57; + seashell: 0xfff5ee; + sienna: 0xa0522d; + silver: 0xc0c0c0; + skyblue: 0x87ceeb; + slateblue: 0x6a5acd; + slategray: 0x708090; + slategrey: 0x708090; + snow: 0xfffafa; + springgreen: 0x00ff7f; + steelblue: 0x4682b4; + tan: 0xd2b48c; + teal: 0x008080; + thistle: 0xd8bfd8; + tomato: 0xff6347; + turquoise: 0x40e0d0; + violet: 0xee82ee; + wheat: 0xf5deb3; + white: 0xffffff; + whitesmoke: 0xf5f5f5; + yellow: 0xffff00; + yellowgreen: 0x9acd32; +}; + +export type ColorRepresentation = Color | string | number; + +export interface HSL { + h: number; + s: number; + l: number; +} + +export interface RGB { + r: number; + g: number; + b: number; +} + +/** + * Represents a color. See also {@link ColorUtils}. + * + * see {@link https://github.com/mrdoob/three.js/blob/master/src/math/Color.js|src/math/Color.js} + * + * @example + * const color = new THREE.Color( 0xff0000 ); + */ +export class Color { + constructor(color?: ColorRepresentation); + constructor(r: number, g: number, b: number); + + readonly isColor: true; + + /** + * Red channel value between 0 and 1. Default is 1. + * @default 1 + */ + r: number; + + /** + * Green channel value between 0 and 1. Default is 1. + * @default 1 + */ + g: number; + + /** + * Blue channel value between 0 and 1. Default is 1. + * @default 1 + */ + b: number; + + set(...args: [color: ColorRepresentation] | [r: number, g: number, b: number]): this; + + /** + * Sets this color's {@link r}, {@link g} and {@link b} components from the x, y, and z components of the specified + * {@link Vector3 | vector}. + */ + setFromVector3(vector: Vector3): this; + + setScalar(scalar: number): Color; + setHex(hex: number, colorSpace?: ColorSpace): Color; + + /** + * Sets this color from RGB values. + * @param r Red channel value between 0 and 1. + * @param g Green channel value between 0 and 1. + * @param b Blue channel value between 0 and 1. + */ + setRGB(r: number, g: number, b: number, colorSpace?: ColorSpace): Color; + + /** + * Sets this color from HSL values. + * Based on MochiKit implementation by Bob Ippolito. + * + * @param h Hue channel value between 0 and 1. + * @param s Saturation value channel between 0 and 1. + * @param l Value channel value between 0 and 1. + */ + setHSL(h: number, s: number, l: number, colorSpace?: ColorSpace): Color; + + /** + * Sets this color from a CSS context style string. + * @param contextStyle Color in CSS context style format. + */ + setStyle(style: string, colorSpace?: ColorSpace): Color; + + /** + * Sets this color from a color name. + * Faster than {@link Color#setStyle .setStyle()} method if you don't need the other CSS-style formats. + * @param style Color name in X11 format. + */ + setColorName(style: string, colorSpace?: ColorSpace): Color; + + /** + * Clones this color. + */ + clone(): this; + + /** + * Copies given color. + * @param color Color to copy. + */ + copy(color: Color): this; + + /** + * Copies given color making conversion from sRGB to linear space. + * @param color Color to copy. + */ + copySRGBToLinear(color: Color): Color; + + /** + * Copies given color making conversion from linear to sRGB space. + * @param color Color to copy. + */ + copyLinearToSRGB(color: Color): Color; + + /** + * Converts this color from sRGB to linear space. + */ + convertSRGBToLinear(): Color; + + /** + * Converts this color from linear to sRGB space. + */ + convertLinearToSRGB(): Color; + + /** + * Returns the hexadecimal value of this color. + */ + getHex(colorSpace?: ColorSpace): number; + + /** + * Returns the string formated hexadecimal value of this color. + */ + getHexString(colorSpace?: ColorSpace): string; + + getHSL(target: HSL, colorSpace?: ColorSpace): HSL; + + getRGB(target: RGB, colorSpace?: ColorSpace): RGB; + + /** + * Returns the value of this color in CSS context style. + * Example: rgb(r, g, b) + */ + getStyle(colorSpace?: ColorSpace): string; + + offsetHSL(h: number, s: number, l: number): this; + + add(color: Color): this; + addColors(color1: Color, color2: Color): this; + addScalar(s: number): this; + + /** + * Applies the transform {@link Matrix3 | m} to this color's RGB components. + */ + applyMatrix3(m: Matrix3): this; + + sub(color: Color): this; + multiply(color: Color): this; + multiplyScalar(s: number): this; + lerp(color: Color, alpha: number): this; + lerpColors(color1: Color, color2: Color, alpha: number): this; + lerpHSL(color: Color, alpha: number): this; + equals(color: Color): boolean; + + /** + * Sets this color's red, green and blue value from the provided array or array-like. + * @param array the source array or array-like. + * @param offset (optional) offset into the array-like. Default is 0. + */ + fromArray(array: number[] | ArrayLike, offset?: number): this; + + /** + * Returns an array [red, green, blue], or copies red, green and blue into the provided array. + * @param array (optional) array to store the color to. If this is not provided, a new array will be created. + * @param offset (optional) optional offset into the array. + * @return The created or provided array. + */ + toArray(array?: number[], offset?: number): number[]; + + /** + * Copies red, green and blue into the provided array-like. + * @param array array-like to store the color to. + * @param offset (optional) optional offset into the array-like. + * @return The provided array-like. + */ + toArray(xyz: ArrayLike, offset?: number): ArrayLike; + + /** + * This method defines the serialization result of Color. + * @return The color as a hexadecimal value. + */ + toJSON(): number; + + fromBufferAttribute(attribute: BufferAttribute | InterleavedBufferAttribute, index: number): this; + + [Symbol.iterator](): Generator; + + /** + * List of X11 color names. + */ + static NAMES: typeof _colorKeywords; +} diff --git a/src-testing/src/math/ColorManagement.d.ts b/src-testing/src/math/ColorManagement.d.ts new file mode 100644 index 000000000..bbe952882 --- /dev/null +++ b/src-testing/src/math/ColorManagement.d.ts @@ -0,0 +1,49 @@ +import { + ColorSpace, + ColorSpacePrimaries, + ColorSpaceTransfer, + DisplayP3ColorSpace, + LinearDisplayP3ColorSpace, + LinearSRGBColorSpace, + SRGBColorSpace, +} from "../constants.js"; +import { Color } from "./Color.js"; +import { Vector3 } from "./Vector3.js"; + +export type WorkingColorSpace = typeof LinearSRGBColorSpace | typeof LinearDisplayP3ColorSpace; +export type DefinedColorSpace = + | typeof LinearSRGBColorSpace + | typeof SRGBColorSpace + | typeof LinearDisplayP3ColorSpace + | typeof DisplayP3ColorSpace; + +export interface ColorManagement { + /** + * @default false + */ + enabled: boolean; + + /** + * @default LinearSRGBColorSpace + */ + get workingColorSpace(): WorkingColorSpace; + set workingColorSpace(colorSpace: WorkingColorSpace); + + convert: (color: Color, sourceColorSpace: DefinedColorSpace, targetColorSpace: DefinedColorSpace) => Color; + + fromWorkingColorSpace: (color: Color, targetColorSpace: DefinedColorSpace) => Color; + + toWorkingColorSpace: (color: Color, sourceColorSpace: DefinedColorSpace) => Color; + + getPrimaries: (colorSpace: DefinedColorSpace) => ColorSpacePrimaries; + + getTransfer: (colorSpace: ColorSpace) => ColorSpaceTransfer; + + getLuminanceCoefficients: (target: Vector3, colorSpace?: ColorSpace) => Vector3; +} + +export const ColorManagement: ColorManagement; + +export function SRGBToLinear(c: number): number; + +export function LinearToSRGB(c: number): number; diff --git a/src-testing/src/math/Cylindrical.d.ts b/src-testing/src/math/Cylindrical.d.ts new file mode 100644 index 000000000..6764f8154 --- /dev/null +++ b/src-testing/src/math/Cylindrical.d.ts @@ -0,0 +1,26 @@ +import { Vector3 } from "./Vector3.js"; + +export class Cylindrical { + constructor(radius?: number, theta?: number, y?: number); + + /** + * @default 1 + */ + radius: number; + + /** + * @default 0 + */ + theta: number; + + /** + * @default 0 + */ + y: number; + + clone(): this; + copy(other: Cylindrical): this; + set(radius: number, theta: number, y: number): this; + setFromVector3(vec3: Vector3): this; + setFromCartesianCoords(x: number, y: number, z: number): this; +} diff --git a/src-testing/src/math/Euler.d.ts b/src-testing/src/math/Euler.d.ts new file mode 100644 index 000000000..81be13d82 --- /dev/null +++ b/src-testing/src/math/Euler.d.ts @@ -0,0 +1,50 @@ +import { Matrix4 } from "./Matrix4.js"; +import { Quaternion } from "./Quaternion.js"; +import { Vector3 } from "./Vector3.js"; + +export type EulerOrder = "XYZ" | "YXZ" | "ZXY" | "ZYX" | "YZX" | "XZY"; + +export type EulerTuple = [x: number, y: number, z: number, order?: EulerOrder]; + +export class Euler { + constructor(x?: number, y?: number, z?: number, order?: EulerOrder); + + /** + * @default 0 + */ + x: number; + + /** + * @default 0 + */ + y: number; + + /** + * @default 0 + */ + z: number; + + /** + * @default THREE.Euler.DEFAULT_ORDER + */ + order: EulerOrder; + readonly isEuler: true; + + _onChangeCallback: () => void; + + set(x: number, y: number, z: number, order?: EulerOrder): Euler; + clone(): this; + copy(euler: Euler): this; + setFromRotationMatrix(m: Matrix4, order?: EulerOrder, update?: boolean): Euler; + setFromQuaternion(q: Quaternion, order?: EulerOrder, update?: boolean): Euler; + setFromVector3(v: Vector3, order?: EulerOrder): Euler; + reorder(newOrder: EulerOrder): Euler; + equals(euler: Euler): boolean; + fromArray(array: EulerTuple): Euler; + toArray(array?: Partial, offset?: number): EulerTuple; + _onChange(callback: () => void): this; + + static DEFAULT_ORDER: "XYZ"; + + [Symbol.iterator](): Generator; +} diff --git a/src-testing/src/math/Frustum.d.ts b/src-testing/src/math/Frustum.d.ts new file mode 100644 index 000000000..364c8e926 --- /dev/null +++ b/src-testing/src/math/Frustum.d.ts @@ -0,0 +1,30 @@ +import { CoordinateSystem } from "../constants.js"; +import { Object3D } from "../core/Object3D.js"; +import { Sprite } from "../objects/Sprite.js"; +import { Box3 } from "./Box3.js"; +import { Matrix4 } from "./Matrix4.js"; +import { Plane } from "./Plane.js"; +import { Sphere } from "./Sphere.js"; +import { Vector3 } from "./Vector3.js"; + +/** + * Frustums are used to determine what is inside the camera's field of view. They help speed up the rendering process. + */ +export class Frustum { + constructor(p0?: Plane, p1?: Plane, p2?: Plane, p3?: Plane, p4?: Plane, p5?: Plane); + + /** + * Array of 6 vectors. + */ + planes: Plane[]; + + set(p0: Plane, p1: Plane, p2: Plane, p3: Plane, p4: Plane, p5: Plane): Frustum; + clone(): this; + copy(frustum: Frustum): this; + setFromProjectionMatrix(m: Matrix4, coordinateSystem?: CoordinateSystem): this; + intersectsObject(object: Object3D): boolean; + intersectsSprite(sprite: Sprite): boolean; + intersectsSphere(sphere: Sphere): boolean; + intersectsBox(box: Box3): boolean; + containsPoint(point: Vector3): boolean; +} diff --git a/src-testing/src/math/Interpolant.d.ts b/src-testing/src/math/Interpolant.d.ts new file mode 100644 index 000000000..9d2b1aced --- /dev/null +++ b/src-testing/src/math/Interpolant.d.ts @@ -0,0 +1,10 @@ +export abstract class Interpolant { + constructor(parameterPositions: any, sampleValues: any, sampleSize: number, resultBuffer?: any); + + parameterPositions: any; + sampleValues: any; + valueSize: number; + resultBuffer: any; + + evaluate(time: number): any; +} diff --git a/src-testing/src/math/Line3.d.ts b/src-testing/src/math/Line3.d.ts new file mode 100644 index 000000000..ab7e749bc --- /dev/null +++ b/src-testing/src/math/Line3.d.ts @@ -0,0 +1,29 @@ +import { Matrix4 } from "./Matrix4.js"; +import { Vector3 } from "./Vector3.js"; + +export class Line3 { + constructor(start?: Vector3, end?: Vector3); + + /** + * @default new THREE.Vector3() + */ + start: Vector3; + + /** + * @default new THREE.Vector3() + */ + end: Vector3; + + set(start?: Vector3, end?: Vector3): Line3; + clone(): this; + copy(line: Line3): this; + getCenter(target: Vector3): Vector3; + delta(target: Vector3): Vector3; + distanceSq(): number; + distance(): number; + at(t: number, target: Vector3): Vector3; + closestPointToPointParameter(point: Vector3, clampToLine?: boolean): number; + closestPointToPoint(point: Vector3, clampToLine: boolean, target: Vector3): Vector3; + applyMatrix4(matrix: Matrix4): Line3; + equals(line: Line3): boolean; +} diff --git a/src-testing/src/math/MathUtils.d.ts b/src-testing/src/math/MathUtils.d.ts new file mode 100644 index 000000000..48f10a3c3 --- /dev/null +++ b/src-testing/src/math/MathUtils.d.ts @@ -0,0 +1,137 @@ +import { Quaternion } from "./Quaternion.js"; + +/** + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/math/MathUtils.js|src/math/MathUtils.js} + */ + +export const DEG2RAD: number; + +export const RAD2DEG: number; + +export function generateUUID(): string; + +/** + * Clamps the x to be between a and b. + * + * @param value Value to be clamped. + * @param min Minimum value. + * @param max Maximum value. + */ +export function clamp(value: number, min: number, max: number): number; + +export function euclideanModulo(n: number, m: number): number; + +/** + * Linear mapping of x from range [a1, a2] to range [b1, b2]. + * + * @param x Value to be mapped. + * @param a1 Minimum value for range A. + * @param a2 Maximum value for range A. + * @param b1 Minimum value for range B. + * @param b2 Maximum value for range B. + */ +export function mapLinear(x: number, a1: number, a2: number, b1: number, b2: number): number; + +export function inverseLerp(x: number, y: number, t: number): number; + +/** + * Returns a value linearly interpolated from two known points based + * on the given interval - t = 0 will return x and t = 1 will return y. + * + * @param x Start point. + * @param y End point. + * @param t interpolation factor in the closed interval [0, 1] + */ +export function lerp(x: number, y: number, t: number): number; + +/** + * Smoothly interpolate a number from x toward y in a spring-like + * manner using the dt to maintain frame rate independent movement. + * + * @param x Current point. + * @param y Target point. + * @param lambda A higher lambda value will make the movement more sudden, and a lower value will make the movement more gradual. + * @param dt Delta time in seconds. + */ +export function damp(x: number, y: number, lambda: number, dt: number): number; + +/** + * Returns a value that alternates between 0 and length. + * + * @param x The value to pingpong. + * @param length The positive value the export function will pingpong to. Default is 1. + */ +export function pingpong(x: number, length?: number): number; + +export function smoothstep(x: number, min: number, max: number): number; + +export function smootherstep(x: number, min: number, max: number): number; + +/** + * Random integer from low to high interval. + */ +export function randInt(low: number, high: number): number; + +/** + * Random float from low to high interval. + */ +export function randFloat(low: number, high: number): number; + +/** + * Random float from - range / 2 to range / 2 interval. + */ +export function randFloatSpread(range: number): number; + +/** + * Deterministic pseudo-random float in the interval [ 0, 1 ]. + */ +export function seededRandom(seed?: number): number; + +export function degToRad(degrees: number): number; + +export function radToDeg(radians: number): number; + +export function isPowerOfTwo(value: number): boolean; + +export function ceilPowerOfTwo(value: number): number; + +export function floorPowerOfTwo(value: number): number; + +export function setQuaternionFromProperEuler(q: Quaternion, a: number, b: number, c: number, order: string): void; + +export function denormalize( + value: number, + array: Float32Array | Uint32Array | Uint16Array | Uint8Array | Int32Array | Int16Array | Int8Array, +): number; + +export function normalize( + value: number, + array: Float32Array | Uint32Array | Uint16Array | Uint8Array | Int32Array | Int16Array | Int8Array, +): number; + +export const MathUtils: { + DEG2RAD: typeof DEG2RAD; + RAD2DEG: typeof RAD2DEG; + generateUUID: typeof generateUUID; + clamp: typeof clamp; + euclideanModulo: typeof euclideanModulo; + mapLinear: typeof mapLinear; + inverseLerp: typeof inverseLerp; + lerp: typeof lerp; + damp: typeof damp; + pingpong: typeof pingpong; + smoothstep: typeof smoothstep; + smootherstep: typeof smootherstep; + randInt: typeof randInt; + randFloat: typeof randFloat; + randFloatSpread: typeof randFloatSpread; + seededRandom: typeof seededRandom; + degToRad: typeof degToRad; + radToDeg: typeof radToDeg; + isPowerOfTwo: typeof isPowerOfTwo; + ceilPowerOfTwo: typeof ceilPowerOfTwo; + floorPowerOfTwo: typeof floorPowerOfTwo; + setQuaternionFromProperEuler: typeof setQuaternionFromProperEuler; + normalize: typeof normalize; + denormalize: typeof denormalize; +}; diff --git a/src-testing/src/math/Matrix2.d.ts b/src-testing/src/math/Matrix2.d.ts new file mode 100644 index 000000000..40ed4c8aa --- /dev/null +++ b/src-testing/src/math/Matrix2.d.ts @@ -0,0 +1,53 @@ +export type Matrix2Tuple = [ + n11: number, + n12: number, + n21: number, + n22: number, +]; + +/** + * A class representing a 2x2 {@link https://en.wikipedia.org/wiki/Matrix_(mathematics) matrix}. + * + * @example + * const m = new Matrix2(); + */ +export class Matrix2 { + readonly isMatrix2: true; + + /** + * A {@link https://en.wikipedia.org/wiki/Row-_and_column-major_order column-major} list of matrix values. + */ + elements: Matrix2Tuple; + + /** + * Creates a 2x2 {@link https://en.wikipedia.org/wiki/Identity_matrix identity matrix}. + */ + constructor(); + + /** + * Creates a 2x2 matrix with the given arguments in row-major order. + */ + constructor(n11: number, n12: number, n21: number, n22: number); + + /** + * Resets this matrix to the 2x2 identity matrix: + */ + identity(): this; + + /** + * Sets the elements of this matrix based on an array in + * {@link https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major} format. + * + * @param array the array to read the elements from + * @param offset (optional) index of first element in the array. Default is `0`. + */ + fromArray(array: ArrayLike, offset?: number): this; + + /** + * Sets the 2x2 matrix values to the given + * {@link https://en.wikipedia.org/wiki/Row-_and_column-major_order row-major} sequence of values: + * [n11, n12, + * n21, n22] + */ + set(n11: number, n12: number, n21: number, n22: number): this; +} diff --git a/src-testing/src/math/Matrix3.d.ts b/src-testing/src/math/Matrix3.d.ts new file mode 100644 index 000000000..0b593fcb8 --- /dev/null +++ b/src-testing/src/math/Matrix3.d.ts @@ -0,0 +1,184 @@ +// https://threejs.org/docs/#api/en/math/Matrix3 + +import { Matrix4 } from "./Matrix4.js"; +import { Vector2 } from "./Vector2.js"; +import { Vector3 } from "./Vector3.js"; + +export type Matrix3Tuple = [ + n11: number, + n12: number, + n13: number, + n21: number, + n22: number, + n23: number, + n31: number, + n32: number, + n33: number, +]; + +export class Matrix3 { + readonly isMatrix3: true; + + /** + * Array with matrix values. + * @default [1, 0, 0, 0, 1, 0, 0, 0, 1] + */ + elements: Matrix3Tuple; + + /** + * Creates an identity matrix. + */ + constructor(); + /** + * Creates a 3x3 matrix with the given arguments in row-major order. + */ + constructor( + n11: number, + n12: number, + n13: number, + n21: number, + n22: number, + n23: number, + n31: number, + n32: number, + n33: number, + ); + + set( + n11: number, + n12: number, + n13: number, + n21: number, + n22: number, + n23: number, + n31: number, + n32: number, + n33: number, + ): Matrix3; + + identity(): this; + + copy(m: Matrix3): this; + + extractBasis(xAxis: Vector3, yAxis: Vector3, zAxis: Vector3): this; + + setFromMatrix4(m: Matrix4): Matrix3; + + /** + * Multiplies this matrix by m. + */ + multiply(m: Matrix3): this; + + premultiply(m: Matrix3): this; + + /** + * Sets this matrix to a x b. + */ + multiplyMatrices(a: Matrix3, b: Matrix3): this; + + multiplyScalar(s: number): this; + + determinant(): number; + + /** + * Inverts this matrix in place. + */ + invert(): this; + + /** + * Transposes this matrix in place. + */ + transpose(): this; + + getNormalMatrix(matrix4: Matrix4): this; + + /** + * Transposes this matrix into the supplied array r, and returns itself. + */ + transposeIntoArray(r: number[]): this; + + setUvTransform(tx: number, ty: number, sx: number, sy: number, rotation: number, cx: number, cy: number): this; + + scale(sx: number, sy: number): this; + + rotate(theta: number): this; + + translate(tx: number, ty: number): this; + + /** + * Sets this matrix as a 2D translation transform: + * + * ``` + * 1, 0, x, + * 0, 1, y, + * 0, 0, 1 + * ``` + * + * @param v the amount to translate. + */ + makeTranslation(v: Vector2): this; + /** + * Sets this matrix as a 2D translation transform: + * + * ``` + * 1, 0, x, + * 0, 1, y, + * 0, 0, 1 + * ``` + * + * @param x the amount to translate in the X axis. + * @param y the amount to translate in the Y axis. + */ + makeTranslation(x: number, y: number): this; + + /** + * Sets this matrix as a 2D rotational transformation by theta radians. The resulting matrix will be: + * + * ``` + * cos(θ) -sin(θ) 0 + * sin(θ) cos(θ) 0 + * 0 0 1 + * ``` + * + * @param theta Rotation angle in radians. Positive values rotate counterclockwise. + */ + makeRotation(theta: number): this; + + /** + * Sets this matrix as a 2D scale transform: + * + * ``` + * x, 0, 0, + * 0, y, 0, + * 0, 0, 1 + * ``` + * + * @param x the amount to scale in the X axis. + * @param y the amount to scale in the Y axis. + */ + makeScale(x: number, y: number): this; + + equals(matrix: Matrix3): boolean; + + /** + * Sets the values of this matrix from the provided array or array-like. + * @param array the source array or array-like. + * @param offset (optional) offset into the array-like. Default is 0. + */ + fromArray(array: ArrayLike, offset?: number): this; + + /** + * Writes the elements of this matrix to an array in + * {@link https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major} format. + */ + toArray(): Matrix3Tuple; + /** + * Writes the elements of this matrix to an array in + * {@link https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major} format. + * @param array array to store the resulting vector in. If not given a new array will be created. + * @param offset (optional) offset in the array at which to put the result. + */ + toArray>(array: TArray, offset?: number): TArray; + + clone(): this; +} diff --git a/src-testing/src/math/Matrix4.d.ts b/src-testing/src/math/Matrix4.d.ts new file mode 100644 index 000000000..9ef2a9ceb --- /dev/null +++ b/src-testing/src/math/Matrix4.d.ts @@ -0,0 +1,284 @@ +import { CoordinateSystem } from "../constants.js"; +import { Euler } from "./Euler.js"; +import { Matrix3 } from "./Matrix3.js"; +import { Quaternion } from "./Quaternion.js"; +import { Vector3 } from "./Vector3.js"; + +export type Matrix4Tuple = [ + n11: number, + n12: number, + n13: number, + n14: number, + n21: number, + n22: number, + n23: number, + n24: number, + n31: number, + n32: number, + n33: number, + n34: number, + n41: number, + n42: number, + n43: number, + n44: number, +]; + +/** + * A 4x4 Matrix. + * + * @example + * // Simple rig for rotating around 3 axes + * const m = new THREE.Matrix4(); + * const m1 = new THREE.Matrix4(); + * const m2 = new THREE.Matrix4(); + * const m3 = new THREE.Matrix4(); + * const alpha = 0; + * const beta = Math.PI; + * const gamma = Math.PI/2; + * m1.makeRotationX( alpha ); + * m2.makeRotationY( beta ); + * m3.makeRotationZ( gamma ); + * m.multiplyMatrices( m1, m2 ); + * m.multiply( m3 ); + */ +export class Matrix4 { + readonly isMatrix4: true; + + /** + * Array with matrix values. + * @default [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] + */ + elements: Matrix4Tuple; + + /** + * Creates an identity matrix. + */ + constructor(); + /** + * Creates a 4x4 matrix with the given arguments in row-major order. + */ + constructor( + n11: number, + n12: number, + n13: number, + n14: number, + n21: number, + n22: number, + n23: number, + n24: number, + n31: number, + n32: number, + n33: number, + n34: number, + n41: number, + n42: number, + n43: number, + n44: number, + ); + + /** + * Sets all fields of this matrix. + */ + set( + n11: number, + n12: number, + n13: number, + n14: number, + n21: number, + n22: number, + n23: number, + n24: number, + n31: number, + n32: number, + n33: number, + n34: number, + n41: number, + n42: number, + n43: number, + n44: number, + ): this; + + /** + * Resets this matrix to identity. + */ + identity(): this; + + clone(): Matrix4; + + copy(m: Matrix4): this; + + copyPosition(m: Matrix4): this; + + /** + * Set the upper 3x3 elements of this matrix to the values of the Matrix3 m. + */ + setFromMatrix3(m: Matrix3): this; + + extractBasis(xAxis: Vector3, yAxis: Vector3, zAxis: Vector3): this; + + makeBasis(xAxis: Vector3, yAxis: Vector3, zAxis: Vector3): this; + + /** + * Copies the rotation component of the supplied matrix m into this matrix rotation component. + */ + extractRotation(m: Matrix4): this; + + makeRotationFromEuler(euler: Euler): this; + + makeRotationFromQuaternion(q: Quaternion): this; + + /** + * Constructs a rotation matrix, looking from eye towards center with defined up vector. + */ + lookAt(eye: Vector3, target: Vector3, up: Vector3): this; + + /** + * Multiplies this matrix by m. + */ + multiply(m: Matrix4): this; + + premultiply(m: Matrix4): this; + + /** + * Sets this matrix to a x b. + */ + multiplyMatrices(a: Matrix4, b: Matrix4): this; + + /** + * Multiplies this matrix by s. + */ + multiplyScalar(s: number): this; + + /** + * Computes determinant of this matrix. + * Based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm + */ + determinant(): number; + + /** + * Transposes this matrix. + */ + transpose(): this; + + /** + * Sets the position component for this matrix from vector v. + */ + setPosition(v: Vector3): this; + setPosition(x: number, y: number, z: number): this; + + /** + * Inverts this matrix. + */ + invert(): this; + + /** + * Multiplies the columns of this matrix by vector v. + */ + scale(v: Vector3): this; + + getMaxScaleOnAxis(): number; + + /** + * Sets this matrix as translation transform. + */ + makeTranslation(v: Vector3): this; + makeTranslation(x: number, y: number, z: number): this; + + /** + * Sets this matrix as rotation transform around x axis by theta radians. + * + * @param theta Rotation angle in radians. + */ + makeRotationX(theta: number): this; + + /** + * Sets this matrix as rotation transform around y axis by theta radians. + * + * @param theta Rotation angle in radians. + */ + makeRotationY(theta: number): this; + + /** + * Sets this matrix as rotation transform around z axis by theta radians. + * + * @param theta Rotation angle in radians. + */ + makeRotationZ(theta: number): this; + + /** + * Sets this matrix as rotation transform around axis by angle radians. + * Based on http://www.gamedev.net/reference/articles/article1199.asp. + * + * @param axis Rotation axis. + * @param angle Rotation angle in radians. + */ + makeRotationAxis(axis: Vector3, angle: number): this; + + /** + * Sets this matrix as scale transform. + */ + makeScale(x: number, y: number, z: number): this; + + /** + * Sets this matrix as shear transform. + */ + makeShear(xy: number, xz: number, yx: number, yz: number, zx: number, zy: number): this; + + /** + * Sets this matrix to the transformation composed of translation, rotation and scale. + */ + compose(position: Vector3, quaternion: Quaternion, scale: Vector3): this; + + /** + * Decomposes this matrix into it's position, quaternion and scale components. + */ + decompose(position: Vector3, quaternion: Quaternion, scale: Vector3): this; + + /** + * Creates a perspective projection matrix. + */ + makePerspective( + left: number, + right: number, + top: number, + bottom: number, + near: number, + far: number, + coordinateSystem?: CoordinateSystem, + ): this; + + /** + * Creates an orthographic projection matrix. + */ + makeOrthographic( + left: number, + right: number, + top: number, + bottom: number, + near: number, + far: number, + coordinateSystem?: CoordinateSystem, + ): this; + + equals(matrix: Matrix4): boolean; + + /** + * Sets the values of this matrix from the provided array or array-like. + * @param array the source array or array-like. + * @param offset (optional) offset into the array-like. Default is 0. + */ + fromArray(array: ArrayLike, offset?: number): this; + + /** + * Writes the elements of this matrix to an array in + * {@link https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major} format. + */ + toArray(): Matrix4Tuple; + /** + * Writes the elements of this matrix to an array in + * {@link https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major} format. + * @param array array to store the resulting vector in. + * @param offset (optional) offset in the array at which to put the result. + */ + toArray>(array: TArray, offset?: number): TArray; +} diff --git a/src-testing/src/math/Plane.d.ts b/src-testing/src/math/Plane.d.ts new file mode 100644 index 000000000..59fa23912 --- /dev/null +++ b/src-testing/src/math/Plane.d.ts @@ -0,0 +1,47 @@ +import { Box3 } from "./Box3.js"; +import { Line3 } from "./Line3.js"; +import { Matrix3 } from "./Matrix3.js"; +import { Matrix4 } from "./Matrix4.js"; +import { Sphere } from "./Sphere.js"; +import { Vector3 } from "./Vector3.js"; + +export class Plane { + constructor(normal?: Vector3, constant?: number); + + /** + * @default new THREE.Vector3( 1, 0, 0 ) + */ + normal: Vector3; + + /** + * @default 0 + */ + constant: number; + + readonly isPlane: true; + + set(normal: Vector3, constant: number): Plane; + setComponents(x: number, y: number, z: number, w: number): Plane; + setFromNormalAndCoplanarPoint(normal: Vector3, point: Vector3): Plane; + setFromCoplanarPoints(a: Vector3, b: Vector3, c: Vector3): Plane; + clone(): this; + copy(plane: Plane): this; + normalize(): Plane; + negate(): Plane; + distanceToPoint(point: Vector3): number; + distanceToSphere(sphere: Sphere): number; + projectPoint(point: Vector3, target: Vector3): Vector3; + intersectLine(line: Line3, target: Vector3): Vector3 | null; + intersectsLine(line: Line3): boolean; + intersectsBox(box: Box3): boolean; + intersectsSphere(sphere: Sphere): boolean; + coplanarPoint(target: Vector3): Vector3; + applyMatrix4(matrix: Matrix4, optionalNormalMatrix?: Matrix3): Plane; + translate(offset: Vector3): Plane; + equals(plane: Plane): boolean; + + /** + * @deprecated Use {@link Plane#intersectsLine .intersectsLine()} instead. + */ + isIntersectionLine(l: any): any; +} diff --git a/src-testing/src/math/Quaternion.d.ts b/src-testing/src/math/Quaternion.d.ts new file mode 100644 index 000000000..c8adc8f11 --- /dev/null +++ b/src-testing/src/math/Quaternion.d.ts @@ -0,0 +1,188 @@ +import { BufferAttribute } from "../core/BufferAttribute.js"; +import { InterleavedBufferAttribute } from "../core/InterleavedBufferAttribute.js"; +import { Euler } from "./Euler.js"; +import { Matrix4 } from "./Matrix4.js"; +import { Vector3, Vector3Like } from "./Vector3.js"; + +export interface QuaternionLike { + readonly x: number; + readonly y: number; + readonly z: number; + readonly w: number; +} + +export type QuaternionTuple = [x: number, y: number, z: number, w: number]; + +/** + * Implementation of a quaternion. This is used for rotating things without incurring in the dreaded gimbal lock issue, amongst other advantages. + * + * @example + * const quaternion = new THREE.Quaternion(); + * quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 ); + * const vector = new THREE.Vector3( 1, 0, 0 ); + * vector.applyQuaternion( quaternion ); + */ +export class Quaternion { + /** + * @param x x coordinate + * @param y y coordinate + * @param z z coordinate + * @param w w coordinate + */ + constructor(x?: number, y?: number, z?: number, w?: number); + + /** + * @default 0 + */ + x: number; + + /** + * @default 0 + */ + y: number; + + /** + * @default 0 + */ + z: number; + + /** + * @default 1 + */ + w: number; + readonly isQuaternion: true; + + /** + * Sets values of this quaternion. + */ + set(x: number, y: number, z: number, w: number): this; + + /** + * Clones this quaternion. + */ + clone(): this; + + /** + * Copies values of q to this quaternion. + */ + copy(q: QuaternionLike): this; + + /** + * Sets this quaternion from rotation specified by Euler angles. + */ + setFromEuler(euler: Euler, update?: boolean): this; + + /** + * Sets this quaternion from rotation specified by axis and angle. + * Adapted from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm. + * Axis have to be normalized, angle is in radians. + */ + setFromAxisAngle(axis: Vector3Like, angle: number): this; + + /** + * Sets this quaternion from rotation component of m. Adapted from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm. + */ + setFromRotationMatrix(m: Matrix4): this; + setFromUnitVectors(vFrom: Vector3, vTo: Vector3Like): this; + angleTo(q: Quaternion): number; + rotateTowards(q: Quaternion, step: number): this; + + identity(): this; + + /** + * Inverts this quaternion. + */ + invert(): this; + + conjugate(): this; + dot(v: Quaternion): number; + lengthSq(): number; + + /** + * Computes length of this quaternion. + */ + length(): number; + + /** + * Normalizes this quaternion. + */ + normalize(): this; + + /** + * Multiplies this quaternion by b. + */ + multiply(q: Quaternion): this; + premultiply(q: Quaternion): this; + + /** + * Sets this quaternion to a x b + * Adapted from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm. + */ + multiplyQuaternions(a: Quaternion, b: Quaternion): this; + + slerp(qb: Quaternion, t: number): this; + slerpQuaternions(qa: Quaternion, qb: Quaternion, t: number): this; + equals(v: Quaternion): boolean; + + /** + * Sets this quaternion's x, y, z and w value from the provided array or array-like. + * @param array the source array or array-like. + * @param offset (optional) offset into the array. Default is 0. + */ + fromArray(array: QuaternionTuple, offset?: number): this; + + /** + * Returns an array [x, y, z, w], or copies x, y, z and w into the provided array. + * @param array (optional) array to store the quaternion to. If this is not provided, a new array will be created. + * @param offset (optional) optional offset into the array. + * @return The created or provided array. + */ + toArray(array?: number[], offset?: number): QuaternionTuple; + + /** + * Copies x, y, z and w into the provided array-like. + * @param array array-like to store the quaternion to. + * @param offset (optional) optional offset into the array. + * @return The provided array-like. + */ + toArray(array: ArrayLike, offset?: number): QuaternionTuple; + + /** + * This method defines the serialization result of Quaternion. + * @return The numerical elements of this quaternion in an array of format [x, y, z, w]. + */ + toJSON(): [number, number, number, number]; + + /** + * Sets x, y, z, w properties of this quaternion from the attribute. + * @param attribute the source attribute. + * @param index index in the attribute. + */ + fromBufferAttribute(attribute: BufferAttribute | InterleavedBufferAttribute, index: number): this; + + _onChange(callback: () => void): this; + _onChangeCallback: () => void; + + static slerpFlat( + dst: number[], + dstOffset: number, + src0: number[], + srcOffset: number, + src1: number[], + stcOffset1: number, + t: number, + ): void; + + static multiplyQuaternionsFlat( + dst: number[], + dstOffset: number, + src0: number[], + srcOffset: number, + src1: number[], + stcOffset1: number, + ): number[]; + + random(): this; + + [Symbol.iterator](): Generator; +} diff --git a/src-testing/src/math/Ray.d.ts b/src-testing/src/math/Ray.d.ts new file mode 100644 index 000000000..98b9fd70f --- /dev/null +++ b/src-testing/src/math/Ray.d.ts @@ -0,0 +1,60 @@ +import { Box3 } from "./Box3.js"; +import { Matrix4 } from "./Matrix4.js"; +import { Plane } from "./Plane.js"; +import { Sphere } from "./Sphere.js"; +import { Vector3 } from "./Vector3.js"; + +export class Ray { + constructor(origin?: Vector3, direction?: Vector3); + + /** + * @default new THREE.Vector3() + */ + origin: Vector3; + + /** + * @default new THREE.Vector3( 0, 0, - 1 ) + */ + direction: Vector3; + + set(origin: Vector3, direction: Vector3): Ray; + clone(): this; + copy(ray: Ray): this; + at(t: number, target: Vector3): Vector3; + lookAt(v: Vector3): Ray; + recast(t: number): Ray; + closestPointToPoint(point: Vector3, target: Vector3): Vector3; + distanceToPoint(point: Vector3): number; + distanceSqToPoint(point: Vector3): number; + distanceSqToSegment( + v0: Vector3, + v1: Vector3, + optionalPointOnRay?: Vector3, + optionalPointOnSegment?: Vector3, + ): number; + intersectSphere(sphere: Sphere, target: Vector3): Vector3 | null; + intersectsSphere(sphere: Sphere): boolean; + distanceToPlane(plane: Plane): number; + intersectPlane(plane: Plane, target: Vector3): Vector3 | null; + intersectsPlane(plane: Plane): boolean; + intersectBox(box: Box3, target: Vector3): Vector3 | null; + intersectsBox(box: Box3): boolean; + intersectTriangle(a: Vector3, b: Vector3, c: Vector3, backfaceCulling: boolean, target: Vector3): Vector3 | null; + applyMatrix4(matrix4: Matrix4): Ray; + equals(ray: Ray): boolean; + + /** + * @deprecated Use {@link Ray#intersectsBox .intersectsBox()} instead. + */ + isIntersectionBox(b: any): any; + + /** + * @deprecated Use {@link Ray#intersectsPlane .intersectsPlane()} instead. + */ + isIntersectionPlane(p: any): any; + + /** + * @deprecated Use {@link Ray#intersectsSphere .intersectsSphere()} instead. + */ + isIntersectionSphere(s: any): any; +} diff --git a/src-testing/src/math/Sphere.d.ts b/src-testing/src/math/Sphere.d.ts new file mode 100644 index 000000000..a423a6f97 --- /dev/null +++ b/src-testing/src/math/Sphere.d.ts @@ -0,0 +1,47 @@ +import { Box3 } from "./Box3.js"; +import { Matrix4 } from "./Matrix4.js"; +import { Plane } from "./Plane.js"; +import { Vector3 } from "./Vector3.js"; + +export class Sphere { + constructor(center?: Vector3, radius?: number); + + /** + * Read-only flag to check if a given object is of type {@link Sphere}. + */ + readonly isSphere: true; + + /** + * @default new Vector3() + */ + center: Vector3; + + /** + * @default 1 + */ + radius: number; + + set(center: Vector3, radius: number): Sphere; + setFromPoints(points: Vector3[], optionalCenter?: Vector3): Sphere; + clone(): this; + copy(sphere: Sphere): this; + expandByPoint(point: Vector3): this; + isEmpty(): boolean; + makeEmpty(): this; + containsPoint(point: Vector3): boolean; + distanceToPoint(point: Vector3): number; + intersectsSphere(sphere: Sphere): boolean; + intersectsBox(box: Box3): boolean; + intersectsPlane(plane: Plane): boolean; + clampPoint(point: Vector3, target: Vector3): Vector3; + getBoundingBox(target: Box3): Box3; + applyMatrix4(matrix: Matrix4): Sphere; + translate(offset: Vector3): Sphere; + equals(sphere: Sphere): boolean; + union(sphere: Sphere): this; + + /** + * @deprecated Use {@link Sphere#isEmpty .isEmpty()} instead. + */ + empty(): any; +} diff --git a/src-testing/src/math/Spherical.d.ts b/src-testing/src/math/Spherical.d.ts new file mode 100644 index 000000000..8d4815a17 --- /dev/null +++ b/src-testing/src/math/Spherical.d.ts @@ -0,0 +1,27 @@ +import { Vector3 } from "./Vector3.js"; + +export class Spherical { + constructor(radius?: number, phi?: number, theta?: number); + + /** + * @default 1 + */ + radius: number; + + /** + * @default 0 + */ + phi: number; + + /** + * @default 0 + */ + theta: number; + + set(radius: number, phi: number, theta: number): this; + clone(): this; + copy(other: Spherical): this; + makeSafe(): this; + setFromVector3(v: Vector3): this; + setFromCartesianCoords(x: number, y: number, z: number): this; +} diff --git a/src-testing/src/math/SphericalHarmonics3.d.ts b/src-testing/src/math/SphericalHarmonics3.d.ts new file mode 100644 index 000000000..5981a0b48 --- /dev/null +++ b/src-testing/src/math/SphericalHarmonics3.d.ts @@ -0,0 +1,50 @@ +import { Vector3 } from "./Vector3.js"; + +export class SphericalHarmonics3 { + constructor(); + + /** + * @default [new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), + * new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3()] + */ + coefficients: Vector3[]; + readonly isSphericalHarmonics3: true; + + set(coefficients: Vector3[]): SphericalHarmonics3; + zero(): SphericalHarmonics3; + add(sh: SphericalHarmonics3): SphericalHarmonics3; + addScaledSH(sh: SphericalHarmonics3, s: number): SphericalHarmonics3; + scale(s: number): SphericalHarmonics3; + lerp(sh: SphericalHarmonics3, alpha: number): SphericalHarmonics3; + equals(sh: SphericalHarmonics3): boolean; + copy(sh: SphericalHarmonics3): SphericalHarmonics3; + clone(): this; + + /** + * Sets the values of this spherical harmonics from the provided array or array-like. + * @param array the source array or array-like. + * @param offset (optional) offset into the array. Default is 0. + */ + fromArray(array: number[] | ArrayLike, offset?: number): this; + + /** + * Returns an array with the values of this spherical harmonics, or copies them into the provided array. + * @param array (optional) array to store the spherical harmonics to. If this is not provided, a new array will be created. + * @param offset (optional) optional offset into the array. + * @return The created or provided array. + */ + toArray(array?: number[], offset?: number): number[]; + + /** + * Returns an array with the values of this spherical harmonics, or copies them into the provided array-like. + * @param array array-like to store the spherical harmonics to. + * @param offset (optional) optional offset into the array-like. + * @return The provided array-like. + */ + toArray(array: ArrayLike, offset?: number): ArrayLike; + + getAt(normal: Vector3, target: Vector3): Vector3; + getIrradianceAt(normal: Vector3, target: Vector3): Vector3; + + static getBasisAt(normal: Vector3, shBasis: number[]): void; +} diff --git a/src-testing/src/math/Triangle.d.ts b/src-testing/src/math/Triangle.d.ts new file mode 100644 index 000000000..75f8d5676 --- /dev/null +++ b/src-testing/src/math/Triangle.d.ts @@ -0,0 +1,86 @@ +import { Box3 } from "./Box3.js"; +import { Plane } from "./Plane.js"; +import { Vector2 } from "./Vector2.js"; +import { Vector3 } from "./Vector3.js"; +import { Vector4 } from "./Vector4.js"; + +import { BufferAttribute } from "../core/BufferAttribute.js"; +import { InterleavedBufferAttribute } from "../core/InterleavedBufferAttribute.js"; + +export class Triangle { + constructor(a?: Vector3, b?: Vector3, c?: Vector3); + + /** + * @default new THREE.Vector3() + */ + a: Vector3; + + /** + * @default new THREE.Vector3() + */ + b: Vector3; + + /** + * @default new THREE.Vector3() + */ + c: Vector3; + + set(a: Vector3, b: Vector3, c: Vector3): Triangle; + setFromPointsAndIndices(points: Vector3[], i0: number, i1: number, i2: number): this; + setFromAttributeAndIndices( + attribute: BufferAttribute | InterleavedBufferAttribute, + i0: number, + i1: number, + i2: number, + ): this; + clone(): this; + copy(triangle: Triangle): this; + getArea(): number; + getMidpoint(target: Vector3): Vector3; + getNormal(target: Vector3): Vector3; + getPlane(target: Plane): Plane; + getBarycoord(point: Vector3, target: Vector3): Vector3 | null; + getInterpolation(point: Vector3, v1: Vector2, v2: Vector2, v3: Vector2, target: Vector2): Vector2 | null; + getInterpolation(point: Vector3, v1: Vector3, v2: Vector3, v3: Vector3, target: Vector3): Vector3 | null; + getInterpolation(point: Vector3, v1: Vector4, v2: Vector4, v3: Vector4, target: Vector4): Vector4 | null; + containsPoint(point: Vector3): boolean; + intersectsBox(box: Box3): boolean; + isFrontFacing(direction: Vector3): boolean; + closestPointToPoint(point: Vector3, target: Vector3): Vector3; + equals(triangle: Triangle): boolean; + + static getNormal(a: Vector3, b: Vector3, c: Vector3, target: Vector3): Vector3; + static getBarycoord(point: Vector3, a: Vector3, b: Vector3, c: Vector3, target: Vector3): Vector3 | null; + static containsPoint(point: Vector3, a: Vector3, b: Vector3, c: Vector3): boolean; + static getInterpolation( + point: Vector3, + p1: Vector3, + p2: Vector3, + p3: Vector3, + v1: Vector2, + v2: Vector2, + v3: Vector2, + target: Vector2, + ): Vector2 | null; + static getInterpolation( + point: Vector3, + p1: Vector3, + p2: Vector3, + p3: Vector3, + v1: Vector3, + v2: Vector3, + v3: Vector3, + target: Vector3, + ): Vector3 | null; + static getInterpolation( + point: Vector3, + p1: Vector3, + p2: Vector3, + p3: Vector3, + v1: Vector4, + v2: Vector4, + v3: Vector4, + target: Vector4, + ): Vector4 | null; + static isFrontFacing(a: Vector3, b: Vector3, c: Vector3, direction: Vector3): boolean; +} diff --git a/src-testing/src/math/Vector2.d.ts b/src-testing/src/math/Vector2.d.ts new file mode 100644 index 000000000..fd2a84a1d --- /dev/null +++ b/src-testing/src/math/Vector2.d.ts @@ -0,0 +1,321 @@ +import { BufferAttribute } from "../core/BufferAttribute.js"; +import { Matrix3 } from "./Matrix3.js"; + +export type Vector2Tuple = [x: number, y: number]; + +export interface Vector2Like { + readonly x: number; + readonly y: number; +} + +/** + * 2D vector. + */ +export class Vector2 { + constructor(x?: number, y?: number); + + /** + * @default 0 + */ + x: number; + + /** + * @default 0 + */ + y: number; + width: number; + height: number; + readonly isVector2: true; + + /** + * Sets value of this vector. + */ + set(x: number, y: number): this; + + /** + * Sets the x and y values of this vector both equal to scalar. + */ + setScalar(scalar: number): this; + + /** + * Sets X component of this vector. + */ + setX(x: number): this; + + /** + * Sets Y component of this vector. + */ + setY(y: number): this; + + /** + * Sets a component of this vector. + */ + setComponent(index: number, value: number): this; + + /** + * Gets a component of this vector. + */ + getComponent(index: number): number; + + /** + * Returns a new Vector2 instance with the same `x` and `y` values. + */ + clone(): this; + + /** + * Copies value of v to this vector. + */ + copy(v: Vector2Like): this; + + /** + * Adds v to this vector. + */ + add(v: Vector2Like): this; + + /** + * Adds the scalar value s to this vector's x and y values. + */ + addScalar(s: number): this; + + /** + * Sets this vector to a + b. + */ + addVectors(a: Vector2Like, b: Vector2Like): this; + + /** + * Adds the multiple of v and s to this vector. + */ + addScaledVector(v: Vector2Like, s: number): this; + + /** + * Subtracts v from this vector. + */ + sub(v: Vector2Like): this; + + /** + * Subtracts s from this vector's x and y components. + */ + subScalar(s: number): this; + + /** + * Sets this vector to a - b. + */ + subVectors(a: Vector2Like, b: Vector2Like): this; + + /** + * Multiplies this vector by v. + */ + multiply(v: Vector2Like): this; + + /** + * Multiplies this vector by scalar s. + */ + multiplyScalar(scalar: number): this; + + /** + * Divides this vector by v. + */ + divide(v: Vector2Like): this; + + /** + * Divides this vector by scalar s. + * Set vector to ( 0, 0 ) if s == 0. + */ + divideScalar(s: number): this; + + /** + * Multiplies this vector (with an implicit 1 as the 3rd component) by m. + */ + applyMatrix3(m: Matrix3): this; + + /** + * If this vector's x or y value is greater than v's x or y value, replace that value with the corresponding min value. + */ + min(v: Vector2Like): this; + + /** + * If this vector's x or y value is less than v's x or y value, replace that value with the corresponding max value. + */ + max(v: Vector2Like): this; + + /** + * If this vector's x or y value is greater than the max vector's x or y value, it is replaced by the corresponding value. + * If this vector's x or y value is less than the min vector's x or y value, it is replaced by the corresponding value. + * @param min the minimum x and y values. + * @param max the maximum x and y values in the desired range. + */ + clamp(min: Vector2Like, max: Vector2Like): this; + + /** + * If this vector's x or y values are greater than the max value, they are replaced by the max value. + * If this vector's x or y values are less than the min value, they are replaced by the min value. + * @param min the minimum value the components will be clamped to. + * @param max the maximum value the components will be clamped to. + */ + clampScalar(min: number, max: number): this; + + /** + * If this vector's length is greater than the max value, it is replaced by the max value. + * If this vector's length is less than the min value, it is replaced by the min value. + * @param min the minimum value the length will be clamped to. + * @param max the maximum value the length will be clamped to. + */ + clampLength(min: number, max: number): this; + + /** + * The components of the vector are rounded down to the nearest integer value. + */ + floor(): this; + + /** + * The x and y components of the vector are rounded up to the nearest integer value. + */ + ceil(): this; + + /** + * The components of the vector are rounded to the nearest integer value. + */ + round(): this; + + /** + * The components of the vector are rounded towards zero (up if negative, down if positive) to an integer value. + */ + roundToZero(): this; + + /** + * Inverts this vector. + */ + negate(): this; + + /** + * Computes dot product of this vector and v. + */ + dot(v: Vector2Like): number; + + /** + * Computes cross product of this vector and v. + */ + cross(v: Vector2Like): number; + + /** + * Computes squared length of this vector. + */ + lengthSq(): number; + + /** + * Computes length of this vector. + */ + length(): number; + + /** + * Computes the Manhattan length of this vector. + * + * see {@link http://en.wikipedia.org/wiki/Taxicab_geometry|Wikipedia: Taxicab Geometry} + */ + manhattanLength(): number; + + /** + * Normalizes this vector. + */ + normalize(): this; + + /** + * computes the angle in radians with respect to the positive x-axis + */ + angle(): number; + + /** + * Returns the angle between this vector and vector {@link Vector2 | v} in radians. + */ + angleTo(v: Vector2): number; + + /** + * Computes distance of this vector to v. + */ + distanceTo(v: Vector2Like): number; + + /** + * Computes squared distance of this vector to v. + */ + distanceToSquared(v: Vector2Like): number; + + /** + * Computes the Manhattan length (distance) from this vector to the given vector v + * + * see {@link http://en.wikipedia.org/wiki/Taxicab_geometry|Wikipedia: Taxicab Geometry} + */ + manhattanDistanceTo(v: Vector2Like): number; + + /** + * Normalizes this vector and multiplies it by l. + */ + setLength(length: number): this; + + /** + * Linearly interpolates between this vector and v, where alpha is the distance along the line - alpha = 0 will be this vector, and alpha = 1 will be v. + * @param v vector to interpolate towards. + * @param alpha interpolation factor in the closed interval [0, 1]. + */ + lerp(v: Vector2Like, alpha: number): this; + + /** + * Sets this vector to be the vector linearly interpolated between v1 and v2 where alpha is the distance along the line connecting the two vectors - alpha = 0 will be v1, and alpha = 1 will be v2. + * @param v1 the starting vector. + * @param v2 vector to interpolate towards. + * @param alpha interpolation factor in the closed interval [0, 1]. + */ + lerpVectors(v1: Vector2Like, v2: Vector2Like, alpha: number): this; + + /** + * Checks for strict equality of this vector and v. + */ + equals(v: Vector2Like): boolean; + + /** + * Sets this vector's x and y value from the provided array or array-like. + * @param array the source array or array-like. + * @param offset (optional) offset into the array. Default is 0. + */ + fromArray(array: number[] | ArrayLike, offset?: number): this; + + /** + * Returns an array [x, y], or copies x and y into the provided array. + * @param array (optional) array to store the vector to. If this is not provided, a new array will be created. + * @param offset (optional) optional offset into the array. + * @return The created or provided array. + */ + toArray(array?: number[], offset?: number): number[]; + toArray(array?: Vector2Tuple, offset?: 0): Vector2Tuple; + + /** + * Copies x and y into the provided array-like. + * @param array array-like to store the vector to. + * @param offset (optional) optional offset into the array. + * @return The provided array-like. + */ + toArray(array: ArrayLike, offset?: number): ArrayLike; + + /** + * Sets this vector's x and y values from the attribute. + * @param attribute the source attribute. + * @param index index in the attribute. + */ + fromBufferAttribute(attribute: BufferAttribute, index: number): this; + + /** + * Rotates the vector around center by angle radians. + * @param center the point around which to rotate. + * @param angle the angle to rotate, in radians. + */ + rotateAround(center: Vector2Like, angle: number): this; + + /** + * Sets this vector's x and y from Math.random + */ + random(): this; + + /** + * Iterating through a Vector2 instance will yield its components (x, y) in the corresponding order. + */ + [Symbol.iterator](): Iterator; +} diff --git a/src-testing/src/math/Vector3.d.ts b/src-testing/src/math/Vector3.d.ts new file mode 100644 index 000000000..56e907ceb --- /dev/null +++ b/src-testing/src/math/Vector3.d.ts @@ -0,0 +1,301 @@ +import { Camera } from "../cameras/Camera.js"; +import { BufferAttribute } from "../core/BufferAttribute.js"; +import { InterleavedBufferAttribute } from "../core/InterleavedBufferAttribute.js"; +import { RGB } from "./Color.js"; +import { Cylindrical } from "./Cylindrical.js"; +import { Euler } from "./Euler.js"; +import { Matrix3 } from "./Matrix3.js"; +import { Matrix4 } from "./Matrix4.js"; +import { QuaternionLike } from "./Quaternion.js"; +import { Spherical } from "./Spherical.js"; + +export type Vector3Tuple = [number, number, number]; + +export interface Vector3Like { + readonly x: number; + readonly y: number; + readonly z: number; +} + +/** + * 3D vector. + * + * see {@link https://github.com/mrdoob/three.js/blob/master/src/math/Vector3.js} + * + * @example + * const a = new THREE.Vector3( 1, 0, 0 ); + * const b = new THREE.Vector3( 0, 1, 0 ); + * const c = new THREE.Vector3(); + * c.crossVectors( a, b ); + */ +export class Vector3 { + constructor(x?: number, y?: number, z?: number); + + /** + * @default 0 + */ + x: number; + + /** + * @default 0 + */ + y: number; + + /** + * @default 0 + */ + z: number; + readonly isVector3: true; + + /** + * Sets value of this vector. + */ + set(x: number, y: number, z: number): this; + + /** + * Sets all values of this vector. + */ + setScalar(scalar: number): this; + + /** + * Sets x value of this vector. + */ + setX(x: number): this; + + /** + * Sets y value of this vector. + */ + setY(y: number): this; + + /** + * Sets z value of this vector. + */ + setZ(z: number): this; + + setComponent(index: number, value: number): this; + + getComponent(index: number): number; + + /** + * Clones this vector. + */ + clone(): this; + + /** + * Copies value of v to this vector. + */ + copy(v: Vector3Like): this; + + /** + * Adds v to this vector. + */ + add(v: Vector3Like): this; + + addScalar(s: number): this; + + /** + * Sets this vector to a + b. + */ + addVectors(a: Vector3Like, b: Vector3Like): this; + + addScaledVector(v: Vector3, s: number): this; + + /** + * Subtracts v from this vector. + */ + sub(a: Vector3Like): this; + + subScalar(s: number): this; + + /** + * Sets this vector to a - b. + */ + subVectors(a: Vector3Like, b: Vector3Like): this; + + multiply(v: Vector3Like): this; + + /** + * Multiplies this vector by scalar s. + */ + multiplyScalar(s: number): this; + + multiplyVectors(a: Vector3Like, b: Vector3Like): this; + + applyEuler(euler: Euler): this; + + applyAxisAngle(axis: Vector3, angle: number): this; + + applyMatrix3(m: Matrix3): this; + + applyNormalMatrix(m: Matrix3): this; + + applyMatrix4(m: Matrix4): this; + + applyQuaternion(q: QuaternionLike): this; + + project(camera: Camera): this; + + unproject(camera: Camera): this; + + transformDirection(m: Matrix4): this; + + divide(v: Vector3Like): this; + + /** + * Divides this vector by scalar s. + * Set vector to ( 0, 0, 0 ) if s == 0. + */ + divideScalar(s: number): this; + + min(v: Vector3Like): this; + + max(v: Vector3Like): this; + + clamp(min: Vector3Like, max: Vector3Like): this; + + clampScalar(min: number, max: number): this; + + clampLength(min: number, max: number): this; + + floor(): this; + + ceil(): this; + + round(): this; + + roundToZero(): this; + + /** + * Inverts this vector. + */ + negate(): this; + + /** + * Computes dot product of this vector and v. + */ + dot(v: Vector3Like): number; + + /** + * Computes squared length of this vector. + */ + lengthSq(): number; + + /** + * Computes length of this vector. + */ + length(): number; + + /** + * Computes the Manhattan length of this vector. + * + * see {@link http://en.wikipedia.org/wiki/Taxicab_geometry|Wikipedia: Taxicab Geometry} + */ + manhattanLength(): number; + + /** + * Normalizes this vector. + */ + normalize(): this; + + /** + * Normalizes this vector and multiplies it by l. + */ + setLength(l: number): this; + lerp(v: Vector3Like, alpha: number): this; + + lerpVectors(v1: Vector3Like, v2: Vector3Like, alpha: number): this; + + /** + * Sets this vector to cross product of itself and v. + */ + cross(a: Vector3Like): this; + + /** + * Sets this vector to cross product of a and b. + */ + crossVectors(a: Vector3Like, b: Vector3Like): this; + projectOnVector(v: Vector3): this; + projectOnPlane(planeNormal: Vector3): this; + reflect(vector: Vector3Like): this; + angleTo(v: Vector3): number; + + /** + * Computes distance of this vector to v. + */ + distanceTo(v: Vector3Like): number; + + /** + * Computes squared distance of this vector to v. + */ + distanceToSquared(v: Vector3Like): number; + + /** + * Computes the Manhattan length (distance) from this vector to the given vector v + * + * see {@link http://en.wikipedia.org/wiki/Taxicab_geometry|Wikipedia: Taxicab Geometry} + */ + manhattanDistanceTo(v: Vector3Like): number; + + setFromSpherical(s: Spherical): this; + setFromSphericalCoords(r: number, phi: number, theta: number): this; + setFromCylindrical(s: Cylindrical): this; + setFromCylindricalCoords(radius: number, theta: number, y: number): this; + setFromMatrixPosition(m: Matrix4): this; + setFromMatrixScale(m: Matrix4): this; + setFromMatrixColumn(matrix: Matrix4, index: number): this; + setFromMatrix3Column(matrix: Matrix3, index: number): this; + + /** + * Sets this vector's {@link x}, {@link y} and {@link z} components from the x, y, and z components of the specified {@link Euler Euler Angle}. + */ + setFromEuler(e: Euler): this; + + /** + * Sets this vector's {@link x}, {@link y} and {@link z} components from the r, g, and b components of the specified + * {@link Color | color}. + */ + setFromColor(color: RGB): this; + + /** + * Checks for strict equality of this vector and v. + */ + equals(v: Vector3Like): boolean; + + /** + * Sets this vector's x, y and z value from the provided array or array-like. + * @param array the source array or array-like. + * @param offset (optional) offset into the array. Default is 0. + */ + fromArray(array: number[] | ArrayLike, offset?: number): this; + + /** + * Returns an array [x, y, z], or copies x, y and z into the provided array. + * @param array (optional) array to store the vector to. If this is not provided, a new array will be created. + * @param offset (optional) optional offset into the array. + * @return The created or provided array. + */ + toArray(array?: number[], offset?: number): number[]; + toArray(array?: Vector3Tuple, offset?: 0): Vector3Tuple; + + /** + * Copies x, y and z into the provided array-like. + * @param array array-like to store the vector to. + * @param offset (optional) optional offset into the array-like. + * @return The provided array-like. + */ + toArray(array: ArrayLike, offset?: number): ArrayLike; + + fromBufferAttribute(attribute: BufferAttribute | InterleavedBufferAttribute, index: number): this; + + /** + * Sets this vector's x, y and z from Math.random + */ + random(): this; + + randomDirection(): this; + + /** + * Iterating through a Vector3 instance will yield its components (x, y, z) in the corresponding order. + */ + [Symbol.iterator](): Iterator; +} diff --git a/src-testing/src/math/Vector4.d.ts b/src-testing/src/math/Vector4.d.ts new file mode 100644 index 000000000..88cf74ff2 --- /dev/null +++ b/src-testing/src/math/Vector4.d.ts @@ -0,0 +1,239 @@ +import { BufferAttribute } from "../core/BufferAttribute.js"; +import { Matrix4 } from "./Matrix4.js"; +import { QuaternionLike } from "./Quaternion.js"; + +export type Vector4Tuple = [number, number, number, number]; + +export interface Vector4Like { + readonly x: number; + readonly y: number; + readonly z: number; + readonly w: number; +} + +/** + * 4D vector. + */ +export class Vector4 { + constructor(x?: number, y?: number, z?: number, w?: number); + + /** + * @default 0 + */ + x: number; + + /** + * @default 0 + */ + y: number; + + /** + * @default 0 + */ + z: number; + + /** + * @default 0 + */ + w: number; + + width: number; + height: number; + readonly isVector4: true; + + /** + * Sets value of this vector. + */ + set(x: number, y: number, z: number, w: number): this; + + /** + * Sets all values of this vector. + */ + setScalar(scalar: number): this; + + /** + * Sets X component of this vector. + */ + setX(x: number): this; + + /** + * Sets Y component of this vector. + */ + setY(y: number): this; + + /** + * Sets Z component of this vector. + */ + setZ(z: number): this; + + /** + * Sets w component of this vector. + */ + setW(w: number): this; + + setComponent(index: number, value: number): this; + + getComponent(index: number): number; + + /** + * Clones this vector. + */ + clone(): this; + + /** + * Copies value of v to this vector. + */ + copy(v: Vector4Like): this; + + /** + * Adds v to this vector. + */ + add(v: Vector4Like): this; + + addScalar(scalar: number): this; + + /** + * Sets this vector to a + b. + */ + addVectors(a: Vector4Like, b: Vector4Like): this; + + addScaledVector(v: Vector4Like, s: number): this; + /** + * Subtracts v from this vector. + */ + sub(v: Vector4Like): this; + + subScalar(s: number): this; + + /** + * Sets this vector to a - b. + */ + subVectors(a: Vector4Like, b: Vector4Like): this; + + multiply(v: Vector4Like): this; + + /** + * Multiplies this vector by scalar s. + */ + multiplyScalar(s: number): this; + + applyMatrix4(m: Matrix4): this; + + /** + * Divides this vector by scalar s. + * Set vector to ( 0, 0, 0 ) if s == 0. + */ + divideScalar(s: number): this; + + /** + * http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm + * @param q is assumed to be normalized + */ + setAxisAngleFromQuaternion(q: QuaternionLike): this; + + /** + * http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm + * @param m assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) + */ + setAxisAngleFromRotationMatrix(m: Matrix4): this; + + /** + * Sets this vector to the position elements of the + * [transformation matrix]{@link https://en.wikipedia.org/wiki/Transformation_matrix} m. + */ + setFromMatrixPosition(m: Matrix4): this; + + min(v: Vector4Like): this; + max(v: Vector4Like): this; + clamp(min: Vector4Like, max: Vector4Like): this; + clampScalar(min: number, max: number): this; + floor(): this; + ceil(): this; + round(): this; + roundToZero(): this; + + /** + * Inverts this vector. + */ + negate(): this; + + /** + * Computes dot product of this vector and v. + */ + dot(v: Vector4Like): number; + + /** + * Computes squared length of this vector. + */ + lengthSq(): number; + + /** + * Computes length of this vector. + */ + length(): number; + + /** + * Computes the Manhattan length of this vector. + * + * see {@link http://en.wikipedia.org/wiki/Taxicab_geometry|Wikipedia: Taxicab Geometry} + */ + manhattanLength(): number; + + /** + * Normalizes this vector. + */ + normalize(): this; + + /** + * Normalizes this vector and multiplies it by l. + */ + setLength(length: number): this; + + /** + * Linearly interpolate between this vector and v with alpha factor. + */ + lerp(v: Vector4Like, alpha: number): this; + + lerpVectors(v1: Vector4Like, v2: Vector4Like, alpha: number): this; + + /** + * Checks for strict equality of this vector and v. + */ + equals(v: Vector4Like): boolean; + + /** + * Sets this vector's x, y, z and w value from the provided array or array-like. + * @param array the source array or array-like. + * @param offset (optional) offset into the array. Default is 0. + */ + fromArray(array: number[] | ArrayLike, offset?: number): this; + + /** + * Returns an array [x, y, z, w], or copies x, y, z and w into the provided array. + * @param array (optional) array to store the vector to. If this is not provided, a new array will be created. + * @param offset (optional) optional offset into the array. + * @return The created or provided array. + */ + toArray(array?: number[], offset?: number): number[]; + toArray(array?: Vector4Tuple, offset?: 0): Vector4Tuple; + + /** + * Copies x, y, z and w into the provided array-like. + * @param array array-like to store the vector to. + * @param offset (optional) optional offset into the array-like. + * @return The provided array-like. + */ + toArray(array: ArrayLike, offset?: number): ArrayLike; + + fromBufferAttribute(attribute: BufferAttribute, index: number): this; + + /** + * Sets this vector's x, y, z and w from Math.random + */ + random(): this; + + /** + * Iterating through a Vector4 instance will yield its components (x, y, z, w) in the corresponding order. + */ + [Symbol.iterator](): Iterator; +} diff --git a/src-testing/src/math/interpolants/CubicInterpolant.d.ts b/src-testing/src/math/interpolants/CubicInterpolant.d.ts new file mode 100644 index 000000000..282b98d7e --- /dev/null +++ b/src-testing/src/math/interpolants/CubicInterpolant.d.ts @@ -0,0 +1,7 @@ +import { Interpolant } from "../Interpolant.js"; + +export class CubicInterpolant extends Interpolant { + constructor(parameterPositions: any, samplesValues: any, sampleSize: number, resultBuffer?: any); + + interpolate_(i1: number, t0: number, t: number, t1: number): any; +} diff --git a/src-testing/src/math/interpolants/DiscreteInterpolant.d.ts b/src-testing/src/math/interpolants/DiscreteInterpolant.d.ts new file mode 100644 index 000000000..28bd458b8 --- /dev/null +++ b/src-testing/src/math/interpolants/DiscreteInterpolant.d.ts @@ -0,0 +1,7 @@ +import { Interpolant } from "../Interpolant.js"; + +export class DiscreteInterpolant extends Interpolant { + constructor(parameterPositions: any, samplesValues: any, sampleSize: number, resultBuffer?: any); + + interpolate_(i1: number, t0: number, t: number, t1: number): any; +} diff --git a/src-testing/src/math/interpolants/LinearInterpolant.d.ts b/src-testing/src/math/interpolants/LinearInterpolant.d.ts new file mode 100644 index 000000000..e6ff11c0b --- /dev/null +++ b/src-testing/src/math/interpolants/LinearInterpolant.d.ts @@ -0,0 +1,7 @@ +import { Interpolant } from "../Interpolant.js"; + +export class LinearInterpolant extends Interpolant { + constructor(parameterPositions: any, samplesValues: any, sampleSize: number, resultBuffer?: any); + + interpolate_(i1: number, t0: number, t: number, t1: number): any; +} diff --git a/src-testing/src/math/interpolants/QuaternionLinearInterpolant.d.ts b/src-testing/src/math/interpolants/QuaternionLinearInterpolant.d.ts new file mode 100644 index 000000000..dccb66976 --- /dev/null +++ b/src-testing/src/math/interpolants/QuaternionLinearInterpolant.d.ts @@ -0,0 +1,7 @@ +import { Interpolant } from "../Interpolant.js"; + +export class QuaternionLinearInterpolant extends Interpolant { + constructor(parameterPositions: any, samplesValues: any, sampleSize: number, resultBuffer?: any); + + interpolate_(i1: number, t0: number, t: number, t1: number): any; +} diff --git a/src-testing/src/nodes/Nodes.ts b/src-testing/src/nodes/Nodes.ts new file mode 100644 index 000000000..41392bede --- /dev/null +++ b/src-testing/src/nodes/Nodes.ts @@ -0,0 +1,435 @@ +// @TODO: We can simplify "export { default as SomeNode, other, exports } from '...'" to just "export * from '...'" if we will use only named exports +// this will also solve issues like "import TempNode from '../core/Node.js'" + +// constants +export * from './core/constants.js'; + +// core +export { default as AssignNode, assign } from './core/AssignNode.js'; +export { default as AttributeNode, attribute } from './core/AttributeNode.js'; +export { default as BypassNode, bypass } from './core/BypassNode.js'; +export { default as CacheNode, cache } from './core/CacheNode.js'; +export { default as ConstNode } from './core/ConstNode.js'; +export { default as ContextNode, context, label } from './core/ContextNode.js'; +export { default as IndexNode, vertexIndex, instanceIndex, drawIndex } from './core/IndexNode.js'; +export { default as LightingModel } from './core/LightingModel.js'; +export { default as Node, addNodeClass, createNodeFromType } from './core/Node.js'; +export { default as VarNode, temp } from './core/VarNode.js'; +export { default as NodeAttribute } from './core/NodeAttribute.js'; +export { default as NodeBuilder } from './core/NodeBuilder.js'; +export { default as NodeCache } from './core/NodeCache.js'; +export { default as NodeCode } from './core/NodeCode.js'; +export { default as NodeFrame } from './core/NodeFrame.js'; +export { default as NodeFunctionInput } from './core/NodeFunctionInput.js'; +export { default as NodeKeywords } from './core/NodeKeywords.js'; +export { default as NodeUniform } from './core/NodeUniform.js'; +export { default as NodeVar } from './core/NodeVar.js'; +export { default as NodeVarying } from './core/NodeVarying.js'; +export { default as ParameterNode, parameter } from './core/ParameterNode.js'; +export { + default as PropertyNode, + property, + varyingProperty, + output, + diffuseColor, + emissive, + roughness, + metalness, + clearcoat, + clearcoatRoughness, + sheen, + sheenRoughness, + iridescence, + iridescenceIOR, + iridescenceThickness, + specularColor, + shininess, + dashSize, + gapSize, + pointWidth, + alphaT, + anisotropy, + anisotropyB, + anisotropyT, +} from './core/PropertyNode.js'; +export { default as StackNode, stack } from './core/StackNode.js'; +export { default as TempNode } from './core/TempNode.js'; +export { + default as UniformGroupNode, + uniformGroup, + objectGroup, + renderGroup, + frameGroup, +} from './core/UniformGroupNode.js'; +export { default as UniformNode, uniform } from './core/UniformNode.js'; +export { default as VaryingNode, varying } from './core/VaryingNode.js'; +export { default as OutputStructNode, outputStruct } from './core/OutputStructNode.js'; +export { default as MRTNode, mrt } from './core/MRTNode.js'; + +import * as NodeUtils from './core/NodeUtils.js'; +export { NodeUtils }; + +// math +export { + default as MathNode, + PI, + PI2, + EPSILON, + INFINITY, + radians, + degrees, + exp, + exp2, + log, + log2, + sqrt, + inverseSqrt, + floor, + ceil, + normalize, + fract, + sin, + cos, + tan, + asin, + acos, + atan, + abs, + sign, + length, + lengthSq, + negate, + oneMinus, + dFdx, + dFdy, + round, + reciprocal, + trunc, + fwidth, + bitcast, + atan2, + min, + max, + mod, + step, + reflect, + distance, + difference, + dot, + cross, + pow, + pow2, + pow3, + pow4, + transformDirection, + mix, + clamp, + saturate, + refract, + smoothstep, + faceForward, + cbrt, + transpose, + all, + any, + equals, + rand, +} from './math/MathNode.js'; + +export { + default as OperatorNode, + add, + sub, + mul, + div, + remainder, + equal, + lessThan, + greaterThan, + lessThanEqual, + greaterThanEqual, + and, + or, + not, + xor, + bitAnd, + bitNot, + bitOr, + bitXor, + shiftLeft, + shiftRight, +} from './math/OperatorNode.js'; +export { default as CondNode, select, cond } from './math/CondNode.js'; +export { default as HashNode, hash } from './math/HashNode.js'; + +// math utils +export { parabola, gain, pcurve, sinc } from './math/MathUtils.js'; +export { triNoise3D } from './math/TriNoise3D.js'; + +// utils +export { default as ArrayElementNode } from './utils/ArrayElementNode.js'; +export { default as ConvertNode } from './utils/ConvertNode.js'; +export { default as DiscardNode, discard, Return } from './utils/DiscardNode.js'; +export { default as EquirectUVNode, equirectUV } from './utils/EquirectUVNode.js'; +export { default as FunctionOverloadingNode, overloadingFn } from './utils/FunctionOverloadingNode.js'; +export { default as JoinNode } from './utils/JoinNode.js'; +export { default as LoopNode, Loop, Continue, Break } from './utils/LoopNode.js'; +export { default as MatcapUVNode, matcapUV } from './utils/MatcapUVNode.js'; +export { default as MaxMipLevelNode, maxMipLevel } from './utils/MaxMipLevelNode.js'; +export { default as OscNode, oscSine, oscSquare, oscTriangle, oscSawtooth } from './utils/OscNode.js'; +export { default as PackingNode, directionToColor, colorToDirection } from './utils/PackingNode.js'; +export { default as RemapNode, remap, remapClamp } from './utils/RemapNode.js'; +export * from './utils/UVUtils.js'; +export * from './utils/SpriteUtils.js'; +export * from './utils/ViewportUtils.js'; +export { default as RotateNode, rotate } from './utils/RotateNode.js'; +export { default as SetNode } from './utils/SetNode.js'; +export { default as SplitNode } from './utils/SplitNode.js'; +export { default as SpriteSheetUVNode, spritesheetUV } from './utils/SpriteSheetUVNode.js'; +export { default as StorageArrayElementNode } from './utils/StorageArrayElementNode.js'; +export { default as TimerNode, timerLocal, timerGlobal, timerDelta, frameId } from './utils/TimerNode.js'; +export { + default as TriplanarTexturesNode, + triplanarTextures, + triplanarTexture, +} from './utils/TriplanarTexturesNode.js'; +export { default as ReflectorNode, reflector } from './utils/ReflectorNode.js'; +export { default as RTTNode, rtt } from './utils/RTTNode.js'; + +// shadernode +export * from './shadernode/ShaderNode.js'; + +// accessors +export { TBNViewMatrix, parallaxDirection, parallaxUV, transformedBentNormalView } from './accessors/AccessorsUtils.js'; +export { default as UniformArrayNode, uniformArray } from './accessors/UniformArrayNode.js'; +export * from './accessors/BitangentNode.js'; +export { + default as BufferAttributeNode, + bufferAttribute, + dynamicBufferAttribute, + instancedBufferAttribute, + instancedDynamicBufferAttribute, +} from './accessors/BufferAttributeNode.js'; +export { default as BufferNode, buffer } from './accessors/BufferNode.js'; +export * from './accessors/CameraNode.js'; +export { default as VertexColorNode, vertexColor } from './accessors/VertexColorNode.js'; +export { default as CubeTextureNode, cubeTexture } from './accessors/CubeTextureNode.js'; +export { default as InstanceNode, instance } from './accessors/InstanceNode.js'; +export { default as BatchNode, batch } from './accessors/BatchNode.js'; +export { + default as MaterialNode, + materialAlphaTest, + materialColor, + materialShininess, + materialEmissive, + materialOpacity, + materialSpecular, + materialSpecularStrength, + materialReflectivity, + materialRoughness, + materialMetalness, + materialNormal, + materialClearcoat, + materialClearcoatRoughness, + materialClearcoatNormal, + materialRotation, + materialSheen, + materialSheenRoughness, + materialIridescence, + materialIridescenceIOR, + materialIridescenceThickness, + materialLineScale, + materialLineDashSize, + materialLineGapSize, + materialLineWidth, + materialLineDashOffset, + materialPointWidth, + materialAnisotropy, + materialAnisotropyVector, + materialDispersion, + materialLightMap, + materialAOMap, +} from './accessors/MaterialNode.js'; +export * from './accessors/MaterialProperties.js'; +export { default as MaterialReferenceNode, materialReference } from './accessors/MaterialReferenceNode.js'; +export { default as RendererReferenceNode, rendererReference } from './accessors/RendererReferenceNode.js'; +export { default as MorphNode, morphReference } from './accessors/MorphNode.js'; +export { default as TextureBicubicNode, textureBicubic } from './accessors/TextureBicubicNode.js'; +export { + default as ModelNode, + modelDirection, + modelViewMatrix, + modelNormalMatrix, + modelWorldMatrix, + modelPosition, + modelViewPosition, + modelScale, + modelWorldMatrixInverse, +} from './accessors/ModelNode.js'; +export { default as ModelViewProjectionNode, modelViewProjection } from './accessors/ModelViewProjectionNode.js'; +export * from './accessors/NormalNode.js'; +export { + default as Object3DNode, + objectDirection, + objectViewMatrix, + objectNormalMatrix, + objectWorldMatrix, + objectPosition, + objectScale, + objectViewPosition, +} from './accessors/Object3DNode.js'; +export { default as PointUVNode, pointUV } from './accessors/PointUVNode.js'; +export * from './accessors/PositionNode.js'; +export { default as ReferenceNode, reference, referenceBuffer } from './accessors/ReferenceNode.js'; +export * from './accessors/ReflectVectorNode.js'; +export { default as SkinningNode, skinning, skinningReference } from './accessors/SkinningNode.js'; +export { default as SceneNode, backgroundBlurriness, backgroundIntensity } from './accessors/SceneNode.js'; +export { default as StorageBufferNode, storage, storageObject } from './accessors/StorageBufferNode.js'; +export * from './accessors/TangentNode.js'; +export { default as TextureNode, texture, textureLoad, /*textureLevel,*/ sampler } from './accessors/TextureNode.js'; +export { default as TextureSizeNode, textureSize } from './accessors/TextureSizeNode.js'; +export { default as StorageTextureNode, storageTexture, textureStore } from './accessors/StorageTextureNode.js'; +export { default as Texture3DNode, texture3D } from './accessors/Texture3DNode.js'; +export * from './accessors/UVNode.js'; +export { default as UserDataNode, userData } from './accessors/UserDataNode.js'; + +// display +export { default as BlendModeNode, burn, dodge, overlay, screen } from './display/BlendModeNode.js'; +export { default as BumpMapNode, bumpMap } from './display/BumpMapNode.js'; +export { + default as ColorAdjustmentNode, + saturation, + vibrance, + hue, + luminance, + threshold, +} from './display/ColorAdjustmentNode.js'; +export { + default as ColorSpaceNode, + linearToColorSpace, + colorSpaceToLinear, + linearTosRGB, + sRGBToLinear, +} from './display/ColorSpaceNode.js'; +export { default as FrontFacingNode, frontFacing, faceDirection } from './display/FrontFacingNode.js'; +export { default as NormalMapNode, normalMap } from './display/NormalMapNode.js'; +export { default as PosterizeNode, posterize } from './display/PosterizeNode.js'; +export { default as ToneMappingNode, toneMapping } from './display/ToneMappingNode.js'; +export { + default as ViewportNode, + viewport, + viewportCoordinate, + viewportResolution, + viewportTopLeft, + viewportBottomLeft, + viewportTopRight, + viewportBottomRight, +} from './display/ViewportNode.js'; +export { default as ViewportTextureNode, viewportTexture, viewportMipTexture } from './display/ViewportTextureNode.js'; +export { default as ViewportSharedTextureNode, viewportSharedTexture } from './display/ViewportSharedTextureNode.js'; +export { default as ViewportDepthTextureNode, viewportDepthTexture } from './display/ViewportDepthTextureNode.js'; +export { + default as ViewportDepthNode, + viewZToOrthographicDepth, + orthographicDepthToViewZ, + viewZToPerspectiveDepth, + perspectiveDepthToViewZ, + depth, + linearDepth, + viewportLinearDepth, +} from './display/ViewportDepthNode.js'; +export { default as GaussianBlurNode, gaussianBlur } from './display/GaussianBlurNode.js'; +export { default as AfterImageNode, afterImage } from './display/AfterImageNode.js'; +export { default as AnamorphicNode, anamorphic } from './display/AnamorphicNode.js'; +export { default as SobelOperatorNode, sobel } from './display/SobelOperatorNode.js'; +export { default as DepthOfFieldNode, dof } from './display/DepthOfFieldNode.js'; +export { default as DotScreenNode, dotScreen } from './display/DotScreenNode.js'; +export { default as RGBShiftNode, rgbShift } from './display/RGBShiftNode.js'; +export { default as FilmNode, film } from './display/FilmNode.js'; +export { default as Lut3DNode, lut3D } from './display/Lut3DNode.js'; +export { default as GTAONode, ao } from './display/GTAONode.js'; +export { default as DenoiseNode, denoise } from './display/DenoiseNode.js'; +export { default as FXAANode, fxaa } from './display/FXAANode.js'; +export { default as BloomNode, bloom } from './display/BloomNode.js'; +export { default as TransitionNode, transition } from './display/TransitionNode.js'; +export { default as RenderOutputNode, renderOutput } from './display/RenderOutputNode.js'; +export { default as PixelationPassNode, pixelationPass } from './display/PixelationPassNode.js'; +export { bleach } from './display/BleachBypassNode.js'; +export { sepia } from './display/SepiaNode.js'; + +export { default as PassNode, pass, passTexture, depthPass } from './display/PassNode.js'; + +// code +export { default as ExpressionNode, expression } from './code/ExpressionNode.js'; +export { default as CodeNode, code, js, wgsl, glsl } from './code/CodeNode.js'; +export { default as FunctionCallNode, call } from './code/FunctionCallNode.js'; +export { default as FunctionNode, wgslFn, glslFn } from './code/FunctionNode.js'; +export { default as ScriptableNode, scriptable, global } from './code/ScriptableNode.js'; +export { default as ScriptableValueNode, scriptableValue } from './code/ScriptableValueNode.js'; + +// fog +export { default as FogNode, fog } from './fog/FogNode.js'; +export { default as FogRangeNode, rangeFog } from './fog/FogRangeNode.js'; +export { default as FogExp2Node, densityFog } from './fog/FogExp2Node.js'; + +// geometry +export { default as RangeNode, range } from './geometry/RangeNode.js'; + +// gpgpu +export { default as ComputeNode, compute } from './gpgpu/ComputeNode.js'; + +// lighting +export { default as LightNode, lightTargetDirection } from './lighting/LightNode.js'; +export { default as PointLightNode } from './lighting/PointLightNode.js'; +export { default as DirectionalLightNode } from './lighting/DirectionalLightNode.js'; +export { default as RectAreaLightNode } from './lighting/RectAreaLightNode.js'; +export { default as SpotLightNode } from './lighting/SpotLightNode.js'; +export { default as IESSpotLightNode } from './lighting/IESSpotLightNode.js'; +export { default as AmbientLightNode } from './lighting/AmbientLightNode.js'; +export { default as LightsNode, lights, lightsNode, addLightNode } from './lighting/LightsNode.js'; +export { default as LightingNode /* @TODO: lighting (abstract), light */ } from './lighting/LightingNode.js'; +export { default as LightingContextNode, lightingContext } from './lighting/LightingContextNode.js'; +export { default as HemisphereLightNode } from './lighting/HemisphereLightNode.js'; +export { default as EnvironmentNode } from './lighting/EnvironmentNode.js'; +export { default as BasicEnvironmentNode } from './lighting/BasicEnvironmentNode.js'; +export { default as IrradianceNode } from './lighting/IrradianceNode.js'; +export { default as AONode } from './lighting/AONode.js'; +export { default as AnalyticLightNode } from './lighting/AnalyticLightNode.js'; + +// pmrem +export { default as PMREMNode, pmremTexture } from './pmrem/PMREMNode.js'; +export * from './pmrem/PMREMUtils.js'; + +// procedural +export { default as CheckerNode, checker } from './procedural/CheckerNode.js'; + +// loaders +export { default as NodeLoader } from './loaders/NodeLoader.js'; +export { default as NodeObjectLoader } from './loaders/NodeObjectLoader.js'; +export { default as NodeMaterialLoader } from './loaders/NodeMaterialLoader.js'; + +// parsers +export { default as GLSLNodeParser } from './parsers/GLSLNodeParser.js'; // @TODO: Move to jsm/renderers/webgl. + +// materials +export * from './materials/Materials.js'; + +// materialX +export * from './materialx/MaterialXNodes.js'; + +// functions +export { default as BRDF_GGX } from './functions/BSDF/BRDF_GGX.js'; +export { default as BRDF_Lambert } from './functions/BSDF/BRDF_Lambert.js'; +export { default as D_GGX } from './functions/BSDF/D_GGX.js'; +export { default as DFGApprox } from './functions/BSDF/DFGApprox.js'; +export { default as F_Schlick } from './functions/BSDF/F_Schlick.js'; +export { default as Schlick_to_F0 } from './functions/BSDF/Schlick_to_F0.js'; +export { default as V_GGX_SmithCorrelated } from './functions/BSDF/V_GGX_SmithCorrelated.js'; + +export { getDistanceAttenuation } from './lighting/LightUtils.js'; + +export { default as getGeometryRoughness } from './functions/material/getGeometryRoughness.js'; +export { default as getRoughness } from './functions/material/getRoughness.js'; + +export { default as PhongLightingModel } from './functions/PhongLightingModel.js'; +export { default as PhysicalLightingModel } from './functions/PhysicalLightingModel.js'; diff --git a/src-testing/src/nodes/accessors/AccessorsUtils.d.ts b/src-testing/src/nodes/accessors/AccessorsUtils.d.ts new file mode 100644 index 000000000..fc2980b0d --- /dev/null +++ b/src-testing/src/nodes/accessors/AccessorsUtils.d.ts @@ -0,0 +1,9 @@ +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const TBNViewMatrix: ShaderNodeObject; + +export const parallaxDirection: ShaderNodeObject; +export const parallaxUV: (uv: ShaderNodeObject, scale: NodeRepresentation) => ShaderNodeObject; + +export const transformedBentNormalView: ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/BatchNode.d.ts b/src-testing/src/nodes/accessors/BatchNode.d.ts new file mode 100644 index 000000000..49c989fdc --- /dev/null +++ b/src-testing/src/nodes/accessors/BatchNode.d.ts @@ -0,0 +1,14 @@ +import { BatchedMesh } from "../../objects/BatchedMesh.js"; +import Node from "../core/Node.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class BatchNode extends Node { + batchMesh: BatchedMesh; + + instanceColorNode: Node | null; + batchingIdNode: Node | null; + + constructor(batchMesh: BatchedMesh); +} + +export const batch: (batchMesh: BatchedMesh) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/BitangentNode.d.ts b/src-testing/src/nodes/accessors/BitangentNode.d.ts new file mode 100644 index 000000000..5a24e8eba --- /dev/null +++ b/src-testing/src/nodes/accessors/BitangentNode.d.ts @@ -0,0 +1,9 @@ +import MathNode from "../math/MathNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const bitangentGeometry: ShaderNodeObject; +export const bitangentLocal: ShaderNodeObject; +export const bitangentView: ShaderNodeObject; +export const bitangentWorld: ShaderNodeObject; +export const transformedBitangentView: ShaderNodeObject; +export const transformedBitangentWorld: ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/BufferAttributeNode.ts b/src-testing/src/nodes/accessors/BufferAttributeNode.ts new file mode 100644 index 000000000..e4a040deb --- /dev/null +++ b/src-testing/src/nodes/accessors/BufferAttributeNode.ts @@ -0,0 +1,134 @@ +import InputNode from '../core/InputNode.js'; +import { addNodeClass } from '../core/Node.js'; +import { varying } from '../core/VaryingNode.js'; +import { nodeObject, addNodeElement } from '../shadernode/ShaderNode.js'; + +import { InterleavedBufferAttribute } from '../../core/InterleavedBufferAttribute.js'; +import { InterleavedBuffer } from '../../core/InterleavedBuffer.js'; +import { StaticDrawUsage, DynamicDrawUsage } from '../../constants.js'; + +class BufferAttributeNode extends InputNode { + constructor(value, bufferType = null, bufferStride = 0, bufferOffset = 0) { + super(value, bufferType); + + this.isBufferNode = true; + + this.bufferType = bufferType; + this.bufferStride = bufferStride; + this.bufferOffset = bufferOffset; + + this.usage = StaticDrawUsage; + this.instanced = false; + + this.attribute = null; + + this.global = true; + + if (value && value.isBufferAttribute === true) { + this.attribute = value; + this.usage = value.usage; + this.instanced = value.isInstancedBufferAttribute; + } + } + + getHash(builder) { + if (this.bufferStride === 0 && this.bufferOffset === 0) { + let bufferData = builder.globalCache.getData(this.value); + + if (bufferData === undefined) { + bufferData = { + node: this, + }; + + builder.globalCache.setData(this.value, bufferData); + } + + return bufferData.node.uuid; + } + + return this.uuid; + } + + getNodeType(builder) { + if (this.bufferType === null) { + this.bufferType = builder.getTypeFromAttribute(this.attribute); + } + + return this.bufferType; + } + + setup(builder) { + if (this.attribute !== null) return; + + const type = this.getNodeType(builder); + const array = this.value; + const itemSize = builder.getTypeLength(type); + const stride = this.bufferStride || itemSize; + const offset = this.bufferOffset; + + const buffer = array.isInterleavedBuffer === true ? array : new InterleavedBuffer(array, stride); + const bufferAttribute = new InterleavedBufferAttribute(buffer, itemSize, offset); + + buffer.setUsage(this.usage); + + this.attribute = bufferAttribute; + this.attribute.isInstancedBufferAttribute = this.instanced; // @TODO: Add a possible: InstancedInterleavedBufferAttribute + } + + generate(builder) { + const nodeType = this.getNodeType(builder); + + const nodeAttribute = builder.getBufferAttributeFromNode(this, nodeType); + const propertyName = builder.getPropertyName(nodeAttribute); + + let output = null; + + if (builder.shaderStage === 'vertex' || builder.shaderStage === 'compute') { + this.name = propertyName; + + output = propertyName; + } else { + const nodeVarying = varying(this); + + output = nodeVarying.build(builder, nodeType); + } + + return output; + } + + getInputType(/*builder*/) { + return 'bufferAttribute'; + } + + setUsage(value) { + this.usage = value; + + if (this.attribute && this.attribute.isBufferAttribute === true) { + this.attribute.usage = value; + } + + return this; + } + + setInstanced(value) { + this.instanced = value; + + return this; + } +} + +export default BufferAttributeNode; + +export const bufferAttribute = (array, type, stride, offset) => + nodeObject(new BufferAttributeNode(array, type, stride, offset)); +export const dynamicBufferAttribute = (array, type, stride, offset) => + bufferAttribute(array, type, stride, offset).setUsage(DynamicDrawUsage); + +export const instancedBufferAttribute = (array, type, stride, offset) => + bufferAttribute(array, type, stride, offset).setInstanced(true); +export const instancedDynamicBufferAttribute = (array, type, stride, offset) => + dynamicBufferAttribute(array, type, stride, offset).setInstanced(true); + +addNodeElement('toAttribute', bufferNode => bufferAttribute(bufferNode.value)); + +addNodeClass('BufferAttributeNode', BufferAttributeNode); diff --git a/src-testing/src/nodes/accessors/BufferNode.d.ts b/src-testing/src/nodes/accessors/BufferNode.d.ts new file mode 100644 index 000000000..050b31ddd --- /dev/null +++ b/src-testing/src/nodes/accessors/BufferNode.d.ts @@ -0,0 +1,17 @@ +import UniformNode from "../core/UniformNode.js"; +import { NodeOrType, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class BufferNode extends UniformNode { + isBufferNode: true; + + bufferType: string; + bufferCount: number; + + constructor(value: unknown, bufferType: string, bufferCount?: number); +} + +export const buffer: ( + value: unknown, + nodeOrType: NodeOrType, + count: number, +) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/CameraNode.d.ts b/src-testing/src/nodes/accessors/CameraNode.d.ts new file mode 100644 index 000000000..dce969e9c --- /dev/null +++ b/src-testing/src/nodes/accessors/CameraNode.d.ts @@ -0,0 +1,15 @@ +import { Matrix3 } from "../../math/Matrix3.js"; +import { Matrix4 } from "../../math/Matrix4.js"; +import { Vector3 } from "../../math/Vector3.js"; +import UniformNode from "../core/UniformNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const cameraNear: ShaderNodeObject>; +export const cameraFar: ShaderNodeObject>; +export const cameraLogDepth: ShaderNodeObject>; +export const cameraProjectionMatrix: ShaderNodeObject>; +export const cameraProjectionMatrixInverse: ShaderNodeObject>; +export const cameraViewMatrix: ShaderNodeObject>; +export const cameraWorldMatrix: ShaderNodeObject>; +export const cameraNormalMatrix: ShaderNodeObject>; +export const cameraPosition: ShaderNodeObject>; diff --git a/src-testing/src/nodes/accessors/ClippingNode.d.ts b/src-testing/src/nodes/accessors/ClippingNode.d.ts new file mode 100644 index 000000000..4c1d42247 --- /dev/null +++ b/src-testing/src/nodes/accessors/ClippingNode.d.ts @@ -0,0 +1,16 @@ +import Node from "../core/Node.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export type ClippingNodeScope = typeof ClippingNode.ALPHA_TO_COVERAGE | typeof ClippingNode.DEFAULT; + +export default class ClippingNode extends Node { + scope: ClippingNodeScope; + + constructor(scope?: ClippingNodeScope); + + static ALPHA_TO_COVERAGE: "alphaToCoverage"; + static DEFAULT: "default"; +} + +export const clipping: () => ShaderNodeObject; +export const clippingAlpha: () => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/CubeTextureNode.d.ts b/src-testing/src/nodes/accessors/CubeTextureNode.d.ts new file mode 100644 index 000000000..6e3b8c19d --- /dev/null +++ b/src-testing/src/nodes/accessors/CubeTextureNode.d.ts @@ -0,0 +1,34 @@ +import { CubeTexture } from "../../textures/CubeTexture.js"; +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import TextureNode from "./TextureNode.js"; + +declare class CubeTextureNode extends TextureNode { + isCubeTextureNode: boolean; + uvNode: ShaderNodeObject | null; + levelNode: ShaderNodeObject | null; + + constructor( + value: CubeTexture, + uvNode?: ShaderNodeObject | null, + levelNode?: ShaderNodeObject | null, + biasNode?: ShaderNodeObject | null, + ); + + getDefaultUV(): Node; +} + +export default CubeTextureNode; + +export const cubeTexture: ( + value: CubeTexture, + uvNode?: NodeRepresentation, + levelNode?: NodeRepresentation, + biasNode?: NodeRepresentation, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + cubeTexture: typeof cubeTexture; + } +} diff --git a/src-testing/src/nodes/accessors/InstanceNode.d.ts b/src-testing/src/nodes/accessors/InstanceNode.d.ts new file mode 100644 index 000000000..bb54f0a81 --- /dev/null +++ b/src-testing/src/nodes/accessors/InstanceNode.d.ts @@ -0,0 +1,13 @@ +import { InstancedMesh } from "../../objects/InstancedMesh.js"; +import Node from "../core/Node.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class InstanceNode extends Node { + instanceMesh: InstancedMesh; + instanceMatrixNode: Node | null; + instanceColorNode: Node | null; + + constructor(instanceMesh: InstancedMesh); +} + +export const instance: (instanceMesh: InstancedMesh) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/MaterialNode.d.ts b/src-testing/src/nodes/accessors/MaterialNode.d.ts new file mode 100644 index 000000000..63f6c9180 --- /dev/null +++ b/src-testing/src/nodes/accessors/MaterialNode.d.ts @@ -0,0 +1,130 @@ +import { Vector2 } from "../../math/Vector2.js"; +import Node from "../core/Node.js"; +import UniformNode from "../core/UniformNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export type MaterialNodeScope = + | typeof MaterialNode.ALPHA_TEST + | typeof MaterialNode.COLOR + | typeof MaterialNode.OPACITY + | typeof MaterialNode.SHININESS + | typeof MaterialNode.SPECULAR + | typeof MaterialNode.SPECULAR_STRENGTH + | typeof MaterialNode.SPECULAR_INTENSITY + | typeof MaterialNode.SPECULAR_COLOR + | typeof MaterialNode.REFLECTIVITY + | typeof MaterialNode.ROUGHNESS + | typeof MaterialNode.METALNESS + | typeof MaterialNode.NORMAL + | typeof MaterialNode.CLEARCOAT + | typeof MaterialNode.CLEARCOAT_ROUGHNESS + | typeof MaterialNode.CLEARCOAT_NORMAL + | typeof MaterialNode.EMISSIVE + | typeof MaterialNode.ROTATION + | typeof MaterialNode.SHEEN + | typeof MaterialNode.SHEEN_ROUGHNESS + | typeof MaterialNode.ANISOTROPY + | typeof MaterialNode.IRIDESCENCE + | typeof MaterialNode.IRIDESCENCE_IOR + | typeof MaterialNode.IRIDESCENCE_THICKNESS + | typeof MaterialNode.IOR + | typeof MaterialNode.TRANSMISSION + | typeof MaterialNode.THICKNESS + | typeof MaterialNode.ATTENUATION_DISTANCE + | typeof MaterialNode.ATTENUATION_COLOR + | typeof MaterialNode.LINE_SCALE + | typeof MaterialNode.LINE_DASH_SIZE + | typeof MaterialNode.LINE_GAP_SIZE + | typeof MaterialNode.LINE_WIDTH + | typeof MaterialNode.LINE_DASH_OFFSET + | typeof MaterialNode.POINT_WIDTH + | typeof MaterialNode.DISPERSION + | typeof MaterialNode.LIGHT_MAP + | typeof MaterialNode.AO_MAP + | typeof MaterialNode.REFRACTION_RATIO; + +export default class MaterialNode extends Node { + static ALPHA_TEST: "alphaTest"; + static COLOR: "color"; + static OPACITY: "opacity"; + static SHININESS: "shininess"; + static SPECULAR: "specular"; + static SPECULAR_STRENGTH: "specularStrength"; + static SPECULAR_INTENSITY: "specularIntensity"; + static SPECULAR_COLOR: "specularColor"; + static REFLECTIVITY: "reflectivity"; + static ROUGHNESS: "roughness"; + static METALNESS: "metalness"; + static NORMAL: "normal"; + static CLEARCOAT: "clearcoat"; + static CLEARCOAT_ROUGHNESS: "clearcoatRoughness"; + static CLEARCOAT_NORMAL: "clearcoatNormal"; + static EMISSIVE: "emissive"; + static ROTATION: "rotation"; + static SHEEN: "sheen"; + static SHEEN_ROUGHNESS: "sheenRoughness"; + static ANISOTROPY: "anisotropy"; + static IRIDESCENCE: "iridescence"; + static IRIDESCENCE_IOR: "iridescenceIOR"; + static IRIDESCENCE_THICKNESS: "iridescenceThickness"; + static IOR: "ior"; + static TRANSMISSION: "transmission"; + static THICKNESS: "thickness"; + static ATTENUATION_DISTANCE: "attenuationDistance"; + static ATTENUATION_COLOR: "attenuationColor"; + static LINE_SCALE: "scale"; + static LINE_DASH_SIZE: "dashSize"; + static LINE_GAP_SIZE: "gapSize"; + static LINE_WIDTH: "linewidth"; + static LINE_DASH_OFFSET: "dashOffset"; + static POINT_WIDTH: "pointWidth"; + static DISPERSION: "dispersion"; + static LIGHT_MAP: "light"; + static AO_MAP: "ao"; + static REFRACTION_RATIO: "refractionRatio"; + + scope: MaterialNodeScope; + constructor(scope?: MaterialNodeScope); +} + +export const materialAlphaTest: ShaderNodeObject; +export const materialColor: ShaderNodeObject; +export const materialShininess: ShaderNodeObject; +export const materialEmissive: ShaderNodeObject; +export const materialOpacity: ShaderNodeObject; +export const materialSpecular: ShaderNodeObject; + +export const materialSpecularIntensity: ShaderNodeObject; +export const materialSpecularColor: ShaderNodeObject; + +export const materialSpecularStrength: ShaderNodeObject; +export const materialReflectivity: ShaderNodeObject; +export const materialRoughness: ShaderNodeObject; +export const materialMetalness: ShaderNodeObject; +export const materialNormal: ShaderNodeObject; +export const materialClearcoat: ShaderNodeObject; +export const materialClearcoatRoughness: ShaderNodeObject; +export const materialClearcoatNormal: ShaderNodeObject; +export const materialRotation: ShaderNodeObject; +export const materialSheen: ShaderNodeObject; +export const materialSheenRoughness: ShaderNodeObject; +export const materialAnisotropy: ShaderNodeObject; +export const materialIridescence: ShaderNodeObject; +export const materialIridescenceIOR: ShaderNodeObject; +export const materialIridescenceThickness: ShaderNodeObject; +export const materialTransmission: ShaderNodeObject; +export const materialThickness: ShaderNodeObject; +export const materialIOR: ShaderNodeObject; +export const materialAttenuationDistance: ShaderNodeObject; +export const materialAttenuationColor: ShaderNodeObject; +export const materialLineScale: ShaderNodeObject; +export const materialLineDashSize: ShaderNodeObject; +export const materialLineGapSize: ShaderNodeObject; +export const materialLineWidth: ShaderNodeObject; +export const materialLineDashOffset: ShaderNodeObject; +export const materialPointWidth: ShaderNodeObject; +export const materialDispersion: ShaderNodeObject; +export const materialLightMap: ShaderNodeObject; +export const materialAOMap: ShaderNodeObject; +export const materialRefractionRatio: ShaderNodeObject; +export const materialAnisotropyVector: ShaderNodeObject>; diff --git a/src-testing/src/nodes/accessors/MaterialProperties.d.ts b/src-testing/src/nodes/accessors/MaterialProperties.d.ts new file mode 100644 index 000000000..e1a0948e1 --- /dev/null +++ b/src-testing/src/nodes/accessors/MaterialProperties.d.ts @@ -0,0 +1,4 @@ +import UniformNode from "../core/UniformNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const materialRefractionRatio: ShaderNodeObject>; diff --git a/src-testing/src/nodes/accessors/MaterialReferenceNode.d.ts b/src-testing/src/nodes/accessors/MaterialReferenceNode.d.ts new file mode 100644 index 000000000..061228d29 --- /dev/null +++ b/src-testing/src/nodes/accessors/MaterialReferenceNode.d.ts @@ -0,0 +1,15 @@ +import { Material } from "../../materials/Material.js"; +import { NodeOrType, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import ReferenceNode from "./ReferenceNode.js"; + +export default class MaterialReferenceNode extends ReferenceNode { + readonly isMaterialReferenceNode: true; + + constructor(property: string, inputType: string, material?: Material | null); +} + +export const materialReference: ( + name: string, + nodeOrType: NodeOrType, + material?: Material | null, +) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/ModelNode.d.ts b/src-testing/src/nodes/accessors/ModelNode.d.ts new file mode 100644 index 000000000..a7dfbcfdb --- /dev/null +++ b/src-testing/src/nodes/accessors/ModelNode.d.ts @@ -0,0 +1,20 @@ +import { Matrix4 } from "../../math/Matrix4.js"; +import { UniformNode } from "../Nodes.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import Object3DNode from "./Object3DNode.js"; + +/** + * Similar to {@link Object3DNode} but the object comes from {@link NodeFrame} + */ +export default class ModelNode extends Object3DNode { + constructor(scope?: string); +} + +export const modelDirection: ShaderNodeObject; +export const modelViewMatrix: ShaderNodeObject; +export const modelNormalMatrix: ShaderNodeObject; +export const modelWorldMatrix: ShaderNodeObject; +export const modelPosition: ShaderNodeObject; +export const modelScale: ShaderNodeObject; +export const modelViewPosition: ShaderNodeObject; +export const modelWorldMatrixInverse: ShaderNodeObject>; diff --git a/src-testing/src/nodes/accessors/ModelViewProjectionNode.d.ts b/src-testing/src/nodes/accessors/ModelViewProjectionNode.d.ts new file mode 100644 index 000000000..dee451a19 --- /dev/null +++ b/src-testing/src/nodes/accessors/ModelViewProjectionNode.d.ts @@ -0,0 +1,8 @@ +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class ModelViewProjectionNode extends Node { + constructor(positionNode?: Node); +} + +export const modelViewProjection: (position?: NodeRepresentation) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/NormalNode.d.ts b/src-testing/src/nodes/accessors/NormalNode.d.ts new file mode 100644 index 000000000..65300ef16 --- /dev/null +++ b/src-testing/src/nodes/accessors/NormalNode.d.ts @@ -0,0 +1,12 @@ +import AttributeNode from "../core/AttributeNode.js"; +import PropertyNode from "../core/PropertyNode.js"; +import VarNode from "../core/VarNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const normalGeometry: ShaderNodeObject; +export const normalLocal: ShaderNodeObject; +export const normalView: ShaderNodeObject; +export const normalWorld: ShaderNodeObject; +export const transformedNormalView: ShaderNodeObject; +export const transformedNormalWorld: ShaderNodeObject; +export const transformedClearcoatNormalView: ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/Object3DNode.d.ts b/src-testing/src/nodes/accessors/Object3DNode.d.ts new file mode 100644 index 000000000..b16f9b00e --- /dev/null +++ b/src-testing/src/nodes/accessors/Object3DNode.d.ts @@ -0,0 +1,26 @@ +import { Object3D } from "../../core/Object3D.js"; +import Node from "../core/Node.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class Object3DNode extends Node { + scope: string; + object3d: Object3D | null; + + constructor(scope?: string, object3d?: Object3D | null); + + static VIEW_MATRIX: "viewMatrix"; + static NORMAL_MATRIX: "normalMatrix"; + static WORLD_MATRIX: "worldMatrix"; + static POSITION: "position"; + static SCALE: "scale"; + static VIEW_POSITION: "viewPosition"; + static DIRECTION: "direction"; +} + +export const objectDirection: (object3d: Object3D) => ShaderNodeObject; +export const objectViewMatrix: (object3d: Object3D) => ShaderNodeObject; +export const objectNormalMatrix: (object3d: Object3D) => ShaderNodeObject; +export const objectWorldMatrix: (object3d: Object3D) => ShaderNodeObject; +export const objectPosition: (object3d: Object3D) => ShaderNodeObject; +export const objectScale: (object3d: Object3D) => ShaderNodeObject; +export const objectViewPosition: (object3d: Object3D) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/PointUVNode.d.ts b/src-testing/src/nodes/accessors/PointUVNode.d.ts new file mode 100644 index 000000000..017a43ffa --- /dev/null +++ b/src-testing/src/nodes/accessors/PointUVNode.d.ts @@ -0,0 +1,10 @@ +import Node from "../core/Node.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class PointUVNode extends Node { + isPointUVNode: true; + + constructor(); +} + +export const pointUV: ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/PositionNode.d.ts b/src-testing/src/nodes/accessors/PositionNode.d.ts new file mode 100644 index 000000000..ae7b31b21 --- /dev/null +++ b/src-testing/src/nodes/accessors/PositionNode.d.ts @@ -0,0 +1,9 @@ +import Node from "../core/Node.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const positionGeometry: ShaderNodeObject; +export const positionLocal: ShaderNodeObject; +export const positionWorld: ShaderNodeObject; +export const positionWorldDirection: ShaderNodeObject; +export const positionView: ShaderNodeObject; +export const positionViewDirection: ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/ReferenceNode.d.ts b/src-testing/src/nodes/accessors/ReferenceNode.d.ts new file mode 100644 index 000000000..f094d8b41 --- /dev/null +++ b/src-testing/src/nodes/accessors/ReferenceNode.d.ts @@ -0,0 +1,27 @@ +import Node from "../core/Node.js"; +import { NodeOrType, NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class ReferenceNode extends Node { + property: string; + + uniformType: string; + + object: T; + count: number | null; + + properties: string[]; + reference: T | null; + node: Node | null; + + constructor(property: string, uniformType: string, object?: T | null, count?: number | null); + + setNodeType(uniformType: string): void; +} + +export const reference: (name: string, nodeOrType: NodeOrType, object: T) => ShaderNodeObject>; +export const referenceBuffer: ( + name: string, + nodeOrType: NodeOrType, + count: NodeRepresentation, + object: T, +) => ShaderNodeObject>; diff --git a/src-testing/src/nodes/accessors/ReflectVectorNode.d.ts b/src-testing/src/nodes/accessors/ReflectVectorNode.d.ts new file mode 100644 index 000000000..31c6a1310 --- /dev/null +++ b/src-testing/src/nodes/accessors/ReflectVectorNode.d.ts @@ -0,0 +1,9 @@ +import Node from "../core/Node.js"; +import VarNode from "../core/VarNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const reflectView: ShaderNodeObject; +export const refractView: ShaderNodeObject; + +export const reflectVector: ShaderNodeObject; +export const refractVector: ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/RendererReferenceNode.d.ts b/src-testing/src/nodes/accessors/RendererReferenceNode.d.ts new file mode 100644 index 000000000..8e6c47130 --- /dev/null +++ b/src-testing/src/nodes/accessors/RendererReferenceNode.d.ts @@ -0,0 +1,15 @@ +import Renderer from "../../renderers/common/Renderer.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import ReferenceNode from "./ReferenceNode.js"; + +export default class RendererReferenceNode extends ReferenceNode { + renderer: Renderer | null; + + constructor(property: string, inputType: string, renderer?: Renderer | null); +} + +export const rendererReference: ( + name: string, + type: string, + renderer?: Renderer | null, +) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/SkinningNode.d.ts b/src-testing/src/nodes/accessors/SkinningNode.d.ts new file mode 100644 index 000000000..11b612cfb --- /dev/null +++ b/src-testing/src/nodes/accessors/SkinningNode.d.ts @@ -0,0 +1,20 @@ +import { SkinnedMesh } from "../../objects/SkinnedMesh.js"; +import Node from "../core/Node.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class SkinningNode extends Node { + skinnedMesh: SkinnedMesh; + useReference: boolean; + + skinIndexNode: Node; + skinWeightNode: Node; + + bindMatrixNode: Node; + bindMatrixInverseNode: Node; + boneMatricesNode: Node; + + constructor(skinnedMesh: SkinnedMesh, useReference?: boolean); +} + +export const skinning: (skinnedMesh: SkinnedMesh) => ShaderNodeObject; +export const skinningReference: (skinnedMesh: SkinnedMesh) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/StorageBufferNode.d.ts b/src-testing/src/nodes/accessors/StorageBufferNode.d.ts new file mode 100644 index 000000000..3d4d64ff7 --- /dev/null +++ b/src-testing/src/nodes/accessors/StorageBufferNode.d.ts @@ -0,0 +1,38 @@ +import StorageBufferAttribute from "../../renderers/common/StorageBufferAttribute.js"; +import StorageInstancedBufferAttribute from "../../renderers/common/StorageInstancedBufferAttribute.js"; +import { GPUBufferBindingType } from "../../renderers/webgpu/utils/WebGPUConstants.js"; +import { NodeOrType, NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import StorageArrayElementNode from "../utils/StoargeArrayElementNode.js"; +import BufferNode from "./BufferNode.js"; + +export default class StorageBufferNode extends BufferNode { + readonly isStorageBufferNode: true; + bufferObject: boolean; + + access: GPUBufferBindingType; + + constructor( + value: StorageBufferAttribute | StorageInstancedBufferAttribute, + bufferType: string, + bufferCount?: number, + ); + + element(indexNode: NodeRepresentation): ShaderNodeObject; + + setBufferObject(value: boolean): this; + + setAccess(value: GPUBufferBindingType): this; + + toReadOnly(): this; +} + +export const storage: ( + value: StorageBufferAttribute | StorageInstancedBufferAttribute, + nodeOrType: NodeOrType, + count: number, +) => ShaderNodeObject; +export const storageObject: ( + value: StorageBufferAttribute | StorageInstancedBufferAttribute, + nodeOrType: NodeOrType, + count: number, +) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/StorageTextureNode.d.ts b/src-testing/src/nodes/accessors/StorageTextureNode.d.ts new file mode 100644 index 000000000..c1e810190 --- /dev/null +++ b/src-testing/src/nodes/accessors/StorageTextureNode.d.ts @@ -0,0 +1,40 @@ +import { GPUStorageTextureAccess } from "../../renderers/webgpu/utils/WebGPUConstants.js"; +import { Texture } from "../../textures/Texture.js"; +import Node from "../core/Node.js"; +import NodeBuilder from "../core/NodeBuilder.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import TextureNode from "./TextureNode.js"; + +export default class StorageTextureNode extends TextureNode { + storeNode: Node | null; + + readonly isStorageTextureNode: true; + + access: GPUStorageTextureAccess; + + constructor( + value: Texture, + uvNode?: ShaderNodeObject | null, + storeNode?: Node | null, + ); + + setAccess(value: GPUStorageTextureAccess): this; + + toReadOnly(): this; + + toWriteOnly(): this; + + generateStore(builder: NodeBuilder): void; +} + +export const storageTexture: ( + value: Texture, + uvNode?: NodeRepresentation, + storeNode?: NodeRepresentation, +) => ShaderNodeObject; + +export const textureStore: ( + value: Texture, + uvNode?: NodeRepresentation, + storeNode?: NodeRepresentation, +) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/TangentNode.d.ts b/src-testing/src/nodes/accessors/TangentNode.d.ts new file mode 100644 index 000000000..0dac9e0dc --- /dev/null +++ b/src-testing/src/nodes/accessors/TangentNode.d.ts @@ -0,0 +1,12 @@ +import AttributeNode from "../core/AttributeNode.js"; +import VarNode from "../core/VarNode.js"; +import VaryingNode from "../core/VaryingNode.js"; +import MathNode from "../math/MathNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const tangentGeometry: ShaderNodeObject; +export const tangentLocal: ShaderNodeObject; +export const tangentView: ShaderNodeObject; +export const tangentWorld: ShaderNodeObject; +export const transformedTangentView: ShaderNodeObject; +export const transformedTangentWorld: ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/Texture3DNode.d.ts b/src-testing/src/nodes/accessors/Texture3DNode.d.ts new file mode 100644 index 000000000..eed254d2e --- /dev/null +++ b/src-testing/src/nodes/accessors/Texture3DNode.d.ts @@ -0,0 +1,17 @@ +import { CubeTexture } from "../../textures/CubeTexture.js"; +import { Texture } from "../../textures/Texture.js"; +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import TextureNode from "./TextureNode.js"; + +export default class Texture3DNode extends TextureNode { + readonly isTexture3DNode: true; + + constructor(value: Texture, uvNode?: ShaderNodeObject | null, levelNode?: ShaderNodeObject | null); +} + +export const texture3D: ( + value: Texture, + uvNode?: NodeRepresentation, + levelNode?: NodeRepresentation, +) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/TextureBicubicNode.d.ts b/src-testing/src/nodes/accessors/TextureBicubicNode.d.ts new file mode 100644 index 000000000..947aa6e01 --- /dev/null +++ b/src-testing/src/nodes/accessors/TextureBicubicNode.d.ts @@ -0,0 +1,18 @@ +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class TextureBicubicNode extends TempNode { + textureNode: Node; + blurNode: Node; + + constructor(textureNode: Node, blurNode?: Node); +} + +export const textureBicubic: (textureNode: Node, blurNode?: NodeRepresentation) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + bicubic: typeof textureBicubic; + } +} diff --git a/src-testing/src/nodes/accessors/TextureNode.ts b/src-testing/src/nodes/accessors/TextureNode.ts new file mode 100644 index 000000000..163e3ccc5 --- /dev/null +++ b/src-testing/src/nodes/accessors/TextureNode.ts @@ -0,0 +1,369 @@ +import UniformNode, { uniform } from '../core/UniformNode.js'; +import { uv } from './UVNode.js'; +import { textureSize } from './TextureSizeNode.js'; +import { colorSpaceToLinear } from '../display/ColorSpaceNode.js'; +import { expression } from '../code/ExpressionNode.js'; +import { addNodeClass } from '../core/Node.js'; +import { maxMipLevel } from '../utils/MaxMipLevelNode.js'; +import { addNodeElement, nodeProxy, vec3, nodeObject } from '../shadernode/ShaderNode.js'; +import { NodeUpdateType } from '../core/constants.js'; + +import { IntType, UnsignedIntType } from '../../constants.js'; + +class TextureNode extends UniformNode { + constructor(value, uvNode = null, levelNode = null, biasNode = null) { + super(value); + + this.isTextureNode = true; + + this.uvNode = uvNode; + this.levelNode = levelNode; + this.biasNode = biasNode; + this.compareNode = null; + this.depthNode = null; + this.gradNode = null; + + this.sampler = true; + this.updateMatrix = false; + this.updateType = NodeUpdateType.NONE; + + this.referenceNode = null; + + this._value = value; + this._matrixUniform = null; + + this.setUpdateMatrix(uvNode === null); + } + + set value(value) { + if (this.referenceNode) { + this.referenceNode.value = value; + } else { + this._value = value; + } + } + + get value() { + return this.referenceNode ? this.referenceNode.value : this._value; + } + + getUniformHash(/*builder*/) { + return this.value.uuid; + } + + getNodeType(/*builder*/) { + if (this.value.isDepthTexture === true) return 'float'; + + if (this.value.type === UnsignedIntType) { + return 'uvec4'; + } else if (this.value.type === IntType) { + return 'ivec4'; + } + + return 'vec4'; + } + + getInputType(/*builder*/) { + return 'texture'; + } + + getDefaultUV() { + return uv(this.value.channel); + } + + updateReference(/*state*/) { + return this.value; + } + + getTransformedUV(uvNode) { + if (this._matrixUniform === null) this._matrixUniform = uniform(this.value.matrix); + + return this._matrixUniform.mul(vec3(uvNode, 1)).xy; + } + + setUpdateMatrix(value) { + this.updateMatrix = value; + this.updateType = value ? NodeUpdateType.FRAME : NodeUpdateType.NONE; + + return this; + } + + setupUV(builder, uvNode) { + const texture = this.value; + + if ( + builder.isFlipY() && + (texture.isRenderTargetTexture === true || + texture.isFramebufferTexture === true || + texture.isDepthTexture === true) + ) { + uvNode = uvNode.setY(uvNode.y.oneMinus()); + } + + return uvNode; + } + + setup(builder) { + const properties = builder.getNodeProperties(this); + properties.referenceNode = this.referenceNode; + + // + + let uvNode = this.uvNode; + + if ((uvNode === null || builder.context.forceUVContext === true) && builder.context.getUV) { + uvNode = builder.context.getUV(this); + } + + if (!uvNode) uvNode = this.getDefaultUV(); + + if (this.updateMatrix === true) { + uvNode = this.getTransformedUV(uvNode); + } + + uvNode = this.setupUV(builder, uvNode); + + // + + let levelNode = this.levelNode; + + if (levelNode === null && builder.context.getTextureLevel) { + levelNode = builder.context.getTextureLevel(this); + } + + // + + properties.uvNode = uvNode; + properties.levelNode = levelNode; + properties.biasNode = this.biasNode; + properties.compareNode = this.compareNode; + properties.gradNode = this.gradNode; + properties.depthNode = this.depthNode; + } + + generateUV(builder, uvNode) { + return uvNode.build(builder, this.sampler === true ? 'vec2' : 'ivec2'); + } + + generateSnippet( + builder, + textureProperty, + uvSnippet, + levelSnippet, + biasSnippet, + depthSnippet, + compareSnippet, + gradSnippet, + ) { + const texture = this.value; + + let snippet; + + if (levelSnippet) { + snippet = builder.generateTextureLevel(texture, textureProperty, uvSnippet, levelSnippet, depthSnippet); + } else if (biasSnippet) { + snippet = builder.generateTextureBias(texture, textureProperty, uvSnippet, biasSnippet, depthSnippet); + } else if (gradSnippet) { + snippet = builder.generateTextureGrad(texture, textureProperty, uvSnippet, gradSnippet, depthSnippet); + } else if (compareSnippet) { + snippet = builder.generateTextureCompare(texture, textureProperty, uvSnippet, compareSnippet, depthSnippet); + } else if (this.sampler === false) { + snippet = builder.generateTextureLoad(texture, textureProperty, uvSnippet, depthSnippet); + } else { + snippet = builder.generateTexture(texture, textureProperty, uvSnippet, depthSnippet); + } + + return snippet; + } + + generate(builder, output) { + const properties = builder.getNodeProperties(this); + + const texture = this.value; + + if (!texture || texture.isTexture !== true) { + throw new Error('TextureNode: Need a three.js texture.'); + } + + const textureProperty = super.generate(builder, 'property'); + + if (output === 'sampler') { + return textureProperty + '_sampler'; + } else if (builder.isReference(output)) { + return textureProperty; + } else { + const nodeData = builder.getDataFromNode(this); + + let propertyName = nodeData.propertyName; + + if (propertyName === undefined) { + const { uvNode, levelNode, biasNode, compareNode, depthNode, gradNode } = properties; + + const uvSnippet = this.generateUV(builder, uvNode); + const levelSnippet = levelNode ? levelNode.build(builder, 'float') : null; + const biasSnippet = biasNode ? biasNode.build(builder, 'float') : null; + const depthSnippet = depthNode ? depthNode.build(builder, 'int') : null; + const compareSnippet = compareNode ? compareNode.build(builder, 'float') : null; + const gradSnippet = gradNode + ? [gradNode[0].build(builder, 'vec2'), gradNode[1].build(builder, 'vec2')] + : null; + + const nodeVar = builder.getVarFromNode(this); + + propertyName = builder.getPropertyName(nodeVar); + + const snippet = this.generateSnippet( + builder, + textureProperty, + uvSnippet, + levelSnippet, + biasSnippet, + depthSnippet, + compareSnippet, + gradSnippet, + ); + + builder.addLineFlowCode(`${propertyName} = ${snippet}`); + + nodeData.snippet = snippet; + nodeData.propertyName = propertyName; + } + + let snippet = propertyName; + const nodeType = this.getNodeType(builder); + + if (builder.needsColorSpaceToLinear(texture)) { + snippet = colorSpaceToLinear(expression(snippet, nodeType), texture.colorSpace) + .setup(builder) + .build(builder, nodeType); + } + + return builder.format(snippet, nodeType, output); + } + } + + setSampler(value) { + this.sampler = value; + + return this; + } + + getSampler() { + return this.sampler; + } + + // @TODO: Move to TSL + + uv(uvNode) { + const textureNode = this.clone(); + textureNode.uvNode = nodeObject(uvNode); + textureNode.referenceNode = this; + + return nodeObject(textureNode); + } + + blur(amountNode) { + const textureNode = this.clone(); + textureNode.biasNode = nodeObject(amountNode).mul(maxMipLevel(textureNode)); + textureNode.referenceNode = this; + + return nodeObject(textureNode); + } + + level(levelNode) { + const textureNode = this.clone(); + textureNode.levelNode = nodeObject(levelNode); + textureNode.referenceNode = this; + + return nodeObject(textureNode); + } + + size(levelNode) { + return textureSize(this, levelNode); + } + + bias(biasNode) { + const textureNode = this.clone(); + textureNode.biasNode = nodeObject(biasNode); + textureNode.referenceNode = this; + + return nodeObject(textureNode); + } + + compare(compareNode) { + const textureNode = this.clone(); + textureNode.compareNode = nodeObject(compareNode); + textureNode.referenceNode = this; + + return nodeObject(textureNode); + } + + grad(gradNodeX, gradNodeY) { + const textureNode = this.clone(); + textureNode.gradNode = [nodeObject(gradNodeX), nodeObject(gradNodeY)]; + + textureNode.referenceNode = this; + + return nodeObject(textureNode); + } + + depth(depthNode) { + const textureNode = this.clone(); + textureNode.depthNode = nodeObject(depthNode); + textureNode.referenceNode = this; + + return nodeObject(textureNode); + } + + // -- + + serialize(data) { + super.serialize(data); + + data.value = this.value.toJSON(data.meta).uuid; + data.sampler = this.sampler; + data.updateMatrix = this.updateMatrix; + data.updateType = this.updateType; + } + + deserialize(data) { + super.deserialize(data); + + this.value = data.meta.textures[data.value]; + this.sampler = data.sampler; + this.updateMatrix = data.updateMatrix; + this.updateType = data.updateType; + } + + update() { + const texture = this.value; + const matrixUniform = this._matrixUniform; + + if (matrixUniform !== null) matrixUniform.value = texture.matrix; + + if (texture.matrixAutoUpdate === true) { + texture.updateMatrix(); + } + } + + clone() { + const newNode = new this.constructor(this.value, this.uvNode, this.levelNode, this.biasNode); + newNode.sampler = this.sampler; + + return newNode; + } +} + +export default TextureNode; + +export const texture = nodeProxy(TextureNode); +export const textureLoad = (...params) => texture(...params).setSampler(false); + +//export const textureLevel = ( value, uv, level ) => texture( value, uv ).level( level ); + +export const sampler = aTexture => (aTexture.isNode === true ? aTexture : texture(aTexture)).convert('sampler'); + +addNodeElement('texture', texture); +//addNodeElement( 'textureLevel', textureLevel ); + +addNodeClass('TextureNode', TextureNode); diff --git a/src-testing/src/nodes/accessors/UVNode.d.ts b/src-testing/src/nodes/accessors/UVNode.d.ts new file mode 100644 index 000000000..4fbe07f79 --- /dev/null +++ b/src-testing/src/nodes/accessors/UVNode.d.ts @@ -0,0 +1,4 @@ +import AttributeNode from "../core/AttributeNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const uv: (index?: number) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/UniformArrayNode.d.ts b/src-testing/src/nodes/accessors/UniformArrayNode.d.ts new file mode 100644 index 000000000..6d02c0272 --- /dev/null +++ b/src-testing/src/nodes/accessors/UniformArrayNode.d.ts @@ -0,0 +1,30 @@ +import Node from "../core/Node.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import ArrayElementNode from "../utils/ArrayElementNode.js"; +import BufferNode from "./BufferNode.js"; + +declare class UniformArrayElementNode extends ArrayElementNode { + constructor(arrayBuffer: Node, indexNode: Node); +} + +declare class UniformArrayNode extends BufferNode { + array: unknown[]; + elementType: string | null; + + readonly isArrayBufferNode: true; + + constructor(value: unknown[], elementType?: string | null); + + getElementLength(): number; + + element(indexNode: number): ShaderNodeObject; +} + +export default UniformArrayNode; + +export const uniformArray: (values: unknown[], nodeType?: string | null) => ShaderNodeObject; + +/** + * @deprecated uniforms() has been renamed to uniformArray(). + */ +export const uniforms: (values: unknown[], nodeType?: string | null) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/UserDataNode.d.ts b/src-testing/src/nodes/accessors/UserDataNode.d.ts new file mode 100644 index 000000000..e2204e302 --- /dev/null +++ b/src-testing/src/nodes/accessors/UserDataNode.d.ts @@ -0,0 +1,15 @@ +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import ReferenceNode from "./ReferenceNode.js"; + +export type NodeUserData = Record; + +export default class UserDataNode extends ReferenceNode { + userData: NodeUserData | null; + constructor(property: string, inputType: string, userData?: NodeUserData | null); +} + +export const userData: ( + name: string, + inputType: string, + userData?: NodeUserData, +) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/VertexColorNode.d.ts b/src-testing/src/nodes/accessors/VertexColorNode.d.ts new file mode 100644 index 000000000..784ca3c5a --- /dev/null +++ b/src-testing/src/nodes/accessors/VertexColorNode.d.ts @@ -0,0 +1,12 @@ +import AttributeNode from "../core/AttributeNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class VertexColorNode extends AttributeNode { + readonly isVertexColorNode: true; + + index: number; + + constructor(index?: number); +} + +export const vertexColor: (index?: number) => ShaderNodeObject; diff --git a/src-testing/src/nodes/code/CodeNode.ts b/src-testing/src/nodes/code/CodeNode.ts new file mode 100644 index 000000000..063475643 --- /dev/null +++ b/src-testing/src/nodes/code/CodeNode.ts @@ -0,0 +1,66 @@ +import Node, { addNodeClass } from '../core/Node.js'; +import { nodeProxy } from '../shadernode/ShaderNode.js'; + +class CodeNode extends Node { + constructor(code = '', includes = [], language = '') { + super('code'); + + this.isCodeNode = true; + + this.code = code; + this.language = language; + + this.includes = includes; + } + + isGlobal() { + return true; + } + + setIncludes(includes) { + this.includes = includes; + + return this; + } + + getIncludes(/*builder*/) { + return this.includes; + } + + generate(builder) { + const includes = this.getIncludes(builder); + + for (const include of includes) { + include.build(builder); + } + + const nodeCode = builder.getCodeFromNode(this, this.getNodeType(builder)); + nodeCode.code = this.code; + + return nodeCode.code; + } + + serialize(data) { + super.serialize(data); + + data.code = this.code; + data.language = this.language; + } + + deserialize(data) { + super.deserialize(data); + + this.code = data.code; + this.language = data.language; + } +} + +export default CodeNode; + +export const code = nodeProxy(CodeNode); + +export const js = (src, includes) => code(src, includes, 'js'); +export const wgsl = (src, includes) => code(src, includes, 'wgsl'); +export const glsl = (src, includes) => code(src, includes, 'glsl'); + +addNodeClass('CodeNode', CodeNode); diff --git a/src-testing/src/nodes/code/ExpressionNode.d.ts b/src-testing/src/nodes/code/ExpressionNode.d.ts new file mode 100644 index 000000000..20683848a --- /dev/null +++ b/src-testing/src/nodes/code/ExpressionNode.d.ts @@ -0,0 +1,9 @@ +import TempNode from "../core/TempNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class ExpressionNode extends TempNode { + snipped: string; /* sic */ + constructor(snipped?: string, nodeType?: string); +} + +export const expression: (snipped?: string, nodeType?: string) => ShaderNodeObject; diff --git a/src-testing/src/nodes/code/FunctionCallNode.d.ts b/src-testing/src/nodes/code/FunctionCallNode.d.ts new file mode 100644 index 000000000..d3766c577 --- /dev/null +++ b/src-testing/src/nodes/code/FunctionCallNode.d.ts @@ -0,0 +1,25 @@ +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { ProxiedObject, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import FunctionNode, { FunctionNodeArguments } from "./FunctionNode.js"; + +export default class FunctionCallNode

extends TempNode { + functionNode: FunctionNode

; + parameters: { [name: string]: Node }; + + constructor(functionNode?: FunctionNode

, parameters?: P); + + setParameters(parameters: P): this; + getParameters(): P; +} + +export const call:

( + functionNode?: FunctionNode

, + parameters?: ProxiedObject

, +) => ShaderNodeObject>; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + call: typeof call; + } +} diff --git a/src-testing/src/nodes/code/FunctionNode.ts b/src-testing/src/nodes/code/FunctionNode.ts new file mode 100644 index 000000000..feeb5a55c --- /dev/null +++ b/src-testing/src/nodes/code/FunctionNode.ts @@ -0,0 +1,100 @@ +import CodeNode from './CodeNode.js'; +import { addNodeClass } from '../core/Node.js'; +import { nodeObject } from '../shadernode/ShaderNode.js'; + +class FunctionNode extends CodeNode { + constructor(code = '', includes = [], language = '') { + super(code, includes, language); + + this.keywords = {}; + } + + getNodeType(builder) { + return this.getNodeFunction(builder).type; + } + + getInputs(builder) { + return this.getNodeFunction(builder).inputs; + } + + getNodeFunction(builder) { + const nodeData = builder.getDataFromNode(this); + + let nodeFunction = nodeData.nodeFunction; + + if (nodeFunction === undefined) { + nodeFunction = builder.parser.parseFunction(this.code); + + nodeData.nodeFunction = nodeFunction; + } + + return nodeFunction; + } + + generate(builder, output) { + super.generate(builder); + + const nodeFunction = this.getNodeFunction(builder); + + const name = nodeFunction.name; + const type = nodeFunction.type; + + const nodeCode = builder.getCodeFromNode(this, type); + + if (name !== '') { + // use a custom property name + + nodeCode.name = name; + } + + const propertyName = builder.getPropertyName(nodeCode); + + let code = this.getNodeFunction(builder).getCode(propertyName); + + const keywords = this.keywords; + const keywordsProperties = Object.keys(keywords); + + if (keywordsProperties.length > 0) { + for (const property of keywordsProperties) { + const propertyRegExp = new RegExp(`\\b${property}\\b`, 'g'); + const nodeProperty = keywords[property].build(builder, 'property'); + + code = code.replace(propertyRegExp, nodeProperty); + } + } + + nodeCode.code = code + '\n'; + + if (output === 'property') { + return propertyName; + } else { + return builder.format(`${propertyName}()`, type, output); + } + } +} + +export default FunctionNode; + +const nativeFn = (code, includes = [], language = '') => { + for (let i = 0; i < includes.length; i++) { + const include = includes[i]; + + // TSL Function: glslFn, wgslFn + + if (typeof include === 'function') { + includes[i] = include.functionNode; + } + } + + const functionNode = nodeObject(new FunctionNode(code, includes, language)); + + const fn = (...params) => functionNode.call(...params); + fn.functionNode = functionNode; + + return fn; +}; + +export const glslFn = (code, includes) => nativeFn(code, includes, 'glsl'); +export const wgslFn = (code, includes) => nativeFn(code, includes, 'wgsl'); + +addNodeClass('FunctionNode', FunctionNode); diff --git a/src-testing/src/nodes/core/AssignNode.d.ts b/src-testing/src/nodes/core/AssignNode.d.ts new file mode 100644 index 000000000..e6809fe50 --- /dev/null +++ b/src-testing/src/nodes/core/AssignNode.d.ts @@ -0,0 +1,18 @@ +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import Node from "./Node.js"; +import NodeBuilder from "./NodeBuilder.js"; +import TempNode from "./TempNode.js"; + +export default class AssignNode extends TempNode { + constructor(targetNode: Node, sourceNode: Node); + + needsSplitAssign(builder: NodeBuilder): boolean; +} + +export const assign: (targetNode: NodeRepresentation, sourceNode: NodeRepresentation) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + assign: typeof assign; + } +} diff --git a/src-testing/src/nodes/core/AttributeNode.d.ts b/src-testing/src/nodes/core/AttributeNode.d.ts new file mode 100644 index 000000000..e2d37c972 --- /dev/null +++ b/src-testing/src/nodes/core/AttributeNode.d.ts @@ -0,0 +1,19 @@ +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import Node from "./Node.js"; +import NodeBuilder from "./NodeBuilder.js"; + +export default class AttributeNode extends Node { + defaultNode: Node | null; + + constructor(attributeName: string, nodeType?: string | null, defaultNode?: Node | null); + + setAttributeName(attributeName: string): this; + + getAttributeName(builder: NodeBuilder): string; +} + +export const attribute: ( + name: string, + nodeType?: string | null, + defaultNode?: NodeRepresentation, +) => ShaderNodeObject; diff --git a/src-testing/src/nodes/core/BypassNode.d.ts b/src-testing/src/nodes/core/BypassNode.d.ts new file mode 100644 index 000000000..e9dbef2cf --- /dev/null +++ b/src-testing/src/nodes/core/BypassNode.d.ts @@ -0,0 +1,18 @@ +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import Node from "./Node.js"; + +export default class BypassNode extends Node { + isBypassNode: true; + outputNode: Node; + callNode: Node; + + constructor(returnNode: Node, callNode: Node); +} + +export const bypass: (returnNode: NodeRepresentation, callNode: NodeRepresentation) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + bypass: typeof bypass; + } +} diff --git a/src-testing/src/nodes/core/CacheNode.d.ts b/src-testing/src/nodes/core/CacheNode.d.ts new file mode 100644 index 000000000..d3e4748e8 --- /dev/null +++ b/src-testing/src/nodes/core/CacheNode.d.ts @@ -0,0 +1,20 @@ +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import Node from "./Node.js"; +import NodeCache from "./NodeCache.js"; + +export default class CacheNode extends Node { + node: Node; + parent: boolean; + + readonly isCacheNode: true; + + constructor(node: Node, parent?: boolean); +} + +export const cache: (node: Node, cache?: NodeCache) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + cache: typeof cache; + } +} diff --git a/src-testing/src/nodes/core/ConstNode.d.ts b/src-testing/src/nodes/core/ConstNode.d.ts new file mode 100644 index 000000000..f866b0c0c --- /dev/null +++ b/src-testing/src/nodes/core/ConstNode.d.ts @@ -0,0 +1,9 @@ +import InputNode from "./InputNode.js"; +import NodeBuilder from "./NodeBuilder.js"; + +export default class ConstNode extends InputNode { + isConstNode: true; + constructor(value: Value, nodeType?: string | null); + + generateConst(builder: NodeBuilder): string; +} diff --git a/src-testing/src/nodes/core/ContextNode.ts b/src-testing/src/nodes/core/ContextNode.ts new file mode 100644 index 000000000..fcd488eba --- /dev/null +++ b/src-testing/src/nodes/core/ContextNode.ts @@ -0,0 +1,55 @@ +import Node, { addNodeClass } from './Node.js'; +import { addNodeElement, nodeProxy } from '../shadernode/ShaderNode.js'; + +class ContextNode extends Node { + constructor(node, context = {}) { + super(); + + this.isContextNode = true; + + this.node = node; + this.context = context; + } + + getNodeType(builder) { + return this.node.getNodeType(builder); + } + + analyze(builder) { + this.node.build(builder); + } + + setup(builder) { + const previousContext = builder.getContext(); + + builder.setContext({ ...builder.context, ...this.context }); + + const node = this.node.build(builder); + + builder.setContext(previousContext); + + return node; + } + + generate(builder, output) { + const previousContext = builder.getContext(); + + builder.setContext({ ...builder.context, ...this.context }); + + const snippet = this.node.build(builder, output); + + builder.setContext(previousContext); + + return snippet; + } +} + +export default ContextNode; + +export const context = nodeProxy(ContextNode); +export const label = (node, name) => context(node, { label: name }); + +addNodeElement('context', context); +addNodeElement('label', label); + +addNodeClass('ContextNode', ContextNode); diff --git a/src-testing/src/nodes/core/IndexNode.d.ts b/src-testing/src/nodes/core/IndexNode.d.ts new file mode 100644 index 000000000..b6b52f718 --- /dev/null +++ b/src-testing/src/nodes/core/IndexNode.d.ts @@ -0,0 +1,20 @@ +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import Node from "./Node.js"; + +export type IndexNodeScope = typeof IndexNode.VERTEX | typeof IndexNode.INSTANCE | typeof IndexNode.DRAW; + +export default class IndexNode extends Node { + scope: IndexNodeScope; + + readonly isInstanceNode: true; + + constructor(scope: IndexNodeScope); + + static VERTEX: "vertex"; + static INSTANCE: "instance"; + static DRAW: "draw"; +} + +export const vertexIndex: ShaderNodeObject; +export const instanceIndex: ShaderNodeObject; +export const drawIndex: ShaderNodeObject; diff --git a/src-testing/src/nodes/core/InputNode.ts b/src-testing/src/nodes/core/InputNode.ts new file mode 100644 index 000000000..4d52ec26f --- /dev/null +++ b/src-testing/src/nodes/core/InputNode.ts @@ -0,0 +1,65 @@ +import Node, { addNodeClass } from './Node.js'; +import { getValueType, getValueFromType, arrayBufferToBase64 } from './NodeUtils.js'; + +class InputNode extends Node { + constructor(value, nodeType = null) { + super(nodeType); + + this.isInputNode = true; + + this.value = value; + this.precision = null; + } + + getNodeType(/*builder*/) { + if (this.nodeType === null) { + return getValueType(this.value); + } + + return this.nodeType; + } + + getInputType(builder) { + return this.getNodeType(builder); + } + + setPrecision(precision) { + this.precision = precision; + + return this; + } + + serialize(data) { + super.serialize(data); + + data.value = this.value; + + if (this.value && this.value.toArray) data.value = this.value.toArray(); + + data.valueType = getValueType(this.value); + data.nodeType = this.nodeType; + + if (data.valueType === 'ArrayBuffer') data.value = arrayBufferToBase64(data.value); + + data.precision = this.precision; + } + + deserialize(data) { + super.deserialize(data); + + this.nodeType = data.nodeType; + this.value = Array.isArray(data.value) ? getValueFromType(data.valueType, ...data.value) : data.value; + + this.precision = data.precision || null; + + if (this.value && this.value.fromArray) this.value = this.value.fromArray(data.value); + } + + generate(/*builder, output*/) { + console.warn('Abstract function.'); + } +} + +export default InputNode; + +addNodeClass('InputNode', InputNode); diff --git a/src-testing/src/nodes/core/LightingModel.d.ts b/src-testing/src/nodes/core/LightingModel.d.ts new file mode 100644 index 000000000..f64dd07db --- /dev/null +++ b/src-testing/src/nodes/core/LightingModel.d.ts @@ -0,0 +1,46 @@ +import Node from "./Node.js"; +import NodeBuilder from "./NodeBuilder.js"; +import StackNode from "./StackNode.js"; + +export interface LightingModelReflectedLight { + directDiffuse: Node; + directSpecular: Node; + indirectDiffuse: Node; + indirectSpecular: Node; +} + +export interface LightingModelDirectInput { + lightDirection: Node; + lightColor: Node; + reflectedLight: LightingModelReflectedLight; +} + +export interface LightingModelDirectRectAreaInput { + lightColor: Node; + lightPosition: Node; + halfWidth: Node; + halfHeight: Node; + reflectedLight: LightingModelReflectedLight; + ltc_1: Node; + ltc_2: Node; +} + +export interface LightingModelIndirectInput { + radiance: Node; + irradiance: Node; + iblIrradiance: Node; + ambientOcclusion: Node; + reflectedLight: LightingModelReflectedLight; + backdrop: Node; + backdropAlpha: Node; + outgoingLight: Node; +} + +export default class LightingModel { + start(input: LightingModelIndirectInput, stack: StackNode, builder: NodeBuilder): void; + finish(input: LightingModelIndirectInput, stack: StackNode, builder: NodeBuilder): void; + direct(input: LightingModelDirectInput, stack: StackNode, builder: NodeBuilder): void; + directRectArea(input: LightingModelDirectRectAreaInput, stack: StackNode, builder: NodeBuilder): void; + indirect(input: LightingModelIndirectInput, stack: StackNode, builder: NodeBuilder): void; + ambientOcclusion(input: LightingModelIndirectInput, stack: StackNode, builder: NodeBuilder): void; +} diff --git a/src-testing/src/nodes/core/MRTNode.d.ts b/src-testing/src/nodes/core/MRTNode.d.ts new file mode 100644 index 000000000..ec993f27b --- /dev/null +++ b/src-testing/src/nodes/core/MRTNode.d.ts @@ -0,0 +1,19 @@ +import { Node } from "../Nodes.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import OutputStructNode from "./OutputStructNode.js"; + +declare class MRTNode extends OutputStructNode { + outputNodes: { [name: string]: Node }; + + readonly isMRTNode: true; + + constructor(outputNodes: { [name: string]: Node }); + + getNode(name: string): Node; + + merge(mrtNode: MRTNode): ShaderNodeObject; +} + +export default MRTNode; + +export const mrt: (outputNodes: { [name: string]: Node }) => ShaderNodeObject; diff --git a/src-testing/src/nodes/core/Node.ts b/src-testing/src/nodes/core/Node.ts new file mode 100644 index 000000000..e67dcb707 --- /dev/null +++ b/src-testing/src/nodes/core/Node.ts @@ -0,0 +1,417 @@ +import { NodeUpdateType } from './constants.js'; +import { getNodeChildren, getCacheKey } from './NodeUtils.js'; + +import { EventDispatcher } from '../../core/EventDispatcher.js'; +import { MathUtils } from '../../math/MathUtils.js'; + +const NodeClasses = new Map(); + +let _nodeId = 0; + +class Node extends EventDispatcher { + constructor(nodeType = null) { + super(); + + this.nodeType = nodeType; + + this.updateType = NodeUpdateType.NONE; + this.updateBeforeType = NodeUpdateType.NONE; + this.updateAfterType = NodeUpdateType.NONE; + + this.uuid = MathUtils.generateUUID(); + + this.version = 0; + + this._cacheKey = null; + this._cacheKeyVersion = 0; + + this.global = false; + + this.isNode = true; + + Object.defineProperty(this, 'id', { value: _nodeId++ }); + } + + set needsUpdate(value) { + if (value === true) { + this.version++; + } + } + + get type() { + return this.constructor.type; + } + + onUpdate(callback, updateType) { + this.updateType = updateType; + this.update = callback.bind(this.getSelf()); + + return this; + } + + onFrameUpdate(callback) { + return this.onUpdate(callback, NodeUpdateType.FRAME); + } + + onRenderUpdate(callback) { + return this.onUpdate(callback, NodeUpdateType.RENDER); + } + + onObjectUpdate(callback) { + return this.onUpdate(callback, NodeUpdateType.OBJECT); + } + + onReference(callback) { + this.updateReference = callback.bind(this.getSelf()); + + return this; + } + + getSelf() { + // Returns non-node object. + + return this.self || this; + } + + updateReference(/*state*/) { + return this; + } + + isGlobal(/*builder*/) { + return this.global; + } + + *getChildren() { + for (const { childNode } of getNodeChildren(this)) { + yield childNode; + } + } + + dispose() { + this.dispatchEvent({ type: 'dispose' }); + } + + traverse(callback) { + callback(this); + + for (const childNode of this.getChildren()) { + childNode.traverse(callback); + } + } + + getCacheKey(force = false) { + force = force || this.version !== this._cacheKeyVersion; + + if (force === true || this._cacheKey === null) { + this._cacheKey = getCacheKey(this, force); + this._cacheKeyVersion = this.version; + } + + return this._cacheKey; + } + + getHash(/*builder*/) { + return this.uuid; + } + + getUpdateType() { + return this.updateType; + } + + getUpdateBeforeType() { + return this.updateBeforeType; + } + + getUpdateAfterType() { + return this.updateAfterType; + } + + getElementType(builder) { + const type = this.getNodeType(builder); + const elementType = builder.getElementType(type); + + return elementType; + } + + getNodeType(builder) { + const nodeProperties = builder.getNodeProperties(this); + + if (nodeProperties.outputNode) { + return nodeProperties.outputNode.getNodeType(builder); + } + + return this.nodeType; + } + + getShared(builder) { + const hash = this.getHash(builder); + const nodeFromHash = builder.getNodeFromHash(hash); + + return nodeFromHash || this; + } + + setup(builder) { + const nodeProperties = builder.getNodeProperties(this); + + let index = 0; + + for (const childNode of this.getChildren()) { + nodeProperties['node' + index++] = childNode; + } + + // return a outputNode if exists + return null; + } + + increaseUsage(builder) { + const nodeData = builder.getDataFromNode(this); + nodeData.usageCount = nodeData.usageCount === undefined ? 1 : nodeData.usageCount + 1; + + return nodeData.usageCount; + } + + analyze(builder) { + const usageCount = this.increaseUsage(builder); + + if (usageCount === 1) { + // node flow children + + const nodeProperties = builder.getNodeProperties(this); + + for (const childNode of Object.values(nodeProperties)) { + if (childNode && childNode.isNode === true) { + childNode.build(builder); + } + } + } + } + + generate(builder, output) { + const { outputNode } = builder.getNodeProperties(this); + + if (outputNode && outputNode.isNode === true) { + return outputNode.build(builder, output); + } + } + + updateBefore(/*frame*/) { + console.warn('Abstract function.'); + } + + updateAfter(/*frame*/) { + console.warn('Abstract function.'); + } + + update(/*frame*/) { + console.warn('Abstract function.'); + } + + build(builder, output = null) { + const refNode = this.getShared(builder); + + if (this !== refNode) { + return refNode.build(builder, output); + } + + builder.addNode(this); + builder.addChain(this); + + /* Build stages expected results: + - "setup" -> Node + - "analyze" -> null + - "generate" -> String + */ + let result = null; + + const buildStage = builder.getBuildStage(); + + if (buildStage === 'setup') { + this.updateReference(builder); + + const properties = builder.getNodeProperties(this); + + if (properties.initialized !== true) { + const stackNodesBeforeSetup = builder.stack.nodes.length; + + properties.initialized = true; + properties.outputNode = this.setup(builder); + + if (properties.outputNode !== null && builder.stack.nodes.length !== stackNodesBeforeSetup) { + properties.outputNode = builder.stack; + } + + for (const childNode of Object.values(properties)) { + if (childNode && childNode.isNode === true) { + childNode.build(builder); + } + } + } + } else if (buildStage === 'analyze') { + this.analyze(builder); + } else if (buildStage === 'generate') { + const isGenerateOnce = this.generate.length === 1; + + if (isGenerateOnce) { + const type = this.getNodeType(builder); + const nodeData = builder.getDataFromNode(this); + + result = nodeData.snippet; + + if (result === undefined) { + result = this.generate(builder) || ''; + + nodeData.snippet = result; + } + + result = builder.format(result, type, output); + } else { + result = this.generate(builder, output) || ''; + } + } + + builder.removeChain(this); + + return result; + } + + getSerializeChildren() { + return getNodeChildren(this); + } + + serialize(json) { + const nodeChildren = this.getSerializeChildren(); + + const inputNodes = {}; + + for (const { property, index, childNode } of nodeChildren) { + if (index !== undefined) { + if (inputNodes[property] === undefined) { + inputNodes[property] = Number.isInteger(index) ? [] : {}; + } + + inputNodes[property][index] = childNode.toJSON(json.meta).uuid; + } else { + inputNodes[property] = childNode.toJSON(json.meta).uuid; + } + } + + if (Object.keys(inputNodes).length > 0) { + json.inputNodes = inputNodes; + } + } + + deserialize(json) { + if (json.inputNodes !== undefined) { + const nodes = json.meta.nodes; + + for (const property in json.inputNodes) { + if (Array.isArray(json.inputNodes[property])) { + const inputArray = []; + + for (const uuid of json.inputNodes[property]) { + inputArray.push(nodes[uuid]); + } + + this[property] = inputArray; + } else if (typeof json.inputNodes[property] === 'object') { + const inputObject = {}; + + for (const subProperty in json.inputNodes[property]) { + const uuid = json.inputNodes[property][subProperty]; + + inputObject[subProperty] = nodes[uuid]; + } + + this[property] = inputObject; + } else { + const uuid = json.inputNodes[property]; + + this[property] = nodes[uuid]; + } + } + } + } + + toJSON(meta) { + const { uuid, type } = this; + const isRoot = meta === undefined || typeof meta === 'string'; + + if (isRoot) { + meta = { + textures: {}, + images: {}, + nodes: {}, + }; + } + + // serialize + + let data = meta.nodes[uuid]; + + if (data === undefined) { + data = { + uuid, + type, + meta, + metadata: { + version: 4.6, + type: 'Node', + generator: 'Node.toJSON', + }, + }; + + if (isRoot !== true) meta.nodes[data.uuid] = data; + + this.serialize(data); + + delete data.meta; + } + + // TODO: Copied from Object3D.toJSON + + function extractFromCache(cache) { + const values = []; + + for (const key in cache) { + const data = cache[key]; + delete data.metadata; + values.push(data); + } + + return values; + } + + if (isRoot) { + const textures = extractFromCache(meta.textures); + const images = extractFromCache(meta.images); + const nodes = extractFromCache(meta.nodes); + + if (textures.length > 0) data.textures = textures; + if (images.length > 0) data.images = images; + if (nodes.length > 0) data.nodes = nodes; + } + + return data; + } +} + +export default Node; + +export function addNodeClass(type, nodeClass) { + if (typeof nodeClass !== 'function' || !type) throw new Error(`Node class ${type} is not a class`); + if (NodeClasses.has(type)) { + console.warn(`Redefinition of node class ${type}`); + return; + } + + NodeClasses.set(type, nodeClass); + nodeClass.type = type; +} + +export function createNodeFromType(type) { + const Class = NodeClasses.get(type); + + if (Class !== undefined) { + return new Class(); + } +} diff --git a/src-testing/src/nodes/core/NodeAttribute.ts b/src-testing/src/nodes/core/NodeAttribute.ts new file mode 100644 index 000000000..190fe8c51 --- /dev/null +++ b/src-testing/src/nodes/core/NodeAttribute.ts @@ -0,0 +1,11 @@ +class NodeAttribute { + constructor(name, type, node = null) { + this.isNodeAttribute = true; + + this.name = name; + this.type = type; + this.node = node; + } +} + +export default NodeAttribute; diff --git a/src-testing/src/nodes/core/NodeBuilder.ts b/src-testing/src/nodes/core/NodeBuilder.ts new file mode 100644 index 000000000..84ea6befa --- /dev/null +++ b/src-testing/src/nodes/core/NodeBuilder.ts @@ -0,0 +1,1128 @@ +import NodeUniform from './NodeUniform.js'; +import NodeAttribute from './NodeAttribute.js'; +import NodeVarying from './NodeVarying.js'; +import NodeVar from './NodeVar.js'; +import NodeCode from './NodeCode.js'; +import NodeKeywords from './NodeKeywords.js'; +import NodeCache from './NodeCache.js'; +import ParameterNode from './ParameterNode.js'; +import FunctionNode from '../code/FunctionNode.js'; +import { createNodeMaterialFromType, default as NodeMaterial } from '../materials/NodeMaterial.js'; +import { NodeUpdateType, defaultBuildStages, shaderStages } from './constants.js'; + +import { + NumberNodeUniform, + Vector2NodeUniform, + Vector3NodeUniform, + Vector4NodeUniform, + ColorNodeUniform, + Matrix3NodeUniform, + Matrix4NodeUniform, +} from '../../renderers/common/nodes/NodeUniform.js'; + +import { stack } from './StackNode.js'; +import { getCurrentStack, setCurrentStack } from '../shadernode/ShaderNode.js'; + +import CubeRenderTarget from '../../renderers/common/CubeRenderTarget.js'; +import ChainMap from '../../renderers/common/ChainMap.js'; + +import PMREMGenerator from '../../renderers/common/extras/PMREMGenerator.js'; + +import BindGroup from '../../renderers/common/BindGroup.js'; + +import { REVISION } from '../../constants.js'; +import { RenderTarget } from '../../core/RenderTarget.js'; +import { Color } from '../../math/Color.js'; +import { Vector2 } from '../../math/Vector2.js'; +import { Vector3 } from '../../math/Vector3.js'; +import { Vector4 } from '../../math/Vector4.js'; +import { Float16BufferAttribute } from '../../core/BufferAttribute.js'; +import { + IntType, + UnsignedIntType, + LinearFilter, + LinearMipmapNearestFilter, + NearestMipmapLinearFilter, + LinearMipmapLinearFilter, +} from '../../constants.js'; + +const rendererCache = new WeakMap(); + +const typeFromLength = new Map([ + [2, 'vec2'], + [3, 'vec3'], + [4, 'vec4'], + [9, 'mat3'], + [16, 'mat4'], +]); + +const typeFromArray = new Map([ + [Int8Array, 'int'], + [Int16Array, 'int'], + [Int32Array, 'int'], + [Uint8Array, 'uint'], + [Uint16Array, 'uint'], + [Uint32Array, 'uint'], + [Float32Array, 'float'], +]); + +const toFloat = value => { + value = Number(value); + + return value + (value % 1 ? '' : '.0'); +}; + +class NodeBuilder { + constructor(object, renderer, parser) { + this.object = object; + this.material = (object && object.material) || null; + this.geometry = (object && object.geometry) || null; + this.renderer = renderer; + this.parser = parser; + this.scene = null; + this.camera = null; + + this.nodes = []; + this.updateNodes = []; + this.updateBeforeNodes = []; + this.updateAfterNodes = []; + this.hashNodes = {}; + + this.lightsNode = null; + this.environmentNode = null; + this.fogNode = null; + + this.clippingContext = null; + + this.vertexShader = null; + this.fragmentShader = null; + this.computeShader = null; + + this.flowNodes = { vertex: [], fragment: [], compute: [] }; + this.flowCode = { vertex: '', fragment: '', compute: '' }; + this.uniforms = { vertex: [], fragment: [], compute: [], index: 0 }; + this.structs = { vertex: [], fragment: [], compute: [], index: 0 }; + this.bindings = { vertex: {}, fragment: {}, compute: {} }; + this.bindingsIndexes = {}; + this.bindGroups = null; + this.attributes = []; + this.bufferAttributes = []; + this.varyings = []; + this.codes = {}; + this.vars = {}; + this.flow = { code: '' }; + this.chaining = []; + this.stack = stack(); + this.stacks = []; + this.tab = '\t'; + + this.instanceBindGroups = true; + + this.currentFunctionNode = null; + + this.context = { + keywords: new NodeKeywords(), + material: this.material, + }; + + this.cache = new NodeCache(); + this.globalCache = this.cache; + + this.flowsData = new WeakMap(); + + this.shaderStage = null; + this.buildStage = null; + + this.useComparisonMethod = false; + } + + getBindGroupsCache() { + let bindGroupsCache = rendererCache.get(this.renderer); + + if (bindGroupsCache === undefined) { + bindGroupsCache = new ChainMap(); + + rendererCache.set(this.renderer, bindGroupsCache); + } + + return bindGroupsCache; + } + + createRenderTarget(width, height, options) { + return new RenderTarget(width, height, options); + } + + createCubeRenderTarget(size, options) { + return new CubeRenderTarget(size, options); + } + + createPMREMGenerator() { + // TODO: Move Materials.js to outside of the Nodes.js in order to remove this function and improve tree-shaking support + + return new PMREMGenerator(this.renderer); + } + + includes(node) { + return this.nodes.includes(node); + } + + _getBindGroup(groupName, bindings) { + const bindGroupsCache = this.getBindGroupsCache(); + + // + + const bindingsArray = []; + + let sharedGroup = true; + + for (const binding of bindings) { + bindingsArray.push(binding); + + sharedGroup = sharedGroup && binding.groupNode.shared !== true; + } + + // + + let bindGroup; + + if (sharedGroup) { + bindGroup = bindGroupsCache.get(bindingsArray); + + if (bindGroup === undefined) { + bindGroup = new BindGroup(groupName, bindingsArray, this.bindingsIndexes[groupName].group); + + bindGroupsCache.set(bindingsArray, bindGroup); + } + } else { + bindGroup = new BindGroup(groupName, bindingsArray, this.bindingsIndexes[groupName].group); + } + + return bindGroup; + } + + getBindGroupArray(groupName, shaderStage) { + const bindings = this.bindings[shaderStage]; + + let bindGroup = bindings[groupName]; + + if (bindGroup === undefined) { + if (this.bindingsIndexes[groupName] === undefined) { + this.bindingsIndexes[groupName] = { binding: 0, group: Object.keys(this.bindingsIndexes).length }; + } + + bindings[groupName] = bindGroup = []; + } + + return bindGroup; + } + + getBindings() { + let bindingsGroups = this.bindGroups; + + if (bindingsGroups === null) { + const groups = {}; + const bindings = this.bindings; + + for (const shaderStage of shaderStages) { + for (const groupName in bindings[shaderStage]) { + const uniforms = bindings[shaderStage][groupName]; + + const groupUniforms = groups[groupName] || (groups[groupName] = []); + groupUniforms.push(...uniforms); + } + } + + bindingsGroups = []; + + for (const groupName in groups) { + const group = groups[groupName]; + + const bindingsGroup = this._getBindGroup(groupName, group); + + bindingsGroups.push(bindingsGroup); + } + + this.bindGroups = bindingsGroups; + } + + return bindingsGroups; + } + + setHashNode(node, hash) { + this.hashNodes[hash] = node; + } + + addNode(node) { + if (this.nodes.includes(node) === false) { + this.nodes.push(node); + + this.setHashNode(node, node.getHash(this)); + } + } + + buildUpdateNodes() { + for (const node of this.nodes) { + const updateType = node.getUpdateType(); + const updateBeforeType = node.getUpdateBeforeType(); + const updateAfterType = node.getUpdateAfterType(); + + if (updateType !== NodeUpdateType.NONE) { + this.updateNodes.push(node.getSelf()); + } + + if (updateBeforeType !== NodeUpdateType.NONE) { + this.updateBeforeNodes.push(node); + } + + if (updateAfterType !== NodeUpdateType.NONE) { + this.updateAfterNodes.push(node); + } + } + } + + get currentNode() { + return this.chaining[this.chaining.length - 1]; + } + + isFilteredTexture(texture) { + return ( + texture.magFilter === LinearFilter || + texture.magFilter === LinearMipmapNearestFilter || + texture.magFilter === NearestMipmapLinearFilter || + texture.magFilter === LinearMipmapLinearFilter || + texture.minFilter === LinearFilter || + texture.minFilter === LinearMipmapNearestFilter || + texture.minFilter === NearestMipmapLinearFilter || + texture.minFilter === LinearMipmapLinearFilter + ); + } + + addChain(node) { + /* + if ( this.chaining.indexOf( node ) !== - 1 ) { + + console.warn( 'Recursive node: ', node ); + + } + */ + + this.chaining.push(node); + } + + removeChain(node) { + const lastChain = this.chaining.pop(); + + if (lastChain !== node) { + throw new Error('NodeBuilder: Invalid node chaining!'); + } + } + + getMethod(method) { + return method; + } + + getNodeFromHash(hash) { + return this.hashNodes[hash]; + } + + addFlow(shaderStage, node) { + this.flowNodes[shaderStage].push(node); + + return node; + } + + setContext(context) { + this.context = context; + } + + getContext() { + return this.context; + } + + getSharedContext() { + const context = { ...this.context }; + + delete context.keywords; + delete context.material; + + return this.context; + } + + setCache(cache) { + this.cache = cache; + } + + getCache() { + return this.cache; + } + + getCacheFromNode(node, parent = true) { + const data = this.getDataFromNode(node); + if (data.cache === undefined) data.cache = new NodeCache(parent ? this.getCache() : null); + + return data.cache; + } + + isAvailable(/*name*/) { + return false; + } + + getVertexIndex() { + console.warn('Abstract function.'); + } + + getInstanceIndex() { + console.warn('Abstract function.'); + } + + getDrawIndex() { + console.warn('Abstract function.'); + } + + getFrontFacing() { + console.warn('Abstract function.'); + } + + getFragCoord() { + console.warn('Abstract function.'); + } + + isFlipY() { + return false; + } + + generateTexture(/* texture, textureProperty, uvSnippet */) { + console.warn('Abstract function.'); + } + + generateTextureLod(/* texture, textureProperty, uvSnippet, levelSnippet */) { + console.warn('Abstract function.'); + } + + generateConst(type, value = null) { + if (value === null) { + if (type === 'float' || type === 'int' || type === 'uint') value = 0; + else if (type === 'bool') value = false; + else if (type === 'color') value = new Color(); + else if (type === 'vec2') value = new Vector2(); + else if (type === 'vec3') value = new Vector3(); + else if (type === 'vec4') value = new Vector4(); + } + + if (type === 'float') return toFloat(value); + if (type === 'int') return `${Math.round(value)}`; + if (type === 'uint') return value >= 0 ? `${Math.round(value)}u` : '0u'; + if (type === 'bool') return value ? 'true' : 'false'; + if (type === 'color') + return `${this.getType('vec3')}( ${toFloat(value.r)}, ${toFloat(value.g)}, ${toFloat(value.b)} )`; + + const typeLength = this.getTypeLength(type); + + const componentType = this.getComponentType(type); + + const generateConst = value => this.generateConst(componentType, value); + + if (typeLength === 2) { + return `${this.getType(type)}( ${generateConst(value.x)}, ${generateConst(value.y)} )`; + } else if (typeLength === 3) { + return `${this.getType(type)}( ${generateConst(value.x)}, ${generateConst(value.y)}, ${generateConst(value.z)} )`; + } else if (typeLength === 4) { + return `${this.getType(type)}( ${generateConst(value.x)}, ${generateConst(value.y)}, ${generateConst(value.z)}, ${generateConst(value.w)} )`; + } else if (typeLength > 4 && value && (value.isMatrix3 || value.isMatrix4)) { + return `${this.getType(type)}( ${value.elements.map(generateConst).join(', ')} )`; + } else if (typeLength > 4) { + return `${this.getType(type)}()`; + } + + throw new Error(`NodeBuilder: Type '${type}' not found in generate constant attempt.`); + } + + getType(type) { + if (type === 'color') return 'vec3'; + + return type; + } + + hasGeometryAttribute(name) { + return this.geometry && this.geometry.getAttribute(name) !== undefined; + } + + getAttribute(name, type) { + const attributes = this.attributes; + + // find attribute + + for (const attribute of attributes) { + if (attribute.name === name) { + return attribute; + } + } + + // create a new if no exist + + const attribute = new NodeAttribute(name, type); + + attributes.push(attribute); + + return attribute; + } + + getPropertyName(node /*, shaderStage*/) { + return node.name; + } + + isVector(type) { + return /vec\d/.test(type); + } + + isMatrix(type) { + return /mat\d/.test(type); + } + + isReference(type) { + return ( + type === 'void' || + type === 'property' || + type === 'sampler' || + type === 'texture' || + type === 'cubeTexture' || + type === 'storageTexture' || + type === 'depthTexture' || + type === 'texture3D' + ); + } + + needsColorSpaceToLinear(/*texture*/) { + return false; + } + + getComponentTypeFromTexture(texture) { + const type = texture.type; + + if (texture.isDataTexture) { + if (type === IntType) return 'int'; + if (type === UnsignedIntType) return 'uint'; + } + + return 'float'; + } + + getElementType(type) { + if (type === 'mat2') return 'vec2'; + if (type === 'mat3') return 'vec3'; + if (type === 'mat4') return 'vec4'; + + return this.getComponentType(type); + } + + getComponentType(type) { + type = this.getVectorType(type); + + if (type === 'float' || type === 'bool' || type === 'int' || type === 'uint') return type; + + const componentType = /(b|i|u|)(vec|mat)([2-4])/.exec(type); + + if (componentType === null) return null; + + if (componentType[1] === 'b') return 'bool'; + if (componentType[1] === 'i') return 'int'; + if (componentType[1] === 'u') return 'uint'; + + return 'float'; + } + + getVectorType(type) { + if (type === 'color') return 'vec3'; + if (type === 'texture' || type === 'cubeTexture' || type === 'storageTexture' || type === 'texture3D') + return 'vec4'; + + return type; + } + + getTypeFromLength(length, componentType = 'float') { + if (length === 1) return componentType; + + const baseType = typeFromLength.get(length); + const prefix = componentType === 'float' ? '' : componentType[0]; + + return prefix + baseType; + } + + getTypeFromArray(array) { + return typeFromArray.get(array.constructor); + } + + getTypeFromAttribute(attribute) { + let dataAttribute = attribute; + + if (attribute.isInterleavedBufferAttribute) dataAttribute = attribute.data; + + const array = dataAttribute.array; + const itemSize = attribute.itemSize; + const normalized = attribute.normalized; + + let arrayType; + + if (!(attribute instanceof Float16BufferAttribute) && normalized !== true) { + arrayType = this.getTypeFromArray(array); + } + + return this.getTypeFromLength(itemSize, arrayType); + } + + getTypeLength(type) { + const vecType = this.getVectorType(type); + const vecNum = /vec([2-4])/.exec(vecType); + + if (vecNum !== null) return Number(vecNum[1]); + if (vecType === 'float' || vecType === 'bool' || vecType === 'int' || vecType === 'uint') return 1; + if (/mat2/.test(type) === true) return 4; + if (/mat3/.test(type) === true) return 9; + if (/mat4/.test(type) === true) return 16; + + return 0; + } + + getVectorFromMatrix(type) { + return type.replace('mat', 'vec'); + } + + changeComponentType(type, newComponentType) { + return this.getTypeFromLength(this.getTypeLength(type), newComponentType); + } + + getIntegerType(type) { + const componentType = this.getComponentType(type); + + if (componentType === 'int' || componentType === 'uint') return type; + + return this.changeComponentType(type, 'int'); + } + + addStack() { + this.stack = stack(this.stack); + + this.stacks.push(getCurrentStack() || this.stack); + setCurrentStack(this.stack); + + return this.stack; + } + + removeStack() { + const lastStack = this.stack; + this.stack = lastStack.parent; + + setCurrentStack(this.stacks.pop()); + + return lastStack; + } + + getDataFromNode(node, shaderStage = this.shaderStage, cache = null) { + cache = cache === null ? (node.isGlobal(this) ? this.globalCache : this.cache) : cache; + + let nodeData = cache.getData(node); + + if (nodeData === undefined) { + nodeData = {}; + + cache.setData(node, nodeData); + } + + if (nodeData[shaderStage] === undefined) nodeData[shaderStage] = {}; + + return nodeData[shaderStage]; + } + + getNodeProperties(node, shaderStage = 'any') { + const nodeData = this.getDataFromNode(node, shaderStage); + + return nodeData.properties || (nodeData.properties = { outputNode: null }); + } + + getBufferAttributeFromNode(node, type) { + const nodeData = this.getDataFromNode(node); + + let bufferAttribute = nodeData.bufferAttribute; + + if (bufferAttribute === undefined) { + const index = this.uniforms.index++; + + bufferAttribute = new NodeAttribute('nodeAttribute' + index, type, node); + + this.bufferAttributes.push(bufferAttribute); + + nodeData.bufferAttribute = bufferAttribute; + } + + return bufferAttribute; + } + + getStructTypeFromNode(node, shaderStage = this.shaderStage) { + const nodeData = this.getDataFromNode(node, shaderStage); + + if (nodeData.structType === undefined) { + const index = this.structs.index++; + + node.name = `StructType${index}`; + this.structs[shaderStage].push(node); + + nodeData.structType = node; + } + + return node; + } + + getUniformFromNode(node, type, shaderStage = this.shaderStage, name = null) { + const nodeData = this.getDataFromNode(node, shaderStage, this.globalCache); + + let nodeUniform = nodeData.uniform; + + if (nodeUniform === undefined) { + const index = this.uniforms.index++; + + nodeUniform = new NodeUniform(name || 'nodeUniform' + index, type, node); + + this.uniforms[shaderStage].push(nodeUniform); + + nodeData.uniform = nodeUniform; + } + + return nodeUniform; + } + + getVarFromNode(node, name = null, type = node.getNodeType(this), shaderStage = this.shaderStage) { + const nodeData = this.getDataFromNode(node, shaderStage); + + let nodeVar = nodeData.variable; + + if (nodeVar === undefined) { + const vars = this.vars[shaderStage] || (this.vars[shaderStage] = []); + + if (name === null) name = 'nodeVar' + vars.length; + + nodeVar = new NodeVar(name, type); + + vars.push(nodeVar); + + nodeData.variable = nodeVar; + } + + return nodeVar; + } + + getVaryingFromNode(node, name = null, type = node.getNodeType(this)) { + const nodeData = this.getDataFromNode(node, 'any'); + + let nodeVarying = nodeData.varying; + + if (nodeVarying === undefined) { + const varyings = this.varyings; + const index = varyings.length; + + if (name === null) name = 'nodeVarying' + index; + + nodeVarying = new NodeVarying(name, type); + + varyings.push(nodeVarying); + + nodeData.varying = nodeVarying; + } + + return nodeVarying; + } + + getCodeFromNode(node, type, shaderStage = this.shaderStage) { + const nodeData = this.getDataFromNode(node); + + let nodeCode = nodeData.code; + + if (nodeCode === undefined) { + const codes = this.codes[shaderStage] || (this.codes[shaderStage] = []); + const index = codes.length; + + nodeCode = new NodeCode('nodeCode' + index, type); + + codes.push(nodeCode); + + nodeData.code = nodeCode; + } + + return nodeCode; + } + + addLineFlowCode(code) { + if (code === '') return this; + + code = this.tab + code; + + if (!/;\s*$/.test(code)) { + code = code + ';\n'; + } + + this.flow.code += code; + + return this; + } + + addFlowCode(code) { + this.flow.code += code; + + return this; + } + + addFlowTab() { + this.tab += '\t'; + + return this; + } + + removeFlowTab() { + this.tab = this.tab.slice(0, -1); + + return this; + } + + getFlowData(node /*, shaderStage*/) { + return this.flowsData.get(node); + } + + flowNode(node) { + const output = node.getNodeType(this); + + const flowData = this.flowChildNode(node, output); + + this.flowsData.set(node, flowData); + + return flowData; + } + + buildFunctionNode(shaderNode) { + const fn = new FunctionNode(); + + const previous = this.currentFunctionNode; + + this.currentFunctionNode = fn; + + fn.code = this.buildFunctionCode(shaderNode); + + this.currentFunctionNode = previous; + + return fn; + } + + flowShaderNode(shaderNode) { + const layout = shaderNode.layout; + + let inputs; + + if (shaderNode.isArrayInput) { + inputs = []; + + for (const input of layout.inputs) { + inputs.push(new ParameterNode(input.type, input.name)); + } + } else { + inputs = {}; + + for (const input of layout.inputs) { + inputs[input.name] = new ParameterNode(input.type, input.name); + } + } + + // + + shaderNode.layout = null; + + const callNode = shaderNode.call(inputs); + const flowData = this.flowStagesNode(callNode, layout.type); + + shaderNode.layout = layout; + + return flowData; + } + + flowStagesNode(node, output = null) { + const previousFlow = this.flow; + const previousVars = this.vars; + const previousCache = this.cache; + const previousBuildStage = this.buildStage; + const previousStack = this.stack; + + const flow = { + code: '', + }; + + this.flow = flow; + this.vars = {}; + this.cache = new NodeCache(); + this.stack = stack(); + + for (const buildStage of defaultBuildStages) { + this.setBuildStage(buildStage); + + flow.result = node.build(this, output); + } + + flow.vars = this.getVars(this.shaderStage); + + this.flow = previousFlow; + this.vars = previousVars; + this.cache = previousCache; + this.stack = previousStack; + + this.setBuildStage(previousBuildStage); + + return flow; + } + + getFunctionOperator() { + return null; + } + + flowChildNode(node, output = null) { + const previousFlow = this.flow; + + const flow = { + code: '', + }; + + this.flow = flow; + + flow.result = node.build(this, output); + + this.flow = previousFlow; + + return flow; + } + + flowNodeFromShaderStage(shaderStage, node, output = null, propertyName = null) { + const previousShaderStage = this.shaderStage; + + this.setShaderStage(shaderStage); + + const flowData = this.flowChildNode(node, output); + + if (propertyName !== null) { + flowData.code += `${this.tab + propertyName} = ${flowData.result};\n`; + } + + this.flowCode[shaderStage] = this.flowCode[shaderStage] + flowData.code; + + this.setShaderStage(previousShaderStage); + + return flowData; + } + + getAttributesArray() { + return this.attributes.concat(this.bufferAttributes); + } + + getAttributes(/*shaderStage*/) { + console.warn('Abstract function.'); + } + + getVaryings(/*shaderStage*/) { + console.warn('Abstract function.'); + } + + getVar(type, name) { + return `${this.getType(type)} ${name}`; + } + + getVars(shaderStage) { + let snippet = ''; + + const vars = this.vars[shaderStage]; + + if (vars !== undefined) { + for (const variable of vars) { + snippet += `${this.getVar(variable.type, variable.name)}; `; + } + } + + return snippet; + } + + getUniforms(/*shaderStage*/) { + console.warn('Abstract function.'); + } + + getCodes(shaderStage) { + const codes = this.codes[shaderStage]; + + let code = ''; + + if (codes !== undefined) { + for (const nodeCode of codes) { + code += nodeCode.code + '\n'; + } + } + + return code; + } + + getHash() { + return this.vertexShader + this.fragmentShader + this.computeShader; + } + + setShaderStage(shaderStage) { + this.shaderStage = shaderStage; + } + + getShaderStage() { + return this.shaderStage; + } + + setBuildStage(buildStage) { + this.buildStage = buildStage; + } + + getBuildStage() { + return this.buildStage; + } + + buildCode() { + console.warn('Abstract function.'); + } + + build() { + const { object, material } = this; + + if (material !== null) { + NodeMaterial.fromMaterial(material).build(this); + } else { + this.addFlow('compute', object); + } + + // setup() -> stage 1: create possible new nodes and returns an output reference node + // analyze() -> stage 2: analyze nodes to possible optimization and validation + // generate() -> stage 3: generate shader + + for (const buildStage of defaultBuildStages) { + this.setBuildStage(buildStage); + + if (this.context.vertex && this.context.vertex.isNode) { + this.flowNodeFromShaderStage('vertex', this.context.vertex); + } + + for (const shaderStage of shaderStages) { + this.setShaderStage(shaderStage); + + const flowNodes = this.flowNodes[shaderStage]; + + for (const node of flowNodes) { + if (buildStage === 'generate') { + this.flowNode(node); + } else { + node.build(this); + } + } + } + } + + this.setBuildStage(null); + this.setShaderStage(null); + + // stage 4: build code for a specific output + + this.buildCode(); + this.buildUpdateNodes(); + + return this; + } + + getNodeUniform(uniformNode, type) { + if (type === 'float' || type === 'int' || type === 'uint') return new NumberNodeUniform(uniformNode); + if (type === 'vec2' || type === 'ivec2' || type === 'uvec2') return new Vector2NodeUniform(uniformNode); + if (type === 'vec3' || type === 'ivec3' || type === 'uvec3') return new Vector3NodeUniform(uniformNode); + if (type === 'vec4' || type === 'ivec4' || type === 'uvec4') return new Vector4NodeUniform(uniformNode); + if (type === 'color') return new ColorNodeUniform(uniformNode); + if (type === 'mat3') return new Matrix3NodeUniform(uniformNode); + if (type === 'mat4') return new Matrix4NodeUniform(uniformNode); + + throw new Error(`Uniform "${type}" not declared.`); + } + + createNodeMaterial(type = 'NodeMaterial') { + // TODO: Move Materials.js to outside of the Nodes.js in order to remove this function and improve tree-shaking support + + return createNodeMaterialFromType(type); + } + + format(snippet, fromType, toType) { + fromType = this.getVectorType(fromType); + toType = this.getVectorType(toType); + + if (fromType === toType || toType === null || this.isReference(toType)) { + return snippet; + } + + const fromTypeLength = this.getTypeLength(fromType); + const toTypeLength = this.getTypeLength(toType); + + if (fromTypeLength === 16 && toTypeLength === 9) { + return `${this.getType(toType)}(${snippet}[0].xyz, ${snippet}[1].xyz, ${snippet}[2].xyz)`; + } + + if (fromTypeLength === 9 && toTypeLength === 4) { + return `${this.getType(toType)}(${snippet}[0].xy, ${snippet}[1].xy)`; + } + + if (fromTypeLength > 4) { + // fromType is matrix-like + + // @TODO: ignore for now + + return snippet; + } + + if (toTypeLength > 4 || toTypeLength === 0) { + // toType is matrix-like or unknown + + // @TODO: ignore for now + + return snippet; + } + + if (fromTypeLength === toTypeLength) { + return `${this.getType(toType)}( ${snippet} )`; + } + + if (fromTypeLength > toTypeLength) { + return this.format( + `${snippet}.${'xyz'.slice(0, toTypeLength)}`, + this.getTypeFromLength(toTypeLength, this.getComponentType(fromType)), + toType, + ); + } + + if (toTypeLength === 4 && fromTypeLength > 1) { + // toType is vec4-like + + return `${this.getType(toType)}( ${this.format(snippet, fromType, 'vec3')}, 1.0 )`; + } + + if (fromTypeLength === 2) { + // fromType is vec2-like and toType is vec3-like + + return `${this.getType(toType)}( ${this.format(snippet, fromType, 'vec2')}, 0.0 )`; + } + + if (fromTypeLength === 1 && toTypeLength > 1 && fromType !== this.getComponentType(toType)) { + // fromType is float-like + + // convert a number value to vector type, e.g: + // vec3( 1u ) -> vec3( float( 1u ) ) + + snippet = `${this.getType(this.getComponentType(toType))}( ${snippet} )`; + } + + return `${this.getType(toType)}( ${snippet} )`; // fromType is float-like + } + + getSignature() { + return `// Three.js r${REVISION} - Node System\n`; + } +} + +export default NodeBuilder; diff --git a/src-testing/src/nodes/core/NodeCache.ts b/src-testing/src/nodes/core/NodeCache.ts new file mode 100644 index 000000000..ad72d50c5 --- /dev/null +++ b/src-testing/src/nodes/core/NodeCache.ts @@ -0,0 +1,26 @@ +let id = 0; + +class NodeCache { + constructor(parent = null) { + this.id = id++; + this.nodesData = new WeakMap(); + + this.parent = parent; + } + + getData(node) { + let data = this.nodesData.get(node); + + if (data === undefined && this.parent !== null) { + data = this.parent.getData(node); + } + + return data; + } + + setData(node, data) { + this.nodesData.set(node, data); + } +} + +export default NodeCache; diff --git a/src-testing/src/nodes/core/NodeCode.ts b/src-testing/src/nodes/core/NodeCode.ts new file mode 100644 index 000000000..2ee509037 --- /dev/null +++ b/src-testing/src/nodes/core/NodeCode.ts @@ -0,0 +1,11 @@ +class NodeCode { + constructor(name, type, code = '') { + this.name = name; + this.type = type; + this.code = code; + + Object.defineProperty(this, 'isNodeCode', { value: true }); + } +} + +export default NodeCode; diff --git a/src-testing/src/nodes/core/NodeFrame.ts b/src-testing/src/nodes/core/NodeFrame.ts new file mode 100644 index 000000000..ee64620ca --- /dev/null +++ b/src-testing/src/nodes/core/NodeFrame.ts @@ -0,0 +1,127 @@ +import { NodeUpdateType } from './constants.js'; + +class NodeFrame { + constructor() { + this.time = 0; + this.deltaTime = 0; + + this.frameId = 0; + this.renderId = 0; + + this.startTime = null; + + this.updateMap = new WeakMap(); + this.updateBeforeMap = new WeakMap(); + this.updateAfterMap = new WeakMap(); + + this.renderer = null; + this.material = null; + this.camera = null; + this.object = null; + this.scene = null; + } + + _getMaps(referenceMap, nodeRef) { + let maps = referenceMap.get(nodeRef); + + if (maps === undefined) { + maps = { + renderMap: new WeakMap(), + frameMap: new WeakMap(), + }; + + referenceMap.set(nodeRef, maps); + } + + return maps; + } + + updateBeforeNode(node) { + const updateType = node.getUpdateBeforeType(); + const reference = node.updateReference(this); + + if (updateType === NodeUpdateType.FRAME) { + const { frameMap } = this._getMaps(this.updateBeforeMap, reference); + + if (frameMap.get(reference) !== this.frameId) { + if (node.updateBefore(this) !== false) { + frameMap.set(reference, this.frameId); + } + } + } else if (updateType === NodeUpdateType.RENDER) { + const { renderMap } = this._getMaps(this.updateBeforeMap, reference); + + if (renderMap.get(reference) !== this.renderId) { + if (node.updateBefore(this) !== false) { + renderMap.set(reference, this.renderId); + } + } + } else if (updateType === NodeUpdateType.OBJECT) { + node.updateBefore(this); + } + } + + updateAfterNode(node) { + const updateType = node.getUpdateAfterType(); + const reference = node.updateReference(this); + + if (updateType === NodeUpdateType.FRAME) { + const { frameMap } = this._getMaps(this.updateAfterMap, reference); + + if (frameMap.get(reference) !== this.frameId) { + if (node.updateAfter(this) !== false) { + frameMap.set(reference, this.frameId); + } + } + } else if (updateType === NodeUpdateType.RENDER) { + const { renderMap } = this._getMaps(this.updateAfterMap, reference); + + if (renderMap.get(reference) !== this.renderId) { + if (node.updateAfter(this) !== false) { + renderMap.set(reference, this.renderId); + } + } + } else if (updateType === NodeUpdateType.OBJECT) { + node.updateAfter(this); + } + } + + updateNode(node) { + const updateType = node.getUpdateType(); + const reference = node.updateReference(this); + + if (updateType === NodeUpdateType.FRAME) { + const { frameMap } = this._getMaps(this.updateMap, reference); + + if (frameMap.get(reference) !== this.frameId) { + if (node.update(this) !== false) { + frameMap.set(reference, this.frameId); + } + } + } else if (updateType === NodeUpdateType.RENDER) { + const { renderMap } = this._getMaps(this.updateMap, reference); + + if (renderMap.get(reference) !== this.renderId) { + if (node.update(this) !== false) { + renderMap.set(reference, this.renderId); + } + } + } else if (updateType === NodeUpdateType.OBJECT) { + node.update(this); + } + } + + update() { + this.frameId++; + + if (this.lastTime === undefined) this.lastTime = performance.now(); + + this.deltaTime = (performance.now() - this.lastTime) / 1000; + + this.lastTime = performance.now(); + + this.time += this.deltaTime; + } +} + +export default NodeFrame; diff --git a/src-testing/src/nodes/core/NodeFunction.ts b/src-testing/src/nodes/core/NodeFunction.ts new file mode 100644 index 000000000..d05afb5e6 --- /dev/null +++ b/src-testing/src/nodes/core/NodeFunction.ts @@ -0,0 +1,16 @@ +class NodeFunction { + constructor(type, inputs, name = '', precision = '') { + this.type = type; + this.inputs = inputs; + this.name = name; + this.precision = precision; + } + + getCode(/*name = this.name*/) { + console.warn('Abstract function.'); + } +} + +NodeFunction.isNodeFunction = true; + +export default NodeFunction; diff --git a/src-testing/src/nodes/core/NodeFunctionInput.d.ts b/src-testing/src/nodes/core/NodeFunctionInput.d.ts new file mode 100644 index 000000000..a8231d126 --- /dev/null +++ b/src-testing/src/nodes/core/NodeFunctionInput.d.ts @@ -0,0 +1,7 @@ +export default class NodeFunctionInput { + isNodeFunctionInput: true; + count: null | number; + qualifier: string; + isConst: boolean; + constructor(type: string, name: string, count?: number, qualifier?: string, isConst?: boolean); +} diff --git a/src-testing/src/nodes/core/NodeKeywords.ts b/src-testing/src/nodes/core/NodeKeywords.ts new file mode 100644 index 000000000..1f468321b --- /dev/null +++ b/src-testing/src/nodes/core/NodeKeywords.ts @@ -0,0 +1,58 @@ +class NodeKeywords { + constructor() { + this.keywords = []; + this.nodes = {}; + this.keywordsCallback = {}; + } + + getNode(name) { + let node = this.nodes[name]; + + if (node === undefined && this.keywordsCallback[name] !== undefined) { + node = this.keywordsCallback[name](name); + + this.nodes[name] = node; + } + + return node; + } + + addKeyword(name, callback) { + this.keywords.push(name); + this.keywordsCallback[name] = callback; + + return this; + } + + parse(code) { + const keywordNames = this.keywords; + + const regExp = new RegExp(`\\b${keywordNames.join('\\b|\\b')}\\b`, 'g'); + + const codeKeywords = code.match(regExp); + + const keywordNodes = []; + + if (codeKeywords !== null) { + for (const keyword of codeKeywords) { + const node = this.getNode(keyword); + + if (node !== undefined && keywordNodes.indexOf(node) === -1) { + keywordNodes.push(node); + } + } + } + + return keywordNodes; + } + + include(builder, code) { + const keywordNodes = this.parse(code); + + for (const keywordNode of keywordNodes) { + keywordNode.build(builder); + } + } +} + +export default NodeKeywords; diff --git a/src-testing/src/nodes/core/NodeParser.ts b/src-testing/src/nodes/core/NodeParser.ts new file mode 100644 index 000000000..9849452f1 --- /dev/null +++ b/src-testing/src/nodes/core/NodeParser.ts @@ -0,0 +1,7 @@ +class NodeParser { + parseFunction(/*source*/) { + console.warn('Abstract function.'); + } +} + +export default NodeParser; diff --git a/src-testing/src/nodes/core/NodeUniform.ts b/src-testing/src/nodes/core/NodeUniform.ts new file mode 100644 index 000000000..ca43958fc --- /dev/null +++ b/src-testing/src/nodes/core/NodeUniform.ts @@ -0,0 +1,27 @@ +class NodeUniform { + constructor(name, type, node) { + this.isNodeUniform = true; + + this.name = name; + this.type = type; + this.node = node.getSelf(); + } + + get value() { + return this.node.value; + } + + set value(val) { + this.node.value = val; + } + + get id() { + return this.node.id; + } + + get groupNode() { + return this.node.groupNode; + } +} + +export default NodeUniform; diff --git a/src-testing/src/nodes/core/NodeUtils.ts b/src-testing/src/nodes/core/NodeUtils.ts new file mode 100644 index 000000000..2314a6ebd --- /dev/null +++ b/src-testing/src/nodes/core/NodeUtils.ts @@ -0,0 +1,137 @@ +import { Color } from '../../math/Color.js'; +import { Matrix3 } from '../../math/Matrix3.js'; +import { Matrix4 } from '../../math/Matrix4.js'; +import { Vector2 } from '../../math/Vector2.js'; +import { Vector3 } from '../../math/Vector3.js'; +import { Vector4 } from '../../math/Vector4.js'; + +export function getCacheKey(object, force = false) { + let cacheKey = '{'; + + if (object.isNode === true) { + cacheKey += object.id; + } + + for (const { property, childNode } of getNodeChildren(object)) { + cacheKey += ',' + property.slice(0, -4) + ':' + childNode.getCacheKey(force); + } + + cacheKey += '}'; + + return cacheKey; +} + +export function* getNodeChildren(node, toJSON = false) { + for (const property in node) { + // Ignore private properties. + if (property.startsWith('_') === true) continue; + + const object = node[property]; + + if (Array.isArray(object) === true) { + for (let i = 0; i < object.length; i++) { + const child = object[i]; + + if (child && (child.isNode === true || (toJSON && typeof child.toJSON === 'function'))) { + yield { property, index: i, childNode: child }; + } + } + } else if (object && object.isNode === true) { + yield { property, childNode: object }; + } else if (typeof object === 'object') { + for (const subProperty in object) { + const child = object[subProperty]; + + if (child && (child.isNode === true || (toJSON && typeof child.toJSON === 'function'))) { + yield { property, index: subProperty, childNode: child }; + } + } + } + } +} + +export function getValueType(value) { + if (value === undefined || value === null) return null; + + const typeOf = typeof value; + + if (value.isNode === true) { + return 'node'; + } else if (typeOf === 'number') { + return 'float'; + } else if (typeOf === 'boolean') { + return 'bool'; + } else if (typeOf === 'string') { + return 'string'; + } else if (typeOf === 'function') { + return 'shader'; + } else if (value.isVector2 === true) { + return 'vec2'; + } else if (value.isVector3 === true) { + return 'vec3'; + } else if (value.isVector4 === true) { + return 'vec4'; + } else if (value.isMatrix3 === true) { + return 'mat3'; + } else if (value.isMatrix4 === true) { + return 'mat4'; + } else if (value.isColor === true) { + return 'color'; + } else if (value instanceof ArrayBuffer) { + return 'ArrayBuffer'; + } + + return null; +} + +export function getValueFromType(type, ...params) { + const last4 = type ? type.slice(-4) : undefined; + + if (params.length === 1) { + // ensure same behaviour as in NodeBuilder.format() + + if (last4 === 'vec2') params = [params[0], params[0]]; + else if (last4 === 'vec3') params = [params[0], params[0], params[0]]; + else if (last4 === 'vec4') params = [params[0], params[0], params[0], params[0]]; + } + + if (type === 'color') { + return new Color(...params); + } else if (last4 === 'vec2') { + return new Vector2(...params); + } else if (last4 === 'vec3') { + return new Vector3(...params); + } else if (last4 === 'vec4') { + return new Vector4(...params); + } else if (last4 === 'mat3') { + return new Matrix3(...params); + } else if (last4 === 'mat4') { + return new Matrix4(...params); + } else if (type === 'bool') { + return params[0] || false; + } else if (type === 'float' || type === 'int' || type === 'uint') { + return params[0] || 0; + } else if (type === 'string') { + return params[0] || ''; + } else if (type === 'ArrayBuffer') { + return base64ToArrayBuffer(params[0]); + } + + return null; +} + +export function arrayBufferToBase64(arrayBuffer) { + let chars = ''; + + const array = new Uint8Array(arrayBuffer); + + for (let i = 0; i < array.length; i++) { + chars += String.fromCharCode(array[i]); + } + + return btoa(chars); +} + +export function base64ToArrayBuffer(base64) { + return Uint8Array.from(atob(base64), c => c.charCodeAt(0)).buffer; +} diff --git a/src-testing/src/nodes/core/NodeVar.ts b/src-testing/src/nodes/core/NodeVar.ts new file mode 100644 index 000000000..e6e935b31 --- /dev/null +++ b/src-testing/src/nodes/core/NodeVar.ts @@ -0,0 +1,10 @@ +class NodeVar { + constructor(name, type) { + this.isNodeVar = true; + + this.name = name; + this.type = type; + } +} + +export default NodeVar; diff --git a/src-testing/src/nodes/core/NodeVarying.ts b/src-testing/src/nodes/core/NodeVarying.ts new file mode 100644 index 000000000..a14823628 --- /dev/null +++ b/src-testing/src/nodes/core/NodeVarying.ts @@ -0,0 +1,13 @@ +import NodeVar from './NodeVar.js'; + +class NodeVarying extends NodeVar { + constructor(name, type) { + super(name, type); + + this.needsInterpolation = false; + + this.isNodeVarying = true; + } +} + +export default NodeVarying; diff --git a/src-testing/src/nodes/core/OutputStructNode.d.ts b/src-testing/src/nodes/core/OutputStructNode.d.ts new file mode 100644 index 000000000..eb2c8fa63 --- /dev/null +++ b/src-testing/src/nodes/core/OutputStructNode.d.ts @@ -0,0 +1,12 @@ +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import Node from "./Node.js"; + +export default class OutputStructNode extends Node { + members: Node[]; + + readonly isOutputStructNode: true; + + constructor(...members: Node[]); +} + +export const outputStruct: (...members: Node[]) => ShaderNodeObject; diff --git a/src-testing/src/nodes/core/PropertyNode.d.ts b/src-testing/src/nodes/core/PropertyNode.d.ts new file mode 100644 index 000000000..d1a523864 --- /dev/null +++ b/src-testing/src/nodes/core/PropertyNode.d.ts @@ -0,0 +1,43 @@ +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import Node from "./Node.js"; + +export default class PropertyNode extends Node { + name: string | null; + varying: boolean; + + readonly isPropertyNode: true; + + constructor(nodeType?: string | null, name?: string | null, varying?: boolean); +} + +export const property: (type?: string | null, name?: string | null) => ShaderNodeObject; +export const varyingProperty: (type?: string | null, name?: string | null) => ShaderNodeObject; + +export const diffuseColor: ShaderNodeObject; +export const emissive: ShaderNodeObject; +export const roughness: ShaderNodeObject; +export const metalness: ShaderNodeObject; +export const clearcoat: ShaderNodeObject; +export const clearcoatRoughness: ShaderNodeObject; +export const sheen: ShaderNodeObject; +export const sheenRoughness: ShaderNodeObject; +export const iridescence: ShaderNodeObject; +export const iridescenceIOR: ShaderNodeObject; +export const iridescenceThickness: ShaderNodeObject; +export const alphaT: ShaderNodeObject; +export const anisotropy: ShaderNodeObject; +export const anisotropyT: ShaderNodeObject; +export const anisotropyB: ShaderNodeObject; +export const specularColor: ShaderNodeObject; +export const specularF90: ShaderNodeObject; +export const shininess: ShaderNodeObject; +export const output: ShaderNodeObject; +export const dashSize: ShaderNodeObject; +export const gapSize: ShaderNodeObject; +export const pointWidth: ShaderNodeObject; +export const ior: ShaderNodeObject; +export const transmission: ShaderNodeObject; +export const thickness: ShaderNodeObject; +export const attenuationDistance: ShaderNodeObject; +export const attenuationColor: ShaderNodeObject; +export const dispersion: ShaderNodeObject; diff --git a/src-testing/src/nodes/core/StackNode.ts b/src-testing/src/nodes/core/StackNode.ts new file mode 100644 index 000000000..5f1871e73 --- /dev/null +++ b/src-testing/src/nodes/core/StackNode.ts @@ -0,0 +1,87 @@ +import Node, { addNodeClass } from './Node.js'; +import { select } from '../math/CondNode.js'; +import { ShaderNode, nodeProxy, getCurrentStack, setCurrentStack } from '../shadernode/ShaderNode.js'; + +class StackNode extends Node { + constructor(parent = null) { + super(); + + this.nodes = []; + this.outputNode = null; + + this.parent = parent; + + this._currentCond = null; + + this.isStackNode = true; + } + + getNodeType(builder) { + return this.outputNode ? this.outputNode.getNodeType(builder) : 'void'; + } + + add(node) { + this.nodes.push(node); + + return this; + } + + If(boolNode, method) { + const methodNode = new ShaderNode(method); + this._currentCond = select(boolNode, methodNode); + + return this.add(this._currentCond); + } + + ElseIf(boolNode, method) { + const methodNode = new ShaderNode(method); + const ifNode = select(boolNode, methodNode); + + this._currentCond.elseNode = ifNode; + this._currentCond = ifNode; + + return this; + } + + Else(method) { + this._currentCond.elseNode = new ShaderNode(method); + + return this; + } + + build(builder, ...params) { + const previousStack = getCurrentStack(); + + setCurrentStack(this); + + for (const node of this.nodes) { + node.build(builder, 'void'); + } + + setCurrentStack(previousStack); + + return this.outputNode ? this.outputNode.build(builder, ...params) : super.build(builder, ...params); + } + + // + + else(...params) { + // @deprecated, r168 + + console.warn('TSL.StackNode: .else() has been renamed to .Else().'); + return this.Else(...params); + } + + elseif(...params) { + // @deprecated, r168 + + console.warn('TSL.StackNode: .elseif() has been renamed to .ElseIf().'); + return this.ElseIf(...params); + } +} + +export default StackNode; + +export const stack = nodeProxy(StackNode); + +addNodeClass('StackNode', StackNode); diff --git a/src-testing/src/nodes/core/StructTypeNode.ts b/src-testing/src/nodes/core/StructTypeNode.ts new file mode 100644 index 000000000..697187999 --- /dev/null +++ b/src-testing/src/nodes/core/StructTypeNode.ts @@ -0,0 +1,18 @@ +import Node, { addNodeClass } from './Node.js'; + +class StructTypeNode extends Node { + constructor(types) { + super(); + + this.types = types; + this.isStructTypeNode = true; + } + + getMemberTypes() { + return this.types; + } +} + +export default StructTypeNode; + +addNodeClass('StructTypeNode', StructTypeNode); diff --git a/src-testing/src/nodes/core/TempNode.d.ts b/src-testing/src/nodes/core/TempNode.d.ts new file mode 100644 index 000000000..367f5ade1 --- /dev/null +++ b/src-testing/src/nodes/core/TempNode.d.ts @@ -0,0 +1,10 @@ +import Node from "./Node.js"; +import NodeBuilder from "./NodeBuilder.js"; + +export default class TempNode extends Node { + isTempNode: true; + + constructor(type: string | null); + + hasDependencies(builder: NodeBuilder): boolean; +} diff --git a/src-testing/src/nodes/core/UniformGroup.d.ts b/src-testing/src/nodes/core/UniformGroup.d.ts new file mode 100644 index 000000000..60339877b --- /dev/null +++ b/src-testing/src/nodes/core/UniformGroup.d.ts @@ -0,0 +1,7 @@ +export default class UniformGroup { + name: string; + + readonly isUniformGroup: true; + + constructor(name: string); +} diff --git a/src-testing/src/nodes/core/UniformGroupNode.ts b/src-testing/src/nodes/core/UniformGroupNode.ts new file mode 100644 index 000000000..4a4eed227 --- /dev/null +++ b/src-testing/src/nodes/core/UniformGroupNode.ts @@ -0,0 +1,46 @@ +import Node from './Node.js'; +import { addNodeClass } from './Node.js'; + +class UniformGroupNode extends Node { + constructor(name, shared = false) { + super('string'); + + this.name = name; + this.version = 0; + + this.shared = shared; + + this.isUniformGroup = true; + } + + set needsUpdate(value) { + if (value === true) this.version++; + } + + serialize(data) { + super.serialize(data); + + data.name = this.name; + data.version = this.version; + data.shared = this.shared; + } + + deserialize(data) { + super.deserialize(data); + + this.name = data.name; + this.version = data.version; + this.shared = data.shared; + } +} + +export const uniformGroup = name => new UniformGroupNode(name); +export const sharedUniformGroup = name => new UniformGroupNode(name, true); + +export const frameGroup = sharedUniformGroup('frame'); +export const renderGroup = sharedUniformGroup('render'); +export const objectGroup = uniformGroup('object'); + +export default UniformGroupNode; + +addNodeClass('UniformGroupNode', UniformGroupNode); diff --git a/src-testing/src/nodes/core/UniformNode.ts b/src-testing/src/nodes/core/UniformNode.ts new file mode 100644 index 000000000..41e523c4f --- /dev/null +++ b/src-testing/src/nodes/core/UniformNode.ts @@ -0,0 +1,90 @@ +import InputNode from './InputNode.js'; +import { objectGroup } from './UniformGroupNode.js'; +import { addNodeClass } from './Node.js'; +import { nodeObject, getConstNodeType } from '../shadernode/ShaderNode.js'; + +class UniformNode extends InputNode { + constructor(value, nodeType = null) { + super(value, nodeType); + + this.isUniformNode = true; + + this.name = ''; + this.groupNode = objectGroup; + } + + label(name) { + this.name = name; + + return this; + } + + setGroup(group) { + this.groupNode = group; + + return this; + } + + getGroup() { + return this.groupNode; + } + + getUniformHash(builder) { + return this.getHash(builder); + } + + onUpdate(callback, updateType) { + const self = this.getSelf(); + + callback = callback.bind(self); + + return super.onUpdate(frame => { + const value = callback(frame, self); + + if (value !== undefined) { + this.value = value; + } + }, updateType); + } + + generate(builder, output) { + const type = this.getNodeType(builder); + + const hash = this.getUniformHash(builder); + + let sharedNode = builder.getNodeFromHash(hash); + + if (sharedNode === undefined) { + builder.setHashNode(this, hash); + + sharedNode = this; + } + + const sharedNodeType = sharedNode.getInputType(builder); + + const nodeUniform = builder.getUniformFromNode( + sharedNode, + sharedNodeType, + builder.shaderStage, + this.name || builder.context.label, + ); + const propertyName = builder.getPropertyName(nodeUniform); + + if (builder.context.label !== undefined) delete builder.context.label; + + return builder.format(propertyName, type, output); + } +} + +export default UniformNode; + +export const uniform = (arg1, arg2) => { + const nodeType = getConstNodeType(arg2 || arg1); + + // @TODO: get ConstNode from .traverse() in the future + const value = arg1 && arg1.isNode === true ? (arg1.node && arg1.node.value) || arg1.value : arg1; + + return nodeObject(new UniformNode(value, nodeType)); +}; + +addNodeClass('UniformNode', UniformNode); diff --git a/src-testing/src/nodes/core/VarNode.d.ts b/src-testing/src/nodes/core/VarNode.d.ts new file mode 100644 index 000000000..b5de30a25 --- /dev/null +++ b/src-testing/src/nodes/core/VarNode.d.ts @@ -0,0 +1,20 @@ +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import Node from "./Node.js"; + +export default class VarNode extends Node { + node: Node; + name: string | null; + + readonly isVarNode: true; + + constructor(node: Node, name?: string | null); +} + +export const temp: (node: NodeRepresentation, name?: string | null) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + temp: typeof temp; + toVar: typeof temp; + } +} diff --git a/src-testing/src/nodes/core/VaryingNode.d.ts b/src-testing/src/nodes/core/VaryingNode.d.ts new file mode 100644 index 000000000..cbdf5e3dd --- /dev/null +++ b/src-testing/src/nodes/core/VaryingNode.d.ts @@ -0,0 +1,21 @@ +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import Node from "./Node.js"; +import NodeBuilder from "./NodeBuilder.js"; +import NodeVarying from "./NodeVarying.js"; + +export default class VaryingNode extends Node { + node: Node; + name: string | null; + + constructor(node: Node, name?: string | null); + + setupVarying(builder: NodeBuilder): NodeVarying; +} + +export const varying: (node: NodeRepresentation, name?: string) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + varying: typeof varying; + } +} diff --git a/src-testing/src/nodes/core/constants.ts b/src-testing/src/nodes/core/constants.ts new file mode 100644 index 000000000..3b01a9a6d --- /dev/null +++ b/src-testing/src/nodes/core/constants.ts @@ -0,0 +1,28 @@ +export const NodeShaderStage = { + VERTEX: 'vertex', + FRAGMENT: 'fragment', +}; + +export const NodeUpdateType = { + NONE: 'none', + FRAME: 'frame', + RENDER: 'render', + OBJECT: 'object', +}; + +export const NodeType = { + BOOLEAN: 'bool', + INTEGER: 'int', + FLOAT: 'float', + VECTOR2: 'vec2', + VECTOR3: 'vec3', + VECTOR4: 'vec4', + MATRIX2: 'mat2', + MATRIX3: 'mat3', + MATRIX4: 'mat4', +}; + +export const defaultShaderStages = ['fragment', 'vertex']; +export const defaultBuildStages = ['setup', 'analyze', 'generate']; +export const shaderStages = [...defaultShaderStages, 'compute']; +export const vectorComponents = ['x', 'y', 'z', 'w']; diff --git a/src-testing/src/nodes/display/AfterImageNode.d.ts b/src-testing/src/nodes/display/AfterImageNode.d.ts new file mode 100644 index 000000000..336be14c7 --- /dev/null +++ b/src-testing/src/nodes/display/AfterImageNode.d.ts @@ -0,0 +1,25 @@ +import TextureNode from "../accessors/TextureNode.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import UniformNode from "../core/UniformNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class AfterImageNode extends TempNode { + textureNode: TextureNode; + textureNodeOld: Node; + damp: UniformNode; + + constructor(textureNode: Node, damp?: number); + + getTextureNode(): TextureNode; + + setSize(width: number, height: number): void; +} + +export const afterImage: (node: Node, damp?: number) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + afterImage: typeof afterImage; + } +} diff --git a/src-testing/src/nodes/display/AnamorphicNode.d.ts b/src-testing/src/nodes/display/AnamorphicNode.d.ts new file mode 100644 index 000000000..5077a2caf --- /dev/null +++ b/src-testing/src/nodes/display/AnamorphicNode.d.ts @@ -0,0 +1,31 @@ +import { Vector2 } from "../../math/Vector2.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class AnamorphicNode extends TempNode { + textureNode: Node; + thresholdNode: Node; + scaleNode: Node; + samples: number; + resolution: Vector2; + + constructor(textureNode: Node, thresholdNode: Node, scaleNode: Node, samples: number); + + getTextureNode(): Node; + + setSize(width: number, height: number): void; +} + +export const anamorphic: ( + node: Node, + threshold?: NodeRepresentation, + scale?: NodeRepresentation, + samples?: NodeRepresentation, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + anamorphic: typeof anamorphic; + } +} diff --git a/src-testing/src/nodes/display/BleachBypassNode.d.ts b/src-testing/src/nodes/display/BleachBypassNode.d.ts new file mode 100644 index 000000000..335de1c67 --- /dev/null +++ b/src-testing/src/nodes/display/BleachBypassNode.d.ts @@ -0,0 +1,10 @@ +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const bleach: (color: NodeRepresentation, opacity?: number) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + bleach: typeof bleach; + } +} diff --git a/src-testing/src/nodes/display/BlendModeNode.d.ts b/src-testing/src/nodes/display/BlendModeNode.d.ts new file mode 100644 index 000000000..c335f8295 --- /dev/null +++ b/src-testing/src/nodes/display/BlendModeNode.d.ts @@ -0,0 +1,47 @@ +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { JoinNode } from "../Nodes.js"; +import { NodeRepresentation, ShaderNode, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const BurnNode: (args: { base: Node; blend: Node }) => ShaderNodeObject; + +export const DodgeNode: (args: { base: Node; blend: Node }) => ShaderNodeObject; + +export const ScreenNode: (args: { base: Node; blend: Node }) => ShaderNodeObject; + +export const OverlayNode: (args: { base: Node; blend: Node }) => ShaderNodeObject; + +export type BlendMode = + | typeof BlendModeNode.BURN + | typeof BlendModeNode.DODGE + | typeof BlendModeNode.SCREEN + | typeof BlendModeNode.OVERLAY; + +export default class BlendModeNode extends TempNode { + static BURN: "burn"; + static DODGE: "dodge"; + static SCREEN: "screen"; + static OVERLAY: "overlay"; + + baseNode: Node; + blendMode: BlendMode; + blendNode: Node; + + constructor(blendMode: BlendMode, baseNode: Node, blendNode: Node); + + setup(): Node; +} + +export const burn: (baseNode: NodeRepresentation, blendNode?: NodeRepresentation) => ShaderNodeObject; +export const dodge: (baseNode: NodeRepresentation, blendNode?: NodeRepresentation) => ShaderNodeObject; +export const overlay: (baseNode: NodeRepresentation, blendNode?: NodeRepresentation) => ShaderNodeObject; +export const screen: (baseNode: NodeRepresentation, blendNode?: NodeRepresentation) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + burn: typeof burn; + dodge: typeof dodge; + overlay: typeof overlay; + screen: typeof screen; + } +} diff --git a/src-testing/src/nodes/display/BloomNode.d.ts b/src-testing/src/nodes/display/BloomNode.d.ts new file mode 100644 index 000000000..4a057236b --- /dev/null +++ b/src-testing/src/nodes/display/BloomNode.d.ts @@ -0,0 +1,35 @@ +import TextureNode from "../accessors/TextureNode.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import UniformNode from "../core/UniformNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +declare class BloomNode extends TempNode { + inputNode: Node; + strength: UniformNode; + radius: UniformNode; + threshold: UniformNode; + + smoothWidth: UniformNode; + + constructor(inputNode: Node, strength?: number, radius?: number, threshold?: number); + + getTexture(): ShaderNodeObject; + + setSize(width: number, height: number): void; +} + +export const bloom: ( + node: NodeRepresentation, + strength?: number, + radius?: number, + threshold?: number, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + bloom: typeof bloom; + } +} + +export default BloomNode; diff --git a/src-testing/src/nodes/display/ColorAdjustmentNode.d.ts b/src-testing/src/nodes/display/ColorAdjustmentNode.d.ts new file mode 100644 index 000000000..b49345bbb --- /dev/null +++ b/src-testing/src/nodes/display/ColorAdjustmentNode.d.ts @@ -0,0 +1,47 @@ +import TempNode from "../core/TempNode.js"; +import MathNode from "../math/MathNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export type ColorAdjustmentMethod = + | typeof ColorAdjustmentNode.SATURATION + | typeof ColorAdjustmentNode.VIBRANCE + | typeof ColorAdjustmentNode.HUE; + +export default class ColorAdjustmentNode extends TempNode { + static SATURATION: "saturation"; + static VIBRANCE: "vibrance"; + static HUE: "hue"; + + method: ColorAdjustmentMethod; + + colorNode: Node; + adjustmentNode: Node; + + constructor(method: ColorAdjustmentMethod, colorNode: Node, adjustmentNode?: Node); +} + +export const saturation: ( + colorNode: NodeRepresentation, + adjustmentNode?: NodeRepresentation, +) => ShaderNodeObject; +export const vibrance: ( + colorNode: NodeRepresentation, + adjustmentNode?: NodeRepresentation, +) => ShaderNodeObject; +export const hue: ( + colorNode: NodeRepresentation, + adjustmentNode?: NodeRepresentation, +) => ShaderNodeObject; + +export const luminance: (a: NodeRepresentation, b: NodeRepresentation) => ShaderNodeObject; + +export const threshold: (color: NodeRepresentation, thershold: NodeRepresentation) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + saturation: typeof saturation; + vibrance: typeof vibrance; + hue: typeof hue; + threshold: typeof threshold; + } +} diff --git a/src-testing/src/nodes/display/ColorSpaceNode.d.ts b/src-testing/src/nodes/display/ColorSpaceNode.d.ts new file mode 100644 index 000000000..e4569f7fd --- /dev/null +++ b/src-testing/src/nodes/display/ColorSpaceNode.d.ts @@ -0,0 +1,35 @@ +import { ColorSpace } from "../../constants.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export type ColorSpaceNodeMethod = + | typeof ColorSpaceNode.LINEAR_TO_LINEAR + | typeof ColorSpaceNode.LINEAR_TO_sRGB + | typeof ColorSpaceNode.sRGB_TO_LINEAR; + +export default class ColorSpaceNode extends TempNode { + static LINEAR_TO_LINEAR: "LinearToLinear"; + static LINEAR_TO_sRGB: "LinearTosRGB"; + static sRGB_TO_LINEAR: "sRGBToLinear"; + + method: ColorSpaceNodeMethod; + node: Node; + + constructor(method: ColorSpaceNodeMethod | null, node: Node); +} + +export const linearToColorSpace: (node: NodeRepresentation, colorSpace: ColorSpace) => ShaderNodeObject; +export const colorSpaceToLinear: (node: NodeRepresentation, colorSpace: ColorSpace) => ShaderNodeObject; + +export const linearTosRGB: (node: NodeRepresentation) => ShaderNodeObject; +export const sRGBToLinear: (node: NodeRepresentation) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + linearTosRGB: typeof linearTosRGB; + sRGBToLinear: typeof sRGBToLinear; + linearToColorSpace: typeof linearToColorSpace; + colorSpaceToLinear: typeof colorSpaceToLinear; + } +} diff --git a/src-testing/src/nodes/display/DenoiseNode.d.ts b/src-testing/src/nodes/display/DenoiseNode.d.ts new file mode 100644 index 000000000..8b5699dfa --- /dev/null +++ b/src-testing/src/nodes/display/DenoiseNode.d.ts @@ -0,0 +1,38 @@ +import { Camera } from "../../cameras/Camera.js"; +import { Matrix4 } from "../../math/Matrix4.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import UniformNode from "../core/UniformNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +declare class DenoiseNode extends TempNode { + textureNode: Node; + depthNode: Node; + normalNode: Node; + noiseNode: Node; + + cameraProjectionMatrixInversion: UniformNode; + lumaPhi: UniformNode; + depthPhi: UniformNode; + normalPhi: UniformNode; + radius: UniformNode; + index: UniformNode; + + constructor(textureNode: Node, depthNode: Node, normalNode: Node, noiseNode: Node, camera: Camera); +} + +export const denoise: ( + node: NodeRepresentation, + depthNode: NodeRepresentation, + normalNode: NodeRepresentation, + noiseNode: NodeRepresentation, + camera: Camera, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + denoise: typeof denoise; + } +} + +export default DenoiseNode; diff --git a/src-testing/src/nodes/display/DepthOfFieldNode.d.ts b/src-testing/src/nodes/display/DepthOfFieldNode.d.ts new file mode 100644 index 000000000..e58e801bc --- /dev/null +++ b/src-testing/src/nodes/display/DepthOfFieldNode.d.ts @@ -0,0 +1,30 @@ +import TextureNode from "../accessors/TextureNode.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import UniformNode from "../core/UniformNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class DepthOfFieldNode extends TempNode { + textureNode: TextureNode; + viewZNode: Node; + + focus: UniformNode; + aperture: UniformNode; + maxblur: UniformNode; + + constructor(textureNode: TextureNode, viewZNode: Node, focusNode: Node, apertureNode: Node, maxblurNode: Node); +} + +export const dof: ( + node: NodeRepresentation, + viewZNode: NodeRepresentation, + focus?: NodeRepresentation, + aperture?: NodeRepresentation, + maxblur?: NodeRepresentation, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + dof: typeof dof; + } +} diff --git a/src-testing/src/nodes/display/DotScreenNode.d.ts b/src-testing/src/nodes/display/DotScreenNode.d.ts new file mode 100644 index 000000000..8d2c7a66f --- /dev/null +++ b/src-testing/src/nodes/display/DotScreenNode.d.ts @@ -0,0 +1,27 @@ +import { Vector2 } from "../../math/Vector2.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import UniformNode from "../core/UniformNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class DotScreenNode extends TempNode { + inputNode: Node; + center: UniformNode; + angle: UniformNode; + scale: UniformNode; + + constructor(inputNode: Node, center?: Vector2, angle?: number, scale?: number); +} + +export const dotScreen: ( + node: NodeRepresentation, + center?: Vector2, + angle?: number, + scale?: number, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + dotScreen: typeof dotScreen; + } +} diff --git a/src-testing/src/nodes/display/FXAANode.d.ts b/src-testing/src/nodes/display/FXAANode.d.ts new file mode 100644 index 000000000..ed23217e6 --- /dev/null +++ b/src-testing/src/nodes/display/FXAANode.d.ts @@ -0,0 +1,19 @@ +import TextureNode from "../accessors/TextureNode.js"; +import TempNode from "../core/TempNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +declare class FXAANode extends TempNode { + textureNode: TextureNode; + + constructor(textureNode: TextureNode); +} + +export const fxaa: (node: NodeRepresentation) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + fxaa: typeof fxaa; + } +} + +export default FXAANode; diff --git a/src-testing/src/nodes/display/FilmNode.d.ts b/src-testing/src/nodes/display/FilmNode.d.ts new file mode 100644 index 000000000..b9febaaae --- /dev/null +++ b/src-testing/src/nodes/display/FilmNode.d.ts @@ -0,0 +1,25 @@ +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +declare class FilmNode extends TempNode { + inputNode: Node; + intensityNode: Node | null; + uvNode: Node | null; + + constructor(inputNode: Node, intensityNode?: Node | null, uvNode?: Node | null); +} + +export const film: ( + inputNode: NodeRepresentation, + intensityNode?: NodeRepresentation | null, + uvNode?: NodeRepresentation | null, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + film: typeof film; + } +} + +export default FilmNode; diff --git a/src-testing/src/nodes/display/FrontFacingNode.d.ts b/src-testing/src/nodes/display/FrontFacingNode.d.ts new file mode 100644 index 000000000..95a6d6906 --- /dev/null +++ b/src-testing/src/nodes/display/FrontFacingNode.d.ts @@ -0,0 +1,10 @@ +import Node from "../core/Node.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class FrontFacingNode extends Node { + isFrontFacingNode: true; + constructor(); +} + +export const frontFacing: ShaderNodeObject; +export const faceDirection: ShaderNodeObject; diff --git a/src-testing/src/nodes/display/GTAONode.d.ts b/src-testing/src/nodes/display/GTAONode.d.ts new file mode 100644 index 000000000..5a6da521b --- /dev/null +++ b/src-testing/src/nodes/display/GTAONode.d.ts @@ -0,0 +1,46 @@ +import { Camera } from "../../cameras/Camera.js"; +import { Matrix4 } from "../../math/Matrix4.js"; +import { Vector2 } from "../../math/Vector2.js"; +import TextureNode from "../accessors/TextureNode.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import UniformNode from "../core/UniformNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +declare class GTAONode extends TempNode { + depthNode: Node; + normalNode: Node; + + radius: ShaderNodeObject>; + resolution: ShaderNodeObject>; + thickness: ShaderNodeObject>; + distanceExponent: ShaderNodeObject>; + distanceFallOff: ShaderNodeObject>; + scale: ShaderNodeObject>; + noiseNode: ShaderNodeObject; + + cameraProjectionMatrix: ShaderNodeObject>; + cameraProjectionMatrixInverse: ShaderNodeObject>; + + SAMPLES: ShaderNodeObject>; + + constructor(depthNode: Node, normalNode: Node, camera: Camera); + + getTextureNode(): ShaderNodeObject; + + setSize(width: number, height: number): void; +} + +export const ao: ( + depthNode: NodeRepresentation, + normalNode: NodeRepresentation, + camera: Camera, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + ao: typeof ao; + } +} + +export default GTAONode; diff --git a/src-testing/src/nodes/display/GaussianBlurNode.d.ts b/src-testing/src/nodes/display/GaussianBlurNode.d.ts new file mode 100644 index 000000000..6cec09eb3 --- /dev/null +++ b/src-testing/src/nodes/display/GaussianBlurNode.d.ts @@ -0,0 +1,31 @@ +import { Vector2 } from "../../math/Vector2.js"; +import TextureNode from "../accessors/TextureNode.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class GaussianBlurNode extends TempNode { + textureNode: TextureNode; + directionNode: Node | null; + sigma: number; + + resolution: Vector2; + + constructor(textureNode: TextureNode, directionNode?: Node | null, sigma?: number); + + setSize(width: number, height: number): void; + + getTextureNode(): TextureNode; +} + +export const gaussianBlur: ( + node: NodeRepresentation, + directionNode?: NodeRepresentation | null, + sigma?: number, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + gaussianBlur: typeof gaussianBlur; + } +} diff --git a/src-testing/src/nodes/display/Lut3DNode.d.ts b/src-testing/src/nodes/display/Lut3DNode.d.ts new file mode 100644 index 000000000..a257845d7 --- /dev/null +++ b/src-testing/src/nodes/display/Lut3DNode.d.ts @@ -0,0 +1,30 @@ +import { Data3DTexture } from "../../textures/Data3DTexture.js"; +import Texture3DNode from "../accessors/Texture3DNode.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import UniformNode from "../core/UniformNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +declare class Lut3DNode extends TempNode { + inputNode: Node; + lutNode: Texture3DNode; + size: ShaderNodeObject>; + intensityNode: UniformNode; + + constructor(inputNode: Node, lutNode: UniformNode, size: number, intensityNode: UniformNode); +} + +export const lut3D: ( + node: NodeRepresentation, + lut: NodeRepresentation, + size: number, + intensity: NodeRepresentation, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + lut3D: typeof lut3D; + } +} + +export default Lut3DNode; diff --git a/src-testing/src/nodes/display/NormalMapNode.d.ts b/src-testing/src/nodes/display/NormalMapNode.d.ts new file mode 100644 index 000000000..82b850547 --- /dev/null +++ b/src-testing/src/nodes/display/NormalMapNode.d.ts @@ -0,0 +1,21 @@ +import { NormalMapTypes } from "../../constants.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class NormalMapNode extends TempNode { + node: Node; + scaleNode: Node | null; + + normalMapType: NormalMapTypes; + + constructor(node: Node, scaleNode?: Node | null); +} + +export const normalMap: (node: Node, scaleNode?: Node) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + normalMap: typeof normalMap; + } +} diff --git a/src-testing/src/nodes/display/PassNode.d.ts b/src-testing/src/nodes/display/PassNode.d.ts new file mode 100644 index 000000000..9b385818f --- /dev/null +++ b/src-testing/src/nodes/display/PassNode.d.ts @@ -0,0 +1,71 @@ +import { Camera } from "../../cameras/Camera.js"; +import { RenderTarget, RenderTargetOptions } from "../../core/RenderTarget.js"; +import { Scene } from "../../scenes/Scene.js"; +import { Texture } from "../../textures/Texture.js"; +import TextureNode from "../accessors/TextureNode.js"; +import MRTNode from "../core/MRTNode.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +declare class PassTextureNode extends TextureNode { + passNode: PassNode; + + constructor(passNode: PassNode, texture: Texture); +} + +declare class PassMultipleTextureNode extends PassTextureNode { + textureName: string; + previousTexture: boolean; + + constructor(passNode: PassNode, textureName: string, previousTexture?: boolean); + + updateTexture(): void; +} + +declare class PassNode extends TempNode { + scope: PassNodeScope; + scene: Scene; + camera: Camera; + + renderTarget: RenderTarget; + + readonly isPassNode: true; + + constructor(scope: PassNodeScope, scene: Scene, camera: Camera, options?: RenderTargetOptions); + + setMRT(mrt: MRTNode | null): this; + + getMRT(): MRTNode | null; + + getTexture(name: string): Texture; + + getPreviousTexture(name: string): Texture; + + toggleTexture(name: string): void; + + getTextureNode(name?: string): ShaderNodeObject; + + getPreviousTextureNode(name?: string): ShaderNodeObject; + + getViewZNode(name?: string): ShaderNodeObject; + + getLinearDepthNode(name?: string): ShaderNodeObject; + + setSize(width: number, height: number): void; + + setPixelRatio(pixelRatio: number): void; + + dispose(): void; + + static COLOR: "color"; + static DEPTH: "depth"; +} + +export type PassNodeScope = typeof PassNode.COLOR | typeof PassNode.DEPTH; + +export default PassNode; + +export const pass: (scene: Scene, camera: Camera, options?: RenderTargetOptions) => ShaderNodeObject; +export const passTexture: (pass: PassNode, texture: Texture) => ShaderNodeObject; +export const depthPass: (scene: Scene, camera: Camera) => ShaderNodeObject; diff --git a/src-testing/src/nodes/display/PixelationPassNode.d.ts b/src-testing/src/nodes/display/PixelationPassNode.d.ts new file mode 100644 index 000000000..e8e76aca8 --- /dev/null +++ b/src-testing/src/nodes/display/PixelationPassNode.d.ts @@ -0,0 +1,67 @@ +import { Camera } from "../../cameras/Camera.js"; +import { Scene } from "../../scenes/Scene.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import UniformNode from "../core/UniformNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import PassNode from "./PassNode.js"; + +declare class PixelationNode extends TempNode { + textureNode: Node; + depthNode: Node; + normalNode: Node; + + pixelSize: Node; + normalEdgeStrength: Node; + depthEdgeStrength: Node; + + constructor( + textureNode: Node, + depthNode: Node, + normalNode: Node, + pixelSize: Node, + normalEdgeStrength: Node, + depthEdgeStrength: Node, + ); +} + +declare const pixelation: ( + node: NodeRepresentation, + depthNode: NodeRepresentation, + normalNode: NodeRepresentation, + pixelSize?: number, + normalEdgeStrength?: number, + depthEdgeStrength?: number, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + pixelation: typeof pixelation; + } +} + +declare class PixelationPassNode extends PassNode { + pixelSize: UniformNode; + normalEdgeStrength: UniformNode; + depthEdgeStrength: UniformNode; + + readonly isPixelationPassNode: true; + + constructor( + scene: Scene, + camera: Camera, + pixelSize: number, + normalEdgeStrength: number, + depthEdgeStrength: number, + ); +} + +export const pixelationPass: ( + scene: Scene, + camera: Camera, + pixelSize: UniformNode, + normalEdgeStrength: UniformNode, + depthEdgeStrength: UniformNode, +) => ShaderNodeObject; + +export default PixelationPassNode; diff --git a/src-testing/src/nodes/display/PosterizeNode.d.ts b/src-testing/src/nodes/display/PosterizeNode.d.ts new file mode 100644 index 000000000..36aa66d8e --- /dev/null +++ b/src-testing/src/nodes/display/PosterizeNode.d.ts @@ -0,0 +1,20 @@ +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class PosterizeNode extends Node { + sourceNode: Node; + stepsNode: Node; + + constructor(sourceNode: Node, stepsNode: Node); +} + +export const posterize: ( + sourceNode: NodeRepresentation, + stepsNode: NodeRepresentation, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + posterize: typeof posterize; + } +} diff --git a/src-testing/src/nodes/display/RGBShiftNode.d.ts b/src-testing/src/nodes/display/RGBShiftNode.d.ts new file mode 100644 index 000000000..804094aba --- /dev/null +++ b/src-testing/src/nodes/display/RGBShiftNode.d.ts @@ -0,0 +1,24 @@ +import TextureNode from "../accessors/TextureNode.js"; +import TempNode from "../core/TempNode.js"; +import UniformNode from "../core/UniformNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class RGBShiftNode extends TempNode { + textureNode: TextureNode; + amount: UniformNode; + angle: UniformNode; + + constructor(textureNode: TextureNode, amount?: number, angle?: number); + + getTextureNode(): TextureNode; + + setSize(width: number, height: number): void; +} + +export const rgbShift: (node: NodeRepresentation, amount?: number, angle?: number) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + rgbShift: typeof rgbShift; + } +} diff --git a/src-testing/src/nodes/display/RenderOutputNode.d.ts b/src-testing/src/nodes/display/RenderOutputNode.d.ts new file mode 100644 index 000000000..c41f56f65 --- /dev/null +++ b/src-testing/src/nodes/display/RenderOutputNode.d.ts @@ -0,0 +1,28 @@ +import { ColorSpace, ToneMapping } from "../../constants.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +declare class RenderOutputNode extends TempNode { + colorNode: Node; + toneMapping: ToneMapping | null; + outputColorSpace: ColorSpace | null; + + readonly isRenderOutput: true; + + constructor(colorNode: Node, toneMapping?: ToneMapping | null, outputColorSpace?: ColorSpace | null); +} + +export default RenderOutputNode; + +export const renderOutput: ( + color: NodeRepresentation, + toneMapping?: ToneMapping | null, + outputColorSpace?: ColorSpace | null, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + renderOutput: typeof renderOutput; + } +} diff --git a/src-testing/src/nodes/display/SepiaNode.d.ts b/src-testing/src/nodes/display/SepiaNode.d.ts new file mode 100644 index 000000000..d5c3007a3 --- /dev/null +++ b/src-testing/src/nodes/display/SepiaNode.d.ts @@ -0,0 +1,10 @@ +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const sepia: (args: [NodeRepresentation]) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + sepia: typeof sepia; + } +} diff --git a/src-testing/src/nodes/display/SobelOperatorNode.d.ts b/src-testing/src/nodes/display/SobelOperatorNode.d.ts new file mode 100644 index 000000000..ab49780f9 --- /dev/null +++ b/src-testing/src/nodes/display/SobelOperatorNode.d.ts @@ -0,0 +1,17 @@ +import TextureNode from "../accessors/TextureNode.js"; +import TempNode from "../core/TempNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class SobelOperatorNode extends TempNode { + textureNode: TextureNode; + + constructor(textureNode: TextureNode); +} + +export const sobel: (node: NodeRepresentation) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + sobel: typeof sobel; + } +} diff --git a/src-testing/src/nodes/display/ToneMappingNode.d.ts b/src-testing/src/nodes/display/ToneMappingNode.d.ts new file mode 100644 index 000000000..95dabe473 --- /dev/null +++ b/src-testing/src/nodes/display/ToneMappingNode.d.ts @@ -0,0 +1,33 @@ +import { ToneMapping } from "../../constants.js"; +import RendererReferenceNode from "../accessors/RendererReferenceNode.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +// exposure only +export const LinearToneMappingNode: Node; + +export default class ToneMappingNode extends TempNode { + toneMapping: ToneMapping; + exposureNode: Node; + colorNode: Node | null; + + constructor(toneMapping: ToneMapping, exposureNode?: Node, colorNode?: Node | null); +} + +export const toneMapping: ( + mapping: ToneMapping, + exposure: NodeRepresentation, + color?: NodeRepresentation, +) => ShaderNodeObject; +export const toneMappingExposure: ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + toneMapping: ( + color: NodeRepresentation, + mapping?: NodeRepresentation, + exposure?: NodeRepresentation, + ) => ShaderNodeObject; + } +} diff --git a/src-testing/src/nodes/display/TransitionNode.d.ts b/src-testing/src/nodes/display/TransitionNode.d.ts new file mode 100644 index 000000000..745578cf4 --- /dev/null +++ b/src-testing/src/nodes/display/TransitionNode.d.ts @@ -0,0 +1,41 @@ +import TextureNode from "../accessors/TextureNode.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import UniformNode from "../core/UniformNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +declare class TransitionNode extends TempNode { + textureNodeA: TextureNode; + textureNodeB: TextureNode; + mixTextureNode: TextureNode; + + mixRatioNode: Node; + thresholdNode: Node; + useTextureNode: Node; + + constructor( + textureNodeA: TextureNode, + textureNodeB: TextureNode, + mixTextureNode: TextureNode, + mixRatioNode: Node, + thresholdNode: Node, + useTextureNode: Node, + ); +} + +export const transition: ( + node: NodeRepresentation, + nodeB: NodeRepresentation, + mixTexture: NodeRepresentation, + mixRatio: UniformNode, + threshold: UniformNode, + useTexture: UniformNode, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + transition: typeof transition; + } +} + +export default TransitionNode; diff --git a/src-testing/src/nodes/display/ViewportDepthNode.d.ts b/src-testing/src/nodes/display/ViewportDepthNode.d.ts new file mode 100644 index 000000000..5b8324748 --- /dev/null +++ b/src-testing/src/nodes/display/ViewportDepthNode.d.ts @@ -0,0 +1,30 @@ +import Node from "../core/Node.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class ViewportDepthNode extends Node { + scope: ViewportDepthNodeScope; + valueNode: Node; + + readonly isViewportDepthNode: true; + + constructor(scope: ViewportDepthNodeScope, valueNode?: Node | null); + + static DEPTH: "depth"; + static LINEAR_DEPTH: "linearDepth"; +} + +export type ViewportDepthNodeScope = + | typeof ViewportDepthNode.DEPTH + | typeof ViewportDepthNode.LINEAR_DEPTH; + +export const viewZToOrthographicDepth: (viewZ: Node, near: Node, far: Node) => Node; + +export const orthographicDepthToViewZ: (depth: Node, near: Node, far: Node) => Node; + +export const viewZToPerspectiveDepth: (viewZ: Node, near: Node, far: Node) => Node; + +export const perspectiveDepthToViewZ: (depth: Node, near: Node, far: Node) => Node; + +export const depth: ShaderNodeObject; +export const linearDepth: (valueNode?: Node | null) => ShaderNodeObject; +export const viewportLinearDepth: ShaderNodeObject; diff --git a/src-testing/src/nodes/display/ViewportDepthTextureNode.d.ts b/src-testing/src/nodes/display/ViewportDepthTextureNode.d.ts new file mode 100644 index 000000000..bb74b6c75 --- /dev/null +++ b/src-testing/src/nodes/display/ViewportDepthTextureNode.d.ts @@ -0,0 +1,12 @@ +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import ViewportTextureNode from "./ViewportTextureNode.js"; + +export default class ViewportDepthTextureNode extends ViewportTextureNode { + constructor(uvNode?: Node, levelNode?: Node | null); +} + +export const viewportDepthTexture: ( + uvNode?: NodeRepresentation, + levelNode?: NodeRepresentation, +) => ShaderNodeObject; diff --git a/src-testing/src/nodes/display/ViewportNode.d.ts b/src-testing/src/nodes/display/ViewportNode.d.ts new file mode 100644 index 000000000..401cd9ed4 --- /dev/null +++ b/src-testing/src/nodes/display/ViewportNode.d.ts @@ -0,0 +1,31 @@ +import Node from "../core/Node.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export type ViewportNodeScope = + | typeof ViewportNode.COORDINATE + | typeof ViewportNode.RESOLUTION + | typeof ViewportNode.TOP_LEFT + | typeof ViewportNode.BOTTOM_LEFT + | typeof ViewportNode.TOP_RIGHT + | typeof ViewportNode.BOTTOM_RIGHT; + +export default class ViewportNode extends Node { + static COORDINATE: "coordinate"; + static RESOLUTION: "resolution"; + static TOP_LEFT: "topLeft"; + static BOTTOM_LEFT: "bottomLeft"; + static TOP_RIGHT: "topRight"; + static BOTTOM_RIGHT: "bottomRight"; + + scope: ViewportNodeScope; + isViewportNode: true; + + constructor(scope: ViewportNodeScope); +} + +export const viewportCoordinate: ShaderNodeObject; +export const viewportResolution: ShaderNodeObject; +export const viewportTopLeft: ShaderNodeObject; +export const viewportBottomLeft: ShaderNodeObject; +export const viewportTopRight: ShaderNodeObject; +export const viewportBottomRight: ShaderNodeObject; diff --git a/src-testing/src/nodes/display/ViewportSharedTextureNode.d.ts b/src-testing/src/nodes/display/ViewportSharedTextureNode.d.ts new file mode 100644 index 000000000..2faf2bcd6 --- /dev/null +++ b/src-testing/src/nodes/display/ViewportSharedTextureNode.d.ts @@ -0,0 +1,18 @@ +import Node from "../core/Node.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import ViewportTextureNode from "./ViewportTextureNode.js"; + +export default class ViewportSharedTextureNode extends ViewportTextureNode { + constructor(uvNode?: Node, levelNode?: Node | null); +} + +export const viewportSharedTexture: ( + uvNode?: Node, + levelNode?: Node | null, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + viewportSharedTexture: typeof viewportSharedTexture; + } +} diff --git a/src-testing/src/nodes/display/ViewportTextureNode.d.ts b/src-testing/src/nodes/display/ViewportTextureNode.d.ts new file mode 100644 index 000000000..96b490766 --- /dev/null +++ b/src-testing/src/nodes/display/ViewportTextureNode.d.ts @@ -0,0 +1,33 @@ +import { FramebufferTexture } from "../../textures/FramebufferTexture.js"; +import TextureNode from "../accessors/TextureNode.js"; +import { NodeUpdateType } from "../core/constants.js"; +import Node from "../core/Node.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class ViewportTextureNode extends TextureNode { + generateMipmaps: boolean; + + readonly isOutputTextureNode: true; + + updateBeforeType: NodeUpdateType; + + constructor(uvNode?: Node, levelNode?: Node | null, framebufferTexture?: FramebufferTexture | null); +} + +export const viewportTexture: ( + uvNode?: Node, + levelNode?: Node | null, + framebufferTexture?: FramebufferTexture | null, +) => ShaderNodeObject; +export const viewportMipTexture: ( + uvNode?: Node, + levelNode?: Node | null, + framebufferTexture?: FramebufferTexture | null, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + viewportTexture: typeof viewportTexture; + viewportMipTexture: typeof viewportMipTexture; + } +} diff --git a/src-testing/src/nodes/fog/FogExp2Node.d.ts b/src-testing/src/nodes/fog/FogExp2Node.d.ts new file mode 100644 index 000000000..3ede63e79 --- /dev/null +++ b/src-testing/src/nodes/fog/FogExp2Node.d.ts @@ -0,0 +1,18 @@ +import Node from "../core/Node.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import FogNode from "./FogNode.js"; + +export default class FogExp2Node extends FogNode { + isFogExp2Node: true; + densityNode: Node; + + constructor(colorNode: Node, densityNode: Node); +} + +export const densityFog: (colorNode: Node, densityNode: Node) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + densityFog: typeof densityFog; + } +} diff --git a/src-testing/src/nodes/fog/FogNode.ts b/src-testing/src/nodes/fog/FogNode.ts new file mode 100644 index 000000000..9417df5a5 --- /dev/null +++ b/src-testing/src/nodes/fog/FogNode.ts @@ -0,0 +1,38 @@ +import Node, { addNodeClass } from '../core/Node.js'; +import { positionView } from '../accessors/PositionNode.js'; +import { addNodeElement, nodeProxy } from '../shadernode/ShaderNode.js'; + +class FogNode extends Node { + constructor(colorNode, factorNode) { + super('float'); + + this.isFogNode = true; + + this.colorNode = colorNode; + this.factorNode = factorNode; + } + + getViewZNode(builder) { + let viewZ; + + const getViewZ = builder.context.getViewZ; + + if (getViewZ !== undefined) { + viewZ = getViewZ(this); + } + + return (viewZ || positionView.z).negate(); + } + + setup() { + return this.factorNode; + } +} + +export default FogNode; + +export const fog = nodeProxy(FogNode); + +addNodeElement('fog', fog); + +addNodeClass('FogNode', FogNode); diff --git a/src-testing/src/nodes/fog/FogRangeNode.d.ts b/src-testing/src/nodes/fog/FogRangeNode.d.ts new file mode 100644 index 000000000..a1d37e7b6 --- /dev/null +++ b/src-testing/src/nodes/fog/FogRangeNode.d.ts @@ -0,0 +1,23 @@ +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import FogNode from "./FogNode.js"; + +export default class FogRangeNode extends FogNode { + isFogRangeNode: true; + nearNode: Node | null; + farNode: Node | null; + + constructor(colorNode: Node | null, nearNode: Node | null, farNode: Node | null); +} + +export const rangeFog: ( + colorNode: NodeRepresentation | null, + nearNode: NodeRepresentation | null, + farNode: NodeRepresentation | null, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + rangeFog: typeof rangeFog; + } +} diff --git a/src-testing/src/nodes/functions/BSDF/BRDF_GGX.d.ts b/src-testing/src/nodes/functions/BSDF/BRDF_GGX.d.ts new file mode 100644 index 000000000..066656347 --- /dev/null +++ b/src-testing/src/nodes/functions/BSDF/BRDF_GGX.d.ts @@ -0,0 +1,15 @@ +import Node from "../../core/Node.js"; +import OperatorNode from "../../math/OperatorNode.js"; +import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; + +declare const BRDF_GGX: (args: { + lightDirection: Node; + f0: Node; + f90: Node; + roughness: Node; + f?: Node; + USE_IRIDESCENCE?: Node; + USE_ANISOTROPY?: Node; +}) => ShaderNodeObject; + +export default BRDF_GGX; diff --git a/src-testing/src/nodes/functions/BSDF/BRDF_Lambert.d.ts b/src-testing/src/nodes/functions/BSDF/BRDF_Lambert.d.ts new file mode 100644 index 000000000..51df53226 --- /dev/null +++ b/src-testing/src/nodes/functions/BSDF/BRDF_Lambert.d.ts @@ -0,0 +1,7 @@ +import Node from "../../core/Node.js"; +import OperatorNode from "../../math/OperatorNode.js"; +import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; + +declare const BRDF_Lambert: (args: { diffuseColor: Node }) => ShaderNodeObject; + +export default BRDF_Lambert; diff --git a/src-testing/src/nodes/functions/BSDF/BRDF_Sheen.d.ts b/src-testing/src/nodes/functions/BSDF/BRDF_Sheen.d.ts new file mode 100644 index 000000000..67574acb1 --- /dev/null +++ b/src-testing/src/nodes/functions/BSDF/BRDF_Sheen.d.ts @@ -0,0 +1,7 @@ +import Node from "../../core/Node.js"; +import OperatorNode from "../../math/OperatorNode.js"; +import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; + +declare const BRDF_Sheen: (args: { lightDirection: Node }) => ShaderNodeObject; + +export default BRDF_Sheen; diff --git a/src-testing/src/nodes/functions/BSDF/DFGApprox.d.ts b/src-testing/src/nodes/functions/BSDF/DFGApprox.d.ts new file mode 100644 index 000000000..ed16ef5e5 --- /dev/null +++ b/src-testing/src/nodes/functions/BSDF/DFGApprox.d.ts @@ -0,0 +1,11 @@ +import Node from "../../core/Node.js"; +import OperatorNode from "../../math/OperatorNode.js"; +import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; + +// Analytical approximation of the DFG LUT, one half of the +// split-sum approximation used in indirect specular lighting. +// via 'environmentBRDF' from "Physically Based Shading on Mobile" +// https://www.unrealengine.com/blog/physically-based-shading-on-mobile +declare const DFGApprox: (args: { roughness: Node; dotNV: Node }) => ShaderNodeObject; + +export default DFGApprox; diff --git a/src-testing/src/nodes/functions/BSDF/D_GGX.d.ts b/src-testing/src/nodes/functions/BSDF/D_GGX.d.ts new file mode 100644 index 000000000..2ba114b41 --- /dev/null +++ b/src-testing/src/nodes/functions/BSDF/D_GGX.d.ts @@ -0,0 +1,10 @@ +import Node from "../../core/Node.js"; +import OperatorNode from "../../math/OperatorNode.js"; +import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; + +// Microfacet Models for Refraction through Rough Surfaces - equation (33) +// http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html +// alpha is "roughness squared" in Disney’s reparameterization +declare const D_GGX: (args: { alpha: Node; dotNH: Node }) => ShaderNodeObject; + +export default D_GGX; diff --git a/src-testing/src/nodes/functions/BSDF/D_GGX_Anisotropic.d.ts b/src-testing/src/nodes/functions/BSDF/D_GGX_Anisotropic.d.ts new file mode 100644 index 000000000..3de167db6 --- /dev/null +++ b/src-testing/src/nodes/functions/BSDF/D_GGX_Anisotropic.d.ts @@ -0,0 +1,10 @@ +import Node from "../../core/Node.js"; +import OperatorNode from "../../math/OperatorNode.js"; +import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; + +// https://google.github.io/filament/Filament.md.html#materialsystem/anisotropicmodel/anisotropicspecularbrdf +declare const D_GGX_Anisotropic: ( + args: { alphaT: Node; alphaB: Node; dotNH: Node; dotTH: Node; dotBH: Node }, +) => ShaderNodeObject; + +export default D_GGX_Anisotropic; diff --git a/src-testing/src/nodes/functions/BSDF/F_Schlick.d.ts b/src-testing/src/nodes/functions/BSDF/F_Schlick.d.ts new file mode 100644 index 000000000..ce3f23ffa --- /dev/null +++ b/src-testing/src/nodes/functions/BSDF/F_Schlick.d.ts @@ -0,0 +1,7 @@ +import Node from "../../core/Node.js"; +import OperatorNode from "../../math/OperatorNode.js"; +import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; + +declare const F_Schlick: (args: { f0: Node; f90: Node; dotVH: Node }) => ShaderNodeObject; + +export default F_Schlick; diff --git a/src-testing/src/nodes/functions/BSDF/LTC.d.ts b/src-testing/src/nodes/functions/BSDF/LTC.d.ts new file mode 100644 index 000000000..4bd6d7f76 --- /dev/null +++ b/src-testing/src/nodes/functions/BSDF/LTC.d.ts @@ -0,0 +1,9 @@ +import Node from "../../core/Node.js"; + +declare const LTC_Uv: (args: { N: Node; V: Node; roughness: Node }) => Node; + +declare const LTC_Evaluate: ( + args: { N: Node; V: Node; P: Node; mInv: Node; p0: Node; p1: Node; p2: Node; p3: Node }, +) => Node; + +export { LTC_Evaluate, LTC_Uv }; diff --git a/src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated.d.ts b/src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated.d.ts new file mode 100644 index 000000000..f736325ff --- /dev/null +++ b/src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated.d.ts @@ -0,0 +1,11 @@ +import Node from "../../core/Node.js"; +import OperatorNode from "../../math/OperatorNode.js"; +import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; + +declare const V_GGX_SmithCorrelated: (inputs: { + alpha: Node; + dotNL: Node; + dotNV: Node; +}) => ShaderNodeObject; + +export default V_GGX_SmithCorrelated; diff --git a/src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated_Anisotropic.d.ts b/src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated_Anisotropic.d.ts new file mode 100644 index 000000000..c2c6f77f7 --- /dev/null +++ b/src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated_Anisotropic.d.ts @@ -0,0 +1,16 @@ +import Node from "../../core/Node.js"; +import MathNode from "../../math/MathNode.js"; +import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; + +declare const V_GGX_SmithCorrelated: (inputs: { + alphaT: Node; + alphaB: Node; + dotTV: Node; + dotBV: Node; + dotTL: Node; + dotBL: Node; + dotNV: Node; + dotNL: Node; +}) => ShaderNodeObject; + +export default V_GGX_SmithCorrelated; diff --git a/src-testing/src/nodes/functions/BasicLightingModel.d.ts b/src-testing/src/nodes/functions/BasicLightingModel.d.ts new file mode 100644 index 000000000..a64fafd44 --- /dev/null +++ b/src-testing/src/nodes/functions/BasicLightingModel.d.ts @@ -0,0 +1,7 @@ +import LightingModel from "../core/LightingModel.js"; + +declare class BasicLightingModel extends LightingModel { + constructor(); +} + +export default BasicLightingModel; diff --git a/src-testing/src/nodes/functions/PhongLightingModel.d.ts b/src-testing/src/nodes/functions/PhongLightingModel.d.ts new file mode 100644 index 000000000..5df595269 --- /dev/null +++ b/src-testing/src/nodes/functions/PhongLightingModel.d.ts @@ -0,0 +1,7 @@ +import BasicLightingModel from "./BasicLightingModel.js"; + +export default class PhongLightingModel extends BasicLightingModel { + specular: boolean; + + constructor(specular?: boolean); +} diff --git a/src-testing/src/nodes/functions/PhysicalLightingModel.d.ts b/src-testing/src/nodes/functions/PhysicalLightingModel.d.ts new file mode 100644 index 000000000..bec381051 --- /dev/null +++ b/src-testing/src/nodes/functions/PhysicalLightingModel.d.ts @@ -0,0 +1,30 @@ +import LightingModel from "../core/LightingModel.js"; +import Node from "../core/Node.js"; + +export default class PhysicalLightingModel extends LightingModel { + clearcoat: boolean; + sheen: boolean; + iridescence: boolean; + anisotropy: boolean; + transmission: boolean; + dispersion: boolean; + + clearcoatRadiance: Node | null; + clearcoatSpecularDirect: Node | null; + clearcoatSpecularIndirect: Node | null; + sheenSpecularDirect: Node | null; + sheenSpecularIndirect: Node | null; + iridescenceFresnel: Node | null; + iridescenceF0: Node | null; + + constructor( + clearcoat?: boolean, + sheen?: boolean, + iridescence?: boolean, + anisotropy?: boolean, + transmission?: boolean, + dispersion?: boolean, + ); + + computeMultiscattering(singleScatter: Node, multiScatter: Node, specularF90: Node): void; +} diff --git a/src-testing/src/nodes/functions/ShadowMaskModel.d.ts b/src-testing/src/nodes/functions/ShadowMaskModel.d.ts new file mode 100644 index 000000000..512b06db3 --- /dev/null +++ b/src-testing/src/nodes/functions/ShadowMaskModel.d.ts @@ -0,0 +1,9 @@ +import LightingModel from "../core/LightingModel.js"; +import VarNode from "../core/VarNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class ShadowMaskModel extends LightingModel { + shadowNode: ShaderNodeObject; + + constructor(); +} diff --git a/src-testing/src/nodes/functions/ToonLightingModel.d.ts b/src-testing/src/nodes/functions/ToonLightingModel.d.ts new file mode 100644 index 000000000..d26db3457 --- /dev/null +++ b/src-testing/src/nodes/functions/ToonLightingModel.d.ts @@ -0,0 +1,4 @@ +import LightingModel from "../core/LightingModel.js"; + +export default class ToonLightingModel extends LightingModel { +} diff --git a/src-testing/src/nodes/functions/material/getGeometryRoughness.d.ts b/src-testing/src/nodes/functions/material/getGeometryRoughness.d.ts new file mode 100644 index 000000000..729111bde --- /dev/null +++ b/src-testing/src/nodes/functions/material/getGeometryRoughness.d.ts @@ -0,0 +1,6 @@ +import MathNode from "../../math/MathNode.js"; +import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; + +declare const getGeometryRoughness: () => ShaderNodeObject; + +export default getGeometryRoughness; diff --git a/src-testing/src/nodes/functions/material/getRoughness.d.ts b/src-testing/src/nodes/functions/material/getRoughness.d.ts new file mode 100644 index 000000000..56e7d3e6b --- /dev/null +++ b/src-testing/src/nodes/functions/material/getRoughness.d.ts @@ -0,0 +1,7 @@ +import Node from "../../core/Node.js"; +import MathNode from "../../math/MathNode.js"; +import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; + +declare const getRoughness: (args: { roughness: Node }) => ShaderNodeObject; + +export default getRoughness; diff --git a/src-testing/src/nodes/geometry/RangeNode.d.ts b/src-testing/src/nodes/geometry/RangeNode.d.ts new file mode 100644 index 000000000..183649866 --- /dev/null +++ b/src-testing/src/nodes/geometry/RangeNode.d.ts @@ -0,0 +1,19 @@ +import { Color } from "../../math/Color.js"; +import { Vector2 } from "../../math/Vector2.js"; +import { Vector3 } from "../../math/Vector3.js"; +import { Vector4 } from "../../math/Vector4.js"; +import Node from "../core/Node.js"; +import NodeBuilder from "../core/NodeBuilder.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export type RangeModeBound = number | Color | Vector2 | Vector3 | Vector4; + +export default class RangeNode extends Node { + min: RangeModeBound; + max: RangeModeBound; + + constructor(min: RangeModeBound, max: RangeModeBound); + getVectorLength(builder: NodeBuilder): number; +} + +export const range: (min: RangeModeBound, max: RangeModeBound) => ShaderNodeObject; diff --git a/src-testing/src/nodes/gpgpu/ComputeNode.ts b/src-testing/src/nodes/gpgpu/ComputeNode.ts new file mode 100644 index 000000000..ab2925a55 --- /dev/null +++ b/src-testing/src/nodes/gpgpu/ComputeNode.ts @@ -0,0 +1,67 @@ +import Node, { addNodeClass } from '../core/Node.js'; +import { NodeUpdateType } from '../core/constants.js'; +import { addNodeElement, nodeObject } from '../shadernode/ShaderNode.js'; + +class ComputeNode extends Node { + constructor(computeNode, count, workgroupSize = [64]) { + super('void'); + + this.isComputeNode = true; + + this.computeNode = computeNode; + + this.count = count; + this.workgroupSize = workgroupSize; + this.dispatchCount = 0; + + this.version = 1; + this.updateBeforeType = NodeUpdateType.OBJECT; + + this.updateDispatchCount(); + } + + dispose() { + this.dispatchEvent({ type: 'dispose' }); + } + + set needsUpdate(value) { + if (value === true) this.version++; + } + + updateDispatchCount() { + const { count, workgroupSize } = this; + + let size = workgroupSize[0]; + + for (let i = 1; i < workgroupSize.length; i++) size *= workgroupSize[i]; + + this.dispatchCount = Math.ceil(count / size); + } + + onInit() {} + + updateBefore({ renderer }) { + renderer.compute(this); + } + + generate(builder) { + const { shaderStage } = builder; + + if (shaderStage === 'compute') { + const snippet = this.computeNode.build(builder, 'void'); + + if (snippet !== '') { + builder.addLineFlowCode(snippet); + } + } + } +} + +export default ComputeNode; + +export const compute = (node, count, workgroupSize) => + nodeObject(new ComputeNode(nodeObject(node), count, workgroupSize)); + +addNodeElement('compute', compute); + +addNodeClass('ComputeNode', ComputeNode); diff --git a/src-testing/src/nodes/lighting/AONode.d.ts b/src-testing/src/nodes/lighting/AONode.d.ts new file mode 100644 index 000000000..998ec5236 --- /dev/null +++ b/src-testing/src/nodes/lighting/AONode.d.ts @@ -0,0 +1,8 @@ +import Node from "../core/Node.js"; +import LightingNode from "./LightingNode.js"; + +export default class AONode extends LightingNode { + aoNode: Node | null; + + constructor(aoNode?: Node | null); +} diff --git a/src-testing/src/nodes/lighting/AnalyticLightNode.d.ts b/src-testing/src/nodes/lighting/AnalyticLightNode.d.ts new file mode 100644 index 000000000..d8cea32ef --- /dev/null +++ b/src-testing/src/nodes/lighting/AnalyticLightNode.d.ts @@ -0,0 +1,8 @@ +import { Light } from "../../lights/Light.js"; +import LightingNode from "./LightingNode.js"; + +export default class AnalyticLightNode extends LightingNode { + light: T | null; + + constructor(light?: T | null); +} diff --git a/src-testing/src/nodes/lighting/BasicEnvironmentNode.d.ts b/src-testing/src/nodes/lighting/BasicEnvironmentNode.d.ts new file mode 100644 index 000000000..de244562f --- /dev/null +++ b/src-testing/src/nodes/lighting/BasicEnvironmentNode.d.ts @@ -0,0 +1,10 @@ +import Node from "../core/Node.js"; +import LightingNode from "./LightingNode.js"; + +declare class BasicEnvironmentNode extends LightingNode { + envNode: Node | null; + + constructor(envNode?: Node | null); +} + +export default BasicEnvironmentNode; diff --git a/src-testing/src/nodes/lighting/BasicLightMapNode.d.ts b/src-testing/src/nodes/lighting/BasicLightMapNode.d.ts new file mode 100644 index 000000000..c759e8857 --- /dev/null +++ b/src-testing/src/nodes/lighting/BasicLightMapNode.d.ts @@ -0,0 +1,8 @@ +import Node from "../core/Node.js"; +import LightingNode from "./LightingNode.js"; + +declare class BasicLightMapNode extends LightingNode { + constructor(lightMapNode?: Node | null); +} + +export default BasicLightMapNode; diff --git a/src-testing/src/nodes/lighting/EnvironmentNode.ts b/src-testing/src/nodes/lighting/EnvironmentNode.ts new file mode 100644 index 000000000..92a8c955d --- /dev/null +++ b/src-testing/src/nodes/lighting/EnvironmentNode.ts @@ -0,0 +1,121 @@ +import LightingNode from './LightingNode.js'; +import { cache } from '../core/CacheNode.js'; +import { context } from '../core/ContextNode.js'; +import { roughness, clearcoatRoughness } from '../core/PropertyNode.js'; +import { cameraViewMatrix } from '../accessors/CameraNode.js'; +import { + transformedClearcoatNormalView, + transformedNormalView, + transformedNormalWorld, +} from '../accessors/NormalNode.js'; +import { positionViewDirection } from '../accessors/PositionNode.js'; +import { addNodeClass } from '../core/Node.js'; +import { float } from '../shadernode/ShaderNode.js'; +import { reference } from '../accessors/ReferenceNode.js'; +import { transformedBentNormalView } from '../accessors/AccessorsUtils.js'; +import { pmremTexture } from '../pmrem/PMREMNode.js'; + +const _envNodeCache = new WeakMap(); + +class EnvironmentNode extends LightingNode { + constructor(envNode = null) { + super(); + + this.envNode = envNode; + } + + setup(builder) { + const { material } = builder; + + let envNode = this.envNode; + + if (envNode.isTextureNode || envNode.isMaterialReferenceNode) { + const value = envNode.isTextureNode ? envNode.value : material[envNode.property]; + + let cacheEnvNode = _envNodeCache.get(value); + + if (cacheEnvNode === undefined) { + cacheEnvNode = pmremTexture(value); + + _envNodeCache.set(value, cacheEnvNode); + } + + envNode = cacheEnvNode; + } + + // + + const envMap = material.envMap; + const intensity = envMap + ? reference('envMapIntensity', 'float', builder.material) + : reference('environmentIntensity', 'float', builder.scene); // @TODO: Add materialEnvIntensity in MaterialNode + + const useAnisotropy = material.useAnisotropy === true || material.anisotropy > 0; + const radianceNormalView = useAnisotropy ? transformedBentNormalView : transformedNormalView; + + const radiance = context(envNode, createRadianceContext(roughness, radianceNormalView)).mul(intensity); + const irradiance = context(envNode, createIrradianceContext(transformedNormalWorld)) + .mul(Math.PI) + .mul(intensity); + + const isolateRadiance = cache(radiance); + const isolateIrradiance = cache(irradiance); + + // + + builder.context.radiance.addAssign(isolateRadiance); + + builder.context.iblIrradiance.addAssign(isolateIrradiance); + + // + + const clearcoatRadiance = builder.context.lightingModel.clearcoatRadiance; + + if (clearcoatRadiance) { + const clearcoatRadianceContext = context( + envNode, + createRadianceContext(clearcoatRoughness, transformedClearcoatNormalView), + ).mul(intensity); + const isolateClearcoatRadiance = cache(clearcoatRadianceContext); + + clearcoatRadiance.addAssign(isolateClearcoatRadiance); + } + } +} + +const createRadianceContext = (roughnessNode, normalViewNode) => { + let reflectVec = null; + + return { + getUV: () => { + if (reflectVec === null) { + reflectVec = positionViewDirection.negate().reflect(normalViewNode); + + // Mixing the reflection with the normal is more accurate and keeps rough objects from gathering light from behind their tangent plane. + reflectVec = roughnessNode.mul(roughnessNode).mix(reflectVec, normalViewNode).normalize(); + + reflectVec = reflectVec.transformDirection(cameraViewMatrix); + } + + return reflectVec; + }, + getTextureLevel: () => { + return roughnessNode; + }, + }; +}; + +const createIrradianceContext = normalWorldNode => { + return { + getUV: () => { + return normalWorldNode; + }, + getTextureLevel: () => { + return float(1.0); + }, + }; +}; + +export default EnvironmentNode; + +addNodeClass('EnvironmentNode', EnvironmentNode); diff --git a/src-testing/src/nodes/lighting/HemisphereLightNode.d.ts b/src-testing/src/nodes/lighting/HemisphereLightNode.d.ts new file mode 100644 index 000000000..7cf38dd79 --- /dev/null +++ b/src-testing/src/nodes/lighting/HemisphereLightNode.d.ts @@ -0,0 +1,13 @@ +import { HemisphereLight } from "../../lights/HemisphereLight.js"; +import Object3DNode from "../accessors/Object3DNode.js"; +import Node from "../core/Node.js"; +import AnalyticLightNode from "./AnalyticLightNode.js"; + +export default class HemisphereLightNode extends AnalyticLightNode { + lightPositionNode: Object3DNode; + lightDirectionNode: Node; + + groundColorNode: Node; + + constructor(light?: HemisphereLight | null); +} diff --git a/src-testing/src/nodes/lighting/IrradianceNode.d.ts b/src-testing/src/nodes/lighting/IrradianceNode.d.ts new file mode 100644 index 000000000..a59838044 --- /dev/null +++ b/src-testing/src/nodes/lighting/IrradianceNode.d.ts @@ -0,0 +1,8 @@ +import Node from "../core/Node.js"; +import LightingNode from "./LightingNode.js"; + +export default class IrradianceNode extends LightingNode { + node: Node | null; + + constructor(node?: Node | null); +} diff --git a/src-testing/src/nodes/lighting/LightUtils.d.ts b/src-testing/src/nodes/lighting/LightUtils.d.ts new file mode 100644 index 000000000..640289d18 --- /dev/null +++ b/src-testing/src/nodes/lighting/LightUtils.d.ts @@ -0,0 +1,9 @@ +import Node from "../core/Node.js"; +import CondNode from "../math/CondNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const getDistanceAttenuation: (args: { + lightDistance: Node; + cutoffDistance: Node; + decayExponent: Node; +}) => ShaderNodeObject; diff --git a/src-testing/src/nodes/lighting/LightingContextNode.ts b/src-testing/src/nodes/lighting/LightingContextNode.ts new file mode 100644 index 000000000..02a8b51f8 --- /dev/null +++ b/src-testing/src/nodes/lighting/LightingContextNode.ts @@ -0,0 +1,58 @@ +import ContextNode from '../core/ContextNode.js'; +import { addNodeClass } from '../core/Node.js'; +import { addNodeElement, nodeProxy, float, vec3 } from '../shadernode/ShaderNode.js'; + +class LightingContextNode extends ContextNode { + constructor(node, lightingModel = null, backdropNode = null, backdropAlphaNode = null) { + super(node); + + this.lightingModel = lightingModel; + this.backdropNode = backdropNode; + this.backdropAlphaNode = backdropAlphaNode; + + this._context = null; + } + + getContext() { + const { backdropNode, backdropAlphaNode } = this; + + const directDiffuse = vec3().temp('directDiffuse'), + directSpecular = vec3().temp('directSpecular'), + indirectDiffuse = vec3().temp('indirectDiffuse'), + indirectSpecular = vec3().temp('indirectSpecular'); + + const reflectedLight = { + directDiffuse, + directSpecular, + indirectDiffuse, + indirectSpecular, + }; + + const context = { + radiance: vec3().temp('radiance'), + irradiance: vec3().temp('irradiance'), + iblIrradiance: vec3().temp('iblIrradiance'), + ambientOcclusion: float(1).temp('ambientOcclusion'), + reflectedLight, + backdrop: backdropNode, + backdropAlpha: backdropAlphaNode, + }; + + return context; + } + + setup(builder) { + this.context = this._context || (this._context = this.getContext()); + this.context.lightingModel = this.lightingModel || builder.context.lightingModel; + + return super.setup(builder); + } +} + +export default LightingContextNode; + +export const lightingContext = nodeProxy(LightingContextNode); + +addNodeElement('lightingContext', lightingContext); + +addNodeClass('LightingContextNode', LightingContextNode); diff --git a/src-testing/src/nodes/lighting/LightingNode.d.ts b/src-testing/src/nodes/lighting/LightingNode.d.ts new file mode 100644 index 000000000..3e8dd5424 --- /dev/null +++ b/src-testing/src/nodes/lighting/LightingNode.d.ts @@ -0,0 +1,7 @@ +import Node from "../core/Node.js"; + +export default abstract class LightingNode extends Node { + readonly isLightingNode: true; + + constructor(); +} diff --git a/src-testing/src/nodes/lighting/LightsNode.ts b/src-testing/src/nodes/lighting/LightsNode.ts new file mode 100644 index 000000000..9a41cc7dc --- /dev/null +++ b/src-testing/src/nodes/lighting/LightsNode.ts @@ -0,0 +1,168 @@ +import Node from '../core/Node.js'; +import AnalyticLightNode from './AnalyticLightNode.js'; +import { nodeObject, nodeProxy, vec3 } from '../shadernode/ShaderNode.js'; + +const LightNodes = new WeakMap(); + +const sortLights = lights => { + return lights.sort((a, b) => a.id - b.id); +}; + +class LightsNode extends Node { + constructor(lightNodes = []) { + super('vec3'); + + this.totalDiffuseNode = vec3().temp('totalDiffuse'); + this.totalSpecularNode = vec3().temp('totalSpecular'); + + this.outgoingLightNode = vec3().temp('outgoingLight'); + + this.lightNodes = lightNodes; + + this._hash = null; + } + + get hasLight() { + return this.lightNodes.length > 0; + } + + getHash() { + if (this._hash === null) { + const hash = []; + + for (const lightNode of this.lightNodes) { + hash.push(lightNode.getHash()); + } + + this._hash = 'lights-' + hash.join(','); + } + + return this._hash; + } + + analyze(builder) { + const properties = builder.getDataFromNode(this); + + for (const node of properties.nodes) { + node.build(builder); + } + } + + setup(builder) { + const context = builder.context; + const lightingModel = context.lightingModel; + + let outgoingLightNode = this.outgoingLightNode; + + if (lightingModel) { + const { lightNodes, totalDiffuseNode, totalSpecularNode } = this; + + context.outgoingLight = outgoingLightNode; + + const stack = builder.addStack(); + + // + + const properties = builder.getDataFromNode(this); + properties.nodes = stack.nodes; + + // + + lightingModel.start(context, stack, builder); + + // lights + + for (const lightNode of lightNodes) { + lightNode.build(builder); + } + + // + + lightingModel.indirect(context, stack, builder); + + // + + const { backdrop, backdropAlpha } = context; + const { directDiffuse, directSpecular, indirectDiffuse, indirectSpecular } = context.reflectedLight; + + let totalDiffuse = directDiffuse.add(indirectDiffuse); + + if (backdrop !== null) { + if (backdropAlpha !== null) { + totalDiffuse = vec3(backdropAlpha.mix(totalDiffuse, backdrop)); + } else { + totalDiffuse = vec3(backdrop); + } + + context.material.transparent = true; + } + + totalDiffuseNode.assign(totalDiffuse); + totalSpecularNode.assign(directSpecular.add(indirectSpecular)); + + outgoingLightNode.assign(totalDiffuseNode.add(totalSpecularNode)); + + // + + lightingModel.finish(context, stack, builder); + + // + + outgoingLightNode = outgoingLightNode.bypass(builder.removeStack()); + } + + return outgoingLightNode; + } + + _getLightNodeById(id) { + for (const lightNode of this.lightNodes) { + if (lightNode.isAnalyticLightNode && lightNode.light.id === id) { + return lightNode; + } + } + + return null; + } + + fromLights(lights = []) { + const lightNodes = []; + + lights = sortLights(lights); + + for (const light of lights) { + let lightNode = this._getLightNodeById(light.id); + + if (lightNode === null) { + const lightClass = light.constructor; + const lightNodeClass = LightNodes.has(lightClass) ? LightNodes.get(lightClass) : AnalyticLightNode; + + lightNode = nodeObject(new lightNodeClass(light)); + } + + lightNodes.push(lightNode); + } + + this.lightNodes = lightNodes; + this._hash = null; + + return this; + } +} + +export default LightsNode; + +export const lights = lights => nodeObject(new LightsNode().fromLights(lights)); +export const lightsNode = nodeProxy(LightsNode); + +export function addLightNode(lightClass, lightNodeClass) { + if (LightNodes.has(lightClass)) { + console.warn(`Redefinition of light node ${lightNodeClass.type}`); + return; + } + + if (typeof lightClass !== 'function') throw new Error(`Light ${lightClass.name} is not a class`); + if (typeof lightNodeClass !== 'function' || !lightNodeClass.type) + throw new Error(`Light node ${lightNodeClass.type} is not a class`); + + LightNodes.set(lightClass, lightNodeClass); +} diff --git a/src-testing/src/nodes/lighting/PointLightNode.d.ts b/src-testing/src/nodes/lighting/PointLightNode.d.ts new file mode 100644 index 000000000..ee0407d25 --- /dev/null +++ b/src-testing/src/nodes/lighting/PointLightNode.d.ts @@ -0,0 +1,10 @@ +import { PointLight } from "../../lights/PointLight.js"; +import Node from "../core/Node.js"; +import AnalyticLightNode from "./AnalyticLightNode.js"; + +export default class PointLightNode extends AnalyticLightNode { + cutoffDistanceNode: Node; + decayExponentNode: Node; + + constructor(light?: PointLight | null); +} diff --git a/src-testing/src/nodes/lighting/RectAreaLightNode.d.ts b/src-testing/src/nodes/lighting/RectAreaLightNode.d.ts new file mode 100644 index 000000000..db4d18b82 --- /dev/null +++ b/src-testing/src/nodes/lighting/RectAreaLightNode.d.ts @@ -0,0 +1,21 @@ +import { RectAreaLight } from "../../lights/RectAreaLight.js"; +import { DataTexture } from "../../textures/DataTexture.js"; +import Node from "../core/Node.js"; +import AnalyticLightNode from "./AnalyticLightNode.js"; + +export interface RectAreaLightTexturesLib { + LTC_HALF_1: DataTexture; + LTC_HALF_2: DataTexture; + + LTC_FLOAT_1: DataTexture; + LTC_FLOAT_2: DataTexture; +} + +export default class RectAreaLightNode extends AnalyticLightNode { + halfHeight: Node; + halfWidth: Node; + + constructor(light?: RectAreaLight | null); + + static setLTC(ltc: RectAreaLightTexturesLib): void; +} diff --git a/src-testing/src/nodes/lighting/SpotLightNode.d.ts b/src-testing/src/nodes/lighting/SpotLightNode.d.ts new file mode 100644 index 000000000..0b1ae4b13 --- /dev/null +++ b/src-testing/src/nodes/lighting/SpotLightNode.d.ts @@ -0,0 +1,15 @@ +import { SpotLight } from "../../lights/SpotLight.js"; +import Node from "../core/Node.js"; +import AnalyticLightNode from "./AnalyticLightNode.js"; + +export default class PointLightNode extends AnalyticLightNode { + directionNode: Node; + + coneCosNode: Node; + penumbraCosNode: Node; + + cutoffDistanceNode: Node; + decayExponentNode: Node; + + constructor(light?: SpotLight | null); +} diff --git a/src-testing/src/nodes/loaders/NodeLoader.d.ts b/src-testing/src/nodes/loaders/NodeLoader.d.ts new file mode 100644 index 000000000..0ced68bab --- /dev/null +++ b/src-testing/src/nodes/loaders/NodeLoader.d.ts @@ -0,0 +1,16 @@ +import { Loader } from "../../loaders/Loader.js"; +import { LoadingManager } from "../../loaders/LoadingManager.js"; +import { Texture } from "../../textures/Texture.js"; +import { Node } from "../Nodes.js"; + +export interface NodeLoaderResult { + [hash: string]: Node; +} + +export default class NodeLoader extends Loader { + constructor(manager?: LoadingManager); + + parseNodes(json: unknown): NodeLoaderResult; + parse(json: unknown): Node; + setTextures(textures: { [key: string]: Texture }): this; +} diff --git a/src-testing/src/nodes/loaders/NodeMaterialLoader.d.ts b/src-testing/src/nodes/loaders/NodeMaterialLoader.d.ts new file mode 100644 index 000000000..4f927141e --- /dev/null +++ b/src-testing/src/nodes/loaders/NodeMaterialLoader.d.ts @@ -0,0 +1,8 @@ +import { MaterialLoader } from "../../loaders/MaterialLoader.js"; +import { NodeLoaderResult } from "./NodeLoader.js"; + +export default class NodeMaterialLoader extends MaterialLoader { + nodes: NodeLoaderResult; + + setNodes(value: NodeLoaderResult): this; +} diff --git a/src-testing/src/nodes/loaders/NodeObjectLoader.d.ts b/src-testing/src/nodes/loaders/NodeObjectLoader.d.ts new file mode 100644 index 000000000..7341a4a28 --- /dev/null +++ b/src-testing/src/nodes/loaders/NodeObjectLoader.d.ts @@ -0,0 +1,10 @@ +import { ObjectLoader } from "../../loaders/ObjectLoader.js"; +import { Material } from "../../materials/Material.js"; +import { Texture } from "../../textures/Texture.js"; +import { NodeLoaderResult } from "./NodeLoader.js"; + +export default class NodeObjectLoader extends ObjectLoader { + parseNodes(json: unknown, textures: { [key: string]: Texture }): NodeLoaderResult; + + parseMaterials(json: unknown, textures: { [key: string]: Texture }): { [key: string]: Material }; +} diff --git a/src-testing/src/nodes/materials/Line2NodeMaterial.d.ts b/src-testing/src/nodes/materials/Line2NodeMaterial.d.ts new file mode 100644 index 000000000..405cc4749 --- /dev/null +++ b/src-testing/src/nodes/materials/Line2NodeMaterial.d.ts @@ -0,0 +1,53 @@ +import { LineDashedMaterialParameters } from "../../materials/LineDashedMaterial.js"; +import { Color } from "../../math/Color.js"; +import { Texture } from "../../textures/Texture.js"; +import Node from "../core/Node.js"; +import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; + +export interface Line2NodeMaterialParameters extends NodeMaterialParameters, LineDashedMaterialParameters { + dashed?: boolean | undefined; +} + +export default class Line2NodeMaterial extends NodeMaterial { + lights: boolean; + + // Properties from LineDashedMaterial + readonly isLineDashedMaterial: true; + scale: number; + dashSize: number; + gapSize: number; + + // Properties from LineBasicMaterial + readonly isLineBasicMaterial: true; + color: Color; + fog: boolean; + linewidth: number; + linecap: string; + linejoin: string; + map: Texture | null; + + useAlphaToCoverage: boolean; + useColor: boolean; + useDash: boolean; + useWorldUnits: boolean; + + dashOffset: number; + lineWidth: number; + + lineColorNode: Node | null; + + offsetNode: Node | null; + dashScaleNode: Node | null; + dashSizeNode: Node | null; + gapSizeNode: Node | null; + + constructor(parameters?: Line2NodeMaterialParameters); + + setupShaders(): void; + + get worldUnits(): boolean; + set worldUnits(value: boolean); + + get dashed(): boolean; + set dashed(value: boolean); +} diff --git a/src-testing/src/nodes/materials/LineBasicNodeMaterial.d.ts b/src-testing/src/nodes/materials/LineBasicNodeMaterial.d.ts new file mode 100644 index 000000000..4ff1f5ee8 --- /dev/null +++ b/src-testing/src/nodes/materials/LineBasicNodeMaterial.d.ts @@ -0,0 +1,22 @@ +import { LineBasicMaterialParameters } from "../../materials/LineBasicMaterial.js"; +import { Color } from "../../math/Color.js"; +import { Texture } from "../../textures/Texture.js"; +import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; + +export interface LineBasicNodeMaterialParameters extends NodeMaterialParameters, LineBasicMaterialParameters { +} + +export default class LineBasicNodeMaterial extends NodeMaterial { + readonly isLineBasicNodeMaterial: true; + + // Properties from LineBasicMaterial + readonly isLineBasicMaterial: true; + color: Color; + fog: boolean; + linewidth: number; + linecap: string; + linejoin: string; + map: Texture | null; + + constructor(parameters?: LineBasicNodeMaterialParameters); +} diff --git a/src-testing/src/nodes/materials/Materials.d.ts b/src-testing/src/nodes/materials/Materials.d.ts new file mode 100644 index 000000000..9d668a8a1 --- /dev/null +++ b/src-testing/src/nodes/materials/Materials.d.ts @@ -0,0 +1,15 @@ +export { default as Line2NodeMaterial } from "./Line2NodeMaterial.js"; +export { default as LineBasicNodeMaterial } from "./LineBasicNodeMaterial.js"; +export { default as MeshBasicNodeMaterial } from "./MeshBasicNodeMaterial.js"; +export { default as MeshMatcapNodeMaterial } from "./MeshMatcapNodeMaterial.js"; +export { default as MeshNormalNodeMaterial } from "./MeshNormalNodeMaterial.js"; +export { default as MeshPhongNodeMaterial } from "./MeshPhongNodeMaterial.js"; +export { default as MeshPhysicalNodeMaterial } from "./MeshPhysicalNodeMaterial.js"; +export { default as MeshSSSPhysicalNodeMaterial } from "./MeshSSSNodeMaterial.js"; +export { default as MeshStandardNodeMaterial } from "./MeshStandardNodeMaterial.js"; +export { default as MeshToonNodeMaterial } from "./MeshToonNodeMaterial.js"; +export { addNodeMaterial, createNodeMaterialFromType, default as NodeMaterial } from "./NodeMaterial.js"; +export { default as PointsNodeMaterial } from "./PointsNodeMaterial.js"; +export { default as ShadowNodeMaterial } from "./ShadowNodeMaterial.js"; +export { default as SpriteNodeMaterial } from "./SpriteNodeMaterial.js"; +export { default as VolumeNodeMaterial } from "./VolumeNodeMaterial.js"; diff --git a/src-testing/src/nodes/materials/MeshBasicNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshBasicNodeMaterial.d.ts new file mode 100644 index 000000000..ff5cc609d --- /dev/null +++ b/src-testing/src/nodes/materials/MeshBasicNodeMaterial.d.ts @@ -0,0 +1,36 @@ +import { Combine } from "../../constants.js"; +import { MeshBasicMaterialParameters } from "../../materials/MeshBasicMaterial.js"; +import { Color } from "../../math/Color.js"; +import { Euler } from "../../math/Euler.js"; +import { Texture } from "../../textures/Texture.js"; +import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; + +export interface MeshBasicNodeMaterialParameters extends NodeMaterialParameters, MeshBasicMaterialParameters { +} + +export default class MeshBasicNodeMaterial extends NodeMaterial { + readonly isMeshBasicNodeMaterial: true; + + // Properties from MeshBasicMaterial + readonly isMeshBasicMaterial: true; + color: Color; + map: Texture | null; + lightMap: Texture | null; + lightMapIntensity: number; + aoMap: Texture | null; + aoMapIntensity: number; + specularMap: Texture | null; + alphaMap: Texture | null; + envMap: Texture | null; + envMapRotation: Euler; + combine: Combine; + reflectivity: number; + refractionRatio: number; + wireframe: boolean; + wireframeLinewidth: number; + wireframeLinecap: string; + wireframeLinejoin: string; + fog: boolean; + + constructor(parameters?: MeshBasicNodeMaterialParameters); +} diff --git a/src-testing/src/nodes/materials/MeshMatcapNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshMatcapNodeMaterial.d.ts new file mode 100644 index 000000000..accb772de --- /dev/null +++ b/src-testing/src/nodes/materials/MeshMatcapNodeMaterial.d.ts @@ -0,0 +1,32 @@ +import { NormalMapTypes } from "../../constants.js"; +import { MeshMatcapMaterialParameters } from "../../materials/MeshMatcapMaterial.js"; +import { Color } from "../../math/Color.js"; +import { Vector2 } from "../../math/Vector2.js"; +import { Texture } from "../../textures/Texture.js"; +import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; + +export interface MeshMatcapNodeMaterialParameters extends NodeMaterialParameters, MeshMatcapMaterialParameters { +} + +export default class MeshMatcapNodeMaterial extends NodeMaterial { + readonly isMeshMatcapNodeMaterial: true; + + // Properties from MeshMatcapMaterial + readonly isMeshMatcapMaterial: true; + color: Color; + matcap: Texture | null; + map: Texture | null; + bumpMap: Texture | null; + bumpScale: number; + normalMap: Texture | null; + normalMapType: NormalMapTypes; + normalScale: Vector2; + displacementMap: Texture | null; + displacementScale: number; + displacementBias: number; + alphaMap: Texture | null; + flatShading: boolean; + fog: boolean; + + constructor(paramters: MeshMatcapNodeMaterialParameters); +} diff --git a/src-testing/src/nodes/materials/MeshNormalNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshNormalNodeMaterial.d.ts new file mode 100644 index 000000000..f6bb618c1 --- /dev/null +++ b/src-testing/src/nodes/materials/MeshNormalNodeMaterial.d.ts @@ -0,0 +1,28 @@ +import { NormalMapTypes } from "../../constants.js"; +import { MeshNormalMaterialParameters } from "../../materials/MeshNormalMaterial.js"; +import { Vector2 } from "../../math/Vector2.js"; +import { Texture } from "../../textures/Texture.js"; +import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; + +export interface MeshBasicNodeMaterialParameters extends NodeMaterialParameters, MeshNormalMaterialParameters { +} + +export default class MeshNormalNodeMaterial extends NodeMaterial { + readonly isMeshNormalNodeMaterial: true; + + // Properties from MeshNormalMaterial + readonly isMeshNormalMaterial: true; + bumpMap: Texture | null; + bumpScale: number; + normalMap: Texture | null; + normalMapType: NormalMapTypes; + normalScale: Vector2; + displacementMap: Texture | null; + displacementScale: number; + displacementBias: number; + wireframe: boolean; + wireframeLinewidth: number; + flatShading: boolean; + + constructor(parameters?: MeshBasicNodeMaterialParameters); +} diff --git a/src-testing/src/nodes/materials/MeshPhongNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshPhongNodeMaterial.d.ts new file mode 100644 index 000000000..97d83d196 --- /dev/null +++ b/src-testing/src/nodes/materials/MeshPhongNodeMaterial.d.ts @@ -0,0 +1,56 @@ +import { Combine, NormalMapTypes } from "../../constants.js"; +import { MeshPhongMaterialParameters } from "../../materials/MeshPhongMaterial.js"; +import { Color } from "../../math/Color.js"; +import { Euler } from "../../math/Euler.js"; +import { Vector2 } from "../../math/Vector2.js"; +import { Texture } from "../../textures/Texture.js"; +import Node from "../core/Node.js"; +import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; + +export interface MeshPhongNodeMaterialParameters extends NodeMaterialParameters, MeshPhongMaterialParameters { +} + +export default class MeshPhongNodeMaterial extends NodeMaterial { + readonly isMeshPhongNodeMaterial: true; + + shininessNode: Node | null; + specularNode: Node | null; + + // Properties from MeshPhongMaterial + readonly isMeshPhongMaterial: true; + color: Color; + specular: Color; + shininess: number; + map: Texture | null; + lightMap: Texture | null; + lightMapIntensity: number; + aoMap: Texture | null; + aoMapIntensity: number; + emissive: Color; + emissiveIntensity: number; + emissiveMap: Texture | null; + bumpMap: Texture | null; + bumpScale: number; + normalMap: Texture | null; + normalMapType: NormalMapTypes; + normalScale: Vector2; + displacementMap: Texture | null; + displacementScale: number; + displacementBias: number; + specularMap: Texture | null; + alphaMap: Texture | null; + envMap: Texture | null; + envMapRotation: Euler; + combine: Combine; + reflectivity: number; + refractionRatio: number; + wireframe: boolean; + wireframeLinewidth: number; + wireframeLinecap: string; + wireframeLinejoin: string; + flatShading: boolean; + metal: boolean; + fog: boolean; + + constructor(parameters?: MeshPhongNodeMaterialParameters); +} diff --git a/src-testing/src/nodes/materials/MeshPhysicalNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshPhysicalNodeMaterial.d.ts new file mode 100644 index 000000000..3b9e500f1 --- /dev/null +++ b/src-testing/src/nodes/materials/MeshPhysicalNodeMaterial.d.ts @@ -0,0 +1,90 @@ +import { MeshPhysicalMaterialParameters } from "../../materials/MeshPhysicalMaterial.js"; +import { Color } from "../../math/Color.js"; +import { Vector2 } from "../../math/Vector2.js"; +import { Texture } from "../../textures/Texture.js"; +import Node from "../core/Node.js"; +import MeshStandardNodeMaterial, { MeshStandardNodeMaterialParameters } from "./MeshStandardNodeMaterial.js"; + +export interface MeshPhysicalNodeMaterialParameters + extends MeshStandardNodeMaterialParameters, MeshPhysicalMaterialParameters +{ +} + +export default class MeshPhysicalNodeMaterial extends MeshStandardNodeMaterial { + readonly isMeshPhysicalNodeMaterial: true; + + clearcoatNode: Node | null; + clearcoatRoughnessNode: Node | null; + clearcoatNormalNode: Node | null; + + sheenNode: Node | null; + sheenRoughnessNode: Node | null; + + iridescenceNode: Node | null; + iridescenceIORNode: Node | null; + iridescenceThicknessNode: Node | null; + + iorNode: Node | null; + + specularIntensityNode: Node | null; + specularColorNode: Node | null; + + transmissionNode: Node | null; + thicknessNode: Node | null; + attenuationDistanceNode: Node | null; + attenuationColorNode: Node | null; + dispersionNode: Node | null; + + anisotropyNode: Node | null; + + // Properties from MeshPhysicalMaterial + readonly isMeshPhysicalMaterial: true; + anisotropyRotation: number; + anisotropyMap: Texture | null; + clearcoatMap: Texture | null; + clearcoatRoughness: number; + clearcoatRoughnessMap: Texture | null; + clearcoatNormalScale: Vector2; + clearcoatNormalMap: Texture | null; + ior: number; + get reflectivity(): number; + set reflectivity(reflectivity: number); + iridescenceMap: Texture | null; + iridescenceIOR: number; + iridescenceThicknessRange: [number, number]; + iridescenceThicknessMap: Texture | null; + sheenColor: Color; + sheenColorMap: Texture | null; + sheenRoughness: number; + sheenRoughnessMap: Texture | null; + transmissionMap: Texture | null; + thickness: number; + thicknessMap: Texture | null; + attenuationDistance: number; + attenuationColor: Color; + specularIntensity: number; + specularIntensityMap: Texture | null; + specularColor: Color; + specularColorMap: Texture | null; + get anisotropy(): number; + set anisotropy(value: number); + get clearcoat(): number; + set clearcoat(value: number); + get iridescence(): number; + set iridescence(value: number); + get dispersion(): number; + set dispersion(value: number); + get sheen(): number; + set sheen(value: number); + get transmission(): number; + set transmission(value: number); + + constructor(parameters?: MeshPhysicalNodeMaterialParameters); + + get useClearcoat(): boolean; + get useIridescence(): boolean; + get useSheen(): boolean; + get useAnisotropy(): boolean; + get useTransmission(): boolean; + get useDispersion(): boolean; +} diff --git a/src-testing/src/nodes/materials/MeshSSSNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshSSSNodeMaterial.d.ts new file mode 100644 index 000000000..8506de76c --- /dev/null +++ b/src-testing/src/nodes/materials/MeshSSSNodeMaterial.d.ts @@ -0,0 +1,16 @@ +import ConstNode from "../core/ConstNode.js"; +import Node from "../core/Node.js"; +import MeshPhysicalNodeMaterial, { MeshPhysicalNodeMaterialParameters } from "./MeshPhysicalNodeMaterial.js"; + +export default class MeshSSSNodeMaterial extends MeshPhysicalNodeMaterial { + thicknessColorNode: Node | null; + thicknessDistortionNode: ConstNode; + thicknessAmbientNode: ConstNode; + thicknessAttenuationNode: ConstNode; + thicknessPowerNode: ConstNode; + thicknessScaleNode: ConstNode; + + constructor(parameters?: MeshPhysicalNodeMaterialParameters); + + get useSSS(): boolean; +} diff --git a/src-testing/src/nodes/materials/MeshStandardNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshStandardNodeMaterial.d.ts new file mode 100644 index 000000000..6bc1dfb38 --- /dev/null +++ b/src-testing/src/nodes/materials/MeshStandardNodeMaterial.d.ts @@ -0,0 +1,56 @@ +import { NormalMapTypes } from "../../constants.js"; +import { MeshStandardMaterialParameters } from "../../materials/MeshStandardMaterial.js"; +import { Color } from "../../math/Color.js"; +import { Euler } from "../../math/Euler.js"; +import { Vector2 } from "../../math/Vector2.js"; +import { Texture } from "../../textures/Texture.js"; +import Node from "../core/Node.js"; +import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; + +export interface MeshStandardNodeMaterialParameters extends NodeMaterialParameters, MeshStandardMaterialParameters { +} + +export default class MeshStandardNodeMaterial extends NodeMaterial { + readonly isMeshStandardNodeMaterial: true; + + emissiveNode: Node | null; + + metalnessNode: Node | null; + roughnessNode: Node | null; + + // Properties from MeshStandardMaterial + readonly isMeshStandardMaterial: true; + color: Color; + roughness: number; + metalness: number; + map: Texture | null; + lightMap: Texture | null; + lightMapIntensity: number; + aoMap: Texture | null; + aoMapIntensity: number; + emissive: Color; + emissiveIntensity: number; + emissiveMap: Texture | null; + bumpMap: Texture | null; + bumpScale: number; + normalMap: Texture | null; + normalMapType: NormalMapTypes; + normalScale: Vector2; + displacementMap: Texture | null; + displacementScale: number; + displacementBias: number; + roughnessMap: Texture | null; + metalnessMap: Texture | null; + alphaMap: Texture | null; + envMap: Texture | null; + envMapRotation: Euler; + envMapIntensity: number; + wireframe: boolean; + wireframeLinewidth: number; + wireframeLinecap: string; + wireframeLinejoin: string; + flatShading: boolean; + fog: boolean; + + constructor(paramters?: MeshStandardNodeMaterialParameters); +} diff --git a/src-testing/src/nodes/materials/MeshToonNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshToonNodeMaterial.d.ts new file mode 100644 index 000000000..6d54dd67e --- /dev/null +++ b/src-testing/src/nodes/materials/MeshToonNodeMaterial.d.ts @@ -0,0 +1,42 @@ +import { NormalMapTypes } from "../../constants.js"; +import { MeshToonMaterialParameters } from "../../materials/MeshToonMaterial.js"; +import { Color } from "../../math/Color.js"; +import { Vector2 } from "../../math/Vector2.js"; +import { Texture } from "../../textures/Texture.js"; +import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; + +export interface MeshToonNodeMaterialParameters extends NodeMaterialParameters, MeshToonMaterialParameters { +} + +export default class MeshToonNodeMaterial extends NodeMaterial { + readonly isMeshToonNodeMaterial: true; + + // Properties from MeshToonMaterial + readonly isMeshToonMaterial: true; + color: Color; + gradientMap: Texture | null; + map: Texture | null; + lightMap: Texture | null; + lightMapIntensity: number; + aoMap: Texture | null; + aoMapIntensity: number; + emissive: Color; + emissiveIntensity: number; + emissiveMap: Texture | null; + bumpMap: Texture | null; + bumpScale: number; + normalMap: Texture | null; + normalMapType: NormalMapTypes; + normalScale: Vector2; + displacementMap: Texture | null; + displacementScale: number; + displacementBias: number; + alphaMap: Texture | null; + wireframe: boolean; + wireframeLinewidth: number; + wireframeLinecap: string; + wireframeLinejoin: string; + fog: boolean; + + constructor(paramters: MeshToonNodeMaterialParameters); +} diff --git a/src-testing/src/nodes/materials/NodeMaterial.ts b/src-testing/src/nodes/materials/NodeMaterial.ts new file mode 100644 index 000000000..ac61dfc59 --- /dev/null +++ b/src-testing/src/nodes/materials/NodeMaterial.ts @@ -0,0 +1,554 @@ +import { Material } from '../../materials/Material.js'; +import { NormalBlending } from '../../constants.js'; + +import { getNodeChildren, getCacheKey } from '../core/NodeUtils.js'; +import { attribute } from '../core/AttributeNode.js'; +import { output, diffuseColor, emissive, varyingProperty } from '../core/PropertyNode.js'; +import { + materialAlphaTest, + materialColor, + materialOpacity, + materialEmissive, + materialNormal, + materialLightMap, + materialAOMap, +} from '../accessors/MaterialNode.js'; +import { modelViewProjection } from '../accessors/ModelViewProjectionNode.js'; +import { transformedNormalView, normalLocal } from '../accessors/NormalNode.js'; +import { instance } from '../accessors/InstanceNode.js'; +import { batch } from '../accessors/BatchNode.js'; +import { materialReference } from '../accessors/MaterialReferenceNode.js'; +import { positionLocal, positionView } from '../accessors/PositionNode.js'; +import { skinningReference } from '../accessors/SkinningNode.js'; +import { morphReference } from '../accessors/MorphNode.js'; +import { lightsNode } from '../lighting/LightsNode.js'; +import { mix } from '../math/MathNode.js'; +import { float, vec3, vec4 } from '../shadernode/ShaderNode.js'; +import AONode from '../lighting/AONode.js'; +import { lightingContext } from '../lighting/LightingContextNode.js'; +import IrradianceNode from '../lighting/IrradianceNode.js'; +import { depth } from '../display/ViewportDepthNode.js'; +import { cameraLogDepth } from '../accessors/CameraNode.js'; +import { clipping, clippingAlpha } from '../accessors/ClippingNode.js'; +import { faceDirection } from '../display/FrontFacingNode.js'; + +const NodeMaterials = new Map(); + +class NodeMaterial extends Material { + constructor() { + super(); + + this.isNodeMaterial = true; + + this.type = this.constructor.type; + + this.forceSinglePass = false; + + this.fog = true; + this.lights = false; + this.normals = true; + + this.lightsNode = null; + this.envNode = null; + this.aoNode = null; + + this.colorNode = null; + this.normalNode = null; + this.opacityNode = null; + this.backdropNode = null; + this.backdropAlphaNode = null; + this.alphaTestNode = null; + + this.positionNode = null; + + this.depthNode = null; + this.shadowNode = null; + this.shadowPositionNode = null; + + this.outputNode = null; + this.mrtNode = null; + + this.fragmentNode = null; + this.vertexNode = null; + } + + customProgramCacheKey() { + return this.type + getCacheKey(this); + } + + build(builder) { + this.setup(builder); + } + + setup(builder) { + // < VERTEX STAGE > + + builder.addStack(); + + builder.stack.outputNode = this.vertexNode || this.setupPosition(builder); + + builder.addFlow('vertex', builder.removeStack()); + + // < FRAGMENT STAGE > + + builder.addStack(); + + let resultNode; + + const clippingNode = this.setupClipping(builder); + + if (this.depthWrite === true) this.setupDepth(builder); + + if (this.fragmentNode === null) { + if (this.normals === true) this.setupNormal(builder); + + this.setupDiffuseColor(builder); + this.setupVariants(builder); + + const outgoingLightNode = this.setupLighting(builder); + + if (clippingNode !== null) builder.stack.add(clippingNode); + + // force unsigned floats - useful for RenderTargets + + const basicOutput = vec4(outgoingLightNode, diffuseColor.a).max(0); + + resultNode = this.setupOutput(builder, basicOutput); + + // OUTPUT NODE + + output.assign(resultNode); + + // + + if (this.outputNode !== null) resultNode = this.outputNode; + + // MRT + + const renderTarget = builder.renderer.getRenderTarget(); + + if (renderTarget !== null) { + const mrt = builder.renderer.getMRT(); + const materialMRT = this.mrtNode; + + if (mrt !== null) { + resultNode = mrt; + + if (materialMRT !== null) { + resultNode = mrt.merge(materialMRT); + } + } else if (materialMRT !== null) { + resultNode = materialMRT; + } + } + } else { + let fragmentNode = this.fragmentNode; + + if (fragmentNode.isOutputStructNode !== true) { + fragmentNode = vec4(fragmentNode); + } + + resultNode = this.setupOutput(builder, fragmentNode); + } + + builder.stack.outputNode = resultNode; + + builder.addFlow('fragment', builder.removeStack()); + } + + setupClipping(builder) { + if (builder.clippingContext === null) return null; + + const { globalClippingCount, localClippingCount } = builder.clippingContext; + + let result = null; + + if (globalClippingCount || localClippingCount) { + if (this.alphaToCoverage) { + // to be added to flow when the color/alpha value has been determined + result = clippingAlpha(); + } else { + builder.stack.add(clipping()); + } + } + + return result; + } + + setupDepth(builder) { + const { renderer } = builder; + + // Depth + + let depthNode = this.depthNode; + + if (depthNode === null && renderer.logarithmicDepthBuffer === true) { + const fragDepth = modelViewProjection().w.add(1); + + depthNode = fragDepth.log2().mul(cameraLogDepth).mul(0.5); + } + + if (depthNode !== null) { + depth.assign(depthNode).append(); + } + } + + setupPosition(builder) { + const { object } = builder; + const geometry = object.geometry; + + builder.addStack(); + + // Vertex + + if (geometry.morphAttributes.position || geometry.morphAttributes.normal || geometry.morphAttributes.color) { + morphReference(object).append(); + } + + if (object.isSkinnedMesh === true) { + skinningReference(object).append(); + } + + if (this.displacementMap) { + const displacementMap = materialReference('displacementMap', 'texture'); + const displacementScale = materialReference('displacementScale', 'float'); + const displacementBias = materialReference('displacementBias', 'float'); + + positionLocal.addAssign( + normalLocal.normalize().mul(displacementMap.x.mul(displacementScale).add(displacementBias)), + ); + } + + if (object.isBatchedMesh) { + batch(object).append(); + } + + if (object.instanceMatrix && object.instanceMatrix.isInstancedBufferAttribute === true) { + instance(object).append(); + } + + if (this.positionNode !== null) { + positionLocal.assign(this.positionNode); + } + + const mvp = modelViewProjection(); + + builder.context.vertex = builder.removeStack(); + builder.context.mvp = mvp; + + return mvp; + } + + setupDiffuseColor({ object, geometry }) { + let colorNode = this.colorNode ? vec4(this.colorNode) : materialColor; + + // VERTEX COLORS + + if (this.vertexColors === true && geometry.hasAttribute('color')) { + colorNode = vec4(colorNode.xyz.mul(attribute('color', 'vec3')), colorNode.a); + } + + // Instanced colors + + if (object.instanceColor) { + const instanceColor = varyingProperty('vec3', 'vInstanceColor'); + + colorNode = instanceColor.mul(colorNode); + } + + // COLOR + + diffuseColor.assign(colorNode); + + // OPACITY + + const opacityNode = this.opacityNode ? float(this.opacityNode) : materialOpacity; + diffuseColor.a.assign(diffuseColor.a.mul(opacityNode)); + + // ALPHA TEST + + if (this.alphaTestNode !== null || this.alphaTest > 0) { + const alphaTestNode = this.alphaTestNode !== null ? float(this.alphaTestNode) : materialAlphaTest; + + diffuseColor.a.lessThanEqual(alphaTestNode).discard(); + } + + if (this.transparent === false && this.blending === NormalBlending && this.alphaToCoverage === false) { + diffuseColor.a.assign(1.0); + } + } + + setupVariants(/*builder*/) { + // Interface function. + } + + setupOutgoingLight() { + return this.lights === true ? vec3(0) : diffuseColor.rgb; + } + + setupNormal() { + // NORMAL VIEW + + if (this.flatShading === true) { + const normalNode = positionView.dFdx().cross(positionView.dFdy()).normalize(); + + transformedNormalView.assign(normalNode.mul(faceDirection)); + } else { + const normalNode = this.normalNode ? vec3(this.normalNode) : materialNormal; + + transformedNormalView.assign(normalNode.mul(faceDirection)); + } + } + + setupEnvironment(/*builder*/) { + let node = null; + + if (this.envNode) { + node = this.envNode; + } else if (this.envMap) { + node = this.envMap.isCubeTexture + ? materialReference('envMap', 'cubeTexture') + : materialReference('envMap', 'texture'); + } + + return node; + } + + setupLightMap(builder) { + let node = null; + + if (builder.material.lightMap) { + node = new IrradianceNode(materialLightMap); + } + + return node; + } + + setupLights(builder) { + const materialLightsNode = []; + + // + + const envNode = this.setupEnvironment(builder); + + if (envNode && envNode.isLightingNode) { + materialLightsNode.push(envNode); + } + + const lightMapNode = this.setupLightMap(builder); + + if (lightMapNode && lightMapNode.isLightingNode) { + materialLightsNode.push(lightMapNode); + } + + if (this.aoNode !== null || builder.material.aoMap) { + const aoNode = this.aoNode !== null ? this.aoNode : materialAOMap; + + materialLightsNode.push(new AONode(aoNode)); + } + + let lightsN = this.lightsNode || builder.lightsNode; + + if (materialLightsNode.length > 0) { + lightsN = lightsNode([...lightsN.lightNodes, ...materialLightsNode]); + } + + return lightsN; + } + + setupLightingModel(/*builder*/) { + // Interface function. + } + + setupLighting(builder) { + const { material } = builder; + const { backdropNode, backdropAlphaNode, emissiveNode } = this; + + // OUTGOING LIGHT + + const lights = this.lights === true || this.lightsNode !== null; + + const lightsNode = lights ? this.setupLights(builder) : null; + + let outgoingLightNode = this.setupOutgoingLight(builder); + + if (lightsNode && lightsNode.hasLight !== false) { + const lightingModel = this.setupLightingModel(builder); + + outgoingLightNode = lightingContext(lightsNode, lightingModel, backdropNode, backdropAlphaNode); + } else if (backdropNode !== null) { + outgoingLightNode = vec3( + backdropAlphaNode !== null ? mix(outgoingLightNode, backdropNode, backdropAlphaNode) : backdropNode, + ); + } + + // EMISSIVE + + if ( + (emissiveNode && emissiveNode.isNode === true) || + (material.emissive && material.emissive.isColor === true) + ) { + emissive.assign(vec3(emissiveNode ? emissiveNode : materialEmissive)); + + outgoingLightNode = outgoingLightNode.add(emissive); + } + + return outgoingLightNode; + } + + setupOutput(builder, outputNode) { + // FOG + + if (this.fog === true) { + const fogNode = builder.fogNode; + + if (fogNode) outputNode = vec4(fogNode.mix(outputNode.rgb, fogNode.colorNode), outputNode.a); + } + + return outputNode; + } + + setDefaultValues(material) { + // This approach is to reuse the native refreshUniforms* + // and turn available the use of features like transmission and environment in core + + for (const property in material) { + const value = material[property]; + + if (this[property] === undefined) { + this[property] = value; + + if (value && value.clone) this[property] = value.clone(); + } + } + + const descriptors = Object.getOwnPropertyDescriptors(material.constructor.prototype); + + for (const key in descriptors) { + if ( + Object.getOwnPropertyDescriptor(this.constructor.prototype, key) === undefined && + descriptors[key].get !== undefined + ) { + Object.defineProperty(this.constructor.prototype, key, descriptors[key]); + } + } + } + + toJSON(meta) { + const isRoot = meta === undefined || typeof meta === 'string'; + + if (isRoot) { + meta = { + textures: {}, + images: {}, + nodes: {}, + }; + } + + const data = Material.prototype.toJSON.call(this, meta); + const nodeChildren = getNodeChildren(this); + + data.inputNodes = {}; + + for (const { property, childNode } of nodeChildren) { + data.inputNodes[property] = childNode.toJSON(meta).uuid; + } + + // TODO: Copied from Object3D.toJSON + + function extractFromCache(cache) { + const values = []; + + for (const key in cache) { + const data = cache[key]; + delete data.metadata; + values.push(data); + } + + return values; + } + + if (isRoot) { + const textures = extractFromCache(meta.textures); + const images = extractFromCache(meta.images); + const nodes = extractFromCache(meta.nodes); + + if (textures.length > 0) data.textures = textures; + if (images.length > 0) data.images = images; + if (nodes.length > 0) data.nodes = nodes; + } + + return data; + } + + copy(source) { + this.lightsNode = source.lightsNode; + this.envNode = source.envNode; + + this.colorNode = source.colorNode; + this.normalNode = source.normalNode; + this.opacityNode = source.opacityNode; + this.backdropNode = source.backdropNode; + this.backdropAlphaNode = source.backdropAlphaNode; + this.alphaTestNode = source.alphaTestNode; + + this.positionNode = source.positionNode; + + this.depthNode = source.depthNode; + this.shadowNode = source.shadowNode; + this.shadowPositionNode = source.shadowPositionNode; + + this.outputNode = source.outputNode; + this.mrtNode = source.mrtNode; + + this.fragmentNode = source.fragmentNode; + this.vertexNode = source.vertexNode; + + return super.copy(source); + } + + static fromMaterial(material) { + if (material.isNodeMaterial === true) { + // is already a node material + + return material; + } + + const type = material.type.replace('Material', 'NodeMaterial'); + + const nodeMaterial = createNodeMaterialFromType(type); + + if (nodeMaterial === undefined) { + throw new Error(`NodeMaterial: Material "${material.type}" is not compatible.`); + } + + for (const key in material) { + nodeMaterial[key] = material[key]; + } + + return nodeMaterial; + } +} + +export default NodeMaterial; + +export function addNodeMaterial(type, nodeMaterial) { + if (typeof nodeMaterial !== 'function' || !type) throw new Error(`Node material ${type} is not a class`); + if (NodeMaterials.has(type)) { + console.warn(`Redefinition of node material ${type}`); + return; + } + + NodeMaterials.set(type, nodeMaterial); + nodeMaterial.type = type; +} + +export function createNodeMaterialFromType(type) { + const Material = NodeMaterials.get(type); + + if (Material !== undefined) { + return new Material(); + } +} + +addNodeMaterial('NodeMaterial', NodeMaterial); diff --git a/src-testing/src/nodes/materials/PointsNodeMaterial.d.ts b/src-testing/src/nodes/materials/PointsNodeMaterial.d.ts new file mode 100644 index 000000000..3132aa3ca --- /dev/null +++ b/src-testing/src/nodes/materials/PointsNodeMaterial.d.ts @@ -0,0 +1,22 @@ +import { PointsMaterialParameters } from "../../materials/PointsMaterial.js"; +import { Color } from "../../math/Color.js"; +import { Texture } from "../../textures/Texture.js"; +import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; + +export interface PointsNodeMaterialParameters extends NodeMaterialParameters, PointsMaterialParameters { +} + +export default class PointsNodeMaterial extends NodeMaterial { + readonly isPointsNodeMaterial: true; + + // Properties from PointsMaterial + readonly isPointsMaterial: true; + color: Color; + map: Texture | null; + alphaMap: Texture | null; + size: number; + sizeAttenuation: boolean; + fog: boolean; + + constructor(parameters?: PointsNodeMaterialParameters); +} diff --git a/src-testing/src/nodes/materials/ShadowNodeMaterial.d.ts b/src-testing/src/nodes/materials/ShadowNodeMaterial.d.ts new file mode 100644 index 000000000..716433eff --- /dev/null +++ b/src-testing/src/nodes/materials/ShadowNodeMaterial.d.ts @@ -0,0 +1,17 @@ +import { ShadowMaterialParameters } from "../../materials/ShadowMaterial.js"; +import { Color } from "../../math/Color.js"; +import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; + +export interface ShadowNodeMaterialParameters extends NodeMaterialParameters, ShadowMaterialParameters { +} + +export default class ShadowNodeMaterial extends NodeMaterial { + readonly isShadowNodeMaterial: true; + + // Properties from ShadowMaterial + readonly isShadowMaterial: true; + color: Color; + fog: boolean; + + constructor(parameters?: ShadowNodeMaterialParameters); +} diff --git a/src-testing/src/nodes/materials/SpriteNodeMaterial.d.ts b/src-testing/src/nodes/materials/SpriteNodeMaterial.d.ts new file mode 100644 index 000000000..c4c567c31 --- /dev/null +++ b/src-testing/src/nodes/materials/SpriteNodeMaterial.d.ts @@ -0,0 +1,26 @@ +import { SpriteMaterialParameters } from "../../materials/SpriteMaterial.js"; +import { Color } from "../../math/Color.js"; +import { Texture } from "../../textures/Texture.js"; +import Node from "../core/Node.js"; +import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; + +export interface SpriteNodeMaterialParameters extends NodeMaterialParameters, SpriteMaterialParameters { +} + +export default class SpriteNodeMaterial extends NodeMaterial { + isSpriteNodeMaterial: true; + + rotationNode: Node | null; + scaleNode: Node | null; + + // Properties from SpriteMaterial + readonly isSpriteMaterial: true; + color: Color; + map: Texture | null; + alphaMap: Texture | null; + rotation: number; + sizeAttenuation: boolean; + fog: boolean; + + constructor(parameters?: SpriteNodeMaterialParameters); +} diff --git a/src-testing/src/nodes/materials/VolumeNodeMaterial.d.ts b/src-testing/src/nodes/materials/VolumeNodeMaterial.d.ts new file mode 100644 index 000000000..d1f4b9cc0 --- /dev/null +++ b/src-testing/src/nodes/materials/VolumeNodeMaterial.d.ts @@ -0,0 +1,10 @@ +import Node from "../core/Node.js"; +import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; + +export default class VolumeNodeMaterial extends NodeMaterial { + lights: boolean; + readonly isVolumeNodeMaterial: true; + testNode: Node | null; + + constructor(parameters?: NodeMaterialParameters); +} diff --git a/src-testing/src/nodes/materialx/MaterialXNodes.d.ts b/src-testing/src/nodes/materialx/MaterialXNodes.d.ts new file mode 100644 index 000000000..cc0383590 --- /dev/null +++ b/src-testing/src/nodes/materialx/MaterialXNodes.d.ts @@ -0,0 +1,107 @@ +import Node from "../core/Node.js"; +import MathNode from "../math/MathNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import { mx_hsvtorgb, mx_rgbtohsv } from "./lib/mx_hsv.js"; +import { mx_srgb_texture_to_lin_rec709 } from "./lib/mx_transform_color.js"; + +export function mx_aastep(threshold: NodeRepresentation, value: NodeRepresentation): ShaderNodeObject; + +export function mx_ramplr( + valuel: NodeRepresentation, + valuer: NodeRepresentation, + texcoord?: NodeRepresentation, +): ShaderNodeObject; +export function mx_ramptb( + valuet: NodeRepresentation, + valueb: NodeRepresentation, + texcoord?: NodeRepresentation, +): ShaderNodeObject; + +export function mx_splitlr( + valuel: NodeRepresentation, + valuer: NodeRepresentation, + center: NodeRepresentation, + texcoord?: NodeRepresentation, +): ShaderNodeObject; +export function mx_splittb( + valuet: NodeRepresentation, + valueb: NodeRepresentation, + center: NodeRepresentation, + texcoord?: NodeRepresentation, +): ShaderNodeObject; + +export function mx_transform_uv( + uv_scale?: NodeRepresentation, + uv_offset?: NodeRepresentation, + uv_geo?: NodeRepresentation, +): ShaderNodeObject; + +export function mx_safepower(in1: NodeRepresentation, in2?: NodeRepresentation): ShaderNodeObject; + +export function mx_contrast( + input: NodeRepresentation, + amount?: NodeRepresentation, + pivot?: NodeRepresentation, +): ShaderNodeObject; + +export function mx_noise_float( + texcoord?: NodeRepresentation, + amplitude?: NodeRepresentation, + pivot?: NodeRepresentation, +): ShaderNodeObject; +export function mx_noise_vec3( + texcoord?: NodeRepresentation, + amplitude?: NodeRepresentation, + pivot?: NodeRepresentation, +): ShaderNodeObject; +export function mx_noise_vec4( + texcoord?: NodeRepresentation, + amplitude?: NodeRepresentation, + pivot?: NodeRepresentation, +): ShaderNodeObject; + +export function mx_worley_noise_float( + texcoord?: NodeRepresentation, + jitter?: NodeRepresentation, +): ShaderNodeObject; +export function mx_worley_noise_vec2( + texcoord?: NodeRepresentation, + jitter?: NodeRepresentation, +): ShaderNodeObject; +export function mx_worley_noise_vec3( + texcoord?: NodeRepresentation, + jitter?: NodeRepresentation, +): ShaderNodeObject; + +export function mx_cell_noise_float(texcoord?: NodeRepresentation): ShaderNodeObject; + +export function mx_fractal_noise_float( + position?: NodeRepresentation, + octaves?: NodeRepresentation, + lacunarity?: NodeRepresentation, + diminish?: NodeRepresentation, + amplitude?: NodeRepresentation, +): ShaderNodeObject; +export function mx_fractal_noise_vec2( + position?: NodeRepresentation, + octaves?: NodeRepresentation, + lacunarity?: NodeRepresentation, + diminish?: NodeRepresentation, + amplitude?: NodeRepresentation, +): ShaderNodeObject; +export function mx_fractal_noise_vec3( + position?: NodeRepresentation, + octaves?: NodeRepresentation, + lacunarity?: NodeRepresentation, + diminish?: NodeRepresentation, + amplitude?: NodeRepresentation, +): ShaderNodeObject; +export function mx_fractal_noise_vec4( + position?: NodeRepresentation, + octaves?: NodeRepresentation, + lacunarity?: NodeRepresentation, + diminish?: NodeRepresentation, + amplitude?: NodeRepresentation, +): ShaderNodeObject; + +export { mx_hsvtorgb, mx_rgbtohsv, mx_srgb_texture_to_lin_rec709 }; diff --git a/src-testing/src/nodes/materialx/lib/mx_hsv.d.ts b/src-testing/src/nodes/materialx/lib/mx_hsv.d.ts new file mode 100644 index 000000000..a826bdea2 --- /dev/null +++ b/src-testing/src/nodes/materialx/lib/mx_hsv.d.ts @@ -0,0 +1,6 @@ +import Node from "../../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../../shadernode/ShaderNode.js"; + +export const mx_hsvtorgb: (hsv_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_rgbtohsv: (c_immutable: NodeRepresentation) => ShaderNodeObject; diff --git a/src-testing/src/nodes/materialx/lib/mx_noise.d.ts b/src-testing/src/nodes/materialx/lib/mx_noise.d.ts new file mode 100644 index 000000000..982615616 --- /dev/null +++ b/src-testing/src/nodes/materialx/lib/mx_noise.d.ts @@ -0,0 +1,359 @@ +import Node from "../../core/Node.js"; +import VarNode from "../../core/VarNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../../shadernode/ShaderNode.js"; + +export const mx_select: ( + b_immutable: NodeRepresentation, + t_immutable: NodeRepresentation, + f_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_negate_if: ( + val_immutable: NodeRepresentation, + b_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_floor: (x_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_floorfrac: (x_immutable: NodeRepresentation, i: ShaderNodeObject) => ShaderNodeObject; + +export const mx_bilerp_0: ( + v0_immutable: NodeRepresentation, + v1_immutable: NodeRepresentation, + v2_immutable: NodeRepresentation, + v3_immutable: NodeRepresentation, + s_immutable: NodeRepresentation, + t_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_bilerp_1: ( + v0_immutable: NodeRepresentation, + v1_immutable: NodeRepresentation, + v2_immutable: NodeRepresentation, + v3_immutable: NodeRepresentation, + s_immutable: NodeRepresentation, + t_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_bilerp: ( + v0_immutable: NodeRepresentation, + v1_immutable: NodeRepresentation, + v2_immutable: NodeRepresentation, + v3_immutable: NodeRepresentation, + s_immutable: NodeRepresentation, + t_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_trilerp_0: ( + v0_immutable: NodeRepresentation, + v1_immutable: NodeRepresentation, + v2_immutable: NodeRepresentation, + v3_immutable: NodeRepresentation, + v4_immutable: NodeRepresentation, + v5_immutable: NodeRepresentation, + v6_immutable: NodeRepresentation, + v7_immutable: NodeRepresentation, + s_immutable: NodeRepresentation, + t_immutable: NodeRepresentation, + r_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_trilerp_1: ( + v0_immutable: NodeRepresentation, + v1_immutable: NodeRepresentation, + v2_immutable: NodeRepresentation, + v3_immutable: NodeRepresentation, + v4_immutable: NodeRepresentation, + v5_immutable: NodeRepresentation, + v6_immutable: NodeRepresentation, + v7_immutable: NodeRepresentation, + s_immutable: NodeRepresentation, + t_immutable: NodeRepresentation, + r_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_trilerp: ( + v0_immutable: NodeRepresentation, + v1_immutable: NodeRepresentation, + v2_immutable: NodeRepresentation, + v3_immutable: NodeRepresentation, + v4_immutable: NodeRepresentation, + v5_immutable: NodeRepresentation, + v6_immutable: NodeRepresentation, + v7_immutable: NodeRepresentation, + s_immutable: NodeRepresentation, + t_immutable: NodeRepresentation, + r_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_gradient_float_0: ( + hash_immutable: NodeRepresentation, + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_gradient_float_1: ( + hash_immutable: NodeRepresentation, + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, + z_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_gradient_float: ( + hash_immutable: NodeRepresentation, + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, + z_immutable?: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_gradient_vec3_0: ( + hash_immutable: NodeRepresentation, + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_gradient_vec3_1: ( + hash_immutable: NodeRepresentation, + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, + z_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_gradient_vec3: ( + hash_immutable: NodeRepresentation, + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, + z_immutable?: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_gradient_scale2d_0: (v_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_gradient_scale3d_0: (v_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_gradient_scale2d_1: (v_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_gradient_scale2d: (v_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_gradient_scale3d_1: (v_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_gradient_scale3d: (v_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_rotl32: (x_immutable: NodeRepresentation, k_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_bjmix: ( + a: ShaderNodeObject, + b: ShaderNodeObject, + c: ShaderNodeObject, +) => ShaderNodeObject; + +export const mx_bjfinal: ( + a_immutable: NodeRepresentation, + b_immutable: NodeRepresentation, + c_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_bits_to_01: (bits_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_fade: (t_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_hash_int_0: (x_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_hash_int_1: ( + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_hash_int_2: ( + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, + z_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_hash_int_3: ( + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, + z_immutable: NodeRepresentation, + xx_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_hash_int_4: ( + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, + z_immutable: NodeRepresentation, + xx_immutable: NodeRepresentation, + yy_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_hash_int: ( + x_immutable: NodeRepresentation, + y_immutable?: NodeRepresentation, + z_immutable?: NodeRepresentation, + xx_immutable?: NodeRepresentation, + yy_immutable?: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_hash_vec3_0: ( + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_hash_vec3_1: ( + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, + z_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_hash_vec3: ( + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, + z_immutable?: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_perlin_noise_float_0: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_perlin_noise_float_1: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_perlin_noise_float: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_perlin_noise_vec3_0: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_perlin_noise_vec3_1: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_perlin_noise_vec3: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_cell_noise_float_0: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_cell_noise_float_1: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_cell_noise_float_2: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_cell_noise_float_3: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_cell_noise_float: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_cell_noise_vec3_0: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_cell_noise_vec3_1: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_cell_noise_vec3_2: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_cell_noise_vec3_3: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_cell_noise_vec3: (p_immutable: NodeRepresentation) => ShaderNodeObject; + +export const mx_fractal_noise_float: ( + p_immutable: NodeRepresentation, + octaves_immutable: NodeRepresentation, + lacunarity_immutable: NodeRepresentation, + diminish_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_fractal_noise_vec3: ( + p_immutable: NodeRepresentation, + octaves_immutable: NodeRepresentation, + lacunarity_immutable: NodeRepresentation, + diminish_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_fractal_noise_vec2: ( + p_immutable: NodeRepresentation, + octaves_immutable: NodeRepresentation, + lacunarity_immutable: NodeRepresentation, + diminish_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_fractal_noise_vec4: ( + p_immutable: NodeRepresentation, + octaves_immutable: NodeRepresentation, + lacunarity_immutable: NodeRepresentation, + diminish_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_worley_distance_0: ( + p_immutable: NodeRepresentation, + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, + z_immutable: NodeRepresentation, + xoff_immutable: NodeRepresentation, + yoff_immutable: NodeRepresentation, + jitter_immutable: NodeRepresentation, + metric_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_worley_distance_1: ( + p_immutable: NodeRepresentation, + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, + z_immutable: NodeRepresentation, + xoff_immutable: NodeRepresentation, + yoff_immutable: NodeRepresentation, + zoff_immutable: NodeRepresentation, + jitter_immutable: NodeRepresentation, + metric_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_worley_distance: ( + p_immutable: NodeRepresentation, + x_immutable: NodeRepresentation, + y_immutable: NodeRepresentation, + z_immutable: NodeRepresentation, + xoff_immutable: NodeRepresentation, + yoff_immutable: NodeRepresentation, + zoff_immutable: NodeRepresentation, + jitter_immutable: NodeRepresentation, + metric_immutable?: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_worley_noise_float_0: ( + p_immutable: NodeRepresentation, + jitter_immutable: NodeRepresentation, + metric_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_worley_noise_vec2_0: ( + p_immutable: NodeRepresentation, + jitter_immutable: NodeRepresentation, + metric_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_worley_noise_vec3_0: ( + p_immutable: NodeRepresentation, + jitter_immutable: NodeRepresentation, + metric_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_worley_noise_float_1: ( + p_immutable: NodeRepresentation, + jitter_immutable: NodeRepresentation, + metric_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_worley_noise_float: ( + p_immutable: NodeRepresentation, + jitter_immutable: NodeRepresentation, + metric_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_worley_noise_vec2_1: ( + p_immutable: NodeRepresentation, + jitter_immutable: NodeRepresentation, + metric_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_worley_noise_vec2: ( + p_immutable: NodeRepresentation, + jitter_immutable: NodeRepresentation, + metric_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_worley_noise_vec3_1: ( + p_immutable: NodeRepresentation, + jitter_immutable: NodeRepresentation, + metric_immutable: NodeRepresentation, +) => ShaderNodeObject; + +export const mx_worley_noise_vec3: ( + p_immutable: NodeRepresentation, + jitter_immutable: NodeRepresentation, + metric_immutable: NodeRepresentation, +) => ShaderNodeObject; diff --git a/src-testing/src/nodes/materialx/lib/mx_transform_color.d.ts b/src-testing/src/nodes/materialx/lib/mx_transform_color.d.ts new file mode 100644 index 000000000..7cdeb5747 --- /dev/null +++ b/src-testing/src/nodes/materialx/lib/mx_transform_color.d.ts @@ -0,0 +1,4 @@ +import Node from "../../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../../shadernode/ShaderNode.js"; + +export const mx_srgb_texture_to_lin_rec709: (color_immutable: NodeRepresentation) => ShaderNodeObject; diff --git a/src-testing/src/nodes/math/CondNode.d.ts b/src-testing/src/nodes/math/CondNode.d.ts new file mode 100644 index 000000000..973a57af2 --- /dev/null +++ b/src-testing/src/nodes/math/CondNode.d.ts @@ -0,0 +1,37 @@ +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class CondNode extends Node { + condNode: Node; + ifNode: Node; + elseNode: Node; + + constructor(condNode: Node, ifNode: Node, elseNode: Node); +} + +export const select: ( + condNode: NodeRepresentation, + ifNode: NodeRepresentation, + elseNode: NodeRepresentation, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + select: typeof select; + } +} + +/** + * @deprecated cond() has been renamed to select() + */ +export const cond: ( + condNode: NodeRepresentation, + ifNode: NodeRepresentation, + elseNode: NodeRepresentation, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + cond: typeof cond; + } +} diff --git a/src-testing/src/nodes/math/HashNode.d.ts b/src-testing/src/nodes/math/HashNode.d.ts new file mode 100644 index 000000000..8e89ac588 --- /dev/null +++ b/src-testing/src/nodes/math/HashNode.d.ts @@ -0,0 +1,16 @@ +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class HashNode extends Node { + seedNode: Node; + + constructor(seedNode: Node); +} + +export const hash: (seedNode: NodeRepresentation) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + hash: typeof hash; + } +} diff --git a/src-testing/src/nodes/math/MathNode.d.ts b/src-testing/src/nodes/math/MathNode.d.ts new file mode 100644 index 000000000..6a5834cba --- /dev/null +++ b/src-testing/src/nodes/math/MathNode.d.ts @@ -0,0 +1,273 @@ +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import OperatorNode from "./OperatorNode.js"; + +export type MathNodeMethod1 = + | typeof MathNode.RADIANS + | typeof MathNode.DEGREES + | typeof MathNode.EXP + | typeof MathNode.EXP2 + | typeof MathNode.LOG + | typeof MathNode.LOG2 + | typeof MathNode.SQRT + | typeof MathNode.INVERSE_SQRT + | typeof MathNode.FLOOR + | typeof MathNode.CEIL + | typeof MathNode.NORMALIZE + | typeof MathNode.FRACT + | typeof MathNode.SIN + | typeof MathNode.COS + | typeof MathNode.TAN + | typeof MathNode.ASIN + | typeof MathNode.ACOS + | typeof MathNode.ATAN + | typeof MathNode.ABS + | typeof MathNode.SIGN + | typeof MathNode.LENGTH + | typeof MathNode.NEGATE + | typeof MathNode.ONE_MINUS + | typeof MathNode.DFDX + | typeof MathNode.DFDY + | typeof MathNode.ROUND + | typeof MathNode.RECIPROCAL + | typeof MathNode.TRUNC + | typeof MathNode.FWIDTH + | typeof MathNode.BITCAST + | typeof MathNode.TRANSPOSE; + +export type MathNodeMethod2 = + | typeof MathNode.ATAN2 + | typeof MathNode.MIN + | typeof MathNode.MAX + | typeof MathNode.MOD + | typeof MathNode.STEP + | typeof MathNode.REFLECT + | typeof MathNode.DISTANCE + | typeof MathNode.DOT + | typeof MathNode.CROSS + | typeof MathNode.POW + | typeof MathNode.TRANSFORM_DIRECTION; + +export type MathNodeMethod3 = + | typeof MathNode.MIX + | typeof MathNode.CLAMP + | typeof MathNode.REFRACT + | typeof MathNode.SMOOTHSTEP + | typeof MathNode.FACEFORWARD; + +export type MathNodeMethod = MathNodeMethod1 | MathNodeMethod2 | MathNodeMethod3; + +export default class MathNode extends TempNode { + // 1 input + + static ALL: "all"; + static ANY: "any"; + static EQUALS: "equals"; + + static RADIANS: "radians"; + static DEGREES: "degrees"; + static EXP: "exp"; + static EXP2: "exp2"; + static LOG: "log"; + static LOG2: "log2"; + static SQRT: "sqrt"; + static INVERSE_SQRT: "inversesqrt"; + static FLOOR: "floor"; + static CEIL: "ceil"; + static NORMALIZE: "normalize"; + static FRACT: "fract"; + static SIN: "sin"; + static COS: "cos"; + static TAN: "tan"; + static ASIN: "asin"; + static ACOS: "acos"; + static ATAN: "atan"; + static ABS: "abs"; + static SIGN: "sign"; + static LENGTH: "length"; + static NEGATE: "negate"; + static ONE_MINUS: "oneMinus"; + static DFDX: "dFdx"; + static DFDY: "dFdy"; + static ROUND: "round"; + static RECIPROCAL: "reciprocal"; + static TRUNC: "trunc"; + static FWIDTH: "fwidth"; + static BITCAST: "bitcast"; + static TRANSPOSE: "transpose"; + + // 2 inputs + + static ATAN2: "atan2"; + static MIN: "min"; + static MAX: "max"; + static MOD: "mod"; + static STEP: "step"; + static REFLECT: "reflect"; + static DISTANCE: "distance"; + static DOT: "dot"; + static CROSS: "cross"; + static POW: "pow"; + static TRANSFORM_DIRECTION: "transformDirection"; + + // 3 inputs + + static MIX: "mix"; + static CLAMP: "clamp"; + static REFRACT: "refract"; + static SMOOTHSTEP: "smoothstep"; + static FACEFORWARD: "faceforward"; + + method: MathNodeMethod; + aNode: Node; + bNode: Node | null; + cNode: Node | null; + + constructor(method: MathNodeMethod1, aNode: Node); + constructor(method: MathNodeMethod2, aNode: Node, bNode: Node); + constructor(method: MathNodeMethod3, aNode: Node, bNode: Node, cNode: Node); +} + +export const EPSILON: ShaderNodeObject; +export const INFINITY: ShaderNodeObject; +export const PI: ShaderNodeObject; +export const PI2: ShaderNodeObject; + +type Unary = (a: NodeRepresentation) => ShaderNodeObject; + +export const all: Unary; +export const any: Unary; +export const equals: Unary; + +export const radians: Unary; +export const degrees: Unary; +export const exp: Unary; +export const exp2: Unary; +export const log: Unary; +export const log2: Unary; +export const sqrt: Unary; +export const inverseSqrt: Unary; +export const floor: Unary; +export const ceil: Unary; +export const normalize: Unary; +export const fract: Unary; +export const sin: Unary; +export const cos: Unary; +export const tan: Unary; +export const asin: Unary; +export const acos: Unary; +export const atan: Unary; +export const abs: Unary; +export const sign: Unary; +export const length: Unary; +export const negate: Unary; +export const oneMinus: Unary; +export const dFdx: Unary; +export const dFdy: Unary; +export const round: Unary; +export const reciprocal: Unary; +export const trunc: Unary; +export const fwidth: Unary; +export const bitcast: Unary; +export const transpose: Unary; + +type Binary = (a: NodeRepresentation, b: NodeRepresentation) => ShaderNodeObject; + +export const atan2: Binary; +export const min: Binary; +export const max: Binary; +export const mod: Binary; +export const step: Binary; +export const reflect: Binary; +export const distance: Binary; +export const difference: Binary; +export const dot: Binary; +export const cross: Binary; +export const pow: Binary; +export const pow2: Binary; +export const pow3: Binary; +export const pow4: Binary; +export const transformDirection: Binary; + +type Ternary = (a: NodeRepresentation, b: NodeRepresentation, c: NodeRepresentation) => ShaderNodeObject; + +export const cbrt: Unary; +export const lengthSq: Unary; +export const mix: Ternary; +export const clamp: ( + a: NodeRepresentation, + b?: NodeRepresentation, + c?: NodeRepresentation, +) => ShaderNodeObject; +export const saturate: Unary; +export const refract: Ternary; +export const smoothstep: Ternary; +export const faceForward: Ternary; + +export const rand: (uv: NodeRepresentation) => ShaderNodeObject; + +export const mixElement: Ternary; +export const smoothstepElement: Ternary; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + all: typeof all; + any: typeof any; + equals: typeof equals; + radians: typeof radians; + degrees: typeof degrees; + exp: typeof exp; + exp2: typeof exp2; + log: typeof log; + log2: typeof log2; + sqrt: typeof sqrt; + inverseSqrt: typeof inverseSqrt; + floor: typeof floor; + ceil: typeof ceil; + normalize: typeof normalize; + fract: typeof fract; + sin: typeof sin; + cos: typeof cos; + tan: typeof tan; + asin: typeof asin; + acos: typeof acos; + atan: typeof atan; + abs: typeof abs; + sign: typeof sign; + length: typeof length; + lengthSq: typeof lengthSq; + negate: typeof negate; + oneMinus: typeof oneMinus; + dFdx: typeof dFdx; + dFdy: typeof dFdy; + round: typeof round; + reciprocal: typeof reciprocal; + trunc: typeof trunc; + fwidth: typeof fwidth; + atan2: typeof atan2; + min: typeof min; + max: typeof max; + mod: typeof mod; + step: typeof step; + reflect: typeof reflect; + distance: typeof distance; + dot: typeof dot; + cross: typeof cross; + pow: typeof pow; + pow2: typeof pow2; + pow3: typeof pow3; + pow4: typeof pow4; + transformDirection: typeof transformDirection; + mix: typeof mixElement; + clamp: typeof clamp; + refract: typeof refract; + smoothstep: typeof smoothstepElement; + faceForward: typeof faceForward; + difference: typeof difference; + saturate: typeof saturate; + cbrt: typeof cbrt; + transpose: typeof transpose; + rand: typeof rand; + } +} diff --git a/src-testing/src/nodes/math/MathUtils.d.ts b/src-testing/src/nodes/math/MathUtils.d.ts new file mode 100644 index 000000000..d2591618b --- /dev/null +++ b/src-testing/src/nodes/math/MathUtils.d.ts @@ -0,0 +1,16 @@ +import { Binary, Ternary } from "./MathNode.js"; + +// remapping functions +export const parabola: Binary; +export const gain: Binary; +export const pcurve: Ternary; +export const sinc: Binary; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + parabola: typeof parabola; + gain: typeof gain; + pcurve: typeof pcurve; + sinc: typeof sinc; + } +} diff --git a/src-testing/src/nodes/math/OperatorNode.d.ts b/src-testing/src/nodes/math/OperatorNode.d.ts new file mode 100644 index 000000000..7e89ed0f9 --- /dev/null +++ b/src-testing/src/nodes/math/OperatorNode.d.ts @@ -0,0 +1,83 @@ +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export type OperatorNodeOp = + | "%" + | "&" + | "|" + | "^" + | ">>" + | "<<" + | "==" + | "&&" + | "||" + | "^^" + | "<" + | ">" + | "<=" + | ">=" + | "+" + | "-" + | "*" + | "/"; + +export default class OperatorNode extends TempNode { + aNode: Node; + bNode: Node; + op: OperatorNodeOp; + + constructor(op: OperatorNodeOp, ...params: [Node, Node, ...Node[]]); +} + +type Operator = ( + a: NodeRepresentation, + b: NodeRepresentation, + ...others: NodeRepresentation[] +) => ShaderNodeObject; + +export const add: Operator; +export const sub: Operator; +export const mul: Operator; +export const div: Operator; +export const remainder: Operator; +export const equal: Operator; +export const lessThan: Operator; +export const greaterThan: Operator; +export const lessThanEqual: Operator; +export const greaterThanEqual: Operator; +export const and: Operator; +export const or: Operator; +export const not: (a: NodeRepresentation) => ShaderNodeObject; +export const xor: Operator; +export const bitAnd: Operator; +export const bitNot: (a: NodeRepresentation) => ShaderNodeObject; +export const bitOr: Operator; +export const bitXor: Operator; +export const shiftLeft: Operator; +export const shiftRight: Operator; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + add: typeof add; + sub: typeof sub; + mul: typeof mul; + div: typeof div; + remainder: typeof remainder; + equal: typeof equal; + lessThan: typeof lessThan; + greaterThan: typeof greaterThan; + lessThanEqual: typeof lessThanEqual; + greaterThanEqual: typeof greaterThanEqual; + and: typeof and; + or: typeof or; + not: typeof not; + xor: typeof xor; + bitAnd: typeof bitAnd; + bitNot: typeof bitNot; + bitOr: typeof bitOr; + bitXor: typeof bitXor; + shiftLeft: typeof shiftLeft; + shiftRight: typeof shiftRight; + } +} diff --git a/src-testing/src/nodes/math/TriNoise3D.d.ts b/src-testing/src/nodes/math/TriNoise3D.d.ts new file mode 100644 index 000000000..88a92a440 --- /dev/null +++ b/src-testing/src/nodes/math/TriNoise3D.d.ts @@ -0,0 +1,12 @@ +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const tri: (x: NodeRepresentation) => ShaderNodeObject; + +export const tri3: (p: NodeRepresentation) => ShaderNodeObject; + +export const triNoise3D: ( + p_immutable: NodeRepresentation, + spd: NodeRepresentation, + time: NodeRepresentation, +) => ShaderNodeObject; diff --git a/src-testing/src/nodes/pmrem/PMREMNode.d.ts b/src-testing/src/nodes/pmrem/PMREMNode.d.ts new file mode 100644 index 000000000..886aa24fc --- /dev/null +++ b/src-testing/src/nodes/pmrem/PMREMNode.d.ts @@ -0,0 +1,20 @@ +import { Texture } from "../../textures/Texture.js"; +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class PMREMNode extends TempNode { + uvNode: Node | null; + levelNode: Node | null; + + constructor(value: Texture, uvNode?: Node | null, levelNode?: Node | null); + + set value(value: Texture); + get value(): Texture; +} + +export const pmremTexture: ( + value: Texture, + uvNode?: NodeRepresentation, + levelNode?: NodeRepresentation, +) => ShaderNodeObject; diff --git a/src-testing/src/nodes/procedural/CheckerNode.d.ts b/src-testing/src/nodes/procedural/CheckerNode.d.ts new file mode 100644 index 000000000..74a1b731e --- /dev/null +++ b/src-testing/src/nodes/procedural/CheckerNode.d.ts @@ -0,0 +1,15 @@ +import TempNode from "../core/TempNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class CheckerNode extends TempNode { + uvNode: Node; + constructor(uvNode?: Node); +} + +export const checker: (uvNode?: NodeRepresentation) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + checker: typeof checker; + } +} diff --git a/src-testing/src/nodes/shadernode/ShaderNode.ts b/src-testing/src/nodes/shadernode/ShaderNode.ts new file mode 100644 index 000000000..9b8e26e58 --- /dev/null +++ b/src-testing/src/nodes/shadernode/ShaderNode.ts @@ -0,0 +1,538 @@ +import Node, { addNodeClass } from '../core/Node.js'; +import ArrayElementNode from '../utils/ArrayElementNode.js'; +import ConvertNode from '../utils/ConvertNode.js'; +import JoinNode from '../utils/JoinNode.js'; +import SplitNode from '../utils/SplitNode.js'; +import SetNode from '../utils/SetNode.js'; +import ConstNode from '../core/ConstNode.js'; +import { getValueFromType, getValueType } from '../core/NodeUtils.js'; + +// + +let currentStack = null; + +const NodeElements = new Map(); // @TODO: Currently only a few nodes are added, probably also add others + +export function addNodeElement(name, nodeElement) { + if (NodeElements.has(name)) { + console.warn(`Redefinition of node element ${name}`); + return; + } + + if (typeof nodeElement !== 'function') throw new Error(`Node element ${name} is not a function`); + + NodeElements.set(name, nodeElement); +} + +const parseSwizzle = props => props.replace(/r|s/g, 'x').replace(/g|t/g, 'y').replace(/b|p/g, 'z').replace(/a|q/g, 'w'); + +const shaderNodeHandler = { + setup(NodeClosure, params) { + const inputs = params.shift(); + + return NodeClosure(nodeObjects(inputs), ...params); + }, + + get(node, prop, nodeObj) { + if (typeof prop === 'string' && node[prop] === undefined) { + if (node.isStackNode !== true && prop === 'assign') { + return (...params) => { + currentStack.assign(nodeObj, ...params); + + return nodeObj; + }; + } else if (NodeElements.has(prop)) { + const nodeElement = NodeElements.get(prop); + + return node.isStackNode + ? (...params) => nodeObj.add(nodeElement(...params)) + : (...params) => nodeElement(nodeObj, ...params); + } else if (prop === 'self') { + return node; + } else if (prop.endsWith('Assign') && NodeElements.has(prop.slice(0, prop.length - 'Assign'.length))) { + const nodeElement = NodeElements.get(prop.slice(0, prop.length - 'Assign'.length)); + + return node.isStackNode + ? (...params) => nodeObj.assign(params[0], nodeElement(...params)) + : (...params) => nodeObj.assign(nodeElement(nodeObj, ...params)); + } else if (/^[xyzwrgbastpq]{1,4}$/.test(prop) === true) { + // accessing properties ( swizzle ) + + prop = parseSwizzle(prop); + + return nodeObject(new SplitNode(nodeObj, prop)); + } else if (/^set[XYZWRGBASTPQ]{1,4}$/.test(prop) === true) { + // set properties ( swizzle ) + + prop = parseSwizzle(prop.slice(3).toLowerCase()); + + // sort to xyzw sequence + + prop = prop.split('').sort().join(''); + + return value => nodeObject(new SetNode(node, prop, value)); + } else if (prop === 'width' || prop === 'height' || prop === 'depth') { + // accessing property + + if (prop === 'width') prop = 'x'; + else if (prop === 'height') prop = 'y'; + else if (prop === 'depth') prop = 'z'; + + return nodeObject(new SplitNode(node, prop)); + } else if (/^\d+$/.test(prop) === true) { + // accessing array + + return nodeObject(new ArrayElementNode(nodeObj, new ConstNode(Number(prop), 'uint'))); + } + } + + return Reflect.get(node, prop, nodeObj); + }, + + set(node, prop, value, nodeObj) { + if (typeof prop === 'string' && node[prop] === undefined) { + // setting properties + + if ( + /^[xyzwrgbastpq]{1,4}$/.test(prop) === true || + prop === 'width' || + prop === 'height' || + prop === 'depth' || + /^\d+$/.test(prop) === true + ) { + nodeObj[prop].assign(value); + + return true; + } + } + + return Reflect.set(node, prop, value, nodeObj); + }, +}; + +const nodeObjectsCacheMap = new WeakMap(); +const nodeBuilderFunctionsCacheMap = new WeakMap(); + +const ShaderNodeObject = function (obj, altType = null) { + const type = getValueType(obj); + + if (type === 'node') { + let nodeObject = nodeObjectsCacheMap.get(obj); + + if (nodeObject === undefined) { + nodeObject = new Proxy(obj, shaderNodeHandler); + + nodeObjectsCacheMap.set(obj, nodeObject); + nodeObjectsCacheMap.set(nodeObject, nodeObject); + } + + return nodeObject; + } else if ( + (altType === null && (type === 'float' || type === 'boolean')) || + (type && type !== 'shader' && type !== 'string') + ) { + return nodeObject(getConstNode(obj, altType)); + } else if (type === 'shader') { + return Fn(obj); + } + + return obj; +}; + +const ShaderNodeObjects = function (objects, altType = null) { + for (const name in objects) { + objects[name] = nodeObject(objects[name], altType); + } + + return objects; +}; + +const ShaderNodeArray = function (array, altType = null) { + const len = array.length; + + for (let i = 0; i < len; i++) { + array[i] = nodeObject(array[i], altType); + } + + return array; +}; + +const ShaderNodeProxy = function (NodeClass, scope = null, factor = null, settings = null) { + const assignNode = node => nodeObject(settings !== null ? Object.assign(node, settings) : node); + + if (scope === null) { + return (...params) => { + return assignNode(new NodeClass(...nodeArray(params))); + }; + } else if (factor !== null) { + factor = nodeObject(factor); + + return (...params) => { + return assignNode(new NodeClass(scope, ...nodeArray(params), factor)); + }; + } else { + return (...params) => { + return assignNode(new NodeClass(scope, ...nodeArray(params))); + }; + } +}; + +const ShaderNodeImmutable = function (NodeClass, ...params) { + return nodeObject(new NodeClass(...nodeArray(params))); +}; + +class ShaderCallNodeInternal extends Node { + constructor(shaderNode, inputNodes) { + super(); + + this.shaderNode = shaderNode; + this.inputNodes = inputNodes; + } + + getNodeType(builder) { + const properties = builder.getNodeProperties(this); + + if (properties.outputNode === null) { + properties.outputNode = this.setupOutput(builder); + } + + return properties.outputNode.getNodeType(builder); + } + + call(builder) { + const { shaderNode, inputNodes } = this; + + if (shaderNode.layout) { + let functionNodesCacheMap = nodeBuilderFunctionsCacheMap.get(builder.constructor); + + if (functionNodesCacheMap === undefined) { + functionNodesCacheMap = new WeakMap(); + + nodeBuilderFunctionsCacheMap.set(builder.constructor, functionNodesCacheMap); + } + + let functionNode = functionNodesCacheMap.get(shaderNode); + + if (functionNode === undefined) { + functionNode = nodeObject(builder.buildFunctionNode(shaderNode)); + + functionNodesCacheMap.set(shaderNode, functionNode); + } + + if (builder.currentFunctionNode !== null) { + builder.currentFunctionNode.includes.push(functionNode); + } + + return nodeObject(functionNode.call(inputNodes)); + } + + const jsFunc = shaderNode.jsFunc; + const outputNode = inputNodes !== null ? jsFunc(inputNodes, builder) : jsFunc(builder); + + return nodeObject(outputNode); + } + + setup(builder) { + const { outputNode } = builder.getNodeProperties(this); + + return outputNode || this.setupOutput(builder); + } + + setupOutput(builder) { + builder.addStack(); + + builder.stack.outputNode = this.call(builder); + + return builder.removeStack(); + } + + generate(builder, output) { + const { outputNode } = builder.getNodeProperties(this); + + if (outputNode === null) { + // TSL: It's recommended to use `tslFn` in setup() pass. + + return this.call(builder).build(builder, output); + } + + return super.generate(builder, output); + } +} + +class ShaderNodeInternal extends Node { + constructor(jsFunc) { + super(); + + this.jsFunc = jsFunc; + this.layout = null; + + this.global = true; + } + + get isArrayInput() { + return /^\((\s+)?\[/.test(this.jsFunc.toString()); + } + + setLayout(layout) { + this.layout = layout; + + return this; + } + + call(inputs = null) { + nodeObjects(inputs); + + return nodeObject(new ShaderCallNodeInternal(this, inputs)); + } + + setup() { + return this.call(); + } +} + +const bools = [false, true]; +const uints = [0, 1, 2, 3]; +const ints = [-1, -2]; +const floats = [ + 0.5, + 1.5, + 1 / 3, + 1e-6, + 1e6, + Math.PI, + Math.PI * 2, + 1 / Math.PI, + 2 / Math.PI, + 1 / (Math.PI * 2), + Math.PI / 2, +]; + +const boolsCacheMap = new Map(); +for (const bool of bools) boolsCacheMap.set(bool, new ConstNode(bool)); + +const uintsCacheMap = new Map(); +for (const uint of uints) uintsCacheMap.set(uint, new ConstNode(uint, 'uint')); + +const intsCacheMap = new Map([...uintsCacheMap].map(el => new ConstNode(el.value, 'int'))); +for (const int of ints) intsCacheMap.set(int, new ConstNode(int, 'int')); + +const floatsCacheMap = new Map([...intsCacheMap].map(el => new ConstNode(el.value))); +for (const float of floats) floatsCacheMap.set(float, new ConstNode(float)); +for (const float of floats) floatsCacheMap.set(-float, new ConstNode(-float)); + +const cacheMaps = { bool: boolsCacheMap, uint: uintsCacheMap, ints: intsCacheMap, float: floatsCacheMap }; + +const constNodesCacheMap = new Map([...boolsCacheMap, ...floatsCacheMap]); + +const getConstNode = (value, type) => { + if (constNodesCacheMap.has(value)) { + return constNodesCacheMap.get(value); + } else if (value.isNode === true) { + return value; + } else { + return new ConstNode(value, type); + } +}; + +const safeGetNodeType = node => { + try { + return node.getNodeType(); + } catch (_) { + return undefined; + } +}; + +const ConvertType = function (type, cacheMap = null) { + return (...params) => { + if ( + params.length === 0 || + (!['bool', 'float', 'int', 'uint'].includes(type) && params.every(param => typeof param !== 'object')) + ) { + params = [getValueFromType(type, ...params)]; + } + + if (params.length === 1 && cacheMap !== null && cacheMap.has(params[0])) { + return nodeObject(cacheMap.get(params[0])); + } + + if (params.length === 1) { + const node = getConstNode(params[0], type); + if (safeGetNodeType(node) === type) return nodeObject(node); + return nodeObject(new ConvertNode(node, type)); + } + + const nodes = params.map(param => getConstNode(param)); + return nodeObject(new JoinNode(nodes, type)); + }; +}; + +// exports + +export const defined = v => (typeof v === 'object' && v !== null ? v.value : v); // TODO: remove boolean conversion and defined function + +// utils + +export const getConstNodeType = value => + value !== undefined && value !== null + ? value.nodeType || value.convertTo || (typeof value === 'string' ? value : null) + : null; + +// shader node base + +export function ShaderNode(jsFunc) { + return new Proxy(new ShaderNodeInternal(jsFunc), shaderNodeHandler); +} + +export const nodeObject = (val, altType = null) => /* new */ ShaderNodeObject(val, altType); +export const nodeObjects = (val, altType = null) => new ShaderNodeObjects(val, altType); +export const nodeArray = (val, altType = null) => new ShaderNodeArray(val, altType); +export const nodeProxy = (...params) => new ShaderNodeProxy(...params); +export const nodeImmutable = (...params) => new ShaderNodeImmutable(...params); + +export const Fn = jsFunc => { + const shaderNode = new ShaderNode(jsFunc); + + const fn = (...params) => { + let inputs; + + nodeObjects(params); + + if (params[0] && params[0].isNode) { + inputs = [...params]; + } else { + inputs = params[0]; + } + + return shaderNode.call(inputs); + }; + + fn.shaderNode = shaderNode; + fn.setLayout = layout => { + shaderNode.setLayout(layout); + + return fn; + }; + + return fn; +}; + +export const tslFn = (...params) => { + // @deprecated, r168 + + console.warn('TSL.tslFn: tslFn() has been renamed to Fn().'); + return Fn(...params); +}; + +addNodeClass('ShaderNode', ShaderNode); + +// + +addNodeElement('toGlobal', node => { + node.global = true; + + return node; +}); + +// + +export const setCurrentStack = stack => { + if (currentStack === stack) { + //throw new Error( 'Stack already defined.' ); + } + + currentStack = stack; +}; + +export const getCurrentStack = () => currentStack; + +export const If = (...params) => currentStack.If(...params); + +export function append(node) { + if (currentStack) currentStack.add(node); + + return node; +} + +addNodeElement('append', append); + +// types +// @TODO: Maybe export from ConstNode.js? + +export const color = new ConvertType('color'); + +export const float = new ConvertType('float', cacheMaps.float); +export const int = new ConvertType('int', cacheMaps.ints); +export const uint = new ConvertType('uint', cacheMaps.uint); +export const bool = new ConvertType('bool', cacheMaps.bool); + +export const vec2 = new ConvertType('vec2'); +export const ivec2 = new ConvertType('ivec2'); +export const uvec2 = new ConvertType('uvec2'); +export const bvec2 = new ConvertType('bvec2'); + +export const vec3 = new ConvertType('vec3'); +export const ivec3 = new ConvertType('ivec3'); +export const uvec3 = new ConvertType('uvec3'); +export const bvec3 = new ConvertType('bvec3'); + +export const vec4 = new ConvertType('vec4'); +export const ivec4 = new ConvertType('ivec4'); +export const uvec4 = new ConvertType('uvec4'); +export const bvec4 = new ConvertType('bvec4'); + +export const mat2 = new ConvertType('mat2'); +export const imat2 = new ConvertType('imat2'); +export const umat2 = new ConvertType('umat2'); +export const bmat2 = new ConvertType('bmat2'); + +export const mat3 = new ConvertType('mat3'); +export const imat3 = new ConvertType('imat3'); +export const umat3 = new ConvertType('umat3'); +export const bmat3 = new ConvertType('bmat3'); + +export const mat4 = new ConvertType('mat4'); +export const imat4 = new ConvertType('imat4'); +export const umat4 = new ConvertType('umat4'); +export const bmat4 = new ConvertType('bmat4'); + +export const string = (value = '') => nodeObject(new ConstNode(value, 'string')); +export const arrayBuffer = value => nodeObject(new ConstNode(value, 'ArrayBuffer')); + +addNodeElement('toColor', color); +addNodeElement('toFloat', float); +addNodeElement('toInt', int); +addNodeElement('toUint', uint); +addNodeElement('toBool', bool); +addNodeElement('toVec2', vec2); +addNodeElement('toIvec2', ivec2); +addNodeElement('toUvec2', uvec2); +addNodeElement('toBvec2', bvec2); +addNodeElement('toVec3', vec3); +addNodeElement('toIvec3', ivec3); +addNodeElement('toUvec3', uvec3); +addNodeElement('toBvec3', bvec3); +addNodeElement('toVec4', vec4); +addNodeElement('toIvec4', ivec4); +addNodeElement('toUvec4', uvec4); +addNodeElement('toBvec4', bvec4); +addNodeElement('toMat2', mat2); +addNodeElement('toImat2', imat2); +addNodeElement('toUmat2', umat2); +addNodeElement('toBmat2', bmat2); +addNodeElement('toMat3', mat3); +addNodeElement('toImat3', imat3); +addNodeElement('toUmat3', umat3); +addNodeElement('toBmat3', bmat3); +addNodeElement('toMat4', mat4); +addNodeElement('toImat4', imat4); +addNodeElement('toUmat4', umat4); +addNodeElement('toBmat4', bmat4); + +// basic nodes +// HACK - we cannot export them from the corresponding files because of the cyclic dependency +export const element = nodeProxy(ArrayElementNode); +export const convert = (node, types) => nodeObject(new ConvertNode(nodeObject(node), types)); +export const split = (node, channels) => nodeObject(new SplitNode(nodeObject(node), channels)); + +addNodeElement('element', element); +addNodeElement('convert', convert); diff --git a/src-testing/src/nodes/utils/ArrayElementNode.d.ts b/src-testing/src/nodes/utils/ArrayElementNode.d.ts new file mode 100644 index 000000000..650f04047 --- /dev/null +++ b/src-testing/src/nodes/utils/ArrayElementNode.d.ts @@ -0,0 +1,9 @@ +import Node from "../core/Node.js"; +import { TempNode } from "../Nodes.js"; + +export default class ArrayElementNode extends TempNode { + node: Node; + indexNode: Node; + + constructor(node: Node, indexNode: Node); +} diff --git a/src-testing/src/nodes/utils/ConvertNode.d.ts b/src-testing/src/nodes/utils/ConvertNode.d.ts new file mode 100644 index 000000000..7972df608 --- /dev/null +++ b/src-testing/src/nodes/utils/ConvertNode.d.ts @@ -0,0 +1,7 @@ +import Node from "../core/Node.js"; + +export default class ConvertNode extends Node { + node: Node; + convertTo: string; + constructor(node: Node, convertTo: string); +} diff --git a/src-testing/src/nodes/utils/DiscardNode.d.ts b/src-testing/src/nodes/utils/DiscardNode.d.ts new file mode 100644 index 000000000..f18567d07 --- /dev/null +++ b/src-testing/src/nodes/utils/DiscardNode.d.ts @@ -0,0 +1,17 @@ +import Node from "../core/Node.js"; +import CondNode from "../math/CondNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class DiscardNode extends CondNode { + constructor(condNode: Node); +} + +export const inlineDiscard: (condNode: NodeRepresentation) => ShaderNodeObject; +export const discard: (condNode: NodeRepresentation) => ShaderNodeObject; +export const Return: (condNode: NodeRepresentation) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + discard: typeof discard; + } +} diff --git a/src-testing/src/nodes/utils/EquirectUVNode.d.ts b/src-testing/src/nodes/utils/EquirectUVNode.d.ts new file mode 100644 index 000000000..9020033d1 --- /dev/null +++ b/src-testing/src/nodes/utils/EquirectUVNode.d.ts @@ -0,0 +1,8 @@ +import { Node, TempNode } from "../Nodes.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class EquirectUVNode extends TempNode { + constructor(dirNode?: ShaderNodeObject); +} + +export const equirectUV: ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/JoinNode.d.ts b/src-testing/src/nodes/utils/JoinNode.d.ts new file mode 100644 index 000000000..7f456bafa --- /dev/null +++ b/src-testing/src/nodes/utils/JoinNode.d.ts @@ -0,0 +1,10 @@ +import Node from "../core/Node.js"; +import { TempNode } from "../Nodes.js"; + +/** + * This node constructs given type from elements, like vec3(a,b,c) + */ +export default class JoinNode extends TempNode { + nodes: Node[]; + constructor(nodes: Node[]); +} diff --git a/src-testing/src/nodes/utils/LoopNode.d.ts b/src-testing/src/nodes/utils/LoopNode.d.ts new file mode 100644 index 000000000..2fb1e1e30 --- /dev/null +++ b/src-testing/src/nodes/utils/LoopNode.d.ts @@ -0,0 +1,22 @@ +import Node from "../core/Node.js"; +import NodeBuilder from "../core/NodeBuilder.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +declare class LoopNode extends Node { + params: unknown[]; + + constructor(params?: unknown[]); + + getProperties(builder: NodeBuilder): unknown; +} + +export default LoopNode; + +export const Loop: (...params: unknown[]) => ShaderNodeObject; +export const Continue: () => ShaderNodeObject; +export const Break: () => ShaderNodeObject; + +/** + * @deprecated loop() has been renamed to Loop() + */ +export const loop: (...params: unknown[]) => ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/MatcapUVNode.d.ts b/src-testing/src/nodes/utils/MatcapUVNode.d.ts new file mode 100644 index 000000000..b59cf8ae6 --- /dev/null +++ b/src-testing/src/nodes/utils/MatcapUVNode.d.ts @@ -0,0 +1,8 @@ +import TempNode from "../core/TempNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class MatcapUVNode extends TempNode { + constructor(); +} + +export const matcapUV: ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/MaxMipLevelNode.d.ts b/src-testing/src/nodes/utils/MaxMipLevelNode.d.ts new file mode 100644 index 000000000..858624571 --- /dev/null +++ b/src-testing/src/nodes/utils/MaxMipLevelNode.d.ts @@ -0,0 +1,14 @@ +import { Texture } from "../../textures/Texture.js"; +import TextureNode from "../accessors/TextureNode.js"; +import UniformNode from "../core/UniformNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class MaxMipLevelNode extends UniformNode<0> { + constructor(textureNode: TextureNode); + + get textureNode(): TextureNode; + + get texture(): Texture; +} + +export const maxMipLevel: (texture: Texture) => ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/OscNode.d.ts b/src-testing/src/nodes/utils/OscNode.d.ts new file mode 100644 index 000000000..d99abbcf4 --- /dev/null +++ b/src-testing/src/nodes/utils/OscNode.d.ts @@ -0,0 +1,25 @@ +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export type OscNodeMethod = + | typeof OscNode.SINE + | typeof OscNode.SQUARE + | typeof OscNode.TRIANGLE + | typeof OscNode.SAWTOOTH; + +export default class OscNode extends Node { + static SINE: "sine"; + static SQUARE: "square"; + static TRIANGLE: "triangle"; + static SAWTOOTH: "sawtooth"; + + method: OscNodeMethod; + timeNode: Node; + + constructor(method: OscNodeMethod, timeNode?: Node); +} + +export const oscSine: (timeNode?: NodeRepresentation) => ShaderNodeObject; +export const oscSquare: (timeNode?: NodeRepresentation) => ShaderNodeObject; +export const oscTriangle: (timeNode?: NodeRepresentation) => ShaderNodeObject; +export const oscSawtooth: (timeNode?: NodeRepresentation) => ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/PackingNode.d.ts b/src-testing/src/nodes/utils/PackingNode.d.ts new file mode 100644 index 000000000..c404b6469 --- /dev/null +++ b/src-testing/src/nodes/utils/PackingNode.d.ts @@ -0,0 +1,24 @@ +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export type PackingNodeScope = typeof PackingNode.DIRECTION_TO_COLOR | typeof PackingNode.COLOR_TO_DIRECTION; + +declare class PackingNode extends TempNode { + constructor(scope: PackingNodeScope, node: Node); + + static DIRECTION_TO_COLOR: "directionToColor"; + static COLOR_TO_DIRECTION: "colorToDirection"; +} + +export default PackingNode; + +export const directionToColor: (node: NodeRepresentation) => ShaderNodeObject; +export const colorToDirection: (node: NodeRepresentation) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + directionToColor: typeof directionToColor; + colorToDirection: typeof colorToDirection; + } +} diff --git a/src-testing/src/nodes/utils/RTTNode.d.ts b/src-testing/src/nodes/utils/RTTNode.d.ts new file mode 100644 index 000000000..34925d7c4 --- /dev/null +++ b/src-testing/src/nodes/utils/RTTNode.d.ts @@ -0,0 +1,45 @@ +import { TextureDataType } from "../../constants.js"; +import { RenderTarget } from "../../core/RenderTarget.js"; +import TextureNode from "../accessors/TextureNode.js"; +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export interface RTTNodeOptions { + type: TextureDataType; +} + +declare class RTTNode extends TextureNode { + node: Node; + width: number | null; + height: number | null; + + renderTarget: RenderTarget | null; + + textureNeedsUpdate: boolean; + autoUpdate: boolean; + + pixelRatio?: number; + + constructor(node: Node, width?: number | null, height?: number | null, options?: RTTNodeOptions); + + get autoSize(): boolean; + + setSize(width: number | null, height: number | null): void; + + setPixelRatio(pixelRatio: number): void; +} + +export default RTTNode; + +export const rtt: ( + node: NodeRepresentation, + width?: number | null, + height?: number | null, + options?: RTTNodeOptions, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + toTexture: typeof rtt; + } +} diff --git a/src-testing/src/nodes/utils/ReflectorNode.d.ts b/src-testing/src/nodes/utils/ReflectorNode.d.ts new file mode 100644 index 000000000..ac6d1e726 --- /dev/null +++ b/src-testing/src/nodes/utils/ReflectorNode.d.ts @@ -0,0 +1,32 @@ +import { Camera } from "../../cameras/Camera.js"; +import { Object3D } from "../../core/Object3D.js"; +import { RenderTarget } from "../../core/RenderTarget.js"; +import TextureNode from "../accessors/TextureNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export interface ReflectorNodeParameters { + target?: Object3D | undefined; + resolution?: number | undefined; + generateMipmaps?: boolean | undefined; + bounces?: boolean | undefined; +} + +export default class ReflectorNode extends TextureNode { + target: Object3D; + resolution: number; + generateMipmaps: boolean; + bounces: boolean; + + virtualCameras: WeakMap; + renderTargets: WeakMap; + + constructor(parameters?: ReflectorNodeParameters); + + getTextureNode(): TextureNode; + + getVirtualCamera(camera: Camera): Camera; + + getRenderTarget(camera: Camera): RenderTarget; +} + +export const reflector: (parameters?: ReflectorNodeParameters) => ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/RemapNode.d.ts b/src-testing/src/nodes/utils/RemapNode.d.ts new file mode 100644 index 000000000..5039cd6fc --- /dev/null +++ b/src-testing/src/nodes/utils/RemapNode.d.ts @@ -0,0 +1,36 @@ +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class RemapNode extends Node { + node: Node; + inLowNode: Node; + inHighNode: Node; + outLowNode: Node; + outHighNode: Node; + + doClamp: boolean; + + constructor(node: Node, inLowNode: Node, inHighNode: Node, outLowNode?: Node, outHighNode?: Node); +} + +export const remap: ( + node: Node, + inLowNode: NodeRepresentation, + inHighNode: NodeRepresentation, + outLowNode?: NodeRepresentation, + outHighNode?: NodeRepresentation, +) => ShaderNodeObject; +export const remapClamp: ( + node: Node, + inLowNode: NodeRepresentation, + inHighNode: NodeRepresentation, + outLowNode?: NodeRepresentation, + outHighNode?: NodeRepresentation, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + remap: typeof remap; + remapClamp: typeof remapClamp; + } +} diff --git a/src-testing/src/nodes/utils/RotateNode.d.ts b/src-testing/src/nodes/utils/RotateNode.d.ts new file mode 100644 index 000000000..34ba61907 --- /dev/null +++ b/src-testing/src/nodes/utils/RotateNode.d.ts @@ -0,0 +1,21 @@ +import Node from "../core/Node.js"; +import TempNode from "../core/TempNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class RotateNode extends TempNode { + positionNode: Node; + rotationNode: Node; + + constructor(positionNode: Node, rotationNode: Node); +} + +export const rotate: ( + positionNode: NodeRepresentation, + rotationNode: NodeRepresentation, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + rotate: typeof rotate; + } +} diff --git a/src-testing/src/nodes/utils/SplitNode.d.ts b/src-testing/src/nodes/utils/SplitNode.d.ts new file mode 100644 index 000000000..f3966fb02 --- /dev/null +++ b/src-testing/src/nodes/utils/SplitNode.d.ts @@ -0,0 +1,15 @@ +import Node from "../core/Node.js"; +import { SwizzleOption } from "../shadernode/ShaderNode.js"; + +/** swizzle node */ +export default class SplitNode extends Node { + node: Node; + components: string; + + /** + * @param node the input node + * @param components swizzle like string, default = "x" + */ + constructor(node: Node, components?: SwizzleOption); + getVectorLength(): number; +} diff --git a/src-testing/src/nodes/utils/SpriteSheetUVNode.d.ts b/src-testing/src/nodes/utils/SpriteSheetUVNode.d.ts new file mode 100644 index 000000000..111d7611f --- /dev/null +++ b/src-testing/src/nodes/utils/SpriteSheetUVNode.d.ts @@ -0,0 +1,16 @@ +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class SpriteSheetUVNode extends Node { + countNode: Node; + uvNode: Node; + frameNode: Node; + + constructor(countNode: Node, uvNode?: Node, frameNode?: Node); +} + +export const spritesheetUV: ( + countNode: NodeRepresentation, + uvNode?: NodeRepresentation, + frameNode?: NodeRepresentation, +) => ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/SpriteUtils.d.ts b/src-testing/src/nodes/utils/SpriteUtils.d.ts new file mode 100644 index 000000000..3952577c4 --- /dev/null +++ b/src-testing/src/nodes/utils/SpriteUtils.d.ts @@ -0,0 +1,6 @@ +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const billboarding: ( + args?: { position?: NodeRepresentation | null; horizontal?: boolean; vertical?: boolean }, +) => ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/StoargeArrayElementNode.d.ts b/src-testing/src/nodes/utils/StoargeArrayElementNode.d.ts new file mode 100644 index 000000000..0d74282e5 --- /dev/null +++ b/src-testing/src/nodes/utils/StoargeArrayElementNode.d.ts @@ -0,0 +1,25 @@ +import StorageBufferNode from "../accessors/StorageBufferNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; +import ArrayElementNode from "./ArrayElementNode.js"; + +export default class StorageArrayElementNode extends ArrayElementNode { + node: StorageBufferNode; + + readonly isStorageArrayElementNode: true; + + constructor(storageBufferNode: StorageBufferNode, indexNode: Node); + + get storageBufferNode(): StorageBufferNode; + set storageBufferNode(value: StorageBufferNode); +} + +export const storageElement: ( + storageBufferNode: NodeRepresentation, + indexNode: NodeRepresentation, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + storageElement: typeof storageElement; + } +} diff --git a/src-testing/src/nodes/utils/TimerNode.d.ts b/src-testing/src/nodes/utils/TimerNode.d.ts new file mode 100644 index 000000000..02cac2909 --- /dev/null +++ b/src-testing/src/nodes/utils/TimerNode.d.ts @@ -0,0 +1,25 @@ +import UniformNode from "../core/UniformNode.js"; +import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export type TimerNodeScope = + | typeof TimerNode.LOCAL + | typeof TimerNode.GLOBAL + | typeof TimerNode.DELTA + | typeof TimerNode.FRAME; + +export default class TimerNode extends UniformNode { + static LOCAL: "local"; + static GLOBAL: "global"; + static DELTA: "delta"; + static FRAME: "frame"; + + scope: TimerNodeScope; + scale: number; + + constructor(scope?: TimerNodeScope, scale?: number, value?: number); +} + +export const timerLocal: (timeScale?: number, value?: number) => ShaderNodeObject; +export const timerGlobal: (timeScale?: number, value?: number) => ShaderNodeObject; +export const timerDelta: (timeScale?: number, value?: number) => ShaderNodeObject; +export const frameId: ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/TriplanarTexturesNode.d.ts b/src-testing/src/nodes/utils/TriplanarTexturesNode.d.ts new file mode 100644 index 000000000..55af273e3 --- /dev/null +++ b/src-testing/src/nodes/utils/TriplanarTexturesNode.d.ts @@ -0,0 +1,42 @@ +import TextureNode from "../accessors/TextureNode.js"; +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export default class TriplanarTexturesNode extends Node { + textureXNode: TextureNode; + textureYNode: TextureNode | null; + textureZNode: TextureNode | null; + + scaleNode: ShaderNodeObject; + + positionNode: ShaderNodeObject; + normalNode: ShaderNodeObject; + + constructor( + textureXNode: Node, + textureYNode?: TextureNode | null, + textureZNode?: TextureNode | null, + scaleNode?: ShaderNodeObject, + positionNode?: ShaderNodeObject, + normalNode?: ShaderNodeObject, + ); +} + +export const triplanarTextures: ( + textureXNode: NodeRepresentation, + textureYNode?: NodeRepresentation, + textureZNode?: NodeRepresentation, + scaleNode?: NodeRepresentation, + positionNode?: NodeRepresentation, + normalNode?: NodeRepresentation, +) => ShaderNodeObject; +export const triplanarTexture: ( + texture: NodeRepresentation, + ...params: NodeRepresentation[] +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + triplanarTexture: typeof triplanarTexture; + } +} diff --git a/src-testing/src/nodes/utils/UVUtils.d.ts b/src-testing/src/nodes/utils/UVUtils.d.ts new file mode 100644 index 000000000..6e5b51f96 --- /dev/null +++ b/src-testing/src/nodes/utils/UVUtils.d.ts @@ -0,0 +1,21 @@ +import OperatorNode from "../math/OperatorNode.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const rotateUV: ( + uv: NodeRepresentation, + rotation: NodeRepresentation, + center?: NodeRepresentation, +) => ShaderNodeObject; + +export const spherizeUV: ( + uv: NodeRepresentation, + strength: NodeRepresentation, + center?: NodeRepresentation, +) => ShaderNodeObject; + +declare module "../shadernode/ShaderNode.js" { + interface NodeElements { + rotateUV: typeof rotateUV; + spherizeUV: typeof spherizeUV; + } +} diff --git a/src-testing/src/nodes/utils/ViewportUtils.d.ts b/src-testing/src/nodes/utils/ViewportUtils.d.ts new file mode 100644 index 000000000..3e971255a --- /dev/null +++ b/src-testing/src/nodes/utils/ViewportUtils.d.ts @@ -0,0 +1,4 @@ +import Node from "../core/Node.js"; +import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; + +export const viewportSafeUV: (uv?: NodeRepresentation | null) => ShaderNodeObject; diff --git a/src-testing/src/objects/BatchedMesh.d.ts b/src-testing/src/objects/BatchedMesh.d.ts new file mode 100644 index 000000000..2669b5c08 --- /dev/null +++ b/src-testing/src/objects/BatchedMesh.d.ts @@ -0,0 +1,197 @@ +import { Camera } from "../cameras/Camera.js"; +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { Material } from "../materials/Material.js"; +import { Box3 } from "../math/Box3.js"; +import { Color } from "../math/Color.js"; +import { Matrix4 } from "../math/Matrix4.js"; +import { Sphere } from "../math/Sphere.js"; +import { Mesh } from "./Mesh.js"; + +/** + * A special version of {@link Mesh} with multi draw batch rendering support. Use {@link BatchedMesh} if you have to + * render a large number of objects with the same material but with different world transformations. The usage of + * {@link BatchedMesh} will help you to reduce the number of draw calls and thus improve the overall rendering + * performance in your application. + * + * If the {@link https://developer.mozilla.org/en-US/docs/Web/API/WEBGL_multi_draw WEBGL_multi_draw extension} is not + * supported then a less performant fallback is used. + * + * @example + * const box = new THREE.BoxGeometry( 1, 1, 1 ); + * const sphere = new THREE.SphereGeometry( 1, 12, 12 ); + * const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } ); + * + * // initialize and add geometries into the batched mesh + * const batchedMesh = new BatchedMesh( 10, 5000, 10000, material ); + * const boxGeometryId = batchedMesh.addGeometry( box ); + * const sphereGeometryId = batchedMesh.addGeometry( sphere ); + * + * // create instances of those geometries + * const boxInstancedId1 = batchedMesh.addInstance( boxGeometryId ); + * const boxInstancedId2 = batchedMesh.addInstance( boxGeometryId ); + * + * const sphereInstancedId1 = batchedMesh.addInstance( sphereGeometryId ); + * const sphereInstancedId2 = batchedMesh.addInstance( sphereGeometryId ); + * + * // position the geometries + * batchedMesh.setMatrixAt( boxInstancedId1, boxMatrix1 ); + * batchedMesh.setMatrixAt( boxInstancedId2, boxMatrix2 ); + * + * batchedMesh.setMatrixAt( sphereInstancedId1, sphereMatrix1 ); + * batchedMesh.setMatrixAt( sphereInstancedId2, sphereMatrix2 ); + * + * scene.add( batchedMesh ); + * + * @also Example: {@link https://threejs.org/examples/#webgl_mesh_batch WebGL / mesh / batch} + */ +declare class BatchedMesh extends Mesh { + /** + * This bounding box encloses all instances of the {@link BatchedMesh}. Can be calculated with + * {@link .computeBoundingBox()}. + * @default null + */ + boundingBox: Box3 | null; + + /** + * This bounding sphere encloses all instances of the {@link BatchedMesh}. Can be calculated with + * {@link .computeBoundingSphere()}. + * @default null + */ + boundingSphere: Sphere | null; + + customSort: ((this: this, list: Array<{ start: number; count: number; z: number }>, camera: Camera) => void) | null; + + /** + * If true then the individual objects within the {@link BatchedMesh} are frustum culled. + * @default true + */ + perObjectFrustumCulled: boolean; + + /** + * If true then the individual objects within the {@link BatchedMesh} are sorted to improve overdraw-related + * artifacts. If the material is marked as "transparent" objects are rendered back to front and if not then they are + * rendered front to back. + * @default true + */ + sortObjects: boolean; + + /** + * The maximum number of individual geometries that can be stored in the {@link BatchedMesh}. Read only. + */ + get maxInstanceCount(): number; + + /** + * Read-only flag to check if a given object is of type {@link BatchedMesh}. + */ + readonly isBatchedMesh: true; + + /** + * @param maxInstanceCount the max number of individual geometries planned to be added. + * @param maxVertexCount the max number of vertices to be used by all geometries. + * @param maxIndexCount the max number of indices to be used by all geometries. + * @param material an instance of {@link Material}. Default is a new {@link MeshBasicMaterial}. + */ + constructor(maxInstanceCount: number, maxVertexCount: number, maxIndexCount?: number, material?: Material); + + /** + * Computes the bounding box, updating {@link .boundingBox} attribute. + * Bounding boxes aren't computed by default. They need to be explicitly computed, otherwise they are `null`. + */ + computeBoundingBox(): void; + + /** + * Computes the bounding sphere, updating {@link .boundingSphere} attribute. + * Bounding spheres aren't computed by default. They need to be explicitly computed, otherwise they are `null`. + */ + computeBoundingSphere(): void; + + /** + * Frees the GPU-related resources allocated by this instance. Call this method whenever this instance is no longer + * used in your app. + */ + dispose(): this; + + /** + * Takes a sort a function that is run before render. The function takes a list of instances to sort and a camera. + * The objects in the list include a "z" field to perform a depth-ordered sort with. + */ + setCustomSort( + sortFunction: + | ((this: this, list: Array<{ start: number; count: number; z: number }>, camera: Camera) => void) + | null, + ): this; + + /** + * Get the color of the defined geometry. + * @param instanceId The id of an instance to get the color of. + * @param target The target object to copy the color in to. + */ + getColorAt(instanceId: number, target: Color): void; + + /** + * Get the local transformation matrix of the defined instance. + * @param instanceId The id of an instance to get the matrix of. + * @param target This 4x4 matrix will be set to the local transformation matrix of the defined instance. + */ + getMatrixAt(instanceId: number, target: Matrix4): Matrix4; + + /** + * Get whether the given instance is marked as "visible" or not. + * @param instanceId The id of an instance to get the visibility state of. + */ + getVisibleAt(instanceId: number): boolean; + + /** + * Sets the given color to the defined geometry instance. + * @param instanceId The id of the instance to set the color of. + * @param color The color to set the instance to. + */ + setColorAt(instanceId: number, color: Color): void; + + /** + * Sets the given local transformation matrix to the defined instance. + * @param instanceId The id of an instance to set the matrix of. + * @param matrix A 4x4 matrix representing the local transformation of a single instance. + */ + setMatrixAt(instanceId: number, matrix: Matrix4): this; + + /** + * Sets the visibility of the instance at the given index. + * @param instanceId The id of the instance to set the visibility of. + * @param visible A boolean value indicating the visibility state. + */ + setVisibleAt(instanceId: number, visible: boolean): this; + + /** + * Adds the given geometry to the {@link BatchedMesh} and returns the associated index referring to it. + * @param geometry The geometry to add into the {@link BatchedMesh}. + * @param reservedVertexRange Optional parameter specifying the amount of vertex buffer space to reserve for the + * added geometry. This is necessary if it is planned to set a new geometry at this index at a later time that is + * larger than the original geometry. Defaults to the length of the given geometry vertex buffer. + * @param reservedIndexRange Optional parameter specifying the amount of index buffer space to reserve for the added + * geometry. This is necessary if it is planned to set a new geometry at this index at a later time that is larger + * than the original geometry. Defaults to the length of the given geometry index buffer. + */ + addGeometry(geometry: BufferGeometry, reservedVertexRange?: number, reservedIndexRange?: number): number; + + /** + * Adds a new instance to the {@link BatchedMesh} using the geometry of the given geometryId and returns a new id + * referring to the new instance to be used by other functions. + * @param geometryId The id of a previously added geometry via "addGeometry" to add into the {@link BatchedMesh} to + * render. + */ + addInstance(geometryId: number): number; + + /** + * Replaces the geometry at `geometryId` with the provided geometry. Throws an error if there is not enough space + * reserved for geometry. Calling this will change all instances that are rendering that geometry. + * @param geometryId Which geometry id to replace with this geometry. + * @param geometry The geometry to substitute at the given geometry id. + */ + setGeometryAt(geometryId: number, geometry: BufferGeometry): number; + + getBoundingBoxAt(geometryId: number, target: Box3): Box3 | null; + getBoundingSphereAt(geometryId: number, target: Sphere): Sphere | null; +} + +export { BatchedMesh }; diff --git a/src-testing/src/objects/Bone.d.ts b/src-testing/src/objects/Bone.d.ts new file mode 100644 index 000000000..3400ea1b6 --- /dev/null +++ b/src-testing/src/objects/Bone.d.ts @@ -0,0 +1,36 @@ +import { Object3D, Object3DEventMap } from "../core/Object3D.js"; + +/** + * A {@link Bone} which is part of a {@link THREE.Skeleton | Skeleton} + * @remarks + * The skeleton in turn is used by the {@link THREE.SkinnedMesh | SkinnedMesh} + * Bones are almost identical to a blank {@link THREE.Object3D | Object3D}. + * @example + * ```typescript + * const root = new THREE.Bone(); + * const child = new THREE.Bone(); + * root.add(child); + * child.position.y = 5; + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/objects/Bone | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Bone.js | Source} + */ +export class Bone extends Object3D { + /** + * Creates a new {@link Bone}. + */ + constructor(); + + /** + * Read-only flag to check if a given object is of type {@link Bone}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isBone: true; + + /** + * @override + * @defaultValue `Bone` + */ + override readonly type: string | "Bone"; +} diff --git a/src-testing/src/objects/Group.d.ts b/src-testing/src/objects/Group.d.ts new file mode 100644 index 000000000..bceb11e5f --- /dev/null +++ b/src-testing/src/objects/Group.d.ts @@ -0,0 +1,43 @@ +import { Object3D, Object3DEventMap } from "../core/Object3D.js"; + +/** + * Its purpose is to make working with groups of objects syntactically clearer. + * @remarks This is almost identical to an {@link Object3D | Object3D} + * @example + * ```typescript + * const geometry = new THREE.BoxGeometry(1, 1, 1); + * const material = new THREE.MeshBasicMaterial({ + * color: 0x00ff00 + * }); + * const cubeA = new THREE.Mesh(geometry, material); + * cubeA.position.set(100, 100, 0); + * const cubeB = new THREE.Mesh(geometry, material); + * cubeB.position.set(-100, -100, 0); + * //create a {@link Group} and add the two cubes + * //These cubes can now be rotated / scaled etc as a {@link Group} * const {@link Group} = new THREE.Group(); + * group.add(cubeA); + * group.add(cubeB); + * scene.add(group); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/objects/Group | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Group.js | Source} + */ +export class Group extends Object3D { + /** + * Creates a new {@link Group}. + */ + constructor(); + + /** + * Read-only flag to check if a given object is of type {@link Group}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isGroup: true; + + /** + * @override + * @defaultValue `Group` + */ + override readonly type: string | "Group"; +} diff --git a/src-testing/src/objects/InstancedMesh.d.ts b/src-testing/src/objects/InstancedMesh.d.ts new file mode 100644 index 000000000..b239afb7a --- /dev/null +++ b/src-testing/src/objects/InstancedMesh.d.ts @@ -0,0 +1,179 @@ +import { BufferAttributeJSON } from "./../core/BufferAttribute.js"; +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { InstancedBufferAttribute } from "../core/InstancedBufferAttribute.js"; +import { JSONMeta, Object3DEventMap } from "../core/Object3D.js"; +import { Material } from "../materials/Material.js"; +import { Box3 } from "../math/Box3.js"; +import { Color } from "../math/Color.js"; +import { Matrix4 } from "../math/Matrix4.js"; +import { Sphere } from "../math/Sphere.js"; +import { DataTexture } from "../textures/DataTexture.js"; +import { Mesh, MeshJSONObject } from "./Mesh.js"; + +export interface InstancedMeshJSONObject extends MeshJSONObject { + count: number; + instanceMatrix: BufferAttributeJSON; + instanceColor?: BufferAttributeJSON; +} + +export interface InstancedMeshJSON extends MeshJSONObject { + object: InstancedMeshJSONObject; +} + +export interface InstancedMeshEventMap extends Object3DEventMap { + dispose: {}; +} + +/** + * A special version of {@link THREE.Mesh | Mesh} with instanced rendering support + * @remarks + * Use {@link InstancedMesh} if you have to render a large number of objects with the same geometry and material(s) but with different world transformations + * @remarks + * The usage of {@link InstancedMesh} will help you to reduce the number of draw calls and thus improve the overall rendering performance in your application. + * @see Example: {@link https://threejs.org/examples/#webgl_instancing_dynamic | WebGL / instancing / dynamic} + * @see Example: {@link https://threejs.org/examples/#webgl_instancing_performance | WebGL / instancing / performance} + * @see Example: {@link https://threejs.org/examples/#webgl_instancing_scatter | WebGL / instancing / scatter} + * @see Example: {@link https://threejs.org/examples/#webgl_instancing_raycast | WebGL / instancing / raycast} + * @see {@link https://threejs.org/docs/index.html#api/en/objects/InstancedMesh | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/InstancedMesh.js | Source} + */ +export class InstancedMesh< + TGeometry extends BufferGeometry = BufferGeometry, + TMaterial extends Material | Material[] = Material | Material[], + TEventMap extends InstancedMeshEventMap = InstancedMeshEventMap, +> extends Mesh { + /** + * Create a new instance of {@link InstancedMesh} + * @param geometry An instance of {@link BufferGeometry}. + * @param material A single or an array of {@link Material}. Default is a new {@link MeshBasicMaterial}. + * @param count The **maximum** number of instances of this Mesh. Expects a `Integer` + */ + constructor(geometry: TGeometry | undefined, material: TMaterial | undefined, count: number); + + /** + * Read-only flag to check if a given object is of type {@link InstancedMesh}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isInstancedMesh: true; + + /** + * This bounding box encloses all instances of the {@link InstancedMesh},, which can be calculated with {@link computeBoundingBox | .computeBoundingBox()}. + * @remarks Bounding boxes aren't computed by default. They need to be explicitly computed, otherwise they are `null`. + * @defaultValue `null` + */ + boundingBox: Box3 | null; + + /** + * This bounding sphere encloses all instances of the {@link InstancedMesh}, which can be calculated with {@link computeBoundingSphere | .computeBoundingSphere()}. + * @remarks bounding spheres aren't computed by default. They need to be explicitly computed, otherwise they are `null`. + * @defaultValue `null` + */ + boundingSphere: Sphere | null; + + /** + * The number of instances. + * @remarks + * The `count` value passed into the {@link InstancedMesh | constructor} represents the **maximum** number of instances of this mesh. + * You can change the number of instances at runtime to an integer value in the range `[0, count]`. + * @remarks If you need more instances than the original `count` value, you have to create a new InstancedMesh. + * @remarks Expects a `Integer` + */ + count: number; + + /** + * Represents the colors of all instances. + * You have to set {@link InstancedBufferAttribute.needsUpdate | .instanceColor.needsUpdate()} flag to `true` if you modify instanced data via {@link setColorAt | .setColorAt()}. + * @defaultValue `null` + */ + instanceColor: InstancedBufferAttribute | null; + + /** + * Represents the local transformation of all instances. + * You have to set {@link InstancedBufferAttribute.needsUpdate | .instanceMatrix.needsUpdate()} flag to `true` if you modify instanced data via {@link setMatrixAt | .setMatrixAt()}. + */ + instanceMatrix: InstancedBufferAttribute; + + /** + * Represents the morph target weights of all instances. You have to set its {@link .needsUpdate} flag to true if + * you modify instanced data via {@link .setMorphAt}. + */ + morphTexture: DataTexture | null; + + /** + * Computes the bounding box of the instanced mesh, and updates the {@link .boundingBox} attribute. The bounding box + * is not computed by the engine; it must be computed by your app. You may need to recompute the bounding box if an + * instance is transformed via {@link .setMatrixAt()}. + */ + computeBoundingBox(): void; + + /** + * Computes the bounding sphere of the instanced mesh, and updates the {@link .boundingSphere} attribute. The engine + * automatically computes the bounding sphere when it is needed, e.g., for ray casting or view frustum culling. You + * may need to recompute the bounding sphere if an instance is transformed via [page:.setMatrixAt](). + */ + computeBoundingSphere(): void; + + /** + * Get the color of the defined instance. + * @param index The index of an instance. Values have to be in the range `[0, count]`. Expects a `Integer` + * @param color This color object will be set to the color of the defined instance. + */ + getColorAt(index: number, color: Color): void; + + /** + * Sets the given color to the defined instance + * @remarks + * Make sure you set {@link InstancedBufferAttribute.needsUpdate | .instanceColor.needsUpdate()} to `true` after updating all the colors. + * @param index The index of an instance. Values have to be in the range `[0, count]`. Expects a `Integer` + * @param color The color of a single instance. + */ + setColorAt(index: number, color: Color): void; + + /** + * Get the local transformation matrix of the defined instance. + * @param index The index of an instance Values have to be in the range `[0, count]`. Expects a `Integer` + * @param matrix This 4x4 matrix will be set to the local transformation matrix of the defined instance. + */ + getMatrixAt(index: number, matrix: Matrix4): void; + + /** + * Get the morph target weights of the defined instance. + * @param index The index of an instance. Values have to be in the range [0, count]. + * @param mesh The {@link .morphTargetInfluences} property of this mesh will be filled with the morph target weights of the defined instance. + */ + getMorphAt(index: number, mesh: Mesh): void; + + /** + * Sets the given local transformation matrix to the defined instance. + * @remarks + * Make sure you set {@link InstancedBufferAttribute.needsUpdate | .instanceMatrix.needsUpdate()} flag to `true` after updating all the matrices. + * @param index The index of an instance. Values have to be in the range `[0, count]`. Expects a `Integer` + * @param matrix A 4x4 matrix representing the local transformation of a single instance. + */ + setMatrixAt(index: number, matrix: Matrix4): void; + + /** + * Sets the morph target weights to the defined instance. Make sure you set {@link .morphTexture}{@link .needsUpdate} + * to true after updating all the influences. + * @param index The index of an instance. Values have to be in the range [0, count]. + * @param mesh A mesh with {@link .morphTargetInfluences} property containing the morph target weights of a single instance. + */ + setMorphAt(index: number, mesh: Mesh): void; + + /** + * No effect in {@link InstancedMesh}. + * @ignore + * @hidden + */ + override updateMorphTargets(): void; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): this; + + toJSON(meta?: JSONMeta): InstancedMeshJSON; +} diff --git a/src-testing/src/objects/LOD.d.ts b/src-testing/src/objects/LOD.d.ts new file mode 100644 index 000000000..ec6657c7f --- /dev/null +++ b/src-testing/src/objects/LOD.d.ts @@ -0,0 +1,104 @@ +import { Camera } from "../cameras/Camera.js"; +import { JSONMeta, Object3D, Object3DEventMap, Object3DJSON, Object3DJSONObject } from "../core/Object3D.js"; + +export interface LODJSONObject extends Object3DJSONObject { + autoUpdate?: boolean; + + levels: Array<{ + object: string; + distance: number; + hysteresis: number; + }>; +} + +export interface LODJSON extends Object3DJSON { + object: LODJSONObject; +} + +/** + * Every level is associated with an object, and rendering can be switched between them at the distances specified + * @remarks + * Typically you would create, say, three meshes, one for far away (low detail), one for mid range (medium detail) and one for close up (high detail). + * @example + * ```typescript + * const {@link LOD} = new THREE.LOD(); + * //Create spheres with 3 levels of detail and create new {@link LOD} levels for them + * for (let i = 0; i & lt; 3; i++) { + * const geometry = new THREE.IcosahedronGeometry(10, 3 - i) + * const mesh = new THREE.Mesh(geometry, material); + * lod.addLevel(mesh, i * 75); + * } + * scene.add(lod); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_lod | webgl / {@link LOD} } + * @see {@link https://threejs.org/docs/index.html#api/en/objects/LOD | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/LOD.js | Source} + */ +export class LOD extends Object3D { + /** + * Creates a new {@link LOD}. + */ + constructor(); + + /** + * Read-only flag to check if a given object is of type {@link LOD}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isLOD: true; + + /** + * @override + * @defaultValue `LOD` + */ + override readonly type: string | "LOD"; + + /** + * An array of level objects + */ + levels: Array<{ + /** The Object3D to display at this level. */ + object: Object3D; + /** The distance at which to display this level of detail. Expects a `Float`. */ + distance: number; + /** Threshold used to avoid flickering at LOD boundaries, as a fraction of distance. Expects a `Float`. */ + hysteresis: number; + }>; + + /** + * Whether the {@link LOD} object is updated automatically by the renderer per frame or not. + * If set to `false`, you have to call {@link update | .update()} in the render loop by yourself. + * @defaultValue `true` + */ + autoUpdate: boolean; + + /** + * Adds a mesh that will display at a certain distance and greater. Typically the further away the distance, the lower the detail on the mesh. + * + * @param object The Object3D to display at this level. + * @param distance The distance at which to display this level of detail. Expects a `Float`. Default `0.0`. + * @param hysteresis Threshold used to avoid flickering at LOD boundaries, as a fraction of distance. Expects a `Float`. Default `0.0`. + */ + addLevel(object: Object3D, distance?: number, hysteresis?: number): this; + + /** + * Get the currently active {@link LOD} level + * @remarks + * As index of the levels array. + */ + getCurrentLevel(): number; + + /** + * Get a reference to the first {@link THREE.Object3D | Object3D} (mesh) that is greater than {@link distance}. + * @param distance Expects a `Float` + */ + getObjectForDistance(distance: number): Object3D | null; + + /** + * Set the visibility of each {@link levels | level}'s {@link THREE.Object3D | object} based on distance from the {@link THREE.Camera | camera}. + * @param camera + */ + update(camera: Camera): void; + + toJSON(meta?: JSONMeta): LODJSON; +} diff --git a/src-testing/src/objects/Line.d.ts b/src-testing/src/objects/Line.d.ts new file mode 100644 index 000000000..2d76dec69 --- /dev/null +++ b/src-testing/src/objects/Line.d.ts @@ -0,0 +1,87 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { Object3D, Object3DEventMap } from "../core/Object3D.js"; +import { Material } from "../materials/Material.js"; + +/** + * A continuous line. + * @remarks + * This is nearly the same as {@link THREE.LineSegments | LineSegments}, + * the only difference is that it is rendered using {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements | gl.LINE_STRIP} + * instead of {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements | gl.LINES} + * @example + * ```typescript + * const material = new THREE.LineBasicMaterial({ + * color: 0x0000ff + * }); + * const points = []; + * points.push(new THREE.Vector3(-10, 0, 0)); + * points.push(new THREE.Vector3(0, 10, 0)); + * points.push(new THREE.Vector3(10, 0, 0)); + * const geometry = new THREE.BufferGeometry().setFromPoints(points); + * const {@link Line} = new THREE.Line(geometry, material); + * scene.add(line); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/objects/Line | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Line.js | Source} + */ +export class Line< + TGeometry extends BufferGeometry = BufferGeometry, + TMaterial extends Material | Material[] = Material | Material[], + TEventMap extends Object3DEventMap = Object3DEventMap, +> extends Object3D { + /** + * Create a new instance of {@link Line} + * @param geometry Vertices representing the {@link Line} segment(s). Default {@link THREE.BufferGeometry | `new THREE.BufferGeometry()`}. + * @param material Material for the line. Default {@link THREE.LineBasicMaterial | `new THREE.LineBasicMaterial()`}. + */ + constructor(geometry?: TGeometry, material?: TMaterial); + + /** + * Read-only flag to check if a given object is of type {@link Line}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isLine: true; + + /** + * @override + * @defaultValue `Line` + */ + override readonly type: string | "Line"; + + /** + * Vertices representing the {@link Line} segment(s). + */ + geometry: TGeometry; + + /** + * Material for the line. + */ + material: TMaterial; + + /** + * An array of weights typically from `0-1` that specify how much of the morph is applied. + * @defaultValue `undefined`, but reset to a blank array by {@link updateMorphTargets | .updateMorphTargets()}. + */ + morphTargetInfluences?: number[] | undefined; + + /** + * A dictionary of morphTargets based on the `morphTarget.name` property. + * @defaultValue `undefined`, but reset to a blank array by {@link updateMorphTargets | .updateMorphTargets()}. + */ + morphTargetDictionary?: { [key: string]: number } | undefined; + + /** + * Computes an array of distance values which are necessary for {@link THREE.LineDashedMaterial | LineDashedMaterial} + * @remarks + * For each vertex in the geometry, the method calculates the cumulative length from the current point to the very beginning of the line. + */ + computeLineDistances(): this; + + /** + * Updates the morphTargets to have no influence on the object + * @remarks + * Resets the {@link morphTargetInfluences | .morphTargetInfluences} and {@link morphTargetDictionary | .morphTargetDictionary} properties. + */ + updateMorphTargets(): void; +} diff --git a/src-testing/src/objects/LineLoop.d.ts b/src-testing/src/objects/LineLoop.d.ts new file mode 100644 index 000000000..0a070a838 --- /dev/null +++ b/src-testing/src/objects/LineLoop.d.ts @@ -0,0 +1,40 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { Object3DEventMap } from "../core/Object3D.js"; +import { Material } from "../materials/Material.js"; +import { Line } from "./Line.js"; + +/** + * A continuous line that connects back to the start. + * @remarks + * This is nearly the same as {@link THREE.Line | Line}, + * the only difference is that it is rendered using {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements | gl.LINE_LOOP} + * instead of {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements | gl.LINE_STRIP}, + * which draws a straight line to the next vertex, and connects the last vertex back to the first. + * @see {@link https://threejs.org/docs/index.html#api/en/objects/LineLoop | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/LineLoop.js | Source} + */ +export class LineLoop< + TGeometry extends BufferGeometry = BufferGeometry, + TMaterial extends Material | Material[] = Material | Material[], + TEventMap extends Object3DEventMap = Object3DEventMap, +> extends Line { + /** + * Create a new instance of {@link LineLoop} + * @param geometry List of vertices representing points on the line loop. Default {@link THREE.BufferGeometry | `new THREE.BufferGeometry()`}. + * @param material Material for the line. Default {@link THREE.LineBasicMaterial | `new THREE.LineBasicMaterial()`}. + */ + constructor(geometry?: TGeometry, material?: TMaterial); + + /** + * Read-only flag to check if a given object is of type {@link LineLoop}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isLineLoop: true; + + /** + * @override + * @defaultValue `LineLoop` + */ + override readonly type: string | "LineLoop"; +} diff --git a/src-testing/src/objects/LineSegments.d.ts b/src-testing/src/objects/LineSegments.d.ts new file mode 100644 index 000000000..9a8199bdc --- /dev/null +++ b/src-testing/src/objects/LineSegments.d.ts @@ -0,0 +1,41 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { Object3DEventMap } from "../core/Object3D.js"; +import { Material } from "../materials/Material.js"; +import { Line } from "./Line.js"; + +/** + * A series of lines drawn between pairs of vertices. + * @remarks + * This is nearly the same as {@link THREE.Line | Line}, + * the only difference is that it is rendered using {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements | gl.LINES} + * instead of {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements | gl.LINE_STRIP}. + * @see {@link https://threejs.org/docs/index.html#api/en/objects/LineSegments | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/LineSegments.js | Source} + */ +export class LineSegments< + TGeometry extends BufferGeometry = BufferGeometry, + TMaterial extends Material | Material[] = Material | Material[], + TEventMap extends Object3DEventMap = Object3DEventMap, +> extends Line { + /** + * Create a new instance of {@link LineSegments} + * @param geometry Pair(s) of vertices representing each line segment(s). Default {@link THREE.BufferGeometry | `new THREE.BufferGeometry()`}. + * @param material Material for the line. Default {@link THREE.LineBasicMaterial | `new THREE.LineBasicMaterial()`}. + */ + constructor(geometry?: TGeometry, material?: TMaterial); + + /** + * Read-only flag to check if a given object is of type {@link LineSegments}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isLineSegments: true; + + /** + * A Read-only _string_ to check if `this` object type. + * @remarks Sub-classes will update this value. + * @override + * @defaultValue `LineSegments` + */ + override readonly type: string | "LineSegments"; +} diff --git a/src-testing/src/objects/Mesh.d.ts b/src-testing/src/objects/Mesh.d.ts new file mode 100644 index 000000000..38dad1c73 --- /dev/null +++ b/src-testing/src/objects/Mesh.d.ts @@ -0,0 +1,94 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { JSONMeta, Object3D, Object3DEventMap, Object3DJSON, Object3DJSONObject } from "../core/Object3D.js"; +import { Material } from "../materials/Material.js"; +import { Vector3 } from "../math/Vector3.js"; + +export interface MeshJSONObject extends Object3DJSONObject { + geometry: string; +} + +export interface MeshJSON extends Object3DJSON { + object: MeshJSONObject; +} + +/** + * Class representing triangular {@link https://en.wikipedia.org/wiki/Polygon_mesh | polygon mesh} based objects. + * @remarks + * Also serves as a base for other classes such as {@link THREE.SkinnedMesh | SkinnedMesh}, {@link THREE.InstancedMesh | InstancedMesh}. + * @example + * ```typescript + * const geometry = new THREE.BoxGeometry(1, 1, 1); + * const material = new THREE.MeshBasicMaterial({ + * color: 0xffff00 + * }); + * const {@link Mesh} = new THREE.Mesh(geometry, material); + * scene.add(mesh); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/objects/Mesh | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Mesh.js | Source} + */ +export class Mesh< + TGeometry extends BufferGeometry = BufferGeometry, + TMaterial extends Material | Material[] = Material | Material[], + TEventMap extends Object3DEventMap = Object3DEventMap, +> extends Object3D { + /** + * Create a new instance of {@link Mesh} + * @param geometry An instance of {@link THREE.BufferGeometry | BufferGeometry}. Default {@link THREE.BufferGeometry | `new THREE.BufferGeometry()`}. + * @param material A single or an array of {@link THREE.Material | Material}. Default {@link THREE.MeshBasicMaterial | `new THREE.MeshBasicMaterial()`}. + */ + constructor(geometry?: TGeometry, material?: TMaterial); + + /** + * Read-only flag to check if a given object is of type {@link Mesh}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isMesh: true; + + /** + * @override + * @defaultValue `Mesh` + */ + override readonly type: string | "Mesh"; + + /** + * An instance of {@link THREE.BufferGeometry | BufferGeometry} (or derived classes), defining the object's structure. + * @defaultValue {@link THREE.BufferGeometry | `new THREE.BufferGeometry()`}. + */ + geometry: TGeometry; + + /** + * An instance of material derived from the {@link THREE.Material | Material} base class or an array of materials, defining the object's appearance. + * @defaultValue {@link THREE.MeshBasicMaterial | `new THREE.MeshBasicMaterial()`}. + */ + material: TMaterial; + + /** + * An array of weights typically from `0-1` that specify how much of the morph is applied. + * @defaultValue `undefined`, _but reset to a blank array by {@link updateMorphTargets | .updateMorphTargets()}._ + */ + morphTargetInfluences?: number[] | undefined; + + /** + * A dictionary of morphTargets based on the `morphTarget.name` property. + * @defaultValue `undefined`, _but rebuilt by {@link updateMorphTargets | .updateMorphTargets()}._ + */ + morphTargetDictionary?: { [key: string]: number } | undefined; + + /** + * Updates the morphTargets to have no influence on the object + * @remarks Resets the {@link morphTargetInfluences} and {@link morphTargetDictionary} properties. + */ + updateMorphTargets(): void; + + /** + * Get the local-space position of the vertex at the given index, + * taking into account the current animation state of both morph targets and skinning. + * @param index Expects a `Integer` + * @param target + */ + getVertexPosition(index: number, target: Vector3): Vector3; + + toJSON(meta?: JSONMeta): MeshJSON; +} diff --git a/src-testing/src/objects/Points.d.ts b/src-testing/src/objects/Points.d.ts new file mode 100644 index 000000000..3cba2c74b --- /dev/null +++ b/src-testing/src/objects/Points.d.ts @@ -0,0 +1,66 @@ +import { BufferGeometry, NormalOrGLBufferAttributes } from "../core/BufferGeometry.js"; +import { Object3D, Object3DEventMap } from "../core/Object3D.js"; +import { Material } from "../materials/Material.js"; + +/** + * A class for displaying {@link Points} + * @remarks + * The {@link Points} are rendered by the {@link THREE.WebGLRenderer | WebGLRenderer} using {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements | gl.POINTS}. + * @see {@link https://threejs.org/docs/index.html#api/en/objects/Points | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Points.js | Source} + */ +export class Points< + TGeometry extends BufferGeometry = BufferGeometry, + TMaterial extends Material | Material[] = Material | Material[], + TEventMap extends Object3DEventMap = Object3DEventMap, +> extends Object3D { + /** + * Create a new instance of {@link Points} + * @param geometry An instance of {@link THREE.BufferGeometry | BufferGeometry}. Default {@link THREE.BufferGeometry | `new THREE.BufferGeometry()`}. + * @param material A single or an array of {@link THREE.Material | Material}. Default {@link THREE.PointsMaterial | `new THREE.PointsMaterial()`}. + */ + constructor(geometry?: TGeometry, material?: TMaterial); + + /** + * Read-only flag to check if a given object is of type {@link Points}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isPoints: true; + + /** + * @override + * @defaultValue `Points` + */ + override readonly type: string | "Points"; + + /** + * An array of weights typically from `0-1` that specify how much of the morph is applied. + * @defaultValue `undefined`, _but reset to a blank array by {@link updateMorphTargets | .updateMorphTargets()}._ + */ + morphTargetInfluences?: number[] | undefined; + + /** + * A dictionary of morphTargets based on the `morphTarget.name` property. + * @defaultValue `undefined`, _but rebuilt by {@link updateMorphTargets | .updateMorphTargets()}._ + */ + morphTargetDictionary?: { [key: string]: number } | undefined; + + /** + * An instance of {@link THREE.BufferGeometry | BufferGeometry} (or derived classes), defining the object's structure. + * @remarks each vertex designates the position of a particle in the system. + */ + geometry: TGeometry; + + /** + * An instance of {@link THREE.Material | Material}, defining the object's appearance. + * @defaultValue {@link THREE.PointsMaterial | `new THREE.PointsMaterial()`}, _with randomised colour_. + */ + material: TMaterial; + + /** + * Updates the morphTargets to have no influence on the object + * @remarks Resets the {@link morphTargetInfluences} and {@link morphTargetDictionary} properties. + */ + updateMorphTargets(): void; +} diff --git a/src-testing/src/objects/Skeleton.d.ts b/src-testing/src/objects/Skeleton.d.ts new file mode 100644 index 000000000..aaeb3198b --- /dev/null +++ b/src-testing/src/objects/Skeleton.d.ts @@ -0,0 +1,120 @@ +import { Matrix4, Matrix4Tuple } from "../math/Matrix4.js"; +import { DataTexture } from "../textures/DataTexture.js"; +import { Bone } from "./Bone.js"; + +export interface SkeletonJSON { + metadata: { version: number; type: string; generator: string }; + bones: string[]; + boneInverses: Matrix4Tuple[]; + uuid: string; +} + +/** + * Use an array of {@link Bone | bones} to create a {@link Skeleton} that can be used by a {@link THREE.SkinnedMesh | SkinnedMesh}. + * @example + * ```typescript + * // Create a simple "arm" + * const bones = []; + * const shoulder = new THREE.Bone(); + * const elbow = new THREE.Bone(); + * const hand = new THREE.Bone(); + * shoulder.add(elbow); + * elbow.add(hand); + * bones.push(shoulder); + * bones.push(elbow); + * bones.push(hand); + * shoulder.position.y = -5; + * elbow.position.y = 0; + * hand.position.y = 5; + * const armSkeleton = new THREE.Skeleton(bones); + * See the[page: SkinnedMesh] page + * for an example of usage with standard[page: BufferGeometry]. + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/objects/Skeleton | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Skeleton.js | Source} + */ +export class Skeleton { + /** + * Creates a new Skeleton. + * @param bones The array of {@link THREE.Bone | bones}. Default `[]`. + * @param boneInverses An array of {@link THREE.Matrix4 | Matrix4s}. Default `[]`. + */ + constructor(bones?: Bone[], boneInverses?: Matrix4[]); + + /** + * {@link http://en.wikipedia.org/wiki/Universally_unique_identifier | UUID} of this object instance. + * @remarks This gets automatically assigned and shouldn't be edited. + */ + uuid: string; + + /** + * The array of {@link THREE.Bone | Bones}. + * @remarks Note this is a copy of the original array, not a reference, so you can modify the original array without effecting this one. + */ + bones: Bone[]; + + /** + * An array of {@link Matrix4 | Matrix4s} that represent the inverse of the {@link THREE.Matrix4 | matrixWorld} of the individual bones. + */ + boneInverses: Matrix4[]; + + /** + * The array buffer holding the bone data when using a vertex texture. + */ + boneMatrices: Float32Array; + + /** + * The {@link THREE.DataTexture | DataTexture} holding the bone data when using a vertex texture. + */ + boneTexture: null | DataTexture; + + frame: number; + + init(): void; + + /** + * Generates the {@link boneInverses} array if not provided in the constructor. + */ + calculateInverses(): void; + + /** + * Computes an instance of {@link THREE.DataTexture | DataTexture} in order to pass the bone data more efficiently to the shader + * @remarks + * The texture is assigned to {@link boneTexture}. + */ + computeBoneTexture(): this; + + /** + * Returns the skeleton to the base pose. + */ + pose(): void; + + /** + * Updates the {@link boneMatrices} and {@link boneTexture} after changing the bones + * @remarks + * This is called automatically by the {@link THREE.WebGLRenderer | WebGLRenderer} if the {@link Skeleton} is used with a {@link THREE.SkinnedMesh | SkinnedMesh}. + */ + update(): void; + + /** + * Returns a clone of this {@link Skeleton} object. + */ + clone(): Skeleton; + + /** + * Searches through the skeleton's bone array and returns the first with a matching name. + * @param name String to match to the Bone's {@link THREE.Bone.name | .name} property. + */ + getBoneByName(name: string): undefined | Bone; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks + * Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; + + toJSON(): SkeletonJSON; + + fromJSON(json: SkeletonJSON, bones: Record): void; +} diff --git a/src-testing/src/objects/SkinnedMesh.d.ts b/src-testing/src/objects/SkinnedMesh.d.ts new file mode 100644 index 000000000..35149c5d1 --- /dev/null +++ b/src-testing/src/objects/SkinnedMesh.d.ts @@ -0,0 +1,160 @@ +import { BindMode } from "../constants.js"; +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { JSONMeta, Object3DEventMap } from "../core/Object3D.js"; +import { Material } from "../materials/Material.js"; +import { Box3 } from "../math/Box3.js"; +import { Matrix4, Matrix4Tuple } from "../math/Matrix4.js"; +import { Sphere } from "../math/Sphere.js"; +import { Vector3 } from "../math/Vector3.js"; +import { Mesh, MeshJSON, MeshJSONObject } from "./Mesh.js"; +import { Skeleton } from "./Skeleton.js"; + +export interface SkinnedMeshJSONObject extends MeshJSONObject { + bindMode: BindMode; + bindMatrix: Matrix4Tuple; + skeleton?: string; +} + +export interface SkinnedMeshJSON extends MeshJSON { + object: SkinnedMeshJSONObject; +} + +/** + * A mesh that has a {@link THREE.Skeleton | Skeleton} with {@link Bone | bones} that can then be used to animate the vertices of the geometry. + * @example + * ```typescript + * const geometry = new THREE.CylinderGeometry(5, 5, 5, 5, 15, 5, 30); + * // create the skin indices and skin weights manually + * // (typically a loader would read this data from a 3D model for you) + * const position = geometry.attributes.position; + * const vertex = new THREE.Vector3(); + * const skinIndices = []; + * const skinWeights = []; + * for (let i = 0; i & lt; position.count; i++) { + * vertex.fromBufferAttribute(position, i); + * // compute skinIndex and skinWeight based on some configuration data + * const y = (vertex.y + sizing.halfHeight); + * const skinIndex = Math.floor(y / sizing.segmentHeight); + * const skinWeight = (y % sizing.segmentHeight) / sizing.segmentHeight; + * skinIndices.push(skinIndex, skinIndex + 1, 0, 0); + * skinWeights.push(1 - skinWeight, skinWeight, 0, 0); + * } + * geometry.setAttribute('skinIndex', new THREE.Uint16BufferAttribute(skinIndices, 4)); + * geometry.setAttribute('skinWeight', new THREE.Float32BufferAttribute(skinWeights, 4)); + * // create skinned mesh and skeleton + * const mesh = new THREE.SkinnedMesh(geometry, material); + * const skeleton = new THREE.Skeleton(bones); + * // see example from THREE.Skeleton + * const rootBone = skeleton.bones[0]; + * mesh.add(rootBone); + * // bind the skeleton to the mesh + * mesh.bind(skeleton); + * // move the bones and manipulate the model + * skeleton.bones[0].rotation.x = -0.1; + * skeleton.bones[1].rotation.x = 0.2; + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/objects/SkinnedMesh | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/SkinnedMesh.js | Source} + */ +export class SkinnedMesh< + TGeometry extends BufferGeometry = BufferGeometry, + TMaterial extends Material | Material[] = Material | Material[], + TEventMap extends Object3DEventMap = Object3DEventMap, +> extends Mesh { + /** + * Create a new instance of {@link SkinnedMesh} + * @param geometry An instance of {@link THREE.BufferGeometry | BufferGeometry}. Default {@link THREE.BufferGeometry | `new THREE.BufferGeometry()`}. + * @param material A single or an array of {@link THREE.Material | Material}. Default {@link THREE.MeshBasicMaterial | `new THREE.MeshBasicMaterial()`}. + */ + constructor(geometry?: TGeometry, material?: TMaterial, useVertexTexture?: boolean); + + /** + * Read-only flag to check if a given object is of type {@link SkinnedMesh}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isSkinnedMesh: true; + + /** + * @override + * @defaultValue `SkinnedMesh` + */ + override readonly type: string | "SkinnedMesh"; + + /** + * Either {@link AttachedBindMode} or {@link DetachedBindMode}. {@link AttachedBindMode} means the skinned mesh + * shares the same world space as the skeleton. This is not true when using {@link DetachedBindMode} which is useful + * when sharing a skeleton across multiple skinned meshes. + * @defaultValue `AttachedBindMode` + */ + bindMode: BindMode; + + /** + * The base matrix that is used for the bound bone transforms. + */ + bindMatrix: Matrix4; + /** + * The base matrix that is used for resetting the bound bone transforms. + */ + bindMatrixInverse: Matrix4; + + /** + * The bounding box of the SkinnedMesh. Can be calculated with {@link computeBoundingBox | .computeBoundingBox()}. + * @default `null` + */ + boundingBox: Box3; + + /** + * The bounding box of the SkinnedMesh. Can be calculated with {@link computeBoundingSphere | .computeBoundingSphere()}. + * @default `null` + */ + boundingSphere: Sphere; + + /** + * {@link THREE.Skeleton | Skeleton} representing the bone hierarchy of the skinned mesh. + */ + skeleton: Skeleton; + + /** + * Bind a skeleton to the skinned mesh + * @remarks + * The bindMatrix gets saved to .bindMatrix property and the .bindMatrixInverse gets calculated. + * @param skeleton {@link THREE.Skeleton | Skeleton} created from a {@link Bone | Bones} tree. + * @param bindMatrix {@link THREE.Matrix4 | Matrix4} that represents the base transform of the skeleton. + */ + bind(skeleton: Skeleton, bindMatrix?: Matrix4): void; + + /** + * Computes the bounding box of the skinned mesh, and updates the {@link .boundingBox} attribute. The bounding box + * is not computed by the engine; it must be computed by your app. If the skinned mesh is animated, the bounding box + * should be recomputed per frame. + */ + computeBoundingBox(): void; + + /** + * Computes the bounding sphere of the skinned mesh, and updates the {@link .boundingSphere} attribute. The bounding + * sphere is automatically computed by the engine when it is needed, e.g., for ray casting and view frustum culling. + * If the skinned mesh is animated, the bounding sphere should be recomputed per frame. + */ + computeBoundingSphere(): void; + + /** + * This method sets the skinned mesh in the rest pose (resets the pose). + */ + pose(): void; + + /** + * Normalizes the skin weights. + */ + normalizeSkinWeights(): void; + + /** + * Applies the bone transform associated with the given index to the given position vector + * @remarks Returns the updated vector. + * @param index Expects a `Integer` + * @param vector + */ + applyBoneTransform(index: number, vector: Vector3): Vector3; + + toJSON(meta?: JSONMeta): SkinnedMeshJSON; +} diff --git a/src-testing/src/objects/Sprite.d.ts b/src-testing/src/objects/Sprite.d.ts new file mode 100644 index 000000000..427b8b414 --- /dev/null +++ b/src-testing/src/objects/Sprite.d.ts @@ -0,0 +1,65 @@ +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { Object3D, Object3DEventMap } from "../core/Object3D.js"; +import { SpriteMaterial } from "../materials/Materials.js"; +import { Vector2 } from "../math/Vector2.js"; + +/** + * A {@link Sprite} is a plane that always faces towards the camera, generally with a partially transparent texture applied. + * @remarks Sprites do not cast shadows, setting `castShadow = true` will have no effect. + * @example + * ```typescript + * const map = new THREE.TextureLoader().load('sprite.png'); + * const material = new THREE.SpriteMaterial({ + * map: map + * }); + * const {@link Sprite} = new THREE.Sprite(material); + * scene.add(sprite); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/objects/Sprite | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Sprite.js | Source} + */ +export class Sprite extends Object3D { + /** + * Creates a new Sprite. + * @param material An instance of {@link THREE.SpriteMaterial | SpriteMaterial}. Default {@link THREE.SpriteMaterial | `new SpriteMaterial()`}, _with white color_. + */ + constructor(material?: SpriteMaterial); + + /** + * Read-only flag to check if a given object is of type {@link Sprite}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isSprite: true; + + /** + * @override + * @defaultValue `Sprite` + */ + override readonly type: string | "Sprite"; + + /** + * Whether the object gets rendered into shadow map. + * No effect in {@link Sprite}. + * @ignore + * @hidden + * @defaultValue `false` + */ + override castShadow: false; + + geometry: BufferGeometry; + + /** + * An instance of {@link THREE.SpriteMaterial | SpriteMaterial}, defining the object's appearance. + * @defaultValue {@link THREE.SpriteMaterial | `new SpriteMaterial()`}, _with white color_. + */ + material: SpriteMaterial; + + /** + * The sprite's anchor point, and the point around which the {@link Sprite} rotates. + * A value of (0.5, 0.5) corresponds to the midpoint of the sprite. + * A value of (0, 0) corresponds to the lower left corner of the sprite. + * @defaultValue {@link THREE.Vector2 | `new Vector2(0.5, 0.5)`}. + */ + center: Vector2; +} diff --git a/src-testing/src/renderers/WebGL3DRenderTarget.d.ts b/src-testing/src/renderers/WebGL3DRenderTarget.d.ts new file mode 100644 index 000000000..420caa97e --- /dev/null +++ b/src-testing/src/renderers/WebGL3DRenderTarget.d.ts @@ -0,0 +1,29 @@ +import { RenderTargetOptions } from "../core/RenderTarget.js"; +import { Data3DTexture } from "../textures/Data3DTexture.js"; +import { WebGLRenderTarget } from "./WebGLRenderTarget.js"; + +/** + * Represents a three-dimensional render target. + */ +export class WebGL3DRenderTarget extends WebGLRenderTarget { + /** + * Creates a new WebGL3DRenderTarget. + * + * @param width the width of the render target, in pixels. Default is `1`. + * @param height the height of the render target, in pixels. Default is `1`. + * @param depth the depth of the render target. Default is `1`. + * @param options optional object that holds texture parameters for an auto-generated target texture and + * depthBuffer/stencilBuffer booleans. See {@link WebGLRenderTarget} for details. + */ + constructor(width?: number, height?: number, depth?: number, options?: RenderTargetOptions); + + textures: Data3DTexture[]; + + /** + * The texture property is overwritten with an instance of {@link Data3DTexture}. + */ + get texture(): Data3DTexture; + set texture(value: Data3DTexture); + + readonly isWebGL3DRenderTarget: true; +} diff --git a/src-testing/src/renderers/WebGLArrayRenderTarget.d.ts b/src-testing/src/renderers/WebGLArrayRenderTarget.d.ts new file mode 100644 index 000000000..1ac617889 --- /dev/null +++ b/src-testing/src/renderers/WebGLArrayRenderTarget.d.ts @@ -0,0 +1,29 @@ +import { RenderTargetOptions } from "../core/RenderTarget.js"; +import { DataArrayTexture } from "../textures/DataArrayTexture.js"; +import { WebGLRenderTarget } from "./WebGLRenderTarget.js"; + +/** + * This type of render target represents an array of textures. + */ +export class WebGLArrayRenderTarget extends WebGLRenderTarget { + /** + * Creates a new WebGLArrayRenderTarget. + * + * @param width the width of the render target, in pixels. Default is `1`. + * @param height the height of the render target, in pixels. Default is `1`. + * @param depth the depth/layer count of the render target. Default is `1`. + * @param options optional object that holds texture parameters for an auto-generated target texture and + * depthBuffer/stencilBuffer booleans. See {@link WebGLRenderTarget} for details. + */ + constructor(width?: number, height?: number, depth?: number, options?: RenderTargetOptions); + + textures: DataArrayTexture[]; + + /** + * The texture property is overwritten with an instance of {@link DataArrayTexture}. + */ + get texture(): DataArrayTexture; + set texture(value: DataArrayTexture); + + readonly isWebGLArrayRenderTarget: true; +} diff --git a/src-testing/src/renderers/WebGLCubeRenderTarget.d.ts b/src-testing/src/renderers/WebGLCubeRenderTarget.d.ts new file mode 100644 index 000000000..c390adb67 --- /dev/null +++ b/src-testing/src/renderers/WebGLCubeRenderTarget.d.ts @@ -0,0 +1,18 @@ +import { RenderTargetOptions } from "../core/RenderTarget.js"; +import { CubeTexture } from "../textures/CubeTexture.js"; +import { Texture } from "../textures/Texture.js"; +import { WebGLRenderer } from "./WebGLRenderer.js"; +import { WebGLRenderTarget } from "./WebGLRenderTarget.js"; + +export class WebGLCubeRenderTarget extends WebGLRenderTarget { + constructor(size?: number, options?: RenderTargetOptions); + + textures: CubeTexture[]; + + get texture(): CubeTexture; + set texture(value: CubeTexture); + + fromEquirectangularTexture(renderer: WebGLRenderer, texture: Texture): this; + + clear(renderer: WebGLRenderer, color: boolean, depth: boolean, stencil: boolean): void; +} diff --git a/src-testing/src/renderers/WebGLRenderTarget.d.ts b/src-testing/src/renderers/WebGLRenderTarget.d.ts new file mode 100644 index 000000000..fdff12e78 --- /dev/null +++ b/src-testing/src/renderers/WebGLRenderTarget.d.ts @@ -0,0 +1,8 @@ +import { RenderTarget, RenderTargetOptions } from "../core/RenderTarget.js"; +import { Texture } from "../textures/Texture.js"; + +export class WebGLRenderTarget extends RenderTarget { + constructor(width?: number, height?: number, options?: RenderTargetOptions); + + readonly isWebGLRenderTarget: true; +} diff --git a/src-testing/src/renderers/WebGLRenderer.d.ts b/src-testing/src/renderers/WebGLRenderer.d.ts new file mode 100644 index 000000000..b53a9d3c3 --- /dev/null +++ b/src-testing/src/renderers/WebGLRenderer.d.ts @@ -0,0 +1,562 @@ +import { Camera } from "../cameras/Camera.js"; +import { ColorSpace, CullFace, ShadowMapType, ToneMapping, WebGLCoordinateSystem } from "../constants.js"; +import { TypedArray } from "../core/BufferAttribute.js"; +import { BufferGeometry } from "../core/BufferGeometry.js"; +import { Object3D } from "../core/Object3D.js"; +import { Material } from "../materials/Material.js"; +import { Box2 } from "../math/Box2.js"; +import { Box3 } from "../math/Box3.js"; +import { Color, ColorRepresentation } from "../math/Color.js"; +import { Plane } from "../math/Plane.js"; +import { Vector2 } from "../math/Vector2.js"; +import { Vector3 } from "../math/Vector3.js"; +import { Vector4 } from "../math/Vector4.js"; +import { Scene } from "../scenes/Scene.js"; +import { Data3DTexture } from "../textures/Data3DTexture.js"; +import { DataArrayTexture } from "../textures/DataArrayTexture.js"; +import { OffscreenCanvas, Texture } from "../textures/Texture.js"; +import { WebGLCapabilities } from "./webgl/WebGLCapabilities.js"; +import { WebGLExtensions } from "./webgl/WebGLExtensions.js"; +import { WebGLInfo } from "./webgl/WebGLInfo.js"; +import { WebGLProgram } from "./webgl/WebGLProgram.js"; +import { WebGLProperties } from "./webgl/WebGLProperties.js"; +import { WebGLRenderLists } from "./webgl/WebGLRenderLists.js"; +import { WebGLShadowMap } from "./webgl/WebGLShadowMap.js"; +import { WebGLState } from "./webgl/WebGLState.js"; +import { WebGLRenderTarget } from "./WebGLRenderTarget.js"; +import { WebXRManager } from "./webxr/WebXRManager.js"; + +export interface Renderer { + domElement: HTMLCanvasElement; + + render(scene: Object3D, camera: Camera): void; + setSize(width: number, height: number, updateStyle?: boolean): void; +} + +export interface WebGLRendererParameters { + /** + * A Canvas where the renderer draws its output. + */ + canvas?: HTMLCanvasElement | OffscreenCanvas | undefined; + + /** + * A WebGL Rendering Context. + * (https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext) + * Default is null + */ + context?: WebGLRenderingContext | undefined; + + /** + * shader precision. Can be "highp", "mediump" or "lowp". + */ + precision?: string | undefined; + + /** + * default is false. + */ + alpha?: boolean | undefined; + + /** + * default is true. + */ + premultipliedAlpha?: boolean | undefined; + + /** + * default is false. + */ + antialias?: boolean | undefined; + + /** + * default is false. + */ + stencil?: boolean | undefined; + + /** + * default is false. + */ + preserveDrawingBuffer?: boolean | undefined; + + /** + * Can be "high-performance", "low-power" or "default" + */ + powerPreference?: string | undefined; + + /** + * default is true. + */ + depth?: boolean | undefined; + + /** + * default is false. + */ + logarithmicDepthBuffer?: boolean | undefined; + + /** + * default is false. + */ + failIfMajorPerformanceCaveat?: boolean | undefined; +} + +export interface WebGLDebug { + /** + * Enables error checking and reporting when shader programs are being compiled. + */ + checkShaderErrors: boolean; + + /** + * A callback function that can be used for custom error reporting. The callback receives the WebGL context, an + * instance of WebGLProgram as well two instances of WebGLShader representing the vertex and fragment shader. + * Assigning a custom function disables the default error reporting. + * @default `null` + */ + onShaderError: + | (( + gl: WebGLRenderingContext, + program: WebGLProgram, + glVertexShader: WebGLShader, + glFragmentShader: WebGLShader, + ) => void) + | null; +} + +/** + * The WebGL renderer displays your beautifully crafted scenes using WebGL, if your device supports it. + * This renderer has way better performance than CanvasRenderer. + * + * see {@link https://github.com/mrdoob/three.js/blob/master/src/renderers/WebGLRenderer.js|src/renderers/WebGLRenderer.js} + */ +export class WebGLRenderer implements Renderer { + /** + * parameters is an optional object with properties defining the renderer's behavior. + * The constructor also accepts no parameters at all. + * In all cases, it will assume sane defaults when parameters are missing. + */ + constructor(parameters?: WebGLRendererParameters); + + /** + * A Canvas where the renderer draws its output. + * This is automatically created by the renderer in the constructor (if not provided already); you just need to add it to your page. + * @default document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' ) + */ + domElement: HTMLCanvasElement; + + /** + * Defines whether the renderer should automatically clear its output before rendering. + * @default true + */ + autoClear: boolean; + + /** + * If autoClear is true, defines whether the renderer should clear the color buffer. Default is true. + * @default true + */ + autoClearColor: boolean; + + /** + * If autoClear is true, defines whether the renderer should clear the depth buffer. Default is true. + * @default true + */ + autoClearDepth: boolean; + + /** + * If autoClear is true, defines whether the renderer should clear the stencil buffer. Default is true. + * @default true + */ + autoClearStencil: boolean; + + /** + * Debug configurations. + * @default { checkShaderErrors: true } + */ + debug: WebGLDebug; + + /** + * Defines whether the renderer should sort objects. Default is true. + * @default true + */ + sortObjects: boolean; + + /** + * @default [] + */ + clippingPlanes: Plane[]; + + /** + * @default false + */ + localClippingEnabled: boolean; + + extensions: WebGLExtensions; + + /** + * Color space used for output to HTMLCanvasElement. Supported values are + * {@link SRGBColorSpace} and {@link LinearSRGBColorSpace}. + * @default THREE.SRGBColorSpace. + */ + get outputColorSpace(): ColorSpace; + set outputColorSpace(colorSpace: ColorSpace); + + get coordinateSystem(): typeof WebGLCoordinateSystem; + + /** + * @default THREE.NoToneMapping + */ + toneMapping: ToneMapping; + + /** + * @default 1 + */ + toneMappingExposure: number; + + info: WebGLInfo; + + shadowMap: WebGLShadowMap; + + pixelRatio: number; + + capabilities: WebGLCapabilities; + properties: WebGLProperties; + renderLists: WebGLRenderLists; + state: WebGLState; + + xr: WebXRManager; + + /** + * Return the WebGL context. + */ + getContext(): WebGLRenderingContext | WebGL2RenderingContext; + getContextAttributes(): any; + forceContextLoss(): void; + forceContextRestore(): void; + + /** + * @deprecated Use {@link WebGLCapabilities#getMaxAnisotropy .capabilities.getMaxAnisotropy()} instead. + */ + getMaxAnisotropy(): number; + + /** + * @deprecated Use {@link WebGLCapabilities#precision .capabilities.precision} instead. + */ + getPrecision(): string; + + getPixelRatio(): number; + setPixelRatio(value: number): void; + + getDrawingBufferSize(target: Vector2): Vector2; + setDrawingBufferSize(width: number, height: number, pixelRatio: number): void; + + getSize(target: Vector2): Vector2; + + /** + * Resizes the output canvas to (width, height), and also sets the viewport to fit that size, starting in (0, 0). + */ + setSize(width: number, height: number, updateStyle?: boolean): void; + + getCurrentViewport(target: Vector4): Vector4; + + /** + * Copies the viewport into target. + */ + getViewport(target: Vector4): Vector4; + + /** + * Sets the viewport to render from (x, y) to (x + width, y + height). + * (x, y) is the lower-left corner of the region. + */ + setViewport(x: Vector4 | number, y?: number, width?: number, height?: number): void; + + /** + * Copies the scissor area into target. + */ + getScissor(target: Vector4): Vector4; + + /** + * Sets the scissor area from (x, y) to (x + width, y + height). + */ + setScissor(x: Vector4 | number, y?: number, width?: number, height?: number): void; + + /** + * Returns true if scissor test is enabled; returns false otherwise. + */ + getScissorTest(): boolean; + + /** + * Enable the scissor test. When this is enabled, only the pixels within the defined scissor area will be affected by further renderer actions. + */ + setScissorTest(enable: boolean): void; + + /** + * Sets the custom opaque sort function for the WebGLRenderLists. Pass null to use the default painterSortStable function. + */ + setOpaqueSort(method: (a: any, b: any) => number): void; + + /** + * Sets the custom transparent sort function for the WebGLRenderLists. Pass null to use the default reversePainterSortStable function. + */ + setTransparentSort(method: (a: any, b: any) => number): void; + + /** + * Returns a THREE.Color instance with the current clear color. + */ + getClearColor(target: Color): Color; + + /** + * Sets the clear color, using color for the color and alpha for the opacity. + */ + setClearColor(color: ColorRepresentation, alpha?: number): void; + + /** + * Returns a float with the current clear alpha. Ranges from 0 to 1. + */ + getClearAlpha(): number; + + setClearAlpha(alpha: number): void; + + /** + * Tells the renderer to clear its color, depth or stencil drawing buffer(s). + * Arguments default to true + */ + clear(color?: boolean, depth?: boolean, stencil?: boolean): void; + + clearColor(): void; + clearDepth(): void; + clearStencil(): void; + clearTarget(renderTarget: WebGLRenderTarget, color: boolean, depth: boolean, stencil: boolean): void; + + /** + * @deprecated Use {@link WebGLState#reset .state.reset()} instead. + */ + resetGLState(): void; + dispose(): void; + + renderBufferDirect( + camera: Camera, + scene: Scene, + geometry: BufferGeometry, + material: Material, + object: Object3D, + geometryGroup: any, + ): void; + + /** + * A build in function that can be used instead of requestAnimationFrame. For WebXR projects this function must be used. + * @param callback The function will be called every available frame. If `null` is passed it will stop any already ongoing animation. + */ + setAnimationLoop(callback: XRFrameRequestCallback | null): void; + + /** + * @deprecated Use {@link WebGLRenderer#setAnimationLoop .setAnimationLoop()} instead. + */ + animate(callback: () => void): void; + + /** + * Compiles all materials in the scene with the camera. This is useful to precompile shaders before the first + * rendering. If you want to add a 3D object to an existing scene, use the third optional parameter for applying the + * target scene. + * Note that the (target) scene's lighting should be configured before calling this method. + */ + compile: (scene: Object3D, camera: Camera, targetScene?: Scene | null) => Set; + + /** + * Asynchronous version of {@link compile}(). The method returns a Promise that resolves when the given scene can be + * rendered without unnecessary stalling due to shader compilation. + * This method makes use of the KHR_parallel_shader_compile WebGL extension. + */ + compileAsync: (scene: Object3D, camera: Camera, targetScene?: Scene | null) => Promise; + + /** + * Render a scene or an object using a camera. + * The render is done to a previously specified {@link WebGLRenderTarget#renderTarget .renderTarget} set by calling + * {@link WebGLRenderer#setRenderTarget .setRenderTarget} or to the canvas as usual. + * + * By default render buffers are cleared before rendering but you can prevent this by setting the property + * {@link WebGLRenderer#autoClear autoClear} to false. If you want to prevent only certain buffers being cleared + * you can set either the {@link WebGLRenderer#autoClearColor autoClearColor}, + * {@link WebGLRenderer#autoClearStencil autoClearStencil} or {@link WebGLRenderer#autoClearDepth autoClearDepth} + * properties to false. To forcibly clear one ore more buffers call {@link WebGLRenderer#clear .clear}. + */ + render(scene: Object3D, camera: Camera): void; + + /** + * Returns the current active cube face. + */ + getActiveCubeFace(): number; + + /** + * Returns the current active mipmap level. + */ + getActiveMipmapLevel(): number; + + /** + * Returns the current render target. If no render target is set, null is returned. + */ + getRenderTarget(): WebGLRenderTarget | null; + + /** + * @deprecated Use {@link WebGLRenderer#getRenderTarget .getRenderTarget()} instead. + */ + getCurrentRenderTarget(): WebGLRenderTarget | null; + + /** + * Sets the active render target. + * + * @param renderTarget The {@link WebGLRenderTarget renderTarget} that needs to be activated. When `null` is given, the canvas is set as the active render target instead. + * @param activeCubeFace Specifies the active cube side (PX 0, NX 1, PY 2, NY 3, PZ 4, NZ 5) of {@link WebGLCubeRenderTarget}. + * @param activeMipmapLevel Specifies the active mipmap level. + */ + setRenderTarget( + renderTarget: WebGLRenderTarget | WebGLRenderTarget | null, + activeCubeFace?: number, + activeMipmapLevel?: number, + ): void; + + readRenderTargetPixels( + renderTarget: WebGLRenderTarget | WebGLRenderTarget, + x: number, + y: number, + width: number, + height: number, + buffer: TypedArray, + activeCubeFaceIndex?: number, + ): void; + + readRenderTargetPixelsAsync( + renderTarget: WebGLRenderTarget | WebGLRenderTarget, + x: number, + y: number, + width: number, + height: number, + buffer: TypedArray, + activeCubeFaceIndex?: number, + ): Promise; + + /** + * Copies a region of the currently bound framebuffer into the selected mipmap level of the selected texture. + * This region is defined by the size of the destination texture's mip level, offset by the input position. + * + * @param texture Specifies the destination texture. + * @param position Specifies the pixel offset from which to copy out of the framebuffer. + * @param level Specifies the destination mipmap level of the texture. + */ + copyFramebufferToTexture(texture: Texture, position?: Vector2 | null, level?: number): void; + + /** + * Copies the pixels of a texture in the bounds `srcRegion` in the destination texture starting from the given + * position. + * + * @param srcTexture Specifies the source texture. + * @param dstTexture Specifies the destination texture. + * @param srcRegion Specifies the bounds + * @param dstPosition Specifies the pixel offset into the dstTexture where the copy will occur. + * @param level Specifies the destination mipmap level of the texture. + */ + copyTextureToTexture( + srcTexture: Texture, + dstTexture: Texture, + srcRegion?: Box2 | null, + dstPosition?: Vector2 | null, + level?: number, + ): void; + + /** + * Copies the pixels of a texture in the bounds `srcRegion` in the destination texture starting from the given + * position. + * + * @param srcTexture Specifies the source texture. + * @param dstTexture Specifies the destination texture. + * @param srcRegion Specifies the bounds + * @param dstPosition Specifies the pixel offset into the dstTexture where the copy will occur. + * @param level Specifies the destination mipmap level of the texture. + */ + copyTextureToTexture3D( + srcTexture: Texture, + dstTexture: Data3DTexture | DataArrayTexture, + srcRegion?: Box3 | null, + dstPosition?: Vector3 | null, + level?: number, + ): void; + + /** + * Initializes the given WebGLRenderTarget memory. Useful for initializing a render target so data can be copied + * into it using {@link WebGLRenderer.copyTextureToTexture} before it has been rendered to. + * @param target + */ + initRenderTarget(target: WebGLRenderTarget): void; + + /** + * Initializes the given texture. Can be used to preload a texture rather than waiting until first render (which can cause noticeable lags due to decode and GPU upload overhead). + * + * @param texture The texture to Initialize. + */ + initTexture(texture: Texture): void; + + /** + * Can be used to reset the internal WebGL state. + */ + resetState(): void; + + /** + * @deprecated Use {@link WebGLRenderer#xr .xr} instead. + */ + vr: boolean; + + /** + * @deprecated Use {@link WebGLShadowMap#enabled .shadowMap.enabled} instead. + */ + shadowMapEnabled: boolean; + + /** + * @deprecated Use {@link WebGLShadowMap#type .shadowMap.type} instead. + */ + shadowMapType: ShadowMapType; + + /** + * @deprecated Use {@link WebGLShadowMap#cullFace .shadowMap.cullFace} instead. + */ + shadowMapCullFace: CullFace; + + /** + * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'OES_texture_float' )} instead. + */ + supportsFloatTextures(): any; + + /** + * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'OES_texture_half_float' )} instead. + */ + supportsHalfFloatTextures(): any; + + /** + * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'OES_standard_derivatives' )} instead. + */ + supportsStandardDerivatives(): any; + + /** + * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'WEBGL_compressed_texture_s3tc' )} instead. + */ + supportsCompressedTextureS3TC(): any; + + /** + * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'WEBGL_compressed_texture_pvrtc' )} instead. + */ + supportsCompressedTexturePVRTC(): any; + + /** + * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'EXT_blend_minmax' )} instead. + */ + supportsBlendMinMax(): any; + + /** + * @deprecated Use {@link WebGLCapabilities#vertexTextures .capabilities.vertexTextures} instead. + */ + supportsVertexTextures(): any; + + /** + * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'ANGLE_instanced_arrays' )} instead. + */ + supportsInstancedArrays(): any; + + /** + * @deprecated Use {@link WebGLRenderer#setScissorTest .setScissorTest()} instead. + */ + enableScissorTest(boolean: any): any; +} diff --git a/src-testing/src/renderers/common/Animation.ts b/src-testing/src/renderers/common/Animation.ts new file mode 100644 index 000000000..0b00319a1 --- /dev/null +++ b/src-testing/src/renderers/common/Animation.ts @@ -0,0 +1,38 @@ +class Animation { + constructor(nodes, info) { + this.nodes = nodes; + this.info = info; + + this.animationLoop = null; + this.requestId = null; + + this._init(); + } + + _init() { + const update = (time, frame) => { + this.requestId = self.requestAnimationFrame(update); + + if (this.info.autoReset === true) this.info.reset(); + + this.nodes.nodeFrame.update(); + + this.info.frame = this.nodes.nodeFrame.frameId; + + if (this.animationLoop !== null) this.animationLoop(time, frame); + }; + + update(); + } + + dispose() { + self.cancelAnimationFrame(this.requestId); + this.requestId = null; + } + + setAnimationLoop(callback) { + this.animationLoop = callback; + } +} + +export default Animation; diff --git a/src-testing/src/renderers/common/Attributes.ts b/src-testing/src/renderers/common/Attributes.ts new file mode 100644 index 000000000..ad7de8401 --- /dev/null +++ b/src-testing/src/renderers/common/Attributes.ts @@ -0,0 +1,54 @@ +import DataMap from './DataMap.js'; +import { AttributeType } from './Constants.js'; + +import { DynamicDrawUsage } from '../../constants.js'; + +class Attributes extends DataMap { + constructor(backend) { + super(); + + this.backend = backend; + } + + delete(attribute) { + const attributeData = super.delete(attribute); + + if (attributeData !== undefined) { + this.backend.destroyAttribute(attribute); + } + + return attributeData; + } + + update(attribute, type) { + const data = this.get(attribute); + + if (data.version === undefined) { + if (type === AttributeType.VERTEX) { + this.backend.createAttribute(attribute); + } else if (type === AttributeType.INDEX) { + this.backend.createIndexAttribute(attribute); + } else if (type === AttributeType.STORAGE) { + this.backend.createStorageAttribute(attribute); + } + + data.version = this._getBufferAttribute(attribute).version; + } else { + const bufferAttribute = this._getBufferAttribute(attribute); + + if (data.version < bufferAttribute.version || bufferAttribute.usage === DynamicDrawUsage) { + this.backend.updateAttribute(attribute); + + data.version = bufferAttribute.version; + } + } + } + + _getBufferAttribute(attribute) { + if (attribute.isInterleavedBufferAttribute) attribute = attribute.data; + + return attribute; + } +} + +export default Attributes; diff --git a/src-testing/src/renderers/common/Backend.ts b/src-testing/src/renderers/common/Backend.ts new file mode 100644 index 000000000..3dac46fe0 --- /dev/null +++ b/src-testing/src/renderers/common/Backend.ts @@ -0,0 +1,170 @@ +let vector2 = null; +let vector4 = null; +let color4 = null; + +import Color4 from './Color4.js'; +import { Vector2 } from '../../math/Vector2.js'; +import { Vector4 } from '../../math/Vector4.js'; +import { createCanvasElement } from '../../utils.js'; +import { REVISION } from '../../constants.js'; + +class Backend { + constructor(parameters = {}) { + this.parameters = Object.assign({}, parameters); + this.data = new WeakMap(); + this.renderer = null; + this.domElement = null; + } + + async init(renderer) { + this.renderer = renderer; + } + + // render context + + begin(/*renderContext*/) {} + + finish(/*renderContext*/) {} + + // render object + + draw(/*renderObject, info*/) {} + + // program + + createProgram(/*program*/) {} + + destroyProgram(/*program*/) {} + + // bindings + + createBindings(/*renderObject*/) {} + + updateBindings(/*renderObject*/) {} + + // pipeline + + createRenderPipeline(/*renderObject*/) {} + + createComputePipeline(/*computeNode, pipeline*/) {} + + destroyPipeline(/*pipeline*/) {} + + // cache key + + needsRenderUpdate(/*renderObject*/) {} // return Boolean ( fast test ) + + getRenderCacheKey(/*renderObject*/) {} // return String + + // node builder + + createNodeBuilder(/*renderObject*/) {} // return NodeBuilder (ADD IT) + + // textures + + createSampler(/*texture*/) {} + + createDefaultTexture(/*texture*/) {} + + createTexture(/*texture*/) {} + + copyTextureToBuffer(/*texture, x, y, width, height*/) {} + + // attributes + + createAttribute(/*attribute*/) {} + + createIndexAttribute(/*attribute*/) {} + + updateAttribute(/*attribute*/) {} + + destroyAttribute(/*attribute*/) {} + + // canvas + + getContext() {} + + updateSize() {} + + // utils + + resolveTimestampAsync(/*renderContext, type*/) {} + + hasFeatureAsync(/*name*/) {} // return Boolean + + hasFeature(/*name*/) {} // return Boolean + + getInstanceCount(renderObject) { + const { object, geometry } = renderObject; + + return geometry.isInstancedBufferGeometry ? geometry.instanceCount : object.count > 1 ? object.count : 1; + } + + getDrawingBufferSize() { + vector2 = vector2 || new Vector2(); + + return this.renderer.getDrawingBufferSize(vector2); + } + + getScissor() { + vector4 = vector4 || new Vector4(); + + return this.renderer.getScissor(vector4); + } + + setScissorTest(/*boolean*/) {} + + getClearColor() { + const renderer = this.renderer; + + color4 = color4 || new Color4(); + + renderer.getClearColor(color4); + + color4.getRGB(color4, this.renderer.currentColorSpace); + + return color4; + } + + getDomElement() { + let domElement = this.domElement; + + if (domElement === null) { + domElement = this.parameters.canvas !== undefined ? this.parameters.canvas : createCanvasElement(); + + // OffscreenCanvas does not have setAttribute, see #22811 + if ('setAttribute' in domElement) domElement.setAttribute('data-engine', `three.js r${REVISION} webgpu`); + + this.domElement = domElement; + } + + return domElement; + } + + // resource properties + + set(object, value) { + this.data.set(object, value); + } + + get(object) { + let map = this.data.get(object); + + if (map === undefined) { + map = {}; + this.data.set(object, map); + } + + return map; + } + + has(object) { + return this.data.has(object); + } + + delete(object) { + this.data.delete(object); + } +} + +export default Backend; diff --git a/src-testing/src/renderers/common/Background.ts b/src-testing/src/renderers/common/Background.ts new file mode 100644 index 000000000..2b163b6c7 --- /dev/null +++ b/src-testing/src/renderers/common/Background.ts @@ -0,0 +1,125 @@ +import DataMap from './DataMap.js'; +import Color4 from './Color4.js'; +import { + vec4, + context, + normalWorld, + backgroundBlurriness, + backgroundIntensity, + NodeMaterial, + modelViewProjection, +} from '../../nodes/Nodes.js'; + +import { Mesh } from '../../objects/Mesh.js'; +import { SphereGeometry } from '../../geometries/SphereGeometry.js'; +import { BackSide, LinearSRGBColorSpace } from '../../constants.js'; + +const _clearColor = /*@__PURE__*/ new Color4(); + +class Background extends DataMap { + constructor(renderer, nodes) { + super(); + + this.renderer = renderer; + this.nodes = nodes; + } + + update(scene, renderList, renderContext) { + const renderer = this.renderer; + const background = this.nodes.getBackgroundNode(scene) || scene.background; + + let forceClear = false; + + if (background === null) { + // no background settings, use clear color configuration from the renderer + + renderer._clearColor.getRGB(_clearColor, LinearSRGBColorSpace); + _clearColor.a = renderer._clearColor.a; + } else if (background.isColor === true) { + // background is an opaque color + + background.getRGB(_clearColor, LinearSRGBColorSpace); + _clearColor.a = 1; + + forceClear = true; + } else if (background.isNode === true) { + const sceneData = this.get(scene); + const backgroundNode = background; + + _clearColor.copy(renderer._clearColor); + + let backgroundMesh = sceneData.backgroundMesh; + + if (backgroundMesh === undefined) { + const backgroundMeshNode = context(vec4(backgroundNode).mul(backgroundIntensity), { + // @TODO: Add Texture2D support using node context + getUV: () => normalWorld, + getTextureLevel: () => backgroundBlurriness, + }); + + let viewProj = modelViewProjection(); + viewProj = viewProj.setZ(viewProj.w); + + const nodeMaterial = new NodeMaterial(); + nodeMaterial.name = 'Background.material'; + nodeMaterial.side = BackSide; + nodeMaterial.depthTest = false; + nodeMaterial.depthWrite = false; + nodeMaterial.fog = false; + nodeMaterial.lights = false; + nodeMaterial.vertexNode = viewProj; + nodeMaterial.colorNode = backgroundMeshNode; + + sceneData.backgroundMeshNode = backgroundMeshNode; + sceneData.backgroundMesh = backgroundMesh = new Mesh(new SphereGeometry(1, 32, 32), nodeMaterial); + backgroundMesh.frustumCulled = false; + backgroundMesh.name = 'Background.mesh'; + + backgroundMesh.onBeforeRender = function (renderer, scene, camera) { + this.matrixWorld.copyPosition(camera.matrixWorld); + }; + } + + const backgroundCacheKey = backgroundNode.getCacheKey(); + + if (sceneData.backgroundCacheKey !== backgroundCacheKey) { + sceneData.backgroundMeshNode.node = vec4(backgroundNode).mul(backgroundIntensity); + sceneData.backgroundMeshNode.needsUpdate = true; + + backgroundMesh.material.needsUpdate = true; + + sceneData.backgroundCacheKey = backgroundCacheKey; + } + + renderList.unshift(backgroundMesh, backgroundMesh.geometry, backgroundMesh.material, 0, 0, null); + } else { + console.error('THREE.Renderer: Unsupported background configuration.', background); + } + + // + + if (renderer.autoClear === true || forceClear === true) { + _clearColor.multiplyScalar(_clearColor.a); + + const clearColorValue = renderContext.clearColorValue; + + clearColorValue.r = _clearColor.r; + clearColorValue.g = _clearColor.g; + clearColorValue.b = _clearColor.b; + clearColorValue.a = _clearColor.a; + + renderContext.depthClearValue = renderer._clearDepth; + renderContext.stencilClearValue = renderer._clearStencil; + + renderContext.clearColor = renderer.autoClearColor === true; + renderContext.clearDepth = renderer.autoClearDepth === true; + renderContext.clearStencil = renderer.autoClearStencil === true; + } else { + renderContext.clearColor = false; + renderContext.clearDepth = false; + renderContext.clearStencil = false; + } + } +} + +export default Background; diff --git a/src-testing/src/renderers/common/BindGroup.ts b/src-testing/src/renderers/common/BindGroup.ts new file mode 100644 index 000000000..54b5f4965 --- /dev/null +++ b/src-testing/src/renderers/common/BindGroup.ts @@ -0,0 +1,13 @@ +let _id = 0; + +class BindGroup { + constructor(name = '', bindings = [], index = 0) { + this.name = name; + this.bindings = bindings; + this.index = index; + + this.id = _id++; + } +} + +export default BindGroup; diff --git a/src-testing/src/renderers/common/Binding.ts b/src-testing/src/renderers/common/Binding.ts new file mode 100644 index 000000000..a12f3563b --- /dev/null +++ b/src-testing/src/renderers/common/Binding.ts @@ -0,0 +1,17 @@ +class Binding { + constructor(name = '') { + this.name = name; + + this.visibility = 0; + } + + setVisibility(visibility) { + this.visibility |= visibility; + } + + clone() { + return Object.assign(new this.constructor(), this); + } +} + +export default Binding; diff --git a/src-testing/src/renderers/common/Bindings.ts b/src-testing/src/renderers/common/Bindings.ts new file mode 100644 index 000000000..51aa31f0d --- /dev/null +++ b/src-testing/src/renderers/common/Bindings.ts @@ -0,0 +1,161 @@ +import DataMap from './DataMap.js'; +import { AttributeType } from './Constants.js'; + +class Bindings extends DataMap { + constructor(backend, nodes, textures, attributes, pipelines, info) { + super(); + + this.backend = backend; + this.textures = textures; + this.pipelines = pipelines; + this.attributes = attributes; + this.nodes = nodes; + this.info = info; + + this.pipelines.bindings = this; // assign bindings to pipelines + } + + getForRender(renderObject) { + const bindings = renderObject.getBindings(); + + for (const bindGroup of bindings) { + const groupData = this.get(bindGroup); + + if (groupData.bindGroup === undefined) { + // each object defines an array of bindings (ubos, textures, samplers etc.) + + this._init(bindGroup); + + this.backend.createBindings(bindGroup, bindings); + + groupData.bindGroup = bindGroup; + } + } + + return bindings; + } + + getForCompute(computeNode) { + const bindings = this.nodes.getForCompute(computeNode).bindings; + + for (const bindGroup of bindings) { + const groupData = this.get(bindGroup); + + if (groupData.bindGroup === undefined) { + this._init(bindGroup); + + this.backend.createBindings(bindGroup, bindings); + + groupData.bindGroup = bindGroup; + } + } + + return bindings; + } + + updateForCompute(computeNode) { + this._updateBindings(computeNode, this.getForCompute(computeNode)); + } + + updateForRender(renderObject) { + this._updateBindings(renderObject, this.getForRender(renderObject)); + } + + _updateBindings(object, bindings) { + for (const bindGroup of bindings) { + this._update(object, bindGroup, bindings); + } + } + + _init(bindGroup) { + for (const binding of bindGroup.bindings) { + if (binding.isSampledTexture) { + this.textures.updateTexture(binding.texture); + } else if (binding.isStorageBuffer) { + const attribute = binding.attribute; + + this.attributes.update(attribute, AttributeType.STORAGE); + } + } + } + + _update(object, bindGroup, bindings) { + const { backend } = this; + + let needsBindingsUpdate = false; + + // iterate over all bindings and check if buffer updates or a new binding group is required + + for (const binding of bindGroup.bindings) { + if (binding.isNodeUniformsGroup) { + const updated = this.nodes.updateGroup(binding); + + if (!updated) continue; + } + + if (binding.isUniformBuffer) { + const updated = binding.update(); + + if (updated) { + backend.updateBinding(binding); + } + } else if (binding.isSampler) { + binding.update(); + } else if (binding.isSampledTexture) { + const texture = binding.texture; + + if (binding.needsBindingsUpdate) needsBindingsUpdate = true; + + const updated = binding.update(); + + if (updated) { + this.textures.updateTexture(binding.texture); + } + + const textureData = backend.get(binding.texture); + + if ( + backend.isWebGPUBackend === true && + textureData.texture === undefined && + textureData.externalTexture === undefined + ) { + // TODO: Remove this once we found why updated === false isn't bound to a texture in the WebGPU backend + console.error( + 'Bindings._update: binding should be available:', + binding, + updated, + binding.texture, + binding.textureNode.value, + ); + + this.textures.updateTexture(binding.texture); + needsBindingsUpdate = true; + } + + if (texture.isStorageTexture === true) { + const textureData = this.get(texture); + + if (binding.store === true) { + textureData.needsMipmap = true; + } else if ( + texture.generateMipmaps === true && + this.textures.needsMipmaps(texture) && + textureData.needsMipmap === true + ) { + this.backend.generateMipmaps(texture); + + textureData.needsMipmap = false; + } + } + } + } + + if (needsBindingsUpdate === true) { + const pipeline = this.pipelines.getForRender(object); + + this.backend.updateBindings(bindGroup, bindings, pipeline); + } + } +} + +export default Bindings; diff --git a/src-testing/src/renderers/common/Buffer.ts b/src-testing/src/renderers/common/Buffer.ts new file mode 100644 index 000000000..17013c6dc --- /dev/null +++ b/src-testing/src/renderers/common/Buffer.ts @@ -0,0 +1,28 @@ +import Binding from './Binding.js'; +import { getFloatLength } from './BufferUtils.js'; + +class Buffer extends Binding { + constructor(name, buffer = null) { + super(name); + + this.isBuffer = true; + + this.bytesPerElement = Float32Array.BYTES_PER_ELEMENT; + + this._buffer = buffer; + } + + get byteLength() { + return getFloatLength(this._buffer.byteLength); + } + + get buffer() { + return this._buffer; + } + + update() { + return true; + } +} + +export default Buffer; diff --git a/src-testing/src/renderers/common/BufferUtils.ts b/src-testing/src/renderers/common/BufferUtils.ts new file mode 100644 index 000000000..99ddcb48b --- /dev/null +++ b/src-testing/src/renderers/common/BufferUtils.ts @@ -0,0 +1,23 @@ +import { GPU_CHUNK_BYTES } from './Constants.js'; + +function getFloatLength(floatLength) { + // ensure chunk size alignment (STD140 layout) + + return floatLength + ((GPU_CHUNK_BYTES - (floatLength % GPU_CHUNK_BYTES)) % GPU_CHUNK_BYTES); +} + +function getVectorLength(count, vectorLength = 4) { + const strideLength = getStrideLength(vectorLength); + + const floatLength = strideLength * count; + + return getFloatLength(floatLength); +} + +function getStrideLength(vectorLength) { + const strideLength = 4; + + return vectorLength + ((strideLength - (vectorLength % strideLength)) % strideLength); +} + +export { getFloatLength, getVectorLength, getStrideLength }; diff --git a/src-testing/src/renderers/common/ChainMap.ts b/src-testing/src/renderers/common/ChainMap.ts new file mode 100644 index 000000000..b17e7080f --- /dev/null +++ b/src-testing/src/renderers/common/ChainMap.ts @@ -0,0 +1,43 @@ +export default class ChainMap { + constructor() { + this.weakMap = new WeakMap(); + } + + get(keys) { + let map = this.weakMap; + + for (let i = 0; i < keys.length; i++) { + map = map.get(keys[i]); + + if (map === undefined) return undefined; + } + + return map.get(keys[keys.length - 1]); + } + + set(keys, value) { + let map = this.weakMap; + + for (let i = 0; i < keys.length; i++) { + const key = keys[i]; + + if (map.has(key) === false) map.set(key, new WeakMap()); + + map = map.get(key); + } + + return map.set(keys[keys.length - 1], value); + } + + delete(keys) { + let map = this.weakMap; + + for (let i = 0; i < keys.length; i++) { + map = map.get(keys[i]); + + if (map === undefined) return false; + } + + return map.delete(keys[keys.length - 1]); + } +} diff --git a/src-testing/src/renderers/common/ClippingContext.ts b/src-testing/src/renderers/common/ClippingContext.ts new file mode 100644 index 000000000..207286052 --- /dev/null +++ b/src-testing/src/renderers/common/ClippingContext.ts @@ -0,0 +1,130 @@ +import { Matrix3 } from '../../math/Matrix3.js'; +import { Plane } from '../../math/Plane.js'; +import { Vector4 } from '../../math/Vector4.js'; + +const _plane = /*@__PURE__*/ new Plane(); + +let _clippingContextVersion = 0; + +class ClippingContext { + constructor() { + this.version = ++_clippingContextVersion; + + this.globalClippingCount = 0; + + this.localClippingCount = 0; + this.localClippingEnabled = false; + this.localClipIntersection = false; + + this.planes = []; + + this.parentVersion = 0; + this.viewNormalMatrix = new Matrix3(); + } + + projectPlanes(source, offset) { + const l = source.length; + const planes = this.planes; + + for (let i = 0; i < l; i++) { + _plane.copy(source[i]).applyMatrix4(this.viewMatrix, this.viewNormalMatrix); + + const v = planes[offset + i]; + const normal = _plane.normal; + + v.x = -normal.x; + v.y = -normal.y; + v.z = -normal.z; + v.w = _plane.constant; + } + } + + updateGlobal(renderer, camera) { + const rendererClippingPlanes = renderer.clippingPlanes; + this.viewMatrix = camera.matrixWorldInverse; + + this.viewNormalMatrix.getNormalMatrix(this.viewMatrix); + + let update = false; + + if (Array.isArray(rendererClippingPlanes) && rendererClippingPlanes.length !== 0) { + const l = rendererClippingPlanes.length; + + if (l !== this.globalClippingCount) { + const planes = []; + + for (let i = 0; i < l; i++) { + planes.push(new Vector4()); + } + + this.globalClippingCount = l; + this.planes = planes; + + update = true; + } + + this.projectPlanes(rendererClippingPlanes, 0); + } else if (this.globalClippingCount !== 0) { + this.globalClippingCount = 0; + this.planes = []; + update = true; + } + + if (renderer.localClippingEnabled !== this.localClippingEnabled) { + this.localClippingEnabled = renderer.localClippingEnabled; + update = true; + } + + if (update) this.version = _clippingContextVersion++; + } + + update(parent, material) { + let update = false; + + if (this !== parent && parent.version !== this.parentVersion) { + this.globalClippingCount = material.isShadowNodeMaterial ? 0 : parent.globalClippingCount; + this.localClippingEnabled = parent.localClippingEnabled; + this.planes = Array.from(parent.planes); + this.parentVersion = parent.version; + this.viewMatrix = parent.viewMatrix; + this.viewNormalMatrix = parent.viewNormalMatrix; + + update = true; + } + + if (this.localClippingEnabled) { + const localClippingPlanes = material.clippingPlanes; + + if (Array.isArray(localClippingPlanes) && localClippingPlanes.length !== 0) { + const l = localClippingPlanes.length; + const planes = this.planes; + const offset = this.globalClippingCount; + + if (update || l !== this.localClippingCount) { + planes.length = offset + l; + + for (let i = 0; i < l; i++) { + planes[offset + i] = new Vector4(); + } + + this.localClippingCount = l; + update = true; + } + + this.projectPlanes(localClippingPlanes, offset); + } else if (this.localClippingCount !== 0) { + this.localClippingCount = 0; + update = true; + } + + if (this.localClipIntersection !== material.clipIntersection) { + this.localClipIntersection = material.clipIntersection; + update = true; + } + } + + if (update) this.version = _clippingContextVersion++; + } +} + +export default ClippingContext; diff --git a/src-testing/src/renderers/common/Color4.ts b/src-testing/src/renderers/common/Color4.ts new file mode 100644 index 000000000..77caa31e0 --- /dev/null +++ b/src-testing/src/renderers/common/Color4.ts @@ -0,0 +1,27 @@ +import { Color } from '../../math/Color.js'; + +class Color4 extends Color { + constructor(r, g, b, a = 1) { + super(r, g, b); + + this.a = a; + } + + set(r, g, b, a = 1) { + this.a = a; + + return super.set(r, g, b); + } + + copy(color) { + if (color.a !== undefined) this.a = color.a; + + return super.copy(color); + } + + clone() { + return new this.constructor(this.r, this.g, this.b, this.a); + } +} + +export default Color4; diff --git a/src-testing/src/renderers/common/ComputePipeline.ts b/src-testing/src/renderers/common/ComputePipeline.ts new file mode 100644 index 000000000..0fd3ca531 --- /dev/null +++ b/src-testing/src/renderers/common/ComputePipeline.ts @@ -0,0 +1,13 @@ +import Pipeline from './Pipeline.js'; + +class ComputePipeline extends Pipeline { + constructor(cacheKey, computeProgram) { + super(cacheKey); + + this.computeProgram = computeProgram; + + this.isComputePipeline = true; + } +} + +export default ComputePipeline; diff --git a/src-testing/src/renderers/common/Constants.ts b/src-testing/src/renderers/common/Constants.ts new file mode 100644 index 000000000..0d0c35a25 --- /dev/null +++ b/src-testing/src/renderers/common/Constants.ts @@ -0,0 +1,14 @@ +export const AttributeType = { + VERTEX: 1, + INDEX: 2, + STORAGE: 4, +}; + +// size of a chunk in bytes (STD140 layout) + +export const GPU_CHUNK_BYTES = 16; + +// @TODO: Move to src/constants.js + +export const BlendColorFactor = 211; +export const OneMinusBlendColorFactor = 212; diff --git a/src-testing/src/renderers/common/CubeRenderTarget.ts b/src-testing/src/renderers/common/CubeRenderTarget.ts new file mode 100644 index 000000000..44f4f3c2a --- /dev/null +++ b/src-testing/src/renderers/common/CubeRenderTarget.ts @@ -0,0 +1,65 @@ +import { equirectUV } from '../../nodes/utils/EquirectUVNode.js'; +import { texture as TSL_Texture } from '../../nodes/accessors/TextureNode.js'; +import { positionWorldDirection } from '../../nodes/accessors/PositionNode.js'; +import { createNodeMaterialFromType } from '../../nodes/materials/NodeMaterial.js'; + +import { WebGLCubeRenderTarget } from '../../renderers/WebGLCubeRenderTarget.js'; +import { Scene } from '../../scenes/Scene.js'; +import { CubeCamera } from '../../cameras/CubeCamera.js'; +import { BoxGeometry } from '../../geometries/BoxGeometry.js'; +import { Mesh } from '../../objects/Mesh.js'; +import { BackSide, NoBlending, LinearFilter, LinearMipmapLinearFilter } from '../../constants.js'; + +// @TODO: Consider rename WebGLCubeRenderTarget to just CubeRenderTarget + +class CubeRenderTarget extends WebGLCubeRenderTarget { + constructor(size = 1, options = {}) { + super(size, options); + + this.isCubeRenderTarget = true; + } + + fromEquirectangularTexture(renderer, texture) { + const currentMinFilter = texture.minFilter; + const currentGenerateMipmaps = texture.generateMipmaps; + + texture.generateMipmaps = true; + + this.texture.type = texture.type; + this.texture.colorSpace = texture.colorSpace; + + this.texture.generateMipmaps = texture.generateMipmaps; + this.texture.minFilter = texture.minFilter; + this.texture.magFilter = texture.magFilter; + + const geometry = new BoxGeometry(5, 5, 5); + + const uvNode = equirectUV(positionWorldDirection); + + const material = createNodeMaterialFromType('MeshBasicNodeMaterial'); + material.colorNode = TSL_Texture(texture, uvNode, 0); + material.side = BackSide; + material.blending = NoBlending; + + const mesh = new Mesh(geometry, material); + + const scene = new Scene(); + scene.add(mesh); + + // Avoid blurred poles + if (texture.minFilter === LinearMipmapLinearFilter) texture.minFilter = LinearFilter; + + const camera = new CubeCamera(1, 10, this); + camera.update(renderer, scene); + + texture.minFilter = currentMinFilter; + texture.currentGenerateMipmaps = currentGenerateMipmaps; + + mesh.geometry.dispose(); + mesh.material.dispose(); + + return this; + } +} + +export default CubeRenderTarget; diff --git a/src-testing/src/renderers/common/DataMap.ts b/src-testing/src/renderers/common/DataMap.ts new file mode 100644 index 000000000..006bc2950 --- /dev/null +++ b/src-testing/src/renderers/common/DataMap.ts @@ -0,0 +1,38 @@ +class DataMap { + constructor() { + this.data = new WeakMap(); + } + + get(object) { + let map = this.data.get(object); + + if (map === undefined) { + map = {}; + this.data.set(object, map); + } + + return map; + } + + delete(object) { + let map; + + if (this.data.has(object)) { + map = this.data.get(object); + + this.data.delete(object); + } + + return map; + } + + has(object) { + return this.data.has(object); + } + + dispose() { + this.data = new WeakMap(); + } +} + +export default DataMap; diff --git a/src-testing/src/renderers/common/Geometries.ts b/src-testing/src/renderers/common/Geometries.ts new file mode 100644 index 000000000..9072f2381 --- /dev/null +++ b/src-testing/src/renderers/common/Geometries.ts @@ -0,0 +1,183 @@ +import DataMap from './DataMap.js'; +import { AttributeType } from './Constants.js'; + +import { Uint16BufferAttribute, Uint32BufferAttribute } from '../../core/BufferAttribute.js'; + +function arrayNeedsUint32(array) { + // assumes larger values usually on last + + for (let i = array.length - 1; i >= 0; --i) { + if (array[i] >= 65535) return true; // account for PRIMITIVE_RESTART_FIXED_INDEX, #24565 + } + + return false; +} + +function getWireframeVersion(geometry) { + return geometry.index !== null ? geometry.index.version : geometry.attributes.position.version; +} + +function getWireframeIndex(geometry) { + const indices = []; + + const geometryIndex = geometry.index; + const geometryPosition = geometry.attributes.position; + + if (geometryIndex !== null) { + const array = geometryIndex.array; + + for (let i = 0, l = array.length; i < l; i += 3) { + const a = array[i + 0]; + const b = array[i + 1]; + const c = array[i + 2]; + + indices.push(a, b, b, c, c, a); + } + } else { + const array = geometryPosition.array; + + for (let i = 0, l = array.length / 3 - 1; i < l; i += 3) { + const a = i + 0; + const b = i + 1; + const c = i + 2; + + indices.push(a, b, b, c, c, a); + } + } + + const attribute = new (arrayNeedsUint32(indices) ? Uint32BufferAttribute : Uint16BufferAttribute)(indices, 1); + attribute.version = getWireframeVersion(geometry); + + return attribute; +} + +class Geometries extends DataMap { + constructor(attributes, info) { + super(); + + this.attributes = attributes; + this.info = info; + + this.wireframes = new WeakMap(); + + this.attributeCall = new WeakMap(); + } + + has(renderObject) { + const geometry = renderObject.geometry; + + return super.has(geometry) && this.get(geometry).initialized === true; + } + + updateForRender(renderObject) { + if (this.has(renderObject) === false) this.initGeometry(renderObject); + + this.updateAttributes(renderObject); + } + + initGeometry(renderObject) { + const geometry = renderObject.geometry; + const geometryData = this.get(geometry); + + geometryData.initialized = true; + + this.info.memory.geometries++; + + const onDispose = () => { + this.info.memory.geometries--; + + const index = geometry.index; + const geometryAttributes = renderObject.getAttributes(); + + if (index !== null) { + this.attributes.delete(index); + } + + for (const geometryAttribute of geometryAttributes) { + this.attributes.delete(geometryAttribute); + } + + const wireframeAttribute = this.wireframes.get(geometry); + + if (wireframeAttribute !== undefined) { + this.attributes.delete(wireframeAttribute); + } + + geometry.removeEventListener('dispose', onDispose); + }; + + geometry.addEventListener('dispose', onDispose); + } + + updateAttributes(renderObject) { + const attributes = renderObject.getAttributes(); + + for (const attribute of attributes) { + if (attribute.isStorageBufferAttribute || attribute.isStorageInstancedBufferAttribute) { + this.updateAttribute(attribute, AttributeType.STORAGE); + } else { + this.updateAttribute(attribute, AttributeType.VERTEX); + } + } + + const index = this.getIndex(renderObject); + + if (index !== null) { + this.updateAttribute(index, AttributeType.INDEX); + } + } + + updateAttribute(attribute, type) { + const callId = this.info.render.calls; + + if (!attribute.isInterleavedBufferAttribute) { + if (this.attributeCall.get(attribute) !== callId) { + this.attributes.update(attribute, type); + + this.attributeCall.set(attribute, callId); + } + } else { + if (this.attributeCall.get(attribute) === undefined) { + this.attributes.update(attribute, type); + + this.attributeCall.set(attribute, callId); + } else if (this.attributeCall.get(attribute.data) !== callId) { + this.attributes.update(attribute, type); + + this.attributeCall.set(attribute.data, callId); + + this.attributeCall.set(attribute, callId); + } + } + } + + getIndex(renderObject) { + const { geometry, material } = renderObject; + + let index = geometry.index; + + if (material.wireframe === true) { + const wireframes = this.wireframes; + + let wireframeAttribute = wireframes.get(geometry); + + if (wireframeAttribute === undefined) { + wireframeAttribute = getWireframeIndex(geometry); + + wireframes.set(geometry, wireframeAttribute); + } else if (wireframeAttribute.version !== getWireframeVersion(geometry)) { + this.attributes.delete(wireframeAttribute); + + wireframeAttribute = getWireframeIndex(geometry); + + wireframes.set(geometry, wireframeAttribute); + } + + index = wireframeAttribute; + } + + return index; + } +} + +export default Geometries; diff --git a/src-testing/src/renderers/common/Info.ts b/src-testing/src/renderers/common/Info.ts new file mode 100644 index 000000000..4ede75de7 --- /dev/null +++ b/src-testing/src/renderers/common/Info.ts @@ -0,0 +1,95 @@ +class Info { + constructor() { + this.autoReset = true; + + this.frame = 0; + this.calls = 0; + + this.render = { + calls: 0, + frameCalls: 0, + drawCalls: 0, + triangles: 0, + points: 0, + lines: 0, + timestamp: 0, + previousFrameCalls: 0, + timestampCalls: 0, + }; + + this.compute = { + calls: 0, + frameCalls: 0, + timestamp: 0, + previousFrameCalls: 0, + timestampCalls: 0, + }; + + this.memory = { + geometries: 0, + textures: 0, + }; + } + + update(object, count, instanceCount) { + this.render.drawCalls++; + + if (object.isMesh || object.isSprite) { + this.render.triangles += instanceCount * (count / 3); + } else if (object.isPoints) { + this.render.points += instanceCount * count; + } else if (object.isLineSegments) { + this.render.lines += instanceCount * (count / 2); + } else if (object.isLine) { + this.render.lines += instanceCount * (count - 1); + } else { + console.error('THREE.WebGPUInfo: Unknown object type.'); + } + } + + updateTimestamp(type, time) { + if (this[type].timestampCalls === 0) { + this[type].timestamp = 0; + } + + this[type].timestamp += time; + + this[type].timestampCalls++; + + if (this[type].timestampCalls >= this[type].previousFrameCalls) { + this[type].timestampCalls = 0; + } + } + + reset() { + const previousRenderFrameCalls = this.render.frameCalls; + this.render.previousFrameCalls = previousRenderFrameCalls; + + const previousComputeFrameCalls = this.compute.frameCalls; + this.compute.previousFrameCalls = previousComputeFrameCalls; + + this.render.drawCalls = 0; + this.render.frameCalls = 0; + this.compute.frameCalls = 0; + + this.render.triangles = 0; + this.render.points = 0; + this.render.lines = 0; + } + + dispose() { + this.reset(); + + this.calls = 0; + + this.render.calls = 0; + this.compute.calls = 0; + + this.render.timestamp = 0; + this.compute.timestamp = 0; + this.memory.geometries = 0; + this.memory.textures = 0; + } +} + +export default Info; diff --git a/src-testing/src/renderers/common/Pipeline.ts b/src-testing/src/renderers/common/Pipeline.ts new file mode 100644 index 000000000..16017455a --- /dev/null +++ b/src-testing/src/renderers/common/Pipeline.ts @@ -0,0 +1,9 @@ +class Pipeline { + constructor(cacheKey) { + this.cacheKey = cacheKey; + + this.usedTimes = 0; + } +} + +export default Pipeline; diff --git a/src-testing/src/renderers/common/Pipelines.ts b/src-testing/src/renderers/common/Pipelines.ts new file mode 100644 index 000000000..68c8f223c --- /dev/null +++ b/src-testing/src/renderers/common/Pipelines.ts @@ -0,0 +1,270 @@ +import DataMap from './DataMap.js'; +import RenderPipeline from './RenderPipeline.js'; +import ComputePipeline from './ComputePipeline.js'; +import ProgrammableStage from './ProgrammableStage.js'; + +class Pipelines extends DataMap { + constructor(backend, nodes) { + super(); + + this.backend = backend; + this.nodes = nodes; + + this.bindings = null; // set by the bindings + + this.caches = new Map(); + this.programs = { + vertex: new Map(), + fragment: new Map(), + compute: new Map(), + }; + } + + getForCompute(computeNode, bindings) { + const { backend } = this; + + const data = this.get(computeNode); + + if (this._needsComputeUpdate(computeNode)) { + const previousPipeline = data.pipeline; + + if (previousPipeline) { + previousPipeline.usedTimes--; + previousPipeline.computeProgram.usedTimes--; + } + + // get shader + + const nodeBuilderState = this.nodes.getForCompute(computeNode); + + // programmable stage + + let stageCompute = this.programs.compute.get(nodeBuilderState.computeShader); + + if (stageCompute === undefined) { + if (previousPipeline && previousPipeline.computeProgram.usedTimes === 0) + this._releaseProgram(previousPipeline.computeProgram); + + stageCompute = new ProgrammableStage( + nodeBuilderState.computeShader, + 'compute', + nodeBuilderState.transforms, + nodeBuilderState.nodeAttributes, + ); + this.programs.compute.set(nodeBuilderState.computeShader, stageCompute); + + backend.createProgram(stageCompute); + } + + // determine compute pipeline + + const cacheKey = this._getComputeCacheKey(computeNode, stageCompute); + + let pipeline = this.caches.get(cacheKey); + + if (pipeline === undefined) { + if (previousPipeline && previousPipeline.usedTimes === 0) this._releasePipeline(previousPipeline); + + pipeline = this._getComputePipeline(computeNode, stageCompute, cacheKey, bindings); + } + + // keep track of all used times + + pipeline.usedTimes++; + stageCompute.usedTimes++; + + // + + data.version = computeNode.version; + data.pipeline = pipeline; + } + + return data.pipeline; + } + + getForRender(renderObject, promises = null) { + const { backend } = this; + + const data = this.get(renderObject); + + if (this._needsRenderUpdate(renderObject)) { + const previousPipeline = data.pipeline; + + if (previousPipeline) { + previousPipeline.usedTimes--; + previousPipeline.vertexProgram.usedTimes--; + previousPipeline.fragmentProgram.usedTimes--; + } + + // get shader + + const nodeBuilderState = renderObject.getNodeBuilderState(); + + // programmable stages + + let stageVertex = this.programs.vertex.get(nodeBuilderState.vertexShader); + + if (stageVertex === undefined) { + if (previousPipeline && previousPipeline.vertexProgram.usedTimes === 0) + this._releaseProgram(previousPipeline.vertexProgram); + + stageVertex = new ProgrammableStage(nodeBuilderState.vertexShader, 'vertex'); + this.programs.vertex.set(nodeBuilderState.vertexShader, stageVertex); + + backend.createProgram(stageVertex); + } + + let stageFragment = this.programs.fragment.get(nodeBuilderState.fragmentShader); + + if (stageFragment === undefined) { + if (previousPipeline && previousPipeline.fragmentProgram.usedTimes === 0) + this._releaseProgram(previousPipeline.fragmentProgram); + + stageFragment = new ProgrammableStage(nodeBuilderState.fragmentShader, 'fragment'); + this.programs.fragment.set(nodeBuilderState.fragmentShader, stageFragment); + + backend.createProgram(stageFragment); + } + + // determine render pipeline + + const cacheKey = this._getRenderCacheKey(renderObject, stageVertex, stageFragment); + + let pipeline = this.caches.get(cacheKey); + + if (pipeline === undefined) { + if (previousPipeline && previousPipeline.usedTimes === 0) this._releasePipeline(previousPipeline); + + pipeline = this._getRenderPipeline(renderObject, stageVertex, stageFragment, cacheKey, promises); + } else { + renderObject.pipeline = pipeline; + } + + // keep track of all used times + + pipeline.usedTimes++; + stageVertex.usedTimes++; + stageFragment.usedTimes++; + + // + + data.pipeline = pipeline; + } + + return data.pipeline; + } + + delete(object) { + const pipeline = this.get(object).pipeline; + + if (pipeline) { + // pipeline + + pipeline.usedTimes--; + + if (pipeline.usedTimes === 0) this._releasePipeline(pipeline); + + // programs + + if (pipeline.isComputePipeline) { + pipeline.computeProgram.usedTimes--; + + if (pipeline.computeProgram.usedTimes === 0) this._releaseProgram(pipeline.computeProgram); + } else { + pipeline.fragmentProgram.usedTimes--; + pipeline.vertexProgram.usedTimes--; + + if (pipeline.vertexProgram.usedTimes === 0) this._releaseProgram(pipeline.vertexProgram); + if (pipeline.fragmentProgram.usedTimes === 0) this._releaseProgram(pipeline.fragmentProgram); + } + } + + return super.delete(object); + } + + dispose() { + super.dispose(); + + this.caches = new Map(); + this.programs = { + vertex: new Map(), + fragment: new Map(), + compute: new Map(), + }; + } + + updateForRender(renderObject) { + this.getForRender(renderObject); + } + + _getComputePipeline(computeNode, stageCompute, cacheKey, bindings) { + // check for existing pipeline + + cacheKey = cacheKey || this._getComputeCacheKey(computeNode, stageCompute); + + let pipeline = this.caches.get(cacheKey); + + if (pipeline === undefined) { + pipeline = new ComputePipeline(cacheKey, stageCompute); + + this.caches.set(cacheKey, pipeline); + + this.backend.createComputePipeline(pipeline, bindings); + } + + return pipeline; + } + + _getRenderPipeline(renderObject, stageVertex, stageFragment, cacheKey, promises) { + // check for existing pipeline + + cacheKey = cacheKey || this._getRenderCacheKey(renderObject, stageVertex, stageFragment); + + let pipeline = this.caches.get(cacheKey); + + if (pipeline === undefined) { + pipeline = new RenderPipeline(cacheKey, stageVertex, stageFragment); + + this.caches.set(cacheKey, pipeline); + + renderObject.pipeline = pipeline; + + this.backend.createRenderPipeline(renderObject, promises); + } + + return pipeline; + } + + _getComputeCacheKey(computeNode, stageCompute) { + return computeNode.id + ',' + stageCompute.id; + } + + _getRenderCacheKey(renderObject, stageVertex, stageFragment) { + return stageVertex.id + ',' + stageFragment.id + ',' + this.backend.getRenderCacheKey(renderObject); + } + + _releasePipeline(pipeline) { + this.caches.delete(pipeline.cacheKey); + } + + _releaseProgram(program) { + const code = program.code; + const stage = program.stage; + + this.programs[stage].delete(code); + } + + _needsComputeUpdate(computeNode) { + const data = this.get(computeNode); + + return data.pipeline === undefined || data.version !== computeNode.version; + } + + _needsRenderUpdate(renderObject) { + const data = this.get(renderObject); + + return data.pipeline === undefined || this.backend.needsRenderUpdate(renderObject); + } +} + +export default Pipelines; diff --git a/src-testing/src/renderers/common/PostProcessing.d.ts b/src-testing/src/renderers/common/PostProcessing.d.ts new file mode 100644 index 000000000..fdd4e5bdc --- /dev/null +++ b/src-testing/src/renderers/common/PostProcessing.d.ts @@ -0,0 +1,21 @@ +import { Node } from "../../nodes/Nodes.js"; +import Renderer from "./Renderer.js"; + +declare class PostProcessing { + renderer: Renderer; + outputNode: Node; + + outputColorTransform: boolean; + + needsUpdate: boolean; + + constructor(renderer: Renderer, outputNode?: Node); + + render(): void; + + update(): void; + + renderAsync(): Promise; +} + +export default PostProcessing; diff --git a/src-testing/src/renderers/common/ProgrammableStage.ts b/src-testing/src/renderers/common/ProgrammableStage.ts new file mode 100644 index 000000000..a684e4443 --- /dev/null +++ b/src-testing/src/renderers/common/ProgrammableStage.ts @@ -0,0 +1,16 @@ +let _id = 0; + +class ProgrammableStage { + constructor(code, type, transforms = null, attributes = null) { + this.id = _id++; + + this.code = code; + this.stage = type; + this.transforms = transforms; + this.attributes = attributes; + + this.usedTimes = 0; + } +} + +export default ProgrammableStage; diff --git a/src-testing/src/renderers/common/QuadMesh.d.ts b/src-testing/src/renderers/common/QuadMesh.d.ts new file mode 100644 index 000000000..fb5eafb1b --- /dev/null +++ b/src-testing/src/renderers/common/QuadMesh.d.ts @@ -0,0 +1,14 @@ +import { OrthographicCamera } from "../../cameras/OrthographicCamera.js"; +import { Material } from "../../materials/Material.js"; +import { Mesh } from "../../objects/Mesh.js"; +import Renderer from "./Renderer.js"; + +export default class QuadMesh extends Mesh { + camera: OrthographicCamera; + + constructor(material?: Material | null); + + renderAsync(renderer: Renderer): Promise; + + render(renderer: Renderer): void; +} diff --git a/src-testing/src/renderers/common/RenderBundle.ts b/src-testing/src/renderers/common/RenderBundle.ts new file mode 100644 index 000000000..e59e49378 --- /dev/null +++ b/src-testing/src/renderers/common/RenderBundle.ts @@ -0,0 +1,12 @@ +class RenderBundle { + constructor(scene, camera) { + this.scene = scene; + this.camera = camera; + } + + clone() { + return Object.assign(new this.constructor(), this); + } +} + +export default RenderBundle; diff --git a/src-testing/src/renderers/common/RenderBundles.ts b/src-testing/src/renderers/common/RenderBundles.ts new file mode 100644 index 000000000..291403652 --- /dev/null +++ b/src-testing/src/renderers/common/RenderBundles.ts @@ -0,0 +1,28 @@ +import ChainMap from './ChainMap.js'; +import RenderBundle from './RenderBundle.js'; + +class RenderBundles { + constructor() { + this.lists = new ChainMap(); + } + + get(scene, camera) { + const lists = this.lists; + const keys = [scene, camera]; + + let list = lists.get(keys); + + if (list === undefined) { + list = new RenderBundle(scene, camera); + lists.set(keys, list); + } + + return list; + } + + dispose() { + this.lists = new ChainMap(); + } +} + +export default RenderBundles; diff --git a/src-testing/src/renderers/common/RenderContext.ts b/src-testing/src/renderers/common/RenderContext.ts new file mode 100644 index 000000000..342029ead --- /dev/null +++ b/src-testing/src/renderers/common/RenderContext.ts @@ -0,0 +1,57 @@ +import { Vector4 } from '../../math/Vector4.js'; + +let id = 0; + +class RenderContext { + constructor() { + this.id = id++; + + this.color = true; + this.clearColor = true; + this.clearColorValue = { r: 0, g: 0, b: 0, a: 1 }; + + this.depth = true; + this.clearDepth = true; + this.clearDepthValue = 1; + + this.stencil = false; + this.clearStencil = true; + this.clearStencilValue = 1; + + this.viewport = false; + this.viewportValue = new Vector4(); + + this.scissor = false; + this.scissorValue = new Vector4(); + + this.textures = null; + this.depthTexture = null; + this.activeCubeFace = 0; + this.sampleCount = 1; + + this.width = 0; + this.height = 0; + + this.isRenderContext = true; + } + + getCacheKey() { + return getCacheKey(this); + } +} + +export function getCacheKey(renderContext) { + const { textures, activeCubeFace } = renderContext; + + let key = ''; + + for (const texture of textures) { + key += texture.id + ','; + } + + key += activeCubeFace; + + return key; +} + +export default RenderContext; diff --git a/src-testing/src/renderers/common/RenderContexts.ts b/src-testing/src/renderers/common/RenderContexts.ts new file mode 100644 index 000000000..e77308c1d --- /dev/null +++ b/src-testing/src/renderers/common/RenderContexts.ts @@ -0,0 +1,47 @@ +import ChainMap from './ChainMap.js'; +import RenderContext from './RenderContext.js'; + +class RenderContexts { + constructor() { + this.chainMaps = {}; + } + + get(scene, camera, renderTarget = null) { + const chainKey = [scene, camera]; + + let attachmentState; + + if (renderTarget === null) { + attachmentState = 'default'; + } else { + const format = renderTarget.texture.format; + const count = renderTarget.textures.length; + + attachmentState = `${count}:${format}:${renderTarget.samples}:${renderTarget.depthBuffer}:${renderTarget.stencilBuffer}`; + } + + const chainMap = this.getChainMap(attachmentState); + + let renderState = chainMap.get(chainKey); + + if (renderState === undefined) { + renderState = new RenderContext(); + + chainMap.set(chainKey, renderState); + } + + if (renderTarget !== null) renderState.sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples; + + return renderState; + } + + getChainMap(attachmentState) { + return this.chainMaps[attachmentState] || (this.chainMaps[attachmentState] = new ChainMap()); + } + + dispose() { + this.chainMaps = {}; + } +} + +export default RenderContexts; diff --git a/src-testing/src/renderers/common/RenderList.ts b/src-testing/src/renderers/common/RenderList.ts new file mode 100644 index 000000000..a72a91dfd --- /dev/null +++ b/src-testing/src/renderers/common/RenderList.ts @@ -0,0 +1,145 @@ +import { LightsNode } from '../../nodes/Nodes.js'; + +function painterSortStable(a, b) { + if (a.groupOrder !== b.groupOrder) { + return a.groupOrder - b.groupOrder; + } else if (a.renderOrder !== b.renderOrder) { + return a.renderOrder - b.renderOrder; + } else if (a.material.id !== b.material.id) { + return a.material.id - b.material.id; + } else if (a.z !== b.z) { + return a.z - b.z; + } else { + return a.id - b.id; + } +} + +function reversePainterSortStable(a, b) { + if (a.groupOrder !== b.groupOrder) { + return a.groupOrder - b.groupOrder; + } else if (a.renderOrder !== b.renderOrder) { + return a.renderOrder - b.renderOrder; + } else if (a.z !== b.z) { + return b.z - a.z; + } else { + return a.id - b.id; + } +} + +class RenderList { + constructor() { + this.renderItems = []; + this.renderItemsIndex = 0; + + this.opaque = []; + this.transparent = []; + this.bundles = []; + + this.lightsNode = new LightsNode([]); + this.lightsArray = []; + + this.occlusionQueryCount = 0; + } + + begin() { + this.renderItemsIndex = 0; + + this.opaque.length = 0; + this.transparent.length = 0; + this.bundles.length = 0; + + this.lightsArray.length = 0; + + this.occlusionQueryCount = 0; + + return this; + } + + getNextRenderItem(object, geometry, material, groupOrder, z, group) { + let renderItem = this.renderItems[this.renderItemsIndex]; + + if (renderItem === undefined) { + renderItem = { + id: object.id, + object: object, + geometry: geometry, + material: material, + groupOrder: groupOrder, + renderOrder: object.renderOrder, + z: z, + group: group, + }; + + this.renderItems[this.renderItemsIndex] = renderItem; + } else { + renderItem.id = object.id; + renderItem.object = object; + renderItem.geometry = geometry; + renderItem.material = material; + renderItem.groupOrder = groupOrder; + renderItem.renderOrder = object.renderOrder; + renderItem.z = z; + renderItem.group = group; + } + + this.renderItemsIndex++; + + return renderItem; + } + + push(object, geometry, material, groupOrder, z, group) { + const renderItem = this.getNextRenderItem(object, geometry, material, groupOrder, z, group); + + if (object.occlusionTest === true) this.occlusionQueryCount++; + + (material.transparent === true || material.transmission > 0 ? this.transparent : this.opaque).push(renderItem); + } + + unshift(object, geometry, material, groupOrder, z, group) { + const renderItem = this.getNextRenderItem(object, geometry, material, groupOrder, z, group); + + (material.transparent === true ? this.transparent : this.opaque).unshift(renderItem); + } + + pushBundle(group) { + this.bundles.push(group); + } + + pushLight(light) { + this.lightsArray.push(light); + } + + getLightsNode() { + return this.lightsNode.fromLights(this.lightsArray); + } + + sort(customOpaqueSort, customTransparentSort) { + if (this.opaque.length > 1) this.opaque.sort(customOpaqueSort || painterSortStable); + if (this.transparent.length > 1) this.transparent.sort(customTransparentSort || reversePainterSortStable); + } + + finish() { + // update lights + + this.lightsNode.fromLights(this.lightsArray); + + // Clear references from inactive renderItems in the list + + for (let i = this.renderItemsIndex, il = this.renderItems.length; i < il; i++) { + const renderItem = this.renderItems[i]; + + if (renderItem.id === null) break; + + renderItem.id = null; + renderItem.object = null; + renderItem.geometry = null; + renderItem.material = null; + renderItem.groupOrder = null; + renderItem.renderOrder = null; + renderItem.z = null; + renderItem.group = null; + } + } +} + +export default RenderList; diff --git a/src-testing/src/renderers/common/RenderLists.ts b/src-testing/src/renderers/common/RenderLists.ts new file mode 100644 index 000000000..3fc3134e6 --- /dev/null +++ b/src-testing/src/renderers/common/RenderLists.ts @@ -0,0 +1,28 @@ +import ChainMap from './ChainMap.js'; +import RenderList from './RenderList.js'; + +class RenderLists { + constructor() { + this.lists = new ChainMap(); + } + + get(scene, camera) { + const lists = this.lists; + const keys = [scene, camera]; + + let list = lists.get(keys); + + if (list === undefined) { + list = new RenderList(); + lists.set(keys, list); + } + + return list; + } + + dispose() { + this.lists = new ChainMap(); + } +} + +export default RenderLists; diff --git a/src-testing/src/renderers/common/RenderObject.ts b/src-testing/src/renderers/common/RenderObject.ts new file mode 100644 index 000000000..703e3d206 --- /dev/null +++ b/src-testing/src/renderers/common/RenderObject.ts @@ -0,0 +1,231 @@ +import ClippingContext from './ClippingContext.js'; + +let _id = 0; + +function getKeys(obj) { + const keys = Object.keys(obj); + + let proto = Object.getPrototypeOf(obj); + + while (proto) { + const descriptors = Object.getOwnPropertyDescriptors(proto); + + for (const key in descriptors) { + if (descriptors[key] !== undefined) { + const descriptor = descriptors[key]; + + if (descriptor && typeof descriptor.get === 'function') { + keys.push(key); + } + } + } + + proto = Object.getPrototypeOf(proto); + } + + return keys; +} + +export default class RenderObject { + constructor(nodes, geometries, renderer, object, material, scene, camera, lightsNode, renderContext) { + this._nodes = nodes; + this._geometries = geometries; + + this.id = _id++; + + this.renderer = renderer; + this.object = object; + this.material = material; + this.scene = scene; + this.camera = camera; + this.lightsNode = lightsNode; + this.context = renderContext; + + this.geometry = object.geometry; + this.version = material.version; + + this.drawRange = null; + + this.attributes = null; + this.pipeline = null; + this.vertexBuffers = null; + + this.updateClipping(renderContext.clippingContext); + + this.clippingContextVersion = this.clippingContext.version; + + this.initialNodesCacheKey = this.getDynamicCacheKey(); + this.initialCacheKey = this.getCacheKey(); + + this._nodeBuilderState = null; + this._bindings = null; + + this.onDispose = null; + + this.isRenderObject = true; + + this.onMaterialDispose = () => { + this.dispose(); + }; + + this.material.addEventListener('dispose', this.onMaterialDispose); + } + + updateClipping(parent) { + const material = this.material; + + let clippingContext = this.clippingContext; + + if (Array.isArray(material.clippingPlanes)) { + if (clippingContext === parent || !clippingContext) { + clippingContext = new ClippingContext(); + this.clippingContext = clippingContext; + } + + clippingContext.update(parent, material); + } else if (this.clippingContext !== parent) { + this.clippingContext = parent; + } + } + + get clippingNeedsUpdate() { + if (this.clippingContext.version === this.clippingContextVersion) return false; + + this.clippingContextVersion = this.clippingContext.version; + + return true; + } + + getNodeBuilderState() { + return this._nodeBuilderState || (this._nodeBuilderState = this._nodes.getForRender(this)); + } + + getBindings() { + return this._bindings || (this._bindings = this.getNodeBuilderState().createBindings()); + } + + getIndex() { + return this._geometries.getIndex(this); + } + + getChainArray() { + return [this.object, this.material, this.context, this.lightsNode]; + } + + getAttributes() { + if (this.attributes !== null) return this.attributes; + + const nodeAttributes = this.getNodeBuilderState().nodeAttributes; + const geometry = this.geometry; + + const attributes = []; + const vertexBuffers = new Set(); + + for (const nodeAttribute of nodeAttributes) { + const attribute = + nodeAttribute.node && nodeAttribute.node.attribute + ? nodeAttribute.node.attribute + : geometry.getAttribute(nodeAttribute.name); + + if (attribute === undefined) continue; + + attributes.push(attribute); + + const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute; + vertexBuffers.add(bufferAttribute); + } + + this.attributes = attributes; + this.vertexBuffers = Array.from(vertexBuffers.values()); + + return attributes; + } + + getVertexBuffers() { + if (this.vertexBuffers === null) this.getAttributes(); + + return this.vertexBuffers; + } + + getMaterialCacheKey() { + const { object, material } = this; + + let cacheKey = material.customProgramCacheKey(); + + for (const property of getKeys(material)) { + if (/^(is[A-Z]|_)|^(visible|version|uuid|name|opacity|userData)$/.test(property)) continue; + + const value = material[property]; + + let valueKey; + + if (value !== null) { + // some material values require a formatting + + const type = typeof value; + + if (type === 'number') { + valueKey = value !== 0 ? '1' : '0'; // Convert to on/off, important for clearcoat, transmission, etc + } else if (type === 'object') { + valueKey = '{'; + + if (value.isTexture) { + valueKey += value.mapping; + } + + valueKey += '}'; + } else { + valueKey = String(value); + } + } else { + valueKey = String(value); + } + + cacheKey += /*property + ':' +*/ valueKey + ','; + } + + cacheKey += this.clippingContextVersion + ','; + + if (object.skeleton) { + cacheKey += object.skeleton.bones.length + ','; + } + + if (object.morphTargetInfluences) { + cacheKey += object.morphTargetInfluences.length + ','; + } + + if (object.isBatchedMesh) { + cacheKey += object._matricesTexture.uuid + ','; + + if (object._colorsTexture !== null) { + cacheKey += object._colorsTexture.uuid + ','; + } + } + + if (object.count > 1) { + cacheKey += object.count + ',' + object.uuid + ','; + } + + return cacheKey; + } + + get needsUpdate() { + return this.initialNodesCacheKey !== this.getDynamicCacheKey() || this.clippingNeedsUpdate; + } + + getDynamicCacheKey() { + // Environment Nodes Cache Key + + return this.object.receiveShadow + ',' + this._nodes.getCacheKey(this.scene, this.lightsNode); + } + + getCacheKey() { + return this.getMaterialCacheKey() + ',' + this.getDynamicCacheKey(); + } + + dispose() { + this.material.removeEventListener('dispose', this.onMaterialDispose); + + this.onDispose(); + } +} diff --git a/src-testing/src/renderers/common/RenderObjects.ts b/src-testing/src/renderers/common/RenderObjects.ts new file mode 100644 index 000000000..76dc482e4 --- /dev/null +++ b/src-testing/src/renderers/common/RenderObjects.ts @@ -0,0 +1,100 @@ +import ChainMap from './ChainMap.js'; +import RenderObject from './RenderObject.js'; + +class RenderObjects { + constructor(renderer, nodes, geometries, pipelines, bindings, info) { + this.renderer = renderer; + this.nodes = nodes; + this.geometries = geometries; + this.pipelines = pipelines; + this.bindings = bindings; + this.info = info; + + this.chainMaps = {}; + } + + get(object, material, scene, camera, lightsNode, renderContext, passId) { + const chainMap = this.getChainMap(passId); + const chainArray = [object, material, renderContext, lightsNode]; + + let renderObject = chainMap.get(chainArray); + + if (renderObject === undefined) { + renderObject = this.createRenderObject( + this.nodes, + this.geometries, + this.renderer, + object, + material, + scene, + camera, + lightsNode, + renderContext, + passId, + ); + + chainMap.set(chainArray, renderObject); + } else { + renderObject.updateClipping(renderContext.clippingContext); + + if (renderObject.version !== material.version || renderObject.needsUpdate) { + if (renderObject.initialCacheKey !== renderObject.getCacheKey()) { + renderObject.dispose(); + + renderObject = this.get(object, material, scene, camera, lightsNode, renderContext, passId); + } else { + renderObject.version = material.version; + } + } + } + + return renderObject; + } + + getChainMap(passId = 'default') { + return this.chainMaps[passId] || (this.chainMaps[passId] = new ChainMap()); + } + + dispose() { + this.chainMaps = {}; + } + + createRenderObject( + nodes, + geometries, + renderer, + object, + material, + scene, + camera, + lightsNode, + renderContext, + passId, + ) { + const chainMap = this.getChainMap(passId); + + const renderObject = new RenderObject( + nodes, + geometries, + renderer, + object, + material, + scene, + camera, + lightsNode, + renderContext, + ); + + renderObject.onDispose = () => { + this.pipelines.delete(renderObject); + this.bindings.delete(renderObject); + this.nodes.delete(renderObject); + + chainMap.delete(renderObject.getChainArray()); + }; + + return renderObject; + } +} + +export default RenderObjects; diff --git a/src-testing/src/renderers/common/RenderPipeline.ts b/src-testing/src/renderers/common/RenderPipeline.ts new file mode 100644 index 000000000..0ec34b043 --- /dev/null +++ b/src-testing/src/renderers/common/RenderPipeline.ts @@ -0,0 +1,12 @@ +import Pipeline from './Pipeline.js'; + +class RenderPipeline extends Pipeline { + constructor(cacheKey, vertexProgram, fragmentProgram) { + super(cacheKey); + + this.vertexProgram = vertexProgram; + this.fragmentProgram = fragmentProgram; + } +} + +export default RenderPipeline; diff --git a/src-testing/src/renderers/common/Renderer.ts b/src-testing/src/renderers/common/Renderer.ts new file mode 100644 index 000000000..be803038b --- /dev/null +++ b/src-testing/src/renderers/common/Renderer.ts @@ -0,0 +1,1399 @@ +import Animation from './Animation.js'; +import RenderObjects from './RenderObjects.js'; +import Attributes from './Attributes.js'; +import Geometries from './Geometries.js'; +import Info from './Info.js'; +import Pipelines from './Pipelines.js'; +import Bindings from './Bindings.js'; +import RenderLists from './RenderLists.js'; +import RenderContexts from './RenderContexts.js'; +import Textures from './Textures.js'; +import Background from './Background.js'; +import Nodes from './nodes/Nodes.js'; +import Color4 from './Color4.js'; +import ClippingContext from './ClippingContext.js'; +import QuadMesh from './QuadMesh.js'; +import RenderBundles from './RenderBundles.js'; + +import { NodeMaterial } from '../../nodes/Nodes.js'; + +import { Scene } from '../../scenes/Scene.js'; +import { Frustum } from '../../math/Frustum.js'; +import { Matrix4 } from '../../math/Matrix4.js'; +import { Vector2 } from '../../math/Vector2.js'; +import { Vector3 } from '../../math/Vector3.js'; +import { Vector4 } from '../../math/Vector4.js'; +import { RenderTarget } from '../../core/RenderTarget.js'; +import { + DoubleSide, + BackSide, + FrontSide, + SRGBColorSpace, + NoColorSpace, + NoToneMapping, + LinearFilter, + LinearSRGBColorSpace, + HalfFloatType, + RGBAFormat, + PCFShadowMap, +} from '../../constants.js'; + +const _scene = /*@__PURE__*/ new Scene(); +const _drawingBufferSize = /*@__PURE__*/ new Vector2(); +const _screen = /*@__PURE__*/ new Vector4(); +const _frustum = /*@__PURE__*/ new Frustum(); +const _projScreenMatrix = /*@__PURE__*/ new Matrix4(); +const _vector3 = /*@__PURE__*/ new Vector3(); + +class Renderer { + constructor(backend, parameters = {}) { + this.isRenderer = true; + + // + + const { logarithmicDepthBuffer = false, alpha = true, antialias = false, samples = 0 } = parameters; + + // public + this.domElement = backend.getDomElement(); + + this.backend = backend; + + this.samples = samples || antialias === true ? 4 : 0; + + this.autoClear = true; + this.autoClearColor = true; + this.autoClearDepth = true; + this.autoClearStencil = true; + + this.alpha = alpha; + + this.logarithmicDepthBuffer = logarithmicDepthBuffer; + + this.outputColorSpace = SRGBColorSpace; + + this.toneMapping = NoToneMapping; + this.toneMappingExposure = 1.0; + + this.sortObjects = true; + + this.depth = true; + this.stencil = false; + + this.clippingPlanes = []; + + this.info = new Info(); + + // internals + + this._pixelRatio = 1; + this._width = this.domElement.width; + this._height = this.domElement.height; + + this._viewport = new Vector4(0, 0, this._width, this._height); + this._scissor = new Vector4(0, 0, this._width, this._height); + this._scissorTest = false; + + this._attributes = null; + this._geometries = null; + this._nodes = null; + this._animation = null; + this._bindings = null; + this._objects = null; + this._pipelines = null; + this._bundles = null; + this._renderLists = null; + this._renderContexts = null; + this._textures = null; + this._background = null; + + this._quad = new QuadMesh(new NodeMaterial()); + + this._currentRenderContext = null; + + this._opaqueSort = null; + this._transparentSort = null; + + this._frameBufferTarget = null; + + const alphaClear = this.alpha === true ? 0 : 1; + + this._clearColor = new Color4(0, 0, 0, alphaClear); + this._clearDepth = 1; + this._clearStencil = 0; + + this._renderTarget = null; + this._activeCubeFace = 0; + this._activeMipmapLevel = 0; + + this._mrt = null; + + this._renderObjectFunction = null; + this._currentRenderObjectFunction = null; + this._currentRenderBundle = null; + + this._handleObjectFunction = this._renderObjectDirect; + + this._initialized = false; + this._initPromise = null; + + this._compilationPromises = null; + + this.transparent = true; + this.opaque = true; + + this.shadowMap = { + enabled: false, + type: PCFShadowMap, + }; + + this.xr = { + enabled: false, + }; + + this.debug = { + checkShaderErrors: true, + onShaderError: null, + getShaderAsync: async (scene, camera, object) => { + await this.compileAsync(scene, camera); + + const renderList = this._renderLists.get(scene, camera); + const renderContext = this._renderContexts.get(scene, camera, this._renderTarget); + + const material = scene.overrideMaterial || object.material; + + const renderObject = this._objects.get( + object, + material, + scene, + camera, + renderList.lightsNode, + renderContext, + ); + + const { fragmentShader, vertexShader } = renderObject.getNodeBuilderState(); + + return { fragmentShader, vertexShader }; + }, + }; + } + + async init() { + if (this._initialized) { + throw new Error('Renderer: Backend has already been initialized.'); + } + + if (this._initPromise !== null) { + return this._initPromise; + } + + this._initPromise = new Promise(async (resolve, reject) => { + const backend = this.backend; + + try { + await backend.init(this); + } catch (error) { + reject(error); + return; + } + + this._nodes = new Nodes(this, backend); + this._animation = new Animation(this._nodes, this.info); + this._attributes = new Attributes(backend); + this._background = new Background(this, this._nodes); + this._geometries = new Geometries(this._attributes, this.info); + this._textures = new Textures(this, backend, this.info); + this._pipelines = new Pipelines(backend, this._nodes); + this._bindings = new Bindings( + backend, + this._nodes, + this._textures, + this._attributes, + this._pipelines, + this.info, + ); + this._objects = new RenderObjects( + this, + this._nodes, + this._geometries, + this._pipelines, + this._bindings, + this.info, + ); + this._renderLists = new RenderLists(); + this._bundles = new RenderBundles(); + this._renderContexts = new RenderContexts(); + + // + + this._initialized = true; + + resolve(); + }); + + return this._initPromise; + } + + get coordinateSystem() { + return this.backend.coordinateSystem; + } + + async compileAsync(scene, camera, targetScene = null) { + if (this._initialized === false) await this.init(); + + // preserve render tree + + const nodeFrame = this._nodes.nodeFrame; + + const previousRenderId = nodeFrame.renderId; + const previousRenderContext = this._currentRenderContext; + const previousRenderObjectFunction = this._currentRenderObjectFunction; + const previousCompilationPromises = this._compilationPromises; + + // + + const sceneRef = scene.isScene === true ? scene : _scene; + + if (targetScene === null) targetScene = scene; + + const renderTarget = this._renderTarget; + const renderContext = this._renderContexts.get(targetScene, camera, renderTarget); + const activeMipmapLevel = this._activeMipmapLevel; + + const compilationPromises = []; + + this._currentRenderContext = renderContext; + this._currentRenderObjectFunction = this.renderObject; + + this._handleObjectFunction = this._createObjectPipeline; + + this._compilationPromises = compilationPromises; + + nodeFrame.renderId++; + + // + + nodeFrame.update(); + + // + + renderContext.depth = this.depth; + renderContext.stencil = this.stencil; + + if (!renderContext.clippingContext) renderContext.clippingContext = new ClippingContext(); + renderContext.clippingContext.updateGlobal(this, camera); + + // + + sceneRef.onBeforeRender(this, scene, camera, renderTarget); + + // + + const renderList = this._renderLists.get(scene, camera); + renderList.begin(); + + this._projectObject(scene, camera, 0, renderList); + + // include lights from target scene + if (targetScene !== scene) { + targetScene.traverseVisible(function (object) { + if (object.isLight && object.layers.test(camera.layers)) { + renderList.pushLight(object); + } + }); + } + + renderList.finish(); + + // + + if (renderTarget !== null) { + this._textures.updateRenderTarget(renderTarget, activeMipmapLevel); + + const renderTargetData = this._textures.get(renderTarget); + + renderContext.textures = renderTargetData.textures; + renderContext.depthTexture = renderTargetData.depthTexture; + } else { + renderContext.textures = null; + renderContext.depthTexture = null; + } + + // + + this._nodes.updateScene(sceneRef); + + // + + this._background.update(sceneRef, renderList, renderContext); + + // process render lists + + const opaqueObjects = renderList.opaque; + const transparentObjects = renderList.transparent; + const lightsNode = renderList.lightsNode; + + if (this.opaque === true && opaqueObjects.length > 0) + this._renderObjects(opaqueObjects, camera, sceneRef, lightsNode); + if (this.transparent === true && transparentObjects.length > 0) + this._renderObjects(transparentObjects, camera, sceneRef, lightsNode); + + // restore render tree + + nodeFrame.renderId = previousRenderId; + + this._currentRenderContext = previousRenderContext; + this._currentRenderObjectFunction = previousRenderObjectFunction; + this._compilationPromises = previousCompilationPromises; + + this._handleObjectFunction = this._renderObjectDirect; + + // wait for all promises setup by backends awaiting compilation/linking/pipeline creation to complete + + await Promise.all(compilationPromises); + } + + async renderAsync(scene, camera) { + if (this._initialized === false) await this.init(); + + const renderContext = this._renderScene(scene, camera); + + await this.backend.resolveTimestampAsync(renderContext, 'render'); + } + + setMRT(mrt) { + this._mrt = mrt; + + return this; + } + + getMRT() { + return this._mrt; + } + + _renderBundle(bundle, sceneRef, lightsNode) { + const { object, camera, renderList } = bundle; + + const renderContext = this._currentRenderContext; + const renderContextData = this.backend.get(renderContext); + + // + + const renderBundle = this._bundles.get(object, camera); + + const renderBundleData = this.backend.get(renderBundle); + if (renderBundleData.renderContexts === undefined) renderBundleData.renderContexts = new Set(); + + // + + const renderBundleNeedsUpdate = + renderBundleData.renderContexts.has(renderContext) === false || object.needsUpdate === true; + + renderBundleData.renderContexts.add(renderContext); + + if (renderBundleNeedsUpdate) { + if (renderContextData.renderObjects === undefined || object.needsUpdate === true) { + const nodeFrame = this._nodes.nodeFrame; + + renderContextData.renderObjects = []; + renderContextData.renderBundles = []; + renderContextData.scene = sceneRef; + renderContextData.camera = camera; + renderContextData.renderId = nodeFrame.renderId; + + renderContextData.registerBundlesPhase = true; + } + + this._currentRenderBundle = renderBundle; + + const opaqueObjects = renderList.opaque; + + if (opaqueObjects.length > 0) this._renderObjects(opaqueObjects, camera, sceneRef, lightsNode); + + this._currentRenderBundle = null; + + // + + object.needsUpdate = false; + } else { + const renderContext = this._currentRenderContext; + const renderContextData = this.backend.get(renderContext); + + for (let i = 0, l = renderContextData.renderObjects.length; i < l; i++) { + const renderObject = renderContextData.renderObjects[i]; + + this._nodes.updateBefore(renderObject); + + // + + renderObject.object.modelViewMatrix.multiplyMatrices( + camera.matrixWorldInverse, + renderObject.object.matrixWorld, + ); + renderObject.object.normalMatrix.getNormalMatrix(renderObject.object.modelViewMatrix); + + this._nodes.updateForRender(renderObject); + this._bindings.updateForRender(renderObject); + + this.backend.draw(renderObject, this.info); + + this._nodes.updateAfter(renderObject); + } + } + } + + render(scene, camera) { + if (this._initialized === false) { + console.warn( + 'THREE.Renderer: .render() called before the backend is initialized. Try using .renderAsync() instead.', + ); + + return this.renderAsync(scene, camera); + } + + this._renderScene(scene, camera); + } + + _getFrameBufferTarget() { + const { currentColorSpace } = this; + + const useToneMapping = this._renderTarget === null && this.toneMapping !== NoToneMapping; + const useColorSpace = + this._renderTarget === null && + currentColorSpace !== LinearSRGBColorSpace && + currentColorSpace !== NoColorSpace; + + if (useToneMapping === false && useColorSpace === false) return null; + + const { width, height } = this.getDrawingBufferSize(_drawingBufferSize); + const { depth, stencil } = this; + + let frameBufferTarget = this._frameBufferTarget; + + if (frameBufferTarget === null) { + frameBufferTarget = new RenderTarget(width, height, { + depthBuffer: depth, + stencilBuffer: stencil, + type: HalfFloatType, // FloatType + format: RGBAFormat, + colorSpace: LinearSRGBColorSpace, + generateMipmaps: false, + minFilter: LinearFilter, + magFilter: LinearFilter, + samples: this.samples, + }); + + frameBufferTarget.isPostProcessingRenderTarget = true; + + this._frameBufferTarget = frameBufferTarget; + } + + frameBufferTarget.depthBuffer = depth; + frameBufferTarget.stencilBuffer = stencil; + frameBufferTarget.setSize(width, height); + frameBufferTarget.viewport.copy(this._viewport); + frameBufferTarget.scissor.copy(this._scissor); + frameBufferTarget.viewport.multiplyScalar(this._pixelRatio); + frameBufferTarget.scissor.multiplyScalar(this._pixelRatio); + frameBufferTarget.scissorTest = this._scissorTest; + + return frameBufferTarget; + } + + _renderScene(scene, camera, useFrameBufferTarget = true) { + const frameBufferTarget = useFrameBufferTarget ? this._getFrameBufferTarget() : null; + + // preserve render tree + + const nodeFrame = this._nodes.nodeFrame; + + const previousRenderId = nodeFrame.renderId; + const previousRenderContext = this._currentRenderContext; + const previousRenderObjectFunction = this._currentRenderObjectFunction; + + // + + const sceneRef = scene.isScene === true ? scene : _scene; + + const outputRenderTarget = this._renderTarget; + + const activeCubeFace = this._activeCubeFace; + const activeMipmapLevel = this._activeMipmapLevel; + + // + + let renderTarget; + + if (frameBufferTarget !== null) { + renderTarget = frameBufferTarget; + + this.setRenderTarget(renderTarget); + } else { + renderTarget = outputRenderTarget; + } + + // + + const renderContext = this._renderContexts.get(scene, camera, renderTarget); + + this._currentRenderContext = renderContext; + this._currentRenderObjectFunction = this._renderObjectFunction || this.renderObject; + + // + + this.info.calls++; + this.info.render.calls++; + this.info.render.frameCalls++; + + nodeFrame.renderId = this.info.calls; + + // + + const coordinateSystem = this.coordinateSystem; + + if (camera.coordinateSystem !== coordinateSystem) { + camera.coordinateSystem = coordinateSystem; + + camera.updateProjectionMatrix(); + } + + // + + if (scene.matrixWorldAutoUpdate === true) scene.updateMatrixWorld(); + + if (camera.parent === null && camera.matrixWorldAutoUpdate === true) camera.updateMatrixWorld(); + + // + + let viewport = this._viewport; + let scissor = this._scissor; + let pixelRatio = this._pixelRatio; + + if (renderTarget !== null) { + viewport = renderTarget.viewport; + scissor = renderTarget.scissor; + pixelRatio = 1; + } + + this.getDrawingBufferSize(_drawingBufferSize); + + _screen.set(0, 0, _drawingBufferSize.width, _drawingBufferSize.height); + + const minDepth = viewport.minDepth === undefined ? 0 : viewport.minDepth; + const maxDepth = viewport.maxDepth === undefined ? 1 : viewport.maxDepth; + + renderContext.viewportValue.copy(viewport).multiplyScalar(pixelRatio).floor(); + renderContext.viewportValue.width >>= activeMipmapLevel; + renderContext.viewportValue.height >>= activeMipmapLevel; + renderContext.viewportValue.minDepth = minDepth; + renderContext.viewportValue.maxDepth = maxDepth; + renderContext.viewport = renderContext.viewportValue.equals(_screen) === false; + + renderContext.scissorValue.copy(scissor).multiplyScalar(pixelRatio).floor(); + renderContext.scissor = this._scissorTest && renderContext.scissorValue.equals(_screen) === false; + renderContext.scissorValue.width >>= activeMipmapLevel; + renderContext.scissorValue.height >>= activeMipmapLevel; + + if (!renderContext.clippingContext) renderContext.clippingContext = new ClippingContext(); + renderContext.clippingContext.updateGlobal(this, camera); + + // + + sceneRef.onBeforeRender(this, scene, camera, renderTarget); + + // + + _projScreenMatrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse); + _frustum.setFromProjectionMatrix(_projScreenMatrix, coordinateSystem); + + const renderList = this._renderLists.get(scene, camera); + renderList.begin(); + + this._projectObject(scene, camera, 0, renderList); + + renderList.finish(); + + if (this.sortObjects === true) { + renderList.sort(this._opaqueSort, this._transparentSort); + } + + // + + if (renderTarget !== null) { + this._textures.updateRenderTarget(renderTarget, activeMipmapLevel); + + const renderTargetData = this._textures.get(renderTarget); + + renderContext.textures = renderTargetData.textures; + renderContext.depthTexture = renderTargetData.depthTexture; + renderContext.width = renderTargetData.width; + renderContext.height = renderTargetData.height; + renderContext.renderTarget = renderTarget; + renderContext.depth = renderTarget.depthBuffer; + renderContext.stencil = renderTarget.stencilBuffer; + } else { + renderContext.textures = null; + renderContext.depthTexture = null; + renderContext.width = this.domElement.width; + renderContext.height = this.domElement.height; + renderContext.depth = this.depth; + renderContext.stencil = this.stencil; + } + + renderContext.width >>= activeMipmapLevel; + renderContext.height >>= activeMipmapLevel; + renderContext.activeCubeFace = activeCubeFace; + renderContext.activeMipmapLevel = activeMipmapLevel; + renderContext.occlusionQueryCount = renderList.occlusionQueryCount; + + // + + this._nodes.updateScene(sceneRef); + + // + + this._background.update(sceneRef, renderList, renderContext); + + // + + this.backend.beginRender(renderContext); + + // process render lists + + const opaqueObjects = renderList.opaque; + const transparentObjects = renderList.transparent; + const bundles = renderList.bundles; + const lightsNode = renderList.lightsNode; + + if (bundles.length > 0) this._renderBundles(bundles, sceneRef, lightsNode); + if (this.opaque === true && opaqueObjects.length > 0) + this._renderObjects(opaqueObjects, camera, sceneRef, lightsNode); + if (this.transparent === true && transparentObjects.length > 0) + this._renderObjects(transparentObjects, camera, sceneRef, lightsNode); + + // finish render pass + + this.backend.finishRender(renderContext); + + // restore render tree + + nodeFrame.renderId = previousRenderId; + + this._currentRenderContext = previousRenderContext; + this._currentRenderObjectFunction = previousRenderObjectFunction; + + // + + if (frameBufferTarget !== null) { + this.setRenderTarget(outputRenderTarget, activeCubeFace, activeMipmapLevel); + + const quad = this._quad; + + if (this._nodes.hasOutputChange(renderTarget.texture)) { + quad.material.fragmentNode = this._nodes.getOutputNode(renderTarget.texture); + quad.material.needsUpdate = true; + } + + this._renderScene(quad, quad.camera, false); + } + + // + + sceneRef.onAfterRender(this, scene, camera, renderTarget); + + // + + return renderContext; + } + + getMaxAnisotropy() { + return this.backend.getMaxAnisotropy(); + } + + getActiveCubeFace() { + return this._activeCubeFace; + } + + getActiveMipmapLevel() { + return this._activeMipmapLevel; + } + + async setAnimationLoop(callback) { + if (this._initialized === false) await this.init(); + + this._animation.setAnimationLoop(callback); + } + + async getArrayBufferAsync(attribute) { + return await this.backend.getArrayBufferAsync(attribute); + } + + getContext() { + return this.backend.getContext(); + } + + getPixelRatio() { + return this._pixelRatio; + } + + getDrawingBufferSize(target) { + return target.set(this._width * this._pixelRatio, this._height * this._pixelRatio).floor(); + } + + getSize(target) { + return target.set(this._width, this._height); + } + + setPixelRatio(value = 1) { + this._pixelRatio = value; + + this.setSize(this._width, this._height, false); + } + + setDrawingBufferSize(width, height, pixelRatio) { + this._width = width; + this._height = height; + + this._pixelRatio = pixelRatio; + + this.domElement.width = Math.floor(width * pixelRatio); + this.domElement.height = Math.floor(height * pixelRatio); + + this.setViewport(0, 0, width, height); + + if (this._initialized) this.backend.updateSize(); + } + + setSize(width, height, updateStyle = true) { + this._width = width; + this._height = height; + + this.domElement.width = Math.floor(width * this._pixelRatio); + this.domElement.height = Math.floor(height * this._pixelRatio); + + if (updateStyle === true) { + this.domElement.style.width = width + 'px'; + this.domElement.style.height = height + 'px'; + } + + this.setViewport(0, 0, width, height); + + if (this._initialized) this.backend.updateSize(); + } + + setOpaqueSort(method) { + this._opaqueSort = method; + } + + setTransparentSort(method) { + this._transparentSort = method; + } + + getScissor(target) { + const scissor = this._scissor; + + target.x = scissor.x; + target.y = scissor.y; + target.width = scissor.width; + target.height = scissor.height; + + return target; + } + + setScissor(x, y, width, height) { + const scissor = this._scissor; + + if (x.isVector4) { + scissor.copy(x); + } else { + scissor.set(x, y, width, height); + } + } + + getScissorTest() { + return this._scissorTest; + } + + setScissorTest(boolean) { + this._scissorTest = boolean; + + this.backend.setScissorTest(boolean); + } + + getViewport(target) { + return target.copy(this._viewport); + } + + setViewport(x, y, width, height, minDepth = 0, maxDepth = 1) { + const viewport = this._viewport; + + if (x.isVector4) { + viewport.copy(x); + } else { + viewport.set(x, y, width, height); + } + + viewport.minDepth = minDepth; + viewport.maxDepth = maxDepth; + } + + getClearColor(target) { + return target.copy(this._clearColor); + } + + setClearColor(color, alpha = 1) { + this._clearColor.set(color); + this._clearColor.a = alpha; + } + + getClearAlpha() { + return this._clearColor.a; + } + + setClearAlpha(alpha) { + this._clearColor.a = alpha; + } + + getClearDepth() { + return this._clearDepth; + } + + setClearDepth(depth) { + this._clearDepth = depth; + } + + getClearStencil() { + return this._clearStencil; + } + + setClearStencil(stencil) { + this._clearStencil = stencil; + } + + isOccluded(object) { + const renderContext = this._currentRenderContext; + + return renderContext && this.backend.isOccluded(renderContext, object); + } + + clear(color = true, depth = true, stencil = true) { + if (this._initialized === false) { + console.warn( + 'THREE.Renderer: .clear() called before the backend is initialized. Try using .clearAsync() instead.', + ); + + return this.clearAsync(color, depth, stencil); + } + + const renderTarget = this._renderTarget || this._getFrameBufferTarget(); + + let renderTargetData = null; + + if (renderTarget !== null) { + this._textures.updateRenderTarget(renderTarget); + + renderTargetData = this._textures.get(renderTarget); + } + + this.backend.clear(color, depth, stencil, renderTargetData); + + if (renderTarget !== null && this._renderTarget === null) { + // If a color space transform or tone mapping is required, + // the clear operation clears the intermediate renderTarget texture, but does not update the screen canvas. + + const quad = this._quad; + + if (this._nodes.hasOutputChange(renderTarget.texture)) { + quad.material.fragmentNode = this._nodes.getOutputNode(renderTarget.texture); + quad.material.needsUpdate = true; + } + + this._renderScene(quad, quad.camera, false); + } + } + + clearColor() { + return this.clear(true, false, false); + } + + clearDepth() { + return this.clear(false, true, false); + } + + clearStencil() { + return this.clear(false, false, true); + } + + async clearAsync(color = true, depth = true, stencil = true) { + if (this._initialized === false) await this.init(); + + this.clear(color, depth, stencil); + } + + clearColorAsync() { + return this.clearAsync(true, false, false); + } + + clearDepthAsync() { + return this.clearAsync(false, true, false); + } + + clearStencilAsync() { + return this.clearAsync(false, false, true); + } + + get currentColorSpace() { + const renderTarget = this._renderTarget; + + if (renderTarget !== null) { + const texture = renderTarget.texture; + + return (Array.isArray(texture) ? texture[0] : texture).colorSpace; + } + + return this.outputColorSpace; + } + + dispose() { + this.info.dispose(); + + this._animation.dispose(); + this._objects.dispose(); + this._pipelines.dispose(); + this._nodes.dispose(); + this._bindings.dispose(); + this._renderLists.dispose(); + this._renderContexts.dispose(); + this._textures.dispose(); + + this.setRenderTarget(null); + this.setAnimationLoop(null); + } + + setRenderTarget(renderTarget, activeCubeFace = 0, activeMipmapLevel = 0) { + this._renderTarget = renderTarget; + this._activeCubeFace = activeCubeFace; + this._activeMipmapLevel = activeMipmapLevel; + } + + getRenderTarget() { + return this._renderTarget; + } + + setRenderObjectFunction(renderObjectFunction) { + this._renderObjectFunction = renderObjectFunction; + } + + getRenderObjectFunction() { + return this._renderObjectFunction; + } + + async computeAsync(computeNodes) { + if (this._initialized === false) await this.init(); + + const nodeFrame = this._nodes.nodeFrame; + + const previousRenderId = nodeFrame.renderId; + + // + + this.info.calls++; + this.info.compute.calls++; + this.info.compute.frameCalls++; + + nodeFrame.renderId = this.info.calls; + + // + + const backend = this.backend; + const pipelines = this._pipelines; + const bindings = this._bindings; + const nodes = this._nodes; + + const computeList = Array.isArray(computeNodes) ? computeNodes : [computeNodes]; + + if (computeList[0] === undefined || computeList[0].isComputeNode !== true) { + throw new Error('THREE.Renderer: .compute() expects a ComputeNode.'); + } + + backend.beginCompute(computeNodes); + + for (const computeNode of computeList) { + // onInit + + if (pipelines.has(computeNode) === false) { + const dispose = () => { + computeNode.removeEventListener('dispose', dispose); + + pipelines.delete(computeNode); + bindings.delete(computeNode); + nodes.delete(computeNode); + }; + + computeNode.addEventListener('dispose', dispose); + + // + + computeNode.onInit({ renderer: this }); + } + + nodes.updateForCompute(computeNode); + bindings.updateForCompute(computeNode); + + const computeBindings = bindings.getForCompute(computeNode); + const computePipeline = pipelines.getForCompute(computeNode, computeBindings); + + backend.compute(computeNodes, computeNode, computeBindings, computePipeline); + } + + backend.finishCompute(computeNodes); + + await this.backend.resolveTimestampAsync(computeNodes, 'compute'); + + // + + nodeFrame.renderId = previousRenderId; + } + + async hasFeatureAsync(name) { + if (this._initialized === false) await this.init(); + + return this.backend.hasFeature(name); + } + + hasFeature(name) { + if (this._initialized === false) { + console.warn( + 'THREE.Renderer: .hasFeature() called before the backend is initialized. Try using .hasFeatureAsync() instead.', + ); + + return false; + } + + return this.backend.hasFeature(name); + } + + copyFramebufferToTexture(framebufferTexture) { + const renderContext = this._currentRenderContext; + + this._textures.updateTexture(framebufferTexture); + + this.backend.copyFramebufferToTexture(framebufferTexture, renderContext); + } + + copyTextureToTexture(srcTexture, dstTexture, srcRegion = null, dstPosition = null, level = 0) { + this._textures.updateTexture(srcTexture); + this._textures.updateTexture(dstTexture); + + this.backend.copyTextureToTexture(srcTexture, dstTexture, srcRegion, dstPosition, level); + } + + readRenderTargetPixelsAsync(renderTarget, x, y, width, height, index = 0) { + return this.backend.copyTextureToBuffer(renderTarget.textures[index], x, y, width, height); + } + + _projectObject(object, camera, groupOrder, renderList) { + if (object.visible === false) return; + + const visible = object.layers.test(camera.layers); + + if (visible) { + if (object.isGroup) { + groupOrder = object.renderOrder; + } else if (object.isLOD) { + if (object.autoUpdate === true) object.update(camera); + } else if (object.isLight) { + renderList.pushLight(object); + } else if (object.isSprite) { + if (!object.frustumCulled || _frustum.intersectsSprite(object)) { + if (this.sortObjects === true) { + _vector3.setFromMatrixPosition(object.matrixWorld).applyMatrix4(_projScreenMatrix); + } + + const geometry = object.geometry; + const material = object.material; + + if (material.visible) { + renderList.push(object, geometry, material, groupOrder, _vector3.z, null); + } + } + } else if (object.isLineLoop) { + console.error( + 'THREE.Renderer: Objects of type THREE.LineLoop are not supported. Please use THREE.Line or THREE.LineSegments.', + ); + } else if (object.isMesh || object.isLine || object.isPoints) { + if (!object.frustumCulled || _frustum.intersectsObject(object)) { + const geometry = object.geometry; + const material = object.material; + + if (this.sortObjects === true) { + if (geometry.boundingSphere === null) geometry.computeBoundingSphere(); + + _vector3 + .copy(geometry.boundingSphere.center) + .applyMatrix4(object.matrixWorld) + .applyMatrix4(_projScreenMatrix); + } + + if (Array.isArray(material)) { + const groups = geometry.groups; + + for (let i = 0, l = groups.length; i < l; i++) { + const group = groups[i]; + const groupMaterial = material[group.materialIndex]; + + if (groupMaterial && groupMaterial.visible) { + renderList.push(object, geometry, groupMaterial, groupOrder, _vector3.z, group); + } + } + } else if (material.visible) { + renderList.push(object, geometry, material, groupOrder, _vector3.z, null); + } + } + } + } + + if (object.static === true) { + const baseRenderList = renderList; + + // replace render list + renderList = this._renderLists.get(object, camera); + + renderList.begin(); + + baseRenderList.pushBundle({ + object, + camera, + renderList, + }); + + renderList.finish(); + } + + const children = object.children; + + for (let i = 0, l = children.length; i < l; i++) { + this._projectObject(children[i], camera, groupOrder, renderList); + } + } + + _renderBundles(bundles, sceneRef, lightsNode) { + for (const bundle of bundles) { + this._renderBundle(bundle, sceneRef, lightsNode); + } + } + + _renderObjects(renderList, camera, scene, lightsNode) { + // process renderable objects + + for (let i = 0, il = renderList.length; i < il; i++) { + const renderItem = renderList[i]; + + // @TODO: Add support for multiple materials per object. This will require to extract + // the material from the renderItem object and pass it with its group data to renderObject(). + + const { object, geometry, material, group } = renderItem; + + if (camera.isArrayCamera) { + const cameras = camera.cameras; + + for (let j = 0, jl = cameras.length; j < jl; j++) { + const camera2 = cameras[j]; + + if (object.layers.test(camera2.layers)) { + const vp = camera2.viewport; + const minDepth = vp.minDepth === undefined ? 0 : vp.minDepth; + const maxDepth = vp.maxDepth === undefined ? 1 : vp.maxDepth; + + const viewportValue = this._currentRenderContext.viewportValue; + viewportValue.copy(vp).multiplyScalar(this._pixelRatio).floor(); + viewportValue.minDepth = minDepth; + viewportValue.maxDepth = maxDepth; + + this.backend.updateViewport(this._currentRenderContext); + + this._currentRenderObjectFunction( + object, + scene, + camera2, + geometry, + material, + group, + lightsNode, + ); + } + } + } else { + this._currentRenderObjectFunction(object, scene, camera, geometry, material, group, lightsNode); + } + } + } + + renderObject(object, scene, camera, geometry, material, group, lightsNode) { + let overridePositionNode; + let overrideFragmentNode; + let overrideDepthNode; + + // + + object.onBeforeRender(this, scene, camera, geometry, material, group); + + // + + if (scene.overrideMaterial !== null) { + const overrideMaterial = scene.overrideMaterial; + + if (material.positionNode && material.positionNode.isNode) { + overridePositionNode = overrideMaterial.positionNode; + overrideMaterial.positionNode = material.positionNode; + } + + if (overrideMaterial.isShadowNodeMaterial) { + overrideMaterial.side = material.shadowSide === null ? material.side : material.shadowSide; + + if (material.depthNode && material.depthNode.isNode) { + overrideDepthNode = overrideMaterial.depthNode; + overrideMaterial.depthNode = material.depthNode; + } + + if (material.shadowNode && material.shadowNode.isNode) { + overrideFragmentNode = overrideMaterial.fragmentNode; + overrideMaterial.fragmentNode = material.shadowNode; + } + + if (this.localClippingEnabled) { + if (material.clipShadows) { + if (overrideMaterial.clippingPlanes !== material.clippingPlanes) { + overrideMaterial.clippingPlanes = material.clippingPlanes; + overrideMaterial.needsUpdate = true; + } + + if (overrideMaterial.clipIntersection !== material.clipIntersection) { + overrideMaterial.clipIntersection = material.clipIntersection; + } + } else if (Array.isArray(overrideMaterial.clippingPlanes)) { + overrideMaterial.clippingPlanes = null; + overrideMaterial.needsUpdate = true; + } + } + } + + material = overrideMaterial; + } + + // + + if (material.transparent === true && material.side === DoubleSide && material.forceSinglePass === false) { + material.side = BackSide; + this._handleObjectFunction(object, material, scene, camera, lightsNode, group, 'backSide'); // create backSide pass id + + material.side = FrontSide; + this._handleObjectFunction(object, material, scene, camera, lightsNode, group); // use default pass id + + material.side = DoubleSide; + } else { + this._handleObjectFunction(object, material, scene, camera, lightsNode, group); + } + + // + + if (overridePositionNode !== undefined) { + scene.overrideMaterial.positionNode = overridePositionNode; + } + + if (overrideDepthNode !== undefined) { + scene.overrideMaterial.depthNode = overrideDepthNode; + } + + if (overrideFragmentNode !== undefined) { + scene.overrideMaterial.fragmentNode = overrideFragmentNode; + } + + // + + object.onAfterRender(this, scene, camera, geometry, material, group); + } + + _renderObjectDirect(object, material, scene, camera, lightsNode, group, passId) { + const renderObject = this._objects.get( + object, + material, + scene, + camera, + lightsNode, + this._currentRenderContext, + passId, + ); + renderObject.drawRange = group || object.geometry.drawRange; + + // + + this._nodes.updateBefore(renderObject); + + // + + object.modelViewMatrix.multiplyMatrices(camera.matrixWorldInverse, object.matrixWorld); + object.normalMatrix.getNormalMatrix(object.modelViewMatrix); + + // + + this._nodes.updateForRender(renderObject); + this._geometries.updateForRender(renderObject); + this._bindings.updateForRender(renderObject); + this._pipelines.updateForRender(renderObject); + + // + + if (this._currentRenderBundle !== null && this._currentRenderBundle.needsUpdate === true) { + const renderObjectData = this.backend.get(renderObject); + + renderObjectData.bundleEncoder = undefined; + renderObjectData.lastPipelineGPU = undefined; + } + + this.backend.draw(renderObject, this.info); + + if (this._currentRenderBundle !== null) { + const renderContextData = this.backend.get(this._currentRenderContext); + + renderContextData.renderObjects.push(renderObject); + } + + this._nodes.updateAfter(renderObject); + } + + _createObjectPipeline(object, material, scene, camera, lightsNode, passId) { + const renderObject = this._objects.get( + object, + material, + scene, + camera, + lightsNode, + this._currentRenderContext, + passId, + ); + + // + + this._nodes.updateBefore(renderObject); + + // + + this._nodes.updateForRender(renderObject); + this._geometries.updateForRender(renderObject); + this._bindings.updateForRender(renderObject); + + this._pipelines.getForRender(renderObject, this._compilationPromises); + + this._nodes.updateAfter(renderObject); + } + + get compute() { + return this.computeAsync; + } + + get compile() { + return this.compileAsync; + } +} + +export default Renderer; diff --git a/src-testing/src/renderers/common/SampledTexture.ts b/src-testing/src/renderers/common/SampledTexture.ts new file mode 100644 index 000000000..c671cb09a --- /dev/null +++ b/src-testing/src/renderers/common/SampledTexture.ts @@ -0,0 +1,61 @@ +import Binding from './Binding.js'; + +let _id = 0; + +class SampledTexture extends Binding { + constructor(name, texture) { + super(name); + + this.id = _id++; + + this.texture = texture; + this.version = texture ? texture.version : 0; + this.store = false; + + this.isSampledTexture = true; + } + + get needsBindingsUpdate() { + const { texture, version } = this; + + return texture.isVideoTexture ? true : version !== texture.version; // @TODO: version === 0 && texture.version > 0 ( add it just to External Textures like PNG,JPG ) + } + + update() { + const { texture, version } = this; + + if (version !== texture.version) { + this.version = texture.version; + + return true; + } + + return false; + } +} + +class SampledArrayTexture extends SampledTexture { + constructor(name, texture) { + super(name, texture); + + this.isSampledArrayTexture = true; + } +} + +class Sampled3DTexture extends SampledTexture { + constructor(name, texture) { + super(name, texture); + + this.isSampled3DTexture = true; + } +} + +class SampledCubeTexture extends SampledTexture { + constructor(name, texture) { + super(name, texture); + + this.isSampledCubeTexture = true; + } +} + +export { SampledTexture, SampledArrayTexture, Sampled3DTexture, SampledCubeTexture }; diff --git a/src-testing/src/renderers/common/Sampler.ts b/src-testing/src/renderers/common/Sampler.ts new file mode 100644 index 000000000..8cd20d04a --- /dev/null +++ b/src-testing/src/renderers/common/Sampler.ts @@ -0,0 +1,14 @@ +import Binding from './Binding.js'; + +class Sampler extends Binding { + constructor(name, texture) { + super(name); + + this.texture = texture; + this.version = texture ? texture.version : 0; + + this.isSampler = true; + } +} + +export default Sampler; diff --git a/src-testing/src/renderers/common/StorageBuffer.ts b/src-testing/src/renderers/common/StorageBuffer.ts new file mode 100644 index 000000000..ef5d3e464 --- /dev/null +++ b/src-testing/src/renderers/common/StorageBuffer.ts @@ -0,0 +1,13 @@ +import Buffer from './Buffer.js'; + +class StorageBuffer extends Buffer { + constructor(name, attribute) { + super(name, attribute ? attribute.array : null); + + this.attribute = attribute; + + this.isStorageBuffer = true; + } +} + +export default StorageBuffer; diff --git a/src-testing/src/renderers/common/StorageBufferAttribute.d.ts b/src-testing/src/renderers/common/StorageBufferAttribute.d.ts new file mode 100644 index 000000000..2a864f54a --- /dev/null +++ b/src-testing/src/renderers/common/StorageBufferAttribute.d.ts @@ -0,0 +1,7 @@ +import { BufferAttribute, TypedArray } from "../../core/BufferAttribute.js"; + +export default class StorageBufferAttribute extends BufferAttribute { + readonly isStorageBufferAttribute: true; + + constructor(array: TypedArray, itemSize: number); +} diff --git a/src-testing/src/renderers/common/StorageInstancedBufferAttribute.d.ts b/src-testing/src/renderers/common/StorageInstancedBufferAttribute.d.ts new file mode 100644 index 000000000..3f01891e8 --- /dev/null +++ b/src-testing/src/renderers/common/StorageInstancedBufferAttribute.d.ts @@ -0,0 +1,8 @@ +import { TypedArray } from "../../core/BufferAttribute.js"; +import { InstancedBufferAttribute } from "../../core/InstancedBufferAttribute.js"; + +export default class StorageInstancedBufferAttribute extends InstancedBufferAttribute { + readonly isStorageInstancedBufferAttribute: true; + + constructor(array: TypedArray | number, itemSize: number); +} diff --git a/src-testing/src/renderers/common/StorageTexture.d.ts b/src-testing/src/renderers/common/StorageTexture.d.ts new file mode 100644 index 000000000..435ef9ba3 --- /dev/null +++ b/src-testing/src/renderers/common/StorageTexture.d.ts @@ -0,0 +1,5 @@ +import { Texture } from "../../textures/Texture.js"; + +export default class StorageTexture extends Texture { + constructor(width?: number, height?: number); +} diff --git a/src-testing/src/renderers/common/Textures.ts b/src-testing/src/renderers/common/Textures.ts new file mode 100644 index 000000000..57507087a --- /dev/null +++ b/src-testing/src/renderers/common/Textures.ts @@ -0,0 +1,290 @@ +import DataMap from './DataMap.js'; + +import { Vector3 } from '../../math/Vector3.js'; +import { DepthTexture } from '../../textures/DepthTexture.js'; +import { + DepthStencilFormat, + DepthFormat, + UnsignedIntType, + UnsignedInt248Type, + LinearFilter, + NearestFilter, + EquirectangularReflectionMapping, + EquirectangularRefractionMapping, + CubeReflectionMapping, + CubeRefractionMapping, + UnsignedByteType, +} from '../../constants.js'; + +const _size = /*@__PURE__*/ new Vector3(); + +class Textures extends DataMap { + constructor(renderer, backend, info) { + super(); + + this.renderer = renderer; + this.backend = backend; + this.info = info; + } + + updateRenderTarget(renderTarget, activeMipmapLevel = 0) { + const renderTargetData = this.get(renderTarget); + + const sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples; + const depthTextureMips = renderTargetData.depthTextureMips || (renderTargetData.depthTextureMips = {}); + + const texture = renderTarget.texture; + const textures = renderTarget.textures; + + const size = this.getSize(texture); + + const mipWidth = size.width >> activeMipmapLevel; + const mipHeight = size.height >> activeMipmapLevel; + + let depthTexture = renderTarget.depthTexture || depthTextureMips[activeMipmapLevel]; + let textureNeedsUpdate = false; + + if (depthTexture === undefined) { + depthTexture = new DepthTexture(); + depthTexture.format = renderTarget.stencilBuffer ? DepthStencilFormat : DepthFormat; + depthTexture.type = renderTarget.stencilBuffer ? UnsignedInt248Type : UnsignedIntType; // FloatType + depthTexture.image.width = mipWidth; + depthTexture.image.height = mipHeight; + + depthTextureMips[activeMipmapLevel] = depthTexture; + } + + if (renderTargetData.width !== size.width || size.height !== renderTargetData.height) { + textureNeedsUpdate = true; + depthTexture.needsUpdate = true; + + depthTexture.image.width = mipWidth; + depthTexture.image.height = mipHeight; + } + + renderTargetData.width = size.width; + renderTargetData.height = size.height; + renderTargetData.textures = textures; + renderTargetData.depthTexture = depthTexture; + renderTargetData.depth = renderTarget.depthBuffer; + renderTargetData.stencil = renderTarget.stencilBuffer; + renderTargetData.renderTarget = renderTarget; + + if (renderTargetData.sampleCount !== sampleCount) { + textureNeedsUpdate = true; + depthTexture.needsUpdate = true; + + renderTargetData.sampleCount = sampleCount; + } + + // + + const options = { sampleCount }; + + for (let i = 0; i < textures.length; i++) { + const texture = textures[i]; + + if (textureNeedsUpdate) texture.needsUpdate = true; + + this.updateTexture(texture, options); + } + + this.updateTexture(depthTexture, options); + + // dispose handler + + if (renderTargetData.initialized !== true) { + renderTargetData.initialized = true; + + // dispose + + const onDispose = () => { + renderTarget.removeEventListener('dispose', onDispose); + + if (textures !== undefined) { + for (let i = 0; i < textures.length; i++) { + this._destroyTexture(textures[i]); + } + } else { + this._destroyTexture(texture); + } + + this._destroyTexture(depthTexture); + + this.delete(renderTarget); + }; + + renderTarget.addEventListener('dispose', onDispose); + } + } + + updateTexture(texture, options = {}) { + const textureData = this.get(texture); + if (textureData.initialized === true && textureData.version === texture.version) return; + + const isRenderTarget = texture.isRenderTargetTexture || texture.isDepthTexture || texture.isFramebufferTexture; + const backend = this.backend; + + if (isRenderTarget && textureData.initialized === true) { + // it's an update + + backend.destroySampler(texture); + backend.destroyTexture(texture); + } + + // + + if (texture.isFramebufferTexture) { + const renderer = this.renderer; + const renderTarget = renderer.getRenderTarget(); + + if (renderTarget) { + texture.type = renderTarget.texture.type; + } else { + texture.type = UnsignedByteType; + } + } + + // + + const { width, height, depth } = this.getSize(texture); + + options.width = width; + options.height = height; + options.depth = depth; + options.needsMipmaps = this.needsMipmaps(texture); + options.levels = options.needsMipmaps ? this.getMipLevels(texture, width, height) : 1; + + // + + if (isRenderTarget || texture.isStorageTexture === true) { + backend.createSampler(texture); + backend.createTexture(texture, options); + } else { + const needsCreate = textureData.initialized !== true; + + if (needsCreate) backend.createSampler(texture); + + if (texture.version > 0) { + const image = texture.image; + + if (image === undefined) { + console.warn('THREE.Renderer: Texture marked for update but image is undefined.'); + } else if (image.complete === false) { + console.warn('THREE.Renderer: Texture marked for update but image is incomplete.'); + } else { + if (texture.images) { + const images = []; + + for (const image of texture.images) { + images.push(image); + } + + options.images = images; + } else { + options.image = image; + } + + if (textureData.isDefaultTexture === undefined || textureData.isDefaultTexture === true) { + backend.createTexture(texture, options); + + textureData.isDefaultTexture = false; + } + + if (texture.source.dataReady === true) backend.updateTexture(texture, options); + + if (options.needsMipmaps && texture.mipmaps.length === 0) backend.generateMipmaps(texture); + } + } else { + // async update + + backend.createDefaultTexture(texture); + + textureData.isDefaultTexture = true; + } + } + + // dispose handler + + if (textureData.initialized !== true) { + textureData.initialized = true; + + // + + this.info.memory.textures++; + + // dispose + + const onDispose = () => { + texture.removeEventListener('dispose', onDispose); + + this._destroyTexture(texture); + + this.info.memory.textures--; + }; + + texture.addEventListener('dispose', onDispose); + } + + // + + textureData.version = texture.version; + } + + getSize(texture, target = _size) { + let image = texture.images ? texture.images[0] : texture.image; + + if (image) { + if (image.image !== undefined) image = image.image; + + target.width = image.width; + target.height = image.height; + target.depth = texture.isCubeTexture ? 6 : image.depth || 1; + } else { + target.width = target.height = target.depth = 1; + } + + return target; + } + + getMipLevels(texture, width, height) { + let mipLevelCount; + + if (texture.isCompressedTexture) { + mipLevelCount = texture.mipmaps.length; + } else { + mipLevelCount = Math.floor(Math.log2(Math.max(width, height))) + 1; + } + + return mipLevelCount; + } + + needsMipmaps(texture) { + if (this.isEnvironmentTexture(texture)) return true; + + return ( + texture.isCompressedTexture === true || + (texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter) + ); + } + + isEnvironmentTexture(texture) { + const mapping = texture.mapping; + + return ( + mapping === EquirectangularReflectionMapping || + mapping === EquirectangularRefractionMapping || + mapping === CubeReflectionMapping || + mapping === CubeRefractionMapping + ); + } + + _destroyTexture(texture) { + this.backend.destroySampler(texture); + this.backend.destroyTexture(texture); + + this.delete(texture); + } +} + +export default Textures; diff --git a/src-testing/src/renderers/common/Uniform.ts b/src-testing/src/renderers/common/Uniform.ts new file mode 100644 index 000000000..80c131494 --- /dev/null +++ b/src-testing/src/renderers/common/Uniform.ts @@ -0,0 +1,105 @@ +import { Color } from '../../math/Color.js'; +import { Matrix3 } from '../../math/Matrix3.js'; +import { Matrix4 } from '../../math/Matrix4.js'; +import { Vector2 } from '../../math/Vector2.js'; +import { Vector3 } from '../../math/Vector3.js'; +import { Vector4 } from '../../math/Vector4.js'; + +class Uniform { + constructor(name, value) { + this.name = name; + this.value = value; + + this.boundary = 0; // used to build the uniform buffer according to the STD140 layout + this.itemSize = 0; + + this.offset = 0; // this property is set by WebGPUUniformsGroup and marks the start position in the uniform buffer + } + + setValue(value) { + this.value = value; + } + + getValue() { + return this.value; + } +} + +class NumberUniform extends Uniform { + constructor(name, value = 0) { + super(name, value); + + this.isNumberUniform = true; + + this.boundary = 4; + this.itemSize = 1; + } +} + +class Vector2Uniform extends Uniform { + constructor(name, value = new Vector2()) { + super(name, value); + + this.isVector2Uniform = true; + + this.boundary = 8; + this.itemSize = 2; + } +} + +class Vector3Uniform extends Uniform { + constructor(name, value = new Vector3()) { + super(name, value); + + this.isVector3Uniform = true; + + this.boundary = 16; + this.itemSize = 3; + } +} + +class Vector4Uniform extends Uniform { + constructor(name, value = new Vector4()) { + super(name, value); + + this.isVector4Uniform = true; + + this.boundary = 16; + this.itemSize = 4; + } +} + +class ColorUniform extends Uniform { + constructor(name, value = new Color()) { + super(name, value); + + this.isColorUniform = true; + + this.boundary = 16; + this.itemSize = 3; + } +} + +class Matrix3Uniform extends Uniform { + constructor(name, value = new Matrix3()) { + super(name, value); + + this.isMatrix3Uniform = true; + + this.boundary = 48; + this.itemSize = 12; + } +} + +class Matrix4Uniform extends Uniform { + constructor(name, value = new Matrix4()) { + super(name, value); + + this.isMatrix4Uniform = true; + + this.boundary = 64; + this.itemSize = 16; + } +} + +export { NumberUniform, Vector2Uniform, Vector3Uniform, Vector4Uniform, ColorUniform, Matrix3Uniform, Matrix4Uniform }; diff --git a/src-testing/src/renderers/common/UniformBuffer.ts b/src-testing/src/renderers/common/UniformBuffer.ts new file mode 100644 index 000000000..28aac0d7e --- /dev/null +++ b/src-testing/src/renderers/common/UniformBuffer.ts @@ -0,0 +1,11 @@ +import Buffer from './Buffer.js'; + +class UniformBuffer extends Buffer { + constructor(name, buffer = null) { + super(name, buffer); + + this.isUniformBuffer = true; + } +} + +export default UniformBuffer; diff --git a/src-testing/src/renderers/common/UniformsGroup.ts b/src-testing/src/renderers/common/UniformsGroup.ts new file mode 100644 index 000000000..e2b62671a --- /dev/null +++ b/src-testing/src/renderers/common/UniformsGroup.ts @@ -0,0 +1,277 @@ +import UniformBuffer from './UniformBuffer.js'; +import { GPU_CHUNK_BYTES } from './Constants.js'; + +class UniformsGroup extends UniformBuffer { + constructor(name) { + super(name); + + this.isUniformsGroup = true; + + this._values = null; + + // the order of uniforms in this array must match the order of uniforms in the shader + + this.uniforms = []; + } + + addUniform(uniform) { + this.uniforms.push(uniform); + + return this; + } + + removeUniform(uniform) { + const index = this.uniforms.indexOf(uniform); + + if (index !== -1) { + this.uniforms.splice(index, 1); + } + + return this; + } + + get values() { + if (this._values === null) { + this._values = Array.from(this.buffer); + } + + return this._values; + } + + get buffer() { + let buffer = this._buffer; + + if (buffer === null) { + const byteLength = this.byteLength; + + buffer = new Float32Array(new ArrayBuffer(byteLength)); + + this._buffer = buffer; + } + + return buffer; + } + + get byteLength() { + let offset = 0; // global buffer offset in bytes + + for (let i = 0, l = this.uniforms.length; i < l; i++) { + const uniform = this.uniforms[i]; + + const { boundary, itemSize } = uniform; + + // offset within a single chunk in bytes + + const chunkOffset = offset % GPU_CHUNK_BYTES; + const remainingSizeInChunk = GPU_CHUNK_BYTES - chunkOffset; + + // conformance tests + + if (chunkOffset !== 0 && remainingSizeInChunk - boundary < 0) { + // check for chunk overflow + + offset += GPU_CHUNK_BYTES - chunkOffset; + } else if (chunkOffset % boundary !== 0) { + // check for correct alignment + + offset += chunkOffset % boundary; + } + + uniform.offset = offset / this.bytesPerElement; + + offset += itemSize * this.bytesPerElement; + } + + return Math.ceil(offset / GPU_CHUNK_BYTES) * GPU_CHUNK_BYTES; + } + + update() { + let updated = false; + + for (const uniform of this.uniforms) { + if (this.updateByType(uniform) === true) { + updated = true; + } + } + + return updated; + } + + updateByType(uniform) { + if (uniform.isNumberUniform) return this.updateNumber(uniform); + if (uniform.isVector2Uniform) return this.updateVector2(uniform); + if (uniform.isVector3Uniform) return this.updateVector3(uniform); + if (uniform.isVector4Uniform) return this.updateVector4(uniform); + if (uniform.isColorUniform) return this.updateColor(uniform); + if (uniform.isMatrix3Uniform) return this.updateMatrix3(uniform); + if (uniform.isMatrix4Uniform) return this.updateMatrix4(uniform); + + console.error('THREE.WebGPUUniformsGroup: Unsupported uniform type.', uniform); + } + + updateNumber(uniform) { + let updated = false; + + const a = this.values; + const v = uniform.getValue(); + const offset = uniform.offset; + + if (a[offset] !== v) { + const b = this.buffer; + + b[offset] = a[offset] = v; + updated = true; + } + + return updated; + } + + updateVector2(uniform) { + let updated = false; + + const a = this.values; + const v = uniform.getValue(); + const offset = uniform.offset; + + if (a[offset + 0] !== v.x || a[offset + 1] !== v.y) { + const b = this.buffer; + + b[offset + 0] = a[offset + 0] = v.x; + b[offset + 1] = a[offset + 1] = v.y; + + updated = true; + } + + return updated; + } + + updateVector3(uniform) { + let updated = false; + + const a = this.values; + const v = uniform.getValue(); + const offset = uniform.offset; + + if (a[offset + 0] !== v.x || a[offset + 1] !== v.y || a[offset + 2] !== v.z) { + const b = this.buffer; + + b[offset + 0] = a[offset + 0] = v.x; + b[offset + 1] = a[offset + 1] = v.y; + b[offset + 2] = a[offset + 2] = v.z; + + updated = true; + } + + return updated; + } + + updateVector4(uniform) { + let updated = false; + + const a = this.values; + const v = uniform.getValue(); + const offset = uniform.offset; + + if (a[offset + 0] !== v.x || a[offset + 1] !== v.y || a[offset + 2] !== v.z || a[offset + 4] !== v.w) { + const b = this.buffer; + + b[offset + 0] = a[offset + 0] = v.x; + b[offset + 1] = a[offset + 1] = v.y; + b[offset + 2] = a[offset + 2] = v.z; + b[offset + 3] = a[offset + 3] = v.w; + + updated = true; + } + + return updated; + } + + updateColor(uniform) { + let updated = false; + + const a = this.values; + const c = uniform.getValue(); + const offset = uniform.offset; + + if (a[offset + 0] !== c.r || a[offset + 1] !== c.g || a[offset + 2] !== c.b) { + const b = this.buffer; + + b[offset + 0] = a[offset + 0] = c.r; + b[offset + 1] = a[offset + 1] = c.g; + b[offset + 2] = a[offset + 2] = c.b; + + updated = true; + } + + return updated; + } + + updateMatrix3(uniform) { + let updated = false; + + const a = this.values; + const e = uniform.getValue().elements; + const offset = uniform.offset; + + if ( + a[offset + 0] !== e[0] || + a[offset + 1] !== e[1] || + a[offset + 2] !== e[2] || + a[offset + 4] !== e[3] || + a[offset + 5] !== e[4] || + a[offset + 6] !== e[5] || + a[offset + 8] !== e[6] || + a[offset + 9] !== e[7] || + a[offset + 10] !== e[8] + ) { + const b = this.buffer; + + b[offset + 0] = a[offset + 0] = e[0]; + b[offset + 1] = a[offset + 1] = e[1]; + b[offset + 2] = a[offset + 2] = e[2]; + b[offset + 4] = a[offset + 4] = e[3]; + b[offset + 5] = a[offset + 5] = e[4]; + b[offset + 6] = a[offset + 6] = e[5]; + b[offset + 8] = a[offset + 8] = e[6]; + b[offset + 9] = a[offset + 9] = e[7]; + b[offset + 10] = a[offset + 10] = e[8]; + + updated = true; + } + + return updated; + } + + updateMatrix4(uniform) { + let updated = false; + + const a = this.values; + const e = uniform.getValue().elements; + const offset = uniform.offset; + + if (arraysEqual(a, e, offset) === false) { + const b = this.buffer; + b.set(e, offset); + setArray(a, e, offset); + updated = true; + } + + return updated; + } +} + +function setArray(a, b, offset) { + for (let i = 0, l = b.length; i < l; i++) { + a[offset + i] = b[i]; + } +} + +function arraysEqual(a, b, offset) { + for (let i = 0, l = b.length; i < l; i++) { + if (a[offset + i] !== b[i]) return false; + } + + return true; +} + +export default UniformsGroup; diff --git a/src-testing/src/renderers/common/extras/PMREMGenerator.ts b/src-testing/src/renderers/common/extras/PMREMGenerator.ts new file mode 100644 index 000000000..e45588271 --- /dev/null +++ b/src-testing/src/renderers/common/extras/PMREMGenerator.ts @@ -0,0 +1,660 @@ +import NodeMaterial from '../../../nodes/materials/NodeMaterial.js'; +import { getDirection, blur } from '../../../nodes/pmrem/PMREMUtils.js'; +import { equirectUV } from '../../../nodes/utils/EquirectUVNode.js'; +import { uniform } from '../../../nodes/core/UniformNode.js'; +import { uniformArray } from '../../../nodes/accessors/UniformArrayNode.js'; +import { texture } from '../../../nodes/accessors/TextureNode.js'; +import { cubeTexture } from '../../../nodes/accessors/CubeTextureNode.js'; +import { float, vec3 } from '../../../nodes/shadernode/ShaderNode.js'; +import { uv } from '../../../nodes/accessors/UVNode.js'; +import { attribute } from '../../../nodes/core/AttributeNode.js'; + +import { OrthographicCamera } from '../../../cameras/OrthographicCamera.js'; +import { Color } from '../../../math/Color.js'; +import { Vector3 } from '../../../math/Vector3.js'; +import { BufferGeometry } from '../../../core/BufferGeometry.js'; +import { BufferAttribute } from '../../../core/BufferAttribute.js'; +import { RenderTarget } from '../../../core/RenderTarget.js'; +import { Mesh } from '../../../objects/Mesh.js'; +import { PerspectiveCamera } from '../../../cameras/PerspectiveCamera.js'; +import { MeshBasicMaterial } from '../../../materials/MeshBasicMaterial.js'; +import { BoxGeometry } from '../../../geometries/BoxGeometry.js'; +import { + CubeReflectionMapping, + CubeRefractionMapping, + CubeUVReflectionMapping, + LinearFilter, + NoBlending, + RGBAFormat, + HalfFloatType, + BackSide, + LinearSRGBColorSpace, +} from '../../../constants.js'; + +const LOD_MIN = 4; + +// The standard deviations (radians) associated with the extra mips. These are +// chosen to approximate a Trowbridge-Reitz distribution function times the +// geometric shadowing function. These sigma values squared must match the +// variance #defines in cube_uv_reflection_fragment.glsl.js. +const EXTRA_LOD_SIGMA = [0.125, 0.215, 0.35, 0.446, 0.526, 0.582]; + +// The maximum length of the blur for loop. Smaller sigmas will use fewer +// samples and exit early, but not recompile the shader. +const MAX_SAMPLES = 20; + +const _flatCamera = /*@__PURE__*/ new OrthographicCamera(-1, 1, 1, -1, 0, 1); +const _cubeCamera = /*@__PURE__*/ new PerspectiveCamera(90, 1); +const _clearColor = /*@__PURE__*/ new Color(); +let _oldTarget = null; +let _oldActiveCubeFace = 0; +let _oldActiveMipmapLevel = 0; + +// Golden Ratio +const PHI = (1 + Math.sqrt(5)) / 2; +const INV_PHI = 1 / PHI; + +// Vertices of a dodecahedron (except the opposites, which represent the +// same axis), used as axis directions evenly spread on a sphere. +const _axisDirections = [ + /*@__PURE__*/ new Vector3(-PHI, INV_PHI, 0), + /*@__PURE__*/ new Vector3(PHI, INV_PHI, 0), + /*@__PURE__*/ new Vector3(-INV_PHI, 0, PHI), + /*@__PURE__*/ new Vector3(INV_PHI, 0, PHI), + /*@__PURE__*/ new Vector3(0, PHI, -INV_PHI), + /*@__PURE__*/ new Vector3(0, PHI, INV_PHI), + /*@__PURE__*/ new Vector3(-1, 1, -1), + /*@__PURE__*/ new Vector3(1, 1, -1), + /*@__PURE__*/ new Vector3(-1, 1, 1), + /*@__PURE__*/ new Vector3(1, 1, 1), +]; + +// + +// WebGPU Face indices +const _faceLib = [3, 1, 5, 0, 4, 2]; + +const direction = getDirection(uv(), attribute('faceIndex')).normalize(); +const outputDirection = vec3(direction.x, direction.y.negate(), direction.z); + +/** + * This class generates a Prefiltered, Mipmapped Radiance Environment Map + * (PMREM) from a cubeMap environment texture. This allows different levels of + * blur to be quickly accessed based on material roughness. It is packed into a + * special CubeUV format that allows us to perform custom interpolation so that + * we can support nonlinear formats such as RGBE. Unlike a traditional mipmap + * chain, it only goes down to the LOD_MIN level (above), and then creates extra + * even more filtered 'mips' at the same LOD_MIN resolution, associated with + * higher roughness levels. In this way we maintain resolution to smoothly + * interpolate diffuse lighting while limiting sampling computation. + * + * Paper: Fast, Accurate Image-Based Lighting + * https://drive.google.com/file/d/15y8r_UpKlU9SvV4ILb0C3qCPecS8pvLz/view + */ + +class PMREMGenerator { + constructor(renderer) { + this._renderer = renderer; + this._pingPongRenderTarget = null; + + this._lodMax = 0; + this._cubeSize = 0; + this._lodPlanes = []; + this._sizeLods = []; + this._sigmas = []; + this._lodMeshes = []; + + this._blurMaterial = null; + this._cubemapMaterial = null; + this._equirectMaterial = null; + this._backgroundBox = null; + } + + /** + * Generates a PMREM from a supplied Scene, which can be faster than using an + * image if networking bandwidth is low. Optional sigma specifies a blur radius + * in radians to be applied to the scene before PMREM generation. Optional near + * and far planes ensure the scene is rendered in its entirety (the cubeCamera + * is placed at the origin). + */ + fromScene(scene, sigma = 0, near = 0.1, far = 100) { + _oldTarget = this._renderer.getRenderTarget(); + _oldActiveCubeFace = this._renderer.getActiveCubeFace(); + _oldActiveMipmapLevel = this._renderer.getActiveMipmapLevel(); + + this._setSize(256); + + const cubeUVRenderTarget = this._allocateTargets(); + cubeUVRenderTarget.depthBuffer = true; + + this._sceneToCubeUV(scene, near, far, cubeUVRenderTarget); + + if (sigma > 0) { + this._blur(cubeUVRenderTarget, 0, 0, sigma); + } + + this._applyPMREM(cubeUVRenderTarget); + + this._cleanup(cubeUVRenderTarget); + + return cubeUVRenderTarget; + } + + /** + * Generates a PMREM from an equirectangular texture, which can be either LDR + * or HDR. The ideal input image size is 1k (1024 x 512), + * as this matches best with the 256 x 256 cubemap output. + */ + fromEquirectangular(equirectangular, renderTarget = null) { + return this._fromTexture(equirectangular, renderTarget); + } + + /** + * Generates a PMREM from an cubemap texture, which can be either LDR + * or HDR. The ideal input cube size is 256 x 256, + * as this matches best with the 256 x 256 cubemap output. + */ + fromCubemap(cubemap, renderTarget = null) { + return this._fromTexture(cubemap, renderTarget); + } + + /** + * Pre-compiles the cubemap shader. You can get faster start-up by invoking this method during + * your texture's network fetch for increased concurrency. + */ + compileCubemapShader() { + if (this._cubemapMaterial === null) { + this._cubemapMaterial = _getCubemapMaterial(); + this._compileMaterial(this._cubemapMaterial); + } + } + + /** + * Pre-compiles the equirectangular shader. You can get faster start-up by invoking this method during + * your texture's network fetch for increased concurrency. + */ + compileEquirectangularShader() { + if (this._equirectMaterial === null) { + this._equirectMaterial = _getEquirectMaterial(); + this._compileMaterial(this._equirectMaterial); + } + } + + /** + * Disposes of the PMREMGenerator's internal memory. Note that PMREMGenerator is a static class, + * so you should not need more than one PMREMGenerator object. If you do, calling dispose() on + * one of them will cause any others to also become unusable. + */ + dispose() { + this._dispose(); + + if (this._cubemapMaterial !== null) this._cubemapMaterial.dispose(); + if (this._equirectMaterial !== null) this._equirectMaterial.dispose(); + if (this._backgroundBox !== null) { + this._backgroundBox.geometry.dispose(); + this._backgroundBox.material.dispose(); + } + } + + // private interface + + _setSize(cubeSize) { + this._lodMax = Math.floor(Math.log2(cubeSize)); + this._cubeSize = Math.pow(2, this._lodMax); + } + + _dispose() { + if (this._blurMaterial !== null) this._blurMaterial.dispose(); + + if (this._pingPongRenderTarget !== null) this._pingPongRenderTarget.dispose(); + + for (let i = 0; i < this._lodPlanes.length; i++) { + this._lodPlanes[i].dispose(); + } + } + + _cleanup(outputTarget) { + this._renderer.setRenderTarget(_oldTarget, _oldActiveCubeFace, _oldActiveMipmapLevel); + outputTarget.scissorTest = false; + _setViewport(outputTarget, 0, 0, outputTarget.width, outputTarget.height); + } + + _fromTexture(texture, renderTarget) { + if (texture.mapping === CubeReflectionMapping || texture.mapping === CubeRefractionMapping) { + this._setSize(texture.image.length === 0 ? 16 : texture.image[0].width || texture.image[0].image.width); + } else { + // Equirectangular + + this._setSize(texture.image.width / 4); + } + + _oldTarget = this._renderer.getRenderTarget(); + _oldActiveCubeFace = this._renderer.getActiveCubeFace(); + _oldActiveMipmapLevel = this._renderer.getActiveMipmapLevel(); + + const cubeUVRenderTarget = renderTarget || this._allocateTargets(); + this._textureToCubeUV(texture, cubeUVRenderTarget); + this._applyPMREM(cubeUVRenderTarget); + this._cleanup(cubeUVRenderTarget); + + return cubeUVRenderTarget; + } + + _allocateTargets() { + const width = 3 * Math.max(this._cubeSize, 16 * 7); + const height = 4 * this._cubeSize; + + const params = { + magFilter: LinearFilter, + minFilter: LinearFilter, + generateMipmaps: false, + type: HalfFloatType, + format: RGBAFormat, + colorSpace: LinearSRGBColorSpace, + //depthBuffer: false + }; + + const cubeUVRenderTarget = _createRenderTarget(width, height, params); + + if ( + this._pingPongRenderTarget === null || + this._pingPongRenderTarget.width !== width || + this._pingPongRenderTarget.height !== height + ) { + if (this._pingPongRenderTarget !== null) { + this._dispose(); + } + + this._pingPongRenderTarget = _createRenderTarget(width, height, params); + + const { _lodMax } = this; + ({ + sizeLods: this._sizeLods, + lodPlanes: this._lodPlanes, + sigmas: this._sigmas, + lodMeshes: this._lodMeshes, + } = _createPlanes(_lodMax)); + + this._blurMaterial = _getBlurShader(_lodMax, width, height); + } + + return cubeUVRenderTarget; + } + + _compileMaterial(material) { + const tmpMesh = this._lodMeshes[0]; + tmpMesh.material = material; + + this._renderer.compile(tmpMesh, _flatCamera); + } + + _sceneToCubeUV(scene, near, far, cubeUVRenderTarget) { + const cubeCamera = _cubeCamera; + cubeCamera.near = near; + cubeCamera.far = far; + + // px, py, pz, nx, ny, nz + const upSign = [-1, 1, -1, -1, -1, -1]; + const forwardSign = [1, 1, 1, -1, -1, -1]; + + const renderer = this._renderer; + + const originalAutoClear = renderer.autoClear; + + renderer.getClearColor(_clearColor); + + renderer.autoClear = false; + + let backgroundBox = this._backgroundBox; + + if (backgroundBox === null) { + const backgroundMaterial = new MeshBasicMaterial({ + name: 'PMREM.Background', + side: BackSide, + depthWrite: false, + depthTest: false, + }); + + backgroundBox = new Mesh(new BoxGeometry(), backgroundMaterial); + } + + let useSolidColor = false; + const background = scene.background; + + if (background) { + if (background.isColor) { + backgroundBox.material.color.copy(background); + scene.background = null; + useSolidColor = true; + } + } else { + backgroundBox.material.color.copy(_clearColor); + useSolidColor = true; + } + + renderer.setRenderTarget(cubeUVRenderTarget); + + renderer.clear(); + + if (useSolidColor) { + renderer.render(backgroundBox, cubeCamera); + } + + for (let i = 0; i < 6; i++) { + const col = i % 3; + + if (col === 0) { + cubeCamera.up.set(0, upSign[i], 0); + cubeCamera.lookAt(forwardSign[i], 0, 0); + } else if (col === 1) { + cubeCamera.up.set(0, 0, upSign[i]); + cubeCamera.lookAt(0, forwardSign[i], 0); + } else { + cubeCamera.up.set(0, upSign[i], 0); + cubeCamera.lookAt(0, 0, forwardSign[i]); + } + + const size = this._cubeSize; + + _setViewport(cubeUVRenderTarget, col * size, i > 2 ? size : 0, size, size); + + renderer.render(scene, cubeCamera); + } + + renderer.autoClear = originalAutoClear; + scene.background = background; + } + + _textureToCubeUV(texture, cubeUVRenderTarget) { + const renderer = this._renderer; + + const isCubeTexture = texture.mapping === CubeReflectionMapping || texture.mapping === CubeRefractionMapping; + + if (isCubeTexture) { + if (this._cubemapMaterial === null) { + this._cubemapMaterial = _getCubemapMaterial(texture); + } + } else { + if (this._equirectMaterial === null) { + this._equirectMaterial = _getEquirectMaterial(texture); + } + } + + const material = isCubeTexture ? this._cubemapMaterial : this._equirectMaterial; + material.fragmentNode.value = texture; + + const mesh = this._lodMeshes[0]; + mesh.material = material; + + const size = this._cubeSize; + + _setViewport(cubeUVRenderTarget, 0, 0, 3 * size, 2 * size); + + renderer.setRenderTarget(cubeUVRenderTarget); + renderer.render(mesh, _flatCamera); + } + + _applyPMREM(cubeUVRenderTarget) { + const renderer = this._renderer; + const autoClear = renderer.autoClear; + renderer.autoClear = false; + const n = this._lodPlanes.length; + + for (let i = 1; i < n; i++) { + const sigma = Math.sqrt(this._sigmas[i] * this._sigmas[i] - this._sigmas[i - 1] * this._sigmas[i - 1]); + + const poleAxis = _axisDirections[(n - i - 1) % _axisDirections.length]; + + this._blur(cubeUVRenderTarget, i - 1, i, sigma, poleAxis); + } + + renderer.autoClear = autoClear; + } + + /** + * This is a two-pass Gaussian blur for a cubemap. Normally this is done + * vertically and horizontally, but this breaks down on a cube. Here we apply + * the blur latitudinally (around the poles), and then longitudinally (towards + * the poles) to approximate the orthogonally-separable blur. It is least + * accurate at the poles, but still does a decent job. + */ + _blur(cubeUVRenderTarget, lodIn, lodOut, sigma, poleAxis) { + const pingPongRenderTarget = this._pingPongRenderTarget; + + this._halfBlur(cubeUVRenderTarget, pingPongRenderTarget, lodIn, lodOut, sigma, 'latitudinal', poleAxis); + + this._halfBlur(pingPongRenderTarget, cubeUVRenderTarget, lodOut, lodOut, sigma, 'longitudinal', poleAxis); + } + + _halfBlur(targetIn, targetOut, lodIn, lodOut, sigmaRadians, direction, poleAxis) { + const renderer = this._renderer; + const blurMaterial = this._blurMaterial; + + if (direction !== 'latitudinal' && direction !== 'longitudinal') { + console.error('blur direction must be either latitudinal or longitudinal!'); + } + + // Number of standard deviations at which to cut off the discrete approximation. + const STANDARD_DEVIATIONS = 3; + + const blurMesh = this._lodMeshes[lodOut]; + blurMesh.material = blurMaterial; + + const blurUniforms = blurMaterial.uniforms; + + const pixels = this._sizeLods[lodIn] - 1; + const radiansPerPixel = isFinite(sigmaRadians) ? Math.PI / (2 * pixels) : (2 * Math.PI) / (2 * MAX_SAMPLES - 1); + const sigmaPixels = sigmaRadians / radiansPerPixel; + const samples = isFinite(sigmaRadians) ? 1 + Math.floor(STANDARD_DEVIATIONS * sigmaPixels) : MAX_SAMPLES; + + if (samples > MAX_SAMPLES) { + console.warn( + `sigmaRadians, ${sigmaRadians}, is too large and will clip, as it requested ${ + samples + } samples when the maximum is set to ${MAX_SAMPLES}`, + ); + } + + const weights = []; + let sum = 0; + + for (let i = 0; i < MAX_SAMPLES; ++i) { + const x = i / sigmaPixels; + const weight = Math.exp((-x * x) / 2); + weights.push(weight); + + if (i === 0) { + sum += weight; + } else if (i < samples) { + sum += 2 * weight; + } + } + + for (let i = 0; i < weights.length; i++) { + weights[i] = weights[i] / sum; + } + + targetIn.texture.frame = (targetIn.texture.frame || 0) + 1; + + blurUniforms.envMap.value = targetIn.texture; + blurUniforms.samples.value = samples; + blurUniforms.weights.array = weights; + blurUniforms.latitudinal.value = direction === 'latitudinal' ? 1 : 0; + + if (poleAxis) { + blurUniforms.poleAxis.value = poleAxis; + } + + const { _lodMax } = this; + blurUniforms.dTheta.value = radiansPerPixel; + blurUniforms.mipInt.value = _lodMax - lodIn; + + const outputSize = this._sizeLods[lodOut]; + const x = 3 * outputSize * (lodOut > _lodMax - LOD_MIN ? lodOut - _lodMax + LOD_MIN : 0); + const y = 4 * (this._cubeSize - outputSize); + + _setViewport(targetOut, x, y, 3 * outputSize, 2 * outputSize); + renderer.setRenderTarget(targetOut); + renderer.render(blurMesh, _flatCamera); + } +} + +function _createPlanes(lodMax) { + const lodPlanes = []; + const sizeLods = []; + const sigmas = []; + const lodMeshes = []; + + let lod = lodMax; + + const totalLods = lodMax - LOD_MIN + 1 + EXTRA_LOD_SIGMA.length; + + for (let i = 0; i < totalLods; i++) { + const sizeLod = Math.pow(2, lod); + sizeLods.push(sizeLod); + let sigma = 1.0 / sizeLod; + + if (i > lodMax - LOD_MIN) { + sigma = EXTRA_LOD_SIGMA[i - lodMax + LOD_MIN - 1]; + } else if (i === 0) { + sigma = 0; + } + + sigmas.push(sigma); + + const texelSize = 1.0 / (sizeLod - 2); + const min = -texelSize; + const max = 1 + texelSize; + const uv1 = [min, min, max, min, max, max, min, min, max, max, min, max]; + + const cubeFaces = 6; + const vertices = 6; + const positionSize = 3; + const uvSize = 2; + const faceIndexSize = 1; + + const position = new Float32Array(positionSize * vertices * cubeFaces); + const uv = new Float32Array(uvSize * vertices * cubeFaces); + const faceIndex = new Float32Array(faceIndexSize * vertices * cubeFaces); + + for (let face = 0; face < cubeFaces; face++) { + const x = ((face % 3) * 2) / 3 - 1; + const y = face > 2 ? 0 : -1; + const coordinates = [ + x, + y, + 0, + x + 2 / 3, + y, + 0, + x + 2 / 3, + y + 1, + 0, + x, + y, + 0, + x + 2 / 3, + y + 1, + 0, + x, + y + 1, + 0, + ]; + + const faceIdx = _faceLib[face]; + position.set(coordinates, positionSize * vertices * faceIdx); + uv.set(uv1, uvSize * vertices * faceIdx); + const fill = [faceIdx, faceIdx, faceIdx, faceIdx, faceIdx, faceIdx]; + faceIndex.set(fill, faceIndexSize * vertices * faceIdx); + } + + const planes = new BufferGeometry(); + planes.setAttribute('position', new BufferAttribute(position, positionSize)); + planes.setAttribute('uv', new BufferAttribute(uv, uvSize)); + planes.setAttribute('faceIndex', new BufferAttribute(faceIndex, faceIndexSize)); + lodPlanes.push(planes); + lodMeshes.push(new Mesh(planes, null)); + + if (lod > LOD_MIN) { + lod--; + } + } + + return { lodPlanes, sizeLods, sigmas, lodMeshes }; +} + +function _createRenderTarget(width, height, params) { + const cubeUVRenderTarget = new RenderTarget(width, height, params); + cubeUVRenderTarget.texture.mapping = CubeUVReflectionMapping; + cubeUVRenderTarget.texture.name = 'PMREM.cubeUv'; + cubeUVRenderTarget.texture.isPMREMTexture = true; + cubeUVRenderTarget.scissorTest = true; + return cubeUVRenderTarget; +} + +function _setViewport(target, x, y, width, height) { + const viewY = target.height - height - y; + + target.viewport.set(x, viewY, width, height); + target.scissor.set(x, viewY, width, height); +} + +function _getMaterial() { + const material = new NodeMaterial(); + material.depthTest = false; + material.depthWrite = false; + material.blending = NoBlending; + + return material; +} + +function _getBlurShader(lodMax, width, height) { + const weights = uniformArray(new Array(MAX_SAMPLES).fill(0)); + const poleAxis = uniform(new Vector3(0, 1, 0)); + const dTheta = uniform(0); + const n = float(MAX_SAMPLES); + const latitudinal = uniform(0); // false, bool + const samples = uniform(1); // int + const envMap = texture(null); + const mipInt = uniform(0); // int + const CUBEUV_TEXEL_WIDTH = float(1 / width); + const CUBEUV_TEXEL_HEIGHT = float(1 / height); + const CUBEUV_MAX_MIP = float(lodMax); + + const materialUniforms = { + n, + latitudinal, + weights, + poleAxis, + outputDirection, + dTheta, + samples, + envMap, + mipInt, + CUBEUV_TEXEL_WIDTH, + CUBEUV_TEXEL_HEIGHT, + CUBEUV_MAX_MIP, + }; + + const material = _getMaterial(); + material.uniforms = materialUniforms; // TODO: Move to outside of the material + material.fragmentNode = blur({ ...materialUniforms, latitudinal: latitudinal.equal(1) }); + + return material; +} + +function _getCubemapMaterial(envTexture) { + const material = _getMaterial(); + material.fragmentNode = cubeTexture(envTexture, outputDirection); + + return material; +} + +function _getEquirectMaterial(envTexture) { + const material = _getMaterial(); + material.fragmentNode = texture(envTexture, equirectUV(outputDirection), 0); + + return material; +} + +export default PMREMGenerator; diff --git a/src-testing/src/renderers/common/nodes/NodeBuilderState.ts b/src-testing/src/renderers/common/nodes/NodeBuilderState.ts new file mode 100644 index 000000000..7516dbdc1 --- /dev/null +++ b/src-testing/src/renderers/common/nodes/NodeBuilderState.ts @@ -0,0 +1,55 @@ +import BindGroup from '../BindGroup.js'; + +class NodeBuilderState { + constructor( + vertexShader, + fragmentShader, + computeShader, + nodeAttributes, + bindings, + updateNodes, + updateBeforeNodes, + updateAfterNodes, + instanceBindGroups = true, + transforms = [], + ) { + this.vertexShader = vertexShader; + this.fragmentShader = fragmentShader; + this.computeShader = computeShader; + this.transforms = transforms; + + this.nodeAttributes = nodeAttributes; + this.bindings = bindings; + + this.updateNodes = updateNodes; + this.updateBeforeNodes = updateBeforeNodes; + this.updateAfterNodes = updateAfterNodes; + + this.instanceBindGroups = instanceBindGroups; + + this.usedTimes = 0; + } + + createBindings() { + const bindings = []; + + for (const instanceGroup of this.bindings) { + const shared = this.instanceBindGroups && instanceGroup.bindings[0].groupNode.shared; + + if (shared !== true) { + const bindingsGroup = new BindGroup(instanceGroup.name, [], instanceGroup.index); + bindings.push(bindingsGroup); + + for (const instanceBinding of instanceGroup.bindings) { + bindingsGroup.bindings.push(instanceBinding.clone()); + } + } else { + bindings.push(instanceGroup); + } + } + + return bindings; + } +} + +export default NodeBuilderState; diff --git a/src-testing/src/renderers/common/nodes/NodeSampledTexture.d.ts b/src-testing/src/renderers/common/nodes/NodeSampledTexture.d.ts new file mode 100644 index 000000000..12a2a4aa7 --- /dev/null +++ b/src-testing/src/renderers/common/nodes/NodeSampledTexture.d.ts @@ -0,0 +1,29 @@ +import TextureNode from "../../../nodes/accessors/TextureNode.js"; +import UniformGroupNode from "../../../nodes/core/UniformGroupNode.js"; +import { SampledTexture } from "../SampledTexture.js"; + +type GPUStorageTextureAccess = "read-only" | "read-write" | "write-only"; + +declare class NodeSampledTexture extends SampledTexture { + textureNode: TextureNode | undefined; + groupNode: UniformGroupNode; + access: "read-write" | "read-only" | "write-only"; + constructor( + name: string, + textureNode: TextureNode | undefined, + groupNode: UniformGroupNode, + access: GPUStorageTextureAccess | null, + ); + get needsBindingsUpdate(): boolean; + update(): boolean; +} + +declare class NodeSampledCubeTexture extends NodeSampledTexture { + readonly isSampledCubeTexture: true; +} + +declare class NodeSampledTexture3D extends NodeSampledTexture { + readonly isSampledTexture3D = true; +} + +export { NodeSampledCubeTexture, NodeSampledTexture, NodeSampledTexture3D }; diff --git a/src-testing/src/renderers/common/nodes/NodeSampler.d.ts b/src-testing/src/renderers/common/nodes/NodeSampler.d.ts new file mode 100644 index 000000000..60db177d5 --- /dev/null +++ b/src-testing/src/renderers/common/nodes/NodeSampler.d.ts @@ -0,0 +1,12 @@ +import TextureNode from "../../../nodes/accessors/TextureNode.js"; +import UniformGroupNode from "../../../nodes/core/UniformGroupNode.js"; +import Sampler from "../Sampler.js"; + +declare class NodeSampler extends Sampler { + textureNode: TextureNode | undefined; + groupNode: UniformGroupNode; + constructor(name: string, textureNode: TextureNode | undefined, groupNode: UniformGroupNode); + update(): void; +} + +export default NodeSampler; diff --git a/src-testing/src/renderers/common/nodes/NodeUniform.ts b/src-testing/src/renderers/common/nodes/NodeUniform.ts new file mode 100644 index 000000000..659f5a82f --- /dev/null +++ b/src-testing/src/renderers/common/nodes/NodeUniform.ts @@ -0,0 +1,103 @@ +import { + NumberUniform, + Vector2Uniform, + Vector3Uniform, + Vector4Uniform, + ColorUniform, + Matrix3Uniform, + Matrix4Uniform, +} from '../Uniform.js'; + +class NumberNodeUniform extends NumberUniform { + constructor(nodeUniform) { + super(nodeUniform.name, nodeUniform.value); + + this.nodeUniform = nodeUniform; + } + + getValue() { + return this.nodeUniform.value; + } +} + +class Vector2NodeUniform extends Vector2Uniform { + constructor(nodeUniform) { + super(nodeUniform.name, nodeUniform.value); + + this.nodeUniform = nodeUniform; + } + + getValue() { + return this.nodeUniform.value; + } +} + +class Vector3NodeUniform extends Vector3Uniform { + constructor(nodeUniform) { + super(nodeUniform.name, nodeUniform.value); + + this.nodeUniform = nodeUniform; + } + + getValue() { + return this.nodeUniform.value; + } +} + +class Vector4NodeUniform extends Vector4Uniform { + constructor(nodeUniform) { + super(nodeUniform.name, nodeUniform.value); + + this.nodeUniform = nodeUniform; + } + + getValue() { + return this.nodeUniform.value; + } +} + +class ColorNodeUniform extends ColorUniform { + constructor(nodeUniform) { + super(nodeUniform.name, nodeUniform.value); + + this.nodeUniform = nodeUniform; + } + + getValue() { + return this.nodeUniform.value; + } +} + +class Matrix3NodeUniform extends Matrix3Uniform { + constructor(nodeUniform) { + super(nodeUniform.name, nodeUniform.value); + + this.nodeUniform = nodeUniform; + } + + getValue() { + return this.nodeUniform.value; + } +} + +class Matrix4NodeUniform extends Matrix4Uniform { + constructor(nodeUniform) { + super(nodeUniform.name, nodeUniform.value); + + this.nodeUniform = nodeUniform; + } + + getValue() { + return this.nodeUniform.value; + } +} + +export { + NumberNodeUniform, + Vector2NodeUniform, + Vector3NodeUniform, + Vector4NodeUniform, + ColorNodeUniform, + Matrix3NodeUniform, + Matrix4NodeUniform, +}; diff --git a/src-testing/src/renderers/common/nodes/NodeUniformsGroup.ts b/src-testing/src/renderers/common/nodes/NodeUniformsGroup.ts new file mode 100644 index 000000000..d2d92cb20 --- /dev/null +++ b/src-testing/src/renderers/common/nodes/NodeUniformsGroup.ts @@ -0,0 +1,30 @@ +import UniformsGroup from '../UniformsGroup.js'; + +let _id = 0; + +class NodeUniformsGroup extends UniformsGroup { + constructor(name, groupNode) { + super(name); + + this.id = _id++; + this.groupNode = groupNode; + + this.isNodeUniformsGroup = true; + } + + getNodes() { + const nodes = []; + + for (const uniform of this.uniforms) { + const node = uniform.nodeUniform.node; + + if (!node) throw new Error('NodeUniformsGroup: Uniform has no node.'); + + nodes.push(node); + } + + return nodes; + } +} + +export default NodeUniformsGroup; diff --git a/src-testing/src/renderers/common/nodes/Nodes.ts b/src-testing/src/renderers/common/nodes/Nodes.ts new file mode 100644 index 000000000..a162ac892 --- /dev/null +++ b/src-testing/src/renderers/common/nodes/Nodes.ts @@ -0,0 +1,399 @@ +import DataMap from '../DataMap.js'; +import ChainMap from '../ChainMap.js'; +import NodeBuilderState from './NodeBuilderState.js'; +import { + NodeFrame, + objectGroup, + renderGroup, + frameGroup, + cubeTexture, + texture, + rangeFog, + densityFog, + reference, + viewportBottomLeft, + normalWorld, + pmremTexture, + viewportTopLeft, +} from '../../../nodes/Nodes.js'; + +import { EquirectangularReflectionMapping, EquirectangularRefractionMapping } from '../../../constants.js'; + +const outputNodeMap = new WeakMap(); + +class Nodes extends DataMap { + constructor(renderer, backend) { + super(); + + this.renderer = renderer; + this.backend = backend; + this.nodeFrame = new NodeFrame(); + this.nodeBuilderCache = new Map(); + this.callHashCache = new ChainMap(); + this.groupsData = new ChainMap(); + } + + updateGroup(nodeUniformsGroup) { + const groupNode = nodeUniformsGroup.groupNode; + const name = groupNode.name; + + // objectGroup is every updated + + if (name === objectGroup.name) return true; + + // renderGroup is updated once per render/compute call + + if (name === renderGroup.name) { + const uniformsGroupData = this.get(nodeUniformsGroup); + const renderId = this.nodeFrame.renderId; + + if (uniformsGroupData.renderId !== renderId) { + uniformsGroupData.renderId = renderId; + + return true; + } + + return false; + } + + // frameGroup is updated once per frame + + if (name === frameGroup.name) { + const uniformsGroupData = this.get(nodeUniformsGroup); + const frameId = this.nodeFrame.frameId; + + if (uniformsGroupData.frameId !== frameId) { + uniformsGroupData.frameId = frameId; + + return true; + } + + return false; + } + + // other groups are updated just when groupNode.needsUpdate is true + + const groupChain = [groupNode, nodeUniformsGroup]; + + let groupData = this.groupsData.get(groupChain); + if (groupData === undefined) this.groupsData.set(groupChain, (groupData = {})); + + if (groupData.version !== groupNode.version) { + groupData.version = groupNode.version; + + return true; + } + + return false; + } + + getForRenderCacheKey(renderObject) { + return renderObject.initialCacheKey; + } + + getForRender(renderObject) { + const renderObjectData = this.get(renderObject); + + let nodeBuilderState = renderObjectData.nodeBuilderState; + + if (nodeBuilderState === undefined) { + const { nodeBuilderCache } = this; + + const cacheKey = this.getForRenderCacheKey(renderObject); + + nodeBuilderState = nodeBuilderCache.get(cacheKey); + + if (nodeBuilderState === undefined) { + const nodeBuilder = this.backend.createNodeBuilder(renderObject.object, this.renderer); + nodeBuilder.scene = renderObject.scene; + nodeBuilder.material = renderObject.material; + nodeBuilder.camera = renderObject.camera; + nodeBuilder.context.material = renderObject.material; + nodeBuilder.lightsNode = renderObject.lightsNode; + nodeBuilder.environmentNode = this.getEnvironmentNode(renderObject.scene); + nodeBuilder.fogNode = this.getFogNode(renderObject.scene); + nodeBuilder.clippingContext = renderObject.clippingContext; + nodeBuilder.build(); + + nodeBuilderState = this._createNodeBuilderState(nodeBuilder); + + nodeBuilderCache.set(cacheKey, nodeBuilderState); + } + + nodeBuilderState.usedTimes++; + + renderObjectData.nodeBuilderState = nodeBuilderState; + } + + return nodeBuilderState; + } + + delete(object) { + if (object.isRenderObject) { + const nodeBuilderState = this.get(object).nodeBuilderState; + nodeBuilderState.usedTimes--; + + if (nodeBuilderState.usedTimes === 0) { + this.nodeBuilderCache.delete(this.getForRenderCacheKey(object)); + } + } + + return super.delete(object); + } + + getForCompute(computeNode) { + const computeData = this.get(computeNode); + + let nodeBuilderState = computeData.nodeBuilderState; + + if (nodeBuilderState === undefined) { + const nodeBuilder = this.backend.createNodeBuilder(computeNode, this.renderer); + nodeBuilder.build(); + + nodeBuilderState = this._createNodeBuilderState(nodeBuilder); + + computeData.nodeBuilderState = nodeBuilderState; + } + + return nodeBuilderState; + } + + _createNodeBuilderState(nodeBuilder) { + return new NodeBuilderState( + nodeBuilder.vertexShader, + nodeBuilder.fragmentShader, + nodeBuilder.computeShader, + nodeBuilder.getAttributesArray(), + nodeBuilder.getBindings(), + nodeBuilder.updateNodes, + nodeBuilder.updateBeforeNodes, + nodeBuilder.updateAfterNodes, + nodeBuilder.instanceBindGroups, + nodeBuilder.transforms, + ); + } + + getEnvironmentNode(scene) { + return scene.environmentNode || this.get(scene).environmentNode || null; + } + + getBackgroundNode(scene) { + return scene.backgroundNode || this.get(scene).backgroundNode || null; + } + + getFogNode(scene) { + return scene.fogNode || this.get(scene).fogNode || null; + } + + getCacheKey(scene, lightsNode) { + const chain = [scene, lightsNode]; + const callId = this.renderer.info.calls; + + let cacheKeyData = this.callHashCache.get(chain); + + if (cacheKeyData === undefined || cacheKeyData.callId !== callId) { + const environmentNode = this.getEnvironmentNode(scene); + const fogNode = this.getFogNode(scene); + + const cacheKey = []; + + if (lightsNode) cacheKey.push(lightsNode.getCacheKey(true)); + if (environmentNode) cacheKey.push(environmentNode.getCacheKey()); + if (fogNode) cacheKey.push(fogNode.getCacheKey()); + + cacheKeyData = { + callId, + cacheKey: cacheKey.join(','), + }; + + this.callHashCache.set(chain, cacheKeyData); + } + + return cacheKeyData.cacheKey; + } + + updateScene(scene) { + this.updateEnvironment(scene); + this.updateFog(scene); + this.updateBackground(scene); + } + + get isToneMappingState() { + return this.renderer.getRenderTarget() ? false : true; + } + + updateBackground(scene) { + const sceneData = this.get(scene); + const background = scene.background; + + if (background) { + if (sceneData.background !== background) { + let backgroundNode = null; + + if ( + background.isCubeTexture === true || + background.mapping === EquirectangularReflectionMapping || + background.mapping === EquirectangularRefractionMapping + ) { + backgroundNode = pmremTexture(background, normalWorld); + } else if (background.isTexture === true) { + backgroundNode = texture(background, viewportBottomLeft).setUpdateMatrix(true); + } else if (background.isColor !== true) { + console.error('WebGPUNodes: Unsupported background configuration.', background); + } + + sceneData.backgroundNode = backgroundNode; + sceneData.background = background; + } + } else if (sceneData.backgroundNode) { + delete sceneData.backgroundNode; + delete sceneData.background; + } + } + + updateFog(scene) { + const sceneData = this.get(scene); + const fog = scene.fog; + + if (fog) { + if (sceneData.fog !== fog) { + let fogNode = null; + + if (fog.isFogExp2) { + fogNode = densityFog(reference('color', 'color', fog), reference('density', 'float', fog)); + } else if (fog.isFog) { + fogNode = rangeFog( + reference('color', 'color', fog), + reference('near', 'float', fog), + reference('far', 'float', fog), + ); + } else { + console.error('WebGPUNodes: Unsupported fog configuration.', fog); + } + + sceneData.fogNode = fogNode; + sceneData.fog = fog; + } + } else { + delete sceneData.fogNode; + delete sceneData.fog; + } + } + + updateEnvironment(scene) { + const sceneData = this.get(scene); + const environment = scene.environment; + + if (environment) { + if (sceneData.environment !== environment) { + let environmentNode = null; + + if (environment.isCubeTexture === true) { + environmentNode = cubeTexture(environment); + } else if (environment.isTexture === true) { + environmentNode = texture(environment); + } else { + console.error('Nodes: Unsupported environment configuration.', environment); + } + + sceneData.environmentNode = environmentNode; + sceneData.environment = environment; + } + } else if (sceneData.environmentNode) { + delete sceneData.environmentNode; + delete sceneData.environment; + } + } + + getNodeFrame(renderer = this.renderer, scene = null, object = null, camera = null, material = null) { + const nodeFrame = this.nodeFrame; + nodeFrame.renderer = renderer; + nodeFrame.scene = scene; + nodeFrame.object = object; + nodeFrame.camera = camera; + nodeFrame.material = material; + + return nodeFrame; + } + + getNodeFrameForRender(renderObject) { + return this.getNodeFrame( + renderObject.renderer, + renderObject.scene, + renderObject.object, + renderObject.camera, + renderObject.material, + ); + } + + getOutputCacheKey() { + const renderer = this.renderer; + + return renderer.toneMapping + ',' + renderer.currentColorSpace; + } + + hasOutputChange(outputTarget) { + const cacheKey = outputNodeMap.get(outputTarget); + + return cacheKey !== this.getOutputCacheKey(); + } + + getOutputNode(outputTexture) { + const renderer = this.renderer; + const cacheKey = this.getOutputCacheKey(); + + const output = texture(outputTexture, viewportTopLeft).renderOutput( + renderer.toneMapping, + renderer.currentColorSpace, + ); + + outputNodeMap.set(outputTexture, cacheKey); + + return output; + } + + updateBefore(renderObject) { + const nodeFrame = this.getNodeFrameForRender(renderObject); + const nodeBuilder = renderObject.getNodeBuilderState(); + + for (const node of nodeBuilder.updateBeforeNodes) { + nodeFrame.updateBeforeNode(node); + } + } + + updateAfter(renderObject) { + const nodeFrame = this.getNodeFrameForRender(renderObject); + const nodeBuilder = renderObject.getNodeBuilderState(); + + for (const node of nodeBuilder.updateAfterNodes) { + nodeFrame.updateAfterNode(node); + } + } + + updateForCompute(computeNode) { + const nodeFrame = this.getNodeFrame(); + const nodeBuilder = this.getForCompute(computeNode); + + for (const node of nodeBuilder.updateNodes) { + nodeFrame.updateNode(node); + } + } + + updateForRender(renderObject) { + const nodeFrame = this.getNodeFrameForRender(renderObject); + const nodeBuilder = renderObject.getNodeBuilderState(); + + for (const node of nodeBuilder.updateNodes) { + nodeFrame.updateNode(node); + } + } + + dispose() { + super.dispose(); + + this.nodeFrame = new NodeFrame(); + this.nodeBuilderCache = new Map(); + } +} + +export default Nodes; diff --git a/src-testing/src/renderers/shaders/ShaderChunk.d.ts b/src-testing/src/renderers/shaders/ShaderChunk.d.ts new file mode 100644 index 000000000..caf4e39bb --- /dev/null +++ b/src-testing/src/renderers/shaders/ShaderChunk.d.ts @@ -0,0 +1,143 @@ +// Renderers / Shaders ///////////////////////////////////////////////////////////////////// +export const ShaderChunk: { + alphahash_fragment: string; + alphahash_pars_fragment: string; + alphamap_fragment: string; + alphamap_pars_fragment: string; + alphatest_fragment: string; + alphatest_pars_fragment: string; + aomap_fragment: string; + aomap_pars_fragment: string; + batching_pars_vertex: string; + begin_vertex: string; + beginnormal_vertex: string; + bsdfs: string; + iridescence_fragment: string; + bumpmap_pars_fragment: string; + clipping_planes_fragment: string; + clipping_planes_pars_fragment: string; + clipping_planes_pars_vertex: string; + clipping_planes_vertex: string; + color_fragment: string; + color_pars_fragment: string; + color_pars_vertex: string; + color_vertex: string; + common: string; + cube_uv_reflection_fragment: string; + defaultnormal_vertex: string; + displacementmap_pars_vertex: string; + displacementmap_vertex: string; + emissivemap_fragment: string; + emissivemap_pars_fragment: string; + colorspace_fragment: string; + colorspace_pars_fragment: string; + envmap_fragment: string; + envmap_common_pars_fragment: string; + envmap_pars_fragment: string; + envmap_pars_vertex: string; + envmap_physical_pars_fragment: string; + envmap_vertex: string; + fog_vertex: string; + fog_pars_vertex: string; + fog_fragment: string; + fog_pars_fragment: string; + gradientmap_pars_fragment: string; + lightmap_pars_fragment: string; + lights_lambert_fragment: string; + lights_lambert_pars_fragment: string; + lights_pars_begin: string; + lights_toon_fragment: string; + lights_toon_pars_fragment: string; + lights_phong_fragment: string; + lights_phong_pars_fragment: string; + lights_physical_fragment: string; + lights_physical_pars_fragment: string; + lights_fragment_begin: string; + lights_fragment_maps: string; + lights_fragment_end: string; + logdepthbuf_fragment: string; + logdepthbuf_pars_fragment: string; + logdepthbuf_pars_vertex: string; + logdepthbuf_vertex: string; + map_fragment: string; + map_pars_fragment: string; + map_particle_fragment: string; + map_particle_pars_fragment: string; + metalnessmap_fragment: string; + metalnessmap_pars_fragment: string; + morphcolor_vertex: string; + morphnormal_vertex: string; + morphtarget_pars_vertex: string; + morphtarget_vertex: string; + normal_fragment_begin: string; + normal_fragment_maps: string; + normal_pars_fragment: string; + normal_pars_vertex: string; + normal_vertex: string; + normalmap_pars_fragment: string; + clearcoat_normal_fragment_begin: string; + clearcoat_normal_fragment_maps: string; + clearcoat_pars_fragment: string; + iridescence_pars_fragment: string; + opaque_fragment: string; + packing: string; + premultiplied_alpha_fragment: string; + project_vertex: string; + dithering_fragment: string; + dithering_pars_fragment: string; + roughnessmap_fragment: string; + roughnessmap_pars_fragment: string; + shadowmap_pars_fragment: string; + shadowmap_pars_vertex: string; + shadowmap_vertex: string; + shadowmask_pars_fragment: string; + skinbase_vertex: string; + skinning_pars_vertex: string; + skinning_vertex: string; + skinnormal_vertex: string; + specularmap_fragment: string; + specularmap_pars_fragment: string; + tonemapping_fragment: string; + tonemapping_pars_fragment: string; + transmission_fragment: string; + transmission_pars_fragment: string; + uv_pars_fragment: string; + uv_pars_vertex: string; + uv_vertex: string; + worldpos_vertex: string; + + background_vert: string; + background_frag: string; + backgroundCube_vert: string; + backgroundCube_frag: string; + cube_vert: string; + cube_frag: string; + depth_vert: string; + depth_frag: string; + distanceRGBA_vert: string; + distanceRGBA_frag: string; + equirect_vert: string; + equirect_frag: string; + linedashed_vert: string; + linedashed_frag: string; + meshbasic_vert: string; + meshbasic_frag: string; + meshlambert_vert: string; + meshlambert_frag: string; + meshmatcap_vert: string; + meshmatcap_frag: string; + meshnormal_vert: string; + meshnormal_frag: string; + meshphong_vert: string; + meshphong_frag: string; + meshphysical_vert: string; + meshphysical_frag: string; + meshtoon_vert: string; + meshtoon_frag: string; + points_vert: string; + points_frag: string; + shadow_vert: string; + shadow_frag: string; + sprite_vert: string; + sprite_frag: string; +}; diff --git a/src-testing/src/renderers/shaders/ShaderLib.d.ts b/src-testing/src/renderers/shaders/ShaderLib.d.ts new file mode 100644 index 000000000..9a52c4dcd --- /dev/null +++ b/src-testing/src/renderers/shaders/ShaderLib.d.ts @@ -0,0 +1,29 @@ +import { IUniform } from "./UniformsLib.js"; + +export interface ShaderLibShader { + uniforms: { [uniform: string]: IUniform }; + vertexShader: string; + fragmentShader: string; +} + +declare const ShaderLib: { + [name: string]: ShaderLibShader; + basic: ShaderLibShader; + lambert: ShaderLibShader; + phong: ShaderLibShader; + standard: ShaderLibShader; + matcap: ShaderLibShader; + points: ShaderLibShader; + dashed: ShaderLibShader; + depth: ShaderLibShader; + normal: ShaderLibShader; + sprite: ShaderLibShader; + background: ShaderLibShader; + cube: ShaderLibShader; + equirect: ShaderLibShader; + distanceRGBA: ShaderLibShader; + shadow: ShaderLibShader; + physical: ShaderLibShader; +}; + +export { ShaderLib }; diff --git a/src-testing/src/renderers/shaders/UniformsLib.d.ts b/src-testing/src/renderers/shaders/UniformsLib.d.ts new file mode 100644 index 000000000..cb0d808bd --- /dev/null +++ b/src-testing/src/renderers/shaders/UniformsLib.d.ts @@ -0,0 +1,189 @@ +import { Color } from "../../math/Color.js"; +import { Matrix3 } from "../../math/Matrix3.js"; +import { Vector2 } from "../../math/Vector2.js"; + +// eslint-disable-next-line @typescript-eslint/naming-convention +export interface IUniform { + value: TValue; +} + +export const UniformsLib: { + common: { + diffuse: IUniform; + opacity: IUniform; + map: IUniform; + mapTransform: IUniform; + alphaMap: IUniform; + alphaMapTransform: IUniform; + alphaTest: IUniform; + }; + specularmap: { + specularMap: IUniform; + specularMapTransform: IUniform; + }; + envmap: { + envMap: IUniform; + envMapRotation: IUniform; + flipEnvMap: IUniform; + reflectivity: IUniform; + ior: IUniform; + refractRatio: IUniform; + }; + aomap: { + aoMap: IUniform; + aoMapIntensity: IUniform; + aoMapTransform: IUniform; + }; + lightmap: { + lightMap: IUniform; + lightMapIntensity: IUniform; + lightMapTransform: IUniform; + }; + bumpmap: { + bumpMap: IUniform; + bumpMapTransform: IUniform; + bumpScale: IUniform; + }; + normalmap: { + normalMap: IUniform; + normalMapTransform: IUniform; + normalScale: IUniform; + }; + displacementmap: { + displacementMap: IUniform; + displacementMapTransform: IUniform; + displacementScale: IUniform; + displacementBias: IUniform; + }; + emissivemap: { + emissiveMap: IUniform; + emissiveMapTransform: IUniform; + }; + metalnessmap: { + metalnessMap: IUniform; + metalnessMapTransform: IUniform; + }; + roughnessmap: { + roughnessMap: IUniform; + roughnessMapTransform: IUniform; + }; + gradientmap: { + gradientMap: IUniform; + }; + fog: { + fogDensity: IUniform; + fogNear: IUniform; + fogFar: IUniform; + fogColor: IUniform; + }; + lights: { + ambientLightColor: IUniform; + lightProbe: IUniform; + directionalLights: { + value: unknown[]; + properties: { + direction: {}; + color: {}; + }; + }; + directionalLightShadows: { + value: unknown[]; + properties: { + shadowIntensity: number; + shadowBias: {}; + shadowNormalBias: {}; + shadowRadius: {}; + shadowMapSize: {}; + }; + }; + directionalShadowMap: IUniform; + directionalShadowMatrix: IUniform; + spotLights: { + value: unknown[]; + properties: { + color: {}; + position: {}; + direction: {}; + distance: {}; + coneCos: {}; + penumbraCos: {}; + decay: {}; + }; + }; + spotLightShadows: { + value: unknown[]; + properties: { + shadowIntensity: number; + shadowBias: {}; + shadowNormalBias: {}; + shadowRadius: {}; + shadowMapSize: {}; + }; + }; + spotLightMap: IUniform; + spotShadowMap: IUniform; + spotLightMatrix: IUniform; + pointLights: { + value: unknown[]; + properties: { + color: {}; + position: {}; + decay: {}; + distance: {}; + }; + }; + pointLightShadows: { + value: unknown[]; + properties: { + shadowIntensity: number; + shadowBias: {}; + shadowNormalBias: {}; + shadowRadius: {}; + shadowMapSize: {}; + shadowCameraNear: {}; + shadowCameraFar: {}; + }; + }; + pointShadowMap: IUniform; + pointShadowMatrix: IUniform; + hemisphereLights: { + value: unknown[]; + properties: { + direction: {}; + skycolor: {}; + groundColor: {}; + }; + }; + rectAreaLights: { + value: unknown[]; + properties: { + color: {}; + position: {}; + width: {}; + height: {}; + }; + }; + ltc_1: IUniform; + ltc_2: IUniform; + }; + points: { + diffuse: IUniform; + opacity: IUniform; + size: IUniform; + scale: IUniform; + map: IUniform; + alphaMap: IUniform; + alphaTest: IUniform; + uvTransform: IUniform; + }; + sprite: { + diffuse: IUniform; + opacity: IUniform; + center: IUniform; + rotation: IUniform; + map: IUniform; + mapTransform: IUniform; + alphaMap: IUniform; + alphaTest: IUniform; + }; +}; diff --git a/src-testing/src/renderers/shaders/UniformsUtils.d.ts b/src-testing/src/renderers/shaders/UniformsUtils.d.ts new file mode 100644 index 000000000..fe5178d55 --- /dev/null +++ b/src-testing/src/renderers/shaders/UniformsUtils.d.ts @@ -0,0 +1,14 @@ +import { UniformsGroup } from "../../core/UniformsGroup.js"; +import { IUniform } from "./UniformsLib.js"; + +export function cloneUniforms(uniformsSrc: T): T; +export function mergeUniforms(uniforms: Array<{ [uniform: string]: IUniform }>): { [uniform: string]: IUniform }; + +export function cloneUniformsGroups(src: UniformsGroup[]): UniformsGroup[]; + +declare const UniformsUtils: { + clone: typeof cloneUniforms; + merge: typeof mergeUniforms; +}; + +export { UniformsUtils }; diff --git a/src-testing/src/renderers/webgl-fallback/WebGLBackend.ts b/src-testing/src/renderers/webgl-fallback/WebGLBackend.ts new file mode 100644 index 000000000..35e79b78e --- /dev/null +++ b/src-testing/src/renderers/webgl-fallback/WebGLBackend.ts @@ -0,0 +1,1280 @@ +import GLSLNodeBuilder from './nodes/GLSLNodeBuilder.js'; +import Backend from '../common/Backend.js'; +import { getCacheKey } from '../common/RenderContext.js'; + +import WebGLAttributeUtils from './utils/WebGLAttributeUtils.js'; +import WebGLState from './utils/WebGLState.js'; +import WebGLUtils from './utils/WebGLUtils.js'; +import WebGLTextureUtils from './utils/WebGLTextureUtils.js'; +import WebGLExtensions from './utils/WebGLExtensions.js'; +import WebGLCapabilities from './utils/WebGLCapabilities.js'; +import { GLFeatureName } from './utils/WebGLConstants.js'; +import { WebGLBufferRenderer } from './WebGLBufferRenderer.js'; + +import { warnOnce } from '../../utils.js'; +import { WebGLCoordinateSystem } from '../../constants.js'; + +// + +class WebGLBackend extends Backend { + constructor(parameters = {}) { + super(parameters); + + this.isWebGLBackend = true; + } + + init(renderer) { + super.init(renderer); + + // + + const parameters = this.parameters; + + const glContext = + parameters.context !== undefined ? parameters.context : renderer.domElement.getContext('webgl2'); + + this.gl = glContext; + + this.extensions = new WebGLExtensions(this); + this.capabilities = new WebGLCapabilities(this); + this.attributeUtils = new WebGLAttributeUtils(this); + this.textureUtils = new WebGLTextureUtils(this); + this.bufferRenderer = new WebGLBufferRenderer(this); + + this.state = new WebGLState(this); + this.utils = new WebGLUtils(this); + + this.vaoCache = {}; + this.transformFeedbackCache = {}; + this.discard = false; + this.trackTimestamp = parameters.trackTimestamp === true; + + this.extensions.get('EXT_color_buffer_float'); + this.extensions.get('WEBGL_multi_draw'); + + this.disjoint = this.extensions.get('EXT_disjoint_timer_query_webgl2'); + this.parallel = this.extensions.get('KHR_parallel_shader_compile'); + this._currentContext = null; + } + + get coordinateSystem() { + return WebGLCoordinateSystem; + } + + async getArrayBufferAsync(attribute) { + return await this.attributeUtils.getArrayBufferAsync(attribute); + } + + initTimestampQuery(renderContext) { + if (!this.disjoint || !this.trackTimestamp) return; + + const renderContextData = this.get(renderContext); + + if (this.queryRunning) { + if (!renderContextData.queryQueue) renderContextData.queryQueue = []; + renderContextData.queryQueue.push(renderContext); + return; + } + + if (renderContextData.activeQuery) { + this.gl.endQuery(this.disjoint.TIME_ELAPSED_EXT); + renderContextData.activeQuery = null; + } + + renderContextData.activeQuery = this.gl.createQuery(); + + if (renderContextData.activeQuery !== null) { + this.gl.beginQuery(this.disjoint.TIME_ELAPSED_EXT, renderContextData.activeQuery); + this.queryRunning = true; + } + } + + // timestamp utils + + prepareTimestampBuffer(renderContext) { + if (!this.disjoint || !this.trackTimestamp) return; + + const renderContextData = this.get(renderContext); + + if (renderContextData.activeQuery) { + this.gl.endQuery(this.disjoint.TIME_ELAPSED_EXT); + + if (!renderContextData.gpuQueries) renderContextData.gpuQueries = []; + renderContextData.gpuQueries.push({ query: renderContextData.activeQuery }); + renderContextData.activeQuery = null; + this.queryRunning = false; + + if (renderContextData.queryQueue && renderContextData.queryQueue.length > 0) { + const nextRenderContext = renderContextData.queryQueue.shift(); + this.initTimestampQuery(nextRenderContext); + } + } + } + + async resolveTimestampAsync(renderContext, type = 'render') { + if (!this.disjoint || !this.trackTimestamp) return; + + const renderContextData = this.get(renderContext); + + if (!renderContextData.gpuQueries) renderContextData.gpuQueries = []; + + for (let i = 0; i < renderContextData.gpuQueries.length; i++) { + const queryInfo = renderContextData.gpuQueries[i]; + const available = this.gl.getQueryParameter(queryInfo.query, this.gl.QUERY_RESULT_AVAILABLE); + const disjoint = this.gl.getParameter(this.disjoint.GPU_DISJOINT_EXT); + + if (available && !disjoint) { + const elapsed = this.gl.getQueryParameter(queryInfo.query, this.gl.QUERY_RESULT); + const duration = Number(elapsed) / 1000000; // Convert nanoseconds to milliseconds + this.gl.deleteQuery(queryInfo.query); + renderContextData.gpuQueries.splice(i, 1); // Remove the processed query + i--; + this.renderer.info.updateTimestamp(type, duration); + } + } + } + + getContext() { + return this.gl; + } + + beginRender(renderContext) { + const { gl } = this; + const renderContextData = this.get(renderContext); + + // + + // + + this.initTimestampQuery(renderContext); + + renderContextData.previousContext = this._currentContext; + this._currentContext = renderContext; + + this._setFramebuffer(renderContext); + + this.clear( + renderContext.clearColor, + renderContext.clearDepth, + renderContext.clearStencil, + renderContext, + false, + ); + + // + if (renderContext.viewport) { + this.updateViewport(renderContext); + } else { + gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); + } + + if (renderContext.scissor) { + const { x, y, width, height } = renderContext.scissorValue; + + gl.scissor(x, y, width, height); + } + + const occlusionQueryCount = renderContext.occlusionQueryCount; + + if (occlusionQueryCount > 0) { + // Get a reference to the array of objects with queries. The renderContextData property + // can be changed by another render pass before the async reading of all previous queries complete + renderContextData.currentOcclusionQueries = renderContextData.occlusionQueries; + renderContextData.currentOcclusionQueryObjects = renderContextData.occlusionQueryObjects; + + renderContextData.lastOcclusionObject = null; + renderContextData.occlusionQueries = new Array(occlusionQueryCount); + renderContextData.occlusionQueryObjects = new Array(occlusionQueryCount); + renderContextData.occlusionQueryIndex = 0; + } + } + + finishRender(renderContext) { + const { gl, state } = this; + const renderContextData = this.get(renderContext); + const previousContext = renderContextData.previousContext; + + const textures = renderContext.textures; + + if (textures !== null) { + for (let i = 0; i < textures.length; i++) { + const texture = textures[i]; + + if (texture.generateMipmaps) { + this.generateMipmaps(texture); + } + } + } + + this._currentContext = previousContext; + + if (renderContext.textures !== null && renderContext.renderTarget) { + const renderTargetContextData = this.get(renderContext.renderTarget); + + const { samples } = renderContext.renderTarget; + + if (samples > 0) { + const fb = renderTargetContextData.framebuffers[renderContext.getCacheKey()]; + + const mask = gl.COLOR_BUFFER_BIT; + + const msaaFrameBuffer = renderTargetContextData.msaaFrameBuffer; + + const textures = renderContext.textures; + + state.bindFramebuffer(gl.READ_FRAMEBUFFER, msaaFrameBuffer); + state.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fb); + + for (let i = 0; i < textures.length; i++) { + // TODO Add support for MRT + + gl.blitFramebuffer( + 0, + 0, + renderContext.width, + renderContext.height, + 0, + 0, + renderContext.width, + renderContext.height, + mask, + gl.NEAREST, + ); + + gl.invalidateFramebuffer(gl.READ_FRAMEBUFFER, renderTargetContextData.invalidationArray); + } + } + } + + if (previousContext !== null) { + this._setFramebuffer(previousContext); + + if (previousContext.viewport) { + this.updateViewport(previousContext); + } else { + const gl = this.gl; + + gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); + } + } + + const occlusionQueryCount = renderContext.occlusionQueryCount; + + if (occlusionQueryCount > 0) { + const renderContextData = this.get(renderContext); + + if (occlusionQueryCount > renderContextData.occlusionQueryIndex) { + const { gl } = this; + + gl.endQuery(gl.ANY_SAMPLES_PASSED); + } + + this.resolveOccludedAsync(renderContext); + } + + this.prepareTimestampBuffer(renderContext); + } + + resolveOccludedAsync(renderContext) { + const renderContextData = this.get(renderContext); + + // handle occlusion query results + + const { currentOcclusionQueries, currentOcclusionQueryObjects } = renderContextData; + + if (currentOcclusionQueries && currentOcclusionQueryObjects) { + const occluded = new WeakSet(); + const { gl } = this; + + renderContextData.currentOcclusionQueryObjects = null; + renderContextData.currentOcclusionQueries = null; + + const check = () => { + let completed = 0; + + // check all queries and requeue as appropriate + for (let i = 0; i < currentOcclusionQueries.length; i++) { + const query = currentOcclusionQueries[i]; + + if (query === null) continue; + + if (gl.getQueryParameter(query, gl.QUERY_RESULT_AVAILABLE)) { + if (gl.getQueryParameter(query, gl.QUERY_RESULT) > 0) + occluded.add(currentOcclusionQueryObjects[i]); + + currentOcclusionQueries[i] = null; + gl.deleteQuery(query); + + completed++; + } + } + + if (completed < currentOcclusionQueries.length) { + requestAnimationFrame(check); + } else { + renderContextData.occluded = occluded; + } + }; + + check(); + } + } + + isOccluded(renderContext, object) { + const renderContextData = this.get(renderContext); + + return renderContextData.occluded && renderContextData.occluded.has(object); + } + + updateViewport(renderContext) { + const gl = this.gl; + const { x, y, width, height } = renderContext.viewportValue; + + gl.viewport(x, y, width, height); + } + + setScissorTest(boolean) { + const gl = this.gl; + + if (boolean) { + gl.enable(gl.SCISSOR_TEST); + } else { + gl.disable(gl.SCISSOR_TEST); + } + } + + clear(color, depth, stencil, descriptor = null, setFrameBuffer = true) { + const { gl } = this; + + if (descriptor === null) { + descriptor = { + textures: null, + clearColorValue: this.getClearColor(), + }; + } + + // + + let clear = 0; + + if (color) clear |= gl.COLOR_BUFFER_BIT; + if (depth) clear |= gl.DEPTH_BUFFER_BIT; + if (stencil) clear |= gl.STENCIL_BUFFER_BIT; + + if (clear !== 0) { + const clearColor = descriptor.clearColorValue || this.getClearColor(); + + if (depth) this.state.setDepthMask(true); + + if (descriptor.textures === null) { + gl.clearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a); + gl.clear(clear); + } else { + if (setFrameBuffer) this._setFramebuffer(descriptor); + + if (color) { + for (let i = 0; i < descriptor.textures.length; i++) { + gl.clearBufferfv(gl.COLOR, i, [clearColor.r, clearColor.g, clearColor.b, clearColor.a]); + } + } + + if (depth && stencil) { + gl.clearBufferfi(gl.DEPTH_STENCIL, 0, 1, 0); + } else if (depth) { + gl.clearBufferfv(gl.DEPTH, 0, [1.0]); + } else if (stencil) { + gl.clearBufferiv(gl.STENCIL, 0, [0]); + } + } + } + } + + beginCompute(computeGroup) { + const gl = this.gl; + + gl.bindFramebuffer(gl.FRAMEBUFFER, null); + this.initTimestampQuery(computeGroup); + } + + compute(computeGroup, computeNode, bindings, pipeline) { + const gl = this.gl; + + if (!this.discard) { + // required here to handle async behaviour of render.compute() + gl.enable(gl.RASTERIZER_DISCARD); + this.discard = true; + } + + const { programGPU, transformBuffers, attributes } = this.get(pipeline); + + const vaoKey = this._getVaoKey(null, attributes); + + const vaoGPU = this.vaoCache[vaoKey]; + + if (vaoGPU === undefined) { + this._createVao(null, attributes); + } else { + gl.bindVertexArray(vaoGPU); + } + + gl.useProgram(programGPU); + + this._bindUniforms(bindings); + + const transformFeedbackGPU = this._getTransformFeedback(transformBuffers); + + gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, transformFeedbackGPU); + gl.beginTransformFeedback(gl.POINTS); + + if (attributes[0].isStorageInstancedBufferAttribute) { + gl.drawArraysInstanced(gl.POINTS, 0, 1, computeNode.count); + } else { + gl.drawArrays(gl.POINTS, 0, computeNode.count); + } + + gl.endTransformFeedback(); + gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, null); + + // switch active buffers + + for (let i = 0; i < transformBuffers.length; i++) { + const dualAttributeData = transformBuffers[i]; + + if (dualAttributeData.pbo) { + this.textureUtils.copyBufferToTexture(dualAttributeData.transformBuffer, dualAttributeData.pbo); + } + + dualAttributeData.switchBuffers(); + } + } + + finishCompute(computeGroup) { + const gl = this.gl; + + this.discard = false; + + gl.disable(gl.RASTERIZER_DISCARD); + + this.prepareTimestampBuffer(computeGroup); + } + + draw(renderObject /*, info*/) { + const { object, pipeline, material, context } = renderObject; + const { programGPU } = this.get(pipeline); + + const { gl, state } = this; + + const contextData = this.get(context); + + // + + this._bindUniforms(renderObject.getBindings()); + + const frontFaceCW = object.isMesh && object.matrixWorld.determinant() < 0; + + state.setMaterial(material, frontFaceCW); + + gl.useProgram(programGPU); + + // + + let vaoGPU = renderObject.staticVao; + + if (vaoGPU === undefined) { + const vaoKey = this._getVaoKey(renderObject.getIndex(), renderObject.getAttributes()); + + vaoGPU = this.vaoCache[vaoKey]; + + if (vaoGPU === undefined) { + let staticVao; + + ({ vaoGPU, staticVao } = this._createVao(renderObject.getIndex(), renderObject.getAttributes())); + + if (staticVao) renderObject.staticVao = vaoGPU; + } + } + + gl.bindVertexArray(vaoGPU); + + // + + const index = renderObject.getIndex(); + + const geometry = renderObject.geometry; + const drawRange = renderObject.drawRange; + const firstVertex = drawRange.start; + + // + + const lastObject = contextData.lastOcclusionObject; + + if (lastObject !== object && lastObject !== undefined) { + if (lastObject !== null && lastObject.occlusionTest === true) { + gl.endQuery(gl.ANY_SAMPLES_PASSED); + + contextData.occlusionQueryIndex++; + } + + if (object.occlusionTest === true) { + const query = gl.createQuery(); + + gl.beginQuery(gl.ANY_SAMPLES_PASSED, query); + + contextData.occlusionQueries[contextData.occlusionQueryIndex] = query; + contextData.occlusionQueryObjects[contextData.occlusionQueryIndex] = object; + } + + contextData.lastOcclusionObject = object; + } + + // + + const renderer = this.bufferRenderer; + + if (object.isPoints) renderer.mode = gl.POINTS; + else if (object.isLineSegments) renderer.mode = gl.LINES; + else if (object.isLine) renderer.mode = gl.LINE_STRIP; + else if (object.isLineLoop) renderer.mode = gl.LINE_LOOP; + else { + if (material.wireframe === true) { + state.setLineWidth(material.wireframeLinewidth * this.renderer.getPixelRatio()); + renderer.mode = gl.LINES; + } else { + renderer.mode = gl.TRIANGLES; + } + } + + // + + let count; + + renderer.object = object; + + if (index !== null) { + const indexData = this.get(index); + const indexCount = drawRange.count !== Infinity ? drawRange.count : index.count; + + renderer.index = index.count; + renderer.type = indexData.type; + + count = indexCount; + } else { + renderer.index = 0; + + const vertexCount = drawRange.count !== Infinity ? drawRange.count : geometry.attributes.position.count; + + count = vertexCount; + } + + const instanceCount = this.getInstanceCount(renderObject); + + if (object.isBatchedMesh) { + if (object._multiDrawInstances !== null) { + renderer.renderMultiDrawInstances( + object._multiDrawStarts, + object._multiDrawCounts, + object._multiDrawCount, + object._multiDrawInstances, + ); + } else if (!this.hasFeature('WEBGL_multi_draw')) { + warnOnce('THREE.WebGLRenderer: WEBGL_multi_draw not supported.'); + } else { + renderer.renderMultiDraw(object._multiDrawStarts, object._multiDrawCounts, object._multiDrawCount); + } + } else if (instanceCount > 1) { + renderer.renderInstances(firstVertex, count, instanceCount); + } else { + renderer.render(firstVertex, count); + } + // + + gl.bindVertexArray(null); + } + + needsRenderUpdate(/*renderObject*/) { + return false; + } + + getRenderCacheKey(renderObject) { + return renderObject.id; + } + + // textures + + createDefaultTexture(texture) { + this.textureUtils.createDefaultTexture(texture); + } + + createTexture(texture, options) { + this.textureUtils.createTexture(texture, options); + } + + updateTexture(texture, options) { + this.textureUtils.updateTexture(texture, options); + } + + generateMipmaps(texture) { + this.textureUtils.generateMipmaps(texture); + } + + destroyTexture(texture) { + this.textureUtils.destroyTexture(texture); + } + + copyTextureToBuffer(texture, x, y, width, height) { + return this.textureUtils.copyTextureToBuffer(texture, x, y, width, height); + } + + createSampler(/*texture*/) { + //console.warn( 'Abstract class.' ); + } + + destroySampler() {} + + // node builder + + createNodeBuilder(object, renderer) { + return new GLSLNodeBuilder(object, renderer); + } + + // program + + createProgram(program) { + const gl = this.gl; + const { stage, code } = program; + + const shader = stage === 'fragment' ? gl.createShader(gl.FRAGMENT_SHADER) : gl.createShader(gl.VERTEX_SHADER); + + gl.shaderSource(shader, code); + gl.compileShader(shader); + + this.set(program, { + shaderGPU: shader, + }); + } + + destroyProgram(/*program*/) { + console.warn('Abstract class.'); + } + + createRenderPipeline(renderObject, promises) { + const gl = this.gl; + const pipeline = renderObject.pipeline; + + // Program + + const { fragmentProgram, vertexProgram } = pipeline; + + const programGPU = gl.createProgram(); + + const fragmentShader = this.get(fragmentProgram).shaderGPU; + const vertexShader = this.get(vertexProgram).shaderGPU; + + gl.attachShader(programGPU, fragmentShader); + gl.attachShader(programGPU, vertexShader); + gl.linkProgram(programGPU); + + this.set(pipeline, { + programGPU, + fragmentShader, + vertexShader, + }); + + if (promises !== null && this.parallel) { + const p = new Promise((resolve /*, reject*/) => { + const parallel = this.parallel; + const checkStatus = () => { + if (gl.getProgramParameter(programGPU, parallel.COMPLETION_STATUS_KHR)) { + this._completeCompile(renderObject, pipeline); + resolve(); + } else { + requestAnimationFrame(checkStatus); + } + }; + + checkStatus(); + }); + + promises.push(p); + + return; + } + + this._completeCompile(renderObject, pipeline); + } + + _handleSource(string, errorLine) { + const lines = string.split('\n'); + const lines2 = []; + + const from = Math.max(errorLine - 6, 0); + const to = Math.min(errorLine + 6, lines.length); + + for (let i = from; i < to; i++) { + const line = i + 1; + lines2.push(`${line === errorLine ? '>' : ' '} ${line}: ${lines[i]}`); + } + + return lines2.join('\n'); + } + + _getShaderErrors(gl, shader, type) { + const status = gl.getShaderParameter(shader, gl.COMPILE_STATUS); + const errors = gl.getShaderInfoLog(shader).trim(); + + if (status && errors === '') return ''; + + const errorMatches = /ERROR: 0:(\d+)/.exec(errors); + if (errorMatches) { + const errorLine = parseInt(errorMatches[1]); + return ( + type.toUpperCase() + + '\n\n' + + errors + + '\n\n' + + this._handleSource(gl.getShaderSource(shader), errorLine) + ); + } else { + return errors; + } + } + + _logProgramError(programGPU, glFragmentShader, glVertexShader) { + if (this.renderer.debug.checkShaderErrors) { + const gl = this.gl; + + const programLog = gl.getProgramInfoLog(programGPU).trim(); + + if (gl.getProgramParameter(programGPU, gl.LINK_STATUS) === false) { + if (typeof this.renderer.debug.onShaderError === 'function') { + this.renderer.debug.onShaderError(gl, programGPU, glVertexShader, glFragmentShader); + } else { + // default error reporting + + const vertexErrors = this._getShaderErrors(gl, glVertexShader, 'vertex'); + const fragmentErrors = this._getShaderErrors(gl, glFragmentShader, 'fragment'); + + console.error( + 'THREE.WebGLProgram: Shader Error ' + + gl.getError() + + ' - ' + + 'VALIDATE_STATUS ' + + gl.getProgramParameter(programGPU, gl.VALIDATE_STATUS) + + '\n\n' + + 'Program Info Log: ' + + programLog + + '\n' + + vertexErrors + + '\n' + + fragmentErrors, + ); + } + } else if (programLog !== '') { + console.warn('THREE.WebGLProgram: Program Info Log:', programLog); + } + } + } + + _completeCompile(renderObject, pipeline) { + const gl = this.gl; + const pipelineData = this.get(pipeline); + const { programGPU, fragmentShader, vertexShader } = pipelineData; + + if (gl.getProgramParameter(programGPU, gl.LINK_STATUS) === false) { + this._logProgramError(programGPU, fragmentShader, vertexShader); + } + + gl.useProgram(programGPU); + + // Bindings + + const bindings = renderObject.getBindings(); + + this._setupBindings(bindings, programGPU); + + // + + this.set(pipeline, { + programGPU, + }); + } + + createComputePipeline(computePipeline, bindings) { + const gl = this.gl; + + // Program + + const fragmentProgram = { + stage: 'fragment', + code: '#version 300 es\nprecision highp float;\nvoid main() {}', + }; + + this.createProgram(fragmentProgram); + + const { computeProgram } = computePipeline; + + const programGPU = gl.createProgram(); + + const fragmentShader = this.get(fragmentProgram).shaderGPU; + const vertexShader = this.get(computeProgram).shaderGPU; + + const transforms = computeProgram.transforms; + + const transformVaryingNames = []; + const transformAttributeNodes = []; + + for (let i = 0; i < transforms.length; i++) { + const transform = transforms[i]; + + transformVaryingNames.push(transform.varyingName); + transformAttributeNodes.push(transform.attributeNode); + } + + gl.attachShader(programGPU, fragmentShader); + gl.attachShader(programGPU, vertexShader); + + gl.transformFeedbackVaryings(programGPU, transformVaryingNames, gl.SEPARATE_ATTRIBS); + + gl.linkProgram(programGPU); + + if (gl.getProgramParameter(programGPU, gl.LINK_STATUS) === false) { + this._logProgramError(programGPU, fragmentShader, vertexShader); + } + + gl.useProgram(programGPU); + + // Bindings + + this.createBindings(null, bindings); + + this._setupBindings(bindings, programGPU); + + const attributeNodes = computeProgram.attributes; + const attributes = []; + const transformBuffers = []; + + for (let i = 0; i < attributeNodes.length; i++) { + const attribute = attributeNodes[i].node.attribute; + + attributes.push(attribute); + + if (!this.has(attribute)) this.attributeUtils.createAttribute(attribute, gl.ARRAY_BUFFER); + } + + for (let i = 0; i < transformAttributeNodes.length; i++) { + const attribute = transformAttributeNodes[i].attribute; + + if (!this.has(attribute)) this.attributeUtils.createAttribute(attribute, gl.ARRAY_BUFFER); + + const attributeData = this.get(attribute); + + transformBuffers.push(attributeData); + } + + // + + this.set(computePipeline, { + programGPU, + transformBuffers, + attributes, + }); + } + + createBindings(bindGroup, bindings) { + this.updateBindings(bindGroup, bindings); + } + + updateBindings(bindGroup, bindings) { + const { gl } = this; + + let groupIndex = 0; + let textureIndex = 0; + + for (const bindGroup of bindings) { + for (const binding of bindGroup.bindings) { + if (binding.isUniformsGroup || binding.isUniformBuffer) { + const bufferGPU = gl.createBuffer(); + const data = binding.buffer; + + gl.bindBuffer(gl.UNIFORM_BUFFER, bufferGPU); + gl.bufferData(gl.UNIFORM_BUFFER, data, gl.DYNAMIC_DRAW); + gl.bindBufferBase(gl.UNIFORM_BUFFER, groupIndex, bufferGPU); + + this.set(binding, { + index: groupIndex++, + bufferGPU, + }); + } else if (binding.isSampledTexture) { + const { textureGPU, glTextureType } = this.get(binding.texture); + + this.set(binding, { + index: textureIndex++, + textureGPU, + glTextureType, + }); + } + } + } + } + + updateBinding(binding) { + const gl = this.gl; + + if (binding.isUniformsGroup || binding.isUniformBuffer) { + const bindingData = this.get(binding); + const bufferGPU = bindingData.bufferGPU; + const data = binding.buffer; + + gl.bindBuffer(gl.UNIFORM_BUFFER, bufferGPU); + gl.bufferData(gl.UNIFORM_BUFFER, data, gl.DYNAMIC_DRAW); + } + } + + // attributes + + createIndexAttribute(attribute) { + const gl = this.gl; + + this.attributeUtils.createAttribute(attribute, gl.ELEMENT_ARRAY_BUFFER); + } + + createAttribute(attribute) { + if (this.has(attribute)) return; + + const gl = this.gl; + + this.attributeUtils.createAttribute(attribute, gl.ARRAY_BUFFER); + } + + createStorageAttribute(attribute) { + if (this.has(attribute)) return; + + const gl = this.gl; + + this.attributeUtils.createAttribute(attribute, gl.ARRAY_BUFFER); + } + + updateAttribute(attribute) { + this.attributeUtils.updateAttribute(attribute); + } + + destroyAttribute(attribute) { + this.attributeUtils.destroyAttribute(attribute); + } + + updateSize() { + //console.warn( 'Abstract class.' ); + } + + hasFeature(name) { + const keysMatching = Object.keys(GLFeatureName).filter(key => GLFeatureName[key] === name); + + const extensions = this.extensions; + + for (let i = 0; i < keysMatching.length; i++) { + if (extensions.has(keysMatching[i])) return true; + } + + return false; + } + + getMaxAnisotropy() { + return this.capabilities.getMaxAnisotropy(); + } + + copyTextureToTexture(position, srcTexture, dstTexture, level) { + this.textureUtils.copyTextureToTexture(position, srcTexture, dstTexture, level); + } + + copyFramebufferToTexture(texture, renderContext) { + this.textureUtils.copyFramebufferToTexture(texture, renderContext); + } + + _setFramebuffer(descriptor) { + const { gl, state } = this; + + let currentFrameBuffer = null; + + if (descriptor.textures !== null) { + const renderTarget = descriptor.renderTarget; + const renderTargetContextData = this.get(renderTarget); + const { samples, depthBuffer, stencilBuffer } = renderTarget; + + const isCube = renderTarget.isWebGLCubeRenderTarget === true; + + let msaaFb = renderTargetContextData.msaaFrameBuffer; + let depthRenderbuffer = renderTargetContextData.depthRenderbuffer; + + const cacheKey = getCacheKey(descriptor); + + let fb; + + if (isCube) { + renderTargetContextData.cubeFramebuffers || (renderTargetContextData.cubeFramebuffers = {}); + + fb = renderTargetContextData.cubeFramebuffers[cacheKey]; + } else { + renderTargetContextData.framebuffers || (renderTargetContextData.framebuffers = {}); + + fb = renderTargetContextData.framebuffers[cacheKey]; + } + + if (fb === undefined) { + fb = gl.createFramebuffer(); + + state.bindFramebuffer(gl.FRAMEBUFFER, fb); + + const textures = descriptor.textures; + + if (isCube) { + renderTargetContextData.cubeFramebuffers[cacheKey] = fb; + + const { textureGPU } = this.get(textures[0]); + + const cubeFace = this.renderer._activeCubeFace; + + gl.framebufferTexture2D( + gl.FRAMEBUFFER, + gl.COLOR_ATTACHMENT0, + gl.TEXTURE_CUBE_MAP_POSITIVE_X + cubeFace, + textureGPU, + 0, + ); + } else { + renderTargetContextData.framebuffers[cacheKey] = fb; + + for (let i = 0; i < textures.length; i++) { + const texture = textures[i]; + const textureData = this.get(texture); + textureData.renderTarget = descriptor.renderTarget; + + const attachment = gl.COLOR_ATTACHMENT0 + i; + + gl.framebufferTexture2D(gl.FRAMEBUFFER, attachment, gl.TEXTURE_2D, textureData.textureGPU, 0); + } + + state.drawBuffers(descriptor, fb); + } + + if (descriptor.depthTexture !== null) { + const textureData = this.get(descriptor.depthTexture); + const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT; + + gl.framebufferTexture2D(gl.FRAMEBUFFER, depthStyle, gl.TEXTURE_2D, textureData.textureGPU, 0); + } + } + + if (samples > 0) { + if (msaaFb === undefined) { + const invalidationArray = []; + + msaaFb = gl.createFramebuffer(); + + state.bindFramebuffer(gl.FRAMEBUFFER, msaaFb); + + const msaaRenderbuffers = []; + + const textures = descriptor.textures; + + for (let i = 0; i < textures.length; i++) { + msaaRenderbuffers[i] = gl.createRenderbuffer(); + + gl.bindRenderbuffer(gl.RENDERBUFFER, msaaRenderbuffers[i]); + + invalidationArray.push(gl.COLOR_ATTACHMENT0 + i); + + if (depthBuffer) { + const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT; + invalidationArray.push(depthStyle); + } + + const texture = descriptor.textures[i]; + const textureData = this.get(texture); + + gl.renderbufferStorageMultisample( + gl.RENDERBUFFER, + samples, + textureData.glInternalFormat, + descriptor.width, + descriptor.height, + ); + gl.framebufferRenderbuffer( + gl.FRAMEBUFFER, + gl.COLOR_ATTACHMENT0 + i, + gl.RENDERBUFFER, + msaaRenderbuffers[i], + ); + } + + renderTargetContextData.msaaFrameBuffer = msaaFb; + renderTargetContextData.msaaRenderbuffers = msaaRenderbuffers; + + if (depthRenderbuffer === undefined) { + depthRenderbuffer = gl.createRenderbuffer(); + this.textureUtils.setupRenderBufferStorage(depthRenderbuffer, descriptor); + + renderTargetContextData.depthRenderbuffer = depthRenderbuffer; + + const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT; + invalidationArray.push(depthStyle); + } + + renderTargetContextData.invalidationArray = invalidationArray; + } + + currentFrameBuffer = renderTargetContextData.msaaFrameBuffer; + } else { + currentFrameBuffer = fb; + } + } + + state.bindFramebuffer(gl.FRAMEBUFFER, currentFrameBuffer); + } + + _getVaoKey(index, attributes) { + let key = []; + + if (index !== null) { + const indexData = this.get(index); + + key += ':' + indexData.id; + } + + for (let i = 0; i < attributes.length; i++) { + const attributeData = this.get(attributes[i]); + + key += ':' + attributeData.id; + } + + return key; + } + + _createVao(index, attributes) { + const { gl } = this; + + const vaoGPU = gl.createVertexArray(); + let key = ''; + + let staticVao = true; + + gl.bindVertexArray(vaoGPU); + + if (index !== null) { + const indexData = this.get(index); + + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexData.bufferGPU); + + key += ':' + indexData.id; + } + + for (let i = 0; i < attributes.length; i++) { + const attribute = attributes[i]; + const attributeData = this.get(attribute); + + key += ':' + attributeData.id; + + gl.bindBuffer(gl.ARRAY_BUFFER, attributeData.bufferGPU); + gl.enableVertexAttribArray(i); + + if (attribute.isStorageBufferAttribute || attribute.isStorageInstancedBufferAttribute) staticVao = false; + + let stride, offset; + + if (attribute.isInterleavedBufferAttribute === true) { + stride = attribute.data.stride * attributeData.bytesPerElement; + offset = attribute.offset * attributeData.bytesPerElement; + } else { + stride = 0; + offset = 0; + } + + if (attributeData.isInteger) { + gl.vertexAttribIPointer(i, attribute.itemSize, attributeData.type, stride, offset); + } else { + gl.vertexAttribPointer(i, attribute.itemSize, attributeData.type, attribute.normalized, stride, offset); + } + + if (attribute.isInstancedBufferAttribute && !attribute.isInterleavedBufferAttribute) { + gl.vertexAttribDivisor(i, attribute.meshPerAttribute); + } else if (attribute.isInterleavedBufferAttribute && attribute.data.isInstancedInterleavedBuffer) { + gl.vertexAttribDivisor(i, attribute.data.meshPerAttribute); + } + } + + gl.bindBuffer(gl.ARRAY_BUFFER, null); + + this.vaoCache[key] = vaoGPU; + + return { vaoGPU, staticVao }; + } + + _getTransformFeedback(transformBuffers) { + let key = ''; + + for (let i = 0; i < transformBuffers.length; i++) { + key += ':' + transformBuffers[i].id; + } + + let transformFeedbackGPU = this.transformFeedbackCache[key]; + + if (transformFeedbackGPU !== undefined) { + return transformFeedbackGPU; + } + + const gl = this.gl; + + transformFeedbackGPU = gl.createTransformFeedback(); + + gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, transformFeedbackGPU); + + for (let i = 0; i < transformBuffers.length; i++) { + const attributeData = transformBuffers[i]; + + gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, i, attributeData.transformBuffer); + } + + gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, null); + + this.transformFeedbackCache[key] = transformFeedbackGPU; + + return transformFeedbackGPU; + } + + _setupBindings(bindings, programGPU) { + const gl = this.gl; + + for (const bindGroup of bindings) { + for (const binding of bindGroup.bindings) { + const bindingData = this.get(binding); + const index = bindingData.index; + + if (binding.isUniformsGroup || binding.isUniformBuffer) { + const location = gl.getUniformBlockIndex(programGPU, binding.name); + gl.uniformBlockBinding(programGPU, location, index); + } else if (binding.isSampledTexture) { + const location = gl.getUniformLocation(programGPU, binding.name); + gl.uniform1i(location, index); + } + } + } + } + + _bindUniforms(bindings) { + const { gl, state } = this; + + for (const bindGroup of bindings) { + for (const binding of bindGroup.bindings) { + const bindingData = this.get(binding); + const index = bindingData.index; + + if (binding.isUniformsGroup || binding.isUniformBuffer) { + gl.bindBufferBase(gl.UNIFORM_BUFFER, index, bindingData.bufferGPU); + } else if (binding.isSampledTexture) { + state.bindTexture(bindingData.glTextureType, bindingData.textureGPU, gl.TEXTURE0 + index); + } + } + } + } +} + +export default WebGLBackend; diff --git a/src-testing/src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.ts b/src-testing/src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.ts new file mode 100644 index 000000000..ec5211b5c --- /dev/null +++ b/src-testing/src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.ts @@ -0,0 +1,794 @@ +import { MathNode, GLSLNodeParser, NodeBuilder, TextureNode, vectorComponents } from '../../../nodes/Nodes.js'; + +import NodeUniformBuffer from '../../common/nodes/NodeUniformBuffer.js'; +import NodeUniformsGroup from '../../common/nodes/NodeUniformsGroup.js'; + +import { + NodeSampledTexture, + NodeSampledCubeTexture, + NodeSampledTexture3D, +} from '../../common/nodes/NodeSampledTexture.js'; + +import { + ByteType, + ShortType, + RGBAIntegerFormat, + RGBIntegerFormat, + RedIntegerFormat, + RGIntegerFormat, + UnsignedByteType, + UnsignedIntType, + UnsignedShortType, + RedFormat, + RGFormat, + IntType, + RGBFormat, + RGBAFormat, + FloatType, +} from '../../../constants.js'; +import { DataTexture } from '../../../textures/DataTexture.js'; + +const glslMethods = { + [MathNode.ATAN2]: 'atan', + textureDimensions: 'textureSize', + equals: 'equal', +}; + +const precisionLib = { + low: 'lowp', + medium: 'mediump', + high: 'highp', +}; + +const supports = { + swizzleAssign: true, + storageBuffer: false, +}; + +const defaultPrecisions = ` +precision highp float; +precision highp int; +precision highp sampler2D; +precision highp sampler3D; +precision highp samplerCube; +precision highp sampler2DArray; + +precision highp usampler2D; +precision highp usampler3D; +precision highp usamplerCube; +precision highp usampler2DArray; + +precision highp isampler2D; +precision highp isampler3D; +precision highp isamplerCube; +precision highp isampler2DArray; + +precision lowp sampler2DShadow; +`; + +class GLSLNodeBuilder extends NodeBuilder { + constructor(object, renderer) { + super(object, renderer, new GLSLNodeParser()); + + this.uniformGroups = {}; + this.transforms = []; + this.extensions = {}; + + this.instanceBindGroups = false; + + this.useComparisonMethod = true; + } + + getMethod(method) { + return glslMethods[method] || method; + } + + getOutputStructName() { + return ''; + } + + buildFunctionCode(shaderNode) { + const layout = shaderNode.layout; + const flowData = this.flowShaderNode(shaderNode); + + const parameters = []; + + for (const input of layout.inputs) { + parameters.push(this.getType(input.type) + ' ' + input.name); + } + + // + + const code = `${this.getType(layout.type)} ${layout.name}( ${parameters.join(', ')} ) { + + ${flowData.vars} + +${flowData.code} + return ${flowData.result}; + +}`; + + // + + return code; + } + + setupPBO(storageBufferNode) { + const attribute = storageBufferNode.value; + + if (attribute.pbo === undefined) { + const originalArray = attribute.array; + const numElements = attribute.count * attribute.itemSize; + + const { itemSize } = attribute; + + const isInteger = attribute.array.constructor.name.toLowerCase().includes('int'); + + let format = isInteger ? RedIntegerFormat : RedFormat; + + if (itemSize === 2) { + format = isInteger ? RGIntegerFormat : RGFormat; + } else if (itemSize === 3) { + format = isInteger ? RGBIntegerFormat : RGBFormat; + } else if (itemSize === 4) { + format = isInteger ? RGBAIntegerFormat : RGBAFormat; + } + + const typeMap = { + Float32Array: FloatType, + Uint8Array: UnsignedByteType, + Uint16Array: UnsignedShortType, + Uint32Array: UnsignedIntType, + Int8Array: ByteType, + Int16Array: ShortType, + Int32Array: IntType, + Uint8ClampedArray: UnsignedByteType, + }; + + const width = Math.pow(2, Math.ceil(Math.log2(Math.sqrt(numElements / itemSize)))); + let height = Math.ceil(numElements / itemSize / width); + if (width * height * itemSize < numElements) height++; // Ensure enough space + + const newSize = width * height * itemSize; + + const newArray = new originalArray.constructor(newSize); + + newArray.set(originalArray, 0); + + attribute.array = newArray; + + const pboTexture = new DataTexture( + attribute.array, + width, + height, + format, + typeMap[attribute.array.constructor.name] || FloatType, + ); + pboTexture.needsUpdate = true; + pboTexture.isPBOTexture = true; + + const pbo = new TextureNode(pboTexture, null, null); + pbo.setPrecision('high'); + + attribute.pboNode = pbo; + attribute.pbo = pbo.value; + + this.getUniformFromNode(attribute.pboNode, 'texture', this.shaderStage, this.context.label); + } + } + + getPropertyName(node, shaderStage = this.shaderStage) { + if (node.isNodeUniform && node.node.isTextureNode !== true && node.node.isBufferNode !== true) { + return shaderStage.charAt(0) + '_' + node.name; + } + + return super.getPropertyName(node, shaderStage); + } + + generatePBO(storageArrayElementNode) { + const { node, indexNode } = storageArrayElementNode; + const attribute = node.value; + + if (this.renderer.backend.has(attribute)) { + const attributeData = this.renderer.backend.get(attribute); + attributeData.pbo = attribute.pbo; + } + + const nodeUniform = this.getUniformFromNode(attribute.pboNode, 'texture', this.shaderStage, this.context.label); + const textureName = this.getPropertyName(nodeUniform); + + indexNode.increaseUsage(this); // force cache generate to be used as index in x,y + const indexSnippet = indexNode.build(this, 'uint'); + + const elementNodeData = this.getDataFromNode(storageArrayElementNode); + + let propertyName = elementNodeData.propertyName; + + if (propertyName === undefined) { + // property element + + const nodeVar = this.getVarFromNode(storageArrayElementNode); + + propertyName = this.getPropertyName(nodeVar); + + // property size + + const bufferNodeData = this.getDataFromNode(node); + + let propertySizeName = bufferNodeData.propertySizeName; + + if (propertySizeName === undefined) { + propertySizeName = propertyName + 'Size'; + + this.getVarFromNode(node, propertySizeName, 'uint'); + + this.addLineFlowCode(`${propertySizeName} = uint( textureSize( ${textureName}, 0 ).x )`); + + bufferNodeData.propertySizeName = propertySizeName; + } + + // + + const { itemSize } = attribute; + + const channel = '.' + vectorComponents.join('').slice(0, itemSize); + const uvSnippet = `ivec2(${indexSnippet} % ${propertySizeName}, ${indexSnippet} / ${propertySizeName})`; + + const snippet = this.generateTextureLoad(null, textureName, uvSnippet, null, '0'); + + // + + let prefix = 'vec4'; + + if (attribute.pbo.type === UnsignedIntType) { + prefix = 'uvec4'; + } else if (attribute.pbo.type === IntType) { + prefix = 'ivec4'; + } + + this.addLineFlowCode(`${propertyName} = ${prefix}(${snippet})${channel}`); + + elementNodeData.propertyName = propertyName; + } + + return propertyName; + } + + generateTextureLoad(texture, textureProperty, uvIndexSnippet, depthSnippet, levelSnippet = '0') { + if (depthSnippet) { + return `texelFetch( ${textureProperty}, ivec3( ${uvIndexSnippet}, ${depthSnippet} ), ${levelSnippet} )`; + } else { + return `texelFetch( ${textureProperty}, ${uvIndexSnippet}, ${levelSnippet} )`; + } + } + + generateTexture(texture, textureProperty, uvSnippet, depthSnippet) { + if (texture.isDepthTexture) { + return `texture( ${textureProperty}, ${uvSnippet} ).x`; + } else { + if (depthSnippet) uvSnippet = `vec3( ${uvSnippet}, ${depthSnippet} )`; + + return `texture( ${textureProperty}, ${uvSnippet} )`; + } + } + + generateTextureLevel(texture, textureProperty, uvSnippet, levelSnippet) { + return `textureLod( ${textureProperty}, ${uvSnippet}, ${levelSnippet} )`; + } + + generateTextureBias(texture, textureProperty, uvSnippet, biasSnippet) { + return `texture( ${textureProperty}, ${uvSnippet}, ${biasSnippet} )`; + } + + generateTextureGrad(texture, textureProperty, uvSnippet, gradSnippet) { + return `textureGrad( ${textureProperty}, ${uvSnippet}, ${gradSnippet[0]}, ${gradSnippet[1]} )`; + } + + generateTextureCompare( + texture, + textureProperty, + uvSnippet, + compareSnippet, + depthSnippet, + shaderStage = this.shaderStage, + ) { + if (shaderStage === 'fragment') { + return `texture( ${textureProperty}, vec3( ${uvSnippet}, ${compareSnippet} ) )`; + } else { + console.error( + `WebGPURenderer: THREE.DepthTexture.compareFunction() does not support ${shaderStage} shader.`, + ); + } + } + + getVars(shaderStage) { + const snippets = []; + + const vars = this.vars[shaderStage]; + + if (vars !== undefined) { + for (const variable of vars) { + snippets.push(`${this.getVar(variable.type, variable.name)};`); + } + } + + return snippets.join('\n\t'); + } + + getUniforms(shaderStage) { + const uniforms = this.uniforms[shaderStage]; + + const bindingSnippets = []; + const uniformGroups = {}; + + for (const uniform of uniforms) { + let snippet = null; + let group = false; + + if (uniform.type === 'texture') { + const texture = uniform.node.value; + + let typePrefix = ''; + + if (texture.isDataTexture === true) { + if (texture.type === UnsignedIntType) { + typePrefix = 'u'; + } else if (texture.type === IntType) { + typePrefix = 'i'; + } + } + + if (texture.compareFunction) { + snippet = `sampler2DShadow ${uniform.name};`; + } else if (texture.isDataArrayTexture === true) { + snippet = `${typePrefix}sampler2DArray ${uniform.name};`; + } else { + snippet = `${typePrefix}sampler2D ${uniform.name};`; + } + } else if (uniform.type === 'cubeTexture') { + snippet = `samplerCube ${uniform.name};`; + } else if (uniform.type === 'texture3D') { + snippet = `sampler3D ${uniform.name};`; + } else if (uniform.type === 'buffer') { + const bufferNode = uniform.node; + const bufferType = this.getType(bufferNode.bufferType); + const bufferCount = bufferNode.bufferCount; + + const bufferCountSnippet = bufferCount > 0 ? bufferCount : ''; + snippet = `${bufferNode.name} {\n\t${bufferType} ${uniform.name}[${bufferCountSnippet}];\n};\n`; + } else { + const vectorType = this.getVectorType(uniform.type); + + snippet = `${vectorType} ${this.getPropertyName(uniform, shaderStage)};`; + + group = true; + } + + const precision = uniform.node.precision; + + if (precision !== null) { + snippet = precisionLib[precision] + ' ' + snippet; + } + + if (group) { + snippet = '\t' + snippet; + + const groupName = uniform.groupNode.name; + const groupSnippets = uniformGroups[groupName] || (uniformGroups[groupName] = []); + + groupSnippets.push(snippet); + } else { + snippet = 'uniform ' + snippet; + + bindingSnippets.push(snippet); + } + } + + let output = ''; + + for (const name in uniformGroups) { + const groupSnippets = uniformGroups[name]; + + output += this._getGLSLUniformStruct(shaderStage + '_' + name, groupSnippets.join('\n')) + '\n'; + } + + output += bindingSnippets.join('\n'); + + return output; + } + + getTypeFromAttribute(attribute) { + let nodeType = super.getTypeFromAttribute(attribute); + + if (/^[iu]/.test(nodeType) && attribute.gpuType !== IntType) { + let dataAttribute = attribute; + + if (attribute.isInterleavedBufferAttribute) dataAttribute = attribute.data; + + const array = dataAttribute.array; + + if ((array instanceof Uint32Array || array instanceof Int32Array) === false) { + nodeType = nodeType.slice(1); + } + } + + return nodeType; + } + + getAttributes(shaderStage) { + let snippet = ''; + + if (shaderStage === 'vertex' || shaderStage === 'compute') { + const attributes = this.getAttributesArray(); + + let location = 0; + + for (const attribute of attributes) { + snippet += `layout( location = ${location++} ) in ${attribute.type} ${attribute.name};\n`; + } + } + + return snippet; + } + + getStructMembers(struct) { + const snippets = []; + const members = struct.getMemberTypes(); + + for (let i = 0; i < members.length; i++) { + const member = members[i]; + snippets.push(`layout( location = ${i} ) out ${member} m${i};`); + } + + return snippets.join('\n'); + } + + getStructs(shaderStage) { + const snippets = []; + const structs = this.structs[shaderStage]; + + if (structs.length === 0) { + return 'layout( location = 0 ) out vec4 fragColor;\n'; + } + + for (let index = 0, length = structs.length; index < length; index++) { + const struct = structs[index]; + + let snippet = '\n'; + snippet += this.getStructMembers(struct); + snippet += '\n'; + + snippets.push(snippet); + } + + return snippets.join('\n\n'); + } + + getVaryings(shaderStage) { + let snippet = ''; + + const varyings = this.varyings; + + if (shaderStage === 'vertex' || shaderStage === 'compute') { + for (const varying of varyings) { + if (shaderStage === 'compute') varying.needsInterpolation = true; + const type = varying.type; + const flat = type.includes('int') || type.includes('uv') || type.includes('iv') ? 'flat ' : ''; + + snippet += `${flat}${varying.needsInterpolation ? 'out' : '/*out*/'} ${type} ${varying.name};\n`; + } + } else if (shaderStage === 'fragment') { + for (const varying of varyings) { + if (varying.needsInterpolation) { + const type = varying.type; + const flat = type.includes('int') || type.includes('uv') || type.includes('iv') ? 'flat ' : ''; + + snippet += `${flat}in ${type} ${varying.name};\n`; + } + } + } + + return snippet; + } + + getVertexIndex() { + return 'uint( gl_VertexID )'; + } + + getInstanceIndex() { + return 'uint( gl_InstanceID )'; + } + + getDrawIndex() { + const extensions = this.renderer.backend.extensions; + + if (extensions.has('WEBGL_multi_draw')) { + return 'uint( gl_DrawID )'; + } + + return null; + } + + getFrontFacing() { + return 'gl_FrontFacing'; + } + + getFragCoord() { + return 'gl_FragCoord'; + } + + getFragDepth() { + return 'gl_FragDepth'; + } + + enableExtension(name, behavior, shaderStage = this.shaderStage) { + const map = this.extensions[shaderStage] || (this.extensions[shaderStage] = new Map()); + + if (map.has(name) === false) { + map.set(name, { + name, + behavior, + }); + } + } + + getExtensions(shaderStage) { + const snippets = []; + + if (shaderStage === 'vertex') { + const ext = this.renderer.backend.extensions; + const isBatchedMesh = this.object.isBatchedMesh; + + if (isBatchedMesh && ext.has('WEBGL_multi_draw')) { + this.enableExtension('GL_ANGLE_multi_draw', 'require'); + } + } + + const extensions = this.extensions[shaderStage]; + + if (extensions !== undefined) { + for (const { name, behavior } of extensions.values()) { + snippets.push(`#extension ${name} : ${behavior}`); + } + } + + return snippets.join('\n'); + } + + isAvailable(name) { + let result = supports[name]; + + if (result === undefined) { + if (name === 'float32Filterable') { + const extensions = this.renderer.backend.extensions; + + if (extensions.has('OES_texture_float_linear')) { + extensions.get('OES_texture_float_linear'); + result = true; + } else { + result = false; + } + } + + supports[name] = result; + } + + return result; + } + + isFlipY() { + return true; + } + + registerTransform(varyingName, attributeNode) { + this.transforms.push({ varyingName, attributeNode }); + } + + getTransforms(/* shaderStage */) { + const transforms = this.transforms; + + let snippet = ''; + + for (let i = 0; i < transforms.length; i++) { + const transform = transforms[i]; + + const attributeName = this.getPropertyName(transform.attributeNode); + + snippet += `${transform.varyingName} = ${attributeName};\n\t`; + } + + return snippet; + } + + _getGLSLUniformStruct(name, vars) { + return ` +layout( std140 ) uniform ${name} { +${vars} +};`; + } + + _getGLSLVertexCode(shaderData) { + return `#version 300 es + +// extensions +${shaderData.extensions} + +// precision +${defaultPrecisions} + +// uniforms +${shaderData.uniforms} + +// varyings +${shaderData.varyings} + +// attributes +${shaderData.attributes} + +// codes +${shaderData.codes} + +void main() { + + // vars + ${shaderData.vars} + + // transforms + ${shaderData.transforms} + + // flow + ${shaderData.flow} + + gl_PointSize = 1.0; + +} +`; + } + + _getGLSLFragmentCode(shaderData) { + return `#version 300 es + +${this.getSignature()} + +// precision +${defaultPrecisions} + +// uniforms +${shaderData.uniforms} + +// varyings +${shaderData.varyings} + +// codes +${shaderData.codes} + +${shaderData.structs} + +void main() { + + // vars + ${shaderData.vars} + + // flow + ${shaderData.flow} + +} +`; + } + + buildCode() { + const shadersData = this.material !== null ? { fragment: {}, vertex: {} } : { compute: {} }; + + for (const shaderStage in shadersData) { + let flow = '// code\n\n'; + flow += this.flowCode[shaderStage]; + + const flowNodes = this.flowNodes[shaderStage]; + const mainNode = flowNodes[flowNodes.length - 1]; + + for (const node of flowNodes) { + const flowSlotData = this.getFlowData(node /*, shaderStage*/); + const slotName = node.name; + + if (slotName) { + if (flow.length > 0) flow += '\n'; + + flow += `\t// flow -> ${slotName}\n\t`; + } + + flow += `${flowSlotData.code}\n\t`; + + if (node === mainNode && shaderStage !== 'compute') { + flow += '// result\n\t'; + + if (shaderStage === 'vertex') { + flow += 'gl_Position = '; + flow += `${flowSlotData.result};`; + } else if (shaderStage === 'fragment') { + if (!node.outputNode.isOutputStructNode) { + flow += 'fragColor = '; + flow += `${flowSlotData.result};`; + } + } + } + } + + const stageData = shadersData[shaderStage]; + + stageData.extensions = this.getExtensions(shaderStage); + stageData.uniforms = this.getUniforms(shaderStage); + stageData.attributes = this.getAttributes(shaderStage); + stageData.varyings = this.getVaryings(shaderStage); + stageData.vars = this.getVars(shaderStage); + stageData.structs = this.getStructs(shaderStage); + stageData.codes = this.getCodes(shaderStage); + stageData.transforms = this.getTransforms(shaderStage); + stageData.flow = flow; + } + + if (this.material !== null) { + this.vertexShader = this._getGLSLVertexCode(shadersData.vertex); + this.fragmentShader = this._getGLSLFragmentCode(shadersData.fragment); + } else { + this.computeShader = this._getGLSLVertexCode(shadersData.compute); + } + } + + getUniformFromNode(node, type, shaderStage, name = null) { + const uniformNode = super.getUniformFromNode(node, type, shaderStage, name); + const nodeData = this.getDataFromNode(node, shaderStage, this.globalCache); + + let uniformGPU = nodeData.uniformGPU; + + if (uniformGPU === undefined) { + const group = node.groupNode; + const groupName = group.name; + + const bindings = this.getBindGroupArray(groupName, shaderStage); + + if (type === 'texture') { + uniformGPU = new NodeSampledTexture(uniformNode.name, uniformNode.node, group); + bindings.push(uniformGPU); + } else if (type === 'cubeTexture') { + uniformGPU = new NodeSampledCubeTexture(uniformNode.name, uniformNode.node, group); + bindings.push(uniformGPU); + } else if (type === 'texture3D') { + uniformGPU = new NodeSampledTexture3D(uniformNode.name, uniformNode.node, group); + bindings.push(uniformGPU); + } else if (type === 'buffer') { + node.name = `NodeBuffer_${node.id}`; + uniformNode.name = `buffer${node.id}`; + + const buffer = new NodeUniformBuffer(node, group); + buffer.name = node.name; + + bindings.push(buffer); + + uniformGPU = buffer; + } else { + const uniformsStage = this.uniformGroups[shaderStage] || (this.uniformGroups[shaderStage] = {}); + + let uniformsGroup = uniformsStage[groupName]; + + if (uniformsGroup === undefined) { + uniformsGroup = new NodeUniformsGroup(shaderStage + '_' + groupName, group); + //uniformsGroup.setVisibility( gpuShaderStageLib[ shaderStage ] ); + + uniformsStage[groupName] = uniformsGroup; + + bindings.push(uniformsGroup); + } + + uniformGPU = this.getNodeUniform(uniformNode, type); + + uniformsGroup.addUniform(uniformGPU); + } + + nodeData.uniformGPU = uniformGPU; + } + + return uniformNode; + } +} + +export default GLSLNodeBuilder; diff --git a/src-testing/src/renderers/webgl/WebGLAttributes.d.ts b/src-testing/src/renderers/webgl/WebGLAttributes.d.ts new file mode 100644 index 000000000..8f4a9757b --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLAttributes.d.ts @@ -0,0 +1,21 @@ +import { BufferAttribute } from "../../core/BufferAttribute.js"; +import { GLBufferAttribute } from "../../core/GLBufferAttribute.js"; +import { InterleavedBufferAttribute } from "../../core/InterleavedBufferAttribute.js"; + +export class WebGLAttributes { + constructor(gl: WebGLRenderingContext | WebGL2RenderingContext); + + get(attribute: BufferAttribute | InterleavedBufferAttribute | GLBufferAttribute): + | { + buffer: WebGLBuffer; + type: number; + bytesPerElement: number; + version: number; + size: number; + } + | undefined; + + remove(attribute: BufferAttribute | InterleavedBufferAttribute | GLBufferAttribute): void; + + update(attribute: BufferAttribute | InterleavedBufferAttribute | GLBufferAttribute, bufferType: number): void; +} diff --git a/src-testing/src/renderers/webgl/WebGLBindingStates.d.ts b/src-testing/src/renderers/webgl/WebGLBindingStates.d.ts new file mode 100644 index 000000000..0b96de770 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLBindingStates.d.ts @@ -0,0 +1,26 @@ +import { BufferAttribute } from "../../core/BufferAttribute.js"; +import { BufferGeometry } from "../../core/BufferGeometry.js"; +import { Object3D } from "../../core/Object3D.js"; +import { Material } from "../../materials/Material.js"; +import { WebGLAttributes } from "./WebGLAttributes.js"; +import { WebGLProgram } from "./WebGLProgram.js"; + +export class WebGLBindingStates { + constructor(gl: WebGLRenderingContext, attributes: WebGLAttributes); + + setup( + object: Object3D, + material: Material, + program: WebGLProgram, + geometry: BufferGeometry, + index: BufferAttribute, + ): void; + reset(): void; + resetDefaultState(): void; + dispose(): void; + releaseStatesOfGeometry(): void; + releaseStatesOfProgram(): void; + initAttributes(): void; + enableAttribute(attribute: number): void; + disableUnusedAttributes(): void; +} diff --git a/src-testing/src/renderers/webgl/WebGLBufferRenderer.d.ts b/src-testing/src/renderers/webgl/WebGLBufferRenderer.d.ts new file mode 100644 index 000000000..c75f74745 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLBufferRenderer.d.ts @@ -0,0 +1,21 @@ +import { WebGLExtensions } from "./WebGLExtensions.js"; +import { WebGLInfo } from "./WebGLInfo.js"; + +export class WebGLBufferRenderer { + constructor( + gl: WebGLRenderingContext, + extensions: WebGLExtensions, + info: WebGLInfo, + ); + + setMode: (value: any) => void; + render: (start: any, count: number) => void; + renderInstances: (start: any, count: number, primcount: number) => void; + renderMultiDraw: (starts: Int32Array, counts: Int32Array, drawCount: number) => void; + renderMultiDrawInstances: ( + starts: Int32Array, + counts: Int32Array, + drawCount: number, + primcount: Int32Array, + ) => void; +} diff --git a/src-testing/src/renderers/webgl/WebGLCapabilities.d.ts b/src-testing/src/renderers/webgl/WebGLCapabilities.d.ts new file mode 100644 index 000000000..c0f3aae41 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLCapabilities.d.ts @@ -0,0 +1,35 @@ +import { PixelFormat, TextureDataType } from "../../constants.js"; + +export interface WebGLCapabilitiesParameters { + precision?: string | undefined; + logarithmicDepthBuffer?: boolean | undefined; +} + +export class WebGLCapabilities { + constructor(gl: WebGLRenderingContext, extensions: any, parameters: WebGLCapabilitiesParameters); + + readonly isWebGL2: boolean; + + getMaxAnisotropy: () => number; + getMaxPrecision: (precision: string) => string; + + textureFormatReadable: (textureFormat: PixelFormat) => boolean; + textureTypeReadable: (textureType: TextureDataType) => boolean; + + precision: string; + logarithmicDepthBuffer: boolean; + + maxTextures: number; + maxVertexTextures: number; + maxTextureSize: number; + maxCubemapSize: number; + + maxAttributes: number; + maxVertexUniforms: number; + maxVaryings: number; + maxFragmentUniforms: number; + + vertexTextures: boolean; + + maxSamples: number; +} diff --git a/src-testing/src/renderers/webgl/WebGLClipping.d.ts b/src-testing/src/renderers/webgl/WebGLClipping.d.ts new file mode 100644 index 000000000..167a16b35 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLClipping.d.ts @@ -0,0 +1,26 @@ +import { Camera } from "../../cameras/Camera.js"; +import { Material } from "../../materials/Material.js"; +import { Plane } from "../../math/Plane.js"; +import { WebGLProperties } from "./WebGLProperties.js"; + +export class WebGLClipping { + constructor(properties: WebGLProperties); + + uniform: { value: any; needsUpdate: boolean }; + + /** + * @default 0 + */ + numPlanes: number; + + /** + * @default 0 + */ + numIntersection: number; + + init(planes: any[], enableLocalClipping: boolean): boolean; + beginShadows(): void; + endShadows(): void; + setGlobalState(planes: Plane[], camera: Camera): void; + setState(material: Material, camera: Camera, useCache: boolean): void; +} diff --git a/src-testing/src/renderers/webgl/WebGLCubeMaps.d.ts b/src-testing/src/renderers/webgl/WebGLCubeMaps.d.ts new file mode 100644 index 000000000..32cda3f61 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLCubeMaps.d.ts @@ -0,0 +1,8 @@ +import { WebGLRenderer } from "../WebGLRenderer.js"; + +export class WebGLCubeMaps { + constructor(renderer: WebGLRenderer); + + get(texture: any): any; + dispose(): void; +} diff --git a/src-testing/src/renderers/webgl/WebGLCubeUVMaps.d.ts b/src-testing/src/renderers/webgl/WebGLCubeUVMaps.d.ts new file mode 100644 index 000000000..e94246325 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLCubeUVMaps.d.ts @@ -0,0 +1,9 @@ +import { Texture } from "../../textures/Texture.js"; +import { WebGLRenderer } from "../WebGLRenderer.js"; + +export class WebGLCubeUVMaps { + constructor(renderer: WebGLRenderer); + + get(texture: T): T extends Texture ? Texture : T; + dispose(): void; +} diff --git a/src-testing/src/renderers/webgl/WebGLExtensions.d.ts b/src-testing/src/renderers/webgl/WebGLExtensions.d.ts new file mode 100644 index 000000000..0f0c0bf4b --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLExtensions.d.ts @@ -0,0 +1,7 @@ +export class WebGLExtensions { + constructor(gl: WebGLRenderingContext); + + has(name: string): boolean; + init(): void; + get(name: string): any; +} diff --git a/src-testing/src/renderers/webgl/WebGLGeometries.d.ts b/src-testing/src/renderers/webgl/WebGLGeometries.d.ts new file mode 100644 index 000000000..422249f37 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLGeometries.d.ts @@ -0,0 +1,13 @@ +import { BufferAttribute } from "../../core/BufferAttribute.js"; +import { BufferGeometry } from "../../core/BufferGeometry.js"; +import { Object3D } from "../../core/Object3D.js"; +import { WebGLAttributes } from "./WebGLAttributes.js"; +import { WebGLInfo } from "./WebGLInfo.js"; + +export class WebGLGeometries { + constructor(gl: WebGLRenderingContext, attributes: WebGLAttributes, info: WebGLInfo); + + get(object: Object3D, geometry: BufferGeometry): BufferGeometry; + update(geometry: BufferGeometry): void; + getWireframeAttribute(geometry: BufferGeometry): BufferAttribute; +} diff --git a/src-testing/src/renderers/webgl/WebGLIndexedBufferRenderer.d.ts b/src-testing/src/renderers/webgl/WebGLIndexedBufferRenderer.d.ts new file mode 100644 index 000000000..8e016e847 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLIndexedBufferRenderer.d.ts @@ -0,0 +1,15 @@ +export class WebGLIndexedBufferRenderer { + constructor(gl: WebGLRenderingContext, extensions: any, info: any); + + setMode: (value: any) => void; + setIndex: (index: any) => void; + render: (start: any, count: number) => void; + renderInstances: (start: any, count: number, primcount: number) => void; + renderMultiDraw: (starts: Int32Array, counts: Int32Array, drawCount: number) => void; + renderMultiDrawInstances: ( + starts: Int32Array, + counts: Int32Array, + drawCount: number, + primcount: Int32Array, + ) => void; +} diff --git a/src-testing/src/renderers/webgl/WebGLInfo.d.ts b/src-testing/src/renderers/webgl/WebGLInfo.d.ts new file mode 100644 index 000000000..952ff6f9f --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLInfo.d.ts @@ -0,0 +1,39 @@ +import { WebGLProgram } from "./WebGLProgram.js"; + +/** + * An object with a series of statistical information about the graphics board memory and the rendering process. + */ +export class WebGLInfo { + constructor(gl: WebGLRenderingContext); + + /** + * @default true + */ + autoReset: boolean; + + /** + * @default { geometries: 0, textures: 0 } + */ + memory: { + geometries: number; + textures: number; + }; + + /** + * @default null + */ + programs: WebGLProgram[] | null; + + /** + * @default { frame: 0, calls: 0, triangles: 0, points: 0, lines: 0 } + */ + render: { + calls: number; + frame: number; + lines: number; + points: number; + triangles: number; + }; + update(count: number, mode: number, instanceCount: number): void; + reset(): void; +} diff --git a/src-testing/src/renderers/webgl/WebGLLights.d.ts b/src-testing/src/renderers/webgl/WebGLLights.d.ts new file mode 100644 index 000000000..4c01422e8 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLLights.d.ts @@ -0,0 +1,49 @@ +import { WebGLExtensions } from "./WebGLExtensions.js"; + +export interface WebGLLightsState { + version: number; + + hash: { + directionalLength: number; + pointLength: number; + spotLength: number; + rectAreaLength: number; + hemiLength: number; + + numDirectionalShadows: number; + numPointShadows: number; + numSpotShadows: number; + numSpotMaps: number; + + numLightProbes: number; + }; + + ambient: number[]; + probe: any[]; + directional: any[]; + directionalShadow: any[]; + directionalShadowMap: any[]; + directionalShadowMatrix: any[]; + spot: any[]; + spotShadow: any[]; + spotShadowMap: any[]; + spotShadowMatrix: any[]; + rectArea: any[]; + point: any[]; + pointShadow: any[]; + pointShadowMap: any[]; + pointShadowMatrix: any[]; + hemi: any[]; + numSpotLightShadowsWithMaps: number; + numLightProbes: number; +} + +export class WebGLLights { + constructor(extensions: WebGLExtensions); + + state: WebGLLightsState; + + get(light: any): any; + setup(lights: any): void; + setupView(lights: any, camera: any): void; +} diff --git a/src-testing/src/renderers/webgl/WebGLObjects.d.ts b/src-testing/src/renderers/webgl/WebGLObjects.d.ts new file mode 100644 index 000000000..aeb0356f7 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLObjects.d.ts @@ -0,0 +1,6 @@ +export class WebGLObjects { + constructor(gl: WebGLRenderingContext, geometries: any, attributes: any, info: any); + + update(object: any): any; + dispose(): void; +} diff --git a/src-testing/src/renderers/webgl/WebGLProgram.d.ts b/src-testing/src/renderers/webgl/WebGLProgram.d.ts new file mode 100644 index 000000000..b3e5d6fd8 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLProgram.d.ts @@ -0,0 +1,30 @@ +import { WebGLRenderer } from "../WebGLRenderer.js"; +import { WebGLUniforms } from "./WebGLUniforms.js"; + +export class WebGLProgram { + constructor(renderer: WebGLRenderer, cacheKey: string, parameters: object); + + name: string; + id: number; + cacheKey: string; // unique identifier for this program, used for looking up compiled programs from cache. + + /** + * @default 1 + */ + usedTimes: number; + program: any; + vertexShader: WebGLShader; + fragmentShader: WebGLShader; + /** + * @deprecated Use {@link WebGLProgram#getUniforms getUniforms()} instead. + */ + uniforms: any; + /** + * @deprecated Use {@link WebGLProgram#getAttributes getAttributes()} instead. + */ + attributes: any; + + getUniforms(): WebGLUniforms; + getAttributes(): any; + destroy(): void; +} diff --git a/src-testing/src/renderers/webgl/WebGLPrograms.d.ts b/src-testing/src/renderers/webgl/WebGLPrograms.d.ts new file mode 100644 index 000000000..cee01169b --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLPrograms.d.ts @@ -0,0 +1,239 @@ +import { + ColorSpace, + Combine, + DepthPackingStrategies, + GLSLVersion, + Mapping, + ShadowMapType, + ToneMapping, +} from "../../constants.js"; +import { Object3D } from "../../core/Object3D.js"; +import { Light } from "../../lights/Light.js"; +import { Material } from "../../materials/Material.js"; +import { Scene } from "../../scenes/Scene.js"; +import { IUniform } from "../shaders/UniformsLib.js"; +import { WebGLRenderer } from "../WebGLRenderer.js"; +import { WebGLBindingStates } from "./WebGLBindingStates.js"; +import { WebGLCapabilities } from "./WebGLCapabilities.js"; +import { WebGLClipping } from "./WebGLClipping.js"; +import { WebGLCubeMaps } from "./WebGLCubeMaps.js"; +import { WebGLExtensions } from "./WebGLExtensions.js"; +import { WebGLLightsState } from "./WebGLLights.js"; +import { WebGLProgram } from "./WebGLProgram.js"; + +export interface WebGLProgramParameters { + shaderID: string; + shaderType: string; + shaderName: string; + + vertexShader: string; + fragmentShader: string; + defines: { [define: string]: string | number | boolean } | undefined; + + customVertexShaderID: string | undefined; + customFragmentShaderID: string | undefined; + + isRawShaderMaterial: boolean; + glslVersion: GLSLVersion | null | undefined; + + precision: "lowp" | "mediump" | "highp"; + + batching: boolean; + batchingColor: boolean; + instancing: boolean; + instancingColor: boolean; + instancingMorph: boolean; + + supportsVertexTextures: boolean; + outputColorSpace: ColorSpace; + alphaToCoverage: boolean; + + map: boolean; + matcap: boolean; + envMap: boolean; + envMapMode: Mapping | false; + envMapCubeUVHeight: number | null; + aoMap: boolean; + lightMap: boolean; + bumpMap: boolean; + normalMap: boolean; + displacementMap: boolean; + emissiveMap: boolean; + + normalMapObjectSpace: boolean; + normalMapTangentSpace: boolean; + + metalnessMap: boolean; + roughnessMap: boolean; + + anisotropy: boolean; + anisotropyMap: boolean; + + clearcoat: boolean; + clearcoatMap: boolean; + clearcoatNormalMap: boolean; + clearcoatRoughnessMap: boolean; + + dispersion: boolean; + + iridescence: boolean; + iridescenceMap: boolean; + iridescenceThicknessMap: boolean; + + sheen: boolean; + sheenColorMap: boolean; + sheenRoughnessMap: boolean; + + specularMap: boolean; + specularColorMap: boolean; + specularIntensityMap: boolean; + + transmission: boolean; + transmissionMap: boolean; + thicknessMap: boolean; + + gradientMap: boolean; + + opaque: boolean; + + alphaMap: boolean; + alphaTest: boolean; + alphaHash: boolean; + + combine: Combine | undefined; + + // + + mapUv: string | false; + aoMapUv: string | false; + lightMapUv: string | false; + bumpMapUv: string | false; + normalMapUv: string | false; + displacementMapUv: string | false; + emissiveMapUv: string | false; + + metalnessMapUv: string | false; + roughnessMapUv: string | false; + + anisotropyMapUv: string | false; + + clearcoatMapUv: string | false; + clearcoatNormalMapUv: string | false; + clearcoatRoughnessMapUv: string | false; + + iridescenceMapUv: string | false; + iridescenceThicknessMapUv: string | false; + + sheenColorMapUv: string | false; + sheenRoughnessMapUv: string | false; + + specularMapUv: string | false; + specularColorMapUv: string | false; + specularIntensityMapUv: string | false; + + transmissionMapUv: string | false; + thicknessMapUv: string | false; + + alphaMapUv: string | false; + + // + + vertexTangents: boolean; + vertexColors: boolean; + vertexAlphas: boolean; + vertexUv1s: boolean; + vertexUv2s: boolean; + vertexUv3s: boolean; + + pointsUvs: boolean; + + fog: boolean; + useFog: boolean; + fogExp2: boolean; + + flatShading: boolean; + + sizeAttenuation: boolean; + logarithmicDepthBuffer: boolean; + + skinning: boolean; + + morphTargets: boolean; + morphNormals: boolean; + morphColors: boolean; + morphTargetsCount: number; + morphTextureStride: number; + + numDirLights: number; + numPointLights: number; + numSpotLights: number; + numSpotLightMaps: number; + numRectAreaLights: number; + numHemiLights: number; + + numDirLightShadows: number; + numPointLightShadows: number; + numSpotLightShadows: number; + numSpotLightShadowsWithMaps: number; + + numLightProbes: number; + + numClippingPlanes: number; + numClipIntersection: number; + + dithering: boolean; + + shadowMapEnabled: boolean; + shadowMapType: ShadowMapType; + + toneMapping: ToneMapping; + + decodeVideoTexture: boolean; + + premultipliedAlpha: boolean; + + doubleSided: boolean; + flipSided: boolean; + + useDepthPacking: boolean; + depthPacking: DepthPackingStrategies | 0; + + index0AttributeName: string | undefined; + + extensionClipCullDistance: boolean; + extensionMultiDraw: boolean; + + rendererExtensionParallelShaderCompile: boolean; + + customProgramCacheKey: string; +} + +export interface WebGLProgramParametersWithUniforms extends WebGLProgramParameters { + uniforms: { [uniform: string]: IUniform }; +} + +export class WebGLPrograms { + constructor( + renderer: WebGLRenderer, + cubemaps: WebGLCubeMaps, + extensions: WebGLExtensions, + capabilities: WebGLCapabilities, + bindingStates: WebGLBindingStates, + clipping: WebGLClipping, + ); + + programs: WebGLProgram[]; + + getParameters( + material: Material, + lights: WebGLLightsState, + shadows: Light[], + scene: Scene, + object: Object3D, + ): WebGLProgramParameters; + + getProgramCacheKey(parameters: WebGLProgramParameters): string; + getUniforms(material: Material): { [uniform: string]: IUniform }; + acquireProgram(parameters: WebGLProgramParametersWithUniforms, cacheKey: string): WebGLProgram; + releaseProgram(program: WebGLProgram): void; +} diff --git a/src-testing/src/renderers/webgl/WebGLProperties.d.ts b/src-testing/src/renderers/webgl/WebGLProperties.d.ts new file mode 100644 index 000000000..adcf01ec9 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLProperties.d.ts @@ -0,0 +1,9 @@ +export class WebGLProperties { + constructor(); + + has: (object: unknown) => boolean; + get: (object: unknown) => unknown; + remove: (object: unknown) => void; + update: (object: unknown, key: unknown, value: unknown) => unknown; + dispose: () => void; +} diff --git a/src-testing/src/renderers/webgl/WebGLRenderLists.d.ts b/src-testing/src/renderers/webgl/WebGLRenderLists.d.ts new file mode 100644 index 000000000..0f16a4287 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLRenderLists.d.ts @@ -0,0 +1,66 @@ +import { Camera } from "../../cameras/Camera.js"; +import { BufferGeometry } from "../../core/BufferGeometry.js"; +import { Object3D } from "../../core/Object3D.js"; +import { Material } from "../../materials/Material.js"; +import { Group } from "../../objects/Group.js"; +import { Scene } from "../../scenes/Scene.js"; +import { WebGLProgram } from "./WebGLProgram.js"; +import { WebGLProperties } from "./WebGLProperties.js"; + +export interface RenderItem { + id: number; + object: Object3D; + geometry: BufferGeometry | null; + material: Material; + program: WebGLProgram; + groupOrder: number; + renderOrder: number; + z: number; + group: Group | null; +} + +export class WebGLRenderList { + constructor(properties: WebGLProperties); + + /** + * @default [] + */ + opaque: RenderItem[]; + + /** + * @default [] + */ + transparent: RenderItem[]; + + /** + * @default [] + */ + transmissive: RenderItem[]; + + init(): void; + push( + object: Object3D, + geometry: BufferGeometry | null, + material: Material, + groupOrder: number, + z: number, + group: Group | null, + ): void; + unshift( + object: Object3D, + geometry: BufferGeometry | null, + material: Material, + groupOrder: number, + z: number, + group: Group | null, + ): void; + sort(opaqueSort: (a: any, b: any) => number, transparentSort: (a: any, b: any) => number): void; + finish(): void; +} + +export class WebGLRenderLists { + constructor(properties: WebGLProperties); + + dispose(): void; + get(scene: Scene, renderCallDepth: number): WebGLRenderList; +} diff --git a/src-testing/src/renderers/webgl/WebGLShader.d.ts b/src-testing/src/renderers/webgl/WebGLShader.d.ts new file mode 100644 index 000000000..5704fb88e --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLShader.d.ts @@ -0,0 +1 @@ +export function WebGLShader(gl: WebGLRenderingContext, type: string, string: string): WebGLShader; diff --git a/src-testing/src/renderers/webgl/WebGLShadowMap.d.ts b/src-testing/src/renderers/webgl/WebGLShadowMap.d.ts new file mode 100644 index 000000000..8368fdee7 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLShadowMap.d.ts @@ -0,0 +1,38 @@ +import { Camera } from "../../cameras/Camera.js"; +import { ShadowMapType } from "../../constants.js"; +import { Light } from "../../lights/Light.js"; +import { Scene } from "../../scenes/Scene.js"; +import { WebGLRenderer } from "../WebGLRenderer.js"; +import { WebGLCapabilities } from "./WebGLCapabilities.js"; +import { WebGLObjects } from "./WebGLObjects.js"; + +export class WebGLShadowMap { + constructor(_renderer: WebGLRenderer, _objects: WebGLObjects, _capabilities: WebGLCapabilities); + + /** + * @default false + */ + enabled: boolean; + + /** + * @default true + */ + autoUpdate: boolean; + + /** + * @default false + */ + needsUpdate: boolean; + + /** + * @default THREE.PCFShadowMap + */ + type: ShadowMapType; + + render(shadowsArray: Light[], scene: Scene, camera: Camera): void; + + /** + * @deprecated Use {@link Material#shadowSide} instead. + */ + cullFace: any; +} diff --git a/src-testing/src/renderers/webgl/WebGLState.d.ts b/src-testing/src/renderers/webgl/WebGLState.d.ts new file mode 100644 index 000000000..13d4850e9 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLState.d.ts @@ -0,0 +1,116 @@ +import { + Blending, + BlendingDstFactor, + BlendingEquation, + BlendingSrcFactor, + CullFace, + DepthModes, +} from "../../constants.js"; +import { Material } from "../../materials/Material.js"; +import { Vector4 } from "../../math/Vector4.js"; +import { WebGLRenderTarget } from "../WebGLRenderTarget.js"; + +export class WebGLColorBuffer { + constructor(); + + setMask(colorMask: boolean): void; + setLocked(lock: boolean): void; + setClear(r: number, g: number, b: number, a: number, premultipliedAlpha: boolean): void; + reset(): void; +} + +export class WebGLDepthBuffer { + constructor(); + + setTest(depthTest: boolean): void; + setMask(depthMask: boolean): void; + setFunc(depthFunc: DepthModes): void; + setLocked(lock: boolean): void; + setClear(depth: number): void; + reset(): void; +} + +export class WebGLStencilBuffer { + constructor(); + + setTest(stencilTest: boolean): void; + setMask(stencilMask: number): void; + setFunc(stencilFunc: number, stencilRef: number, stencilMask: number): void; + setOp(stencilFail: number, stencilZFail: number, stencilZPass: number): void; + setLocked(lock: boolean): void; + setClear(stencil: number): void; + reset(): void; +} + +export class WebGLState { + constructor(gl: WebGLRenderingContext); + + buffers: { + color: WebGLColorBuffer; + depth: WebGLDepthBuffer; + stencil: WebGLStencilBuffer; + }; + + enable(id: number): void; + disable(id: number): void; + bindFramebuffer(target: number, framebuffer: WebGLFramebuffer | null): void; + drawBuffers(renderTarget: WebGLRenderTarget | null, framebuffer: WebGLFramebuffer | null): void; + useProgram(program: any): boolean; + setBlending( + blending: Blending, + blendEquation?: BlendingEquation, + blendSrc?: BlendingSrcFactor, + blendDst?: BlendingDstFactor, + blendEquationAlpha?: BlendingEquation, + blendSrcAlpha?: BlendingSrcFactor, + blendDstAlpha?: BlendingDstFactor, + premultiplyAlpha?: boolean, + ): void; + setMaterial(material: Material, frontFaceCW: boolean): void; + setFlipSided(flipSided: boolean): void; + setCullFace(cullFace: CullFace): void; + setLineWidth(width: number): void; + setPolygonOffset(polygonoffset: boolean, factor?: number, units?: number): void; + setScissorTest(scissorTest: boolean): void; + activeTexture(webglSlot: number): void; + bindTexture(webglType: number, webglTexture: any): void; + unbindTexture(): void; + // Same interface as https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/compressedTexImage2D + compressedTexImage2D( + target: number, + level: number, + internalformat: number, + width: number, + height: number, + border: number, + data: ArrayBufferView, + ): void; + // Same interface as https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texImage2D + texImage2D( + target: number, + level: number, + internalformat: number, + width: number, + height: number, + border: number, + format: number, + type: number, + pixels: ArrayBufferView | null, + ): void; + texImage2D(target: number, level: number, internalformat: number, format: number, type: number, source: any): void; + texImage3D( + target: number, + level: number, + internalformat: number, + width: number, + height: number, + depth: number, + border: number, + format: number, + type: number, + pixels: any, + ): void; + scissor(scissor: Vector4): void; + viewport(viewport: Vector4): void; + reset(): void; +} diff --git a/src-testing/src/renderers/webgl/WebGLTextures.d.ts b/src-testing/src/renderers/webgl/WebGLTextures.d.ts new file mode 100644 index 000000000..031866dee --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLTextures.d.ts @@ -0,0 +1,30 @@ +import { WebGLCapabilities } from "./WebGLCapabilities.js"; +import { WebGLExtensions } from "./WebGLExtensions.js"; +import { WebGLInfo } from "./WebGLInfo.js"; +import { WebGLProperties } from "./WebGLProperties.js"; +import { WebGLState } from "./WebGLState.js"; +import { WebGLUtils } from "./WebGLUtils.js"; + +export class WebGLTextures { + constructor( + gl: WebGLRenderingContext, + extensions: WebGLExtensions, + state: WebGLState, + properties: WebGLProperties, + capabilities: WebGLCapabilities, + utils: WebGLUtils, + info: WebGLInfo, + ); + + allocateTextureUnit(): void; + resetTextureUnits(): void; + setTexture2D(texture: any, slot: number): void; + setTexture2DArray(texture: any, slot: number): void; + setTexture3D(texture: any, slot: number): void; + setTextureCube(texture: any, slot: number): void; + setupRenderTarget(renderTarget: any): void; + updateRenderTargetMipmap(renderTarget: any): void; + updateMultisampleRenderTarget(renderTarget: any): void; + safeSetTexture2D(texture: any, slot: number): void; + safeSetTextureCube(texture: any, slot: number): void; +} diff --git a/src-testing/src/renderers/webgl/WebGLUniforms.d.ts b/src-testing/src/renderers/webgl/WebGLUniforms.d.ts new file mode 100644 index 000000000..925b5e4bf --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLUniforms.d.ts @@ -0,0 +1,12 @@ +import { WebGLProgram } from "./WebGLProgram.js"; +import { WebGLTextures } from "./WebGLTextures.js"; + +export class WebGLUniforms { + constructor(gl: WebGLRenderingContext, program: WebGLProgram); + + setValue(gl: WebGLRenderingContext, name: string, value: any, textures: WebGLTextures): void; + setOptional(gl: WebGLRenderingContext, object: any, name: string): void; + + static upload(gl: WebGLRenderingContext, seq: any, values: any[], textures: WebGLTextures): void; + static seqWithValue(seq: any, values: any[]): any[]; +} diff --git a/src-testing/src/renderers/webgl/WebGLUniformsGroups.d.ts b/src-testing/src/renderers/webgl/WebGLUniformsGroups.d.ts new file mode 100644 index 000000000..a5e052c1f --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLUniformsGroups.d.ts @@ -0,0 +1,17 @@ +import { UniformsGroup } from "../../core/UniformsGroup.js"; + +import { WebGLCapabilities } from "./WebGLCapabilities.js"; +import { WebGLInfo } from "./WebGLInfo.js"; +import { WebGLProgram } from "./WebGLProgram.js"; +import { WebGLState } from "./WebGLState.js"; + +export function WebGLUniformsGroups( + gl: WebGLRenderingContext, + info: WebGLInfo, + capabilities: WebGLCapabilities, + state: WebGLState, +): { + dispose: () => void; + update: (uniformsGroup: UniformsGroup, program: WebGLProgram) => void; + bind: (uniformsGroup: UniformsGroup, program: WebGLProgram) => void; +}; diff --git a/src-testing/src/renderers/webgl/WebGLUtils.d.ts b/src-testing/src/renderers/webgl/WebGLUtils.d.ts new file mode 100644 index 000000000..b70a6e449 --- /dev/null +++ b/src-testing/src/renderers/webgl/WebGLUtils.d.ts @@ -0,0 +1,11 @@ +import { ColorSpace, CompressedPixelFormat, PixelFormat, TextureDataType } from "../../constants.js"; +import { WebGLExtensions } from "./WebGLExtensions.js"; + +export class WebGLUtils { + constructor( + gl: WebGLRenderingContext | WebGL2RenderingContext, + extensions: WebGLExtensions, + ); + + convert(p: PixelFormat | CompressedPixelFormat | TextureDataType, colorSpace?: ColorSpace): number | null; +} diff --git a/src-testing/src/renderers/webgpu/WebGPUBackend.ts b/src-testing/src/renderers/webgpu/WebGPUBackend.ts new file mode 100644 index 000000000..2b2aeefcd --- /dev/null +++ b/src-testing/src/renderers/webgpu/WebGPUBackend.ts @@ -0,0 +1,1249 @@ +/*// debugger tools +import 'https://greggman.github.io/webgpu-avoid-redundant-state-setting/webgpu-check-redundant-state-setting.js'; +//*/ + +import { + GPUFeatureName, + GPUTextureFormat, + GPULoadOp, + GPUStoreOp, + GPUIndexFormat, + GPUTextureViewDimension, +} from './utils/WebGPUConstants.js'; + +import WGSLNodeBuilder from './nodes/WGSLNodeBuilder.js'; +import Backend from '../common/Backend.js'; + +import WebGPUUtils from './utils/WebGPUUtils.js'; +import WebGPUAttributeUtils from './utils/WebGPUAttributeUtils.js'; +import WebGPUBindingUtils from './utils/WebGPUBindingUtils.js'; +import WebGPUPipelineUtils from './utils/WebGPUPipelineUtils.js'; +import WebGPUTextureUtils from './utils/WebGPUTextureUtils.js'; + +import { WebGPUCoordinateSystem } from '../../constants.js'; + +// + +class WebGPUBackend extends Backend { + constructor(parameters = {}) { + super(parameters); + + this.isWebGPUBackend = true; + + // some parameters require default values other than "undefined" + this.parameters.alpha = parameters.alpha === undefined ? true : parameters.alpha; + + this.parameters.requiredLimits = parameters.requiredLimits === undefined ? {} : parameters.requiredLimits; + + this.trackTimestamp = parameters.trackTimestamp === true; + + this.device = null; + this.context = null; + this.colorBuffer = null; + this.defaultRenderPassdescriptor = null; + + this.utils = new WebGPUUtils(this); + this.attributeUtils = new WebGPUAttributeUtils(this); + this.bindingUtils = new WebGPUBindingUtils(this); + this.pipelineUtils = new WebGPUPipelineUtils(this); + this.textureUtils = new WebGPUTextureUtils(this); + this.occludedResolveCache = new Map(); + } + + async init(renderer) { + await super.init(renderer); + + // + + const parameters = this.parameters; + + // create the device if it is not passed with parameters + + let device; + + if (parameters.device === undefined) { + const adapterOptions = { + powerPreference: parameters.powerPreference, + }; + + const adapter = await navigator.gpu.requestAdapter(adapterOptions); + + if (adapter === null) { + throw new Error('WebGPUBackend: Unable to create WebGPU adapter.'); + } + + // feature support + + const features = Object.values(GPUFeatureName); + + const supportedFeatures = []; + + for (const name of features) { + if (adapter.features.has(name)) { + supportedFeatures.push(name); + } + } + + const deviceDescriptor = { + requiredFeatures: supportedFeatures, + requiredLimits: parameters.requiredLimits, + }; + + device = await adapter.requestDevice(deviceDescriptor); + } else { + device = parameters.device; + } + + const context = + parameters.context !== undefined ? parameters.context : renderer.domElement.getContext('webgpu'); + + this.device = device; + this.context = context; + + const alphaMode = parameters.alpha ? 'premultiplied' : 'opaque'; + + this.context.configure({ + device: this.device, + format: GPUTextureFormat.BGRA8Unorm, + usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC, + alphaMode: alphaMode, + }); + + this.updateSize(); + } + + get coordinateSystem() { + return WebGPUCoordinateSystem; + } + + async getArrayBufferAsync(attribute) { + return await this.attributeUtils.getArrayBufferAsync(attribute); + } + + getContext() { + return this.context; + } + + _getDefaultRenderPassDescriptor() { + let descriptor = this.defaultRenderPassdescriptor; + + if (descriptor === null) { + const renderer = this.renderer; + + descriptor = { + colorAttachments: [ + { + view: null, + }, + ], + depthStencilAttachment: { + view: this.textureUtils.getDepthBuffer(renderer.depth, renderer.stencil).createView(), + }, + }; + + const colorAttachment = descriptor.colorAttachments[0]; + + if (this.renderer.samples > 0) { + colorAttachment.view = this.colorBuffer.createView(); + } else { + colorAttachment.resolveTarget = undefined; + } + + this.defaultRenderPassdescriptor = descriptor; + } + + const colorAttachment = descriptor.colorAttachments[0]; + + if (this.renderer.samples > 0) { + colorAttachment.resolveTarget = this.context.getCurrentTexture().createView(); + } else { + colorAttachment.view = this.context.getCurrentTexture().createView(); + } + + return descriptor; + } + + _getRenderPassDescriptor(renderContext) { + const renderTarget = renderContext.renderTarget; + const renderTargetData = this.get(renderTarget); + + let descriptors = renderTargetData.descriptors; + + if ( + descriptors === undefined || + renderTargetData.width !== renderTarget.width || + renderTargetData.height !== renderTarget.height || + renderTargetData.activeMipmapLevel !== renderTarget.activeMipmapLevel || + renderTargetData.samples !== renderTarget.samples + ) { + descriptors = {}; + + renderTargetData.descriptors = descriptors; + + // dispose + + const onDispose = () => { + renderTarget.removeEventListener('dispose', onDispose); + + this.delete(renderTarget); + }; + + renderTarget.addEventListener('dispose', onDispose); + } + + const cacheKey = renderContext.getCacheKey(); + + let descriptor = descriptors[cacheKey]; + + if (descriptor === undefined) { + const textures = renderContext.textures; + const colorAttachments = []; + + for (let i = 0; i < textures.length; i++) { + const textureData = this.get(textures[i]); + + const textureView = textureData.texture.createView({ + baseMipLevel: renderContext.activeMipmapLevel, + mipLevelCount: 1, + baseArrayLayer: renderContext.activeCubeFace, + dimension: GPUTextureViewDimension.TwoD, + }); + + let view, resolveTarget; + + if (textureData.msaaTexture !== undefined) { + view = textureData.msaaTexture.createView(); + resolveTarget = textureView; + } else { + view = textureView; + resolveTarget = undefined; + } + + colorAttachments.push({ + view, + resolveTarget, + loadOp: GPULoadOp.Load, + storeOp: GPUStoreOp.Store, + }); + } + + const depthTextureData = this.get(renderContext.depthTexture); + + const depthStencilAttachment = { + view: depthTextureData.texture.createView(), + }; + + descriptor = { + colorAttachments, + depthStencilAttachment, + }; + + descriptors[cacheKey] = descriptor; + + renderTargetData.width = renderTarget.width; + renderTargetData.height = renderTarget.height; + renderTargetData.samples = renderTarget.samples; + renderTargetData.activeMipmapLevel = renderTarget.activeMipmapLevel; + } + + return descriptor; + } + + beginRender(renderContext) { + const renderContextData = this.get(renderContext); + + const device = this.device; + const occlusionQueryCount = renderContext.occlusionQueryCount; + + let occlusionQuerySet; + + if (occlusionQueryCount > 0) { + if (renderContextData.currentOcclusionQuerySet) renderContextData.currentOcclusionQuerySet.destroy(); + if (renderContextData.currentOcclusionQueryBuffer) renderContextData.currentOcclusionQueryBuffer.destroy(); + + // Get a reference to the array of objects with queries. The renderContextData property + // can be changed by another render pass before the buffer.mapAsyc() completes. + renderContextData.currentOcclusionQuerySet = renderContextData.occlusionQuerySet; + renderContextData.currentOcclusionQueryBuffer = renderContextData.occlusionQueryBuffer; + renderContextData.currentOcclusionQueryObjects = renderContextData.occlusionQueryObjects; + + // + + occlusionQuerySet = device.createQuerySet({ type: 'occlusion', count: occlusionQueryCount }); + + renderContextData.occlusionQuerySet = occlusionQuerySet; + renderContextData.occlusionQueryIndex = 0; + renderContextData.occlusionQueryObjects = new Array(occlusionQueryCount); + + renderContextData.lastOcclusionObject = null; + } + + let descriptor; + + if (renderContext.textures === null) { + descriptor = this._getDefaultRenderPassDescriptor(); + } else { + descriptor = this._getRenderPassDescriptor(renderContext); + } + + this.initTimestampQuery(renderContext, descriptor); + + descriptor.occlusionQuerySet = occlusionQuerySet; + + const depthStencilAttachment = descriptor.depthStencilAttachment; + + if (renderContext.textures !== null) { + const colorAttachments = descriptor.colorAttachments; + + for (let i = 0; i < colorAttachments.length; i++) { + const colorAttachment = colorAttachments[i]; + + if (renderContext.clearColor) { + colorAttachment.clearValue = renderContext.clearColorValue; + colorAttachment.loadOp = GPULoadOp.Clear; + colorAttachment.storeOp = GPUStoreOp.Store; + } else { + colorAttachment.loadOp = GPULoadOp.Load; + colorAttachment.storeOp = GPUStoreOp.Store; + } + } + } else { + const colorAttachment = descriptor.colorAttachments[0]; + + if (renderContext.clearColor) { + colorAttachment.clearValue = renderContext.clearColorValue; + colorAttachment.loadOp = GPULoadOp.Clear; + colorAttachment.storeOp = GPUStoreOp.Store; + } else { + colorAttachment.loadOp = GPULoadOp.Load; + colorAttachment.storeOp = GPUStoreOp.Store; + } + } + + // + + if (renderContext.depth) { + if (renderContext.clearDepth) { + depthStencilAttachment.depthClearValue = renderContext.clearDepthValue; + depthStencilAttachment.depthLoadOp = GPULoadOp.Clear; + depthStencilAttachment.depthStoreOp = GPUStoreOp.Store; + } else { + depthStencilAttachment.depthLoadOp = GPULoadOp.Load; + depthStencilAttachment.depthStoreOp = GPUStoreOp.Store; + } + } + + if (renderContext.stencil) { + if (renderContext.clearStencil) { + depthStencilAttachment.stencilClearValue = renderContext.clearStencilValue; + depthStencilAttachment.stencilLoadOp = GPULoadOp.Clear; + depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store; + } else { + depthStencilAttachment.stencilLoadOp = GPULoadOp.Load; + depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store; + } + } + + // + + const encoder = device.createCommandEncoder({ label: 'renderContext_' + renderContext.id }); + const currentPass = encoder.beginRenderPass(descriptor); + + // + + renderContextData.descriptor = descriptor; + renderContextData.encoder = encoder; + renderContextData.currentPass = currentPass; + renderContextData.currentSets = { attributes: {} }; + + // + + if (renderContext.viewport) { + this.updateViewport(renderContext); + } + + if (renderContext.scissor) { + const { x, y, width, height } = renderContext.scissorValue; + + currentPass.setScissorRect(x, renderContext.height - height - y, width, height); + } + } + + finishRender(renderContext) { + const renderContextData = this.get(renderContext); + const occlusionQueryCount = renderContext.occlusionQueryCount; + + if (renderContextData.renderBundles !== undefined && renderContextData.renderBundles.length > 0) { + renderContextData.registerBundlesPhase = false; + renderContextData.currentPass.executeBundles(renderContextData.renderBundles); + } + + if (occlusionQueryCount > renderContextData.occlusionQueryIndex) { + renderContextData.currentPass.endOcclusionQuery(); + } + + renderContextData.currentPass.end(); + + if (occlusionQueryCount > 0) { + const bufferSize = occlusionQueryCount * 8; // 8 byte entries for query results + + // + + let queryResolveBuffer = this.occludedResolveCache.get(bufferSize); + + if (queryResolveBuffer === undefined) { + queryResolveBuffer = this.device.createBuffer({ + size: bufferSize, + usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC, + }); + + this.occludedResolveCache.set(bufferSize, queryResolveBuffer); + } + + // + + const readBuffer = this.device.createBuffer({ + size: bufferSize, + usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ, + }); + + // two buffers required here - WebGPU doesn't allow usage of QUERY_RESOLVE & MAP_READ to be combined + renderContextData.encoder.resolveQuerySet( + renderContextData.occlusionQuerySet, + 0, + occlusionQueryCount, + queryResolveBuffer, + 0, + ); + renderContextData.encoder.copyBufferToBuffer(queryResolveBuffer, 0, readBuffer, 0, bufferSize); + + renderContextData.occlusionQueryBuffer = readBuffer; + + // + + this.resolveOccludedAsync(renderContext); + } + + this.prepareTimestampBuffer(renderContext, renderContextData.encoder); + + this.device.queue.submit([renderContextData.encoder.finish()]); + + // + + if (renderContext.textures !== null) { + const textures = renderContext.textures; + + for (let i = 0; i < textures.length; i++) { + const texture = textures[i]; + + if (texture.generateMipmaps === true) { + this.textureUtils.generateMipmaps(texture); + } + } + } + } + + isOccluded(renderContext, object) { + const renderContextData = this.get(renderContext); + + return renderContextData.occluded && renderContextData.occluded.has(object); + } + + async resolveOccludedAsync(renderContext) { + const renderContextData = this.get(renderContext); + + // handle occlusion query results + + const { currentOcclusionQueryBuffer, currentOcclusionQueryObjects } = renderContextData; + + if (currentOcclusionQueryBuffer && currentOcclusionQueryObjects) { + const occluded = new WeakSet(); + + renderContextData.currentOcclusionQueryObjects = null; + renderContextData.currentOcclusionQueryBuffer = null; + + await currentOcclusionQueryBuffer.mapAsync(GPUMapMode.READ); + + const buffer = currentOcclusionQueryBuffer.getMappedRange(); + const results = new BigUint64Array(buffer); + + for (let i = 0; i < currentOcclusionQueryObjects.length; i++) { + if (results[i] !== BigInt(0)) { + occluded.add(currentOcclusionQueryObjects[i]); + } + } + + currentOcclusionQueryBuffer.destroy(); + + renderContextData.occluded = occluded; + } + } + + updateViewport(renderContext) { + const { currentPass } = this.get(renderContext); + const { x, y, width, height, minDepth, maxDepth } = renderContext.viewportValue; + + currentPass.setViewport(x, renderContext.height - height - y, width, height, minDepth, maxDepth); + } + + clear(color, depth, stencil, renderTargetData = null) { + const device = this.device; + const renderer = this.renderer; + + let colorAttachments = []; + + let depthStencilAttachment; + let clearValue; + + let supportsDepth; + let supportsStencil; + + if (color) { + const clearColor = this.getClearColor(); + + clearValue = { r: clearColor.r, g: clearColor.g, b: clearColor.b, a: clearColor.a }; + } + + if (renderTargetData === null) { + supportsDepth = renderer.depth; + supportsStencil = renderer.stencil; + + const descriptor = this._getDefaultRenderPassDescriptor(); + + if (color) { + colorAttachments = descriptor.colorAttachments; + + const colorAttachment = colorAttachments[0]; + + colorAttachment.clearValue = clearValue; + colorAttachment.loadOp = GPULoadOp.Clear; + colorAttachment.storeOp = GPUStoreOp.Store; + } + + if (supportsDepth || supportsStencil) { + depthStencilAttachment = descriptor.depthStencilAttachment; + } + } else { + supportsDepth = renderTargetData.depth; + supportsStencil = renderTargetData.stencil; + + if (color) { + for (const texture of renderTargetData.textures) { + const textureData = this.get(texture); + const textureView = textureData.texture.createView(); + + let view, resolveTarget; + + if (textureData.msaaTexture !== undefined) { + view = textureData.msaaTexture.createView(); + resolveTarget = textureView; + } else { + view = textureView; + resolveTarget = undefined; + } + + colorAttachments.push({ + view, + resolveTarget, + clearValue, + loadOp: GPULoadOp.Clear, + storeOp: GPUStoreOp.Store, + }); + } + } + + if (supportsDepth || supportsStencil) { + const depthTextureData = this.get(renderTargetData.depthTexture); + + depthStencilAttachment = { + view: depthTextureData.texture.createView(), + }; + } + } + + // + + if (supportsDepth) { + if (depth) { + depthStencilAttachment.depthLoadOp = GPULoadOp.Clear; + depthStencilAttachment.depthClearValue = renderer.getClearDepth(); + depthStencilAttachment.depthStoreOp = GPUStoreOp.Store; + } else { + depthStencilAttachment.depthLoadOp = GPULoadOp.Load; + depthStencilAttachment.depthStoreOp = GPUStoreOp.Store; + } + } + + // + + if (supportsStencil) { + if (stencil) { + depthStencilAttachment.stencilLoadOp = GPULoadOp.Clear; + depthStencilAttachment.stencilClearValue = renderer.getClearStencil(); + depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store; + } else { + depthStencilAttachment.stencilLoadOp = GPULoadOp.Load; + depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store; + } + } + + // + + const encoder = device.createCommandEncoder({}); + const currentPass = encoder.beginRenderPass({ + colorAttachments, + depthStencilAttachment, + }); + + currentPass.end(); + + device.queue.submit([encoder.finish()]); + } + + // compute + + beginCompute(computeGroup) { + const groupGPU = this.get(computeGroup); + + const descriptor = {}; + + this.initTimestampQuery(computeGroup, descriptor); + + groupGPU.cmdEncoderGPU = this.device.createCommandEncoder(); + + groupGPU.passEncoderGPU = groupGPU.cmdEncoderGPU.beginComputePass(descriptor); + } + + compute(computeGroup, computeNode, bindings, pipeline) { + const { passEncoderGPU } = this.get(computeGroup); + + // pipeline + + const pipelineGPU = this.get(pipeline).pipeline; + passEncoderGPU.setPipeline(pipelineGPU); + + // bind groups + + for (let i = 0, l = bindings.length; i < l; i++) { + const bindGroup = bindings[i]; + const bindingsData = this.get(bindGroup); + + passEncoderGPU.setBindGroup(i, bindingsData.group); + } + + const maxComputeWorkgroupsPerDimension = this.device.limits.maxComputeWorkgroupsPerDimension; + + const computeNodeData = this.get(computeNode); + + if (computeNodeData.dispatchSize === undefined) computeNodeData.dispatchSize = { x: 0, y: 1, z: 1 }; + + const { dispatchSize } = computeNodeData; + + if (computeNode.dispatchCount > maxComputeWorkgroupsPerDimension) { + dispatchSize.x = Math.min(computeNode.dispatchCount, maxComputeWorkgroupsPerDimension); + dispatchSize.y = Math.ceil(computeNode.dispatchCount / maxComputeWorkgroupsPerDimension); + } else { + dispatchSize.x = computeNode.dispatchCount; + } + + passEncoderGPU.dispatchWorkgroups(dispatchSize.x, dispatchSize.y, dispatchSize.z); + } + + finishCompute(computeGroup) { + const groupData = this.get(computeGroup); + + groupData.passEncoderGPU.end(); + + this.prepareTimestampBuffer(computeGroup, groupData.cmdEncoderGPU); + + this.device.queue.submit([groupData.cmdEncoderGPU.finish()]); + } + + // render object + + draw(renderObject, info) { + const { object, geometry, context, pipeline } = renderObject; + + const bindings = renderObject.getBindings(); + const contextData = this.get(context); + const pipelineGPU = this.get(pipeline).pipeline; + const currentSets = contextData.currentSets; + + const renderObjectData = this.get(renderObject); + + const { bundleEncoder, renderBundle, lastPipelineGPU } = renderObjectData; + + const renderContextData = this.get(context); + + if ( + renderContextData.registerBundlesPhase === true && + bundleEncoder !== undefined && + lastPipelineGPU === pipelineGPU + ) { + renderContextData.renderBundles.push(renderBundle); + return; + } + + const passEncoderGPU = this.renderer._currentRenderBundle + ? this.createBundleEncoder(context, renderObject) + : contextData.currentPass; + + // pipeline + + if (currentSets.pipeline !== pipelineGPU) { + passEncoderGPU.setPipeline(pipelineGPU); + + currentSets.pipeline = pipelineGPU; + } + + // bind groups + + for (let i = 0, l = bindings.length; i < l; i++) { + const bindGroup = bindings[i]; + const bindingsData = this.get(bindGroup); + + passEncoderGPU.setBindGroup(bindGroup.index, bindingsData.group); + } + + // attributes + + const index = renderObject.getIndex(); + + const hasIndex = index !== null; + + // index + + if (hasIndex === true) { + if (currentSets.index !== index) { + const buffer = this.get(index).buffer; + const indexFormat = index.array instanceof Uint16Array ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32; + + passEncoderGPU.setIndexBuffer(buffer, indexFormat); + + currentSets.index = index; + } + } + + // vertex buffers + + const vertexBuffers = renderObject.getVertexBuffers(); + + for (let i = 0, l = vertexBuffers.length; i < l; i++) { + const vertexBuffer = vertexBuffers[i]; + + if (currentSets.attributes[i] !== vertexBuffer) { + const buffer = this.get(vertexBuffer).buffer; + passEncoderGPU.setVertexBuffer(i, buffer); + + currentSets.attributes[i] = vertexBuffer; + } + } + + // occlusion queries - handle multiple consecutive draw calls for an object + + if (contextData.occlusionQuerySet !== undefined) { + const lastObject = contextData.lastOcclusionObject; + + if (lastObject !== object) { + if (lastObject !== null && lastObject.occlusionTest === true) { + passEncoderGPU.endOcclusionQuery(); + contextData.occlusionQueryIndex++; + } + + if (object.occlusionTest === true) { + passEncoderGPU.beginOcclusionQuery(contextData.occlusionQueryIndex); + contextData.occlusionQueryObjects[contextData.occlusionQueryIndex] = object; + } + + contextData.lastOcclusionObject = object; + } + } + + // draw + + const drawRange = renderObject.drawRange; + const firstVertex = drawRange.start; + + const instanceCount = this.getInstanceCount(renderObject); + if (instanceCount === 0) return; + + if (object.isBatchedMesh === true) { + const starts = object._multiDrawStarts; + const counts = object._multiDrawCounts; + const drawCount = object._multiDrawCount; + const drawInstances = object._multiDrawInstances; + + const bytesPerElement = index.bytesPerElement || 1; + + for (let i = 0; i < drawCount; i++) { + const count = drawInstances ? drawInstances[i] : 1; + const firstInstance = count > 1 ? 0 : i; + + passEncoderGPU.drawIndexed(counts[i] / bytesPerElement, count, starts[i] / 4, 0, firstInstance); + } + } else if (hasIndex === true) { + const indexCount = drawRange.count !== Infinity ? drawRange.count : index.count; + + passEncoderGPU.drawIndexed(indexCount, instanceCount, firstVertex, 0, 0); + + info.update(object, indexCount, instanceCount); + } else { + const positionAttribute = geometry.attributes.position; + const vertexCount = drawRange.count !== Infinity ? drawRange.count : positionAttribute.count; + + passEncoderGPU.draw(vertexCount, instanceCount, firstVertex, 0); + + info.update(object, vertexCount, instanceCount); + } + + if (this.renderer._currentRenderBundle) { + const renderBundle = passEncoderGPU.finish(); + renderObjectData.lastPipelineGPU = pipelineGPU; + renderObjectData.renderBundle = renderBundle; + renderObjectData.bundleEncoder = passEncoderGPU; + } + } + + // cache key + + needsRenderUpdate(renderObject) { + const data = this.get(renderObject); + + const { object, material } = renderObject; + + const utils = this.utils; + + const sampleCount = utils.getSampleCountRenderContext(renderObject.context); + const colorSpace = utils.getCurrentColorSpace(renderObject.context); + const colorFormat = utils.getCurrentColorFormat(renderObject.context); + const depthStencilFormat = utils.getCurrentDepthStencilFormat(renderObject.context); + const primitiveTopology = utils.getPrimitiveTopology(object, material); + + let needsUpdate = false; + + if ( + data.material !== material || + data.materialVersion !== material.version || + data.transparent !== material.transparent || + data.blending !== material.blending || + data.premultipliedAlpha !== material.premultipliedAlpha || + data.blendSrc !== material.blendSrc || + data.blendDst !== material.blendDst || + data.blendEquation !== material.blendEquation || + data.blendSrcAlpha !== material.blendSrcAlpha || + data.blendDstAlpha !== material.blendDstAlpha || + data.blendEquationAlpha !== material.blendEquationAlpha || + data.colorWrite !== material.colorWrite || + data.depthWrite !== material.depthWrite || + data.depthTest !== material.depthTest || + data.depthFunc !== material.depthFunc || + data.stencilWrite !== material.stencilWrite || + data.stencilFunc !== material.stencilFunc || + data.stencilFail !== material.stencilFail || + data.stencilZFail !== material.stencilZFail || + data.stencilZPass !== material.stencilZPass || + data.stencilFuncMask !== material.stencilFuncMask || + data.stencilWriteMask !== material.stencilWriteMask || + data.side !== material.side || + data.alphaToCoverage !== material.alphaToCoverage || + data.sampleCount !== sampleCount || + data.colorSpace !== colorSpace || + data.colorFormat !== colorFormat || + data.depthStencilFormat !== depthStencilFormat || + data.primitiveTopology !== primitiveTopology || + data.clippingContextVersion !== renderObject.clippingContextVersion + ) { + data.material = material; + data.materialVersion = material.version; + data.transparent = material.transparent; + data.blending = material.blending; + data.premultipliedAlpha = material.premultipliedAlpha; + data.blendSrc = material.blendSrc; + data.blendDst = material.blendDst; + data.blendEquation = material.blendEquation; + data.blendSrcAlpha = material.blendSrcAlpha; + data.blendDstAlpha = material.blendDstAlpha; + data.blendEquationAlpha = material.blendEquationAlpha; + data.colorWrite = material.colorWrite; + data.depthWrite = material.depthWrite; + data.depthTest = material.depthTest; + data.depthFunc = material.depthFunc; + data.stencilWrite = material.stencilWrite; + data.stencilFunc = material.stencilFunc; + data.stencilFail = material.stencilFail; + data.stencilZFail = material.stencilZFail; + data.stencilZPass = material.stencilZPass; + data.stencilFuncMask = material.stencilFuncMask; + data.stencilWriteMask = material.stencilWriteMask; + data.side = material.side; + data.alphaToCoverage = material.alphaToCoverage; + data.sampleCount = sampleCount; + data.colorSpace = colorSpace; + data.colorFormat = colorFormat; + data.depthStencilFormat = depthStencilFormat; + data.primitiveTopology = primitiveTopology; + data.clippingContextVersion = renderObject.clippingContextVersion; + + needsUpdate = true; + } + + return needsUpdate; + } + + getRenderCacheKey(renderObject) { + const { object, material } = renderObject; + + const utils = this.utils; + const renderContext = renderObject.context; + + return [ + material.transparent, + material.blending, + material.premultipliedAlpha, + material.blendSrc, + material.blendDst, + material.blendEquation, + material.blendSrcAlpha, + material.blendDstAlpha, + material.blendEquationAlpha, + material.colorWrite, + material.depthWrite, + material.depthTest, + material.depthFunc, + material.stencilWrite, + material.stencilFunc, + material.stencilFail, + material.stencilZFail, + material.stencilZPass, + material.stencilFuncMask, + material.stencilWriteMask, + material.side, + utils.getSampleCountRenderContext(renderContext), + utils.getCurrentColorSpace(renderContext), + utils.getCurrentColorFormat(renderContext), + utils.getCurrentDepthStencilFormat(renderContext), + utils.getPrimitiveTopology(object, material), + renderObject.clippingContextVersion, + ].join(); + } + + // textures + + createSampler(texture) { + this.textureUtils.createSampler(texture); + } + + destroySampler(texture) { + this.textureUtils.destroySampler(texture); + } + + createDefaultTexture(texture) { + this.textureUtils.createDefaultTexture(texture); + } + + createTexture(texture, options) { + this.textureUtils.createTexture(texture, options); + } + + updateTexture(texture, options) { + this.textureUtils.updateTexture(texture, options); + } + + generateMipmaps(texture) { + this.textureUtils.generateMipmaps(texture); + } + + destroyTexture(texture) { + this.textureUtils.destroyTexture(texture); + } + + copyTextureToBuffer(texture, x, y, width, height) { + return this.textureUtils.copyTextureToBuffer(texture, x, y, width, height); + } + + initTimestampQuery(renderContext, descriptor) { + if (!this.hasFeature(GPUFeatureName.TimestampQuery) || !this.trackTimestamp) return; + + const renderContextData = this.get(renderContext); + + if (!renderContextData.timeStampQuerySet) { + // Create a GPUQuerySet which holds 2 timestamp query results: one for the + // beginning and one for the end of compute pass execution. + const timeStampQuerySet = this.device.createQuerySet({ type: 'timestamp', count: 2 }); + + const timestampWrites = { + querySet: timeStampQuerySet, + beginningOfPassWriteIndex: 0, // Write timestamp in index 0 when pass begins. + endOfPassWriteIndex: 1, // Write timestamp in index 1 when pass ends. + }; + + Object.assign(descriptor, { + timestampWrites, + }); + + renderContextData.timeStampQuerySet = timeStampQuerySet; + } + } + + // timestamp utils + + prepareTimestampBuffer(renderContext, encoder) { + if (!this.hasFeature(GPUFeatureName.TimestampQuery) || !this.trackTimestamp) return; + + const renderContextData = this.get(renderContext); + + const size = 2 * BigInt64Array.BYTES_PER_ELEMENT; + + if (renderContextData.currentTimestampQueryBuffers === undefined) { + renderContextData.currentTimestampQueryBuffers = { + resolveBuffer: this.device.createBuffer({ + label: 'timestamp resolve buffer', + size: size, + usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC, + }), + resultBuffer: this.device.createBuffer({ + label: 'timestamp result buffer', + size: size, + usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ, + }), + isMappingPending: false, + }; + } + + const { resolveBuffer, resultBuffer, isMappingPending } = renderContextData.currentTimestampQueryBuffers; + + if (isMappingPending === true) return; + + encoder.resolveQuerySet(renderContextData.timeStampQuerySet, 0, 2, resolveBuffer, 0); + encoder.copyBufferToBuffer(resolveBuffer, 0, resultBuffer, 0, size); + } + + async resolveTimestampAsync(renderContext, type = 'render') { + if (!this.hasFeature(GPUFeatureName.TimestampQuery) || !this.trackTimestamp) return; + + const renderContextData = this.get(renderContext); + + if (renderContextData.currentTimestampQueryBuffers === undefined) return; + + const { resultBuffer, isMappingPending } = renderContextData.currentTimestampQueryBuffers; + + if (isMappingPending === true) return; + + renderContextData.currentTimestampQueryBuffers.isMappingPending = true; + + resultBuffer.mapAsync(GPUMapMode.READ).then(() => { + const times = new BigUint64Array(resultBuffer.getMappedRange()); + const duration = Number(times[1] - times[0]) / 1000000; + + this.renderer.info.updateTimestamp(type, duration); + + resultBuffer.unmap(); + + renderContextData.currentTimestampQueryBuffers.isMappingPending = false; + }); + } + + // node builder + + createNodeBuilder(object, renderer) { + return new WGSLNodeBuilder(object, renderer); + } + + // program + + createProgram(program) { + const programGPU = this.get(program); + + programGPU.module = { + module: this.device.createShaderModule({ code: program.code, label: program.stage }), + entryPoint: 'main', + }; + } + + destroyProgram(program) { + this.delete(program); + } + + // pipelines + + createRenderPipeline(renderObject, promises) { + this.pipelineUtils.createRenderPipeline(renderObject, promises); + } + + createComputePipeline(computePipeline, bindings) { + this.pipelineUtils.createComputePipeline(computePipeline, bindings); + } + + createBundleEncoder(renderContext, renderObject) { + return this.pipelineUtils.createBundleEncoder(renderContext, renderObject); + } + + // bindings + + createBindings(bindGroup) { + this.bindingUtils.createBindings(bindGroup); + } + + updateBindings(bindGroup) { + this.bindingUtils.createBindings(bindGroup); + } + + updateBinding(binding) { + this.bindingUtils.updateBinding(binding); + } + + // attributes + + createIndexAttribute(attribute) { + this.attributeUtils.createAttribute( + attribute, + GPUBufferUsage.INDEX | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, + ); + } + + createAttribute(attribute) { + this.attributeUtils.createAttribute( + attribute, + GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, + ); + } + + createStorageAttribute(attribute) { + this.attributeUtils.createAttribute( + attribute, + GPUBufferUsage.STORAGE | GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, + ); + } + + updateAttribute(attribute) { + this.attributeUtils.updateAttribute(attribute); + } + + destroyAttribute(attribute) { + this.attributeUtils.destroyAttribute(attribute); + } + + // canvas + + updateSize() { + this.colorBuffer = this.textureUtils.getColorBuffer(); + this.defaultRenderPassdescriptor = null; + } + + // utils public + + getMaxAnisotropy() { + return 16; + } + + hasFeature(name) { + return this.device.features.has(name); + } + + copyTextureToTexture(srcTexture, dstTexture, srcRegion = null, dstPosition = null, level = 0) { + let dstX = 0; + let dstY = 0; + + let srcX = 0; + let srcY = 0; + + let srcWidth = srcTexture.image.width; + let srcHeight = srcTexture.image.height; + + if (srcRegion !== null) { + srcX = srcRegion.x; + srcY = srcRegion.y; + srcWidth = srcRegion.width; + srcHeight = srcRegion.height; + } + + if (dstPosition !== null) { + dstX = dstPosition.x; + dstY = dstPosition.y; + } + + const encoder = this.device.createCommandEncoder({ + label: 'copyTextureToTexture_' + srcTexture.id + '_' + dstTexture.id, + }); + + const sourceGPU = this.get(srcTexture).texture; + const destinationGPU = this.get(dstTexture).texture; + + encoder.copyTextureToTexture( + { + texture: sourceGPU, + mipLevel: level, + origin: { x: srcX, y: srcY, z: 0 }, + }, + { + texture: destinationGPU, + mipLevel: level, + origin: { x: dstX, y: dstY, z: 0 }, + }, + [srcWidth, srcHeight], + ); + + this.device.queue.submit([encoder.finish()]); + } + + copyFramebufferToTexture(texture, renderContext) { + const renderContextData = this.get(renderContext); + + const { encoder, descriptor } = renderContextData; + + let sourceGPU = null; + + if (renderContext.renderTarget) { + if (texture.isDepthTexture) { + sourceGPU = this.get(renderContext.depthTexture).texture; + } else { + sourceGPU = this.get(renderContext.textures[0]).texture; + } + } else { + if (texture.isDepthTexture) { + sourceGPU = this.textureUtils.getDepthBuffer(renderContext.depth, renderContext.stencil); + } else { + sourceGPU = this.context.getCurrentTexture(); + } + } + + const destinationGPU = this.get(texture).texture; + + if (sourceGPU.format !== destinationGPU.format) { + console.error( + 'WebGPUBackend: copyFramebufferToTexture: Source and destination formats do not match.', + sourceGPU.format, + destinationGPU.format, + ); + + return; + } + + renderContextData.currentPass.end(); + + encoder.copyTextureToTexture( + { + texture: sourceGPU, + origin: { x: 0, y: 0, z: 0 }, + }, + { + texture: destinationGPU, + }, + [texture.image.width, texture.image.height], + ); + + if (texture.generateMipmaps) this.textureUtils.generateMipmaps(texture); + + for (let i = 0; i < descriptor.colorAttachments.length; i++) { + descriptor.colorAttachments[i].loadOp = GPULoadOp.Load; + } + + if (renderContext.depth) descriptor.depthStencilAttachment.depthLoadOp = GPULoadOp.Load; + if (renderContext.stencil) descriptor.depthStencilAttachment.stencilLoadOp = GPULoadOp.Load; + + renderContextData.currentPass = encoder.beginRenderPass(descriptor); + renderContextData.currentSets = { attributes: {} }; + } +} + +export default WebGPUBackend; diff --git a/src-testing/src/renderers/webgpu/WebGPURenderer.ts b/src-testing/src/renderers/webgpu/WebGPURenderer.ts new file mode 100644 index 000000000..7a25c2677 --- /dev/null +++ b/src-testing/src/renderers/webgpu/WebGPURenderer.ts @@ -0,0 +1,43 @@ +import WebGPU from '../../../examples/jsm/capabilities/WebGPU.js'; + +import Renderer from '../common/Renderer.js'; +import WebGLBackend from '../webgl-fallback/WebGLBackend.js'; +import WebGPUBackend from './WebGPUBackend.js'; +/* +const debugHandler = { + + get: function ( target, name ) { + + // Add |update + if ( /^(create|destroy)/.test( name ) ) console.log( 'WebGPUBackend.' + name ); + + return target[ name ]; + + } + +}; +*/ +class WebGPURenderer extends Renderer { + constructor(parameters = {}) { + let BackendClass; + + if (parameters.forceWebGL) { + BackendClass = WebGLBackend; + } else if (WebGPU.isAvailable()) { + BackendClass = WebGPUBackend; + } else { + BackendClass = WebGLBackend; + + console.warn('THREE.WebGPURenderer: WebGPU is not available, running under WebGL2 backend.'); + } + + const backend = new BackendClass(parameters); + + //super( new Proxy( backend, debugHandler ) ); + super(backend, parameters); + + this.isWebGPURenderer = true; + } +} + +export default WebGPURenderer; diff --git a/src-testing/src/renderers/webgpu/nodes/WGSLNodeBuilder.ts b/src-testing/src/renderers/webgpu/nodes/WGSLNodeBuilder.ts new file mode 100644 index 000000000..1d808f4be --- /dev/null +++ b/src-testing/src/renderers/webgpu/nodes/WGSLNodeBuilder.ts @@ -0,0 +1,1114 @@ +import NodeUniformsGroup from '../../common/nodes/NodeUniformsGroup.js'; + +import NodeSampler from '../../common/nodes/NodeSampler.js'; +import { + NodeSampledTexture, + NodeSampledCubeTexture, + NodeSampledTexture3D, +} from '../../common/nodes/NodeSampledTexture.js'; + +import NodeUniformBuffer from '../../common/nodes/NodeUniformBuffer.js'; +import NodeStorageBuffer from '../../common/nodes/NodeStorageBuffer.js'; + +import { NodeBuilder, CodeNode } from '../../../nodes/Nodes.js'; + +import { getFormat } from '../utils/WebGPUTextureUtils.js'; + +import WGSLNodeParser from './WGSLNodeParser.js'; +import { GPUBufferBindingType, GPUStorageTextureAccess } from '../utils/WebGPUConstants.js'; + +import { NoColorSpace, FloatType } from '../../../constants.js'; + +// GPUShaderStage is not defined in browsers not supporting WebGPU +const GPUShaderStage = self.GPUShaderStage; + +const gpuShaderStageLib = { + vertex: GPUShaderStage ? GPUShaderStage.VERTEX : 1, + fragment: GPUShaderStage ? GPUShaderStage.FRAGMENT : 2, + compute: GPUShaderStage ? GPUShaderStage.COMPUTE : 4, +}; + +const supports = { + instance: true, + swizzleAssign: false, + storageBuffer: true, +}; + +const wgslFnOpLib = { + '^^': 'tsl_xor', +}; + +const wgslTypeLib = { + float: 'f32', + int: 'i32', + uint: 'u32', + bool: 'bool', + color: 'vec3', + + vec2: 'vec2', + ivec2: 'vec2', + uvec2: 'vec2', + bvec2: 'vec2', + + vec3: 'vec3', + ivec3: 'vec3', + uvec3: 'vec3', + bvec3: 'vec3', + + vec4: 'vec4', + ivec4: 'vec4', + uvec4: 'vec4', + bvec4: 'vec4', + + mat2: 'mat2x2', + imat2: 'mat2x2', + umat2: 'mat2x2', + bmat2: 'mat2x2', + + mat3: 'mat3x3', + imat3: 'mat3x3', + umat3: 'mat3x3', + bmat3: 'mat3x3', + + mat4: 'mat4x4', + imat4: 'mat4x4', + umat4: 'mat4x4', + bmat4: 'mat4x4', +}; + +const wgslPolyfill = { + tsl_xor: new CodeNode('fn tsl_xor( a : bool, b : bool ) -> bool { return ( a || b ) && !( a && b ); }'), + mod_float: new CodeNode('fn tsl_mod_float( x : f32, y : f32 ) -> f32 { return x - y * floor( x / y ); }'), + mod_vec2: new CodeNode('fn tsl_mod_vec2( x : vec2f, y : vec2f ) -> vec2f { return x - y * floor( x / y ); }'), + mod_vec3: new CodeNode('fn tsl_mod_vec3( x : vec3f, y : vec3f ) -> vec3f { return x - y * floor( x / y ); }'), + mod_vec4: new CodeNode('fn tsl_mod_vec4( x : vec4f, y : vec4f ) -> vec4f { return x - y * floor( x / y ); }'), + equals_bool: new CodeNode('fn tsl_equals_bool( a : bool, b : bool ) -> bool { return a == b; }'), + equals_bvec2: new CodeNode( + 'fn tsl_equals_bvec2( a : vec2f, b : vec2f ) -> vec2 { return vec2( a.x == b.x, a.y == b.y ); }', + ), + equals_bvec3: new CodeNode( + 'fn tsl_equals_bvec3( a : vec3f, b : vec3f ) -> vec3 { return vec3( a.x == b.x, a.y == b.y, a.z == b.z ); }', + ), + equals_bvec4: new CodeNode( + 'fn tsl_equals_bvec4( a : vec4f, b : vec4f ) -> vec4 { return vec4( a.x == b.x, a.y == b.y, a.z == b.z, a.w == b.w ); }', + ), + repeatWrapping: new CodeNode(` +fn tsl_repeatWrapping( uv : vec2, dimension : vec2 ) -> vec2 { + + let uvScaled = vec2( uv * vec2( dimension ) ); + + return ( ( uvScaled % dimension ) + dimension ) % dimension; + +} +`), + biquadraticTexture: new CodeNode(` +fn tsl_biquadraticTexture( map : texture_2d, coord : vec2f, level : i32 ) -> vec4f { + + let res = vec2f( textureDimensions( map, level ) ); + + let uvScaled = coord * res; + let uvWrapping = ( ( uvScaled % res ) + res ) % res; + + // https://www.shadertoy.com/view/WtyXRy + + let uv = uvWrapping - 0.5; + let iuv = floor( uv ); + let f = fract( uv ); + + let rg1 = textureLoad( map, vec2i( iuv + vec2( 0.5, 0.5 ) ), level ); + let rg2 = textureLoad( map, vec2i( iuv + vec2( 1.5, 0.5 ) ), level ); + let rg3 = textureLoad( map, vec2i( iuv + vec2( 0.5, 1.5 ) ), level ); + let rg4 = textureLoad( map, vec2i( iuv + vec2( 1.5, 1.5 ) ), level ); + + return mix( mix( rg1, rg2, f.x ), mix( rg3, rg4, f.x ), f.y ); + +} +`), +}; + +const wgslMethods = { + dFdx: 'dpdx', + dFdy: '- dpdy', + mod_float: 'tsl_mod_float', + mod_vec2: 'tsl_mod_vec2', + mod_vec3: 'tsl_mod_vec3', + mod_vec4: 'tsl_mod_vec4', + equals_bool: 'tsl_equals_bool', + equals_bvec2: 'tsl_equals_bvec2', + equals_bvec3: 'tsl_equals_bvec3', + equals_bvec4: 'tsl_equals_bvec4', + inversesqrt: 'inverseSqrt', + bitcast: 'bitcast', +}; + +// WebGPU issue: does not support pow() with negative base on Windows + +if (/Windows/g.test(navigator.userAgent)) { + wgslPolyfill.pow_float = new CodeNode( + 'fn tsl_pow_float( a : f32, b : f32 ) -> f32 { return select( -pow( -a, b ), pow( a, b ), a > 0.0 ); }', + ); + wgslPolyfill.pow_vec2 = new CodeNode( + 'fn tsl_pow_vec2( a : vec2f, b : vec2f ) -> vec2f { return vec2f( tsl_pow_float( a.x, b.x ), tsl_pow_float( a.y, b.y ) ); }', + [wgslPolyfill.pow_float], + ); + wgslPolyfill.pow_vec3 = new CodeNode( + 'fn tsl_pow_vec3( a : vec3f, b : vec3f ) -> vec3f { return vec3f( tsl_pow_float( a.x, b.x ), tsl_pow_float( a.y, b.y ), tsl_pow_float( a.z, b.z ) ); }', + [wgslPolyfill.pow_float], + ); + wgslPolyfill.pow_vec4 = new CodeNode( + 'fn tsl_pow_vec4( a : vec4f, b : vec4f ) -> vec4f { return vec4f( tsl_pow_float( a.x, b.x ), tsl_pow_float( a.y, b.y ), tsl_pow_float( a.z, b.z ), tsl_pow_float( a.w, b.w ) ); }', + [wgslPolyfill.pow_float], + ); + + wgslMethods.pow_float = 'tsl_pow_float'; + wgslMethods.pow_vec2 = 'tsl_pow_vec2'; + wgslMethods.pow_vec3 = 'tsl_pow_vec3'; + wgslMethods.pow_vec4 = 'tsl_pow_vec4'; +} + +// + +class WGSLNodeBuilder extends NodeBuilder { + constructor(object, renderer) { + super(object, renderer, new WGSLNodeParser()); + + this.uniformGroups = {}; + + this.builtins = {}; + + this.directives = {}; + } + + needsColorSpaceToLinear(texture) { + return texture.isVideoTexture === true && texture.colorSpace !== NoColorSpace; + } + + _generateTextureSample(texture, textureProperty, uvSnippet, depthSnippet, shaderStage = this.shaderStage) { + if (shaderStage === 'fragment') { + if (depthSnippet) { + return `textureSample( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet}, ${depthSnippet} )`; + } else { + return `textureSample( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet} )`; + } + } else if (this.isFilteredTexture(texture)) { + return this.generateFilteredTexture(texture, textureProperty, uvSnippet); + } else { + return this.generateTextureLod(texture, textureProperty, uvSnippet, '0'); + } + } + + _generateVideoSample(textureProperty, uvSnippet, shaderStage = this.shaderStage) { + if (shaderStage === 'fragment') { + return `textureSampleBaseClampToEdge( ${textureProperty}, ${textureProperty}_sampler, vec2( ${uvSnippet}.x, 1.0 - ${uvSnippet}.y ) )`; + } else { + console.error(`WebGPURenderer: THREE.VideoTexture does not support ${shaderStage} shader.`); + } + } + + _generateTextureSampleLevel( + texture, + textureProperty, + uvSnippet, + levelSnippet, + depthSnippet, + shaderStage = this.shaderStage, + ) { + if (shaderStage === 'fragment' && this.isUnfilterable(texture) === false) { + return `textureSampleLevel( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet}, ${levelSnippet} )`; + } else if (this.isFilteredTexture(texture)) { + return this.generateFilteredTexture(texture, textureProperty, uvSnippet, levelSnippet); + } else { + return this.generateTextureLod(texture, textureProperty, uvSnippet, levelSnippet); + } + } + + generateFilteredTexture(texture, textureProperty, uvSnippet, levelSnippet = '0') { + this._include('biquadraticTexture'); + + return `tsl_biquadraticTexture( ${textureProperty}, ${uvSnippet}, i32( ${levelSnippet} ) )`; + } + + generateTextureLod(texture, textureProperty, uvSnippet, levelSnippet = '0') { + this._include('repeatWrapping'); + + const dimension = + texture.isMultisampleRenderTargetTexture === true + ? `textureDimensions( ${textureProperty} )` + : `textureDimensions( ${textureProperty}, 0 )`; + + return `textureLoad( ${textureProperty}, tsl_repeatWrapping( ${uvSnippet}, ${dimension} ), i32( ${levelSnippet} ) )`; + } + + generateTextureLoad(texture, textureProperty, uvIndexSnippet, depthSnippet, levelSnippet = '0u') { + if (depthSnippet) { + return `textureLoad( ${textureProperty}, ${uvIndexSnippet}, ${depthSnippet}, ${levelSnippet} )`; + } else { + return `textureLoad( ${textureProperty}, ${uvIndexSnippet}, ${levelSnippet} )`; + } + } + + generateTextureStore(texture, textureProperty, uvIndexSnippet, valueSnippet) { + return `textureStore( ${textureProperty}, ${uvIndexSnippet}, ${valueSnippet} )`; + } + + isUnfilterable(texture) { + return ( + this.getComponentTypeFromTexture(texture) !== 'float' || + (texture.isDataTexture === true && texture.type === FloatType) || + texture.isMultisampleRenderTargetTexture === true + ); + } + + generateTexture(texture, textureProperty, uvSnippet, depthSnippet, shaderStage = this.shaderStage) { + let snippet = null; + + if (texture.isVideoTexture === true) { + snippet = this._generateVideoSample(textureProperty, uvSnippet, shaderStage); + } else if (this.isUnfilterable(texture)) { + snippet = this.generateTextureLod(texture, textureProperty, uvSnippet, '0', depthSnippet, shaderStage); + } else { + snippet = this._generateTextureSample(texture, textureProperty, uvSnippet, depthSnippet, shaderStage); + } + + return snippet; + } + + generateTextureGrad( + texture, + textureProperty, + uvSnippet, + gradSnippet, + depthSnippet, + shaderStage = this.shaderStage, + ) { + if (shaderStage === 'fragment') { + // TODO handle i32 or u32 --> uvSnippet, array_index: A, ddx, ddy + return `textureSampleGrad( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet}, ${gradSnippet[0]}, ${gradSnippet[1]} )`; + } else { + console.error(`WebGPURenderer: THREE.TextureNode.gradient() does not support ${shaderStage} shader.`); + } + } + + generateTextureCompare( + texture, + textureProperty, + uvSnippet, + compareSnippet, + depthSnippet, + shaderStage = this.shaderStage, + ) { + if (shaderStage === 'fragment') { + return `textureSampleCompare( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet}, ${compareSnippet} )`; + } else { + console.error( + `WebGPURenderer: THREE.DepthTexture.compareFunction() does not support ${shaderStage} shader.`, + ); + } + } + + generateTextureLevel( + texture, + textureProperty, + uvSnippet, + levelSnippet, + depthSnippet, + shaderStage = this.shaderStage, + ) { + let snippet = null; + + if (texture.isVideoTexture === true) { + snippet = this._generateVideoSample(textureProperty, uvSnippet, shaderStage); + } else { + snippet = this._generateTextureSampleLevel( + texture, + textureProperty, + uvSnippet, + levelSnippet, + depthSnippet, + shaderStage, + ); + } + + return snippet; + } + + generateTextureBias( + texture, + textureProperty, + uvSnippet, + biasSnippet, + depthSnippet, + shaderStage = this.shaderStage, + ) { + if (shaderStage === 'fragment') { + return `textureSampleBias( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet}, ${biasSnippet} )`; + } else { + console.error(`WebGPURenderer: THREE.TextureNode.biasNode does not support ${shaderStage} shader.`); + } + } + + getPropertyName(node, shaderStage = this.shaderStage) { + if (node.isNodeVarying === true && node.needsInterpolation === true) { + if (shaderStage === 'vertex') { + return `varyings.${node.name}`; + } + } else if (node.isNodeUniform === true) { + const name = node.name; + const type = node.type; + + if (type === 'texture' || type === 'cubeTexture' || type === 'storageTexture' || type === 'texture3D') { + return name; + } else if (type === 'buffer' || type === 'storageBuffer') { + return `NodeBuffer_${node.id}.${name}`; + } else { + return node.groupNode.name + '.' + name; + } + } + + return super.getPropertyName(node); + } + + getOutputStructName() { + return 'output'; + } + + _getUniformGroupCount(shaderStage) { + return Object.keys(this.uniforms[shaderStage]).length; + } + + getFunctionOperator(op) { + const fnOp = wgslFnOpLib[op]; + + if (fnOp !== undefined) { + this._include(fnOp); + + return fnOp; + } + + return null; + } + + getStorageAccess(node) { + if (node.isStorageTextureNode) { + switch (node.access) { + case GPUStorageTextureAccess.ReadOnly: + return 'read'; + + case GPUStorageTextureAccess.WriteOnly: + return 'write'; + + default: + return 'read_write'; + } + } else { + switch (node.access) { + case GPUBufferBindingType.Storage: + return 'read_write'; + + case GPUBufferBindingType.ReadOnlyStorage: + return 'read'; + + default: + return 'write'; + } + } + } + + getUniformFromNode(node, type, shaderStage, name = null) { + const uniformNode = super.getUniformFromNode(node, type, shaderStage, name); + const nodeData = this.getDataFromNode(node, shaderStage, this.globalCache); + + if (nodeData.uniformGPU === undefined) { + let uniformGPU; + + const group = node.groupNode; + const groupName = group.name; + + const bindings = this.getBindGroupArray(groupName, shaderStage); + + if (type === 'texture' || type === 'cubeTexture' || type === 'storageTexture' || type === 'texture3D') { + let texture = null; + + if (type === 'texture' || type === 'storageTexture') { + texture = new NodeSampledTexture( + uniformNode.name, + uniformNode.node, + group, + node.access ? node.access : null, + ); + } else if (type === 'cubeTexture') { + texture = new NodeSampledCubeTexture( + uniformNode.name, + uniformNode.node, + group, + node.access ? node.access : null, + ); + } else if (type === 'texture3D') { + texture = new NodeSampledTexture3D( + uniformNode.name, + uniformNode.node, + group, + node.access ? node.access : null, + ); + } + + texture.store = node.isStorageTextureNode === true; + texture.setVisibility(gpuShaderStageLib[shaderStage]); + + if ( + shaderStage === 'fragment' && + this.isUnfilterable(node.value) === false && + texture.store === false + ) { + const sampler = new NodeSampler(`${uniformNode.name}_sampler`, uniformNode.node, group); + sampler.setVisibility(gpuShaderStageLib[shaderStage]); + + bindings.push(sampler, texture); + + uniformGPU = [sampler, texture]; + } else { + bindings.push(texture); + + uniformGPU = [texture]; + } + } else if (type === 'buffer' || type === 'storageBuffer') { + const bufferClass = type === 'storageBuffer' ? NodeStorageBuffer : NodeUniformBuffer; + const buffer = new bufferClass(node, group); + buffer.setVisibility(gpuShaderStageLib[shaderStage]); + + bindings.push(buffer); + + uniformGPU = buffer; + } else { + const uniformsStage = this.uniformGroups[shaderStage] || (this.uniformGroups[shaderStage] = {}); + + let uniformsGroup = uniformsStage[groupName]; + + if (uniformsGroup === undefined) { + uniformsGroup = new NodeUniformsGroup(groupName, group); + uniformsGroup.setVisibility(gpuShaderStageLib[shaderStage]); + + uniformsStage[groupName] = uniformsGroup; + + bindings.push(uniformsGroup); + } + + uniformGPU = this.getNodeUniform(uniformNode, type); + + uniformsGroup.addUniform(uniformGPU); + } + + nodeData.uniformGPU = uniformGPU; + } + + return uniformNode; + } + + getBuiltin(name, property, type, shaderStage = this.shaderStage) { + const map = this.builtins[shaderStage] || (this.builtins[shaderStage] = new Map()); + + if (map.has(name) === false) { + map.set(name, { + name, + property, + type, + }); + } + + return property; + } + + getVertexIndex() { + if (this.shaderStage === 'vertex') { + return this.getBuiltin('vertex_index', 'vertexIndex', 'u32', 'attribute'); + } + + return 'vertexIndex'; + } + + buildFunctionCode(shaderNode) { + const layout = shaderNode.layout; + const flowData = this.flowShaderNode(shaderNode); + + const parameters = []; + + for (const input of layout.inputs) { + parameters.push(input.name + ' : ' + this.getType(input.type)); + } + + // + + const code = `fn ${layout.name}( ${parameters.join(', ')} ) -> ${this.getType(layout.type)} { +${flowData.vars} +${flowData.code} + return ${flowData.result}; + +}`; + + // + + return code; + } + + getInstanceIndex() { + if (this.shaderStage === 'vertex') { + return this.getBuiltin('instance_index', 'instanceIndex', 'u32', 'attribute'); + } + + return 'instanceIndex'; + } + + getSubgroupSize() { + this.enableSubGroups(); + + return this.getBuiltin('subgroup_size', 'subgroupSize', 'u32', 'attribute'); + } + + getSubgroupIndex() { + this.enableSubGroups(); + + return this.getBuiltin('subgroup_invocation_id', 'subgroupIndex', 'u32', 'attribute'); + } + + getDrawIndex() { + return null; + } + + getFrontFacing() { + return this.getBuiltin('front_facing', 'isFront', 'bool'); + } + + getFragCoord() { + return this.getBuiltin('position', 'fragCoord', 'vec4') + '.xyz'; + } + + getFragDepth() { + return 'output.' + this.getBuiltin('frag_depth', 'depth', 'f32', 'output'); + } + + isFlipY() { + return false; + } + + enableDirective(name, shaderStage = this.shaderStage) { + const stage = this.directives[shaderStage] || (this.directives[shaderStage] = new Set()); + stage.add(name); + } + + getDirectives(shaderStage) { + const snippets = []; + const directives = this.directives[shaderStage]; + + if (directives !== undefined) { + for (const directive of directives) { + snippets.push(`enable ${directive};`); + } + } + + return snippets.join('\n'); + } + + enableSubGroups() { + this.enableDirective('subgroups'); + } + + enableSubgroupsF16() { + this.enableDirective('subgroups-f16'); + } + + enableClipDistances() { + this.enableDirective('clip_distances'); + } + + enableShaderF16() { + this.enableDirective('f16'); + } + + enableDualSourceBlending() { + this.enableDirective('dual_source_blending'); + } + + getBuiltins(shaderStage) { + const snippets = []; + const builtins = this.builtins[shaderStage]; + + if (builtins !== undefined) { + for (const { name, property, type } of builtins.values()) { + snippets.push(`@builtin( ${name} ) ${property} : ${type}`); + } + } + + return snippets.join(',\n\t'); + } + + getAttributes(shaderStage) { + const snippets = []; + + if (shaderStage === 'compute') { + this.getBuiltin('global_invocation_id', 'id', 'vec3', 'attribute'); + this.getBuiltin('workgroup_id', 'workgroupId', 'vec3', 'attribute'); + this.getBuiltin('local_invocation_id', 'localId', 'vec3', 'attribute'); + this.getBuiltin('num_workgroups', 'numWorkgroups', 'vec3', 'attribute'); + } + + if (shaderStage === 'vertex' || shaderStage === 'compute') { + const builtins = this.getBuiltins('attribute'); + + if (builtins) snippets.push(builtins); + + const attributes = this.getAttributesArray(); + + for (let index = 0, length = attributes.length; index < length; index++) { + const attribute = attributes[index]; + const name = attribute.name; + const type = this.getType(attribute.type); + + snippets.push(`@location( ${index} ) ${name} : ${type}`); + } + } + + return snippets.join(',\n\t'); + } + + getStructMembers(struct) { + const snippets = []; + const members = struct.getMemberTypes(); + + for (let i = 0; i < members.length; i++) { + const member = members[i]; + snippets.push(`\t@location( ${i} ) m${i} : ${member}`); + } + + const builtins = this.getBuiltins('output'); + + if (builtins) snippets.push(builtins); + + return snippets.join(',\n'); + } + + getStructs(shaderStage) { + const snippets = []; + const structs = this.structs[shaderStage]; + + for (let index = 0, length = structs.length; index < length; index++) { + const struct = structs[index]; + const name = struct.name; + + let snippet = `\struct ${name} {\n`; + snippet += this.getStructMembers(struct); + snippet += '\n}'; + + snippets.push(snippet); + + snippets.push(`\nvar output : ${name};\n\n`); + } + + return snippets.join('\n\n'); + } + + getVar(type, name) { + return `var ${name} : ${this.getType(type)}`; + } + + getVars(shaderStage) { + const snippets = []; + const vars = this.vars[shaderStage]; + + if (vars !== undefined) { + for (const variable of vars) { + snippets.push(`\t${this.getVar(variable.type, variable.name)};`); + } + } + + return `\n${snippets.join('\n')}\n`; + } + + getVaryings(shaderStage) { + const snippets = []; + + if (shaderStage === 'vertex') { + this.getBuiltin('position', 'Vertex', 'vec4', 'vertex'); + } + + if (shaderStage === 'vertex' || shaderStage === 'fragment') { + const varyings = this.varyings; + const vars = this.vars[shaderStage]; + + for (let index = 0; index < varyings.length; index++) { + const varying = varyings[index]; + + if (varying.needsInterpolation) { + let attributesSnippet = `@location( ${index} )`; + + if (/^(int|uint|ivec|uvec)/.test(varying.type)) { + attributesSnippet += ' @interpolate( flat )'; + } + + snippets.push(`${attributesSnippet} ${varying.name} : ${this.getType(varying.type)}`); + } else if (shaderStage === 'vertex' && vars.includes(varying) === false) { + vars.push(varying); + } + } + } + + const builtins = this.getBuiltins(shaderStage); + + if (builtins) snippets.push(builtins); + + const code = snippets.join(',\n\t'); + + return shaderStage === 'vertex' ? this._getWGSLStruct('VaryingsStruct', '\t' + code) : code; + } + + getUniforms(shaderStage) { + const uniforms = this.uniforms[shaderStage]; + + const bindingSnippets = []; + const bufferSnippets = []; + const structSnippets = []; + const uniformGroups = {}; + + for (const uniform of uniforms) { + const groundName = uniform.groupNode.name; + const uniformIndexes = this.bindingsIndexes[groundName]; + + if ( + uniform.type === 'texture' || + uniform.type === 'cubeTexture' || + uniform.type === 'storageTexture' || + uniform.type === 'texture3D' + ) { + const texture = uniform.node.value; + + if ( + shaderStage === 'fragment' && + this.isUnfilterable(texture) === false && + uniform.node.isStorageTextureNode !== true + ) { + if (texture.isDepthTexture === true && texture.compareFunction !== null) { + bindingSnippets.push( + `@binding( ${uniformIndexes.binding++} ) @group( ${uniformIndexes.group} ) var ${uniform.name}_sampler : sampler_comparison;`, + ); + } else { + bindingSnippets.push( + `@binding( ${uniformIndexes.binding++} ) @group( ${uniformIndexes.group} ) var ${uniform.name}_sampler : sampler;`, + ); + } + } + + let textureType; + + let multisampled = ''; + + if (texture.isMultisampleRenderTargetTexture === true) { + multisampled = '_multisampled'; + } + + if (texture.isCubeTexture === true) { + textureType = 'texture_cube'; + } else if (texture.isDataArrayTexture === true) { + textureType = 'texture_2d_array'; + } else if (texture.isDepthTexture === true) { + textureType = `texture_depth${multisampled}_2d`; + } else if (texture.isVideoTexture === true) { + textureType = 'texture_external'; + } else if (texture.isData3DTexture === true) { + textureType = 'texture_3d'; + } else if (uniform.node.isStorageTextureNode === true) { + const format = getFormat(texture); + const access = this.getStorageAccess(uniform.node); + + textureType = `texture_storage_2d<${format}, ${access}>`; + } else { + const componentPrefix = this.getComponentTypeFromTexture(texture).charAt(0); + + textureType = `texture${multisampled}_2d<${componentPrefix}32>`; + } + + bindingSnippets.push( + `@binding( ${uniformIndexes.binding++} ) @group( ${uniformIndexes.group} ) var ${uniform.name} : ${textureType};`, + ); + } else if (uniform.type === 'buffer' || uniform.type === 'storageBuffer') { + const bufferNode = uniform.node; + const bufferType = this.getType(bufferNode.bufferType); + const bufferCount = bufferNode.bufferCount; + + const bufferCountSnippet = bufferCount > 0 ? ', ' + bufferCount : ''; + const bufferSnippet = `\t${uniform.name} : array< ${bufferType}${bufferCountSnippet} >\n`; + const bufferAccessMode = bufferNode.isStorageBufferNode + ? `storage, ${this.getStorageAccess(bufferNode)}` + : 'uniform'; + + bufferSnippets.push( + this._getWGSLStructBinding( + 'NodeBuffer_' + bufferNode.id, + bufferSnippet, + bufferAccessMode, + uniformIndexes.binding++, + uniformIndexes.group, + ), + ); + } else { + const vectorType = this.getType(this.getVectorType(uniform.type)); + const groupName = uniform.groupNode.name; + + const group = + uniformGroups[groupName] || + (uniformGroups[groupName] = { + index: uniformIndexes.binding++, + id: uniformIndexes.group, + snippets: [], + }); + + group.snippets.push(`\t${uniform.name} : ${vectorType}`); + } + } + + for (const name in uniformGroups) { + const group = uniformGroups[name]; + + structSnippets.push( + this._getWGSLStructBinding(name, group.snippets.join(',\n'), 'uniform', group.index, group.id), + ); + } + + let code = bindingSnippets.join('\n'); + code += bufferSnippets.join('\n'); + code += structSnippets.join('\n'); + + return code; + } + + buildCode() { + const shadersData = this.material !== null ? { fragment: {}, vertex: {} } : { compute: {} }; + + for (const shaderStage in shadersData) { + const stageData = shadersData[shaderStage]; + stageData.uniforms = this.getUniforms(shaderStage); + stageData.attributes = this.getAttributes(shaderStage); + stageData.varyings = this.getVaryings(shaderStage); + stageData.structs = this.getStructs(shaderStage); + stageData.vars = this.getVars(shaderStage); + stageData.codes = this.getCodes(shaderStage); + stageData.directives = this.getDirectives(shaderStage); + + // + + let flow = '// code\n\n'; + flow += this.flowCode[shaderStage]; + + const flowNodes = this.flowNodes[shaderStage]; + const mainNode = flowNodes[flowNodes.length - 1]; + + const outputNode = mainNode.outputNode; + const isOutputStruct = outputNode !== undefined && outputNode.isOutputStructNode === true; + + for (const node of flowNodes) { + const flowSlotData = this.getFlowData(node /*, shaderStage*/); + const slotName = node.name; + + if (slotName) { + if (flow.length > 0) flow += '\n'; + + flow += `\t// flow -> ${slotName}\n\t`; + } + + flow += `${flowSlotData.code}\n\t`; + + if (node === mainNode && shaderStage !== 'compute') { + flow += '// result\n\n\t'; + + if (shaderStage === 'vertex') { + flow += `varyings.Vertex = ${flowSlotData.result};`; + } else if (shaderStage === 'fragment') { + if (isOutputStruct) { + stageData.returnType = outputNode.nodeType; + + flow += `return ${flowSlotData.result};`; + } else { + let structSnippet = '\t@location(0) color: vec4'; + + const builtins = this.getBuiltins('output'); + + if (builtins) structSnippet += ',\n\t' + builtins; + + stageData.returnType = 'OutputStruct'; + stageData.structs += this._getWGSLStruct('OutputStruct', structSnippet); + stageData.structs += '\nvar output : OutputStruct;\n\n'; + + flow += `output.color = ${flowSlotData.result};\n\n\treturn output;`; + } + } + } + } + + stageData.flow = flow; + } + + if (this.material !== null) { + this.vertexShader = this._getWGSLVertexCode(shadersData.vertex); + this.fragmentShader = this._getWGSLFragmentCode(shadersData.fragment); + } else { + this.computeShader = this._getWGSLComputeCode( + shadersData.compute, + (this.object.workgroupSize || [64]).join(', '), + ); + } + } + + getMethod(method, output = null) { + let wgslMethod; + + if (output !== null) { + wgslMethod = this._getWGSLMethod(method + '_' + output); + } + + if (wgslMethod === undefined) { + wgslMethod = this._getWGSLMethod(method); + } + + return wgslMethod || method; + } + + getType(type) { + return wgslTypeLib[type] || type; + } + + isAvailable(name) { + let result = supports[name]; + + if (result === undefined) { + if (name === 'float32Filterable') { + result = this.renderer.hasFeature('float32-filterable'); + } + + supports[name] = result; + } + + return result; + } + + _getWGSLMethod(method) { + if (wgslPolyfill[method] !== undefined) { + this._include(method); + } + + return wgslMethods[method]; + } + + _include(name) { + const codeNode = wgslPolyfill[name]; + codeNode.build(this); + + if (this.currentFunctionNode !== null) { + this.currentFunctionNode.includes.push(codeNode); + } + + return codeNode; + } + + _getWGSLVertexCode(shaderData) { + return `${this.getSignature()} +// directives +${shaderData.directives} + +// uniforms +${shaderData.uniforms} + +// varyings +${shaderData.varyings} +var varyings : VaryingsStruct; + +// codes +${shaderData.codes} + +@vertex +fn main( ${shaderData.attributes} ) -> VaryingsStruct { + + // vars + ${shaderData.vars} + + // flow + ${shaderData.flow} + + return varyings; + +} +`; + } + + _getWGSLFragmentCode(shaderData) { + return `${this.getSignature()} + +diagnostic( off, derivative_uniformity ); + +// uniforms +${shaderData.uniforms} + +// structs +${shaderData.structs} + +// codes +${shaderData.codes} + +@fragment +fn main( ${shaderData.varyings} ) -> ${shaderData.returnType} { + + // vars + ${shaderData.vars} + + // flow + ${shaderData.flow} + +} +`; + } + + _getWGSLComputeCode(shaderData, workgroupSize) { + return `${this.getSignature()} +// directives +${shaderData.directives} + +// system +var instanceIndex : u32; + +// uniforms +${shaderData.uniforms} + +// codes +${shaderData.codes} + +@compute @workgroup_size( ${workgroupSize} ) +fn main( ${shaderData.attributes} ) { + + // system + instanceIndex = id.x + id.y * numWorkgroups.x * u32(${workgroupSize}) + id.z * numWorkgroups.x * numWorkgroups.y * u32(${workgroupSize}); + + // vars + ${shaderData.vars} + + // flow + ${shaderData.flow} + +} +`; + } + + _getWGSLStruct(name, vars) { + return ` +struct ${name} { +${vars} +};`; + } + + _getWGSLStructBinding(name, vars, access, binding = 0, group = 0) { + const structName = name + 'Struct'; + const structSnippet = this._getWGSLStruct(structName, vars); + + return `${structSnippet} +@binding( ${binding} ) @group( ${group} ) +var<${access}> ${name} : ${structName};`; + } +} + +export default WGSLNodeBuilder; diff --git a/src-testing/src/renderers/webgpu/nodes/WGSLNodeFunction.ts b/src-testing/src/renderers/webgpu/nodes/WGSLNodeFunction.ts new file mode 100644 index 000000000..beab7253d --- /dev/null +++ b/src-testing/src/renderers/webgpu/nodes/WGSLNodeFunction.ts @@ -0,0 +1,140 @@ +import NodeFunction from '../../../nodes/core/NodeFunction.js'; +import NodeFunctionInput from '../../../nodes/core/NodeFunctionInput.js'; + +const declarationRegexp = /^[fn]*\s*([a-z_0-9]+)?\s*\(([\s\S]*?)\)\s*[\-\>]*\s*([a-z_0-9]+(?:<[\s\S]+?>)?)/i; +const propertiesRegexp = /([a-z_0-9]+)\s*:\s*([a-z_0-9]+(?:<[\s\S]+?>)?)/gi; + +const wgslTypeLib = { + f32: 'float', + i32: 'int', + u32: 'uint', + bool: 'bool', + + 'vec2': 'vec2', + 'vec2': 'ivec2', + 'vec2': 'uvec2', + 'vec2': 'bvec2', + + vec2f: 'vec2', + vec2i: 'ivec2', + vec2u: 'uvec2', + vec2b: 'bvec2', + + 'vec3': 'vec3', + 'vec3': 'ivec3', + 'vec3': 'uvec3', + 'vec3': 'bvec3', + + vec3f: 'vec3', + vec3i: 'ivec3', + vec3u: 'uvec3', + vec3b: 'bvec3', + + 'vec4': 'vec4', + 'vec4': 'ivec4', + 'vec4': 'uvec4', + 'vec4': 'bvec4', + + vec4f: 'vec4', + vec4i: 'ivec4', + vec4u: 'uvec4', + vec4b: 'bvec4', + + 'mat2x2': 'mat2', + mat2x2f: 'mat2', + + 'mat3x3': 'mat3', + mat3x3f: 'mat3', + + 'mat4x4': 'mat4', + mat4x4f: 'mat4', + + sampler: 'sampler', + + texture_1d: 'texture', + + texture_2d: 'texture', + texture_2d_array: 'texture', + texture_multisampled_2d: 'cubeTexture', + + texture_depth_2d: 'depthTexture', + + texture_3d: 'texture3D', + + texture_cube: 'cubeTexture', + texture_cube_array: 'cubeTexture', + + texture_storage_1d: 'storageTexture', + texture_storage_2d: 'storageTexture', + texture_storage_2d_array: 'storageTexture', + texture_storage_3d: 'storageTexture', +}; + +const parse = source => { + source = source.trim(); + + const declaration = source.match(declarationRegexp); + + if (declaration !== null && declaration.length === 4) { + const inputsCode = declaration[2]; + const propsMatches = []; + let match = null; + + while ((match = propertiesRegexp.exec(inputsCode)) !== null) { + propsMatches.push({ name: match[1], type: match[2] }); + } + + // Process matches to correctly pair names and types + const inputs = []; + for (let i = 0; i < propsMatches.length; i++) { + const { name, type } = propsMatches[i]; + + let resolvedType = type; + + if (resolvedType.startsWith('texture')) { + resolvedType = type.split('<')[0]; + } + + resolvedType = wgslTypeLib[resolvedType] || resolvedType; + + inputs.push(new NodeFunctionInput(resolvedType, name)); + } + + const blockCode = source.substring(declaration[0].length); + const outputType = declaration[3] || 'void'; + + const name = declaration[1] !== undefined ? declaration[1] : ''; + const type = wgslTypeLib[outputType] || outputType; + + return { + type, + inputs, + name, + inputsCode, + blockCode, + outputType, + }; + } else { + throw new Error('FunctionNode: Function is not a WGSL code.'); + } +}; + +class WGSLNodeFunction extends NodeFunction { + constructor(source) { + const { type, inputs, name, inputsCode, blockCode, outputType } = parse(source); + + super(type, inputs, name); + + this.inputsCode = inputsCode; + this.blockCode = blockCode; + this.outputType = outputType; + } + + getCode(name = this.name) { + const outputType = this.outputType !== 'void' ? '-> ' + this.outputType : ''; + + return `fn ${name} ( ${this.inputsCode.trim()} ) ${outputType}` + this.blockCode; + } +} + +export default WGSLNodeFunction; diff --git a/src-testing/src/renderers/webgpu/nodes/WGSLNodeParser.ts b/src-testing/src/renderers/webgpu/nodes/WGSLNodeParser.ts new file mode 100644 index 000000000..c32133df4 --- /dev/null +++ b/src-testing/src/renderers/webgpu/nodes/WGSLNodeParser.ts @@ -0,0 +1,10 @@ +import NodeParser from '../../../nodes/core/NodeParser.js'; +import WGSLNodeFunction from './WGSLNodeFunction.js'; + +class WGSLNodeParser extends NodeParser { + parseFunction(source) { + return new WGSLNodeFunction(source); + } +} + +export default WGSLNodeParser; diff --git a/src-testing/src/renderers/webgpu/utils/WebGPUConstants.d.ts b/src-testing/src/renderers/webgpu/utils/WebGPUConstants.d.ts new file mode 100644 index 000000000..baa36f901 --- /dev/null +++ b/src-testing/src/renderers/webgpu/utils/WebGPUConstants.d.ts @@ -0,0 +1,328 @@ +export enum GPUPrimitiveTopology { + PointList = "point-list", + LineList = "line-list", + LineStrip = "line-strip", + TriangleList = "triangle-list", + TriangleStrip = "triangle-strip", +} + +export enum GPUCompareFunction { + Never = "never", + Less = "less", + Equal = "equal", + LessEqual = "less-equal", + Greater = "greater", + NotEqual = "not-equal", + GreaterEqual = "greater-equal", + Always = "always", +} + +export enum GPUStoreOp { + Store = "store", + Discard = "discard", +} + +export enum GPULoadOp { + Load = "load", + Clear = "clear", +} + +export enum GPUFrontFace { + CCW = "ccw", + CW = "cw", +} + +export enum GPUCullMode { + None = "none", + Front = "front", + Back = "back", +} + +export enum GPUIndexFormat { + Uint16 = "uint16", + Uint32 = "uint32", +} + +export enum GPUVertexFormat { + Uint8x2 = "uint8x2", + Uint8x4 = "uint8x4", + Sint8x2 = "sint8x2", + Sint8x4 = "sint8x4", + Unorm8x2 = "unorm8x2", + Unorm8x4 = "unorm8x4", + Snorm8x2 = "snorm8x2", + Snorm8x4 = "snorm8x4", + Uint16x2 = "uint16x2", + Uint16x4 = "uint16x4", + Sint16x2 = "sint16x2", + Sint16x4 = "sint16x4", + Unorm16x2 = "unorm16x2", + Unorm16x4 = "unorm16x4", + Snorm16x2 = "snorm16x2", + Snorm16x4 = "snorm16x4", + Float16x2 = "float16x2", + Float16x4 = "float16x4", + Float32 = "float32", + Float32x2 = "float32x2", + Float32x3 = "float32x3", + Float32x4 = "float32x4", + Uint32 = "uint32", + Uint32x2 = "uint32x2", + Uint32x3 = "uint32x3", + Uint32x4 = "uint32x4", + Sint32 = "sint32", + Sint32x2 = "sint32x2", + Sint32x3 = "sint32x3", + Sint32x4 = "sint32x4", +} + +export enum GPUTextureFormat { + // 8-bit formats + + R8Unorm = "r8unorm", + R8Snorm = "r8snorm", + R8Uint = "r8uint", + R8Sint = "r8sint", + + // 16-bit formats + + R16Uint = "r16uint", + R16Sint = "r16sint", + R16Float = "r16float", + RG8Unorm = "rg8unorm", + RG8Snorm = "rg8snorm", + RG8Uint = "rg8uint", + RG8Sint = "rg8sint", + + // 32-bit formats + + R32Uint = "r32uint", + R32Sint = "r32sint", + R32Float = "r32float", + RG16Uint = "rg16uint", + RG16Sint = "rg16sint", + RG16Float = "rg16float", + RGBA8Unorm = "rgba8unorm", + RGBA8UnormSRGB = "rgba8unorm-srgb", + RGBA8Snorm = "rgba8snorm", + RGBA8Uint = "rgba8uint", + RGBA8Sint = "rgba8sint", + BGRA8Unorm = "bgra8unorm", + BGRA8UnormSRGB = "bgra8unorm-srgb", + // Packed 32-bit formats + RGB9E5UFloat = "rgb9e5ufloat", + RGB10A2Unorm = "rgb10a2unorm", + RG11B10uFloat = "rgb10a2unorm", + + // 64-bit formats + + RG32Uint = "rg32uint", + RG32Sint = "rg32sint", + RG32Float = "rg32float", + RGBA16Uint = "rgba16uint", + RGBA16Sint = "rgba16sint", + RGBA16Float = "rgba16float", + + // 128-bit formats + + RGBA32Uint = "rgba32uint", + RGBA32Sint = "rgba32sint", + RGBA32Float = "rgba32float", + + // Depth and stencil formats + + Stencil8 = "stencil8", + Depth16Unorm = "depth16unorm", + Depth24Plus = "depth24plus", + Depth24PlusStencil8 = "depth24plus-stencil8", + Depth32Float = "depth32float", + + // 'depth32float-stencil8' extension + + Depth32FloatStencil8 = "depth32float-stencil8", + + // BC compressed formats usable if 'texture-compression-bc' is both + // supported by the device/user agent and enabled in requestDevice. + + BC1RGBAUnorm = "bc1-rgba-unorm", + BC1RGBAUnormSRGB = "bc1-rgba-unorm-srgb", + BC2RGBAUnorm = "bc2-rgba-unorm", + BC2RGBAUnormSRGB = "bc2-rgba-unorm-srgb", + BC3RGBAUnorm = "bc3-rgba-unorm", + BC3RGBAUnormSRGB = "bc3-rgba-unorm-srgb", + BC4RUnorm = "bc4-r-unorm", + BC4RSnorm = "bc4-r-snorm", + BC5RGUnorm = "bc5-rg-unorm", + BC5RGSnorm = "bc5-rg-snorm", + BC6HRGBUFloat = "bc6h-rgb-ufloat", + BC6HRGBFloat = "bc6h-rgb-float", + BC7RGBAUnorm = "bc7-rgba-unorm", + BC7RGBAUnormSRGB = "bc7-rgba-srgb", + + // ETC2 compressed formats usable if 'texture-compression-etc2' is both + // supported by the device/user agent and enabled in requestDevice. + + ETC2RGB8Unorm = "etc2-rgb8unorm", + ETC2RGB8UnormSRGB = "etc2-rgb8unorm-srgb", + ETC2RGB8A1Unorm = "etc2-rgb8a1unorm", + ETC2RGB8A1UnormSRGB = "etc2-rgb8a1unorm-srgb", + ETC2RGBA8Unorm = "etc2-rgba8unorm", + ETC2RGBA8UnormSRGB = "etc2-rgba8unorm-srgb", + EACR11Unorm = "eac-r11unorm", + EACR11Snorm = "eac-r11snorm", + EACRG11Unorm = "eac-rg11unorm", + EACRG11Snorm = "eac-rg11snorm", + + // ASTC compressed formats usable if 'texture-compression-astc' is both + // supported by the device/user agent and enabled in requestDevice. + + ASTC4x4Unorm = "astc-4x4-unorm", + ASTC4x4UnormSRGB = "astc-4x4-unorm-srgb", + ASTC5x4Unorm = "astc-5x4-unorm", + ASTC5x4UnormSRGB = "astc-5x4-unorm-srgb", + ASTC5x5Unorm = "astc-5x5-unorm", + ASTC5x5UnormSRGB = "astc-5x5-unorm-srgb", + ASTC6x5Unorm = "astc-6x5-unorm", + ASTC6x5UnormSRGB = "astc-6x5-unorm-srgb", + ASTC6x6Unorm = "astc-6x6-unorm", + ASTC6x6UnormSRGB = "astc-6x6-unorm-srgb", + ASTC8x5Unorm = "astc-8x5-unorm", + ASTC8x5UnormSRGB = "astc-8x5-unorm-srgb", + ASTC8x6Unorm = "astc-8x6-unorm", + ASTC8x6UnormSRGB = "astc-8x6-unorm-srgb", + ASTC8x8Unorm = "astc-8x8-unorm", + ASTC8x8UnormSRGB = "astc-8x8-unorm-srgb", + ASTC10x5Unorm = "astc-10x5-unorm", + ASTC10x5UnormSRGB = "astc-10x5-unorm-srgb", + ASTC10x6Unorm = "astc-10x6-unorm", + ASTC10x6UnormSRGB = "astc-10x6-unorm-srgb", + ASTC10x8Unorm = "astc-10x8-unorm", + ASTC10x8UnormSRGB = "astc-10x8-unorm-srgb", + ASTC10x10Unorm = "astc-10x10-unorm", + ASTC10x10UnormSRGB = "astc-10x10-unorm-srgb", + ASTC12x10Unorm = "astc-12x10-unorm", + ASTC12x10UnormSRGB = "astc-12x10-unorm-srgb", + ASTC12x12Unorm = "astc-12x12-unorm", + ASTC12x12UnormSRGB = "astc-12x12-unorm-srgb", +} + +export enum GPUAddressMode { + ClampToEdge = "clamp-to-edge", + Repeat = "repeat", + MirrorRepeat = "mirror-repeat", +} + +export enum GPUFilterMode { + Linear = "linear", + Nearest = "nearest", +} + +export enum GPUBlendFactor { + Zero = "zero", + One = "one", + Src = "src", + OneMinusSrc = "one-minus-src", + SrcAlpha = "src-alpha", + OneMinusSrcAlpha = "one-minus-src-alpha", + Dst = "dst", + OneMinusDstColor = "one-minus-dst", + DstAlpha = "dst-alpha", + OneMinusDstAlpha = "one-minus-dst-alpha", + SrcAlphaSaturated = "src-alpha-saturated", + Constant = "constant", + OneMinusConstant = "one-minus-constant", +} + +export enum GPUBlendOperation { + Add = "add", + Subtract = "subtract", + ReverseSubtract = "reverse-subtract", + Min = "min", + Max = "max", +} + +export enum GPUColorWriteFlags { + None = 0, + Red = 0x1, + Green = 0x2, + Blue = 0x4, + Alpha = 0x8, + All = 0xF, +} + +export enum GPUStencilOperation { + Keep = "keep", + Zero = "zero", + Replace = "replace", + Invert = "invert", + IncrementClamp = "increment-clamp", + DecrementClamp = "decrement-clamp", + IncrementWrap = "increment-wrap", + DecrementWrap = "decrement-wrap", +} + +export enum GPUBufferBindingType { + Uniform = "uniform", + Storage = "storage", + ReadOnlyStorage = "read-only-storage", +} + +export enum GPUStorageTextureAccess { + WriteOnly = "write-only", + ReadOnly = "read-only", + ReadWrite = "read-write", +} + +export enum GPUSamplerBindingType { + Filtering = "filtering", + NonFiltering = "non-filtering", + Comparison = "comparison", +} + +export enum GPUTextureSampleType { + Float = "float", + UnfilterableFloat = "unfilterable-float", + Depth = "depth", + SInt = "sint", + UInt = "uint", +} + +export enum GPUTextureDimension { + OneD = "1d", + TwoD = "2d", + ThreeD = "3d", +} + +export enum GPUTextureViewDimension { + OneD = "1d", + TwoD = "2d", + TwoDArray = "2d-array", + Cube = "cube", + CubeArray = "cube-array", + ThreeD = "3d", +} + +export enum GPUTextureAspect { + All = "all", + StencilOnly = "stencil-only", + DepthOnly = "depth-only", +} + +export enum GPUInputStepMode { + Vertex = "vertex", + Instance = "instance", +} + +export enum GPUFeatureName { + DepthClipControl = "depth-clip-control", + Depth32FloatStencil8 = "depth32float-stencil8", + TextureCompressionBC = "texture-compression-bc", + TextureCompressionETC2 = "texture-compression-etc2", + TextureCompressionASTC = "texture-compression-astc", + TimestampQuery = "timestamp-query", + IndirectFirstInstance = "indirect-first-instance", + ShaderF16 = "shader-f16", + RG11B10UFloat = "rg11b10ufloat-renderable", + BGRA8UNormStorage = "bgra8unorm-storage", + Float32Filterable = "float32-filterable", +} diff --git a/src-testing/src/renderers/webxr/WebXRController.d.ts b/src-testing/src/renderers/webxr/WebXRController.d.ts new file mode 100644 index 000000000..956a036b4 --- /dev/null +++ b/src-testing/src/renderers/webxr/WebXRController.d.ts @@ -0,0 +1,63 @@ +import { Object3DEventMap } from "../../core/Object3D.js"; +import { Vector3 } from "../../math/Vector3.js"; +import { Group } from "../../objects/Group.js"; + +export type XRControllerEventType = XRSessionEventType | XRInputSourceEventType | "disconnected" | "connected"; + +export class XRJointSpace extends Group { + readonly jointRadius: number | undefined; +} + +export type XRHandJoints = Record; + +export interface XRHandInputState { + pinching: boolean; +} + +export interface WebXRSpaceEventMap extends Object3DEventMap { + select: { data: XRInputSource }; + selectstart: { data: XRInputSource }; + selectend: { data: XRInputSource }; + squeeze: { data: XRInputSource }; + squeezestart: { data: XRInputSource }; + squeezeend: { data: XRInputSource }; + + connected: { data: XRInputSource }; + disconnected: { data: XRInputSource }; + + pinchend: { handedness: XRHandedness; target: WebXRController }; // This Event break the THREE.EventDispatcher contract, replacing the target to the wrong instance. + pinchstart: { handedness: XRHandedness; target: WebXRController }; // This Event break the THREE.EventDispatcher contract, replacing the target to the wrong instance. + + move: {}; +} + +export class XRHandSpace extends Group { + readonly joints: Partial; + readonly inputState: XRHandInputState; +} + +export class XRTargetRaySpace extends Group { + hasLinearVelocity: boolean; + readonly linearVelocity: Vector3; + hasAngularVelocity: boolean; + readonly angularVelocity: Vector3; +} + +export class XRGripSpace extends Group { + hasLinearVelocity: boolean; + readonly linearVelocity: Vector3; + hasAngularVelocity: boolean; + readonly angularVelocity: Vector3; +} + +export class WebXRController { + constructor(); + + getHandSpace(): XRHandSpace; + getTargetRaySpace(): XRTargetRaySpace; + getGripSpace(): XRGripSpace; + dispatchEvent(event: { type: XRControllerEventType; data?: XRInputSource }): this; + connect(inputSource: XRInputSource): this; + disconnect(inputSource: XRInputSource): this; + update(inputSource: XRInputSource, frame: XRFrame, referenceSpace: XRReferenceSpace): this; +} diff --git a/src-testing/src/renderers/webxr/WebXRDepthSensing.d.ts b/src-testing/src/renderers/webxr/WebXRDepthSensing.d.ts new file mode 100644 index 000000000..23914f679 --- /dev/null +++ b/src-testing/src/renderers/webxr/WebXRDepthSensing.d.ts @@ -0,0 +1,22 @@ +import { Mesh } from "../../objects/Mesh.js"; +import { Texture } from "../../textures/Texture.js"; +import { WebGLRenderer } from "../WebGLRenderer.js"; +import { WebXRArrayCamera } from "./WebXRManager.js"; + +export class WebXRDepthSensing { + texture: Texture | null; + mesh: Mesh | null; + + depthNear: number; + depthFar: number; + + constructor(); + + init(renderer: WebGLRenderer, depthData: XRWebGLDepthInformation, renderState: XRRenderState): void; + + getMesh(cameraXR: WebXRArrayCamera): Mesh | null; + + reset(): void; + + getDepthTexture(): Texture | null; +} diff --git a/src-testing/src/renderers/webxr/WebXRManager.d.ts b/src-testing/src/renderers/webxr/WebXRManager.d.ts new file mode 100644 index 000000000..4b2073101 --- /dev/null +++ b/src-testing/src/renderers/webxr/WebXRManager.d.ts @@ -0,0 +1,85 @@ +/// + +import { ArrayCamera } from "../../cameras/ArrayCamera.js"; +import { PerspectiveCamera } from "../../cameras/PerspectiveCamera.js"; +import { EventDispatcher } from "../../core/EventDispatcher.js"; +import { Vector4 } from "../../math/Vector4.js"; +import { Mesh } from "../../objects/Mesh.js"; +import { Texture } from "../../textures/Texture.js"; +import { WebGLRenderer } from "../WebGLRenderer.js"; +import { XRGripSpace, XRHandSpace, XRTargetRaySpace } from "./WebXRController.js"; + +export type WebXRCamera = PerspectiveCamera & { viewport: Vector4 }; +export type WebXRArrayCamera = Omit & { cameras: [WebXRCamera, WebXRCamera] }; + +export interface WebXRManagerEventMap { + sessionstart: {}; + sessionend: {}; + planeadded: { data: XRPlane }; + planeremoved: { data: XRPlane }; + planechanged: { data: XRPlane }; + planesdetected: { data: XRPlaneSet }; +} + +export class WebXRManager extends EventDispatcher { + /** + * @default true + */ + cameraAutoUpdate: boolean; + + /** + * @default false + */ + enabled: boolean; + + /** + * @default false + */ + isPresenting: boolean; + + constructor(renderer: WebGLRenderer, gl: WebGLRenderingContext); + + getController: (index: number) => XRTargetRaySpace; + + getControllerGrip: (index: number) => XRGripSpace; + + getHand: (index: number) => XRHandSpace; + + setFramebufferScaleFactor: (value: number) => void; + + setReferenceSpaceType: (value: XRReferenceSpaceType) => void; + + getReferenceSpace: () => XRReferenceSpace | null; + + setReferenceSpace: (value: XRReferenceSpace) => void; + + getBaseLayer: () => XRWebGLLayer | XRProjectionLayer; + + getBinding: () => XRWebGLBinding; + + getFrame: () => XRFrame; + + getSession: () => XRSession | null; + + setSession: (value: XRSession | null) => Promise; + + getEnvironmentBlendMode: () => XREnvironmentBlendMode | undefined; + + getDepthTexture: () => Texture | null; + + updateCamera: (camera: PerspectiveCamera) => void; + + getCamera: () => WebXRArrayCamera; + + getFoveation: () => number | undefined; + + setFoveation: (value: number) => void; + + hasDepthSensing: () => boolean; + + getDepthSensingMesh: () => Mesh | null; + + setAnimationLoop: (callback: XRFrameRequestCallback | null) => void; + + dispose: () => void; +} diff --git a/src-testing/src/scenes/Fog.d.ts b/src-testing/src/scenes/Fog.d.ts new file mode 100644 index 000000000..fc0f18019 --- /dev/null +++ b/src-testing/src/scenes/Fog.d.ts @@ -0,0 +1,77 @@ +import { Color, ColorRepresentation } from "../math/Color.js"; + +export interface FogJSON { + type: string; + name: string; + color: number; + near: number; + far: number; +} + +/** + * This class contains the parameters that define linear fog, i.e., that grows linearly denser with the distance. + * @example + * ```typescript + * const scene = new THREE.Scene(); + * scene.fog = new THREE.Fog(0xcccccc, 10, 15); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/scenes/Fog | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/scenes/Fog.js | Source} + */ +export class Fog { + /** + * The color parameter is passed to the {@link THREE.Color | Color} constructor to set the color property + * @remarks + * Color can be a hexadecimal integer or a CSS-style string. + * @param color + * @param near Expects a `Float` + * @param far Expects a `Float` + */ + constructor(color: ColorRepresentation, near?: number, far?: number); + + /** + * Read-only flag to check if a given object is of type {@link Fog}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isFog: true; + + /** + * Optional name of the object + * @remarks _(doesn't need to be unique)_. + * @defaultValue `""` + */ + name: string; + + /** + * Fog color. + * @remarks If set to black, far away objects will be rendered black. + */ + color: Color; + + /** + * The minimum distance to start applying fog. + * @remarks Objects that are less than **near** units from the active camera won't be affected by fog. + * @defaultValue `1` + * @remarks Expects a `Float` + */ + near: number; + + /** + * The maximum distance at which fog stops being calculated and applied. + * @remarks Objects that are more than **far** units away from the active camera won't be affected by fog. + * @defaultValue `1000` + * @remarks Expects a `Float` + */ + far: number; + + /** + * Returns a new {@link Fog} instance with the same parameters as this one. + */ + clone(): Fog; + + /** + * Return {@link Fog} data in JSON format. + */ + toJSON(): FogJSON; +} diff --git a/src-testing/src/scenes/FogExp2.d.ts b/src-testing/src/scenes/FogExp2.d.ts new file mode 100644 index 000000000..af00981e6 --- /dev/null +++ b/src-testing/src/scenes/FogExp2.d.ts @@ -0,0 +1,66 @@ +import { Color, ColorRepresentation } from "../math/Color.js"; + +export interface FogExp2JSON { + type: string; + name: string; + color: number; + density: number; +} + +/** + * This class contains the parameters that define exponential squared fog, which gives a clear view near the camera and a faster than exponentially densening fog farther from the camera. + * @example + * ```typescript + * const scene = new THREE.Scene(); + * scene.fog = new THREE.FogExp2(0xcccccc, 0.002); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_geometry_terrain | webgl geometry terrain} + * @see {@link https://threejs.org/docs/index.html#api/en/scenes/FogExp2 | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/scenes/FogExp2.js | Source} + */ +export class FogExp2 { + /** + * The color parameter is passed to the {@link THREE.Color | Color} constructor to set the color property + * @remarks Color can be a hexadecimal integer or a CSS-style string. + * @param color + * @param density Expects a `Float` + */ + constructor(color: ColorRepresentation, density?: number); + + /** + * Read-only flag to check if a given object is of type {@link FogExp2}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isFogExp2: true; + + /** + * Optional name of the object + * @remarks _(doesn't need to be unique)_. + * @defaultValue `""` + */ + name: string; + + /** + * Fog color. + * @remarks If set to black, far away objects will be rendered black. + */ + color: Color; + + /** + * Defines how fast the fog will grow dense. + * @defaultValue `0.00025` + * @remarks Expects a `Float` + */ + density: number; + + /** + * Returns a new {@link FogExp2} instance with the same parameters as this one. + */ + clone(): FogExp2; + + /** + * Return {@link FogExp2} data in JSON format. + */ + toJSON(): FogExp2JSON; +} diff --git a/src-testing/src/scenes/Scene.d.ts b/src-testing/src/scenes/Scene.d.ts new file mode 100644 index 000000000..c2f43afd7 --- /dev/null +++ b/src-testing/src/scenes/Scene.d.ts @@ -0,0 +1,118 @@ +import { JSONMeta, Object3D, Object3DJSON, Object3DJSONObject } from "../core/Object3D.js"; +import { Material } from "../materials/Material.js"; +import { Color } from "../math/Color.js"; +import { Euler, EulerTuple } from "../math/Euler.js"; +import { CubeTexture } from "../textures/CubeTexture.js"; +import { Texture } from "../textures/Texture.js"; +import { Fog, FogJSON } from "./Fog.js"; +import { FogExp2, FogExp2JSON } from "./FogExp2.js"; + +export interface SceneJSONObject extends Object3DJSONObject { + fog?: FogJSON | FogExp2JSON; + + backgroundBlurriness?: number; + backgroundIntensity?: number; + backgroundRotation: EulerTuple; + + environmentIntensity?: number; + environmentRotation: EulerTuple; +} + +export interface SceneJSON extends Object3DJSON { + object: SceneJSONObject; +} + +/** + * Scenes allow you to set up what and where is to be rendered by three.js + * @remarks + * This is where you place objects, lights and cameras. + * @see Example: {@link https://threejs.org/examples/#webgl_multiple_scenes_comparison | webgl multiple scenes comparison} + * @see {@link https://threejs.org/docs/index.html#manual/en/introduction/Creating-a-scene | Manual: Creating a scene} + * @see {@link https://threejs.org/docs/index.html#api/en/scenes/Scene | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/scenes/Scene.js | Source} + */ +export class Scene extends Object3D { + /** + * Create a new {@link Scene} object. + */ + constructor(); + + /** + * Read-only flag to check if a given object is of type {@link Scene}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isScene: true; + + /** + * @defaultValue `Scene` + */ + type: "Scene"; + + /** + * A {@link Fog | fog} instance defining the type of fog that affects everything rendered in the scene. + * @defaultValue `null` + */ + fog: Fog | FogExp2 | null; + + /** + * Sets the blurriness of the background. Only influences environment maps assigned to {@link THREE.Scene.background | Scene.background}. + * @defaultValue `0` + * @remarks Expects a `Float` between `0` and `1`. + */ + backgroundBlurriness: number; + + /** + * Attenuates the color of the background. Only applies to background textures. + * @defaultValue `1` + * @remarks Expects a `Float` + */ + backgroundIntensity: number; + + /** + * Forces everything in the {@link Scene} to be rendered with the defined material. + * @defaultValue `null` + */ + overrideMaterial: Material | null; + + /** + * Defines the background of the scene. + * @remarks Valid inputs are: + * - A {@link THREE.Color | Color} for defining a uniform colored background. + * - A {@link THREE.Texture | Texture} for defining a (flat) textured background. + * - Texture cubes ({@link THREE.CubeTexture | CubeTexture}) or equirectangular textures for defining a skybox. + * @defaultValue `null` + */ + background: Color | Texture | CubeTexture | null; + + /** + * The rotation of the background in radians. Only influences environment maps assigned to {@link .background}. + * Default is `(0,0,0)`. + */ + backgroundRotation: Euler; + + /** + * Sets the environment map for all physical materials in the scene. + * However, it's not possible to overwrite an existing texture assigned to {@link THREE.MeshStandardMaterial.envMap | MeshStandardMaterial.envMap}. + * @defaultValue `null` + */ + environment: Texture | null; + + /** + * Attenuates the color of the environment. Only influences environment maps assigned to {@link Scene.environment}. + * @default 1 + */ + environmentIntensity: number; + + /** + * The rotation of the environment map in radians. Only influences physical materials in the scene when + * {@link .environment} is used. Default is `(0,0,0)`. + */ + environmentRotation: Euler; + + /** + * Convert the {@link Scene} to three.js {@link https://github.com/mrdoob/three.js/wiki/JSON-Object-Scene-format-4 | JSON Object/Scene format}. + * @param meta Object containing metadata such as textures or images for the scene. + */ + toJSON(meta?: JSONMeta): SceneJSON; +} diff --git a/src-testing/src/textures/CanvasTexture.d.ts b/src-testing/src/textures/CanvasTexture.d.ts new file mode 100644 index 000000000..6445338fa --- /dev/null +++ b/src-testing/src/textures/CanvasTexture.d.ts @@ -0,0 +1,51 @@ +import { + MagnificationTextureFilter, + Mapping, + MinificationTextureFilter, + PixelFormat, + TextureDataType, + Wrapping, +} from "../constants.js"; +import { OffscreenCanvas, Texture } from "./Texture.js"; + +/** + * Creates a texture from a {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas | canvas element}. + * @remarks + * This is almost the same as the base {@link Texture | Texture} class, + * except that it sets {@link Texture.needsUpdate | needsUpdate} to `true` immediately. + * @see {@link THREE.Texture | Texture} + * @see {@link https://threejs.org/docs/index.html#api/en/textures/CanvasTexture | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/CanvasTexture.js | Source} + */ +export class CanvasTexture extends Texture { + /** + * This creates a new {@link THREE.CanvasTexture | CanvasTexture} object. + * @param canvas The HTML canvas element from which to load the texture. + * @param mapping See {@link Texture.mapping | .mapping}. Default {@link THREE.Texture.DEFAULT_MAPPING} + * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping} + * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping} + * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.LinearFilter} + * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.LinearMipmapLinearFilter} + * @param format See {@link Texture.format | .format}. Default {@link THREE.RGBAFormat} + * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} + * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY} + */ + constructor( + canvas: TexImageSource | OffscreenCanvas, + mapping?: Mapping, + wrapS?: Wrapping, + wrapT?: Wrapping, + magFilter?: MagnificationTextureFilter, + minFilter?: MinificationTextureFilter, + format?: PixelFormat, + type?: TextureDataType, + anisotropy?: number, + ); + + /** + * Read-only flag to check if a given object is of type {@link CanvasTexture}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isCanvasTexture: true; +} diff --git a/src-testing/src/textures/CompressedArrayTexture.d.ts b/src-testing/src/textures/CompressedArrayTexture.d.ts new file mode 100644 index 000000000..e46c3d478 --- /dev/null +++ b/src-testing/src/textures/CompressedArrayTexture.d.ts @@ -0,0 +1,68 @@ +import { CompressedPixelFormat, TextureDataType, Wrapping } from "../constants.js"; +import { CompressedTexture, CompressedTextureMipmap } from "./CompressedTexture.js"; + +/** + * Creates an texture 2D array based on data in compressed form, for example from a + * {@link https://en.wikipedia.org/wiki/DirectDraw_Surface | DDS} file. + * @remarks For use with the {@link THREE.CompressedTextureLoader | CompressedTextureLoader}. + * @see {@link https://threejs.org/docs/index.html#api/en/textures/CompressedArrayTexture | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/CompressedArrayTexture.js | Source} + */ +export class CompressedArrayTexture extends CompressedTexture { + /** + * Read-only flag to check if a given object is of type {@link CompressedArrayTexture}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isCompressedArrayTexture: true; + + /** + * Overridden with a object containing width and height. + * @override + */ + get image(): { width: number; height: number; depth: number }; + set image(value: { width: number; height: number; depth: number }); + + /** + * This defines how the texture is wrapped in the depth direction. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + * @defaultValue {@link THREE.ClampToEdgeWrapping} + */ + wrapR: Wrapping; + + /** + * A set of all layers which need to be updated in the texture. See {@link CompressedArrayTexture.addLayerUpdate}. + */ + layerUpdates: Set; + + /** + * Create a new instance of {@link CompressedArrayTexture} + * @param mipmaps The mipmaps array should contain objects with data, width and height. The mipmaps should be of the + * correct format and type. + * @param width The width of the biggest mipmap. + * @param height The height of the biggest mipmap. + * @param depth The number of layers of the 2D array texture + * @param format The format used in the mipmaps. See {@link THREE.CompressedPixelFormat}. + * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} + */ + constructor( + mipmaps: CompressedTextureMipmap[], + width: number, + height: number, + depth: number, + format: CompressedPixelFormat, + type?: TextureDataType, + ); + + /** + * Describes that a specific layer of the texture needs to be updated. Normally when {@link Texture.needsUpdate} is + * set to true, the entire compressed texture array is sent to the GPU. Marking specific layers will only transmit + * subsets of all mipmaps associated with a specific depth in the array which is often much more performant. + */ + addLayerUpdate(layerIndex: number): void; + + /** + * Resets the layer updates registry. See {@link CompressedArrayTexture.addLayerUpdate}. + */ + clearLayoutUpdates(): void; +} diff --git a/src-testing/src/textures/CompressedCubeTexture.d.ts b/src-testing/src/textures/CompressedCubeTexture.d.ts new file mode 100644 index 000000000..9c72145a0 --- /dev/null +++ b/src-testing/src/textures/CompressedCubeTexture.d.ts @@ -0,0 +1,13 @@ +import { CompressedPixelFormat, TextureDataType } from "../constants.js"; +import { CompressedTexture } from "./CompressedTexture.js"; + +export class CompressedCubeTexture extends CompressedTexture { + readonly isCompressedCubeTexture: true; + readonly isCubeTexture: true; + + constructor( + images: Array<{ width: number; height: number }>, + format?: CompressedPixelFormat, + type?: TextureDataType, + ); +} diff --git a/src-testing/src/textures/CompressedTexture.d.ts b/src-testing/src/textures/CompressedTexture.d.ts new file mode 100644 index 000000000..21f5427c7 --- /dev/null +++ b/src-testing/src/textures/CompressedTexture.d.ts @@ -0,0 +1,95 @@ +import { + ColorSpace, + CompressedPixelFormat, + MagnificationTextureFilter, + Mapping, + MinificationTextureFilter, + TextureDataType, + Wrapping, +} from "../constants.js"; +import { TypedArray } from "../core/BufferAttribute.js"; +import { Texture } from "./Texture.js"; + +export interface CompressedTextureMipmap { + data: TypedArray; + width: number; + height: number; +} + +/** + * Creates a texture based on data in compressed form, for example from a {@link https://en.wikipedia.org/wiki/DirectDraw_Surface | DDS} file. + * @remarks For use with the {@link THREE.CompressedTextureLoader | CompressedTextureLoader}. + * @see {@link https://threejs.org/docs/index.html#api/en/textures/CompressedTexture | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/CompressedTexture.js | Source} + */ +export class CompressedTexture extends Texture { + /** + * This creates a new {@link THREE.CompressedTexture | CompressedTexture} object. + * @param mipmaps The mipmaps array should contain objects with data, width and height. The mipmaps should be of the + * correct format and type. + * @param width The width of the biggest mipmap. + * @param height The height of the biggest mipmap. + * @param format The format used in the mipmaps. See {@link THREE.CompressedPixelFormat}. + * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} + * @param mapping See {@link Texture.mapping | .mapping}. Default {@link THREE.Texture.DEFAULT_MAPPING} + * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping} + * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping} + * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.LinearFilter} + * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.LinearMipmapLinearFilter} + * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY} + * @param colorSpace See {@link Texture.colorSpace .colorSpace}. Default {@link NoColorSpace} + */ + constructor( + mipmaps?: CompressedTextureMipmap[], + width?: number, + height?: number, + format?: CompressedPixelFormat, + type?: TextureDataType, + mapping?: Mapping, + wrapS?: Wrapping, + wrapT?: Wrapping, + magFilter?: MagnificationTextureFilter, + minFilter?: MinificationTextureFilter, + anisotropy?: number, + colorSpace?: ColorSpace, + ); + + /** + * Read-only flag to check if a given object is of type {@link CompressedTexture}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isCompressedTexture: true; + + /** + * Overridden with a object containing width and height. + * @override + */ + get image(): { width: number; height: number }; + set image(value: { width: number; height: number }); + + /** + * The mipmaps array should contain objects with data, width and height. The mipmaps should be of the correct + * format and type. + */ + mipmaps: CompressedTextureMipmap[] | undefined; + + /** + * @override + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + * @see {@link THREE.CompressedPixelFormat} + */ + format: CompressedPixelFormat; + + /** + * @override No flipping for cube textures. (also flipping doesn't work for compressed textures) + * @defaultValue `false` + */ + flipY: boolean; + + /** + * @override Can't generate mipmaps for compressed textures. mips must be embedded in DDS files + * @defaultValue `false` + */ + generateMipmaps: boolean; +} diff --git a/src-testing/src/textures/CubeTexture.d.ts b/src-testing/src/textures/CubeTexture.d.ts new file mode 100644 index 000000000..1a4def7dd --- /dev/null +++ b/src-testing/src/textures/CubeTexture.d.ts @@ -0,0 +1,90 @@ +import { + ColorSpace, + CubeTextureMapping, + MagnificationTextureFilter, + MinificationTextureFilter, + PixelFormat, + TextureDataType, + Wrapping, +} from "../constants.js"; +import { Texture } from "./Texture.js"; + +/** + * Creates a cube texture made up of six images. + * @remarks + * {@link CubeTexture} is almost equivalent in functionality and usage to {@link Texture}. + * The only differences are that the images are an array of _6_ images as opposed to a single image, + * and the mapping options are {@link THREE.CubeReflectionMapping} (default) or {@link THREE.CubeRefractionMapping} + * @example + * ```typescript + * const loader = new THREE.CubeTextureLoader(); + * loader.setPath('textures/cube/pisa/'); + * const textureCube = loader.load(['px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png']); + * const material = new THREE.MeshBasicMaterial({ + * color: 0xffffff, + * envMap: textureCube + * }); + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/textures/CubeTexture | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/CubeTexture.js | Source} + */ +export class CubeTexture extends Texture { + /** + * This creates a new {@link THREE.CubeTexture | CubeTexture} object. + * @param images + * @param mapping See {@link CubeTexture.mapping | .mapping}. Default {@link THREE.CubeReflectionMapping} + * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping} + * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping} + * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.LinearFilter} + * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.LinearMipmapLinearFilter} + * @param format See {@link Texture.format | .format}. Default {@link THREE.RGBAFormat} + * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} + * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY} + * @param colorSpace See {@link Texture.colorSpace | .colorSpace}. Default {@link NoColorSpace} + */ + constructor( + images?: any[], // HTMLImageElement or HTMLCanvasElement + mapping?: CubeTextureMapping, + wrapS?: Wrapping, + wrapT?: Wrapping, + magFilter?: MagnificationTextureFilter, + minFilter?: MinificationTextureFilter, + format?: PixelFormat, + type?: TextureDataType, + anisotropy?: number, + colorSpace?: ColorSpace, + ); + + /** + * Read-only flag to check if a given object is of type {@link CubeTexture}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isCubeTexture: true; + + /** + * An image object, typically created using the {@link THREE.CubeTextureLoader.load | CubeTextureLoader.load()} method. + * @see {@link Texture.image} + */ + get image(): any; + set image(data: any); + + /** + * An image object, typically created using the {@link THREE.CubeTextureLoader.load | CubeTextureLoader.load()} method. + * @see {@link Texture.image} + */ + get images(): any; + set images(data: any); + + /** + * @inheritDoc + * @defaultValue {@link THREE.CubeReflectionMapping} + */ + mapping: CubeTextureMapping; + + /** + * @inheritDoc + * @defaultValue `false` + */ + flipY: boolean; +} diff --git a/src-testing/src/textures/Data3DTexture.d.ts b/src-testing/src/textures/Data3DTexture.d.ts new file mode 100644 index 000000000..9e5986eed --- /dev/null +++ b/src-testing/src/textures/Data3DTexture.d.ts @@ -0,0 +1,96 @@ +import { MagnificationTextureFilter, MinificationTextureFilter, Wrapping } from "../constants.js"; +import { Texture } from "./Texture.js"; +import { Texture3DImageData } from "./types.js"; + +/** + * Creates a three-dimensional texture from raw data, with parameters to divide it into width, height, and depth + * @example + * ```typescript + * This creates a[name] with repeating data, 0 to 255 + * // create a buffer with some data + * const sizeX = 64; + * const sizeY = 64; + * const sizeZ = 64; + * const data = new Uint8Array(sizeX * sizeY * sizeZ); + * let i = 0; + * for (let z = 0; z & lt; sizeZ; z++) { + * for (let y = 0; y & lt; sizeY; y++) { + * for (let x = 0; x & lt; sizeX; x++) { + * data[i] = i % 256; + * i++; + * } + * } + * } + * // use the buffer to create the texture + * const texture = new THREE.Data3DTexture(data, sizeX, sizeY, sizeZ); + * texture.needsUpdate = true; + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl2_materials_texture3d | WebGL2 / materials / texture3d} + * @see Example: {@link https://threejs.org/examples/#webgl2_materials_texture3d_partialupdate | WebGL2 / materials / texture3d / partialupdate} + * @see Example: {@link https://threejs.org/examples/#webgl2_volume_cloud | WebGL2 / volume / cloud} + * @see Example: {@link https://threejs.org/examples/#webgl2_volume_perlin | WebGL2 / volume / perlin} + * @see {@link https://threejs.org/docs/index.html#api/en/textures/Data3DTexture | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/Data3DTexture.js | Source} + */ +export class Data3DTexture extends Texture { + /** + * Create a new instance of {@link Data3DTexture} + * @param data {@link https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView | ArrayBufferView} of the texture. Default `null`. + * @param width Width of the texture. Default `1`. + * @param height Height of the texture. Default `1`. + * @param depth Depth of the texture. Default `1`. + */ + constructor(data?: BufferSource | null, width?: number, height?: number, depth?: number); + + /** + * Read-only flag to check if a given object is of type {@link Data3DTexture}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isData3DTexture: true; + + /** + * Overridden with a record type holding data, width and height and depth. + * @override + */ + get image(): Texture3DImageData; + set image(data: Texture3DImageData); + + /** + * @override + * @defaultValue {@link THREE.NearestFilter} + */ + magFilter: MagnificationTextureFilter; + + /** + * @override + * @defaultValue {@link THREE.NearestFilter} + */ + minFilter: MinificationTextureFilter; + + /** + * @override + * @defaultValue {@link THREE.ClampToEdgeWrapping} + */ + wrapR: Wrapping; + + /** + * @override + * @defaultValue `false` + */ + flipY: boolean; + + /** + * @override + * @defaultValue `false` + */ + generateMipmaps: boolean; + + /** + * @override + * @defaultValue `1` + */ + unpackAlignment: number; +} + +export {}; diff --git a/src-testing/src/textures/DataArrayTexture.d.ts b/src-testing/src/textures/DataArrayTexture.d.ts new file mode 100644 index 000000000..d3a82c1e8 --- /dev/null +++ b/src-testing/src/textures/DataArrayTexture.d.ts @@ -0,0 +1,123 @@ +import { MagnificationTextureFilter, MinificationTextureFilter } from "../constants.js"; +import { Texture } from "./Texture.js"; +import { Texture3DImageData } from "./types.js"; + +/** + * Creates an array of textures directly from raw data, width and height and depth + * @example + * ```typescript + * This creates a[name] where each texture has a different color. + * // create a buffer with color data + * const width = 512; + * const height = 512; + * const depth = 100; + * const size = width * height; + * const data = new Uint8Array(4 * size * depth); + * for (let i = 0; i & lt; depth; i++) { + * const color = new THREE.Color(Math.random(), Math.random(), Math.random()); + * const r = Math.floor(color.r * 255); + * const g = Math.floor(color.g * 255); + * const b = Math.floor(color.b * 255); + * for (let j = 0; j & lt; size; j++) { + * const stride = (i * size + j) * 4; + * data[stride] = r; + * data[stride + 1] = g; + * data[stride + 2] = b; + * data[stride + 3] = 255; + * } + * } + * // used the buffer to create a [name] + * const texture = new THREE.DataArrayTexture(data, width, height, depth); + * texture.needsUpdate = true; + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl2_materials_texture2darray | WebGL2 / materials / texture2darray} + * @see Example: {@link https://threejs.org/examples/#webgl2_rendertarget_texture2darray | WebGL2 / rendertarget / texture2darray} + * @see {@link https://threejs.org/docs/index.html#api/en/textures/DataArrayTexture | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/DataArrayTexture.js | Source} + */ +export class DataArrayTexture extends Texture { + /** + * Read-only flag to check if a given object is of type {@link DataArrayTexture}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isDataArrayTexture: true; + + /** + * Overridden with a record type holding data, width and height and depth. + * @override + */ + get image(): Texture3DImageData; + set image(data: Texture3DImageData); + + /** + * @override + * @defaultValue {@link THREE.NearestFilter} + */ + magFilter: MagnificationTextureFilter; + + /** + * @override + * @defaultValue {@link THREE.NearestFilter} + */ + minFilter: MinificationTextureFilter; + + /** + * @override + * @defaultValue {@link THREE.ClampToEdgeWrapping} + */ + wrapR: boolean; + + /** + * @override + * @defaultValue `false` + */ + generateMipmaps: boolean; + + /** + * @override + * @defaultValue `false` + */ + flipY: boolean; + + /** + * @override + * @defaultValue `1` + */ + unpackAlignment: number; + + /** + * A set of all layers which need to be updated in the texture. See {@link DataArrayTexture.addLayerUpdate}. + */ + layerUpdates: Set; + + /** + * This creates a new {@link THREE.DataArrayTexture | DataArrayTexture} object. + * @remarks The interpretation of the data depends on {@link format} and {@link type}. + * @remarks If the {@link type} is {@link THREE.UnsignedByteType}, a {@link Uint8Array} will be useful for addressing the texel data + * @remarks If the {@link format} is {@link THREE.RGBAFormat}, data needs four values for one texel; Red, Green, Blue and Alpha (typically the opacity). + * @remarks For the packed {@link type | types}, {@link THREE.UnsignedShort4444Type} and {@link THREE.UnsignedShort5551Type} + * all color components of one texel can be addressed as bitfields within an integer element of a {@link Uint16Array}. + * @remarks In order to use the {@link type | types} {@link THREE.FloatType} and {@link THREE.HalfFloatType}, + * the WebGL implementation must support the respective extensions _OES_texture_float_ and _OES_texture_half_float_ + * @remarks In order to use {@link THREE.LinearFilter} for component-wise, bilinear interpolation of the texels based on these types, + * the WebGL extensions _OES_texture_float_linear_ or _OES_texture_half_float_linear_ must also be present. + * @param data {@link https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView | ArrayBufferView} of the texture. Default `null`. + * @param width Width of the texture. Default `1`. + * @param height Height of the texture. Default `1`. + * @param depth Depth of the texture. Default `1`. + */ + constructor(data?: BufferSource | null, width?: number, height?: number, depth?: number); + + /** + * Describes that a specific layer of the texture needs to be updated. Normally when {@link Texture.needsUpdate} is + * set to true, the entire compressed texture array is sent to the GPU. Marking specific layers will only transmit + * subsets of all mipmaps associated with a specific depth in the array which is often much more performant. + */ + addLayerUpdate(layerIndex: number): void; + + /** + * Resets the layer updates registry. See {@link DataArrayTexture.addLayerUpdate}. + */ + clearLayoutUpdates(): void; +} diff --git a/src-testing/src/textures/DataTexture.d.ts b/src-testing/src/textures/DataTexture.d.ts new file mode 100644 index 000000000..e679414cf --- /dev/null +++ b/src-testing/src/textures/DataTexture.d.ts @@ -0,0 +1,113 @@ +import { + ColorSpace, + MagnificationTextureFilter, + Mapping, + MinificationTextureFilter, + PixelFormat, + TextureDataType, + Wrapping, +} from "../constants.js"; +import { Texture } from "./Texture.js"; +import { TextureImageData } from "./types.js"; + +/** + * Creates a texture directly from raw data, width and height. + * @example + * ```typescript + * // create a buffer with color data + * const width = 512; + * const height = 512; + * const size = width * height; + * const data = new Uint8Array(4 * size); + * const color = new THREE.Color(0xffffff); + * const r = Math.floor(color.r * 255); + * const g = Math.floor(color.g * 255); + * const b = Math.floor(color.b * 255); + * for (let i = 0; i & lt; size; i++) { + * const stride = i * 4; + * data[stride] = r; + * data[stride + 1] = g; + * data[stride + 2] = b; + * data[stride + 3] = 255; + * } + * // used the buffer to create a [name] + * const texture = new THREE.DataTexture(data, width, height); + * texture.needsUpdate = true; + * ``` + * @see {@link https://threejs.org/docs/index.html#api/en/textures/DataTexture | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/DataTexture.js | Source} + */ +export class DataTexture extends Texture { + /** + * @param data {@link https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView | ArrayBufferView} of the texture. Default `null`. + * @param width Width of the texture. Default `1`. + * @param height Height of the texture. Default `1`. + * @param format See {@link Texture.format | .format}. Default {@link THREE.RGBAFormat} + * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} + * @param mapping See {@link Texture.mapping | .mapping}. Default {@link THREE.Texture.DEFAULT_MAPPING} + * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping} + * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping} + * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.NearestFilter} + * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.NearestFilter} + * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY} + * @param colorSpace See {@link Texture.colorSpace | .colorSpace}. Default {@link NoColorSpace} + */ + constructor( + data?: BufferSource | null, + width?: number, + height?: number, + format?: PixelFormat, + type?: TextureDataType, + mapping?: Mapping, + wrapS?: Wrapping, + wrapT?: Wrapping, + magFilter?: MagnificationTextureFilter, + minFilter?: MinificationTextureFilter, + anisotropy?: number, + colorSpace?: ColorSpace, + ); + + /** + * Read-only flag to check if a given object is of type {@link DataTexture}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isDataTexture: true; + + /** + * Overridden with a record type holding data, width and height and depth. + * @override + */ + get image(): TextureImageData; + set image(value: TextureImageData); + + /** + * @override + * @defaultValue {@link THREE.NearestFilter} + */ + magFilter: MagnificationTextureFilter; + + /** + * @override + * @defaultValue {@link THREE.NearestFilter} + */ + minFilter: MinificationTextureFilter; + + /** + * @override + * @defaultValue `false` + */ + flipY: boolean; + + /** + * @override + * @defaultValue `false` + */ + generateMipmaps: boolean; + + /** + * @override + * @defaultValue `1` + */ + unpackAlignment: number; +} diff --git a/src-testing/src/textures/DepthTexture.d.ts b/src-testing/src/textures/DepthTexture.d.ts new file mode 100644 index 000000000..f524e91cc --- /dev/null +++ b/src-testing/src/textures/DepthTexture.d.ts @@ -0,0 +1,104 @@ +import { + DepthTexturePixelFormat, + MagnificationTextureFilter, + Mapping, + MinificationTextureFilter, + TextureComparisonFunction, + TextureDataType, + Wrapping, +} from "../constants.js"; +import { Texture } from "./Texture.js"; + +/** + * This class can be used to automatically save the depth information of a rendering into a texture + * @see Example: {@link https://threejs.org/examples/#webgl_depth_texture | depth / texture} + * @see {@link https://threejs.org/docs/index.html#api/en/textures/DepthTexture | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/DepthTexture.js | Source} + */ +export class DepthTexture extends Texture { + /** + * Create a new instance of {@link DepthTexture} + * @param width Width of the texture. + * @param height Height of the texture. + * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} or {@link THREE.UnsignedInt248Type} + * @param mapping See {@link Texture.mapping | .mapping}. Default {@link THREE.Texture.DEFAULT_MAPPING} + * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping} + * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping} + * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.NearestFilter} + * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.NearestFilter} + * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY} + * @param format See {@link DepthTexture.format | .format}. Default {@link THREE.DepthFormat} + */ + constructor( + width: number, + height: number, + type?: TextureDataType, + mapping?: Mapping, + wrapS?: Wrapping, + wrapT?: Wrapping, + magFilter?: MagnificationTextureFilter, + minFilter?: MinificationTextureFilter, + anisotropy?: number, + format?: DepthTexturePixelFormat, + ); + + /** + * Read-only flag to check if a given object is of type {@link DepthTexture}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isDepthTexture: true; + + /** + * Overridden with a record type holding width and height. + * @override + */ + get image(): { width: number; height: number }; + set image(value: { width: number; height: number }); + + /** + * @override + * @defaultValue `false` + */ + flipY: boolean; + + /** + * @override + * @defaultValue {@link THREE.NearestFilter} + */ + magFilter: MagnificationTextureFilter; + + /** + * @override + * @defaultValue {@link THREE.NearestFilter} + */ + minFilter: MinificationTextureFilter; + + /** + * @override Depth textures do not use mipmaps. + * @defaultValue `false` + */ + generateMipmaps: boolean; + + /** + * @override + * @see {@link Texture.format | Texture.format} + * @defaultValue {@link THREE.DepthFormat}. + */ + format: DepthTexturePixelFormat; + + /** + * @override + * @defaultValue {@link THREE.UnsignedByteType} when {@link format | .format} === {@link THREE.DepthFormat} + * @defaultValue {@link THREE.UnsignedInt248Type} when {@link format | .format} === {@link THREE.DepthStencilFormat} + */ + type: TextureDataType; + + /** + * This is used to define the comparison function used when comparing texels in the depth texture to the value in + * the depth buffer. Default is `null` which means comparison is disabled. + * + * See {@link THREE.TextureComparisonFunction} for functions. + */ + compareFunction: TextureComparisonFunction | null; +} diff --git a/src-testing/src/textures/FramebufferTexture.d.ts b/src-testing/src/textures/FramebufferTexture.d.ts new file mode 100644 index 000000000..ad54c5175 --- /dev/null +++ b/src-testing/src/textures/FramebufferTexture.d.ts @@ -0,0 +1,62 @@ +import { MagnificationTextureFilter, MinificationTextureFilter } from "../constants.js"; +import { Texture } from "./Texture.js"; + +/** + * This class can only be used in combination with {@link THREE.WebGLRenderer.copyFramebufferToTexture | WebGLRenderer.copyFramebufferToTexture()}. + * @example + * ```typescript + * const pixelRatio = window.devicePixelRatio; + * const textureSize = 128 * pixelRatio; + * + * // instantiate a framebuffer texture + * const frameTexture = new FramebufferTexture( textureSize, textureSize, RGBAFormat ); + * + * // calculate start position for copying part of the frame data + * const vector = new Vector2(); + * vector.x = ( window.innerWidth * pixelRatio / 2 ) - ( textureSize / 2 ); + * vector.y = ( window.innerHeight * pixelRatio / 2 ) - ( textureSize / 2 ); + * + * // render the scene + * renderer.clear(); + * renderer.render( scene, camera ); + * + * // copy part of the rendered frame into the framebuffer texture + * renderer.copyFramebufferToTexture( frameTexture, vector ); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_framebuffer_texture | webgl_framebuffer_texture} + * @see {@link https://threejs.org/docs/index.html#api/en/textures/FramebufferTexture | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/FramebufferTexture.js | Source} + */ +export class FramebufferTexture extends Texture { + /** + * Create a new instance of {@link FramebufferTexture} + * @param width The width of the texture. + * @param height The height of the texture. + */ + constructor(width: number, height: number); + + /** + * Read-only flag to check if a given object is of type {@link FramebufferTexture}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isFramebufferTexture: true; + + /** + * @override + * @defaultValue {@link THREE.NearestFilter} + */ + magFilter: MagnificationTextureFilter; + + /** + * @override + * @defaultValue {@link THREE.NearestFilter} + */ + minFilter: MinificationTextureFilter; + + /** + * @override + * @defaultValue `false` + */ + generateMipmaps: boolean; +} diff --git a/src-testing/src/textures/Source.d.ts b/src-testing/src/textures/Source.d.ts new file mode 100644 index 000000000..404d1d8a1 --- /dev/null +++ b/src-testing/src/textures/Source.d.ts @@ -0,0 +1,75 @@ +export type SerializedImage = + | string + | { + data: number[]; + width: number; + height: number; + type: string; + }; + +export class SourceJSON { + uuid: string; + url: SerializedImage | SerializedImage[]; +} + +/** + * Represents the data {@link Source} of a texture. + * @see {@link https://threejs.org/docs/index.html#api/en/textures/Source | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/Source.js | Source} + */ +export class Source { + /** + * Create a new instance of {@link Source} + * @param data The data definition of a texture. Default `null` + */ + constructor(data: any); + + /** + * Flag to check if a given object is of type {@link Source}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isSource: true; + + readonly id: number; + + /** + * The actual data of a texture. + * @remarks The type of this property depends on the texture that uses this instance. + */ + data: any; + + /** + * This property is only relevant when {@link .needsUpdate} is set to `true` and provides more control on how + * texture data should be processed. + * When `dataReady` is set to `false`, the engine performs the memory allocation (if necessary) but does not + * transfer the data into the GPU memory. + * @default true + */ + dataReady: boolean; + + /** + * When the property is set to `true`, the engine allocates the memory for the texture (if necessary) and triggers + * the actual texture upload to the GPU next time the source is used. + */ + set needsUpdate(value: boolean); + + /** + * {@link http://en.wikipedia.org/wiki/Universally_unique_identifier | UUID} of this object instance. + * @remarks This gets automatically assigned and shouldn't be edited. + */ + uuid: string; + + /** + * This starts at `0` and counts how many times {@link needsUpdate | .needsUpdate} is set to `true`. + * @remarks Expects a `Integer` + * @defaultValue `0` + */ + version: number; + + /** + * Convert the data {@link Source} to three.js {@link https://github.com/mrdoob/three.js/wiki/JSON-Object-Scene-format-4 | JSON Object/Scene format}. + * @param meta Optional object containing metadata. + */ + toJSON(meta?: string | {}): SourceJSON; +} diff --git a/src-testing/src/textures/Texture.d.ts b/src-testing/src/textures/Texture.d.ts new file mode 100644 index 000000000..48a178966 --- /dev/null +++ b/src-testing/src/textures/Texture.d.ts @@ -0,0 +1,477 @@ +import { + AnyMapping, + AnyPixelFormat, + ColorSpace, + MagnificationTextureFilter, + Mapping, + MinificationTextureFilter, + PixelFormat, + PixelFormatGPU, + TextureDataType, + Wrapping, +} from "../constants.js"; +import { EventDispatcher } from "../core/EventDispatcher.js"; +import { Matrix3 } from "../math/Matrix3.js"; +import { Vector2 } from "../math/Vector2.js"; +import { CompressedTextureMipmap } from "./CompressedTexture.js"; +import { CubeTexture } from "./CubeTexture.js"; +import { Source } from "./Source.js"; + +export interface TextureJSON { + metadata: { version: number; type: string; generator: string }; + + uuid: string; + name: string; + + image: string; + + mapping: AnyMapping; + channel: number; + + repeat: [x: number, y: number]; + offset: [x: number, y: number]; + center: [x: number, y: number]; + rotation: number; + + wrap: [wrapS: number, wrapT: number]; + + format: AnyPixelFormat; + internalFormat: PixelFormatGPU | null; + type: TextureDataType; + colorSpace: ColorSpace; + + minFilter: MinificationTextureFilter; + magFilter: MagnificationTextureFilter; + anisotropy: number; + + flipY: boolean; + + generateMipmaps: boolean; + premultiplyAlpha: boolean; + unpackAlignment: number; + + userData?: Record; +} + +/** Shim for OffscreenCanvas. */ +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface OffscreenCanvas extends EventTarget {} + +/** + * Create a {@link Texture} to apply to a surface or as a reflection or refraction map. + * @remarks + * After the initial use of a texture, its **dimensions**, {@link format}, and {@link type} cannot be changed + * Instead, call {@link dispose | .dispose()} on the {@link Texture} and instantiate a new {@link Texture}. + * @example + * ```typescript + * // load a texture, set wrap mode to repeat + * const texture = new THREE.TextureLoader().load("textures/water.jpg"); + * texture.wrapS = THREE.RepeatWrapping; + * texture.wrapT = THREE.RepeatWrapping; + * texture.repeat.set(4, 4); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_materials_texture_filters | webgl materials texture filters} + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + * @see {@link https://threejs.org/docs/index.html#api/en/textures/Texture | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/Textures/Texture.js | Source} + */ +export class Texture extends EventDispatcher<{ dispose: {} }> { + /** + * This creates a new {@link THREE.Texture | Texture} object. + * @param image See {@link Texture.image | .image}. Default {@link THREE.Texture.DEFAULT_IMAGE} + * @param mapping See {@link Texture.mapping | .mapping}. Default {@link THREE.Texture.DEFAULT_MAPPING} + * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping} + * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping} + * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.LinearFilter} + * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.LinearMipmapLinearFilter} + * @param format See {@link Texture.format | .format}. Default {@link THREE.RGBAFormat} + * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} + * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY} + * @param colorSpace See {@link Texture.colorSpace | .colorSpace}. Default {@link THREE.NoColorSpace} + */ + constructor( + image?: TexImageSource | OffscreenCanvas, + mapping?: Mapping, + wrapS?: Wrapping, + wrapT?: Wrapping, + magFilter?: MagnificationTextureFilter, + minFilter?: MinificationTextureFilter, + format?: PixelFormat, + type?: TextureDataType, + anisotropy?: number, + colorSpace?: ColorSpace, + ); + + /** + * @deprecated + */ + constructor( + image: TexImageSource | OffscreenCanvas, + mapping: Mapping, + wrapS: Wrapping, + wrapT: Wrapping, + magFilter: MagnificationTextureFilter, + minFilter: MinificationTextureFilter, + format: PixelFormat, + type: TextureDataType, + anisotropy: number, + ); + + /** + * Read-only flag to check if a given object is of type {@link Texture}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isTexture: true; + + /** + * Unique number for this {@link Texture} instance. + * @remarks Note that ids are assigned in chronological order: 1, 2, 3, ..., incrementing by one for each new object. + * @remarks Expects a `Integer` + */ + readonly id: number; + + /** + * {@link http://en.wikipedia.org/wiki/Universally_unique_identifier | UUID} of this object instance. + * @remarks This gets automatically assigned and shouldn't be edited. + */ + uuid: string; + + /** + * Optional name of the object + * @remarks _(doesn't need to be unique)_. + * @defaultValue `""` + */ + name: string; + + /** + * The data definition of a texture. A reference to the data source can be shared across textures. + * This is often useful in context of spritesheets where multiple textures render the same data + * but with different {@link Texture} transformations. + */ + source: Source; + + /** + * An image object, typically created using the {@link THREE.TextureLoader.load | TextureLoader.load()} method. + * @remarks This can be any image (e.g., PNG, JPG, GIF, DDS) or video (e.g., MP4, OGG/OGV) type supported by three.js. + * @remarks To use video as a {@link Texture} you need to have a playing HTML5 video element as a source + * for your {@link Texture} image and continuously update this {@link Texture} + * as long as video is playing - the {@link THREE.VideoTexture | VideoTexture} class handles this automatically. + */ + get image(): any; + set image(data: any); + + /** + * Array of user-specified mipmaps + * @defaultValue `[]` + */ + mipmaps: CompressedTextureMipmap[] | CubeTexture[] | HTMLCanvasElement[] | undefined; + + /** + * How the image is applied to the object. + * @remarks All {@link Texture} types except {@link THREE.CubeTexture} expect the _values_ be {@link THREE.Mapping} + * @remarks {@link CubeTexture} expect the _values_ be {@link THREE.CubeTextureMapping} + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + * @defaultValue _value of_ {@link THREE.Texture.DEFAULT_MAPPING} + */ + mapping: AnyMapping; + + /** + * Lets you select the uv attribute to map the texture to. `0` for `uv`, `1` for `uv1`, `2` for `uv2` and `3` for + * `uv3`. + */ + channel: number; + + /** + * This defines how the {@link Texture} is wrapped *horizontally* and corresponds to **U** in UV mapping. + * @remarks for **WEBGL1** - tiling of images in textures only functions if image dimensions are powers of two + * (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, ...) in terms of pixels. + * Individual dimensions need not be equal, but each must be a power of two. This is a limitation of WebGL1, not three.js. + * **WEBGL2** does not have this limitation. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + * @see {@link wrapT} + * @see {@link repeat} + * @defaultValue {@link THREE.ClampToEdgeWrapping} + */ + wrapS: Wrapping; + + /** + * This defines how the {@link Texture} is wrapped *vertically* and corresponds to **V** in UV mapping. + * @remarks for **WEBGL1** - tiling of images in textures only functions if image dimensions are powers of two + * (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, ...) in terms of pixels. + * Individual dimensions need not be equal, but each must be a power of two. This is a limitation of WebGL1, not three.js. + * **WEBGL2** does not have this limitation. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + * @see {@link wrapS} + * @see {@link repeat} + * @defaultValue {@link THREE.ClampToEdgeWrapping} + */ + wrapT: Wrapping; + + /** + * How the {@link Texture} is sampled when a texel covers more than one pixel. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + * @see {@link minFilter} + * @see {@link THREE.MagnificationTextureFilter} + * @defaultValue {@link THREE.LinearFilter} + */ + magFilter: MagnificationTextureFilter; + + /** + * How the {@link Texture} is sampled when a texel covers less than one pixel. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + * @see {@link magFilter} + * @see {@link THREE.MinificationTextureFilter} + * @defaultValue {@link THREE.LinearMipmapLinearFilter} + */ + minFilter: MinificationTextureFilter; + + /** + * The number of samples taken along the axis through the pixel that has the highest density of texels. + * @remarks A higher value gives a less blurry result than a basic mipmap, at the cost of more {@link Texture} samples being used. + * @remarks Use {@link THREE.WebGLCapabilities.getMaxAnisotropy() | renderer.capabilities.getMaxAnisotropy()} to find the maximum valid anisotropy value for the GPU; + * @remarks This value is usually a power of 2. + * @default _value of_ {@link THREE.Texture.DEFAULT_ANISOTROPY}. That is normally `1`. + */ + anisotropy: number; + + /** + * These define how elements of a 2D texture, or texels, are read by shaders. + * @remarks All {@link Texture} types except {@link THREE.DepthTexture} and {@link THREE.CompressedPixelFormat} expect the _values_ be {@link THREE.PixelFormat} + * @remarks {@link DepthTexture} expect the _values_ be {@link THREE.CubeTextureMapping} + * @remarks {@link CompressedPixelFormat} expect the _values_ be {@link THREE.CubeTextureMapping} + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + * @see {@link THREE.PixelFormat} + * @defaultValue {@link THREE.RGBAFormat}. + */ + format: AnyPixelFormat; + + /** + * This must correspond to the {@link Texture.format | .format}. + * @remarks {@link THREE.UnsignedByteType}, is the type most used by Texture formats. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + * @see {@link THREE.TextureDataType} + * @defaultValue {@link THREE.UnsignedByteType} + */ + type: TextureDataType; + + /** + * The GPU Pixel Format allows the developer to specify how the data is going to be stored on the GPU. + * @remarks Compatible only with {@link WebGL2RenderingContext | WebGL 2 Rendering Context}. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + * @defaultValue The default value is obtained using a combination of {@link Texture.format | .format} and {@link Texture.type | .type}. + */ + internalFormat: PixelFormatGPU | null; + + /** + * The uv-transform matrix for the texture. + * @remarks + * When {@link Texture.matrixAutoUpdate | .matrixAutoUpdate} property is `true`. + * Will be updated by the renderer from the properties: + * - {@link Texture.offset | .offset} + * - {@link Texture.repeat | .repeat} + * - {@link Texture.rotation | .rotation} + * - {@link Texture.center | .center} + * @remarks + * When {@link Texture.matrixAutoUpdate | .matrixAutoUpdate} property is `false`. + * This matrix may be set manually. + * @see {@link matrixAutoUpdate | .matrixAutoUpdate} + * @defaultValue `new THREE.Matrix3()` + */ + matrix: Matrix3; + + /** + * Whether is to update the texture's uv-transform {@link matrix | .matrix}. + * @remarks Set this to `false` if you are specifying the uv-transform {@link matrix} directly. + * @see {@link matrix | .matrix} + * @defaultValue `true` + */ + matrixAutoUpdate: boolean; + + /** + * How much a single repetition of the texture is offset from the beginning, in each direction **U** and **V**. + * @remarks Typical range is `0.0` to `1.0`. + * @defaultValue `new THREE.Vector2(0, 0)` + */ + offset: Vector2; + + /** + * How many times the texture is repeated across the surface, in each direction **U** and **V**. + * @remarks + * If repeat is set greater than `1` in either direction, the corresponding *Wrap* parameter should + * also be set to {@link THREE.RepeatWrapping} or {@link THREE.MirroredRepeatWrapping} to achieve the desired tiling effect. + * @see {@link wrapS} + * @see {@link wrapT} + * @defaultValue `new THREE.Vector2( 1, 1 )` + */ + repeat: Vector2; + + /** + * The point around which rotation occurs. + * @remarks A value of `(0.5, 0.5)` corresponds to the center of the texture. + * @defaultValue `new THREE.Vector2( 0, 0 )`, _lower left._ + */ + center: Vector2; + + /** + * How much the texture is rotated around the center point, in radians. + * @remarks Positive values are counter-clockwise. + * @defaultValue `0` + */ + rotation: number; + + /** + * Whether to generate mipmaps, _(if possible)_ for a texture. + * @remarks Set this to false if you are creating mipmaps manually. + * @defaultValue true + */ + generateMipmaps: boolean; + + /** + * If set to `true`, the alpha channel, if present, is multiplied into the color channels when the texture is uploaded to the GPU. + * @remarks + * Note that this property has no effect for {@link https://developer.mozilla.org/en-US/docs/Web/API/ImageBitmap | ImageBitmap}. + * You need to configure on bitmap creation instead. See {@link THREE.ImageBitmapLoader | ImageBitmapLoader}. + * @see {@link THREE.ImageBitmapLoader | ImageBitmapLoader}. + * @defaultValue `false` + */ + premultiplyAlpha: boolean; + + /** + * If set to `true`, the texture is flipped along the vertical axis when uploaded to the GPU. + * @remarks + * Note that this property has no effect for {@link https://developer.mozilla.org/en-US/docs/Web/API/ImageBitmap | ImageBitmap}. + * You need to configure on bitmap creation instead. See {@link THREE.ImageBitmapLoader | ImageBitmapLoader}. + * @see {@link THREE.ImageBitmapLoader | ImageBitmapLoader}. + * @defaultValue `true` + */ + flipY: boolean; + + /** + * Specifies the alignment requirements for the start of each pixel row in memory. + * @remarks + * The allowable values are: + * - `1` (byte-alignment) + * - `2` (rows aligned to even-numbered bytes) + * - `4` (word-alignment) + * - `8` (rows start on double-word boundaries). + * @see {@link http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml | glPixelStorei} for more information. + * @defaultValue `4` + */ + unpackAlignment: number; // TODO Fix typing to only allow the expected values. + + /** + * The {@link Textures | {@link Texture} constants} page for details of other color spaces. + * @remarks + * Textures containing color data should be annotated with {@link SRGBColorSpace THREE.SRGBColorSpace} or + * {@link LinearSRGBColorSpace THREE.LinearSRGBColorSpace}. + * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} + * @see {@link THREE.TextureDataType} + * @defaultValue {@link THREE.NoColorSpace} + */ + colorSpace: ColorSpace; + + /** + * Indicates whether a texture belongs to a render target or not + * @defaultValue `false` + */ + isRenderTargetTexture: boolean; + + /** + * An object that can be used to store custom data about the texture. + * @remarks It should not hold references to functions as these will not be cloned. + * @defaultValue `{}` + */ + userData: Record; + + /** + * This starts at `0` and counts how many times {@link needsUpdate | .needsUpdate} is set to `true`. + * @remarks Expects a `Integer` + * @defaultValue `0` + */ + version: number; + + /** + * Indicates whether this texture should be processed by PMREMGenerator or not (only relevant for render target + * textures) + */ + pmremVersion: number; + + /** + * Set this to `true` to trigger an update next time the texture is used. Particularly important for setting the wrap mode. + */ + set needsUpdate(value: boolean); + + /** + * Indicates whether this texture should be processed by {@link THREE.PMREMGenerator} or not. + * @remarks Only relevant for render target textures. + * @defaultValue `false` + */ + set needsPMREMUpdate(value: boolean); + + /** + * The Global default value for {@link anisotropy | .anisotropy}. + * @defaultValue `1`. + */ + static DEFAULT_ANISOTROPY: number; + + /** + * The Global default value for {@link Texture.image | .image}. + * @defaultValue `null`. + */ + static DEFAULT_IMAGE: any; + + /** + * The Global default value for {@link mapping | .mapping}. + * @defaultValue {@link THREE.UVMapping} + */ + static DEFAULT_MAPPING: Mapping; + + /** + * A callback function, called when the texture is updated _(e.g., when needsUpdate has been set to true and then the texture is used)_. + */ + onUpdate: () => void; + + /** + * Transform the **UV** based on the value of this texture's + * {@link offset | .offset}, + * {@link repeat | .repeat}, + * {@link wrapS | .wrapS}, + * {@link wrapT | .wrapT} and + * {@link flipY | .flipY} properties. + * @param uv + */ + transformUv(uv: Vector2): Vector2; + + /** + * Update the texture's **UV-transform** {@link matrix | .matrix} from the texture properties + * {@link offset | .offset}, + * {@link repeat | .repeat}, + * {@link rotation | .rotation} and + * {@link center | .center}. + */ + updateMatrix(): void; + + /** + * Make copy of the texture + * @remarks Note this is not a **"deep copy"**, the image is shared + * @remarks + * Besides, cloning a texture does not automatically mark it for a texture upload + * You have to set {@link needsUpdate | .needsUpdate} to `true` as soon as it's image property (the data source) is fully loaded or ready. + */ + clone(): this; + + copy(source: Texture): this; + + /** + * Convert the texture to three.js {@link https://github.com/mrdoob/three.js/wiki/JSON-Object-Scene-format-4 | JSON Object/Scene format}. + * @param meta Optional object containing metadata. + */ + toJSON(meta?: string | {}): TextureJSON; + + /** + * Frees the GPU-related resources allocated by this instance + * @remarks Call this method whenever this instance is no longer used in your app. + */ + dispose(): void; +} diff --git a/src-testing/src/textures/VideoTexture.d.ts b/src-testing/src/textures/VideoTexture.d.ts new file mode 100644 index 000000000..31dc5d456 --- /dev/null +++ b/src-testing/src/textures/VideoTexture.d.ts @@ -0,0 +1,90 @@ +import { + MagnificationTextureFilter, + Mapping, + MinificationTextureFilter, + PixelFormat, + TextureDataType, + Wrapping, +} from "../constants.js"; +import { Texture } from "./Texture.js"; + +/** + * Creates a texture for use with a video. + * @remarks + * Note: After the initial use of a texture, the video cannot be changed + * Instead, call {@link dispose | .dispose()} on the texture and instantiate a new one. + * @example + * ```typescript + * // assuming you have created a HTML video element with id="video" + * const video = document.getElementById('video'); + * const texture = new THREE.VideoTexture(video); + * ``` + * @see Example: {@link https://threejs.org/examples/#webgl_materials_video | materials / video} + * @see Example: {@link https://threejs.org/examples/#webgl_materials_video_webcam | materials / video / webcam} + * @see Example: {@link https://threejs.org/examples/#webgl_video_kinect | video / kinect} + * @see Example: {@link https://threejs.org/examples/#webgl_video_panorama_equirectangular | video / panorama / equirectangular} + * @see Example: {@link https://threejs.org/examples/#webxr_vr_video | vr / video} + * @see {@link https://threejs.org/docs/index.html#api/en/textures/VideoTexture | Official Documentation} + * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/VideoTexture.js | Source} + */ +export class VideoTexture extends Texture { + /** + * Create a new instance of {@link VideoTexture} + * @param video The video element to use as the texture. + * @param mapping See {@link Texture.mapping | .mapping}. Default {@link THREE.Texture.DEFAULT_MAPPING} + * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping} + * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping} + * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.LinearFilter} + * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.LinearFilter} + * @param format See {@link Texture.format | .format}. Default {@link THREE.RGBAFormat} + * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} + * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY} + */ + constructor( + video: HTMLVideoElement, + mapping?: Mapping, + wrapS?: Wrapping, + wrapT?: Wrapping, + magFilter?: MagnificationTextureFilter, + minFilter?: MinificationTextureFilter, + format?: PixelFormat, + type?: TextureDataType, + anisotropy?: number, + ); + + /** + * Read-only flag to check if a given object is of type {@link VideoTexture}. + * @remarks This is a _constant_ value + * @defaultValue `true` + */ + readonly isVideoTexture: true; + + /** + * @override + * @defaultValue {@link THREE.LinearFilter} + */ + magFilter: MagnificationTextureFilter; + + /** + * @override + * @defaultValue {@link THREE.LinearFilter} + */ + minFilter: MinificationTextureFilter; + + /** + * @override + * @defaultValue `false` + */ + generateMipmaps: boolean; + + /** + * @override + * You will **not** need to set this manually here as it is handled by the {@link update | update()} method. + */ + set needsUpdate(value: boolean); + + /** + * This is called automatically and sets {@link needsUpdate | .needsUpdate } to `true` every time a new frame is available. + */ + update(): void; +} diff --git a/src-testing/src/textures/types.d.ts b/src-testing/src/textures/types.d.ts new file mode 100644 index 000000000..86b7f2fd2 --- /dev/null +++ b/src-testing/src/textures/types.d.ts @@ -0,0 +1,9 @@ +export interface TextureImageData { + data: Uint8Array | Uint8ClampedArray; + height: number; + width: number; +} + +export interface Texture3DImageData extends TextureImageData { + depth: number; +} diff --git a/src-testing/src/utils.d.ts b/src-testing/src/utils.d.ts new file mode 100644 index 000000000..3fda1cfda --- /dev/null +++ b/src-testing/src/utils.d.ts @@ -0,0 +1,3 @@ +export function createCanvasElement(): HTMLCanvasElement; + +export function probeAsync(gl: WebGLRenderingContext, sync: WebGLSync, interval: number): Promise; From 6e2637870defc52fd7709376f3e46322097a7a01 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Sat, 24 Aug 2024 15:40:44 -0400 Subject: [PATCH 4/7] Update patch and delete src --- src-testing/changes.patch | 45 +- src-testing/src/Three.Legacy.d.ts | 20 - src-testing/src/Three.WebGPU.d.ts | 195 --- src-testing/src/Three.d.ts | 213 --- .../src/animation/AnimationAction.d.ts | 86 - src-testing/src/animation/AnimationClip.d.ts | 59 - src-testing/src/animation/AnimationMixer.d.ts | 39 - .../src/animation/AnimationObjectGroup.d.ts | 17 - src-testing/src/animation/AnimationUtils.d.ts | 60 - src-testing/src/animation/KeyframeTrack.d.ts | 55 - .../src/animation/PropertyBinding.d.ts | 43 - src-testing/src/animation/PropertyMixer.d.ts | 17 - .../tracks/BooleanKeyframeTrack.d.ts | 10 - .../animation/tracks/ColorKeyframeTrack.d.ts | 11 - .../animation/tracks/NumberKeyframeTrack.d.ts | 11 - .../tracks/QuaternionKeyframeTrack.d.ts | 11 - .../animation/tracks/StringKeyframeTrack.d.ts | 10 - .../animation/tracks/VectorKeyframeTrack.d.ts | 11 - src-testing/src/audio/Audio.d.ts | 270 ---- src-testing/src/audio/AudioAnalyser.d.ts | 58 - src-testing/src/audio/AudioContext.d.ts | 19 - src-testing/src/audio/AudioListener.d.ts | 96 -- src-testing/src/audio/PositionalAudio.d.ts | 101 -- src-testing/src/cameras/ArrayCamera.d.ts | 32 - src-testing/src/cameras/Camera.d.ts | 74 - src-testing/src/cameras/CubeCamera.d.ts | 68 - .../src/cameras/OrthographicCamera.d.ts | 174 -- .../src/cameras/PerspectiveCamera.d.ts | 254 --- src-testing/src/cameras/StereoCamera.d.ts | 50 - src-testing/src/constants.d.ts | 926 ----------- src-testing/src/core/BufferAttribute.d.ts | 637 -------- src-testing/src/core/BufferGeometry.d.ts | 422 ----- src-testing/src/core/Clock.d.ts | 72 - src-testing/src/core/EventDispatcher.d.ts | 85 - src-testing/src/core/GLBufferAttribute.d.ts | 121 -- .../src/core/InstancedBufferAttribute.d.ts | 32 - .../src/core/InstancedBufferGeometry.d.ts | 37 - .../src/core/InstancedInterleavedBuffer.d.ts | 22 - src-testing/src/core/InterleavedBuffer.d.ts | 162 -- .../src/core/InterleavedBufferAttribute.d.ts | 201 --- src-testing/src/core/Layers.d.ts | 72 - src-testing/src/core/Object3D.d.ts | 672 -------- src-testing/src/core/Raycaster.d.ts | 207 --- src-testing/src/core/RenderTarget.d.ts | 96 -- src-testing/src/core/Uniform.d.ts | 38 - src-testing/src/core/UniformsGroup.d.ts | 33 - src-testing/src/extras/DataUtils.d.ts | 22 - src-testing/src/extras/Earcut.d.ts | 15 - src-testing/src/extras/ImageUtils.d.ts | 32 - src-testing/src/extras/PMREMGenerator.d.ts | 82 - src-testing/src/extras/ShapeUtils.d.ts | 25 - src-testing/src/extras/TextureUtils.d.ts | 42 - src-testing/src/extras/core/Curve.d.ts | 162 -- src-testing/src/extras/core/CurvePath.d.ts | 77 - .../src/extras/core/Interpolations.d.ts | 36 - src-testing/src/extras/core/Path.d.ts | 166 -- src-testing/src/extras/core/Shape.d.ts | 86 - src-testing/src/extras/core/ShapePath.d.ts | 98 -- src-testing/src/extras/curves/ArcCurve.d.ts | 41 - .../src/extras/curves/CatmullRomCurve3.d.ts | 77 - .../src/extras/curves/CubicBezierCurve.d.ts | 72 - .../src/extras/curves/CubicBezierCurve3.d.ts | 72 - src-testing/src/extras/curves/Curves.d.ts | 10 - .../src/extras/curves/EllipseCurve.d.ts | 115 -- src-testing/src/extras/curves/LineCurve.d.ts | 42 - src-testing/src/extras/curves/LineCurve3.d.ts | 42 - .../extras/curves/QuadraticBezierCurve.d.ts | 64 - .../extras/curves/QuadraticBezierCurve3.d.ts | 64 - .../src/extras/curves/SplineCurve.d.ts | 52 - src-testing/src/geometries/BoxGeometry.d.ts | 59 - .../src/geometries/CapsuleGeometry.d.ts | 48 - .../src/geometries/CircleGeometry.d.ts | 51 - src-testing/src/geometries/ConeGeometry.d.ts | 64 - .../src/geometries/CylinderGeometry.d.ts | 64 - .../src/geometries/DodecahedronGeometry.d.ts | 25 - src-testing/src/geometries/EdgesGeometry.d.ts | 41 - .../src/geometries/ExtrudeGeometry.d.ts | 152 -- src-testing/src/geometries/Geometries.d.ts | 21 - .../src/geometries/IcosahedronGeometry.d.ts | 26 - src-testing/src/geometries/LatheGeometry.d.ts | 55 - .../src/geometries/OctahedronGeometry.d.ts | 25 - src-testing/src/geometries/PlaneGeometry.d.ts | 48 - .../src/geometries/PolyhedronGeometry.d.ts | 54 - src-testing/src/geometries/RingGeometry.d.ts | 59 - src-testing/src/geometries/ShapeGeometry.d.ts | 53 - .../src/geometries/SphereGeometry.d.ts | 67 - .../src/geometries/TetrahedronGeometry.d.ts | 25 - src-testing/src/geometries/TorusGeometry.d.ts | 49 - .../src/geometries/TorusKnotGeometry.d.ts | 59 - src-testing/src/geometries/TubeGeometry.d.ts | 86 - .../src/geometries/WireframeGeometry.d.ts | 40 - src-testing/src/helpers/ArrowHelper.d.ts | 93 -- src-testing/src/helpers/AxesHelper.d.ts | 50 - src-testing/src/helpers/Box3Helper.d.ts | 44 - src-testing/src/helpers/BoxHelper.d.ts | 64 - src-testing/src/helpers/CameraHelper.d.ts | 80 - .../src/helpers/DirectionalLightHelper.d.ts | 81 - src-testing/src/helpers/GridHelper.d.ts | 47 - .../src/helpers/HemisphereLightHelper.d.ts | 72 - src-testing/src/helpers/PlaneHelper.d.ts | 50 - src-testing/src/helpers/PointLightHelper.d.ts | 73 - src-testing/src/helpers/PolarGridHelper.d.ts | 55 - src-testing/src/helpers/SkeletonHelper.d.ts | 78 - src-testing/src/helpers/SpotLightHelper.d.ts | 77 - src-testing/src/lights/AmbientLight.d.ts | 36 - src-testing/src/lights/DirectionalLight.d.ts | 103 -- .../src/lights/DirectionalLightShadow.d.ts | 73 - src-testing/src/lights/HemisphereLight.d.ts | 61 - src-testing/src/lights/Light.d.ts | 82 - src-testing/src/lights/LightProbe.d.ts | 47 - src-testing/src/lights/LightShadow.d.ts | 169 -- src-testing/src/lights/PointLight.d.ts | 102 -- src-testing/src/lights/PointLightShadow.d.ts | 22 - src-testing/src/lights/RectAreaLight.d.ts | 82 - src-testing/src/lights/SpotLight.d.ts | 164 -- src-testing/src/lights/SpotLightShadow.d.ts | 72 - .../src/lights/webgpu/IESSpotLight.d.ts | 6 - src-testing/src/loaders/AnimationLoader.d.ts | 9 - src-testing/src/loaders/AudioLoader.d.ts | 6 - .../src/loaders/BufferGeometryLoader.d.ts | 10 - src-testing/src/loaders/Cache.d.ts | 21 - .../src/loaders/CompressedTextureLoader.d.ts | 14 - .../src/loaders/CubeTextureLoader.d.ts | 14 - .../src/loaders/DataTextureLoader.d.ts | 14 - src-testing/src/loaders/FileLoader.d.ts | 19 - .../src/loaders/ImageBitmapLoader.d.ts | 22 - src-testing/src/loaders/ImageLoader.d.ts | 17 - src-testing/src/loaders/Loader.d.ts | 50 - src-testing/src/loaders/LoaderUtils.d.ts | 10 - src-testing/src/loaders/LoadingManager.d.ts | 69 - src-testing/src/loaders/MaterialLoader.d.ts | 19 - src-testing/src/loaders/ObjectLoader.d.ts | 35 - src-testing/src/loaders/TextureLoader.d.ts | 18 - .../src/materials/LineBasicMaterial.d.ts | 60 - .../src/materials/LineDashedMaterial.d.ts | 40 - src-testing/src/materials/Material.d.ts | 631 -------- src-testing/src/materials/Materials.d.ts | 18 - .../src/materials/MeshBasicMaterial.d.ts | 139 -- .../src/materials/MeshDepthMaterial.d.ts | 76 - .../src/materials/MeshDistanceMaterial.d.ts | 62 - .../src/materials/MeshLambertMaterial.d.ts | 204 --- .../src/materials/MeshMatcapMaterial.d.ts | 117 -- .../src/materials/MeshNormalMaterial.d.ts | 93 -- .../src/materials/MeshPhongMaterial.d.ts | 228 --- .../src/materials/MeshPhysicalMaterial.d.ts | 236 --- .../src/materials/MeshStandardMaterial.d.ts | 218 --- .../src/materials/MeshToonMaterial.d.ts | 178 --- src-testing/src/materials/PointsMaterial.d.ts | 61 - .../src/materials/RawShaderMaterial.d.ts | 14 - src-testing/src/materials/ShaderMaterial.d.ts | 167 -- src-testing/src/materials/ShadowMaterial.d.ts | 39 - src-testing/src/materials/SpriteMaterial.d.ts | 66 - src-testing/src/math/Box2.d.ts | 48 - src-testing/src/math/Box3.d.ts | 66 - src-testing/src/math/Color.d.ts | 358 ----- src-testing/src/math/ColorManagement.d.ts | 49 - src-testing/src/math/Cylindrical.d.ts | 26 - src-testing/src/math/Euler.d.ts | 50 - src-testing/src/math/Frustum.d.ts | 30 - src-testing/src/math/Interpolant.d.ts | 10 - src-testing/src/math/Line3.d.ts | 29 - src-testing/src/math/MathUtils.d.ts | 137 -- src-testing/src/math/Matrix2.d.ts | 53 - src-testing/src/math/Matrix3.d.ts | 184 --- src-testing/src/math/Matrix4.d.ts | 284 ---- src-testing/src/math/Plane.d.ts | 47 - src-testing/src/math/Quaternion.d.ts | 188 --- src-testing/src/math/Ray.d.ts | 60 - src-testing/src/math/Sphere.d.ts | 47 - src-testing/src/math/Spherical.d.ts | 27 - src-testing/src/math/SphericalHarmonics3.d.ts | 50 - src-testing/src/math/Triangle.d.ts | 86 - src-testing/src/math/Vector2.d.ts | 321 ---- src-testing/src/math/Vector3.d.ts | 301 ---- src-testing/src/math/Vector4.d.ts | 239 --- .../math/interpolants/CubicInterpolant.d.ts | 7 - .../interpolants/DiscreteInterpolant.d.ts | 7 - .../math/interpolants/LinearInterpolant.d.ts | 7 - .../QuaternionLinearInterpolant.d.ts | 7 - src-testing/src/nodes/Nodes.ts | 435 ----- .../src/nodes/accessors/AccessorsUtils.d.ts | 9 - .../src/nodes/accessors/BatchNode.d.ts | 14 - .../src/nodes/accessors/BitangentNode.d.ts | 9 - .../nodes/accessors/BufferAttributeNode.ts | 134 -- .../src/nodes/accessors/BufferNode.d.ts | 17 - .../src/nodes/accessors/CameraNode.d.ts | 15 - .../src/nodes/accessors/ClippingNode.d.ts | 16 - .../src/nodes/accessors/CubeTextureNode.d.ts | 34 - .../src/nodes/accessors/InstanceNode.d.ts | 13 - .../src/nodes/accessors/MaterialNode.d.ts | 130 -- .../nodes/accessors/MaterialProperties.d.ts | 4 - .../accessors/MaterialReferenceNode.d.ts | 15 - .../src/nodes/accessors/ModelNode.d.ts | 20 - .../accessors/ModelViewProjectionNode.d.ts | 8 - .../src/nodes/accessors/NormalNode.d.ts | 12 - .../src/nodes/accessors/Object3DNode.d.ts | 26 - .../src/nodes/accessors/PointUVNode.d.ts | 10 - .../src/nodes/accessors/PositionNode.d.ts | 9 - .../src/nodes/accessors/ReferenceNode.d.ts | 27 - .../nodes/accessors/ReflectVectorNode.d.ts | 9 - .../accessors/RendererReferenceNode.d.ts | 15 - .../src/nodes/accessors/SkinningNode.d.ts | 20 - .../nodes/accessors/StorageBufferNode.d.ts | 38 - .../nodes/accessors/StorageTextureNode.d.ts | 40 - .../src/nodes/accessors/TangentNode.d.ts | 12 - .../src/nodes/accessors/Texture3DNode.d.ts | 17 - .../nodes/accessors/TextureBicubicNode.d.ts | 18 - .../src/nodes/accessors/TextureNode.ts | 369 ----- src-testing/src/nodes/accessors/UVNode.d.ts | 4 - .../src/nodes/accessors/UniformArrayNode.d.ts | 30 - .../src/nodes/accessors/UserDataNode.d.ts | 15 - .../src/nodes/accessors/VertexColorNode.d.ts | 12 - src-testing/src/nodes/code/CodeNode.ts | 66 - .../src/nodes/code/ExpressionNode.d.ts | 9 - .../src/nodes/code/FunctionCallNode.d.ts | 25 - src-testing/src/nodes/code/FunctionNode.ts | 100 -- src-testing/src/nodes/core/AssignNode.d.ts | 18 - src-testing/src/nodes/core/AttributeNode.d.ts | 19 - src-testing/src/nodes/core/BypassNode.d.ts | 18 - src-testing/src/nodes/core/CacheNode.d.ts | 20 - src-testing/src/nodes/core/ConstNode.d.ts | 9 - src-testing/src/nodes/core/ContextNode.ts | 55 - src-testing/src/nodes/core/IndexNode.d.ts | 20 - src-testing/src/nodes/core/InputNode.ts | 65 - src-testing/src/nodes/core/LightingModel.d.ts | 46 - src-testing/src/nodes/core/MRTNode.d.ts | 19 - src-testing/src/nodes/core/Node.ts | 417 ----- src-testing/src/nodes/core/NodeAttribute.ts | 11 - src-testing/src/nodes/core/NodeBuilder.ts | 1128 ------------- src-testing/src/nodes/core/NodeCache.ts | 26 - src-testing/src/nodes/core/NodeCode.ts | 11 - src-testing/src/nodes/core/NodeFrame.ts | 127 -- src-testing/src/nodes/core/NodeFunction.ts | 16 - .../src/nodes/core/NodeFunctionInput.d.ts | 7 - src-testing/src/nodes/core/NodeKeywords.ts | 58 - src-testing/src/nodes/core/NodeParser.ts | 7 - src-testing/src/nodes/core/NodeUniform.ts | 27 - src-testing/src/nodes/core/NodeUtils.ts | 137 -- src-testing/src/nodes/core/NodeVar.ts | 10 - src-testing/src/nodes/core/NodeVarying.ts | 13 - .../src/nodes/core/OutputStructNode.d.ts | 12 - src-testing/src/nodes/core/PropertyNode.d.ts | 43 - src-testing/src/nodes/core/StackNode.ts | 87 - src-testing/src/nodes/core/StructTypeNode.ts | 18 - src-testing/src/nodes/core/TempNode.d.ts | 10 - src-testing/src/nodes/core/UniformGroup.d.ts | 7 - .../src/nodes/core/UniformGroupNode.ts | 46 - src-testing/src/nodes/core/UniformNode.ts | 90 -- src-testing/src/nodes/core/VarNode.d.ts | 20 - src-testing/src/nodes/core/VaryingNode.d.ts | 21 - src-testing/src/nodes/core/constants.ts | 28 - .../src/nodes/display/AfterImageNode.d.ts | 25 - .../src/nodes/display/AnamorphicNode.d.ts | 31 - .../src/nodes/display/BleachBypassNode.d.ts | 10 - .../src/nodes/display/BlendModeNode.d.ts | 47 - src-testing/src/nodes/display/BloomNode.d.ts | 35 - .../nodes/display/ColorAdjustmentNode.d.ts | 47 - .../src/nodes/display/ColorSpaceNode.d.ts | 35 - .../src/nodes/display/DenoiseNode.d.ts | 38 - .../src/nodes/display/DepthOfFieldNode.d.ts | 30 - .../src/nodes/display/DotScreenNode.d.ts | 27 - src-testing/src/nodes/display/FXAANode.d.ts | 19 - src-testing/src/nodes/display/FilmNode.d.ts | 25 - .../src/nodes/display/FrontFacingNode.d.ts | 10 - src-testing/src/nodes/display/GTAONode.d.ts | 46 - .../src/nodes/display/GaussianBlurNode.d.ts | 31 - src-testing/src/nodes/display/Lut3DNode.d.ts | 30 - .../src/nodes/display/NormalMapNode.d.ts | 21 - src-testing/src/nodes/display/PassNode.d.ts | 71 - .../src/nodes/display/PixelationPassNode.d.ts | 67 - .../src/nodes/display/PosterizeNode.d.ts | 20 - .../src/nodes/display/RGBShiftNode.d.ts | 24 - .../src/nodes/display/RenderOutputNode.d.ts | 28 - src-testing/src/nodes/display/SepiaNode.d.ts | 10 - .../src/nodes/display/SobelOperatorNode.d.ts | 17 - .../src/nodes/display/ToneMappingNode.d.ts | 33 - .../src/nodes/display/TransitionNode.d.ts | 41 - .../src/nodes/display/ViewportDepthNode.d.ts | 30 - .../display/ViewportDepthTextureNode.d.ts | 12 - .../src/nodes/display/ViewportNode.d.ts | 31 - .../display/ViewportSharedTextureNode.d.ts | 18 - .../nodes/display/ViewportTextureNode.d.ts | 33 - src-testing/src/nodes/fog/FogExp2Node.d.ts | 18 - src-testing/src/nodes/fog/FogNode.ts | 38 - src-testing/src/nodes/fog/FogRangeNode.d.ts | 23 - .../src/nodes/functions/BSDF/BRDF_GGX.d.ts | 15 - .../nodes/functions/BSDF/BRDF_Lambert.d.ts | 7 - .../src/nodes/functions/BSDF/BRDF_Sheen.d.ts | 7 - .../src/nodes/functions/BSDF/DFGApprox.d.ts | 11 - .../src/nodes/functions/BSDF/D_GGX.d.ts | 10 - .../functions/BSDF/D_GGX_Anisotropic.d.ts | 10 - .../src/nodes/functions/BSDF/F_Schlick.d.ts | 7 - src-testing/src/nodes/functions/BSDF/LTC.d.ts | 9 - .../functions/BSDF/V_GGX_SmithCorrelated.d.ts | 11 - .../V_GGX_SmithCorrelated_Anisotropic.d.ts | 16 - .../nodes/functions/BasicLightingModel.d.ts | 7 - .../nodes/functions/PhongLightingModel.d.ts | 7 - .../functions/PhysicalLightingModel.d.ts | 30 - .../src/nodes/functions/ShadowMaskModel.d.ts | 9 - .../nodes/functions/ToonLightingModel.d.ts | 4 - .../material/getGeometryRoughness.d.ts | 6 - .../functions/material/getRoughness.d.ts | 7 - src-testing/src/nodes/geometry/RangeNode.d.ts | 19 - src-testing/src/nodes/gpgpu/ComputeNode.ts | 67 - src-testing/src/nodes/lighting/AONode.d.ts | 8 - .../src/nodes/lighting/AnalyticLightNode.d.ts | 8 - .../nodes/lighting/BasicEnvironmentNode.d.ts | 10 - .../src/nodes/lighting/BasicLightMapNode.d.ts | 8 - .../src/nodes/lighting/EnvironmentNode.ts | 121 -- .../nodes/lighting/HemisphereLightNode.d.ts | 13 - .../src/nodes/lighting/IrradianceNode.d.ts | 8 - .../src/nodes/lighting/LightUtils.d.ts | 9 - .../src/nodes/lighting/LightingContextNode.ts | 58 - .../src/nodes/lighting/LightingNode.d.ts | 7 - src-testing/src/nodes/lighting/LightsNode.ts | 168 -- .../src/nodes/lighting/PointLightNode.d.ts | 10 - .../src/nodes/lighting/RectAreaLightNode.d.ts | 21 - .../src/nodes/lighting/SpotLightNode.d.ts | 15 - src-testing/src/nodes/loaders/NodeLoader.d.ts | 16 - .../src/nodes/loaders/NodeMaterialLoader.d.ts | 8 - .../src/nodes/loaders/NodeObjectLoader.d.ts | 10 - .../nodes/materials/Line2NodeMaterial.d.ts | 53 - .../materials/LineBasicNodeMaterial.d.ts | 22 - .../src/nodes/materials/Materials.d.ts | 15 - .../materials/MeshBasicNodeMaterial.d.ts | 36 - .../materials/MeshMatcapNodeMaterial.d.ts | 32 - .../materials/MeshNormalNodeMaterial.d.ts | 28 - .../materials/MeshPhongNodeMaterial.d.ts | 56 - .../materials/MeshPhysicalNodeMaterial.d.ts | 90 -- .../nodes/materials/MeshSSSNodeMaterial.d.ts | 16 - .../materials/MeshStandardNodeMaterial.d.ts | 56 - .../nodes/materials/MeshToonNodeMaterial.d.ts | 42 - .../src/nodes/materials/NodeMaterial.ts | 554 ------- .../nodes/materials/PointsNodeMaterial.d.ts | 22 - .../nodes/materials/ShadowNodeMaterial.d.ts | 17 - .../nodes/materials/SpriteNodeMaterial.d.ts | 26 - .../nodes/materials/VolumeNodeMaterial.d.ts | 10 - .../src/nodes/materialx/MaterialXNodes.d.ts | 107 -- .../src/nodes/materialx/lib/mx_hsv.d.ts | 6 - .../src/nodes/materialx/lib/mx_noise.d.ts | 359 ----- .../materialx/lib/mx_transform_color.d.ts | 4 - src-testing/src/nodes/math/CondNode.d.ts | 37 - src-testing/src/nodes/math/HashNode.d.ts | 16 - src-testing/src/nodes/math/MathNode.d.ts | 273 ---- src-testing/src/nodes/math/MathUtils.d.ts | 16 - src-testing/src/nodes/math/OperatorNode.d.ts | 83 - src-testing/src/nodes/math/TriNoise3D.d.ts | 12 - src-testing/src/nodes/pmrem/PMREMNode.d.ts | 20 - .../src/nodes/procedural/CheckerNode.d.ts | 15 - .../src/nodes/shadernode/ShaderNode.ts | 538 ------- .../src/nodes/utils/ArrayElementNode.d.ts | 9 - src-testing/src/nodes/utils/ConvertNode.d.ts | 7 - src-testing/src/nodes/utils/DiscardNode.d.ts | 17 - .../src/nodes/utils/EquirectUVNode.d.ts | 8 - src-testing/src/nodes/utils/JoinNode.d.ts | 10 - src-testing/src/nodes/utils/LoopNode.d.ts | 22 - src-testing/src/nodes/utils/MatcapUVNode.d.ts | 8 - .../src/nodes/utils/MaxMipLevelNode.d.ts | 14 - src-testing/src/nodes/utils/OscNode.d.ts | 25 - src-testing/src/nodes/utils/PackingNode.d.ts | 24 - src-testing/src/nodes/utils/RTTNode.d.ts | 45 - .../src/nodes/utils/ReflectorNode.d.ts | 32 - src-testing/src/nodes/utils/RemapNode.d.ts | 36 - src-testing/src/nodes/utils/RotateNode.d.ts | 21 - src-testing/src/nodes/utils/SplitNode.d.ts | 15 - .../src/nodes/utils/SpriteSheetUVNode.d.ts | 16 - src-testing/src/nodes/utils/SpriteUtils.d.ts | 6 - .../nodes/utils/StoargeArrayElementNode.d.ts | 25 - src-testing/src/nodes/utils/TimerNode.d.ts | 25 - .../nodes/utils/TriplanarTexturesNode.d.ts | 42 - src-testing/src/nodes/utils/UVUtils.d.ts | 21 - .../src/nodes/utils/ViewportUtils.d.ts | 4 - src-testing/src/objects/BatchedMesh.d.ts | 197 --- src-testing/src/objects/Bone.d.ts | 36 - src-testing/src/objects/Group.d.ts | 43 - src-testing/src/objects/InstancedMesh.d.ts | 179 --- src-testing/src/objects/LOD.d.ts | 104 -- src-testing/src/objects/Line.d.ts | 87 - src-testing/src/objects/LineLoop.d.ts | 40 - src-testing/src/objects/LineSegments.d.ts | 41 - src-testing/src/objects/Mesh.d.ts | 94 -- src-testing/src/objects/Points.d.ts | 66 - src-testing/src/objects/Skeleton.d.ts | 120 -- src-testing/src/objects/SkinnedMesh.d.ts | 160 -- src-testing/src/objects/Sprite.d.ts | 65 - .../src/renderers/WebGL3DRenderTarget.d.ts | 29 - .../src/renderers/WebGLArrayRenderTarget.d.ts | 29 - .../src/renderers/WebGLCubeRenderTarget.d.ts | 18 - .../src/renderers/WebGLRenderTarget.d.ts | 8 - src-testing/src/renderers/WebGLRenderer.d.ts | 562 ------- src-testing/src/renderers/common/Animation.ts | 38 - .../src/renderers/common/Attributes.ts | 54 - src-testing/src/renderers/common/Backend.ts | 170 -- .../src/renderers/common/Background.ts | 125 -- src-testing/src/renderers/common/BindGroup.ts | 13 - src-testing/src/renderers/common/Binding.ts | 17 - src-testing/src/renderers/common/Bindings.ts | 161 -- src-testing/src/renderers/common/Buffer.ts | 28 - .../src/renderers/common/BufferUtils.ts | 23 - src-testing/src/renderers/common/ChainMap.ts | 43 - .../src/renderers/common/ClippingContext.ts | 130 -- src-testing/src/renderers/common/Color4.ts | 27 - .../src/renderers/common/ComputePipeline.ts | 13 - src-testing/src/renderers/common/Constants.ts | 14 - .../src/renderers/common/CubeRenderTarget.ts | 65 - src-testing/src/renderers/common/DataMap.ts | 38 - .../src/renderers/common/Geometries.ts | 183 --- src-testing/src/renderers/common/Info.ts | 95 -- src-testing/src/renderers/common/Pipeline.ts | 9 - src-testing/src/renderers/common/Pipelines.ts | 270 ---- .../src/renderers/common/PostProcessing.d.ts | 21 - .../src/renderers/common/ProgrammableStage.ts | 16 - .../src/renderers/common/QuadMesh.d.ts | 14 - .../src/renderers/common/RenderBundle.ts | 12 - .../src/renderers/common/RenderBundles.ts | 28 - .../src/renderers/common/RenderContext.ts | 57 - .../src/renderers/common/RenderContexts.ts | 47 - .../src/renderers/common/RenderList.ts | 145 -- .../src/renderers/common/RenderLists.ts | 28 - .../src/renderers/common/RenderObject.ts | 231 --- .../src/renderers/common/RenderObjects.ts | 100 -- .../src/renderers/common/RenderPipeline.ts | 12 - src-testing/src/renderers/common/Renderer.ts | 1399 ----------------- .../src/renderers/common/SampledTexture.ts | 61 - src-testing/src/renderers/common/Sampler.ts | 14 - .../src/renderers/common/StorageBuffer.ts | 13 - .../common/StorageBufferAttribute.d.ts | 7 - .../StorageInstancedBufferAttribute.d.ts | 8 - .../src/renderers/common/StorageTexture.d.ts | 5 - src-testing/src/renderers/common/Textures.ts | 290 ---- src-testing/src/renderers/common/Uniform.ts | 105 -- .../src/renderers/common/UniformBuffer.ts | 11 - .../src/renderers/common/UniformsGroup.ts | 277 ---- .../renderers/common/extras/PMREMGenerator.ts | 660 -------- .../common/nodes/NodeBuilderState.ts | 55 - .../common/nodes/NodeSampledTexture.d.ts | 29 - .../renderers/common/nodes/NodeSampler.d.ts | 12 - .../src/renderers/common/nodes/NodeUniform.ts | 103 -- .../common/nodes/NodeUniformsGroup.ts | 30 - .../src/renderers/common/nodes/Nodes.ts | 399 ----- .../src/renderers/shaders/ShaderChunk.d.ts | 143 -- .../src/renderers/shaders/ShaderLib.d.ts | 29 - .../src/renderers/shaders/UniformsLib.d.ts | 189 --- .../src/renderers/shaders/UniformsUtils.d.ts | 14 - .../renderers/webgl-fallback/WebGLBackend.ts | 1280 --------------- .../webgl-fallback/nodes/GLSLNodeBuilder.ts | 794 ---------- .../src/renderers/webgl/WebGLAttributes.d.ts | 21 - .../renderers/webgl/WebGLBindingStates.d.ts | 26 - .../renderers/webgl/WebGLBufferRenderer.d.ts | 21 - .../renderers/webgl/WebGLCapabilities.d.ts | 35 - .../src/renderers/webgl/WebGLClipping.d.ts | 26 - .../src/renderers/webgl/WebGLCubeMaps.d.ts | 8 - .../src/renderers/webgl/WebGLCubeUVMaps.d.ts | 9 - .../src/renderers/webgl/WebGLExtensions.d.ts | 7 - .../src/renderers/webgl/WebGLGeometries.d.ts | 13 - .../webgl/WebGLIndexedBufferRenderer.d.ts | 15 - .../src/renderers/webgl/WebGLInfo.d.ts | 39 - .../src/renderers/webgl/WebGLLights.d.ts | 49 - .../src/renderers/webgl/WebGLObjects.d.ts | 6 - .../src/renderers/webgl/WebGLProgram.d.ts | 30 - .../src/renderers/webgl/WebGLPrograms.d.ts | 239 --- .../src/renderers/webgl/WebGLProperties.d.ts | 9 - .../src/renderers/webgl/WebGLRenderLists.d.ts | 66 - .../src/renderers/webgl/WebGLShader.d.ts | 1 - .../src/renderers/webgl/WebGLShadowMap.d.ts | 38 - .../src/renderers/webgl/WebGLState.d.ts | 116 -- .../src/renderers/webgl/WebGLTextures.d.ts | 30 - .../src/renderers/webgl/WebGLUniforms.d.ts | 12 - .../renderers/webgl/WebGLUniformsGroups.d.ts | 17 - .../src/renderers/webgl/WebGLUtils.d.ts | 11 - .../src/renderers/webgpu/WebGPUBackend.ts | 1249 --------------- .../src/renderers/webgpu/WebGPURenderer.ts | 43 - .../renderers/webgpu/nodes/WGSLNodeBuilder.ts | 1114 ------------- .../webgpu/nodes/WGSLNodeFunction.ts | 140 -- .../renderers/webgpu/nodes/WGSLNodeParser.ts | 10 - .../webgpu/utils/WebGPUConstants.d.ts | 328 ---- .../src/renderers/webxr/WebXRController.d.ts | 63 - .../renderers/webxr/WebXRDepthSensing.d.ts | 22 - .../src/renderers/webxr/WebXRManager.d.ts | 85 - src-testing/src/scenes/Fog.d.ts | 77 - src-testing/src/scenes/FogExp2.d.ts | 66 - src-testing/src/scenes/Scene.d.ts | 118 -- src-testing/src/textures/CanvasTexture.d.ts | 51 - .../src/textures/CompressedArrayTexture.d.ts | 68 - .../src/textures/CompressedCubeTexture.d.ts | 13 - .../src/textures/CompressedTexture.d.ts | 95 -- src-testing/src/textures/CubeTexture.d.ts | 90 -- src-testing/src/textures/Data3DTexture.d.ts | 96 -- .../src/textures/DataArrayTexture.d.ts | 123 -- src-testing/src/textures/DataTexture.d.ts | 113 -- src-testing/src/textures/DepthTexture.d.ts | 104 -- .../src/textures/FramebufferTexture.d.ts | 62 - src-testing/src/textures/Source.d.ts | 75 - src-testing/src/textures/Texture.d.ts | 477 ------ src-testing/src/textures/VideoTexture.d.ts | 90 -- src-testing/src/textures/types.d.ts | 9 - src-testing/src/utils.d.ts | 3 - 497 files changed, 30 insertions(+), 41027 deletions(-) delete mode 100644 src-testing/src/Three.Legacy.d.ts delete mode 100644 src-testing/src/Three.WebGPU.d.ts delete mode 100644 src-testing/src/Three.d.ts delete mode 100644 src-testing/src/animation/AnimationAction.d.ts delete mode 100644 src-testing/src/animation/AnimationClip.d.ts delete mode 100644 src-testing/src/animation/AnimationMixer.d.ts delete mode 100644 src-testing/src/animation/AnimationObjectGroup.d.ts delete mode 100644 src-testing/src/animation/AnimationUtils.d.ts delete mode 100644 src-testing/src/animation/KeyframeTrack.d.ts delete mode 100644 src-testing/src/animation/PropertyBinding.d.ts delete mode 100644 src-testing/src/animation/PropertyMixer.d.ts delete mode 100644 src-testing/src/animation/tracks/BooleanKeyframeTrack.d.ts delete mode 100644 src-testing/src/animation/tracks/ColorKeyframeTrack.d.ts delete mode 100644 src-testing/src/animation/tracks/NumberKeyframeTrack.d.ts delete mode 100644 src-testing/src/animation/tracks/QuaternionKeyframeTrack.d.ts delete mode 100644 src-testing/src/animation/tracks/StringKeyframeTrack.d.ts delete mode 100644 src-testing/src/animation/tracks/VectorKeyframeTrack.d.ts delete mode 100644 src-testing/src/audio/Audio.d.ts delete mode 100644 src-testing/src/audio/AudioAnalyser.d.ts delete mode 100644 src-testing/src/audio/AudioContext.d.ts delete mode 100644 src-testing/src/audio/AudioListener.d.ts delete mode 100644 src-testing/src/audio/PositionalAudio.d.ts delete mode 100644 src-testing/src/cameras/ArrayCamera.d.ts delete mode 100644 src-testing/src/cameras/Camera.d.ts delete mode 100644 src-testing/src/cameras/CubeCamera.d.ts delete mode 100644 src-testing/src/cameras/OrthographicCamera.d.ts delete mode 100644 src-testing/src/cameras/PerspectiveCamera.d.ts delete mode 100644 src-testing/src/cameras/StereoCamera.d.ts delete mode 100644 src-testing/src/constants.d.ts delete mode 100644 src-testing/src/core/BufferAttribute.d.ts delete mode 100644 src-testing/src/core/BufferGeometry.d.ts delete mode 100644 src-testing/src/core/Clock.d.ts delete mode 100644 src-testing/src/core/EventDispatcher.d.ts delete mode 100644 src-testing/src/core/GLBufferAttribute.d.ts delete mode 100644 src-testing/src/core/InstancedBufferAttribute.d.ts delete mode 100644 src-testing/src/core/InstancedBufferGeometry.d.ts delete mode 100644 src-testing/src/core/InstancedInterleavedBuffer.d.ts delete mode 100644 src-testing/src/core/InterleavedBuffer.d.ts delete mode 100644 src-testing/src/core/InterleavedBufferAttribute.d.ts delete mode 100644 src-testing/src/core/Layers.d.ts delete mode 100644 src-testing/src/core/Object3D.d.ts delete mode 100644 src-testing/src/core/Raycaster.d.ts delete mode 100644 src-testing/src/core/RenderTarget.d.ts delete mode 100644 src-testing/src/core/Uniform.d.ts delete mode 100644 src-testing/src/core/UniformsGroup.d.ts delete mode 100644 src-testing/src/extras/DataUtils.d.ts delete mode 100644 src-testing/src/extras/Earcut.d.ts delete mode 100644 src-testing/src/extras/ImageUtils.d.ts delete mode 100644 src-testing/src/extras/PMREMGenerator.d.ts delete mode 100644 src-testing/src/extras/ShapeUtils.d.ts delete mode 100644 src-testing/src/extras/TextureUtils.d.ts delete mode 100644 src-testing/src/extras/core/Curve.d.ts delete mode 100644 src-testing/src/extras/core/CurvePath.d.ts delete mode 100644 src-testing/src/extras/core/Interpolations.d.ts delete mode 100644 src-testing/src/extras/core/Path.d.ts delete mode 100644 src-testing/src/extras/core/Shape.d.ts delete mode 100644 src-testing/src/extras/core/ShapePath.d.ts delete mode 100644 src-testing/src/extras/curves/ArcCurve.d.ts delete mode 100644 src-testing/src/extras/curves/CatmullRomCurve3.d.ts delete mode 100644 src-testing/src/extras/curves/CubicBezierCurve.d.ts delete mode 100644 src-testing/src/extras/curves/CubicBezierCurve3.d.ts delete mode 100644 src-testing/src/extras/curves/Curves.d.ts delete mode 100644 src-testing/src/extras/curves/EllipseCurve.d.ts delete mode 100644 src-testing/src/extras/curves/LineCurve.d.ts delete mode 100644 src-testing/src/extras/curves/LineCurve3.d.ts delete mode 100644 src-testing/src/extras/curves/QuadraticBezierCurve.d.ts delete mode 100644 src-testing/src/extras/curves/QuadraticBezierCurve3.d.ts delete mode 100644 src-testing/src/extras/curves/SplineCurve.d.ts delete mode 100644 src-testing/src/geometries/BoxGeometry.d.ts delete mode 100644 src-testing/src/geometries/CapsuleGeometry.d.ts delete mode 100644 src-testing/src/geometries/CircleGeometry.d.ts delete mode 100644 src-testing/src/geometries/ConeGeometry.d.ts delete mode 100644 src-testing/src/geometries/CylinderGeometry.d.ts delete mode 100644 src-testing/src/geometries/DodecahedronGeometry.d.ts delete mode 100644 src-testing/src/geometries/EdgesGeometry.d.ts delete mode 100644 src-testing/src/geometries/ExtrudeGeometry.d.ts delete mode 100644 src-testing/src/geometries/Geometries.d.ts delete mode 100644 src-testing/src/geometries/IcosahedronGeometry.d.ts delete mode 100644 src-testing/src/geometries/LatheGeometry.d.ts delete mode 100644 src-testing/src/geometries/OctahedronGeometry.d.ts delete mode 100644 src-testing/src/geometries/PlaneGeometry.d.ts delete mode 100644 src-testing/src/geometries/PolyhedronGeometry.d.ts delete mode 100644 src-testing/src/geometries/RingGeometry.d.ts delete mode 100644 src-testing/src/geometries/ShapeGeometry.d.ts delete mode 100644 src-testing/src/geometries/SphereGeometry.d.ts delete mode 100644 src-testing/src/geometries/TetrahedronGeometry.d.ts delete mode 100644 src-testing/src/geometries/TorusGeometry.d.ts delete mode 100644 src-testing/src/geometries/TorusKnotGeometry.d.ts delete mode 100644 src-testing/src/geometries/TubeGeometry.d.ts delete mode 100644 src-testing/src/geometries/WireframeGeometry.d.ts delete mode 100644 src-testing/src/helpers/ArrowHelper.d.ts delete mode 100644 src-testing/src/helpers/AxesHelper.d.ts delete mode 100644 src-testing/src/helpers/Box3Helper.d.ts delete mode 100644 src-testing/src/helpers/BoxHelper.d.ts delete mode 100644 src-testing/src/helpers/CameraHelper.d.ts delete mode 100644 src-testing/src/helpers/DirectionalLightHelper.d.ts delete mode 100644 src-testing/src/helpers/GridHelper.d.ts delete mode 100644 src-testing/src/helpers/HemisphereLightHelper.d.ts delete mode 100644 src-testing/src/helpers/PlaneHelper.d.ts delete mode 100644 src-testing/src/helpers/PointLightHelper.d.ts delete mode 100644 src-testing/src/helpers/PolarGridHelper.d.ts delete mode 100644 src-testing/src/helpers/SkeletonHelper.d.ts delete mode 100644 src-testing/src/helpers/SpotLightHelper.d.ts delete mode 100644 src-testing/src/lights/AmbientLight.d.ts delete mode 100644 src-testing/src/lights/DirectionalLight.d.ts delete mode 100644 src-testing/src/lights/DirectionalLightShadow.d.ts delete mode 100644 src-testing/src/lights/HemisphereLight.d.ts delete mode 100644 src-testing/src/lights/Light.d.ts delete mode 100644 src-testing/src/lights/LightProbe.d.ts delete mode 100644 src-testing/src/lights/LightShadow.d.ts delete mode 100644 src-testing/src/lights/PointLight.d.ts delete mode 100644 src-testing/src/lights/PointLightShadow.d.ts delete mode 100644 src-testing/src/lights/RectAreaLight.d.ts delete mode 100644 src-testing/src/lights/SpotLight.d.ts delete mode 100644 src-testing/src/lights/SpotLightShadow.d.ts delete mode 100644 src-testing/src/lights/webgpu/IESSpotLight.d.ts delete mode 100644 src-testing/src/loaders/AnimationLoader.d.ts delete mode 100644 src-testing/src/loaders/AudioLoader.d.ts delete mode 100644 src-testing/src/loaders/BufferGeometryLoader.d.ts delete mode 100644 src-testing/src/loaders/Cache.d.ts delete mode 100644 src-testing/src/loaders/CompressedTextureLoader.d.ts delete mode 100644 src-testing/src/loaders/CubeTextureLoader.d.ts delete mode 100644 src-testing/src/loaders/DataTextureLoader.d.ts delete mode 100644 src-testing/src/loaders/FileLoader.d.ts delete mode 100644 src-testing/src/loaders/ImageBitmapLoader.d.ts delete mode 100644 src-testing/src/loaders/ImageLoader.d.ts delete mode 100644 src-testing/src/loaders/Loader.d.ts delete mode 100644 src-testing/src/loaders/LoaderUtils.d.ts delete mode 100644 src-testing/src/loaders/LoadingManager.d.ts delete mode 100644 src-testing/src/loaders/MaterialLoader.d.ts delete mode 100644 src-testing/src/loaders/ObjectLoader.d.ts delete mode 100644 src-testing/src/loaders/TextureLoader.d.ts delete mode 100644 src-testing/src/materials/LineBasicMaterial.d.ts delete mode 100644 src-testing/src/materials/LineDashedMaterial.d.ts delete mode 100644 src-testing/src/materials/Material.d.ts delete mode 100644 src-testing/src/materials/Materials.d.ts delete mode 100644 src-testing/src/materials/MeshBasicMaterial.d.ts delete mode 100644 src-testing/src/materials/MeshDepthMaterial.d.ts delete mode 100644 src-testing/src/materials/MeshDistanceMaterial.d.ts delete mode 100644 src-testing/src/materials/MeshLambertMaterial.d.ts delete mode 100644 src-testing/src/materials/MeshMatcapMaterial.d.ts delete mode 100644 src-testing/src/materials/MeshNormalMaterial.d.ts delete mode 100644 src-testing/src/materials/MeshPhongMaterial.d.ts delete mode 100644 src-testing/src/materials/MeshPhysicalMaterial.d.ts delete mode 100644 src-testing/src/materials/MeshStandardMaterial.d.ts delete mode 100644 src-testing/src/materials/MeshToonMaterial.d.ts delete mode 100644 src-testing/src/materials/PointsMaterial.d.ts delete mode 100644 src-testing/src/materials/RawShaderMaterial.d.ts delete mode 100644 src-testing/src/materials/ShaderMaterial.d.ts delete mode 100644 src-testing/src/materials/ShadowMaterial.d.ts delete mode 100644 src-testing/src/materials/SpriteMaterial.d.ts delete mode 100644 src-testing/src/math/Box2.d.ts delete mode 100644 src-testing/src/math/Box3.d.ts delete mode 100644 src-testing/src/math/Color.d.ts delete mode 100644 src-testing/src/math/ColorManagement.d.ts delete mode 100644 src-testing/src/math/Cylindrical.d.ts delete mode 100644 src-testing/src/math/Euler.d.ts delete mode 100644 src-testing/src/math/Frustum.d.ts delete mode 100644 src-testing/src/math/Interpolant.d.ts delete mode 100644 src-testing/src/math/Line3.d.ts delete mode 100644 src-testing/src/math/MathUtils.d.ts delete mode 100644 src-testing/src/math/Matrix2.d.ts delete mode 100644 src-testing/src/math/Matrix3.d.ts delete mode 100644 src-testing/src/math/Matrix4.d.ts delete mode 100644 src-testing/src/math/Plane.d.ts delete mode 100644 src-testing/src/math/Quaternion.d.ts delete mode 100644 src-testing/src/math/Ray.d.ts delete mode 100644 src-testing/src/math/Sphere.d.ts delete mode 100644 src-testing/src/math/Spherical.d.ts delete mode 100644 src-testing/src/math/SphericalHarmonics3.d.ts delete mode 100644 src-testing/src/math/Triangle.d.ts delete mode 100644 src-testing/src/math/Vector2.d.ts delete mode 100644 src-testing/src/math/Vector3.d.ts delete mode 100644 src-testing/src/math/Vector4.d.ts delete mode 100644 src-testing/src/math/interpolants/CubicInterpolant.d.ts delete mode 100644 src-testing/src/math/interpolants/DiscreteInterpolant.d.ts delete mode 100644 src-testing/src/math/interpolants/LinearInterpolant.d.ts delete mode 100644 src-testing/src/math/interpolants/QuaternionLinearInterpolant.d.ts delete mode 100644 src-testing/src/nodes/Nodes.ts delete mode 100644 src-testing/src/nodes/accessors/AccessorsUtils.d.ts delete mode 100644 src-testing/src/nodes/accessors/BatchNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/BitangentNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/BufferAttributeNode.ts delete mode 100644 src-testing/src/nodes/accessors/BufferNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/CameraNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/ClippingNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/CubeTextureNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/InstanceNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/MaterialNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/MaterialProperties.d.ts delete mode 100644 src-testing/src/nodes/accessors/MaterialReferenceNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/ModelNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/ModelViewProjectionNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/NormalNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/Object3DNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/PointUVNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/PositionNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/ReferenceNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/ReflectVectorNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/RendererReferenceNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/SkinningNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/StorageBufferNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/StorageTextureNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/TangentNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/Texture3DNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/TextureBicubicNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/TextureNode.ts delete mode 100644 src-testing/src/nodes/accessors/UVNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/UniformArrayNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/UserDataNode.d.ts delete mode 100644 src-testing/src/nodes/accessors/VertexColorNode.d.ts delete mode 100644 src-testing/src/nodes/code/CodeNode.ts delete mode 100644 src-testing/src/nodes/code/ExpressionNode.d.ts delete mode 100644 src-testing/src/nodes/code/FunctionCallNode.d.ts delete mode 100644 src-testing/src/nodes/code/FunctionNode.ts delete mode 100644 src-testing/src/nodes/core/AssignNode.d.ts delete mode 100644 src-testing/src/nodes/core/AttributeNode.d.ts delete mode 100644 src-testing/src/nodes/core/BypassNode.d.ts delete mode 100644 src-testing/src/nodes/core/CacheNode.d.ts delete mode 100644 src-testing/src/nodes/core/ConstNode.d.ts delete mode 100644 src-testing/src/nodes/core/ContextNode.ts delete mode 100644 src-testing/src/nodes/core/IndexNode.d.ts delete mode 100644 src-testing/src/nodes/core/InputNode.ts delete mode 100644 src-testing/src/nodes/core/LightingModel.d.ts delete mode 100644 src-testing/src/nodes/core/MRTNode.d.ts delete mode 100644 src-testing/src/nodes/core/Node.ts delete mode 100644 src-testing/src/nodes/core/NodeAttribute.ts delete mode 100644 src-testing/src/nodes/core/NodeBuilder.ts delete mode 100644 src-testing/src/nodes/core/NodeCache.ts delete mode 100644 src-testing/src/nodes/core/NodeCode.ts delete mode 100644 src-testing/src/nodes/core/NodeFrame.ts delete mode 100644 src-testing/src/nodes/core/NodeFunction.ts delete mode 100644 src-testing/src/nodes/core/NodeFunctionInput.d.ts delete mode 100644 src-testing/src/nodes/core/NodeKeywords.ts delete mode 100644 src-testing/src/nodes/core/NodeParser.ts delete mode 100644 src-testing/src/nodes/core/NodeUniform.ts delete mode 100644 src-testing/src/nodes/core/NodeUtils.ts delete mode 100644 src-testing/src/nodes/core/NodeVar.ts delete mode 100644 src-testing/src/nodes/core/NodeVarying.ts delete mode 100644 src-testing/src/nodes/core/OutputStructNode.d.ts delete mode 100644 src-testing/src/nodes/core/PropertyNode.d.ts delete mode 100644 src-testing/src/nodes/core/StackNode.ts delete mode 100644 src-testing/src/nodes/core/StructTypeNode.ts delete mode 100644 src-testing/src/nodes/core/TempNode.d.ts delete mode 100644 src-testing/src/nodes/core/UniformGroup.d.ts delete mode 100644 src-testing/src/nodes/core/UniformGroupNode.ts delete mode 100644 src-testing/src/nodes/core/UniformNode.ts delete mode 100644 src-testing/src/nodes/core/VarNode.d.ts delete mode 100644 src-testing/src/nodes/core/VaryingNode.d.ts delete mode 100644 src-testing/src/nodes/core/constants.ts delete mode 100644 src-testing/src/nodes/display/AfterImageNode.d.ts delete mode 100644 src-testing/src/nodes/display/AnamorphicNode.d.ts delete mode 100644 src-testing/src/nodes/display/BleachBypassNode.d.ts delete mode 100644 src-testing/src/nodes/display/BlendModeNode.d.ts delete mode 100644 src-testing/src/nodes/display/BloomNode.d.ts delete mode 100644 src-testing/src/nodes/display/ColorAdjustmentNode.d.ts delete mode 100644 src-testing/src/nodes/display/ColorSpaceNode.d.ts delete mode 100644 src-testing/src/nodes/display/DenoiseNode.d.ts delete mode 100644 src-testing/src/nodes/display/DepthOfFieldNode.d.ts delete mode 100644 src-testing/src/nodes/display/DotScreenNode.d.ts delete mode 100644 src-testing/src/nodes/display/FXAANode.d.ts delete mode 100644 src-testing/src/nodes/display/FilmNode.d.ts delete mode 100644 src-testing/src/nodes/display/FrontFacingNode.d.ts delete mode 100644 src-testing/src/nodes/display/GTAONode.d.ts delete mode 100644 src-testing/src/nodes/display/GaussianBlurNode.d.ts delete mode 100644 src-testing/src/nodes/display/Lut3DNode.d.ts delete mode 100644 src-testing/src/nodes/display/NormalMapNode.d.ts delete mode 100644 src-testing/src/nodes/display/PassNode.d.ts delete mode 100644 src-testing/src/nodes/display/PixelationPassNode.d.ts delete mode 100644 src-testing/src/nodes/display/PosterizeNode.d.ts delete mode 100644 src-testing/src/nodes/display/RGBShiftNode.d.ts delete mode 100644 src-testing/src/nodes/display/RenderOutputNode.d.ts delete mode 100644 src-testing/src/nodes/display/SepiaNode.d.ts delete mode 100644 src-testing/src/nodes/display/SobelOperatorNode.d.ts delete mode 100644 src-testing/src/nodes/display/ToneMappingNode.d.ts delete mode 100644 src-testing/src/nodes/display/TransitionNode.d.ts delete mode 100644 src-testing/src/nodes/display/ViewportDepthNode.d.ts delete mode 100644 src-testing/src/nodes/display/ViewportDepthTextureNode.d.ts delete mode 100644 src-testing/src/nodes/display/ViewportNode.d.ts delete mode 100644 src-testing/src/nodes/display/ViewportSharedTextureNode.d.ts delete mode 100644 src-testing/src/nodes/display/ViewportTextureNode.d.ts delete mode 100644 src-testing/src/nodes/fog/FogExp2Node.d.ts delete mode 100644 src-testing/src/nodes/fog/FogNode.ts delete mode 100644 src-testing/src/nodes/fog/FogRangeNode.d.ts delete mode 100644 src-testing/src/nodes/functions/BSDF/BRDF_GGX.d.ts delete mode 100644 src-testing/src/nodes/functions/BSDF/BRDF_Lambert.d.ts delete mode 100644 src-testing/src/nodes/functions/BSDF/BRDF_Sheen.d.ts delete mode 100644 src-testing/src/nodes/functions/BSDF/DFGApprox.d.ts delete mode 100644 src-testing/src/nodes/functions/BSDF/D_GGX.d.ts delete mode 100644 src-testing/src/nodes/functions/BSDF/D_GGX_Anisotropic.d.ts delete mode 100644 src-testing/src/nodes/functions/BSDF/F_Schlick.d.ts delete mode 100644 src-testing/src/nodes/functions/BSDF/LTC.d.ts delete mode 100644 src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated.d.ts delete mode 100644 src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated_Anisotropic.d.ts delete mode 100644 src-testing/src/nodes/functions/BasicLightingModel.d.ts delete mode 100644 src-testing/src/nodes/functions/PhongLightingModel.d.ts delete mode 100644 src-testing/src/nodes/functions/PhysicalLightingModel.d.ts delete mode 100644 src-testing/src/nodes/functions/ShadowMaskModel.d.ts delete mode 100644 src-testing/src/nodes/functions/ToonLightingModel.d.ts delete mode 100644 src-testing/src/nodes/functions/material/getGeometryRoughness.d.ts delete mode 100644 src-testing/src/nodes/functions/material/getRoughness.d.ts delete mode 100644 src-testing/src/nodes/geometry/RangeNode.d.ts delete mode 100644 src-testing/src/nodes/gpgpu/ComputeNode.ts delete mode 100644 src-testing/src/nodes/lighting/AONode.d.ts delete mode 100644 src-testing/src/nodes/lighting/AnalyticLightNode.d.ts delete mode 100644 src-testing/src/nodes/lighting/BasicEnvironmentNode.d.ts delete mode 100644 src-testing/src/nodes/lighting/BasicLightMapNode.d.ts delete mode 100644 src-testing/src/nodes/lighting/EnvironmentNode.ts delete mode 100644 src-testing/src/nodes/lighting/HemisphereLightNode.d.ts delete mode 100644 src-testing/src/nodes/lighting/IrradianceNode.d.ts delete mode 100644 src-testing/src/nodes/lighting/LightUtils.d.ts delete mode 100644 src-testing/src/nodes/lighting/LightingContextNode.ts delete mode 100644 src-testing/src/nodes/lighting/LightingNode.d.ts delete mode 100644 src-testing/src/nodes/lighting/LightsNode.ts delete mode 100644 src-testing/src/nodes/lighting/PointLightNode.d.ts delete mode 100644 src-testing/src/nodes/lighting/RectAreaLightNode.d.ts delete mode 100644 src-testing/src/nodes/lighting/SpotLightNode.d.ts delete mode 100644 src-testing/src/nodes/loaders/NodeLoader.d.ts delete mode 100644 src-testing/src/nodes/loaders/NodeMaterialLoader.d.ts delete mode 100644 src-testing/src/nodes/loaders/NodeObjectLoader.d.ts delete mode 100644 src-testing/src/nodes/materials/Line2NodeMaterial.d.ts delete mode 100644 src-testing/src/nodes/materials/LineBasicNodeMaterial.d.ts delete mode 100644 src-testing/src/nodes/materials/Materials.d.ts delete mode 100644 src-testing/src/nodes/materials/MeshBasicNodeMaterial.d.ts delete mode 100644 src-testing/src/nodes/materials/MeshMatcapNodeMaterial.d.ts delete mode 100644 src-testing/src/nodes/materials/MeshNormalNodeMaterial.d.ts delete mode 100644 src-testing/src/nodes/materials/MeshPhongNodeMaterial.d.ts delete mode 100644 src-testing/src/nodes/materials/MeshPhysicalNodeMaterial.d.ts delete mode 100644 src-testing/src/nodes/materials/MeshSSSNodeMaterial.d.ts delete mode 100644 src-testing/src/nodes/materials/MeshStandardNodeMaterial.d.ts delete mode 100644 src-testing/src/nodes/materials/MeshToonNodeMaterial.d.ts delete mode 100644 src-testing/src/nodes/materials/NodeMaterial.ts delete mode 100644 src-testing/src/nodes/materials/PointsNodeMaterial.d.ts delete mode 100644 src-testing/src/nodes/materials/ShadowNodeMaterial.d.ts delete mode 100644 src-testing/src/nodes/materials/SpriteNodeMaterial.d.ts delete mode 100644 src-testing/src/nodes/materials/VolumeNodeMaterial.d.ts delete mode 100644 src-testing/src/nodes/materialx/MaterialXNodes.d.ts delete mode 100644 src-testing/src/nodes/materialx/lib/mx_hsv.d.ts delete mode 100644 src-testing/src/nodes/materialx/lib/mx_noise.d.ts delete mode 100644 src-testing/src/nodes/materialx/lib/mx_transform_color.d.ts delete mode 100644 src-testing/src/nodes/math/CondNode.d.ts delete mode 100644 src-testing/src/nodes/math/HashNode.d.ts delete mode 100644 src-testing/src/nodes/math/MathNode.d.ts delete mode 100644 src-testing/src/nodes/math/MathUtils.d.ts delete mode 100644 src-testing/src/nodes/math/OperatorNode.d.ts delete mode 100644 src-testing/src/nodes/math/TriNoise3D.d.ts delete mode 100644 src-testing/src/nodes/pmrem/PMREMNode.d.ts delete mode 100644 src-testing/src/nodes/procedural/CheckerNode.d.ts delete mode 100644 src-testing/src/nodes/shadernode/ShaderNode.ts delete mode 100644 src-testing/src/nodes/utils/ArrayElementNode.d.ts delete mode 100644 src-testing/src/nodes/utils/ConvertNode.d.ts delete mode 100644 src-testing/src/nodes/utils/DiscardNode.d.ts delete mode 100644 src-testing/src/nodes/utils/EquirectUVNode.d.ts delete mode 100644 src-testing/src/nodes/utils/JoinNode.d.ts delete mode 100644 src-testing/src/nodes/utils/LoopNode.d.ts delete mode 100644 src-testing/src/nodes/utils/MatcapUVNode.d.ts delete mode 100644 src-testing/src/nodes/utils/MaxMipLevelNode.d.ts delete mode 100644 src-testing/src/nodes/utils/OscNode.d.ts delete mode 100644 src-testing/src/nodes/utils/PackingNode.d.ts delete mode 100644 src-testing/src/nodes/utils/RTTNode.d.ts delete mode 100644 src-testing/src/nodes/utils/ReflectorNode.d.ts delete mode 100644 src-testing/src/nodes/utils/RemapNode.d.ts delete mode 100644 src-testing/src/nodes/utils/RotateNode.d.ts delete mode 100644 src-testing/src/nodes/utils/SplitNode.d.ts delete mode 100644 src-testing/src/nodes/utils/SpriteSheetUVNode.d.ts delete mode 100644 src-testing/src/nodes/utils/SpriteUtils.d.ts delete mode 100644 src-testing/src/nodes/utils/StoargeArrayElementNode.d.ts delete mode 100644 src-testing/src/nodes/utils/TimerNode.d.ts delete mode 100644 src-testing/src/nodes/utils/TriplanarTexturesNode.d.ts delete mode 100644 src-testing/src/nodes/utils/UVUtils.d.ts delete mode 100644 src-testing/src/nodes/utils/ViewportUtils.d.ts delete mode 100644 src-testing/src/objects/BatchedMesh.d.ts delete mode 100644 src-testing/src/objects/Bone.d.ts delete mode 100644 src-testing/src/objects/Group.d.ts delete mode 100644 src-testing/src/objects/InstancedMesh.d.ts delete mode 100644 src-testing/src/objects/LOD.d.ts delete mode 100644 src-testing/src/objects/Line.d.ts delete mode 100644 src-testing/src/objects/LineLoop.d.ts delete mode 100644 src-testing/src/objects/LineSegments.d.ts delete mode 100644 src-testing/src/objects/Mesh.d.ts delete mode 100644 src-testing/src/objects/Points.d.ts delete mode 100644 src-testing/src/objects/Skeleton.d.ts delete mode 100644 src-testing/src/objects/SkinnedMesh.d.ts delete mode 100644 src-testing/src/objects/Sprite.d.ts delete mode 100644 src-testing/src/renderers/WebGL3DRenderTarget.d.ts delete mode 100644 src-testing/src/renderers/WebGLArrayRenderTarget.d.ts delete mode 100644 src-testing/src/renderers/WebGLCubeRenderTarget.d.ts delete mode 100644 src-testing/src/renderers/WebGLRenderTarget.d.ts delete mode 100644 src-testing/src/renderers/WebGLRenderer.d.ts delete mode 100644 src-testing/src/renderers/common/Animation.ts delete mode 100644 src-testing/src/renderers/common/Attributes.ts delete mode 100644 src-testing/src/renderers/common/Backend.ts delete mode 100644 src-testing/src/renderers/common/Background.ts delete mode 100644 src-testing/src/renderers/common/BindGroup.ts delete mode 100644 src-testing/src/renderers/common/Binding.ts delete mode 100644 src-testing/src/renderers/common/Bindings.ts delete mode 100644 src-testing/src/renderers/common/Buffer.ts delete mode 100644 src-testing/src/renderers/common/BufferUtils.ts delete mode 100644 src-testing/src/renderers/common/ChainMap.ts delete mode 100644 src-testing/src/renderers/common/ClippingContext.ts delete mode 100644 src-testing/src/renderers/common/Color4.ts delete mode 100644 src-testing/src/renderers/common/ComputePipeline.ts delete mode 100644 src-testing/src/renderers/common/Constants.ts delete mode 100644 src-testing/src/renderers/common/CubeRenderTarget.ts delete mode 100644 src-testing/src/renderers/common/DataMap.ts delete mode 100644 src-testing/src/renderers/common/Geometries.ts delete mode 100644 src-testing/src/renderers/common/Info.ts delete mode 100644 src-testing/src/renderers/common/Pipeline.ts delete mode 100644 src-testing/src/renderers/common/Pipelines.ts delete mode 100644 src-testing/src/renderers/common/PostProcessing.d.ts delete mode 100644 src-testing/src/renderers/common/ProgrammableStage.ts delete mode 100644 src-testing/src/renderers/common/QuadMesh.d.ts delete mode 100644 src-testing/src/renderers/common/RenderBundle.ts delete mode 100644 src-testing/src/renderers/common/RenderBundles.ts delete mode 100644 src-testing/src/renderers/common/RenderContext.ts delete mode 100644 src-testing/src/renderers/common/RenderContexts.ts delete mode 100644 src-testing/src/renderers/common/RenderList.ts delete mode 100644 src-testing/src/renderers/common/RenderLists.ts delete mode 100644 src-testing/src/renderers/common/RenderObject.ts delete mode 100644 src-testing/src/renderers/common/RenderObjects.ts delete mode 100644 src-testing/src/renderers/common/RenderPipeline.ts delete mode 100644 src-testing/src/renderers/common/Renderer.ts delete mode 100644 src-testing/src/renderers/common/SampledTexture.ts delete mode 100644 src-testing/src/renderers/common/Sampler.ts delete mode 100644 src-testing/src/renderers/common/StorageBuffer.ts delete mode 100644 src-testing/src/renderers/common/StorageBufferAttribute.d.ts delete mode 100644 src-testing/src/renderers/common/StorageInstancedBufferAttribute.d.ts delete mode 100644 src-testing/src/renderers/common/StorageTexture.d.ts delete mode 100644 src-testing/src/renderers/common/Textures.ts delete mode 100644 src-testing/src/renderers/common/Uniform.ts delete mode 100644 src-testing/src/renderers/common/UniformBuffer.ts delete mode 100644 src-testing/src/renderers/common/UniformsGroup.ts delete mode 100644 src-testing/src/renderers/common/extras/PMREMGenerator.ts delete mode 100644 src-testing/src/renderers/common/nodes/NodeBuilderState.ts delete mode 100644 src-testing/src/renderers/common/nodes/NodeSampledTexture.d.ts delete mode 100644 src-testing/src/renderers/common/nodes/NodeSampler.d.ts delete mode 100644 src-testing/src/renderers/common/nodes/NodeUniform.ts delete mode 100644 src-testing/src/renderers/common/nodes/NodeUniformsGroup.ts delete mode 100644 src-testing/src/renderers/common/nodes/Nodes.ts delete mode 100644 src-testing/src/renderers/shaders/ShaderChunk.d.ts delete mode 100644 src-testing/src/renderers/shaders/ShaderLib.d.ts delete mode 100644 src-testing/src/renderers/shaders/UniformsLib.d.ts delete mode 100644 src-testing/src/renderers/shaders/UniformsUtils.d.ts delete mode 100644 src-testing/src/renderers/webgl-fallback/WebGLBackend.ts delete mode 100644 src-testing/src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLAttributes.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLBindingStates.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLBufferRenderer.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLCapabilities.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLClipping.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLCubeMaps.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLCubeUVMaps.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLExtensions.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLGeometries.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLIndexedBufferRenderer.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLInfo.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLLights.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLObjects.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLProgram.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLPrograms.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLProperties.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLRenderLists.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLShader.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLShadowMap.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLState.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLTextures.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLUniforms.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLUniformsGroups.d.ts delete mode 100644 src-testing/src/renderers/webgl/WebGLUtils.d.ts delete mode 100644 src-testing/src/renderers/webgpu/WebGPUBackend.ts delete mode 100644 src-testing/src/renderers/webgpu/WebGPURenderer.ts delete mode 100644 src-testing/src/renderers/webgpu/nodes/WGSLNodeBuilder.ts delete mode 100644 src-testing/src/renderers/webgpu/nodes/WGSLNodeFunction.ts delete mode 100644 src-testing/src/renderers/webgpu/nodes/WGSLNodeParser.ts delete mode 100644 src-testing/src/renderers/webgpu/utils/WebGPUConstants.d.ts delete mode 100644 src-testing/src/renderers/webxr/WebXRController.d.ts delete mode 100644 src-testing/src/renderers/webxr/WebXRDepthSensing.d.ts delete mode 100644 src-testing/src/renderers/webxr/WebXRManager.d.ts delete mode 100644 src-testing/src/scenes/Fog.d.ts delete mode 100644 src-testing/src/scenes/FogExp2.d.ts delete mode 100644 src-testing/src/scenes/Scene.d.ts delete mode 100644 src-testing/src/textures/CanvasTexture.d.ts delete mode 100644 src-testing/src/textures/CompressedArrayTexture.d.ts delete mode 100644 src-testing/src/textures/CompressedCubeTexture.d.ts delete mode 100644 src-testing/src/textures/CompressedTexture.d.ts delete mode 100644 src-testing/src/textures/CubeTexture.d.ts delete mode 100644 src-testing/src/textures/Data3DTexture.d.ts delete mode 100644 src-testing/src/textures/DataArrayTexture.d.ts delete mode 100644 src-testing/src/textures/DataTexture.d.ts delete mode 100644 src-testing/src/textures/DepthTexture.d.ts delete mode 100644 src-testing/src/textures/FramebufferTexture.d.ts delete mode 100644 src-testing/src/textures/Source.d.ts delete mode 100644 src-testing/src/textures/Texture.d.ts delete mode 100644 src-testing/src/textures/VideoTexture.d.ts delete mode 100644 src-testing/src/textures/types.d.ts delete mode 100644 src-testing/src/utils.d.ts diff --git a/src-testing/changes.patch b/src-testing/changes.patch index 563e4ea30..afd2226e0 100644 --- a/src-testing/changes.patch +++ b/src-testing/changes.patch @@ -2649,7 +2649,7 @@ index 9a41cc7d..df337a72 100644 lights = sortLights(lights); diff --git a/src-testing/src/nodes/shadernode/ShaderNode.ts b/src-testing/src/nodes/shadernode/ShaderNode.ts -index 1781b70c..22aa238f 100644 +index 9b8e26e5..0b448c55 100644 --- a/src-testing/src/nodes/shadernode/ShaderNode.ts +++ b/src-testing/src/nodes/shadernode/ShaderNode.ts @@ -6,11 +6,49 @@ import SplitNode from '../utils/SplitNode.js'; @@ -2873,7 +2873,7 @@ index 1781b70c..22aa238f 100644 const { shaderNode, inputNodes } = this; if (shaderNode.layout) { -@@ -260,7 +435,15 @@ class ShaderCallNodeInternal extends Node { +@@ -259,7 +434,15 @@ class ShaderCallNodeInternal extends Node { } } @@ -2889,7 +2889,7 @@ index 1781b70c..22aa238f 100644 constructor(jsFunc) { super(); -@@ -274,7 +457,7 @@ class ShaderNodeInternal extends Node { +@@ -273,7 +456,7 @@ class ShaderNodeInternal extends Node { return /^\((\s+)?\[/.test(this.jsFunc.toString()); } @@ -2898,7 +2898,7 @@ index 1781b70c..22aa238f 100644 this.layout = layout; return this; -@@ -291,6 +474,8 @@ class ShaderNodeInternal extends Node { +@@ -290,6 +473,8 @@ class ShaderNodeInternal extends Node { } } @@ -2907,7 +2907,7 @@ index 1781b70c..22aa238f 100644 const bools = [false, true]; const uints = [0, 1, 2, 3]; const ints = [-1, -2]; -@@ -384,10 +569,27 @@ export function ShaderNode(jsFunc) { +@@ -383,10 +568,27 @@ export function ShaderNode(jsFunc) { return new Proxy(new ShaderNodeInternal(jsFunc), shaderNodeHandler); } @@ -4373,7 +4373,7 @@ index 29140365..165ece53 100644 } diff --git a/src-testing/src/renderers/common/RenderContext.ts b/src-testing/src/renderers/common/RenderContext.ts -index 55d35c49..a2c91530 100644 +index 342029ea..ab04452a 100644 --- a/src-testing/src/renderers/common/RenderContext.ts +++ b/src-testing/src/renderers/common/RenderContext.ts @@ -1,8 +1,51 @@ @@ -4428,6 +4428,21 @@ index 55d35c49..a2c91530 100644 constructor() { this.id = id++; +@@ -40,12 +83,12 @@ class RenderContext { + } + } + +-export function getCacheKey(renderContext) { ++export function getCacheKey(renderContext: RenderContext) { + const { textures, activeCubeFace } = renderContext; + + let key = ''; + +- for (const texture of textures) { ++ for (const texture of textures!) { + key += texture.id + ','; + } + diff --git a/src-testing/src/renderers/common/RenderContexts.ts b/src-testing/src/renderers/common/RenderContexts.ts index e77308c1..9989358d 100644 --- a/src-testing/src/renderers/common/RenderContexts.ts @@ -7056,10 +7071,10 @@ index a162ac89..d43ca101 100644 const nodeBuilder = renderObject.getNodeBuilderState(); diff --git a/src-testing/src/renderers/webgl-fallback/WebGLBackend.ts b/src-testing/src/renderers/webgl-fallback/WebGLBackend.ts -index 63c66ce4..6f3e94ce 100644 +index 35e79b78..74f3e1c5 100644 --- a/src-testing/src/renderers/webgl-fallback/WebGLBackend.ts +++ b/src-testing/src/renderers/webgl-fallback/WebGLBackend.ts -@@ -12,6 +12,7 @@ import { WebGLBufferRenderer } from './WebGLBufferRenderer.js'; +@@ -13,6 +13,7 @@ import { WebGLBufferRenderer } from './WebGLBufferRenderer.js'; import { warnOnce } from '../../utils.js'; import { WebGLCoordinateSystem } from '../../constants.js'; @@ -7067,7 +7082,7 @@ index 63c66ce4..6f3e94ce 100644 // -@@ -619,7 +620,7 @@ class WebGLBackend extends Backend { +@@ -621,7 +622,7 @@ class WebGLBackend extends Backend { this.textureUtils.destroyTexture(texture); } @@ -7481,10 +7496,10 @@ index ec5211b5..4fe4f075 100644 const nodeData = this.getDataFromNode(node, shaderStage, this.globalCache); diff --git a/src-testing/src/renderers/webgpu/WebGPUBackend.ts b/src-testing/src/renderers/webgpu/WebGPUBackend.ts -index 8dcf5435..db61bf4c 100644 +index 2b2aeefc..82d67043 100644 --- a/src-testing/src/renderers/webgpu/WebGPUBackend.ts +++ b/src-testing/src/renderers/webgpu/WebGPUBackend.ts -@@ -959,7 +959,7 @@ class WebGPUBackend extends Backend { +@@ -957,7 +957,7 @@ class WebGPUBackend extends Backend { this.textureUtils.destroyTexture(texture); } @@ -7494,7 +7509,7 @@ index 8dcf5435..db61bf4c 100644 } diff --git a/src-testing/src/renderers/webgpu/nodes/WGSLNodeBuilder.ts b/src-testing/src/renderers/webgpu/nodes/WGSLNodeBuilder.ts -index 4a7730a7..2e766a82 100644 +index 1d808f4b..a1bfb45f 100644 --- a/src-testing/src/renderers/webgpu/nodes/WGSLNodeBuilder.ts +++ b/src-testing/src/renderers/webgpu/nodes/WGSLNodeBuilder.ts @@ -10,7 +10,7 @@ import { @@ -7659,7 +7674,7 @@ index 4a7730a7..2e766a82 100644 const layout = shaderNode.layout; const flowData = this.flowShaderNode(shaderNode); -@@ -621,8 +645,8 @@ ${flowData.code} +@@ -641,8 +665,8 @@ ${flowData.code} return snippets.join(',\n\t'); } @@ -7670,7 +7685,7 @@ index 4a7730a7..2e766a82 100644 if (shaderStage === 'compute') { this.getBuiltin('global_invocation_id', 'id', 'vec3', 'attribute'); -@@ -703,8 +727,8 @@ ${flowData.code} +@@ -723,8 +747,8 @@ ${flowData.code} return `\n${snippets.join('\n')}\n`; } @@ -7681,7 +7696,7 @@ index 4a7730a7..2e766a82 100644 if (shaderStage === 'vertex') { this.getBuiltin('position', 'Vertex', 'vec4', 'vertex'); -@@ -740,7 +764,7 @@ ${flowData.code} +@@ -760,7 +784,7 @@ ${flowData.code} return shaderStage === 'vertex' ? this._getWGSLStruct('VaryingsStruct', '\t' + code) : code; } diff --git a/src-testing/src/Three.Legacy.d.ts b/src-testing/src/Three.Legacy.d.ts deleted file mode 100644 index 07f4743b9..000000000 --- a/src-testing/src/Three.Legacy.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { RenderTargetOptions } from "./core/RenderTarget.js"; -import { WebGLRenderTarget } from "./renderers/WebGLRenderTarget.js"; -import { Texture } from "./textures/Texture.js"; - -/** - * @deprecated THREE.WebGLMultipleRenderTargets has been deprecated and will be removed in r172. Use THREE.WebGLRenderTarget and set the "count" parameter to enable MRT. - */ -export class WebGLMultipleRenderTargets extends WebGLRenderTarget { - readonly isWebGLMultipleRenderTargets: true; - - /** - * @deprecated THREE.WebGLMultipleRenderTargets has been deprecated and will be removed in r172. Use THREE.WebGLRenderTarget and set the "count" parameter to enable MRT. - * @param width The width of the render target. - * @param height The height of the render target. - * @param count The number of render targets. - * @param options object that holds texture parameters for an auto-generated target texture and depthBuffer/stencilBuffer booleans. - * For an explanation of the texture parameters see {@link Texture}. - */ - constructor(width?: number, height?: number, count?: number, options?: RenderTargetOptions); -} diff --git a/src-testing/src/Three.WebGPU.d.ts b/src-testing/src/Three.WebGPU.d.ts deleted file mode 100644 index 708281771..000000000 --- a/src-testing/src/Three.WebGPU.d.ts +++ /dev/null @@ -1,195 +0,0 @@ -export * from "./animation/AnimationAction.js"; -export * from "./animation/AnimationClip.js"; -export * from "./animation/AnimationMixer.js"; -export * from "./animation/AnimationObjectGroup.js"; -export { AnimationUtils } from "./animation/AnimationUtils.js"; -export * from "./animation/KeyframeTrack.js"; -export * from "./animation/PropertyBinding.js"; -export * from "./animation/PropertyMixer.js"; -export * from "./animation/tracks/BooleanKeyframeTrack.js"; -export * from "./animation/tracks/ColorKeyframeTrack.js"; -export * from "./animation/tracks/NumberKeyframeTrack.js"; -export * from "./animation/tracks/QuaternionKeyframeTrack.js"; -export * from "./animation/tracks/StringKeyframeTrack.js"; -export * from "./animation/tracks/VectorKeyframeTrack.js"; -export * from "./audio/Audio.js"; -export * from "./audio/AudioAnalyser.js"; -export * from "./audio/AudioContext.js"; -export * from "./audio/AudioListener.js"; -export * from "./audio/PositionalAudio.js"; -export * from "./cameras/ArrayCamera.js"; -export * from "./cameras/Camera.js"; -export * from "./cameras/CubeCamera.js"; -export * from "./cameras/OrthographicCamera.js"; -export * from "./cameras/PerspectiveCamera.js"; -export * from "./cameras/StereoCamera.js"; -export * from "./constants.js"; -export * from "./core/BufferAttribute.js"; -export * from "./core/BufferGeometry.js"; -export * from "./core/Clock.js"; -export * from "./core/EventDispatcher.js"; -export * from "./core/GLBufferAttribute.js"; -export * from "./core/InstancedBufferAttribute.js"; -export * from "./core/InstancedBufferGeometry.js"; -export * from "./core/InstancedInterleavedBuffer.js"; -export * from "./core/InterleavedBuffer.js"; -export * from "./core/InterleavedBufferAttribute.js"; -export * from "./core/Layers.js"; -export * from "./core/Object3D.js"; -export * from "./core/Raycaster.js"; -export * from "./core/RenderTarget.js"; -export * from "./core/Uniform.js"; -export * from "./core/UniformsGroup.js"; -export * from "./extras/core/Curve.js"; -export * from "./extras/core/CurvePath.js"; -export * from "./extras/core/Path.js"; -export * from "./extras/core/Shape.js"; -export * from "./extras/core/ShapePath.js"; -export * from "./extras/curves/Curves.js"; -export { DataUtils } from "./extras/DataUtils.js"; -export * from "./extras/ImageUtils.js"; -// export * from "./extras/PMREMGenerator.js"; -export * from "./extras/ShapeUtils.js"; -export { TextureUtils } from "./extras/TextureUtils.js"; -export * from "./geometries/Geometries.js"; -export * from "./helpers/ArrowHelper.js"; -export * from "./helpers/AxesHelper.js"; -export * from "./helpers/Box3Helper.js"; -export * from "./helpers/BoxHelper.js"; -export * from "./helpers/CameraHelper.js"; -export * from "./helpers/DirectionalLightHelper.js"; -export * from "./helpers/GridHelper.js"; -export * from "./helpers/HemisphereLightHelper.js"; -export * from "./helpers/PlaneHelper.js"; -export * from "./helpers/PointLightHelper.js"; -export * from "./helpers/PolarGridHelper.js"; -export * from "./helpers/SkeletonHelper.js"; -export * from "./helpers/SpotLightHelper.js"; -export * from "./lights/AmbientLight.js"; -export * from "./lights/DirectionalLight.js"; -export type { DirectionalLightShadow } from "./lights/DirectionalLightShadow.js"; -export * from "./lights/HemisphereLight.js"; -export * from "./lights/Light.js"; -export * from "./lights/LightProbe.js"; -export type { LightShadow, LightShadowJSON } from "./lights/LightShadow.js"; -export * from "./lights/PointLight.js"; -export type { PointLightShadow } from "./lights/PointLightShadow.js"; -export * from "./lights/RectAreaLight.js"; -export * from "./lights/SpotLight.js"; -export type { SpotLightShadow } from "./lights/SpotLightShadow.js"; -export * from "./loaders/AnimationLoader.js"; -export * from "./loaders/AudioLoader.js"; -export * from "./loaders/BufferGeometryLoader.js"; -export * from "./loaders/Cache.js"; -export * from "./loaders/CompressedTextureLoader.js"; -export * from "./loaders/CubeTextureLoader.js"; -export * from "./loaders/DataTextureLoader.js"; -export * from "./loaders/FileLoader.js"; -export * from "./loaders/ImageBitmapLoader.js"; -export * from "./loaders/ImageLoader.js"; -export * from "./loaders/Loader.js"; -export * from "./loaders/LoaderUtils.js"; -export * from "./loaders/LoadingManager.js"; -export * from "./loaders/MaterialLoader.js"; -export * from "./loaders/ObjectLoader.js"; -export * from "./loaders/TextureLoader.js"; -export * from "./materials/Materials.js"; -export * from "./math/Box2.js"; -export * from "./math/Box3.js"; -export * from "./math/Color.js"; -export { ColorManagement, DefinedColorSpace, WorkingColorSpace } from "./math/ColorManagement.js"; -export * from "./math/Cylindrical.js"; -export * from "./math/Euler.js"; -export * from "./math/Frustum.js"; -export * from "./math/Interpolant.js"; -export * from "./math/interpolants/CubicInterpolant.js"; -export * from "./math/interpolants/DiscreteInterpolant.js"; -export * from "./math/interpolants/LinearInterpolant.js"; -export * from "./math/interpolants/QuaternionLinearInterpolant.js"; -export * from "./math/Line3.js"; -export { MathUtils } from "./math/MathUtils.js"; -export * from "./math/Matrix2.js"; -export * from "./math/Matrix3.js"; -export * from "./math/Matrix4.js"; -export * from "./math/Plane.js"; -export * from "./math/Quaternion.js"; -export * from "./math/Ray.js"; -export * from "./math/Sphere.js"; -export * from "./math/Spherical.js"; -export * from "./math/SphericalHarmonics3.js"; -export * from "./math/Triangle.js"; -export * from "./math/Vector2.js"; -export * from "./math/Vector3.js"; -export * from "./math/Vector4.js"; -export * from "./objects/BatchedMesh.js"; -export * from "./objects/Bone.js"; -export * from "./objects/Group.js"; -export * from "./objects/InstancedMesh.js"; -export * from "./objects/Line.js"; -export * from "./objects/LineLoop.js"; -export * from "./objects/LineSegments.js"; -export * from "./objects/LOD.js"; -export * from "./objects/Mesh.js"; -export * from "./objects/Points.js"; -export * from "./objects/Skeleton.js"; -export * from "./objects/SkinnedMesh.js"; -export * from "./objects/Sprite.js"; -// export * from "./renderers/shaders/ShaderChunk.js"; -// export * from "./renderers/shaders/ShaderLib.js"; -// export * from "./renderers/shaders/UniformsLib.js"; -// export { UniformsUtils } from './renderers/shaders/UniformsUtils.js'; -export type { WebGLProgramParameters, WebGLProgramParametersWithUniforms } from "./renderers/webgl/WebGLPrograms.js"; -export type { WebGLShadowMap } from "./renderers/webgl/WebGLShadowMap.js"; -// export * from "./renderers/webgl/WebGLUtils.js"; -export * from "./renderers/WebGL3DRenderTarget.js"; -export * from "./renderers/WebGLArrayRenderTarget.js"; -export * from "./renderers/WebGLCubeRenderTarget.js"; -// export * from "./renderers/WebGLRenderer.js"; -export * from "./renderers/WebGLRenderTarget.js"; -export type { - WebXRController, - WebXRSpaceEventMap, - XRControllerEventType, - XRGripSpace, - XRHandInputState, - XRHandJoints, - XRHandSpace, - XRJointSpace, - XRTargetRaySpace, -} from "./renderers/webxr/WebXRController.js"; -export type { WebXRDepthSensing } from "./renderers/webxr/WebXRDepthSensing.js"; -export type { - WebXRArrayCamera, - WebXRCamera, - WebXRManager, - WebXRManagerEventMap, -} from "./renderers/webxr/WebXRManager.js"; -export * from "./scenes/Fog.js"; -export * from "./scenes/FogExp2.js"; -export * from "./scenes/Scene.js"; -export * from "./textures/CanvasTexture.js"; -export * from "./textures/CompressedArrayTexture.js"; -export * from "./textures/CompressedCubeTexture.js"; -export * from "./textures/CompressedTexture.js"; -export * from "./textures/CubeTexture.js"; -export * from "./textures/Data3DTexture.js"; -export * from "./textures/DataArrayTexture.js"; -export * from "./textures/DataTexture.js"; -export * from "./textures/DepthTexture.js"; -export * from "./textures/FramebufferTexture.js"; -export * from "./textures/Source.js"; -export * from "./textures/Texture.js"; -export * from "./textures/VideoTexture.js"; -export * from "./Three.Legacy.js"; -export { createCanvasElement } from "./utils.js"; - -export { default as IESSpotLight } from "./lights/webgpu/IESSpotLight.js"; -export * from "./nodes/Nodes.js"; -export { default as PMREMGenerator } from "./renderers/common/extras/PMREMGenerator.js"; -export { default as PostProcessing } from "./renderers/common/PostProcessing.js"; -export { default as QuadMesh } from "./renderers/common/QuadMesh.js"; -export type { default as Renderer } from "./renderers/common/Renderer.js"; -export { default as StorageBufferAttribute } from "./renderers/common/StorageBufferAttribute.js"; -export { default as StorageInstancedBufferAttribute } from "./renderers/common/StorageInstancedBufferAttribute.js"; -export { default as StorageTexture } from "./renderers/common/StorageTexture.js"; -export { default as WebGPURenderer } from "./renderers/webgpu/WebGPURenderer.js"; diff --git a/src-testing/src/Three.d.ts b/src-testing/src/Three.d.ts deleted file mode 100644 index 2b2ef3cb7..000000000 --- a/src-testing/src/Three.d.ts +++ /dev/null @@ -1,213 +0,0 @@ -export * from "./animation/AnimationAction.js"; -export * from "./animation/AnimationClip.js"; -export * from "./animation/AnimationMixer.js"; -export * from "./animation/AnimationObjectGroup.js"; -export { AnimationUtils } from "./animation/AnimationUtils.js"; -export * from "./animation/KeyframeTrack.js"; -export * from "./animation/PropertyBinding.js"; -export * from "./animation/PropertyMixer.js"; -export * from "./animation/tracks/BooleanKeyframeTrack.js"; -export * from "./animation/tracks/ColorKeyframeTrack.js"; -export * from "./animation/tracks/NumberKeyframeTrack.js"; -export * from "./animation/tracks/QuaternionKeyframeTrack.js"; -export * from "./animation/tracks/StringKeyframeTrack.js"; -export * from "./animation/tracks/VectorKeyframeTrack.js"; -export * from "./audio/Audio.js"; -export * from "./audio/AudioAnalyser.js"; -export * from "./audio/AudioContext.js"; -export * from "./audio/AudioListener.js"; -export * from "./audio/PositionalAudio.js"; -export * from "./cameras/ArrayCamera.js"; -export * from "./cameras/Camera.js"; -export * from "./cameras/CubeCamera.js"; -export * from "./cameras/OrthographicCamera.js"; -export * from "./cameras/PerspectiveCamera.js"; -export * from "./cameras/StereoCamera.js"; -export * from "./constants.js"; -export * from "./core/BufferAttribute.js"; -export * from "./core/BufferGeometry.js"; -export * from "./core/Clock.js"; -export * from "./core/EventDispatcher.js"; -export * from "./core/GLBufferAttribute.js"; -export * from "./core/InstancedBufferAttribute.js"; -export * from "./core/InstancedBufferGeometry.js"; -export * from "./core/InstancedInterleavedBuffer.js"; -export * from "./core/InterleavedBuffer.js"; -export * from "./core/InterleavedBufferAttribute.js"; -export * from "./core/Layers.js"; -export * from "./core/Object3D.js"; -export * from "./core/Raycaster.js"; -export * from "./core/RenderTarget.js"; -export * from "./core/Uniform.js"; -export * from "./core/UniformsGroup.js"; -export * from "./extras/core/Curve.js"; -export * from "./extras/core/CurvePath.js"; -export * from "./extras/core/Path.js"; -export * from "./extras/core/Shape.js"; -export * from "./extras/core/ShapePath.js"; -export * from "./extras/curves/Curves.js"; -export { DataUtils } from "./extras/DataUtils.js"; -export * from "./extras/ImageUtils.js"; -export * from "./extras/PMREMGenerator.js"; -export * from "./extras/ShapeUtils.js"; -export { TextureUtils } from "./extras/TextureUtils.js"; -export * from "./geometries/Geometries.js"; -export * from "./helpers/ArrowHelper.js"; -export * from "./helpers/AxesHelper.js"; -export * from "./helpers/Box3Helper.js"; -export * from "./helpers/BoxHelper.js"; -export * from "./helpers/CameraHelper.js"; -export * from "./helpers/DirectionalLightHelper.js"; -export * from "./helpers/GridHelper.js"; -export * from "./helpers/HemisphereLightHelper.js"; -export * from "./helpers/PlaneHelper.js"; -export * from "./helpers/PointLightHelper.js"; -export * from "./helpers/PolarGridHelper.js"; -export * from "./helpers/SkeletonHelper.js"; -export * from "./helpers/SpotLightHelper.js"; -export * from "./lights/AmbientLight.js"; -export * from "./lights/DirectionalLight.js"; -export type { DirectionalLightShadow } from "./lights/DirectionalLightShadow.js"; -export * from "./lights/HemisphereLight.js"; -export * from "./lights/Light.js"; -export * from "./lights/LightProbe.js"; -export type { LightShadow, LightShadowJSON } from "./lights/LightShadow.js"; -export * from "./lights/PointLight.js"; -export type { PointLightShadow } from "./lights/PointLightShadow.js"; -export * from "./lights/RectAreaLight.js"; -export * from "./lights/SpotLight.js"; -export type { SpotLightShadow } from "./lights/SpotLightShadow.js"; -export * from "./loaders/AnimationLoader.js"; -export * from "./loaders/AudioLoader.js"; -export * from "./loaders/BufferGeometryLoader.js"; -export * from "./loaders/Cache.js"; -export * from "./loaders/CompressedTextureLoader.js"; -export * from "./loaders/CubeTextureLoader.js"; -export * from "./loaders/DataTextureLoader.js"; -export * from "./loaders/FileLoader.js"; -export * from "./loaders/ImageBitmapLoader.js"; -export * from "./loaders/ImageLoader.js"; -export * from "./loaders/Loader.js"; -export * from "./loaders/LoaderUtils.js"; -export * from "./loaders/LoadingManager.js"; -export * from "./loaders/MaterialLoader.js"; -export * from "./loaders/ObjectLoader.js"; -export * from "./loaders/TextureLoader.js"; -export * from "./materials/Materials.js"; -export * from "./math/Box2.js"; -export * from "./math/Box3.js"; -export * from "./math/Color.js"; -export { ColorManagement, DefinedColorSpace, WorkingColorSpace } from "./math/ColorManagement.js"; -export * from "./math/Cylindrical.js"; -export * from "./math/Euler.js"; -export * from "./math/Frustum.js"; -export * from "./math/Interpolant.js"; -export * from "./math/interpolants/CubicInterpolant.js"; -export * from "./math/interpolants/DiscreteInterpolant.js"; -export * from "./math/interpolants/LinearInterpolant.js"; -export * from "./math/interpolants/QuaternionLinearInterpolant.js"; -export * from "./math/Line3.js"; -export { MathUtils } from "./math/MathUtils.js"; -export * from "./math/Matrix2.js"; -export * from "./math/Matrix3.js"; -export * from "./math/Matrix4.js"; -export * from "./math/Plane.js"; -export * from "./math/Quaternion.js"; -export * from "./math/Ray.js"; -export * from "./math/Sphere.js"; -export * from "./math/Spherical.js"; -export * from "./math/SphericalHarmonics3.js"; -export * from "./math/Triangle.js"; -export * from "./math/Vector2.js"; -export * from "./math/Vector3.js"; -export * from "./math/Vector4.js"; -export * from "./objects/BatchedMesh.js"; -export * from "./objects/Bone.js"; -export * from "./objects/Group.js"; -export * from "./objects/InstancedMesh.js"; -export * from "./objects/Line.js"; -export * from "./objects/LineLoop.js"; -export * from "./objects/LineSegments.js"; -export * from "./objects/LOD.js"; -export * from "./objects/Mesh.js"; -export * from "./objects/Points.js"; -export * from "./objects/Skeleton.js"; -export * from "./objects/SkinnedMesh.js"; -export * from "./objects/Sprite.js"; -export * from "./renderers/shaders/ShaderChunk.js"; -export * from "./renderers/shaders/ShaderLib.js"; -export * from "./renderers/shaders/UniformsLib.js"; -export { UniformsUtils } from "./renderers/shaders/UniformsUtils.js"; -export type { WebGLAttributes } from "./renderers/webgl/WebGLAttributes.js"; -export type { WebGLBindingStates } from "./renderers/webgl/WebGLBindingStates.js"; -export type { WebGLBufferRenderer } from "./renderers/webgl/WebGLBufferRenderer.js"; -export type { WebGLCapabilities, WebGLCapabilitiesParameters } from "./renderers/webgl/WebGLCapabilities.js"; -export type { WebGLClipping } from "./renderers/webgl/WebGLClipping.js"; -export type { WebGLCubeMaps } from "./renderers/webgl/WebGLCubeMaps.js"; -export type { WebGLCubeUVMaps } from "./renderers/webgl/WebGLCubeUVMaps.js"; -export type { WebGLExtensions } from "./renderers/webgl/WebGLExtensions.js"; -export type { WebGLGeometries } from "./renderers/webgl/WebGLGeometries.js"; -export type { WebGLIndexedBufferRenderer } from "./renderers/webgl/WebGLIndexedBufferRenderer.js"; -export type { WebGLInfo } from "./renderers/webgl/WebGLInfo.js"; -export type { WebGLLights, WebGLLightsState } from "./renderers/webgl/WebGLLights.js"; -export type { WebGLObjects } from "./renderers/webgl/WebGLObjects.js"; -export type { WebGLProgram } from "./renderers/webgl/WebGLProgram.js"; -export type { - WebGLProgramParameters, - WebGLProgramParametersWithUniforms, - WebGLPrograms, -} from "./renderers/webgl/WebGLPrograms.js"; -export type { WebGLProperties } from "./renderers/webgl/WebGLProperties.js"; -export type { RenderItem, WebGLRenderList, WebGLRenderLists } from "./renderers/webgl/WebGLRenderLists.js"; -export type { WebGLShader } from "./renderers/webgl/WebGLShader.js"; -export type { WebGLShadowMap } from "./renderers/webgl/WebGLShadowMap.js"; -export type { - WebGLColorBuffer, - WebGLDepthBuffer, - WebGLState, - WebGLStencilBuffer, -} from "./renderers/webgl/WebGLState.js"; -export type { WebGLTextures } from "./renderers/webgl/WebGLTextures.js"; -export type { WebGLUniforms } from "./renderers/webgl/WebGLUniforms.js"; -export * from "./renderers/webgl/WebGLUtils.js"; -export * from "./renderers/WebGL3DRenderTarget.js"; -export * from "./renderers/WebGLArrayRenderTarget.js"; -export * from "./renderers/WebGLCubeRenderTarget.js"; -export * from "./renderers/WebGLRenderer.js"; -export * from "./renderers/WebGLRenderTarget.js"; -export type { - WebXRController, - WebXRSpaceEventMap, - XRControllerEventType, - XRGripSpace, - XRHandInputState, - XRHandJoints, - XRHandSpace, - XRJointSpace, - XRTargetRaySpace, -} from "./renderers/webxr/WebXRController.js"; -export type { WebXRDepthSensing } from "./renderers/webxr/WebXRDepthSensing.js"; -export type { - WebXRArrayCamera, - WebXRCamera, - WebXRManager, - WebXRManagerEventMap, -} from "./renderers/webxr/WebXRManager.js"; -export * from "./scenes/Fog.js"; -export * from "./scenes/FogExp2.js"; -export * from "./scenes/Scene.js"; -export * from "./textures/CanvasTexture.js"; -export * from "./textures/CompressedArrayTexture.js"; -export * from "./textures/CompressedCubeTexture.js"; -export * from "./textures/CompressedTexture.js"; -export * from "./textures/CubeTexture.js"; -export * from "./textures/Data3DTexture.js"; -export * from "./textures/DataArrayTexture.js"; -export * from "./textures/DataTexture.js"; -export * from "./textures/DepthTexture.js"; -export * from "./textures/FramebufferTexture.js"; -export * from "./textures/Source.js"; -export * from "./textures/Texture.js"; -export * from "./textures/VideoTexture.js"; -export * from "./Three.Legacy.js"; -export { createCanvasElement } from "./utils.js"; diff --git a/src-testing/src/animation/AnimationAction.d.ts b/src-testing/src/animation/AnimationAction.d.ts deleted file mode 100644 index 3fa3852a2..000000000 --- a/src-testing/src/animation/AnimationAction.d.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { AnimationActionLoopStyles, AnimationBlendMode } from "../constants.js"; -import { Object3D } from "../core/Object3D.js"; -import { AnimationClip } from "./AnimationClip.js"; -import { AnimationMixer } from "./AnimationMixer.js"; -// Animation //////////////////////////////////////////////////////////////////////////////////////// - -export class AnimationAction { - constructor(mixer: AnimationMixer, clip: AnimationClip, localRoot?: Object3D, blendMode?: AnimationBlendMode); - - blendMode: AnimationBlendMode; - - /** - * @default THREE.LoopRepeat - */ - loop: AnimationActionLoopStyles; - - /** - * @default 0 - */ - time: number; - - /** - * @default 1 - */ - timeScale: number; - - /** - * @default 1 - */ - weight: number; - - /** - * @default Infinity - */ - repetitions: number; - - /** - * @default false - */ - paused: boolean; - - /** - * @default true - */ - enabled: boolean; - - /** - * @default false - */ - clampWhenFinished: boolean; - - /** - * @default true - */ - zeroSlopeAtStart: boolean; - - /** - * @default true - */ - zeroSlopeAtEnd: boolean; - - play(): AnimationAction; - stop(): AnimationAction; - reset(): AnimationAction; - isRunning(): boolean; - isScheduled(): boolean; - startAt(time: number): AnimationAction; - setLoop(mode: AnimationActionLoopStyles, repetitions: number): AnimationAction; - setEffectiveWeight(weight: number): AnimationAction; - getEffectiveWeight(): number; - fadeIn(duration: number): AnimationAction; - fadeOut(duration: number): AnimationAction; - crossFadeFrom(fadeOutAction: AnimationAction, duration: number, warp: boolean): AnimationAction; - crossFadeTo(fadeInAction: AnimationAction, duration: number, warp: boolean): AnimationAction; - stopFading(): AnimationAction; - setEffectiveTimeScale(timeScale: number): AnimationAction; - getEffectiveTimeScale(): number; - setDuration(duration: number): AnimationAction; - syncWith(action: AnimationAction): AnimationAction; - halt(duration: number): AnimationAction; - warp(statTimeScale: number, endTimeScale: number, duration: number): AnimationAction; - stopWarping(): AnimationAction; - getMixer(): AnimationMixer; - getClip(): AnimationClip; - getRoot(): Object3D; -} diff --git a/src-testing/src/animation/AnimationClip.d.ts b/src-testing/src/animation/AnimationClip.d.ts deleted file mode 100644 index 4efc3df72..000000000 --- a/src-testing/src/animation/AnimationClip.d.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { AnimationBlendMode } from "../constants.js"; -import { Vector3 } from "../math/Vector3.js"; -import { Bone } from "../objects/Bone.js"; -import { KeyframeTrack, KeyframeTrackJSON } from "./KeyframeTrack.js"; - -export interface AnimationClipJSON { - name: string; - duration: number; - tracks: KeyframeTrackJSON[]; - uuid: string; - blendMode: AnimationBlendMode; -} - -export interface MorphTarget { - name: string; - vertices: Vector3[]; -} - -export class AnimationClip { - constructor(name?: string, duration?: number, tracks?: KeyframeTrack[], blendMode?: AnimationBlendMode); - - name: string; - tracks: KeyframeTrack[]; - - /** - * @default THREE.NormalAnimationBlendMode - */ - blendMode: AnimationBlendMode; - - /** - * @default -1 - */ - duration: number; - uuid: string; - results: any[]; - - resetDuration(): AnimationClip; - trim(): AnimationClip; - validate(): boolean; - optimize(): AnimationClip; - clone(): this; - toJSON(clip: AnimationClip): any; - - static CreateFromMorphTargetSequence( - name: string, - morphTargetSequence: MorphTarget[], - fps: number, - noLoop: boolean, - ): AnimationClip; - static findByName(clipArray: AnimationClip[], name: string): AnimationClip; - static CreateClipsFromMorphTargetSequences( - morphTargets: MorphTarget[], - fps: number, - noLoop: boolean, - ): AnimationClip[]; - static parse(json: AnimationClipJSON): AnimationClip; - static parseAnimation(animation: AnimationClipJSON, bones: Bone[]): AnimationClip; - static toJSON(clip: AnimationClip): AnimationClipJSON; -} diff --git a/src-testing/src/animation/AnimationMixer.d.ts b/src-testing/src/animation/AnimationMixer.d.ts deleted file mode 100644 index 95712d893..000000000 --- a/src-testing/src/animation/AnimationMixer.d.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { AnimationBlendMode } from "../constants.js"; -import { EventDispatcher } from "../core/EventDispatcher.js"; -import { Object3D } from "../core/Object3D.js"; -import { AnimationAction } from "./AnimationAction.js"; -import { AnimationClip } from "./AnimationClip.js"; -import { AnimationObjectGroup } from "./AnimationObjectGroup.js"; - -export interface AnimationMixerEventMap { - loop: { action: AnimationAction; loopDelta: number }; - finished: { action: AnimationAction; direction: number }; -} - -export class AnimationMixer extends EventDispatcher { - constructor(root: Object3D | AnimationObjectGroup); - - /** - * @default 0 - */ - time: number; - - /** - * @default 1.0 - */ - timeScale: number; - - clipAction( - clip: AnimationClip, - root?: Object3D | AnimationObjectGroup, - blendMode?: AnimationBlendMode, - ): AnimationAction; - existingAction(clip: AnimationClip, root?: Object3D | AnimationObjectGroup): AnimationAction | null; - stopAllAction(): AnimationMixer; - update(deltaTime: number): AnimationMixer; - setTime(timeInSeconds: number): AnimationMixer; - getRoot(): Object3D | AnimationObjectGroup; - uncacheClip(clip: AnimationClip): void; - uncacheRoot(root: Object3D | AnimationObjectGroup): void; - uncacheAction(clip: AnimationClip, root?: Object3D | AnimationObjectGroup): void; -} diff --git a/src-testing/src/animation/AnimationObjectGroup.d.ts b/src-testing/src/animation/AnimationObjectGroup.d.ts deleted file mode 100644 index 9d4f29ca9..000000000 --- a/src-testing/src/animation/AnimationObjectGroup.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -export class AnimationObjectGroup { - constructor(...args: any[]); - - uuid: string; - stats: { - bindingsPerObject: number; - objects: { - total: number; - inUse: number; - }; - }; - readonly isAnimationObjectGroup: true; - - add(...args: any[]): void; - remove(...args: any[]): void; - uncache(...args: any[]): void; -} diff --git a/src-testing/src/animation/AnimationUtils.d.ts b/src-testing/src/animation/AnimationUtils.d.ts deleted file mode 100644 index 4a712ed30..000000000 --- a/src-testing/src/animation/AnimationUtils.d.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { AnimationClip } from "./AnimationClip.js"; - -declare function convertArray(array: any, type: any, forceClone: boolean): any; - -declare function isTypedArray(object: any): boolean; - -declare function getKeyframeOrder(times: number[]): number[]; - -declare function sortedArray(values: any[], stride: number, order: number[]): any[]; - -declare function flattenJSON(jsonKeys: string[], times: any[], values: any[], valuePropertyName: string): void; - -/** - * @param sourceClip - * @param name - * @param startFrame - * @param endFrame - * @param [fps=30] - */ -declare function subclip( - sourceClip: AnimationClip, - name: string, - startFrame: number, - endFrame: number, - fps?: number, -): AnimationClip; - -/** - * @param targetClip - * @param [referenceFrame=0] - * @param [referenceClip=targetClip] - * @param [fps=30] - */ -declare function makeClipAdditive( - targetClip: AnimationClip, - referenceFrame?: number, - referenceClip?: AnimationClip, - fps?: number, -): AnimationClip; - -declare const AnimationUtils: { - convertArray: typeof convertArray; - isTypedArray: typeof isTypedArray; - getKeyframeOrder: typeof getKeyframeOrder; - sortedArray: typeof sortedArray; - flattenJSON: typeof flattenJSON; - subclip: typeof subclip; - makeClipAdditive: typeof makeClipAdditive; -}; - -export { - AnimationUtils, - convertArray, - flattenJSON, - getKeyframeOrder, - isTypedArray, - makeClipAdditive, - sortedArray, - subclip, -}; diff --git a/src-testing/src/animation/KeyframeTrack.d.ts b/src-testing/src/animation/KeyframeTrack.d.ts deleted file mode 100644 index 2a48e2651..000000000 --- a/src-testing/src/animation/KeyframeTrack.d.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { InterpolationModes } from "../constants.js"; -import { Interpolant } from "../math/Interpolant.js"; -import { CubicInterpolant } from "../math/interpolants/CubicInterpolant.js"; -import { DiscreteInterpolant } from "../math/interpolants/DiscreteInterpolant.js"; -import { LinearInterpolant } from "../math/interpolants/LinearInterpolant.js"; - -export interface KeyframeTrackJSON { - name: string; - times: number[]; - values: number[]; - interpolation?: InterpolationModes; - type: string; -} - -export class KeyframeTrack { - /** - * @param name - * @param times - * @param values - * @param [interpolation=THREE.InterpolateLinear] - */ - constructor(name: string, times: ArrayLike, values: ArrayLike, interpolation?: InterpolationModes); - - name: string; - times: Float32Array; - values: Float32Array; - - ValueTypeName: string; - TimeBufferType: Float32Array; - ValueBufferType: Float32Array; - - /** - * @default THREE.InterpolateLinear - */ - DefaultInterpolation: InterpolationModes; - - InterpolantFactoryMethodDiscrete(result: any): DiscreteInterpolant; - InterpolantFactoryMethodLinear(result: any): LinearInterpolant; - InterpolantFactoryMethodSmooth(result: any): CubicInterpolant; - - setInterpolation(interpolation: InterpolationModes): KeyframeTrack; - getInterpolation(): InterpolationModes; - createInterpolant(): Interpolant; - - getValueSize(): number; - - shift(timeOffset: number): KeyframeTrack; - scale(timeScale: number): KeyframeTrack; - trim(startTime: number, endTime: number): KeyframeTrack; - validate(): boolean; - optimize(): KeyframeTrack; - clone(): this; - - static toJSON(track: KeyframeTrack): KeyframeTrackJSON; -} diff --git a/src-testing/src/animation/PropertyBinding.d.ts b/src-testing/src/animation/PropertyBinding.d.ts deleted file mode 100644 index fec777634..000000000 --- a/src-testing/src/animation/PropertyBinding.d.ts +++ /dev/null @@ -1,43 +0,0 @@ -export interface ParseTrackNameResults { - nodeName: string; - objectName: string; - objectIndex: string; - propertyName: string; - propertyIndex: string; -} - -declare class Composite { - constructor(targetGroup: any, path: any, parsedPath?: any); - - getValue(array: any, offset: number): any; - setValue(array: any, offset: number): void; - bind(): void; - unbind(): void; -} - -declare class PropertyBinding { - constructor(rootNode: any, path: string, parsedPath?: any); - - path: string; - parsedPath: any; - node: any; - rootNode: any; - - getValue(targetArray: any, offset: number): any; - setValue(sourceArray: any, offset: number): void; - bind(): void; - unbind(): void; - - BindingType: { [bindingType: string]: number }; - Versioning: { [versioning: string]: number }; - - GetterByBindingType: Array<() => void>; - SetterByBindingTypeAndVersioning: Array void>>; - - static create(root: any, path: any, parsedPath?: any): PropertyBinding | Composite; - static sanitizeNodeName(name: string): string; - static parseTrackName(trackName: string): ParseTrackNameResults; - static findNode(root: any, nodeName: string): any; -} - -export { PropertyBinding }; diff --git a/src-testing/src/animation/PropertyMixer.d.ts b/src-testing/src/animation/PropertyMixer.d.ts deleted file mode 100644 index f413291fa..000000000 --- a/src-testing/src/animation/PropertyMixer.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -export class PropertyMixer { - constructor(binding: any, typeName: string, valueSize: number); - - binding: any; - valueSize: number; - buffer: any; - cumulativeWeight: number; - cumulativeWeightAdditive: number; - useCount: number; - referenceCount: number; - - accumulate(accuIndex: number, weight: number): void; - accumulateAdditive(weight: number): void; - apply(accuIndex: number): void; - saveOriginalState(): void; - restoreOriginalState(): void; -} diff --git a/src-testing/src/animation/tracks/BooleanKeyframeTrack.d.ts b/src-testing/src/animation/tracks/BooleanKeyframeTrack.d.ts deleted file mode 100644 index 45b65448a..000000000 --- a/src-testing/src/animation/tracks/BooleanKeyframeTrack.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { KeyframeTrack } from "../KeyframeTrack.js"; - -export class BooleanKeyframeTrack extends KeyframeTrack { - constructor(name: string, times: ArrayLike, values: ArrayLike); - - /** - * @default 'bool' - */ - ValueTypeName: string; -} diff --git a/src-testing/src/animation/tracks/ColorKeyframeTrack.d.ts b/src-testing/src/animation/tracks/ColorKeyframeTrack.d.ts deleted file mode 100644 index 60d0fe9f7..000000000 --- a/src-testing/src/animation/tracks/ColorKeyframeTrack.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { InterpolationModes } from "../../constants.js"; -import { KeyframeTrack } from "../KeyframeTrack.js"; - -export class ColorKeyframeTrack extends KeyframeTrack { - constructor(name: string, times: ArrayLike, values: ArrayLike, interpolation?: InterpolationModes); - - /** - * @default 'color' - */ - ValueTypeName: string; -} diff --git a/src-testing/src/animation/tracks/NumberKeyframeTrack.d.ts b/src-testing/src/animation/tracks/NumberKeyframeTrack.d.ts deleted file mode 100644 index e27ff0337..000000000 --- a/src-testing/src/animation/tracks/NumberKeyframeTrack.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { InterpolationModes } from "../../constants.js"; -import { KeyframeTrack } from "../KeyframeTrack.js"; - -export class NumberKeyframeTrack extends KeyframeTrack { - constructor(name: string, times: ArrayLike, values: ArrayLike, interpolation?: InterpolationModes); - - /** - * @default 'number' - */ - ValueTypeName: string; -} diff --git a/src-testing/src/animation/tracks/QuaternionKeyframeTrack.d.ts b/src-testing/src/animation/tracks/QuaternionKeyframeTrack.d.ts deleted file mode 100644 index 29942bad5..000000000 --- a/src-testing/src/animation/tracks/QuaternionKeyframeTrack.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { InterpolationModes } from "../../constants.js"; -import { KeyframeTrack } from "../KeyframeTrack.js"; - -export class QuaternionKeyframeTrack extends KeyframeTrack { - constructor(name: string, times: ArrayLike, values: ArrayLike, interpolation?: InterpolationModes); - - /** - * @default 'quaternion' - */ - ValueTypeName: string; -} diff --git a/src-testing/src/animation/tracks/StringKeyframeTrack.d.ts b/src-testing/src/animation/tracks/StringKeyframeTrack.d.ts deleted file mode 100644 index 9bbfd2027..000000000 --- a/src-testing/src/animation/tracks/StringKeyframeTrack.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { KeyframeTrack } from "../KeyframeTrack.js"; - -export class StringKeyframeTrack extends KeyframeTrack { - constructor(name: string, times: ArrayLike, values: ArrayLike); - - /** - * @default 'string' - */ - ValueTypeName: string; -} diff --git a/src-testing/src/animation/tracks/VectorKeyframeTrack.d.ts b/src-testing/src/animation/tracks/VectorKeyframeTrack.d.ts deleted file mode 100644 index 0909d4493..000000000 --- a/src-testing/src/animation/tracks/VectorKeyframeTrack.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { InterpolationModes } from "../../constants.js"; -import { KeyframeTrack } from "../KeyframeTrack.js"; - -export class VectorKeyframeTrack extends KeyframeTrack { - constructor(name: string, times: ArrayLike, values: ArrayLike, interpolation?: InterpolationModes); - - /** - * @default 'vector' - */ - ValueTypeName: string; -} diff --git a/src-testing/src/audio/Audio.d.ts b/src-testing/src/audio/Audio.d.ts deleted file mode 100644 index 434ff7fef..000000000 --- a/src-testing/src/audio/Audio.d.ts +++ /dev/null @@ -1,270 +0,0 @@ -import { Object3D } from "../core/Object3D.js"; -import { AudioContext } from "./AudioContext.js"; -import { AudioListener } from "./AudioListener.js"; - -// Extras / Audio ///////////////////////////////////////////////////////////////////// - -/** - * Create a non-positional ( global ) {@link Audio} object. - * This uses the {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API | Web {@link Audio} API}. - * @example - * ```typescript - * // create an AudioListener and add it to the camera - * const listener = new THREE.AudioListener(); - * camera.add(listener); - * // create a global {@link Audio} source - * const sound = new THREE.Audio(listener); - * // load a sound and set it as the {@link Audio} object's buffer - * const audioLoader = new THREE.AudioLoader(); - * audioLoader.load('sounds/ambient.ogg', function (buffer) { - * sound.setBuffer(buffer); - * sound.setLoop(true); - * sound.setVolume(0.5); - * sound.play(); - * }); - * ``` - * @see Example: {@link https://threejs.org/examples/#webaudio_sandbox | webaudio / sandbox } - * @see Example: {@link https://threejs.org/examples/#webaudio_visualizer | webaudio / visualizer } - * @see {@link https://threejs.org/docs/index.html#api/en/audio/Audio | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/audio/Audio.js | Source} - */ -export class Audio extends Object3D { - /** - * Create a new instance of {@link Audio} - * @param listener (required) {@link AudioListener | AudioListener} instance. - */ - constructor(listener: AudioListener); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `Audio` - */ - readonly type: string | "Audio"; - - /** - * A reference to the listener object of this audio. - */ - listener: AudioListener; - - /** - * The {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext | AudioContext} of the {@link AudioListener | listener} given in the constructor. - */ - context: AudioContext; - - /** - * A {@link https://developer.mozilla.org/en-US/docs/Web/API/GainNode | GainNode} created using - * {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createGain | AudioContext.createGain}(). - */ - gain: GainNode; - - /** - * Whether to start playback automatically. - * @defaultValue `false` - */ - autoplay: boolean; - - buffer: AudioBuffer | null; - - /** - * Modify pitch, measured in cents. +/- 100 is a semitone. +/- 1200 is an octave. - * @defaultValue `0` - */ - detune: number; - - /** - * @default false - */ - loop: boolean; - - /** - * @default 0 - */ - loopStart: number; - - /** - * @default 0 - */ - loopEnd: number; - - /** - * An offset to the time within the {@link Audio} buffer that playback should begin. - * Same as the {@link Audio.offset | offset} parameter of {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start | AudioBufferSourceNode.start()}. - * @defaultValue `0` - */ - offset: number; - - /** - * Overrides the duration of the audio. Same as the {@link Audio.duration | duration} parameter of - * {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start | AudioBufferSourceNode.start()}. - * @defaultValue `undefined` _to play the whole buffer_. - */ - duration: number | undefined; - - /** - * Speed of playback. - * @defaultValue `1` - */ - playbackRate: number; - - /** - * Whether the {@link Audio} is currently playing. - * @defaultValue `false` - */ - isPlaying: boolean; - - /** - * Whether playback can be controlled using the {@link Audio.play | play}(), {@link Audio.pause | pause}() etc. methods. - * @defaultValue `true` - */ - hasPlaybackControl: boolean; - - /** - * Type of the {@link Audio} source. - * @defaultValue 'empty'. - */ - sourceType: string; - - /** - * An {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode | AudioBufferSourceNode} created using - * {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createBufferSource | AudioContext.createBufferSource()}. - */ - source: AudioScheduledSourceNode | null; - - /** - * Represents an array of {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioNode | AudioNodes}. - * Can be used to apply a variety of low-order filters to create more complex sound effects. - * In most cases, the array contains instances of {@link https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode | BiquadFilterNodes}. - * Filters are set via {@link THREE.Audio.setFilter | Audio.setFilter} or {@link THREE.Audio.setFilters | Audio.setFilters}. - * @defaultValue `[]` - */ - filters: AudioNode[]; - - /** - * Return the {@link Audio.gain | gainNode}. - */ - getOutput(): NodeType; - - /** - * Setup the {@link Audio.source | source} to the audioBuffer, and sets {@link Audio.sourceType | sourceType} to 'audioNode'. - * @remarks Also sets {@link Audio.hasPlaybackControl | hasPlaybackControl} to false. - */ - setNodeSource(audioNode: AudioScheduledSourceNode): this; - - /** - * Applies the given object of type {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement | HTMLMediaElement} as the source of this audio. - * @remarks Also sets {@link Audio.hasPlaybackControl | hasPlaybackControl} to false. - */ - setMediaElementSource(mediaElement: HTMLMediaElement): this; - - /** - * Applies the given object of type {@link https://developer.mozilla.org/en-US/docs/Web/API/MediaStream | MediaStream} as the source of this audio. - * @remarks Also sets {@link Audio.hasPlaybackControl | hasPlaybackControl} to false. - */ - setMediaStreamSource(mediaStream: MediaStream): this; - - /** - * Setup the {@link Audio.source | source} to the audioBuffer, and sets {@link Audio.sourceType | sourceType} to 'buffer'. - * @remarks If {@link Audio.autoplay | autoplay}, also starts playback. - */ - setBuffer(audioBuffer: AudioBuffer): this; - - /** - * If {@link Audio.hasPlaybackControl | hasPlaybackControl} is true, starts playback. - */ - play(delay?: number): this; - /** - * If {@link Audio.hasPlaybackControl | hasPlaybackControl} is true, pauses playback. - */ - pause(): this; - /** - * If {@link Audio.hasPlaybackControl | hasPlaybackControl} is enabled, stops playback. - */ - stop(): this; - - /** - * Called automatically when playback finished. - */ - onEnded(): void; - - /** - * Connect to the {@link THREE.Audio.source | Audio.source} - * @remarks This is used internally on initialisation and when setting / removing filters. - */ - connect(): this; - /** - * Disconnect from the {@link THREE.Audio.source | Audio.source} - * @remarks This is used internally when setting / removing filters. - */ - disconnect(): this; - - /** - * Returns the detuning of oscillation in cents. - */ - getDetune(): number; - /** - * Defines the detuning of oscillation in cents. - * @param value Expects a `Float` - */ - setDetune(value: number): this; - - /** - * Returns the first element of the {@link Audio.filters | filters} array. - */ - getFilter(): AudioNode; - /** - * Applies a single filter node to the audio. - */ - setFilter(filter: AudioNode): this; - - /** - * Returns the {@link Audio.filters | filters} array. - */ - getFilters(): AudioNode[]; - /** - * Applies an array of filter nodes to the audio. - * @param value Arrays of filters. - */ - setFilters(value: AudioNode[]): this; - - /** - * Return the value of {@link Audio.playbackRate | playbackRate}. - */ - getPlaybackRate(): number; - /** - * If {@link Audio.hasPlaybackControl | hasPlaybackControl} is enabled, set the {@link Audio.playbackRate | playbackRate} to `value`. - * @param value Expects a `Float` - */ - setPlaybackRate(value: number): this; - - /** - * Return the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop | source.loop} (whether playback should loop). - */ - getLoop(): boolean; - /** - * Set {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop | source.loop} to `value` (whether playback should loop). - * @param value - */ - setLoop(value: boolean): this; - - /** - * Set {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopStart | source.loopStart} to `value`. - * @param value Expects a `Float` - */ - setLoopStart(value: number): this; - /** - * Set {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopEnd | source.loopEnd} to `value`. - * @param value Expects a `Float` - */ - setLoopEnd(value: number): this; - - /** - * Return the current volume. - */ - getVolume(): number; - /** - * Set the volume. - * @param value Expects a `Float` - */ - setVolume(value: number): this; -} diff --git a/src-testing/src/audio/AudioAnalyser.d.ts b/src-testing/src/audio/AudioAnalyser.d.ts deleted file mode 100644 index 474583ec7..000000000 --- a/src-testing/src/audio/AudioAnalyser.d.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { Audio } from "./Audio.js"; - -/** - * Create a {@link AudioAnalyser} object, which uses an {@link https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode | AnalyserNode} to analyse audio data. - * This uses the {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API | Web Audio API}. - * @example - * ```typescript - * // create an AudioListener and add it to the camera - * const listener = new THREE.AudioListener(); - * camera.add(listener); - * // create an Audio source - * const sound = new THREE.Audio(listener); - * // load a sound and set it as the Audio object's buffer - * const audioLoader = new THREE.AudioLoader(); - * audioLoader.load('sounds/ambient.ogg', function (buffer) { - * sound.setBuffer(buffer); - * sound.setLoop(true); - * sound.setVolume(0.5); - * sound.play(); - * }); - * // create an AudioAnalyser, passing in the sound and desired fftSize - * const analyser = new THREE.AudioAnalyser(sound, 32); - * // get the average frequency of the sound - * const data = analyser.getAverageFrequency(); - * ``` - * @see Example: {@link https://threejs.org/examples/#webaudio_sandbox | webaudio / sandbox } - * @see Example: {@link https://threejs.org/examples/#webaudio_visualizer | webaudio / visualizer } - * @see {@link https://threejs.org/docs/index.html#api/en/audio/AudioAnalyser | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/audio/AudioAnalyser.js | Source} - */ -export class AudioAnalyser { - /** - * Create a new {@link {@link AudioAnalyser} | AudioAnalyser}. - * @param audio - * @param fftSize See {@link https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/fftSize | AnalyserNode.fftSize }. Expects a `unsigned integer`. Default `2048`. - */ - constructor(audio: Audio, fftSize?: number); - - /** - * An {@link https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode | AnalyserNode} used to analyze audio. - */ - analyser: AnalyserNode; - - /** - * A Uint8Array with size determined by {@link https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/frequencyBinCount | analyser.frequencyBinCount} used to hold analysis data. - */ - data: Uint8Array; - - /** - * Uses the Web Audio's {@link https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/getByteFrequencyData | getByteFrequencyData} method - */ - getFrequencyData(): Uint8Array; - - /** - * Get the average of the frequencies returned by the {@link AudioAnalyser.getFrequencyData | getFrequencyData} method. - */ - getAverageFrequency(): number; -} diff --git a/src-testing/src/audio/AudioContext.d.ts b/src-testing/src/audio/AudioContext.d.ts deleted file mode 100644 index 50a7f3d6e..000000000 --- a/src-testing/src/audio/AudioContext.d.ts +++ /dev/null @@ -1,19 +0,0 @@ -/** - * This contains methods for setting up an {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext | AudioContext}. - * Used internally by the {@link AudioListener | AudioListener} and {@link AudioLoader | AudioLoader} classes. - * This uses the {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API | Web Audio API}. - * @see {@link https://threejs.org/docs/index.html#api/en/audio/AudioContext | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/audio/AudioContext.js | Source} - */ -export namespace AudioContext { - /** - * Return the value of the variable `context` in the outer scope, if defined, otherwise set it to a new {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext | AudioContext}. - */ - function getContext(): AudioContext; - - /** - * Set the variable `context` in the outer scope to `value`. - * @param value - */ - function setContext(context: AudioContext): void; -} diff --git a/src-testing/src/audio/AudioListener.d.ts b/src-testing/src/audio/AudioListener.d.ts deleted file mode 100644 index a49e4d5c7..000000000 --- a/src-testing/src/audio/AudioListener.d.ts +++ /dev/null @@ -1,96 +0,0 @@ -import { Object3D } from "../core/Object3D.js"; -import { AudioContext } from "./AudioContext.js"; - -/** - * The {@link AudioListener} represents a virtual {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioListener | listener} of the all positional and non-positional audio effects in the scene. - * A three.js application usually creates a single instance of {@link AudioListener} * @remarks - * It is a mandatory construtor parameter for audios entities like {@link Audio | Audio} and {@link PositionalAudio | PositionalAudio}. - * In most cases, the listener object is a child of the camera - * So the 3D transformation of the camera represents the 3D transformation of the listener. - * @example - * ```typescript - * // create an {@link AudioListener} and add it to the camera - * const listener = new THREE.AudioListener(); - * camera.add(listener); - * // create a global audio source - * const sound = new THREE.Audio(listener); - * // load a sound and set it as the Audio object's buffer - * const audioLoader = new THREE.AudioLoader(); - * audioLoader.load('sounds/ambient.ogg', function (buffer) { - * sound.setBuffer(buffer); - * sound.setLoop(true); - * sound.setVolume(0.5); - * sound.play(); - * }); - * ``` - * @see Example: {@link https://threejs.org/examples/#webaudio_sandbox | webaudio / sandbox } - * @see Example: {@link https://threejs.org/examples/#webaudio_timing | webaudio / timing } - * @see Example: {@link https://threejs.org/examples/#webaudio_visualizer | webaudio / visualizer } - * @see {@link https://threejs.org/docs/index.html#api/en/audio/AudioListener | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/audio/AudioListener.js | Source} - */ -export class AudioListener extends Object3D { - /** - * Create a new AudioListener. - */ - constructor(); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `AudioListener` - */ - readonly type: string | "AudioListener"; - - /** - * The {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext | AudioContext} of the {@link {@link AudioListener} | listener} given in the constructor. - */ - context: AudioContext; - - /** - * A {@link https://developer.mozilla.org/en-US/docs/Web/API/GainNode | GainNode} created using - * {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createGain | AudioContext.createGain()}. - */ - gain: GainNode; - - /** - * @defaultValue `null` - */ - filter: AudioNode; - - /** - * Time delta value for audio entities. Use in context of {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioParam/linearRampToValueAtTime | AudioParam.linearRampToValueAtTimeDefault()}. - * @defaultValue `0` - */ - timeDelta: number; - - /** - * Return the {@link AudioListener.gain | gainNode}. - */ - getInput(): GainNode; - /** - * Set the {@link AudioListener.filter | filter} property to `null`. - */ - removeFilter(): this; - - /** - * Returns the value of the {@link AudioListener.filter | filter} property. - */ - getFilter(): AudioNode; - /** - * Set the {@link AudioListener.filter | filter} property to `value`. - * @param value - */ - setFilter(value: AudioNode): this; - - /** - * Return the volume. - */ - getMasterVolume(): number; - - /** - * Set the volume. - * @param value - */ - setMasterVolume(value: number): this; -} diff --git a/src-testing/src/audio/PositionalAudio.d.ts b/src-testing/src/audio/PositionalAudio.d.ts deleted file mode 100644 index d72d10674..000000000 --- a/src-testing/src/audio/PositionalAudio.d.ts +++ /dev/null @@ -1,101 +0,0 @@ -import { Audio } from "./Audio.js"; -import { AudioListener } from "./AudioListener.js"; - -/** - * Create a positional audio object. - * This uses the {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API | Web Audio API}. - * @example - * ```typescript - * // create an AudioListener and add it to the camera - * const listener = new THREE.AudioListener(); - * camera.add(listener); - * // create the {@link PositionalAudio} object (passing in the listener) - * const sound = new THREE.PositionalAudio(listener); - * // load a sound and set it as the {@link PositionalAudio} object's buffer - * const audioLoader = new THREE.AudioLoader(); - * audioLoader.load('sounds/song.ogg', function (buffer) { - * sound.setBuffer(buffer); - * sound.setRefDistance(20); - * sound.play(); - * }); - * // create an object for the sound to play from - * const sphere = new THREE.SphereGeometry(20, 32, 16); - * const material = new THREE.MeshPhongMaterial({ - * color: 0xff2200 - * }); - * const mesh = new THREE.Mesh(sphere, material); - * scene.add(mesh); - * // finally add the sound to the mesh - * mesh.add(sound); - * ``` - * @see Example: {@link https://threejs.org/examples/#webaudio_orientation | webaudio / orientation } - * @see Example: {@link https://threejs.org/examples/#webaudio_sandbox | webaudio / sandbox } - * @see Example: {@link https://threejs.org/examples/#webaudio_timing | webaudio / timing } - * @see {@link https://threejs.org/docs/index.html#api/en/audio/PositionalAudio | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/audio/PositionalAudio.js | Source} - */ -export class PositionalAudio extends Audio { - /** - * Create a new instance of {@link PositionalAudio} - * @param listener (required) {@link AudioListener | AudioListener} instance. - */ - constructor(listener: AudioListener); - - /** - * The PositionalAudio's {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode | PannerNode}. - */ - panner: PannerNode; - - /** - * Returns the {@link PositionalAudio.panner | panner}. - */ - getOutput(): PannerNode; - - /** - * Returns the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/refDistance | panner.refDistance}. - */ - getRefDistance(): number; - /** - * Sets the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/refDistance | panner.refDistance}. - * @param value Expects a `Float` - */ - setRefDistance(value: number): this; - - /** - * Returns the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/rolloffFactor | panner.rolloffFactor}. - */ - getRolloffFactor(): number; - /** - * Sets the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/rolloffFactor | panner.rolloffFactor}. - * @param value Expects a `Float` - */ - setRolloffFactor(value: number): this; - - /** - * Returns the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/distanceModel | panner.distanceModel}. - */ - getDistanceModel(): string; - /** - * Sets the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/distanceModel | panner.distanceModel}. - * @param value - */ - setDistanceModel(value: string): this; - - /** - * Returns the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/maxDistance | panner.maxDistance}. - */ - getMaxDistance(): number; - /** - * Sets the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/maxDistance | panner.maxDistance}. - * @param value Expects a `Float` - */ - setMaxDistance(value: number): this; - - /** - * This method can be used in order to transform an omnidirectional sound into a {@link https://developer.mozilla.org/en-US/docs/Web/API/PannerNode | directional sound}. - * @param coneInnerAngle Expects a `Float` - * @param coneOuterAngle Expects a `Float` - * @param coneOuterGain Expects a `Float` - */ - setDirectionalCone(coneInnerAngle: number, coneOuterAngle: number, coneOuterGain: number): this; -} diff --git a/src-testing/src/cameras/ArrayCamera.d.ts b/src-testing/src/cameras/ArrayCamera.d.ts deleted file mode 100644 index e9e9a221d..000000000 --- a/src-testing/src/cameras/ArrayCamera.d.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { PerspectiveCamera } from "./PerspectiveCamera.js"; - -/** - * {@link ArrayCamera} can be used in order to efficiently render a scene with a predefined set of cameras - * @remarks - * This is an important performance aspect for rendering VR scenes. - * An instance of {@link ArrayCamera} always has an array of sub cameras - * It's mandatory to define for each sub camera the `viewport` property which determines the part of the viewport that is rendered with this camera. - * @see Example: {@link https://threejs.org/examples/#webgl_camera_array | camera / array } - * @see {@link https://threejs.org/docs/index.html#api/en/cameras/ArrayCamera | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/cameras/ArrayCamera.js | Source} - */ -export class ArrayCamera extends PerspectiveCamera { - /** - * An array of cameras. - * @param array. Default `[]`. - */ - constructor(cameras?: PerspectiveCamera[]); - - /** - * Read-only flag to check if a given object is of type {@link ArrayCamera}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isArrayCamera: true; - - /** - * An array of cameras. - * @defaultValue `[]` - */ - cameras: PerspectiveCamera[]; -} diff --git a/src-testing/src/cameras/Camera.d.ts b/src-testing/src/cameras/Camera.d.ts deleted file mode 100644 index b49add52e..000000000 --- a/src-testing/src/cameras/Camera.d.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { CoordinateSystem } from "../constants.js"; -import { Layers } from "../core/Layers.js"; -import { Object3D } from "../core/Object3D.js"; -import { Matrix4 } from "../math/Matrix4.js"; -import { Vector3 } from "../math/Vector3.js"; -import { Vector4 } from "../math/Vector4.js"; - -/** - * Abstract base class for cameras - * @remarks - * This class should always be inherited when you build a new camera. - * @see {@link https://threejs.org/docs/index.html#api/en/cameras/Camera | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/cameras/Camera.js | Source} - */ -export class Camera extends Object3D { - /** - * @remarks - * Note that this class is not intended to be called directly; you probably want a - * {@link THREE.PerspectiveCamera | PerspectiveCamera} or - * {@link THREE.OrthographicCamera | OrthographicCamera} instead. - */ - constructor(); - - /** - * Read-only flag to check if a given object is of type {@link Camera}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isCamera: true; - - /** - * @override - * @defaultValue `Camera` - */ - override readonly type: string | "Camera"; - - /** - * @override - * The {@link THREE.Layers | layers} that the {@link Camera} is a member of. - * @remarks Objects must share at least one layer with the {@link Camera} to be n when the camera's viewpoint is rendered. - * @defaultValue `new THREE.Layers()` - */ - override layers: Layers; - - /** - * This is the inverse of matrixWorld. - * @remarks MatrixWorld contains the Matrix which has the world transform of the {@link Camera} . - * @defaultValue {@link THREE.Matrix4 | `new THREE.Matrix4()`} - */ - matrixWorldInverse: Matrix4; - - /** - * This is the matrix which contains the projection. - * @defaultValue {@link THREE.Matrix4 | `new THREE.Matrix4()`} - */ - projectionMatrix: Matrix4; - - /** - * This is the inverse of projectionMatrix. - * @defaultValue {@link THREE.Matrix4 | `new THREE.Matrix4()`} - */ - projectionMatrixInverse: Matrix4; - - coordinateSystem: CoordinateSystem; - - viewport?: Vector4; - - /** - * Returns a {@link THREE.Vector3 | Vector3} representing the world space direction in which the {@link Camera} is looking. - * @remarks Note: A {@link Camera} looks down its local, negative z-axis. - * @param target The result will be copied into this Vector3. - */ - getWorldDirection(target: Vector3): Vector3; -} diff --git a/src-testing/src/cameras/CubeCamera.d.ts b/src-testing/src/cameras/CubeCamera.d.ts deleted file mode 100644 index e6e82fb19..000000000 --- a/src-testing/src/cameras/CubeCamera.d.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { CoordinateSystem } from "../constants.js"; -import { Object3D } from "../core/Object3D.js"; -import { WebGLCubeRenderTarget } from "../renderers/WebGLCubeRenderTarget.js"; -import { WebGLRenderer } from "../renderers/WebGLRenderer.js"; - -/** - * Creates **6** {@link THREE.PerspectiveCamera | cameras} that render to a {@link THREE.WebGLCubeRenderTarget | WebGLCubeRenderTarget}. - * @remarks The cameras are added to the {@link children} array. - * @example - * ```typescript - * // Create cube render target - * const cubeRenderTarget = new THREE.WebGLCubeRenderTarget( 128, { generateMipmaps: true, minFilter: THREE.LinearMipmapLinearFilter } ); - * - * // Create cube camera - * const cubeCamera = new THREE.CubeCamera( 1, 100000, cubeRenderTarget ); - * scene.add( cubeCamera ); - * - * // Create car - * const chromeMaterial = new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: cubeRenderTarget.texture } ); - * const car = new THREE.Mesh( carGeometry, chromeMaterial ); - * scene.add( car ); - * - * // Update the render target cube - * car.visible = false; - * cubeCamera.position.copy( car.position ); - * cubeCamera.update( renderer, scene ); - * - * // Render the scene - * car.visible = true; - * renderer.render( scene, camera ); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_materials_cubemap_dynamic | materials / cubemap / dynamic } - * @see {@link https://threejs.org/docs/index.html#api/en/cameras/CubeCamera | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/cameras/CubeCamera.js | Source} - */ -export class CubeCamera extends Object3D { - /** - * Constructs a {@link CubeCamera} that contains 6 {@link PerspectiveCamera | PerspectiveCameras} that render to a {@link THREE.WebGLCubeRenderTarget | WebGLCubeRenderTarget}. - * @param near The near clipping distance. - * @param far The far clipping distance. - * @param renderTarget The destination cube render target. - */ - constructor(near: number, far: number, renderTarget: WebGLCubeRenderTarget); - - /** - * @override - * @defaultValue `CubeCamera` - */ - override readonly type: string | "CubeCamera"; - - /** - * The destination cube render target. - */ - renderTarget: WebGLCubeRenderTarget; - - coordinateSystem: CoordinateSystem; - - activeMipmapLevel: number; - - updateCoordinateSystem(): void; - - /** - * Call this to update the {@link CubeCamera.renderTarget | renderTarget}. - * @param renderer The current WebGL renderer - * @param scene The current scene - */ - update(renderer: WebGLRenderer, scene: Object3D): void; -} diff --git a/src-testing/src/cameras/OrthographicCamera.d.ts b/src-testing/src/cameras/OrthographicCamera.d.ts deleted file mode 100644 index 745d11416..000000000 --- a/src-testing/src/cameras/OrthographicCamera.d.ts +++ /dev/null @@ -1,174 +0,0 @@ -import { JSONMeta, Object3DJSON, Object3DJSONObject } from "../core/Object3D.js"; -import { Camera } from "./Camera.js"; - -export interface OrthographicCameraJSONObject extends Object3DJSONObject { - zoom: number; - left: number; - right: number; - top: number; - bottom: number; - near: number; - far: number; - - view?: { - enabled: boolean; - fullWidth: number; - fullHeight: number; - offsetX: number; - offsetY: number; - width: number; - height: number; - }; -} - -export interface OrthographicCameraJSON extends Object3DJSON { - object: OrthographicCameraJSONObject; -} - -/** - * Camera that uses {@link https://en.wikipedia.org/wiki/Orthographic_projection | orthographic projection}. - * In this projection mode, an object's size in the rendered image stays constant regardless of its distance from the camera. - * This can be useful for rendering 2D scenes and UI elements, amongst other things. - * @example - * ```typescript - * const camera = new THREE.OrthographicCamera(width / -2, width / 2, height / 2, height / -2, 1, 1000); - * scene.add(camera); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_camera | camera } - * @see Example: {@link https://threejs.org/examples/#webgl_interactive_cubes_ortho | interactive / cubes / ortho } - * @see Example: {@link https://threejs.org/examples/#webgl_materials_cubemap_dynamic | materials / cubemap / dynamic } - * @see Example: {@link https://threejs.org/examples/#webgl_postprocessing_advanced | postprocessing / advanced } - * @see Example: {@link https://threejs.org/examples/#webgl_postprocessing_dof2 | postprocessing / dof2 } - * @see Example: {@link https://threejs.org/examples/#webgl_postprocessing_godrays | postprocessing / godrays } - * @see Example: {@link https://threejs.org/examples/#webgl_rtt | rtt } - * @see Example: {@link https://threejs.org/examples/#webgl_shaders_tonemapping | shaders / tonemapping } - * @see Example: {@link https://threejs.org/examples/#webgl_shadowmap | shadowmap } - * @see {@link https://threejs.org/docs/index.html#api/en/cameras/OrthographicCamera | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/cameras/OrthographicCamera.js | Source} - */ -export class OrthographicCamera extends Camera { - /** - * Creates a new {@link OrthographicCamera}. - * @remarks Together these define the camera's {@link https://en.wikipedia.org/wiki/Viewing_frustum | viewing frustum}. - * @param left Camera frustum left plane. Default `-1`. - * @param right Camera frustum right plane. Default `1`. - * @param top Camera frustum top plane. Default `1`. - * @param bottom Camera frustum bottom plane. Default `-1`. - * @param near Camera frustum near plane. Default `0.1`. - * @param far Camera frustum far plane. Default `2000`. - */ - constructor(left?: number, right?: number, top?: number, bottom?: number, near?: number, far?: number); - - /** - * Read-only flag to check if a given object is of type {@link OrthographicCamera}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isOrthographicCamera: true; - - /** - * @override - * @defaultValue `OrthographicCamera` - */ - override readonly type: string | "OrthographicCamera"; - - /** - * Gets or sets the zoom factor of the camera. - * @defaultValue `1` - */ - zoom: number; - - /** - * Set by {@link setViewOffset | .setViewOffset()}. - * @defaultValue `null` - */ - view: null | { - enabled: boolean; - fullWidth: number; - fullHeight: number; - offsetX: number; - offsetY: number; - width: number; - height: number; - }; - - /** - * Camera frustum left plane. - * @remarks Expects a `Float` - * @defaultValue `-1` - */ - left: number; - - /** - * Camera frustum right plane. - * @remarks Expects a `Float` - * @defaultValue `1` - */ - right: number; - - /** - * Camera frustum top plane. - * @remarks Expects a `Float` - * @defaultValue `1` - */ - top: number; - - /** - * Camera frustum bottom plane. - * @remarks Expects a `Float`. - * @defaultValue `-1` - */ - bottom: number; - - /** - * Camera frustum near plane.`. - * @remarks The valid range is between `0` and the current value of the {@link far | .far} plane. - * @remarks Note that, unlike for the {@link THREE.PerspectiveCamera | PerspectiveCamera}, `0` is a valid value for an {@link THREE.OrthographicCamera | OrthographicCamera's} near plane. - * @remarks Expects a `Float` - * @defaultValue `0.1` - */ - near: number; - - /** - * Camera frustum far plane. - * @remarks Must be greater than the current value of {@link near | .near} plane. - * @remarks Expects a `Float` - * @defaultValue `2000` - */ - far: number; - - /** - * Updates the camera projection matrix - * @remarks Must be called after any change of parameters. - */ - updateProjectionMatrix(): void; - - /** - * Sets an offset in a larger {@link https://en.wikipedia.org/wiki/Viewing_frustum | viewing frustum} - * @remarks - * This is useful for multi-window or multi-monitor/multi-machine setups - * For an example on how to use it see {@link PerspectiveCamera.setViewOffset | PerspectiveCamera}. - * @see {@link THREE.PerspectiveCamera.setViewOffset | PerspectiveCamera}. - * @param fullWidth Full width of multiview setup Expects a `Float`. - * @param fullHeight Full height of multiview setup Expects a `Float`. - * @param x Horizontal offset of subcamera Expects a `Float`. - * @param y Vertical offset of subcamera Expects a `Float`. - * @param width Width of subcamera Expects a `Float`. - * @param height Height of subcamera Expects a `Float`. - */ - setViewOffset( - fullWidth: number, - fullHeight: number, - offsetX: number, - offsetY: number, - width: number, - height: number, - ): void; - - /** - * Removes any offset set by the {@link setViewOffset | .setViewOffset} method. - */ - clearViewOffset(): void; - - toJSON(meta?: JSONMeta): OrthographicCameraJSON; -} diff --git a/src-testing/src/cameras/PerspectiveCamera.d.ts b/src-testing/src/cameras/PerspectiveCamera.d.ts deleted file mode 100644 index 60567105f..000000000 --- a/src-testing/src/cameras/PerspectiveCamera.d.ts +++ /dev/null @@ -1,254 +0,0 @@ -import { JSONMeta, Object3DJSON, Object3DJSONObject } from "../core/Object3D.js"; -import { Vector2 } from "../math/Vector2.js"; -import { Camera } from "./Camera.js"; - -export interface PerspectiveCameraJSONObject extends Object3DJSONObject { - fov: number; - zoom: number; - - near: number; - far: number; - focus: number; - - aspect: number; - - view?: { - enabled: boolean; - fullWidth: number; - fullHeight: number; - offsetX: number; - offsetY: number; - width: number; - height: number; - }; - - filmGauge: number; - filmOffset: number; -} - -export interface PerspectiveCameraJSON extends Object3DJSON { - object: PerspectiveCameraJSONObject; -} - -/** - * Camera that uses {@link https://en.wikipedia.org/wiki/Perspective_(graphical) | perspective projection}. - * This projection mode is designed to mimic the way the human eye sees - * @remarks - * It is the most common projection mode used for rendering a 3D scene. - * @example - * ```typescript - * const camera = new THREE.PerspectiveCamera(45, width / height, 1, 1000); - * scene.add(camera); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_animation_skinning_blending | animation / skinning / blending } - * @see Example: {@link https://threejs.org/examples/#webgl_animation_skinning_morph | animation / skinning / morph } - * @see Example: {@link https://threejs.org/examples/#webgl_effects_stereo | effects / stereo } - * @see Example: {@link https://threejs.org/examples/#webgl_interactive_cubes | interactive / cubes } - * @see Example: {@link https://threejs.org/examples/#webgl_loader_collada_skinning | loader / collada / skinning } - * @see {@link https://threejs.org/docs/index.html#api/en/cameras/PerspectiveCamera | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/cameras/PerspectiveCamera.js | Source} - */ -export class PerspectiveCamera extends Camera { - /** - * Creates a new {@link PerspectiveCamera}. - * @remarks Together these define the camera's {@link https://en.wikipedia.org/wiki/Viewing_frustum | viewing frustum}. - * @param fov Camera frustum vertical field of view. Default `50`. - * @param aspect Camera frustum aspect ratio. Default `1`. - * @param near Camera frustum near plane. Default `0.1`. - * @param far Camera frustum far plane. Default `2000`. - */ - constructor(fov?: number, aspect?: number, near?: number, far?: number); - - /** - * Read-only flag to check if a given object is of type {@link Camera}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isPerspectiveCamera: true; - - /** - * @override - * @defaultValue `PerspectiveCamera` - */ - override readonly type: string | "PerspectiveCamera"; - - /** - * Gets or sets the zoom factor of the camera. - * @defaultValue `1` - */ - zoom: number; - - /** - * Camera frustum vertical field of view, from bottom to top of view, in degrees. - * @remarks Expects a `Float` - * @defaultValue `50` - */ - fov: number; - - /** - * Camera frustum aspect ratio, usually the canvas width / canvas height. - * @remarks Expects a `Float` - * @defaultValue `1`, _(square canvas)_. - */ - aspect: number; - - /** - * Camera frustum near plane. - * @remarks The valid range is greater than `0` and less than the current value of the {@link far | .far} plane. - * @remarks Note that, unlike for the {@link THREE.OrthographicCamera | OrthographicCamera}, `0` is **not** a valid value for a {@link PerspectiveCamera |PerspectiveCamera's}. near plane. - * @defaultValue `0.1` - * @remarks Expects a `Float` - */ - near: number; - - /** - * Camera frustum far plane. - * @remarks Must be greater than the current value of {@link near | .near} plane. - * @remarks Expects a `Float` - * @defaultValue `2000` - */ - far: number; - - /** - * Object distance used for stereoscopy and depth-of-field effects. - * @remarks This parameter does not influence the projection matrix unless a {@link THREE.StereoCamera | StereoCamera} is being used. - * @remarks Expects a `Float` - * @defaultValue `10` - */ - focus: number; - - /** - * Frustum window specification or null. - * This is set using the {@link setViewOffset | .setViewOffset} method and cleared using {@link clearViewOffset | .clearViewOffset}. - * @defaultValue `null` - */ - view: null | { - enabled: boolean; - fullWidth: number; - fullHeight: number; - offsetX: number; - offsetY: number; - width: number; - height: number; - }; - - /** - * Film size used for the larger axis. - * This parameter does not influence the projection matrix unless {@link filmOffset | .filmOffset} is set to a nonzero value. - * @remarks Expects a `Float` - * @defaultValue `35`, _millimeters_. - */ - filmGauge: number; - - /** - * Horizontal off-center offset in the same unit as {@link filmGauge | .filmGauge}. - * @remarks Expects a `Float` - * @defaultValue `0` - */ - filmOffset: number; - - /** - * Returns the focal length of the current {@link .fov | fov} in respect to {@link filmGauge | .filmGauge}. - */ - getFocalLength(): number; - - /** - * Sets the FOV by focal length in respect to the current {@link filmGauge | .filmGauge}. - * @remarks By default, the focal length is specified for a `35mm` (full frame) camera. - * @param focalLength Expects a `Float` - */ - setFocalLength(focalLength: number): void; - - /** - * Returns the current vertical field of view angle in degrees considering {@link zoom | .zoom}. - */ - getEffectiveFOV(): number; - - /** - * Returns the width of the image on the film - * @remarks - * If {@link aspect | .aspect}. is greater than or equal to one (landscape format), the result equals {@link filmGauge | .filmGauge}. - */ - getFilmWidth(): number; - - /** - * Returns the height of the image on the film - * @remarks - * If {@link aspect | .aspect}. is less than or equal to one (portrait format), the result equals {@link filmGauge | .filmGauge}. - */ - getFilmHeight(): number; - - /** - * Computes the 2D bounds of the camera's viewable rectangle at a given distance along the viewing direction. - * Sets minTarget and maxTarget to the coordinates of the lower-left and upper-right corners of the view rectangle. - */ - getViewBounds(distance: number, minTarget: Vector2, maxTarget: Vector2): void; - - /** - * Computes the width and height of the camera's viewable rectangle at a given distance along the viewing direction. - * Copies the result into the target Vector2, where x is width and y is height. - */ - getViewSize(distance: number, target: Vector2): Vector2; - - /** - * Sets an offset in a larger frustum. - * @remarks - * This is useful for multi-window or multi-monitor/multi-machine setups. - * - * For example, if you have 3x2 monitors and each monitor is _1920x1080_ and - * the monitors are in grid like this - * ``` - * ┌───┬───┬───┐ - * │ A │ B │ C │ - * ├───┼───┼───┤ - * │ D │ E │ F │ - * └───┴───┴───┘ - * ``` - * then for each monitor you would call it like this - * ```typescript - * const w = 1920; - * const h = 1080; - * const fullWidth = w * 3; - * const fullHeight = h * 2; - * - * // Monitor - A - * camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 0, w, h ); - * // Monitor - B - * camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 0, w, h ); - * // Monitor - C - * camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 0, w, h ); - * // Monitor - D - * camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 1, w, h ); - * // Monitor - E - * camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 1, w, h ); - * // Monitor - F - * camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 1, w, h ); - * ``` - * Note there is no reason monitors have to be the same size or in a grid. - * @param fullWidth Full width of multiview setup Expects a `Float`. - * @param fullHeight Full height of multiview setup Expects a `Float`. - * @param x Horizontal offset of subcamera Expects a `Float`. - * @param y Vertical offset of subcamera Expects a `Float`. - * @param width Width of subcamera Expects a `Float`. - * @param height Height of subcamera Expects a `Float`. - */ - setViewOffset(fullWidth: number, fullHeight: number, x: number, y: number, width: number, height: number): void; - - /** - * Removes any offset set by the {@link setViewOffset | .setViewOffset} method. - */ - clearViewOffset(): void; - - /** - * Updates the camera projection matrix - * @remarks Must be called after any change of parameters. - */ - updateProjectionMatrix(): void; - - /** - * @deprecated Use {@link PerspectiveCamera.setFocalLength | .setFocalLength()} and {@link PerspectiveCamera.filmGauge | .filmGauge} instead. - */ - setLens(focalLength: number, frameHeight?: number): void; - - toJSON(meta?: JSONMeta): PerspectiveCameraJSON; -} diff --git a/src-testing/src/cameras/StereoCamera.d.ts b/src-testing/src/cameras/StereoCamera.d.ts deleted file mode 100644 index 3f359e3e0..000000000 --- a/src-testing/src/cameras/StereoCamera.d.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { Camera } from "./Camera.js"; -import { PerspectiveCamera } from "./PerspectiveCamera.js"; - -/** - * Dual {@link PerspectiveCamera | PerspectiveCamera}s used for effects such as - * {@link https://en.wikipedia.org/wiki/Anaglyph_3D | 3D Anaglyph} or - * {@link https://en.wikipedia.org/wiki/parallax_barrier | Parallax Barrier}. - * @see Example: {@link https://threejs.org/examples/#webgl_effects_anaglyph | effects / anaglyph } - * @see Example: {@link https://threejs.org/examples/#webgl_effects_parallaxbarrier | effects / parallaxbarrier } - * @see Example: {@link https://threejs.org/examples/#webgl_effects_stereo | effects / stereo } - * @see {@link https://threejs.org/docs/index.html#api/en/cameras/StereoCamera | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/cameras/StereoCamera.js | Source} - */ -export class StereoCamera extends Camera { - constructor(); - - type: "StereoCamera"; - - /** - * @remarks Expects a `Float` - * @defaultValue `1` - */ - aspect: number; - - /** - * @remarks Expects a `Float` - * @defaultValue `0.064` - */ - eyeSep: number; - - /** - * The Left camera. - * A {@link PerspectiveCamera } added to {@link THREE.PerspectiveCamera.layers | layer 1} - * @remarks Objects to be rendered by the **left** camera must also be added to this layer. - */ - cameraL: PerspectiveCamera; - - /** - * The Right camera. - * A {@link PerspectiveCamera } added to {@link THREE.PerspectiveCamera.layers | layer 2} - * @remarks Objects to be rendered by the **right** camera must also be added to this layer. - */ - cameraR: PerspectiveCamera; - - /** - * Update the stereo cameras based on the camera passed in. - * @param camera - */ - update(camera: PerspectiveCamera): void; -} diff --git a/src-testing/src/constants.d.ts b/src-testing/src/constants.d.ts deleted file mode 100644 index 26ee6e2a5..000000000 --- a/src-testing/src/constants.d.ts +++ /dev/null @@ -1,926 +0,0 @@ -export const REVISION: string; - -// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent.button -export enum MOUSE { - LEFT = 0, - MIDDLE = 1, - RIGHT = 2, - ROTATE = 0, - DOLLY = 1, - PAN = 2, -} - -export enum TOUCH { - ROTATE = 0, - PAN = 1, - DOLLY_PAN = 2, - DOLLY_ROTATE = 3, -} - -// GL STATE CONSTANTS -export const CullFaceNone: 0; -export const CullFaceBack: 1; -export const CullFaceFront: 2; -export const CullFaceFrontBack: 3; -export type CullFace = typeof CullFaceNone | typeof CullFaceBack | typeof CullFaceFront | typeof CullFaceFrontBack; - -// Shadowing Type -export const BasicShadowMap: 0; -export const PCFShadowMap: 1; -export const PCFSoftShadowMap: 2; -export const VSMShadowMap: 3; -export type ShadowMapType = typeof BasicShadowMap | typeof PCFShadowMap | typeof PCFSoftShadowMap | typeof VSMShadowMap; - -// MATERIAL CONSTANTS - -// side -export const FrontSide: 0; -export const BackSide: 1; -export const DoubleSide: 2; -/** - * Defines which side of faces will be rendered - front, back or both. - * Default is {@link FrontSide}. - */ -export type Side = typeof FrontSide | typeof BackSide | typeof DoubleSide; - -// blending modes -export const NoBlending: 0; -export const NormalBlending: 1; -export const AdditiveBlending: 2; -export const SubtractiveBlending: 3; -export const MultiplyBlending: 4; -export const CustomBlending: 5; -export type Blending = - | typeof NoBlending - | typeof NormalBlending - | typeof AdditiveBlending - | typeof SubtractiveBlending - | typeof MultiplyBlending - | typeof CustomBlending; - -// custom blending equations -// (numbers start from 100 not to clash with other -// mappings to OpenGL constants defined in Texture.js) -export const AddEquation: 100; -export const SubtractEquation: 101; -export const ReverseSubtractEquation: 102; -export const MinEquation: 103; -export const MaxEquation: 104; -export type BlendingEquation = - | typeof AddEquation - | typeof SubtractEquation - | typeof ReverseSubtractEquation - | typeof MinEquation - | typeof MaxEquation; - -// custom blending factors -export const ZeroFactor: 200; -export const OneFactor: 201; -export const SrcColorFactor: 202; -export const OneMinusSrcColorFactor: 203; -export const SrcAlphaFactor: 204; -export const OneMinusSrcAlphaFactor: 205; -export const DstAlphaFactor: 206; -export const OneMinusDstAlphaFactor: 207; -export const DstColorFactor: 208; -export const OneMinusDstColorFactor: 209; -export const SrcAlphaSaturateFactor: 210; -export const ConstantColorFactor: 211; -export const OneMinusConstantColorFactor: 212; -export const ConstantAlphaFactor: 213; -export const OneMinusConstantAlphaFactor: 214; -export type BlendingDstFactor = - | typeof ZeroFactor - | typeof OneFactor - | typeof SrcColorFactor - | typeof OneMinusSrcColorFactor - | typeof SrcAlphaFactor - | typeof OneMinusSrcAlphaFactor - | typeof DstAlphaFactor - | typeof OneMinusDstAlphaFactor - | typeof DstColorFactor - | typeof OneMinusDstColorFactor - | typeof ConstantColorFactor - | typeof OneMinusConstantColorFactor - | typeof ConstantAlphaFactor - | typeof OneMinusConstantAlphaFactor; -export type BlendingSrcFactor = BlendingDstFactor | typeof SrcAlphaSaturateFactor; - -// depth modes -export const NeverDepth: 0; -export const AlwaysDepth: 1; -export const LessDepth: 2; -export const LessEqualDepth: 3; -export const EqualDepth: 4; -export const GreaterEqualDepth: 5; -export const GreaterDepth: 6; -export const NotEqualDepth: 7; -export type DepthModes = - | typeof NeverDepth - | typeof AlwaysDepth - | typeof LessDepth - | typeof LessEqualDepth - | typeof EqualDepth - | typeof GreaterEqualDepth - | typeof GreaterDepth - | typeof NotEqualDepth; - -// TEXTURE CONSTANTS -// Operations -export const MultiplyOperation: 0; -export const MixOperation: 1; -export const AddOperation: 2; -export type Combine = typeof MultiplyOperation | typeof MixOperation | typeof AddOperation; - -// Tone Mapping modes -export const NoToneMapping: 0; -export const LinearToneMapping: 1; -export const ReinhardToneMapping: 2; -export const CineonToneMapping: 3; -export const ACESFilmicToneMapping: 4; -export const CustomToneMapping: 5; -export const AgXToneMapping: 6; -export const NeutralToneMapping: 7; -export type ToneMapping = - | typeof NoToneMapping - | typeof LinearToneMapping - | typeof ReinhardToneMapping - | typeof CineonToneMapping - | typeof ACESFilmicToneMapping - | typeof CustomToneMapping - | typeof AgXToneMapping - | typeof NeutralToneMapping; - -// Bind modes -export const AttachedBindMode: "attached"; -export const DetachedBindMode: "detached"; -export type BindMode = typeof AttachedBindMode | typeof DetachedBindMode; - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// -// Mapping modes - -/** - * Maps the texture using the mesh's UV coordinates. - * @remarks This is the _default_ value and behaver for Texture Mapping. - */ -export const UVMapping: 300; - -/** - * @remarks This is the _default_ value and behaver for Cube Texture Mapping. - */ -export const CubeReflectionMapping: 301; -export const CubeRefractionMapping: 302; -export const CubeUVReflectionMapping: 306; - -export const EquirectangularReflectionMapping: 303; -export const EquirectangularRefractionMapping: 304; - -/** - * Texture Mapping Modes for non-cube Textures - * @remarks {@link UVMapping} is the _default_ value and behaver for Texture Mapping. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - */ -export type Mapping = - | typeof UVMapping - | typeof EquirectangularReflectionMapping - | typeof EquirectangularRefractionMapping; - -/** - * Texture Mapping Modes for cube Textures - * @remarks {@link CubeReflectionMapping} is the _default_ value and behaver for Cube Texture Mapping. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - */ -export type CubeTextureMapping = - | typeof CubeReflectionMapping - | typeof CubeRefractionMapping - | typeof CubeUVReflectionMapping; - -/** - * Texture Mapping Modes for any type of Textures - * @see {@link Mapping} and {@link CubeTextureMapping} - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - */ -export type AnyMapping = Mapping | CubeTextureMapping; - -/////////////////////////////////////////////////////////////////////////////// -// Wrapping modes - -/** With {@link RepeatWrapping} the texture will simply repeat to infinity. */ -export const RepeatWrapping: 1000; -/** - * With {@link ClampToEdgeWrapping} the last pixel of the texture stretches to the edge of the mesh. - * @remarks This is the _default_ value and behaver for Wrapping Mapping. - */ -export const ClampToEdgeWrapping: 1001; -/** With {@link MirroredRepeatWrapping} the texture will repeats to infinity, mirroring on each repeat. */ -export const MirroredRepeatWrapping: 1002; - -/** - * Texture Wrapping Modes - * @remarks {@link ClampToEdgeWrapping} is the _default_ value and behaver for Wrapping Mapping. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - */ -export type Wrapping = typeof RepeatWrapping | typeof ClampToEdgeWrapping | typeof MirroredRepeatWrapping; - -/////////////////////////////////////////////////////////////////////////////// -// Filters - -/** {@link NearestFilter} returns the value of the texture element that is nearest (in Manhattan distance) to the specified texture coordinates. */ -export const NearestFilter: 1003; - -/** - * {@link NearestMipmapNearestFilter} chooses the mipmap that most closely matches the size of the pixel being textured - * and uses the {@link NearestFilter} criterion (the texel nearest to the center of the pixel) to produce a texture value. - */ -export const NearestMipmapNearestFilter: 1004; -/** - * {@link NearestMipmapNearestFilter} chooses the mipmap that most closely matches the size of the pixel being textured - * and uses the {@link NearestFilter} criterion (the texel nearest to the center of the pixel) to produce a texture value. - */ -export const NearestMipMapNearestFilter: 1004; - -/** - * {@link NearestMipmapLinearFilter} chooses the two mipmaps that most closely match the size of the pixel being textured - * and uses the {@link NearestFilter} criterion to produce a texture value from each mipmap. - * The final texture value is a weighted average of those two values. - */ -export const NearestMipmapLinearFilter: 1005; -/** - * {@link NearestMipMapLinearFilter} chooses the two mipmaps that most closely match the size of the pixel being textured - * and uses the {@link NearestFilter} criterion to produce a texture value from each mipmap. - * The final texture value is a weighted average of those two values. - */ -export const NearestMipMapLinearFilter: 1005; - -/** - * {@link LinearFilter} returns the weighted average of the four texture elements that are closest to the specified texture coordinates, - * and can include items wrapped or repeated from other parts of a texture, - * depending on the values of {@link THREE.Texture.wrapS | wrapS} and {@link THREE.Texture.wrapT | wrapT}, and on the exact mapping. - */ -export const LinearFilter: 1006; - -/** - * {@link LinearMipmapNearestFilter} chooses the mipmap that most closely matches the size of the pixel being textured and - * uses the {@link LinearFilter} criterion (a weighted average of the four texels that are closest to the center of the pixel) to produce a texture value. - */ -export const LinearMipmapNearestFilter: 1007; -/** - * {@link LinearMipMapNearestFilter} chooses the mipmap that most closely matches the size of the pixel being textured and - * uses the {@link LinearFilter} criterion (a weighted average of the four texels that are closest to the center of the pixel) to produce a texture value. - */ -export const LinearMipMapNearestFilter: 1007; - -/** - * {@link LinearMipmapLinearFilter} is the default and chooses the two mipmaps that most closely match the size of the pixel being textured and - * uses the {@link LinearFilter} criterion to produce a texture value from each mipmap. - * The final texture value is a weighted average of those two values. - */ -export const LinearMipmapLinearFilter: 1008; - -/** - * {@link LinearMipMapLinearFilter} is the default and chooses the two mipmaps that most closely match the size of the pixel being textured and - * uses the {@link LinearFilter} criterion to produce a texture value from each mipmap. - * The final texture value is a weighted average of those two values. - */ -export const LinearMipMapLinearFilter: 1008; - -/** - * Texture Magnification Filter Modes. - * For use with a texture's {@link THREE.Texture.magFilter | magFilter} property, - * these define the texture magnification function to be used when the pixel being textured maps to an area less than or equal to one texture element (texel). - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - * @see {@link https://sbcode.net/threejs/mipmaps/ | Texture Mipmaps (non-official)} - */ -export type MagnificationTextureFilter = typeof NearestFilter | typeof LinearFilter; - -/** - * Texture Minification Filter Modes. - * For use with a texture's {@link THREE.Texture.minFilter | minFilter} property, - * these define the texture minifying function that is used whenever the pixel being textured maps to an area greater than one texture element (texel). - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - * @see {@link https://sbcode.net/threejs/mipmaps/ | Texture Mipmaps (non-official)} - */ -export type MinificationTextureFilter = - | typeof NearestFilter - | typeof NearestMipmapNearestFilter - | typeof NearestMipMapNearestFilter - | typeof NearestMipmapLinearFilter - | typeof NearestMipMapLinearFilter - | typeof LinearFilter - | typeof LinearMipmapNearestFilter - | typeof LinearMipMapNearestFilter - | typeof LinearMipmapLinearFilter - | typeof LinearMipMapLinearFilter; - -/** - * Texture all Magnification and Minification Filter Modes. - * @see {@link MagnificationTextureFilter} and {@link MinificationTextureFilter} - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - * @see {@link https://sbcode.net/threejs/mipmaps/ | Texture Mipmaps (non-official)} - */ -export type TextureFilter = MagnificationTextureFilter | MinificationTextureFilter; - -/////////////////////////////////////////////////////////////////////////////// -// Data types - -export const UnsignedByteType: 1009; -export const ByteType: 1010; -export const ShortType: 1011; -export const UnsignedShortType: 1012; -export const IntType: 1013; -export const UnsignedIntType: 1014; -export const FloatType: 1015; -export const HalfFloatType: 1016; -export const UnsignedShort4444Type: 1017; -export const UnsignedShort5551Type: 1018; -export const UnsignedInt248Type: 1020; -export const UnsignedInt5999Type: 35902; - -export type AttributeGPUType = typeof FloatType | typeof IntType; - -/** - * Texture Types. - * @remarks Must correspond to the correct {@link PixelFormat | format}. - * @see {@link THREE.Texture.type} - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - */ -export type TextureDataType = - | typeof UnsignedByteType - | typeof ByteType - | typeof ShortType - | typeof UnsignedShortType - | typeof IntType - | typeof UnsignedIntType - | typeof FloatType - | typeof HalfFloatType - | typeof UnsignedShort4444Type - | typeof UnsignedShort5551Type - | typeof UnsignedInt248Type - | typeof UnsignedInt5999Type; - -/////////////////////////////////////////////////////////////////////////////// -// Pixel formats - -/** {@link AlphaFormat} discards the red, green and blue components and reads just the alpha component. */ -export const AlphaFormat: 1021; - -export const RGBFormat: 1022; - -/** {@link RGBAFormat} is the default and reads the red, green, blue and alpha components. */ -export const RGBAFormat: 1023; - -/** - * {@link LuminanceFormat} reads each element as a single luminance component. - * This is then converted to a floating point, clamped to the range `[0,1]`, and then assembled into an RGBA element by - * placing the luminance value in the red, green and blue channels, and attaching `1.0` to the alpha channel. - */ -export const LuminanceFormat: 1024; - -/** - * {@link LuminanceAlphaFormat} reads each element as a luminance/alpha double. - * The same process occurs as for the {@link LuminanceFormat}, except that the alpha channel may have values other than `1.0`. - */ -export const LuminanceAlphaFormat: 1025; - -/** - * {@link DepthFormat} reads each element as a single depth value, converts it to floating point, and clamps to the range `[0,1]`. - * @remarks This is the default for {@link THREE.DepthTexture}. - */ -export const DepthFormat: 1026; - -/** - * {@link DepthStencilFormat} reads each element is a pair of depth and stencil values. - * The depth component of the pair is interpreted as in {@link DepthFormat}. - * The stencil component is interpreted based on the depth + stencil internal format. - */ -export const DepthStencilFormat: 1027; - -/** - * {@link RedFormat} discards the green and blue components and reads just the red component. - */ -export const RedFormat: 1028; - -/** - * {@link RedIntegerFormat} discards the green and blue components and reads just the red component. - * The texels are read as integers instead of floating point. - */ -export const RedIntegerFormat: 1029; - -/** - * {@link RGFormat} discards the alpha, and blue components and reads the red, and green components. - */ -export const RGFormat: 1030; - -/** - * {@link RGIntegerFormat} discards the alpha, and blue components and reads the red, and green components. - * The texels are read as integers instead of floating point. - */ -export const RGIntegerFormat: 1031; - -/** - * {@link RGBIntegerFormat} discrads the alpha components and reads the red, green, and blue components. - */ -export const RGBIntegerFormat: 1032; - -/** - * {@link RGBAIntegerFormat} reads the red, green, blue and alpha component - * @remarks This is the default for {@link THREE.Texture}. - */ -export const RGBAIntegerFormat: 1033; - -/** - * All Texture Pixel Formats Modes. - * @remarks Note that the texture must have the correct {@link THREE.Texture.type} set, as described in {@link TextureDataType}. - * @see {@link WebGLRenderingContext.texImage2D} for details. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - */ -export type PixelFormat = - | typeof AlphaFormat - | typeof RGBFormat - | typeof RGBAFormat - | typeof LuminanceFormat - | typeof LuminanceAlphaFormat - | typeof DepthFormat - | typeof DepthStencilFormat - | typeof RedFormat - | typeof RedIntegerFormat - | typeof RGFormat - | typeof RGIntegerFormat - | typeof RGBIntegerFormat - | typeof RGBAIntegerFormat; - -/** - * All Texture Pixel Formats Modes for {@link THREE.DepthTexture}. - * @see {@link WebGLRenderingContext.texImage2D} for details. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - */ -export type DepthTexturePixelFormat = typeof DepthFormat | typeof DepthStencilFormat; - -/////////////////////////////////////////////////////////////////////////////// -// Compressed texture formats -// DDS / ST3C Compressed texture formats - -/** - * A DXT1-compressed image in an RGB image format. - * @remarks Require support for the _WEBGL_compressed_texture_s3tc_ WebGL extension. - */ -export const RGB_S3TC_DXT1_Format: 33776; -/** - * A DXT1-compressed image in an RGB image format with a simple on/off alpha value. - * @remarks Require support for the _WEBGL_compressed_texture_s3tc_ WebGL extension. - */ -export const RGBA_S3TC_DXT1_Format: 33777; -/** - * A DXT3-compressed image in an RGBA image format. Compared to a 32-bit RGBA texture, it offers 4:1 compression. - * @remarks Require support for the _WEBGL_compressed_texture_s3tc_ WebGL extension. - */ -export const RGBA_S3TC_DXT3_Format: 33778; -/** - * A DXT5-compressed image in an RGBA image format. It also provides a 4:1 compression, but differs to the DXT3 compression in how the alpha compression is done. - * @remarks Require support for the _WEBGL_compressed_texture_s3tc_ WebGL extension. - */ -export const RGBA_S3TC_DXT5_Format: 33779; - -// PVRTC compressed './texture formats - -/** - * RGB compression in 4-bit mode. One block for each 4×4 pixels. - * @remarks Require support for the _WEBGL_compressed_texture_pvrtc_ WebGL extension. - */ -export const RGB_PVRTC_4BPPV1_Format: 35840; -/** - * RGB compression in 2-bit mode. One block for each 8×4 pixels. - * @remarks Require support for the _WEBGL_compressed_texture_pvrtc_ WebGL extension. - */ -export const RGB_PVRTC_2BPPV1_Format: 35841; -/** - * RGBA compression in 4-bit mode. One block for each 4×4 pixels. - * @remarks Require support for the _WEBGL_compressed_texture_pvrtc_ WebGL extension. - */ -export const RGBA_PVRTC_4BPPV1_Format: 35842; -/** - * RGBA compression in 2-bit mode. One block for each 8×4 pixels. - * @remarks Require support for the _WEBGL_compressed_texture_pvrtc_ WebGL extension. - */ -export const RGBA_PVRTC_2BPPV1_Format: 35843; - -// ETC compressed texture formats - -/** - * @remarks Require support for the _WEBGL_compressed_texture_etc1_ (ETC1) or _WEBGL_compressed_texture_etc_ (ETC2) WebGL extension. - */ -export const RGB_ETC1_Format: 36196; -/** - * @remarks Require support for the _WEBGL_compressed_texture_etc1_ (ETC1) or _WEBGL_compressed_texture_etc_ (ETC2) WebGL extension. - */ -export const RGB_ETC2_Format: 37492; -/** - * @remarks Require support for the _WEBGL_compressed_texture_etc1_ (ETC1) or _WEBGL_compressed_texture_etc_ (ETC2) WebGL extension. - */ -export const RGBA_ETC2_EAC_Format: 37496; - -// ASTC compressed texture formats - -/** - * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. - */ -export const RGBA_ASTC_4x4_Format: 37808; -/** - * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. - */ -export const RGBA_ASTC_5x4_Format: 37809; -/** - * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. - */ -export const RGBA_ASTC_5x5_Format: 37810; -/** - * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. - */ -export const RGBA_ASTC_6x5_Format: 37811; -/** - * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. - */ -export const RGBA_ASTC_6x6_Format: 37812; -/** - * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. - */ -export const RGBA_ASTC_8x5_Format: 37813; -/** - * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. - */ -export const RGBA_ASTC_8x6_Format: 37814; -/** - * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. - */ -export const RGBA_ASTC_8x8_Format: 37815; -/** - * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. - */ -export const RGBA_ASTC_10x5_Format: 37816; -/** - * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. - */ -export const RGBA_ASTC_10x6_Format: 37817; -/** - * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. - */ -export const RGBA_ASTC_10x8_Format: 37818; -/** - * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. - */ -export const RGBA_ASTC_10x10_Format: 37819; -/** - * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. - */ -export const RGBA_ASTC_12x10_Format: 37820; -/** - * @remarks Require support for the _WEBGL_compressed_texture_astc_ WebGL extension. - */ -export const RGBA_ASTC_12x12_Format: 37821; - -// BPTC compressed texture formats - -/** - * @remarks Require support for the _EXT_texture_compression_bptc_ WebGL extension. - */ -export const RGBA_BPTC_Format: 36492; -export const RGB_BPTC_SIGNED_Format = 36494; -export const RGB_BPTC_UNSIGNED_Format = 36495; - -// RGTC compressed texture formats -export const RED_RGTC1_Format: 36283; -export const SIGNED_RED_RGTC1_Format: 36284; -export const RED_GREEN_RGTC2_Format: 36285; -export const SIGNED_RED_GREEN_RGTC2_Format: 36286; - -/** - * For use with a {@link THREE.CompressedTexture}'s {@link THREE.CompressedTexture.format | .format} property. - * @remarks Compressed Require support for correct WebGL extension. - */ -export type CompressedPixelFormat = - | typeof RGB_S3TC_DXT1_Format - | typeof RGBA_S3TC_DXT1_Format - | typeof RGBA_S3TC_DXT3_Format - | typeof RGBA_S3TC_DXT5_Format - | typeof RGB_PVRTC_4BPPV1_Format - | typeof RGB_PVRTC_2BPPV1_Format - | typeof RGBA_PVRTC_4BPPV1_Format - | typeof RGBA_PVRTC_2BPPV1_Format - | typeof RGB_ETC1_Format - | typeof RGB_ETC2_Format - | typeof RGBA_ETC2_EAC_Format - | typeof RGBA_ASTC_4x4_Format - | typeof RGBA_ASTC_5x4_Format - | typeof RGBA_ASTC_5x5_Format - | typeof RGBA_ASTC_6x5_Format - | typeof RGBA_ASTC_6x6_Format - | typeof RGBA_ASTC_8x5_Format - | typeof RGBA_ASTC_8x6_Format - | typeof RGBA_ASTC_8x8_Format - | typeof RGBA_ASTC_10x5_Format - | typeof RGBA_ASTC_10x6_Format - | typeof RGBA_ASTC_10x8_Format - | typeof RGBA_ASTC_10x10_Format - | typeof RGBA_ASTC_12x10_Format - | typeof RGBA_ASTC_12x12_Format - | typeof RGBA_BPTC_Format - | typeof RGB_BPTC_SIGNED_Format - | typeof RGB_BPTC_UNSIGNED_Format - | typeof RED_RGTC1_Format - | typeof SIGNED_RED_RGTC1_Format - | typeof RED_GREEN_RGTC2_Format - | typeof SIGNED_RED_GREEN_RGTC2_Format; - -/////////////////////////////////////////////////////////////////////////////// - -/** - * All Possible Texture Pixel Formats Modes. For any Type or SubType of Textures. - * @remarks Note that the texture must have the correct {@link THREE.Texture.type} set, as described in {@link TextureDataType}. - * @see {@link WebGLRenderingContext.texImage2D} for details. - * @see {@link PixelFormat} and {@link DepthTexturePixelFormat} and {@link CompressedPixelFormat} - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - */ -export type AnyPixelFormat = PixelFormat | DepthTexturePixelFormat | CompressedPixelFormat; - -/////////////////////////////////////////////////////////////////////////////// -// Loop styles for AnimationAction -export const LoopOnce: 2200; -export const LoopRepeat: 2201; -export const LoopPingPong: 2202; -export type AnimationActionLoopStyles = typeof LoopOnce | typeof LoopRepeat | typeof LoopPingPong; - -// Interpolation -export const InterpolateDiscrete: 2300; -export const InterpolateLinear: 2301; -export const InterpolateSmooth: 2302; -export type InterpolationModes = typeof InterpolateDiscrete | typeof InterpolateLinear | typeof InterpolateSmooth; - -// Interpolant ending modes -export const ZeroCurvatureEnding: 2400; -export const ZeroSlopeEnding: 2401; -export const WrapAroundEnding: 2402; -export type InterpolationEndingModes = typeof ZeroCurvatureEnding | typeof ZeroSlopeEnding | typeof WrapAroundEnding; - -// Animation blending modes -export const NormalAnimationBlendMode: 2500; -export const AdditiveAnimationBlendMode: 2501; -export type AnimationBlendMode = typeof NormalAnimationBlendMode | typeof AdditiveAnimationBlendMode; - -// Triangle Draw modes -export const TrianglesDrawMode: 0; -export const TriangleStripDrawMode: 1; -export const TriangleFanDrawMode: 2; -export type TrianglesDrawModes = typeof TrianglesDrawMode | typeof TriangleStripDrawMode | typeof TriangleFanDrawMode; - -/////////////////////////////////////////////////////////////////////////////// -// Depth packing strategies - -export const BasicDepthPacking: 3200; -export const RGBADepthPacking: 3201; -export const RGBDepthPacking: 3202; -export const RGDepthPacking: 3203; -export type DepthPackingStrategies = - | typeof BasicDepthPacking - | typeof RGBADepthPacking - | typeof RGBDepthPacking - | typeof RGDepthPacking; - -/////////////////////////////////////////////////////////////////////////////// -// Normal Map types - -export const TangentSpaceNormalMap: 0; -export const ObjectSpaceNormalMap: 1; -export type NormalMapTypes = typeof TangentSpaceNormalMap | typeof ObjectSpaceNormalMap; - -export const NoColorSpace: ""; -export const SRGBColorSpace: "srgb"; -export const LinearSRGBColorSpace: "srgb-linear"; -export const DisplayP3ColorSpace: "display-p3"; -export const LinearDisplayP3ColorSpace = "display-p3-linear"; -export type ColorSpace = - | typeof NoColorSpace - | typeof SRGBColorSpace - | typeof LinearSRGBColorSpace - | typeof DisplayP3ColorSpace - | typeof LinearDisplayP3ColorSpace; - -export const LinearTransfer: "linear"; -export const SRGBTransfer: "srgb"; -export type ColorSpaceTransfer = typeof LinearTransfer | typeof SRGBTransfer; - -export const Rec709Primaries: "rec709"; -export const P3Primaries: "p3"; -export type ColorSpacePrimaries = typeof Rec709Primaries | typeof P3Primaries; - -// Stencil Op types -export const ZeroStencilOp: 0; -export const KeepStencilOp: 7680; -export const ReplaceStencilOp: 7681; -export const IncrementStencilOp: 7682; -export const DecrementStencilOp: 7283; -export const IncrementWrapStencilOp: 34055; -export const DecrementWrapStencilOp: 34056; -export const InvertStencilOp: 5386; -export type StencilOp = - | typeof ZeroStencilOp - | typeof KeepStencilOp - | typeof ReplaceStencilOp - | typeof IncrementStencilOp - | typeof DecrementStencilOp - | typeof IncrementWrapStencilOp - | typeof DecrementWrapStencilOp - | typeof InvertStencilOp; - -// Stencil Func types -export const NeverStencilFunc: 512; -export const LessStencilFunc: 513; -export const EqualStencilFunc: 514; -export const LessEqualStencilFunc: 515; -export const GreaterStencilFunc: 516; -export const NotEqualStencilFunc: 517; -export const GreaterEqualStencilFunc: 518; -export const AlwaysStencilFunc: 519; -export type StencilFunc = - | typeof NeverStencilFunc - | typeof LessStencilFunc - | typeof EqualStencilFunc - | typeof LessEqualStencilFunc - | typeof GreaterStencilFunc - | typeof NotEqualStencilFunc - | typeof GreaterEqualStencilFunc - | typeof AlwaysStencilFunc; - -export const NeverCompare: 512; -export const LessCompare: 513; -export const EqualCompare: 514; -export const LessEqualCompare: 515; -export const GreaterCompare: 516; -export const NotEqualCompare: 517; -export const GreaterEqualCompare: 518; -export const AlwaysCompare: 519; -export type TextureComparisonFunction = - | typeof NeverCompare - | typeof LessCompare - | typeof EqualCompare - | typeof LessEqualCompare - | typeof GreaterCompare - | typeof NotEqualCompare - | typeof GreaterEqualCompare - | typeof AlwaysCompare; - -// usage types -export const StaticDrawUsage: 35044; -export const DynamicDrawUsage: 35048; -export const StreamDrawUsage: 35040; -export const StaticReadUsage: 35045; -export const DynamicReadUsage: 35049; -export const StreamReadUsage: 35041; -export const StaticCopyUsage: 35046; -export const DynamicCopyUsage: 35050; -export const StreamCopyUsage: 35042; -export type Usage = - | typeof StaticDrawUsage - | typeof DynamicDrawUsage - | typeof StreamDrawUsage - | typeof StaticReadUsage - | typeof DynamicReadUsage - | typeof StreamReadUsage - | typeof StaticCopyUsage - | typeof DynamicCopyUsage - | typeof StreamCopyUsage; - -export const GLSL1: "100"; -export const GLSL3: "300 es"; -export type GLSLVersion = typeof GLSL1 | typeof GLSL3; - -export const WebGLCoordinateSystem: 2000; -export const WebGPUCoordinateSystem: 2001; -export type CoordinateSystem = typeof WebGLCoordinateSystem | typeof WebGPUCoordinateSystem; - -/////////////////////////////////////////////////////////////////////////////// -// Texture - Internal Pixel Formats - -/** - * For use with a texture's {@link THREE.Texture.internalFormat} property, these define how elements of a {@link THREE.Texture}, or texels, are stored on the GPU. - * - `R8` stores the red component on 8 bits. - * - `R8_SNORM` stores the red component on 8 bits. The component is stored as normalized. - * - `R8I` stores the red component on 8 bits. The component is stored as an integer. - * - `R8UI` stores the red component on 8 bits. The component is stored as an unsigned integer. - * - `R16I` stores the red component on 16 bits. The component is stored as an integer. - * - `R16UI` stores the red component on 16 bits. The component is stored as an unsigned integer. - * - `R16F` stores the red component on 16 bits. The component is stored as floating point. - * - `R32I` stores the red component on 32 bits. The component is stored as an integer. - * - `R32UI` stores the red component on 32 bits. The component is stored as an unsigned integer. - * - `R32F` stores the red component on 32 bits. The component is stored as floating point. - * - `RG8` stores the red and green components on 8 bits each. - * - `RG8_SNORM` stores the red and green components on 8 bits each. Every component is stored as normalized. - * - `RG8I` stores the red and green components on 8 bits each. Every component is stored as an integer. - * - `RG8UI` stores the red and green components on 8 bits each. Every component is stored as an unsigned integer. - * - `RG16I` stores the red and green components on 16 bits each. Every component is stored as an integer. - * - `RG16UI` stores the red and green components on 16 bits each. Every component is stored as an unsigned integer. - * - `RG16F` stores the red and green components on 16 bits each. Every component is stored as floating point. - * - `RG32I` stores the red and green components on 32 bits each. Every component is stored as an integer. - * - `RG32UI` stores the red and green components on 32 bits. Every component is stored as an unsigned integer. - * - `RG32F` stores the red and green components on 32 bits. Every component is stored as floating point. - * - `RGB8` stores the red, green, and blue components on 8 bits each. RGB8_SNORM` stores the red, green, and blue components on 8 bits each. Every component is stored as normalized. - * - `RGB8I` stores the red, green, and blue components on 8 bits each. Every component is stored as an integer. - * - `RGB8UI` stores the red, green, and blue components on 8 bits each. Every component is stored as an unsigned integer. - * - `RGB16I` stores the red, green, and blue components on 16 bits each. Every component is stored as an integer. - * - `RGB16UI` stores the red, green, and blue components on 16 bits each. Every component is stored as an unsigned integer. - * - `RGB16F` stores the red, green, and blue components on 16 bits each. Every component is stored as floating point - * - `RGB32I` stores the red, green, and blue components on 32 bits each. Every component is stored as an integer. - * - `RGB32UI` stores the red, green, and blue components on 32 bits each. Every component is stored as an unsigned integer. - * - `RGB32F` stores the red, green, and blue components on 32 bits each. Every component is stored as floating point - * - `R11F_G11F_B10F` stores the red, green, and blue components respectively on 11 bits, 11 bits, and 10bits. Every component is stored as floating point. - * - `RGB565` stores the red, green, and blue components respectively on 5 bits, 6 bits, and 5 bits. - * - `RGB9_E5` stores the red, green, and blue components on 9 bits each. - * - `RGBA8` stores the red, green, blue, and alpha components on 8 bits each. - * - `RGBA8_SNORM` stores the red, green, blue, and alpha components on 8 bits. Every component is stored as normalized. - * - `RGBA8I` stores the red, green, blue, and alpha components on 8 bits each. Every component is stored as an integer. - * - `RGBA8UI` stores the red, green, blue, and alpha components on 8 bits. Every component is stored as an unsigned integer. - * - `RGBA16I` stores the red, green, blue, and alpha components on 16 bits. Every component is stored as an integer. - * - `RGBA16UI` stores the red, green, blue, and alpha components on 16 bits. Every component is stored as an unsigned integer. - * - `RGBA16F` stores the red, green, blue, and alpha components on 16 bits. Every component is stored as floating point. - * - `RGBA32I` stores the red, green, blue, and alpha components on 32 bits. Every component is stored as an integer. - * - `RGBA32UI` stores the red, green, blue, and alpha components on 32 bits. Every component is stored as an unsigned integer. - * - `RGBA32F` stores the red, green, blue, and alpha components on 32 bits. Every component is stored as floating point. - * - `RGB5_A1` stores the red, green, blue, and alpha components respectively on 5 bits, 5 bits, 5 bits, and 1 bit. - * - `RGB10_A2` stores the red, green, blue, and alpha components respectively on 10 bits, 10 bits, 10 bits and 2 bits. - * - `RGB10_A2UI` stores the red, green, blue, and alpha components respectively on 10 bits, 10 bits, 10 bits and 2 bits. Every component is stored as an unsigned integer. - * - `SRGB8` stores the red, green, and blue components on 8 bits each. - * - `SRGB8_ALPHA8` stores the red, green, blue, and alpha components on 8 bits each. - * - `DEPTH_COMPONENT16` stores the depth component on 16bits. - * - `DEPTH_COMPONENT24` stores the depth component on 24bits. - * - `DEPTH_COMPONENT32F` stores the depth component on 32bits. The component is stored as floating point. - * - `DEPTH24_STENCIL8` stores the depth, and stencil components respectively on 24 bits and 8 bits. The stencil component is stored as an unsigned integer. - * - `DEPTH32F_STENCIL8` stores the depth, and stencil components respectively on 32 bits and 8 bits. The depth component is stored as floating point, and the stencil component as an unsigned integer. - * @remark Note that the texture must have the correct {@link THREE.Texture.type} set, as well as the correct {@link THREE.Texture.format}. - * @see {@link WebGLRenderingContext.texImage2D} and {@link WebGLRenderingContext.texImage3D} for more details regarding the possible combination - * of {@link THREE.Texture.format}, {@link THREE.Texture.internalFormat}, and {@link THREE.Texture.type}. - * @see {@link https://registry.khronos.org/webgl/specs/latest/2.0/ | WebGL2 Specification} and - * {@link https://registry.khronos.org/OpenGL/specs/es/3.0/es_spec_3.0.pdf | OpenGL ES 3.0 Specification} For more in-depth information regarding internal formats. - */ -export type PixelFormatGPU = - | "ALPHA" - | "RGB" - | "RGBA" - | "LUMINANCE" - | "LUMINANCE_ALPHA" - | "RED_INTEGER" - | "R8" - | "R8_SNORM" - | "R8I" - | "R8UI" - | "R16I" - | "R16UI" - | "R16F" - | "R32I" - | "R32UI" - | "R32F" - | "RG8" - | "RG8_SNORM" - | "RG8I" - | "RG8UI" - | "RG16I" - | "RG16UI" - | "RG16F" - | "RG32I" - | "RG32UI" - | "RG32F" - | "RGB565" - | "RGB8" - | "RGB8_SNORM" - | "RGB8I" - | "RGB8UI" - | "RGB16I" - | "RGB16UI" - | "RGB16F" - | "RGB32I" - | "RGB32UI" - | "RGB32F" - | "RGB9_E5" - | "SRGB8" - | "R11F_G11F_B10F" - | "RGBA4" - | "RGBA8" - | "RGBA8_SNORM" - | "RGBA8I" - | "RGBA8UI" - | "RGBA16I" - | "RGBA16UI" - | "RGBA16F" - | "RGBA32I" - | "RGBA32UI" - | "RGBA32F" - | "RGB5_A1" - | "RGB10_A2" - | "RGB10_A2UI" - | "SRGB8_ALPHA8" - | "SRGB8" - | "DEPTH_COMPONENT16" - | "DEPTH_COMPONENT24" - | "DEPTH_COMPONENT32F" - | "DEPTH24_STENCIL8" - | "DEPTH32F_STENCIL8"; diff --git a/src-testing/src/core/BufferAttribute.d.ts b/src-testing/src/core/BufferAttribute.d.ts deleted file mode 100644 index c0dbba8e0..000000000 --- a/src-testing/src/core/BufferAttribute.d.ts +++ /dev/null @@ -1,637 +0,0 @@ -import { AttributeGPUType, Usage } from "../constants.js"; -import { Matrix3 } from "../math/Matrix3.js"; -import { Matrix4 } from "../math/Matrix4.js"; - -export type TypedArray = - | Int8Array - | Uint8Array - | Uint8ClampedArray - | Int16Array - | Uint16Array - | Int32Array - | Uint32Array - | Float32Array - | Float64Array; - -export interface BufferAttributeJSON { - itemSize: number; - type: string; - array: number[]; - normalized: boolean; - - name?: string; - usage?: Usage; -} - -/** - * This class stores data for an attribute (such as vertex positions, face indices, normals, colors, UVs, and any custom attributes ) - * associated with a {@link THREE.BufferGeometry | BufferGeometry}, which allows for more efficient passing of data to the GPU - * @remarks - * When working with _vector-like_ data, the _`.fromBufferAttribute( attribute, index )`_ helper methods on - * {@link THREE.Vector2.fromBufferAttribute | Vector2}, - * {@link THREE.Vector3.fromBufferAttribute | Vector3}, - * {@link THREE.Vector4.fromBufferAttribute | Vector4}, and - * {@link THREE.Color.fromBufferAttribute | Color} classes may be helpful. - * @see {@link THREE.BufferGeometry | BufferGeometry} for details and a usage examples. - * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry | WebGL / BufferGeometry - Clean up Memory} - * @see {@link https://threejs.org/docs/index.html#api/en/core/BufferAttribute | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} - */ -export class BufferAttribute { - /** - * This creates a new {@link THREE.GLBufferAttribute | GLBufferAttribute} object. - * @param array Must be a `TypedArray`. Used to instantiate the buffer. - * This array should have `itemSize * numVertices` elements, where numVertices is the number of vertices in the associated {@link THREE.BufferGeometry | BufferGeometry}. - * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. - * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), - * then itemSize should be `3`. - * @param normalized Applies to integer data only. - * Indicates how the underlying data in the buffer maps to the values in the GLSL code. - * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, - * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. - * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. - * If normalized is false, the values will be converted to floats unmodified, - * i.e. `32767` becomes `32767.0f`. - * Default `false`. - * @throws `TypeError` When the {@link array} is not a `TypedArray`; - */ - constructor(array: TypedArray, itemSize: number, normalized?: boolean); - - /** - * Optional name for this attribute instance. - * @defaultValue '' - */ - name: string; - - /** - * The {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray | TypedArray} holding data stored in the buffer. - * @returns `TypedArray` - */ - array: TypedArray; - - /** - * The length of vectors that are being stored in the {@link BufferAttribute.array | array}. - * @remarks Expects a `Integer` - */ - itemSize: number; - - /** - * Defines the intended usage pattern of the data store for optimization purposes. - * Corresponds to the {@link BufferAttribute.usage | usage} parameter of - * {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferData | WebGLRenderingContext.bufferData}. - * @remarks - * After the initial use of a buffer, its usage cannot be changed. Instead, instantiate a new one and set the desired usage before the next render. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/BufferAttributeUsage | Buffer Attribute Usage Constants} for all possible values. - * @see {@link BufferAttribute.setUsage | setUsage} - * @defaultValue {@link THREE.StaticDrawUsage | THREE.StaticDrawUsage}. - */ - usage: Usage; - - /** - * Configures the bound GPU type for use in shaders. Either {@link FloatType} or {@link IntType}, default is {@link FloatType}. - * - * Note: this only has an effect for integer arrays and is not configurable for float arrays. For lower precision - * float types, see https://threejs.org/docs/#api/en/core/bufferAttributeTypes/BufferAttributeTypes. - */ - gpuType: AttributeGPUType; - - /** - * This can be used to only update some components of stored vectors (for example, just the component related to color). - * @defaultValue `{ offset: number = 0; count: number = -1 }` - * @deprecated Will be removed in r169. Use "addUpdateRange()" instead. - */ - updateRange: { - /** - * Position at which to start update. - * @defaultValue `0` - */ - offset: number; - /** @defaultValue `-1`, which means don't use update ranges. */ - count: number; - }; - - /** - * This can be used to only update some components of stored vectors (for example, just the component related to - * color). Use the {@link .addUpdateRange} function to add ranges to this array. - */ - updateRanges: Array<{ - /** - * Position at which to start update. - */ - start: number; - /** - * The number of components to update. - */ - count: number; - }>; - - /** - * A version number, incremented every time the {@link BufferAttribute.needsUpdate | needsUpdate} property is set to true. - * @remarks Expects a `Integer` - * @defaultValue `0` - */ - version: number; - - /** - * Indicates how the underlying data in the buffer maps to the values in the GLSL shader code. - * @see `constructor` above for details. - * @defaultValue `false` - */ - normalized: boolean; - - /** - * Represents the number of items this buffer attribute stores. It is internally computed by dividing the - * {@link BufferAttribute.array | array}'s length by the {@link BufferAttribute.itemSize | itemSize}. Read-only - * property. - */ - readonly count: number; - - /** - * Flag to indicate that this attribute has changed and should be re-sent to the GPU. - * Set this to true when you modify the value of the array. - * @remarks Setting this to true also increments the {@link BufferAttribute.version | version}. - * @remarks _set-only property_. - */ - set needsUpdate(value: boolean); - - /** - * Read-only flag to check if a given object is of type {@link BufferAttribute}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isBufferAttribute: true; - - /** - * A callback function that is executed after the Renderer has transferred the attribute array data to the GPU. - */ - onUploadCallback: () => void; - - /** - * Sets the value of the {@link onUploadCallback} property. - * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry | WebGL / BufferGeometry} this is used to free memory after the buffer has been transferred to the GPU. - * @see {@link onUploadCallback} - * @param callback function that is executed after the Renderer has transferred the attribute array data to the GPU. - */ - onUpload(callback: () => void): this; - - /** - * Set {@link BufferAttribute.usage | usage} - * @remarks - * After the initial use of a buffer, its usage cannot be changed. Instead, instantiate a new one and set the desired usage before the next render. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/BufferAttributeUsage | Buffer Attribute Usage Constants} for all possible values. - * @see {@link BufferAttribute.usage | usage} - * @param value Corresponds to the {@link BufferAttribute.usage | usage} parameter of - * {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferData | WebGLRenderingContext.bufferData}. - */ - setUsage(usage: Usage): this; - - /** - * Adds a range of data in the data array to be updated on the GPU. Adds an object describing the range to the - * {@link .updateRanges} array. - */ - addUpdateRange(start: number, count: number): void; - - /** - * Clears the {@link .updateRanges} array. - */ - clearUpdateRanges(): void; - - /** - * @returns a copy of this {@link BufferAttribute}. - */ - clone(): BufferAttribute; - - /** - * Copies another {@link BufferAttribute} to this {@link BufferAttribute}. - * @param bufferAttribute - */ - copy(source: BufferAttribute): this; - - /** - * Copy a vector from bufferAttribute[index2] to {@link BufferAttribute.array | array}[index1]. - * @param index1 - * @param bufferAttribute - * @param index2 - */ - copyAt(index1: number, attribute: BufferAttribute, index2: number): this; - - /** - * Copy the array given here (which can be a normal array or `TypedArray`) into {@link BufferAttribute.array | array}. - * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set | TypedArray.set} for notes on requirements if copying a `TypedArray`. - */ - copyArray(array: ArrayLike): this; - - /** - * Applies matrix {@link Matrix3 | m} to every Vector3 element of this {@link BufferAttribute}. - * @param m - */ - applyMatrix3(m: Matrix3): this; - - /** - * Applies matrix {@link Matrix4 | m} to every Vector3 element of this {@link BufferAttribute}. - * @param m - */ - applyMatrix4(m: Matrix4): this; - - /** - * Applies normal matrix {@link Matrix3 | m} to every Vector3 element of this {@link BufferAttribute}. - * @param m - */ - applyNormalMatrix(m: Matrix3): this; - - /** - * Applies matrix {@link Matrix4 | m} to every Vector3 element of this {@link BufferAttribute}, interpreting the elements as a direction vectors. - * @param m - */ - transformDirection(m: Matrix4): this; - - /** - * Calls {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set | TypedArray.set}( {@link value}, {@link offset} ) - * on the {@link BufferAttribute.array | array}. - * @param value {@link Array | Array} or `TypedArray` from which to copy values. - * @param offset index of the {@link BufferAttribute.array | array} at which to start copying. Expects a `Integer`. Default `0`. - * @throws `RangeError` When {@link offset} is negative or is too large. - */ - set(value: ArrayLike | ArrayBufferView, offset?: number): this; - - /** - * Returns the given component of the vector at the given index. - */ - getComponent(index: number, component: number): number; - - /** - * Sets the given component of the vector at the given index. - */ - setComponent(index: number, component: number, value: number): void; - - /** - * Returns the x component of the vector at the given index. - * @param index Expects a `Integer` - */ - getX(index: number): number; - - /** - * Sets the x component of the vector at the given index. - * @param index Expects a `Integer` - * @param x - */ - setX(index: number, x: number): this; - - /** - * Returns the y component of the vector at the given index. - * @param index Expects a `Integer` - */ - getY(index: number): number; - - /** - * Sets the y component of the vector at the given index. - * @param index Expects a `Integer` - * @param y - */ - setY(index: number, y: number): this; - - /** - * Returns the z component of the vector at the given index. - * @param index Expects a `Integer` - */ - getZ(index: number): number; - - /** - * Sets the z component of the vector at the given index. - * @param index Expects a `Integer` - * @param z - */ - setZ(index: number, z: number): this; - - /** - * Returns the w component of the vector at the given index. - * @param index Expects a `Integer` - */ - getW(index: number): number; - - /** - * Sets the w component of the vector at the given index. - * @param index Expects a `Integer` - * @param w - */ - setW(index: number, z: number): this; - - /** - * Sets the x and y components of the vector at the given index. - * @param index Expects a `Integer` - * @param x - * @param y - */ - setXY(index: number, x: number, y: number): this; - - /** - * Sets the x, y and z components of the vector at the given index. - * @param index Expects a `Integer` - * @param x - * @param y - * @param z - */ - setXYZ(index: number, x: number, y: number, z: number): this; - - /** - * Sets the x, y, z and w components of the vector at the given index. - * @param index Expects a `Integer` - * @param x - * @param y - * @param z - * @param w - */ - setXYZW(index: number, x: number, y: number, z: number, w: number): this; - - /** - * Convert this object to three.js to the `data.attributes` part of {@link https://github.com/mrdoob/three.js/wiki/JSON-Geometry-format-4 | JSON Geometry format v4}, - */ - toJSON(): BufferAttributeJSON; -} - -/** - * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array: Int8Array} - * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} - * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. - * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} - */ -export class Int8BufferAttribute extends BufferAttribute { - /** - * This creates a new {@link THREE.Int8BufferAttribute | Int8BufferAttribute} object. - * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Int8Array`. - * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. - * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. - * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), - * then itemSize should be `3`. - * @param normalized Applies to integer data only. - * Indicates how the underlying data in the buffer maps to the values in the GLSL code. - * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, - * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. - * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. - * If normalized is false, the values will be converted to floats unmodified, - * i.e. `32767` becomes `32767.0f`. - * Default `false`. - */ - constructor( - array: Iterable | ArrayLike | ArrayBuffer | number, - itemSize: number, - normalized?: boolean, - ); -} - -/** - * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array: Uint8Array} - * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} - * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. - * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} - */ -export class Uint8BufferAttribute extends BufferAttribute { - /** - * This creates a new {@link THREE.Uint8BufferAttribute | Uint8BufferAttribute} object. - * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Uint8Array`. - * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. - * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. - * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), - * then itemSize should be `3`. - * @param normalized Applies to integer data only. - * Indicates how the underlying data in the buffer maps to the values in the GLSL code. - * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, - * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. - * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. - * If normalized is false, the values will be converted to floats unmodified, - * i.e. `32767` becomes `32767.0f`. - * Default `false`. - * @see {@link THREE.BufferAttribute | BufferAttribute} - */ - constructor( - array: Iterable | ArrayLike | ArrayBuffer | number, - itemSize: number, - normalized?: boolean, - ); -} - -/** - * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray: Uint8ClampedArray} - * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} - * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. - * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} - */ -export class Uint8ClampedBufferAttribute extends BufferAttribute { - /** - * This creates a new {@link THREE.Uint8ClampedBufferAttribute | Uint8ClampedBufferAttribute} object. - * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Uint8ClampedArray`. - * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. - * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. - * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), - * then itemSize should be `3`. - * @param normalized Applies to integer data only. - * Indicates how the underlying data in the buffer maps to the values in the GLSL code. - * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, - * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. - * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. - * If normalized is false, the values will be converted to floats unmodified, - * i.e. `32767` becomes `32767.0f`. - * Default `false`. - * @see {@link THREE.BufferAttribute | BufferAttribute} - */ - constructor( - array: Iterable | ArrayLike | ArrayBuffer | number, - itemSize: number, - normalized?: boolean, - ); -} - -/** - * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int16Array: Int16Array} - * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} - * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. - * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} - */ -export class Int16BufferAttribute extends BufferAttribute { - /** - * This creates a new {@link THREE.Int16BufferAttribute | Int16BufferAttribute} object. - * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Int16Array`. - * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. - * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. - * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), - * then itemSize should be `3`. - * @param normalized Applies to integer data only. - * Indicates how the underlying data in the buffer maps to the values in the GLSL code. - * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, - * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. - * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. - * If normalized is false, the values will be converted to floats unmodified, - * i.e. `32767` becomes `32767.0f`. - * Default `false`. - * @see {@link THREE.BufferAttribute | BufferAttribute} - */ - constructor( - array: Iterable | ArrayLike | ArrayBuffer | number, - itemSize: number, - normalized?: boolean, - ); -} - -/** - * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array: Uint16Array} - * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} - * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. - * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} - */ -export class Uint16BufferAttribute extends BufferAttribute { - /** - * This creates a new {@link THREE.Uint16BufferAttribute | Uint16BufferAttribute} object. - * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Uint16Array`. - * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. - * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. - * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), - * then itemSize should be `3`. - * @param normalized Applies to integer data only. - * Indicates how the underlying data in the buffer maps to the values in the GLSL code. - * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, - * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. - * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. - * If normalized is false, the values will be converted to floats unmodified, - * i.e. `32767` becomes `32767.0f`. - * Default `false`. - * @see {@link THREE.BufferAttribute | BufferAttribute} - */ - constructor( - array: Iterable | ArrayLike | ArrayBuffer | number, - itemSize: number, - normalized?: boolean, - ); -} - -/** - * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array: Int32Array} - * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} - * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. - * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} - */ -export class Int32BufferAttribute extends BufferAttribute { - /** - * This creates a new {@link THREE.Int32BufferAttribute | Int32BufferAttribute} object. - * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Int32Array`. - * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. - * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. - * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), - * then itemSize should be `3`. - * @param normalized Applies to integer data only. - * Indicates how the underlying data in the buffer maps to the values in the GLSL code. - * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, - * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. - * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. - * If normalized is false, the values will be converted to floats unmodified, - * i.e. `32767` becomes `32767.0f`. - * Default `false`. - * @see {@link THREE.BufferAttribute | BufferAttribute} - */ - constructor( - array: Iterable | ArrayLike | ArrayBuffer | number, - itemSize: number, - normalized?: boolean, - ); -} - -/** - * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array: Uint32Array} - * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} - * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. - * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} - */ -export class Uint32BufferAttribute extends BufferAttribute { - /** - * This creates a new {@link THREE.Uint32BufferAttribute | Uint32BufferAttribute} object. - * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Uint32Array`. - * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. - * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. - * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), - * then itemSize should be `3`. - * @param normalized Applies to integer data only. - * Indicates how the underlying data in the buffer maps to the values in the GLSL code. - * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, - * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. - * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. - * If normalized is false, the values will be converted to floats unmodified, - * i.e. `32767` becomes `32767.0f`. - * Default `false`. - * @see {@link THREE.BufferAttribute | BufferAttribute} - */ - constructor( - array: Iterable | ArrayLike | ArrayBuffer | number, - itemSize: number, - normalized?: boolean, - ); -} - -/** - * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array: Uint16Array} - * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} - * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. - * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} - */ -export class Float16BufferAttribute extends BufferAttribute { - /** - * This creates a new {@link THREE.Float16BufferAttribute | Float16BufferAttribute} object. - * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Uint16Array`. - * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. - * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. - * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), - * then itemSize should be `3`. - * @param normalized Applies to integer data only. - * Indicates how the underlying data in the buffer maps to the values in the GLSL code. - * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, - * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. - * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. - * If normalized is false, the values will be converted to floats unmodified, - * i.e. `32767` becomes `32767.0f`. - * Default `false`. - * @see {@link THREE.BufferAttribute | BufferAttribute} - */ - constructor( - array: Iterable | ArrayLike | ArrayBuffer | number, - itemSize: number, - normalized?: boolean, - ); -} - -/** - * A {@link THREE.BufferAttribute | BufferAttribute} for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array: Float32Array} - * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#typedarray_objects | TypedArray} - * @see {@link THREE.BufferAttribute | BufferAttribute} for details and for inherited methods and properties. - * @see {@link https://threejs.org/docs/index.html#api/en/core/bufferAttributeTypes/BufferAttributeTypes | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferAttribute.js | Source} - */ -export class Float32BufferAttribute extends BufferAttribute { - /** - * This creates a new {@link THREE.Float32BufferAttribute | Float32BufferAttribute} object. - * @param array This can be a typed or untyped (normal) array or an integer length. An array value will be converted to `Float32Array`. - * If a length is given a new `TypedArray` will created, initialized with all elements set to zero. - * @param itemSize the number of values of the {@link array} that should be associated with a particular vertex. - * For instance, if this attribute is storing a 3-component vector (such as a _position_, _normal_, or _color_), - * then itemSize should be `3`. - * @param normalized Applies to integer data only. - * Indicates how the underlying data in the buffer maps to the values in the GLSL code. - * For instance, if {@link array} is an instance of `UInt16Array`, and {@link normalized} is true, - * the values `0` - `+65535` in the array data will be mapped to `0.0f` - `+1.0f` in the GLSL attribute. - * An `Int16Array` (signed) would map from `-32768` - `+32767` to `-1.0f` - `+1.0f`. - * If normalized is false, the values will be converted to floats unmodified, - * i.e. `32767` becomes `32767.0f`. - * Default `false`. - * @see {@link THREE.BufferAttribute | BufferAttribute} - */ - constructor( - array: Iterable | ArrayLike | ArrayBuffer | number, - itemSize: number, - normalized?: boolean, - ); -} diff --git a/src-testing/src/core/BufferGeometry.d.ts b/src-testing/src/core/BufferGeometry.d.ts deleted file mode 100644 index 82cf8ad03..000000000 --- a/src-testing/src/core/BufferGeometry.d.ts +++ /dev/null @@ -1,422 +0,0 @@ -import { Box3 } from "../math/Box3.js"; -import { Matrix4 } from "../math/Matrix4.js"; -import { Quaternion } from "../math/Quaternion.js"; -import { Sphere } from "../math/Sphere.js"; -import { Vector2 } from "../math/Vector2.js"; -import { Vector3, Vector3Tuple } from "../math/Vector3.js"; -import { BufferAttribute, BufferAttributeJSON } from "./BufferAttribute.js"; -import { EventDispatcher } from "./EventDispatcher.js"; -import { GLBufferAttribute } from "./GLBufferAttribute.js"; -import { InterleavedBufferAttribute } from "./InterleavedBufferAttribute.js"; - -export type NormalBufferAttributes = Record; -export type NormalOrGLBufferAttributes = Record< - string, - BufferAttribute | InterleavedBufferAttribute | GLBufferAttribute ->; - -export interface BufferGeometryJSON { - metadata?: { version: number; type: string; generator: string }; - - uuid: string; - type: string; - - name?: string; - userData?: Record; - - data?: { - attributes: Record; - - index?: { type: string; array: number[] }; - - morphAttributes?: Record; - morphTargetsRelative?: boolean; - - groups?: GeometryGroup[]; - - boundingSphere?: { center: Vector3Tuple; radius: number }; - }; -} - -export interface GeometryGroup { - /** - * Specifies the first element in this draw call – the first vertex for non-indexed geometry, otherwise the first triangle index. - * @remarks Expects a `Integer` - */ - start: number; - /** - * Specifies how many vertices (or indices) are included. - * @remarks Expects a `Integer` - */ - count: number; - /** - * Specifies the material array index to use. - * @remarks Expects a `Integer` - */ - materialIndex?: number | undefined; -} - -/** - * A representation of mesh, line, or point geometry - * Includes vertex positions, face indices, normals, colors, UVs, and custom attributes within buffers, reducing the cost of passing all this data to the GPU. - * @remarks - * To read and edit data in BufferGeometry attributes, see {@link THREE.BufferAttribute | BufferAttribute} documentation. - * @example - * ```typescript - * const geometry = new THREE.BufferGeometry(); - * - * // create a simple square shape. We duplicate the top left and bottom right - * // vertices because each vertex needs to appear once per triangle. - * const vertices = new Float32Array( [ - * -1.0, -1.0, 1.0, // v0 - * 1.0, -1.0, 1.0, // v1 - * 1.0, 1.0, 1.0, // v2 - * - * 1.0, 1.0, 1.0, // v3 - * -1.0, 1.0, 1.0, // v4 - * -1.0, -1.0, 1.0 // v5 - * ] ); - * - * // itemSize = 3 because there are 3 values (components) per vertex - * geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) ); - * const material = new THREE.MeshBasicMaterial( { color: 0xff0000 } ); - * const mesh = new THREE.Mesh( geometry, material ); - * ``` - * @example - * ```typescript - * const geometry = new THREE.BufferGeometry(); - * - * const vertices = new Float32Array( [ - * -1.0, -1.0, 1.0, // v0 - * 1.0, -1.0, 1.0, // v1 - * 1.0, 1.0, 1.0, // v2 - * -1.0, 1.0, 1.0, // v3 - * ] ); - * geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) ); - * - * const indices = [ - * 0, 1, 2, - * 2, 3, 0, - * ]; - * - * geometry.setIndex( indices ); - * geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) ); - * - * const material = new THREE.MeshBasicMaterial( { color: 0xff0000 } ); - * const mesh = new THREE.Mesh( geometry, material ); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry | Mesh with non-indexed faces} - * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_indexed | Mesh with indexed faces} - * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_lines | Lines} - * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_lines_indexed | Indexed Lines} - * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_custom_attributes_particles | Particles} - * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_rawshader | Raw Shaders} - * @see {@link https://threejs.org/docs/index.html#api/en/core/BufferGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferGeometry.js | Source} - */ -export class BufferGeometry< - Attributes extends NormalOrGLBufferAttributes = NormalBufferAttributes, -> extends EventDispatcher<{ dispose: {} }> { - /** - * This creates a new {@link THREE.BufferGeometry | BufferGeometry} object. - */ - constructor(); - - /** - * Unique number for this {@link THREE.BufferGeometry | BufferGeometry} instance. - * @remarks Expects a `Integer` - */ - id: number; - - /** - * {@link http://en.wikipedia.org/wiki/Universally_unique_identifier | UUID} of this object instance. - * @remarks This gets automatically assigned and shouldn't be edited. - */ - uuid: string; - - /** - * Optional name for this {@link THREE.BufferGeometry | BufferGeometry} instance. - * @defaultValue `''` - */ - name: string; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `BufferGeometry` - */ - readonly type: string | "BufferGeometry"; - - /** - * Allows for vertices to be re-used across multiple triangles; this is called using "indexed triangles". - * Each triangle is associated with the indices of three vertices. This attribute therefore stores the index of each vertex for each triangular face. - * If this attribute is not set, the {@link THREE.WebGLRenderer | renderer} assumes that each three contiguous positions represent a single triangle. - * @defaultValue `null` - */ - index: BufferAttribute | null; - - /** - * This hashmap has as id the name of the attribute to be set and as value the {@link THREE.BufferAttribute | buffer} to set it to. Rather than accessing this property directly, - * use {@link setAttribute | .setAttribute} and {@link getAttribute | .getAttribute} to access attributes of this geometry. - * @defaultValue `{}` - */ - attributes: Attributes; - - /** - * Hashmap of {@link THREE.BufferAttribute | BufferAttributes} holding details of the geometry's morph targets. - * @remarks - * Once the geometry has been rendered, the morph attribute data cannot be changed. - * You will have to call {@link dispose | .dispose}(), and create a new instance of {@link THREE.BufferGeometry | BufferGeometry}. - * @defaultValue `{}` - */ - morphAttributes: { - [name: string]: Array; // TODO Replace for 'Record<>' - }; - - /** - * Used to control the morph target behavior; when set to true, the morph target data is treated as relative offsets, rather than as absolute positions/normals. - * @defaultValue `false` - */ - morphTargetsRelative: boolean; - - /** - * Split the geometry into groups, each of which will be rendered in a separate WebGL draw call. This allows an array of materials to be used with the geometry. - * @remarks Every vertex and index must belong to exactly one group — groups must not share vertices or indices, and must not leave vertices or indices unused. - * @remarks Use {@link addGroup | .addGroup} to add groups, rather than modifying this array directly. - * @defaultValue `[]` - */ - groups: GeometryGroup[]; - - /** - * Bounding box for the {@link THREE.BufferGeometry | BufferGeometry}, which can be calculated with {@link computeBoundingBox | .computeBoundingBox()}. - * @remarks Bounding boxes aren't computed by default. They need to be explicitly computed, otherwise they are `null`. - * @defaultValue `null` - */ - boundingBox: Box3 | null; - - /** - * Bounding sphere for the {@link THREE.BufferGeometry | BufferGeometry}, which can be calculated with {@link computeBoundingSphere | .computeBoundingSphere()}. - * @remarks bounding spheres aren't computed by default. They need to be explicitly computed, otherwise they are `null`. - * @defaultValue `null` - */ - boundingSphere: Sphere | null; - - /** - * Determines the part of the geometry to render. This should not be set directly, instead use {@link setDrawRange | .setDrawRange(...)}. - * @remarks For non-indexed {@link THREE.BufferGeometry | BufferGeometry}, count is the number of vertices to render. - * @remarks For indexed {@link THREE.BufferGeometry | BufferGeometry}, count is the number of indices to render. - * @defaultValue `{ start: 0, count: Infinity }` - */ - drawRange: { start: number; count: number }; - - /** - * An object that can be used to store custom data about the BufferGeometry. It should not hold references to functions as these will not be cloned. - * @defaultValue `{}` - */ - userData: Record; - - /** - * Read-only flag to check if a given object is of type {@link BufferGeometry}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isBufferGeometry: true; - - /** - * Return the {@link index | .index} buffer. - */ - getIndex(): BufferAttribute | null; - - /** - * Set the {@link THREE.BufferGeometry.index | .index} buffer. - * @param index - */ - setIndex(index: BufferAttribute | number[] | null): this; - - /** - * Sets an {@link attributes | attribute} to this geometry with the specified name. - * @remarks - * Use this rather than the attributes property, because an internal hashmap of {@link attributes | .attributes} is maintained to speed up iterating over attributes. - * @param name - * @param attribute - */ - setAttribute(name: K, attribute: Attributes[K]): this; - - /** - * Returns the {@link attributes | attribute} with the specified name. - * @param name - */ - getAttribute(name: K): Attributes[K]; - - /** - * Deletes the {@link attributes | attribute} with the specified name. - * @param name - */ - deleteAttribute(name: keyof Attributes): this; - - /** - * Returns true if the {@link attributes | attribute} with the specified name exists. - * @param name - */ - hasAttribute(name: keyof Attributes): boolean; - - /** - * Adds a group to this geometry - * @see the {@link BufferGeometry.groups | groups} property for details. - * @param start - * @param count - * @param materialIndex - */ - addGroup(start: number, count: number, materialIndex?: number): void; - - /** - * Clears all groups. - */ - clearGroups(): void; - - /** - * Set the {@link drawRange | .drawRange} property - * @remarks For non-indexed BufferGeometry, count is the number of vertices to render - * @remarks For indexed BufferGeometry, count is the number of indices to render. - * @param start - * @param count is the number of vertices or indices to render. Expects a `Integer` - */ - setDrawRange(start: number, count: number): void; - - /** - * Applies the matrix transform to the geometry. - * @param matrix - */ - applyMatrix4(matrix: Matrix4): this; - - /** - * Applies the rotation represented by the quaternion to the geometry. - * @param quaternion - */ - applyQuaternion(quaternion: Quaternion): this; - - /** - * Rotate the geometry about the X axis. This is typically done as a one time operation, and not during a loop. - * @remarks Use {@link THREE.Object3D.rotation | Object3D.rotation} for typical real-time mesh rotation. - * @param angle radians. Expects a `Float` - */ - rotateX(angle: number): this; - - /** - * Rotate the geometry about the Y axis. - * @remarks This is typically done as a one time operation, and not during a loop. - * @remarks Use {@link THREE.Object3D.rotation | Object3D.rotation} for typical real-time mesh rotation. - * @param angle radians. Expects a `Float` - */ - rotateY(angle: number): this; - - /** - * Rotate the geometry about the Z axis. - * @remarks This is typically done as a one time operation, and not during a loop. - * @remarks Use {@link THREE.Object3D.rotation | Object3D.rotation} for typical real-time mesh rotation. - * @param angle radians. Expects a `Float` - */ - rotateZ(angle: number): this; - - /** - * Translate the geometry. - * @remarks This is typically done as a one time operation, and not during a loop. - * @remarks Use {@link THREE.Object3D.position | Object3D.position} for typical real-time mesh rotation. - * @param x Expects a `Float` - * @param y Expects a `Float` - * @param z Expects a `Float` - */ - translate(x: number, y: number, z: number): this; - - /** - * Scale the geometry data. - * @remarks This is typically done as a one time operation, and not during a loop. - * @remarks Use {@link THREE.Object3D.scale | Object3D.scale} for typical real-time mesh scaling. - * @param x Expects a `Float` - * @param y Expects a `Float` - * @param z Expects a `Float` - */ - scale(x: number, y: number, z: number): this; - - /** - * Rotates the geometry to face a point in space. - * @remarks This is typically done as a one time operation, and not during a loop. - * @remarks Use {@link THREE.Object3D.lookAt | Object3D.lookAt} for typical real-time mesh usage. - * @param vector A world vector to look at. - */ - lookAt(vector: Vector3): this; - - /** - * Center the geometry based on the bounding box. - */ - center(): this; - - /** - * Sets the attributes for this BufferGeometry from an array of points. - * @param points - */ - setFromPoints(points: Vector3[] | Vector2[]): this; - - /** - * Computes the bounding box of the geometry, and updates the {@link .boundingBox} attribute. The bounding box is - * not computed by the engine; it must be computed by your app. You may need to recompute the bounding box if the - * geometry vertices are modified. - */ - computeBoundingBox(): void; - - /** - * Computes the bounding sphere of the geometry, and updates the {@link .boundingSphere} attribute. The engine - * automatically computes the bounding sphere when it is needed, e.g., for ray casting or view frustum culling. You - * may need to recompute the bounding sphere if the geometry vertices are modified. - */ - computeBoundingSphere(): void; - - /** - * Calculates and adds a tangent attribute to this geometry. - * The computation is only supported for indexed geometries and if position, normal, and uv attributes are defined - * @remarks - * When using a tangent space normal map, prefer the MikkTSpace algorithm provided by - * {@link BufferGeometryUtils.computeMikkTSpaceTangents} instead. - */ - computeTangents(): void; - - /** - * Computes vertex normals for the given vertex data. For indexed geometries, the method sets each vertex normal to - * be the average of the face normals of the faces that share that vertex. For non-indexed geometries, vertices are - * not shared, and the method sets each vertex normal to be the same as the face normal. - */ - computeVertexNormals(): void; - - /** - * Every normal vector in a geometry will have a magnitude of 1 - * @remarks This will correct lighting on the geometry surfaces. - */ - normalizeNormals(): void; - - /** - * Return a non-index version of an indexed BufferGeometry. - */ - toNonIndexed(): BufferGeometry; - - /** - * Convert the buffer geometry to three.js {@link https://github.com/mrdoob/three.js/wiki/JSON-Object-Scene-format-4 | JSON Object/Scene format}. - */ - toJSON(): BufferGeometryJSON; - - /** - * Creates a clone of this BufferGeometry - */ - clone(): this; - - /** - * Copies another BufferGeometry to this BufferGeometry. - * @param source - */ - copy(source: BufferGeometry): this; - - /** - * Frees the GPU-related resources allocated by this instance. - * @remarks Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/core/Clock.d.ts b/src-testing/src/core/Clock.d.ts deleted file mode 100644 index 05307083c..000000000 --- a/src-testing/src/core/Clock.d.ts +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Object for keeping track of time - * @remarks - * This uses {@link https://developer.mozilla.org/en-US/docs/Web/API/Performance/now | performance.now} if it is available, - * otherwise it reverts to the less accurate {@link https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/now | Date.now}. - * @see {@link https://threejs.org/docs/index.html#api/en/core/Clock | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/Clock.js | Source} - */ -export class Clock { - /** - * Create a new instance of {@link THREE.Clock | Clock} - * @param autoStart - Whether to automatically start the clock when {@link getDelta | .getDelta()} is called for the first time. Default `true` - */ - constructor(autoStart?: boolean); - - /** - * If set, starts the clock automatically when {@link getDelta | .getDelta()} is called for the first time. - * @defaultValue `true` - */ - autoStart: boolean; - - /** - * Holds the time at which the clock's {@link start | .start()} method was last called. - * @defaultValue `0` - */ - startTime: number; - - /** - * Holds the time at which the clock's {@link start | .start()}, {@link getElapsedTime | .getElapsedTime()} or {@link getDelta | .getDelta()} methods were last called. - * @defaultValue `0` - */ - oldTime: number; - - /** - * Keeps track of the total time that the clock has been running. - * @defaultValue `0` - */ - elapsedTime: number; - - /** - * Whether the clock is running or not. - * @defaultValue `false` - */ - running: boolean; - - /** - * Starts clock. - * @remarks - * Also sets the {@link startTime | .startTime} and {@link oldTime | .oldTime} to the current time, - * sets {@link elapsedTime | .elapsedTime} to `0` and {@link running | .running} to `true`. - */ - start(): void; - - /** - * Stops clock and sets {@link oldTime | oldTime} to the current time. - */ - stop(): void; - - /** - * Get the seconds passed since the clock started and sets {@link oldTime | .oldTime} to the current time. - * @remarks - * If {@link autoStart | .autoStart} is `true` and the clock is not running, also starts the clock. - */ - getElapsedTime(): number; - - /** - * Get the seconds passed since the time {@link oldTime | .oldTime} was set and sets {@link oldTime | .oldTime} to the current time. - * @remarks - * If {@link autoStart | .autoStart} is `true` and the clock is not running, also starts the clock. - */ - getDelta(): number; -} diff --git a/src-testing/src/core/EventDispatcher.d.ts b/src-testing/src/core/EventDispatcher.d.ts deleted file mode 100644 index fa8048a96..000000000 --- a/src-testing/src/core/EventDispatcher.d.ts +++ /dev/null @@ -1,85 +0,0 @@ -/** - * The minimal basic Event that can be dispatched by a {@link EventDispatcher<>}. - */ -export interface BaseEvent { - readonly type: TEventType; -} - -/** - * The minimal expected contract of a fired Event that was dispatched by a {@link EventDispatcher<>}. - */ -export interface Event { - readonly type: TEventType; - readonly target: TTarget; -} - -export type EventListener = ( - event: TEventData & Event, -) => void; - -/** - * JavaScript events for custom objects - * @example - * ```typescript - * // Adding events to a custom object - * class Car extends EventDispatcher { - * start() { - * this.dispatchEvent( { type: 'start', message: 'vroom vroom!' } ); - * } - * }; - * // Using events with the custom object - * const car = new Car(); - * car.addEventListener( 'start', ( event ) => { - * alert( event.message ); - * } ); - * car.start(); - * ``` - * @see {@link https://github.com/mrdoob/eventdispatcher.js | mrdoob EventDispatcher on GitHub} - * @see {@link https://threejs.org/docs/index.html#api/en/core/EventDispatcher | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/EventDispatcher.js | Source} - */ -export class EventDispatcher { - /** - * Creates {@link THREE.EventDispatcher | EventDispatcher} object. - */ - constructor(); - - /** - * Adds a listener to an event type. - * @param type The type of event to listen to. - * @param listener The function that gets called when the event is fired. - */ - addEventListener>( - type: T, - listener: EventListener, - ): void; - addEventListener(type: T, listener: EventListener<{}, T, this>): void; - - /** - * Checks if listener is added to an event type. - * @param type The type of event to listen to. - * @param listener The function that gets called when the event is fired. - */ - hasEventListener>( - type: T, - listener: EventListener, - ): boolean; - hasEventListener(type: T, listener: EventListener<{}, T, this>): boolean; - - /** - * Removes a listener from an event type. - * @param type The type of the listener that gets removed. - * @param listener The listener function that gets removed. - */ - removeEventListener>( - type: T, - listener: EventListener, - ): void; - removeEventListener(type: T, listener: EventListener<{}, T, this>): void; - - /** - * Fire an event type. - * @param event The event that gets fired. - */ - dispatchEvent>(event: BaseEvent & TEventMap[T]): void; -} diff --git a/src-testing/src/core/GLBufferAttribute.d.ts b/src-testing/src/core/GLBufferAttribute.d.ts deleted file mode 100644 index c549518dd..000000000 --- a/src-testing/src/core/GLBufferAttribute.d.ts +++ /dev/null @@ -1,121 +0,0 @@ -/** - * This buffer attribute class does not construct a VBO. - * Instead, it uses whatever VBO is passed in constructor and can later be altered via the {@link buffer | .buffer} property. - * @remarks - * It is required to pass additional params alongside the VBO - * Those are: the GL context, the GL data type, the number of components per vertex, the number of bytes per component, and the number of vertices. - * @remarks - * The most common use case for this class is when some kind of GPGPU calculation interferes or even produces the VBOs in question. - * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_glbufferattribute | WebGL / buffergeometry / glbufferattribute} - * @see {@link https://threejs.org/docs/index.html#api/en/core/GLBufferAttribute | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/GLBufferAttribute.js | Source} - */ -export class GLBufferAttribute { - /** - * This creates a new GLBufferAttribute object. - * @param buffer Must be a {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLBuffer | WebGLBuffer}. See {@link GLBufferAttribute.buffer | .buffer} - * @param type One of {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Constants#Data_types | WebGL Data Types}. See {@link GLBufferAttribute.type | .type} - * @param itemSize How many values make up each item (vertex). See {@link GLBufferAttribute.itemSize | .itemSize} - * @param elementSize `1`, `2` or `4`. The corresponding size (in bytes) for the given {@link type} param. See {@link GLBufferAttribute.elementSize | .elementSize} - * @param count The expected number of vertices in VBO. See {@link GLBufferAttribute.count | .count} - */ - constructor(buffer: WebGLBuffer, type: GLenum, itemSize: number, elementSize: 1 | 2 | 4, count: number); - - /** - * Read-only flag to check if a given object is of type {@link GLBufferAttribute}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isGLBufferAttribute: true; - - /** - * Optional name for this attribute instance. - * @defaultValue `""` - */ - name: string; - - /** - * The current {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLBuffer | WebGLBuffer} instance. - */ - buffer: WebGLBuffer; - - /** - * A {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Constants#Data_types | WebGL Data Type} describing the underlying VBO contents. - * - * #### WebGL Data Type (`GLenum`) - * - gl.BYTE: 0x1400 - * - gl.UNSIGNED_BYTE: 0x1401 - * - gl.SHORT: 0x1402 - * - gl.UNSIGNED_SHORT: 0x1403 - * - gl.INT: 0x1404 - * - gl.UNSIGNED_INT: 0x1405 - * - gl.FLOAT: 0x1406 - * @remarks Set this property together with {@link elementSize | .elementSize}. The recommended way is using the {@link setType | .setType()} method. - * @remarks Expects a `DataType` `GLenum` _possible values:_ `0x1400` `0x1401` `0x1402` `0x1403` `0x1404` `0x1405` `0x1406` - */ - type: GLenum; - - /** - * How many values make up each item (vertex). - * @remarks The number of values of the array that should be associated with a particular vertex. - * For instance, if this attribute is storing a 3-component vector (such as a position, normal, or color), then itemSize should be 3. - * @remarks Expects a `Integer` - */ - itemSize: number; - - /** - * Stores the corresponding size in bytes for the current {@link type | .type} property value. - * - * The corresponding size (_in bytes_) for the given "type" param. - * #### WebGL Data Type (`GLenum`) - * - gl.BYTE: 1 - * - gl.UNSIGNED_BYTE: 1 - * - gl.SHORT: 2 - * - gl.UNSIGNED_SHORT: 2 - * - gl.INT: 4 - * - gl.UNSIGNED_INT: 4 - * - gl.FLOAT: 4 - * @remarks Set this property together with {@link type | .type}. The recommended way is using the {@link setType | .setType} method. - * @see `constructor`` for a list of known type sizes. - * @remarks Expects a `1`, `2` or `4` - */ - elementSize: 1 | 2 | 4; - - /** - * The expected number of vertices in VBO. - * @remarks Expects a `Integer` - */ - count: number; - - /** - * A version number, incremented every time the needsUpdate property is set to true. - * @remarks Expects a `Integer` - */ - version: number; - - /** - * Setting this to true increments {@link version | .version}. - * @remarks _set-only property_. - */ - set needsUpdate(value: boolean); - - /** - * Sets the {@link buffer | .buffer} property. - */ - setBuffer(buffer: WebGLBuffer): this; - - /** - * Sets the both {@link GLBufferAttribute.type | type} and {@link GLBufferAttribute.elementSize | elementSize} properties. - */ - setType(type: GLenum, elementSize: 1 | 2 | 4): this; - - /** - * Sets the {@link GLBufferAttribute.itemSize | itemSize} property. - */ - setItemSize(itemSize: number): this; - - /** - * Sets the {@link GLBufferAttribute.count | count} property. - */ - setCount(count: number): this; -} diff --git a/src-testing/src/core/InstancedBufferAttribute.d.ts b/src-testing/src/core/InstancedBufferAttribute.d.ts deleted file mode 100644 index fde083b81..000000000 --- a/src-testing/src/core/InstancedBufferAttribute.d.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { BufferAttribute, TypedArray } from "./BufferAttribute.js"; - -/** - * An instanced version of {@link THREE.BufferAttribute | BufferAttribute}. - * @see {@link https://threejs.org/docs/index.html#api/en/core/InstancedBufferAttribute | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/InstancedBufferAttribute.js | Source} - */ -export class InstancedBufferAttribute extends BufferAttribute { - /** - * Create a new instance of {@link THREE.InstancedBufferAttribute | InstancedBufferAttribute} - * @param array - * @param itemSize - * @param normalized - * @param meshPerAttribute - */ - constructor(array: TypedArray, itemSize: number, normalized?: boolean, meshPerAttribute?: number); - - /** - * Defines how often a value of this buffer attribute should be repeated. - * A value of one means that each value of the instanced attribute is used for a single instance. - * A value of two means that each value is used for two consecutive instances (and so on). - * @defaultValue `1` - */ - meshPerAttribute: number; - - /** - * Read-only flag to check if a given object is of type {@link InstancedBufferAttribute}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isInstancedBufferAttribute: true; -} diff --git a/src-testing/src/core/InstancedBufferGeometry.d.ts b/src-testing/src/core/InstancedBufferGeometry.d.ts deleted file mode 100644 index 421294c49..000000000 --- a/src-testing/src/core/InstancedBufferGeometry.d.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { BufferGeometry } from "./BufferGeometry.js"; - -/** - * An instanced version of {@link THREE.BufferGeometry | BufferGeometry}. - * @see {@link https://threejs.org/docs/index.html#api/en/core/InstancedBufferGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/InstancedBufferGeometry.js | Source} - */ -export class InstancedBufferGeometry extends BufferGeometry { - /** - * Create a new instance of {@link InstancedBufferGeometry} - */ - constructor(); - - /** - * @defaultValue `InstancedBufferGeometry` - */ - type: string; - - /** - * Read-only flag to check if a given object is of type {@link InstancedBufferGeometry}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isInstancedBufferGeometry: true; - - /** - * @defaultValue `Infinity` - */ - instanceCount: number; - - /** - * Copies the given {@link InstancedBufferGeometry} to this instance. - * @param source - * @override - */ - copy(source: InstancedBufferGeometry): this; -} diff --git a/src-testing/src/core/InstancedInterleavedBuffer.d.ts b/src-testing/src/core/InstancedInterleavedBuffer.d.ts deleted file mode 100644 index 3c78beae4..000000000 --- a/src-testing/src/core/InstancedInterleavedBuffer.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { TypedArray } from "./BufferAttribute.js"; -import { InterleavedBuffer } from "./InterleavedBuffer.js"; - -/** - * An instanced version of {@link THREE.InterleavedBuffer | InterleavedBuffer}. - * @see {@link https://threejs.org/docs/index.html#api/en/core/InstancedInterleavedBuffer | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/InstancedInterleavedBuffer.js | Source} - */ -export class InstancedInterleavedBuffer extends InterleavedBuffer { - /** - * Create a new instance of {@link InstancedInterleavedBuffer} - * @param array - * @param itemSize - * @param meshPerAttribute - */ - constructor(array: TypedArray, stride: number, meshPerAttribute?: number); - - /** - * @defaultValue `1` - */ - meshPerAttribute: number; -} diff --git a/src-testing/src/core/InterleavedBuffer.d.ts b/src-testing/src/core/InterleavedBuffer.d.ts deleted file mode 100644 index 7b01ef5bf..000000000 --- a/src-testing/src/core/InterleavedBuffer.d.ts +++ /dev/null @@ -1,162 +0,0 @@ -import { Usage } from "../constants.js"; -import { TypedArray } from "./BufferAttribute.js"; -import { InterleavedBufferAttribute } from "./InterleavedBufferAttribute.js"; - -/** - * **"Interleaved"** means that multiple attributes, possibly of different types, (e.g., _position, normal, uv, color_) are packed into a single array buffer. - * An introduction into interleaved arrays can be found here: {@link https://blog.tojicode.com/2011/05/interleaved-array-basics.html | Interleaved array basics} - * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_points_interleaved | webgl / buffergeometry / points / interleaved} - * @see {@link https://threejs.org/docs/index.html#api/en/core/InterleavedBuffer | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/InterleavedBuffer.js | Source} - */ -export class InterleavedBuffer { - readonly isInterleavedBuffer: true; - - /** - * Create a new instance of {@link InterleavedBuffer} - * @param array A {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray | TypedArray} with a shared buffer. Stores the geometry data. - * @param stride The number of typed-array elements per vertex. Expects a `Integer` - */ - constructor(array: TypedArray, stride: number); - - /** - * A {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray | TypedArray} with a shared buffer. Stores the geometry data. - */ - array: TypedArray; - - /** - * The number of {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray | TypedArray} elements per vertex. - * @remarks Expects a `Integer` - */ - stride: number; - - /** - * Defines the intended usage pattern of the data store for optimization purposes. - * Corresponds to the {@link BufferAttribute.usage | usage} parameter of - * {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferData | WebGLRenderingContext.bufferData}. - * @remarks - * After the initial use of a buffer, its usage cannot be changed. Instead, instantiate a new one and set the desired usage before the next render. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/BufferAttributeUsage | Buffer Attribute Usage Constants} for all possible values. - * @see {@link BufferAttribute.setUsage | setUsage} - * @defaultValue {@link THREE.StaticDrawUsage | THREE.StaticDrawUsage}. - */ - usage: Usage; - - /** - * Object containing offset and count. - * @defaultValue `{ offset: number = 0; count: number = -1 }` - * @deprecated Will be removed in r169. Use "addUpdateRange()" instead. - */ - updateRange: { - /** @defaultValue `0` */ - offset: number; - /** @defaultValue `-1` */ - count: number; - }; - - /** - * This can be used to only update some components of stored data. Use the {@link .addUpdateRange} function to add - * ranges to this array. - */ - updateRanges: Array<{ - /** - * Position at which to start update. - */ - start: number; - /** - * The number of components to update. - */ - count: number; - }>; - - /** - * A version number, incremented every time the {@link BufferAttribute.needsUpdate | needsUpdate} property is set to true. - * @remarks Expects a `Integer` - * @defaultValue `0` - */ - version: number; - - /** - * Gives the total number of elements in the array. - * @remarks Expects a `Integer` - * @defaultValue 0 - */ - count: number; - - /** - * Flag to indicate that this attribute has changed and should be re-sent to the GPU. - * Set this to true when you modify the value of the array. - * @remarks Setting this to true also increments the {@link BufferAttribute.version | version}. - * @remarks _set-only property_. - */ - set needsUpdate(value: boolean); - - /** - * {@link http://en.wikipedia.org/wiki/Universally_unique_identifier | UUID} of this object instance. - * @remarks This gets automatically assigned and shouldn't be edited. - */ - uuid: string; - - /** - * Calls {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set | TypedArray.set}( {@link value}, {@link offset} ) - * on the {@link BufferAttribute.array | array}. - * @param value The source `TypedArray`. - * @param offset index of the {@link BufferAttribute.array | array} at which to start copying. Expects a `Integer`. Default `0`. - * @throws `RangeError` When {@link offset} is negative or is too large. - */ - set(value: ArrayLike, offset: number): this; - - /** - * Set {@link BufferAttribute.usage | usage} - * @remarks - * After the initial use of a buffer, its usage cannot be changed. Instead, instantiate a new one and set the desired usage before the next render. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/BufferAttributeUsage | Buffer Attribute Usage Constants} for all possible values. - * @see {@link BufferAttribute.usage | usage} - * @param value Corresponds to the {@link BufferAttribute.usage | usage} parameter of - * {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferData | WebGLRenderingContext.bufferData}. - */ - setUsage(value: Usage): this; - - /** - * Adds a range of data in the data array to be updated on the GPU. Adds an object describing the range to the - * {@link .updateRanges} array. - */ - addUpdateRange(start: number, count: number): void; - - /** - * Clears the {@link .updateRanges} array. - */ - clearUpdateRanges(): void; - - /** - * Copies another {@link InterleavedBuffer} to this {@link InterleavedBuffer} instance. - * @param source - */ - copy(source: InterleavedBuffer): this; - - /** - * Copies data from {@link attribute}[{@link index2}] to {@link InterleavedBuffer.array | array}[{@link index1}]. - * @param index1 Expects a `Integer` - * @param attribute - * @param index2 Expects a `Integer` - */ - copyAt(index1: number, attribute: InterleavedBufferAttribute, index2: number): this; - - /** - * Creates a clone of this {@link InterleavedBuffer}. - * @param data This object holds shared array buffers required for properly cloning geometries with interleaved attributes. - */ - clone(data: {}): InterleavedBuffer; - - /** - * Serializes this {@link InterleavedBuffer}. - * Converting to {@link https://github.com/mrdoob/three.js/wiki/JSON-Geometry-format-4 | JSON Geometry format v4}, - * @param data This object holds shared array buffers required for properly serializing geometries with interleaved attributes. - */ - toJSON(data: {}): { - uuid: string; - buffer: string; - type: string; - stride: number; - }; -} diff --git a/src-testing/src/core/InterleavedBufferAttribute.d.ts b/src-testing/src/core/InterleavedBufferAttribute.d.ts deleted file mode 100644 index 955049938..000000000 --- a/src-testing/src/core/InterleavedBufferAttribute.d.ts +++ /dev/null @@ -1,201 +0,0 @@ -import { Matrix3 } from "../math/Matrix3.js"; -import { Matrix4 } from "../math/Matrix4.js"; -import { BufferAttribute, TypedArray } from "./BufferAttribute.js"; -import { InterleavedBuffer } from "./InterleavedBuffer.js"; - -/** - * @see {@link https://threejs.org/docs/index.html#api/en/core/InterleavedBufferAttribute | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/InterleavedBufferAttribute.js | Source} - */ -export class InterleavedBufferAttribute { - /** - * Create a new instance of {@link THREE.InterleavedBufferAttribute | InterleavedBufferAttribute}. - * @param interleavedBuffer - * @param itemSize - * @param offset - * @param normalized Default `false`. - */ - constructor(interleavedBuffer: InterleavedBuffer, itemSize: number, offset: number, normalized?: boolean); - - /** - * Optional name for this attribute instance. - * @defaultValue `''` - */ - name: string; - - /** - * The {@link InterleavedBuffer | InterleavedBuffer} instance passed in the constructor. - */ - data: InterleavedBuffer; - - /** - * How many values make up each item. - * @remarks Expects a `Integer` - */ - itemSize: number; - - /** - * The offset in the underlying array buffer where an item starts. - * @remarks Expects a `Integer` - */ - offset: number; - - /** - * @defaultValue `false` - */ - normalized: boolean; - - /** - * The value of {@link data | .data}.{@link InterleavedBuffer.count | count}. - * If the buffer is storing a 3-component item (such as a _position, normal, or color_), then this will count the number of such items stored. - * @remarks _get-only property_. - * @remarks Expects a `Integer` - */ - get count(): number; - - /** - * The value of {@link InterleavedBufferAttribute.data | data}.{@link InterleavedBuffer.array | array}. - * @remarks _get-only property_. - */ - get array(): TypedArray; - - /** - * Flag to indicate that the {@link data | .data} ({@link InterleavedBuffer}) attribute has changed and should be re-sent to the GPU. - * @remarks Setting this to have the same result of setting true also increments the {@link InterleavedBuffer.needsUpdate | InterleavedBuffer.needsUpdate} of {@link data | .data}. - * @remarks Setting this to true also increments the {@link InterleavedBuffer.version | InterleavedBuffer.version}. - * @remarks _set-only property_. - */ - set needsUpdate(value: boolean); - - /** - * Read-only flag to check if a given object is of type {@link InterleavedBufferAttribute}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isInterleavedBufferAttribute: true; - - /** - * Applies matrix {@link Matrix4 | m} to every Vector3 element of this InterleavedBufferAttribute. - * @param m - */ - applyMatrix4(m: Matrix4): this; - - /** - * Applies normal matrix {@link Matrix3 | m} to every Vector3 element of this InterleavedBufferAttribute. - * @param m - */ - applyNormalMatrix(m: Matrix3): this; - - /** - * Applies matrix {@link Matrix4 | m} to every Vector3 element of this InterleavedBufferAttribute, interpreting the elements as a direction vectors. - * @param m - */ - transformDirection(m: Matrix4): this; - - /** - * Returns the given component of the vector at the given index. - */ - getComponent(index: number, component: number): number; - - /** - * Sets the given component of the vector at the given index. - */ - setComponent(index: number, component: number, value: number): this; - - /** - * Returns the x component of the item at the given index. - * @param index Expects a `Integer` - */ - getX(index: number): number; - - /** - * Sets the x component of the item at the given index. - * @param index Expects a `Integer` - * @param x Expects a `Float` - */ - setX(index: number, x: number): this; - - /** - * Returns the y component of the item at the given index. - * @param index Expects a `Integer` - */ - getY(index: number): number; - - /** - * Sets the y component of the item at the given index. - * @param index Expects a `Integer` - * @param y Expects a `Float` - */ - setY(index: number, y: number): this; - - /** - * Returns the z component of the item at the given index. - * @param index Expects a `Integer` - */ - getZ(index: number): number; - - /** - * Sets the z component of the item at the given index. - * @param index Expects a `Integer` - * @param z Expects a `Float` - */ - setZ(index: number, z: number): this; - - /** - * Returns the w component of the item at the given index. - * @param index Expects a `Integer` - */ - getW(index: number): number; - - /** - * Sets the w component of the item at the given index. - * @param index Expects a `Integer` - * @param w Expects a `Float` - */ - setW(index: number, z: number): this; - - /** - * Sets the x and y components of the item at the given index. - * @param index Expects a `Integer` - * @param x Expects a `Float` - * @param y Expects a `Float` - */ - setXY(index: number, x: number, y: number): this; - /** - * Sets the x, y and z components of the item at the given index. - * @param index Expects a `Integer` - * @param x Expects a `Float` - * @param y Expects a `Float` - * @param z Expects a `Float` - */ - setXYZ(index: number, x: number, y: number, z: number): this; - - /** - * Sets the x, y, z and w components of the item at the given index. - * @param index Expects a `Integer` - * @param x Expects a `Float` - * @param y Expects a `Float` - * @param z Expects a `Float` - * @param w Expects a `Float` - */ - setXYZW(index: number, x: number, y: number, z: number, w: number): this; - - /** - * Creates a clone of this {@link InterleavedBufferAttribute}. - * @param data This object holds shared array buffers required for properly cloning geometries with interleaved attributes. - */ - clone(data?: {}): BufferAttribute; - - /** - * Serializes this {@link InterleavedBufferAttribute}. - * Converting to {@link https://github.com/mrdoob/three.js/wiki/JSON-Geometry-format-4 | JSON Geometry format v4}, - * @param data This object holds shared array buffers required for properly serializing geometries with interleaved attributes. - */ - toJSON(data?: {}): { - isInterleavedBufferAttribute: true; - itemSize: number; - data: string; - offset: number; - normalized: boolean; - }; -} diff --git a/src-testing/src/core/Layers.d.ts b/src-testing/src/core/Layers.d.ts deleted file mode 100644 index ad95f892d..000000000 --- a/src-testing/src/core/Layers.d.ts +++ /dev/null @@ -1,72 +0,0 @@ -/** - * A {@link THREE.Layers | Layers} object assigns an {@link THREE.Object3D | Object3D} to 1 or more of 32 layers numbered `0` to `31` - internally the - * layers are stored as a {@link https://en.wikipedia.org/wiki/Mask_(computing) | bit mask}, and - * by default all Object3Ds are a member of layer `0`. - * @remarks - * This can be used to control visibility - an object must share a layer with a {@link Camera | camera} to be visible when that camera's view is rendered. - * @remarks - * All classes that inherit from {@link THREE.Object3D | Object3D} have an {@link THREE.Object3D.layers | Object3D.layers} property which is an instance of this class. - * @see Example: {@link https://threejs.org/examples/#webgl_layers | WebGL / layers} - * @see Example: {@link https://threejs.org/examples/#webxr_vr_layers | Webxr / vr / layers} - * @see {@link https://threejs.org/docs/index.html#api/en/core/Layers | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/Layers.js | Source} - */ -export class Layers { - /** - * Create a new Layers object, with membership initially set to layer 0. - */ - constructor(); - - /** - * A bit mask storing which of the 32 layers this layers object is currently a member of. - * @defaultValue `1 | 0` - * @remarks Expects a `Integer` - */ - mask: number; - - /** - * Set membership to `layer`, and remove membership all other layers. - * @param layer An integer from 0 to 31. - */ - set(layer: number): void; - - /** - * Add membership of this `layer`. - * @param layer An integer from 0 to 31. - */ - enable(layer: number): void; - - /** - * Add membership to all layers. - */ - enableAll(): void; - - /** - * Toggle membership of `layer`. - * @param layer An integer from 0 to 31. - */ - toggle(layer: number): void; - - /** - * Remove membership of this `layer`. - * @param layer An integer from 0 to 31. - */ - disable(layer: number): void; - - /** - * Remove membership from all layers. - */ - disableAll(): void; - - /** - * Returns true if this and the passed `layers` object have at least one layer in common. - * @param layers A Layers object - */ - test(layers: Layers): boolean; - - /** - * Returns true if the given layer is enabled. - * @param layer An integer from 0 to 31. - */ - isEnabled(layer: number): boolean; -} diff --git a/src-testing/src/core/Object3D.d.ts b/src-testing/src/core/Object3D.d.ts deleted file mode 100644 index 1c35b6e09..000000000 --- a/src-testing/src/core/Object3D.d.ts +++ /dev/null @@ -1,672 +0,0 @@ -import { AnimationClip, AnimationClipJSON } from "../animation/AnimationClip.js"; -import { Camera } from "../cameras/Camera.js"; -import { ShapeJSON } from "../extras/core/Shape.js"; -import { Material, MaterialJSON } from "../materials/Material.js"; -import { Euler } from "../math/Euler.js"; -import { Matrix3 } from "../math/Matrix3.js"; -import { Matrix4, Matrix4Tuple } from "../math/Matrix4.js"; -import { Quaternion } from "../math/Quaternion.js"; -import { Vector3, Vector3Tuple } from "../math/Vector3.js"; -import { Group } from "../objects/Group.js"; -import { SkeletonJSON } from "../objects/Skeleton.js"; -import { WebGLRenderer } from "../renderers/WebGLRenderer.js"; -import { Scene } from "../scenes/Scene.js"; -import { SourceJSON } from "../textures/Source.js"; -import { TextureJSON } from "../textures/Texture.js"; -import { BufferGeometry, BufferGeometryJSON } from "./BufferGeometry.js"; -import { EventDispatcher } from "./EventDispatcher.js"; -import { Layers } from "./Layers.js"; -import { Intersection, Raycaster } from "./Raycaster.js"; - -export interface Object3DJSONObject { - uuid: string; - type: string; - - name?: string; - castShadow?: boolean; - receiveShadow?: boolean; - visible?: boolean; - frustumCulled?: boolean; - renderOrder?: number; - userData?: Record; - - layers: number; - matrix: Matrix4Tuple; - up: Vector3Tuple; - - matrixAutoUpdate?: boolean; - - material?: string | string[]; - - children?: string[]; - - animations?: string[]; -} - -export interface Object3DJSON { - metadata?: { version: number; type: string; generator: string }; - object: Object3DJSONObject; -} - -export interface JSONMeta { - geometries: Record; - materials: Record; - textures: Record; - images: Record; - shapes: Record; - skeletons: Record; - animations: Record; - nodes: Record; -} - -export interface Object3DEventMap { - /** - * Fires when the object has been added to its parent object. - */ - added: {}; - - /** - * Fires when the object has been removed from its parent object. - */ - removed: {}; - - /** - * Fires when a new child object has been added. - */ - childadded: { child: Object3D }; - - /** - * Fires when a new child object has been removed. - */ - childremoved: { child: Object3D }; -} - -/** - * This is the base class for most objects in three.js and provides a set of properties and methods for manipulating objects in 3D space. - * @remarks Note that this can be used for grouping objects via the {@link THREE.Object3D.add | .add()} method which adds the object as a child, - * however it is better to use {@link THREE.Group | Group} for this. - * @see {@link https://threejs.org/docs/index.html#api/en/core/Object3D | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/Object3D.js | Source} - */ -export class Object3D extends EventDispatcher { - /** - * This creates a new {@link Object3D} object. - */ - constructor(); - - /** - * Flag to check if a given object is of type {@link Object3D}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isObject3D: true; - - /** - * Unique number for this {@link Object3D} instance. - * @remarks Note that ids are assigned in chronological order: 1, 2, 3, ..., incrementing by one for each new object. - * Expects a `Integer` - */ - readonly id: number; - - /** - * {@link http://en.wikipedia.org/wiki/Universally_unique_identifier | UUID} of this object instance. - * @remarks This gets automatically assigned and shouldn't be edited. - */ - uuid: string; - - /** - * Optional name of the object - * @remarks _(doesn't need to be unique)_. - * @defaultValue `""` - */ - name: string; - - /** - * A Read-only _string_ to check `this` object type. - * @remarks This can be used to find a specific type of Object3D in a scene. - * Sub-classes will update this value. - * @defaultValue `Object3D` - */ - readonly type: string | "Object3D"; - - /** - * Object's parent in the {@link https://en.wikipedia.org/wiki/Scene_graph | scene graph}. - * @remarks An object can have at most one parent. - * @defaultValue `null` - */ - parent: Object3D | null; - - /** - * Array with object's children. - * @see {@link THREE.Object3DGroup | Group} for info on manually grouping objects. - * @defaultValue `[]` - */ - - children: Object3D[]; - - /** - * This is used by the {@link lookAt | lookAt} method, for example, to determine the orientation of the result. - * @defaultValue {@link DEFAULT_UP | Object3D.DEFAULT_UP} - that is `(0, 1, 0)`. - */ - up: Vector3; - - /** - * Object's local position. - * @defaultValue `new THREE.Vector3()` - that is `(0, 0, 0)`. - */ - readonly position: Vector3; - - /** - * Object's local rotation ({@link https://en.wikipedia.org/wiki/Euler_angles | Euler angles}), in radians. - * @defaultValue `new THREE.Euler()` - that is `(0, 0, 0, Euler.DEFAULT_ORDER)`. - */ - readonly rotation: Euler; - - /** - * Object's local rotation as a {@link THREE.Quaternion | Quaternion}. - * @defaultValue `new THREE.Quaternion()` - that is `(0, 0, 0, 1)`. - */ - readonly quaternion: Quaternion; - - /** - * The object's local scale. - * @defaultValue `new THREE.Vector3( 1, 1, 1 )` - */ - readonly scale: Vector3; - - /** - * @defaultValue `new THREE.Matrix4()` - */ - readonly modelViewMatrix: Matrix4; - - /** - * @defaultValue `new THREE.Matrix3()` - */ - readonly normalMatrix: Matrix3; - - /** - * The local transform matrix. - * @defaultValue `new THREE.Matrix4()` - */ - matrix: Matrix4; - - /** - * The global transform of the object. - * @remarks If the {@link Object3D} has no parent, then it's identical to the local transform {@link THREE.Object3D.matrix | .matrix}. - * @defaultValue `new THREE.Matrix4()` - */ - matrixWorld: Matrix4; - - /** - * When this is set, it calculates the matrix of position, (rotation or quaternion) and - * scale every frame and also recalculates the matrixWorld property. - * @defaultValue {@link DEFAULT_MATRIX_AUTO_UPDATE} - that is `(true)`. - */ - matrixAutoUpdate: boolean; - - /** - * If set, then the renderer checks every frame if the object and its children need matrix updates. - * When it isn't, then you have to maintain all matrices in the object and its children yourself. - * @defaultValue {@link DEFAULT_MATRIX_WORLD_AUTO_UPDATE} - that is `(true)`. - */ - matrixWorldAutoUpdate: boolean; - - /** - * When this is set, it calculates the matrixWorld in that frame and resets this property to false. - * @defaultValue `false` - */ - matrixWorldNeedsUpdate: boolean; - - /** - * The layer membership of the object. - * @remarks The object is only visible if it has at least one layer in common with the {@link THREE.Object3DCamera | Camera} in use. - * This property can also be used to filter out unwanted objects in ray-intersection tests when using {@link THREE.Raycaster | Raycaster}. - * @defaultValue `new THREE.Layers()` - */ - layers: Layers; - - /** - * Object gets rendered if `true`. - * @defaultValue `true` - */ - visible: boolean; - - /** - * Whether the object gets rendered into shadow map. - * @defaultValue `false` - */ - castShadow: boolean; - - /** - * Whether the material receives shadows. - * @defaultValue `false` - */ - receiveShadow: boolean; - - /** - * When this is set, it checks every frame if the object is in the frustum of the camera before rendering the object. - * If set to `false` the object gets rendered every frame even if it is not in the frustum of the camera. - * @defaultValue `true` - */ - frustumCulled: boolean; - - /** - * This value allows the default rendering order of {@link https://en.wikipedia.org/wiki/Scene_graph | scene graph} - * objects to be overridden although opaque and transparent objects remain sorted independently. - * @remarks When this property is set for an instance of {@link Group | Group}, all descendants objects will be sorted and rendered together. - * Sorting is from lowest to highest renderOrder. - * @defaultValue `0` - */ - renderOrder: number; - - /** - * Array with object's animation clips. - * @defaultValue `[]` - */ - animations: AnimationClip[]; - - /** - * An object that can be used to store custom data about the {@link Object3D}. - * @remarks It should not hold references to _functions_ as these **will not** be cloned. - * @default `{}` - */ - userData: Record; - - /** - * Custom depth material to be used when rendering to the depth map. - * @remarks Can only be used in context of meshes. - * When shadow-casting with a {@link THREE.DirectionalLight | DirectionalLight} or {@link THREE.SpotLight | SpotLight}, - * if you are modifying vertex positions in the vertex shader you must specify a customDepthMaterial for proper shadows. - * @defaultValue `undefined` - */ - customDepthMaterial?: Material | undefined; - - /** - * Same as {@link customDepthMaterial}, but used with {@link THREE.Object3DPointLight | PointLight}. - * @defaultValue `undefined` - */ - customDistanceMaterial?: Material | undefined; - - /** - * An optional callback that is executed immediately before a 3D object is rendered to a shadow map. - * @remarks This function is called with the following parameters: renderer, scene, camera, shadowCamera, geometry, - * depthMaterial, group. - * Please notice that this callback is only executed for `renderable` 3D objects. Meaning 3D objects which - * define their visual appearance with geometries and materials like instances of {@link Mesh}, {@link Line}, - * {@link Points} or {@link Sprite}. Instances of {@link Object3D}, {@link Group} or {@link Bone} are not renderable - * and thus this callback is not executed for such objects. - */ - onBeforeShadow( - renderer: WebGLRenderer, - scene: Scene, - shadowCamera: Camera, - geometry: BufferGeometry, - depthMaterial: Material, - group: Group, - ): void; - - /** - * An optional callback that is executed immediately after a 3D object is rendered to a shadow map. - * @remarks This function is called with the following parameters: renderer, scene, camera, shadowCamera, geometry, - * depthMaterial, group. - * Please notice that this callback is only executed for `renderable` 3D objects. Meaning 3D objects which - * define their visual appearance with geometries and materials like instances of {@link Mesh}, {@link Line}, - * {@link Points} or {@link Sprite}. Instances of {@link Object3D}, {@link Group} or {@link Bone} are not renderable - * and thus this callback is not executed for such objects. - */ - onAfterShadow( - renderer: WebGLRenderer, - scene: Scene, - shadowCamera: Camera, - geometry: BufferGeometry, - depthMaterial: Material, - group: Group, - ): void; - - /** - * An optional callback that is executed immediately before a 3D object is rendered. - * @remarks This function is called with the following parameters: renderer, scene, camera, geometry, material, group. - * Please notice that this callback is only executed for `renderable` 3D objects. Meaning 3D objects which - * define their visual appearance with geometries and materials like instances of {@link Mesh}, {@link Line}, - * {@link Points} or {@link Sprite}. Instances of {@link Object3D}, {@link Group} or {@link Bone} are not renderable - * and thus this callback is not executed for such objects. - */ - onBeforeRender( - renderer: WebGLRenderer, - scene: Scene, - camera: Camera, - geometry: BufferGeometry, - material: Material, - group: Group, - ): void; - - /** - * An optional callback that is executed immediately after a 3D object is rendered. - * @remarks This function is called with the following parameters: renderer, scene, camera, geometry, material, group. - * Please notice that this callback is only executed for `renderable` 3D objects. Meaning 3D objects which - * define their visual appearance with geometries and materials like instances of {@link Mesh}, {@link Line}, - * {@link Points} or {@link Sprite}. Instances of {@link Object3D}, {@link Group} or {@link Bone} are not renderable - * and thus this callback is not executed for such objects. - */ - onAfterRender( - renderer: WebGLRenderer, - scene: Scene, - camera: Camera, - geometry: BufferGeometry, - material: Material, - group: Group, - ): void; - - /** - * The default {@link up} direction for objects, also used as the default position for {@link THREE.DirectionalLight | DirectionalLight}, - * {@link THREE.HemisphereLight | HemisphereLight} and {@link THREE.Spotlight | Spotlight} (which creates lights shining from the top down). - * @defaultValue `new THREE.Vector3( 0, 1, 0)` - */ - static DEFAULT_UP: Vector3; - - /** - * The default setting for {@link matrixAutoUpdate} for newly created Object3Ds. - * @defaultValue `true` - */ - static DEFAULT_MATRIX_AUTO_UPDATE: boolean; - - /** - * The default setting for {@link matrixWorldAutoUpdate} for newly created Object3Ds. - * @defaultValue `true` - */ - static DEFAULT_MATRIX_WORLD_AUTO_UPDATE: boolean; - - /** - * Applies the matrix transform to the object and updates the object's position, rotation and scale. - * @param matrix - */ - applyMatrix4(matrix: Matrix4): void; - - /** - * Applies the rotation represented by the quaternion to the object. - * @param quaternion - */ - applyQuaternion(quaternion: Quaternion): this; - - /** - * Calls {@link THREE.Quaternion.setFromAxisAngle | setFromAxisAngle}({@link axis}, {@link angle}) on the {@link quaternion | .quaternion}. - * @param axis A normalized vector in object space. - * @param angle Angle in radians. Expects a `Float` - */ - setRotationFromAxisAngle(axis: Vector3, angle: number): void; - - /** - * Calls {@link THREE.Quaternion.setFromEuler | setFromEuler}({@link euler}) on the {@link quaternion | .quaternion}. - * @param euler Euler angle specifying rotation amount. - */ - setRotationFromEuler(euler: Euler): void; - - /** - * Calls {@link THREE.Quaternion.setFromRotationMatrix | setFromRotationMatrix}({@link m}) on the {@link quaternion | .quaternion}. - * @remarks Note that this assumes that the upper 3x3 of m is a pure rotation matrix (i.e, unscaled). - * @param m Rotate the quaternion by the rotation component of the matrix. - */ - setRotationFromMatrix(m: Matrix4): void; - - /** - * Copy the given {@link THREE.Quaternion | Quaternion} into {@link quaternion | .quaternion}. - * @param q Normalized Quaternion. - */ - setRotationFromQuaternion(q: Quaternion): void; - - /** - * Rotate an object along an axis in object space. - * @remarks The axis is assumed to be normalized. - * @param axis A normalized vector in object space. - * @param angle The angle in radians. Expects a `Float` - */ - rotateOnAxis(axis: Vector3, angle: number): this; - - /** - * Rotate an object along an axis in world space. - * @remarks The axis is assumed to be normalized - * Method Assumes no rotated parent. - * @param axis A normalized vector in world space. - * @param angle The angle in radians. Expects a `Float` - */ - rotateOnWorldAxis(axis: Vector3, angle: number): this; - - /** - * Rotates the object around _x_ axis in local space. - * @param rad The angle to rotate in radians. Expects a `Float` - */ - rotateX(angle: number): this; - - /** - * Rotates the object around _y_ axis in local space. - * @param rad The angle to rotate in radians. Expects a `Float` - */ - rotateY(angle: number): this; - - /** - * Rotates the object around _z_ axis in local space. - * @param rad The angle to rotate in radians. Expects a `Float` - */ - rotateZ(angle: number): this; - - /** - * Translate an object by distance along an axis in object space - * @remarks The axis is assumed to be normalized. - * @param axis A normalized vector in object space. - * @param distance The distance to translate. Expects a `Float` - */ - translateOnAxis(axis: Vector3, distance: number): this; - - /** - * Translates object along x axis in object space by {@link distance} units. - * @param distance Expects a `Float` - */ - translateX(distance: number): this; - - /** - * Translates object along _y_ axis in object space by {@link distance} units. - * @param distance Expects a `Float` - */ - translateY(distance: number): this; - - /** - * Translates object along _z_ axis in object space by {@link distance} units. - * @param distance Expects a `Float` - */ - translateZ(distance: number): this; - - /** - * Converts the vector from this object's local space to world space. - * @param vector A vector representing a position in this object's local space. - */ - localToWorld(vector: Vector3): Vector3; - - /** - * Converts the vector from world space to this object's local space. - * @param vector A vector representing a position in world space. - */ - worldToLocal(vector: Vector3): Vector3; - - /** - * Rotates the object to face a point in world space. - * @remarks This method does not support objects having non-uniformly-scaled parent(s). - * @param vector A vector representing a position in world space to look at. - */ - lookAt(vector: Vector3): void; - /** - * Rotates the object to face a point in world space. - * @remarks This method does not support objects having non-uniformly-scaled parent(s). - * @param x Expects a `Float` - * @param y Expects a `Float` - * @param z Expects a `Float` - */ - lookAt(x: number, y: number, z: number): void; - - /** - * Adds another {@link Object3D} as child of this {@link Object3D}. - * @remarks An arbitrary number of objects may be added - * Any current parent on an {@link object} passed in here will be removed, since an {@link Object3D} can have at most one parent. - * @see {@link attach} - * @see {@link THREE.Group | Group} for info on manually grouping objects. - * @param object - */ - add(...object: Object3D[]): this; - - /** - * Removes a {@link Object3D} as child of this {@link Object3D}. - * @remarks An arbitrary number of objects may be removed. - * @see {@link THREE.Group | Group} for info on manually grouping objects. - * @param object - */ - remove(...object: Object3D[]): this; - - /** - * Removes this object from its current parent. - */ - removeFromParent(): this; - - /** - * Removes all child objects. - */ - clear(): this; - - /** - * Adds a {@link Object3D} as a child of this, while maintaining the object's world transform. - * @remarks Note: This method does not support scene graphs having non-uniformly-scaled nodes(s). - * @see {@link add} - * @param object - */ - attach(object: Object3D): this; - - /** - * Searches through an object and its children, starting with the object itself, and returns the first with a matching id. - * @remarks Note that ids are assigned in chronological order: 1, 2, 3, ..., incrementing by one for each new object. - * @see {@link id} - * @param id Unique number of the object instance. Expects a `Integer` - */ - getObjectById(id: number): Object3D | undefined; - - /** - * Searches through an object and its children, starting with the object itself, and returns the first with a matching name. - * @remarks Note that for most objects the name is an empty string by default - * You will have to set it manually to make use of this method. - * @param name String to match to the children's Object3D.name property. - */ - getObjectByName(name: string): Object3D | undefined; - - /** - * Searches through an object and its children, starting with the object itself, - * and returns the first with a property that matches the value given. - * - * @param name - the property name to search for. - * @param value - value of the given property. - */ - getObjectByProperty(name: string, value: any): Object3D | undefined; - - /** - * Searches through an object and its children, starting with the object itself, - * and returns the first with a property that matches the value given. - * @param name The property name to search for. - * @param value Value of the given property. - * @param optionalTarget target to set the result. Otherwise a new Array is instantiated. If set, you must clear - * this array prior to each call (i.e., array.length = 0;). - */ - getObjectsByProperty(name: string, value: any, optionalTarget?: Object3D[]): Object3D[]; - - /** - * Returns a vector representing the position of the object in world space. - * @param target The result will be copied into this Vector3. - */ - getWorldPosition(target: Vector3): Vector3; - - /** - * Returns a quaternion representing the rotation of the object in world space. - * @param target The result will be copied into this Quaternion. - */ - getWorldQuaternion(target: Quaternion): Quaternion; - - /** - * Returns a vector of the scaling factors applied to the object for each axis in world space. - * @param target The result will be copied into this Vector3. - */ - getWorldScale(target: Vector3): Vector3; - - /** - * Returns a vector representing the direction of object's positive z-axis in world space. - * @param target The result will be copied into this Vector3. - */ - getWorldDirection(target: Vector3): Vector3; - - /** - * Abstract (empty) method to get intersections between a casted ray and this object - * @remarks Subclasses such as {@link THREE.Mesh | Mesh}, {@link THREE.Line | Line}, and {@link THREE.Points | Points} implement this method in order to use raycasting. - * @see {@link THREE.Raycaster | Raycaster} - * @param raycaster - * @param intersects - * @defaultValue `() => {}` - */ - raycast(raycaster: Raycaster, intersects: Intersection[]): void; - - /** - * Executes the callback on this object and all descendants. - * @remarks Note: Modifying the scene graph inside the callback is discouraged. - * @param callback A function with as first argument an {@link Object3D} object. - */ - traverse(callback: (object: Object3D) => any): void; - - /** - * Like traverse, but the callback will only be executed for visible objects - * @remarks Descendants of invisible objects are not traversed. - * Note: Modifying the scene graph inside the callback is discouraged. - * @param callback A function with as first argument an {@link Object3D} object. - */ - traverseVisible(callback: (object: Object3D) => any): void; - - /** - * Executes the callback on all ancestors. - * @remarks Note: Modifying the scene graph inside the callback is discouraged. - * @param callback A function with as first argument an {@link Object3D} object. - */ - traverseAncestors(callback: (object: Object3D) => any): void; - - /** - * Updates local transform. - */ - updateMatrix(): void; - - /** - * Updates the global transform of the object. - * And will update the object descendants if {@link matrixWorldNeedsUpdate | .matrixWorldNeedsUpdate} is set to true or if the {@link force} parameter is set to `true`. - * @param force A boolean that can be used to bypass {@link matrixWorldAutoUpdate | .matrixWorldAutoUpdate}, to recalculate the world matrix of the object and descendants on the current frame. - * Useful if you cannot wait for the renderer to update it on the next frame, assuming {@link matrixWorldAutoUpdate | .matrixWorldAutoUpdate} set to `true`. - */ - updateMatrixWorld(force?: boolean): void; - - /** - * Updates the global transform of the object. - * @param updateParents Recursively updates global transform of ancestors. - * @param updateChildren Recursively updates global transform of descendants. - */ - updateWorldMatrix(updateParents: boolean, updateChildren: boolean): void; - - /** - * Convert the object to three.js {@link https://github.com/mrdoob/three.js/wiki/JSON-Object-Scene-format-4 | JSON Object/Scene format}. - * @param meta Object containing metadata such as materials, textures or images for the object. - */ - toJSON(meta?: JSONMeta): Object3DJSON; - - /** - * Returns a clone of `this` object and optionally all descendants. - * @param recursive If true, descendants of the object are also cloned. Default `true` - */ - clone(recursive?: boolean): this; - - /** - * Copies the given object into this object. - * @remarks Event listeners and user-defined callbacks ({@link .onAfterRender} and {@link .onBeforeRender}) are not copied. - * @param object - * @param recursive If set to `true`, descendants of the object are copied next to the existing ones. If set to - * `false`, descendants are left unchanged. Default is `true`. - */ - copy(object: Object3D, recursive?: boolean): this; -} diff --git a/src-testing/src/core/Raycaster.d.ts b/src-testing/src/core/Raycaster.d.ts deleted file mode 100644 index f3c4d44a0..000000000 --- a/src-testing/src/core/Raycaster.d.ts +++ /dev/null @@ -1,207 +0,0 @@ -import { Camera } from "../cameras/Camera.js"; -import { Ray } from "../math/Ray.js"; -import { Vector2 } from "../math/Vector2.js"; -import { Vector3 } from "../math/Vector3.js"; -import { XRTargetRaySpace } from "../renderers/webxr/WebXRController.js"; -import { Layers } from "./Layers.js"; -import { Object3D } from "./Object3D.js"; - -export interface Face { - a: number; - b: number; - c: number; - normal: Vector3; - materialIndex: number; -} - -export interface Intersection { - /** Distance between the origin of the ray and the intersection */ - distance: number; - distanceToRay?: number | undefined; - /** Point of intersection, in world coordinates */ - point: Vector3; - index?: number | undefined; - /** Intersected face */ - face?: Face | null | undefined; - /** Index of the intersected face */ - faceIndex?: number | undefined; - /** The intersected object */ - object: TIntersected; - uv?: Vector2 | undefined; - uv1?: Vector2 | undefined; - normal?: Vector3; - /** The index number of the instance where the ray intersects the {@link THREE.InstancedMesh | InstancedMesh } */ - instanceId?: number | undefined; - pointOnLine?: Vector3; - batchId?: number; -} - -export interface RaycasterParameters { - Mesh: any; - Line: { threshold: number }; - Line2?: { threshold: number }; - LOD: any; - Points: { threshold: number }; - Sprite: any; -} - -/** - * This class is designed to assist with {@link https://en.wikipedia.org/wiki/Ray_casting | raycasting} - * @remarks - * Raycasting is used for mouse picking (working out what objects in the 3d space the mouse is over) amongst other things. - * @example - * ```typescript - * const raycaster = new THREE.Raycaster(); - * const pointer = new THREE.Vector2(); - * - * function onPointerMove(event) { - * // calculate pointer position in normalized device coordinates (-1 to +1) for both components - * pointer.x = (event.clientX / window.innerWidth) * 2 - 1; - * pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; - * } - * - * function render() { - * // update the picking ray with the camera and pointer position - * raycaster.setFromCamera(pointer, camera); - * // calculate objects intersecting the picking ray - * const intersects = raycaster.intersectObjects(scene.children); - * for (let i = 0; i & lt; intersects.length; i++) { - * intersects[i].object.material.color.set(0xff0000); - * } - * renderer.render(scene, camera); - * } - * window.addEventListener('pointermove', onPointerMove); - * window.requestAnimationFrame(render); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_interactive_cubes | Raycasting to a Mesh} - * @see Example: {@link https://threejs.org/examples/#webgl_interactive_cubes_ortho | Raycasting to a Mesh in using an OrthographicCamera} - * @see Example: {@link https://threejs.org/examples/#webgl_interactive_buffergeometry | Raycasting to a Mesh with BufferGeometry} - * @see Example: {@link https://threejs.org/examples/#webgl_instancing_raycast | Raycasting to a InstancedMesh} - * @see Example: {@link https://threejs.org/examples/#webgl_interactive_lines | Raycasting to a Line} - * @see Example: {@link https://threejs.org/examples/#webgl_interactive_raycasting_points | Raycasting to Points} - * @see Example: {@link https://threejs.org/examples/#webgl_geometry_terrain_raycast | Terrain raycasting} - * @see Example: {@link https://threejs.org/examples/#webgl_interactive_voxelpainter | Raycasting to paint voxels} - * @see Example: {@link https://threejs.org/examples/#webgl_raycaster_texture | Raycast to a Texture} - * @see {@link https://threejs.org/docs/index.html#api/en/core/Raycaster | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/Raycaster.js | Source} - */ -export class Raycaster { - /** - * This creates a new {@link Raycaster} object. - * @param origin The origin vector where the ray casts from. Default `new Vector3()` - * @param direction The direction vector that gives direction to the ray. Should be normalized. Default `new Vector3(0, 0, -1)` - * @param near All results returned are further away than near. Near can't be negative. Expects a `Float`. Default `0` - * @param far All results returned are closer than far. Far can't be lower than near. Expects a `Float`. Default `Infinity` - */ - constructor(origin?: Vector3, direction?: Vector3, near?: number, far?: number); - - /** - * The {@link THREE.RaycasterRay | Ray} used for the raycasting. - */ - ray: Ray; - - /** - * The near factor of the raycaster. This value indicates which objects can be discarded based on the distance. - * This value shouldn't be negative and should be smaller than the far property. - * @remarks Expects a `Float` - * @defaultValue `0` - */ - near: number; - - /** - * The far factor of the raycaster. This value indicates which objects can be discarded based on the distance. - * This value shouldn't be negative and should be larger than the near property. - * @remarks Expects a `Float` - * @defaultValue `Infinity` - */ - far: number; - - /** - * The camera to use when raycasting against view-dependent objects such as billboarded objects like {@link THREE.Sprites | Sprites}. - * This field can be set manually or is set when calling {@link setFromCamera}. - * @defaultValue `null` - */ - camera: Camera; - - /** - * Used by {@link Raycaster} to selectively ignore 3D objects when performing intersection tests. - * The following code example ensures that only 3D objects on layer `1` will be honored by the instance of Raycaster. - * ``` - * raycaster.layers.set( 1 ); - * object.layers.enable( 1 ); - * ``` - * @defaultValue `new THREE.Layers()` - See {@link THREE.Layers | Layers}. - */ - layers: Layers; - - /** - * An data object where threshold is the precision of the {@link Raycaster} when intersecting objects, in world units. - * @defaultValue `{ Mesh: {}, Line: { threshold: 1 }, LOD: {}, Points: { threshold: 1 }, Sprite: {} }` - */ - params: RaycasterParameters; - - /** - * Updates the ray with a new origin and direction - * @remarks - * Please note that this method only copies the values from the arguments. - * @param origin The origin vector where the ray casts from. - * @param direction The normalized direction vector that gives direction to the ray. - */ - set(origin: Vector3, direction: Vector3): void; - - /** - * Updates the ray with a new origin and direction. - * @param coords 2D coordinates of the mouse, in normalized device coordinates (NDC)---X and Y components should be between -1 and 1. - * @param camera camera from which the ray should originate - */ - setFromCamera(coords: Vector2, camera: Camera): void; - - /** - * Updates the ray with a new origin and direction. - * @param controller The controller to copy the position and direction from. - */ - setFromXRController(controller: XRTargetRaySpace): this; - - /** - * Checks all intersection between the ray and the object with or without the descendants - * @remarks Intersections are returned sorted by distance, closest first - * @remarks {@link Raycaster} delegates to the {@link Object3D.raycast | raycast} method of the passed object, when evaluating whether the ray intersects the object or not - * This allows {@link THREE.Mesh | meshes} to respond differently to ray casting than {@link THREE.Line | lines} and {@link THREE.Points | pointclouds}. - * **Note** that for meshes, faces must be pointed towards the origin of the {@link Raycaster.ray | ray} in order to be detected; - * intersections of the ray passing through the back of a face will not be detected - * To raycast against both faces of an object, you'll want to set the {@link Mesh.material | material}'s {@link Material.side | side} property to `THREE.DoubleSide`. - * @see {@link intersectObjects | .intersectObjects()}. - * @param object The object to check for intersection with the ray. - * @param recursive If true, it also checks all descendants. Otherwise it only checks intersection with the object. Default `true` - * @param optionalTarget Target to set the result. Otherwise a new {@link Array | Array} is instantiated. - * If set, you must clear this array prior to each call (i.e., array.length = 0;). Default `[]` - * @returns An array of intersections is returned. - */ - intersectObject( - object: Object3D, - recursive?: boolean, - optionalTarget?: Array>, - ): Array>; - - /** - * Checks all intersection between the ray and the objects with or without the descendants - * @remarks Intersections are returned sorted by distance, closest first - * @remarks Intersections are of the same form as those returned by {@link intersectObject | .intersectObject()}. - * @remarks {@link Raycaster} delegates to the {@link Object3D.raycast | raycast} method of the passed object, when evaluating whether the ray intersects the object or not - * This allows {@link THREE.Mesh | meshes} to respond differently to ray casting than {@link THREE.Line | lines} and {@link THREE.Points | pointclouds}. - * **Note** that for meshes, faces must be pointed towards the origin of the {@link Raycaster.ray | ray} in order to be detected; - * intersections of the ray passing through the back of a face will not be detected - * To raycast against both faces of an object, you'll want to set the {@link Mesh.material | material}'s {@link Material.side | side} property to `THREE.DoubleSide`. - * @see {@link intersectObject | .intersectObject()}. - * @param objects The objects to check for intersection with the ray. - * @param recursive If true, it also checks all descendants of the objects. Otherwise it only checks intersection with the objects. Default `true` - * @param optionalTarget Target to set the result. Otherwise a new {@link Array | Array} is instantiated. - * If set, you must clear this array prior to each call (i.e., array.length = 0;). Default `[]` - * @returns An array of intersections is returned. - */ - intersectObjects( - objects: Object3D[], - recursive?: boolean, - optionalTarget?: Array>, - ): Array>; -} diff --git a/src-testing/src/core/RenderTarget.d.ts b/src-testing/src/core/RenderTarget.d.ts deleted file mode 100644 index 5268ae370..000000000 --- a/src-testing/src/core/RenderTarget.d.ts +++ /dev/null @@ -1,96 +0,0 @@ -import { - ColorSpace, - MagnificationTextureFilter, - MinificationTextureFilter, - PixelFormatGPU, - TextureDataType, - Wrapping, -} from "../constants.js"; -import { Vector4 } from "../math/Vector4.js"; -import { DepthTexture } from "../textures/DepthTexture.js"; -import { Texture } from "../textures/Texture.js"; -import { EventDispatcher } from "./EventDispatcher.js"; - -export interface RenderTargetOptions { - wrapS?: Wrapping | undefined; - wrapT?: Wrapping | undefined; - magFilter?: MagnificationTextureFilter | undefined; - minFilter?: MinificationTextureFilter | undefined; - generateMipmaps?: boolean | undefined; // true - format?: number | undefined; // RGBAFormat - type?: TextureDataType | undefined; // UnsignedByteType - anisotropy?: number | undefined; // 1 - colorSpace?: ColorSpace | undefined; - internalFormat?: PixelFormatGPU | null | undefined; // null - depthBuffer?: boolean | undefined; // true - stencilBuffer?: boolean | undefined; // false - resolveDepthBuffer?: boolean | undefined; // true - resolveStencilBuffer?: boolean | undefined; // true - depthTexture?: DepthTexture | null | undefined; // null - /** - * Defines the count of MSAA samples. Can only be used with WebGL 2. Default is **0**. - * @default 0 - */ - samples?: number | undefined; - count?: number | undefined; -} - -export class RenderTarget extends EventDispatcher<{ dispose: {} }> { - readonly isRenderTarget: true; - - width: number; - height: number; - depth: number; - - scissor: Vector4; - /** - * @default false - */ - scissorTest: boolean; - viewport: Vector4; - textures: TTexture[]; - - /** - * @default true - */ - depthBuffer: boolean; - - /** - * @default false - */ - stencilBuffer: boolean; - - /** - * Defines whether the depth buffer should be resolved when rendering into a multisampled render target. - * @default true - */ - resolveDepthBuffer: boolean; - - /** - * Defines whether the stencil buffer should be resolved when rendering into a multisampled render target. - * This property has no effect when {@link .resolveDepthBuffer} is set to `false`. - * @default true - */ - resolveStencilBuffer: boolean; - - /** - * @default null - */ - depthTexture: DepthTexture | null; - - /** - * Defines the count of MSAA samples. Can only be used with WebGL 2. Default is **0**. - * @default 0 - */ - samples: number; - - constructor(width?: number, height?: number, options?: RenderTargetOptions); - - get texture(): TTexture; - set texture(value: TTexture); - - setSize(width: number, height: number, depth?: number): void; - clone(): this; - copy(source: RenderTarget): this; - dispose(): void; -} diff --git a/src-testing/src/core/Uniform.d.ts b/src-testing/src/core/Uniform.d.ts deleted file mode 100644 index 851ae2bf9..000000000 --- a/src-testing/src/core/Uniform.d.ts +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Uniforms are global GLSL variables. - * They are passed to shader programs. - * @example - * When declaring a uniform of a {@link THREE.ShaderMaterial | ShaderMaterial}, it is declared by value or by object. - * ```typescript - * uniforms: { - * time: { - * value: 1.0 - * }, - * resolution: new Uniform(new Vector2()) - * }; - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_nodes_materials_instance_uniform | WebGL2 / nodes / materials / instance / uniform} - * @see Example: {@link https://threejs.org/examples/#webgpu_instance_uniform| WebGPU / instance / uniform} - * @see {@link https://threejs.org/docs/index.html#api/en/core/Uniform | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/Uniform.js | Source} - */ -export class Uniform { - /** - * Create a new instance of {@link THREE.Uniform | Uniform} - * @param value An object containing the value to set up the uniform. It's type must be one of the Uniform Types described above. - */ - constructor(value: T); - - /** - * Current value of the uniform. - */ - value: T; - - /** - * Returns a clone of this uniform. - * @remarks - * If the uniform's {@link value} property is an {@link Object | Object} with a `clone()` method, this is used, - * otherwise the value is copied by assignment Array values are **shared** between cloned {@link THREE.UniformUniform | Uniform}s. - */ - clone(): Uniform; -} diff --git a/src-testing/src/core/UniformsGroup.d.ts b/src-testing/src/core/UniformsGroup.d.ts deleted file mode 100644 index 4fb30b2c1..000000000 --- a/src-testing/src/core/UniformsGroup.d.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { Usage } from "../constants.js"; -import { EventDispatcher } from "./EventDispatcher.js"; -import { Uniform } from "./Uniform.js"; - -/** - * @see Example: {@link https://threejs.org/examples/#webgl2_ubo | WebGL2 / UBO} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/UniformsGroup.js | Source} - */ -export class UniformsGroup extends EventDispatcher<{ dispose: {} }> { - constructor(); - - readonly isUniformsGroup: true; - - id: number; - - usage: Usage; - - uniforms: Array; - - add(uniform: Uniform | Uniform[]): this; - - remove(uniform: Uniform | Uniform[]): this; - - setName(name: string): this; - - setUsage(value: Usage): this; - - dispose(): this; - - copy(source: UniformsGroup): this; - - clone(): UniformsGroup; -} diff --git a/src-testing/src/extras/DataUtils.d.ts b/src-testing/src/extras/DataUtils.d.ts deleted file mode 100644 index fd678dbf4..000000000 --- a/src-testing/src/extras/DataUtils.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Returns a half precision floating point value from the given single precision floating point value. - * @param val A single precision floating point value. - * @see {@link https://threejs.org/docs/index.html#api/en/extras/DataUtils | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/DataUtils.js | Source} - */ -declare function toHalfFloat(val: number): number; - -/** - * Returns a single precision floating point value from the given half precision floating point value. - * @param val A half precision floating point value. - * @see {@link https://threejs.org/docs/index.html#api/en/extras/DataUtils | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/DataUtils.js | Source} - */ -declare function fromHalfFloat(val: number): number; - -declare const DataUtils: { - toHalfFloat: typeof toHalfFloat; - fromHalfFloat: typeof fromHalfFloat; -}; - -export { DataUtils, fromHalfFloat, toHalfFloat }; diff --git a/src-testing/src/extras/Earcut.d.ts b/src-testing/src/extras/Earcut.d.ts deleted file mode 100644 index 623b8ee82..000000000 --- a/src-testing/src/extras/Earcut.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -/** - * An implementation of the {@link Earcut} polygon triangulation algorithm - * @remarks - * The code is a port of {@link https://github.com/mapbox/earcut | mapbox/earcut}. - * @see {@link https://threejs.org/docs/index.html#api/en/extras/Earcut | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/Earcut.js | Source} - */ -export const Earcut: { - /** - * Triangulates the given shape definition by returning an array of triangles - * @remarks - * A triangle is defined by three consecutive integers representing vertex indices. - */ - triangulate(data: number[], holeIndices?: number[], dim?: number): number[]; -}; diff --git a/src-testing/src/extras/ImageUtils.d.ts b/src-testing/src/extras/ImageUtils.d.ts deleted file mode 100644 index 798fc3e90..000000000 --- a/src-testing/src/extras/ImageUtils.d.ts +++ /dev/null @@ -1,32 +0,0 @@ -/** - * A class containing utility functions for images. - * @see {@link https://threejs.org/docs/index.html#api/en/extras/ImageUtils | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/ImageUtils.js | Source} - */ -declare class ImageUtils { - /** - * Returns a data URI containing a representation of the given image. - * @param image The image object. - */ - static getDataURL( - image: HTMLImageElement | HTMLCanvasElement | CanvasImageSource | ImageBitmap | ImageData, - ): string; - - /** - * Converts the given sRGB image data to linear color space. - * @param image - */ - static sRGBToLinear(image: HTMLImageElement | HTMLCanvasElement | ImageBitmap): HTMLCanvasElement; - - /** - * Converts the given sRGB image data to linear color space. - * @param image - */ - static sRGBToLinear(image: ImageData): { - data: ImageData["data"]; - width: ImageData["width"]; - height: ImageData["height"]; - }; -} - -export { ImageUtils }; diff --git a/src-testing/src/extras/PMREMGenerator.d.ts b/src-testing/src/extras/PMREMGenerator.d.ts deleted file mode 100644 index 3eb4a15e1..000000000 --- a/src-testing/src/extras/PMREMGenerator.d.ts +++ /dev/null @@ -1,82 +0,0 @@ -import { WebGLRenderer } from "../renderers/WebGLRenderer.js"; -import { WebGLRenderTarget } from "../renderers/WebGLRenderTarget.js"; -import { Scene } from "../scenes/Scene.js"; -import { CubeTexture } from "../textures/CubeTexture.js"; -import { Texture } from "../textures/Texture.js"; - -/** - * This class generates a Prefiltered, Mipmapped Radiance Environment Map (PMREM) from a cubeMap environment texture. - * @remarks - * This allows different levels of blur to be quickly accessed based on material roughness - * Unlike a traditional mipmap chain, it only goes down to the LOD_MIN level (above), and then creates extra even more filtered 'mips' at the same LOD_MIN resolution, - * associated with higher roughness levels - * In this way we maintain resolution to smoothly interpolate diffuse lighting while limiting sampling computation. - * @remarks - * Note: The minimum {@link THREE.MeshStandardMaterial | MeshStandardMaterial}'s roughness depends on the size of the provided texture - * If your render has small dimensions or the shiny parts have a lot of curvature, you may still be able to get away with a smaller texture size. - * - * | texture size | minimum roughness | - * |--------------|--------------------| - * | 16 | 0.21 | - * | 32 | 0.15 | - * | 64 | 0.11 | - * | 128 | 0.076 | - * | 256 | 0.054 | - * | 512 | 0.038 | - * | 1024 | 0.027 | - * - * @see {@link https://threejs.org/docs/index.html#api/en/extras/PMREMGenerator | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/PMREMGenerator.js | Source} - */ -export class PMREMGenerator { - /** - * This constructor creates a new PMREMGenerator. - * @param renderer - */ - constructor(renderer: WebGLRenderer); - - /** - * Generates a PMREM from a supplied Scene, which can be faster than using an image if networking bandwidth is low - * @remarks - * Optional near and far planes ensure the scene is rendered in its entirety (the cubeCamera is placed at the origin). - * @param scene The given scene. - * @param sigma Specifies a blur radius in radians to be applied to the scene before PMREM generation. Default `0`. - * @param near The near plane value. Default `0.1`. - * @param far The far plane value. Default `100`. - */ - fromScene(scene: Scene, sigma?: number, near?: number, far?: number): WebGLRenderTarget; - - /** - * Generates a PMREM from an equirectangular texture, which can be either LDR or HDR. The ideal input image size is - * 1k (1024 x 512), as this matches best with the 256 x 256 cubemap output. The smallest supported equirectangular - * image size is 64 x 32. - */ - fromEquirectangular(equirectangular: Texture, renderTarget?: WebGLRenderTarget | null): WebGLRenderTarget; - - /** - * Generates a PMREM from an cubemap texture, which can be either LDR or HDR. The ideal input cube size is - * 256 x 256, as this matches best with the 256 x 256 cubemap output. The smallest supported cube size is 16 x 16. - */ - fromCubemap(cubemap: CubeTexture, renderTarget?: WebGLRenderTarget | null): WebGLRenderTarget; - - /** - * Pre-compiles the cubemap shader - * @remarks - * You can get faster start-up by invoking this method during your texture's network fetch for increased concurrency. - */ - compileCubemapShader(): void; - - /** - * Pre-compiles the equirectangular shader - * @remarks - * You can get faster start-up by invoking this method during your texture's network fetch for increased concurrency. - */ - compileEquirectangularShader(): void; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/extras/ShapeUtils.d.ts b/src-testing/src/extras/ShapeUtils.d.ts deleted file mode 100644 index 60fe8aaf9..000000000 --- a/src-testing/src/extras/ShapeUtils.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Vector2Like } from "../math/Vector2.js"; - -/** - * A class containing utility functions for shapes. - * @remarks Note that these are all linear functions so it is necessary to calculate separately for x, y (and z, w if present) components of a vector. - * @see {@link https://threejs.org/docs/index.html#api/en/extras/ShapeUtils | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/ShapeUtils.js | Source} - */ -export class ShapeUtils { - /** - * Calculate area of a ( 2D ) contour polygon. - */ - static area(contour: readonly Vector2Like[]): number; - - /** - * Note that this is a linear function so it is necessary to calculate separately for x, y components of a polygon. - * @remarks Used internally by {@link THREE.Path | Path}, {@link THREE.ExtrudeGeometry | ExtrudeGeometry} and {@link THREE.ShapeGeometry | ShapeGeometry}. - */ - static isClockWise(pts: readonly Vector2Like[]): boolean; - - /** - * Used internally by {@link THREE.ExtrudeGeometry | ExtrudeGeometry} and {@link THREE.ShapeGeometry | ShapeGeometry} to calculate faces in shapes with holes. - */ - static triangulateShape(contour: Vector2Like[], holes: Vector2Like[][]): number[][]; -} diff --git a/src-testing/src/extras/TextureUtils.d.ts b/src-testing/src/extras/TextureUtils.d.ts deleted file mode 100644 index 254458b43..000000000 --- a/src-testing/src/extras/TextureUtils.d.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { CompressedPixelFormat, PixelFormat, TextureDataType } from "../constants.js"; -import { Texture } from "../textures/Texture.js"; - -/** - * Scales the texture as large as possible within its surface without cropping or stretching the texture. The method - * preserves the original aspect ratio of the texture. Akin to CSS `object-fit: contain`. - */ -declare function contain(texture: Texture, aspect: number): Texture; - -/** - * Scales the texture to the smallest possible size to fill the surface, leaving no empty space. The method preserves - * the original aspect ratio of the texture. Akin to CSS `object-fit: cover`. - */ -declare function cover(texture: Texture, aspect: number): Texture; - -/** - * Configures the texture to the default transformation. Akin to CSS `object-fit: fill`. - */ -declare function fill(texture: Texture): Texture; - -/** - * Given the width, height, format, and type of a texture. Determines how many bytes must be used to represent the - * texture. - */ -declare function getByteLength( - width: number, - height: number, - format: PixelFormat | CompressedPixelFormat, - type: TextureDataType, -): number; - -/** - * A class containing utility functions for textures. - */ -declare const TextureUtils: { - contain: typeof contain; - cover: typeof cover; - fill: typeof fill; - getByteLength: typeof getByteLength; -}; - -export { contain, cover, fill, getByteLength, TextureUtils }; diff --git a/src-testing/src/extras/core/Curve.d.ts b/src-testing/src/extras/core/Curve.d.ts deleted file mode 100644 index 9e3610903..000000000 --- a/src-testing/src/extras/core/Curve.d.ts +++ /dev/null @@ -1,162 +0,0 @@ -import { Vector2 } from "../../math/Vector2.js"; -import { Vector3 } from "../../math/Vector3.js"; - -export interface CurveJSON { - metadata: { version: number; type: string; generator: string }; - arcLengthDivisions: number; - type: string; -} - -/** - * An abstract base class for creating a {@link Curve} object that contains methods for interpolation - * @remarks - * For an array of Curves see {@link THREE.CurvePath | CurvePath}. - * @remarks - * This following curves inherit from THREE.Curve: - * - * **2D curves** - * - {@link THREE.ArcCurve} - * - {@link THREE.CubicBezierCurve} - * - {@link THREE.EllipseCurve} - * - {@link THREE.LineCurve} - * - {@link THREE.QuadraticBezierCurve} - * - {@link THREE.SplineCurve} - * - * **3D curves** - * - {@link THREE.CatmullRomCurve3} - * - {@link THREE.CubicBezierCurve3} - * - {@link THREE.LineCurve3} - * - {@link THREE.QuadraticBezierCurve3} - * - * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/Curve | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/Curve.js | Source} - */ -export abstract class Curve { - protected constructor(); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `Curve` - */ - readonly type: string | "Curve"; - - /** - * This value determines the amount of divisions when calculating the cumulative segment lengths of a {@link Curve} - * via {@link .getLengths}. - * To ensure precision when using methods like {@link .getSpacedPoints}, it is recommended to increase {@link .arcLengthDivisions} if the {@link Curve} is very large. - * @defaultValue `200` - * @remarks Expects a `Integer` - */ - arcLengthDivisions: number; - - /** - * Returns a vector for a given position on the curve. - * @param t A position on the curve. Must be in the range `[ 0, 1 ]`. Expects a `Float` - * @param optionalTarget If specified, the result will be copied into this Vector, otherwise a new Vector will be created. Default `new T`. - */ - getPoint(t: number, optionalTarget?: TVector): TVector; - - /** - * Returns a vector for a given position on the {@link Curve} according to the arc length. - * @param u A position on the {@link Curve} according to the arc length. Must be in the range `[ 0, 1 ]`. Expects a `Float` - * @param optionalTarget If specified, the result will be copied into this Vector, otherwise a new Vector will be created. Default `new T`. - */ - getPointAt(u: number, optionalTarget?: TVector): TVector; - - /** - * Returns a set of divisions `+1` points using {@link .getPoint | getPoint(t)}. - * @param divisions Number of pieces to divide the {@link Curve} into. Expects a `Integer`. Default `5` - */ - getPoints(divisions?: number): TVector[]; - - /** - * Returns a set of divisions `+1` equi-spaced points using {@link .getPointAt | getPointAt(u)}. - * @param divisions Number of pieces to divide the {@link Curve} into. Expects a `Integer`. Default `5` - */ - getSpacedPoints(divisions?: number): TVector[]; - - /** - * Get total {@link Curve} arc length. - */ - getLength(): number; - - /** - * Get list of cumulative segment lengths. - * @param divisions Expects a `Integer` - */ - getLengths(divisions?: number): number[]; - - /** - * Update the cumulative segment distance cache - * @remarks - * The method must be called every time {@link Curve} parameters are changed - * If an updated {@link Curve} is part of a composed {@link Curve} like {@link THREE.CurvePath | CurvePath}, - * {@link .updateArcLengths}() must be called on the composed curve, too. - */ - updateArcLengths(): void; - - /** - * Given u in the range `[ 0, 1 ]`, - * @remarks - * `u` and `t` can then be used to give you points which are equidistant from the ends of the curve, using {@link .getPoint}. - * @param u Expects a `Float` - * @param distance Expects a `Float` - * @returns `t` also in the range `[ 0, 1 ]`. Expects a `Float`. - */ - getUtoTmapping(u: number, distance: number): number; - - /** - * Returns a unit vector tangent at t - * @remarks - * If the derived {@link Curve} does not implement its tangent derivation, two points a small delta apart will be used to find its gradient which seems to give a reasonable approximation. - * @param t A position on the curve. Must be in the range `[ 0, 1 ]`. Expects a `Float` - * @param optionalTarget If specified, the result will be copied into this Vector, otherwise a new Vector will be created. - */ - getTangent(t: number, optionalTarget?: TVector): TVector; - - /** - * Returns tangent at a point which is equidistant to the ends of the {@link Curve} from the point given in {@link .getTangent}. - * @param u A position on the {@link Curve} according to the arc length. Must be in the range `[ 0, 1 ]`. Expects a `Float` - * @param optionalTarget If specified, the result will be copied into this Vector, otherwise a new Vector will be created. - */ - getTangentAt(u: number, optionalTarget?: TVector): TVector; - - /** - * Generates the Frenet Frames - * @remarks - * Requires a {@link Curve} definition in 3D space - * Used in geometries like {@link THREE.TubeGeometry | TubeGeometry} or {@link THREE.ExtrudeGeometry | ExtrudeGeometry}. - * @param segments Expects a `Integer` - * @param closed - */ - computeFrenetFrames( - segments: number, - closed?: boolean, - ): { - tangents: Vector3[]; - normals: Vector3[]; - binormals: Vector3[]; - }; - - /** - * Creates a clone of this instance. - */ - clone(): this; - /** - * Copies another {@link Curve} object to this instance. - * @param source - */ - copy(source: Curve): this; - - /** - * Returns a JSON object representation of this instance. - */ - toJSON(): CurveJSON; - - /** - * Copies the data from the given JSON object to this instance. - * @param json - */ - fromJSON(json: CurveJSON): this; -} diff --git a/src-testing/src/extras/core/CurvePath.d.ts b/src-testing/src/extras/core/CurvePath.d.ts deleted file mode 100644 index 3a8577fd1..000000000 --- a/src-testing/src/extras/core/CurvePath.d.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { Vector2 } from "../../math/Vector2.js"; -import { Vector3 } from "../../math/Vector3.js"; -import { Curve, CurveJSON } from "./Curve.js"; - -export interface CurvePathJSON extends CurveJSON { - autoClose: boolean; - curves: CurveJSON[]; -} - -/** - * Curved Path - a curve path is simply a array of connected curves, but retains the api of a curve. - * @remarks - * A {@link CurvePath} is simply an array of connected curves, but retains the api of a curve. - * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/CurvePath | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/CurvePath.js | Source} - */ -export class CurvePath extends Curve { - /** - * The constructor take no parameters. - */ - constructor(); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `CurvePath` - */ - override readonly type: string | "CurvePath"; - - /** - * The array of {@link Curve | Curves}. - * @defaultValue `[]` - */ - curves: Array>; - - /** - * Whether or not to automatically close the path. - * @defaultValue false - */ - autoClose: boolean; - - /** - * Add a curve to the {@link .curves} array. - * @param curve - */ - add(curve: Curve): void; - /** - * Adds a {@link LineCurve | lineCurve} to close the path. - */ - closePath(): this; - - getPoint(t: number, optionalTarget?: TVector): TVector; - - /** - * Get list of cumulative curve lengths of the curves in the {@link .curves} array. - */ - getCurveLengths(): number[]; - - /** - * Returns an array of points representing a sequence of curves - * @remarks - * The `division` parameter defines the number of pieces each curve is divided into - * However, for optimization and quality purposes, the actual sampling resolution for each curve depends on its type - * For example, for a {@link THREE.LineCurve | LineCurve}, the returned number of points is always just 2. - * @param divisions Number of pieces to divide the curve into. Expects a `Integer`. Default `12` - */ - override getPoints(divisions?: number): TVector[]; - - /** - * Returns a set of divisions `+1` equi-spaced points using {@link .getPointAt | getPointAt(u)}. - * @param divisions Number of pieces to divide the curve into. Expects a `Integer`. Default `40` - */ - override getSpacedPoints(divisions?: number): TVector[]; - - toJSON(): CurvePathJSON; - fromJSON(json: CurvePathJSON): this; -} diff --git a/src-testing/src/extras/core/Interpolations.d.ts b/src-testing/src/extras/core/Interpolations.d.ts deleted file mode 100644 index 9f5ed3784..000000000 --- a/src-testing/src/extras/core/Interpolations.d.ts +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Used internally by {@link THREE.SplineCurve | SplineCurve}. - * @param t Interpolation weight. Expects a `Float` - * @param p0 Expects a `Float` - * @param p1 Expects a `Float` - * @param p2 Expects a `Float` - * @param p3 P0, p1, p2, the points defining the spline curve. Expects a `Float` - * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/Interpolations | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/Interpolations.js | Source} - */ -declare function CatmullRom(t: number, p0: number, p1: number, p2: number, p3: number): number; - -/** - * Used internally by {@link THREE.QuadraticBezierCurve3 | QuadraticBezierCurve3} and {@link THREE.QuadraticBezierCurve | QuadraticBezierCurve}. - * @param t Interpolation weight. Expects a `Float` - * @param p0 Expects a `Float` - * @param p1 Expects a `Float` - * @param p2 P0, p1, the starting, control and end points defining the curve. Expects a `Float` - * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/Interpolations | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/Interpolations.js | Source} - */ -declare function QuadraticBezier(t: number, p0: number, p1: number, p2: number): number; - -/** - * Used internally by {@link THREE.CubicBezierCurve3 | CubicBezierCurve3} and {@link THREE.CubicBezierCurve | CubicBezierCurve}. - * @param t Interpolation weight. Expects a `Float` - * @param p0 Expects a `Float` - * @param p1 Expects a `Float` - * @param p2 Expects a `Float` - * @param p3 P0, p1, p2, the starting, control(twice) and end points defining the curve. Expects a `Float` - * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/Interpolations | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/Interpolations.js | Source} - */ -declare function CubicBezier(t: number, p0: number, p1: number, p2: number, p3: number): number; - -export { CatmullRom, CubicBezier, QuadraticBezier }; diff --git a/src-testing/src/extras/core/Path.d.ts b/src-testing/src/extras/core/Path.d.ts deleted file mode 100644 index 28be15b9c..000000000 --- a/src-testing/src/extras/core/Path.d.ts +++ /dev/null @@ -1,166 +0,0 @@ -import { Vector2, Vector2Tuple } from "../../math/Vector2.js"; -import { CurvePath, CurvePathJSON } from "./CurvePath.js"; - -export interface PathJSON extends CurvePathJSON { - currentPoint: Vector2Tuple; -} - -/** - * A 2D {@link Path} representation. - * @remarks - * The class provides methods for creating paths and contours of 2D shapes similar to the 2D Canvas API. - * @example - * ```typescript - * const {@link Path} = new THREE.Path(); - * path.lineTo(0, 0.8); - * path.quadraticCurveTo(0, 1, 0.2, 1); - * path.lineTo(1, 1); - * const points = path.getPoints(); - * const geometry = new THREE.BufferGeometry().setFromPoints(points); - * const material = new THREE.LineBasicMaterial({ - * color: 0xffffff - * }); - * const line = new THREE.Line(geometry, material); - * scene.add(line); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/Path | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/Path.js | Source} - */ -export class Path extends CurvePath { - /** - * Creates a {@link Path} from the points - * @remarks - * The first point defines the offset, then successive points are added to the {@link CurvePath.curves | curves} array as {@link LineCurve | LineCurves}. - * If no points are specified, an empty {@link Path} is created and the {@link .currentPoint} is set to the origin. - * @param points Array of {@link Vector2 | Vector2s}. - */ - constructor(points?: Vector2[]); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `Path` - */ - override readonly type: string | "Path"; - - /** - * The current offset of the path. Any new {@link THREE.Curve | Curve} added will start here. - * @defaultValue `new THREE.Vector2()` - */ - currentPoint: Vector2; - - /** - * Adds an absolutely positioned {@link THREE.EllipseCurve | EllipseCurve} to the path. - * @param x Expects a `Float` - * @param y X, The absolute center of the arc. Expects a `Float` - * @param radius The radius of the arc. Expects a `Float` - * @param startAngle The start angle in radians. Expects a `Float` - * @param endAngle The end angle in radians. Expects a `Float` - * @param clockwise Sweep the arc clockwise. Default `false` - */ - absarc(aX: number, aY: number, aRadius: number, aStartAngle: number, aEndAngle: number, aClockwise?: boolean): this; - - /** - * Adds an absolutely positioned {@link THREE.EllipseCurve | EllipseCurve} to the path. - * @param x Expects a `Float` - * @param y X, The absolute center of the ellipse. Expects a `Float` - * @param xRadius The radius of the ellipse in the x axis. Expects a `Float` - * @param yRadius The radius of the ellipse in the y axis. Expects a `Float` - * @param startAngle The start angle in radians. Expects a `Float` - * @param endAngle The end angle in radians. Expects a `Float` - * @param clockwise Sweep the ellipse clockwise. Default `false` - * @param rotation The rotation angle of the ellipse in radians, counterclockwise from the positive X axis. Optional, Expects a `Float`. Default `0` - */ - absellipse( - aX: number, - aY: number, - xRadius: number, - yRadius: number, - aStartAngle: number, - aEndAngle: number, - aClockwise?: boolean, - aRotation?: number, - ): this; - - /** - * Adds an {@link THREE.EllipseCurve | EllipseCurve} to the path, positioned relative to {@link .currentPoint}. - * @param x Expects a `Float` - * @param y X, The center of the arc offset from the last call. Expects a `Float` - * @param radius The radius of the arc. Expects a `Float` - * @param startAngle The start angle in radians. Expects a `Float` - * @param endAngle The end angle in radians. Expects a `Float` - * @param clockwise Sweep the arc clockwise. Default `false` - */ - arc(aX: number, aY: number, aRadius: number, aStartAngle: number, aEndAngle: number, aClockwise?: boolean): this; - - /** - * This creates a bezier curve from {@link .currentPoint} with (cp1X, cp1Y) and (cp2X, cp2Y) as control points and updates {@link .currentPoint} to x and y. - * @param cp1X Expects a `Float` - * @param cp1Y Expects a `Float` - * @param cp2X Expects a `Float` - * @param cp2Y Expects a `Float` - * @param x Expects a `Float` - * @param y Expects a `Float` - */ - bezierCurveTo(aCP1x: number, aCP1y: number, aCP2x: number, aCP2y: number, aX: number, aY: number): this; - - /** - * Adds an {@link THREE.EllipseCurve | EllipseCurve} to the path, positioned relative to {@link .currentPoint}. - * @param x Expects a `Float` - * @param y X, The center of the ellipse offset from the last call. Expects a `Float` - * @param xRadius The radius of the ellipse in the x axis. Expects a `Float` - * @param yRadius The radius of the ellipse in the y axis. Expects a `Float` - * @param startAngle The start angle in radians. Expects a `Float` - * @param endAngle The end angle in radians. Expects a `Float` - * @param clockwise Sweep the ellipse clockwise. Default `false` - * @param rotation The rotation angle of the ellipse in radians, counterclockwise from the positive X axis. Optional, Expects a `Float`. Default `0` - */ - ellipse( - aX: number, - aY: number, - xRadius: number, - yRadius: number, - aStartAngle: number, - aEndAngle: number, - aClockwise?: boolean, - aRotation?: number, - ): this; - - /** - * Connects a {@link THREE.LineCurve | LineCurve} from {@link .currentPoint} to x, y onto the path. - * @param x Expects a `Float` - * @param y Expects a `Float` - */ - lineTo(x: number, y: number): this; - - /** - * Move the {@link .currentPoint} to x, y. - * @param x Expects a `Float` - * @param y Expects a `Float` - */ - moveTo(x: number, y: number): this; - - /** - * Creates a quadratic curve from {@link .currentPoint} with cpX and cpY as control point and updates {@link .currentPoint} to x and y. - * @param cpX Expects a `Float` - * @param cpY Expects a `Float` - * @param x Expects a `Float` - * @param y Expects a `Float` - */ - quadraticCurveTo(aCPx: number, aCPy: number, aX: number, aY: number): this; - - /** - * Points are added to the {@link CurvePath.curves | curves} array as {@link THREE.LineCurve | LineCurves}. - * @param vector2s - */ - setFromPoints(vectors: Vector2[]): this; - - /** - * Connects a new {@link THREE.SplineCurve | SplineCurve} onto the path. - * @param points An array of {@link Vector2 | Vector2's} - */ - splineThru(pts: Vector2[]): this; - - toJSON(): PathJSON; - fromJSON(json: PathJSON): this; -} diff --git a/src-testing/src/extras/core/Shape.d.ts b/src-testing/src/extras/core/Shape.d.ts deleted file mode 100644 index 84e5ff37f..000000000 --- a/src-testing/src/extras/core/Shape.d.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { Vector2 } from "../../math/Vector2.js"; -import { Path, PathJSON } from "./Path.js"; - -export interface ShapeJSON extends PathJSON { - uuid: string; - holes: PathJSON[]; -} - -/** - * Defines an arbitrary 2d {@link Shape} plane using paths with optional holes - * @remarks - * It can be used with {@link THREE.ExtrudeGeometry | ExtrudeGeometry}, {@link THREE.ShapeGeometry | ShapeGeometry}, to get points, or to get triangulated faces. - * @example - * ```typescript - * const heartShape = new THREE.Shape(); - * heartShape.moveTo(25, 25); - * heartShape.bezierCurveTo(25, 25, 20, 0, 0, 0); - * heartShape.bezierCurveTo(-30, 0, -30, 35, -30, 35); - * heartShape.bezierCurveTo(-30, 55, -10, 77, 25, 95); - * heartShape.bezierCurveTo(60, 77, 80, 55, 80, 35); - * heartShape.bezierCurveTo(80, 35, 80, 0, 50, 0); - * heartShape.bezierCurveTo(35, 0, 25, 25, 25, 25); - * const extrudeSettings = { - * depth: 8, - * bevelEnabled: true, - * bevelSegments: 2, - * steps: 2, - * bevelSize: 1, - * bevelThickness: 1 - * }; - * const geometry = new THREE.ExtrudeGeometry(heartShape, extrudeSettings); - * const mesh = new THREE.Mesh(geometry, new THREE.MeshPhongMaterial()); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_geometry_shapes | geometry / shapes } - * @see Example: {@link https://threejs.org/examples/#webgl_geometry_extrude_shapes | geometry / extrude / shapes } - * @see Example: {@link https://threejs.org/examples/#webgl_geometry_extrude_shapes2 | geometry / extrude / shapes2 } - * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/Shape | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/Shape.js | Source} - */ -export class Shape extends Path { - /** - * Creates a {@link Shape} from the points - * @remarks - * The first point defines the offset, then successive points are added to the {@link CurvePath.curves | curves} array as {@link THREE.LineCurve | LineCurves}. - * If no points are specified, an empty {@link Shape} is created and the {@link .currentPoint} is set to the origin. - * @param points Array of {@link Vector2 | Vector2s}. - */ - constructor(points?: Vector2[]); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `Shape` - */ - override readonly type: string | "Shape"; - - /** - * {@link http://en.wikipedia.org/wiki/Universally_unique_identifier | UUID} of this object instance. - * @remarks This gets automatically assigned and shouldn't be edited. - */ - uuid: string; - - /** - * An array of {@link Path | paths} that define the holes in the shape. - * @defaultValue `[]` - */ - holes: Path[]; - - /** - * Call {@link THREE.Curve.getPoints | getPoints} on the {@link Shape} and the {@link holes} array - * @param divisions The fineness of the result. Expects a `Integer` - */ - extractPoints(divisions: number): { - shape: Vector2[]; - holes: Vector2[][]; - }; - - /** - * Get an array of {@link Vector2 | Vector2's} that represent the holes in the shape. - * @param divisions The fineness of the result. Expects a `Integer` - */ - getPointsHoles(divisions: number): Vector2[][]; - - toJSON(): ShapeJSON; - fromJSON(json: ShapeJSON): this; -} diff --git a/src-testing/src/extras/core/ShapePath.d.ts b/src-testing/src/extras/core/ShapePath.d.ts deleted file mode 100644 index 0fcd88312..000000000 --- a/src-testing/src/extras/core/ShapePath.d.ts +++ /dev/null @@ -1,98 +0,0 @@ -import { Color } from "../../math/Color.js"; -import { Vector2 } from "../../math/Vector2.js"; -import { Path } from "./Path.js"; -import { Shape } from "./Shape.js"; - -/** - * This class is used to convert a series of shapes to an array of {@link THREE.Path | Path's}, - * for example an SVG shape to a path. - * @see {@link https://threejs.org/docs/index.html#api/en/extras/core/ShapePath | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/ShapePath.js | Source} - */ -export class ShapePath { - /** - * Creates a new {@link ShapePath} - * @remarks - * Unlike a {@link THREE.Path | Path}, no points are passed in as the {@link ShapePath} is designed to be generated after creation. - */ - constructor(); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `ShapePath` - */ - readonly type: "ShapePath"; - - /** - * Array of {@link THREE.Path | Path's}s. - * @defaultValue `[]` - */ - subPaths: Path[]; - - /** - * The current {@link THREE.Path | Path} that is being generated. - * @defaultValue `null` - */ - readonly currentPath: Path | null; - - /** - * {@link THREE.Color | Color} of the shape, by default set to white _(0xffffff)_. - * @defaultValue `new THREE.Color()` - */ - color: Color; - - /** - * Starts a new {@link THREE.Path | Path} and calls {@link THREE.Path.moveTo | Path.moveTo}( x, y ) on that {@link THREE.Path | Path} - * @remarks - * Also points {@link ShapePath.currentPath | currentPath} to that {@link THREE.Path | Path}. - * @param x Expects a `Float` - * @param y Expects a `Float` - */ - moveTo(x: number, y: number): this; - - /** - * This creates a line from the {@link ShapePath.currentPath | currentPath}'s offset to X and Y and updates the offset to X and Y. - * @param x Expects a `Float` - * @param y Expects a `Float` - */ - lineTo(x: number, y: number): this; - - /** - * This creates a quadratic curve from the {@link ShapePath.currentPath | currentPath}'s - * offset to _x_ and _y_ with _cpX_ and _cpY_ as control point and updates the {@link ShapePath.currentPath | currentPath}'s offset to _x_ and _y_. - * @param cpX Expects a `Float` - * @param cpY Expects a `Float` - * @param x Expects a `Float` - * @param y Expects a `Float` - */ - quadraticCurveTo(aCPx: number, aCPy: number, aX: number, aY: number): this; - - /** - * This creates a bezier curve from the {@link ShapePath.currentPath | currentPath}'s - * offset to _x_ and _y_ with _cp1X_, _cp1Y_ and _cp2X_, _cp2Y_ as control points and - * updates the {@link ShapePath.currentPath | currentPath}'s offset to _x_ and _y_. - * @param cp1X Expects a `Float` - * @param cp1Y Expects a `Float` - * @param cp2X Expects a `Float` - * @param cp2Y Expects a `Float` - * @param x Expects a `Float` - * @param y Expects a `Float` - */ - bezierCurveTo(aCP1x: number, aCP1y: number, aCP2x: number, aCP2y: number, aX: number, aY: number): this; - - /** - * Connects a new {@link THREE.SplineCurve | SplineCurve} onto the {@link ShapePath.currentPath | currentPath}. - * @param points An array of {@link THREE.Vector2 | Vector2}s - */ - splineThru(pts: Vector2[]): this; - - /** - * Converts the {@link ShapePath.subPaths | subPaths} array into an array of Shapes - * @remarks - * By default solid shapes are defined clockwise (CW) and holes are defined counterclockwise (CCW) - * If isCCW is set to true, then those are flipped. - * @param isCCW Changes how solids and holes are generated - */ - toShapes(isCCW: boolean): Shape[]; -} diff --git a/src-testing/src/extras/curves/ArcCurve.d.ts b/src-testing/src/extras/curves/ArcCurve.d.ts deleted file mode 100644 index 0932f7ffb..000000000 --- a/src-testing/src/extras/curves/ArcCurve.d.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { EllipseCurve } from "./EllipseCurve.js"; - -/** - * Alias for {@link THREE.EllipseCurve | EllipseCurve}. - * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/ArcCurve | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/ArcCurve.js | Source} - */ -export class ArcCurve extends EllipseCurve { - /** - * This constructor creates a new {@link ArcCurve}. - * @param aX The X center of the ellipse. Expects a `Float`. Default is `0`. - * @param aY The Y center of the ellipse. Expects a `Float`. Default is `0`. - * @param xRadius The radius of the ellipse in the x direction. Expects a `Float`. Default is `1`. - * @param yRadius The radius of the ellipse in the y direction. Expects a `Float`. Default is `1`. - * @param aStartAngle The start angle of the curve in radians starting from the positive X axis. Default is `0`. - * @param aEndAngle The end angle of the curve in radians starting from the positive X axis. Default is `2 x Math.PI`. - * @param aClockwise Whether the ellipse is drawn clockwise. Default is `false`. - */ - constructor( - aX?: number, - aY?: number, - aRadius?: number, - aStartAngle?: number, - aEndAngle?: number, - aClockwise?: boolean, - ); - - /** - * Read-only flag to check if a given object is of type {@link ArcCurve}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isArcCurve = true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `ArcCurve` - */ - override readonly type: string | "ArcCurve"; -} diff --git a/src-testing/src/extras/curves/CatmullRomCurve3.d.ts b/src-testing/src/extras/curves/CatmullRomCurve3.d.ts deleted file mode 100644 index 55088e412..000000000 --- a/src-testing/src/extras/curves/CatmullRomCurve3.d.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { Vector3 } from "../../math/Vector3.js"; -import { Curve } from "../core/Curve.js"; - -export type CurveType = "centripetal" | "chordal" | "catmullrom"; - -/** - * Create a smooth **3D** spline curve from a series of points using the {@link https://en.wikipedia.org/wiki/Centripetal_Catmull-Rom_spline | Catmull-Rom} algorithm. - * @example - * ```typescript - * //Create a closed wavey loop - * const curve = new THREE.CatmullRomCurve3([ - * new THREE.Vector3(-10, 0, 10), - * new THREE.Vector3(-5, 5, 5), - * new THREE.Vector3(0, 0, 0), - * new THREE.Vector3(5, -5, 5), - * new THREE.Vector3(10, 0, 10)]); - * const points = curve.getPoints(50); - * const geometry = new THREE.BufferGeometry().setFromPoints(points); - * const material = new THREE.LineBasicMaterial({ - * color: 0xff0000 - * }); - * // Create the final object to add to the scene - * const curveObject = new THREE.Line(geometry, material); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_geometry_extrude_splines | WebGL / geometry / extrude / splines} - * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/CatmullRomCurve3 | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/CatmullRomCurve3.js | Source} - */ -export class CatmullRomCurve3 extends Curve { - /** - * This constructor creates a new {@link CatmullRomCurve3}. - * @param points An array of {@link THREE.Vector3 | Vector3} points - * @param closed Whether the curve is closed. Default `false` - * @param curveType Type of the curve. Default `centripetal` - * @param tension Tension of the curve. Expects a `Float`. Default `0.5` - */ - constructor(points?: Vector3[], closed?: boolean, curveType?: CurveType, tension?: number); - - /** - * Read-only flag to check if a given object is of type {@link CatmullRomCurve3}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isCatmullRomCurve3 = true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `CatmullRomCurve3` - */ - override readonly type: string | "CatmullRomCurve3"; - - /** - * The curve will loop back onto itself when this is true. - * @defaultValue `false` - */ - closed: boolean; - - /** - * The array of {@link THREE.Vector3 | Vector3} points that define the curve. - * @remarks It needs at least two entries. - * @defaultValue `[]` - */ - points: Vector3[]; - - /** - * Possible values are `centripetal`, `chordal` and `catmullrom`. - * @defaultValue `centripetal` - */ - curveType: CurveType; - - /** - * When {@link .curveType} is `catmullrom`, defines catmullrom's tension. - * @remarks Expects a `Float` - */ - tension: number; -} diff --git a/src-testing/src/extras/curves/CubicBezierCurve.d.ts b/src-testing/src/extras/curves/CubicBezierCurve.d.ts deleted file mode 100644 index ae4518312..000000000 --- a/src-testing/src/extras/curves/CubicBezierCurve.d.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { Vector2 } from "../../math/Vector2.js"; -import { Curve } from "../core/Curve.js"; - -/** - * Create a smooth **2D** {@link http://en.wikipedia.org/wiki/B%C3%A9zier_curve#mediaviewer/File:Bezier_curve.svg | cubic bezier curve}, - * defined by a start point, endpoint and two control points. - * @example - * ```typescript - * const curve = new THREE.CubicBezierCurve( - * new THREE.Vector2(-10, 0), - * new THREE.Vector2(-5, 15), - * new THREE.Vector2(20, 15), - * new THREE.Vector2(10, 0)); - * const points = curve.getPoints(50); - * const geometry = new THREE.BufferGeometry().setFromPoints(points); - * const material = new THREE.LineBasicMaterial({ - * color: 0xff0000 - * }); - * // Create the final object to add to the scene - * const curveObject = new THREE.Line(geometry, material); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/CubicBezierCurve | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/CubicBezierCurve.js | Source} - */ -export class CubicBezierCurve extends Curve { - /** - * This constructor creates a new {@link CubicBezierCurve}. - * @param v0 The starting point. Default is `new THREE.Vector2()`. - * @param v1 The first control point. Default is `new THREE.Vector2()`. - * @param v2 The second control point. Default is `new THREE.Vector2()`. - * @param v3 The ending point. Default is `new THREE.Vector2()`. - */ - constructor(v0?: Vector2, v1?: Vector2, v2?: Vector2, v3?: Vector2); - - /** - * Read-only flag to check if a given object is of type {@link CubicBezierCurve}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isCubicBezierCurve = true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `CubicBezierCurve` - */ - override readonly type: string | "CubicBezierCurve"; - - /** - * The starting point. - * @defaultValue `new THREE.Vector2()` - */ - v0: Vector2; - - /** - * The first control point. - * @defaultValue `new THREE.Vector2()` - */ - v1: Vector2; - - /** - * The second control point. - * @defaultValue `new THREE.Vector2()` - */ - v2: Vector2; - - /** - * The ending point. - * @defaultValue `new THREE.Vector2()` - */ - v3: Vector2; -} diff --git a/src-testing/src/extras/curves/CubicBezierCurve3.d.ts b/src-testing/src/extras/curves/CubicBezierCurve3.d.ts deleted file mode 100644 index ad8edb356..000000000 --- a/src-testing/src/extras/curves/CubicBezierCurve3.d.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { Vector3 } from "../../math/Vector3.js"; -import { Curve } from "../core/Curve.js"; - -/** - * Create a smooth **3D** {@link http://en.wikipedia.org/wiki/B%C3%A9zier_curve#mediaviewer/File:Bezier_curve.svg | cubic bezier curve}, - * defined by a start point, endpoint and two control points. - * @example - * ```typescript - * const curve = new THREE.CubicBezierCurve( - * new THREE.Vector2(-10, 0), - * new THREE.Vector2(-5, 15), - * new THREE.Vector2(20, 15), - * new THREE.Vector2(10, 0)); - * const points = curve.getPoints(50); - * const geometry = new THREE.BufferGeometry().setFromPoints(points); - * const material = new THREE.LineBasicMaterial({ - * color: 0xff0000 - * }); - * // Create the final object to add to the scene - * const curveObject = new THREE.Line(geometry, material); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/CubicBezierCurve | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/CubicBezierCurve.js | Source} - */ -export class CubicBezierCurve3 extends Curve { - /** - * This constructor creates a new {@link CubicBezierCurve3}. - * @param v0 The starting point. Default is `new THREE.Vector3()`. - * @param v1 The first control point. Default is `new THREE.Vector3()`. - * @param v2 The second control point. Default is `new THREE.Vector3()`. - * @param v3 The ending point. Default is `new THREE.Vector3()`. - */ - constructor(v0?: Vector3, v1?: Vector3, v2?: Vector3, v3?: Vector3); - - /** - * Read-only flag to check if a given object is of type {@link CubicBezierCurve3}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isCubicBezierCurve3 = true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `CubicBezierCurve3` - */ - override readonly type: string | "CubicBezierCurve3"; - - /** - * The starting point. - * @defaultValue `new THREE.Vector3()`. - */ - v0: Vector3; - - /** - * The first control point. - * @defaultValue `new THREE.Vector3()`. - */ - v1: Vector3; - - /** - * The second control point. - * @defaultValue `new THREE.Vector3()`. - */ - v2: Vector3; - - /** - * The ending point. - * @defaultValue `new THREE.Vector3()`. - */ - v3: Vector3; -} diff --git a/src-testing/src/extras/curves/Curves.d.ts b/src-testing/src/extras/curves/Curves.d.ts deleted file mode 100644 index 996021d72..000000000 --- a/src-testing/src/extras/curves/Curves.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -export * from "./ArcCurve.js"; -export * from "./CatmullRomCurve3.js"; -export * from "./CubicBezierCurve.js"; -export * from "./CubicBezierCurve3.js"; -export * from "./EllipseCurve.js"; -export * from "./LineCurve.js"; -export * from "./LineCurve3.js"; -export * from "./QuadraticBezierCurve.js"; -export * from "./QuadraticBezierCurve3.js"; -export * from "./SplineCurve.js"; diff --git a/src-testing/src/extras/curves/EllipseCurve.d.ts b/src-testing/src/extras/curves/EllipseCurve.d.ts deleted file mode 100644 index 4d15ca8c2..000000000 --- a/src-testing/src/extras/curves/EllipseCurve.d.ts +++ /dev/null @@ -1,115 +0,0 @@ -import { Vector2 } from "../../math/Vector2.js"; -import { Curve } from "../core/Curve.js"; - -/** - * Creates a 2d curve in the shape of an ellipse - * @remarks - * Setting the {@link xRadius} equal to the {@link yRadius} will result in a circle. - * @example - * ```typescript - * const curve = new THREE.EllipseCurve( - * 0, 0, // ax, aY - * 10, 10, // xRadius, yRadius - * 0, 2 * Math.PI, // aStartAngle, aEndAngle - * false, // aClockwise - * 0 // aRotation - * ); - * const points = curve.getPoints(50); - * const geometry = new THREE.BufferGeometry().setFromPoints(points); - * const material = new THREE.LineBasicMaterial({ color: 0xff0000 }); - * // Create the final object to add to the scene - * const ellipse = new THREE.Line(geometry, material); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/EllipseCurve | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/EllipseCurve.js | Source} - */ -export class EllipseCurve extends Curve { - /** - * This constructor creates a new {@link EllipseCurve}. - * @param aX The X center of the ellipse. Expects a `Float`. Default is `0`. - * @param aY The Y center of the ellipse. Expects a `Float`. Default is `0`. - * @param xRadius The radius of the ellipse in the x direction. Expects a `Float`. Default is `1`. - * @param yRadius The radius of the ellipse in the y direction. Expects a `Float`. Default is `1`. - * @param aStartAngle The start angle of the curve in radians starting from the positive X axis. Default is `0`. - * @param aEndAngle The end angle of the curve in radians starting from the positive X axis. Default is `2 x Math.PI`. - * @param aClockwise Whether the ellipse is drawn clockwise. Default is `false`. - * @param aRotation The rotation angle of the ellipse in radians, counterclockwise from the positive X axis. Default is `0`. - */ - constructor( - aX?: number, - aY?: number, - xRadius?: number, - yRadius?: number, - aStartAngle?: number, - aEndAngle?: number, - aClockwise?: boolean, - aRotation?: number, - ); - - /** - * Read-only flag to check if a given object is of type {@link EllipseCurve}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isEllipseCurve = true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `EllipseCurve` - */ - override readonly type: string | "EllipseCurve"; - - /** - * The X center of the ellipse. - * @remarks Expects a `Float` - * @defaultValue `0` - */ - aX: number; - - /** - * The Y center of the ellipse. - * @remarks Expects a `Float` - * @defaultValue `0` - */ - aY: number; - - /** - * The radius of the ellipse in the x direction. - * @defaultValue `1` - */ - xRadius: number; - - /** - * The radius of the ellipse in the y direction. - * @defaultValue `1` - */ - yRadius: number; - - /** - * The start angle of the curve in radians starting from the middle right side. - * @remarks Expects a `Float` - * @defaultValue `0` - */ - aStartAngle: number; - - /** - * The end angle of the curve in radians starting from the middle right side. - * @remarks Expects a `Float` - * @defaultValue `2 * Math.PI` - */ - aEndAngle: number; - - /** - * Whether the ellipse is drawn clockwise. - * @defaultValue `false`` - */ - aClockwise: boolean; - - /** - * The rotation angle of the ellipse in radians, counterclockwise from the positive X axis (optional). - * @remarks Expects a `Float` - * @defaultValue `0` - */ - aRotation: number; -} diff --git a/src-testing/src/extras/curves/LineCurve.d.ts b/src-testing/src/extras/curves/LineCurve.d.ts deleted file mode 100644 index 3bcb000d3..000000000 --- a/src-testing/src/extras/curves/LineCurve.d.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { Vector2 } from "../../math/Vector2.js"; -import { Curve } from "../core/Curve.js"; - -/** - * A curve representing a **2D** line segment. - * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/LineCurve | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/LineCurve.js | Source} - */ -export class LineCurve extends Curve { - /** - * This constructor creates a new {@link LineCurve}. - * @param v1 The start point. Default is `new THREE.Vector2()`. - * @param v2 The end point. Default is `new THREE.Vector2()`. - */ - constructor(v1?: Vector2, v2?: Vector2); - - /** - * Read-only flag to check if a given object is of type {@link LineCurve}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isLineCurve = true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `LineCurve` - */ - override readonly type: string | "LineCurve"; - - /** - * The start point. - * @defaultValue `new THREE.Vector2()` - */ - v1: Vector2; - - /** - * The end point - * @defaultValue `new THREE.Vector2()` - */ - v2: Vector2; -} diff --git a/src-testing/src/extras/curves/LineCurve3.d.ts b/src-testing/src/extras/curves/LineCurve3.d.ts deleted file mode 100644 index c7ae7c301..000000000 --- a/src-testing/src/extras/curves/LineCurve3.d.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { Vector3 } from "../../math/Vector3.js"; -import { Curve } from "../core/Curve.js"; - -/** - * A curve representing a **3D** line segment. - * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/LineCurve3 | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/LineCurve3.js | Source} - */ -export class LineCurve3 extends Curve { - /** - * This constructor creates a new {@link LineCurve3}. - * @param v1 The start point. Default is `new THREE.Vector3()`. - * @param v2 The end point. Default is `new THREE.Vector3()`. - */ - constructor(v1?: Vector3, v2?: Vector3); - - /** - * Read-only flag to check if a given object is of type {@link LineCurve3}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isLineCurve3 = true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `LineCurve3` - */ - override readonly type: string | "LineCurve3"; - - /** - * The start point. - * @defaultValue `new THREE.Vector3()`. - */ - v1: Vector3; - - /** - * The end point. - * @defaultValue `new THREE.Vector3()`. - */ - v2: Vector3; -} diff --git a/src-testing/src/extras/curves/QuadraticBezierCurve.d.ts b/src-testing/src/extras/curves/QuadraticBezierCurve.d.ts deleted file mode 100644 index 8ae4c3213..000000000 --- a/src-testing/src/extras/curves/QuadraticBezierCurve.d.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { Vector2 } from "../../math/Vector2.js"; -import { Curve } from "../core/Curve.js"; - -/** - * Create a smooth **2D** {@link http://en.wikipedia.org/wiki/B%C3%A9zier_curve#mediaviewer/File:B%C3%A9zier_2_big.gif | quadratic bezier curve}, - * defined by a start point, end point and a single control point. - * @example - * ```typescript - * const curve = new THREE.QuadraticBezierCurve( - * new THREE.Vector2(-10, 0), - * new THREE.Vector2(20, 15), - * new THREE.Vector2(10, 0)); - * const points = curve.getPoints(50); - * const geometry = new THREE.BufferGeometry().setFromPoints(points); - * const material = new THREE.LineBasicMaterial({ - * color: 0xff0000 - * }); - * // Create the final object to add to the scene - * const curveObject = new THREE.Line(geometry, material); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/QuadraticBezierCurve | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/QuadraticBezierCurve.js | Source} - */ -export class QuadraticBezierCurve extends Curve { - /** - * This constructor creates a new {@link QuadraticBezierCurve}. - * @param v0 The start point. Default is `new THREE.Vector2()`. - * @param v1 The control point. Default is `new THREE.Vector2()`. - * @param v2 The end point. Default is `new THREE.Vector2()`. - */ - constructor(v0?: Vector2, v1?: Vector2, v2?: Vector2); - - /** - * Read-only flag to check if a given object is of type {@link LineCurve3}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isQuadraticBezierCurve = true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `QuadraticBezierCurve` - */ - override readonly type: string | "QuadraticBezierCurve"; - - /** - * The start point. - * @defaultValue `new THREE.Vector2()` - */ - v0: Vector2; - - /** - * The control point. - * @defaultValue `new THREE.Vector2()` - */ - v1: Vector2; - - /** - * The end point. - * @defaultValue `new THREE.Vector2()` - */ - v2: Vector2; -} diff --git a/src-testing/src/extras/curves/QuadraticBezierCurve3.d.ts b/src-testing/src/extras/curves/QuadraticBezierCurve3.d.ts deleted file mode 100644 index 3964af820..000000000 --- a/src-testing/src/extras/curves/QuadraticBezierCurve3.d.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { Vector3 } from "../../math/Vector3.js"; -import { Curve } from "../core/Curve.js"; - -/** - * Create a smooth **3D** {@link http://en.wikipedia.org/wiki/B%C3%A9zier_curve#mediaviewer/File:B%C3%A9zier_2_big.gif | quadratic bezier curve}, - * defined by a start point, end point and a single control point. - * @example - * ```typescript - * const curve = new THREE.QuadraticBezierCurve3( - * new THREE.Vector3(-10, 0, 0), - * new THREE.Vector3(20, 15, 0), - * new THREE.Vector3(10, 0, 0)); - * const points = curve.getPoints(50); - * const geometry = new THREE.BufferGeometry().setFromPoints(points); - * const material = new THREE.LineBasicMaterial({ - * color: 0xff0000 - * }); - * // Create the final object to add to the scene - * const curveObject = new THREE.Line(geometry, material); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/QuadraticBezierCurve3 | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/QuadraticBezierCurve3.js | Source} - */ -export class QuadraticBezierCurve3 extends Curve { - /** - * This constructor creates a new {@link QuadraticBezierCurve}. - * @param v0 The start point. Default is `new THREE.Vector3()`. - * @param v1 The control point. Default is `new THREE.Vector3()`. - * @param v2 The end point. Default is `new THREE.Vector3()`. - */ - constructor(v0?: Vector3, v1?: Vector3, v2?: Vector3); - - /** - * Read-only flag to check if a given object is of type {@link QuadraticBezierCurve3}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isQuadraticBezierCurve3 = true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `QuadraticBezierCurve3` - */ - override readonly type: string | "QuadraticBezierCurve3"; - - /** - * The start point. - * @defaultValue `new THREE.Vector3()` - */ - v0: Vector3; - - /** - * The control point. - * @defaultValue `new THREE.Vector3()` - */ - v1: Vector3; - - /** - * The end point. - * @defaultValue `new THREE.Vector3()` - */ - v2: Vector3; -} diff --git a/src-testing/src/extras/curves/SplineCurve.d.ts b/src-testing/src/extras/curves/SplineCurve.d.ts deleted file mode 100644 index c3c56210a..000000000 --- a/src-testing/src/extras/curves/SplineCurve.d.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { Vector2 } from "../../math/Vector2.js"; -import { Curve } from "../core/Curve.js"; - -/** - * Create a smooth **2D** spline curve from a series of points. - * @example - * ```typescript - * // Create a sine-like wave - * const curve = new THREE.SplineCurve([ - * new THREE.Vector2(-10, 0), - * new THREE.Vector2(-5, 5), - * new THREE.Vector2(0, 0), - * new THREE.Vector2(5, -5), - * new THREE.Vector2(10, 0)]); - * const points = curve.getPoints(50); - * const geometry = new THREE.BufferGeometry().setFromPoints(points); - * const material = new THREE.LineBasicMaterial({ - * color: 0xff0000 - * }); - * // Create the final object to add to the scene - * const splineObject = new THREE.Line(geometry, material); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/extras/curves/SplineCurve | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/curves/SplineCurve.js | Source} - */ -export class SplineCurve extends Curve { - /** - * This constructor creates a new {@link SplineCurve}. - * @param points An array of {@link THREE.Vector2 | Vector2} points that define the curve. Default `[]` - */ - constructor(points?: Vector2[]); - - /** - * Read-only flag to check if a given object is of type {@link SplineCurve}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isSplineCurve = true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `SplineCurve` - */ - override readonly type: string | "SplineCurve"; - - /** - * The array of {@link THREE.Vector2 | Vector2} points that define the curve. - * @defaultValue `[]` - */ - points: Vector2[]; -} diff --git a/src-testing/src/geometries/BoxGeometry.d.ts b/src-testing/src/geometries/BoxGeometry.d.ts deleted file mode 100644 index 7a756ae56..000000000 --- a/src-testing/src/geometries/BoxGeometry.d.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; - -/** - * {@link BoxGeometry} is a geometry class for a rectangular cuboid with a given 'width', 'height', and 'depth' - * @remarks On creation, the cuboid is centred on the origin, with each edge parallel to one of the axes. - * @example - * ```typescript - * const geometry = new THREE.BoxGeometry(1, 1, 1); - * const material = new THREE.MeshBasicMaterial({ - * color: 0x00ff00 - * }); - * const cube = new THREE.Mesh(geometry, material); - * scene.add(cube); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/BoxGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/BoxGeometry.js | Source} - */ -export class BoxGeometry extends BufferGeometry { - /** - * Create a new instance of {@link BoxGeometry} - * @param width Width; that is, the length of the edges parallel to the X axis. Optional; Expects a `Float`. Default `1` - * @param height Height; that is, the length of the edges parallel to the Y axis. Optional; Expects a `Float`. Default `1` - * @param depth Depth; that is, the length of the edges parallel to the Z axis. Optional; Expects a `Float`. Default `1` - * @param widthSegments Number of segmented rectangular faces along the width of the sides. Optional; Expects a `Integer`. Default `1` - * @param heightSegments Number of segmented rectangular faces along the height of the sides. Optional; Expects a `Integer`. Default `1` - * @param depthSegments Number of segmented rectangular faces along the depth of the sides. Optional; Expects a `Integer`. Default `1` - */ - constructor( - width?: number, - height?: number, - depth?: number, - widthSegments?: number, - heightSegments?: number, - depthSegments?: number, - ); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `BoxGeometry` - */ - override readonly type: string | "BoxGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly width: number; - readonly height: number; - readonly depth: number; - readonly widthSegments: number; - readonly heightSegments: number; - readonly depthSegments: number; - }; - - /** @internal */ - static fromJSON(data: {}): BoxGeometry; -} diff --git a/src-testing/src/geometries/CapsuleGeometry.d.ts b/src-testing/src/geometries/CapsuleGeometry.d.ts deleted file mode 100644 index b20616035..000000000 --- a/src-testing/src/geometries/CapsuleGeometry.d.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; - -/** - * {@link CapsuleGeometry} is a geometry class for a capsule with given radii and height - * @remarks It is constructed using a lathe. - * @example - * ```typescript - * const geometry = new THREE.CapsuleGeometry(1, 1, 4, 8); - * const material = new THREE.MeshBasicMaterial({ - * color: 0x00ff00 - * }); - * const capsule = new THREE.Mesh(geometry, material); - * scene.add(capsule); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/CapsuleGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/CapsuleGeometry.js | Source} - */ -export class CapsuleGeometry extends BufferGeometry { - /** - * Create a new instance of {@link CapsuleGeometry} - * @param radius Radius of the capsule. Expects a `Float`. Default `1` - * @param length Length of the middle section. Expects a `Float`. Default `1` - * @param capSegments Number of curve segments used to build the caps. Expects a `Integer`. Default `4` - * @param radialSegments Number of segmented faces around the circumference of the capsule. Expects a `Integer`. Default `8` - */ - constructor(radius?: number, length?: number, capSegments?: number, radialSegments?: number); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `CapsuleGeometry` - */ - override readonly type: string | "CapsuleGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly radius: number; - readonly length: number; - readonly capSegments: number; - readonly radialSegments: number; - }; - - /** @internal */ - static fromJSON(data: {}): CapsuleGeometry; -} diff --git a/src-testing/src/geometries/CircleGeometry.d.ts b/src-testing/src/geometries/CircleGeometry.d.ts deleted file mode 100644 index b512e4866..000000000 --- a/src-testing/src/geometries/CircleGeometry.d.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; - -/** - * {@link CircleGeometry} is a simple shape of Euclidean geometry - * @remarks - * It is constructed from a number of triangular segments that are oriented around a central point and extend as far out as a given radius - * It is built counter-clockwise from a start angle and a given central angle - * It can also be used to create regular polygons, where the number of segments determines the number of sides. - * @example - * ```typescript - * const geometry = new THREE.CircleGeometry(5, 32); - * const material = new THREE.MeshBasicMaterial({ - * color: 0xffff00 - * }); - * const circle = new THREE.Mesh(geometry, material); - * scene.add(circle); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/CircleGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/CircleGeometry.js | Source} - */ -export class CircleGeometry extends BufferGeometry { - /** - * Create a new instance of {@link CircleGeometry} - * @param radius Radius of the circle. Expects a `Float`. Default `1` - * @param segments Number of segments (triangles). Expects a `Integer`. Minimum `3`. Default `32` - * @param thetaStart Start angle for first segment. Expects a `Float`. Default `0`, _(three o'clock position)_. - * @param thetaLength The central angle, often called theta, of the circular sector. Expects a `Float`. Default `Math.PI * 2`, _which makes for a complete circle_. - */ - constructor(radius?: number, segments?: number, thetaStart?: number, thetaLength?: number); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `CircleGeometry` - */ - override readonly type: string | "CircleGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly radius: number; - readonly segments: number; - readonly thetaStart: number; - readonly thetaLength: number; - }; - - /** @internal */ - static fromJSON(data: {}): CircleGeometry; -} diff --git a/src-testing/src/geometries/ConeGeometry.d.ts b/src-testing/src/geometries/ConeGeometry.d.ts deleted file mode 100644 index 683376429..000000000 --- a/src-testing/src/geometries/ConeGeometry.d.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { CylinderGeometry } from "./CylinderGeometry.js"; - -/** - * A class for generating cone geometries. - * @example - * ```typescript - * const geometry = new THREE.ConeGeometry(5, 20, 32); - * const material = new THREE.MeshBasicMaterial({ - * color: 0xffff00 - * }); - * const cone = new THREE.Mesh(geometry, material); - * scene.add(cone); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/ConeGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/ConeGeometry.js | Source} - */ -export class ConeGeometry extends CylinderGeometry { - /** - * Create a new instance of {@link ConeGeometry} - * @param radius Radius of the cone base. Expects a `Float`. Default `1` - * @param height Height of the cone. Expects a `Float`. Default `1` - * @param radialSegments Number of segmented faces around the circumference of the cone. Expects a `Integer`. Default `32` - * @param heightSegments Number of rows of faces along the height of the cone. Expects a `Integer`. Default `1` - * @param openEnded A Boolean indicating whether the base of the cone is open or capped. Default `false`, _meaning capped_. - * @param thetaStart Start angle for first segment. Expects a `Float`. Default `0`, _(three o'clock position)_. - * @param thetaLength The central angle, often called theta, of the circular sector. Expects a `Float`. Default `Math.PI * 2`, _which makes for a complete cone_. - */ - constructor( - radius?: number, - height?: number, - radialSegments?: number, - heightSegments?: number, - openEnded?: boolean, - thetaStart?: number, - thetaLength?: number, - ); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `ConeGeometry` - */ - override readonly type: string | "ConeGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks {@link radiusTop} and {@link radiusBottom} are from base {@link THREE.CylinderGeometry} class. - * @remarks Any modification after instantiation does not change the geometry. - */ - override readonly parameters: { - readonly radius: number; - readonly radiusTop: number; - readonly radiusBottom: number; - readonly height: number; - readonly radialSegments: number; - readonly heightSegments: number; - readonly openEnded: boolean; - readonly thetaStart: number; - readonly thetaLength: number; - }; - - /** @internal */ - static fromJSON(data: {}): ConeGeometry; -} diff --git a/src-testing/src/geometries/CylinderGeometry.d.ts b/src-testing/src/geometries/CylinderGeometry.d.ts deleted file mode 100644 index 90f1b06f3..000000000 --- a/src-testing/src/geometries/CylinderGeometry.d.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; - -/** - * A class for generating cylinder geometries. - * @example - * ```typescript - * const geometry = new THREE.CylinderGeometry(5, 5, 20, 32); - * const material = new THREE.MeshBasicMaterial({ - * color: 0xffff00 - * }); - * const cylinder = new THREE.Mesh(geometry, material); - * scene.add(cylinder); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/CylinderGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/CylinderGeometry.js | Source} - */ -export class CylinderGeometry extends BufferGeometry { - /** - * Create a new instance of {@link CylinderGeometry} - * @param radiusTop Radius of the cylinder at the top. Default `1` - * @param radiusBottom Radius of the cylinder at the bottom. Default `1` - * @param height Height of the cylinder. Default `1` - * @param radialSegments Number of segmented faces around the circumference of the cylinder. Default `32` - * @param heightSegments Number of rows of faces along the height of the cylinder. Expects a `Integer`. Default `1` - * @param openEnded A Boolean indicating whether the ends of the cylinder are open or capped. Default `false`, _meaning capped_. - * @param thetaStart Start angle for first segment. Default `0`, _(three o'clock position)_. - * @param thetaLength The central angle, often called theta, of the circular sector. Default `Math.PI * 2`, _which makes for a complete cylinder. - */ - constructor( - radiusTop?: number, - radiusBottom?: number, - height?: number, - radialSegments?: number, - heightSegments?: number, - openEnded?: boolean, - thetaStart?: number, - thetaLength?: number, - ); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `CylinderGeometry` - */ - override readonly type: string | "CylinderGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly radiusTop: number; - readonly radiusBottom: number; - readonly height: number; - readonly radialSegments: number; - readonly heightSegments: number; - readonly openEnded: boolean; - readonly thetaStart: number; - readonly thetaLength: number; - }; - - /** @internal */ - static fromJSON(data: any): CylinderGeometry; -} diff --git a/src-testing/src/geometries/DodecahedronGeometry.d.ts b/src-testing/src/geometries/DodecahedronGeometry.d.ts deleted file mode 100644 index e79e5a895..000000000 --- a/src-testing/src/geometries/DodecahedronGeometry.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { PolyhedronGeometry } from "./PolyhedronGeometry.js"; - -/** - * A class for generating a dodecahedron geometries. - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/DodecahedronGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/DodecahedronGeometry.js | Source} - */ -export class DodecahedronGeometry extends PolyhedronGeometry { - /** - * Create a new instance of {@link DodecahedronGeometry} - * @param radius Radius of the dodecahedron. Expects a `Float`. Default `1` - * @param detail Setting this to a value greater than 0 adds vertices making it no longer a dodecahedron. Expects a `Integer`. Default `0` - */ - constructor(radius?: number, detail?: number); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `DodecahedronGeometry` - */ - override readonly type: string | "DodecahedronGeometry"; - - /** @internal */ - static fromJSON(data: {}): DodecahedronGeometry; -} diff --git a/src-testing/src/geometries/EdgesGeometry.d.ts b/src-testing/src/geometries/EdgesGeometry.d.ts deleted file mode 100644 index b901b10d7..000000000 --- a/src-testing/src/geometries/EdgesGeometry.d.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; - -/** - * This can be used as a helper object to view the edges of a {@link THREE.BufferGeometry | geometry}. - * @example - * ```typescript - * const geometry = new THREE.BoxGeometry(100, 100, 100); - * const edges = new THREE.EdgesGeometry(geometry); - * const line = new THREE.LineSegments(edges, new THREE.LineBasicMaterial({ - * color: 0xffffff - * })); - * scene.add(line); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_helpers | helpers} - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/EdgesGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/EdgesGeometry.js | Source} - */ -export class EdgesGeometry extends BufferGeometry { - /** - * Create a new instance of {@link EdgesGeometry} - * @param geometry Any geometry object. Default `null`. - * @param thresholdAngle An edge is only rendered if the angle (in degrees) between the face normals of the adjoining faces exceeds this value. Expects a `Integer`. Default `1` _degree_. - */ - constructor(geometry?: TBufferGeometry | null, thresholdAngle?: number); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `EdgesGeometry` - */ - override readonly type: string | "EdgesGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly geometry: TBufferGeometry | null; - readonly thresholdAngle: number; - }; -} diff --git a/src-testing/src/geometries/ExtrudeGeometry.d.ts b/src-testing/src/geometries/ExtrudeGeometry.d.ts deleted file mode 100644 index d872b7c21..000000000 --- a/src-testing/src/geometries/ExtrudeGeometry.d.ts +++ /dev/null @@ -1,152 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { Curve } from "../extras/core/Curve.js"; -import { Shape } from "../extras/core/Shape.js"; -import { Vector2 } from "../math/Vector2.js"; -import { Vector3 } from "../math/Vector3.js"; - -export interface ExtrudeGeometryOptions { - /** - * Number of points on the curves. - * Expects a `Integer`. - * @defaultValue `12` - */ - curveSegments?: number | undefined; - - /** - * Number of points used for subdividing segments along the depth of the extruded spline. - * @defaultValue `1` - */ - steps?: number | undefined; - - /** - * Depth to extrude the shape. - * @defaultValue `1` - */ - depth?: number | undefined; - - /** - * Turn on bevel. Applying beveling to the shape. - * @defaultValue `true` - */ - bevelEnabled?: boolean | undefined; - - /** - * How deep into the original shape the bevel goes. - * Expects a `Float`. - * @defaultValue `0.2` - */ - bevelThickness?: number | undefined; - - /** - * Distance from the shape outline that the bevel extends - * Expects a `Float`. - * @defaultValue `bevelThickness - 0.1` - */ - bevelSize?: number | undefined; - - /** - * Distance from the shape outline that the bevel starts. - * Expects a `Float`. - * @defaultValue `0` - */ - bevelOffset?: number | undefined; - - /** - * Number of bevel layers/segments. - * Expects a `Integer`. - * @defaultValue `3` - */ - bevelSegments?: number | undefined; - - /** - * A 3D spline path along which the shape should be extruded. - * @remarks Bevels not supported for path extrusion. - */ - extrudePath?: Curve | undefined; - - /** - * A object that provides UV generator functions. - */ - UVGenerator?: UVGenerator | undefined; -} - -export interface UVGenerator { - generateTopUV( - geometry: ExtrudeGeometry, - vertices: number[], - indexA: number, - indexB: number, - indexC: number, - ): Vector2[]; - generateSideWallUV( - geometry: ExtrudeGeometry, - vertices: number[], - indexA: number, - indexB: number, - indexC: number, - indexD: number, - ): Vector2[]; -} - -/** - * Creates extruded geometry from a path shape. - * @remarks This object extrudes a 2D shape to a 3D geometry. - * @remarks When creating a Mesh with this geometry, if you'd like to have a separate material used for its face and its extruded sides, you can use an array of materials - * @remarks The first material will be applied to the face; the second material will be applied to the sides. - * @example - * ```typescript - * const length = 12, width = 8; - * const shape = new THREE.Shape(); - * shape.moveTo(0, 0); - * shape.lineTo(0, width); - * shape.lineTo(length, width); - * shape.lineTo(length, 0); - * shape.lineTo(0, 0); - * const extrudeSettings = { - * steps: 2, - * depth: 16, - * bevelEnabled: true, - * bevelThickness: 1, - * bevelSize: 1, - * bevelOffset: 0, - * bevelSegments: 1 - * }; - * const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings); - * const material = new THREE.MeshBasicMaterial({ - * color: 0x00ff00 - * }); - * const mesh = new THREE.Mesh(geometry, material); - * scene.add(mesh); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/ExtrudeGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/ExtrudeGeometry.js | Source} - */ -export class ExtrudeGeometry extends BufferGeometry { - /** - * Create a new instance of {@link ExtrudeGeometry} - * @param shapes Shape or an array of shapes. Default `new Shape([new Vector2(0.5, 0.5), new Vector2(-0.5, 0.5), new Vector2(-0.5, -0.5), new Vector2(0.5, -0.5)])`. - * @param options Object that can contain the following parameters. @see {@link ExtrudeGeometryOptions} for defaults. - */ - constructor(shapes?: Shape | Shape[], options?: ExtrudeGeometryOptions); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `ExtrudeGeometry` - */ - override readonly type: string | "ExtrudeGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly shapes: Shape | Shape[]; - readonly options: ExtrudeGeometryOptions; - }; - - addShape(shape: Shape): void; - - /** @internal */ - static fromJSON(data: {}, shapes: unknown): ExtrudeGeometry; -} diff --git a/src-testing/src/geometries/Geometries.d.ts b/src-testing/src/geometries/Geometries.d.ts deleted file mode 100644 index b1be46c46..000000000 --- a/src-testing/src/geometries/Geometries.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -export * from "./BoxGeometry.js"; -export * from "./CapsuleGeometry.js"; -export * from "./CircleGeometry.js"; -export * from "./ConeGeometry.js"; -export * from "./CylinderGeometry.js"; -export * from "./DodecahedronGeometry.js"; -export * from "./EdgesGeometry.js"; -export * from "./ExtrudeGeometry.js"; -export * from "./IcosahedronGeometry.js"; -export * from "./LatheGeometry.js"; -export * from "./OctahedronGeometry.js"; -export * from "./PlaneGeometry.js"; -export * from "./PolyhedronGeometry.js"; -export * from "./RingGeometry.js"; -export * from "./ShapeGeometry.js"; -export * from "./SphereGeometry.js"; -export * from "./TetrahedronGeometry.js"; -export * from "./TorusGeometry.js"; -export * from "./TorusKnotGeometry.js"; -export * from "./TubeGeometry.js"; -export * from "./WireframeGeometry.js"; diff --git a/src-testing/src/geometries/IcosahedronGeometry.d.ts b/src-testing/src/geometries/IcosahedronGeometry.d.ts deleted file mode 100644 index 4c05b54de..000000000 --- a/src-testing/src/geometries/IcosahedronGeometry.d.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { PolyhedronGeometry } from "./PolyhedronGeometry.js"; - -/** - * A class for generating an icosahedron geometry. - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/IcosahedronGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/IcosahedronGeometry.js | Source} - */ -export class IcosahedronGeometry extends PolyhedronGeometry { - /** - * Create a new instance of {@link IcosahedronGeometry} - * @param radius Expects a `Float`. Default `1` - * @param detail Setting this to a value greater than 0 adds more vertices making it no longer an icosahedron. - * When detail is greater than 1, it's effectively a sphere. Expects a `Integer`. Default `0` - */ - constructor(radius?: number, detail?: number); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `IcosahedronGeometry` - */ - override readonly type: string | "IcosahedronGeometry"; - - /** @internal */ - static fromJSON(data: {}): IcosahedronGeometry; -} diff --git a/src-testing/src/geometries/LatheGeometry.d.ts b/src-testing/src/geometries/LatheGeometry.d.ts deleted file mode 100644 index f4194e514..000000000 --- a/src-testing/src/geometries/LatheGeometry.d.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { Vector2 } from "../math/Vector2.js"; - -/** - * Creates meshes with axial symmetry like vases - * @remarks - * The lathe rotates around the Y axis. - * @example - * ```typescript - * const points = []; - * for (let i = 0; i & lt; 10; i++) { - * points.push(new THREE.Vector2(Math.sin(i * 0.2) * 10 + 5, (i - 5) * 2)); - * } - * const geometry = new THREE.LatheGeometry(points); - * const material = new THREE.MeshBasicMaterial({ - * color: 0xffff00 - * }); - * const lathe = new THREE.Mesh(geometry, material); - * scene.add(lathe); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/LatheGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/LatheGeometry.js | Source} - */ -export class LatheGeometry extends BufferGeometry { - /** - * This creates a {@link LatheGeometry} based on the parameters. - * @param points Array of Vector2s. The x-coordinate of each point must be greater than zero. - * Default `[new Vector2(0, -0.5), new Vector2(0.5, 0), new Vector2(0, 0.5)]` _which creates a simple diamond shape_. - * @param segments The number of circumference segments to generate. Expects a `Integer`. Default `12`. - * @param phiStart The starting angle in radians. Expects a `Float`. Default `0`. - * @param phiLength The radian (0 to 2*PI) range of the lathed section 2*PI is a closed lathe, less than 2PI is a portion. Expects a `Float`. Default `Math.PI * 2`. - */ - constructor(points?: Vector2[], segments?: number, phiStart?: number, phiLength?: number); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `LatheGeometry` - */ - override readonly type: string | "LatheGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly points: Vector2[]; - readonly segments: number; - readonly phiStart: number; - readonly phiLength: number; - }; - - /** @internal */ - static fromJSON(data: {}): LatheGeometry; -} diff --git a/src-testing/src/geometries/OctahedronGeometry.d.ts b/src-testing/src/geometries/OctahedronGeometry.d.ts deleted file mode 100644 index 09ba0b1e7..000000000 --- a/src-testing/src/geometries/OctahedronGeometry.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { PolyhedronGeometry } from "./PolyhedronGeometry.js"; - -/** - * A class for generating an octahedron geometry. - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/OctahedronGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/OctahedronGeometry.js | Source} - */ -export class OctahedronGeometry extends PolyhedronGeometry { - /** - * Create a new instance of {@link OctahedronGeometry} - * @param radius Radius of the octahedron. Expects a `Float`. Default `1` - * @param detail Setting this to a value greater than zero add vertices making it no longer an octahedron. Expects a `Integer`. Default `0` - */ - constructor(radius?: number, detail?: number); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `OctahedronGeometry` - */ - override readonly type: string | "OctahedronGeometry"; - - /** @internal */ - static fromJSON(data: {}): OctahedronGeometry; -} diff --git a/src-testing/src/geometries/PlaneGeometry.d.ts b/src-testing/src/geometries/PlaneGeometry.d.ts deleted file mode 100644 index fda5f4908..000000000 --- a/src-testing/src/geometries/PlaneGeometry.d.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; - -/** - * A class for generating plane geometries. - * @example - * ```typescript - * const geometry = new THREE.PlaneGeometry(1, 1); - * const material = new THREE.MeshBasicMaterial({ - * color: 0xffff00, - * side: THREE.DoubleSide - * }); - * const plane = new THREE.Mesh(geometry, material); - * scene.add(plane); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/PlaneGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/PlaneGeometry.js | Source} - */ -export class PlaneGeometry extends BufferGeometry { - /** - * Create a new instance of {@link PlaneGeometry} - * @param width Width along the X axis. Expects a `Float`. Default `1` - * @param height Height along the Y axis. Expects a `Float`. Default `1` - * @param widthSegments Number of segmented faces along the width of the sides. Expects a `Integer`. Default `1` - * @param heightSegments Number of segmented faces along the height of the sides. Expects a `Integer`. Default `1` - */ - constructor(width?: number, height?: number, widthSegments?: number, heightSegments?: number); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `PlaneGeometry` - */ - override readonly type: string | "PlaneGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly width: number; - readonly height: number; - readonly widthSegments: number; - readonly heightSegments: number; - }; - - /** @internal */ - static fromJSON(data: {}): PlaneGeometry; -} diff --git a/src-testing/src/geometries/PolyhedronGeometry.d.ts b/src-testing/src/geometries/PolyhedronGeometry.d.ts deleted file mode 100644 index f4c07772b..000000000 --- a/src-testing/src/geometries/PolyhedronGeometry.d.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; - -/** - * A polyhedron is a solid in three dimensions with flat faces - * @remarks - * This class will take an array of vertices, project them onto a sphere, and then divide them up to the desired level of detail - * This class is used by {@link THREE.DodecahedronGeometry | DodecahedronGeometry}, {@link THREE.IcosahedronGeometry | IcosahedronGeometry}, - * {@link THREE.OctahedronGeometry | OctahedronGeometry}, and {@link THREE.TetrahedronGeometry | TetrahedronGeometry} to generate their respective geometries. - * @example - * ```typescript - * const verticesOfCube = [-1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, ]; - * const indicesOfFaces = [ - * 2, 1, 0, 0, 3, 2, - * 0, 4, 7, 7, 3, 0, - * 0, 1, 5, 5, 4, 0, - * 1, 2, 6, 6, 5, 1, - * 2, 3, 7, 7, 6, 2, - * 4, 5, 6, 6, 7, 4]; - * const geometry = new THREE.PolyhedronGeometry(verticesOfCube, indicesOfFaces, 6, 2); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/PolyhedronGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/PolyhedronGeometry.js | Source} - */ -export class PolyhedronGeometry extends BufferGeometry { - /** - * Create a new instance of {@link PolyhedronGeometry} - * @param vertices Array of points of the form [1,1,1, -1,-1,-1, ... ]. Default `[]`. - * @param indices Array of indices that make up the faces of the form [0,1,2, 2,3,0, ... ]. Default `[]`. - * @param radius [page:The radius of the final shape Expects a `Float`. Default `1` - * @param detail [page:How many levels to subdivide the geometry. The more detail, the smoother the shape. Expects a `Integer`. Default `0` - */ - constructor(vertices?: number[], indices?: number[], radius?: number, detail?: number); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `PolyhedronGeometry` - */ - override readonly type: string | "PolyhedronGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly vertices: number[]; - readonly indices: number[]; - readonly radius: number; - readonly detail: number; - }; - - /** @internal */ - static fromJSON(data: {}): PolyhedronGeometry; -} diff --git a/src-testing/src/geometries/RingGeometry.d.ts b/src-testing/src/geometries/RingGeometry.d.ts deleted file mode 100644 index 2cbc63ef4..000000000 --- a/src-testing/src/geometries/RingGeometry.d.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; - -/** - * A class for generating a two-dimensional ring geometry. - * @example - * ```typescript - * const geometry = new THREE.RingGeometry(1, 5, 32); - * const material = new THREE.MeshBasicMaterial({ - * color: 0xffff00, - * side: THREE.DoubleSide - * }); - * const mesh = new THREE.Mesh(geometry, material); - * scene.add(mesh); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/RingGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/RingGeometry.js | Source} - */ -export class RingGeometry extends BufferGeometry { - /** - * Create a new instance of {@link RingGeometry} - * @param innerRadius Expects a `Float`. Default `0.5`. - * @param outerRadius Expects a `Float`. Default `1`. - * @param thetaSegments Number of segments. A higher number means the ring will be more round. Minimum is 3. Expects a `Integer`. Default `32`. - * @param phiSegments Number of segments per ring segment. Minimum is `1`. Expects a `Integer`. Default `1`. - * @param thetaStart Starting angle. Expects a `Float`. Default `0`. - * @param thetaLength Central angle. Expects a `Float`. Default `Math.PI * 2`. - */ - constructor( - innerRadius?: number, - outerRadius?: number, - thetaSegments?: number, - phiSegments?: number, - thetaStart?: number, - thetaLength?: number, - ); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `RingGeometry` - */ - override readonly type: string | "RingGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly innerRadius: number; - readonly outerRadius: number; - readonly thetaSegments: number; - readonly phiSegments: number; - readonly thetaStart: number; - readonly thetaLength: number; - }; - - /** @internal */ - static fromJSON(data: {}): RingGeometry; -} diff --git a/src-testing/src/geometries/ShapeGeometry.d.ts b/src-testing/src/geometries/ShapeGeometry.d.ts deleted file mode 100644 index a3089a645..000000000 --- a/src-testing/src/geometries/ShapeGeometry.d.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { Shape } from "../extras/core/Shape.js"; - -/** - * Creates an one-sided polygonal geometry from one or more path shapes. - * @example - * ```typescript - * const x = 0, y = 0; - * const heartShape = new THREE.Shape(); - * heartShape.moveTo(x + 5, y + 5); - * heartShape.bezierCurveTo(x + 5, y + 5, x + 4, y, x, y); - * heartShape.bezierCurveTo(x - 6, y, x - 6, y + 7, x - 6, y + 7); - * heartShape.bezierCurveTo(x - 6, y + 11, x - 3, y + 15.4, x + 5, y + 19); - * heartShape.bezierCurveTo(x + 12, y + 15.4, x + 16, y + 11, x + 16, y + 7); - * heartShape.bezierCurveTo(x + 16, y + 7, x + 16, y, x + 10, y); - * heartShape.bezierCurveTo(x + 7, y, x + 5, y + 5, x + 5, y + 5); - * const geometry = new THREE.ShapeGeometry(heartShape); - * const material = new THREE.MeshBasicMaterial({ - * color: 0x00ff00 - * }); - * const mesh = new THREE.Mesh(geometry, material); - * scene.add(mesh); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/ShapeGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/ShapeGeometry.js | Source} - */ -export class ShapeGeometry extends BufferGeometry { - /** - * Create a new instance of {@link ShapeGeometry} - * @param shapes Array of shapes or a single {@link THREE.Shape | Shape}. Default `new Shape([new Vector2(0, 0.5), new Vector2(-0.5, -0.5), new Vector2(0.5, -0.5)])`, _a single triangle shape_. - * @param curveSegments Number of segments per shape. Expects a `Integer`. Default `12` - */ - constructor(shapes?: Shape | Shape[], curveSegments?: number); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `ShapeGeometry` - */ - override readonly type: string | "ShapeGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly shapes: Shape | Shape[]; - readonly curveSegments: number; - }; - - /** @internal */ - static fromJSON(data: {}): ShapeGeometry; -} diff --git a/src-testing/src/geometries/SphereGeometry.d.ts b/src-testing/src/geometries/SphereGeometry.d.ts deleted file mode 100644 index b597bb26c..000000000 --- a/src-testing/src/geometries/SphereGeometry.d.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; - -/** - * A class for generating sphere geometries. - * @example - * ```typescript - * const geometry = new THREE.SphereGeometry(15, 32, 16); - * const material = new THREE.MeshBasicMaterial({ - * color: 0xffff00 - * }); - * const sphere = new THREE.Mesh(geometry, material); - * scene.add(sphere); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/SphereGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/SphereGeometry.js | Source} - */ -export class SphereGeometry extends BufferGeometry { - /** - * Create a new instance of {@link SphereGeometry} - * @remarks - * The geometry is created by sweeping and calculating vertexes - * around the **Y** axis (horizontal sweep) and the **Z** axis (vertical sweep) - * Thus, incomplete spheres (akin to `'sphere slices'`) can be created - * through the use of different values of {@link phiStart}, {@link phiLength}, {@link thetaStart} and {@link thetaLength}, - * in order to define the points in which we start (or end) calculating those vertices. - * @param radius Sphere radius. Expects a `Float`. Default `1` - * @param widthSegments Number of horizontal segments. Minimum value is 3, and the Expects a `Integer`. Default `32` - * @param heightSegments Number of vertical segments. Minimum value is 2, and the Expects a `Integer`. Default `16` - * @param phiStart Specify horizontal starting angle. Expects a `Float`. Default `0` - * @param phiLength Specify horizontal sweep angle size. Expects a `Float`. Default `Math.PI * 2` - * @param thetaStart Specify vertical starting angle. Expects a `Float`. Default `0` - * @param thetaLength Specify vertical sweep angle size. Expects a `Float`. Default `Math.PI` - */ - constructor( - radius?: number, - widthSegments?: number, - heightSegments?: number, - phiStart?: number, - phiLength?: number, - thetaStart?: number, - thetaLength?: number, - ); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `SphereGeometry` - */ - override readonly type: string | "SphereGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly radius: number; - readonly widthSegments: number; - readonly heightSegments: number; - readonly phiStart: number; - readonly phiLength: number; - readonly thetaStart: number; - readonly thetaLength: number; - }; - - /** @internal */ - static fromJSON(data: {}): SphereGeometry; -} diff --git a/src-testing/src/geometries/TetrahedronGeometry.d.ts b/src-testing/src/geometries/TetrahedronGeometry.d.ts deleted file mode 100644 index 2dd0fe5b6..000000000 --- a/src-testing/src/geometries/TetrahedronGeometry.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { PolyhedronGeometry } from "./PolyhedronGeometry.js"; - -/** - * A class for generating a tetrahedron geometries. - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/TetrahedronGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/TetrahedronGeometry.js | Source} - */ -export class TetrahedronGeometry extends PolyhedronGeometry { - /** - * Create a new instance of {@link TetrahedronGeometry} - * @param radius Radius of the tetrahedron. Expects a `Float`. Default `1` - * @param detail Setting this to a value greater than 0 adds vertices making it no longer a tetrahedron. Expects a `Integer`. Default `0` - */ - constructor(radius?: number, detail?: number); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `TetrahedronGeometry` - */ - override readonly type: string | "TetrahedronGeometry"; - - /** @internal */ - static fromJSON(data: {}): TetrahedronGeometry; -} diff --git a/src-testing/src/geometries/TorusGeometry.d.ts b/src-testing/src/geometries/TorusGeometry.d.ts deleted file mode 100644 index 47a70ceea..000000000 --- a/src-testing/src/geometries/TorusGeometry.d.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; - -/** - * A class for generating torus geometries. - * @example - * ```typescript - * const geometry = new THREE.TorusGeometry(10, 3, 16, 100); - * const material = new THREE.MeshBasicMaterial({ - * color: 0xffff00 - * }); - * const torus = new THREE.Mesh(geometry, material); - * scene.add(torus); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/TorusGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/TorusGeometry.js | Source} - */ -export class TorusGeometry extends BufferGeometry { - /** - * Create a new instance of {@link TorusGeometry} - * @param radius Radius of the torus, from the center of the torus to the center of the tube. Expects a `Float`. Default `1`. - * @param tube Radius of the tube. Expects a `Float`. Default `0.4`. - * @param radialSegments Expects a `Integer`.Default is `12`. - * @param tubularSegments Expects a `Integer`. Default `48`. - * @param arc Central angle. Expects a `Float`. Default `Math.PI * 2` - */ - constructor(radius?: number, tube?: number, radialSegments?: number, tubularSegments?: number, arc?: number); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `TorusGeometry` - */ - override readonly type: string | "TorusGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly radius: number; - readonly tube: number; - readonly radialSegments: number; - readonly tubularSegments: number; - readonly arc: number; - }; - - /** @internal */ - static fromJSON(data: any): TorusGeometry; -} diff --git a/src-testing/src/geometries/TorusKnotGeometry.d.ts b/src-testing/src/geometries/TorusKnotGeometry.d.ts deleted file mode 100644 index 103b6916e..000000000 --- a/src-testing/src/geometries/TorusKnotGeometry.d.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; - -/** - * Creates a torus knot, the particular shape of which is defined by a pair of coprime integers, p and q - * If p and q are not coprime, the result will be a torus link. - * @example - * ```typescript - * const geometry = new THREE.TorusKnotGeometry(10, 3, 100, 16); - * const material = new THREE.MeshBasicMaterial({ - * color: 0xffff00 - * }); - * const torusKnot = new THREE.Mesh(geometry, material); - * scene.add(torusKnot); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/TorusKnotGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/TorusKnotGeometry.js | Source} - */ -export class TorusKnotGeometry extends BufferGeometry { - /** - * Create a new instance of {@link TorusKnotGeometry} - * @param radius Radius of the torus.. Default `1`. - * @param tube Expects a `Float`. Default `0.4`. - * @param tubularSegments Expects a `Integer`. Default `64`. - * @param radialSegments Expects a `Integer`. Default `8`. - * @param p This value determines, how many times the geometry winds around its axis of rotational symmetry. Expects a `Integer`. Default `2`. - * @param q This value determines, how many times the geometry winds around a circle in the interior of the torus. Expects a `Integer`. Default `3`. - */ - constructor( - radius?: number, - tube?: number, - tubularSegments?: number, - radialSegments?: number, - p?: number, - q?: number, - ); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `TorusKnotGeometry` - */ - override readonly type: string | "TorusKnotGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly radius: number; - readonly tube: number; - readonly tubularSegments: number; - readonly radialSegments: number; - readonly p: number; - readonly q: number; - }; - - /** @internal */ - static fromJSON(data: {}): TorusKnotGeometry; -} diff --git a/src-testing/src/geometries/TubeGeometry.d.ts b/src-testing/src/geometries/TubeGeometry.d.ts deleted file mode 100644 index 37e7129ad..000000000 --- a/src-testing/src/geometries/TubeGeometry.d.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { Curve } from "../extras/core/Curve.js"; -import { Vector3 } from "../math/Vector3.js"; - -/** - * Creates a tube that extrudes along a 3d curve. - * @example - * ```typescript - * class CustomSinCurve extends THREE.Curve { - * constructor(scale = 1) { - * super(); - * this.scale = scale; - * } - * getPoint(t, optionalTarget = new THREE.Vector3()) { - * const tx = t * 3 - 1.5; - * const ty = Math.sin(2 * Math.PI * t); - * const tz = 0; - * return optionalTarget.set(tx, ty, tz).multiplyScalar(this.scale); - * } - * } - * const path = new CustomSinCurve(10); - * const geometry = new THREE.TubeGeometry(path, 20, 2, 8, false); - * const material = new THREE.MeshBasicMaterial({ - * color: 0x00ff00 - * }); - * const mesh = new THREE.Mesh(geometry, material); - * scene.add(mesh); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/TubeGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/TubeGeometry.js | Source} - */ -export class TubeGeometry extends BufferGeometry { - /** - * Create a new instance of {@link TubeGeometry} - * @param path A 3D path that inherits from the {@link THREE.Curve | Curve} base class. - * Default {@link THREE.QuadraticBezierCurve3 | new THREE.QuadraticBezierCurve3(new Vector3(-1, -1, 0 ), new Vector3(-1, 1, 0), new Vector3(1, 1, 0))}. - * @param tubularSegments The number of segments that make up the tube. Expects a `Integer`. Default `64`. - * @param radius The radius of the tube. Expects a `Float`. Default `1`. - * @param radialSegments The number of segments that make up the cross-section. Expects a `Integer`. Default `8`. - * @param closed Is the tube open or closed. Default `false`. - */ - constructor( - path?: Curve, - tubularSegments?: number, - radius?: number, - radialSegments?: number, - closed?: boolean, - ); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `TubeGeometry` - */ - override readonly type: string | "TubeGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly path: Curve; - readonly tubularSegments: number; - readonly radius: number; - readonly radialSegments: number; - readonly closed: boolean; - }; - - /** - * An array of {@link THREE.Vector3 | Vector3} tangents - */ - tangents: Vector3[]; - - /** - * An array of {@link THREE.Vector3 | Vector3} normals - */ - normals: Vector3[]; - - /** - * An array of {@link THREE.Vector3 | Vector3} binormals - */ - binormals: Vector3[]; - - /** @internal */ - static fromJSON(data: {}): TubeGeometry; -} diff --git a/src-testing/src/geometries/WireframeGeometry.d.ts b/src-testing/src/geometries/WireframeGeometry.d.ts deleted file mode 100644 index 6263316a6..000000000 --- a/src-testing/src/geometries/WireframeGeometry.d.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; - -/** - * This can be used as a helper object to view a {@link BufferGeometry | geometry} as a wireframe. - * @example - * ```typescript - * const geometry = new THREE.SphereGeometry(100, 100, 100); - * const wireframe = new THREE.WireframeGeometry(geometry); - * const line = new THREE.LineSegments(wireframe); - * line.material.depthTest = false; - * line.material.opacity = 0.25; - * line.material.transparent = true; - * scene.add(line); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_helpers | helpers} - * @see {@link https://threejs.org/docs/index.html#api/en/geometries/WireframeGeometry | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/geometries/WireframeGeometry.js | Source} - */ -export class WireframeGeometry extends BufferGeometry { - /** - * Create a new instance of {@link WireframeGeometry} - * @param geometry Any geometry object. Default `null`. - */ - constructor(geometry?: TBufferGeometry); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `WireframeGeometry` - */ - override readonly type: string | "WireframeGeometry"; - - /** - * An object with a property for each of the constructor parameters. - * @remarks Any modification after instantiation does not change the geometry. - */ - readonly parameters: { - readonly geometry: TBufferGeometry; - }; -} diff --git a/src-testing/src/helpers/ArrowHelper.d.ts b/src-testing/src/helpers/ArrowHelper.d.ts deleted file mode 100644 index 94896123c..000000000 --- a/src-testing/src/helpers/ArrowHelper.d.ts +++ /dev/null @@ -1,93 +0,0 @@ -import { Object3D } from "../core/Object3D.js"; -import { ColorRepresentation } from "../math/Color.js"; -import { Vector3 } from "../math/Vector3.js"; -import { Line } from "../objects/Line.js"; -import { Mesh } from "../objects/Mesh.js"; - -/** - * An 3D arrow object for visualizing directions. - * @example - * ```typescript - * const dir = new THREE.Vector3(1, 2, 0); - * //normalize the direction vector (convert to vector of length 1) - * dir.normalize(); - * const origin = new THREE.Vector3(0, 0, 0); - * const length = 1; - * const hex = 0xffff00; - * const {@link ArrowHelper} = new THREE.ArrowHelper(dir, origin, length, hex); - * scene.add(arrowHelper); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_shadowmesh | WebGL / shadowmesh} - * @see {@link https://threejs.org/docs/index.html#api/en/helpers/ArrowHelper | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/ArrowHelper.js | Source} - */ -export class ArrowHelper extends Object3D { - /** - * Create a new instance of {@link ArrowHelper} - * @param dir Direction from origin. Must be a unit vector. Default `new THREE.Vector3(0, 0, 1)` - * @param origin Point at which the arrow starts. Default `new THREE.Vector3(0, 0, 0)` - * @param length Length of the arrow. Default `1` - * @param hex Hexadecimal value to define color. Default `0xffff00` - * @param headLength The length of the head of the arrow. Default `0.2 * length` - * @param headWidth The width of the head of the arrow. Default `0.2 * headLength` - */ - constructor( - dir?: Vector3, - origin?: Vector3, - length?: number, - color?: ColorRepresentation, - headLength?: number, - headWidth?: number, - ); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @override - * @defaultValue `ArrowHelper` - */ - override readonly type: string | "ArrowHelper"; - - /** - * Contains the line part of the arrowHelper. - */ - line: Line; - - /** - * Contains the cone part of the arrowHelper. - */ - cone: Mesh; - - /** - * Sets the color of the arrowHelper. - * @param color The desired color. - */ - setColor(color: ColorRepresentation): void; - - /** - * @param dir The desired direction. Must be a unit vector. - */ - setDirection(dir: Vector3): void; - - /** - * Sets the length of the arrowhelper. - * @param length The desired length. - * @param headLength The length of the head of the arrow. Default `0.2 * length` - * @param headWidth The width of the head of the arrow. Default `0.2 * headLength` - */ - setLength(length: number, headLength?: number, headWidth?: number): void; - - /** - * Copy the given object into this object - * @remarks Note: event listeners and user-defined callbacks ({@link onAfterRender | .onAfterRender} and {@link onBeforeRender | .onBeforeRender}) are not copied. - * @param source - */ - override copy(source: this): this; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/helpers/AxesHelper.d.ts b/src-testing/src/helpers/AxesHelper.d.ts deleted file mode 100644 index c0633c102..000000000 --- a/src-testing/src/helpers/AxesHelper.d.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { ColorRepresentation } from "../math/Color.js"; -import { LineSegments } from "../objects/LineSegments.js"; - -/** - * An axis object to visualize the 3 axes in a simple way. - * @remarks - * The X axis is red - * The Y axis is green - * The Z axis is blue. - * @example - * ```typescript - * const {@link AxesHelper} = new THREE.AxesHelper(5); - * scene.add(axesHelper); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_compression | WebGL / buffergeometry / compression} - * @see Example: {@link https://threejs.org/examples/#webgl_geometry_convex | WebGL / geometry / convex} - * @see Example: {@link https://threejs.org/examples/#webgl_loader_nrrd | WebGL / loader / nrrd} - * @see {@link https://threejs.org/docs/index.html#api/en/helpers/AxesHelper | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/AxesHelper.js | Source} - */ -export class AxesHelper extends LineSegments { - /** - * Create a new instance of {@link AxesHelper} - * @param size Size of the lines representing the axes. Default `1` - */ - constructor(size?: number); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @override - * @defaultValue `AxesHelper` - */ - override readonly type: string | "AxesHelper"; - - /** - * Sets the axes colors to {@link Color | xAxisColor}, {@link Color | yAxisColor}, {@link Color | zAxisColor}. - * @param xAxisColor - * @param yAxisColor - * @param zAxisColor - */ - setColors(xAxisColor: ColorRepresentation, yAxisColor: ColorRepresentation, zAxisColor: ColorRepresentation): this; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/helpers/Box3Helper.d.ts b/src-testing/src/helpers/Box3Helper.d.ts deleted file mode 100644 index 78e1c6f82..000000000 --- a/src-testing/src/helpers/Box3Helper.d.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { Box3 } from "../math/Box3.js"; -import { ColorRepresentation } from "../math/Color.js"; -import { LineSegments } from "../objects/LineSegments.js"; - -/** - * Helper object to visualize a {@link THREE.Box3 | Box3}. - * @example - * ```typescript - * const box = new THREE.Box3(); - * box.setFromCenterAndSize(new THREE.Vector3(1, 1, 1), new THREE.Vector3(2, 1, 3)); - * const helper = new THREE.Box3Helper(box, 0xffff00); - * scene.add(helper); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/helpers/Box3Helper | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/Box3Helper.js | Source} - */ -export class Box3Helper extends LineSegments { - /** - * Creates a new wireframe box that represents the passed Box3. - * @param box The Box3 to show. - * @param color The box's color. Default `0xffff00` - */ - constructor(box: Box3, color?: ColorRepresentation); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @override - * @defaultValue `Box3Helper` - */ - override readonly type: string | "Box3Helper"; - - /** - * The Box3 being visualized. - */ - box: Box3; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/helpers/BoxHelper.d.ts b/src-testing/src/helpers/BoxHelper.d.ts deleted file mode 100644 index d049a5b72..000000000 --- a/src-testing/src/helpers/BoxHelper.d.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { Object3D } from "../core/Object3D.js"; -import { LineBasicMaterial } from "../materials/LineBasicMaterial.js"; -import { ColorRepresentation } from "../math/Color.js"; -import { LineSegments } from "../objects/LineSegments.js"; - -/** - * Helper object to graphically show the world-axis-aligned bounding box around an object - * @remarks - * The actual bounding box is handled with {@link THREE.Box3 | Box3}, this is just a visual helper for debugging - * It can be automatically resized with the {@link THREE.BoxHelper.update | BoxHelper.update} method when the object it's created from is transformed - * Note that the object must have a {@link THREE.BufferGeometry | BufferGeometry} for this to work, so it won't work with {@link Sprite | Sprites}. - * @example - * ```typescript - * const sphere = new THREE.SphereGeometry(); - * const object = new THREE.Mesh(sphere, new THREE.MeshBasicMaterial(0xff0000)); - * const box = new THREE.BoxHelper(object, 0xffff00); - * scene.add(box); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_helpers | WebGL / helpers} - * @see Example: {@link https://threejs.org/examples/#webgl_loader_nrrd | WebGL / loader / nrrd} - * @see Example: {@link https://threejs.org/examples/#webgl_buffergeometry_drawrange | WebGL / buffergeometry / drawrange} - * @see {@link https://threejs.org/docs/index.html#api/en/helpers/BoxHelper | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/BoxHelper.js | Source} - */ -export class BoxHelper extends LineSegments { - /** - * Creates a new wireframe box that bounds the passed object - * @remarks - * Internally this uses {@link THREE.Box3.setFromObject | Box3.setFromObject} to calculate the dimensions - * Note that this includes any children. - * @param object The object3D to show the world-axis-aligned bounding box. - * @param color Hexadecimal value that defines the box's color. Default `0xffff00` - */ - constructor(object: Object3D, color?: ColorRepresentation); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @override - * @defaultValue `BoxHelper` - */ - override readonly type: string | "BoxHelper"; - - /** - * Updates the helper's geometry to match the dimensions of the object, including any children - * @remarks - * See {@link THREE.Box3.setFromObject | Box3.setFromObject}. - */ - update(object?: Object3D): void; - - /** - * Updates the wireframe box for the passed object. - * @param object {@link THREE.Object3D | Object3D} to create the helper of. - */ - setFromObject(object: Object3D): this; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/helpers/CameraHelper.d.ts b/src-testing/src/helpers/CameraHelper.d.ts deleted file mode 100644 index 469dcaa0d..000000000 --- a/src-testing/src/helpers/CameraHelper.d.ts +++ /dev/null @@ -1,80 +0,0 @@ -import { Camera } from "../cameras/Camera.js"; -import { Color } from "../math/Color.js"; -import { Matrix4 } from "../math/Matrix4.js"; -import { LineSegments } from "../objects/LineSegments.js"; - -/** - * This helps with visualizing what a camera contains in its frustum - * @remarks - * It visualizes the frustum of a camera using a {@link THREE.LineSegments | LineSegments}. - * @remarks {@link CameraHelper} must be a child of the scene. - * @example - * ```typescript - * const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); - * const helper = new THREE.CameraHelper(camera); - * scene.add(helper); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_camera | WebGL / camera} - * @see Example: {@link https://threejs.org/examples/#webgl_geometry_extrude_splines | WebGL / extrude / splines} - * @see {@link https://threejs.org/docs/index.html#api/en/helpers/CameraHelper | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/CameraHelper.js | Source} - */ -export class CameraHelper extends LineSegments { - /** - * This create a new {@link CameraHelper} for the specified camera. - * @param camera The camera to visualize. - */ - constructor(camera: Camera); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @override - * @defaultValue `CameraHelper` - */ - override readonly type: string | "CameraHelper"; - - /** - * The camera being visualized. - */ - camera: Camera; - - /** - * This contains the points used to visualize the camera. - */ - pointMap: { [id: string]: number[] }; - - /** - * Reference to the {@link THREE.Camera.matrixWorld | camera.matrixWorld}. - */ - matrix: Matrix4; - - /** - * Is set to `false`, as the helper is using the {@link THREE.Camera.matrixWorld | camera.matrixWorld}. - * @see {@link THREE.Object3D.matrixAutoUpdate | Object3D.matrixAutoUpdate}. - * @defaultValue `false`. - */ - override matrixAutoUpdate: boolean; - - /** - * Defines the colors of the helper. - * @param frustum - * @param cone - * @param up - * @param target - * @param cross - */ - setColors(frustum: Color, cone: Color, up: Color, target: Color, cross: Color): this; - - /** - * Updates the helper based on the projectionMatrix of the camera. - */ - update(): void; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/helpers/DirectionalLightHelper.d.ts b/src-testing/src/helpers/DirectionalLightHelper.d.ts deleted file mode 100644 index 729eccedd..000000000 --- a/src-testing/src/helpers/DirectionalLightHelper.d.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { Object3D } from "../core/Object3D.js"; -import { DirectionalLight } from "../lights/DirectionalLight.js"; -import { ColorRepresentation } from "../math/Color.js"; -import { Matrix4 } from "../math/Matrix4.js"; -import { Line } from "../objects/Line.js"; - -/** - * Helper object to assist with visualizing a {@link THREE.DirectionalLight | DirectionalLight}'s effect on the scene - * @remarks - * This consists of plane and a line representing the light's position and direction. - * @example - * ```typescript - * const light = new THREE.DirectionalLight(0xFFFFFF); - * scene.add(light); - * - * const helper = new THREE.DirectionalLightHelper(light, 5); - * scene.add(helper); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/helpers/DirectionalLightHelper | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/DirectionalLightHelper.js | Source} - */ -export class DirectionalLightHelper extends Object3D { - /** - * Create a new instance of {@link DirectionalLightHelper} - * @param light The light to be visualized. - * @param size Dimensions of the plane. Default `1` - * @param color If this is not the set the helper will take the color of the light. Default `light.color` - */ - constructor(light: DirectionalLight, size?: number, color?: ColorRepresentation); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @override - * @defaultValue `DirectionalLightHelper` - */ - override readonly type: string | "DirectionalLightHelper"; - - /** - * Contains the line mesh showing the location of the directional light. - */ - lightPlane: Line; - - /** - * Reference to the {@link THREE.DirectionalLight | directionalLight} being visualized. - */ - light: DirectionalLight; - - /** - * Reference to the {@link THREE.DirectionalLight.matrixWorld | light.matrixWorld}. - */ - matrix: Matrix4; - - /** - * Is set to `false`, as the helper is using the {@link THREE.DirectionalLight.matrixWorld | light.matrixWorld}. - * @see {@link THREE.Object3D.matrixAutoUpdate | Object3D.matrixAutoUpdate}. - * @defaultValue `false`. - */ - override matrixAutoUpdate: boolean; - - /** - * The color parameter passed in the constructor. - * @remarks If this is changed, the helper's color will update the next time {@link update} is called. - * @defaultValue `undefined` - */ - color: ColorRepresentation | undefined; - - targetLine: Line; // TODO: Double check if this need to be exposed or not. - - /** - * Updates the helper to match the position and direction of the {@link light | DirectionalLight} being visualized. - */ - update(): void; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/helpers/GridHelper.d.ts b/src-testing/src/helpers/GridHelper.d.ts deleted file mode 100644 index 0b786b992..000000000 --- a/src-testing/src/helpers/GridHelper.d.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { LineBasicMaterial } from "../materials/LineBasicMaterial.js"; -import { ColorRepresentation } from "../math/Color.js"; -import { LineSegments } from "../objects/LineSegments.js"; - -/** - * The {@link GridHelper} is an object to define grids - * @remarks - * Grids are two-dimensional arrays of lines. - * @example - * ```typescript - * const size = 10; - * const divisions = 10; - * const {@link GridHelper} = new THREE.GridHelper(size, divisions); - * scene.add(gridHelper); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_helpers | WebGL / helpers} - * @see {@link https://threejs.org/docs/index.html#api/en/helpers/GridHelper | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/GridHelper.js | Source} - */ -export class GridHelper extends LineSegments { - /** - * Creates a new {@link GridHelper} of size 'size' and divided into 'divisions' segments per side - * @remarks - * Colors are optional. - * @param size The size of the grid. Default `10` - * @param divisions The number of divisions across the grid. Default `10` - * @param colorCenterLine The color of the centerline. This can be a {@link THREE.Color | Color}, a hexadecimal value and an CSS-Color name. Default `0x444444` - * @param colorGrid The color of the lines of the grid. This can be a {@link THREE.Color | Color}, a hexadecimal value and an CSS-Color name. Default `0x888888` - */ - constructor(size?: number, divisions?: number, color1?: ColorRepresentation, color2?: ColorRepresentation); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @override - * @defaultValue `GridHelper` - */ - override readonly type: string | "GridHelper"; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/helpers/HemisphereLightHelper.d.ts b/src-testing/src/helpers/HemisphereLightHelper.d.ts deleted file mode 100644 index 80366b63b..000000000 --- a/src-testing/src/helpers/HemisphereLightHelper.d.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { Object3D } from "../core/Object3D.js"; -import { HemisphereLight } from "../lights/HemisphereLight.js"; -import { MeshBasicMaterial } from "../materials/MeshBasicMaterial.js"; -import { ColorRepresentation } from "../math/Color.js"; -import { Matrix4 } from "../math/Matrix4.js"; - -/** - * Creates a visual aid consisting of a spherical {@link THREE.Mesh | Mesh} for a {@link THREE.HemisphereLight | HemisphereLight}. - * @example - * ```typescript - * const light = new THREE.HemisphereLight(0xffffbb, 0x080820, 1); - * const helper = new THREE.HemisphereLightHelper(light, 5); - * scene.add(helper); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/helpers/HemisphereLightHelper | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/HemisphereLightHelper.js | Source} - */ -export class HemisphereLightHelper extends Object3D { - /** - * Create a new instance of {@link HemisphereLightHelper} - * @param light The light being visualized. - * @param size Thr sphere size - * @param color If this is not the set the helper will take the color of the light. - */ - constructor(light: HemisphereLight, size: number, color?: ColorRepresentation); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @override - * @defaultValue `HemisphereLightHelper` - */ - override readonly type: string | "HemisphereLightHelper"; - - /** - * Reference to the HemisphereLight being visualized. - */ - light: HemisphereLight; - - /** - * Reference to the {@link THREE.HemisphereLight.matrixWorld | light.matrixWorld}. - */ - matrix: Matrix4; - - /** - * Is set to `false`, as the helper is using the {@link THREE.HemisphereLight.matrixWorld | light.matrixWorld}. - * @see {@link THREE.Object3D.matrixAutoUpdate | Object3D.matrixAutoUpdate}. - * @defaultValue `false`. - */ - override matrixAutoUpdate: boolean; - - material: MeshBasicMaterial; // TODO: Double check if this need to be exposed or not. - - /** - * The color parameter passed in the constructor. - * @remarks If this is changed, the helper's color will update the next time {@link update} is called. - * @defaultValue `undefined` - */ - color: ColorRepresentation | undefined; - - /** - * Updates the helper to match the position and direction of the {@link .light | HemisphereLight}. - */ - update(): void; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/helpers/PlaneHelper.d.ts b/src-testing/src/helpers/PlaneHelper.d.ts deleted file mode 100644 index 43c9821cb..000000000 --- a/src-testing/src/helpers/PlaneHelper.d.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { Plane } from "../math/Plane.js"; -import { LineSegments } from "../objects/LineSegments.js"; - -/** - * Helper object to visualize a {@link THREE.Plane | Plane}. - * @example - * ```typescript - * const plane = new THREE.Plane(new THREE.Vector3(1, 1, 0.2), 3); - * const helper = new THREE.PlaneHelper(plane, 1, 0xffff00); - * scene.add(helper); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/helpers/PlaneHelper | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/PlaneHelper.js | Source} - */ -export class PlaneHelper extends LineSegments { - /** - * Creates a new wireframe representation of the passed plane. - * @param plane The plane to visualize. - * @param size Side length of plane helper. Expects a `Float`. Default `1` - * @param hex Color. Default `0xffff00` - */ - constructor(plane: Plane, size?: number, hex?: number); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @override - * @defaultValue `PlaneHelper` - */ - override readonly type: string | "PlaneHelper"; - - /** - * The {@link Plane | plane} being visualized. - */ - plane: Plane; - - /** - * The side lengths of plane helper. - * @remarks Expects a `Float` - * @defaultValue `1` - */ - size: number; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/helpers/PointLightHelper.d.ts b/src-testing/src/helpers/PointLightHelper.d.ts deleted file mode 100644 index 7d61da5e3..000000000 --- a/src-testing/src/helpers/PointLightHelper.d.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { Object3D } from "../core/Object3D.js"; -import { PointLight } from "../lights/PointLight.js"; -import { ColorRepresentation } from "../math/Color.js"; -import { Matrix4 } from "../math/Matrix4.js"; - -/** - * This displays a helper object consisting of a spherical {@link THREE.Mesh | Mesh} for visualizing a {@link THREE.PointLight | PointLight}. - * @example - * ```typescript - * const pointLight = new THREE.PointLight(0xff0000, 1, 100); - * pointLight.position.set(10, 10, 10); - * scene.add(pointLight); - * const sphereSize = 1; - * const {@link PointLightHelper} = new THREE.PointLightHelper(pointLight, sphereSize); - * scene.add(pointLightHelper); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_helpers | WebGL / helpers} - * @see {@link https://threejs.org/docs/index.html#api/en/helpers/PointLightHelper | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/PointLightHelper.js | Source} - */ -export class PointLightHelper extends Object3D { - /** - * Create a new instance of {@link PointLightHelper} - * @param light The light to be visualized. - * @param sphereSize The size of the sphere helper. Expects a `Float`. Default `1` - * @param color If this is not the set the helper will take the color of the light. - */ - constructor(light: PointLight, sphereSize?: number, color?: ColorRepresentation); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @override - * @defaultValue `PointLightHelper` - */ - override readonly type: string | "PointLightHelper"; - - /** - * The {@link THREE.PointLight | PointLight} that is being visualized. - */ - light: PointLight; - - /** - * Reference to the {@link THREE.PointLight.matrixWorld | light.matrixWorld}. - */ - matrix: Matrix4; - - /** - * The color parameter passed in the constructor. - * @remarks If this is changed, the helper's color will update the next time {@link update} is called. - * @defaultValue `undefined` - */ - color: ColorRepresentation | undefined; - - /** - * Is set to `false`, as the helper is using the {@link THREE.PointLight.matrixWorld | light.matrixWorld}. - * @see {@link THREE.Object3D.matrixAutoUpdate | Object3D.matrixAutoUpdate}. - * @defaultValue `false`. - */ - override matrixAutoUpdate: boolean; - - /** - * Updates the helper to match the position of the {@link THREE..light | .light}. - */ - update(): void; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/helpers/PolarGridHelper.d.ts b/src-testing/src/helpers/PolarGridHelper.d.ts deleted file mode 100644 index 994d71c94..000000000 --- a/src-testing/src/helpers/PolarGridHelper.d.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { ColorRepresentation } from "../math/Color.js"; -import { LineSegments } from "../objects/LineSegments.js"; - -/** - * The {@link PolarGridHelper} is an object to define polar grids - * @remarks - * Grids are two-dimensional arrays of lines. - * @example - * ```typescript - * const radius = 10; - * const sectors = 16; - * const rings = 8; - * const divisions = 64; - * const helper = new THREE.PolarGridHelper(radius, sectors, rings, divisions); - * scene.add(helper); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_helpers | WebGL / helpers} - * @see {@link https://threejs.org/docs/index.html#api/en/helpers/PolarGridHelper | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/PolarGridHelper.js | Source} - */ -export class PolarGridHelper extends LineSegments { - /** - * Creates a new {@link PolarGridHelper} of radius 'radius' with 'sectors' number of sectors and 'rings' number of rings, where each circle is smoothed into 'divisions' number of line segments. - * @remarks Colors are optional. - * @param radius The radius of the polar grid. This can be any positive number. Default `10`. - * @param sectors The number of sectors the grid will be divided into. This can be any positive integer. Default `16`. - * @param rings The number of rings. This can be any positive integer. Default `8`. - * @param divisions The number of line segments used for each circle. This can be any positive integer that is 3 or greater. Default `64`. - * @param color1 The first color used for grid elements. This can be a {@link THREE.Color | Color}, a hexadecimal value and an CSS-Color name. Default `0x444444`. - * @param color2 The second color used for grid elements. This can be a {@link THREE.Color | Color}, a hexadecimal value and an CSS-Color name. Default `0x888888`. - */ - constructor( - radius?: number, - radials?: number, - circles?: number, - divisions?: number, - color1?: ColorRepresentation, - color2?: ColorRepresentation, - ); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @override - * @defaultValue `PolarGridHelper` - */ - override readonly type: string | "PolarGridHelper"; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/helpers/SkeletonHelper.d.ts b/src-testing/src/helpers/SkeletonHelper.d.ts deleted file mode 100644 index 772ebf30e..000000000 --- a/src-testing/src/helpers/SkeletonHelper.d.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { Object3D } from "../core/Object3D.js"; -import { Matrix4 } from "../math/Matrix4.js"; -import { Bone } from "../objects/Bone.js"; -import { LineSegments } from "../objects/LineSegments.js"; -import { SkinnedMesh } from "../objects/SkinnedMesh.js"; - -/** - * A helper object to assist with visualizing a {@link Skeleton | Skeleton} - * @remarks - * The helper is rendered using a {@link LineBasicMaterial | LineBasicMaterial}. - * @example - * ```typescript - * const helper = new THREE.SkeletonHelper(skinnedMesh); - * scene.add(helper); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_animation_skinning_blending | WebGL / animation / skinning / blending} - * @see Example: {@link https://threejs.org/examples/#webgl_animation_skinning_morph | WebGL / animation / skinning / morph} - * @see Example: {@link https://threejs.org/examples/#webgl_loader_bvh | WebGL / loader / bvh } - * @see {@link https://threejs.org/docs/index.html#api/en/helpers/SkeletonHelper | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/SkeletonHelper.js | Source} - */ -export class SkeletonHelper extends LineSegments { - /** - * Create a new instance of {@link SkeletonHelper} - * @param object Usually an instance of {@link THREE.SkinnedMesh | SkinnedMesh}. - * However, any instance of {@link THREE.Object3D | Object3D} can be used if it represents a hierarchy of {@link Bone | Bone}s (via {@link THREE.Object3D.children | Object3D.children}). - */ - constructor(object: SkinnedMesh | Object3D); - - /** - * Read-only flag to check if a given object is of type {@link SkeletonHelper}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isSkeletonHelper = true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @override - * @defaultValue `SkeletonHelper` - */ - override readonly type: string | "SkeletonHelper"; - - /** - * The list of bones that the helper renders as {@link Line | Lines}. - */ - bones: Bone[]; - - /** - * The object passed in the constructor. - */ - root: SkinnedMesh | Object3D; - - /** - * Reference to the {@link THREE.Object3D.matrixWorld | root.matrixWorld}. - */ - matrix: Matrix4; - - /** - * Is set to `false`, as the helper is using the {@link THREE.Object3D.matrixWorld | root.matrixWorld}. - * @see {@link THREE.Object3D.matrixAutoUpdate | Object3D.matrixAutoUpdate}. - * @defaultValue `false`. - */ - override matrixAutoUpdate: boolean; - - /** - * Updates the helper. - */ - update(): void; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/helpers/SpotLightHelper.d.ts b/src-testing/src/helpers/SpotLightHelper.d.ts deleted file mode 100644 index f620ed990..000000000 --- a/src-testing/src/helpers/SpotLightHelper.d.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { Object3D } from "../core/Object3D.js"; -import { Light } from "../lights/Light.js"; -import { ColorRepresentation } from "../math/Color.js"; -import { Matrix4 } from "../math/Matrix4.js"; -import { LineSegments } from "../objects/LineSegments.js"; - -/** - * This displays a cone shaped helper object for a {@link THREE.SpotLight | SpotLight}. - * @example - * ```typescript - * const spotLight = new THREE.SpotLight(0xffffff); - * spotLight.position.set(10, 10, 10); - * scene.add(spotLight); - * const {@link SpotLightHelper} = new THREE.SpotLightHelper(spotLight); - * scene.add(spotLightHelper); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_lights_spotlights | WebGL/ lights / spotlights } - * @see {@link https://threejs.org/docs/index.html#api/en/helpers/SpotLightHelper | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/helpers/SpotLightHelper.js | Source} - */ -export class SpotLightHelper extends Object3D { - /** - * Create a new instance of {@link SpotLightHelper} - * @param light The {@link THREE.SpotLight | SpotLight} to be visualized. - * @param color If this is not the set the helper will take the color of the light. Default `light.color` - */ - constructor(light: Light, color?: ColorRepresentation); - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @override - * @defaultValue `SpotLightHelper` - */ - override readonly type: string | "SpotLightHelper"; - - /** - * {@link THREE.LineSegments | LineSegments} used to visualize the light. - */ - cone: LineSegments; - - /** - * Reference to the {@link THREE.SpotLight | SpotLight} being visualized. - */ - light: Light; - - /** - * Reference to the spotLight's {@link Object3D.matrixWorld | matrixWorld}. - */ - matrix: Matrix4; - - /** - * The color parameter passed in the constructor. - * If this is changed, the helper's color will update the next time {@link SpotLightHelper.update | update} is called. - * @defaultValue `undefined` - */ - color: ColorRepresentation | undefined; - - /** - * Is set to `false`, as the helper is using the {@link THREE.Light.matrixWorld | light.matrixWorld}. - * @see {@link THREE.Object3D.matrixAutoUpdate | Object3D.matrixAutoUpdate}. - * @defaultValue `false`. - */ - override matrixAutoUpdate: boolean; - - /** - * Updates the light helper. - */ - update(): void; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/lights/AmbientLight.d.ts b/src-testing/src/lights/AmbientLight.d.ts deleted file mode 100644 index 7000a37e7..000000000 --- a/src-testing/src/lights/AmbientLight.d.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { ColorRepresentation } from "../math/Color.js"; -import { Light } from "./Light.js"; - -/** - * This light globally illuminates all objects in the scene equally. - * @remarks This light cannot be used to cast shadows as it does not have a direction. - * @example - * ```typescript - * const light = new THREE.AmbientLight(0x404040); // soft white light - * scene.add(light); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/lights/AmbientLight | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/AmbientLight.js | Source} - */ -export class AmbientLight extends Light { - /** - * Creates a new {@link AmbientLight}. - * @param color Numeric value of the RGB component of the color. Default `0xffffff` - * @param intensity Numeric value of the light's strength/intensity. Expects a `Float`. Default `1` - */ - constructor(color?: ColorRepresentation, intensity?: number); - - /** - * Read-only flag to check if a given object is of type {@link AmbientLight}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isAmbientLight: true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `AmbientLight` - */ - override readonly type: string | "AmbientLight"; -} diff --git a/src-testing/src/lights/DirectionalLight.d.ts b/src-testing/src/lights/DirectionalLight.d.ts deleted file mode 100644 index 3d43b7d89..000000000 --- a/src-testing/src/lights/DirectionalLight.d.ts +++ /dev/null @@ -1,103 +0,0 @@ -import { Object3D } from "../core/Object3D.js"; -import { ColorRepresentation } from "../math/Color.js"; -import { Vector3 } from "../math/Vector3.js"; -import { DirectionalLightShadow } from "./DirectionalLightShadow.js"; -import { Light } from "./Light.js"; - -/** - * A light that gets emitted in a specific direction - * @remarks - * This light will behave as though it is infinitely far away and the rays produced from it are all parallel - * The common use case for this is to simulate daylight; the sun is far enough away that its position can be considered to be infinite, and all light rays coming from it are parallel. - * A common point of confusion for directional lights is that setting the rotation has no effect - * @remarks - * This is because three.js's {@link DirectionalLight} is the equivalent to what is often called a 'Target Direct Light' in other applications. - * This means that its direction is calculated as pointing from the light's {@link THREE.Object3D.position | position} to the {@link THREE.DirectionalLight.target | target}'s - * position (as opposed to a 'Free Direct Light' that just has a rotation component). - * See the {@link THREE.DirectionalLight.target | target} property below for details on updating the target. - * @example - * ```typescript - * // White directional light at half intensity shining from the top. - * const {@link DirectionalLight} = new THREE.DirectionalLight(0xffffff, 0.5); - * scene.add(directionalLight); - * ``` - * @see Example: {@link https://threejs.org/examples/#misc_controls_fly | controls / fly } - * @see Example: {@link https://threejs.org/examples/#webgl_effects_parallaxbarrier | effects / parallaxbarrier } - * @see Example: {@link https://threejs.org/examples/#webgl_effects_stereo | effects / stereo } - * @see Example: {@link https://threejs.org/examples/#webgl_geometry_extrude_splines | geometry / extrude / splines } - * @see Example: {@link https://threejs.org/examples/#webgl_materials_bumpmap | materials / bumpmap } - * @see {@link https://threejs.org/docs/index.html#api/en/lights/DirectionalLight | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/DirectionalLight.js | Source} - */ -export class DirectionalLight extends Light { - /** - * Creates a new {@link DirectionalLight}. - * @param color Hexadecimal color of the light. Default `0xffffff` _(white)_. - * @param intensity Numeric value of the light's strength/intensity. Expects a `Float`. Default `1` - */ - constructor(color?: ColorRepresentation, intensity?: number); - - /** - * Read-only flag to check if a given object is of type {@link DirectionalLight}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isDirectionalLight: true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `DirectionalLight` - */ - override readonly type: string | "DirectionalLight"; - - /** - * Whether the object gets rendered into shadow map. - * @remarks - * If set to `true` light will cast dynamic shadows. - * **Warning**: This is expensive and requires tweaking to get shadows looking right. - * @see {@link THREE.DirectionalLightShadow | DirectionalLightShadow} for details. - * @defaultValue `false` - */ - override castShadow: boolean; - - /** - * This is set equal to {@link THREE.Object3D.DEFAULT_UP}, so that the light shines from the top down. - * @defaultValue {@link Object3D.DEFAULT_UP} _(0, 1, 0)_ - */ - override readonly position: Vector3; - - /** - * A {@link THREE.DirectionalLightShadow | DirectionalLightShadow} used to calculate shadows for this light. - * @defaultValue `new THREE.DirectionalLightShadow()` - */ - shadow: DirectionalLightShadow; - - /** - * The {@link DirectionalLight} points from its {@link DirectionalLight.position | position} to target.position. - * @remarks **Note**: For the target's position to be changed to anything other than the default, - * it must be added to the {@link THREE.Scene | scene} using - * ```typescript - * Scene.add( light.target ); - * ``` - * This is so that the target's {@link THREE.Object3D.matrixWorld | matrixWorld} gets automatically updated each frame. - * - * It is also possible to set the target to be another object in the scene (anything with a {@link THREE.Object3D.position | position} property), - * like so: - * ```typescript - * const targetObject = new THREE.Object3D(); - * scene.add(targetObject); - * light.target = targetObject; - * ``` - * The {@link DirectionalLight} will now track the target object. - * @defaultValue `new THREE.Object3D()` at _(0, 0, 0)_ - */ - target: Object3D; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/lights/DirectionalLightShadow.d.ts b/src-testing/src/lights/DirectionalLightShadow.d.ts deleted file mode 100644 index 805e4fa0b..000000000 --- a/src-testing/src/lights/DirectionalLightShadow.d.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { OrthographicCamera } from "../cameras/OrthographicCamera.js"; -import { LightShadow } from "./LightShadow.js"; - -/** - * This is used internally by {@link DirectionalLight | DirectionalLights} for calculating shadows. - * Unlike the other shadow classes, this uses an {@link THREE.OrthographicCamera | OrthographicCamera} to calculate the shadows, - * rather than a {@link THREE.PerspectiveCamera | PerspectiveCamera} - * @remarks - * This is because light rays from a {@link THREE.DirectionalLight | DirectionalLight} are parallel. - * @example - * ```typescript - * //Create a WebGLRenderer and turn on shadows in the renderer - * const renderer = new THREE.WebGLRenderer(); - * renderer.shadowMap.enabled = true; - * renderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap - * //Create a DirectionalLight and turn on shadows for the light - * const light = new THREE.DirectionalLight(0xffffff, 1); - * light.position.set(0, 1, 0); //default; light shining from top - * light.castShadow = true; // default false - * scene.add(light); - * //Set up shadow properties for the light - * light.shadow.mapSize.width = 512; // default - * light.shadow.mapSize.height = 512; // default - * light.shadow.camera.near = 0.5; // default - * light.shadow.camera.far = 500; // default - * //Create a sphere that cast shadows (but does not receive them) - * const sphereGeometry = new THREE.SphereGeometry(5, 32, 32); - * const sphereMaterial = new THREE.MeshStandardMaterial({ - * color: 0xff0000 - * }); - * const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); - * sphere.castShadow = true; //default is false - * sphere.receiveShadow = false; //default - * scene.add(sphere); - * //Create a plane that receives shadows (but does not cast them) - * const planeGeometry = new THREE.PlaneGeometry(20, 20, 32, 32); - * const planeMaterial = new THREE.MeshStandardMaterial({ - * color: 0x00ff00 - * }) - * const plane = new THREE.Mesh(planeGeometry, planeMaterial); - * plane.receiveShadow = true; - * scene.add(plane); - * //Create a helper for the shadow camera (optional) - * const helper = new THREE.CameraHelper(light.shadow.camera); - * scene.add(helper); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/lights/shadows/DirectionalLightShadow | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/DirectionalLightShadow.js | Source} - */ -export class DirectionalLightShadow extends LightShadow { - /** - * Create a new instance of {@link DirectionalLightShadow} - */ - constructor(); - - /** - * Read-only flag to check if a given object is of type {@link DirectionalLightShadow}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isDirectionalLightShadow: true; - - /** - * The light's view of the world. - * @remarks This is used to generate a depth map of the scene; objects behind other objects from the light's perspective will be in shadow. - * @defaultValue is an {@link THREE.OrthographicCamera | OrthographicCamera} with - * {@link OrthographicCamera.left | left} and {@link OrthographicCamera.bottom | bottom} set to -5, - * {@link OrthographicCamera.right | right} and {@link OrthographicCamera.top | top} set to 5, - * the {@link OrthographicCamera.near | near} clipping plane at 0.5 and - * the {@link OrthographicCamera.far | far} clipping plane at 500. - */ - camera: OrthographicCamera; -} diff --git a/src-testing/src/lights/HemisphereLight.d.ts b/src-testing/src/lights/HemisphereLight.d.ts deleted file mode 100644 index 1a796dfc8..000000000 --- a/src-testing/src/lights/HemisphereLight.d.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { Color, ColorRepresentation } from "../math/Color.js"; -import { Vector3 } from "../math/Vector3.js"; -import { Light } from "./Light.js"; - -/** - * A light source positioned directly above the scene, with color fading from the sky color to the ground color. - * @remarks This light cannot be used to cast shadows. - * @example - * ```typescript - * const light = new THREE.HemisphereLight(0xffffbb, 0x080820, 1); - * scene.add(light); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_animation_skinning_blending | animation / skinning / blending } - * @see Example: {@link https://threejs.org/examples/#webgl_lights_hemisphere | lights / hemisphere } - * @see Example: {@link https://threejs.org/examples/#misc_controls_pointerlock | controls / pointerlock } - * @see Example: {@link https://threejs.org/examples/#webgl_loader_collada_kinematics | loader / collada / kinematics } - * @see Example: {@link https://threejs.org/examples/#webgl_loader_stl | loader / stl } - * @see {@link https://threejs.org/docs/index.html#api/en/lights/HemisphereLight | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/HemisphereLight.js | Source} - */ -export class HemisphereLight extends Light { - /** - * Creates a new {@link HemisphereLight}. - * @param skyColor Hexadecimal color of the sky. Expects a `Integer`. Default `0xffffff` _(white)_. - * @param groundColor Hexadecimal color of the ground. Expects a `Integer`. Default `0xffffff` _(white)_. - * @param intensity Numeric value of the light's strength/intensity. Expects a `Float`. Default `1`. - */ - constructor(skyColor?: ColorRepresentation, groundColor?: ColorRepresentation, intensity?: number); - - /** - * Read-only flag to check if a given object is of type {@link HemisphereLight}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isHemisphereLight: true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `HemisphereLight` - */ - override readonly type: string | "HemisphereLight"; - - /** - * This is set equal to {@link THREE.Object3D.DEFAULT_UP}, so that the light shines from the top down. - * @defaultValue {@link Object3D.DEFAULT_UP} _(0, 1, 0)_ - */ - override readonly position: Vector3; - - /** - * The light's sky color, as passed in the constructor. - * @defaultValue `new THREE.Color()` set to white _(0xffffff)_. - */ - override color: Color; - - /** - * The light's ground color, as passed in the constructor. - * @defaultValue `new THREE.Color()` set to white _(0xffffff)_. - */ - groundColor: Color; -} diff --git a/src-testing/src/lights/Light.d.ts b/src-testing/src/lights/Light.d.ts deleted file mode 100644 index 3ae757e3b..000000000 --- a/src-testing/src/lights/Light.d.ts +++ /dev/null @@ -1,82 +0,0 @@ -import { JSONMeta, Object3D, Object3DJSON } from "../core/Object3D.js"; -import { Color, ColorRepresentation } from "../math/Color.js"; -import { LightShadow, LightShadowJSON } from "./LightShadow.js"; - -export interface LightJSON extends Object3DJSON { - color: number; - intensity: number; - - groundColor?: number; - - distance?: number; - angle?: number; - decay?: number; - penumbra?: number; - - shadow?: LightShadowJSON; - target?: string; -} - -/** - * Abstract base class for lights. - * @remarks All other light types inherit the properties and methods described here. - */ -export abstract class Light extends Object3D { - /** - * Creates a new {@link Light} - * @remarks - * **Note** that this is not intended to be called directly (use one of derived classes instead). - * @param color Hexadecimal color of the light. Default `0xffffff` _(white)_. - * @param intensity Numeric value of the light's strength/intensity. Expects a `Float`. Default `1`. - */ - constructor(color?: ColorRepresentation, intensity?: number); - - /** - * Read-only flag to check if a given object is of type {@link HemisphereLight}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isLight: true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `Light` - */ - override readonly type: string | "Light"; - - /** - * Color of the light. \ - * @defaultValue `new THREE.Color(0xffffff)` _(white)_. - */ - color: Color; - - /** - * The light's intensity, or strength. - * The units of intensity depend on the type of light. - * @defaultValue `1` - */ - intensity: number; - - /** - * A {@link THREE.LightShadow | LightShadow} used to calculate shadows for this light. - * @remarks Available only on Light's that support shadows. - */ - shadow: TShadowSupport; - - /** - * Copies value of all the properties from the {@link Light | source} to this instance. - * @param source - * @param recursive - */ - copy(source: this, recursive?: boolean): this; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; - - toJSON(meta?: JSONMeta): LightJSON; -} diff --git a/src-testing/src/lights/LightProbe.d.ts b/src-testing/src/lights/LightProbe.d.ts deleted file mode 100644 index a63ffdc57..000000000 --- a/src-testing/src/lights/LightProbe.d.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { SphericalHarmonics3 } from "../math/SphericalHarmonics3.js"; -import { Light } from "./Light.js"; - -/** - * Light probes are an alternative way of adding light to a 3D scene. - * @remarks - * Unlike classical light sources (e.g - * directional, point or spot lights), light probes do not emit light - * Instead they store information about light passing through 3D space - * During rendering, the light that hits a 3D object is approximated by using the data from the light probe. - * Light probes are usually created from (radiance) environment maps - * The class {@link THREE.LightProbeGenerator | LightProbeGenerator} can be used to create light probes from - * instances of {@link THREE.CubeTexture | CubeTexture} or {@link THREE.WebGLCubeRenderTarget | WebGLCubeRenderTarget} - * However, light estimation data could also be provided in other forms e.g - * by WebXR - * This enables the rendering of augmented reality content that reacts to real world lighting. - * The current probe implementation in three.js supports so-called diffuse light probes - * This type of light probe is functionally equivalent to an irradiance environment map. - * @see Example: {@link https://threejs.org/examples/#webgl_lightprobe | WebGL / light probe } - * @see Example: {@link https://threejs.org/examples/#webgl_lightprobe_cubecamera | WebGL / light probe / cube camera } - * @see {@link https://threejs.org/docs/index.html#api/en/lights/LightProbe | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/LightProbe.js | Source} - */ -export class LightProbe extends Light { - /** - * Creates a new LightProbe. - * @param sh An instance of {@link THREE.SphericalHarmonics3 | SphericalHarmonics3}. Default `new THREE.SphericalHarmonics3()``. - * @param intensity Numeric value of the light probe's intensity. Expects a `Float`. Default `1`. - */ - constructor(sh?: SphericalHarmonics3, intensity?: number); - - /** - * Read-only flag to check if a given object is of type {@link DirectionalLight}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isLightProbe: true; - - /** - * A light probe uses spherical harmonics to encode lighting information. - * @defaultValue `new THREE.SphericalHarmonics3()` - */ - sh: SphericalHarmonics3; - - /** @internal */ - fromJSON(json: {}): LightProbe; -} diff --git a/src-testing/src/lights/LightShadow.d.ts b/src-testing/src/lights/LightShadow.d.ts deleted file mode 100644 index 53c163c3e..000000000 --- a/src-testing/src/lights/LightShadow.d.ts +++ /dev/null @@ -1,169 +0,0 @@ -import { Camera } from "../cameras/Camera.js"; -import { Object3DJSONObject } from "../core/Object3D.js"; -import { Frustum } from "../math/Frustum.js"; -import { Matrix4 } from "../math/Matrix4.js"; -import { Vector2, Vector2Tuple } from "../math/Vector2.js"; -import { Vector4 } from "../math/Vector4.js"; -import { WebGLRenderTarget } from "../renderers/WebGLRenderTarget.js"; -import { Light } from "./Light.js"; - -export interface LightShadowJSON { - intensity?: number; - bias?: number; - normalBias?: number; - radius?: number; - mapSize?: Vector2Tuple; - - camera: Omit; -} - -/** - * Serves as a base class for the other shadow classes. - * @see {@link https://threejs.org/docs/index.html#api/en/lights/shadows/LightShadow | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/LightShadow.js | Source} - */ -export class LightShadow { - /** - * Create a new instance of {@link LightShadow} - * @param camera The light's view of the world. - */ - constructor(camera: TCamera); - - /** - * The light's view of the world. - * @remark This is used to generate a depth map of the scene; objects behind other objects from the light's perspective will be in shadow. - */ - camera: TCamera; - - /** - * The intensity of the shadow. The default is `1`. Valid values are in the range `[0, 1]`. - */ - intensity: number; - - /** - * Shadow map bias, how much to add or subtract from the normalized depth when deciding whether a surface is in shadow. - * @remark The Very tiny adjustments here (in the order of 0.0001) may help reduce artifacts in shadows. - * @remarks Expects a `Float` - * @defaultValue `0` - */ - bias: number; - - /** - * Defines how much the position used to query the shadow map is offset along the object normal. - * @remark The Increasing this value can be used to reduce shadow acne especially in large scenes where light shines onto geometry at a shallow angle. - * @remark The cost is that shadows may appear distorted. - * @remarks Expects a `Float` - * @defaultValue `0` - */ - normalBias: number; - - /** - * Setting this to values greater than 1 will blur the edges of the shadow.toi - * @remark High values will cause unwanted banding effects in the shadows - a greater {@link LightShadow.mapSize | mapSize - * will allow for a higher value to be used here before these effects become visible. - * @remark If {@link THREE.WebGLRenderer.shadowMap.type | WebGLRenderer.shadowMap.type} is set to {@link Renderer | PCFSoftShadowMap}, - * radius has no effect and it is recommended to increase softness by decreasing {@link LightShadow.mapSize | mapSize} instead. - * @remark Note that this has no effect if the {@link THREE.WebGLRenderer.shadowMap | WebGLRenderer.shadowMap}.{@link THREE.WebGLShadowMap.type | type} - * is set to {@link THREE.BasicShadowMap | BasicShadowMap}. - * @remarks Expects a `Float` - * @defaultValue `1` - */ - radius: number; - - /** - * The amount of samples to use when blurring a VSM shadow map. - * @remarks Expects a `Integer` - * @defaultValue `8` - */ - blurSamples: number; - - /** - * A {@link THREE.Vector2 | Vector2} defining the width and height of the shadow map. - * @remarks Higher values give better quality shadows at the cost of computation time. - * @remarks Values must be powers of 2, up to the {@link THREE.WebGLRenderer.capabilities | WebGLRenderer.capabilities}.maxTextureSize for a given device, - * although the width and height don't have to be the same (so, for example, (512, 1024) is valid). - * @defaultValue `new THREE.Vector2(512, 512)` - */ - mapSize: Vector2; - - /** - * The depth map generated using the internal camera; a location beyond a pixel's depth is in shadow. Computed internally during rendering. - * @defaultValue null - */ - map: WebGLRenderTarget | null; - - /** - * The distribution map generated using the internal camera; an occlusion is calculated based on the distribution of depths. Computed internally during rendering. - * @defaultValue null - */ - mapPass: WebGLRenderTarget | null; - - /** - * Model to shadow camera space, to compute location and depth in shadow map. - * Stored in a {@link Matrix4 | Matrix4}. - * @remarks This is computed internally during rendering. - * @defaultValue new THREE.Matrix4() - */ - matrix: Matrix4; - - /** - * Enables automatic updates of the light's shadow. If you do not require dynamic lighting / shadows, you may set this to `false`. - * @defaultValue `true` - */ - autoUpdate: boolean; - - /** - * When set to `true`, shadow maps will be updated in the next `render` call. - * If you have set {@link autoUpdate} to `false`, you will need to set this property to `true` and then make a render call to update the light's shadow. - * @defaultValue `false` - */ - needsUpdate: boolean; - - /** - * Used internally by the renderer to get the number of viewports that need to be rendered for this shadow. - */ - getViewportCount(): number; - - /** - * Copies value of all the properties from the {@link {@link LightShadow} | source} to this Light. - * @param source - */ - copy(source: LightShadow): this; - - /** - * Creates a new {@link LightShadow} with the same properties as this one. - */ - clone(recursive?: boolean): this; - - /** - * Serialize this LightShadow. - */ - toJSON(): LightShadowJSON; - - /** - * Gets the shadow cameras frustum - * @remarks - * Used internally by the renderer to cull objects. - */ - getFrustum(): Frustum; - - /** - * Update the matrices for the camera and shadow, used internally by the renderer. - * @param light The light for which the shadow is being rendered. - */ - updateMatrices(light: Light): void; - - getViewport(viewportIndex: number): Vector4; - - /** - * Used internally by the renderer to extend the shadow map to contain all viewports - */ - getFrameExtents(): Vector2; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/lights/PointLight.d.ts b/src-testing/src/lights/PointLight.d.ts deleted file mode 100644 index c13044e12..000000000 --- a/src-testing/src/lights/PointLight.d.ts +++ /dev/null @@ -1,102 +0,0 @@ -import { ColorRepresentation } from "../math/Color.js"; -import { Light } from "./Light.js"; -import { PointLightShadow } from "./PointLightShadow.js"; - -/** - * A light that gets emitted from a single point in all directions - * @remarks - * A common use case for this is to replicate the light emitted from a bare lightbulb. - * @example - * ```typescript - * const light = new THREE.PointLight(0xff0000, 1, 100); - * light.position.set(50, 50, 50); - * scene.add(light); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_lights_pointlights | lights / pointlights } - * @see Example: {@link https://threejs.org/examples/#webgl_effects_anaglyph | effects / anaglyph } - * @see Example: {@link https://threejs.org/examples/#webgl_geometry_text | geometry / text } - * @see Example: {@link https://threejs.org/examples/#webgl_lensflares | lensflares } - * @see {@link https://threejs.org/docs/index.html#api/en/lights/PointLight | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/PointLight.js | Source} - */ -export class PointLight extends Light { - /** - * Creates a new PointLight. - * @param color Hexadecimal color of the light. Default is 0xffffff (white). Expects a `Integer` - * @param intensity Numeric value of the light's strength/intensity. Expects a `Float`. Default `1` - * @param distance Maximum range of the light. Default is 0 (no limit). - * @param decay The amount the light dims along the distance of the light. Expects a `Float`. Default `2` - */ - constructor(color?: ColorRepresentation, intensity?: number, distance?: number, decay?: number); - - /** - * Read-only flag to check if a given object is of type {@link PointLight}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isPointLight: true; - - /** - * @default 'PointLight' - */ - type: string; - - /** - * The light's intensity. - * - * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — intensity is the luminous intensity of the light measured in candela (cd). - * @remarks Changing the intensity will also change the light's power. - * @remarks Expects a `Float` - * @defaultValue `1` - */ - intensity: number; - - /** - * When **Default mode** — When distance is zero, light does not attenuate. When distance is non-zero, - * light will attenuate linearly from maximum intensity at the light's position down to zero at this distance from the light. - * - * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — When distance is zero, - * light will attenuate according to inverse-square law to infinite distance. - * When distance is non-zero, light will attenuate according to inverse-square law until near the distance cutoff, - * where it will then attenuate quickly and smoothly to 0. Inherently, cutoffs are not physically correct. - * - * @defaultValue `0.0` - * @remarks Expects a `Float` - */ - distance: number; - - /** - * If set to `true` light will cast dynamic shadows. - * **Warning**: This is expensive and requires tweaking to get shadows looking right. - * @see {@link THREE.PointLightShadow | PointLightShadow} for details. - * @defaultValue `false` - */ - castShadow: boolean; - - /** - * The amount the light dims along the distance of the light. - * In context of physically-correct rendering the default value should not be changed. - * @remarks Expects a `Float` - * @defaultValue `2` - */ - decay: number; - - /** - * A {@link THREE.PointLightShadow | PointLightShadow} used to calculate shadows for this light. - * The lightShadow's {@link LightShadow.camera | camera} is set to - * a {@link THREE.PerspectiveCamera | PerspectiveCamera} with {@link PerspectiveCamera.fov | fov} of 90, - * {@link PerspectiveCamera.aspect | aspect} of 1, - * {@link PerspectiveCamera.near | near} clipping plane at 0.5 - * and {@link PerspectiveCamera.far | far} clipping plane at 500. - * @defaultValue new THREE.PointLightShadow() - */ - shadow: PointLightShadow; - - /** - * The light's power. - * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — power is the luminous power of the light measured in lumens (lm). - * @remarks Changing the power will also change the light's intensity. - * @remarks Expects a `Float` - */ - power: number; -} diff --git a/src-testing/src/lights/PointLightShadow.d.ts b/src-testing/src/lights/PointLightShadow.d.ts deleted file mode 100644 index 1d0e7e4af..000000000 --- a/src-testing/src/lights/PointLightShadow.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { PerspectiveCamera } from "../cameras/PerspectiveCamera.js"; -import { Light } from "./Light.js"; -import { LightShadow } from "./LightShadow.js"; - -/** - * Shadow for {@link THREE.PointLight | PointLight} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/PointLightShadow.js | Source} - */ -export class PointLightShadow extends LightShadow { - /** - * Read-only flag to check if a given object is of type {@link PointLightShadow}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isPointLightShadow = true; - - /** - * Update the matrices for the camera and shadow, used internally by the renderer. - * @param light The light for which the shadow is being rendered. - */ - override updateMatrices(light: Light, viewportIndex?: number): void; -} diff --git a/src-testing/src/lights/RectAreaLight.d.ts b/src-testing/src/lights/RectAreaLight.d.ts deleted file mode 100644 index 2861e9794..000000000 --- a/src-testing/src/lights/RectAreaLight.d.ts +++ /dev/null @@ -1,82 +0,0 @@ -import { ColorRepresentation } from "../math/Color.js"; -import { Light } from "./Light.js"; - -/** - * {@link RectAreaLight} emits light uniformly across the face a rectangular plane - * @remarks - * This light type can be used to simulate light sources such as bright windows or strip lighting. - * Important Notes: - * - There is no shadow support. - * - Only {@link MeshStandardMaterial | MeshStandardMaterial} and {@link MeshPhysicalMaterial | MeshPhysicalMaterial} are supported. - * - You have to include {@link https://threejs.org/examples/jsm/lights/RectAreaLightUniformsLib.js | RectAreaLightUniformsLib} into your scene and call `init()`. - * @example - * ```typescript - * const width = 10; - * const height = 10; - * const intensity = 1; - * const rectLight = new THREE.RectAreaLight(0xffffff, intensity, width, height); - * rectLight.position.set(5, 5, 0); - * rectLight.lookAt(0, 0, 0); - * scene.add(rectLight) - * const rectLightHelper = new RectAreaLightHelper(rectLight); - * rectLight.add(rectLightHelper); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_lights_rectarealight | WebGL / {@link RectAreaLight} } - * @see {@link https://threejs.org/docs/index.html#api/en/lights/RectAreaLight | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/RectAreaLight.js | Source} - */ -export class RectAreaLight extends Light { - /** - * Creates a new {@link RectAreaLight}. - * @param color Hexadecimal color of the light. Default `0xffffff` _(white)_. - * @param intensity The light's intensity, or brightness. Expects a `Float`. Default `1` - * @param width Width of the light. Expects a `Float`. Default `10` - * @param height Height of the light. Expects a `Float`. Default `10` - */ - constructor(color?: ColorRepresentation, intensity?: number, width?: number, height?: number); - - /** - * Read-only flag to check if a given object is of type {@link RectAreaLight}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isRectAreaLight: true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `RectAreaLight` - */ - override readonly type: string | "RectAreaLight"; - - /** - * The width of the light. - * @remarks Expects a `Float` - * @defaultValue `10` - */ - width: number; - - /** - * The height of the light. - * @remarks Expects a `Float` - * @defaultValue `10` - */ - height: number; - - /** - * The light's intensity. - * @remarks Changing the intensity will also change the light's power. - * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — intensity is the luminance (brightness) of the light measured in nits (cd/m^2). - * @remarks Expects a `Float` - * @defaultValue `1` - */ - intensity: number; - - /** - * The light's power. - * @remarks Changing the power will also change the light's intensity. - * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — power is the luminous power of the light measured in lumens (lm). - * @remarks Expects a `Float` - */ - power: number; -} diff --git a/src-testing/src/lights/SpotLight.d.ts b/src-testing/src/lights/SpotLight.d.ts deleted file mode 100644 index 7f42488a8..000000000 --- a/src-testing/src/lights/SpotLight.d.ts +++ /dev/null @@ -1,164 +0,0 @@ -import { Object3D } from "../core/Object3D.js"; -import { ColorRepresentation } from "../math/Color.js"; -import { Vector3 } from "../math/Vector3.js"; -import { Texture } from "../textures/Texture.js"; -import { Light } from "./Light.js"; -import { SpotLightShadow } from "./SpotLightShadow.js"; - -/** - * This light gets emitted from a single point in one direction, along a cone that increases in size the further from the light it gets. - * @example - * ```typescript - * // white {@link SpotLight} shining from the side, modulated by a texture, casting a shadow - * const {@link SpotLight} = new THREE.SpotLight(0xffffff); - * spotLight.position.set(100, 1000, 100); - * spotLight.map = new THREE.TextureLoader().load(url); - * spotLight.castShadow = true; - * spotLight.shadow.mapSize.width = 1024; - * spotLight.shadow.mapSize.height = 1024; - * spotLight.shadow.camera.near = 500; - * spotLight.shadow.camera.far = 4000; - * spotLight.shadow.camera.fov = 30; - * scene.add(spotLight); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_lights_spotlight | lights / {@link SpotLight} } - * @see Example: {@link https://threejs.org/examples/#webgl_lights_spotlights | lights / spotlights } - * @see {@link https://threejs.org/docs/index.html#api/en/lights/SpotLight | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/SpotLight.js | Source} - */ -export class SpotLight extends Light { - /** - * Creates a new SpotLight. - * @param color Hexadecimal color of the light. Default `0xffffff` _(white)_. - * @param intensity Numeric value of the light's strength/intensity. Expects a `Float`. Default `1`. - * @param distance Maximum range of the light. Default is 0 (no limit). Expects a `Float`. - * @param angle Maximum angle of light dispersion from its direction whose upper bound is Math.PI/2. - * @param penumbra Percent of the {@link SpotLight} cone that is attenuated due to penumbra. Takes values between zero and 1. Expects a `Float`. Default `0`. - * @param decay The amount the light dims along the distance of the light. Expects a `Float`. Default `2`. - */ - constructor( - color?: ColorRepresentation, - intensity?: number, - distance?: number, - angle?: number, - penumbra?: number, - decay?: number, - ); - - /** - * Read-only flag to check if a given object is of type {@link SpotLight}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isSpotLight: true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @defaultValue `SpotLight` - */ - override readonly type: string | "SpotLight"; - - /** - * This is set equal to {@link THREE.Object3D.DEFAULT_UP | Object3D.DEFAULT_UP} (0, 1, 0), so that the light shines from the top down. - * @defaultValue `{@link Object3D.DEFAULT_UP}` - */ - readonly position: Vector3; - - /** - * The {@link SpotLight} points from its {@link SpotLight.position | position} to target.position. - * @remarks - * **Note**: For the target's position to be changed to anything other than the default, - * it must be added to the {@link Scene | scene} using - * - * ```typescript - * scene.add( light.target ); - * ``` - * - * This is so that the target's {@link Object3D.matrixWorld | matrixWorld} gets automatically updated each frame. - * It is also possible to set the target to be another object in the scene (anything with a {@link THREE.Object3D.position | position} property), like so: - * ```typescript - * const targetObject = new THREE.Object3D(); - * scene.add(targetObject); - * light.target = targetObject; - * ``` - * The {@link SpotLight} will now track the target object. - * @defaultValue `new THREE.Object3D()` _The default position of the target is *(0, 0, 0)*._ - */ - target: Object3D; - - /** - * If set to `true` light will cast dynamic shadows. - * @remarks **Warning**: This is expensive and requires tweaking to get shadows looking right. the {@link THREE.SpotLightShadow | SpotLightShadow} for details. - * @defaultValue `false` - */ - override castShadow: boolean; - - /** - * The light's intensity. - * @remarks Changing the intensity will also change the light's power. - * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — intensity is the luminous intensity of the light measured in candela (cd). - * @remarks Expects a `Float` - * @defaultValue `1` - */ - intensity: number; - - /** - * When **Default mode** — When distance is zero, light does not attenuate. When distance is non-zero, - * light will attenuate linearly from maximum intensity at the light's position down to zero at this distance from the light. - * - * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — When distance is zero, - * light will attenuate according to inverse-square law to infinite distance. - * When distance is non-zero, light will attenuate according to inverse-square law until near the distance cutoff, - * where it will then attenuate quickly and smoothly to `0`. Inherently, cutoffs are not physically correct. - * @remarks Expects a `Float` - * @defaultValue `0.0` - */ - distance: number; - - /** - * Maximum extent of the spotlight, in radians, from its direction. - * @remarks Should be no more than `Math.PI/2`. - * @remarks Expects a `Float` - * @defaultValue `Math.PI / 3` - */ - angle: number; - - /** - * The amount the light dims along the distance of the light. - * In context of physically-correct rendering the default value should not be changed. - * @remarks Expects a `Float` - * @defaultValue `2` - */ - decay: number; - - /** - * A {@link THREE.SpotLightShadow | SpotLightShadow} used to calculate shadows for this light. - * @defaultValue `new THREE.SpotLightShadow()` - */ - shadow: SpotLightShadow; - - /** - * The light's power. - * @remarks Changing the power will also change the light's intensity. - * When **{@link WebGLRenderer.useLegacyLights | legacy lighting mode} is disabled** — power is the luminous power of the light measured in lumens (lm). - * @remarks Expects a `Float` - */ - power: number; - - /** - * Percent of the {@link SpotLight} cone that is attenuated due to penumbra. - * @remarks Takes values between zero and 1. - * @remarks Expects a `Float` - * @defaultValue `0.0` - */ - penumbra: number; - - /** - * A {@link THREE.Texture | Texture} used to modulate the color of the light. - * The spot light color is mixed with the _RGB_ value of this texture, with a ratio corresponding to its alpha value. - * The cookie-like masking effect is reproduced using pixel values (0, 0, 0, 1-cookie_value). - * @remarks **Warning**: {@link SpotLight.map} is disabled if {@link SpotLight.castShadow} is `false`. - */ - map: Texture | null; -} diff --git a/src-testing/src/lights/SpotLightShadow.d.ts b/src-testing/src/lights/SpotLightShadow.d.ts deleted file mode 100644 index 77f075c44..000000000 --- a/src-testing/src/lights/SpotLightShadow.d.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { PerspectiveCamera } from "../cameras/PerspectiveCamera.js"; -import { LightShadow } from "./LightShadow.js"; - -/** - * This is used internally by {@link SpotLight | SpotLights} for calculating shadows. - * @example - * ```typescript - * //Create a WebGLRenderer and turn on shadows in the renderer - * const renderer = new THREE.WebGLRenderer(); - * renderer.shadowMap.enabled = true; - * renderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap - * //Create a SpotLight and turn on shadows for the light - * const light = new THREE.SpotLight(0xffffff); - * light.castShadow = true; // default false - * scene.add(light); - * //Set up shadow properties for the light - * light.shadow.mapSize.width = 512; // default - * light.shadow.mapSize.height = 512; // default - * light.shadow.camera.near = 0.5; // default - * light.shadow.camera.far = 500; // default - * light.shadow.focus = 1; // default - * //Create a sphere that cast shadows (but does not receive them) - * const sphereGeometry = new THREE.SphereGeometry(5, 32, 32); - * const sphereMaterial = new THREE.MeshStandardMaterial({ - * color: 0xff0000 - * }); - * const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); - * sphere.castShadow = true; //default is false - * sphere.receiveShadow = false; //default - * scene.add(sphere); - * //Create a plane that receives shadows (but does not cast them) - * const planeGeometry = new THREE.PlaneGeometry(20, 20, 32, 32); - * const planeMaterial = new THREE.MeshStandardMaterial({ - * color: 0x00ff00 - * }) - * const plane = new THREE.Mesh(planeGeometry, planeMaterial); - * plane.receiveShadow = true; - * scene.add(plane); - * //Create a helper for the shadow camera (optional) - * const helper = new THREE.CameraHelper(light.shadow.camera); - * scene.add(helper); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/lights/shadows/SpotLightShadow | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/SpotLightShadow.js | Source} - */ -export class SpotLightShadow extends LightShadow { - /** - * Read-only flag to check if a given object is of type {@link SpotLightShadow}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isSpotLightShadow: true; - - /** - * The light's view of the world. - * @remarks This is used to generate a depth map of the scene; objects behind other objects from the light's perspective will be in shadow. - * @remarks - * The {@link THREE.PerspectiveCamera.fov | fov} will track the {@link THREE.SpotLight.angle | angle} property - * of the owning {@link SpotLight | SpotLight} via the {@link SpotLightShadow.update | update} method. - * Similarly, the {@link THREE.PerspectiveCamera.aspect | aspect} property will track the aspect of the {@link LightShadow.mapSize | mapSize}. - * If the {@link SpotLight.distance | distance} property of the light is set, the {@link THREE.PerspectiveCamera.far | far} clipping plane will track that, otherwise it defaults to `500`. - * @defaultValue is a {@link THREE.PerspectiveCamera | PerspectiveCamera} with {@link THREE.PerspectiveCamera.near | near} clipping plane at `0.5`. - */ - camera: PerspectiveCamera; - - /** - * Used to focus the shadow camera. - * @remarks The camera's field of view is set as a percentage of the spotlight's field-of-view. Range is `[0, 1]`. 0`. - * @defaultValue `1` - */ - focus: number; -} diff --git a/src-testing/src/lights/webgpu/IESSpotLight.d.ts b/src-testing/src/lights/webgpu/IESSpotLight.d.ts deleted file mode 100644 index bf1b66006..000000000 --- a/src-testing/src/lights/webgpu/IESSpotLight.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Texture } from "../../textures/Texture.js"; -import { SpotLight } from "../SpotLight.js"; - -export default class IESSpotLight extends SpotLight { - iesMap: Texture | null; -} diff --git a/src-testing/src/loaders/AnimationLoader.d.ts b/src-testing/src/loaders/AnimationLoader.d.ts deleted file mode 100644 index 567f30f30..000000000 --- a/src-testing/src/loaders/AnimationLoader.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { AnimationClip } from "../animation/AnimationClip.js"; -import { Loader } from "./Loader.js"; -import { LoadingManager } from "./LoadingManager.js"; - -export class AnimationLoader extends Loader { - constructor(manager?: LoadingManager); - - parse(json: readonly unknown[]): AnimationClip[]; -} diff --git a/src-testing/src/loaders/AudioLoader.d.ts b/src-testing/src/loaders/AudioLoader.d.ts deleted file mode 100644 index 0204bef47..000000000 --- a/src-testing/src/loaders/AudioLoader.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Loader } from "./Loader.js"; -import { LoadingManager } from "./LoadingManager.js"; - -export class AudioLoader extends Loader { - constructor(manager?: LoadingManager); -} diff --git a/src-testing/src/loaders/BufferGeometryLoader.d.ts b/src-testing/src/loaders/BufferGeometryLoader.d.ts deleted file mode 100644 index 0aa994011..000000000 --- a/src-testing/src/loaders/BufferGeometryLoader.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { InstancedBufferGeometry } from "../core/InstancedBufferGeometry.js"; -import { Loader } from "./Loader.js"; -import { LoadingManager } from "./LoadingManager.js"; - -export class BufferGeometryLoader extends Loader { - constructor(manager?: LoadingManager); - - parse(json: unknown): InstancedBufferGeometry | BufferGeometry; -} diff --git a/src-testing/src/loaders/Cache.d.ts b/src-testing/src/loaders/Cache.d.ts deleted file mode 100644 index 0742af8f5..000000000 --- a/src-testing/src/loaders/Cache.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -declare const Cache: { - /** - * @default false - */ - enabled: boolean; - - /** - * @default {} - */ - files: any; - - add(key: string, file: any): void; - - get(key: string): any; - - remove(key: string): void; - - clear(): void; -}; - -export { Cache }; diff --git a/src-testing/src/loaders/CompressedTextureLoader.d.ts b/src-testing/src/loaders/CompressedTextureLoader.d.ts deleted file mode 100644 index eeca01ded..000000000 --- a/src-testing/src/loaders/CompressedTextureLoader.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { CompressedTexture } from "../textures/CompressedTexture.js"; -import { Loader } from "./Loader.js"; -import { LoadingManager } from "./LoadingManager.js"; - -export class CompressedTextureLoader extends Loader { - constructor(manager?: LoadingManager); - - load( - url: string, - onLoad?: (data: CompressedTexture) => void, - onProgress?: (event: ProgressEvent) => void, - onError?: (err: unknown) => void, - ): CompressedTexture; -} diff --git a/src-testing/src/loaders/CubeTextureLoader.d.ts b/src-testing/src/loaders/CubeTextureLoader.d.ts deleted file mode 100644 index f6cd285c4..000000000 --- a/src-testing/src/loaders/CubeTextureLoader.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { CubeTexture } from "../textures/CubeTexture.js"; -import { Loader } from "./Loader.js"; -import { LoadingManager } from "./LoadingManager.js"; - -export class CubeTextureLoader extends Loader { - constructor(manager?: LoadingManager); - - load( - url: readonly string[], - onLoad?: (data: CubeTexture) => void, - onProgress?: (event: ProgressEvent) => void, - onError?: (err: unknown) => void, - ): CubeTexture; -} diff --git a/src-testing/src/loaders/DataTextureLoader.d.ts b/src-testing/src/loaders/DataTextureLoader.d.ts deleted file mode 100644 index 0cc8d7475..000000000 --- a/src-testing/src/loaders/DataTextureLoader.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { DataTexture } from "../textures/DataTexture.js"; -import { Loader } from "./Loader.js"; -import { LoadingManager } from "./LoadingManager.js"; - -export class DataTextureLoader extends Loader { - constructor(manager?: LoadingManager); - - load( - url: string, - onLoad?: (data: DataTexture, texData: object) => void, - onProgress?: (event: ProgressEvent) => void, - onError?: (err: unknown) => void, - ): DataTexture; -} diff --git a/src-testing/src/loaders/FileLoader.d.ts b/src-testing/src/loaders/FileLoader.d.ts deleted file mode 100644 index a07ca5152..000000000 --- a/src-testing/src/loaders/FileLoader.d.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { Loader } from "./Loader.js"; -import { LoadingManager } from "./LoadingManager.js"; - -export class FileLoader extends Loader { - constructor(manager?: LoadingManager); - - load( - url: string, - onLoad?: (data: string | ArrayBuffer) => void, - onProgress?: (event: ProgressEvent) => void, - onError?: (err: unknown) => void, - ): void; - - mimeType: undefined | MimeType; - responseType: undefined | string; - - setMimeType(mimeType: MimeType): FileLoader; - setResponseType(responseType: string): FileLoader; -} diff --git a/src-testing/src/loaders/ImageBitmapLoader.d.ts b/src-testing/src/loaders/ImageBitmapLoader.d.ts deleted file mode 100644 index f182d326a..000000000 --- a/src-testing/src/loaders/ImageBitmapLoader.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Loader } from "./Loader.js"; -import { LoadingManager } from "./LoadingManager.js"; - -export class ImageBitmapLoader extends Loader { - constructor(manager?: LoadingManager); - - load( - url: string, - onLoad?: (data: ImageBitmap) => void, - onProgress?: (event: ProgressEvent) => void, - onError?: (err: unknown) => void, - ): void; - - /** - * @default { premultiplyAlpha: 'none' } - */ - options: undefined | object; - - readonly isImageBitmapLoader: true; - - setOptions(options: object): ImageBitmapLoader; -} diff --git a/src-testing/src/loaders/ImageLoader.d.ts b/src-testing/src/loaders/ImageLoader.d.ts deleted file mode 100644 index 2189198e4..000000000 --- a/src-testing/src/loaders/ImageLoader.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { Loader } from "./Loader.js"; -import { LoadingManager } from "./LoadingManager.js"; - -/** - * A loader for loading an image. - * Unlike other loaders, this one emits events instead of using predefined callbacks. So if you're interested in getting notified when things happen, you need to add listeners to the object. - */ -export class ImageLoader extends Loader { - constructor(manager?: LoadingManager); - - load( - url: string, - onLoad?: (data: HTMLImageElement) => void, - onProgress?: (event: ProgressEvent) => void, - onError?: (err: unknown) => void, - ): HTMLImageElement; -} diff --git a/src-testing/src/loaders/Loader.d.ts b/src-testing/src/loaders/Loader.d.ts deleted file mode 100644 index 0f65e66f3..000000000 --- a/src-testing/src/loaders/Loader.d.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { LoadingManager } from "./LoadingManager.js"; - -/** - * Base class for implementing loaders. - */ -export class Loader { - constructor(manager?: LoadingManager); - - /** - * @default 'anonymous' - */ - crossOrigin: string; - - /** - * @default false - */ - withCredentials: boolean; - - /** - * @default '' - */ - path: string; - - /** - * @default '' - */ - resourcePath: string; - manager: LoadingManager; - - /** - * @default {} - */ - requestHeader: { [header: string]: string }; - - load( - url: TUrl, - onLoad: (data: TData) => void, - onProgress?: (event: ProgressEvent) => void, - onError?: (err: unknown) => void, - ): void; - loadAsync(url: TUrl, onProgress?: (event: ProgressEvent) => void): Promise; - - setCrossOrigin(crossOrigin: string): this; - setWithCredentials(value: boolean): this; - setPath(path: string): this; - setResourcePath(resourcePath: string): this; - setRequestHeader(requestHeader: { [header: string]: string }): this; - - static DEFAULT_MATERIAL_NAME: string; -} diff --git a/src-testing/src/loaders/LoaderUtils.d.ts b/src-testing/src/loaders/LoaderUtils.d.ts deleted file mode 100644 index 2f00baeff..000000000 --- a/src-testing/src/loaders/LoaderUtils.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -export class LoaderUtils { - /** - * @deprecated decodeText() has been deprecated with r165 and will be removed with r175. Use TextDecoder instead. - */ - static decodeText(array: BufferSource): string; - - static extractUrlBase(url: string): string; - - static resolveURL(url: string, path: string): string; -} diff --git a/src-testing/src/loaders/LoadingManager.d.ts b/src-testing/src/loaders/LoadingManager.d.ts deleted file mode 100644 index ab5b546cd..000000000 --- a/src-testing/src/loaders/LoadingManager.d.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { Loader } from "./Loader.js"; - -export const DefaultLoadingManager: LoadingManager; - -/** - * Handles and keeps track of loaded and pending data. - */ -export class LoadingManager { - constructor( - onLoad?: () => void, - onProgress?: (url: string, loaded: number, total: number) => void, - onError?: (url: string) => void, - ); - - /** - * Will be called when loading of an item starts. - * @param url The url of the item that started loading. - * @param loaded The number of items already loaded so far. - * @param total The total amount of items to be loaded. - */ - onStart?: ((url: string, loaded: number, total: number) => void) | undefined; - - /** - * Will be called when all items finish loading. - * The default is a function with empty body. - */ - onLoad: () => void; - - /** - * Will be called for each loaded item. - * The default is a function with empty body. - * @param url The url of the item just loaded. - * @param loaded The number of items already loaded so far. - * @param total The total amount of items to be loaded. - */ - onProgress: (url: string, loaded: number, total: number) => void; - - /** - * Will be called when item loading fails. - * The default is a function with empty body. - * @param url The url of the item that errored. - */ - onError: (url: string) => void; - - /** - * If provided, the callback will be passed each resource URL before a request is sent. - * The callback may return the original URL, or a new URL to override loading behavior. - * This behavior can be used to load assets from .ZIP files, drag-and-drop APIs, and Data URIs. - * @param callback URL modifier callback. Called with url argument, and must return resolvedURL. - */ - setURLModifier(callback?: (url: string) => string): this; - - /** - * Given a URL, uses the URL modifier callback (if any) and returns a resolved URL. - * If no URL modifier is set, returns the original URL. - * @param url the url to load - */ - resolveURL(url: string): string; - - itemStart(url: string): void; - itemEnd(url: string): void; - itemError(url: string): void; - - // handlers - - addHandler(regex: RegExp, loader: Loader): this; - removeHandler(regex: RegExp): this; - getHandler(file: string): Loader | null; -} diff --git a/src-testing/src/loaders/MaterialLoader.d.ts b/src-testing/src/loaders/MaterialLoader.d.ts deleted file mode 100644 index 67db71113..000000000 --- a/src-testing/src/loaders/MaterialLoader.d.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { Material } from "../materials/Material.js"; -import { Texture } from "../textures/Texture.js"; -import { Loader } from "./Loader.js"; -import { LoadingManager } from "./LoadingManager.js"; - -export class MaterialLoader extends Loader { - /** - * @default {} - */ - textures: { [key: string]: Texture }; - - constructor(manager?: LoadingManager); - - parse(json: unknown): Material; - - setTextures(textures: { [key: string]: Texture }): this; - - static createMaterialFromType(type: string): Material; -} diff --git a/src-testing/src/loaders/ObjectLoader.d.ts b/src-testing/src/loaders/ObjectLoader.d.ts deleted file mode 100644 index 306c75700..000000000 --- a/src-testing/src/loaders/ObjectLoader.d.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { AnimationClip } from "../animation/AnimationClip.js"; -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { InstancedBufferGeometry } from "../core/InstancedBufferGeometry.js"; -import { Object3D } from "../core/Object3D.js"; -import { Material } from "../materials/Material.js"; -import { Source } from "../textures/Source.js"; -import { Texture } from "../textures/Texture.js"; -import { Loader } from "./Loader.js"; -import { LoadingManager } from "./LoadingManager.js"; - -export class ObjectLoader extends Loader { - constructor(manager?: LoadingManager); - - load( - url: string, - onLoad?: (data: Object3D) => void, - onProgress?: (event: ProgressEvent) => void, - onError?: (err: unknown) => void, - ): void; - - parse(json: unknown, onLoad?: (object: Object3D) => void): Object3D; - parseAsync(json: unknown): Promise; - parseGeometries(json: unknown): { [key: string]: InstancedBufferGeometry | BufferGeometry }; - parseMaterials(json: unknown, textures: { [key: string]: Texture }): { [key: string]: Material }; - parseAnimations(json: unknown): { [key: string]: AnimationClip }; - parseImages(json: unknown, onLoad?: () => void): { [key: string]: Source }; - parseImagesAsync(json: unknown): Promise<{ [key: string]: Source }>; - parseTextures(json: unknown, images: { [key: string]: Source }): { [key: string]: Texture }; - parseObject( - data: unknown, - geometries: { [key: string]: InstancedBufferGeometry | BufferGeometry }, - materials: { [key: string]: Material }, - animations: { [key: string]: AnimationClip }, - ): Object3D; -} diff --git a/src-testing/src/loaders/TextureLoader.d.ts b/src-testing/src/loaders/TextureLoader.d.ts deleted file mode 100644 index 3cc07f5c3..000000000 --- a/src-testing/src/loaders/TextureLoader.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Texture } from "../textures/Texture.js"; -import { Loader } from "./Loader.js"; -import { LoadingManager } from "./LoadingManager.js"; - -/** - * Class for loading a texture. - * Unlike other loaders, this one emits events instead of using predefined callbacks. So if you're interested in getting notified when things happen, you need to add listeners to the object. - */ -export class TextureLoader extends Loader { - constructor(manager?: LoadingManager); - - load( - url: string, - onLoad?: (data: Texture) => void, - onProgress?: (event: ProgressEvent) => void, - onError?: (err: unknown) => void, - ): Texture; -} diff --git a/src-testing/src/materials/LineBasicMaterial.d.ts b/src-testing/src/materials/LineBasicMaterial.d.ts deleted file mode 100644 index 791193eee..000000000 --- a/src-testing/src/materials/LineBasicMaterial.d.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { Color, ColorRepresentation } from "../math/Color.js"; -import { Texture } from "../textures/Texture.js"; -import { Material, MaterialParameters } from "./Material.js"; - -export interface LineBasicMaterialParameters extends MaterialParameters { - color?: ColorRepresentation | undefined; - fog?: boolean | undefined; - linewidth?: number | undefined; - linecap?: string | undefined; - linejoin?: string | undefined; -} - -export class LineBasicMaterial extends Material { - constructor(parameters?: LineBasicMaterialParameters); - - /** - * Read-only flag to check if a given object is of type {@link LineBasicMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isLineBasicMaterial: true; - - /** - * @default 'LineBasicMaterial' - */ - type: string; - - /** - * @default 0xffffff - */ - color: Color; - - /** - * Whether the material is affected by fog. Default is true. - * @default true - */ - fog: boolean; - - /** - * @default 1 - */ - linewidth: number; - - /** - * @default 'round' - */ - linecap: string; - - /** - * @default 'round' - */ - linejoin: string; - - /** - * Sets the color of the lines using data from a {@link Texture}. - */ - map: Texture | null; - - setValues(parameters: LineBasicMaterialParameters): void; -} diff --git a/src-testing/src/materials/LineDashedMaterial.d.ts b/src-testing/src/materials/LineDashedMaterial.d.ts deleted file mode 100644 index fd5ee9ae6..000000000 --- a/src-testing/src/materials/LineDashedMaterial.d.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { LineBasicMaterial, LineBasicMaterialParameters } from "./LineBasicMaterial.js"; - -export interface LineDashedMaterialParameters extends LineBasicMaterialParameters { - scale?: number | undefined; - dashSize?: number | undefined; - gapSize?: number | undefined; -} - -export class LineDashedMaterial extends LineBasicMaterial { - constructor(parameters?: LineDashedMaterialParameters); - - /** - * Read-only flag to check if a given object is of type {@link LineDashedMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isLineDashedMaterial: true; - - /** - * @default 'LineDashedMaterial' - */ - type: string; - - /** - * @default 1 - */ - scale: number; - - /** - * @default 1 - */ - dashSize: number; - - /** - * @default 1 - */ - gapSize: number; - - setValues(parameters: LineDashedMaterialParameters): void; -} diff --git a/src-testing/src/materials/Material.d.ts b/src-testing/src/materials/Material.d.ts deleted file mode 100644 index 9abe3ab50..000000000 --- a/src-testing/src/materials/Material.d.ts +++ /dev/null @@ -1,631 +0,0 @@ -import { Camera } from "../cameras/Camera.js"; -import { - Blending, - BlendingDstFactor, - BlendingEquation, - BlendingSrcFactor, - Combine, - DepthModes, - NormalMapTypes, - PixelFormat, - Side, - StencilFunc, - StencilOp, -} from "../constants.js"; -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { EventDispatcher } from "../core/EventDispatcher.js"; -import { JSONMeta, Object3D } from "../core/Object3D.js"; -import { Color, ColorRepresentation } from "../math/Color.js"; -import { Plane } from "../math/Plane.js"; -import { Group } from "../objects/Group.js"; -import { WebGLProgramParametersWithUniforms } from "../renderers/webgl/WebGLPrograms.js"; -import { WebGLRenderer } from "../renderers/WebGLRenderer.js"; -import { Scene } from "../scenes/Scene.js"; -import { EulerTuple, SourceJSON, TextureJSON, Vector2Tuple } from "../Three.js"; - -export interface MaterialParameters { - alphaHash?: boolean | undefined; - alphaTest?: number | undefined; - alphaToCoverage?: boolean | undefined; - blendAlpha?: number | undefined; - blendColor?: ColorRepresentation | undefined; - blendDst?: BlendingDstFactor | undefined; - blendDstAlpha?: number | undefined; - blendEquation?: BlendingEquation | undefined; - blendEquationAlpha?: number | undefined; - blending?: Blending | undefined; - blendSrc?: BlendingSrcFactor | BlendingDstFactor | undefined; - blendSrcAlpha?: number | undefined; - clipIntersection?: boolean | undefined; - clippingPlanes?: Plane[] | undefined; - clipShadows?: boolean | undefined; - colorWrite?: boolean | undefined; - defines?: any; - depthFunc?: DepthModes | undefined; - depthTest?: boolean | undefined; - depthWrite?: boolean | undefined; - name?: string | undefined; - opacity?: number | undefined; - polygonOffset?: boolean | undefined; - polygonOffsetFactor?: number | undefined; - polygonOffsetUnits?: number | undefined; - precision?: "highp" | "mediump" | "lowp" | null | undefined; - premultipliedAlpha?: boolean | undefined; - forceSinglePass?: boolean | undefined; - dithering?: boolean | undefined; - side?: Side | undefined; - shadowSide?: Side | undefined; - toneMapped?: boolean | undefined; - transparent?: boolean | undefined; - vertexColors?: boolean | undefined; - visible?: boolean | undefined; - format?: PixelFormat | undefined; - stencilWrite?: boolean | undefined; - stencilFunc?: StencilFunc | undefined; - stencilRef?: number | undefined; - stencilWriteMask?: number | undefined; - stencilFuncMask?: number | undefined; - stencilFail?: StencilOp | undefined; - stencilZFail?: StencilOp | undefined; - stencilZPass?: StencilOp | undefined; - userData?: Record | undefined; -} - -export interface MaterialJSON { - metadata: { version: number; type: string; generator: string }; - - uuid: string; - type: string; - - name?: string; - - color?: number; - roughness?: number; - metalness?: number; - - sheen?: number; - sheenColor?: number; - sheenRoughness?: number; - emissive?: number; - emissiveIntensity?: number; - - specular?: number; - specularIntensity?: number; - specularColor?: number; - shininess?: number; - clearcoat?: number; - clearcoatRoughness?: number; - clearcoatMap?: string; - clearcoatRoughnessMap?: string; - clearcoatNormalMap?: string; - clearcoatNormalScale?: Vector2Tuple; - - dispersion?: number; - - iridescence?: number; - iridescenceIOR?: number; - iridescenceThicknessRange?: number; - iridescenceMap?: string; - iridescenceThicknessMap?: string; - - anisotropy?: number; - anisotropyRotation?: number; - anisotropyMap?: string; - - map?: string; - matcap?: string; - alphaMap?: string; - - lightMap?: string; - lightMapIntensity?: number; - - aoMap?: string; - aoMapIntensity?: number; - - bumpMap?: string; - bumpScale?: number; - - normalMap?: string; - normalMapType?: NormalMapTypes; - normalScale?: Vector2Tuple; - - displacementMap?: string; - displacementScale?: number; - displacementBias?: number; - - roughnessMap?: string; - metalnessMap?: string; - - emissiveMap?: string; - specularMap?: string; - specularIntensityMap?: string; - specularColorMap?: string; - - envMap?: string; - combine?: Combine; - - envMapRotation?: EulerTuple; - envMapIntensity?: number; - reflectivity?: number; - refractionRatio?: number; - - gradientMap?: string; - - transmission?: number; - transmissionMap?: string; - thickness?: number; - thicknessMap?: string; - attenuationDistance?: number; - attenuationColor?: number; - - size?: number; - shadowSide?: number; - sizeAttenuation?: boolean; - - blending?: Blending; - side?: Side; - vertexColors?: boolean; - - opacity?: number; - transparent?: boolean; - - blendSrc?: BlendingSrcFactor; - blendDst?: BlendingDstFactor; - blendEquation?: BlendingEquation; - blendSrcAlpha?: number | null; - blendDstAlpha?: number | null; - blendEquationAlpha?: number | null; - blendColor?: number; - blendAlpha?: number; - - depthFunc?: DepthModes; - depthTest?: boolean; - depthWrite?: boolean; - colorWrite?: boolean; - - stencilWriteMask?: number; - stencilFunc?: StencilFunc; - stencilRef?: number; - stencilFuncMask?: number; - stencilFail?: StencilOp; - stencilZFail?: StencilOp; - stencilZPass?: StencilOp; - stencilWrite?: boolean; - - rotation?: number; - - polygonOffset?: boolean; - polygonOffsetFactor?: number; - polygonOffsetUnits?: number; - - linewidth?: number; - dashSize?: number; - gapSize?: number; - scale?: number; - - dithering?: boolean; - - alphaTest?: number; - alphaHash?: boolean; - alphaToCoverage?: boolean; - premultipliedAlpha?: boolean; - forceSinglePass?: boolean; - - wireframe?: boolean; - wireframeLinewidth?: number; - wireframeLinecap?: string; - wireframeLinejoin?: string; - - flatShading?: boolean; - - visible?: boolean; - - toneMapped?: boolean; - - fog?: boolean; - - userData?: Record; - - textures?: Array>; - images?: SourceJSON[]; -} - -/** - * Materials describe the appearance of objects. They are defined in a (mostly) renderer-independent way, so you don't have to rewrite materials if you decide to use a different renderer. - */ -export class Material extends EventDispatcher<{ dispose: {} }> { - constructor(); - - /** - * Read-only flag to check if a given object is of type {@link Material}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isMaterial: true; - - /** - * Enables alpha hashed transparency, an alternative to {@link .transparent} or {@link .alphaTest}. The material - * will not be rendered if opacity is lower than a random threshold. Randomization introduces some grain or noise, - * but approximates alpha blending without the associated problems of sorting. Using TAARenderPass can reduce the - * resulting noise. - */ - alphaHash: boolean; - - /** - * Enables alpha to coverage. Can only be used with MSAA-enabled rendering contexts (meaning when the renderer was - * created with *antialias* parameter set to `true`). Enabling this will smooth aliasing on clip plane edges and - * alphaTest-clipped edges. - * @default false - */ - alphaToCoverage: boolean; - - /** - * Represents the alpha value of the constant blend color. This property has only an effect when using custom - * blending with {@link ConstantAlphaFactor} or {@link OneMinusConstantAlphaFactor}. - * @default 0 - */ - blendAlpha: number; - - /** - * Represent the RGB values of the constant blend color. This property has only an effect when using custom - * blending with {@link ConstantColorFactor} or {@link OneMinusConstantColorFactor}. - * @default 0x000000 - */ - blendColor: Color; - - /** - * Blending destination. It's one of the blending mode constants defined in Three.js. Default is {@link OneMinusSrcAlphaFactor}. - * @default THREE.OneMinusSrcAlphaFactor - */ - blendDst: BlendingDstFactor; - - /** - * The tranparency of the .blendDst. Default is null. - * @default null - */ - blendDstAlpha: number | null; - - /** - * Blending equation to use when applying blending. It's one of the constants defined in Three.js. Default is {@link AddEquation}. - * @default THREE.AddEquation - */ - blendEquation: BlendingEquation; - - /** - * The tranparency of the .blendEquation. Default is null. - * @default null - */ - blendEquationAlpha: number | null; - - /** - * Which blending to use when displaying objects with this material. Default is {@link NormalBlending}. - * @default THREE.NormalBlending - */ - blending: Blending; - - /** - * Blending source. It's one of the blending mode constants defined in Three.js. Default is {@link SrcAlphaFactor}. - * @default THREE.SrcAlphaFactor - */ - blendSrc: BlendingSrcFactor | BlendingDstFactor; - - /** - * The tranparency of the .blendSrc. Default is null. - * @default null - */ - blendSrcAlpha: number | null; - - /** - * Changes the behavior of clipping planes so that only their intersection is clipped, rather than their union. Default is false. - * @default false - */ - clipIntersection: boolean; - - /** - * User-defined clipping planes specified as THREE.Plane objects in world space. - * These planes apply to the objects this material is attached to. - * Points in space whose signed distance to the plane is negative are clipped (not rendered). - * See the WebGL / clipping /intersection example. Default is null. - * @default null - */ - clippingPlanes: Plane[] | null; - - /** - * Defines whether to clip shadows according to the clipping planes specified on this material. Default is false. - * @default false - */ - clipShadows: boolean; - - /** - * Whether to render the material's color. This can be used in conjunction with a mesh's .renderOrder property to create invisible objects that occlude other objects. Default is true. - * @default true - */ - colorWrite: boolean; - - /** - * Custom defines to be injected into the shader. These are passed in form of an object literal, with key/value pairs. { MY_CUSTOM_DEFINE: '' , PI2: Math.PI * 2 }. - * The pairs are defined in both vertex and fragment shaders. Default is undefined. - * @default undefined - */ - defines: undefined | { [key: string]: any }; - - /** - * Which depth function to use. Default is {@link LessEqualDepth}. See the depth mode constants for all possible values. - * @default THREE.LessEqualDepth - */ - depthFunc: DepthModes; - - /** - * Whether to have depth test enabled when rendering this material. When the depth test is disabled, the depth write - * will also be implicitly disabled. - * @default true - */ - depthTest: boolean; - - /** - * Whether rendering this material has any effect on the depth buffer. Default is true. - * When drawing 2D overlays it can be useful to disable the depth writing in order to layer several things together without creating z-index artifacts. - * @default true - */ - depthWrite: boolean; - - /** - * Unique number of this material instance. - */ - id: number; - - /** - * Whether rendering this material has any effect on the stencil buffer. Default is *false*. - * @default false - */ - stencilWrite: boolean; - - /** - * The stencil comparison function to use. Default is {@link AlwaysStencilFunc}. See stencil operation constants for all possible values. - * @default THREE.AlwaysStencilFunc - */ - stencilFunc: StencilFunc; - - /** - * The value to use when performing stencil comparisons or stencil operations. Default is *0*. - * @default 0 - */ - stencilRef: number; - - /** - * The bit mask to use when writing to the stencil buffer. Default is *0xFF*. - * @default 0xff - */ - stencilWriteMask: number; - - /** - * The bit mask to use when comparing against the stencil buffer. Default is *0xFF*. - * @default 0xff - */ - stencilFuncMask: number; - - /** - * Which stencil operation to perform when the comparison function returns false. Default is {@link KeepStencilOp}. See the stencil operation constants for all possible values. - * @default THREE.KeepStencilOp - */ - stencilFail: StencilOp; - - /** - * Which stencil operation to perform when the comparison function returns true but the depth test fails. - * Default is {@link KeepStencilOp}. - * See the stencil operation constants for all possible values. - * @default THREE.KeepStencilOp - */ - stencilZFail: StencilOp; - - /** - * Which stencil operation to perform when the comparison function returns true and the depth test passes. - * Default is {@link KeepStencilOp}. - * See the stencil operation constants for all possible values. - * @default THREE.KeepStencilOp - */ - stencilZPass: StencilOp; - - /** - * Material name. Default is an empty string. - * @default '' - */ - name: string; - - /** - * Opacity. Default is 1. - * @default 1 - */ - opacity: number; - - /** - * Whether to use polygon offset. Default is false. This corresponds to the POLYGON_OFFSET_FILL WebGL feature. - * @default false - */ - polygonOffset: boolean; - - /** - * Sets the polygon offset factor. Default is 0. - * @default 0 - */ - polygonOffsetFactor: number; - - /** - * Sets the polygon offset units. Default is 0. - * @default 0 - */ - polygonOffsetUnits: number; - - /** - * Override the renderer's default precision for this material. Can be "highp", "mediump" or "lowp". Defaults is null. - * @default null - */ - precision: "highp" | "mediump" | "lowp" | null; - - /** - * Whether to premultiply the alpha (transparency) value. See WebGL / Materials / Transparency for an example of the difference. Default is false. - * @default false - */ - premultipliedAlpha: boolean; - - /** - * @default false - */ - forceSinglePass: boolean; - - /** - * Whether to apply dithering to the color to remove the appearance of banding. Default is false. - * @default false - */ - dithering: boolean; - - /** - * Defines which of the face sides will be rendered - front, back or both. - * Default is {@link THREE.FrontSide}. Other options are {@link THREE.BackSide} and {@link THREE.DoubleSide}. - * - * @default {@link THREE.FrontSide} - */ - side: Side; - - /** - * Defines which of the face sides will cast shadows. Default is *null*. - * If *null*, the value is opposite that of side, above. - * @default null - */ - shadowSide: Side | null; - - /** - * Defines whether this material is tone mapped according to the renderer's - * {@link WebGLRenderer.toneMapping toneMapping} setting. It is ignored when rendering to a render target or using - * post processing. - * @default true - */ - toneMapped: boolean; - - /** - * Defines whether this material is transparent. This has an effect on rendering as transparent objects need special treatment and are rendered after non-transparent objects. - * When set to true, the extent to which the material is transparent is controlled by setting it's .opacity property. - * @default false - */ - transparent: boolean; - - /** - * Value is the string 'Material'. This shouldn't be changed, and can be used to find all objects of this type in a scene. - * @default 'Material' - */ - type: string; - - /** - * UUID of this material instance. This gets automatically assigned, so this shouldn't be edited. - */ - uuid: string; - - /** - * Defines whether vertex coloring is used. Default is false. - * @default false - */ - vertexColors: boolean; - - /** - * Defines whether this material is visible. Default is true. - * @default true - */ - visible: boolean; - - /** - * An object that can be used to store custom data about the Material. It should not hold references to functions as these will not be cloned. - * @default {} - */ - userData: Record; - - /** - * This starts at 0 and counts how many times .needsUpdate is set to true. - * @default 0 - */ - version: number; - - /** - * Gets the alpha value to be used when running an alpha test. Default is 0. - * @default 0 - */ - get alphaTest(): number; - - /** - * Sets the alpha value to be used when running an alpha test. Default is 0. - * @default 0 - */ - set alphaTest(value: number); - - /** - * An optional callback that is executed immediately before the material is used to render a 3D object. - * Unlike properties, the callback is not supported by {@link .clone()}, {@link .copy()} and {@link .toJSON()}. - * This callback is only supported in `WebGLRenderer` (not `WebGPURenderer`). - */ - onBeforeRender( - renderer: WebGLRenderer, - scene: Scene, - camera: Camera, - geometry: BufferGeometry, - object: Object3D, - group: Group, - ): void; - - /** - * An optional callback that is executed immediately before the shader program is compiled. - * This function is called with the shader source code as a parameter. - * Useful for the modification of built-in materials. - * Unlike properties, the callback is not supported by {@link .clone()}, {@link .copy()} and {@link .toJSON()}. - * This callback is only supported in `WebGLRenderer` (not `WebGPURenderer`). - * @param parameters WebGL program parameters - * @param renderer WebGLRenderer context that is initializing the material - */ - onBeforeCompile(parameters: WebGLProgramParametersWithUniforms, renderer: WebGLRenderer): void; - - /** - * In case onBeforeCompile is used, this callback can be used to identify values of settings used in onBeforeCompile, so three.js can reuse a cached shader or recompile the shader as needed. - */ - customProgramCacheKey(): string; - - /** - * Sets the properties based on the values. - * @param values A container with parameters. - */ - setValues(values: MaterialParameters): void; - - /** - * Convert the material to three.js JSON format. - * @param meta Object containing metadata such as textures or images for the material. - */ - toJSON(meta?: JSONMeta): MaterialJSON; - - /** - * Return a new material with the same parameters as this material. - */ - clone(): this; - - /** - * Copy the parameters from the passed material into this material. - * @param material - */ - copy(material: Material): this; - - /** - * Frees the GPU-related resources allocated by this instance. Call this method whenever this instance is no longer - * used in your app. - * - * Material textures must be disposed of by the dispose() method of {@link Texture}. - */ - dispose(): void; - - /** - * Specifies that the material needs to be updated, WebGL wise. Set it to true if you made changes that need to be reflected in WebGL. - * This property is automatically set to true when instancing a new material. - * @default false - */ - set needsUpdate(value: boolean); - - /** - * @deprecated onBuild() has been removed. - */ - onBuild(object: Object3D, parameters: WebGLProgramParametersWithUniforms, renderer: WebGLRenderer): void; -} diff --git a/src-testing/src/materials/Materials.d.ts b/src-testing/src/materials/Materials.d.ts deleted file mode 100644 index dbca5e5b7..000000000 --- a/src-testing/src/materials/Materials.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -export * from "./LineBasicMaterial.js"; -export * from "./LineDashedMaterial.js"; -export * from "./Material.js"; -export * from "./MeshBasicMaterial.js"; -export * from "./MeshDepthMaterial.js"; -export * from "./MeshDistanceMaterial.js"; -export * from "./MeshLambertMaterial.js"; -export * from "./MeshMatcapMaterial.js"; -export * from "./MeshNormalMaterial.js"; -export * from "./MeshPhongMaterial.js"; -export * from "./MeshPhysicalMaterial.js"; -export * from "./MeshStandardMaterial.js"; -export * from "./MeshToonMaterial.js"; -export * from "./PointsMaterial.js"; -export * from "./RawShaderMaterial.js"; -export * from "./ShaderMaterial.js"; -export * from "./ShadowMaterial.js"; -export * from "./SpriteMaterial.js"; diff --git a/src-testing/src/materials/MeshBasicMaterial.d.ts b/src-testing/src/materials/MeshBasicMaterial.d.ts deleted file mode 100644 index 10fab3cfa..000000000 --- a/src-testing/src/materials/MeshBasicMaterial.d.ts +++ /dev/null @@ -1,139 +0,0 @@ -import { Combine } from "../constants.js"; -import { Color, ColorRepresentation } from "../math/Color.js"; -import { Euler } from "../math/Euler.js"; -import { Texture } from "../textures/Texture.js"; -import { Material, MaterialParameters } from "./Material.js"; - -/** - * parameters is an object with one or more properties defining the material's appearance. - */ -export interface MeshBasicMaterialParameters extends MaterialParameters { - color?: ColorRepresentation | undefined; - opacity?: number | undefined; - map?: Texture | null | undefined; - lightMap?: Texture | null; - lightMapIntensity?: number | undefined; - aoMap?: Texture | null | undefined; - aoMapIntensity?: number | undefined; - specularMap?: Texture | null | undefined; - alphaMap?: Texture | null | undefined; - fog?: boolean | undefined; - envMap?: Texture | null | undefined; - envMapRotation?: Euler | undefined; - combine?: Combine | undefined; - reflectivity?: number | undefined; - refractionRatio?: number | undefined; - wireframe?: boolean | undefined; - wireframeLinewidth?: number | undefined; - wireframeLinecap?: string | undefined; - wireframeLinejoin?: string | undefined; -} - -export class MeshBasicMaterial extends Material { - constructor(parameters?: MeshBasicMaterialParameters); - - /** - * Read-only flag to check if a given object is of type {@link MeshBasicMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isMeshBasicMaterial: true; - - /** - * @default 'MeshBasicMaterial' - */ - type: string; - - /** - * @default new THREE.Color( 0xffffff ) - */ - color: Color; - - /** - * @default null - */ - map: Texture | null; - - /** - * @default null - */ - lightMap: Texture | null; - - /** - * @default 1 - */ - lightMapIntensity: number; - - /** - * @default null - */ - aoMap: Texture | null; - - /** - * @default 1 - */ - aoMapIntensity: number; - - /** - * @default null - */ - specularMap: Texture | null; - - /** - * @default null - */ - alphaMap: Texture | null; - - /** - * @default null - */ - envMap: Texture | null; - - /** - * The rotation of the environment map in radians. Default is `(0,0,0)`. - */ - envMapRotation: Euler; - - /** - * @default THREE.MultiplyOperation - */ - combine: Combine; - - /** - * @default 1 - */ - reflectivity: number; - - /** - * @default 0.98 - */ - refractionRatio: number; - - /** - * @default false - */ - wireframe: boolean; - - /** - * @default 1 - */ - wireframeLinewidth: number; - - /** - * @default 'round' - */ - wireframeLinecap: string; - - /** - * @default 'round' - */ - wireframeLinejoin: string; - - /** - * Whether the material is affected by fog. Default is true. - * @default fog - */ - fog: boolean; - - setValues(parameters: MeshBasicMaterialParameters): void; -} diff --git a/src-testing/src/materials/MeshDepthMaterial.d.ts b/src-testing/src/materials/MeshDepthMaterial.d.ts deleted file mode 100644 index 86be99b6c..000000000 --- a/src-testing/src/materials/MeshDepthMaterial.d.ts +++ /dev/null @@ -1,76 +0,0 @@ -import { DepthPackingStrategies } from "../constants.js"; -import { Texture } from "../textures/Texture.js"; -import { Material, MaterialParameters } from "./Material.js"; - -export interface MeshDepthMaterialParameters extends MaterialParameters { - map?: Texture | null | undefined; - alphaMap?: Texture | null | undefined; - depthPacking?: DepthPackingStrategies | undefined; - displacementMap?: Texture | null | undefined; - displacementScale?: number | undefined; - displacementBias?: number | undefined; - wireframe?: boolean | undefined; - wireframeLinewidth?: number | undefined; -} - -export class MeshDepthMaterial extends Material { - constructor(parameters?: MeshDepthMaterialParameters); - /** - * Read-only flag to check if a given object is of type {@link MeshDepthMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isMeshDepthMaterial: true; - - /** - * @default 'MeshDepthMaterial' - */ - type: string; - - /** - * @default null - */ - map: Texture | null; - - /** - * @default null - */ - alphaMap: Texture | null; - - /** - * @default THREE.BasicDepthPacking - */ - depthPacking: DepthPackingStrategies; - - /** - * @default null - */ - displacementMap: Texture | null; - - /** - * @default 1 - */ - displacementScale: number; - - /** - * @default 0 - */ - displacementBias: number; - - /** - * @default false - */ - wireframe: boolean; - - /** - * @default 1 - */ - wireframeLinewidth: number; - - /** - * @default false - */ - fog: boolean; - - setValues(parameters: MeshDepthMaterialParameters): void; -} diff --git a/src-testing/src/materials/MeshDistanceMaterial.d.ts b/src-testing/src/materials/MeshDistanceMaterial.d.ts deleted file mode 100644 index 8a27c00ee..000000000 --- a/src-testing/src/materials/MeshDistanceMaterial.d.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { Vector3 } from "../math/Vector3.js"; -import { Texture } from "../textures/Texture.js"; -import { Material, MaterialParameters } from "./Material.js"; - -export interface MeshDistanceMaterialParameters extends MaterialParameters { - map?: Texture | null | undefined; - alphaMap?: Texture | null | undefined; - displacementMap?: Texture | null | undefined; - displacementScale?: number | undefined; - displacementBias?: number | undefined; - farDistance?: number | undefined; - nearDistance?: number | undefined; - referencePosition?: Vector3 | undefined; -} - -export class MeshDistanceMaterial extends Material { - constructor(parameters?: MeshDistanceMaterialParameters); - - /** - * Read-only flag to check if a given object is of type {@link MeshDistanceMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isMeshDistanceMaterial: true; - - /** - * @default 'MeshDistanceMaterial' - */ - type: string; - - /** - * @default null - */ - map: Texture | null; - - /** - * @default null - */ - alphaMap: Texture | null; - - /** - * @default null - */ - displacementMap: Texture | null; - - /** - * @default 1 - */ - displacementScale: number; - - /** - * @default 0 - */ - displacementBias: number; - - /** - * @default false - */ - fog: boolean; - - setValues(parameters: MeshDistanceMaterialParameters): void; -} diff --git a/src-testing/src/materials/MeshLambertMaterial.d.ts b/src-testing/src/materials/MeshLambertMaterial.d.ts deleted file mode 100644 index 47467d100..000000000 --- a/src-testing/src/materials/MeshLambertMaterial.d.ts +++ /dev/null @@ -1,204 +0,0 @@ -import { Combine, NormalMapTypes } from "../constants.js"; -import { Color, ColorRepresentation } from "../math/Color.js"; -import { Euler } from "../math/Euler.js"; -import { Vector2 } from "../math/Vector2.js"; -import { Texture } from "../textures/Texture.js"; -import { Material, MaterialParameters } from "./Material.js"; - -export interface MeshLambertMaterialParameters extends MaterialParameters { - bumpMap?: Texture | undefined; - bumpScale?: number | undefined; - color?: ColorRepresentation | undefined; - displacementMap?: Texture | undefined; - displacementScale?: number | undefined; - displacementBias?: number | undefined; - emissive?: ColorRepresentation | undefined; - emissiveIntensity?: number | undefined; - emissiveMap?: Texture | null | undefined; - flatShading?: boolean | undefined; - map?: Texture | null | undefined; - lightMap?: Texture | null | undefined; - lightMapIntensity?: number | undefined; - normalMap?: Texture | undefined; - normalScale?: Vector2 | undefined; - aoMap?: Texture | null | undefined; - aoMapIntensity?: number | undefined; - specularMap?: Texture | null | undefined; - alphaMap?: Texture | null | undefined; - envMap?: Texture | null | undefined; - envMapRotation?: Euler | undefined; - combine?: Combine | undefined; - reflectivity?: number | undefined; - refractionRatio?: number | undefined; - wireframe?: boolean | undefined; - wireframeLinewidth?: number | undefined; - wireframeLinecap?: string | undefined; - wireframeLinejoin?: string | undefined; - fog?: boolean | undefined; -} - -export class MeshLambertMaterial extends Material { - constructor(parameters?: MeshLambertMaterialParameters); - - /** - * Read-only flag to check if a given object is of type {@link MeshLambertMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isMeshLambertMaterial: true; - - /** - * @default 'MeshLambertMaterial' - */ - type: string; - - /** - * @default new THREE.Color( 0xffffff ) - */ - color: Color; - - /** - * @default null - */ - bumpMap: Texture | null; - - /** - * @default 1 - */ - bumpScale: number; - - /** - * @default null - */ - displacementMap: Texture | null; - - /** - * @default 1 - */ - displacementScale: number; - - /** - * @default 0 - */ - displacementBias: number; - - /** - * @default new THREE.Color( 0x000000 ) - */ - emissive: Color; - - /** - * @default 1 - */ - emissiveIntensity: number; - - /** - * @default null - */ - emissiveMap: Texture | null; - - /** - * @default false - */ - flatShading: boolean; - - /** - * @default null - */ - map: Texture | null; - - /** - * @default null - */ - lightMap: Texture | null; - - /** - * @default 1 - */ - lightMapIntensity: number; - - /** - * @default null - */ - normalMap: Texture | null; - - normalMapType: NormalMapTypes; - - /** - * @default new THREE.Vector2( 1, 1 ) - */ - normalScale: Vector2; - - /** - * @default null - */ - aoMap: Texture | null; - - /** - * @default 1 - */ - aoMapIntensity: number; - - /** - * @default null - */ - specularMap: Texture | null; - - /** - * @default null - */ - alphaMap: Texture | null; - - /** - * @default null - */ - envMap: Texture | null; - - /** - * The rotation of the environment map in radians. Default is `(0,0,0)`. - */ - envMapRotation: Euler; - - /** - * @default THREE.MultiplyOperation - */ - combine: Combine; - - /** - * @default 1 - */ - reflectivity: number; - - /** - * @default 0.98 - */ - refractionRatio: number; - - /** - * @default false - */ - wireframe: boolean; - - /** - * @default 1 - */ - wireframeLinewidth: number; - - /** - * @default 'round' - */ - wireframeLinecap: string; - - /** - * @default 'round' - */ - wireframeLinejoin: string; - - /** - * Whether the material is affected by fog. Default is true. - * @default fog - */ - fog: boolean; - - setValues(parameters: MeshLambertMaterialParameters): void; -} diff --git a/src-testing/src/materials/MeshMatcapMaterial.d.ts b/src-testing/src/materials/MeshMatcapMaterial.d.ts deleted file mode 100644 index 51429464b..000000000 --- a/src-testing/src/materials/MeshMatcapMaterial.d.ts +++ /dev/null @@ -1,117 +0,0 @@ -import { NormalMapTypes } from "../constants.js"; -import { Color, ColorRepresentation } from "../math/Color.js"; -import { Vector2 } from "../math/Vector2.js"; -import { Texture } from "../textures/Texture.js"; -import { Material, MaterialParameters } from "./Material.js"; - -export interface MeshMatcapMaterialParameters extends MaterialParameters { - color?: ColorRepresentation | undefined; - matcap?: Texture | null | undefined; - map?: Texture | null | undefined; - bumpMap?: Texture | null | undefined; - bumpScale?: number | undefined; - normalMap?: Texture | null | undefined; - normalMapType?: NormalMapTypes | undefined; - normalScale?: Vector2 | undefined; - displacementMap?: Texture | null | undefined; - displacementScale?: number | undefined; - displacementBias?: number | undefined; - alphaMap?: Texture | null | undefined; - fog?: boolean | undefined; - flatShading?: boolean | undefined; -} - -export class MeshMatcapMaterial extends Material { - constructor(parameters?: MeshMatcapMaterialParameters); - - /** - * Read-only flag to check if a given object is of type {@link MeshMatcapMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isMeshMatcapMaterial: true; - - /** - * @default 'MeshMatcapMaterial' - */ - type: string; - - /** - * @default { 'MATCAP': '' } - */ - defines: { [key: string]: any }; - - /** - * @default new THREE.Color( 0xffffff ) - */ - color: Color; - - /** - * @default null - */ - matcap: Texture | null; - - /** - * @default null - */ - map: Texture | null; - - /** - * @default null - */ - bumpMap: Texture | null; - - /** - * @default 1 - */ - bumpScale: number; - - /** - * @default null - */ - normalMap: Texture | null; - - /** - * @default THREE.TangentSpaceNormalMap - */ - normalMapType: NormalMapTypes; - - /** - * @default new Vector2( 1, 1 ) - */ - normalScale: Vector2; - - /** - * @default null - */ - displacementMap: Texture | null; - - /** - * @default 1 - */ - displacementScale: number; - - /** - * @default 0 - */ - displacementBias: number; - - /** - * @default null - */ - alphaMap: Texture | null; - - /** - * Define whether the material is rendered with flat shading. Default is false. - * @default false - */ - flatShading: boolean; - - /** - * Whether the material is affected by fog. Default is true. - * @default fog - */ - fog: boolean; - - setValues(parameters: MeshMatcapMaterialParameters): void; -} diff --git a/src-testing/src/materials/MeshNormalMaterial.d.ts b/src-testing/src/materials/MeshNormalMaterial.d.ts deleted file mode 100644 index b779f5e29..000000000 --- a/src-testing/src/materials/MeshNormalMaterial.d.ts +++ /dev/null @@ -1,93 +0,0 @@ -import { NormalMapTypes } from "../constants.js"; -import { Vector2 } from "../math/Vector2.js"; -import { Texture } from "../textures/Texture.js"; -import { Material, MaterialParameters } from "./Material.js"; - -export interface MeshNormalMaterialParameters extends MaterialParameters { - bumpMap?: Texture | null | undefined; - bumpScale?: number | undefined; - normalMap?: Texture | null | undefined; - normalMapType?: NormalMapTypes | undefined; - normalScale?: Vector2 | undefined; - displacementMap?: Texture | null | undefined; - displacementScale?: number | undefined; - displacementBias?: number | undefined; - wireframe?: boolean | undefined; - wireframeLinewidth?: number | undefined; - - flatShading?: boolean | undefined; -} - -export class MeshNormalMaterial extends Material { - constructor(parameters?: MeshNormalMaterialParameters); - - /** - * Read-only flag to check if a given object is of type {@link MeshNormalMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isMeshNormalMaterial: true; - - /** - * @default 'MeshNormalMaterial' - */ - type: string; - - /** - * @default null - */ - bumpMap: Texture | null; - - /** - * @default 1 - */ - bumpScale: number; - - /** - * @default null - */ - normalMap: Texture | null; - - /** - * @default THREE.TangentSpaceNormalMap - */ - normalMapType: NormalMapTypes; - - /** - * @default new THREE.Vector2( 1, 1 ) - */ - normalScale: Vector2; - - /** - * @default null - */ - displacementMap: Texture | null; - - /** - * @default 1 - */ - displacementScale: number; - - /** - * @default 0 - */ - displacementBias: number; - - /** - * @default false - */ - wireframe: boolean; - - /** - * @default 1 - */ - wireframeLinewidth: number; - - /** - * Define whether the material is rendered with flat shading. Default is false. - * @default false - */ - flatShading: boolean; - - setValues(parameters: MeshNormalMaterialParameters): void; -} diff --git a/src-testing/src/materials/MeshPhongMaterial.d.ts b/src-testing/src/materials/MeshPhongMaterial.d.ts deleted file mode 100644 index fb6bce256..000000000 --- a/src-testing/src/materials/MeshPhongMaterial.d.ts +++ /dev/null @@ -1,228 +0,0 @@ -import { Combine, NormalMapTypes } from "../constants.js"; -import { Color, ColorRepresentation } from "../math/Color.js"; -import { Euler } from "../math/Euler.js"; -import { Vector2 } from "../math/Vector2.js"; -import { Texture } from "../textures/Texture.js"; -import { Material, MaterialParameters } from "./Material.js"; - -export interface MeshPhongMaterialParameters extends MaterialParameters { - /** geometry color in hexadecimal. Default is 0xffffff. */ - color?: ColorRepresentation | undefined; - specular?: ColorRepresentation | undefined; - shininess?: number | undefined; - opacity?: number | undefined; - map?: Texture | null | undefined; - lightMap?: Texture | null | undefined; - lightMapIntensity?: number | undefined; - aoMap?: Texture | null | undefined; - aoMapIntensity?: number | undefined; - emissive?: ColorRepresentation | undefined; - emissiveIntensity?: number | undefined; - emissiveMap?: Texture | null | undefined; - bumpMap?: Texture | null | undefined; - bumpScale?: number | undefined; - normalMap?: Texture | null | undefined; - normalMapType?: NormalMapTypes | undefined; - normalScale?: Vector2 | undefined; - displacementMap?: Texture | null | undefined; - displacementScale?: number | undefined; - displacementBias?: number | undefined; - specularMap?: Texture | null | undefined; - alphaMap?: Texture | null | undefined; - envMap?: Texture | null | undefined; - envMapRotation?: Euler | undefined; - combine?: Combine | undefined; - reflectivity?: number | undefined; - refractionRatio?: number | undefined; - wireframe?: boolean | undefined; - wireframeLinewidth?: number | undefined; - wireframeLinecap?: string | undefined; - wireframeLinejoin?: string | undefined; - fog?: boolean | undefined; - flatShading?: boolean | undefined; -} - -export class MeshPhongMaterial extends Material { - constructor(parameters?: MeshPhongMaterialParameters); - - /** - * Read-only flag to check if a given object is of type {@link MeshPhongMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isMeshPhongMaterial: true; - - /** - * @default 'MeshNormalMaterial' - */ - type: string; - - /** - * @default new THREE.Color( 0xffffff ) - */ - color: Color; - - /** - * @default new THREE.Color( 0x111111 ) - */ - specular: Color; - - /** - * @default 30 - */ - shininess: number; - - /** - * @default null - */ - map: Texture | null; - - /** - * @default null - */ - lightMap: Texture | null; - - /** - * @default null - */ - lightMapIntensity: number; - - /** - * @default null - */ - aoMap: Texture | null; - - /** - * @default null - */ - aoMapIntensity: number; - - /** - * @default new THREE.Color( 0x000000 ) - */ - emissive: Color; - - /** - * @default 1 - */ - emissiveIntensity: number; - - /** - * @default null - */ - emissiveMap: Texture | null; - - /** - * @default null - */ - bumpMap: Texture | null; - - /** - * @default 1 - */ - bumpScale: number; - - /** - * @default null - */ - normalMap: Texture | null; - - /** - * @default THREE.TangentSpaceNormalMap - */ - normalMapType: NormalMapTypes; - - /** - * @default new Vector2( 1, 1 ) - */ - normalScale: Vector2; - - /** - * @default null - */ - displacementMap: Texture | null; - - /** - * @default 1 - */ - displacementScale: number; - - /** - * @default 0 - */ - displacementBias: number; - - /** - * @default null - */ - specularMap: Texture | null; - - /** - * @default null - */ - alphaMap: Texture | null; - - /** - * @default null - */ - envMap: Texture | null; - - /** - * The rotation of the environment map in radians. Default is `(0,0,0)`. - */ - envMapRotation: Euler; - - /** - * @default THREE.MultiplyOperation - */ - combine: Combine; - - /** - * @default 1 - */ - reflectivity: number; - - /** - * @default 0.98 - */ - refractionRatio: number; - - /** - * @default false - */ - wireframe: boolean; - - /** - * @default 1 - */ - wireframeLinewidth: number; - - /** - * @default 'round' - */ - wireframeLinecap: string; - - /** - * @default 'round' - */ - wireframeLinejoin: string; - - /** - * Define whether the material is rendered with flat shading. Default is false. - * @default false - */ - flatShading: boolean; - - /** - * @deprecated Use {@link MeshStandardMaterial THREE.MeshStandardMaterial} instead. - */ - metal: boolean; - - /** - * Whether the material is affected by fog. Default is true. - * @default fog - */ - fog: boolean; - - setValues(parameters: MeshPhongMaterialParameters): void; -} diff --git a/src-testing/src/materials/MeshPhysicalMaterial.d.ts b/src-testing/src/materials/MeshPhysicalMaterial.d.ts deleted file mode 100644 index 503fe6a6b..000000000 --- a/src-testing/src/materials/MeshPhysicalMaterial.d.ts +++ /dev/null @@ -1,236 +0,0 @@ -import { Color, ColorRepresentation } from "../math/Color.js"; -import { Vector2 } from "../math/Vector2.js"; -import { Texture } from "../textures/Texture.js"; -import { MeshStandardMaterial, MeshStandardMaterialParameters } from "./MeshStandardMaterial.js"; - -export interface MeshPhysicalMaterialParameters extends MeshStandardMaterialParameters { - anisotropyRotation?: number | undefined; - anisotropyMap?: Texture | null | undefined; - - clearcoatMap?: Texture | null | undefined; - clearcoatRoughness?: number | undefined; - clearcoatRoughnessMap?: Texture | null | undefined; - clearcoatNormalScale?: Vector2 | undefined; - clearcoatNormalMap?: Texture | null | undefined; - - ior?: number | undefined; - - reflectivity?: number | undefined; - - iridescenceMap?: Texture | null | undefined; - iridescenceIOR?: number | undefined; - iridescenceThicknessRange?: [number, number] | undefined; - iridescenceThicknessMap?: Texture | null | undefined; - - sheenColor?: ColorRepresentation | undefined; - sheenColorMap?: Texture | null | undefined; - sheenRoughness?: number | undefined; - sheenRoughnessMap?: Texture | null | undefined; - - transmissionMap?: Texture | null | undefined; - - thickness?: number | undefined; - thicknessMap?: Texture | null | undefined; - attenuationDistance?: number | undefined; - attenuationColor?: ColorRepresentation | undefined; - - specularIntensity?: number | undefined; - specularIntensityMap?: Texture | null | undefined; - specularColor?: ColorRepresentation | undefined; - specularColorMap?: Texture | null | undefined; - - anisotropy?: number | undefined; - clearcoat?: number | undefined; - iridescence?: number | undefined; - dispersion?: number | undefined; - sheen?: number | undefined; - transmission?: number | undefined; -} - -export class MeshPhysicalMaterial extends MeshStandardMaterial { - constructor(parameters?: MeshPhysicalMaterialParameters); - - /** - * Read-only flag to check if a given object is of type {@link MeshPhysicalMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isMeshPhysicalMaterial: true; - - /** - * @default { 'STANDARD': '', 'PHYSICAL': '' } - */ - defines: { [key: string]: any }; - - /** - * @default 'MeshPhysicalMaterial' - */ - type: string; - - /** - * @default 0 - */ - anisotropyRotation?: number; - - /** - * @default null - */ - anisotropyMap?: Texture | null; - - /** - * @default null - */ - clearcoatMap: Texture | null; - - /** - * @default 0 - */ - clearcoatRoughness: number; - - /** - * @default null - */ - clearcoatRoughnessMap: Texture | null; - - /** - * @default new THREE.Vector2( 1, 1 ) - */ - clearcoatNormalScale: Vector2; - - /** - * @default null - */ - clearcoatNormalMap: Texture | null; - - /** - * @default 1.5 - */ - ior: number; - - /** - * @default 0.5 - */ - get reflectivity(): number; - set reflectivity(reflectivity: number); - - /** - * @default null - */ - iridescenceMap: Texture | null; - - /** - * @default 1.3 - */ - iridescenceIOR: number; - - /** - * @default [100, 400] - */ - iridescenceThicknessRange: [number, number]; - - /** - * @default null - */ - iridescenceThicknessMap: Texture | null; - - /** - * @default Color( 0x000000 ) - */ - sheenColor: Color; - - /** - * @default null - */ - sheenColorMap: Texture | null; - - /** - * @default 1.0 - */ - sheenRoughness: number; - - /** - * @default null - */ - sheenRoughnessMap: Texture | null; - - /** - * @default null - */ - transmissionMap: Texture | null; - - /** - * @default 0.01 - */ - thickness: number; - - /** - * @default null - */ - thicknessMap: Texture | null; - - /** - * @default 0.0 - */ - attenuationDistance: number; - - /** - * @default Color( 1, 1, 1 ) - */ - attenuationColor: Color; - - /** - * @default 1.0 - */ - specularIntensity: number; - - /** - * @default null - */ - specularIntensityMap: Texture | null; - - /** - * @default Color(1, 1, 1) - */ - specularColor: Color; - - /** - * @default null - */ - specularColorMap: Texture | null; - - /** - * @default 0 - */ - get anisotropy(): number; - set anisotropy(value: number); - - /** - * @default 0 - */ - get clearcoat(): number; - set clearcoat(value: number); - - /** - * @default 0 - */ - get iridescence(): number; - set iridescence(value: number); - - /** - * @default 0 - */ - get dispersion(): number; - set dispersion(value: number); - - /** - * @default 0.0 - */ - get sheen(): number; - set sheen(value: number); - - /** - * @default 0 - */ - get transmission(): number; - set transmission(value: number); -} diff --git a/src-testing/src/materials/MeshStandardMaterial.d.ts b/src-testing/src/materials/MeshStandardMaterial.d.ts deleted file mode 100644 index ddde9cfba..000000000 --- a/src-testing/src/materials/MeshStandardMaterial.d.ts +++ /dev/null @@ -1,218 +0,0 @@ -import { NormalMapTypes } from "../constants.js"; -import { Color, ColorRepresentation } from "../math/Color.js"; -import { Euler } from "../math/Euler.js"; -import { Vector2 } from "../math/Vector2.js"; -import { Texture } from "../textures/Texture.js"; -import { Material, MaterialParameters } from "./Material.js"; - -export interface MeshStandardMaterialParameters extends MaterialParameters { - color?: ColorRepresentation | undefined; - roughness?: number | undefined; - metalness?: number | undefined; - map?: Texture | null | undefined; - lightMap?: Texture | null | undefined; - lightMapIntensity?: number | undefined; - aoMap?: Texture | null | undefined; - aoMapIntensity?: number | undefined; - emissive?: ColorRepresentation | undefined; - emissiveIntensity?: number | undefined; - emissiveMap?: Texture | null | undefined; - bumpMap?: Texture | null | undefined; - bumpScale?: number | undefined; - normalMap?: Texture | null | undefined; - normalMapType?: NormalMapTypes | undefined; - normalScale?: Vector2 | undefined; - displacementMap?: Texture | null | undefined; - displacementScale?: number | undefined; - displacementBias?: number | undefined; - roughnessMap?: Texture | null | undefined; - metalnessMap?: Texture | null | undefined; - alphaMap?: Texture | null | undefined; - envMap?: Texture | null | undefined; - envMapRotation?: Euler | undefined; - envMapIntensity?: number | undefined; - wireframe?: boolean | undefined; - wireframeLinewidth?: number | undefined; - fog?: boolean | undefined; - flatShading?: boolean | undefined; -} - -export class MeshStandardMaterial extends Material { - constructor(parameters?: MeshStandardMaterialParameters); - - /** - * Read-only flag to check if a given object is of type {@link MeshStandardMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isMeshStandardMaterial: true; - - /** - * @default 'MeshStandardMaterial' - */ - type: string; - - /** - * @default { 'STANDARD': '' } - */ - defines: { [key: string]: any }; - - /** - * @default new THREE.Color( 0xffffff ) - */ - color: Color; - - /** - * @default 1 - */ - roughness: number; - - /** - * @default 0 - */ - metalness: number; - - /** - * @default null - */ - map: Texture | null; - - /** - * @default null - */ - lightMap: Texture | null; - - /** - * @default 1 - */ - lightMapIntensity: number; - - /** - * @default null - */ - aoMap: Texture | null; - - /** - * @default 1 - */ - aoMapIntensity: number; - - /** - * @default new THREE.Color( 0x000000 ) - */ - emissive: Color; - - /** - * @default 1 - */ - emissiveIntensity: number; - - /** - * @default null - */ - emissiveMap: Texture | null; - - /** - * @default null - */ - bumpMap: Texture | null; - - /** - * @default 1 - */ - bumpScale: number; - - /** - * @default null - */ - normalMap: Texture | null; - - /** - * @default THREE.TangentSpaceNormalMap - */ - normalMapType: NormalMapTypes; - - /** - * @default new THREE.Vector2( 1, 1 ) - */ - normalScale: Vector2; - - /** - * @default null - */ - displacementMap: Texture | null; - - /** - * @default 1 - */ - displacementScale: number; - - /** - * @default 0 - */ - displacementBias: number; - - /** - * @default null - */ - roughnessMap: Texture | null; - - /** - * @default null - */ - metalnessMap: Texture | null; - - /** - * @default null - */ - alphaMap: Texture | null; - - /** - * @default null - */ - envMap: Texture | null; - - /** - * The rotation of the environment map in radians. Default is `(0,0,0)`. - */ - envMapRotation: Euler; - - /** - * @default 1 - */ - envMapIntensity: number; - - /** - * @default false - */ - wireframe: boolean; - - /** - * @default 1 - */ - wireframeLinewidth: number; - - /** - * @default 'round' - */ - wireframeLinecap: string; - - /** - * @default 'round' - */ - wireframeLinejoin: string; - - /** - * Define whether the material is rendered with flat shading. Default is false. - * @default false - */ - flatShading: boolean; - - /** - * Whether the material is affected by fog. Default is true. - * @default fog - */ - fog: boolean; - - setValues(parameters: MeshStandardMaterialParameters): void; -} diff --git a/src-testing/src/materials/MeshToonMaterial.d.ts b/src-testing/src/materials/MeshToonMaterial.d.ts deleted file mode 100644 index 0f0c6f61d..000000000 --- a/src-testing/src/materials/MeshToonMaterial.d.ts +++ /dev/null @@ -1,178 +0,0 @@ -import { NormalMapTypes } from "../constants.js"; -import { Color, ColorRepresentation } from "../math/Color.js"; -import { Vector2 } from "../math/Vector2.js"; -import { Texture } from "../textures/Texture.js"; -import { Material, MaterialParameters } from "./Material.js"; - -export interface MeshToonMaterialParameters extends MaterialParameters { - /** geometry color in hexadecimal. Default is 0xffffff. */ - color?: ColorRepresentation | undefined; - opacity?: number | undefined; - gradientMap?: Texture | null | undefined; - map?: Texture | null | undefined; - lightMap?: Texture | null | undefined; - lightMapIntensity?: number | undefined; - aoMap?: Texture | null | undefined; - aoMapIntensity?: number | undefined; - emissive?: ColorRepresentation | undefined; - emissiveIntensity?: number | undefined; - emissiveMap?: Texture | null | undefined; - bumpMap?: Texture | null | undefined; - bumpScale?: number | undefined; - normalMap?: Texture | null | undefined; - normalMapType?: NormalMapTypes | undefined; - normalScale?: Vector2 | undefined; - displacementMap?: Texture | null | undefined; - displacementScale?: number | undefined; - displacementBias?: number | undefined; - alphaMap?: Texture | null | undefined; - wireframe?: boolean | undefined; - wireframeLinewidth?: number | undefined; - wireframeLinecap?: string | undefined; - wireframeLinejoin?: string | undefined; - fog?: boolean | undefined; -} - -export class MeshToonMaterial extends Material { - constructor(parameters?: MeshToonMaterialParameters); - - /** - * Read-only flag to check if a given object is of type {@link MeshToonMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isMeshToonMaterial: true; - - /** - * @default 'MeshToonMaterial' - */ - type: string; - - /** - * @default { 'TOON': '' } - */ - defines: { [key: string]: any }; - - /** - * @default new THREE.Color( 0xffffff ) - */ - color: Color; - - /** - * @default null - */ - gradientMap: Texture | null; - - /** - * @default null - */ - map: Texture | null; - - /** - * @default null - */ - lightMap: Texture | null; - - /** - * @default 1 - */ - lightMapIntensity: number; - - /** - * @default null - */ - aoMap: Texture | null; - - /** - * @default 1 - */ - aoMapIntensity: number; - - /** - * @default new THREE.Color( 0x000000 ) - */ - emissive: Color; - - /** - * @default 1 - */ - emissiveIntensity: number; - - /** - * @default null - */ - emissiveMap: Texture | null; - - /** - * @default null - */ - bumpMap: Texture | null; - - /** - * @default 1 - */ - bumpScale: number; - - /** - * @default null - */ - normalMap: Texture | null; - - /** - * @default THREE.TangentSpaceNormalMap - */ - normalMapType: NormalMapTypes; - - /** - * @default new THREE.Vector2( 1, 1 ) - */ - normalScale: Vector2; - - /** - * @default null - */ - displacementMap: Texture | null; - - /** - * @default 1 - */ - displacementScale: number; - - /** - * @default 0 - */ - displacementBias: number; - - /** - * @default null - */ - alphaMap: Texture | null; - - /** - * @default false - */ - wireframe: boolean; - - /** - * @default 1 - */ - wireframeLinewidth: number; - - /** - * @default 'round' - */ - wireframeLinecap: string; - - /** - * @default 'round' - */ - wireframeLinejoin: string; - - /** - * Whether the material is affected by fog. Default is true. - * @default fog - */ - fog: boolean; - - setValues(parameters: MeshToonMaterialParameters): void; -} diff --git a/src-testing/src/materials/PointsMaterial.d.ts b/src-testing/src/materials/PointsMaterial.d.ts deleted file mode 100644 index 58e32868c..000000000 --- a/src-testing/src/materials/PointsMaterial.d.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { Color, ColorRepresentation } from "../math/Color.js"; -import { Texture } from "../textures/Texture.js"; -import { Material, MaterialParameters } from "./Material.js"; - -export interface PointsMaterialParameters extends MaterialParameters { - color?: ColorRepresentation | undefined; - map?: Texture | null | undefined; - alphaMap?: Texture | null | undefined; - size?: number | undefined; - sizeAttenuation?: boolean | undefined; - fog?: boolean | undefined; -} - -export class PointsMaterial extends Material { - constructor(parameters?: PointsMaterialParameters); - - /** - * Read-only flag to check if a given object is of type {@link PointsMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isPointsMaterial: true; - - /** - * @default 'PointsMaterial' - */ - type: string; - - /** - * @default new THREE.Color( 0xffffff ) - */ - color: Color; - - /** - * @default null - */ - map: Texture | null; - - /** - * @default null - */ - alphaMap: Texture | null; - - /** - * @default 1 - */ - size: number; - - /** - * @default true - */ - sizeAttenuation: boolean; - - /** - * Whether the material is affected by fog. Default is true. - * @default fog - */ - fog: boolean; - - setValues(parameters: PointsMaterialParameters): void; -} diff --git a/src-testing/src/materials/RawShaderMaterial.d.ts b/src-testing/src/materials/RawShaderMaterial.d.ts deleted file mode 100644 index f033fc6b0..000000000 --- a/src-testing/src/materials/RawShaderMaterial.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { ShaderMaterial, ShaderMaterialParameters } from "./ShaderMaterial.js"; - -export class RawShaderMaterial extends ShaderMaterial { - constructor(parameters?: ShaderMaterialParameters); - - /** - * Read-only flag to check if a given object is of type {@link RawShaderMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isRawShaderMaterial: true; - - override readonly type: "RawShaderMaterial"; -} diff --git a/src-testing/src/materials/ShaderMaterial.d.ts b/src-testing/src/materials/ShaderMaterial.d.ts deleted file mode 100644 index 671b3e2c3..000000000 --- a/src-testing/src/materials/ShaderMaterial.d.ts +++ /dev/null @@ -1,167 +0,0 @@ -import { GLSLVersion } from "../constants.js"; -import { JSONMeta } from "../core/Object3D.js"; -import { UniformsGroup } from "../core/UniformsGroup.js"; -import { Matrix3, Matrix3Tuple } from "../math/Matrix3.js"; -import { Matrix4, Matrix4Tuple } from "../math/Matrix4.js"; -import { Vector2Tuple } from "../math/Vector2.js"; -import { Vector3Tuple } from "../math/Vector3.js"; -import { Vector4Tuple } from "../math/Vector4.js"; -import { IUniform } from "../renderers/shaders/UniformsLib.js"; -import { Material, MaterialJSON, MaterialParameters } from "./Material.js"; - -export interface ShaderMaterialParameters extends MaterialParameters { - uniforms?: { [uniform: string]: IUniform } | undefined; - uniformsGroups?: UniformsGroup[] | undefined; - vertexShader?: string | undefined; - fragmentShader?: string | undefined; - linewidth?: number | undefined; - wireframe?: boolean | undefined; - wireframeLinewidth?: number | undefined; - lights?: boolean | undefined; - clipping?: boolean | undefined; - fog?: boolean | undefined; - extensions?: - | { - clipCullDistance?: boolean | undefined; - multiDraw?: boolean | undefined; - } - | undefined; - glslVersion?: GLSLVersion | undefined; -} - -export type ShaderMaterialUniformJSON = { - type: "t"; - value: string; -} | { - type: "c"; - value: number; -} | { - type: "v2"; - value: Vector2Tuple; -} | { - type: "v3"; - value: Vector3Tuple; -} | { - type: "v4"; - value: Vector4Tuple; -} | { - type: "m3"; - value: Matrix3Tuple; -} | { - type: "m4"; - value: Matrix4Tuple; -} | { - value: unknown; -}; - -export interface ShaderMaterialJSON extends MaterialJSON { - glslVersion: number | null; - uniforms: Record; - - defines?: Record; - - vertexShader: string; - ragmentShader: string; - - lights: boolean; - clipping: boolean; - - extensions?: Record; -} - -export class ShaderMaterial extends Material { - constructor(parameters?: ShaderMaterialParameters); - - /** - * Read-only flag to check if a given object is of type {@link ShaderMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isShaderMaterial: true; - - /** - * @default 'ShaderMaterial' - */ - type: string; - - /** - * @default {} - */ - defines: { [key: string]: any }; - - /** - * @default {} - */ - uniforms: { [uniform: string]: IUniform }; - - uniformsGroups: UniformsGroup[]; - - vertexShader: string; - - fragmentShader: string; - - /** - * @default 1 - */ - linewidth: number; - - /** - * @default false - */ - wireframe: boolean; - - /** - * @default 1 - */ - wireframeLinewidth: number; - - /** - * @default false - */ - fog: boolean; - - /** - * @default false - */ - lights: boolean; - - /** - * @default false - */ - clipping: boolean; - - /** - * @default { - * clipCullDistance: false, - * multiDraw: false - * } - */ - extensions: { - clipCullDistance: boolean; - multiDraw: boolean; - }; - - /** - * @default { 'color': [ 1, 1, 1 ], 'uv': [ 0, 0 ], 'uv1': [ 0, 0 ] } - */ - defaultAttributeValues: any; - - /** - * @default undefined - */ - index0AttributeName: string | undefined; - - /** - * @default false - */ - uniformsNeedUpdate: boolean; - - /** - * @default null - */ - glslVersion: GLSLVersion | null; - - setValues(parameters: ShaderMaterialParameters): void; - - toJSON(meta?: JSONMeta): ShaderMaterialJSON; -} diff --git a/src-testing/src/materials/ShadowMaterial.d.ts b/src-testing/src/materials/ShadowMaterial.d.ts deleted file mode 100644 index 46c8741d8..000000000 --- a/src-testing/src/materials/ShadowMaterial.d.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { Color, ColorRepresentation } from "../math/Color.js"; -import { Material, MaterialParameters } from "./Material.js"; - -export interface ShadowMaterialParameters extends MaterialParameters { - color?: ColorRepresentation | undefined; - fog?: boolean | undefined; -} - -export class ShadowMaterial extends Material { - constructor(parameters?: ShadowMaterialParameters); - - /** - * Read-only flag to check if a given object is of type {@link ShadowMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isShadowMaterial: true; - - /** - * @default 'ShadowMaterial' - */ - type: string; - - /** - * @default new THREE.Color( 0x000000 ) - */ - color: Color; - - /** - * @default true - */ - transparent: boolean; - - /** - * Whether the material is affected by fog. Default is true. - * @default fog - */ - fog: boolean; -} diff --git a/src-testing/src/materials/SpriteMaterial.d.ts b/src-testing/src/materials/SpriteMaterial.d.ts deleted file mode 100644 index 8dc4d9738..000000000 --- a/src-testing/src/materials/SpriteMaterial.d.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { Color, ColorRepresentation } from "../math/Color.js"; -import { Texture } from "../textures/Texture.js"; -import { Material, MaterialParameters } from "./Material.js"; - -export interface SpriteMaterialParameters extends MaterialParameters { - color?: ColorRepresentation | undefined; - map?: Texture | null | undefined; - alphaMap?: Texture | null | undefined; - rotation?: number | undefined; - sizeAttenuation?: boolean | undefined; - fog?: boolean | undefined; -} - -export class SpriteMaterial extends Material { - constructor(parameters?: SpriteMaterialParameters); - /** - * Read-only flag to check if a given object is of type {@link SpriteMaterial}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isSpriteMaterial: true; - - /** - * @default 'SpriteMaterial' - */ - type: string; - - /** - * @default new THREE.Color( 0xffffff ) - */ - color: Color; - - /** - * @default null - */ - map: Texture | null; - - /** - * @default null - */ - alphaMap: Texture | null; - - /** - * @default 0 - */ - rotation: number; - - /** - * @default true - */ - sizeAttenuation: boolean; - - /** - * @default true - */ - transparent: boolean; - - /** - * Whether the material is affected by fog. Default is true. - * @default fog - */ - fog: boolean; - - setValues(parameters: SpriteMaterialParameters): void; - copy(source: SpriteMaterial): this; -} diff --git a/src-testing/src/math/Box2.d.ts b/src-testing/src/math/Box2.d.ts deleted file mode 100644 index de05083d0..000000000 --- a/src-testing/src/math/Box2.d.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { Vector2 } from "./Vector2.js"; - -// Math ////////////////////////////////////////////////////////////////////////////////// - -export class Box2 { - constructor(min?: Vector2, max?: Vector2); - - /** - * @default new THREE.Vector2( + Infinity, + Infinity ) - */ - min: Vector2; - - /** - * @default new THREE.Vector2( - Infinity, - Infinity ) - */ - max: Vector2; - - set(min: Vector2, max: Vector2): Box2; - setFromPoints(points: Vector2[]): Box2; - setFromCenterAndSize(center: Vector2, size: Vector2): Box2; - clone(): this; - copy(box: Box2): this; - makeEmpty(): Box2; - isEmpty(): boolean; - getCenter(target: Vector2): Vector2; - getSize(target: Vector2): Vector2; - expandByPoint(point: Vector2): Box2; - expandByVector(vector: Vector2): Box2; - expandByScalar(scalar: number): Box2; - containsPoint(point: Vector2): boolean; - containsBox(box: Box2): boolean; - getParameter(point: Vector2, target: Vector2): Vector2; - intersectsBox(box: Box2): boolean; - clampPoint(point: Vector2, target: Vector2): Vector2; - distanceToPoint(point: Vector2): number; - intersect(box: Box2): Box2; - union(box: Box2): Box2; - translate(offset: Vector2): Box2; - equals(box: Box2): boolean; - /** - * @deprecated Use {@link Box2#isEmpty .isEmpty()} instead. - */ - empty(): any; - /** - * @deprecated Use {@link Box2#intersectsBox .intersectsBox()} instead. - */ - isIntersectionBox(b: any): any; -} diff --git a/src-testing/src/math/Box3.d.ts b/src-testing/src/math/Box3.d.ts deleted file mode 100644 index 2e7b11bc9..000000000 --- a/src-testing/src/math/Box3.d.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { BufferAttribute } from "../core/BufferAttribute.js"; -import { Object3D } from "../core/Object3D.js"; -import { Matrix4 } from "./Matrix4.js"; -import { Plane } from "./Plane.js"; -import { Sphere } from "./Sphere.js"; -import { Triangle } from "./Triangle.js"; -import { Vector3 } from "./Vector3.js"; - -export class Box3 { - constructor(min?: Vector3, max?: Vector3); - - /** - * @default new THREE.Vector3( + Infinity, + Infinity, + Infinity ) - */ - min: Vector3; - - /** - * @default new THREE.Vector3( - Infinity, - Infinity, - Infinity ) - */ - max: Vector3; - readonly isBox3: true; - - set(min: Vector3, max: Vector3): this; - setFromArray(array: ArrayLike): this; - setFromBufferAttribute(bufferAttribute: BufferAttribute): this; - setFromPoints(points: Vector3[]): this; - setFromCenterAndSize(center: Vector3, size: Vector3): this; - setFromObject(object: Object3D, precise?: boolean): this; - clone(): this; - copy(box: Box3): this; - makeEmpty(): this; - isEmpty(): boolean; - getCenter(target: Vector3): Vector3; - getSize(target: Vector3): Vector3; - expandByPoint(point: Vector3): this; - expandByVector(vector: Vector3): this; - expandByScalar(scalar: number): this; - expandByObject(object: Object3D, precise?: boolean): this; - containsPoint(point: Vector3): boolean; - containsBox(box: Box3): boolean; - getParameter(point: Vector3, target: Vector3): Vector3; - intersectsBox(box: Box3): boolean; - intersectsSphere(sphere: Sphere): boolean; - intersectsPlane(plane: Plane): boolean; - intersectsTriangle(triangle: Triangle): boolean; - clampPoint(point: Vector3, target: Vector3): Vector3; - distanceToPoint(point: Vector3): number; - getBoundingSphere(target: Sphere): Sphere; - intersect(box: Box3): this; - union(box: Box3): this; - applyMatrix4(matrix: Matrix4): this; - translate(offset: Vector3): this; - equals(box: Box3): boolean; - /** - * @deprecated Use {@link Box3#isEmpty .isEmpty()} instead. - */ - empty(): any; - /** - * @deprecated Use {@link Box3#intersectsBox .intersectsBox()} instead. - */ - isIntersectionBox(b: any): any; - /** - * @deprecated Use {@link Box3#intersectsSphere .intersectsSphere()} instead. - */ - isIntersectionSphere(s: any): any; -} diff --git a/src-testing/src/math/Color.d.ts b/src-testing/src/math/Color.d.ts deleted file mode 100644 index 487d4c57a..000000000 --- a/src-testing/src/math/Color.d.ts +++ /dev/null @@ -1,358 +0,0 @@ -import { ColorSpace } from "../constants.js"; -import { Matrix3 } from "./Matrix3.js"; -import { Vector3 } from "./Vector3.js"; - -import { BufferAttribute } from "../core/BufferAttribute.js"; -import { InterleavedBufferAttribute } from "../core/InterleavedBufferAttribute.js"; - -export { SRGBToLinear } from "./ColorManagement.js"; - -declare const _colorKeywords: { - aliceblue: 0xf0f8ff; - antiquewhite: 0xfaebd7; - aqua: 0x00ffff; - aquamarine: 0x7fffd4; - azure: 0xf0ffff; - beige: 0xf5f5dc; - bisque: 0xffe4c4; - black: 0x000000; - blanchedalmond: 0xffebcd; - blue: 0x0000ff; - blueviolet: 0x8a2be2; - brown: 0xa52a2a; - burlywood: 0xdeb887; - cadetblue: 0x5f9ea0; - chartreuse: 0x7fff00; - chocolate: 0xd2691e; - coral: 0xff7f50; - cornflowerblue: 0x6495ed; - cornsilk: 0xfff8dc; - crimson: 0xdc143c; - cyan: 0x00ffff; - darkblue: 0x00008b; - darkcyan: 0x008b8b; - darkgoldenrod: 0xb8860b; - darkgray: 0xa9a9a9; - darkgreen: 0x006400; - darkgrey: 0xa9a9a9; - darkkhaki: 0xbdb76b; - darkmagenta: 0x8b008b; - darkolivegreen: 0x556b2f; - darkorange: 0xff8c00; - darkorchid: 0x9932cc; - darkred: 0x8b0000; - darksalmon: 0xe9967a; - darkseagreen: 0x8fbc8f; - darkslateblue: 0x483d8b; - darkslategray: 0x2f4f4f; - darkslategrey: 0x2f4f4f; - darkturquoise: 0x00ced1; - darkviolet: 0x9400d3; - deeppink: 0xff1493; - deepskyblue: 0x00bfff; - dimgray: 0x696969; - dimgrey: 0x696969; - dodgerblue: 0x1e90ff; - firebrick: 0xb22222; - floralwhite: 0xfffaf0; - forestgreen: 0x228b22; - fuchsia: 0xff00ff; - gainsboro: 0xdcdcdc; - ghostwhite: 0xf8f8ff; - gold: 0xffd700; - goldenrod: 0xdaa520; - gray: 0x808080; - green: 0x008000; - greenyellow: 0xadff2f; - grey: 0x808080; - honeydew: 0xf0fff0; - hotpink: 0xff69b4; - indianred: 0xcd5c5c; - indigo: 0x4b0082; - ivory: 0xfffff0; - khaki: 0xf0e68c; - lavender: 0xe6e6fa; - lavenderblush: 0xfff0f5; - lawngreen: 0x7cfc00; - lemonchiffon: 0xfffacd; - lightblue: 0xadd8e6; - lightcoral: 0xf08080; - lightcyan: 0xe0ffff; - lightgoldenrodyellow: 0xfafad2; - lightgray: 0xd3d3d3; - lightgreen: 0x90ee90; - lightgrey: 0xd3d3d3; - lightpink: 0xffb6c1; - lightsalmon: 0xffa07a; - lightseagreen: 0x20b2aa; - lightskyblue: 0x87cefa; - lightslategray: 0x778899; - lightslategrey: 0x778899; - lightsteelblue: 0xb0c4de; - lightyellow: 0xffffe0; - lime: 0x00ff00; - limegreen: 0x32cd32; - linen: 0xfaf0e6; - magenta: 0xff00ff; - maroon: 0x800000; - mediumaquamarine: 0x66cdaa; - mediumblue: 0x0000cd; - mediumorchid: 0xba55d3; - mediumpurple: 0x9370db; - mediumseagreen: 0x3cb371; - mediumslateblue: 0x7b68ee; - mediumspringgreen: 0x00fa9a; - mediumturquoise: 0x48d1cc; - mediumvioletred: 0xc71585; - midnightblue: 0x191970; - mintcream: 0xf5fffa; - mistyrose: 0xffe4e1; - moccasin: 0xffe4b5; - navajowhite: 0xffdead; - navy: 0x000080; - oldlace: 0xfdf5e6; - olive: 0x808000; - olivedrab: 0x6b8e23; - orange: 0xffa500; - orangered: 0xff4500; - orchid: 0xda70d6; - palegoldenrod: 0xeee8aa; - palegreen: 0x98fb98; - paleturquoise: 0xafeeee; - palevioletred: 0xdb7093; - papayawhip: 0xffefd5; - peachpuff: 0xffdab9; - peru: 0xcd853f; - pink: 0xffc0cb; - plum: 0xdda0dd; - powderblue: 0xb0e0e6; - purple: 0x800080; - rebeccapurple: 0x663399; - red: 0xff0000; - rosybrown: 0xbc8f8f; - royalblue: 0x4169e1; - saddlebrown: 0x8b4513; - salmon: 0xfa8072; - sandybrown: 0xf4a460; - seagreen: 0x2e8b57; - seashell: 0xfff5ee; - sienna: 0xa0522d; - silver: 0xc0c0c0; - skyblue: 0x87ceeb; - slateblue: 0x6a5acd; - slategray: 0x708090; - slategrey: 0x708090; - snow: 0xfffafa; - springgreen: 0x00ff7f; - steelblue: 0x4682b4; - tan: 0xd2b48c; - teal: 0x008080; - thistle: 0xd8bfd8; - tomato: 0xff6347; - turquoise: 0x40e0d0; - violet: 0xee82ee; - wheat: 0xf5deb3; - white: 0xffffff; - whitesmoke: 0xf5f5f5; - yellow: 0xffff00; - yellowgreen: 0x9acd32; -}; - -export type ColorRepresentation = Color | string | number; - -export interface HSL { - h: number; - s: number; - l: number; -} - -export interface RGB { - r: number; - g: number; - b: number; -} - -/** - * Represents a color. See also {@link ColorUtils}. - * - * see {@link https://github.com/mrdoob/three.js/blob/master/src/math/Color.js|src/math/Color.js} - * - * @example - * const color = new THREE.Color( 0xff0000 ); - */ -export class Color { - constructor(color?: ColorRepresentation); - constructor(r: number, g: number, b: number); - - readonly isColor: true; - - /** - * Red channel value between 0 and 1. Default is 1. - * @default 1 - */ - r: number; - - /** - * Green channel value between 0 and 1. Default is 1. - * @default 1 - */ - g: number; - - /** - * Blue channel value between 0 and 1. Default is 1. - * @default 1 - */ - b: number; - - set(...args: [color: ColorRepresentation] | [r: number, g: number, b: number]): this; - - /** - * Sets this color's {@link r}, {@link g} and {@link b} components from the x, y, and z components of the specified - * {@link Vector3 | vector}. - */ - setFromVector3(vector: Vector3): this; - - setScalar(scalar: number): Color; - setHex(hex: number, colorSpace?: ColorSpace): Color; - - /** - * Sets this color from RGB values. - * @param r Red channel value between 0 and 1. - * @param g Green channel value between 0 and 1. - * @param b Blue channel value between 0 and 1. - */ - setRGB(r: number, g: number, b: number, colorSpace?: ColorSpace): Color; - - /** - * Sets this color from HSL values. - * Based on MochiKit implementation by Bob Ippolito. - * - * @param h Hue channel value between 0 and 1. - * @param s Saturation value channel between 0 and 1. - * @param l Value channel value between 0 and 1. - */ - setHSL(h: number, s: number, l: number, colorSpace?: ColorSpace): Color; - - /** - * Sets this color from a CSS context style string. - * @param contextStyle Color in CSS context style format. - */ - setStyle(style: string, colorSpace?: ColorSpace): Color; - - /** - * Sets this color from a color name. - * Faster than {@link Color#setStyle .setStyle()} method if you don't need the other CSS-style formats. - * @param style Color name in X11 format. - */ - setColorName(style: string, colorSpace?: ColorSpace): Color; - - /** - * Clones this color. - */ - clone(): this; - - /** - * Copies given color. - * @param color Color to copy. - */ - copy(color: Color): this; - - /** - * Copies given color making conversion from sRGB to linear space. - * @param color Color to copy. - */ - copySRGBToLinear(color: Color): Color; - - /** - * Copies given color making conversion from linear to sRGB space. - * @param color Color to copy. - */ - copyLinearToSRGB(color: Color): Color; - - /** - * Converts this color from sRGB to linear space. - */ - convertSRGBToLinear(): Color; - - /** - * Converts this color from linear to sRGB space. - */ - convertLinearToSRGB(): Color; - - /** - * Returns the hexadecimal value of this color. - */ - getHex(colorSpace?: ColorSpace): number; - - /** - * Returns the string formated hexadecimal value of this color. - */ - getHexString(colorSpace?: ColorSpace): string; - - getHSL(target: HSL, colorSpace?: ColorSpace): HSL; - - getRGB(target: RGB, colorSpace?: ColorSpace): RGB; - - /** - * Returns the value of this color in CSS context style. - * Example: rgb(r, g, b) - */ - getStyle(colorSpace?: ColorSpace): string; - - offsetHSL(h: number, s: number, l: number): this; - - add(color: Color): this; - addColors(color1: Color, color2: Color): this; - addScalar(s: number): this; - - /** - * Applies the transform {@link Matrix3 | m} to this color's RGB components. - */ - applyMatrix3(m: Matrix3): this; - - sub(color: Color): this; - multiply(color: Color): this; - multiplyScalar(s: number): this; - lerp(color: Color, alpha: number): this; - lerpColors(color1: Color, color2: Color, alpha: number): this; - lerpHSL(color: Color, alpha: number): this; - equals(color: Color): boolean; - - /** - * Sets this color's red, green and blue value from the provided array or array-like. - * @param array the source array or array-like. - * @param offset (optional) offset into the array-like. Default is 0. - */ - fromArray(array: number[] | ArrayLike, offset?: number): this; - - /** - * Returns an array [red, green, blue], or copies red, green and blue into the provided array. - * @param array (optional) array to store the color to. If this is not provided, a new array will be created. - * @param offset (optional) optional offset into the array. - * @return The created or provided array. - */ - toArray(array?: number[], offset?: number): number[]; - - /** - * Copies red, green and blue into the provided array-like. - * @param array array-like to store the color to. - * @param offset (optional) optional offset into the array-like. - * @return The provided array-like. - */ - toArray(xyz: ArrayLike, offset?: number): ArrayLike; - - /** - * This method defines the serialization result of Color. - * @return The color as a hexadecimal value. - */ - toJSON(): number; - - fromBufferAttribute(attribute: BufferAttribute | InterleavedBufferAttribute, index: number): this; - - [Symbol.iterator](): Generator; - - /** - * List of X11 color names. - */ - static NAMES: typeof _colorKeywords; -} diff --git a/src-testing/src/math/ColorManagement.d.ts b/src-testing/src/math/ColorManagement.d.ts deleted file mode 100644 index bbe952882..000000000 --- a/src-testing/src/math/ColorManagement.d.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { - ColorSpace, - ColorSpacePrimaries, - ColorSpaceTransfer, - DisplayP3ColorSpace, - LinearDisplayP3ColorSpace, - LinearSRGBColorSpace, - SRGBColorSpace, -} from "../constants.js"; -import { Color } from "./Color.js"; -import { Vector3 } from "./Vector3.js"; - -export type WorkingColorSpace = typeof LinearSRGBColorSpace | typeof LinearDisplayP3ColorSpace; -export type DefinedColorSpace = - | typeof LinearSRGBColorSpace - | typeof SRGBColorSpace - | typeof LinearDisplayP3ColorSpace - | typeof DisplayP3ColorSpace; - -export interface ColorManagement { - /** - * @default false - */ - enabled: boolean; - - /** - * @default LinearSRGBColorSpace - */ - get workingColorSpace(): WorkingColorSpace; - set workingColorSpace(colorSpace: WorkingColorSpace); - - convert: (color: Color, sourceColorSpace: DefinedColorSpace, targetColorSpace: DefinedColorSpace) => Color; - - fromWorkingColorSpace: (color: Color, targetColorSpace: DefinedColorSpace) => Color; - - toWorkingColorSpace: (color: Color, sourceColorSpace: DefinedColorSpace) => Color; - - getPrimaries: (colorSpace: DefinedColorSpace) => ColorSpacePrimaries; - - getTransfer: (colorSpace: ColorSpace) => ColorSpaceTransfer; - - getLuminanceCoefficients: (target: Vector3, colorSpace?: ColorSpace) => Vector3; -} - -export const ColorManagement: ColorManagement; - -export function SRGBToLinear(c: number): number; - -export function LinearToSRGB(c: number): number; diff --git a/src-testing/src/math/Cylindrical.d.ts b/src-testing/src/math/Cylindrical.d.ts deleted file mode 100644 index 6764f8154..000000000 --- a/src-testing/src/math/Cylindrical.d.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Vector3 } from "./Vector3.js"; - -export class Cylindrical { - constructor(radius?: number, theta?: number, y?: number); - - /** - * @default 1 - */ - radius: number; - - /** - * @default 0 - */ - theta: number; - - /** - * @default 0 - */ - y: number; - - clone(): this; - copy(other: Cylindrical): this; - set(radius: number, theta: number, y: number): this; - setFromVector3(vec3: Vector3): this; - setFromCartesianCoords(x: number, y: number, z: number): this; -} diff --git a/src-testing/src/math/Euler.d.ts b/src-testing/src/math/Euler.d.ts deleted file mode 100644 index 81be13d82..000000000 --- a/src-testing/src/math/Euler.d.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { Matrix4 } from "./Matrix4.js"; -import { Quaternion } from "./Quaternion.js"; -import { Vector3 } from "./Vector3.js"; - -export type EulerOrder = "XYZ" | "YXZ" | "ZXY" | "ZYX" | "YZX" | "XZY"; - -export type EulerTuple = [x: number, y: number, z: number, order?: EulerOrder]; - -export class Euler { - constructor(x?: number, y?: number, z?: number, order?: EulerOrder); - - /** - * @default 0 - */ - x: number; - - /** - * @default 0 - */ - y: number; - - /** - * @default 0 - */ - z: number; - - /** - * @default THREE.Euler.DEFAULT_ORDER - */ - order: EulerOrder; - readonly isEuler: true; - - _onChangeCallback: () => void; - - set(x: number, y: number, z: number, order?: EulerOrder): Euler; - clone(): this; - copy(euler: Euler): this; - setFromRotationMatrix(m: Matrix4, order?: EulerOrder, update?: boolean): Euler; - setFromQuaternion(q: Quaternion, order?: EulerOrder, update?: boolean): Euler; - setFromVector3(v: Vector3, order?: EulerOrder): Euler; - reorder(newOrder: EulerOrder): Euler; - equals(euler: Euler): boolean; - fromArray(array: EulerTuple): Euler; - toArray(array?: Partial, offset?: number): EulerTuple; - _onChange(callback: () => void): this; - - static DEFAULT_ORDER: "XYZ"; - - [Symbol.iterator](): Generator; -} diff --git a/src-testing/src/math/Frustum.d.ts b/src-testing/src/math/Frustum.d.ts deleted file mode 100644 index 364c8e926..000000000 --- a/src-testing/src/math/Frustum.d.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { CoordinateSystem } from "../constants.js"; -import { Object3D } from "../core/Object3D.js"; -import { Sprite } from "../objects/Sprite.js"; -import { Box3 } from "./Box3.js"; -import { Matrix4 } from "./Matrix4.js"; -import { Plane } from "./Plane.js"; -import { Sphere } from "./Sphere.js"; -import { Vector3 } from "./Vector3.js"; - -/** - * Frustums are used to determine what is inside the camera's field of view. They help speed up the rendering process. - */ -export class Frustum { - constructor(p0?: Plane, p1?: Plane, p2?: Plane, p3?: Plane, p4?: Plane, p5?: Plane); - - /** - * Array of 6 vectors. - */ - planes: Plane[]; - - set(p0: Plane, p1: Plane, p2: Plane, p3: Plane, p4: Plane, p5: Plane): Frustum; - clone(): this; - copy(frustum: Frustum): this; - setFromProjectionMatrix(m: Matrix4, coordinateSystem?: CoordinateSystem): this; - intersectsObject(object: Object3D): boolean; - intersectsSprite(sprite: Sprite): boolean; - intersectsSphere(sphere: Sphere): boolean; - intersectsBox(box: Box3): boolean; - containsPoint(point: Vector3): boolean; -} diff --git a/src-testing/src/math/Interpolant.d.ts b/src-testing/src/math/Interpolant.d.ts deleted file mode 100644 index 9d2b1aced..000000000 --- a/src-testing/src/math/Interpolant.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -export abstract class Interpolant { - constructor(parameterPositions: any, sampleValues: any, sampleSize: number, resultBuffer?: any); - - parameterPositions: any; - sampleValues: any; - valueSize: number; - resultBuffer: any; - - evaluate(time: number): any; -} diff --git a/src-testing/src/math/Line3.d.ts b/src-testing/src/math/Line3.d.ts deleted file mode 100644 index ab7e749bc..000000000 --- a/src-testing/src/math/Line3.d.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { Matrix4 } from "./Matrix4.js"; -import { Vector3 } from "./Vector3.js"; - -export class Line3 { - constructor(start?: Vector3, end?: Vector3); - - /** - * @default new THREE.Vector3() - */ - start: Vector3; - - /** - * @default new THREE.Vector3() - */ - end: Vector3; - - set(start?: Vector3, end?: Vector3): Line3; - clone(): this; - copy(line: Line3): this; - getCenter(target: Vector3): Vector3; - delta(target: Vector3): Vector3; - distanceSq(): number; - distance(): number; - at(t: number, target: Vector3): Vector3; - closestPointToPointParameter(point: Vector3, clampToLine?: boolean): number; - closestPointToPoint(point: Vector3, clampToLine: boolean, target: Vector3): Vector3; - applyMatrix4(matrix: Matrix4): Line3; - equals(line: Line3): boolean; -} diff --git a/src-testing/src/math/MathUtils.d.ts b/src-testing/src/math/MathUtils.d.ts deleted file mode 100644 index 48f10a3c3..000000000 --- a/src-testing/src/math/MathUtils.d.ts +++ /dev/null @@ -1,137 +0,0 @@ -import { Quaternion } from "./Quaternion.js"; - -/** - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/math/MathUtils.js|src/math/MathUtils.js} - */ - -export const DEG2RAD: number; - -export const RAD2DEG: number; - -export function generateUUID(): string; - -/** - * Clamps the x to be between a and b. - * - * @param value Value to be clamped. - * @param min Minimum value. - * @param max Maximum value. - */ -export function clamp(value: number, min: number, max: number): number; - -export function euclideanModulo(n: number, m: number): number; - -/** - * Linear mapping of x from range [a1, a2] to range [b1, b2]. - * - * @param x Value to be mapped. - * @param a1 Minimum value for range A. - * @param a2 Maximum value for range A. - * @param b1 Minimum value for range B. - * @param b2 Maximum value for range B. - */ -export function mapLinear(x: number, a1: number, a2: number, b1: number, b2: number): number; - -export function inverseLerp(x: number, y: number, t: number): number; - -/** - * Returns a value linearly interpolated from two known points based - * on the given interval - t = 0 will return x and t = 1 will return y. - * - * @param x Start point. - * @param y End point. - * @param t interpolation factor in the closed interval [0, 1] - */ -export function lerp(x: number, y: number, t: number): number; - -/** - * Smoothly interpolate a number from x toward y in a spring-like - * manner using the dt to maintain frame rate independent movement. - * - * @param x Current point. - * @param y Target point. - * @param lambda A higher lambda value will make the movement more sudden, and a lower value will make the movement more gradual. - * @param dt Delta time in seconds. - */ -export function damp(x: number, y: number, lambda: number, dt: number): number; - -/** - * Returns a value that alternates between 0 and length. - * - * @param x The value to pingpong. - * @param length The positive value the export function will pingpong to. Default is 1. - */ -export function pingpong(x: number, length?: number): number; - -export function smoothstep(x: number, min: number, max: number): number; - -export function smootherstep(x: number, min: number, max: number): number; - -/** - * Random integer from low to high interval. - */ -export function randInt(low: number, high: number): number; - -/** - * Random float from low to high interval. - */ -export function randFloat(low: number, high: number): number; - -/** - * Random float from - range / 2 to range / 2 interval. - */ -export function randFloatSpread(range: number): number; - -/** - * Deterministic pseudo-random float in the interval [ 0, 1 ]. - */ -export function seededRandom(seed?: number): number; - -export function degToRad(degrees: number): number; - -export function radToDeg(radians: number): number; - -export function isPowerOfTwo(value: number): boolean; - -export function ceilPowerOfTwo(value: number): number; - -export function floorPowerOfTwo(value: number): number; - -export function setQuaternionFromProperEuler(q: Quaternion, a: number, b: number, c: number, order: string): void; - -export function denormalize( - value: number, - array: Float32Array | Uint32Array | Uint16Array | Uint8Array | Int32Array | Int16Array | Int8Array, -): number; - -export function normalize( - value: number, - array: Float32Array | Uint32Array | Uint16Array | Uint8Array | Int32Array | Int16Array | Int8Array, -): number; - -export const MathUtils: { - DEG2RAD: typeof DEG2RAD; - RAD2DEG: typeof RAD2DEG; - generateUUID: typeof generateUUID; - clamp: typeof clamp; - euclideanModulo: typeof euclideanModulo; - mapLinear: typeof mapLinear; - inverseLerp: typeof inverseLerp; - lerp: typeof lerp; - damp: typeof damp; - pingpong: typeof pingpong; - smoothstep: typeof smoothstep; - smootherstep: typeof smootherstep; - randInt: typeof randInt; - randFloat: typeof randFloat; - randFloatSpread: typeof randFloatSpread; - seededRandom: typeof seededRandom; - degToRad: typeof degToRad; - radToDeg: typeof radToDeg; - isPowerOfTwo: typeof isPowerOfTwo; - ceilPowerOfTwo: typeof ceilPowerOfTwo; - floorPowerOfTwo: typeof floorPowerOfTwo; - setQuaternionFromProperEuler: typeof setQuaternionFromProperEuler; - normalize: typeof normalize; - denormalize: typeof denormalize; -}; diff --git a/src-testing/src/math/Matrix2.d.ts b/src-testing/src/math/Matrix2.d.ts deleted file mode 100644 index 40ed4c8aa..000000000 --- a/src-testing/src/math/Matrix2.d.ts +++ /dev/null @@ -1,53 +0,0 @@ -export type Matrix2Tuple = [ - n11: number, - n12: number, - n21: number, - n22: number, -]; - -/** - * A class representing a 2x2 {@link https://en.wikipedia.org/wiki/Matrix_(mathematics) matrix}. - * - * @example - * const m = new Matrix2(); - */ -export class Matrix2 { - readonly isMatrix2: true; - - /** - * A {@link https://en.wikipedia.org/wiki/Row-_and_column-major_order column-major} list of matrix values. - */ - elements: Matrix2Tuple; - - /** - * Creates a 2x2 {@link https://en.wikipedia.org/wiki/Identity_matrix identity matrix}. - */ - constructor(); - - /** - * Creates a 2x2 matrix with the given arguments in row-major order. - */ - constructor(n11: number, n12: number, n21: number, n22: number); - - /** - * Resets this matrix to the 2x2 identity matrix: - */ - identity(): this; - - /** - * Sets the elements of this matrix based on an array in - * {@link https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major} format. - * - * @param array the array to read the elements from - * @param offset (optional) index of first element in the array. Default is `0`. - */ - fromArray(array: ArrayLike, offset?: number): this; - - /** - * Sets the 2x2 matrix values to the given - * {@link https://en.wikipedia.org/wiki/Row-_and_column-major_order row-major} sequence of values: - * [n11, n12, - * n21, n22] - */ - set(n11: number, n12: number, n21: number, n22: number): this; -} diff --git a/src-testing/src/math/Matrix3.d.ts b/src-testing/src/math/Matrix3.d.ts deleted file mode 100644 index 0b593fcb8..000000000 --- a/src-testing/src/math/Matrix3.d.ts +++ /dev/null @@ -1,184 +0,0 @@ -// https://threejs.org/docs/#api/en/math/Matrix3 - -import { Matrix4 } from "./Matrix4.js"; -import { Vector2 } from "./Vector2.js"; -import { Vector3 } from "./Vector3.js"; - -export type Matrix3Tuple = [ - n11: number, - n12: number, - n13: number, - n21: number, - n22: number, - n23: number, - n31: number, - n32: number, - n33: number, -]; - -export class Matrix3 { - readonly isMatrix3: true; - - /** - * Array with matrix values. - * @default [1, 0, 0, 0, 1, 0, 0, 0, 1] - */ - elements: Matrix3Tuple; - - /** - * Creates an identity matrix. - */ - constructor(); - /** - * Creates a 3x3 matrix with the given arguments in row-major order. - */ - constructor( - n11: number, - n12: number, - n13: number, - n21: number, - n22: number, - n23: number, - n31: number, - n32: number, - n33: number, - ); - - set( - n11: number, - n12: number, - n13: number, - n21: number, - n22: number, - n23: number, - n31: number, - n32: number, - n33: number, - ): Matrix3; - - identity(): this; - - copy(m: Matrix3): this; - - extractBasis(xAxis: Vector3, yAxis: Vector3, zAxis: Vector3): this; - - setFromMatrix4(m: Matrix4): Matrix3; - - /** - * Multiplies this matrix by m. - */ - multiply(m: Matrix3): this; - - premultiply(m: Matrix3): this; - - /** - * Sets this matrix to a x b. - */ - multiplyMatrices(a: Matrix3, b: Matrix3): this; - - multiplyScalar(s: number): this; - - determinant(): number; - - /** - * Inverts this matrix in place. - */ - invert(): this; - - /** - * Transposes this matrix in place. - */ - transpose(): this; - - getNormalMatrix(matrix4: Matrix4): this; - - /** - * Transposes this matrix into the supplied array r, and returns itself. - */ - transposeIntoArray(r: number[]): this; - - setUvTransform(tx: number, ty: number, sx: number, sy: number, rotation: number, cx: number, cy: number): this; - - scale(sx: number, sy: number): this; - - rotate(theta: number): this; - - translate(tx: number, ty: number): this; - - /** - * Sets this matrix as a 2D translation transform: - * - * ``` - * 1, 0, x, - * 0, 1, y, - * 0, 0, 1 - * ``` - * - * @param v the amount to translate. - */ - makeTranslation(v: Vector2): this; - /** - * Sets this matrix as a 2D translation transform: - * - * ``` - * 1, 0, x, - * 0, 1, y, - * 0, 0, 1 - * ``` - * - * @param x the amount to translate in the X axis. - * @param y the amount to translate in the Y axis. - */ - makeTranslation(x: number, y: number): this; - - /** - * Sets this matrix as a 2D rotational transformation by theta radians. The resulting matrix will be: - * - * ``` - * cos(θ) -sin(θ) 0 - * sin(θ) cos(θ) 0 - * 0 0 1 - * ``` - * - * @param theta Rotation angle in radians. Positive values rotate counterclockwise. - */ - makeRotation(theta: number): this; - - /** - * Sets this matrix as a 2D scale transform: - * - * ``` - * x, 0, 0, - * 0, y, 0, - * 0, 0, 1 - * ``` - * - * @param x the amount to scale in the X axis. - * @param y the amount to scale in the Y axis. - */ - makeScale(x: number, y: number): this; - - equals(matrix: Matrix3): boolean; - - /** - * Sets the values of this matrix from the provided array or array-like. - * @param array the source array or array-like. - * @param offset (optional) offset into the array-like. Default is 0. - */ - fromArray(array: ArrayLike, offset?: number): this; - - /** - * Writes the elements of this matrix to an array in - * {@link https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major} format. - */ - toArray(): Matrix3Tuple; - /** - * Writes the elements of this matrix to an array in - * {@link https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major} format. - * @param array array to store the resulting vector in. If not given a new array will be created. - * @param offset (optional) offset in the array at which to put the result. - */ - toArray>(array: TArray, offset?: number): TArray; - - clone(): this; -} diff --git a/src-testing/src/math/Matrix4.d.ts b/src-testing/src/math/Matrix4.d.ts deleted file mode 100644 index 9ef2a9ceb..000000000 --- a/src-testing/src/math/Matrix4.d.ts +++ /dev/null @@ -1,284 +0,0 @@ -import { CoordinateSystem } from "../constants.js"; -import { Euler } from "./Euler.js"; -import { Matrix3 } from "./Matrix3.js"; -import { Quaternion } from "./Quaternion.js"; -import { Vector3 } from "./Vector3.js"; - -export type Matrix4Tuple = [ - n11: number, - n12: number, - n13: number, - n14: number, - n21: number, - n22: number, - n23: number, - n24: number, - n31: number, - n32: number, - n33: number, - n34: number, - n41: number, - n42: number, - n43: number, - n44: number, -]; - -/** - * A 4x4 Matrix. - * - * @example - * // Simple rig for rotating around 3 axes - * const m = new THREE.Matrix4(); - * const m1 = new THREE.Matrix4(); - * const m2 = new THREE.Matrix4(); - * const m3 = new THREE.Matrix4(); - * const alpha = 0; - * const beta = Math.PI; - * const gamma = Math.PI/2; - * m1.makeRotationX( alpha ); - * m2.makeRotationY( beta ); - * m3.makeRotationZ( gamma ); - * m.multiplyMatrices( m1, m2 ); - * m.multiply( m3 ); - */ -export class Matrix4 { - readonly isMatrix4: true; - - /** - * Array with matrix values. - * @default [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] - */ - elements: Matrix4Tuple; - - /** - * Creates an identity matrix. - */ - constructor(); - /** - * Creates a 4x4 matrix with the given arguments in row-major order. - */ - constructor( - n11: number, - n12: number, - n13: number, - n14: number, - n21: number, - n22: number, - n23: number, - n24: number, - n31: number, - n32: number, - n33: number, - n34: number, - n41: number, - n42: number, - n43: number, - n44: number, - ); - - /** - * Sets all fields of this matrix. - */ - set( - n11: number, - n12: number, - n13: number, - n14: number, - n21: number, - n22: number, - n23: number, - n24: number, - n31: number, - n32: number, - n33: number, - n34: number, - n41: number, - n42: number, - n43: number, - n44: number, - ): this; - - /** - * Resets this matrix to identity. - */ - identity(): this; - - clone(): Matrix4; - - copy(m: Matrix4): this; - - copyPosition(m: Matrix4): this; - - /** - * Set the upper 3x3 elements of this matrix to the values of the Matrix3 m. - */ - setFromMatrix3(m: Matrix3): this; - - extractBasis(xAxis: Vector3, yAxis: Vector3, zAxis: Vector3): this; - - makeBasis(xAxis: Vector3, yAxis: Vector3, zAxis: Vector3): this; - - /** - * Copies the rotation component of the supplied matrix m into this matrix rotation component. - */ - extractRotation(m: Matrix4): this; - - makeRotationFromEuler(euler: Euler): this; - - makeRotationFromQuaternion(q: Quaternion): this; - - /** - * Constructs a rotation matrix, looking from eye towards center with defined up vector. - */ - lookAt(eye: Vector3, target: Vector3, up: Vector3): this; - - /** - * Multiplies this matrix by m. - */ - multiply(m: Matrix4): this; - - premultiply(m: Matrix4): this; - - /** - * Sets this matrix to a x b. - */ - multiplyMatrices(a: Matrix4, b: Matrix4): this; - - /** - * Multiplies this matrix by s. - */ - multiplyScalar(s: number): this; - - /** - * Computes determinant of this matrix. - * Based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm - */ - determinant(): number; - - /** - * Transposes this matrix. - */ - transpose(): this; - - /** - * Sets the position component for this matrix from vector v. - */ - setPosition(v: Vector3): this; - setPosition(x: number, y: number, z: number): this; - - /** - * Inverts this matrix. - */ - invert(): this; - - /** - * Multiplies the columns of this matrix by vector v. - */ - scale(v: Vector3): this; - - getMaxScaleOnAxis(): number; - - /** - * Sets this matrix as translation transform. - */ - makeTranslation(v: Vector3): this; - makeTranslation(x: number, y: number, z: number): this; - - /** - * Sets this matrix as rotation transform around x axis by theta radians. - * - * @param theta Rotation angle in radians. - */ - makeRotationX(theta: number): this; - - /** - * Sets this matrix as rotation transform around y axis by theta radians. - * - * @param theta Rotation angle in radians. - */ - makeRotationY(theta: number): this; - - /** - * Sets this matrix as rotation transform around z axis by theta radians. - * - * @param theta Rotation angle in radians. - */ - makeRotationZ(theta: number): this; - - /** - * Sets this matrix as rotation transform around axis by angle radians. - * Based on http://www.gamedev.net/reference/articles/article1199.asp. - * - * @param axis Rotation axis. - * @param angle Rotation angle in radians. - */ - makeRotationAxis(axis: Vector3, angle: number): this; - - /** - * Sets this matrix as scale transform. - */ - makeScale(x: number, y: number, z: number): this; - - /** - * Sets this matrix as shear transform. - */ - makeShear(xy: number, xz: number, yx: number, yz: number, zx: number, zy: number): this; - - /** - * Sets this matrix to the transformation composed of translation, rotation and scale. - */ - compose(position: Vector3, quaternion: Quaternion, scale: Vector3): this; - - /** - * Decomposes this matrix into it's position, quaternion and scale components. - */ - decompose(position: Vector3, quaternion: Quaternion, scale: Vector3): this; - - /** - * Creates a perspective projection matrix. - */ - makePerspective( - left: number, - right: number, - top: number, - bottom: number, - near: number, - far: number, - coordinateSystem?: CoordinateSystem, - ): this; - - /** - * Creates an orthographic projection matrix. - */ - makeOrthographic( - left: number, - right: number, - top: number, - bottom: number, - near: number, - far: number, - coordinateSystem?: CoordinateSystem, - ): this; - - equals(matrix: Matrix4): boolean; - - /** - * Sets the values of this matrix from the provided array or array-like. - * @param array the source array or array-like. - * @param offset (optional) offset into the array-like. Default is 0. - */ - fromArray(array: ArrayLike, offset?: number): this; - - /** - * Writes the elements of this matrix to an array in - * {@link https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major} format. - */ - toArray(): Matrix4Tuple; - /** - * Writes the elements of this matrix to an array in - * {@link https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major} format. - * @param array array to store the resulting vector in. - * @param offset (optional) offset in the array at which to put the result. - */ - toArray>(array: TArray, offset?: number): TArray; -} diff --git a/src-testing/src/math/Plane.d.ts b/src-testing/src/math/Plane.d.ts deleted file mode 100644 index 59fa23912..000000000 --- a/src-testing/src/math/Plane.d.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { Box3 } from "./Box3.js"; -import { Line3 } from "./Line3.js"; -import { Matrix3 } from "./Matrix3.js"; -import { Matrix4 } from "./Matrix4.js"; -import { Sphere } from "./Sphere.js"; -import { Vector3 } from "./Vector3.js"; - -export class Plane { - constructor(normal?: Vector3, constant?: number); - - /** - * @default new THREE.Vector3( 1, 0, 0 ) - */ - normal: Vector3; - - /** - * @default 0 - */ - constant: number; - - readonly isPlane: true; - - set(normal: Vector3, constant: number): Plane; - setComponents(x: number, y: number, z: number, w: number): Plane; - setFromNormalAndCoplanarPoint(normal: Vector3, point: Vector3): Plane; - setFromCoplanarPoints(a: Vector3, b: Vector3, c: Vector3): Plane; - clone(): this; - copy(plane: Plane): this; - normalize(): Plane; - negate(): Plane; - distanceToPoint(point: Vector3): number; - distanceToSphere(sphere: Sphere): number; - projectPoint(point: Vector3, target: Vector3): Vector3; - intersectLine(line: Line3, target: Vector3): Vector3 | null; - intersectsLine(line: Line3): boolean; - intersectsBox(box: Box3): boolean; - intersectsSphere(sphere: Sphere): boolean; - coplanarPoint(target: Vector3): Vector3; - applyMatrix4(matrix: Matrix4, optionalNormalMatrix?: Matrix3): Plane; - translate(offset: Vector3): Plane; - equals(plane: Plane): boolean; - - /** - * @deprecated Use {@link Plane#intersectsLine .intersectsLine()} instead. - */ - isIntersectionLine(l: any): any; -} diff --git a/src-testing/src/math/Quaternion.d.ts b/src-testing/src/math/Quaternion.d.ts deleted file mode 100644 index c8adc8f11..000000000 --- a/src-testing/src/math/Quaternion.d.ts +++ /dev/null @@ -1,188 +0,0 @@ -import { BufferAttribute } from "../core/BufferAttribute.js"; -import { InterleavedBufferAttribute } from "../core/InterleavedBufferAttribute.js"; -import { Euler } from "./Euler.js"; -import { Matrix4 } from "./Matrix4.js"; -import { Vector3, Vector3Like } from "./Vector3.js"; - -export interface QuaternionLike { - readonly x: number; - readonly y: number; - readonly z: number; - readonly w: number; -} - -export type QuaternionTuple = [x: number, y: number, z: number, w: number]; - -/** - * Implementation of a quaternion. This is used for rotating things without incurring in the dreaded gimbal lock issue, amongst other advantages. - * - * @example - * const quaternion = new THREE.Quaternion(); - * quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 ); - * const vector = new THREE.Vector3( 1, 0, 0 ); - * vector.applyQuaternion( quaternion ); - */ -export class Quaternion { - /** - * @param x x coordinate - * @param y y coordinate - * @param z z coordinate - * @param w w coordinate - */ - constructor(x?: number, y?: number, z?: number, w?: number); - - /** - * @default 0 - */ - x: number; - - /** - * @default 0 - */ - y: number; - - /** - * @default 0 - */ - z: number; - - /** - * @default 1 - */ - w: number; - readonly isQuaternion: true; - - /** - * Sets values of this quaternion. - */ - set(x: number, y: number, z: number, w: number): this; - - /** - * Clones this quaternion. - */ - clone(): this; - - /** - * Copies values of q to this quaternion. - */ - copy(q: QuaternionLike): this; - - /** - * Sets this quaternion from rotation specified by Euler angles. - */ - setFromEuler(euler: Euler, update?: boolean): this; - - /** - * Sets this quaternion from rotation specified by axis and angle. - * Adapted from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm. - * Axis have to be normalized, angle is in radians. - */ - setFromAxisAngle(axis: Vector3Like, angle: number): this; - - /** - * Sets this quaternion from rotation component of m. Adapted from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm. - */ - setFromRotationMatrix(m: Matrix4): this; - setFromUnitVectors(vFrom: Vector3, vTo: Vector3Like): this; - angleTo(q: Quaternion): number; - rotateTowards(q: Quaternion, step: number): this; - - identity(): this; - - /** - * Inverts this quaternion. - */ - invert(): this; - - conjugate(): this; - dot(v: Quaternion): number; - lengthSq(): number; - - /** - * Computes length of this quaternion. - */ - length(): number; - - /** - * Normalizes this quaternion. - */ - normalize(): this; - - /** - * Multiplies this quaternion by b. - */ - multiply(q: Quaternion): this; - premultiply(q: Quaternion): this; - - /** - * Sets this quaternion to a x b - * Adapted from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm. - */ - multiplyQuaternions(a: Quaternion, b: Quaternion): this; - - slerp(qb: Quaternion, t: number): this; - slerpQuaternions(qa: Quaternion, qb: Quaternion, t: number): this; - equals(v: Quaternion): boolean; - - /** - * Sets this quaternion's x, y, z and w value from the provided array or array-like. - * @param array the source array or array-like. - * @param offset (optional) offset into the array. Default is 0. - */ - fromArray(array: QuaternionTuple, offset?: number): this; - - /** - * Returns an array [x, y, z, w], or copies x, y, z and w into the provided array. - * @param array (optional) array to store the quaternion to. If this is not provided, a new array will be created. - * @param offset (optional) optional offset into the array. - * @return The created or provided array. - */ - toArray(array?: number[], offset?: number): QuaternionTuple; - - /** - * Copies x, y, z and w into the provided array-like. - * @param array array-like to store the quaternion to. - * @param offset (optional) optional offset into the array. - * @return The provided array-like. - */ - toArray(array: ArrayLike, offset?: number): QuaternionTuple; - - /** - * This method defines the serialization result of Quaternion. - * @return The numerical elements of this quaternion in an array of format [x, y, z, w]. - */ - toJSON(): [number, number, number, number]; - - /** - * Sets x, y, z, w properties of this quaternion from the attribute. - * @param attribute the source attribute. - * @param index index in the attribute. - */ - fromBufferAttribute(attribute: BufferAttribute | InterleavedBufferAttribute, index: number): this; - - _onChange(callback: () => void): this; - _onChangeCallback: () => void; - - static slerpFlat( - dst: number[], - dstOffset: number, - src0: number[], - srcOffset: number, - src1: number[], - stcOffset1: number, - t: number, - ): void; - - static multiplyQuaternionsFlat( - dst: number[], - dstOffset: number, - src0: number[], - srcOffset: number, - src1: number[], - stcOffset1: number, - ): number[]; - - random(): this; - - [Symbol.iterator](): Generator; -} diff --git a/src-testing/src/math/Ray.d.ts b/src-testing/src/math/Ray.d.ts deleted file mode 100644 index 98b9fd70f..000000000 --- a/src-testing/src/math/Ray.d.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { Box3 } from "./Box3.js"; -import { Matrix4 } from "./Matrix4.js"; -import { Plane } from "./Plane.js"; -import { Sphere } from "./Sphere.js"; -import { Vector3 } from "./Vector3.js"; - -export class Ray { - constructor(origin?: Vector3, direction?: Vector3); - - /** - * @default new THREE.Vector3() - */ - origin: Vector3; - - /** - * @default new THREE.Vector3( 0, 0, - 1 ) - */ - direction: Vector3; - - set(origin: Vector3, direction: Vector3): Ray; - clone(): this; - copy(ray: Ray): this; - at(t: number, target: Vector3): Vector3; - lookAt(v: Vector3): Ray; - recast(t: number): Ray; - closestPointToPoint(point: Vector3, target: Vector3): Vector3; - distanceToPoint(point: Vector3): number; - distanceSqToPoint(point: Vector3): number; - distanceSqToSegment( - v0: Vector3, - v1: Vector3, - optionalPointOnRay?: Vector3, - optionalPointOnSegment?: Vector3, - ): number; - intersectSphere(sphere: Sphere, target: Vector3): Vector3 | null; - intersectsSphere(sphere: Sphere): boolean; - distanceToPlane(plane: Plane): number; - intersectPlane(plane: Plane, target: Vector3): Vector3 | null; - intersectsPlane(plane: Plane): boolean; - intersectBox(box: Box3, target: Vector3): Vector3 | null; - intersectsBox(box: Box3): boolean; - intersectTriangle(a: Vector3, b: Vector3, c: Vector3, backfaceCulling: boolean, target: Vector3): Vector3 | null; - applyMatrix4(matrix4: Matrix4): Ray; - equals(ray: Ray): boolean; - - /** - * @deprecated Use {@link Ray#intersectsBox .intersectsBox()} instead. - */ - isIntersectionBox(b: any): any; - - /** - * @deprecated Use {@link Ray#intersectsPlane .intersectsPlane()} instead. - */ - isIntersectionPlane(p: any): any; - - /** - * @deprecated Use {@link Ray#intersectsSphere .intersectsSphere()} instead. - */ - isIntersectionSphere(s: any): any; -} diff --git a/src-testing/src/math/Sphere.d.ts b/src-testing/src/math/Sphere.d.ts deleted file mode 100644 index a423a6f97..000000000 --- a/src-testing/src/math/Sphere.d.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { Box3 } from "./Box3.js"; -import { Matrix4 } from "./Matrix4.js"; -import { Plane } from "./Plane.js"; -import { Vector3 } from "./Vector3.js"; - -export class Sphere { - constructor(center?: Vector3, radius?: number); - - /** - * Read-only flag to check if a given object is of type {@link Sphere}. - */ - readonly isSphere: true; - - /** - * @default new Vector3() - */ - center: Vector3; - - /** - * @default 1 - */ - radius: number; - - set(center: Vector3, radius: number): Sphere; - setFromPoints(points: Vector3[], optionalCenter?: Vector3): Sphere; - clone(): this; - copy(sphere: Sphere): this; - expandByPoint(point: Vector3): this; - isEmpty(): boolean; - makeEmpty(): this; - containsPoint(point: Vector3): boolean; - distanceToPoint(point: Vector3): number; - intersectsSphere(sphere: Sphere): boolean; - intersectsBox(box: Box3): boolean; - intersectsPlane(plane: Plane): boolean; - clampPoint(point: Vector3, target: Vector3): Vector3; - getBoundingBox(target: Box3): Box3; - applyMatrix4(matrix: Matrix4): Sphere; - translate(offset: Vector3): Sphere; - equals(sphere: Sphere): boolean; - union(sphere: Sphere): this; - - /** - * @deprecated Use {@link Sphere#isEmpty .isEmpty()} instead. - */ - empty(): any; -} diff --git a/src-testing/src/math/Spherical.d.ts b/src-testing/src/math/Spherical.d.ts deleted file mode 100644 index 8d4815a17..000000000 --- a/src-testing/src/math/Spherical.d.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { Vector3 } from "./Vector3.js"; - -export class Spherical { - constructor(radius?: number, phi?: number, theta?: number); - - /** - * @default 1 - */ - radius: number; - - /** - * @default 0 - */ - phi: number; - - /** - * @default 0 - */ - theta: number; - - set(radius: number, phi: number, theta: number): this; - clone(): this; - copy(other: Spherical): this; - makeSafe(): this; - setFromVector3(v: Vector3): this; - setFromCartesianCoords(x: number, y: number, z: number): this; -} diff --git a/src-testing/src/math/SphericalHarmonics3.d.ts b/src-testing/src/math/SphericalHarmonics3.d.ts deleted file mode 100644 index 5981a0b48..000000000 --- a/src-testing/src/math/SphericalHarmonics3.d.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { Vector3 } from "./Vector3.js"; - -export class SphericalHarmonics3 { - constructor(); - - /** - * @default [new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), - * new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3()] - */ - coefficients: Vector3[]; - readonly isSphericalHarmonics3: true; - - set(coefficients: Vector3[]): SphericalHarmonics3; - zero(): SphericalHarmonics3; - add(sh: SphericalHarmonics3): SphericalHarmonics3; - addScaledSH(sh: SphericalHarmonics3, s: number): SphericalHarmonics3; - scale(s: number): SphericalHarmonics3; - lerp(sh: SphericalHarmonics3, alpha: number): SphericalHarmonics3; - equals(sh: SphericalHarmonics3): boolean; - copy(sh: SphericalHarmonics3): SphericalHarmonics3; - clone(): this; - - /** - * Sets the values of this spherical harmonics from the provided array or array-like. - * @param array the source array or array-like. - * @param offset (optional) offset into the array. Default is 0. - */ - fromArray(array: number[] | ArrayLike, offset?: number): this; - - /** - * Returns an array with the values of this spherical harmonics, or copies them into the provided array. - * @param array (optional) array to store the spherical harmonics to. If this is not provided, a new array will be created. - * @param offset (optional) optional offset into the array. - * @return The created or provided array. - */ - toArray(array?: number[], offset?: number): number[]; - - /** - * Returns an array with the values of this spherical harmonics, or copies them into the provided array-like. - * @param array array-like to store the spherical harmonics to. - * @param offset (optional) optional offset into the array-like. - * @return The provided array-like. - */ - toArray(array: ArrayLike, offset?: number): ArrayLike; - - getAt(normal: Vector3, target: Vector3): Vector3; - getIrradianceAt(normal: Vector3, target: Vector3): Vector3; - - static getBasisAt(normal: Vector3, shBasis: number[]): void; -} diff --git a/src-testing/src/math/Triangle.d.ts b/src-testing/src/math/Triangle.d.ts deleted file mode 100644 index 75f8d5676..000000000 --- a/src-testing/src/math/Triangle.d.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { Box3 } from "./Box3.js"; -import { Plane } from "./Plane.js"; -import { Vector2 } from "./Vector2.js"; -import { Vector3 } from "./Vector3.js"; -import { Vector4 } from "./Vector4.js"; - -import { BufferAttribute } from "../core/BufferAttribute.js"; -import { InterleavedBufferAttribute } from "../core/InterleavedBufferAttribute.js"; - -export class Triangle { - constructor(a?: Vector3, b?: Vector3, c?: Vector3); - - /** - * @default new THREE.Vector3() - */ - a: Vector3; - - /** - * @default new THREE.Vector3() - */ - b: Vector3; - - /** - * @default new THREE.Vector3() - */ - c: Vector3; - - set(a: Vector3, b: Vector3, c: Vector3): Triangle; - setFromPointsAndIndices(points: Vector3[], i0: number, i1: number, i2: number): this; - setFromAttributeAndIndices( - attribute: BufferAttribute | InterleavedBufferAttribute, - i0: number, - i1: number, - i2: number, - ): this; - clone(): this; - copy(triangle: Triangle): this; - getArea(): number; - getMidpoint(target: Vector3): Vector3; - getNormal(target: Vector3): Vector3; - getPlane(target: Plane): Plane; - getBarycoord(point: Vector3, target: Vector3): Vector3 | null; - getInterpolation(point: Vector3, v1: Vector2, v2: Vector2, v3: Vector2, target: Vector2): Vector2 | null; - getInterpolation(point: Vector3, v1: Vector3, v2: Vector3, v3: Vector3, target: Vector3): Vector3 | null; - getInterpolation(point: Vector3, v1: Vector4, v2: Vector4, v3: Vector4, target: Vector4): Vector4 | null; - containsPoint(point: Vector3): boolean; - intersectsBox(box: Box3): boolean; - isFrontFacing(direction: Vector3): boolean; - closestPointToPoint(point: Vector3, target: Vector3): Vector3; - equals(triangle: Triangle): boolean; - - static getNormal(a: Vector3, b: Vector3, c: Vector3, target: Vector3): Vector3; - static getBarycoord(point: Vector3, a: Vector3, b: Vector3, c: Vector3, target: Vector3): Vector3 | null; - static containsPoint(point: Vector3, a: Vector3, b: Vector3, c: Vector3): boolean; - static getInterpolation( - point: Vector3, - p1: Vector3, - p2: Vector3, - p3: Vector3, - v1: Vector2, - v2: Vector2, - v3: Vector2, - target: Vector2, - ): Vector2 | null; - static getInterpolation( - point: Vector3, - p1: Vector3, - p2: Vector3, - p3: Vector3, - v1: Vector3, - v2: Vector3, - v3: Vector3, - target: Vector3, - ): Vector3 | null; - static getInterpolation( - point: Vector3, - p1: Vector3, - p2: Vector3, - p3: Vector3, - v1: Vector4, - v2: Vector4, - v3: Vector4, - target: Vector4, - ): Vector4 | null; - static isFrontFacing(a: Vector3, b: Vector3, c: Vector3, direction: Vector3): boolean; -} diff --git a/src-testing/src/math/Vector2.d.ts b/src-testing/src/math/Vector2.d.ts deleted file mode 100644 index fd2a84a1d..000000000 --- a/src-testing/src/math/Vector2.d.ts +++ /dev/null @@ -1,321 +0,0 @@ -import { BufferAttribute } from "../core/BufferAttribute.js"; -import { Matrix3 } from "./Matrix3.js"; - -export type Vector2Tuple = [x: number, y: number]; - -export interface Vector2Like { - readonly x: number; - readonly y: number; -} - -/** - * 2D vector. - */ -export class Vector2 { - constructor(x?: number, y?: number); - - /** - * @default 0 - */ - x: number; - - /** - * @default 0 - */ - y: number; - width: number; - height: number; - readonly isVector2: true; - - /** - * Sets value of this vector. - */ - set(x: number, y: number): this; - - /** - * Sets the x and y values of this vector both equal to scalar. - */ - setScalar(scalar: number): this; - - /** - * Sets X component of this vector. - */ - setX(x: number): this; - - /** - * Sets Y component of this vector. - */ - setY(y: number): this; - - /** - * Sets a component of this vector. - */ - setComponent(index: number, value: number): this; - - /** - * Gets a component of this vector. - */ - getComponent(index: number): number; - - /** - * Returns a new Vector2 instance with the same `x` and `y` values. - */ - clone(): this; - - /** - * Copies value of v to this vector. - */ - copy(v: Vector2Like): this; - - /** - * Adds v to this vector. - */ - add(v: Vector2Like): this; - - /** - * Adds the scalar value s to this vector's x and y values. - */ - addScalar(s: number): this; - - /** - * Sets this vector to a + b. - */ - addVectors(a: Vector2Like, b: Vector2Like): this; - - /** - * Adds the multiple of v and s to this vector. - */ - addScaledVector(v: Vector2Like, s: number): this; - - /** - * Subtracts v from this vector. - */ - sub(v: Vector2Like): this; - - /** - * Subtracts s from this vector's x and y components. - */ - subScalar(s: number): this; - - /** - * Sets this vector to a - b. - */ - subVectors(a: Vector2Like, b: Vector2Like): this; - - /** - * Multiplies this vector by v. - */ - multiply(v: Vector2Like): this; - - /** - * Multiplies this vector by scalar s. - */ - multiplyScalar(scalar: number): this; - - /** - * Divides this vector by v. - */ - divide(v: Vector2Like): this; - - /** - * Divides this vector by scalar s. - * Set vector to ( 0, 0 ) if s == 0. - */ - divideScalar(s: number): this; - - /** - * Multiplies this vector (with an implicit 1 as the 3rd component) by m. - */ - applyMatrix3(m: Matrix3): this; - - /** - * If this vector's x or y value is greater than v's x or y value, replace that value with the corresponding min value. - */ - min(v: Vector2Like): this; - - /** - * If this vector's x or y value is less than v's x or y value, replace that value with the corresponding max value. - */ - max(v: Vector2Like): this; - - /** - * If this vector's x or y value is greater than the max vector's x or y value, it is replaced by the corresponding value. - * If this vector's x or y value is less than the min vector's x or y value, it is replaced by the corresponding value. - * @param min the minimum x and y values. - * @param max the maximum x and y values in the desired range. - */ - clamp(min: Vector2Like, max: Vector2Like): this; - - /** - * If this vector's x or y values are greater than the max value, they are replaced by the max value. - * If this vector's x or y values are less than the min value, they are replaced by the min value. - * @param min the minimum value the components will be clamped to. - * @param max the maximum value the components will be clamped to. - */ - clampScalar(min: number, max: number): this; - - /** - * If this vector's length is greater than the max value, it is replaced by the max value. - * If this vector's length is less than the min value, it is replaced by the min value. - * @param min the minimum value the length will be clamped to. - * @param max the maximum value the length will be clamped to. - */ - clampLength(min: number, max: number): this; - - /** - * The components of the vector are rounded down to the nearest integer value. - */ - floor(): this; - - /** - * The x and y components of the vector are rounded up to the nearest integer value. - */ - ceil(): this; - - /** - * The components of the vector are rounded to the nearest integer value. - */ - round(): this; - - /** - * The components of the vector are rounded towards zero (up if negative, down if positive) to an integer value. - */ - roundToZero(): this; - - /** - * Inverts this vector. - */ - negate(): this; - - /** - * Computes dot product of this vector and v. - */ - dot(v: Vector2Like): number; - - /** - * Computes cross product of this vector and v. - */ - cross(v: Vector2Like): number; - - /** - * Computes squared length of this vector. - */ - lengthSq(): number; - - /** - * Computes length of this vector. - */ - length(): number; - - /** - * Computes the Manhattan length of this vector. - * - * see {@link http://en.wikipedia.org/wiki/Taxicab_geometry|Wikipedia: Taxicab Geometry} - */ - manhattanLength(): number; - - /** - * Normalizes this vector. - */ - normalize(): this; - - /** - * computes the angle in radians with respect to the positive x-axis - */ - angle(): number; - - /** - * Returns the angle between this vector and vector {@link Vector2 | v} in radians. - */ - angleTo(v: Vector2): number; - - /** - * Computes distance of this vector to v. - */ - distanceTo(v: Vector2Like): number; - - /** - * Computes squared distance of this vector to v. - */ - distanceToSquared(v: Vector2Like): number; - - /** - * Computes the Manhattan length (distance) from this vector to the given vector v - * - * see {@link http://en.wikipedia.org/wiki/Taxicab_geometry|Wikipedia: Taxicab Geometry} - */ - manhattanDistanceTo(v: Vector2Like): number; - - /** - * Normalizes this vector and multiplies it by l. - */ - setLength(length: number): this; - - /** - * Linearly interpolates between this vector and v, where alpha is the distance along the line - alpha = 0 will be this vector, and alpha = 1 will be v. - * @param v vector to interpolate towards. - * @param alpha interpolation factor in the closed interval [0, 1]. - */ - lerp(v: Vector2Like, alpha: number): this; - - /** - * Sets this vector to be the vector linearly interpolated between v1 and v2 where alpha is the distance along the line connecting the two vectors - alpha = 0 will be v1, and alpha = 1 will be v2. - * @param v1 the starting vector. - * @param v2 vector to interpolate towards. - * @param alpha interpolation factor in the closed interval [0, 1]. - */ - lerpVectors(v1: Vector2Like, v2: Vector2Like, alpha: number): this; - - /** - * Checks for strict equality of this vector and v. - */ - equals(v: Vector2Like): boolean; - - /** - * Sets this vector's x and y value from the provided array or array-like. - * @param array the source array or array-like. - * @param offset (optional) offset into the array. Default is 0. - */ - fromArray(array: number[] | ArrayLike, offset?: number): this; - - /** - * Returns an array [x, y], or copies x and y into the provided array. - * @param array (optional) array to store the vector to. If this is not provided, a new array will be created. - * @param offset (optional) optional offset into the array. - * @return The created or provided array. - */ - toArray(array?: number[], offset?: number): number[]; - toArray(array?: Vector2Tuple, offset?: 0): Vector2Tuple; - - /** - * Copies x and y into the provided array-like. - * @param array array-like to store the vector to. - * @param offset (optional) optional offset into the array. - * @return The provided array-like. - */ - toArray(array: ArrayLike, offset?: number): ArrayLike; - - /** - * Sets this vector's x and y values from the attribute. - * @param attribute the source attribute. - * @param index index in the attribute. - */ - fromBufferAttribute(attribute: BufferAttribute, index: number): this; - - /** - * Rotates the vector around center by angle radians. - * @param center the point around which to rotate. - * @param angle the angle to rotate, in radians. - */ - rotateAround(center: Vector2Like, angle: number): this; - - /** - * Sets this vector's x and y from Math.random - */ - random(): this; - - /** - * Iterating through a Vector2 instance will yield its components (x, y) in the corresponding order. - */ - [Symbol.iterator](): Iterator; -} diff --git a/src-testing/src/math/Vector3.d.ts b/src-testing/src/math/Vector3.d.ts deleted file mode 100644 index 56e907ceb..000000000 --- a/src-testing/src/math/Vector3.d.ts +++ /dev/null @@ -1,301 +0,0 @@ -import { Camera } from "../cameras/Camera.js"; -import { BufferAttribute } from "../core/BufferAttribute.js"; -import { InterleavedBufferAttribute } from "../core/InterleavedBufferAttribute.js"; -import { RGB } from "./Color.js"; -import { Cylindrical } from "./Cylindrical.js"; -import { Euler } from "./Euler.js"; -import { Matrix3 } from "./Matrix3.js"; -import { Matrix4 } from "./Matrix4.js"; -import { QuaternionLike } from "./Quaternion.js"; -import { Spherical } from "./Spherical.js"; - -export type Vector3Tuple = [number, number, number]; - -export interface Vector3Like { - readonly x: number; - readonly y: number; - readonly z: number; -} - -/** - * 3D vector. - * - * see {@link https://github.com/mrdoob/three.js/blob/master/src/math/Vector3.js} - * - * @example - * const a = new THREE.Vector3( 1, 0, 0 ); - * const b = new THREE.Vector3( 0, 1, 0 ); - * const c = new THREE.Vector3(); - * c.crossVectors( a, b ); - */ -export class Vector3 { - constructor(x?: number, y?: number, z?: number); - - /** - * @default 0 - */ - x: number; - - /** - * @default 0 - */ - y: number; - - /** - * @default 0 - */ - z: number; - readonly isVector3: true; - - /** - * Sets value of this vector. - */ - set(x: number, y: number, z: number): this; - - /** - * Sets all values of this vector. - */ - setScalar(scalar: number): this; - - /** - * Sets x value of this vector. - */ - setX(x: number): this; - - /** - * Sets y value of this vector. - */ - setY(y: number): this; - - /** - * Sets z value of this vector. - */ - setZ(z: number): this; - - setComponent(index: number, value: number): this; - - getComponent(index: number): number; - - /** - * Clones this vector. - */ - clone(): this; - - /** - * Copies value of v to this vector. - */ - copy(v: Vector3Like): this; - - /** - * Adds v to this vector. - */ - add(v: Vector3Like): this; - - addScalar(s: number): this; - - /** - * Sets this vector to a + b. - */ - addVectors(a: Vector3Like, b: Vector3Like): this; - - addScaledVector(v: Vector3, s: number): this; - - /** - * Subtracts v from this vector. - */ - sub(a: Vector3Like): this; - - subScalar(s: number): this; - - /** - * Sets this vector to a - b. - */ - subVectors(a: Vector3Like, b: Vector3Like): this; - - multiply(v: Vector3Like): this; - - /** - * Multiplies this vector by scalar s. - */ - multiplyScalar(s: number): this; - - multiplyVectors(a: Vector3Like, b: Vector3Like): this; - - applyEuler(euler: Euler): this; - - applyAxisAngle(axis: Vector3, angle: number): this; - - applyMatrix3(m: Matrix3): this; - - applyNormalMatrix(m: Matrix3): this; - - applyMatrix4(m: Matrix4): this; - - applyQuaternion(q: QuaternionLike): this; - - project(camera: Camera): this; - - unproject(camera: Camera): this; - - transformDirection(m: Matrix4): this; - - divide(v: Vector3Like): this; - - /** - * Divides this vector by scalar s. - * Set vector to ( 0, 0, 0 ) if s == 0. - */ - divideScalar(s: number): this; - - min(v: Vector3Like): this; - - max(v: Vector3Like): this; - - clamp(min: Vector3Like, max: Vector3Like): this; - - clampScalar(min: number, max: number): this; - - clampLength(min: number, max: number): this; - - floor(): this; - - ceil(): this; - - round(): this; - - roundToZero(): this; - - /** - * Inverts this vector. - */ - negate(): this; - - /** - * Computes dot product of this vector and v. - */ - dot(v: Vector3Like): number; - - /** - * Computes squared length of this vector. - */ - lengthSq(): number; - - /** - * Computes length of this vector. - */ - length(): number; - - /** - * Computes the Manhattan length of this vector. - * - * see {@link http://en.wikipedia.org/wiki/Taxicab_geometry|Wikipedia: Taxicab Geometry} - */ - manhattanLength(): number; - - /** - * Normalizes this vector. - */ - normalize(): this; - - /** - * Normalizes this vector and multiplies it by l. - */ - setLength(l: number): this; - lerp(v: Vector3Like, alpha: number): this; - - lerpVectors(v1: Vector3Like, v2: Vector3Like, alpha: number): this; - - /** - * Sets this vector to cross product of itself and v. - */ - cross(a: Vector3Like): this; - - /** - * Sets this vector to cross product of a and b. - */ - crossVectors(a: Vector3Like, b: Vector3Like): this; - projectOnVector(v: Vector3): this; - projectOnPlane(planeNormal: Vector3): this; - reflect(vector: Vector3Like): this; - angleTo(v: Vector3): number; - - /** - * Computes distance of this vector to v. - */ - distanceTo(v: Vector3Like): number; - - /** - * Computes squared distance of this vector to v. - */ - distanceToSquared(v: Vector3Like): number; - - /** - * Computes the Manhattan length (distance) from this vector to the given vector v - * - * see {@link http://en.wikipedia.org/wiki/Taxicab_geometry|Wikipedia: Taxicab Geometry} - */ - manhattanDistanceTo(v: Vector3Like): number; - - setFromSpherical(s: Spherical): this; - setFromSphericalCoords(r: number, phi: number, theta: number): this; - setFromCylindrical(s: Cylindrical): this; - setFromCylindricalCoords(radius: number, theta: number, y: number): this; - setFromMatrixPosition(m: Matrix4): this; - setFromMatrixScale(m: Matrix4): this; - setFromMatrixColumn(matrix: Matrix4, index: number): this; - setFromMatrix3Column(matrix: Matrix3, index: number): this; - - /** - * Sets this vector's {@link x}, {@link y} and {@link z} components from the x, y, and z components of the specified {@link Euler Euler Angle}. - */ - setFromEuler(e: Euler): this; - - /** - * Sets this vector's {@link x}, {@link y} and {@link z} components from the r, g, and b components of the specified - * {@link Color | color}. - */ - setFromColor(color: RGB): this; - - /** - * Checks for strict equality of this vector and v. - */ - equals(v: Vector3Like): boolean; - - /** - * Sets this vector's x, y and z value from the provided array or array-like. - * @param array the source array or array-like. - * @param offset (optional) offset into the array. Default is 0. - */ - fromArray(array: number[] | ArrayLike, offset?: number): this; - - /** - * Returns an array [x, y, z], or copies x, y and z into the provided array. - * @param array (optional) array to store the vector to. If this is not provided, a new array will be created. - * @param offset (optional) optional offset into the array. - * @return The created or provided array. - */ - toArray(array?: number[], offset?: number): number[]; - toArray(array?: Vector3Tuple, offset?: 0): Vector3Tuple; - - /** - * Copies x, y and z into the provided array-like. - * @param array array-like to store the vector to. - * @param offset (optional) optional offset into the array-like. - * @return The provided array-like. - */ - toArray(array: ArrayLike, offset?: number): ArrayLike; - - fromBufferAttribute(attribute: BufferAttribute | InterleavedBufferAttribute, index: number): this; - - /** - * Sets this vector's x, y and z from Math.random - */ - random(): this; - - randomDirection(): this; - - /** - * Iterating through a Vector3 instance will yield its components (x, y, z) in the corresponding order. - */ - [Symbol.iterator](): Iterator; -} diff --git a/src-testing/src/math/Vector4.d.ts b/src-testing/src/math/Vector4.d.ts deleted file mode 100644 index 88cf74ff2..000000000 --- a/src-testing/src/math/Vector4.d.ts +++ /dev/null @@ -1,239 +0,0 @@ -import { BufferAttribute } from "../core/BufferAttribute.js"; -import { Matrix4 } from "./Matrix4.js"; -import { QuaternionLike } from "./Quaternion.js"; - -export type Vector4Tuple = [number, number, number, number]; - -export interface Vector4Like { - readonly x: number; - readonly y: number; - readonly z: number; - readonly w: number; -} - -/** - * 4D vector. - */ -export class Vector4 { - constructor(x?: number, y?: number, z?: number, w?: number); - - /** - * @default 0 - */ - x: number; - - /** - * @default 0 - */ - y: number; - - /** - * @default 0 - */ - z: number; - - /** - * @default 0 - */ - w: number; - - width: number; - height: number; - readonly isVector4: true; - - /** - * Sets value of this vector. - */ - set(x: number, y: number, z: number, w: number): this; - - /** - * Sets all values of this vector. - */ - setScalar(scalar: number): this; - - /** - * Sets X component of this vector. - */ - setX(x: number): this; - - /** - * Sets Y component of this vector. - */ - setY(y: number): this; - - /** - * Sets Z component of this vector. - */ - setZ(z: number): this; - - /** - * Sets w component of this vector. - */ - setW(w: number): this; - - setComponent(index: number, value: number): this; - - getComponent(index: number): number; - - /** - * Clones this vector. - */ - clone(): this; - - /** - * Copies value of v to this vector. - */ - copy(v: Vector4Like): this; - - /** - * Adds v to this vector. - */ - add(v: Vector4Like): this; - - addScalar(scalar: number): this; - - /** - * Sets this vector to a + b. - */ - addVectors(a: Vector4Like, b: Vector4Like): this; - - addScaledVector(v: Vector4Like, s: number): this; - /** - * Subtracts v from this vector. - */ - sub(v: Vector4Like): this; - - subScalar(s: number): this; - - /** - * Sets this vector to a - b. - */ - subVectors(a: Vector4Like, b: Vector4Like): this; - - multiply(v: Vector4Like): this; - - /** - * Multiplies this vector by scalar s. - */ - multiplyScalar(s: number): this; - - applyMatrix4(m: Matrix4): this; - - /** - * Divides this vector by scalar s. - * Set vector to ( 0, 0, 0 ) if s == 0. - */ - divideScalar(s: number): this; - - /** - * http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm - * @param q is assumed to be normalized - */ - setAxisAngleFromQuaternion(q: QuaternionLike): this; - - /** - * http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm - * @param m assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) - */ - setAxisAngleFromRotationMatrix(m: Matrix4): this; - - /** - * Sets this vector to the position elements of the - * [transformation matrix]{@link https://en.wikipedia.org/wiki/Transformation_matrix} m. - */ - setFromMatrixPosition(m: Matrix4): this; - - min(v: Vector4Like): this; - max(v: Vector4Like): this; - clamp(min: Vector4Like, max: Vector4Like): this; - clampScalar(min: number, max: number): this; - floor(): this; - ceil(): this; - round(): this; - roundToZero(): this; - - /** - * Inverts this vector. - */ - negate(): this; - - /** - * Computes dot product of this vector and v. - */ - dot(v: Vector4Like): number; - - /** - * Computes squared length of this vector. - */ - lengthSq(): number; - - /** - * Computes length of this vector. - */ - length(): number; - - /** - * Computes the Manhattan length of this vector. - * - * see {@link http://en.wikipedia.org/wiki/Taxicab_geometry|Wikipedia: Taxicab Geometry} - */ - manhattanLength(): number; - - /** - * Normalizes this vector. - */ - normalize(): this; - - /** - * Normalizes this vector and multiplies it by l. - */ - setLength(length: number): this; - - /** - * Linearly interpolate between this vector and v with alpha factor. - */ - lerp(v: Vector4Like, alpha: number): this; - - lerpVectors(v1: Vector4Like, v2: Vector4Like, alpha: number): this; - - /** - * Checks for strict equality of this vector and v. - */ - equals(v: Vector4Like): boolean; - - /** - * Sets this vector's x, y, z and w value from the provided array or array-like. - * @param array the source array or array-like. - * @param offset (optional) offset into the array. Default is 0. - */ - fromArray(array: number[] | ArrayLike, offset?: number): this; - - /** - * Returns an array [x, y, z, w], or copies x, y, z and w into the provided array. - * @param array (optional) array to store the vector to. If this is not provided, a new array will be created. - * @param offset (optional) optional offset into the array. - * @return The created or provided array. - */ - toArray(array?: number[], offset?: number): number[]; - toArray(array?: Vector4Tuple, offset?: 0): Vector4Tuple; - - /** - * Copies x, y, z and w into the provided array-like. - * @param array array-like to store the vector to. - * @param offset (optional) optional offset into the array-like. - * @return The provided array-like. - */ - toArray(array: ArrayLike, offset?: number): ArrayLike; - - fromBufferAttribute(attribute: BufferAttribute, index: number): this; - - /** - * Sets this vector's x, y, z and w from Math.random - */ - random(): this; - - /** - * Iterating through a Vector4 instance will yield its components (x, y, z, w) in the corresponding order. - */ - [Symbol.iterator](): Iterator; -} diff --git a/src-testing/src/math/interpolants/CubicInterpolant.d.ts b/src-testing/src/math/interpolants/CubicInterpolant.d.ts deleted file mode 100644 index 282b98d7e..000000000 --- a/src-testing/src/math/interpolants/CubicInterpolant.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Interpolant } from "../Interpolant.js"; - -export class CubicInterpolant extends Interpolant { - constructor(parameterPositions: any, samplesValues: any, sampleSize: number, resultBuffer?: any); - - interpolate_(i1: number, t0: number, t: number, t1: number): any; -} diff --git a/src-testing/src/math/interpolants/DiscreteInterpolant.d.ts b/src-testing/src/math/interpolants/DiscreteInterpolant.d.ts deleted file mode 100644 index 28bd458b8..000000000 --- a/src-testing/src/math/interpolants/DiscreteInterpolant.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Interpolant } from "../Interpolant.js"; - -export class DiscreteInterpolant extends Interpolant { - constructor(parameterPositions: any, samplesValues: any, sampleSize: number, resultBuffer?: any); - - interpolate_(i1: number, t0: number, t: number, t1: number): any; -} diff --git a/src-testing/src/math/interpolants/LinearInterpolant.d.ts b/src-testing/src/math/interpolants/LinearInterpolant.d.ts deleted file mode 100644 index e6ff11c0b..000000000 --- a/src-testing/src/math/interpolants/LinearInterpolant.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Interpolant } from "../Interpolant.js"; - -export class LinearInterpolant extends Interpolant { - constructor(parameterPositions: any, samplesValues: any, sampleSize: number, resultBuffer?: any); - - interpolate_(i1: number, t0: number, t: number, t1: number): any; -} diff --git a/src-testing/src/math/interpolants/QuaternionLinearInterpolant.d.ts b/src-testing/src/math/interpolants/QuaternionLinearInterpolant.d.ts deleted file mode 100644 index dccb66976..000000000 --- a/src-testing/src/math/interpolants/QuaternionLinearInterpolant.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Interpolant } from "../Interpolant.js"; - -export class QuaternionLinearInterpolant extends Interpolant { - constructor(parameterPositions: any, samplesValues: any, sampleSize: number, resultBuffer?: any); - - interpolate_(i1: number, t0: number, t: number, t1: number): any; -} diff --git a/src-testing/src/nodes/Nodes.ts b/src-testing/src/nodes/Nodes.ts deleted file mode 100644 index 41392bede..000000000 --- a/src-testing/src/nodes/Nodes.ts +++ /dev/null @@ -1,435 +0,0 @@ -// @TODO: We can simplify "export { default as SomeNode, other, exports } from '...'" to just "export * from '...'" if we will use only named exports -// this will also solve issues like "import TempNode from '../core/Node.js'" - -// constants -export * from './core/constants.js'; - -// core -export { default as AssignNode, assign } from './core/AssignNode.js'; -export { default as AttributeNode, attribute } from './core/AttributeNode.js'; -export { default as BypassNode, bypass } from './core/BypassNode.js'; -export { default as CacheNode, cache } from './core/CacheNode.js'; -export { default as ConstNode } from './core/ConstNode.js'; -export { default as ContextNode, context, label } from './core/ContextNode.js'; -export { default as IndexNode, vertexIndex, instanceIndex, drawIndex } from './core/IndexNode.js'; -export { default as LightingModel } from './core/LightingModel.js'; -export { default as Node, addNodeClass, createNodeFromType } from './core/Node.js'; -export { default as VarNode, temp } from './core/VarNode.js'; -export { default as NodeAttribute } from './core/NodeAttribute.js'; -export { default as NodeBuilder } from './core/NodeBuilder.js'; -export { default as NodeCache } from './core/NodeCache.js'; -export { default as NodeCode } from './core/NodeCode.js'; -export { default as NodeFrame } from './core/NodeFrame.js'; -export { default as NodeFunctionInput } from './core/NodeFunctionInput.js'; -export { default as NodeKeywords } from './core/NodeKeywords.js'; -export { default as NodeUniform } from './core/NodeUniform.js'; -export { default as NodeVar } from './core/NodeVar.js'; -export { default as NodeVarying } from './core/NodeVarying.js'; -export { default as ParameterNode, parameter } from './core/ParameterNode.js'; -export { - default as PropertyNode, - property, - varyingProperty, - output, - diffuseColor, - emissive, - roughness, - metalness, - clearcoat, - clearcoatRoughness, - sheen, - sheenRoughness, - iridescence, - iridescenceIOR, - iridescenceThickness, - specularColor, - shininess, - dashSize, - gapSize, - pointWidth, - alphaT, - anisotropy, - anisotropyB, - anisotropyT, -} from './core/PropertyNode.js'; -export { default as StackNode, stack } from './core/StackNode.js'; -export { default as TempNode } from './core/TempNode.js'; -export { - default as UniformGroupNode, - uniformGroup, - objectGroup, - renderGroup, - frameGroup, -} from './core/UniformGroupNode.js'; -export { default as UniformNode, uniform } from './core/UniformNode.js'; -export { default as VaryingNode, varying } from './core/VaryingNode.js'; -export { default as OutputStructNode, outputStruct } from './core/OutputStructNode.js'; -export { default as MRTNode, mrt } from './core/MRTNode.js'; - -import * as NodeUtils from './core/NodeUtils.js'; -export { NodeUtils }; - -// math -export { - default as MathNode, - PI, - PI2, - EPSILON, - INFINITY, - radians, - degrees, - exp, - exp2, - log, - log2, - sqrt, - inverseSqrt, - floor, - ceil, - normalize, - fract, - sin, - cos, - tan, - asin, - acos, - atan, - abs, - sign, - length, - lengthSq, - negate, - oneMinus, - dFdx, - dFdy, - round, - reciprocal, - trunc, - fwidth, - bitcast, - atan2, - min, - max, - mod, - step, - reflect, - distance, - difference, - dot, - cross, - pow, - pow2, - pow3, - pow4, - transformDirection, - mix, - clamp, - saturate, - refract, - smoothstep, - faceForward, - cbrt, - transpose, - all, - any, - equals, - rand, -} from './math/MathNode.js'; - -export { - default as OperatorNode, - add, - sub, - mul, - div, - remainder, - equal, - lessThan, - greaterThan, - lessThanEqual, - greaterThanEqual, - and, - or, - not, - xor, - bitAnd, - bitNot, - bitOr, - bitXor, - shiftLeft, - shiftRight, -} from './math/OperatorNode.js'; -export { default as CondNode, select, cond } from './math/CondNode.js'; -export { default as HashNode, hash } from './math/HashNode.js'; - -// math utils -export { parabola, gain, pcurve, sinc } from './math/MathUtils.js'; -export { triNoise3D } from './math/TriNoise3D.js'; - -// utils -export { default as ArrayElementNode } from './utils/ArrayElementNode.js'; -export { default as ConvertNode } from './utils/ConvertNode.js'; -export { default as DiscardNode, discard, Return } from './utils/DiscardNode.js'; -export { default as EquirectUVNode, equirectUV } from './utils/EquirectUVNode.js'; -export { default as FunctionOverloadingNode, overloadingFn } from './utils/FunctionOverloadingNode.js'; -export { default as JoinNode } from './utils/JoinNode.js'; -export { default as LoopNode, Loop, Continue, Break } from './utils/LoopNode.js'; -export { default as MatcapUVNode, matcapUV } from './utils/MatcapUVNode.js'; -export { default as MaxMipLevelNode, maxMipLevel } from './utils/MaxMipLevelNode.js'; -export { default as OscNode, oscSine, oscSquare, oscTriangle, oscSawtooth } from './utils/OscNode.js'; -export { default as PackingNode, directionToColor, colorToDirection } from './utils/PackingNode.js'; -export { default as RemapNode, remap, remapClamp } from './utils/RemapNode.js'; -export * from './utils/UVUtils.js'; -export * from './utils/SpriteUtils.js'; -export * from './utils/ViewportUtils.js'; -export { default as RotateNode, rotate } from './utils/RotateNode.js'; -export { default as SetNode } from './utils/SetNode.js'; -export { default as SplitNode } from './utils/SplitNode.js'; -export { default as SpriteSheetUVNode, spritesheetUV } from './utils/SpriteSheetUVNode.js'; -export { default as StorageArrayElementNode } from './utils/StorageArrayElementNode.js'; -export { default as TimerNode, timerLocal, timerGlobal, timerDelta, frameId } from './utils/TimerNode.js'; -export { - default as TriplanarTexturesNode, - triplanarTextures, - triplanarTexture, -} from './utils/TriplanarTexturesNode.js'; -export { default as ReflectorNode, reflector } from './utils/ReflectorNode.js'; -export { default as RTTNode, rtt } from './utils/RTTNode.js'; - -// shadernode -export * from './shadernode/ShaderNode.js'; - -// accessors -export { TBNViewMatrix, parallaxDirection, parallaxUV, transformedBentNormalView } from './accessors/AccessorsUtils.js'; -export { default as UniformArrayNode, uniformArray } from './accessors/UniformArrayNode.js'; -export * from './accessors/BitangentNode.js'; -export { - default as BufferAttributeNode, - bufferAttribute, - dynamicBufferAttribute, - instancedBufferAttribute, - instancedDynamicBufferAttribute, -} from './accessors/BufferAttributeNode.js'; -export { default as BufferNode, buffer } from './accessors/BufferNode.js'; -export * from './accessors/CameraNode.js'; -export { default as VertexColorNode, vertexColor } from './accessors/VertexColorNode.js'; -export { default as CubeTextureNode, cubeTexture } from './accessors/CubeTextureNode.js'; -export { default as InstanceNode, instance } from './accessors/InstanceNode.js'; -export { default as BatchNode, batch } from './accessors/BatchNode.js'; -export { - default as MaterialNode, - materialAlphaTest, - materialColor, - materialShininess, - materialEmissive, - materialOpacity, - materialSpecular, - materialSpecularStrength, - materialReflectivity, - materialRoughness, - materialMetalness, - materialNormal, - materialClearcoat, - materialClearcoatRoughness, - materialClearcoatNormal, - materialRotation, - materialSheen, - materialSheenRoughness, - materialIridescence, - materialIridescenceIOR, - materialIridescenceThickness, - materialLineScale, - materialLineDashSize, - materialLineGapSize, - materialLineWidth, - materialLineDashOffset, - materialPointWidth, - materialAnisotropy, - materialAnisotropyVector, - materialDispersion, - materialLightMap, - materialAOMap, -} from './accessors/MaterialNode.js'; -export * from './accessors/MaterialProperties.js'; -export { default as MaterialReferenceNode, materialReference } from './accessors/MaterialReferenceNode.js'; -export { default as RendererReferenceNode, rendererReference } from './accessors/RendererReferenceNode.js'; -export { default as MorphNode, morphReference } from './accessors/MorphNode.js'; -export { default as TextureBicubicNode, textureBicubic } from './accessors/TextureBicubicNode.js'; -export { - default as ModelNode, - modelDirection, - modelViewMatrix, - modelNormalMatrix, - modelWorldMatrix, - modelPosition, - modelViewPosition, - modelScale, - modelWorldMatrixInverse, -} from './accessors/ModelNode.js'; -export { default as ModelViewProjectionNode, modelViewProjection } from './accessors/ModelViewProjectionNode.js'; -export * from './accessors/NormalNode.js'; -export { - default as Object3DNode, - objectDirection, - objectViewMatrix, - objectNormalMatrix, - objectWorldMatrix, - objectPosition, - objectScale, - objectViewPosition, -} from './accessors/Object3DNode.js'; -export { default as PointUVNode, pointUV } from './accessors/PointUVNode.js'; -export * from './accessors/PositionNode.js'; -export { default as ReferenceNode, reference, referenceBuffer } from './accessors/ReferenceNode.js'; -export * from './accessors/ReflectVectorNode.js'; -export { default as SkinningNode, skinning, skinningReference } from './accessors/SkinningNode.js'; -export { default as SceneNode, backgroundBlurriness, backgroundIntensity } from './accessors/SceneNode.js'; -export { default as StorageBufferNode, storage, storageObject } from './accessors/StorageBufferNode.js'; -export * from './accessors/TangentNode.js'; -export { default as TextureNode, texture, textureLoad, /*textureLevel,*/ sampler } from './accessors/TextureNode.js'; -export { default as TextureSizeNode, textureSize } from './accessors/TextureSizeNode.js'; -export { default as StorageTextureNode, storageTexture, textureStore } from './accessors/StorageTextureNode.js'; -export { default as Texture3DNode, texture3D } from './accessors/Texture3DNode.js'; -export * from './accessors/UVNode.js'; -export { default as UserDataNode, userData } from './accessors/UserDataNode.js'; - -// display -export { default as BlendModeNode, burn, dodge, overlay, screen } from './display/BlendModeNode.js'; -export { default as BumpMapNode, bumpMap } from './display/BumpMapNode.js'; -export { - default as ColorAdjustmentNode, - saturation, - vibrance, - hue, - luminance, - threshold, -} from './display/ColorAdjustmentNode.js'; -export { - default as ColorSpaceNode, - linearToColorSpace, - colorSpaceToLinear, - linearTosRGB, - sRGBToLinear, -} from './display/ColorSpaceNode.js'; -export { default as FrontFacingNode, frontFacing, faceDirection } from './display/FrontFacingNode.js'; -export { default as NormalMapNode, normalMap } from './display/NormalMapNode.js'; -export { default as PosterizeNode, posterize } from './display/PosterizeNode.js'; -export { default as ToneMappingNode, toneMapping } from './display/ToneMappingNode.js'; -export { - default as ViewportNode, - viewport, - viewportCoordinate, - viewportResolution, - viewportTopLeft, - viewportBottomLeft, - viewportTopRight, - viewportBottomRight, -} from './display/ViewportNode.js'; -export { default as ViewportTextureNode, viewportTexture, viewportMipTexture } from './display/ViewportTextureNode.js'; -export { default as ViewportSharedTextureNode, viewportSharedTexture } from './display/ViewportSharedTextureNode.js'; -export { default as ViewportDepthTextureNode, viewportDepthTexture } from './display/ViewportDepthTextureNode.js'; -export { - default as ViewportDepthNode, - viewZToOrthographicDepth, - orthographicDepthToViewZ, - viewZToPerspectiveDepth, - perspectiveDepthToViewZ, - depth, - linearDepth, - viewportLinearDepth, -} from './display/ViewportDepthNode.js'; -export { default as GaussianBlurNode, gaussianBlur } from './display/GaussianBlurNode.js'; -export { default as AfterImageNode, afterImage } from './display/AfterImageNode.js'; -export { default as AnamorphicNode, anamorphic } from './display/AnamorphicNode.js'; -export { default as SobelOperatorNode, sobel } from './display/SobelOperatorNode.js'; -export { default as DepthOfFieldNode, dof } from './display/DepthOfFieldNode.js'; -export { default as DotScreenNode, dotScreen } from './display/DotScreenNode.js'; -export { default as RGBShiftNode, rgbShift } from './display/RGBShiftNode.js'; -export { default as FilmNode, film } from './display/FilmNode.js'; -export { default as Lut3DNode, lut3D } from './display/Lut3DNode.js'; -export { default as GTAONode, ao } from './display/GTAONode.js'; -export { default as DenoiseNode, denoise } from './display/DenoiseNode.js'; -export { default as FXAANode, fxaa } from './display/FXAANode.js'; -export { default as BloomNode, bloom } from './display/BloomNode.js'; -export { default as TransitionNode, transition } from './display/TransitionNode.js'; -export { default as RenderOutputNode, renderOutput } from './display/RenderOutputNode.js'; -export { default as PixelationPassNode, pixelationPass } from './display/PixelationPassNode.js'; -export { bleach } from './display/BleachBypassNode.js'; -export { sepia } from './display/SepiaNode.js'; - -export { default as PassNode, pass, passTexture, depthPass } from './display/PassNode.js'; - -// code -export { default as ExpressionNode, expression } from './code/ExpressionNode.js'; -export { default as CodeNode, code, js, wgsl, glsl } from './code/CodeNode.js'; -export { default as FunctionCallNode, call } from './code/FunctionCallNode.js'; -export { default as FunctionNode, wgslFn, glslFn } from './code/FunctionNode.js'; -export { default as ScriptableNode, scriptable, global } from './code/ScriptableNode.js'; -export { default as ScriptableValueNode, scriptableValue } from './code/ScriptableValueNode.js'; - -// fog -export { default as FogNode, fog } from './fog/FogNode.js'; -export { default as FogRangeNode, rangeFog } from './fog/FogRangeNode.js'; -export { default as FogExp2Node, densityFog } from './fog/FogExp2Node.js'; - -// geometry -export { default as RangeNode, range } from './geometry/RangeNode.js'; - -// gpgpu -export { default as ComputeNode, compute } from './gpgpu/ComputeNode.js'; - -// lighting -export { default as LightNode, lightTargetDirection } from './lighting/LightNode.js'; -export { default as PointLightNode } from './lighting/PointLightNode.js'; -export { default as DirectionalLightNode } from './lighting/DirectionalLightNode.js'; -export { default as RectAreaLightNode } from './lighting/RectAreaLightNode.js'; -export { default as SpotLightNode } from './lighting/SpotLightNode.js'; -export { default as IESSpotLightNode } from './lighting/IESSpotLightNode.js'; -export { default as AmbientLightNode } from './lighting/AmbientLightNode.js'; -export { default as LightsNode, lights, lightsNode, addLightNode } from './lighting/LightsNode.js'; -export { default as LightingNode /* @TODO: lighting (abstract), light */ } from './lighting/LightingNode.js'; -export { default as LightingContextNode, lightingContext } from './lighting/LightingContextNode.js'; -export { default as HemisphereLightNode } from './lighting/HemisphereLightNode.js'; -export { default as EnvironmentNode } from './lighting/EnvironmentNode.js'; -export { default as BasicEnvironmentNode } from './lighting/BasicEnvironmentNode.js'; -export { default as IrradianceNode } from './lighting/IrradianceNode.js'; -export { default as AONode } from './lighting/AONode.js'; -export { default as AnalyticLightNode } from './lighting/AnalyticLightNode.js'; - -// pmrem -export { default as PMREMNode, pmremTexture } from './pmrem/PMREMNode.js'; -export * from './pmrem/PMREMUtils.js'; - -// procedural -export { default as CheckerNode, checker } from './procedural/CheckerNode.js'; - -// loaders -export { default as NodeLoader } from './loaders/NodeLoader.js'; -export { default as NodeObjectLoader } from './loaders/NodeObjectLoader.js'; -export { default as NodeMaterialLoader } from './loaders/NodeMaterialLoader.js'; - -// parsers -export { default as GLSLNodeParser } from './parsers/GLSLNodeParser.js'; // @TODO: Move to jsm/renderers/webgl. - -// materials -export * from './materials/Materials.js'; - -// materialX -export * from './materialx/MaterialXNodes.js'; - -// functions -export { default as BRDF_GGX } from './functions/BSDF/BRDF_GGX.js'; -export { default as BRDF_Lambert } from './functions/BSDF/BRDF_Lambert.js'; -export { default as D_GGX } from './functions/BSDF/D_GGX.js'; -export { default as DFGApprox } from './functions/BSDF/DFGApprox.js'; -export { default as F_Schlick } from './functions/BSDF/F_Schlick.js'; -export { default as Schlick_to_F0 } from './functions/BSDF/Schlick_to_F0.js'; -export { default as V_GGX_SmithCorrelated } from './functions/BSDF/V_GGX_SmithCorrelated.js'; - -export { getDistanceAttenuation } from './lighting/LightUtils.js'; - -export { default as getGeometryRoughness } from './functions/material/getGeometryRoughness.js'; -export { default as getRoughness } from './functions/material/getRoughness.js'; - -export { default as PhongLightingModel } from './functions/PhongLightingModel.js'; -export { default as PhysicalLightingModel } from './functions/PhysicalLightingModel.js'; diff --git a/src-testing/src/nodes/accessors/AccessorsUtils.d.ts b/src-testing/src/nodes/accessors/AccessorsUtils.d.ts deleted file mode 100644 index fc2980b0d..000000000 --- a/src-testing/src/nodes/accessors/AccessorsUtils.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const TBNViewMatrix: ShaderNodeObject; - -export const parallaxDirection: ShaderNodeObject; -export const parallaxUV: (uv: ShaderNodeObject, scale: NodeRepresentation) => ShaderNodeObject; - -export const transformedBentNormalView: ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/BatchNode.d.ts b/src-testing/src/nodes/accessors/BatchNode.d.ts deleted file mode 100644 index 49c989fdc..000000000 --- a/src-testing/src/nodes/accessors/BatchNode.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { BatchedMesh } from "../../objects/BatchedMesh.js"; -import Node from "../core/Node.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class BatchNode extends Node { - batchMesh: BatchedMesh; - - instanceColorNode: Node | null; - batchingIdNode: Node | null; - - constructor(batchMesh: BatchedMesh); -} - -export const batch: (batchMesh: BatchedMesh) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/BitangentNode.d.ts b/src-testing/src/nodes/accessors/BitangentNode.d.ts deleted file mode 100644 index 5a24e8eba..000000000 --- a/src-testing/src/nodes/accessors/BitangentNode.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import MathNode from "../math/MathNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const bitangentGeometry: ShaderNodeObject; -export const bitangentLocal: ShaderNodeObject; -export const bitangentView: ShaderNodeObject; -export const bitangentWorld: ShaderNodeObject; -export const transformedBitangentView: ShaderNodeObject; -export const transformedBitangentWorld: ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/BufferAttributeNode.ts b/src-testing/src/nodes/accessors/BufferAttributeNode.ts deleted file mode 100644 index e4a040deb..000000000 --- a/src-testing/src/nodes/accessors/BufferAttributeNode.ts +++ /dev/null @@ -1,134 +0,0 @@ -import InputNode from '../core/InputNode.js'; -import { addNodeClass } from '../core/Node.js'; -import { varying } from '../core/VaryingNode.js'; -import { nodeObject, addNodeElement } from '../shadernode/ShaderNode.js'; - -import { InterleavedBufferAttribute } from '../../core/InterleavedBufferAttribute.js'; -import { InterleavedBuffer } from '../../core/InterleavedBuffer.js'; -import { StaticDrawUsage, DynamicDrawUsage } from '../../constants.js'; - -class BufferAttributeNode extends InputNode { - constructor(value, bufferType = null, bufferStride = 0, bufferOffset = 0) { - super(value, bufferType); - - this.isBufferNode = true; - - this.bufferType = bufferType; - this.bufferStride = bufferStride; - this.bufferOffset = bufferOffset; - - this.usage = StaticDrawUsage; - this.instanced = false; - - this.attribute = null; - - this.global = true; - - if (value && value.isBufferAttribute === true) { - this.attribute = value; - this.usage = value.usage; - this.instanced = value.isInstancedBufferAttribute; - } - } - - getHash(builder) { - if (this.bufferStride === 0 && this.bufferOffset === 0) { - let bufferData = builder.globalCache.getData(this.value); - - if (bufferData === undefined) { - bufferData = { - node: this, - }; - - builder.globalCache.setData(this.value, bufferData); - } - - return bufferData.node.uuid; - } - - return this.uuid; - } - - getNodeType(builder) { - if (this.bufferType === null) { - this.bufferType = builder.getTypeFromAttribute(this.attribute); - } - - return this.bufferType; - } - - setup(builder) { - if (this.attribute !== null) return; - - const type = this.getNodeType(builder); - const array = this.value; - const itemSize = builder.getTypeLength(type); - const stride = this.bufferStride || itemSize; - const offset = this.bufferOffset; - - const buffer = array.isInterleavedBuffer === true ? array : new InterleavedBuffer(array, stride); - const bufferAttribute = new InterleavedBufferAttribute(buffer, itemSize, offset); - - buffer.setUsage(this.usage); - - this.attribute = bufferAttribute; - this.attribute.isInstancedBufferAttribute = this.instanced; // @TODO: Add a possible: InstancedInterleavedBufferAttribute - } - - generate(builder) { - const nodeType = this.getNodeType(builder); - - const nodeAttribute = builder.getBufferAttributeFromNode(this, nodeType); - const propertyName = builder.getPropertyName(nodeAttribute); - - let output = null; - - if (builder.shaderStage === 'vertex' || builder.shaderStage === 'compute') { - this.name = propertyName; - - output = propertyName; - } else { - const nodeVarying = varying(this); - - output = nodeVarying.build(builder, nodeType); - } - - return output; - } - - getInputType(/*builder*/) { - return 'bufferAttribute'; - } - - setUsage(value) { - this.usage = value; - - if (this.attribute && this.attribute.isBufferAttribute === true) { - this.attribute.usage = value; - } - - return this; - } - - setInstanced(value) { - this.instanced = value; - - return this; - } -} - -export default BufferAttributeNode; - -export const bufferAttribute = (array, type, stride, offset) => - nodeObject(new BufferAttributeNode(array, type, stride, offset)); -export const dynamicBufferAttribute = (array, type, stride, offset) => - bufferAttribute(array, type, stride, offset).setUsage(DynamicDrawUsage); - -export const instancedBufferAttribute = (array, type, stride, offset) => - bufferAttribute(array, type, stride, offset).setInstanced(true); -export const instancedDynamicBufferAttribute = (array, type, stride, offset) => - dynamicBufferAttribute(array, type, stride, offset).setInstanced(true); - -addNodeElement('toAttribute', bufferNode => bufferAttribute(bufferNode.value)); - -addNodeClass('BufferAttributeNode', BufferAttributeNode); diff --git a/src-testing/src/nodes/accessors/BufferNode.d.ts b/src-testing/src/nodes/accessors/BufferNode.d.ts deleted file mode 100644 index 050b31ddd..000000000 --- a/src-testing/src/nodes/accessors/BufferNode.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -import UniformNode from "../core/UniformNode.js"; -import { NodeOrType, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class BufferNode extends UniformNode { - isBufferNode: true; - - bufferType: string; - bufferCount: number; - - constructor(value: unknown, bufferType: string, bufferCount?: number); -} - -export const buffer: ( - value: unknown, - nodeOrType: NodeOrType, - count: number, -) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/CameraNode.d.ts b/src-testing/src/nodes/accessors/CameraNode.d.ts deleted file mode 100644 index dce969e9c..000000000 --- a/src-testing/src/nodes/accessors/CameraNode.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Matrix3 } from "../../math/Matrix3.js"; -import { Matrix4 } from "../../math/Matrix4.js"; -import { Vector3 } from "../../math/Vector3.js"; -import UniformNode from "../core/UniformNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const cameraNear: ShaderNodeObject>; -export const cameraFar: ShaderNodeObject>; -export const cameraLogDepth: ShaderNodeObject>; -export const cameraProjectionMatrix: ShaderNodeObject>; -export const cameraProjectionMatrixInverse: ShaderNodeObject>; -export const cameraViewMatrix: ShaderNodeObject>; -export const cameraWorldMatrix: ShaderNodeObject>; -export const cameraNormalMatrix: ShaderNodeObject>; -export const cameraPosition: ShaderNodeObject>; diff --git a/src-testing/src/nodes/accessors/ClippingNode.d.ts b/src-testing/src/nodes/accessors/ClippingNode.d.ts deleted file mode 100644 index 4c1d42247..000000000 --- a/src-testing/src/nodes/accessors/ClippingNode.d.ts +++ /dev/null @@ -1,16 +0,0 @@ -import Node from "../core/Node.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export type ClippingNodeScope = typeof ClippingNode.ALPHA_TO_COVERAGE | typeof ClippingNode.DEFAULT; - -export default class ClippingNode extends Node { - scope: ClippingNodeScope; - - constructor(scope?: ClippingNodeScope); - - static ALPHA_TO_COVERAGE: "alphaToCoverage"; - static DEFAULT: "default"; -} - -export const clipping: () => ShaderNodeObject; -export const clippingAlpha: () => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/CubeTextureNode.d.ts b/src-testing/src/nodes/accessors/CubeTextureNode.d.ts deleted file mode 100644 index 6e3b8c19d..000000000 --- a/src-testing/src/nodes/accessors/CubeTextureNode.d.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { CubeTexture } from "../../textures/CubeTexture.js"; -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import TextureNode from "./TextureNode.js"; - -declare class CubeTextureNode extends TextureNode { - isCubeTextureNode: boolean; - uvNode: ShaderNodeObject | null; - levelNode: ShaderNodeObject | null; - - constructor( - value: CubeTexture, - uvNode?: ShaderNodeObject | null, - levelNode?: ShaderNodeObject | null, - biasNode?: ShaderNodeObject | null, - ); - - getDefaultUV(): Node; -} - -export default CubeTextureNode; - -export const cubeTexture: ( - value: CubeTexture, - uvNode?: NodeRepresentation, - levelNode?: NodeRepresentation, - biasNode?: NodeRepresentation, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - cubeTexture: typeof cubeTexture; - } -} diff --git a/src-testing/src/nodes/accessors/InstanceNode.d.ts b/src-testing/src/nodes/accessors/InstanceNode.d.ts deleted file mode 100644 index bb54f0a81..000000000 --- a/src-testing/src/nodes/accessors/InstanceNode.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { InstancedMesh } from "../../objects/InstancedMesh.js"; -import Node from "../core/Node.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class InstanceNode extends Node { - instanceMesh: InstancedMesh; - instanceMatrixNode: Node | null; - instanceColorNode: Node | null; - - constructor(instanceMesh: InstancedMesh); -} - -export const instance: (instanceMesh: InstancedMesh) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/MaterialNode.d.ts b/src-testing/src/nodes/accessors/MaterialNode.d.ts deleted file mode 100644 index 63f6c9180..000000000 --- a/src-testing/src/nodes/accessors/MaterialNode.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -import { Vector2 } from "../../math/Vector2.js"; -import Node from "../core/Node.js"; -import UniformNode from "../core/UniformNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export type MaterialNodeScope = - | typeof MaterialNode.ALPHA_TEST - | typeof MaterialNode.COLOR - | typeof MaterialNode.OPACITY - | typeof MaterialNode.SHININESS - | typeof MaterialNode.SPECULAR - | typeof MaterialNode.SPECULAR_STRENGTH - | typeof MaterialNode.SPECULAR_INTENSITY - | typeof MaterialNode.SPECULAR_COLOR - | typeof MaterialNode.REFLECTIVITY - | typeof MaterialNode.ROUGHNESS - | typeof MaterialNode.METALNESS - | typeof MaterialNode.NORMAL - | typeof MaterialNode.CLEARCOAT - | typeof MaterialNode.CLEARCOAT_ROUGHNESS - | typeof MaterialNode.CLEARCOAT_NORMAL - | typeof MaterialNode.EMISSIVE - | typeof MaterialNode.ROTATION - | typeof MaterialNode.SHEEN - | typeof MaterialNode.SHEEN_ROUGHNESS - | typeof MaterialNode.ANISOTROPY - | typeof MaterialNode.IRIDESCENCE - | typeof MaterialNode.IRIDESCENCE_IOR - | typeof MaterialNode.IRIDESCENCE_THICKNESS - | typeof MaterialNode.IOR - | typeof MaterialNode.TRANSMISSION - | typeof MaterialNode.THICKNESS - | typeof MaterialNode.ATTENUATION_DISTANCE - | typeof MaterialNode.ATTENUATION_COLOR - | typeof MaterialNode.LINE_SCALE - | typeof MaterialNode.LINE_DASH_SIZE - | typeof MaterialNode.LINE_GAP_SIZE - | typeof MaterialNode.LINE_WIDTH - | typeof MaterialNode.LINE_DASH_OFFSET - | typeof MaterialNode.POINT_WIDTH - | typeof MaterialNode.DISPERSION - | typeof MaterialNode.LIGHT_MAP - | typeof MaterialNode.AO_MAP - | typeof MaterialNode.REFRACTION_RATIO; - -export default class MaterialNode extends Node { - static ALPHA_TEST: "alphaTest"; - static COLOR: "color"; - static OPACITY: "opacity"; - static SHININESS: "shininess"; - static SPECULAR: "specular"; - static SPECULAR_STRENGTH: "specularStrength"; - static SPECULAR_INTENSITY: "specularIntensity"; - static SPECULAR_COLOR: "specularColor"; - static REFLECTIVITY: "reflectivity"; - static ROUGHNESS: "roughness"; - static METALNESS: "metalness"; - static NORMAL: "normal"; - static CLEARCOAT: "clearcoat"; - static CLEARCOAT_ROUGHNESS: "clearcoatRoughness"; - static CLEARCOAT_NORMAL: "clearcoatNormal"; - static EMISSIVE: "emissive"; - static ROTATION: "rotation"; - static SHEEN: "sheen"; - static SHEEN_ROUGHNESS: "sheenRoughness"; - static ANISOTROPY: "anisotropy"; - static IRIDESCENCE: "iridescence"; - static IRIDESCENCE_IOR: "iridescenceIOR"; - static IRIDESCENCE_THICKNESS: "iridescenceThickness"; - static IOR: "ior"; - static TRANSMISSION: "transmission"; - static THICKNESS: "thickness"; - static ATTENUATION_DISTANCE: "attenuationDistance"; - static ATTENUATION_COLOR: "attenuationColor"; - static LINE_SCALE: "scale"; - static LINE_DASH_SIZE: "dashSize"; - static LINE_GAP_SIZE: "gapSize"; - static LINE_WIDTH: "linewidth"; - static LINE_DASH_OFFSET: "dashOffset"; - static POINT_WIDTH: "pointWidth"; - static DISPERSION: "dispersion"; - static LIGHT_MAP: "light"; - static AO_MAP: "ao"; - static REFRACTION_RATIO: "refractionRatio"; - - scope: MaterialNodeScope; - constructor(scope?: MaterialNodeScope); -} - -export const materialAlphaTest: ShaderNodeObject; -export const materialColor: ShaderNodeObject; -export const materialShininess: ShaderNodeObject; -export const materialEmissive: ShaderNodeObject; -export const materialOpacity: ShaderNodeObject; -export const materialSpecular: ShaderNodeObject; - -export const materialSpecularIntensity: ShaderNodeObject; -export const materialSpecularColor: ShaderNodeObject; - -export const materialSpecularStrength: ShaderNodeObject; -export const materialReflectivity: ShaderNodeObject; -export const materialRoughness: ShaderNodeObject; -export const materialMetalness: ShaderNodeObject; -export const materialNormal: ShaderNodeObject; -export const materialClearcoat: ShaderNodeObject; -export const materialClearcoatRoughness: ShaderNodeObject; -export const materialClearcoatNormal: ShaderNodeObject; -export const materialRotation: ShaderNodeObject; -export const materialSheen: ShaderNodeObject; -export const materialSheenRoughness: ShaderNodeObject; -export const materialAnisotropy: ShaderNodeObject; -export const materialIridescence: ShaderNodeObject; -export const materialIridescenceIOR: ShaderNodeObject; -export const materialIridescenceThickness: ShaderNodeObject; -export const materialTransmission: ShaderNodeObject; -export const materialThickness: ShaderNodeObject; -export const materialIOR: ShaderNodeObject; -export const materialAttenuationDistance: ShaderNodeObject; -export const materialAttenuationColor: ShaderNodeObject; -export const materialLineScale: ShaderNodeObject; -export const materialLineDashSize: ShaderNodeObject; -export const materialLineGapSize: ShaderNodeObject; -export const materialLineWidth: ShaderNodeObject; -export const materialLineDashOffset: ShaderNodeObject; -export const materialPointWidth: ShaderNodeObject; -export const materialDispersion: ShaderNodeObject; -export const materialLightMap: ShaderNodeObject; -export const materialAOMap: ShaderNodeObject; -export const materialRefractionRatio: ShaderNodeObject; -export const materialAnisotropyVector: ShaderNodeObject>; diff --git a/src-testing/src/nodes/accessors/MaterialProperties.d.ts b/src-testing/src/nodes/accessors/MaterialProperties.d.ts deleted file mode 100644 index e1a0948e1..000000000 --- a/src-testing/src/nodes/accessors/MaterialProperties.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -import UniformNode from "../core/UniformNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const materialRefractionRatio: ShaderNodeObject>; diff --git a/src-testing/src/nodes/accessors/MaterialReferenceNode.d.ts b/src-testing/src/nodes/accessors/MaterialReferenceNode.d.ts deleted file mode 100644 index 061228d29..000000000 --- a/src-testing/src/nodes/accessors/MaterialReferenceNode.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Material } from "../../materials/Material.js"; -import { NodeOrType, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import ReferenceNode from "./ReferenceNode.js"; - -export default class MaterialReferenceNode extends ReferenceNode { - readonly isMaterialReferenceNode: true; - - constructor(property: string, inputType: string, material?: Material | null); -} - -export const materialReference: ( - name: string, - nodeOrType: NodeOrType, - material?: Material | null, -) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/ModelNode.d.ts b/src-testing/src/nodes/accessors/ModelNode.d.ts deleted file mode 100644 index a7dfbcfdb..000000000 --- a/src-testing/src/nodes/accessors/ModelNode.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Matrix4 } from "../../math/Matrix4.js"; -import { UniformNode } from "../Nodes.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import Object3DNode from "./Object3DNode.js"; - -/** - * Similar to {@link Object3DNode} but the object comes from {@link NodeFrame} - */ -export default class ModelNode extends Object3DNode { - constructor(scope?: string); -} - -export const modelDirection: ShaderNodeObject; -export const modelViewMatrix: ShaderNodeObject; -export const modelNormalMatrix: ShaderNodeObject; -export const modelWorldMatrix: ShaderNodeObject; -export const modelPosition: ShaderNodeObject; -export const modelScale: ShaderNodeObject; -export const modelViewPosition: ShaderNodeObject; -export const modelWorldMatrixInverse: ShaderNodeObject>; diff --git a/src-testing/src/nodes/accessors/ModelViewProjectionNode.d.ts b/src-testing/src/nodes/accessors/ModelViewProjectionNode.d.ts deleted file mode 100644 index dee451a19..000000000 --- a/src-testing/src/nodes/accessors/ModelViewProjectionNode.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class ModelViewProjectionNode extends Node { - constructor(positionNode?: Node); -} - -export const modelViewProjection: (position?: NodeRepresentation) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/NormalNode.d.ts b/src-testing/src/nodes/accessors/NormalNode.d.ts deleted file mode 100644 index 65300ef16..000000000 --- a/src-testing/src/nodes/accessors/NormalNode.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -import AttributeNode from "../core/AttributeNode.js"; -import PropertyNode from "../core/PropertyNode.js"; -import VarNode from "../core/VarNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const normalGeometry: ShaderNodeObject; -export const normalLocal: ShaderNodeObject; -export const normalView: ShaderNodeObject; -export const normalWorld: ShaderNodeObject; -export const transformedNormalView: ShaderNodeObject; -export const transformedNormalWorld: ShaderNodeObject; -export const transformedClearcoatNormalView: ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/Object3DNode.d.ts b/src-testing/src/nodes/accessors/Object3DNode.d.ts deleted file mode 100644 index b16f9b00e..000000000 --- a/src-testing/src/nodes/accessors/Object3DNode.d.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Object3D } from "../../core/Object3D.js"; -import Node from "../core/Node.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class Object3DNode extends Node { - scope: string; - object3d: Object3D | null; - - constructor(scope?: string, object3d?: Object3D | null); - - static VIEW_MATRIX: "viewMatrix"; - static NORMAL_MATRIX: "normalMatrix"; - static WORLD_MATRIX: "worldMatrix"; - static POSITION: "position"; - static SCALE: "scale"; - static VIEW_POSITION: "viewPosition"; - static DIRECTION: "direction"; -} - -export const objectDirection: (object3d: Object3D) => ShaderNodeObject; -export const objectViewMatrix: (object3d: Object3D) => ShaderNodeObject; -export const objectNormalMatrix: (object3d: Object3D) => ShaderNodeObject; -export const objectWorldMatrix: (object3d: Object3D) => ShaderNodeObject; -export const objectPosition: (object3d: Object3D) => ShaderNodeObject; -export const objectScale: (object3d: Object3D) => ShaderNodeObject; -export const objectViewPosition: (object3d: Object3D) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/PointUVNode.d.ts b/src-testing/src/nodes/accessors/PointUVNode.d.ts deleted file mode 100644 index 017a43ffa..000000000 --- a/src-testing/src/nodes/accessors/PointUVNode.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import Node from "../core/Node.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class PointUVNode extends Node { - isPointUVNode: true; - - constructor(); -} - -export const pointUV: ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/PositionNode.d.ts b/src-testing/src/nodes/accessors/PositionNode.d.ts deleted file mode 100644 index ae7b31b21..000000000 --- a/src-testing/src/nodes/accessors/PositionNode.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import Node from "../core/Node.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const positionGeometry: ShaderNodeObject; -export const positionLocal: ShaderNodeObject; -export const positionWorld: ShaderNodeObject; -export const positionWorldDirection: ShaderNodeObject; -export const positionView: ShaderNodeObject; -export const positionViewDirection: ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/ReferenceNode.d.ts b/src-testing/src/nodes/accessors/ReferenceNode.d.ts deleted file mode 100644 index f094d8b41..000000000 --- a/src-testing/src/nodes/accessors/ReferenceNode.d.ts +++ /dev/null @@ -1,27 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeOrType, NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class ReferenceNode extends Node { - property: string; - - uniformType: string; - - object: T; - count: number | null; - - properties: string[]; - reference: T | null; - node: Node | null; - - constructor(property: string, uniformType: string, object?: T | null, count?: number | null); - - setNodeType(uniformType: string): void; -} - -export const reference: (name: string, nodeOrType: NodeOrType, object: T) => ShaderNodeObject>; -export const referenceBuffer: ( - name: string, - nodeOrType: NodeOrType, - count: NodeRepresentation, - object: T, -) => ShaderNodeObject>; diff --git a/src-testing/src/nodes/accessors/ReflectVectorNode.d.ts b/src-testing/src/nodes/accessors/ReflectVectorNode.d.ts deleted file mode 100644 index 31c6a1310..000000000 --- a/src-testing/src/nodes/accessors/ReflectVectorNode.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import Node from "../core/Node.js"; -import VarNode from "../core/VarNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const reflectView: ShaderNodeObject; -export const refractView: ShaderNodeObject; - -export const reflectVector: ShaderNodeObject; -export const refractVector: ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/RendererReferenceNode.d.ts b/src-testing/src/nodes/accessors/RendererReferenceNode.d.ts deleted file mode 100644 index 8e6c47130..000000000 --- a/src-testing/src/nodes/accessors/RendererReferenceNode.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -import Renderer from "../../renderers/common/Renderer.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import ReferenceNode from "./ReferenceNode.js"; - -export default class RendererReferenceNode extends ReferenceNode { - renderer: Renderer | null; - - constructor(property: string, inputType: string, renderer?: Renderer | null); -} - -export const rendererReference: ( - name: string, - type: string, - renderer?: Renderer | null, -) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/SkinningNode.d.ts b/src-testing/src/nodes/accessors/SkinningNode.d.ts deleted file mode 100644 index 11b612cfb..000000000 --- a/src-testing/src/nodes/accessors/SkinningNode.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { SkinnedMesh } from "../../objects/SkinnedMesh.js"; -import Node from "../core/Node.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class SkinningNode extends Node { - skinnedMesh: SkinnedMesh; - useReference: boolean; - - skinIndexNode: Node; - skinWeightNode: Node; - - bindMatrixNode: Node; - bindMatrixInverseNode: Node; - boneMatricesNode: Node; - - constructor(skinnedMesh: SkinnedMesh, useReference?: boolean); -} - -export const skinning: (skinnedMesh: SkinnedMesh) => ShaderNodeObject; -export const skinningReference: (skinnedMesh: SkinnedMesh) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/StorageBufferNode.d.ts b/src-testing/src/nodes/accessors/StorageBufferNode.d.ts deleted file mode 100644 index 3d4d64ff7..000000000 --- a/src-testing/src/nodes/accessors/StorageBufferNode.d.ts +++ /dev/null @@ -1,38 +0,0 @@ -import StorageBufferAttribute from "../../renderers/common/StorageBufferAttribute.js"; -import StorageInstancedBufferAttribute from "../../renderers/common/StorageInstancedBufferAttribute.js"; -import { GPUBufferBindingType } from "../../renderers/webgpu/utils/WebGPUConstants.js"; -import { NodeOrType, NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import StorageArrayElementNode from "../utils/StoargeArrayElementNode.js"; -import BufferNode from "./BufferNode.js"; - -export default class StorageBufferNode extends BufferNode { - readonly isStorageBufferNode: true; - bufferObject: boolean; - - access: GPUBufferBindingType; - - constructor( - value: StorageBufferAttribute | StorageInstancedBufferAttribute, - bufferType: string, - bufferCount?: number, - ); - - element(indexNode: NodeRepresentation): ShaderNodeObject; - - setBufferObject(value: boolean): this; - - setAccess(value: GPUBufferBindingType): this; - - toReadOnly(): this; -} - -export const storage: ( - value: StorageBufferAttribute | StorageInstancedBufferAttribute, - nodeOrType: NodeOrType, - count: number, -) => ShaderNodeObject; -export const storageObject: ( - value: StorageBufferAttribute | StorageInstancedBufferAttribute, - nodeOrType: NodeOrType, - count: number, -) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/StorageTextureNode.d.ts b/src-testing/src/nodes/accessors/StorageTextureNode.d.ts deleted file mode 100644 index c1e810190..000000000 --- a/src-testing/src/nodes/accessors/StorageTextureNode.d.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { GPUStorageTextureAccess } from "../../renderers/webgpu/utils/WebGPUConstants.js"; -import { Texture } from "../../textures/Texture.js"; -import Node from "../core/Node.js"; -import NodeBuilder from "../core/NodeBuilder.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import TextureNode from "./TextureNode.js"; - -export default class StorageTextureNode extends TextureNode { - storeNode: Node | null; - - readonly isStorageTextureNode: true; - - access: GPUStorageTextureAccess; - - constructor( - value: Texture, - uvNode?: ShaderNodeObject | null, - storeNode?: Node | null, - ); - - setAccess(value: GPUStorageTextureAccess): this; - - toReadOnly(): this; - - toWriteOnly(): this; - - generateStore(builder: NodeBuilder): void; -} - -export const storageTexture: ( - value: Texture, - uvNode?: NodeRepresentation, - storeNode?: NodeRepresentation, -) => ShaderNodeObject; - -export const textureStore: ( - value: Texture, - uvNode?: NodeRepresentation, - storeNode?: NodeRepresentation, -) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/TangentNode.d.ts b/src-testing/src/nodes/accessors/TangentNode.d.ts deleted file mode 100644 index 0dac9e0dc..000000000 --- a/src-testing/src/nodes/accessors/TangentNode.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -import AttributeNode from "../core/AttributeNode.js"; -import VarNode from "../core/VarNode.js"; -import VaryingNode from "../core/VaryingNode.js"; -import MathNode from "../math/MathNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const tangentGeometry: ShaderNodeObject; -export const tangentLocal: ShaderNodeObject; -export const tangentView: ShaderNodeObject; -export const tangentWorld: ShaderNodeObject; -export const transformedTangentView: ShaderNodeObject; -export const transformedTangentWorld: ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/Texture3DNode.d.ts b/src-testing/src/nodes/accessors/Texture3DNode.d.ts deleted file mode 100644 index eed254d2e..000000000 --- a/src-testing/src/nodes/accessors/Texture3DNode.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { CubeTexture } from "../../textures/CubeTexture.js"; -import { Texture } from "../../textures/Texture.js"; -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import TextureNode from "./TextureNode.js"; - -export default class Texture3DNode extends TextureNode { - readonly isTexture3DNode: true; - - constructor(value: Texture, uvNode?: ShaderNodeObject | null, levelNode?: ShaderNodeObject | null); -} - -export const texture3D: ( - value: Texture, - uvNode?: NodeRepresentation, - levelNode?: NodeRepresentation, -) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/TextureBicubicNode.d.ts b/src-testing/src/nodes/accessors/TextureBicubicNode.d.ts deleted file mode 100644 index 947aa6e01..000000000 --- a/src-testing/src/nodes/accessors/TextureBicubicNode.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class TextureBicubicNode extends TempNode { - textureNode: Node; - blurNode: Node; - - constructor(textureNode: Node, blurNode?: Node); -} - -export const textureBicubic: (textureNode: Node, blurNode?: NodeRepresentation) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - bicubic: typeof textureBicubic; - } -} diff --git a/src-testing/src/nodes/accessors/TextureNode.ts b/src-testing/src/nodes/accessors/TextureNode.ts deleted file mode 100644 index 163e3ccc5..000000000 --- a/src-testing/src/nodes/accessors/TextureNode.ts +++ /dev/null @@ -1,369 +0,0 @@ -import UniformNode, { uniform } from '../core/UniformNode.js'; -import { uv } from './UVNode.js'; -import { textureSize } from './TextureSizeNode.js'; -import { colorSpaceToLinear } from '../display/ColorSpaceNode.js'; -import { expression } from '../code/ExpressionNode.js'; -import { addNodeClass } from '../core/Node.js'; -import { maxMipLevel } from '../utils/MaxMipLevelNode.js'; -import { addNodeElement, nodeProxy, vec3, nodeObject } from '../shadernode/ShaderNode.js'; -import { NodeUpdateType } from '../core/constants.js'; - -import { IntType, UnsignedIntType } from '../../constants.js'; - -class TextureNode extends UniformNode { - constructor(value, uvNode = null, levelNode = null, biasNode = null) { - super(value); - - this.isTextureNode = true; - - this.uvNode = uvNode; - this.levelNode = levelNode; - this.biasNode = biasNode; - this.compareNode = null; - this.depthNode = null; - this.gradNode = null; - - this.sampler = true; - this.updateMatrix = false; - this.updateType = NodeUpdateType.NONE; - - this.referenceNode = null; - - this._value = value; - this._matrixUniform = null; - - this.setUpdateMatrix(uvNode === null); - } - - set value(value) { - if (this.referenceNode) { - this.referenceNode.value = value; - } else { - this._value = value; - } - } - - get value() { - return this.referenceNode ? this.referenceNode.value : this._value; - } - - getUniformHash(/*builder*/) { - return this.value.uuid; - } - - getNodeType(/*builder*/) { - if (this.value.isDepthTexture === true) return 'float'; - - if (this.value.type === UnsignedIntType) { - return 'uvec4'; - } else if (this.value.type === IntType) { - return 'ivec4'; - } - - return 'vec4'; - } - - getInputType(/*builder*/) { - return 'texture'; - } - - getDefaultUV() { - return uv(this.value.channel); - } - - updateReference(/*state*/) { - return this.value; - } - - getTransformedUV(uvNode) { - if (this._matrixUniform === null) this._matrixUniform = uniform(this.value.matrix); - - return this._matrixUniform.mul(vec3(uvNode, 1)).xy; - } - - setUpdateMatrix(value) { - this.updateMatrix = value; - this.updateType = value ? NodeUpdateType.FRAME : NodeUpdateType.NONE; - - return this; - } - - setupUV(builder, uvNode) { - const texture = this.value; - - if ( - builder.isFlipY() && - (texture.isRenderTargetTexture === true || - texture.isFramebufferTexture === true || - texture.isDepthTexture === true) - ) { - uvNode = uvNode.setY(uvNode.y.oneMinus()); - } - - return uvNode; - } - - setup(builder) { - const properties = builder.getNodeProperties(this); - properties.referenceNode = this.referenceNode; - - // - - let uvNode = this.uvNode; - - if ((uvNode === null || builder.context.forceUVContext === true) && builder.context.getUV) { - uvNode = builder.context.getUV(this); - } - - if (!uvNode) uvNode = this.getDefaultUV(); - - if (this.updateMatrix === true) { - uvNode = this.getTransformedUV(uvNode); - } - - uvNode = this.setupUV(builder, uvNode); - - // - - let levelNode = this.levelNode; - - if (levelNode === null && builder.context.getTextureLevel) { - levelNode = builder.context.getTextureLevel(this); - } - - // - - properties.uvNode = uvNode; - properties.levelNode = levelNode; - properties.biasNode = this.biasNode; - properties.compareNode = this.compareNode; - properties.gradNode = this.gradNode; - properties.depthNode = this.depthNode; - } - - generateUV(builder, uvNode) { - return uvNode.build(builder, this.sampler === true ? 'vec2' : 'ivec2'); - } - - generateSnippet( - builder, - textureProperty, - uvSnippet, - levelSnippet, - biasSnippet, - depthSnippet, - compareSnippet, - gradSnippet, - ) { - const texture = this.value; - - let snippet; - - if (levelSnippet) { - snippet = builder.generateTextureLevel(texture, textureProperty, uvSnippet, levelSnippet, depthSnippet); - } else if (biasSnippet) { - snippet = builder.generateTextureBias(texture, textureProperty, uvSnippet, biasSnippet, depthSnippet); - } else if (gradSnippet) { - snippet = builder.generateTextureGrad(texture, textureProperty, uvSnippet, gradSnippet, depthSnippet); - } else if (compareSnippet) { - snippet = builder.generateTextureCompare(texture, textureProperty, uvSnippet, compareSnippet, depthSnippet); - } else if (this.sampler === false) { - snippet = builder.generateTextureLoad(texture, textureProperty, uvSnippet, depthSnippet); - } else { - snippet = builder.generateTexture(texture, textureProperty, uvSnippet, depthSnippet); - } - - return snippet; - } - - generate(builder, output) { - const properties = builder.getNodeProperties(this); - - const texture = this.value; - - if (!texture || texture.isTexture !== true) { - throw new Error('TextureNode: Need a three.js texture.'); - } - - const textureProperty = super.generate(builder, 'property'); - - if (output === 'sampler') { - return textureProperty + '_sampler'; - } else if (builder.isReference(output)) { - return textureProperty; - } else { - const nodeData = builder.getDataFromNode(this); - - let propertyName = nodeData.propertyName; - - if (propertyName === undefined) { - const { uvNode, levelNode, biasNode, compareNode, depthNode, gradNode } = properties; - - const uvSnippet = this.generateUV(builder, uvNode); - const levelSnippet = levelNode ? levelNode.build(builder, 'float') : null; - const biasSnippet = biasNode ? biasNode.build(builder, 'float') : null; - const depthSnippet = depthNode ? depthNode.build(builder, 'int') : null; - const compareSnippet = compareNode ? compareNode.build(builder, 'float') : null; - const gradSnippet = gradNode - ? [gradNode[0].build(builder, 'vec2'), gradNode[1].build(builder, 'vec2')] - : null; - - const nodeVar = builder.getVarFromNode(this); - - propertyName = builder.getPropertyName(nodeVar); - - const snippet = this.generateSnippet( - builder, - textureProperty, - uvSnippet, - levelSnippet, - biasSnippet, - depthSnippet, - compareSnippet, - gradSnippet, - ); - - builder.addLineFlowCode(`${propertyName} = ${snippet}`); - - nodeData.snippet = snippet; - nodeData.propertyName = propertyName; - } - - let snippet = propertyName; - const nodeType = this.getNodeType(builder); - - if (builder.needsColorSpaceToLinear(texture)) { - snippet = colorSpaceToLinear(expression(snippet, nodeType), texture.colorSpace) - .setup(builder) - .build(builder, nodeType); - } - - return builder.format(snippet, nodeType, output); - } - } - - setSampler(value) { - this.sampler = value; - - return this; - } - - getSampler() { - return this.sampler; - } - - // @TODO: Move to TSL - - uv(uvNode) { - const textureNode = this.clone(); - textureNode.uvNode = nodeObject(uvNode); - textureNode.referenceNode = this; - - return nodeObject(textureNode); - } - - blur(amountNode) { - const textureNode = this.clone(); - textureNode.biasNode = nodeObject(amountNode).mul(maxMipLevel(textureNode)); - textureNode.referenceNode = this; - - return nodeObject(textureNode); - } - - level(levelNode) { - const textureNode = this.clone(); - textureNode.levelNode = nodeObject(levelNode); - textureNode.referenceNode = this; - - return nodeObject(textureNode); - } - - size(levelNode) { - return textureSize(this, levelNode); - } - - bias(biasNode) { - const textureNode = this.clone(); - textureNode.biasNode = nodeObject(biasNode); - textureNode.referenceNode = this; - - return nodeObject(textureNode); - } - - compare(compareNode) { - const textureNode = this.clone(); - textureNode.compareNode = nodeObject(compareNode); - textureNode.referenceNode = this; - - return nodeObject(textureNode); - } - - grad(gradNodeX, gradNodeY) { - const textureNode = this.clone(); - textureNode.gradNode = [nodeObject(gradNodeX), nodeObject(gradNodeY)]; - - textureNode.referenceNode = this; - - return nodeObject(textureNode); - } - - depth(depthNode) { - const textureNode = this.clone(); - textureNode.depthNode = nodeObject(depthNode); - textureNode.referenceNode = this; - - return nodeObject(textureNode); - } - - // -- - - serialize(data) { - super.serialize(data); - - data.value = this.value.toJSON(data.meta).uuid; - data.sampler = this.sampler; - data.updateMatrix = this.updateMatrix; - data.updateType = this.updateType; - } - - deserialize(data) { - super.deserialize(data); - - this.value = data.meta.textures[data.value]; - this.sampler = data.sampler; - this.updateMatrix = data.updateMatrix; - this.updateType = data.updateType; - } - - update() { - const texture = this.value; - const matrixUniform = this._matrixUniform; - - if (matrixUniform !== null) matrixUniform.value = texture.matrix; - - if (texture.matrixAutoUpdate === true) { - texture.updateMatrix(); - } - } - - clone() { - const newNode = new this.constructor(this.value, this.uvNode, this.levelNode, this.biasNode); - newNode.sampler = this.sampler; - - return newNode; - } -} - -export default TextureNode; - -export const texture = nodeProxy(TextureNode); -export const textureLoad = (...params) => texture(...params).setSampler(false); - -//export const textureLevel = ( value, uv, level ) => texture( value, uv ).level( level ); - -export const sampler = aTexture => (aTexture.isNode === true ? aTexture : texture(aTexture)).convert('sampler'); - -addNodeElement('texture', texture); -//addNodeElement( 'textureLevel', textureLevel ); - -addNodeClass('TextureNode', TextureNode); diff --git a/src-testing/src/nodes/accessors/UVNode.d.ts b/src-testing/src/nodes/accessors/UVNode.d.ts deleted file mode 100644 index 4fbe07f79..000000000 --- a/src-testing/src/nodes/accessors/UVNode.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -import AttributeNode from "../core/AttributeNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const uv: (index?: number) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/UniformArrayNode.d.ts b/src-testing/src/nodes/accessors/UniformArrayNode.d.ts deleted file mode 100644 index 6d02c0272..000000000 --- a/src-testing/src/nodes/accessors/UniformArrayNode.d.ts +++ /dev/null @@ -1,30 +0,0 @@ -import Node from "../core/Node.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import ArrayElementNode from "../utils/ArrayElementNode.js"; -import BufferNode from "./BufferNode.js"; - -declare class UniformArrayElementNode extends ArrayElementNode { - constructor(arrayBuffer: Node, indexNode: Node); -} - -declare class UniformArrayNode extends BufferNode { - array: unknown[]; - elementType: string | null; - - readonly isArrayBufferNode: true; - - constructor(value: unknown[], elementType?: string | null); - - getElementLength(): number; - - element(indexNode: number): ShaderNodeObject; -} - -export default UniformArrayNode; - -export const uniformArray: (values: unknown[], nodeType?: string | null) => ShaderNodeObject; - -/** - * @deprecated uniforms() has been renamed to uniformArray(). - */ -export const uniforms: (values: unknown[], nodeType?: string | null) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/UserDataNode.d.ts b/src-testing/src/nodes/accessors/UserDataNode.d.ts deleted file mode 100644 index e2204e302..000000000 --- a/src-testing/src/nodes/accessors/UserDataNode.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import ReferenceNode from "./ReferenceNode.js"; - -export type NodeUserData = Record; - -export default class UserDataNode extends ReferenceNode { - userData: NodeUserData | null; - constructor(property: string, inputType: string, userData?: NodeUserData | null); -} - -export const userData: ( - name: string, - inputType: string, - userData?: NodeUserData, -) => ShaderNodeObject; diff --git a/src-testing/src/nodes/accessors/VertexColorNode.d.ts b/src-testing/src/nodes/accessors/VertexColorNode.d.ts deleted file mode 100644 index 784ca3c5a..000000000 --- a/src-testing/src/nodes/accessors/VertexColorNode.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -import AttributeNode from "../core/AttributeNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class VertexColorNode extends AttributeNode { - readonly isVertexColorNode: true; - - index: number; - - constructor(index?: number); -} - -export const vertexColor: (index?: number) => ShaderNodeObject; diff --git a/src-testing/src/nodes/code/CodeNode.ts b/src-testing/src/nodes/code/CodeNode.ts deleted file mode 100644 index 063475643..000000000 --- a/src-testing/src/nodes/code/CodeNode.ts +++ /dev/null @@ -1,66 +0,0 @@ -import Node, { addNodeClass } from '../core/Node.js'; -import { nodeProxy } from '../shadernode/ShaderNode.js'; - -class CodeNode extends Node { - constructor(code = '', includes = [], language = '') { - super('code'); - - this.isCodeNode = true; - - this.code = code; - this.language = language; - - this.includes = includes; - } - - isGlobal() { - return true; - } - - setIncludes(includes) { - this.includes = includes; - - return this; - } - - getIncludes(/*builder*/) { - return this.includes; - } - - generate(builder) { - const includes = this.getIncludes(builder); - - for (const include of includes) { - include.build(builder); - } - - const nodeCode = builder.getCodeFromNode(this, this.getNodeType(builder)); - nodeCode.code = this.code; - - return nodeCode.code; - } - - serialize(data) { - super.serialize(data); - - data.code = this.code; - data.language = this.language; - } - - deserialize(data) { - super.deserialize(data); - - this.code = data.code; - this.language = data.language; - } -} - -export default CodeNode; - -export const code = nodeProxy(CodeNode); - -export const js = (src, includes) => code(src, includes, 'js'); -export const wgsl = (src, includes) => code(src, includes, 'wgsl'); -export const glsl = (src, includes) => code(src, includes, 'glsl'); - -addNodeClass('CodeNode', CodeNode); diff --git a/src-testing/src/nodes/code/ExpressionNode.d.ts b/src-testing/src/nodes/code/ExpressionNode.d.ts deleted file mode 100644 index 20683848a..000000000 --- a/src-testing/src/nodes/code/ExpressionNode.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import TempNode from "../core/TempNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class ExpressionNode extends TempNode { - snipped: string; /* sic */ - constructor(snipped?: string, nodeType?: string); -} - -export const expression: (snipped?: string, nodeType?: string) => ShaderNodeObject; diff --git a/src-testing/src/nodes/code/FunctionCallNode.d.ts b/src-testing/src/nodes/code/FunctionCallNode.d.ts deleted file mode 100644 index d3766c577..000000000 --- a/src-testing/src/nodes/code/FunctionCallNode.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { ProxiedObject, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import FunctionNode, { FunctionNodeArguments } from "./FunctionNode.js"; - -export default class FunctionCallNode

extends TempNode { - functionNode: FunctionNode

; - parameters: { [name: string]: Node }; - - constructor(functionNode?: FunctionNode

, parameters?: P); - - setParameters(parameters: P): this; - getParameters(): P; -} - -export const call:

( - functionNode?: FunctionNode

, - parameters?: ProxiedObject

, -) => ShaderNodeObject>; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - call: typeof call; - } -} diff --git a/src-testing/src/nodes/code/FunctionNode.ts b/src-testing/src/nodes/code/FunctionNode.ts deleted file mode 100644 index feeb5a55c..000000000 --- a/src-testing/src/nodes/code/FunctionNode.ts +++ /dev/null @@ -1,100 +0,0 @@ -import CodeNode from './CodeNode.js'; -import { addNodeClass } from '../core/Node.js'; -import { nodeObject } from '../shadernode/ShaderNode.js'; - -class FunctionNode extends CodeNode { - constructor(code = '', includes = [], language = '') { - super(code, includes, language); - - this.keywords = {}; - } - - getNodeType(builder) { - return this.getNodeFunction(builder).type; - } - - getInputs(builder) { - return this.getNodeFunction(builder).inputs; - } - - getNodeFunction(builder) { - const nodeData = builder.getDataFromNode(this); - - let nodeFunction = nodeData.nodeFunction; - - if (nodeFunction === undefined) { - nodeFunction = builder.parser.parseFunction(this.code); - - nodeData.nodeFunction = nodeFunction; - } - - return nodeFunction; - } - - generate(builder, output) { - super.generate(builder); - - const nodeFunction = this.getNodeFunction(builder); - - const name = nodeFunction.name; - const type = nodeFunction.type; - - const nodeCode = builder.getCodeFromNode(this, type); - - if (name !== '') { - // use a custom property name - - nodeCode.name = name; - } - - const propertyName = builder.getPropertyName(nodeCode); - - let code = this.getNodeFunction(builder).getCode(propertyName); - - const keywords = this.keywords; - const keywordsProperties = Object.keys(keywords); - - if (keywordsProperties.length > 0) { - for (const property of keywordsProperties) { - const propertyRegExp = new RegExp(`\\b${property}\\b`, 'g'); - const nodeProperty = keywords[property].build(builder, 'property'); - - code = code.replace(propertyRegExp, nodeProperty); - } - } - - nodeCode.code = code + '\n'; - - if (output === 'property') { - return propertyName; - } else { - return builder.format(`${propertyName}()`, type, output); - } - } -} - -export default FunctionNode; - -const nativeFn = (code, includes = [], language = '') => { - for (let i = 0; i < includes.length; i++) { - const include = includes[i]; - - // TSL Function: glslFn, wgslFn - - if (typeof include === 'function') { - includes[i] = include.functionNode; - } - } - - const functionNode = nodeObject(new FunctionNode(code, includes, language)); - - const fn = (...params) => functionNode.call(...params); - fn.functionNode = functionNode; - - return fn; -}; - -export const glslFn = (code, includes) => nativeFn(code, includes, 'glsl'); -export const wgslFn = (code, includes) => nativeFn(code, includes, 'wgsl'); - -addNodeClass('FunctionNode', FunctionNode); diff --git a/src-testing/src/nodes/core/AssignNode.d.ts b/src-testing/src/nodes/core/AssignNode.d.ts deleted file mode 100644 index e6809fe50..000000000 --- a/src-testing/src/nodes/core/AssignNode.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import Node from "./Node.js"; -import NodeBuilder from "./NodeBuilder.js"; -import TempNode from "./TempNode.js"; - -export default class AssignNode extends TempNode { - constructor(targetNode: Node, sourceNode: Node); - - needsSplitAssign(builder: NodeBuilder): boolean; -} - -export const assign: (targetNode: NodeRepresentation, sourceNode: NodeRepresentation) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - assign: typeof assign; - } -} diff --git a/src-testing/src/nodes/core/AttributeNode.d.ts b/src-testing/src/nodes/core/AttributeNode.d.ts deleted file mode 100644 index e2d37c972..000000000 --- a/src-testing/src/nodes/core/AttributeNode.d.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import Node from "./Node.js"; -import NodeBuilder from "./NodeBuilder.js"; - -export default class AttributeNode extends Node { - defaultNode: Node | null; - - constructor(attributeName: string, nodeType?: string | null, defaultNode?: Node | null); - - setAttributeName(attributeName: string): this; - - getAttributeName(builder: NodeBuilder): string; -} - -export const attribute: ( - name: string, - nodeType?: string | null, - defaultNode?: NodeRepresentation, -) => ShaderNodeObject; diff --git a/src-testing/src/nodes/core/BypassNode.d.ts b/src-testing/src/nodes/core/BypassNode.d.ts deleted file mode 100644 index e9dbef2cf..000000000 --- a/src-testing/src/nodes/core/BypassNode.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import Node from "./Node.js"; - -export default class BypassNode extends Node { - isBypassNode: true; - outputNode: Node; - callNode: Node; - - constructor(returnNode: Node, callNode: Node); -} - -export const bypass: (returnNode: NodeRepresentation, callNode: NodeRepresentation) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - bypass: typeof bypass; - } -} diff --git a/src-testing/src/nodes/core/CacheNode.d.ts b/src-testing/src/nodes/core/CacheNode.d.ts deleted file mode 100644 index d3e4748e8..000000000 --- a/src-testing/src/nodes/core/CacheNode.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import Node from "./Node.js"; -import NodeCache from "./NodeCache.js"; - -export default class CacheNode extends Node { - node: Node; - parent: boolean; - - readonly isCacheNode: true; - - constructor(node: Node, parent?: boolean); -} - -export const cache: (node: Node, cache?: NodeCache) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - cache: typeof cache; - } -} diff --git a/src-testing/src/nodes/core/ConstNode.d.ts b/src-testing/src/nodes/core/ConstNode.d.ts deleted file mode 100644 index f866b0c0c..000000000 --- a/src-testing/src/nodes/core/ConstNode.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import InputNode from "./InputNode.js"; -import NodeBuilder from "./NodeBuilder.js"; - -export default class ConstNode extends InputNode { - isConstNode: true; - constructor(value: Value, nodeType?: string | null); - - generateConst(builder: NodeBuilder): string; -} diff --git a/src-testing/src/nodes/core/ContextNode.ts b/src-testing/src/nodes/core/ContextNode.ts deleted file mode 100644 index fcd488eba..000000000 --- a/src-testing/src/nodes/core/ContextNode.ts +++ /dev/null @@ -1,55 +0,0 @@ -import Node, { addNodeClass } from './Node.js'; -import { addNodeElement, nodeProxy } from '../shadernode/ShaderNode.js'; - -class ContextNode extends Node { - constructor(node, context = {}) { - super(); - - this.isContextNode = true; - - this.node = node; - this.context = context; - } - - getNodeType(builder) { - return this.node.getNodeType(builder); - } - - analyze(builder) { - this.node.build(builder); - } - - setup(builder) { - const previousContext = builder.getContext(); - - builder.setContext({ ...builder.context, ...this.context }); - - const node = this.node.build(builder); - - builder.setContext(previousContext); - - return node; - } - - generate(builder, output) { - const previousContext = builder.getContext(); - - builder.setContext({ ...builder.context, ...this.context }); - - const snippet = this.node.build(builder, output); - - builder.setContext(previousContext); - - return snippet; - } -} - -export default ContextNode; - -export const context = nodeProxy(ContextNode); -export const label = (node, name) => context(node, { label: name }); - -addNodeElement('context', context); -addNodeElement('label', label); - -addNodeClass('ContextNode', ContextNode); diff --git a/src-testing/src/nodes/core/IndexNode.d.ts b/src-testing/src/nodes/core/IndexNode.d.ts deleted file mode 100644 index b6b52f718..000000000 --- a/src-testing/src/nodes/core/IndexNode.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import Node from "./Node.js"; - -export type IndexNodeScope = typeof IndexNode.VERTEX | typeof IndexNode.INSTANCE | typeof IndexNode.DRAW; - -export default class IndexNode extends Node { - scope: IndexNodeScope; - - readonly isInstanceNode: true; - - constructor(scope: IndexNodeScope); - - static VERTEX: "vertex"; - static INSTANCE: "instance"; - static DRAW: "draw"; -} - -export const vertexIndex: ShaderNodeObject; -export const instanceIndex: ShaderNodeObject; -export const drawIndex: ShaderNodeObject; diff --git a/src-testing/src/nodes/core/InputNode.ts b/src-testing/src/nodes/core/InputNode.ts deleted file mode 100644 index 4d52ec26f..000000000 --- a/src-testing/src/nodes/core/InputNode.ts +++ /dev/null @@ -1,65 +0,0 @@ -import Node, { addNodeClass } from './Node.js'; -import { getValueType, getValueFromType, arrayBufferToBase64 } from './NodeUtils.js'; - -class InputNode extends Node { - constructor(value, nodeType = null) { - super(nodeType); - - this.isInputNode = true; - - this.value = value; - this.precision = null; - } - - getNodeType(/*builder*/) { - if (this.nodeType === null) { - return getValueType(this.value); - } - - return this.nodeType; - } - - getInputType(builder) { - return this.getNodeType(builder); - } - - setPrecision(precision) { - this.precision = precision; - - return this; - } - - serialize(data) { - super.serialize(data); - - data.value = this.value; - - if (this.value && this.value.toArray) data.value = this.value.toArray(); - - data.valueType = getValueType(this.value); - data.nodeType = this.nodeType; - - if (data.valueType === 'ArrayBuffer') data.value = arrayBufferToBase64(data.value); - - data.precision = this.precision; - } - - deserialize(data) { - super.deserialize(data); - - this.nodeType = data.nodeType; - this.value = Array.isArray(data.value) ? getValueFromType(data.valueType, ...data.value) : data.value; - - this.precision = data.precision || null; - - if (this.value && this.value.fromArray) this.value = this.value.fromArray(data.value); - } - - generate(/*builder, output*/) { - console.warn('Abstract function.'); - } -} - -export default InputNode; - -addNodeClass('InputNode', InputNode); diff --git a/src-testing/src/nodes/core/LightingModel.d.ts b/src-testing/src/nodes/core/LightingModel.d.ts deleted file mode 100644 index f64dd07db..000000000 --- a/src-testing/src/nodes/core/LightingModel.d.ts +++ /dev/null @@ -1,46 +0,0 @@ -import Node from "./Node.js"; -import NodeBuilder from "./NodeBuilder.js"; -import StackNode from "./StackNode.js"; - -export interface LightingModelReflectedLight { - directDiffuse: Node; - directSpecular: Node; - indirectDiffuse: Node; - indirectSpecular: Node; -} - -export interface LightingModelDirectInput { - lightDirection: Node; - lightColor: Node; - reflectedLight: LightingModelReflectedLight; -} - -export interface LightingModelDirectRectAreaInput { - lightColor: Node; - lightPosition: Node; - halfWidth: Node; - halfHeight: Node; - reflectedLight: LightingModelReflectedLight; - ltc_1: Node; - ltc_2: Node; -} - -export interface LightingModelIndirectInput { - radiance: Node; - irradiance: Node; - iblIrradiance: Node; - ambientOcclusion: Node; - reflectedLight: LightingModelReflectedLight; - backdrop: Node; - backdropAlpha: Node; - outgoingLight: Node; -} - -export default class LightingModel { - start(input: LightingModelIndirectInput, stack: StackNode, builder: NodeBuilder): void; - finish(input: LightingModelIndirectInput, stack: StackNode, builder: NodeBuilder): void; - direct(input: LightingModelDirectInput, stack: StackNode, builder: NodeBuilder): void; - directRectArea(input: LightingModelDirectRectAreaInput, stack: StackNode, builder: NodeBuilder): void; - indirect(input: LightingModelIndirectInput, stack: StackNode, builder: NodeBuilder): void; - ambientOcclusion(input: LightingModelIndirectInput, stack: StackNode, builder: NodeBuilder): void; -} diff --git a/src-testing/src/nodes/core/MRTNode.d.ts b/src-testing/src/nodes/core/MRTNode.d.ts deleted file mode 100644 index ec993f27b..000000000 --- a/src-testing/src/nodes/core/MRTNode.d.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { Node } from "../Nodes.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import OutputStructNode from "./OutputStructNode.js"; - -declare class MRTNode extends OutputStructNode { - outputNodes: { [name: string]: Node }; - - readonly isMRTNode: true; - - constructor(outputNodes: { [name: string]: Node }); - - getNode(name: string): Node; - - merge(mrtNode: MRTNode): ShaderNodeObject; -} - -export default MRTNode; - -export const mrt: (outputNodes: { [name: string]: Node }) => ShaderNodeObject; diff --git a/src-testing/src/nodes/core/Node.ts b/src-testing/src/nodes/core/Node.ts deleted file mode 100644 index e67dcb707..000000000 --- a/src-testing/src/nodes/core/Node.ts +++ /dev/null @@ -1,417 +0,0 @@ -import { NodeUpdateType } from './constants.js'; -import { getNodeChildren, getCacheKey } from './NodeUtils.js'; - -import { EventDispatcher } from '../../core/EventDispatcher.js'; -import { MathUtils } from '../../math/MathUtils.js'; - -const NodeClasses = new Map(); - -let _nodeId = 0; - -class Node extends EventDispatcher { - constructor(nodeType = null) { - super(); - - this.nodeType = nodeType; - - this.updateType = NodeUpdateType.NONE; - this.updateBeforeType = NodeUpdateType.NONE; - this.updateAfterType = NodeUpdateType.NONE; - - this.uuid = MathUtils.generateUUID(); - - this.version = 0; - - this._cacheKey = null; - this._cacheKeyVersion = 0; - - this.global = false; - - this.isNode = true; - - Object.defineProperty(this, 'id', { value: _nodeId++ }); - } - - set needsUpdate(value) { - if (value === true) { - this.version++; - } - } - - get type() { - return this.constructor.type; - } - - onUpdate(callback, updateType) { - this.updateType = updateType; - this.update = callback.bind(this.getSelf()); - - return this; - } - - onFrameUpdate(callback) { - return this.onUpdate(callback, NodeUpdateType.FRAME); - } - - onRenderUpdate(callback) { - return this.onUpdate(callback, NodeUpdateType.RENDER); - } - - onObjectUpdate(callback) { - return this.onUpdate(callback, NodeUpdateType.OBJECT); - } - - onReference(callback) { - this.updateReference = callback.bind(this.getSelf()); - - return this; - } - - getSelf() { - // Returns non-node object. - - return this.self || this; - } - - updateReference(/*state*/) { - return this; - } - - isGlobal(/*builder*/) { - return this.global; - } - - *getChildren() { - for (const { childNode } of getNodeChildren(this)) { - yield childNode; - } - } - - dispose() { - this.dispatchEvent({ type: 'dispose' }); - } - - traverse(callback) { - callback(this); - - for (const childNode of this.getChildren()) { - childNode.traverse(callback); - } - } - - getCacheKey(force = false) { - force = force || this.version !== this._cacheKeyVersion; - - if (force === true || this._cacheKey === null) { - this._cacheKey = getCacheKey(this, force); - this._cacheKeyVersion = this.version; - } - - return this._cacheKey; - } - - getHash(/*builder*/) { - return this.uuid; - } - - getUpdateType() { - return this.updateType; - } - - getUpdateBeforeType() { - return this.updateBeforeType; - } - - getUpdateAfterType() { - return this.updateAfterType; - } - - getElementType(builder) { - const type = this.getNodeType(builder); - const elementType = builder.getElementType(type); - - return elementType; - } - - getNodeType(builder) { - const nodeProperties = builder.getNodeProperties(this); - - if (nodeProperties.outputNode) { - return nodeProperties.outputNode.getNodeType(builder); - } - - return this.nodeType; - } - - getShared(builder) { - const hash = this.getHash(builder); - const nodeFromHash = builder.getNodeFromHash(hash); - - return nodeFromHash || this; - } - - setup(builder) { - const nodeProperties = builder.getNodeProperties(this); - - let index = 0; - - for (const childNode of this.getChildren()) { - nodeProperties['node' + index++] = childNode; - } - - // return a outputNode if exists - return null; - } - - increaseUsage(builder) { - const nodeData = builder.getDataFromNode(this); - nodeData.usageCount = nodeData.usageCount === undefined ? 1 : nodeData.usageCount + 1; - - return nodeData.usageCount; - } - - analyze(builder) { - const usageCount = this.increaseUsage(builder); - - if (usageCount === 1) { - // node flow children - - const nodeProperties = builder.getNodeProperties(this); - - for (const childNode of Object.values(nodeProperties)) { - if (childNode && childNode.isNode === true) { - childNode.build(builder); - } - } - } - } - - generate(builder, output) { - const { outputNode } = builder.getNodeProperties(this); - - if (outputNode && outputNode.isNode === true) { - return outputNode.build(builder, output); - } - } - - updateBefore(/*frame*/) { - console.warn('Abstract function.'); - } - - updateAfter(/*frame*/) { - console.warn('Abstract function.'); - } - - update(/*frame*/) { - console.warn('Abstract function.'); - } - - build(builder, output = null) { - const refNode = this.getShared(builder); - - if (this !== refNode) { - return refNode.build(builder, output); - } - - builder.addNode(this); - builder.addChain(this); - - /* Build stages expected results: - - "setup" -> Node - - "analyze" -> null - - "generate" -> String - */ - let result = null; - - const buildStage = builder.getBuildStage(); - - if (buildStage === 'setup') { - this.updateReference(builder); - - const properties = builder.getNodeProperties(this); - - if (properties.initialized !== true) { - const stackNodesBeforeSetup = builder.stack.nodes.length; - - properties.initialized = true; - properties.outputNode = this.setup(builder); - - if (properties.outputNode !== null && builder.stack.nodes.length !== stackNodesBeforeSetup) { - properties.outputNode = builder.stack; - } - - for (const childNode of Object.values(properties)) { - if (childNode && childNode.isNode === true) { - childNode.build(builder); - } - } - } - } else if (buildStage === 'analyze') { - this.analyze(builder); - } else if (buildStage === 'generate') { - const isGenerateOnce = this.generate.length === 1; - - if (isGenerateOnce) { - const type = this.getNodeType(builder); - const nodeData = builder.getDataFromNode(this); - - result = nodeData.snippet; - - if (result === undefined) { - result = this.generate(builder) || ''; - - nodeData.snippet = result; - } - - result = builder.format(result, type, output); - } else { - result = this.generate(builder, output) || ''; - } - } - - builder.removeChain(this); - - return result; - } - - getSerializeChildren() { - return getNodeChildren(this); - } - - serialize(json) { - const nodeChildren = this.getSerializeChildren(); - - const inputNodes = {}; - - for (const { property, index, childNode } of nodeChildren) { - if (index !== undefined) { - if (inputNodes[property] === undefined) { - inputNodes[property] = Number.isInteger(index) ? [] : {}; - } - - inputNodes[property][index] = childNode.toJSON(json.meta).uuid; - } else { - inputNodes[property] = childNode.toJSON(json.meta).uuid; - } - } - - if (Object.keys(inputNodes).length > 0) { - json.inputNodes = inputNodes; - } - } - - deserialize(json) { - if (json.inputNodes !== undefined) { - const nodes = json.meta.nodes; - - for (const property in json.inputNodes) { - if (Array.isArray(json.inputNodes[property])) { - const inputArray = []; - - for (const uuid of json.inputNodes[property]) { - inputArray.push(nodes[uuid]); - } - - this[property] = inputArray; - } else if (typeof json.inputNodes[property] === 'object') { - const inputObject = {}; - - for (const subProperty in json.inputNodes[property]) { - const uuid = json.inputNodes[property][subProperty]; - - inputObject[subProperty] = nodes[uuid]; - } - - this[property] = inputObject; - } else { - const uuid = json.inputNodes[property]; - - this[property] = nodes[uuid]; - } - } - } - } - - toJSON(meta) { - const { uuid, type } = this; - const isRoot = meta === undefined || typeof meta === 'string'; - - if (isRoot) { - meta = { - textures: {}, - images: {}, - nodes: {}, - }; - } - - // serialize - - let data = meta.nodes[uuid]; - - if (data === undefined) { - data = { - uuid, - type, - meta, - metadata: { - version: 4.6, - type: 'Node', - generator: 'Node.toJSON', - }, - }; - - if (isRoot !== true) meta.nodes[data.uuid] = data; - - this.serialize(data); - - delete data.meta; - } - - // TODO: Copied from Object3D.toJSON - - function extractFromCache(cache) { - const values = []; - - for (const key in cache) { - const data = cache[key]; - delete data.metadata; - values.push(data); - } - - return values; - } - - if (isRoot) { - const textures = extractFromCache(meta.textures); - const images = extractFromCache(meta.images); - const nodes = extractFromCache(meta.nodes); - - if (textures.length > 0) data.textures = textures; - if (images.length > 0) data.images = images; - if (nodes.length > 0) data.nodes = nodes; - } - - return data; - } -} - -export default Node; - -export function addNodeClass(type, nodeClass) { - if (typeof nodeClass !== 'function' || !type) throw new Error(`Node class ${type} is not a class`); - if (NodeClasses.has(type)) { - console.warn(`Redefinition of node class ${type}`); - return; - } - - NodeClasses.set(type, nodeClass); - nodeClass.type = type; -} - -export function createNodeFromType(type) { - const Class = NodeClasses.get(type); - - if (Class !== undefined) { - return new Class(); - } -} diff --git a/src-testing/src/nodes/core/NodeAttribute.ts b/src-testing/src/nodes/core/NodeAttribute.ts deleted file mode 100644 index 190fe8c51..000000000 --- a/src-testing/src/nodes/core/NodeAttribute.ts +++ /dev/null @@ -1,11 +0,0 @@ -class NodeAttribute { - constructor(name, type, node = null) { - this.isNodeAttribute = true; - - this.name = name; - this.type = type; - this.node = node; - } -} - -export default NodeAttribute; diff --git a/src-testing/src/nodes/core/NodeBuilder.ts b/src-testing/src/nodes/core/NodeBuilder.ts deleted file mode 100644 index 84ea6befa..000000000 --- a/src-testing/src/nodes/core/NodeBuilder.ts +++ /dev/null @@ -1,1128 +0,0 @@ -import NodeUniform from './NodeUniform.js'; -import NodeAttribute from './NodeAttribute.js'; -import NodeVarying from './NodeVarying.js'; -import NodeVar from './NodeVar.js'; -import NodeCode from './NodeCode.js'; -import NodeKeywords from './NodeKeywords.js'; -import NodeCache from './NodeCache.js'; -import ParameterNode from './ParameterNode.js'; -import FunctionNode from '../code/FunctionNode.js'; -import { createNodeMaterialFromType, default as NodeMaterial } from '../materials/NodeMaterial.js'; -import { NodeUpdateType, defaultBuildStages, shaderStages } from './constants.js'; - -import { - NumberNodeUniform, - Vector2NodeUniform, - Vector3NodeUniform, - Vector4NodeUniform, - ColorNodeUniform, - Matrix3NodeUniform, - Matrix4NodeUniform, -} from '../../renderers/common/nodes/NodeUniform.js'; - -import { stack } from './StackNode.js'; -import { getCurrentStack, setCurrentStack } from '../shadernode/ShaderNode.js'; - -import CubeRenderTarget from '../../renderers/common/CubeRenderTarget.js'; -import ChainMap from '../../renderers/common/ChainMap.js'; - -import PMREMGenerator from '../../renderers/common/extras/PMREMGenerator.js'; - -import BindGroup from '../../renderers/common/BindGroup.js'; - -import { REVISION } from '../../constants.js'; -import { RenderTarget } from '../../core/RenderTarget.js'; -import { Color } from '../../math/Color.js'; -import { Vector2 } from '../../math/Vector2.js'; -import { Vector3 } from '../../math/Vector3.js'; -import { Vector4 } from '../../math/Vector4.js'; -import { Float16BufferAttribute } from '../../core/BufferAttribute.js'; -import { - IntType, - UnsignedIntType, - LinearFilter, - LinearMipmapNearestFilter, - NearestMipmapLinearFilter, - LinearMipmapLinearFilter, -} from '../../constants.js'; - -const rendererCache = new WeakMap(); - -const typeFromLength = new Map([ - [2, 'vec2'], - [3, 'vec3'], - [4, 'vec4'], - [9, 'mat3'], - [16, 'mat4'], -]); - -const typeFromArray = new Map([ - [Int8Array, 'int'], - [Int16Array, 'int'], - [Int32Array, 'int'], - [Uint8Array, 'uint'], - [Uint16Array, 'uint'], - [Uint32Array, 'uint'], - [Float32Array, 'float'], -]); - -const toFloat = value => { - value = Number(value); - - return value + (value % 1 ? '' : '.0'); -}; - -class NodeBuilder { - constructor(object, renderer, parser) { - this.object = object; - this.material = (object && object.material) || null; - this.geometry = (object && object.geometry) || null; - this.renderer = renderer; - this.parser = parser; - this.scene = null; - this.camera = null; - - this.nodes = []; - this.updateNodes = []; - this.updateBeforeNodes = []; - this.updateAfterNodes = []; - this.hashNodes = {}; - - this.lightsNode = null; - this.environmentNode = null; - this.fogNode = null; - - this.clippingContext = null; - - this.vertexShader = null; - this.fragmentShader = null; - this.computeShader = null; - - this.flowNodes = { vertex: [], fragment: [], compute: [] }; - this.flowCode = { vertex: '', fragment: '', compute: '' }; - this.uniforms = { vertex: [], fragment: [], compute: [], index: 0 }; - this.structs = { vertex: [], fragment: [], compute: [], index: 0 }; - this.bindings = { vertex: {}, fragment: {}, compute: {} }; - this.bindingsIndexes = {}; - this.bindGroups = null; - this.attributes = []; - this.bufferAttributes = []; - this.varyings = []; - this.codes = {}; - this.vars = {}; - this.flow = { code: '' }; - this.chaining = []; - this.stack = stack(); - this.stacks = []; - this.tab = '\t'; - - this.instanceBindGroups = true; - - this.currentFunctionNode = null; - - this.context = { - keywords: new NodeKeywords(), - material: this.material, - }; - - this.cache = new NodeCache(); - this.globalCache = this.cache; - - this.flowsData = new WeakMap(); - - this.shaderStage = null; - this.buildStage = null; - - this.useComparisonMethod = false; - } - - getBindGroupsCache() { - let bindGroupsCache = rendererCache.get(this.renderer); - - if (bindGroupsCache === undefined) { - bindGroupsCache = new ChainMap(); - - rendererCache.set(this.renderer, bindGroupsCache); - } - - return bindGroupsCache; - } - - createRenderTarget(width, height, options) { - return new RenderTarget(width, height, options); - } - - createCubeRenderTarget(size, options) { - return new CubeRenderTarget(size, options); - } - - createPMREMGenerator() { - // TODO: Move Materials.js to outside of the Nodes.js in order to remove this function and improve tree-shaking support - - return new PMREMGenerator(this.renderer); - } - - includes(node) { - return this.nodes.includes(node); - } - - _getBindGroup(groupName, bindings) { - const bindGroupsCache = this.getBindGroupsCache(); - - // - - const bindingsArray = []; - - let sharedGroup = true; - - for (const binding of bindings) { - bindingsArray.push(binding); - - sharedGroup = sharedGroup && binding.groupNode.shared !== true; - } - - // - - let bindGroup; - - if (sharedGroup) { - bindGroup = bindGroupsCache.get(bindingsArray); - - if (bindGroup === undefined) { - bindGroup = new BindGroup(groupName, bindingsArray, this.bindingsIndexes[groupName].group); - - bindGroupsCache.set(bindingsArray, bindGroup); - } - } else { - bindGroup = new BindGroup(groupName, bindingsArray, this.bindingsIndexes[groupName].group); - } - - return bindGroup; - } - - getBindGroupArray(groupName, shaderStage) { - const bindings = this.bindings[shaderStage]; - - let bindGroup = bindings[groupName]; - - if (bindGroup === undefined) { - if (this.bindingsIndexes[groupName] === undefined) { - this.bindingsIndexes[groupName] = { binding: 0, group: Object.keys(this.bindingsIndexes).length }; - } - - bindings[groupName] = bindGroup = []; - } - - return bindGroup; - } - - getBindings() { - let bindingsGroups = this.bindGroups; - - if (bindingsGroups === null) { - const groups = {}; - const bindings = this.bindings; - - for (const shaderStage of shaderStages) { - for (const groupName in bindings[shaderStage]) { - const uniforms = bindings[shaderStage][groupName]; - - const groupUniforms = groups[groupName] || (groups[groupName] = []); - groupUniforms.push(...uniforms); - } - } - - bindingsGroups = []; - - for (const groupName in groups) { - const group = groups[groupName]; - - const bindingsGroup = this._getBindGroup(groupName, group); - - bindingsGroups.push(bindingsGroup); - } - - this.bindGroups = bindingsGroups; - } - - return bindingsGroups; - } - - setHashNode(node, hash) { - this.hashNodes[hash] = node; - } - - addNode(node) { - if (this.nodes.includes(node) === false) { - this.nodes.push(node); - - this.setHashNode(node, node.getHash(this)); - } - } - - buildUpdateNodes() { - for (const node of this.nodes) { - const updateType = node.getUpdateType(); - const updateBeforeType = node.getUpdateBeforeType(); - const updateAfterType = node.getUpdateAfterType(); - - if (updateType !== NodeUpdateType.NONE) { - this.updateNodes.push(node.getSelf()); - } - - if (updateBeforeType !== NodeUpdateType.NONE) { - this.updateBeforeNodes.push(node); - } - - if (updateAfterType !== NodeUpdateType.NONE) { - this.updateAfterNodes.push(node); - } - } - } - - get currentNode() { - return this.chaining[this.chaining.length - 1]; - } - - isFilteredTexture(texture) { - return ( - texture.magFilter === LinearFilter || - texture.magFilter === LinearMipmapNearestFilter || - texture.magFilter === NearestMipmapLinearFilter || - texture.magFilter === LinearMipmapLinearFilter || - texture.minFilter === LinearFilter || - texture.minFilter === LinearMipmapNearestFilter || - texture.minFilter === NearestMipmapLinearFilter || - texture.minFilter === LinearMipmapLinearFilter - ); - } - - addChain(node) { - /* - if ( this.chaining.indexOf( node ) !== - 1 ) { - - console.warn( 'Recursive node: ', node ); - - } - */ - - this.chaining.push(node); - } - - removeChain(node) { - const lastChain = this.chaining.pop(); - - if (lastChain !== node) { - throw new Error('NodeBuilder: Invalid node chaining!'); - } - } - - getMethod(method) { - return method; - } - - getNodeFromHash(hash) { - return this.hashNodes[hash]; - } - - addFlow(shaderStage, node) { - this.flowNodes[shaderStage].push(node); - - return node; - } - - setContext(context) { - this.context = context; - } - - getContext() { - return this.context; - } - - getSharedContext() { - const context = { ...this.context }; - - delete context.keywords; - delete context.material; - - return this.context; - } - - setCache(cache) { - this.cache = cache; - } - - getCache() { - return this.cache; - } - - getCacheFromNode(node, parent = true) { - const data = this.getDataFromNode(node); - if (data.cache === undefined) data.cache = new NodeCache(parent ? this.getCache() : null); - - return data.cache; - } - - isAvailable(/*name*/) { - return false; - } - - getVertexIndex() { - console.warn('Abstract function.'); - } - - getInstanceIndex() { - console.warn('Abstract function.'); - } - - getDrawIndex() { - console.warn('Abstract function.'); - } - - getFrontFacing() { - console.warn('Abstract function.'); - } - - getFragCoord() { - console.warn('Abstract function.'); - } - - isFlipY() { - return false; - } - - generateTexture(/* texture, textureProperty, uvSnippet */) { - console.warn('Abstract function.'); - } - - generateTextureLod(/* texture, textureProperty, uvSnippet, levelSnippet */) { - console.warn('Abstract function.'); - } - - generateConst(type, value = null) { - if (value === null) { - if (type === 'float' || type === 'int' || type === 'uint') value = 0; - else if (type === 'bool') value = false; - else if (type === 'color') value = new Color(); - else if (type === 'vec2') value = new Vector2(); - else if (type === 'vec3') value = new Vector3(); - else if (type === 'vec4') value = new Vector4(); - } - - if (type === 'float') return toFloat(value); - if (type === 'int') return `${Math.round(value)}`; - if (type === 'uint') return value >= 0 ? `${Math.round(value)}u` : '0u'; - if (type === 'bool') return value ? 'true' : 'false'; - if (type === 'color') - return `${this.getType('vec3')}( ${toFloat(value.r)}, ${toFloat(value.g)}, ${toFloat(value.b)} )`; - - const typeLength = this.getTypeLength(type); - - const componentType = this.getComponentType(type); - - const generateConst = value => this.generateConst(componentType, value); - - if (typeLength === 2) { - return `${this.getType(type)}( ${generateConst(value.x)}, ${generateConst(value.y)} )`; - } else if (typeLength === 3) { - return `${this.getType(type)}( ${generateConst(value.x)}, ${generateConst(value.y)}, ${generateConst(value.z)} )`; - } else if (typeLength === 4) { - return `${this.getType(type)}( ${generateConst(value.x)}, ${generateConst(value.y)}, ${generateConst(value.z)}, ${generateConst(value.w)} )`; - } else if (typeLength > 4 && value && (value.isMatrix3 || value.isMatrix4)) { - return `${this.getType(type)}( ${value.elements.map(generateConst).join(', ')} )`; - } else if (typeLength > 4) { - return `${this.getType(type)}()`; - } - - throw new Error(`NodeBuilder: Type '${type}' not found in generate constant attempt.`); - } - - getType(type) { - if (type === 'color') return 'vec3'; - - return type; - } - - hasGeometryAttribute(name) { - return this.geometry && this.geometry.getAttribute(name) !== undefined; - } - - getAttribute(name, type) { - const attributes = this.attributes; - - // find attribute - - for (const attribute of attributes) { - if (attribute.name === name) { - return attribute; - } - } - - // create a new if no exist - - const attribute = new NodeAttribute(name, type); - - attributes.push(attribute); - - return attribute; - } - - getPropertyName(node /*, shaderStage*/) { - return node.name; - } - - isVector(type) { - return /vec\d/.test(type); - } - - isMatrix(type) { - return /mat\d/.test(type); - } - - isReference(type) { - return ( - type === 'void' || - type === 'property' || - type === 'sampler' || - type === 'texture' || - type === 'cubeTexture' || - type === 'storageTexture' || - type === 'depthTexture' || - type === 'texture3D' - ); - } - - needsColorSpaceToLinear(/*texture*/) { - return false; - } - - getComponentTypeFromTexture(texture) { - const type = texture.type; - - if (texture.isDataTexture) { - if (type === IntType) return 'int'; - if (type === UnsignedIntType) return 'uint'; - } - - return 'float'; - } - - getElementType(type) { - if (type === 'mat2') return 'vec2'; - if (type === 'mat3') return 'vec3'; - if (type === 'mat4') return 'vec4'; - - return this.getComponentType(type); - } - - getComponentType(type) { - type = this.getVectorType(type); - - if (type === 'float' || type === 'bool' || type === 'int' || type === 'uint') return type; - - const componentType = /(b|i|u|)(vec|mat)([2-4])/.exec(type); - - if (componentType === null) return null; - - if (componentType[1] === 'b') return 'bool'; - if (componentType[1] === 'i') return 'int'; - if (componentType[1] === 'u') return 'uint'; - - return 'float'; - } - - getVectorType(type) { - if (type === 'color') return 'vec3'; - if (type === 'texture' || type === 'cubeTexture' || type === 'storageTexture' || type === 'texture3D') - return 'vec4'; - - return type; - } - - getTypeFromLength(length, componentType = 'float') { - if (length === 1) return componentType; - - const baseType = typeFromLength.get(length); - const prefix = componentType === 'float' ? '' : componentType[0]; - - return prefix + baseType; - } - - getTypeFromArray(array) { - return typeFromArray.get(array.constructor); - } - - getTypeFromAttribute(attribute) { - let dataAttribute = attribute; - - if (attribute.isInterleavedBufferAttribute) dataAttribute = attribute.data; - - const array = dataAttribute.array; - const itemSize = attribute.itemSize; - const normalized = attribute.normalized; - - let arrayType; - - if (!(attribute instanceof Float16BufferAttribute) && normalized !== true) { - arrayType = this.getTypeFromArray(array); - } - - return this.getTypeFromLength(itemSize, arrayType); - } - - getTypeLength(type) { - const vecType = this.getVectorType(type); - const vecNum = /vec([2-4])/.exec(vecType); - - if (vecNum !== null) return Number(vecNum[1]); - if (vecType === 'float' || vecType === 'bool' || vecType === 'int' || vecType === 'uint') return 1; - if (/mat2/.test(type) === true) return 4; - if (/mat3/.test(type) === true) return 9; - if (/mat4/.test(type) === true) return 16; - - return 0; - } - - getVectorFromMatrix(type) { - return type.replace('mat', 'vec'); - } - - changeComponentType(type, newComponentType) { - return this.getTypeFromLength(this.getTypeLength(type), newComponentType); - } - - getIntegerType(type) { - const componentType = this.getComponentType(type); - - if (componentType === 'int' || componentType === 'uint') return type; - - return this.changeComponentType(type, 'int'); - } - - addStack() { - this.stack = stack(this.stack); - - this.stacks.push(getCurrentStack() || this.stack); - setCurrentStack(this.stack); - - return this.stack; - } - - removeStack() { - const lastStack = this.stack; - this.stack = lastStack.parent; - - setCurrentStack(this.stacks.pop()); - - return lastStack; - } - - getDataFromNode(node, shaderStage = this.shaderStage, cache = null) { - cache = cache === null ? (node.isGlobal(this) ? this.globalCache : this.cache) : cache; - - let nodeData = cache.getData(node); - - if (nodeData === undefined) { - nodeData = {}; - - cache.setData(node, nodeData); - } - - if (nodeData[shaderStage] === undefined) nodeData[shaderStage] = {}; - - return nodeData[shaderStage]; - } - - getNodeProperties(node, shaderStage = 'any') { - const nodeData = this.getDataFromNode(node, shaderStage); - - return nodeData.properties || (nodeData.properties = { outputNode: null }); - } - - getBufferAttributeFromNode(node, type) { - const nodeData = this.getDataFromNode(node); - - let bufferAttribute = nodeData.bufferAttribute; - - if (bufferAttribute === undefined) { - const index = this.uniforms.index++; - - bufferAttribute = new NodeAttribute('nodeAttribute' + index, type, node); - - this.bufferAttributes.push(bufferAttribute); - - nodeData.bufferAttribute = bufferAttribute; - } - - return bufferAttribute; - } - - getStructTypeFromNode(node, shaderStage = this.shaderStage) { - const nodeData = this.getDataFromNode(node, shaderStage); - - if (nodeData.structType === undefined) { - const index = this.structs.index++; - - node.name = `StructType${index}`; - this.structs[shaderStage].push(node); - - nodeData.structType = node; - } - - return node; - } - - getUniformFromNode(node, type, shaderStage = this.shaderStage, name = null) { - const nodeData = this.getDataFromNode(node, shaderStage, this.globalCache); - - let nodeUniform = nodeData.uniform; - - if (nodeUniform === undefined) { - const index = this.uniforms.index++; - - nodeUniform = new NodeUniform(name || 'nodeUniform' + index, type, node); - - this.uniforms[shaderStage].push(nodeUniform); - - nodeData.uniform = nodeUniform; - } - - return nodeUniform; - } - - getVarFromNode(node, name = null, type = node.getNodeType(this), shaderStage = this.shaderStage) { - const nodeData = this.getDataFromNode(node, shaderStage); - - let nodeVar = nodeData.variable; - - if (nodeVar === undefined) { - const vars = this.vars[shaderStage] || (this.vars[shaderStage] = []); - - if (name === null) name = 'nodeVar' + vars.length; - - nodeVar = new NodeVar(name, type); - - vars.push(nodeVar); - - nodeData.variable = nodeVar; - } - - return nodeVar; - } - - getVaryingFromNode(node, name = null, type = node.getNodeType(this)) { - const nodeData = this.getDataFromNode(node, 'any'); - - let nodeVarying = nodeData.varying; - - if (nodeVarying === undefined) { - const varyings = this.varyings; - const index = varyings.length; - - if (name === null) name = 'nodeVarying' + index; - - nodeVarying = new NodeVarying(name, type); - - varyings.push(nodeVarying); - - nodeData.varying = nodeVarying; - } - - return nodeVarying; - } - - getCodeFromNode(node, type, shaderStage = this.shaderStage) { - const nodeData = this.getDataFromNode(node); - - let nodeCode = nodeData.code; - - if (nodeCode === undefined) { - const codes = this.codes[shaderStage] || (this.codes[shaderStage] = []); - const index = codes.length; - - nodeCode = new NodeCode('nodeCode' + index, type); - - codes.push(nodeCode); - - nodeData.code = nodeCode; - } - - return nodeCode; - } - - addLineFlowCode(code) { - if (code === '') return this; - - code = this.tab + code; - - if (!/;\s*$/.test(code)) { - code = code + ';\n'; - } - - this.flow.code += code; - - return this; - } - - addFlowCode(code) { - this.flow.code += code; - - return this; - } - - addFlowTab() { - this.tab += '\t'; - - return this; - } - - removeFlowTab() { - this.tab = this.tab.slice(0, -1); - - return this; - } - - getFlowData(node /*, shaderStage*/) { - return this.flowsData.get(node); - } - - flowNode(node) { - const output = node.getNodeType(this); - - const flowData = this.flowChildNode(node, output); - - this.flowsData.set(node, flowData); - - return flowData; - } - - buildFunctionNode(shaderNode) { - const fn = new FunctionNode(); - - const previous = this.currentFunctionNode; - - this.currentFunctionNode = fn; - - fn.code = this.buildFunctionCode(shaderNode); - - this.currentFunctionNode = previous; - - return fn; - } - - flowShaderNode(shaderNode) { - const layout = shaderNode.layout; - - let inputs; - - if (shaderNode.isArrayInput) { - inputs = []; - - for (const input of layout.inputs) { - inputs.push(new ParameterNode(input.type, input.name)); - } - } else { - inputs = {}; - - for (const input of layout.inputs) { - inputs[input.name] = new ParameterNode(input.type, input.name); - } - } - - // - - shaderNode.layout = null; - - const callNode = shaderNode.call(inputs); - const flowData = this.flowStagesNode(callNode, layout.type); - - shaderNode.layout = layout; - - return flowData; - } - - flowStagesNode(node, output = null) { - const previousFlow = this.flow; - const previousVars = this.vars; - const previousCache = this.cache; - const previousBuildStage = this.buildStage; - const previousStack = this.stack; - - const flow = { - code: '', - }; - - this.flow = flow; - this.vars = {}; - this.cache = new NodeCache(); - this.stack = stack(); - - for (const buildStage of defaultBuildStages) { - this.setBuildStage(buildStage); - - flow.result = node.build(this, output); - } - - flow.vars = this.getVars(this.shaderStage); - - this.flow = previousFlow; - this.vars = previousVars; - this.cache = previousCache; - this.stack = previousStack; - - this.setBuildStage(previousBuildStage); - - return flow; - } - - getFunctionOperator() { - return null; - } - - flowChildNode(node, output = null) { - const previousFlow = this.flow; - - const flow = { - code: '', - }; - - this.flow = flow; - - flow.result = node.build(this, output); - - this.flow = previousFlow; - - return flow; - } - - flowNodeFromShaderStage(shaderStage, node, output = null, propertyName = null) { - const previousShaderStage = this.shaderStage; - - this.setShaderStage(shaderStage); - - const flowData = this.flowChildNode(node, output); - - if (propertyName !== null) { - flowData.code += `${this.tab + propertyName} = ${flowData.result};\n`; - } - - this.flowCode[shaderStage] = this.flowCode[shaderStage] + flowData.code; - - this.setShaderStage(previousShaderStage); - - return flowData; - } - - getAttributesArray() { - return this.attributes.concat(this.bufferAttributes); - } - - getAttributes(/*shaderStage*/) { - console.warn('Abstract function.'); - } - - getVaryings(/*shaderStage*/) { - console.warn('Abstract function.'); - } - - getVar(type, name) { - return `${this.getType(type)} ${name}`; - } - - getVars(shaderStage) { - let snippet = ''; - - const vars = this.vars[shaderStage]; - - if (vars !== undefined) { - for (const variable of vars) { - snippet += `${this.getVar(variable.type, variable.name)}; `; - } - } - - return snippet; - } - - getUniforms(/*shaderStage*/) { - console.warn('Abstract function.'); - } - - getCodes(shaderStage) { - const codes = this.codes[shaderStage]; - - let code = ''; - - if (codes !== undefined) { - for (const nodeCode of codes) { - code += nodeCode.code + '\n'; - } - } - - return code; - } - - getHash() { - return this.vertexShader + this.fragmentShader + this.computeShader; - } - - setShaderStage(shaderStage) { - this.shaderStage = shaderStage; - } - - getShaderStage() { - return this.shaderStage; - } - - setBuildStage(buildStage) { - this.buildStage = buildStage; - } - - getBuildStage() { - return this.buildStage; - } - - buildCode() { - console.warn('Abstract function.'); - } - - build() { - const { object, material } = this; - - if (material !== null) { - NodeMaterial.fromMaterial(material).build(this); - } else { - this.addFlow('compute', object); - } - - // setup() -> stage 1: create possible new nodes and returns an output reference node - // analyze() -> stage 2: analyze nodes to possible optimization and validation - // generate() -> stage 3: generate shader - - for (const buildStage of defaultBuildStages) { - this.setBuildStage(buildStage); - - if (this.context.vertex && this.context.vertex.isNode) { - this.flowNodeFromShaderStage('vertex', this.context.vertex); - } - - for (const shaderStage of shaderStages) { - this.setShaderStage(shaderStage); - - const flowNodes = this.flowNodes[shaderStage]; - - for (const node of flowNodes) { - if (buildStage === 'generate') { - this.flowNode(node); - } else { - node.build(this); - } - } - } - } - - this.setBuildStage(null); - this.setShaderStage(null); - - // stage 4: build code for a specific output - - this.buildCode(); - this.buildUpdateNodes(); - - return this; - } - - getNodeUniform(uniformNode, type) { - if (type === 'float' || type === 'int' || type === 'uint') return new NumberNodeUniform(uniformNode); - if (type === 'vec2' || type === 'ivec2' || type === 'uvec2') return new Vector2NodeUniform(uniformNode); - if (type === 'vec3' || type === 'ivec3' || type === 'uvec3') return new Vector3NodeUniform(uniformNode); - if (type === 'vec4' || type === 'ivec4' || type === 'uvec4') return new Vector4NodeUniform(uniformNode); - if (type === 'color') return new ColorNodeUniform(uniformNode); - if (type === 'mat3') return new Matrix3NodeUniform(uniformNode); - if (type === 'mat4') return new Matrix4NodeUniform(uniformNode); - - throw new Error(`Uniform "${type}" not declared.`); - } - - createNodeMaterial(type = 'NodeMaterial') { - // TODO: Move Materials.js to outside of the Nodes.js in order to remove this function and improve tree-shaking support - - return createNodeMaterialFromType(type); - } - - format(snippet, fromType, toType) { - fromType = this.getVectorType(fromType); - toType = this.getVectorType(toType); - - if (fromType === toType || toType === null || this.isReference(toType)) { - return snippet; - } - - const fromTypeLength = this.getTypeLength(fromType); - const toTypeLength = this.getTypeLength(toType); - - if (fromTypeLength === 16 && toTypeLength === 9) { - return `${this.getType(toType)}(${snippet}[0].xyz, ${snippet}[1].xyz, ${snippet}[2].xyz)`; - } - - if (fromTypeLength === 9 && toTypeLength === 4) { - return `${this.getType(toType)}(${snippet}[0].xy, ${snippet}[1].xy)`; - } - - if (fromTypeLength > 4) { - // fromType is matrix-like - - // @TODO: ignore for now - - return snippet; - } - - if (toTypeLength > 4 || toTypeLength === 0) { - // toType is matrix-like or unknown - - // @TODO: ignore for now - - return snippet; - } - - if (fromTypeLength === toTypeLength) { - return `${this.getType(toType)}( ${snippet} )`; - } - - if (fromTypeLength > toTypeLength) { - return this.format( - `${snippet}.${'xyz'.slice(0, toTypeLength)}`, - this.getTypeFromLength(toTypeLength, this.getComponentType(fromType)), - toType, - ); - } - - if (toTypeLength === 4 && fromTypeLength > 1) { - // toType is vec4-like - - return `${this.getType(toType)}( ${this.format(snippet, fromType, 'vec3')}, 1.0 )`; - } - - if (fromTypeLength === 2) { - // fromType is vec2-like and toType is vec3-like - - return `${this.getType(toType)}( ${this.format(snippet, fromType, 'vec2')}, 0.0 )`; - } - - if (fromTypeLength === 1 && toTypeLength > 1 && fromType !== this.getComponentType(toType)) { - // fromType is float-like - - // convert a number value to vector type, e.g: - // vec3( 1u ) -> vec3( float( 1u ) ) - - snippet = `${this.getType(this.getComponentType(toType))}( ${snippet} )`; - } - - return `${this.getType(toType)}( ${snippet} )`; // fromType is float-like - } - - getSignature() { - return `// Three.js r${REVISION} - Node System\n`; - } -} - -export default NodeBuilder; diff --git a/src-testing/src/nodes/core/NodeCache.ts b/src-testing/src/nodes/core/NodeCache.ts deleted file mode 100644 index ad72d50c5..000000000 --- a/src-testing/src/nodes/core/NodeCache.ts +++ /dev/null @@ -1,26 +0,0 @@ -let id = 0; - -class NodeCache { - constructor(parent = null) { - this.id = id++; - this.nodesData = new WeakMap(); - - this.parent = parent; - } - - getData(node) { - let data = this.nodesData.get(node); - - if (data === undefined && this.parent !== null) { - data = this.parent.getData(node); - } - - return data; - } - - setData(node, data) { - this.nodesData.set(node, data); - } -} - -export default NodeCache; diff --git a/src-testing/src/nodes/core/NodeCode.ts b/src-testing/src/nodes/core/NodeCode.ts deleted file mode 100644 index 2ee509037..000000000 --- a/src-testing/src/nodes/core/NodeCode.ts +++ /dev/null @@ -1,11 +0,0 @@ -class NodeCode { - constructor(name, type, code = '') { - this.name = name; - this.type = type; - this.code = code; - - Object.defineProperty(this, 'isNodeCode', { value: true }); - } -} - -export default NodeCode; diff --git a/src-testing/src/nodes/core/NodeFrame.ts b/src-testing/src/nodes/core/NodeFrame.ts deleted file mode 100644 index ee64620ca..000000000 --- a/src-testing/src/nodes/core/NodeFrame.ts +++ /dev/null @@ -1,127 +0,0 @@ -import { NodeUpdateType } from './constants.js'; - -class NodeFrame { - constructor() { - this.time = 0; - this.deltaTime = 0; - - this.frameId = 0; - this.renderId = 0; - - this.startTime = null; - - this.updateMap = new WeakMap(); - this.updateBeforeMap = new WeakMap(); - this.updateAfterMap = new WeakMap(); - - this.renderer = null; - this.material = null; - this.camera = null; - this.object = null; - this.scene = null; - } - - _getMaps(referenceMap, nodeRef) { - let maps = referenceMap.get(nodeRef); - - if (maps === undefined) { - maps = { - renderMap: new WeakMap(), - frameMap: new WeakMap(), - }; - - referenceMap.set(nodeRef, maps); - } - - return maps; - } - - updateBeforeNode(node) { - const updateType = node.getUpdateBeforeType(); - const reference = node.updateReference(this); - - if (updateType === NodeUpdateType.FRAME) { - const { frameMap } = this._getMaps(this.updateBeforeMap, reference); - - if (frameMap.get(reference) !== this.frameId) { - if (node.updateBefore(this) !== false) { - frameMap.set(reference, this.frameId); - } - } - } else if (updateType === NodeUpdateType.RENDER) { - const { renderMap } = this._getMaps(this.updateBeforeMap, reference); - - if (renderMap.get(reference) !== this.renderId) { - if (node.updateBefore(this) !== false) { - renderMap.set(reference, this.renderId); - } - } - } else if (updateType === NodeUpdateType.OBJECT) { - node.updateBefore(this); - } - } - - updateAfterNode(node) { - const updateType = node.getUpdateAfterType(); - const reference = node.updateReference(this); - - if (updateType === NodeUpdateType.FRAME) { - const { frameMap } = this._getMaps(this.updateAfterMap, reference); - - if (frameMap.get(reference) !== this.frameId) { - if (node.updateAfter(this) !== false) { - frameMap.set(reference, this.frameId); - } - } - } else if (updateType === NodeUpdateType.RENDER) { - const { renderMap } = this._getMaps(this.updateAfterMap, reference); - - if (renderMap.get(reference) !== this.renderId) { - if (node.updateAfter(this) !== false) { - renderMap.set(reference, this.renderId); - } - } - } else if (updateType === NodeUpdateType.OBJECT) { - node.updateAfter(this); - } - } - - updateNode(node) { - const updateType = node.getUpdateType(); - const reference = node.updateReference(this); - - if (updateType === NodeUpdateType.FRAME) { - const { frameMap } = this._getMaps(this.updateMap, reference); - - if (frameMap.get(reference) !== this.frameId) { - if (node.update(this) !== false) { - frameMap.set(reference, this.frameId); - } - } - } else if (updateType === NodeUpdateType.RENDER) { - const { renderMap } = this._getMaps(this.updateMap, reference); - - if (renderMap.get(reference) !== this.renderId) { - if (node.update(this) !== false) { - renderMap.set(reference, this.renderId); - } - } - } else if (updateType === NodeUpdateType.OBJECT) { - node.update(this); - } - } - - update() { - this.frameId++; - - if (this.lastTime === undefined) this.lastTime = performance.now(); - - this.deltaTime = (performance.now() - this.lastTime) / 1000; - - this.lastTime = performance.now(); - - this.time += this.deltaTime; - } -} - -export default NodeFrame; diff --git a/src-testing/src/nodes/core/NodeFunction.ts b/src-testing/src/nodes/core/NodeFunction.ts deleted file mode 100644 index d05afb5e6..000000000 --- a/src-testing/src/nodes/core/NodeFunction.ts +++ /dev/null @@ -1,16 +0,0 @@ -class NodeFunction { - constructor(type, inputs, name = '', precision = '') { - this.type = type; - this.inputs = inputs; - this.name = name; - this.precision = precision; - } - - getCode(/*name = this.name*/) { - console.warn('Abstract function.'); - } -} - -NodeFunction.isNodeFunction = true; - -export default NodeFunction; diff --git a/src-testing/src/nodes/core/NodeFunctionInput.d.ts b/src-testing/src/nodes/core/NodeFunctionInput.d.ts deleted file mode 100644 index a8231d126..000000000 --- a/src-testing/src/nodes/core/NodeFunctionInput.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -export default class NodeFunctionInput { - isNodeFunctionInput: true; - count: null | number; - qualifier: string; - isConst: boolean; - constructor(type: string, name: string, count?: number, qualifier?: string, isConst?: boolean); -} diff --git a/src-testing/src/nodes/core/NodeKeywords.ts b/src-testing/src/nodes/core/NodeKeywords.ts deleted file mode 100644 index 1f468321b..000000000 --- a/src-testing/src/nodes/core/NodeKeywords.ts +++ /dev/null @@ -1,58 +0,0 @@ -class NodeKeywords { - constructor() { - this.keywords = []; - this.nodes = {}; - this.keywordsCallback = {}; - } - - getNode(name) { - let node = this.nodes[name]; - - if (node === undefined && this.keywordsCallback[name] !== undefined) { - node = this.keywordsCallback[name](name); - - this.nodes[name] = node; - } - - return node; - } - - addKeyword(name, callback) { - this.keywords.push(name); - this.keywordsCallback[name] = callback; - - return this; - } - - parse(code) { - const keywordNames = this.keywords; - - const regExp = new RegExp(`\\b${keywordNames.join('\\b|\\b')}\\b`, 'g'); - - const codeKeywords = code.match(regExp); - - const keywordNodes = []; - - if (codeKeywords !== null) { - for (const keyword of codeKeywords) { - const node = this.getNode(keyword); - - if (node !== undefined && keywordNodes.indexOf(node) === -1) { - keywordNodes.push(node); - } - } - } - - return keywordNodes; - } - - include(builder, code) { - const keywordNodes = this.parse(code); - - for (const keywordNode of keywordNodes) { - keywordNode.build(builder); - } - } -} - -export default NodeKeywords; diff --git a/src-testing/src/nodes/core/NodeParser.ts b/src-testing/src/nodes/core/NodeParser.ts deleted file mode 100644 index 9849452f1..000000000 --- a/src-testing/src/nodes/core/NodeParser.ts +++ /dev/null @@ -1,7 +0,0 @@ -class NodeParser { - parseFunction(/*source*/) { - console.warn('Abstract function.'); - } -} - -export default NodeParser; diff --git a/src-testing/src/nodes/core/NodeUniform.ts b/src-testing/src/nodes/core/NodeUniform.ts deleted file mode 100644 index ca43958fc..000000000 --- a/src-testing/src/nodes/core/NodeUniform.ts +++ /dev/null @@ -1,27 +0,0 @@ -class NodeUniform { - constructor(name, type, node) { - this.isNodeUniform = true; - - this.name = name; - this.type = type; - this.node = node.getSelf(); - } - - get value() { - return this.node.value; - } - - set value(val) { - this.node.value = val; - } - - get id() { - return this.node.id; - } - - get groupNode() { - return this.node.groupNode; - } -} - -export default NodeUniform; diff --git a/src-testing/src/nodes/core/NodeUtils.ts b/src-testing/src/nodes/core/NodeUtils.ts deleted file mode 100644 index 2314a6ebd..000000000 --- a/src-testing/src/nodes/core/NodeUtils.ts +++ /dev/null @@ -1,137 +0,0 @@ -import { Color } from '../../math/Color.js'; -import { Matrix3 } from '../../math/Matrix3.js'; -import { Matrix4 } from '../../math/Matrix4.js'; -import { Vector2 } from '../../math/Vector2.js'; -import { Vector3 } from '../../math/Vector3.js'; -import { Vector4 } from '../../math/Vector4.js'; - -export function getCacheKey(object, force = false) { - let cacheKey = '{'; - - if (object.isNode === true) { - cacheKey += object.id; - } - - for (const { property, childNode } of getNodeChildren(object)) { - cacheKey += ',' + property.slice(0, -4) + ':' + childNode.getCacheKey(force); - } - - cacheKey += '}'; - - return cacheKey; -} - -export function* getNodeChildren(node, toJSON = false) { - for (const property in node) { - // Ignore private properties. - if (property.startsWith('_') === true) continue; - - const object = node[property]; - - if (Array.isArray(object) === true) { - for (let i = 0; i < object.length; i++) { - const child = object[i]; - - if (child && (child.isNode === true || (toJSON && typeof child.toJSON === 'function'))) { - yield { property, index: i, childNode: child }; - } - } - } else if (object && object.isNode === true) { - yield { property, childNode: object }; - } else if (typeof object === 'object') { - for (const subProperty in object) { - const child = object[subProperty]; - - if (child && (child.isNode === true || (toJSON && typeof child.toJSON === 'function'))) { - yield { property, index: subProperty, childNode: child }; - } - } - } - } -} - -export function getValueType(value) { - if (value === undefined || value === null) return null; - - const typeOf = typeof value; - - if (value.isNode === true) { - return 'node'; - } else if (typeOf === 'number') { - return 'float'; - } else if (typeOf === 'boolean') { - return 'bool'; - } else if (typeOf === 'string') { - return 'string'; - } else if (typeOf === 'function') { - return 'shader'; - } else if (value.isVector2 === true) { - return 'vec2'; - } else if (value.isVector3 === true) { - return 'vec3'; - } else if (value.isVector4 === true) { - return 'vec4'; - } else if (value.isMatrix3 === true) { - return 'mat3'; - } else if (value.isMatrix4 === true) { - return 'mat4'; - } else if (value.isColor === true) { - return 'color'; - } else if (value instanceof ArrayBuffer) { - return 'ArrayBuffer'; - } - - return null; -} - -export function getValueFromType(type, ...params) { - const last4 = type ? type.slice(-4) : undefined; - - if (params.length === 1) { - // ensure same behaviour as in NodeBuilder.format() - - if (last4 === 'vec2') params = [params[0], params[0]]; - else if (last4 === 'vec3') params = [params[0], params[0], params[0]]; - else if (last4 === 'vec4') params = [params[0], params[0], params[0], params[0]]; - } - - if (type === 'color') { - return new Color(...params); - } else if (last4 === 'vec2') { - return new Vector2(...params); - } else if (last4 === 'vec3') { - return new Vector3(...params); - } else if (last4 === 'vec4') { - return new Vector4(...params); - } else if (last4 === 'mat3') { - return new Matrix3(...params); - } else if (last4 === 'mat4') { - return new Matrix4(...params); - } else if (type === 'bool') { - return params[0] || false; - } else if (type === 'float' || type === 'int' || type === 'uint') { - return params[0] || 0; - } else if (type === 'string') { - return params[0] || ''; - } else if (type === 'ArrayBuffer') { - return base64ToArrayBuffer(params[0]); - } - - return null; -} - -export function arrayBufferToBase64(arrayBuffer) { - let chars = ''; - - const array = new Uint8Array(arrayBuffer); - - for (let i = 0; i < array.length; i++) { - chars += String.fromCharCode(array[i]); - } - - return btoa(chars); -} - -export function base64ToArrayBuffer(base64) { - return Uint8Array.from(atob(base64), c => c.charCodeAt(0)).buffer; -} diff --git a/src-testing/src/nodes/core/NodeVar.ts b/src-testing/src/nodes/core/NodeVar.ts deleted file mode 100644 index e6e935b31..000000000 --- a/src-testing/src/nodes/core/NodeVar.ts +++ /dev/null @@ -1,10 +0,0 @@ -class NodeVar { - constructor(name, type) { - this.isNodeVar = true; - - this.name = name; - this.type = type; - } -} - -export default NodeVar; diff --git a/src-testing/src/nodes/core/NodeVarying.ts b/src-testing/src/nodes/core/NodeVarying.ts deleted file mode 100644 index a14823628..000000000 --- a/src-testing/src/nodes/core/NodeVarying.ts +++ /dev/null @@ -1,13 +0,0 @@ -import NodeVar from './NodeVar.js'; - -class NodeVarying extends NodeVar { - constructor(name, type) { - super(name, type); - - this.needsInterpolation = false; - - this.isNodeVarying = true; - } -} - -export default NodeVarying; diff --git a/src-testing/src/nodes/core/OutputStructNode.d.ts b/src-testing/src/nodes/core/OutputStructNode.d.ts deleted file mode 100644 index eb2c8fa63..000000000 --- a/src-testing/src/nodes/core/OutputStructNode.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import Node from "./Node.js"; - -export default class OutputStructNode extends Node { - members: Node[]; - - readonly isOutputStructNode: true; - - constructor(...members: Node[]); -} - -export const outputStruct: (...members: Node[]) => ShaderNodeObject; diff --git a/src-testing/src/nodes/core/PropertyNode.d.ts b/src-testing/src/nodes/core/PropertyNode.d.ts deleted file mode 100644 index d1a523864..000000000 --- a/src-testing/src/nodes/core/PropertyNode.d.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import Node from "./Node.js"; - -export default class PropertyNode extends Node { - name: string | null; - varying: boolean; - - readonly isPropertyNode: true; - - constructor(nodeType?: string | null, name?: string | null, varying?: boolean); -} - -export const property: (type?: string | null, name?: string | null) => ShaderNodeObject; -export const varyingProperty: (type?: string | null, name?: string | null) => ShaderNodeObject; - -export const diffuseColor: ShaderNodeObject; -export const emissive: ShaderNodeObject; -export const roughness: ShaderNodeObject; -export const metalness: ShaderNodeObject; -export const clearcoat: ShaderNodeObject; -export const clearcoatRoughness: ShaderNodeObject; -export const sheen: ShaderNodeObject; -export const sheenRoughness: ShaderNodeObject; -export const iridescence: ShaderNodeObject; -export const iridescenceIOR: ShaderNodeObject; -export const iridescenceThickness: ShaderNodeObject; -export const alphaT: ShaderNodeObject; -export const anisotropy: ShaderNodeObject; -export const anisotropyT: ShaderNodeObject; -export const anisotropyB: ShaderNodeObject; -export const specularColor: ShaderNodeObject; -export const specularF90: ShaderNodeObject; -export const shininess: ShaderNodeObject; -export const output: ShaderNodeObject; -export const dashSize: ShaderNodeObject; -export const gapSize: ShaderNodeObject; -export const pointWidth: ShaderNodeObject; -export const ior: ShaderNodeObject; -export const transmission: ShaderNodeObject; -export const thickness: ShaderNodeObject; -export const attenuationDistance: ShaderNodeObject; -export const attenuationColor: ShaderNodeObject; -export const dispersion: ShaderNodeObject; diff --git a/src-testing/src/nodes/core/StackNode.ts b/src-testing/src/nodes/core/StackNode.ts deleted file mode 100644 index 5f1871e73..000000000 --- a/src-testing/src/nodes/core/StackNode.ts +++ /dev/null @@ -1,87 +0,0 @@ -import Node, { addNodeClass } from './Node.js'; -import { select } from '../math/CondNode.js'; -import { ShaderNode, nodeProxy, getCurrentStack, setCurrentStack } from '../shadernode/ShaderNode.js'; - -class StackNode extends Node { - constructor(parent = null) { - super(); - - this.nodes = []; - this.outputNode = null; - - this.parent = parent; - - this._currentCond = null; - - this.isStackNode = true; - } - - getNodeType(builder) { - return this.outputNode ? this.outputNode.getNodeType(builder) : 'void'; - } - - add(node) { - this.nodes.push(node); - - return this; - } - - If(boolNode, method) { - const methodNode = new ShaderNode(method); - this._currentCond = select(boolNode, methodNode); - - return this.add(this._currentCond); - } - - ElseIf(boolNode, method) { - const methodNode = new ShaderNode(method); - const ifNode = select(boolNode, methodNode); - - this._currentCond.elseNode = ifNode; - this._currentCond = ifNode; - - return this; - } - - Else(method) { - this._currentCond.elseNode = new ShaderNode(method); - - return this; - } - - build(builder, ...params) { - const previousStack = getCurrentStack(); - - setCurrentStack(this); - - for (const node of this.nodes) { - node.build(builder, 'void'); - } - - setCurrentStack(previousStack); - - return this.outputNode ? this.outputNode.build(builder, ...params) : super.build(builder, ...params); - } - - // - - else(...params) { - // @deprecated, r168 - - console.warn('TSL.StackNode: .else() has been renamed to .Else().'); - return this.Else(...params); - } - - elseif(...params) { - // @deprecated, r168 - - console.warn('TSL.StackNode: .elseif() has been renamed to .ElseIf().'); - return this.ElseIf(...params); - } -} - -export default StackNode; - -export const stack = nodeProxy(StackNode); - -addNodeClass('StackNode', StackNode); diff --git a/src-testing/src/nodes/core/StructTypeNode.ts b/src-testing/src/nodes/core/StructTypeNode.ts deleted file mode 100644 index 697187999..000000000 --- a/src-testing/src/nodes/core/StructTypeNode.ts +++ /dev/null @@ -1,18 +0,0 @@ -import Node, { addNodeClass } from './Node.js'; - -class StructTypeNode extends Node { - constructor(types) { - super(); - - this.types = types; - this.isStructTypeNode = true; - } - - getMemberTypes() { - return this.types; - } -} - -export default StructTypeNode; - -addNodeClass('StructTypeNode', StructTypeNode); diff --git a/src-testing/src/nodes/core/TempNode.d.ts b/src-testing/src/nodes/core/TempNode.d.ts deleted file mode 100644 index 367f5ade1..000000000 --- a/src-testing/src/nodes/core/TempNode.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import Node from "./Node.js"; -import NodeBuilder from "./NodeBuilder.js"; - -export default class TempNode extends Node { - isTempNode: true; - - constructor(type: string | null); - - hasDependencies(builder: NodeBuilder): boolean; -} diff --git a/src-testing/src/nodes/core/UniformGroup.d.ts b/src-testing/src/nodes/core/UniformGroup.d.ts deleted file mode 100644 index 60339877b..000000000 --- a/src-testing/src/nodes/core/UniformGroup.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -export default class UniformGroup { - name: string; - - readonly isUniformGroup: true; - - constructor(name: string); -} diff --git a/src-testing/src/nodes/core/UniformGroupNode.ts b/src-testing/src/nodes/core/UniformGroupNode.ts deleted file mode 100644 index 4a4eed227..000000000 --- a/src-testing/src/nodes/core/UniformGroupNode.ts +++ /dev/null @@ -1,46 +0,0 @@ -import Node from './Node.js'; -import { addNodeClass } from './Node.js'; - -class UniformGroupNode extends Node { - constructor(name, shared = false) { - super('string'); - - this.name = name; - this.version = 0; - - this.shared = shared; - - this.isUniformGroup = true; - } - - set needsUpdate(value) { - if (value === true) this.version++; - } - - serialize(data) { - super.serialize(data); - - data.name = this.name; - data.version = this.version; - data.shared = this.shared; - } - - deserialize(data) { - super.deserialize(data); - - this.name = data.name; - this.version = data.version; - this.shared = data.shared; - } -} - -export const uniformGroup = name => new UniformGroupNode(name); -export const sharedUniformGroup = name => new UniformGroupNode(name, true); - -export const frameGroup = sharedUniformGroup('frame'); -export const renderGroup = sharedUniformGroup('render'); -export const objectGroup = uniformGroup('object'); - -export default UniformGroupNode; - -addNodeClass('UniformGroupNode', UniformGroupNode); diff --git a/src-testing/src/nodes/core/UniformNode.ts b/src-testing/src/nodes/core/UniformNode.ts deleted file mode 100644 index 41e523c4f..000000000 --- a/src-testing/src/nodes/core/UniformNode.ts +++ /dev/null @@ -1,90 +0,0 @@ -import InputNode from './InputNode.js'; -import { objectGroup } from './UniformGroupNode.js'; -import { addNodeClass } from './Node.js'; -import { nodeObject, getConstNodeType } from '../shadernode/ShaderNode.js'; - -class UniformNode extends InputNode { - constructor(value, nodeType = null) { - super(value, nodeType); - - this.isUniformNode = true; - - this.name = ''; - this.groupNode = objectGroup; - } - - label(name) { - this.name = name; - - return this; - } - - setGroup(group) { - this.groupNode = group; - - return this; - } - - getGroup() { - return this.groupNode; - } - - getUniformHash(builder) { - return this.getHash(builder); - } - - onUpdate(callback, updateType) { - const self = this.getSelf(); - - callback = callback.bind(self); - - return super.onUpdate(frame => { - const value = callback(frame, self); - - if (value !== undefined) { - this.value = value; - } - }, updateType); - } - - generate(builder, output) { - const type = this.getNodeType(builder); - - const hash = this.getUniformHash(builder); - - let sharedNode = builder.getNodeFromHash(hash); - - if (sharedNode === undefined) { - builder.setHashNode(this, hash); - - sharedNode = this; - } - - const sharedNodeType = sharedNode.getInputType(builder); - - const nodeUniform = builder.getUniformFromNode( - sharedNode, - sharedNodeType, - builder.shaderStage, - this.name || builder.context.label, - ); - const propertyName = builder.getPropertyName(nodeUniform); - - if (builder.context.label !== undefined) delete builder.context.label; - - return builder.format(propertyName, type, output); - } -} - -export default UniformNode; - -export const uniform = (arg1, arg2) => { - const nodeType = getConstNodeType(arg2 || arg1); - - // @TODO: get ConstNode from .traverse() in the future - const value = arg1 && arg1.isNode === true ? (arg1.node && arg1.node.value) || arg1.value : arg1; - - return nodeObject(new UniformNode(value, nodeType)); -}; - -addNodeClass('UniformNode', UniformNode); diff --git a/src-testing/src/nodes/core/VarNode.d.ts b/src-testing/src/nodes/core/VarNode.d.ts deleted file mode 100644 index b5de30a25..000000000 --- a/src-testing/src/nodes/core/VarNode.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import Node from "./Node.js"; - -export default class VarNode extends Node { - node: Node; - name: string | null; - - readonly isVarNode: true; - - constructor(node: Node, name?: string | null); -} - -export const temp: (node: NodeRepresentation, name?: string | null) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - temp: typeof temp; - toVar: typeof temp; - } -} diff --git a/src-testing/src/nodes/core/VaryingNode.d.ts b/src-testing/src/nodes/core/VaryingNode.d.ts deleted file mode 100644 index cbdf5e3dd..000000000 --- a/src-testing/src/nodes/core/VaryingNode.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import Node from "./Node.js"; -import NodeBuilder from "./NodeBuilder.js"; -import NodeVarying from "./NodeVarying.js"; - -export default class VaryingNode extends Node { - node: Node; - name: string | null; - - constructor(node: Node, name?: string | null); - - setupVarying(builder: NodeBuilder): NodeVarying; -} - -export const varying: (node: NodeRepresentation, name?: string) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - varying: typeof varying; - } -} diff --git a/src-testing/src/nodes/core/constants.ts b/src-testing/src/nodes/core/constants.ts deleted file mode 100644 index 3b01a9a6d..000000000 --- a/src-testing/src/nodes/core/constants.ts +++ /dev/null @@ -1,28 +0,0 @@ -export const NodeShaderStage = { - VERTEX: 'vertex', - FRAGMENT: 'fragment', -}; - -export const NodeUpdateType = { - NONE: 'none', - FRAME: 'frame', - RENDER: 'render', - OBJECT: 'object', -}; - -export const NodeType = { - BOOLEAN: 'bool', - INTEGER: 'int', - FLOAT: 'float', - VECTOR2: 'vec2', - VECTOR3: 'vec3', - VECTOR4: 'vec4', - MATRIX2: 'mat2', - MATRIX3: 'mat3', - MATRIX4: 'mat4', -}; - -export const defaultShaderStages = ['fragment', 'vertex']; -export const defaultBuildStages = ['setup', 'analyze', 'generate']; -export const shaderStages = [...defaultShaderStages, 'compute']; -export const vectorComponents = ['x', 'y', 'z', 'w']; diff --git a/src-testing/src/nodes/display/AfterImageNode.d.ts b/src-testing/src/nodes/display/AfterImageNode.d.ts deleted file mode 100644 index 336be14c7..000000000 --- a/src-testing/src/nodes/display/AfterImageNode.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -import TextureNode from "../accessors/TextureNode.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import UniformNode from "../core/UniformNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class AfterImageNode extends TempNode { - textureNode: TextureNode; - textureNodeOld: Node; - damp: UniformNode; - - constructor(textureNode: Node, damp?: number); - - getTextureNode(): TextureNode; - - setSize(width: number, height: number): void; -} - -export const afterImage: (node: Node, damp?: number) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - afterImage: typeof afterImage; - } -} diff --git a/src-testing/src/nodes/display/AnamorphicNode.d.ts b/src-testing/src/nodes/display/AnamorphicNode.d.ts deleted file mode 100644 index 5077a2caf..000000000 --- a/src-testing/src/nodes/display/AnamorphicNode.d.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { Vector2 } from "../../math/Vector2.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class AnamorphicNode extends TempNode { - textureNode: Node; - thresholdNode: Node; - scaleNode: Node; - samples: number; - resolution: Vector2; - - constructor(textureNode: Node, thresholdNode: Node, scaleNode: Node, samples: number); - - getTextureNode(): Node; - - setSize(width: number, height: number): void; -} - -export const anamorphic: ( - node: Node, - threshold?: NodeRepresentation, - scale?: NodeRepresentation, - samples?: NodeRepresentation, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - anamorphic: typeof anamorphic; - } -} diff --git a/src-testing/src/nodes/display/BleachBypassNode.d.ts b/src-testing/src/nodes/display/BleachBypassNode.d.ts deleted file mode 100644 index 335de1c67..000000000 --- a/src-testing/src/nodes/display/BleachBypassNode.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const bleach: (color: NodeRepresentation, opacity?: number) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - bleach: typeof bleach; - } -} diff --git a/src-testing/src/nodes/display/BlendModeNode.d.ts b/src-testing/src/nodes/display/BlendModeNode.d.ts deleted file mode 100644 index c335f8295..000000000 --- a/src-testing/src/nodes/display/BlendModeNode.d.ts +++ /dev/null @@ -1,47 +0,0 @@ -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { JoinNode } from "../Nodes.js"; -import { NodeRepresentation, ShaderNode, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const BurnNode: (args: { base: Node; blend: Node }) => ShaderNodeObject; - -export const DodgeNode: (args: { base: Node; blend: Node }) => ShaderNodeObject; - -export const ScreenNode: (args: { base: Node; blend: Node }) => ShaderNodeObject; - -export const OverlayNode: (args: { base: Node; blend: Node }) => ShaderNodeObject; - -export type BlendMode = - | typeof BlendModeNode.BURN - | typeof BlendModeNode.DODGE - | typeof BlendModeNode.SCREEN - | typeof BlendModeNode.OVERLAY; - -export default class BlendModeNode extends TempNode { - static BURN: "burn"; - static DODGE: "dodge"; - static SCREEN: "screen"; - static OVERLAY: "overlay"; - - baseNode: Node; - blendMode: BlendMode; - blendNode: Node; - - constructor(blendMode: BlendMode, baseNode: Node, blendNode: Node); - - setup(): Node; -} - -export const burn: (baseNode: NodeRepresentation, blendNode?: NodeRepresentation) => ShaderNodeObject; -export const dodge: (baseNode: NodeRepresentation, blendNode?: NodeRepresentation) => ShaderNodeObject; -export const overlay: (baseNode: NodeRepresentation, blendNode?: NodeRepresentation) => ShaderNodeObject; -export const screen: (baseNode: NodeRepresentation, blendNode?: NodeRepresentation) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - burn: typeof burn; - dodge: typeof dodge; - overlay: typeof overlay; - screen: typeof screen; - } -} diff --git a/src-testing/src/nodes/display/BloomNode.d.ts b/src-testing/src/nodes/display/BloomNode.d.ts deleted file mode 100644 index 4a057236b..000000000 --- a/src-testing/src/nodes/display/BloomNode.d.ts +++ /dev/null @@ -1,35 +0,0 @@ -import TextureNode from "../accessors/TextureNode.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import UniformNode from "../core/UniformNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -declare class BloomNode extends TempNode { - inputNode: Node; - strength: UniformNode; - radius: UniformNode; - threshold: UniformNode; - - smoothWidth: UniformNode; - - constructor(inputNode: Node, strength?: number, radius?: number, threshold?: number); - - getTexture(): ShaderNodeObject; - - setSize(width: number, height: number): void; -} - -export const bloom: ( - node: NodeRepresentation, - strength?: number, - radius?: number, - threshold?: number, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - bloom: typeof bloom; - } -} - -export default BloomNode; diff --git a/src-testing/src/nodes/display/ColorAdjustmentNode.d.ts b/src-testing/src/nodes/display/ColorAdjustmentNode.d.ts deleted file mode 100644 index b49345bbb..000000000 --- a/src-testing/src/nodes/display/ColorAdjustmentNode.d.ts +++ /dev/null @@ -1,47 +0,0 @@ -import TempNode from "../core/TempNode.js"; -import MathNode from "../math/MathNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export type ColorAdjustmentMethod = - | typeof ColorAdjustmentNode.SATURATION - | typeof ColorAdjustmentNode.VIBRANCE - | typeof ColorAdjustmentNode.HUE; - -export default class ColorAdjustmentNode extends TempNode { - static SATURATION: "saturation"; - static VIBRANCE: "vibrance"; - static HUE: "hue"; - - method: ColorAdjustmentMethod; - - colorNode: Node; - adjustmentNode: Node; - - constructor(method: ColorAdjustmentMethod, colorNode: Node, adjustmentNode?: Node); -} - -export const saturation: ( - colorNode: NodeRepresentation, - adjustmentNode?: NodeRepresentation, -) => ShaderNodeObject; -export const vibrance: ( - colorNode: NodeRepresentation, - adjustmentNode?: NodeRepresentation, -) => ShaderNodeObject; -export const hue: ( - colorNode: NodeRepresentation, - adjustmentNode?: NodeRepresentation, -) => ShaderNodeObject; - -export const luminance: (a: NodeRepresentation, b: NodeRepresentation) => ShaderNodeObject; - -export const threshold: (color: NodeRepresentation, thershold: NodeRepresentation) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - saturation: typeof saturation; - vibrance: typeof vibrance; - hue: typeof hue; - threshold: typeof threshold; - } -} diff --git a/src-testing/src/nodes/display/ColorSpaceNode.d.ts b/src-testing/src/nodes/display/ColorSpaceNode.d.ts deleted file mode 100644 index e4569f7fd..000000000 --- a/src-testing/src/nodes/display/ColorSpaceNode.d.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { ColorSpace } from "../../constants.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export type ColorSpaceNodeMethod = - | typeof ColorSpaceNode.LINEAR_TO_LINEAR - | typeof ColorSpaceNode.LINEAR_TO_sRGB - | typeof ColorSpaceNode.sRGB_TO_LINEAR; - -export default class ColorSpaceNode extends TempNode { - static LINEAR_TO_LINEAR: "LinearToLinear"; - static LINEAR_TO_sRGB: "LinearTosRGB"; - static sRGB_TO_LINEAR: "sRGBToLinear"; - - method: ColorSpaceNodeMethod; - node: Node; - - constructor(method: ColorSpaceNodeMethod | null, node: Node); -} - -export const linearToColorSpace: (node: NodeRepresentation, colorSpace: ColorSpace) => ShaderNodeObject; -export const colorSpaceToLinear: (node: NodeRepresentation, colorSpace: ColorSpace) => ShaderNodeObject; - -export const linearTosRGB: (node: NodeRepresentation) => ShaderNodeObject; -export const sRGBToLinear: (node: NodeRepresentation) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - linearTosRGB: typeof linearTosRGB; - sRGBToLinear: typeof sRGBToLinear; - linearToColorSpace: typeof linearToColorSpace; - colorSpaceToLinear: typeof colorSpaceToLinear; - } -} diff --git a/src-testing/src/nodes/display/DenoiseNode.d.ts b/src-testing/src/nodes/display/DenoiseNode.d.ts deleted file mode 100644 index 8b5699dfa..000000000 --- a/src-testing/src/nodes/display/DenoiseNode.d.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { Camera } from "../../cameras/Camera.js"; -import { Matrix4 } from "../../math/Matrix4.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import UniformNode from "../core/UniformNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -declare class DenoiseNode extends TempNode { - textureNode: Node; - depthNode: Node; - normalNode: Node; - noiseNode: Node; - - cameraProjectionMatrixInversion: UniformNode; - lumaPhi: UniformNode; - depthPhi: UniformNode; - normalPhi: UniformNode; - radius: UniformNode; - index: UniformNode; - - constructor(textureNode: Node, depthNode: Node, normalNode: Node, noiseNode: Node, camera: Camera); -} - -export const denoise: ( - node: NodeRepresentation, - depthNode: NodeRepresentation, - normalNode: NodeRepresentation, - noiseNode: NodeRepresentation, - camera: Camera, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - denoise: typeof denoise; - } -} - -export default DenoiseNode; diff --git a/src-testing/src/nodes/display/DepthOfFieldNode.d.ts b/src-testing/src/nodes/display/DepthOfFieldNode.d.ts deleted file mode 100644 index e58e801bc..000000000 --- a/src-testing/src/nodes/display/DepthOfFieldNode.d.ts +++ /dev/null @@ -1,30 +0,0 @@ -import TextureNode from "../accessors/TextureNode.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import UniformNode from "../core/UniformNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class DepthOfFieldNode extends TempNode { - textureNode: TextureNode; - viewZNode: Node; - - focus: UniformNode; - aperture: UniformNode; - maxblur: UniformNode; - - constructor(textureNode: TextureNode, viewZNode: Node, focusNode: Node, apertureNode: Node, maxblurNode: Node); -} - -export const dof: ( - node: NodeRepresentation, - viewZNode: NodeRepresentation, - focus?: NodeRepresentation, - aperture?: NodeRepresentation, - maxblur?: NodeRepresentation, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - dof: typeof dof; - } -} diff --git a/src-testing/src/nodes/display/DotScreenNode.d.ts b/src-testing/src/nodes/display/DotScreenNode.d.ts deleted file mode 100644 index 8d2c7a66f..000000000 --- a/src-testing/src/nodes/display/DotScreenNode.d.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { Vector2 } from "../../math/Vector2.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import UniformNode from "../core/UniformNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class DotScreenNode extends TempNode { - inputNode: Node; - center: UniformNode; - angle: UniformNode; - scale: UniformNode; - - constructor(inputNode: Node, center?: Vector2, angle?: number, scale?: number); -} - -export const dotScreen: ( - node: NodeRepresentation, - center?: Vector2, - angle?: number, - scale?: number, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - dotScreen: typeof dotScreen; - } -} diff --git a/src-testing/src/nodes/display/FXAANode.d.ts b/src-testing/src/nodes/display/FXAANode.d.ts deleted file mode 100644 index ed23217e6..000000000 --- a/src-testing/src/nodes/display/FXAANode.d.ts +++ /dev/null @@ -1,19 +0,0 @@ -import TextureNode from "../accessors/TextureNode.js"; -import TempNode from "../core/TempNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -declare class FXAANode extends TempNode { - textureNode: TextureNode; - - constructor(textureNode: TextureNode); -} - -export const fxaa: (node: NodeRepresentation) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - fxaa: typeof fxaa; - } -} - -export default FXAANode; diff --git a/src-testing/src/nodes/display/FilmNode.d.ts b/src-testing/src/nodes/display/FilmNode.d.ts deleted file mode 100644 index b9febaaae..000000000 --- a/src-testing/src/nodes/display/FilmNode.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -declare class FilmNode extends TempNode { - inputNode: Node; - intensityNode: Node | null; - uvNode: Node | null; - - constructor(inputNode: Node, intensityNode?: Node | null, uvNode?: Node | null); -} - -export const film: ( - inputNode: NodeRepresentation, - intensityNode?: NodeRepresentation | null, - uvNode?: NodeRepresentation | null, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - film: typeof film; - } -} - -export default FilmNode; diff --git a/src-testing/src/nodes/display/FrontFacingNode.d.ts b/src-testing/src/nodes/display/FrontFacingNode.d.ts deleted file mode 100644 index 95a6d6906..000000000 --- a/src-testing/src/nodes/display/FrontFacingNode.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import Node from "../core/Node.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class FrontFacingNode extends Node { - isFrontFacingNode: true; - constructor(); -} - -export const frontFacing: ShaderNodeObject; -export const faceDirection: ShaderNodeObject; diff --git a/src-testing/src/nodes/display/GTAONode.d.ts b/src-testing/src/nodes/display/GTAONode.d.ts deleted file mode 100644 index 5a6da521b..000000000 --- a/src-testing/src/nodes/display/GTAONode.d.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { Camera } from "../../cameras/Camera.js"; -import { Matrix4 } from "../../math/Matrix4.js"; -import { Vector2 } from "../../math/Vector2.js"; -import TextureNode from "../accessors/TextureNode.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import UniformNode from "../core/UniformNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -declare class GTAONode extends TempNode { - depthNode: Node; - normalNode: Node; - - radius: ShaderNodeObject>; - resolution: ShaderNodeObject>; - thickness: ShaderNodeObject>; - distanceExponent: ShaderNodeObject>; - distanceFallOff: ShaderNodeObject>; - scale: ShaderNodeObject>; - noiseNode: ShaderNodeObject; - - cameraProjectionMatrix: ShaderNodeObject>; - cameraProjectionMatrixInverse: ShaderNodeObject>; - - SAMPLES: ShaderNodeObject>; - - constructor(depthNode: Node, normalNode: Node, camera: Camera); - - getTextureNode(): ShaderNodeObject; - - setSize(width: number, height: number): void; -} - -export const ao: ( - depthNode: NodeRepresentation, - normalNode: NodeRepresentation, - camera: Camera, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - ao: typeof ao; - } -} - -export default GTAONode; diff --git a/src-testing/src/nodes/display/GaussianBlurNode.d.ts b/src-testing/src/nodes/display/GaussianBlurNode.d.ts deleted file mode 100644 index 6cec09eb3..000000000 --- a/src-testing/src/nodes/display/GaussianBlurNode.d.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { Vector2 } from "../../math/Vector2.js"; -import TextureNode from "../accessors/TextureNode.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class GaussianBlurNode extends TempNode { - textureNode: TextureNode; - directionNode: Node | null; - sigma: number; - - resolution: Vector2; - - constructor(textureNode: TextureNode, directionNode?: Node | null, sigma?: number); - - setSize(width: number, height: number): void; - - getTextureNode(): TextureNode; -} - -export const gaussianBlur: ( - node: NodeRepresentation, - directionNode?: NodeRepresentation | null, - sigma?: number, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - gaussianBlur: typeof gaussianBlur; - } -} diff --git a/src-testing/src/nodes/display/Lut3DNode.d.ts b/src-testing/src/nodes/display/Lut3DNode.d.ts deleted file mode 100644 index a257845d7..000000000 --- a/src-testing/src/nodes/display/Lut3DNode.d.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { Data3DTexture } from "../../textures/Data3DTexture.js"; -import Texture3DNode from "../accessors/Texture3DNode.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import UniformNode from "../core/UniformNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -declare class Lut3DNode extends TempNode { - inputNode: Node; - lutNode: Texture3DNode; - size: ShaderNodeObject>; - intensityNode: UniformNode; - - constructor(inputNode: Node, lutNode: UniformNode, size: number, intensityNode: UniformNode); -} - -export const lut3D: ( - node: NodeRepresentation, - lut: NodeRepresentation, - size: number, - intensity: NodeRepresentation, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - lut3D: typeof lut3D; - } -} - -export default Lut3DNode; diff --git a/src-testing/src/nodes/display/NormalMapNode.d.ts b/src-testing/src/nodes/display/NormalMapNode.d.ts deleted file mode 100644 index 82b850547..000000000 --- a/src-testing/src/nodes/display/NormalMapNode.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { NormalMapTypes } from "../../constants.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class NormalMapNode extends TempNode { - node: Node; - scaleNode: Node | null; - - normalMapType: NormalMapTypes; - - constructor(node: Node, scaleNode?: Node | null); -} - -export const normalMap: (node: Node, scaleNode?: Node) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - normalMap: typeof normalMap; - } -} diff --git a/src-testing/src/nodes/display/PassNode.d.ts b/src-testing/src/nodes/display/PassNode.d.ts deleted file mode 100644 index 9b385818f..000000000 --- a/src-testing/src/nodes/display/PassNode.d.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { Camera } from "../../cameras/Camera.js"; -import { RenderTarget, RenderTargetOptions } from "../../core/RenderTarget.js"; -import { Scene } from "../../scenes/Scene.js"; -import { Texture } from "../../textures/Texture.js"; -import TextureNode from "../accessors/TextureNode.js"; -import MRTNode from "../core/MRTNode.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -declare class PassTextureNode extends TextureNode { - passNode: PassNode; - - constructor(passNode: PassNode, texture: Texture); -} - -declare class PassMultipleTextureNode extends PassTextureNode { - textureName: string; - previousTexture: boolean; - - constructor(passNode: PassNode, textureName: string, previousTexture?: boolean); - - updateTexture(): void; -} - -declare class PassNode extends TempNode { - scope: PassNodeScope; - scene: Scene; - camera: Camera; - - renderTarget: RenderTarget; - - readonly isPassNode: true; - - constructor(scope: PassNodeScope, scene: Scene, camera: Camera, options?: RenderTargetOptions); - - setMRT(mrt: MRTNode | null): this; - - getMRT(): MRTNode | null; - - getTexture(name: string): Texture; - - getPreviousTexture(name: string): Texture; - - toggleTexture(name: string): void; - - getTextureNode(name?: string): ShaderNodeObject; - - getPreviousTextureNode(name?: string): ShaderNodeObject; - - getViewZNode(name?: string): ShaderNodeObject; - - getLinearDepthNode(name?: string): ShaderNodeObject; - - setSize(width: number, height: number): void; - - setPixelRatio(pixelRatio: number): void; - - dispose(): void; - - static COLOR: "color"; - static DEPTH: "depth"; -} - -export type PassNodeScope = typeof PassNode.COLOR | typeof PassNode.DEPTH; - -export default PassNode; - -export const pass: (scene: Scene, camera: Camera, options?: RenderTargetOptions) => ShaderNodeObject; -export const passTexture: (pass: PassNode, texture: Texture) => ShaderNodeObject; -export const depthPass: (scene: Scene, camera: Camera) => ShaderNodeObject; diff --git a/src-testing/src/nodes/display/PixelationPassNode.d.ts b/src-testing/src/nodes/display/PixelationPassNode.d.ts deleted file mode 100644 index e8e76aca8..000000000 --- a/src-testing/src/nodes/display/PixelationPassNode.d.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { Camera } from "../../cameras/Camera.js"; -import { Scene } from "../../scenes/Scene.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import UniformNode from "../core/UniformNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import PassNode from "./PassNode.js"; - -declare class PixelationNode extends TempNode { - textureNode: Node; - depthNode: Node; - normalNode: Node; - - pixelSize: Node; - normalEdgeStrength: Node; - depthEdgeStrength: Node; - - constructor( - textureNode: Node, - depthNode: Node, - normalNode: Node, - pixelSize: Node, - normalEdgeStrength: Node, - depthEdgeStrength: Node, - ); -} - -declare const pixelation: ( - node: NodeRepresentation, - depthNode: NodeRepresentation, - normalNode: NodeRepresentation, - pixelSize?: number, - normalEdgeStrength?: number, - depthEdgeStrength?: number, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - pixelation: typeof pixelation; - } -} - -declare class PixelationPassNode extends PassNode { - pixelSize: UniformNode; - normalEdgeStrength: UniformNode; - depthEdgeStrength: UniformNode; - - readonly isPixelationPassNode: true; - - constructor( - scene: Scene, - camera: Camera, - pixelSize: number, - normalEdgeStrength: number, - depthEdgeStrength: number, - ); -} - -export const pixelationPass: ( - scene: Scene, - camera: Camera, - pixelSize: UniformNode, - normalEdgeStrength: UniformNode, - depthEdgeStrength: UniformNode, -) => ShaderNodeObject; - -export default PixelationPassNode; diff --git a/src-testing/src/nodes/display/PosterizeNode.d.ts b/src-testing/src/nodes/display/PosterizeNode.d.ts deleted file mode 100644 index 36aa66d8e..000000000 --- a/src-testing/src/nodes/display/PosterizeNode.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class PosterizeNode extends Node { - sourceNode: Node; - stepsNode: Node; - - constructor(sourceNode: Node, stepsNode: Node); -} - -export const posterize: ( - sourceNode: NodeRepresentation, - stepsNode: NodeRepresentation, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - posterize: typeof posterize; - } -} diff --git a/src-testing/src/nodes/display/RGBShiftNode.d.ts b/src-testing/src/nodes/display/RGBShiftNode.d.ts deleted file mode 100644 index 804094aba..000000000 --- a/src-testing/src/nodes/display/RGBShiftNode.d.ts +++ /dev/null @@ -1,24 +0,0 @@ -import TextureNode from "../accessors/TextureNode.js"; -import TempNode from "../core/TempNode.js"; -import UniformNode from "../core/UniformNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class RGBShiftNode extends TempNode { - textureNode: TextureNode; - amount: UniformNode; - angle: UniformNode; - - constructor(textureNode: TextureNode, amount?: number, angle?: number); - - getTextureNode(): TextureNode; - - setSize(width: number, height: number): void; -} - -export const rgbShift: (node: NodeRepresentation, amount?: number, angle?: number) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - rgbShift: typeof rgbShift; - } -} diff --git a/src-testing/src/nodes/display/RenderOutputNode.d.ts b/src-testing/src/nodes/display/RenderOutputNode.d.ts deleted file mode 100644 index c41f56f65..000000000 --- a/src-testing/src/nodes/display/RenderOutputNode.d.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { ColorSpace, ToneMapping } from "../../constants.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -declare class RenderOutputNode extends TempNode { - colorNode: Node; - toneMapping: ToneMapping | null; - outputColorSpace: ColorSpace | null; - - readonly isRenderOutput: true; - - constructor(colorNode: Node, toneMapping?: ToneMapping | null, outputColorSpace?: ColorSpace | null); -} - -export default RenderOutputNode; - -export const renderOutput: ( - color: NodeRepresentation, - toneMapping?: ToneMapping | null, - outputColorSpace?: ColorSpace | null, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - renderOutput: typeof renderOutput; - } -} diff --git a/src-testing/src/nodes/display/SepiaNode.d.ts b/src-testing/src/nodes/display/SepiaNode.d.ts deleted file mode 100644 index d5c3007a3..000000000 --- a/src-testing/src/nodes/display/SepiaNode.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const sepia: (args: [NodeRepresentation]) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - sepia: typeof sepia; - } -} diff --git a/src-testing/src/nodes/display/SobelOperatorNode.d.ts b/src-testing/src/nodes/display/SobelOperatorNode.d.ts deleted file mode 100644 index ab49780f9..000000000 --- a/src-testing/src/nodes/display/SobelOperatorNode.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -import TextureNode from "../accessors/TextureNode.js"; -import TempNode from "../core/TempNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class SobelOperatorNode extends TempNode { - textureNode: TextureNode; - - constructor(textureNode: TextureNode); -} - -export const sobel: (node: NodeRepresentation) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - sobel: typeof sobel; - } -} diff --git a/src-testing/src/nodes/display/ToneMappingNode.d.ts b/src-testing/src/nodes/display/ToneMappingNode.d.ts deleted file mode 100644 index 95dabe473..000000000 --- a/src-testing/src/nodes/display/ToneMappingNode.d.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { ToneMapping } from "../../constants.js"; -import RendererReferenceNode from "../accessors/RendererReferenceNode.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -// exposure only -export const LinearToneMappingNode: Node; - -export default class ToneMappingNode extends TempNode { - toneMapping: ToneMapping; - exposureNode: Node; - colorNode: Node | null; - - constructor(toneMapping: ToneMapping, exposureNode?: Node, colorNode?: Node | null); -} - -export const toneMapping: ( - mapping: ToneMapping, - exposure: NodeRepresentation, - color?: NodeRepresentation, -) => ShaderNodeObject; -export const toneMappingExposure: ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - toneMapping: ( - color: NodeRepresentation, - mapping?: NodeRepresentation, - exposure?: NodeRepresentation, - ) => ShaderNodeObject; - } -} diff --git a/src-testing/src/nodes/display/TransitionNode.d.ts b/src-testing/src/nodes/display/TransitionNode.d.ts deleted file mode 100644 index 745578cf4..000000000 --- a/src-testing/src/nodes/display/TransitionNode.d.ts +++ /dev/null @@ -1,41 +0,0 @@ -import TextureNode from "../accessors/TextureNode.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import UniformNode from "../core/UniformNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -declare class TransitionNode extends TempNode { - textureNodeA: TextureNode; - textureNodeB: TextureNode; - mixTextureNode: TextureNode; - - mixRatioNode: Node; - thresholdNode: Node; - useTextureNode: Node; - - constructor( - textureNodeA: TextureNode, - textureNodeB: TextureNode, - mixTextureNode: TextureNode, - mixRatioNode: Node, - thresholdNode: Node, - useTextureNode: Node, - ); -} - -export const transition: ( - node: NodeRepresentation, - nodeB: NodeRepresentation, - mixTexture: NodeRepresentation, - mixRatio: UniformNode, - threshold: UniformNode, - useTexture: UniformNode, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - transition: typeof transition; - } -} - -export default TransitionNode; diff --git a/src-testing/src/nodes/display/ViewportDepthNode.d.ts b/src-testing/src/nodes/display/ViewportDepthNode.d.ts deleted file mode 100644 index 5b8324748..000000000 --- a/src-testing/src/nodes/display/ViewportDepthNode.d.ts +++ /dev/null @@ -1,30 +0,0 @@ -import Node from "../core/Node.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class ViewportDepthNode extends Node { - scope: ViewportDepthNodeScope; - valueNode: Node; - - readonly isViewportDepthNode: true; - - constructor(scope: ViewportDepthNodeScope, valueNode?: Node | null); - - static DEPTH: "depth"; - static LINEAR_DEPTH: "linearDepth"; -} - -export type ViewportDepthNodeScope = - | typeof ViewportDepthNode.DEPTH - | typeof ViewportDepthNode.LINEAR_DEPTH; - -export const viewZToOrthographicDepth: (viewZ: Node, near: Node, far: Node) => Node; - -export const orthographicDepthToViewZ: (depth: Node, near: Node, far: Node) => Node; - -export const viewZToPerspectiveDepth: (viewZ: Node, near: Node, far: Node) => Node; - -export const perspectiveDepthToViewZ: (depth: Node, near: Node, far: Node) => Node; - -export const depth: ShaderNodeObject; -export const linearDepth: (valueNode?: Node | null) => ShaderNodeObject; -export const viewportLinearDepth: ShaderNodeObject; diff --git a/src-testing/src/nodes/display/ViewportDepthTextureNode.d.ts b/src-testing/src/nodes/display/ViewportDepthTextureNode.d.ts deleted file mode 100644 index bb74b6c75..000000000 --- a/src-testing/src/nodes/display/ViewportDepthTextureNode.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import ViewportTextureNode from "./ViewportTextureNode.js"; - -export default class ViewportDepthTextureNode extends ViewportTextureNode { - constructor(uvNode?: Node, levelNode?: Node | null); -} - -export const viewportDepthTexture: ( - uvNode?: NodeRepresentation, - levelNode?: NodeRepresentation, -) => ShaderNodeObject; diff --git a/src-testing/src/nodes/display/ViewportNode.d.ts b/src-testing/src/nodes/display/ViewportNode.d.ts deleted file mode 100644 index 401cd9ed4..000000000 --- a/src-testing/src/nodes/display/ViewportNode.d.ts +++ /dev/null @@ -1,31 +0,0 @@ -import Node from "../core/Node.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export type ViewportNodeScope = - | typeof ViewportNode.COORDINATE - | typeof ViewportNode.RESOLUTION - | typeof ViewportNode.TOP_LEFT - | typeof ViewportNode.BOTTOM_LEFT - | typeof ViewportNode.TOP_RIGHT - | typeof ViewportNode.BOTTOM_RIGHT; - -export default class ViewportNode extends Node { - static COORDINATE: "coordinate"; - static RESOLUTION: "resolution"; - static TOP_LEFT: "topLeft"; - static BOTTOM_LEFT: "bottomLeft"; - static TOP_RIGHT: "topRight"; - static BOTTOM_RIGHT: "bottomRight"; - - scope: ViewportNodeScope; - isViewportNode: true; - - constructor(scope: ViewportNodeScope); -} - -export const viewportCoordinate: ShaderNodeObject; -export const viewportResolution: ShaderNodeObject; -export const viewportTopLeft: ShaderNodeObject; -export const viewportBottomLeft: ShaderNodeObject; -export const viewportTopRight: ShaderNodeObject; -export const viewportBottomRight: ShaderNodeObject; diff --git a/src-testing/src/nodes/display/ViewportSharedTextureNode.d.ts b/src-testing/src/nodes/display/ViewportSharedTextureNode.d.ts deleted file mode 100644 index 2faf2bcd6..000000000 --- a/src-testing/src/nodes/display/ViewportSharedTextureNode.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -import Node from "../core/Node.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import ViewportTextureNode from "./ViewportTextureNode.js"; - -export default class ViewportSharedTextureNode extends ViewportTextureNode { - constructor(uvNode?: Node, levelNode?: Node | null); -} - -export const viewportSharedTexture: ( - uvNode?: Node, - levelNode?: Node | null, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - viewportSharedTexture: typeof viewportSharedTexture; - } -} diff --git a/src-testing/src/nodes/display/ViewportTextureNode.d.ts b/src-testing/src/nodes/display/ViewportTextureNode.d.ts deleted file mode 100644 index 96b490766..000000000 --- a/src-testing/src/nodes/display/ViewportTextureNode.d.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { FramebufferTexture } from "../../textures/FramebufferTexture.js"; -import TextureNode from "../accessors/TextureNode.js"; -import { NodeUpdateType } from "../core/constants.js"; -import Node from "../core/Node.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class ViewportTextureNode extends TextureNode { - generateMipmaps: boolean; - - readonly isOutputTextureNode: true; - - updateBeforeType: NodeUpdateType; - - constructor(uvNode?: Node, levelNode?: Node | null, framebufferTexture?: FramebufferTexture | null); -} - -export const viewportTexture: ( - uvNode?: Node, - levelNode?: Node | null, - framebufferTexture?: FramebufferTexture | null, -) => ShaderNodeObject; -export const viewportMipTexture: ( - uvNode?: Node, - levelNode?: Node | null, - framebufferTexture?: FramebufferTexture | null, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - viewportTexture: typeof viewportTexture; - viewportMipTexture: typeof viewportMipTexture; - } -} diff --git a/src-testing/src/nodes/fog/FogExp2Node.d.ts b/src-testing/src/nodes/fog/FogExp2Node.d.ts deleted file mode 100644 index 3ede63e79..000000000 --- a/src-testing/src/nodes/fog/FogExp2Node.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -import Node from "../core/Node.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import FogNode from "./FogNode.js"; - -export default class FogExp2Node extends FogNode { - isFogExp2Node: true; - densityNode: Node; - - constructor(colorNode: Node, densityNode: Node); -} - -export const densityFog: (colorNode: Node, densityNode: Node) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - densityFog: typeof densityFog; - } -} diff --git a/src-testing/src/nodes/fog/FogNode.ts b/src-testing/src/nodes/fog/FogNode.ts deleted file mode 100644 index 9417df5a5..000000000 --- a/src-testing/src/nodes/fog/FogNode.ts +++ /dev/null @@ -1,38 +0,0 @@ -import Node, { addNodeClass } from '../core/Node.js'; -import { positionView } from '../accessors/PositionNode.js'; -import { addNodeElement, nodeProxy } from '../shadernode/ShaderNode.js'; - -class FogNode extends Node { - constructor(colorNode, factorNode) { - super('float'); - - this.isFogNode = true; - - this.colorNode = colorNode; - this.factorNode = factorNode; - } - - getViewZNode(builder) { - let viewZ; - - const getViewZ = builder.context.getViewZ; - - if (getViewZ !== undefined) { - viewZ = getViewZ(this); - } - - return (viewZ || positionView.z).negate(); - } - - setup() { - return this.factorNode; - } -} - -export default FogNode; - -export const fog = nodeProxy(FogNode); - -addNodeElement('fog', fog); - -addNodeClass('FogNode', FogNode); diff --git a/src-testing/src/nodes/fog/FogRangeNode.d.ts b/src-testing/src/nodes/fog/FogRangeNode.d.ts deleted file mode 100644 index a1d37e7b6..000000000 --- a/src-testing/src/nodes/fog/FogRangeNode.d.ts +++ /dev/null @@ -1,23 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import FogNode from "./FogNode.js"; - -export default class FogRangeNode extends FogNode { - isFogRangeNode: true; - nearNode: Node | null; - farNode: Node | null; - - constructor(colorNode: Node | null, nearNode: Node | null, farNode: Node | null); -} - -export const rangeFog: ( - colorNode: NodeRepresentation | null, - nearNode: NodeRepresentation | null, - farNode: NodeRepresentation | null, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - rangeFog: typeof rangeFog; - } -} diff --git a/src-testing/src/nodes/functions/BSDF/BRDF_GGX.d.ts b/src-testing/src/nodes/functions/BSDF/BRDF_GGX.d.ts deleted file mode 100644 index 066656347..000000000 --- a/src-testing/src/nodes/functions/BSDF/BRDF_GGX.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -import Node from "../../core/Node.js"; -import OperatorNode from "../../math/OperatorNode.js"; -import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; - -declare const BRDF_GGX: (args: { - lightDirection: Node; - f0: Node; - f90: Node; - roughness: Node; - f?: Node; - USE_IRIDESCENCE?: Node; - USE_ANISOTROPY?: Node; -}) => ShaderNodeObject; - -export default BRDF_GGX; diff --git a/src-testing/src/nodes/functions/BSDF/BRDF_Lambert.d.ts b/src-testing/src/nodes/functions/BSDF/BRDF_Lambert.d.ts deleted file mode 100644 index 51df53226..000000000 --- a/src-testing/src/nodes/functions/BSDF/BRDF_Lambert.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import Node from "../../core/Node.js"; -import OperatorNode from "../../math/OperatorNode.js"; -import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; - -declare const BRDF_Lambert: (args: { diffuseColor: Node }) => ShaderNodeObject; - -export default BRDF_Lambert; diff --git a/src-testing/src/nodes/functions/BSDF/BRDF_Sheen.d.ts b/src-testing/src/nodes/functions/BSDF/BRDF_Sheen.d.ts deleted file mode 100644 index 67574acb1..000000000 --- a/src-testing/src/nodes/functions/BSDF/BRDF_Sheen.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import Node from "../../core/Node.js"; -import OperatorNode from "../../math/OperatorNode.js"; -import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; - -declare const BRDF_Sheen: (args: { lightDirection: Node }) => ShaderNodeObject; - -export default BRDF_Sheen; diff --git a/src-testing/src/nodes/functions/BSDF/DFGApprox.d.ts b/src-testing/src/nodes/functions/BSDF/DFGApprox.d.ts deleted file mode 100644 index ed16ef5e5..000000000 --- a/src-testing/src/nodes/functions/BSDF/DFGApprox.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -import Node from "../../core/Node.js"; -import OperatorNode from "../../math/OperatorNode.js"; -import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; - -// Analytical approximation of the DFG LUT, one half of the -// split-sum approximation used in indirect specular lighting. -// via 'environmentBRDF' from "Physically Based Shading on Mobile" -// https://www.unrealengine.com/blog/physically-based-shading-on-mobile -declare const DFGApprox: (args: { roughness: Node; dotNV: Node }) => ShaderNodeObject; - -export default DFGApprox; diff --git a/src-testing/src/nodes/functions/BSDF/D_GGX.d.ts b/src-testing/src/nodes/functions/BSDF/D_GGX.d.ts deleted file mode 100644 index 2ba114b41..000000000 --- a/src-testing/src/nodes/functions/BSDF/D_GGX.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import Node from "../../core/Node.js"; -import OperatorNode from "../../math/OperatorNode.js"; -import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; - -// Microfacet Models for Refraction through Rough Surfaces - equation (33) -// http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html -// alpha is "roughness squared" in Disney’s reparameterization -declare const D_GGX: (args: { alpha: Node; dotNH: Node }) => ShaderNodeObject; - -export default D_GGX; diff --git a/src-testing/src/nodes/functions/BSDF/D_GGX_Anisotropic.d.ts b/src-testing/src/nodes/functions/BSDF/D_GGX_Anisotropic.d.ts deleted file mode 100644 index 3de167db6..000000000 --- a/src-testing/src/nodes/functions/BSDF/D_GGX_Anisotropic.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import Node from "../../core/Node.js"; -import OperatorNode from "../../math/OperatorNode.js"; -import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; - -// https://google.github.io/filament/Filament.md.html#materialsystem/anisotropicmodel/anisotropicspecularbrdf -declare const D_GGX_Anisotropic: ( - args: { alphaT: Node; alphaB: Node; dotNH: Node; dotTH: Node; dotBH: Node }, -) => ShaderNodeObject; - -export default D_GGX_Anisotropic; diff --git a/src-testing/src/nodes/functions/BSDF/F_Schlick.d.ts b/src-testing/src/nodes/functions/BSDF/F_Schlick.d.ts deleted file mode 100644 index ce3f23ffa..000000000 --- a/src-testing/src/nodes/functions/BSDF/F_Schlick.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import Node from "../../core/Node.js"; -import OperatorNode from "../../math/OperatorNode.js"; -import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; - -declare const F_Schlick: (args: { f0: Node; f90: Node; dotVH: Node }) => ShaderNodeObject; - -export default F_Schlick; diff --git a/src-testing/src/nodes/functions/BSDF/LTC.d.ts b/src-testing/src/nodes/functions/BSDF/LTC.d.ts deleted file mode 100644 index 4bd6d7f76..000000000 --- a/src-testing/src/nodes/functions/BSDF/LTC.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import Node from "../../core/Node.js"; - -declare const LTC_Uv: (args: { N: Node; V: Node; roughness: Node }) => Node; - -declare const LTC_Evaluate: ( - args: { N: Node; V: Node; P: Node; mInv: Node; p0: Node; p1: Node; p2: Node; p3: Node }, -) => Node; - -export { LTC_Evaluate, LTC_Uv }; diff --git a/src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated.d.ts b/src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated.d.ts deleted file mode 100644 index f736325ff..000000000 --- a/src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -import Node from "../../core/Node.js"; -import OperatorNode from "../../math/OperatorNode.js"; -import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; - -declare const V_GGX_SmithCorrelated: (inputs: { - alpha: Node; - dotNL: Node; - dotNV: Node; -}) => ShaderNodeObject; - -export default V_GGX_SmithCorrelated; diff --git a/src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated_Anisotropic.d.ts b/src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated_Anisotropic.d.ts deleted file mode 100644 index c2c6f77f7..000000000 --- a/src-testing/src/nodes/functions/BSDF/V_GGX_SmithCorrelated_Anisotropic.d.ts +++ /dev/null @@ -1,16 +0,0 @@ -import Node from "../../core/Node.js"; -import MathNode from "../../math/MathNode.js"; -import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; - -declare const V_GGX_SmithCorrelated: (inputs: { - alphaT: Node; - alphaB: Node; - dotTV: Node; - dotBV: Node; - dotTL: Node; - dotBL: Node; - dotNV: Node; - dotNL: Node; -}) => ShaderNodeObject; - -export default V_GGX_SmithCorrelated; diff --git a/src-testing/src/nodes/functions/BasicLightingModel.d.ts b/src-testing/src/nodes/functions/BasicLightingModel.d.ts deleted file mode 100644 index a64fafd44..000000000 --- a/src-testing/src/nodes/functions/BasicLightingModel.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import LightingModel from "../core/LightingModel.js"; - -declare class BasicLightingModel extends LightingModel { - constructor(); -} - -export default BasicLightingModel; diff --git a/src-testing/src/nodes/functions/PhongLightingModel.d.ts b/src-testing/src/nodes/functions/PhongLightingModel.d.ts deleted file mode 100644 index 5df595269..000000000 --- a/src-testing/src/nodes/functions/PhongLightingModel.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import BasicLightingModel from "./BasicLightingModel.js"; - -export default class PhongLightingModel extends BasicLightingModel { - specular: boolean; - - constructor(specular?: boolean); -} diff --git a/src-testing/src/nodes/functions/PhysicalLightingModel.d.ts b/src-testing/src/nodes/functions/PhysicalLightingModel.d.ts deleted file mode 100644 index bec381051..000000000 --- a/src-testing/src/nodes/functions/PhysicalLightingModel.d.ts +++ /dev/null @@ -1,30 +0,0 @@ -import LightingModel from "../core/LightingModel.js"; -import Node from "../core/Node.js"; - -export default class PhysicalLightingModel extends LightingModel { - clearcoat: boolean; - sheen: boolean; - iridescence: boolean; - anisotropy: boolean; - transmission: boolean; - dispersion: boolean; - - clearcoatRadiance: Node | null; - clearcoatSpecularDirect: Node | null; - clearcoatSpecularIndirect: Node | null; - sheenSpecularDirect: Node | null; - sheenSpecularIndirect: Node | null; - iridescenceFresnel: Node | null; - iridescenceF0: Node | null; - - constructor( - clearcoat?: boolean, - sheen?: boolean, - iridescence?: boolean, - anisotropy?: boolean, - transmission?: boolean, - dispersion?: boolean, - ); - - computeMultiscattering(singleScatter: Node, multiScatter: Node, specularF90: Node): void; -} diff --git a/src-testing/src/nodes/functions/ShadowMaskModel.d.ts b/src-testing/src/nodes/functions/ShadowMaskModel.d.ts deleted file mode 100644 index 512b06db3..000000000 --- a/src-testing/src/nodes/functions/ShadowMaskModel.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import LightingModel from "../core/LightingModel.js"; -import VarNode from "../core/VarNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class ShadowMaskModel extends LightingModel { - shadowNode: ShaderNodeObject; - - constructor(); -} diff --git a/src-testing/src/nodes/functions/ToonLightingModel.d.ts b/src-testing/src/nodes/functions/ToonLightingModel.d.ts deleted file mode 100644 index d26db3457..000000000 --- a/src-testing/src/nodes/functions/ToonLightingModel.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -import LightingModel from "../core/LightingModel.js"; - -export default class ToonLightingModel extends LightingModel { -} diff --git a/src-testing/src/nodes/functions/material/getGeometryRoughness.d.ts b/src-testing/src/nodes/functions/material/getGeometryRoughness.d.ts deleted file mode 100644 index 729111bde..000000000 --- a/src-testing/src/nodes/functions/material/getGeometryRoughness.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -import MathNode from "../../math/MathNode.js"; -import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; - -declare const getGeometryRoughness: () => ShaderNodeObject; - -export default getGeometryRoughness; diff --git a/src-testing/src/nodes/functions/material/getRoughness.d.ts b/src-testing/src/nodes/functions/material/getRoughness.d.ts deleted file mode 100644 index 56e7d3e6b..000000000 --- a/src-testing/src/nodes/functions/material/getRoughness.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import Node from "../../core/Node.js"; -import MathNode from "../../math/MathNode.js"; -import { ShaderNodeObject } from "../../shadernode/ShaderNode.js"; - -declare const getRoughness: (args: { roughness: Node }) => ShaderNodeObject; - -export default getRoughness; diff --git a/src-testing/src/nodes/geometry/RangeNode.d.ts b/src-testing/src/nodes/geometry/RangeNode.d.ts deleted file mode 100644 index 183649866..000000000 --- a/src-testing/src/nodes/geometry/RangeNode.d.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { Color } from "../../math/Color.js"; -import { Vector2 } from "../../math/Vector2.js"; -import { Vector3 } from "../../math/Vector3.js"; -import { Vector4 } from "../../math/Vector4.js"; -import Node from "../core/Node.js"; -import NodeBuilder from "../core/NodeBuilder.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export type RangeModeBound = number | Color | Vector2 | Vector3 | Vector4; - -export default class RangeNode extends Node { - min: RangeModeBound; - max: RangeModeBound; - - constructor(min: RangeModeBound, max: RangeModeBound); - getVectorLength(builder: NodeBuilder): number; -} - -export const range: (min: RangeModeBound, max: RangeModeBound) => ShaderNodeObject; diff --git a/src-testing/src/nodes/gpgpu/ComputeNode.ts b/src-testing/src/nodes/gpgpu/ComputeNode.ts deleted file mode 100644 index ab2925a55..000000000 --- a/src-testing/src/nodes/gpgpu/ComputeNode.ts +++ /dev/null @@ -1,67 +0,0 @@ -import Node, { addNodeClass } from '../core/Node.js'; -import { NodeUpdateType } from '../core/constants.js'; -import { addNodeElement, nodeObject } from '../shadernode/ShaderNode.js'; - -class ComputeNode extends Node { - constructor(computeNode, count, workgroupSize = [64]) { - super('void'); - - this.isComputeNode = true; - - this.computeNode = computeNode; - - this.count = count; - this.workgroupSize = workgroupSize; - this.dispatchCount = 0; - - this.version = 1; - this.updateBeforeType = NodeUpdateType.OBJECT; - - this.updateDispatchCount(); - } - - dispose() { - this.dispatchEvent({ type: 'dispose' }); - } - - set needsUpdate(value) { - if (value === true) this.version++; - } - - updateDispatchCount() { - const { count, workgroupSize } = this; - - let size = workgroupSize[0]; - - for (let i = 1; i < workgroupSize.length; i++) size *= workgroupSize[i]; - - this.dispatchCount = Math.ceil(count / size); - } - - onInit() {} - - updateBefore({ renderer }) { - renderer.compute(this); - } - - generate(builder) { - const { shaderStage } = builder; - - if (shaderStage === 'compute') { - const snippet = this.computeNode.build(builder, 'void'); - - if (snippet !== '') { - builder.addLineFlowCode(snippet); - } - } - } -} - -export default ComputeNode; - -export const compute = (node, count, workgroupSize) => - nodeObject(new ComputeNode(nodeObject(node), count, workgroupSize)); - -addNodeElement('compute', compute); - -addNodeClass('ComputeNode', ComputeNode); diff --git a/src-testing/src/nodes/lighting/AONode.d.ts b/src-testing/src/nodes/lighting/AONode.d.ts deleted file mode 100644 index 998ec5236..000000000 --- a/src-testing/src/nodes/lighting/AONode.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import Node from "../core/Node.js"; -import LightingNode from "./LightingNode.js"; - -export default class AONode extends LightingNode { - aoNode: Node | null; - - constructor(aoNode?: Node | null); -} diff --git a/src-testing/src/nodes/lighting/AnalyticLightNode.d.ts b/src-testing/src/nodes/lighting/AnalyticLightNode.d.ts deleted file mode 100644 index d8cea32ef..000000000 --- a/src-testing/src/nodes/lighting/AnalyticLightNode.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Light } from "../../lights/Light.js"; -import LightingNode from "./LightingNode.js"; - -export default class AnalyticLightNode extends LightingNode { - light: T | null; - - constructor(light?: T | null); -} diff --git a/src-testing/src/nodes/lighting/BasicEnvironmentNode.d.ts b/src-testing/src/nodes/lighting/BasicEnvironmentNode.d.ts deleted file mode 100644 index de244562f..000000000 --- a/src-testing/src/nodes/lighting/BasicEnvironmentNode.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import Node from "../core/Node.js"; -import LightingNode from "./LightingNode.js"; - -declare class BasicEnvironmentNode extends LightingNode { - envNode: Node | null; - - constructor(envNode?: Node | null); -} - -export default BasicEnvironmentNode; diff --git a/src-testing/src/nodes/lighting/BasicLightMapNode.d.ts b/src-testing/src/nodes/lighting/BasicLightMapNode.d.ts deleted file mode 100644 index c759e8857..000000000 --- a/src-testing/src/nodes/lighting/BasicLightMapNode.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import Node from "../core/Node.js"; -import LightingNode from "./LightingNode.js"; - -declare class BasicLightMapNode extends LightingNode { - constructor(lightMapNode?: Node | null); -} - -export default BasicLightMapNode; diff --git a/src-testing/src/nodes/lighting/EnvironmentNode.ts b/src-testing/src/nodes/lighting/EnvironmentNode.ts deleted file mode 100644 index 92a8c955d..000000000 --- a/src-testing/src/nodes/lighting/EnvironmentNode.ts +++ /dev/null @@ -1,121 +0,0 @@ -import LightingNode from './LightingNode.js'; -import { cache } from '../core/CacheNode.js'; -import { context } from '../core/ContextNode.js'; -import { roughness, clearcoatRoughness } from '../core/PropertyNode.js'; -import { cameraViewMatrix } from '../accessors/CameraNode.js'; -import { - transformedClearcoatNormalView, - transformedNormalView, - transformedNormalWorld, -} from '../accessors/NormalNode.js'; -import { positionViewDirection } from '../accessors/PositionNode.js'; -import { addNodeClass } from '../core/Node.js'; -import { float } from '../shadernode/ShaderNode.js'; -import { reference } from '../accessors/ReferenceNode.js'; -import { transformedBentNormalView } from '../accessors/AccessorsUtils.js'; -import { pmremTexture } from '../pmrem/PMREMNode.js'; - -const _envNodeCache = new WeakMap(); - -class EnvironmentNode extends LightingNode { - constructor(envNode = null) { - super(); - - this.envNode = envNode; - } - - setup(builder) { - const { material } = builder; - - let envNode = this.envNode; - - if (envNode.isTextureNode || envNode.isMaterialReferenceNode) { - const value = envNode.isTextureNode ? envNode.value : material[envNode.property]; - - let cacheEnvNode = _envNodeCache.get(value); - - if (cacheEnvNode === undefined) { - cacheEnvNode = pmremTexture(value); - - _envNodeCache.set(value, cacheEnvNode); - } - - envNode = cacheEnvNode; - } - - // - - const envMap = material.envMap; - const intensity = envMap - ? reference('envMapIntensity', 'float', builder.material) - : reference('environmentIntensity', 'float', builder.scene); // @TODO: Add materialEnvIntensity in MaterialNode - - const useAnisotropy = material.useAnisotropy === true || material.anisotropy > 0; - const radianceNormalView = useAnisotropy ? transformedBentNormalView : transformedNormalView; - - const radiance = context(envNode, createRadianceContext(roughness, radianceNormalView)).mul(intensity); - const irradiance = context(envNode, createIrradianceContext(transformedNormalWorld)) - .mul(Math.PI) - .mul(intensity); - - const isolateRadiance = cache(radiance); - const isolateIrradiance = cache(irradiance); - - // - - builder.context.radiance.addAssign(isolateRadiance); - - builder.context.iblIrradiance.addAssign(isolateIrradiance); - - // - - const clearcoatRadiance = builder.context.lightingModel.clearcoatRadiance; - - if (clearcoatRadiance) { - const clearcoatRadianceContext = context( - envNode, - createRadianceContext(clearcoatRoughness, transformedClearcoatNormalView), - ).mul(intensity); - const isolateClearcoatRadiance = cache(clearcoatRadianceContext); - - clearcoatRadiance.addAssign(isolateClearcoatRadiance); - } - } -} - -const createRadianceContext = (roughnessNode, normalViewNode) => { - let reflectVec = null; - - return { - getUV: () => { - if (reflectVec === null) { - reflectVec = positionViewDirection.negate().reflect(normalViewNode); - - // Mixing the reflection with the normal is more accurate and keeps rough objects from gathering light from behind their tangent plane. - reflectVec = roughnessNode.mul(roughnessNode).mix(reflectVec, normalViewNode).normalize(); - - reflectVec = reflectVec.transformDirection(cameraViewMatrix); - } - - return reflectVec; - }, - getTextureLevel: () => { - return roughnessNode; - }, - }; -}; - -const createIrradianceContext = normalWorldNode => { - return { - getUV: () => { - return normalWorldNode; - }, - getTextureLevel: () => { - return float(1.0); - }, - }; -}; - -export default EnvironmentNode; - -addNodeClass('EnvironmentNode', EnvironmentNode); diff --git a/src-testing/src/nodes/lighting/HemisphereLightNode.d.ts b/src-testing/src/nodes/lighting/HemisphereLightNode.d.ts deleted file mode 100644 index 7cf38dd79..000000000 --- a/src-testing/src/nodes/lighting/HemisphereLightNode.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { HemisphereLight } from "../../lights/HemisphereLight.js"; -import Object3DNode from "../accessors/Object3DNode.js"; -import Node from "../core/Node.js"; -import AnalyticLightNode from "./AnalyticLightNode.js"; - -export default class HemisphereLightNode extends AnalyticLightNode { - lightPositionNode: Object3DNode; - lightDirectionNode: Node; - - groundColorNode: Node; - - constructor(light?: HemisphereLight | null); -} diff --git a/src-testing/src/nodes/lighting/IrradianceNode.d.ts b/src-testing/src/nodes/lighting/IrradianceNode.d.ts deleted file mode 100644 index a59838044..000000000 --- a/src-testing/src/nodes/lighting/IrradianceNode.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import Node from "../core/Node.js"; -import LightingNode from "./LightingNode.js"; - -export default class IrradianceNode extends LightingNode { - node: Node | null; - - constructor(node?: Node | null); -} diff --git a/src-testing/src/nodes/lighting/LightUtils.d.ts b/src-testing/src/nodes/lighting/LightUtils.d.ts deleted file mode 100644 index 640289d18..000000000 --- a/src-testing/src/nodes/lighting/LightUtils.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import Node from "../core/Node.js"; -import CondNode from "../math/CondNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const getDistanceAttenuation: (args: { - lightDistance: Node; - cutoffDistance: Node; - decayExponent: Node; -}) => ShaderNodeObject; diff --git a/src-testing/src/nodes/lighting/LightingContextNode.ts b/src-testing/src/nodes/lighting/LightingContextNode.ts deleted file mode 100644 index 02a8b51f8..000000000 --- a/src-testing/src/nodes/lighting/LightingContextNode.ts +++ /dev/null @@ -1,58 +0,0 @@ -import ContextNode from '../core/ContextNode.js'; -import { addNodeClass } from '../core/Node.js'; -import { addNodeElement, nodeProxy, float, vec3 } from '../shadernode/ShaderNode.js'; - -class LightingContextNode extends ContextNode { - constructor(node, lightingModel = null, backdropNode = null, backdropAlphaNode = null) { - super(node); - - this.lightingModel = lightingModel; - this.backdropNode = backdropNode; - this.backdropAlphaNode = backdropAlphaNode; - - this._context = null; - } - - getContext() { - const { backdropNode, backdropAlphaNode } = this; - - const directDiffuse = vec3().temp('directDiffuse'), - directSpecular = vec3().temp('directSpecular'), - indirectDiffuse = vec3().temp('indirectDiffuse'), - indirectSpecular = vec3().temp('indirectSpecular'); - - const reflectedLight = { - directDiffuse, - directSpecular, - indirectDiffuse, - indirectSpecular, - }; - - const context = { - radiance: vec3().temp('radiance'), - irradiance: vec3().temp('irradiance'), - iblIrradiance: vec3().temp('iblIrradiance'), - ambientOcclusion: float(1).temp('ambientOcclusion'), - reflectedLight, - backdrop: backdropNode, - backdropAlpha: backdropAlphaNode, - }; - - return context; - } - - setup(builder) { - this.context = this._context || (this._context = this.getContext()); - this.context.lightingModel = this.lightingModel || builder.context.lightingModel; - - return super.setup(builder); - } -} - -export default LightingContextNode; - -export const lightingContext = nodeProxy(LightingContextNode); - -addNodeElement('lightingContext', lightingContext); - -addNodeClass('LightingContextNode', LightingContextNode); diff --git a/src-testing/src/nodes/lighting/LightingNode.d.ts b/src-testing/src/nodes/lighting/LightingNode.d.ts deleted file mode 100644 index 3e8dd5424..000000000 --- a/src-testing/src/nodes/lighting/LightingNode.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import Node from "../core/Node.js"; - -export default abstract class LightingNode extends Node { - readonly isLightingNode: true; - - constructor(); -} diff --git a/src-testing/src/nodes/lighting/LightsNode.ts b/src-testing/src/nodes/lighting/LightsNode.ts deleted file mode 100644 index 9a41cc7dc..000000000 --- a/src-testing/src/nodes/lighting/LightsNode.ts +++ /dev/null @@ -1,168 +0,0 @@ -import Node from '../core/Node.js'; -import AnalyticLightNode from './AnalyticLightNode.js'; -import { nodeObject, nodeProxy, vec3 } from '../shadernode/ShaderNode.js'; - -const LightNodes = new WeakMap(); - -const sortLights = lights => { - return lights.sort((a, b) => a.id - b.id); -}; - -class LightsNode extends Node { - constructor(lightNodes = []) { - super('vec3'); - - this.totalDiffuseNode = vec3().temp('totalDiffuse'); - this.totalSpecularNode = vec3().temp('totalSpecular'); - - this.outgoingLightNode = vec3().temp('outgoingLight'); - - this.lightNodes = lightNodes; - - this._hash = null; - } - - get hasLight() { - return this.lightNodes.length > 0; - } - - getHash() { - if (this._hash === null) { - const hash = []; - - for (const lightNode of this.lightNodes) { - hash.push(lightNode.getHash()); - } - - this._hash = 'lights-' + hash.join(','); - } - - return this._hash; - } - - analyze(builder) { - const properties = builder.getDataFromNode(this); - - for (const node of properties.nodes) { - node.build(builder); - } - } - - setup(builder) { - const context = builder.context; - const lightingModel = context.lightingModel; - - let outgoingLightNode = this.outgoingLightNode; - - if (lightingModel) { - const { lightNodes, totalDiffuseNode, totalSpecularNode } = this; - - context.outgoingLight = outgoingLightNode; - - const stack = builder.addStack(); - - // - - const properties = builder.getDataFromNode(this); - properties.nodes = stack.nodes; - - // - - lightingModel.start(context, stack, builder); - - // lights - - for (const lightNode of lightNodes) { - lightNode.build(builder); - } - - // - - lightingModel.indirect(context, stack, builder); - - // - - const { backdrop, backdropAlpha } = context; - const { directDiffuse, directSpecular, indirectDiffuse, indirectSpecular } = context.reflectedLight; - - let totalDiffuse = directDiffuse.add(indirectDiffuse); - - if (backdrop !== null) { - if (backdropAlpha !== null) { - totalDiffuse = vec3(backdropAlpha.mix(totalDiffuse, backdrop)); - } else { - totalDiffuse = vec3(backdrop); - } - - context.material.transparent = true; - } - - totalDiffuseNode.assign(totalDiffuse); - totalSpecularNode.assign(directSpecular.add(indirectSpecular)); - - outgoingLightNode.assign(totalDiffuseNode.add(totalSpecularNode)); - - // - - lightingModel.finish(context, stack, builder); - - // - - outgoingLightNode = outgoingLightNode.bypass(builder.removeStack()); - } - - return outgoingLightNode; - } - - _getLightNodeById(id) { - for (const lightNode of this.lightNodes) { - if (lightNode.isAnalyticLightNode && lightNode.light.id === id) { - return lightNode; - } - } - - return null; - } - - fromLights(lights = []) { - const lightNodes = []; - - lights = sortLights(lights); - - for (const light of lights) { - let lightNode = this._getLightNodeById(light.id); - - if (lightNode === null) { - const lightClass = light.constructor; - const lightNodeClass = LightNodes.has(lightClass) ? LightNodes.get(lightClass) : AnalyticLightNode; - - lightNode = nodeObject(new lightNodeClass(light)); - } - - lightNodes.push(lightNode); - } - - this.lightNodes = lightNodes; - this._hash = null; - - return this; - } -} - -export default LightsNode; - -export const lights = lights => nodeObject(new LightsNode().fromLights(lights)); -export const lightsNode = nodeProxy(LightsNode); - -export function addLightNode(lightClass, lightNodeClass) { - if (LightNodes.has(lightClass)) { - console.warn(`Redefinition of light node ${lightNodeClass.type}`); - return; - } - - if (typeof lightClass !== 'function') throw new Error(`Light ${lightClass.name} is not a class`); - if (typeof lightNodeClass !== 'function' || !lightNodeClass.type) - throw new Error(`Light node ${lightNodeClass.type} is not a class`); - - LightNodes.set(lightClass, lightNodeClass); -} diff --git a/src-testing/src/nodes/lighting/PointLightNode.d.ts b/src-testing/src/nodes/lighting/PointLightNode.d.ts deleted file mode 100644 index ee0407d25..000000000 --- a/src-testing/src/nodes/lighting/PointLightNode.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { PointLight } from "../../lights/PointLight.js"; -import Node from "../core/Node.js"; -import AnalyticLightNode from "./AnalyticLightNode.js"; - -export default class PointLightNode extends AnalyticLightNode { - cutoffDistanceNode: Node; - decayExponentNode: Node; - - constructor(light?: PointLight | null); -} diff --git a/src-testing/src/nodes/lighting/RectAreaLightNode.d.ts b/src-testing/src/nodes/lighting/RectAreaLightNode.d.ts deleted file mode 100644 index db4d18b82..000000000 --- a/src-testing/src/nodes/lighting/RectAreaLightNode.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { RectAreaLight } from "../../lights/RectAreaLight.js"; -import { DataTexture } from "../../textures/DataTexture.js"; -import Node from "../core/Node.js"; -import AnalyticLightNode from "./AnalyticLightNode.js"; - -export interface RectAreaLightTexturesLib { - LTC_HALF_1: DataTexture; - LTC_HALF_2: DataTexture; - - LTC_FLOAT_1: DataTexture; - LTC_FLOAT_2: DataTexture; -} - -export default class RectAreaLightNode extends AnalyticLightNode { - halfHeight: Node; - halfWidth: Node; - - constructor(light?: RectAreaLight | null); - - static setLTC(ltc: RectAreaLightTexturesLib): void; -} diff --git a/src-testing/src/nodes/lighting/SpotLightNode.d.ts b/src-testing/src/nodes/lighting/SpotLightNode.d.ts deleted file mode 100644 index 0b1ae4b13..000000000 --- a/src-testing/src/nodes/lighting/SpotLightNode.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { SpotLight } from "../../lights/SpotLight.js"; -import Node from "../core/Node.js"; -import AnalyticLightNode from "./AnalyticLightNode.js"; - -export default class PointLightNode extends AnalyticLightNode { - directionNode: Node; - - coneCosNode: Node; - penumbraCosNode: Node; - - cutoffDistanceNode: Node; - decayExponentNode: Node; - - constructor(light?: SpotLight | null); -} diff --git a/src-testing/src/nodes/loaders/NodeLoader.d.ts b/src-testing/src/nodes/loaders/NodeLoader.d.ts deleted file mode 100644 index 0ced68bab..000000000 --- a/src-testing/src/nodes/loaders/NodeLoader.d.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { Loader } from "../../loaders/Loader.js"; -import { LoadingManager } from "../../loaders/LoadingManager.js"; -import { Texture } from "../../textures/Texture.js"; -import { Node } from "../Nodes.js"; - -export interface NodeLoaderResult { - [hash: string]: Node; -} - -export default class NodeLoader extends Loader { - constructor(manager?: LoadingManager); - - parseNodes(json: unknown): NodeLoaderResult; - parse(json: unknown): Node; - setTextures(textures: { [key: string]: Texture }): this; -} diff --git a/src-testing/src/nodes/loaders/NodeMaterialLoader.d.ts b/src-testing/src/nodes/loaders/NodeMaterialLoader.d.ts deleted file mode 100644 index 4f927141e..000000000 --- a/src-testing/src/nodes/loaders/NodeMaterialLoader.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { MaterialLoader } from "../../loaders/MaterialLoader.js"; -import { NodeLoaderResult } from "./NodeLoader.js"; - -export default class NodeMaterialLoader extends MaterialLoader { - nodes: NodeLoaderResult; - - setNodes(value: NodeLoaderResult): this; -} diff --git a/src-testing/src/nodes/loaders/NodeObjectLoader.d.ts b/src-testing/src/nodes/loaders/NodeObjectLoader.d.ts deleted file mode 100644 index 7341a4a28..000000000 --- a/src-testing/src/nodes/loaders/NodeObjectLoader.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { ObjectLoader } from "../../loaders/ObjectLoader.js"; -import { Material } from "../../materials/Material.js"; -import { Texture } from "../../textures/Texture.js"; -import { NodeLoaderResult } from "./NodeLoader.js"; - -export default class NodeObjectLoader extends ObjectLoader { - parseNodes(json: unknown, textures: { [key: string]: Texture }): NodeLoaderResult; - - parseMaterials(json: unknown, textures: { [key: string]: Texture }): { [key: string]: Material }; -} diff --git a/src-testing/src/nodes/materials/Line2NodeMaterial.d.ts b/src-testing/src/nodes/materials/Line2NodeMaterial.d.ts deleted file mode 100644 index 405cc4749..000000000 --- a/src-testing/src/nodes/materials/Line2NodeMaterial.d.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { LineDashedMaterialParameters } from "../../materials/LineDashedMaterial.js"; -import { Color } from "../../math/Color.js"; -import { Texture } from "../../textures/Texture.js"; -import Node from "../core/Node.js"; -import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; - -export interface Line2NodeMaterialParameters extends NodeMaterialParameters, LineDashedMaterialParameters { - dashed?: boolean | undefined; -} - -export default class Line2NodeMaterial extends NodeMaterial { - lights: boolean; - - // Properties from LineDashedMaterial - readonly isLineDashedMaterial: true; - scale: number; - dashSize: number; - gapSize: number; - - // Properties from LineBasicMaterial - readonly isLineBasicMaterial: true; - color: Color; - fog: boolean; - linewidth: number; - linecap: string; - linejoin: string; - map: Texture | null; - - useAlphaToCoverage: boolean; - useColor: boolean; - useDash: boolean; - useWorldUnits: boolean; - - dashOffset: number; - lineWidth: number; - - lineColorNode: Node | null; - - offsetNode: Node | null; - dashScaleNode: Node | null; - dashSizeNode: Node | null; - gapSizeNode: Node | null; - - constructor(parameters?: Line2NodeMaterialParameters); - - setupShaders(): void; - - get worldUnits(): boolean; - set worldUnits(value: boolean); - - get dashed(): boolean; - set dashed(value: boolean); -} diff --git a/src-testing/src/nodes/materials/LineBasicNodeMaterial.d.ts b/src-testing/src/nodes/materials/LineBasicNodeMaterial.d.ts deleted file mode 100644 index 4ff1f5ee8..000000000 --- a/src-testing/src/nodes/materials/LineBasicNodeMaterial.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { LineBasicMaterialParameters } from "../../materials/LineBasicMaterial.js"; -import { Color } from "../../math/Color.js"; -import { Texture } from "../../textures/Texture.js"; -import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; - -export interface LineBasicNodeMaterialParameters extends NodeMaterialParameters, LineBasicMaterialParameters { -} - -export default class LineBasicNodeMaterial extends NodeMaterial { - readonly isLineBasicNodeMaterial: true; - - // Properties from LineBasicMaterial - readonly isLineBasicMaterial: true; - color: Color; - fog: boolean; - linewidth: number; - linecap: string; - linejoin: string; - map: Texture | null; - - constructor(parameters?: LineBasicNodeMaterialParameters); -} diff --git a/src-testing/src/nodes/materials/Materials.d.ts b/src-testing/src/nodes/materials/Materials.d.ts deleted file mode 100644 index 9d668a8a1..000000000 --- a/src-testing/src/nodes/materials/Materials.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -export { default as Line2NodeMaterial } from "./Line2NodeMaterial.js"; -export { default as LineBasicNodeMaterial } from "./LineBasicNodeMaterial.js"; -export { default as MeshBasicNodeMaterial } from "./MeshBasicNodeMaterial.js"; -export { default as MeshMatcapNodeMaterial } from "./MeshMatcapNodeMaterial.js"; -export { default as MeshNormalNodeMaterial } from "./MeshNormalNodeMaterial.js"; -export { default as MeshPhongNodeMaterial } from "./MeshPhongNodeMaterial.js"; -export { default as MeshPhysicalNodeMaterial } from "./MeshPhysicalNodeMaterial.js"; -export { default as MeshSSSPhysicalNodeMaterial } from "./MeshSSSNodeMaterial.js"; -export { default as MeshStandardNodeMaterial } from "./MeshStandardNodeMaterial.js"; -export { default as MeshToonNodeMaterial } from "./MeshToonNodeMaterial.js"; -export { addNodeMaterial, createNodeMaterialFromType, default as NodeMaterial } from "./NodeMaterial.js"; -export { default as PointsNodeMaterial } from "./PointsNodeMaterial.js"; -export { default as ShadowNodeMaterial } from "./ShadowNodeMaterial.js"; -export { default as SpriteNodeMaterial } from "./SpriteNodeMaterial.js"; -export { default as VolumeNodeMaterial } from "./VolumeNodeMaterial.js"; diff --git a/src-testing/src/nodes/materials/MeshBasicNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshBasicNodeMaterial.d.ts deleted file mode 100644 index ff5cc609d..000000000 --- a/src-testing/src/nodes/materials/MeshBasicNodeMaterial.d.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Combine } from "../../constants.js"; -import { MeshBasicMaterialParameters } from "../../materials/MeshBasicMaterial.js"; -import { Color } from "../../math/Color.js"; -import { Euler } from "../../math/Euler.js"; -import { Texture } from "../../textures/Texture.js"; -import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; - -export interface MeshBasicNodeMaterialParameters extends NodeMaterialParameters, MeshBasicMaterialParameters { -} - -export default class MeshBasicNodeMaterial extends NodeMaterial { - readonly isMeshBasicNodeMaterial: true; - - // Properties from MeshBasicMaterial - readonly isMeshBasicMaterial: true; - color: Color; - map: Texture | null; - lightMap: Texture | null; - lightMapIntensity: number; - aoMap: Texture | null; - aoMapIntensity: number; - specularMap: Texture | null; - alphaMap: Texture | null; - envMap: Texture | null; - envMapRotation: Euler; - combine: Combine; - reflectivity: number; - refractionRatio: number; - wireframe: boolean; - wireframeLinewidth: number; - wireframeLinecap: string; - wireframeLinejoin: string; - fog: boolean; - - constructor(parameters?: MeshBasicNodeMaterialParameters); -} diff --git a/src-testing/src/nodes/materials/MeshMatcapNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshMatcapNodeMaterial.d.ts deleted file mode 100644 index accb772de..000000000 --- a/src-testing/src/nodes/materials/MeshMatcapNodeMaterial.d.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { NormalMapTypes } from "../../constants.js"; -import { MeshMatcapMaterialParameters } from "../../materials/MeshMatcapMaterial.js"; -import { Color } from "../../math/Color.js"; -import { Vector2 } from "../../math/Vector2.js"; -import { Texture } from "../../textures/Texture.js"; -import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; - -export interface MeshMatcapNodeMaterialParameters extends NodeMaterialParameters, MeshMatcapMaterialParameters { -} - -export default class MeshMatcapNodeMaterial extends NodeMaterial { - readonly isMeshMatcapNodeMaterial: true; - - // Properties from MeshMatcapMaterial - readonly isMeshMatcapMaterial: true; - color: Color; - matcap: Texture | null; - map: Texture | null; - bumpMap: Texture | null; - bumpScale: number; - normalMap: Texture | null; - normalMapType: NormalMapTypes; - normalScale: Vector2; - displacementMap: Texture | null; - displacementScale: number; - displacementBias: number; - alphaMap: Texture | null; - flatShading: boolean; - fog: boolean; - - constructor(paramters: MeshMatcapNodeMaterialParameters); -} diff --git a/src-testing/src/nodes/materials/MeshNormalNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshNormalNodeMaterial.d.ts deleted file mode 100644 index f6bb618c1..000000000 --- a/src-testing/src/nodes/materials/MeshNormalNodeMaterial.d.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { NormalMapTypes } from "../../constants.js"; -import { MeshNormalMaterialParameters } from "../../materials/MeshNormalMaterial.js"; -import { Vector2 } from "../../math/Vector2.js"; -import { Texture } from "../../textures/Texture.js"; -import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; - -export interface MeshBasicNodeMaterialParameters extends NodeMaterialParameters, MeshNormalMaterialParameters { -} - -export default class MeshNormalNodeMaterial extends NodeMaterial { - readonly isMeshNormalNodeMaterial: true; - - // Properties from MeshNormalMaterial - readonly isMeshNormalMaterial: true; - bumpMap: Texture | null; - bumpScale: number; - normalMap: Texture | null; - normalMapType: NormalMapTypes; - normalScale: Vector2; - displacementMap: Texture | null; - displacementScale: number; - displacementBias: number; - wireframe: boolean; - wireframeLinewidth: number; - flatShading: boolean; - - constructor(parameters?: MeshBasicNodeMaterialParameters); -} diff --git a/src-testing/src/nodes/materials/MeshPhongNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshPhongNodeMaterial.d.ts deleted file mode 100644 index 97d83d196..000000000 --- a/src-testing/src/nodes/materials/MeshPhongNodeMaterial.d.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { Combine, NormalMapTypes } from "../../constants.js"; -import { MeshPhongMaterialParameters } from "../../materials/MeshPhongMaterial.js"; -import { Color } from "../../math/Color.js"; -import { Euler } from "../../math/Euler.js"; -import { Vector2 } from "../../math/Vector2.js"; -import { Texture } from "../../textures/Texture.js"; -import Node from "../core/Node.js"; -import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; - -export interface MeshPhongNodeMaterialParameters extends NodeMaterialParameters, MeshPhongMaterialParameters { -} - -export default class MeshPhongNodeMaterial extends NodeMaterial { - readonly isMeshPhongNodeMaterial: true; - - shininessNode: Node | null; - specularNode: Node | null; - - // Properties from MeshPhongMaterial - readonly isMeshPhongMaterial: true; - color: Color; - specular: Color; - shininess: number; - map: Texture | null; - lightMap: Texture | null; - lightMapIntensity: number; - aoMap: Texture | null; - aoMapIntensity: number; - emissive: Color; - emissiveIntensity: number; - emissiveMap: Texture | null; - bumpMap: Texture | null; - bumpScale: number; - normalMap: Texture | null; - normalMapType: NormalMapTypes; - normalScale: Vector2; - displacementMap: Texture | null; - displacementScale: number; - displacementBias: number; - specularMap: Texture | null; - alphaMap: Texture | null; - envMap: Texture | null; - envMapRotation: Euler; - combine: Combine; - reflectivity: number; - refractionRatio: number; - wireframe: boolean; - wireframeLinewidth: number; - wireframeLinecap: string; - wireframeLinejoin: string; - flatShading: boolean; - metal: boolean; - fog: boolean; - - constructor(parameters?: MeshPhongNodeMaterialParameters); -} diff --git a/src-testing/src/nodes/materials/MeshPhysicalNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshPhysicalNodeMaterial.d.ts deleted file mode 100644 index 3b9e500f1..000000000 --- a/src-testing/src/nodes/materials/MeshPhysicalNodeMaterial.d.ts +++ /dev/null @@ -1,90 +0,0 @@ -import { MeshPhysicalMaterialParameters } from "../../materials/MeshPhysicalMaterial.js"; -import { Color } from "../../math/Color.js"; -import { Vector2 } from "../../math/Vector2.js"; -import { Texture } from "../../textures/Texture.js"; -import Node from "../core/Node.js"; -import MeshStandardNodeMaterial, { MeshStandardNodeMaterialParameters } from "./MeshStandardNodeMaterial.js"; - -export interface MeshPhysicalNodeMaterialParameters - extends MeshStandardNodeMaterialParameters, MeshPhysicalMaterialParameters -{ -} - -export default class MeshPhysicalNodeMaterial extends MeshStandardNodeMaterial { - readonly isMeshPhysicalNodeMaterial: true; - - clearcoatNode: Node | null; - clearcoatRoughnessNode: Node | null; - clearcoatNormalNode: Node | null; - - sheenNode: Node | null; - sheenRoughnessNode: Node | null; - - iridescenceNode: Node | null; - iridescenceIORNode: Node | null; - iridescenceThicknessNode: Node | null; - - iorNode: Node | null; - - specularIntensityNode: Node | null; - specularColorNode: Node | null; - - transmissionNode: Node | null; - thicknessNode: Node | null; - attenuationDistanceNode: Node | null; - attenuationColorNode: Node | null; - dispersionNode: Node | null; - - anisotropyNode: Node | null; - - // Properties from MeshPhysicalMaterial - readonly isMeshPhysicalMaterial: true; - anisotropyRotation: number; - anisotropyMap: Texture | null; - clearcoatMap: Texture | null; - clearcoatRoughness: number; - clearcoatRoughnessMap: Texture | null; - clearcoatNormalScale: Vector2; - clearcoatNormalMap: Texture | null; - ior: number; - get reflectivity(): number; - set reflectivity(reflectivity: number); - iridescenceMap: Texture | null; - iridescenceIOR: number; - iridescenceThicknessRange: [number, number]; - iridescenceThicknessMap: Texture | null; - sheenColor: Color; - sheenColorMap: Texture | null; - sheenRoughness: number; - sheenRoughnessMap: Texture | null; - transmissionMap: Texture | null; - thickness: number; - thicknessMap: Texture | null; - attenuationDistance: number; - attenuationColor: Color; - specularIntensity: number; - specularIntensityMap: Texture | null; - specularColor: Color; - specularColorMap: Texture | null; - get anisotropy(): number; - set anisotropy(value: number); - get clearcoat(): number; - set clearcoat(value: number); - get iridescence(): number; - set iridescence(value: number); - get dispersion(): number; - set dispersion(value: number); - get sheen(): number; - set sheen(value: number); - get transmission(): number; - set transmission(value: number); - - constructor(parameters?: MeshPhysicalNodeMaterialParameters); - - get useClearcoat(): boolean; - get useIridescence(): boolean; - get useSheen(): boolean; - get useAnisotropy(): boolean; - get useTransmission(): boolean; - get useDispersion(): boolean; -} diff --git a/src-testing/src/nodes/materials/MeshSSSNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshSSSNodeMaterial.d.ts deleted file mode 100644 index 8506de76c..000000000 --- a/src-testing/src/nodes/materials/MeshSSSNodeMaterial.d.ts +++ /dev/null @@ -1,16 +0,0 @@ -import ConstNode from "../core/ConstNode.js"; -import Node from "../core/Node.js"; -import MeshPhysicalNodeMaterial, { MeshPhysicalNodeMaterialParameters } from "./MeshPhysicalNodeMaterial.js"; - -export default class MeshSSSNodeMaterial extends MeshPhysicalNodeMaterial { - thicknessColorNode: Node | null; - thicknessDistortionNode: ConstNode; - thicknessAmbientNode: ConstNode; - thicknessAttenuationNode: ConstNode; - thicknessPowerNode: ConstNode; - thicknessScaleNode: ConstNode; - - constructor(parameters?: MeshPhysicalNodeMaterialParameters); - - get useSSS(): boolean; -} diff --git a/src-testing/src/nodes/materials/MeshStandardNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshStandardNodeMaterial.d.ts deleted file mode 100644 index 6bc1dfb38..000000000 --- a/src-testing/src/nodes/materials/MeshStandardNodeMaterial.d.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { NormalMapTypes } from "../../constants.js"; -import { MeshStandardMaterialParameters } from "../../materials/MeshStandardMaterial.js"; -import { Color } from "../../math/Color.js"; -import { Euler } from "../../math/Euler.js"; -import { Vector2 } from "../../math/Vector2.js"; -import { Texture } from "../../textures/Texture.js"; -import Node from "../core/Node.js"; -import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; - -export interface MeshStandardNodeMaterialParameters extends NodeMaterialParameters, MeshStandardMaterialParameters { -} - -export default class MeshStandardNodeMaterial extends NodeMaterial { - readonly isMeshStandardNodeMaterial: true; - - emissiveNode: Node | null; - - metalnessNode: Node | null; - roughnessNode: Node | null; - - // Properties from MeshStandardMaterial - readonly isMeshStandardMaterial: true; - color: Color; - roughness: number; - metalness: number; - map: Texture | null; - lightMap: Texture | null; - lightMapIntensity: number; - aoMap: Texture | null; - aoMapIntensity: number; - emissive: Color; - emissiveIntensity: number; - emissiveMap: Texture | null; - bumpMap: Texture | null; - bumpScale: number; - normalMap: Texture | null; - normalMapType: NormalMapTypes; - normalScale: Vector2; - displacementMap: Texture | null; - displacementScale: number; - displacementBias: number; - roughnessMap: Texture | null; - metalnessMap: Texture | null; - alphaMap: Texture | null; - envMap: Texture | null; - envMapRotation: Euler; - envMapIntensity: number; - wireframe: boolean; - wireframeLinewidth: number; - wireframeLinecap: string; - wireframeLinejoin: string; - flatShading: boolean; - fog: boolean; - - constructor(paramters?: MeshStandardNodeMaterialParameters); -} diff --git a/src-testing/src/nodes/materials/MeshToonNodeMaterial.d.ts b/src-testing/src/nodes/materials/MeshToonNodeMaterial.d.ts deleted file mode 100644 index 6d54dd67e..000000000 --- a/src-testing/src/nodes/materials/MeshToonNodeMaterial.d.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { NormalMapTypes } from "../../constants.js"; -import { MeshToonMaterialParameters } from "../../materials/MeshToonMaterial.js"; -import { Color } from "../../math/Color.js"; -import { Vector2 } from "../../math/Vector2.js"; -import { Texture } from "../../textures/Texture.js"; -import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; - -export interface MeshToonNodeMaterialParameters extends NodeMaterialParameters, MeshToonMaterialParameters { -} - -export default class MeshToonNodeMaterial extends NodeMaterial { - readonly isMeshToonNodeMaterial: true; - - // Properties from MeshToonMaterial - readonly isMeshToonMaterial: true; - color: Color; - gradientMap: Texture | null; - map: Texture | null; - lightMap: Texture | null; - lightMapIntensity: number; - aoMap: Texture | null; - aoMapIntensity: number; - emissive: Color; - emissiveIntensity: number; - emissiveMap: Texture | null; - bumpMap: Texture | null; - bumpScale: number; - normalMap: Texture | null; - normalMapType: NormalMapTypes; - normalScale: Vector2; - displacementMap: Texture | null; - displacementScale: number; - displacementBias: number; - alphaMap: Texture | null; - wireframe: boolean; - wireframeLinewidth: number; - wireframeLinecap: string; - wireframeLinejoin: string; - fog: boolean; - - constructor(paramters: MeshToonNodeMaterialParameters); -} diff --git a/src-testing/src/nodes/materials/NodeMaterial.ts b/src-testing/src/nodes/materials/NodeMaterial.ts deleted file mode 100644 index ac61dfc59..000000000 --- a/src-testing/src/nodes/materials/NodeMaterial.ts +++ /dev/null @@ -1,554 +0,0 @@ -import { Material } from '../../materials/Material.js'; -import { NormalBlending } from '../../constants.js'; - -import { getNodeChildren, getCacheKey } from '../core/NodeUtils.js'; -import { attribute } from '../core/AttributeNode.js'; -import { output, diffuseColor, emissive, varyingProperty } from '../core/PropertyNode.js'; -import { - materialAlphaTest, - materialColor, - materialOpacity, - materialEmissive, - materialNormal, - materialLightMap, - materialAOMap, -} from '../accessors/MaterialNode.js'; -import { modelViewProjection } from '../accessors/ModelViewProjectionNode.js'; -import { transformedNormalView, normalLocal } from '../accessors/NormalNode.js'; -import { instance } from '../accessors/InstanceNode.js'; -import { batch } from '../accessors/BatchNode.js'; -import { materialReference } from '../accessors/MaterialReferenceNode.js'; -import { positionLocal, positionView } from '../accessors/PositionNode.js'; -import { skinningReference } from '../accessors/SkinningNode.js'; -import { morphReference } from '../accessors/MorphNode.js'; -import { lightsNode } from '../lighting/LightsNode.js'; -import { mix } from '../math/MathNode.js'; -import { float, vec3, vec4 } from '../shadernode/ShaderNode.js'; -import AONode from '../lighting/AONode.js'; -import { lightingContext } from '../lighting/LightingContextNode.js'; -import IrradianceNode from '../lighting/IrradianceNode.js'; -import { depth } from '../display/ViewportDepthNode.js'; -import { cameraLogDepth } from '../accessors/CameraNode.js'; -import { clipping, clippingAlpha } from '../accessors/ClippingNode.js'; -import { faceDirection } from '../display/FrontFacingNode.js'; - -const NodeMaterials = new Map(); - -class NodeMaterial extends Material { - constructor() { - super(); - - this.isNodeMaterial = true; - - this.type = this.constructor.type; - - this.forceSinglePass = false; - - this.fog = true; - this.lights = false; - this.normals = true; - - this.lightsNode = null; - this.envNode = null; - this.aoNode = null; - - this.colorNode = null; - this.normalNode = null; - this.opacityNode = null; - this.backdropNode = null; - this.backdropAlphaNode = null; - this.alphaTestNode = null; - - this.positionNode = null; - - this.depthNode = null; - this.shadowNode = null; - this.shadowPositionNode = null; - - this.outputNode = null; - this.mrtNode = null; - - this.fragmentNode = null; - this.vertexNode = null; - } - - customProgramCacheKey() { - return this.type + getCacheKey(this); - } - - build(builder) { - this.setup(builder); - } - - setup(builder) { - // < VERTEX STAGE > - - builder.addStack(); - - builder.stack.outputNode = this.vertexNode || this.setupPosition(builder); - - builder.addFlow('vertex', builder.removeStack()); - - // < FRAGMENT STAGE > - - builder.addStack(); - - let resultNode; - - const clippingNode = this.setupClipping(builder); - - if (this.depthWrite === true) this.setupDepth(builder); - - if (this.fragmentNode === null) { - if (this.normals === true) this.setupNormal(builder); - - this.setupDiffuseColor(builder); - this.setupVariants(builder); - - const outgoingLightNode = this.setupLighting(builder); - - if (clippingNode !== null) builder.stack.add(clippingNode); - - // force unsigned floats - useful for RenderTargets - - const basicOutput = vec4(outgoingLightNode, diffuseColor.a).max(0); - - resultNode = this.setupOutput(builder, basicOutput); - - // OUTPUT NODE - - output.assign(resultNode); - - // - - if (this.outputNode !== null) resultNode = this.outputNode; - - // MRT - - const renderTarget = builder.renderer.getRenderTarget(); - - if (renderTarget !== null) { - const mrt = builder.renderer.getMRT(); - const materialMRT = this.mrtNode; - - if (mrt !== null) { - resultNode = mrt; - - if (materialMRT !== null) { - resultNode = mrt.merge(materialMRT); - } - } else if (materialMRT !== null) { - resultNode = materialMRT; - } - } - } else { - let fragmentNode = this.fragmentNode; - - if (fragmentNode.isOutputStructNode !== true) { - fragmentNode = vec4(fragmentNode); - } - - resultNode = this.setupOutput(builder, fragmentNode); - } - - builder.stack.outputNode = resultNode; - - builder.addFlow('fragment', builder.removeStack()); - } - - setupClipping(builder) { - if (builder.clippingContext === null) return null; - - const { globalClippingCount, localClippingCount } = builder.clippingContext; - - let result = null; - - if (globalClippingCount || localClippingCount) { - if (this.alphaToCoverage) { - // to be added to flow when the color/alpha value has been determined - result = clippingAlpha(); - } else { - builder.stack.add(clipping()); - } - } - - return result; - } - - setupDepth(builder) { - const { renderer } = builder; - - // Depth - - let depthNode = this.depthNode; - - if (depthNode === null && renderer.logarithmicDepthBuffer === true) { - const fragDepth = modelViewProjection().w.add(1); - - depthNode = fragDepth.log2().mul(cameraLogDepth).mul(0.5); - } - - if (depthNode !== null) { - depth.assign(depthNode).append(); - } - } - - setupPosition(builder) { - const { object } = builder; - const geometry = object.geometry; - - builder.addStack(); - - // Vertex - - if (geometry.morphAttributes.position || geometry.morphAttributes.normal || geometry.morphAttributes.color) { - morphReference(object).append(); - } - - if (object.isSkinnedMesh === true) { - skinningReference(object).append(); - } - - if (this.displacementMap) { - const displacementMap = materialReference('displacementMap', 'texture'); - const displacementScale = materialReference('displacementScale', 'float'); - const displacementBias = materialReference('displacementBias', 'float'); - - positionLocal.addAssign( - normalLocal.normalize().mul(displacementMap.x.mul(displacementScale).add(displacementBias)), - ); - } - - if (object.isBatchedMesh) { - batch(object).append(); - } - - if (object.instanceMatrix && object.instanceMatrix.isInstancedBufferAttribute === true) { - instance(object).append(); - } - - if (this.positionNode !== null) { - positionLocal.assign(this.positionNode); - } - - const mvp = modelViewProjection(); - - builder.context.vertex = builder.removeStack(); - builder.context.mvp = mvp; - - return mvp; - } - - setupDiffuseColor({ object, geometry }) { - let colorNode = this.colorNode ? vec4(this.colorNode) : materialColor; - - // VERTEX COLORS - - if (this.vertexColors === true && geometry.hasAttribute('color')) { - colorNode = vec4(colorNode.xyz.mul(attribute('color', 'vec3')), colorNode.a); - } - - // Instanced colors - - if (object.instanceColor) { - const instanceColor = varyingProperty('vec3', 'vInstanceColor'); - - colorNode = instanceColor.mul(colorNode); - } - - // COLOR - - diffuseColor.assign(colorNode); - - // OPACITY - - const opacityNode = this.opacityNode ? float(this.opacityNode) : materialOpacity; - diffuseColor.a.assign(diffuseColor.a.mul(opacityNode)); - - // ALPHA TEST - - if (this.alphaTestNode !== null || this.alphaTest > 0) { - const alphaTestNode = this.alphaTestNode !== null ? float(this.alphaTestNode) : materialAlphaTest; - - diffuseColor.a.lessThanEqual(alphaTestNode).discard(); - } - - if (this.transparent === false && this.blending === NormalBlending && this.alphaToCoverage === false) { - diffuseColor.a.assign(1.0); - } - } - - setupVariants(/*builder*/) { - // Interface function. - } - - setupOutgoingLight() { - return this.lights === true ? vec3(0) : diffuseColor.rgb; - } - - setupNormal() { - // NORMAL VIEW - - if (this.flatShading === true) { - const normalNode = positionView.dFdx().cross(positionView.dFdy()).normalize(); - - transformedNormalView.assign(normalNode.mul(faceDirection)); - } else { - const normalNode = this.normalNode ? vec3(this.normalNode) : materialNormal; - - transformedNormalView.assign(normalNode.mul(faceDirection)); - } - } - - setupEnvironment(/*builder*/) { - let node = null; - - if (this.envNode) { - node = this.envNode; - } else if (this.envMap) { - node = this.envMap.isCubeTexture - ? materialReference('envMap', 'cubeTexture') - : materialReference('envMap', 'texture'); - } - - return node; - } - - setupLightMap(builder) { - let node = null; - - if (builder.material.lightMap) { - node = new IrradianceNode(materialLightMap); - } - - return node; - } - - setupLights(builder) { - const materialLightsNode = []; - - // - - const envNode = this.setupEnvironment(builder); - - if (envNode && envNode.isLightingNode) { - materialLightsNode.push(envNode); - } - - const lightMapNode = this.setupLightMap(builder); - - if (lightMapNode && lightMapNode.isLightingNode) { - materialLightsNode.push(lightMapNode); - } - - if (this.aoNode !== null || builder.material.aoMap) { - const aoNode = this.aoNode !== null ? this.aoNode : materialAOMap; - - materialLightsNode.push(new AONode(aoNode)); - } - - let lightsN = this.lightsNode || builder.lightsNode; - - if (materialLightsNode.length > 0) { - lightsN = lightsNode([...lightsN.lightNodes, ...materialLightsNode]); - } - - return lightsN; - } - - setupLightingModel(/*builder*/) { - // Interface function. - } - - setupLighting(builder) { - const { material } = builder; - const { backdropNode, backdropAlphaNode, emissiveNode } = this; - - // OUTGOING LIGHT - - const lights = this.lights === true || this.lightsNode !== null; - - const lightsNode = lights ? this.setupLights(builder) : null; - - let outgoingLightNode = this.setupOutgoingLight(builder); - - if (lightsNode && lightsNode.hasLight !== false) { - const lightingModel = this.setupLightingModel(builder); - - outgoingLightNode = lightingContext(lightsNode, lightingModel, backdropNode, backdropAlphaNode); - } else if (backdropNode !== null) { - outgoingLightNode = vec3( - backdropAlphaNode !== null ? mix(outgoingLightNode, backdropNode, backdropAlphaNode) : backdropNode, - ); - } - - // EMISSIVE - - if ( - (emissiveNode && emissiveNode.isNode === true) || - (material.emissive && material.emissive.isColor === true) - ) { - emissive.assign(vec3(emissiveNode ? emissiveNode : materialEmissive)); - - outgoingLightNode = outgoingLightNode.add(emissive); - } - - return outgoingLightNode; - } - - setupOutput(builder, outputNode) { - // FOG - - if (this.fog === true) { - const fogNode = builder.fogNode; - - if (fogNode) outputNode = vec4(fogNode.mix(outputNode.rgb, fogNode.colorNode), outputNode.a); - } - - return outputNode; - } - - setDefaultValues(material) { - // This approach is to reuse the native refreshUniforms* - // and turn available the use of features like transmission and environment in core - - for (const property in material) { - const value = material[property]; - - if (this[property] === undefined) { - this[property] = value; - - if (value && value.clone) this[property] = value.clone(); - } - } - - const descriptors = Object.getOwnPropertyDescriptors(material.constructor.prototype); - - for (const key in descriptors) { - if ( - Object.getOwnPropertyDescriptor(this.constructor.prototype, key) === undefined && - descriptors[key].get !== undefined - ) { - Object.defineProperty(this.constructor.prototype, key, descriptors[key]); - } - } - } - - toJSON(meta) { - const isRoot = meta === undefined || typeof meta === 'string'; - - if (isRoot) { - meta = { - textures: {}, - images: {}, - nodes: {}, - }; - } - - const data = Material.prototype.toJSON.call(this, meta); - const nodeChildren = getNodeChildren(this); - - data.inputNodes = {}; - - for (const { property, childNode } of nodeChildren) { - data.inputNodes[property] = childNode.toJSON(meta).uuid; - } - - // TODO: Copied from Object3D.toJSON - - function extractFromCache(cache) { - const values = []; - - for (const key in cache) { - const data = cache[key]; - delete data.metadata; - values.push(data); - } - - return values; - } - - if (isRoot) { - const textures = extractFromCache(meta.textures); - const images = extractFromCache(meta.images); - const nodes = extractFromCache(meta.nodes); - - if (textures.length > 0) data.textures = textures; - if (images.length > 0) data.images = images; - if (nodes.length > 0) data.nodes = nodes; - } - - return data; - } - - copy(source) { - this.lightsNode = source.lightsNode; - this.envNode = source.envNode; - - this.colorNode = source.colorNode; - this.normalNode = source.normalNode; - this.opacityNode = source.opacityNode; - this.backdropNode = source.backdropNode; - this.backdropAlphaNode = source.backdropAlphaNode; - this.alphaTestNode = source.alphaTestNode; - - this.positionNode = source.positionNode; - - this.depthNode = source.depthNode; - this.shadowNode = source.shadowNode; - this.shadowPositionNode = source.shadowPositionNode; - - this.outputNode = source.outputNode; - this.mrtNode = source.mrtNode; - - this.fragmentNode = source.fragmentNode; - this.vertexNode = source.vertexNode; - - return super.copy(source); - } - - static fromMaterial(material) { - if (material.isNodeMaterial === true) { - // is already a node material - - return material; - } - - const type = material.type.replace('Material', 'NodeMaterial'); - - const nodeMaterial = createNodeMaterialFromType(type); - - if (nodeMaterial === undefined) { - throw new Error(`NodeMaterial: Material "${material.type}" is not compatible.`); - } - - for (const key in material) { - nodeMaterial[key] = material[key]; - } - - return nodeMaterial; - } -} - -export default NodeMaterial; - -export function addNodeMaterial(type, nodeMaterial) { - if (typeof nodeMaterial !== 'function' || !type) throw new Error(`Node material ${type} is not a class`); - if (NodeMaterials.has(type)) { - console.warn(`Redefinition of node material ${type}`); - return; - } - - NodeMaterials.set(type, nodeMaterial); - nodeMaterial.type = type; -} - -export function createNodeMaterialFromType(type) { - const Material = NodeMaterials.get(type); - - if (Material !== undefined) { - return new Material(); - } -} - -addNodeMaterial('NodeMaterial', NodeMaterial); diff --git a/src-testing/src/nodes/materials/PointsNodeMaterial.d.ts b/src-testing/src/nodes/materials/PointsNodeMaterial.d.ts deleted file mode 100644 index 3132aa3ca..000000000 --- a/src-testing/src/nodes/materials/PointsNodeMaterial.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { PointsMaterialParameters } from "../../materials/PointsMaterial.js"; -import { Color } from "../../math/Color.js"; -import { Texture } from "../../textures/Texture.js"; -import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; - -export interface PointsNodeMaterialParameters extends NodeMaterialParameters, PointsMaterialParameters { -} - -export default class PointsNodeMaterial extends NodeMaterial { - readonly isPointsNodeMaterial: true; - - // Properties from PointsMaterial - readonly isPointsMaterial: true; - color: Color; - map: Texture | null; - alphaMap: Texture | null; - size: number; - sizeAttenuation: boolean; - fog: boolean; - - constructor(parameters?: PointsNodeMaterialParameters); -} diff --git a/src-testing/src/nodes/materials/ShadowNodeMaterial.d.ts b/src-testing/src/nodes/materials/ShadowNodeMaterial.d.ts deleted file mode 100644 index 716433eff..000000000 --- a/src-testing/src/nodes/materials/ShadowNodeMaterial.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { ShadowMaterialParameters } from "../../materials/ShadowMaterial.js"; -import { Color } from "../../math/Color.js"; -import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; - -export interface ShadowNodeMaterialParameters extends NodeMaterialParameters, ShadowMaterialParameters { -} - -export default class ShadowNodeMaterial extends NodeMaterial { - readonly isShadowNodeMaterial: true; - - // Properties from ShadowMaterial - readonly isShadowMaterial: true; - color: Color; - fog: boolean; - - constructor(parameters?: ShadowNodeMaterialParameters); -} diff --git a/src-testing/src/nodes/materials/SpriteNodeMaterial.d.ts b/src-testing/src/nodes/materials/SpriteNodeMaterial.d.ts deleted file mode 100644 index c4c567c31..000000000 --- a/src-testing/src/nodes/materials/SpriteNodeMaterial.d.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { SpriteMaterialParameters } from "../../materials/SpriteMaterial.js"; -import { Color } from "../../math/Color.js"; -import { Texture } from "../../textures/Texture.js"; -import Node from "../core/Node.js"; -import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; - -export interface SpriteNodeMaterialParameters extends NodeMaterialParameters, SpriteMaterialParameters { -} - -export default class SpriteNodeMaterial extends NodeMaterial { - isSpriteNodeMaterial: true; - - rotationNode: Node | null; - scaleNode: Node | null; - - // Properties from SpriteMaterial - readonly isSpriteMaterial: true; - color: Color; - map: Texture | null; - alphaMap: Texture | null; - rotation: number; - sizeAttenuation: boolean; - fog: boolean; - - constructor(parameters?: SpriteNodeMaterialParameters); -} diff --git a/src-testing/src/nodes/materials/VolumeNodeMaterial.d.ts b/src-testing/src/nodes/materials/VolumeNodeMaterial.d.ts deleted file mode 100644 index d1f4b9cc0..000000000 --- a/src-testing/src/nodes/materials/VolumeNodeMaterial.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import Node from "../core/Node.js"; -import NodeMaterial, { NodeMaterialParameters } from "./NodeMaterial.js"; - -export default class VolumeNodeMaterial extends NodeMaterial { - lights: boolean; - readonly isVolumeNodeMaterial: true; - testNode: Node | null; - - constructor(parameters?: NodeMaterialParameters); -} diff --git a/src-testing/src/nodes/materialx/MaterialXNodes.d.ts b/src-testing/src/nodes/materialx/MaterialXNodes.d.ts deleted file mode 100644 index cc0383590..000000000 --- a/src-testing/src/nodes/materialx/MaterialXNodes.d.ts +++ /dev/null @@ -1,107 +0,0 @@ -import Node from "../core/Node.js"; -import MathNode from "../math/MathNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import { mx_hsvtorgb, mx_rgbtohsv } from "./lib/mx_hsv.js"; -import { mx_srgb_texture_to_lin_rec709 } from "./lib/mx_transform_color.js"; - -export function mx_aastep(threshold: NodeRepresentation, value: NodeRepresentation): ShaderNodeObject; - -export function mx_ramplr( - valuel: NodeRepresentation, - valuer: NodeRepresentation, - texcoord?: NodeRepresentation, -): ShaderNodeObject; -export function mx_ramptb( - valuet: NodeRepresentation, - valueb: NodeRepresentation, - texcoord?: NodeRepresentation, -): ShaderNodeObject; - -export function mx_splitlr( - valuel: NodeRepresentation, - valuer: NodeRepresentation, - center: NodeRepresentation, - texcoord?: NodeRepresentation, -): ShaderNodeObject; -export function mx_splittb( - valuet: NodeRepresentation, - valueb: NodeRepresentation, - center: NodeRepresentation, - texcoord?: NodeRepresentation, -): ShaderNodeObject; - -export function mx_transform_uv( - uv_scale?: NodeRepresentation, - uv_offset?: NodeRepresentation, - uv_geo?: NodeRepresentation, -): ShaderNodeObject; - -export function mx_safepower(in1: NodeRepresentation, in2?: NodeRepresentation): ShaderNodeObject; - -export function mx_contrast( - input: NodeRepresentation, - amount?: NodeRepresentation, - pivot?: NodeRepresentation, -): ShaderNodeObject; - -export function mx_noise_float( - texcoord?: NodeRepresentation, - amplitude?: NodeRepresentation, - pivot?: NodeRepresentation, -): ShaderNodeObject; -export function mx_noise_vec3( - texcoord?: NodeRepresentation, - amplitude?: NodeRepresentation, - pivot?: NodeRepresentation, -): ShaderNodeObject; -export function mx_noise_vec4( - texcoord?: NodeRepresentation, - amplitude?: NodeRepresentation, - pivot?: NodeRepresentation, -): ShaderNodeObject; - -export function mx_worley_noise_float( - texcoord?: NodeRepresentation, - jitter?: NodeRepresentation, -): ShaderNodeObject; -export function mx_worley_noise_vec2( - texcoord?: NodeRepresentation, - jitter?: NodeRepresentation, -): ShaderNodeObject; -export function mx_worley_noise_vec3( - texcoord?: NodeRepresentation, - jitter?: NodeRepresentation, -): ShaderNodeObject; - -export function mx_cell_noise_float(texcoord?: NodeRepresentation): ShaderNodeObject; - -export function mx_fractal_noise_float( - position?: NodeRepresentation, - octaves?: NodeRepresentation, - lacunarity?: NodeRepresentation, - diminish?: NodeRepresentation, - amplitude?: NodeRepresentation, -): ShaderNodeObject; -export function mx_fractal_noise_vec2( - position?: NodeRepresentation, - octaves?: NodeRepresentation, - lacunarity?: NodeRepresentation, - diminish?: NodeRepresentation, - amplitude?: NodeRepresentation, -): ShaderNodeObject; -export function mx_fractal_noise_vec3( - position?: NodeRepresentation, - octaves?: NodeRepresentation, - lacunarity?: NodeRepresentation, - diminish?: NodeRepresentation, - amplitude?: NodeRepresentation, -): ShaderNodeObject; -export function mx_fractal_noise_vec4( - position?: NodeRepresentation, - octaves?: NodeRepresentation, - lacunarity?: NodeRepresentation, - diminish?: NodeRepresentation, - amplitude?: NodeRepresentation, -): ShaderNodeObject; - -export { mx_hsvtorgb, mx_rgbtohsv, mx_srgb_texture_to_lin_rec709 }; diff --git a/src-testing/src/nodes/materialx/lib/mx_hsv.d.ts b/src-testing/src/nodes/materialx/lib/mx_hsv.d.ts deleted file mode 100644 index a826bdea2..000000000 --- a/src-testing/src/nodes/materialx/lib/mx_hsv.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -import Node from "../../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../../shadernode/ShaderNode.js"; - -export const mx_hsvtorgb: (hsv_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_rgbtohsv: (c_immutable: NodeRepresentation) => ShaderNodeObject; diff --git a/src-testing/src/nodes/materialx/lib/mx_noise.d.ts b/src-testing/src/nodes/materialx/lib/mx_noise.d.ts deleted file mode 100644 index 982615616..000000000 --- a/src-testing/src/nodes/materialx/lib/mx_noise.d.ts +++ /dev/null @@ -1,359 +0,0 @@ -import Node from "../../core/Node.js"; -import VarNode from "../../core/VarNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../../shadernode/ShaderNode.js"; - -export const mx_select: ( - b_immutable: NodeRepresentation, - t_immutable: NodeRepresentation, - f_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_negate_if: ( - val_immutable: NodeRepresentation, - b_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_floor: (x_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_floorfrac: (x_immutable: NodeRepresentation, i: ShaderNodeObject) => ShaderNodeObject; - -export const mx_bilerp_0: ( - v0_immutable: NodeRepresentation, - v1_immutable: NodeRepresentation, - v2_immutable: NodeRepresentation, - v3_immutable: NodeRepresentation, - s_immutable: NodeRepresentation, - t_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_bilerp_1: ( - v0_immutable: NodeRepresentation, - v1_immutable: NodeRepresentation, - v2_immutable: NodeRepresentation, - v3_immutable: NodeRepresentation, - s_immutable: NodeRepresentation, - t_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_bilerp: ( - v0_immutable: NodeRepresentation, - v1_immutable: NodeRepresentation, - v2_immutable: NodeRepresentation, - v3_immutable: NodeRepresentation, - s_immutable: NodeRepresentation, - t_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_trilerp_0: ( - v0_immutable: NodeRepresentation, - v1_immutable: NodeRepresentation, - v2_immutable: NodeRepresentation, - v3_immutable: NodeRepresentation, - v4_immutable: NodeRepresentation, - v5_immutable: NodeRepresentation, - v6_immutable: NodeRepresentation, - v7_immutable: NodeRepresentation, - s_immutable: NodeRepresentation, - t_immutable: NodeRepresentation, - r_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_trilerp_1: ( - v0_immutable: NodeRepresentation, - v1_immutable: NodeRepresentation, - v2_immutable: NodeRepresentation, - v3_immutable: NodeRepresentation, - v4_immutable: NodeRepresentation, - v5_immutable: NodeRepresentation, - v6_immutable: NodeRepresentation, - v7_immutable: NodeRepresentation, - s_immutable: NodeRepresentation, - t_immutable: NodeRepresentation, - r_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_trilerp: ( - v0_immutable: NodeRepresentation, - v1_immutable: NodeRepresentation, - v2_immutable: NodeRepresentation, - v3_immutable: NodeRepresentation, - v4_immutable: NodeRepresentation, - v5_immutable: NodeRepresentation, - v6_immutable: NodeRepresentation, - v7_immutable: NodeRepresentation, - s_immutable: NodeRepresentation, - t_immutable: NodeRepresentation, - r_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_gradient_float_0: ( - hash_immutable: NodeRepresentation, - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_gradient_float_1: ( - hash_immutable: NodeRepresentation, - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, - z_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_gradient_float: ( - hash_immutable: NodeRepresentation, - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, - z_immutable?: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_gradient_vec3_0: ( - hash_immutable: NodeRepresentation, - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_gradient_vec3_1: ( - hash_immutable: NodeRepresentation, - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, - z_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_gradient_vec3: ( - hash_immutable: NodeRepresentation, - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, - z_immutable?: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_gradient_scale2d_0: (v_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_gradient_scale3d_0: (v_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_gradient_scale2d_1: (v_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_gradient_scale2d: (v_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_gradient_scale3d_1: (v_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_gradient_scale3d: (v_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_rotl32: (x_immutable: NodeRepresentation, k_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_bjmix: ( - a: ShaderNodeObject, - b: ShaderNodeObject, - c: ShaderNodeObject, -) => ShaderNodeObject; - -export const mx_bjfinal: ( - a_immutable: NodeRepresentation, - b_immutable: NodeRepresentation, - c_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_bits_to_01: (bits_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_fade: (t_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_hash_int_0: (x_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_hash_int_1: ( - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_hash_int_2: ( - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, - z_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_hash_int_3: ( - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, - z_immutable: NodeRepresentation, - xx_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_hash_int_4: ( - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, - z_immutable: NodeRepresentation, - xx_immutable: NodeRepresentation, - yy_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_hash_int: ( - x_immutable: NodeRepresentation, - y_immutable?: NodeRepresentation, - z_immutable?: NodeRepresentation, - xx_immutable?: NodeRepresentation, - yy_immutable?: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_hash_vec3_0: ( - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_hash_vec3_1: ( - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, - z_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_hash_vec3: ( - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, - z_immutable?: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_perlin_noise_float_0: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_perlin_noise_float_1: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_perlin_noise_float: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_perlin_noise_vec3_0: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_perlin_noise_vec3_1: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_perlin_noise_vec3: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_cell_noise_float_0: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_cell_noise_float_1: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_cell_noise_float_2: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_cell_noise_float_3: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_cell_noise_float: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_cell_noise_vec3_0: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_cell_noise_vec3_1: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_cell_noise_vec3_2: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_cell_noise_vec3_3: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_cell_noise_vec3: (p_immutable: NodeRepresentation) => ShaderNodeObject; - -export const mx_fractal_noise_float: ( - p_immutable: NodeRepresentation, - octaves_immutable: NodeRepresentation, - lacunarity_immutable: NodeRepresentation, - diminish_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_fractal_noise_vec3: ( - p_immutable: NodeRepresentation, - octaves_immutable: NodeRepresentation, - lacunarity_immutable: NodeRepresentation, - diminish_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_fractal_noise_vec2: ( - p_immutable: NodeRepresentation, - octaves_immutable: NodeRepresentation, - lacunarity_immutable: NodeRepresentation, - diminish_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_fractal_noise_vec4: ( - p_immutable: NodeRepresentation, - octaves_immutable: NodeRepresentation, - lacunarity_immutable: NodeRepresentation, - diminish_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_worley_distance_0: ( - p_immutable: NodeRepresentation, - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, - z_immutable: NodeRepresentation, - xoff_immutable: NodeRepresentation, - yoff_immutable: NodeRepresentation, - jitter_immutable: NodeRepresentation, - metric_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_worley_distance_1: ( - p_immutable: NodeRepresentation, - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, - z_immutable: NodeRepresentation, - xoff_immutable: NodeRepresentation, - yoff_immutable: NodeRepresentation, - zoff_immutable: NodeRepresentation, - jitter_immutable: NodeRepresentation, - metric_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_worley_distance: ( - p_immutable: NodeRepresentation, - x_immutable: NodeRepresentation, - y_immutable: NodeRepresentation, - z_immutable: NodeRepresentation, - xoff_immutable: NodeRepresentation, - yoff_immutable: NodeRepresentation, - zoff_immutable: NodeRepresentation, - jitter_immutable: NodeRepresentation, - metric_immutable?: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_worley_noise_float_0: ( - p_immutable: NodeRepresentation, - jitter_immutable: NodeRepresentation, - metric_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_worley_noise_vec2_0: ( - p_immutable: NodeRepresentation, - jitter_immutable: NodeRepresentation, - metric_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_worley_noise_vec3_0: ( - p_immutable: NodeRepresentation, - jitter_immutable: NodeRepresentation, - metric_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_worley_noise_float_1: ( - p_immutable: NodeRepresentation, - jitter_immutable: NodeRepresentation, - metric_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_worley_noise_float: ( - p_immutable: NodeRepresentation, - jitter_immutable: NodeRepresentation, - metric_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_worley_noise_vec2_1: ( - p_immutable: NodeRepresentation, - jitter_immutable: NodeRepresentation, - metric_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_worley_noise_vec2: ( - p_immutable: NodeRepresentation, - jitter_immutable: NodeRepresentation, - metric_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_worley_noise_vec3_1: ( - p_immutable: NodeRepresentation, - jitter_immutable: NodeRepresentation, - metric_immutable: NodeRepresentation, -) => ShaderNodeObject; - -export const mx_worley_noise_vec3: ( - p_immutable: NodeRepresentation, - jitter_immutable: NodeRepresentation, - metric_immutable: NodeRepresentation, -) => ShaderNodeObject; diff --git a/src-testing/src/nodes/materialx/lib/mx_transform_color.d.ts b/src-testing/src/nodes/materialx/lib/mx_transform_color.d.ts deleted file mode 100644 index 7cdeb5747..000000000 --- a/src-testing/src/nodes/materialx/lib/mx_transform_color.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -import Node from "../../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../../shadernode/ShaderNode.js"; - -export const mx_srgb_texture_to_lin_rec709: (color_immutable: NodeRepresentation) => ShaderNodeObject; diff --git a/src-testing/src/nodes/math/CondNode.d.ts b/src-testing/src/nodes/math/CondNode.d.ts deleted file mode 100644 index 973a57af2..000000000 --- a/src-testing/src/nodes/math/CondNode.d.ts +++ /dev/null @@ -1,37 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class CondNode extends Node { - condNode: Node; - ifNode: Node; - elseNode: Node; - - constructor(condNode: Node, ifNode: Node, elseNode: Node); -} - -export const select: ( - condNode: NodeRepresentation, - ifNode: NodeRepresentation, - elseNode: NodeRepresentation, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - select: typeof select; - } -} - -/** - * @deprecated cond() has been renamed to select() - */ -export const cond: ( - condNode: NodeRepresentation, - ifNode: NodeRepresentation, - elseNode: NodeRepresentation, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - cond: typeof cond; - } -} diff --git a/src-testing/src/nodes/math/HashNode.d.ts b/src-testing/src/nodes/math/HashNode.d.ts deleted file mode 100644 index 8e89ac588..000000000 --- a/src-testing/src/nodes/math/HashNode.d.ts +++ /dev/null @@ -1,16 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class HashNode extends Node { - seedNode: Node; - - constructor(seedNode: Node); -} - -export const hash: (seedNode: NodeRepresentation) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - hash: typeof hash; - } -} diff --git a/src-testing/src/nodes/math/MathNode.d.ts b/src-testing/src/nodes/math/MathNode.d.ts deleted file mode 100644 index 6a5834cba..000000000 --- a/src-testing/src/nodes/math/MathNode.d.ts +++ /dev/null @@ -1,273 +0,0 @@ -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import OperatorNode from "./OperatorNode.js"; - -export type MathNodeMethod1 = - | typeof MathNode.RADIANS - | typeof MathNode.DEGREES - | typeof MathNode.EXP - | typeof MathNode.EXP2 - | typeof MathNode.LOG - | typeof MathNode.LOG2 - | typeof MathNode.SQRT - | typeof MathNode.INVERSE_SQRT - | typeof MathNode.FLOOR - | typeof MathNode.CEIL - | typeof MathNode.NORMALIZE - | typeof MathNode.FRACT - | typeof MathNode.SIN - | typeof MathNode.COS - | typeof MathNode.TAN - | typeof MathNode.ASIN - | typeof MathNode.ACOS - | typeof MathNode.ATAN - | typeof MathNode.ABS - | typeof MathNode.SIGN - | typeof MathNode.LENGTH - | typeof MathNode.NEGATE - | typeof MathNode.ONE_MINUS - | typeof MathNode.DFDX - | typeof MathNode.DFDY - | typeof MathNode.ROUND - | typeof MathNode.RECIPROCAL - | typeof MathNode.TRUNC - | typeof MathNode.FWIDTH - | typeof MathNode.BITCAST - | typeof MathNode.TRANSPOSE; - -export type MathNodeMethod2 = - | typeof MathNode.ATAN2 - | typeof MathNode.MIN - | typeof MathNode.MAX - | typeof MathNode.MOD - | typeof MathNode.STEP - | typeof MathNode.REFLECT - | typeof MathNode.DISTANCE - | typeof MathNode.DOT - | typeof MathNode.CROSS - | typeof MathNode.POW - | typeof MathNode.TRANSFORM_DIRECTION; - -export type MathNodeMethod3 = - | typeof MathNode.MIX - | typeof MathNode.CLAMP - | typeof MathNode.REFRACT - | typeof MathNode.SMOOTHSTEP - | typeof MathNode.FACEFORWARD; - -export type MathNodeMethod = MathNodeMethod1 | MathNodeMethod2 | MathNodeMethod3; - -export default class MathNode extends TempNode { - // 1 input - - static ALL: "all"; - static ANY: "any"; - static EQUALS: "equals"; - - static RADIANS: "radians"; - static DEGREES: "degrees"; - static EXP: "exp"; - static EXP2: "exp2"; - static LOG: "log"; - static LOG2: "log2"; - static SQRT: "sqrt"; - static INVERSE_SQRT: "inversesqrt"; - static FLOOR: "floor"; - static CEIL: "ceil"; - static NORMALIZE: "normalize"; - static FRACT: "fract"; - static SIN: "sin"; - static COS: "cos"; - static TAN: "tan"; - static ASIN: "asin"; - static ACOS: "acos"; - static ATAN: "atan"; - static ABS: "abs"; - static SIGN: "sign"; - static LENGTH: "length"; - static NEGATE: "negate"; - static ONE_MINUS: "oneMinus"; - static DFDX: "dFdx"; - static DFDY: "dFdy"; - static ROUND: "round"; - static RECIPROCAL: "reciprocal"; - static TRUNC: "trunc"; - static FWIDTH: "fwidth"; - static BITCAST: "bitcast"; - static TRANSPOSE: "transpose"; - - // 2 inputs - - static ATAN2: "atan2"; - static MIN: "min"; - static MAX: "max"; - static MOD: "mod"; - static STEP: "step"; - static REFLECT: "reflect"; - static DISTANCE: "distance"; - static DOT: "dot"; - static CROSS: "cross"; - static POW: "pow"; - static TRANSFORM_DIRECTION: "transformDirection"; - - // 3 inputs - - static MIX: "mix"; - static CLAMP: "clamp"; - static REFRACT: "refract"; - static SMOOTHSTEP: "smoothstep"; - static FACEFORWARD: "faceforward"; - - method: MathNodeMethod; - aNode: Node; - bNode: Node | null; - cNode: Node | null; - - constructor(method: MathNodeMethod1, aNode: Node); - constructor(method: MathNodeMethod2, aNode: Node, bNode: Node); - constructor(method: MathNodeMethod3, aNode: Node, bNode: Node, cNode: Node); -} - -export const EPSILON: ShaderNodeObject; -export const INFINITY: ShaderNodeObject; -export const PI: ShaderNodeObject; -export const PI2: ShaderNodeObject; - -type Unary = (a: NodeRepresentation) => ShaderNodeObject; - -export const all: Unary; -export const any: Unary; -export const equals: Unary; - -export const radians: Unary; -export const degrees: Unary; -export const exp: Unary; -export const exp2: Unary; -export const log: Unary; -export const log2: Unary; -export const sqrt: Unary; -export const inverseSqrt: Unary; -export const floor: Unary; -export const ceil: Unary; -export const normalize: Unary; -export const fract: Unary; -export const sin: Unary; -export const cos: Unary; -export const tan: Unary; -export const asin: Unary; -export const acos: Unary; -export const atan: Unary; -export const abs: Unary; -export const sign: Unary; -export const length: Unary; -export const negate: Unary; -export const oneMinus: Unary; -export const dFdx: Unary; -export const dFdy: Unary; -export const round: Unary; -export const reciprocal: Unary; -export const trunc: Unary; -export const fwidth: Unary; -export const bitcast: Unary; -export const transpose: Unary; - -type Binary = (a: NodeRepresentation, b: NodeRepresentation) => ShaderNodeObject; - -export const atan2: Binary; -export const min: Binary; -export const max: Binary; -export const mod: Binary; -export const step: Binary; -export const reflect: Binary; -export const distance: Binary; -export const difference: Binary; -export const dot: Binary; -export const cross: Binary; -export const pow: Binary; -export const pow2: Binary; -export const pow3: Binary; -export const pow4: Binary; -export const transformDirection: Binary; - -type Ternary = (a: NodeRepresentation, b: NodeRepresentation, c: NodeRepresentation) => ShaderNodeObject; - -export const cbrt: Unary; -export const lengthSq: Unary; -export const mix: Ternary; -export const clamp: ( - a: NodeRepresentation, - b?: NodeRepresentation, - c?: NodeRepresentation, -) => ShaderNodeObject; -export const saturate: Unary; -export const refract: Ternary; -export const smoothstep: Ternary; -export const faceForward: Ternary; - -export const rand: (uv: NodeRepresentation) => ShaderNodeObject; - -export const mixElement: Ternary; -export const smoothstepElement: Ternary; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - all: typeof all; - any: typeof any; - equals: typeof equals; - radians: typeof radians; - degrees: typeof degrees; - exp: typeof exp; - exp2: typeof exp2; - log: typeof log; - log2: typeof log2; - sqrt: typeof sqrt; - inverseSqrt: typeof inverseSqrt; - floor: typeof floor; - ceil: typeof ceil; - normalize: typeof normalize; - fract: typeof fract; - sin: typeof sin; - cos: typeof cos; - tan: typeof tan; - asin: typeof asin; - acos: typeof acos; - atan: typeof atan; - abs: typeof abs; - sign: typeof sign; - length: typeof length; - lengthSq: typeof lengthSq; - negate: typeof negate; - oneMinus: typeof oneMinus; - dFdx: typeof dFdx; - dFdy: typeof dFdy; - round: typeof round; - reciprocal: typeof reciprocal; - trunc: typeof trunc; - fwidth: typeof fwidth; - atan2: typeof atan2; - min: typeof min; - max: typeof max; - mod: typeof mod; - step: typeof step; - reflect: typeof reflect; - distance: typeof distance; - dot: typeof dot; - cross: typeof cross; - pow: typeof pow; - pow2: typeof pow2; - pow3: typeof pow3; - pow4: typeof pow4; - transformDirection: typeof transformDirection; - mix: typeof mixElement; - clamp: typeof clamp; - refract: typeof refract; - smoothstep: typeof smoothstepElement; - faceForward: typeof faceForward; - difference: typeof difference; - saturate: typeof saturate; - cbrt: typeof cbrt; - transpose: typeof transpose; - rand: typeof rand; - } -} diff --git a/src-testing/src/nodes/math/MathUtils.d.ts b/src-testing/src/nodes/math/MathUtils.d.ts deleted file mode 100644 index d2591618b..000000000 --- a/src-testing/src/nodes/math/MathUtils.d.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { Binary, Ternary } from "./MathNode.js"; - -// remapping functions -export const parabola: Binary; -export const gain: Binary; -export const pcurve: Ternary; -export const sinc: Binary; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - parabola: typeof parabola; - gain: typeof gain; - pcurve: typeof pcurve; - sinc: typeof sinc; - } -} diff --git a/src-testing/src/nodes/math/OperatorNode.d.ts b/src-testing/src/nodes/math/OperatorNode.d.ts deleted file mode 100644 index 7e89ed0f9..000000000 --- a/src-testing/src/nodes/math/OperatorNode.d.ts +++ /dev/null @@ -1,83 +0,0 @@ -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export type OperatorNodeOp = - | "%" - | "&" - | "|" - | "^" - | ">>" - | "<<" - | "==" - | "&&" - | "||" - | "^^" - | "<" - | ">" - | "<=" - | ">=" - | "+" - | "-" - | "*" - | "/"; - -export default class OperatorNode extends TempNode { - aNode: Node; - bNode: Node; - op: OperatorNodeOp; - - constructor(op: OperatorNodeOp, ...params: [Node, Node, ...Node[]]); -} - -type Operator = ( - a: NodeRepresentation, - b: NodeRepresentation, - ...others: NodeRepresentation[] -) => ShaderNodeObject; - -export const add: Operator; -export const sub: Operator; -export const mul: Operator; -export const div: Operator; -export const remainder: Operator; -export const equal: Operator; -export const lessThan: Operator; -export const greaterThan: Operator; -export const lessThanEqual: Operator; -export const greaterThanEqual: Operator; -export const and: Operator; -export const or: Operator; -export const not: (a: NodeRepresentation) => ShaderNodeObject; -export const xor: Operator; -export const bitAnd: Operator; -export const bitNot: (a: NodeRepresentation) => ShaderNodeObject; -export const bitOr: Operator; -export const bitXor: Operator; -export const shiftLeft: Operator; -export const shiftRight: Operator; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - add: typeof add; - sub: typeof sub; - mul: typeof mul; - div: typeof div; - remainder: typeof remainder; - equal: typeof equal; - lessThan: typeof lessThan; - greaterThan: typeof greaterThan; - lessThanEqual: typeof lessThanEqual; - greaterThanEqual: typeof greaterThanEqual; - and: typeof and; - or: typeof or; - not: typeof not; - xor: typeof xor; - bitAnd: typeof bitAnd; - bitNot: typeof bitNot; - bitOr: typeof bitOr; - bitXor: typeof bitXor; - shiftLeft: typeof shiftLeft; - shiftRight: typeof shiftRight; - } -} diff --git a/src-testing/src/nodes/math/TriNoise3D.d.ts b/src-testing/src/nodes/math/TriNoise3D.d.ts deleted file mode 100644 index 88a92a440..000000000 --- a/src-testing/src/nodes/math/TriNoise3D.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const tri: (x: NodeRepresentation) => ShaderNodeObject; - -export const tri3: (p: NodeRepresentation) => ShaderNodeObject; - -export const triNoise3D: ( - p_immutable: NodeRepresentation, - spd: NodeRepresentation, - time: NodeRepresentation, -) => ShaderNodeObject; diff --git a/src-testing/src/nodes/pmrem/PMREMNode.d.ts b/src-testing/src/nodes/pmrem/PMREMNode.d.ts deleted file mode 100644 index 886aa24fc..000000000 --- a/src-testing/src/nodes/pmrem/PMREMNode.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Texture } from "../../textures/Texture.js"; -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class PMREMNode extends TempNode { - uvNode: Node | null; - levelNode: Node | null; - - constructor(value: Texture, uvNode?: Node | null, levelNode?: Node | null); - - set value(value: Texture); - get value(): Texture; -} - -export const pmremTexture: ( - value: Texture, - uvNode?: NodeRepresentation, - levelNode?: NodeRepresentation, -) => ShaderNodeObject; diff --git a/src-testing/src/nodes/procedural/CheckerNode.d.ts b/src-testing/src/nodes/procedural/CheckerNode.d.ts deleted file mode 100644 index 74a1b731e..000000000 --- a/src-testing/src/nodes/procedural/CheckerNode.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -import TempNode from "../core/TempNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class CheckerNode extends TempNode { - uvNode: Node; - constructor(uvNode?: Node); -} - -export const checker: (uvNode?: NodeRepresentation) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - checker: typeof checker; - } -} diff --git a/src-testing/src/nodes/shadernode/ShaderNode.ts b/src-testing/src/nodes/shadernode/ShaderNode.ts deleted file mode 100644 index 9b8e26e58..000000000 --- a/src-testing/src/nodes/shadernode/ShaderNode.ts +++ /dev/null @@ -1,538 +0,0 @@ -import Node, { addNodeClass } from '../core/Node.js'; -import ArrayElementNode from '../utils/ArrayElementNode.js'; -import ConvertNode from '../utils/ConvertNode.js'; -import JoinNode from '../utils/JoinNode.js'; -import SplitNode from '../utils/SplitNode.js'; -import SetNode from '../utils/SetNode.js'; -import ConstNode from '../core/ConstNode.js'; -import { getValueFromType, getValueType } from '../core/NodeUtils.js'; - -// - -let currentStack = null; - -const NodeElements = new Map(); // @TODO: Currently only a few nodes are added, probably also add others - -export function addNodeElement(name, nodeElement) { - if (NodeElements.has(name)) { - console.warn(`Redefinition of node element ${name}`); - return; - } - - if (typeof nodeElement !== 'function') throw new Error(`Node element ${name} is not a function`); - - NodeElements.set(name, nodeElement); -} - -const parseSwizzle = props => props.replace(/r|s/g, 'x').replace(/g|t/g, 'y').replace(/b|p/g, 'z').replace(/a|q/g, 'w'); - -const shaderNodeHandler = { - setup(NodeClosure, params) { - const inputs = params.shift(); - - return NodeClosure(nodeObjects(inputs), ...params); - }, - - get(node, prop, nodeObj) { - if (typeof prop === 'string' && node[prop] === undefined) { - if (node.isStackNode !== true && prop === 'assign') { - return (...params) => { - currentStack.assign(nodeObj, ...params); - - return nodeObj; - }; - } else if (NodeElements.has(prop)) { - const nodeElement = NodeElements.get(prop); - - return node.isStackNode - ? (...params) => nodeObj.add(nodeElement(...params)) - : (...params) => nodeElement(nodeObj, ...params); - } else if (prop === 'self') { - return node; - } else if (prop.endsWith('Assign') && NodeElements.has(prop.slice(0, prop.length - 'Assign'.length))) { - const nodeElement = NodeElements.get(prop.slice(0, prop.length - 'Assign'.length)); - - return node.isStackNode - ? (...params) => nodeObj.assign(params[0], nodeElement(...params)) - : (...params) => nodeObj.assign(nodeElement(nodeObj, ...params)); - } else if (/^[xyzwrgbastpq]{1,4}$/.test(prop) === true) { - // accessing properties ( swizzle ) - - prop = parseSwizzle(prop); - - return nodeObject(new SplitNode(nodeObj, prop)); - } else if (/^set[XYZWRGBASTPQ]{1,4}$/.test(prop) === true) { - // set properties ( swizzle ) - - prop = parseSwizzle(prop.slice(3).toLowerCase()); - - // sort to xyzw sequence - - prop = prop.split('').sort().join(''); - - return value => nodeObject(new SetNode(node, prop, value)); - } else if (prop === 'width' || prop === 'height' || prop === 'depth') { - // accessing property - - if (prop === 'width') prop = 'x'; - else if (prop === 'height') prop = 'y'; - else if (prop === 'depth') prop = 'z'; - - return nodeObject(new SplitNode(node, prop)); - } else if (/^\d+$/.test(prop) === true) { - // accessing array - - return nodeObject(new ArrayElementNode(nodeObj, new ConstNode(Number(prop), 'uint'))); - } - } - - return Reflect.get(node, prop, nodeObj); - }, - - set(node, prop, value, nodeObj) { - if (typeof prop === 'string' && node[prop] === undefined) { - // setting properties - - if ( - /^[xyzwrgbastpq]{1,4}$/.test(prop) === true || - prop === 'width' || - prop === 'height' || - prop === 'depth' || - /^\d+$/.test(prop) === true - ) { - nodeObj[prop].assign(value); - - return true; - } - } - - return Reflect.set(node, prop, value, nodeObj); - }, -}; - -const nodeObjectsCacheMap = new WeakMap(); -const nodeBuilderFunctionsCacheMap = new WeakMap(); - -const ShaderNodeObject = function (obj, altType = null) { - const type = getValueType(obj); - - if (type === 'node') { - let nodeObject = nodeObjectsCacheMap.get(obj); - - if (nodeObject === undefined) { - nodeObject = new Proxy(obj, shaderNodeHandler); - - nodeObjectsCacheMap.set(obj, nodeObject); - nodeObjectsCacheMap.set(nodeObject, nodeObject); - } - - return nodeObject; - } else if ( - (altType === null && (type === 'float' || type === 'boolean')) || - (type && type !== 'shader' && type !== 'string') - ) { - return nodeObject(getConstNode(obj, altType)); - } else if (type === 'shader') { - return Fn(obj); - } - - return obj; -}; - -const ShaderNodeObjects = function (objects, altType = null) { - for (const name in objects) { - objects[name] = nodeObject(objects[name], altType); - } - - return objects; -}; - -const ShaderNodeArray = function (array, altType = null) { - const len = array.length; - - for (let i = 0; i < len; i++) { - array[i] = nodeObject(array[i], altType); - } - - return array; -}; - -const ShaderNodeProxy = function (NodeClass, scope = null, factor = null, settings = null) { - const assignNode = node => nodeObject(settings !== null ? Object.assign(node, settings) : node); - - if (scope === null) { - return (...params) => { - return assignNode(new NodeClass(...nodeArray(params))); - }; - } else if (factor !== null) { - factor = nodeObject(factor); - - return (...params) => { - return assignNode(new NodeClass(scope, ...nodeArray(params), factor)); - }; - } else { - return (...params) => { - return assignNode(new NodeClass(scope, ...nodeArray(params))); - }; - } -}; - -const ShaderNodeImmutable = function (NodeClass, ...params) { - return nodeObject(new NodeClass(...nodeArray(params))); -}; - -class ShaderCallNodeInternal extends Node { - constructor(shaderNode, inputNodes) { - super(); - - this.shaderNode = shaderNode; - this.inputNodes = inputNodes; - } - - getNodeType(builder) { - const properties = builder.getNodeProperties(this); - - if (properties.outputNode === null) { - properties.outputNode = this.setupOutput(builder); - } - - return properties.outputNode.getNodeType(builder); - } - - call(builder) { - const { shaderNode, inputNodes } = this; - - if (shaderNode.layout) { - let functionNodesCacheMap = nodeBuilderFunctionsCacheMap.get(builder.constructor); - - if (functionNodesCacheMap === undefined) { - functionNodesCacheMap = new WeakMap(); - - nodeBuilderFunctionsCacheMap.set(builder.constructor, functionNodesCacheMap); - } - - let functionNode = functionNodesCacheMap.get(shaderNode); - - if (functionNode === undefined) { - functionNode = nodeObject(builder.buildFunctionNode(shaderNode)); - - functionNodesCacheMap.set(shaderNode, functionNode); - } - - if (builder.currentFunctionNode !== null) { - builder.currentFunctionNode.includes.push(functionNode); - } - - return nodeObject(functionNode.call(inputNodes)); - } - - const jsFunc = shaderNode.jsFunc; - const outputNode = inputNodes !== null ? jsFunc(inputNodes, builder) : jsFunc(builder); - - return nodeObject(outputNode); - } - - setup(builder) { - const { outputNode } = builder.getNodeProperties(this); - - return outputNode || this.setupOutput(builder); - } - - setupOutput(builder) { - builder.addStack(); - - builder.stack.outputNode = this.call(builder); - - return builder.removeStack(); - } - - generate(builder, output) { - const { outputNode } = builder.getNodeProperties(this); - - if (outputNode === null) { - // TSL: It's recommended to use `tslFn` in setup() pass. - - return this.call(builder).build(builder, output); - } - - return super.generate(builder, output); - } -} - -class ShaderNodeInternal extends Node { - constructor(jsFunc) { - super(); - - this.jsFunc = jsFunc; - this.layout = null; - - this.global = true; - } - - get isArrayInput() { - return /^\((\s+)?\[/.test(this.jsFunc.toString()); - } - - setLayout(layout) { - this.layout = layout; - - return this; - } - - call(inputs = null) { - nodeObjects(inputs); - - return nodeObject(new ShaderCallNodeInternal(this, inputs)); - } - - setup() { - return this.call(); - } -} - -const bools = [false, true]; -const uints = [0, 1, 2, 3]; -const ints = [-1, -2]; -const floats = [ - 0.5, - 1.5, - 1 / 3, - 1e-6, - 1e6, - Math.PI, - Math.PI * 2, - 1 / Math.PI, - 2 / Math.PI, - 1 / (Math.PI * 2), - Math.PI / 2, -]; - -const boolsCacheMap = new Map(); -for (const bool of bools) boolsCacheMap.set(bool, new ConstNode(bool)); - -const uintsCacheMap = new Map(); -for (const uint of uints) uintsCacheMap.set(uint, new ConstNode(uint, 'uint')); - -const intsCacheMap = new Map([...uintsCacheMap].map(el => new ConstNode(el.value, 'int'))); -for (const int of ints) intsCacheMap.set(int, new ConstNode(int, 'int')); - -const floatsCacheMap = new Map([...intsCacheMap].map(el => new ConstNode(el.value))); -for (const float of floats) floatsCacheMap.set(float, new ConstNode(float)); -for (const float of floats) floatsCacheMap.set(-float, new ConstNode(-float)); - -const cacheMaps = { bool: boolsCacheMap, uint: uintsCacheMap, ints: intsCacheMap, float: floatsCacheMap }; - -const constNodesCacheMap = new Map([...boolsCacheMap, ...floatsCacheMap]); - -const getConstNode = (value, type) => { - if (constNodesCacheMap.has(value)) { - return constNodesCacheMap.get(value); - } else if (value.isNode === true) { - return value; - } else { - return new ConstNode(value, type); - } -}; - -const safeGetNodeType = node => { - try { - return node.getNodeType(); - } catch (_) { - return undefined; - } -}; - -const ConvertType = function (type, cacheMap = null) { - return (...params) => { - if ( - params.length === 0 || - (!['bool', 'float', 'int', 'uint'].includes(type) && params.every(param => typeof param !== 'object')) - ) { - params = [getValueFromType(type, ...params)]; - } - - if (params.length === 1 && cacheMap !== null && cacheMap.has(params[0])) { - return nodeObject(cacheMap.get(params[0])); - } - - if (params.length === 1) { - const node = getConstNode(params[0], type); - if (safeGetNodeType(node) === type) return nodeObject(node); - return nodeObject(new ConvertNode(node, type)); - } - - const nodes = params.map(param => getConstNode(param)); - return nodeObject(new JoinNode(nodes, type)); - }; -}; - -// exports - -export const defined = v => (typeof v === 'object' && v !== null ? v.value : v); // TODO: remove boolean conversion and defined function - -// utils - -export const getConstNodeType = value => - value !== undefined && value !== null - ? value.nodeType || value.convertTo || (typeof value === 'string' ? value : null) - : null; - -// shader node base - -export function ShaderNode(jsFunc) { - return new Proxy(new ShaderNodeInternal(jsFunc), shaderNodeHandler); -} - -export const nodeObject = (val, altType = null) => /* new */ ShaderNodeObject(val, altType); -export const nodeObjects = (val, altType = null) => new ShaderNodeObjects(val, altType); -export const nodeArray = (val, altType = null) => new ShaderNodeArray(val, altType); -export const nodeProxy = (...params) => new ShaderNodeProxy(...params); -export const nodeImmutable = (...params) => new ShaderNodeImmutable(...params); - -export const Fn = jsFunc => { - const shaderNode = new ShaderNode(jsFunc); - - const fn = (...params) => { - let inputs; - - nodeObjects(params); - - if (params[0] && params[0].isNode) { - inputs = [...params]; - } else { - inputs = params[0]; - } - - return shaderNode.call(inputs); - }; - - fn.shaderNode = shaderNode; - fn.setLayout = layout => { - shaderNode.setLayout(layout); - - return fn; - }; - - return fn; -}; - -export const tslFn = (...params) => { - // @deprecated, r168 - - console.warn('TSL.tslFn: tslFn() has been renamed to Fn().'); - return Fn(...params); -}; - -addNodeClass('ShaderNode', ShaderNode); - -// - -addNodeElement('toGlobal', node => { - node.global = true; - - return node; -}); - -// - -export const setCurrentStack = stack => { - if (currentStack === stack) { - //throw new Error( 'Stack already defined.' ); - } - - currentStack = stack; -}; - -export const getCurrentStack = () => currentStack; - -export const If = (...params) => currentStack.If(...params); - -export function append(node) { - if (currentStack) currentStack.add(node); - - return node; -} - -addNodeElement('append', append); - -// types -// @TODO: Maybe export from ConstNode.js? - -export const color = new ConvertType('color'); - -export const float = new ConvertType('float', cacheMaps.float); -export const int = new ConvertType('int', cacheMaps.ints); -export const uint = new ConvertType('uint', cacheMaps.uint); -export const bool = new ConvertType('bool', cacheMaps.bool); - -export const vec2 = new ConvertType('vec2'); -export const ivec2 = new ConvertType('ivec2'); -export const uvec2 = new ConvertType('uvec2'); -export const bvec2 = new ConvertType('bvec2'); - -export const vec3 = new ConvertType('vec3'); -export const ivec3 = new ConvertType('ivec3'); -export const uvec3 = new ConvertType('uvec3'); -export const bvec3 = new ConvertType('bvec3'); - -export const vec4 = new ConvertType('vec4'); -export const ivec4 = new ConvertType('ivec4'); -export const uvec4 = new ConvertType('uvec4'); -export const bvec4 = new ConvertType('bvec4'); - -export const mat2 = new ConvertType('mat2'); -export const imat2 = new ConvertType('imat2'); -export const umat2 = new ConvertType('umat2'); -export const bmat2 = new ConvertType('bmat2'); - -export const mat3 = new ConvertType('mat3'); -export const imat3 = new ConvertType('imat3'); -export const umat3 = new ConvertType('umat3'); -export const bmat3 = new ConvertType('bmat3'); - -export const mat4 = new ConvertType('mat4'); -export const imat4 = new ConvertType('imat4'); -export const umat4 = new ConvertType('umat4'); -export const bmat4 = new ConvertType('bmat4'); - -export const string = (value = '') => nodeObject(new ConstNode(value, 'string')); -export const arrayBuffer = value => nodeObject(new ConstNode(value, 'ArrayBuffer')); - -addNodeElement('toColor', color); -addNodeElement('toFloat', float); -addNodeElement('toInt', int); -addNodeElement('toUint', uint); -addNodeElement('toBool', bool); -addNodeElement('toVec2', vec2); -addNodeElement('toIvec2', ivec2); -addNodeElement('toUvec2', uvec2); -addNodeElement('toBvec2', bvec2); -addNodeElement('toVec3', vec3); -addNodeElement('toIvec3', ivec3); -addNodeElement('toUvec3', uvec3); -addNodeElement('toBvec3', bvec3); -addNodeElement('toVec4', vec4); -addNodeElement('toIvec4', ivec4); -addNodeElement('toUvec4', uvec4); -addNodeElement('toBvec4', bvec4); -addNodeElement('toMat2', mat2); -addNodeElement('toImat2', imat2); -addNodeElement('toUmat2', umat2); -addNodeElement('toBmat2', bmat2); -addNodeElement('toMat3', mat3); -addNodeElement('toImat3', imat3); -addNodeElement('toUmat3', umat3); -addNodeElement('toBmat3', bmat3); -addNodeElement('toMat4', mat4); -addNodeElement('toImat4', imat4); -addNodeElement('toUmat4', umat4); -addNodeElement('toBmat4', bmat4); - -// basic nodes -// HACK - we cannot export them from the corresponding files because of the cyclic dependency -export const element = nodeProxy(ArrayElementNode); -export const convert = (node, types) => nodeObject(new ConvertNode(nodeObject(node), types)); -export const split = (node, channels) => nodeObject(new SplitNode(nodeObject(node), channels)); - -addNodeElement('element', element); -addNodeElement('convert', convert); diff --git a/src-testing/src/nodes/utils/ArrayElementNode.d.ts b/src-testing/src/nodes/utils/ArrayElementNode.d.ts deleted file mode 100644 index 650f04047..000000000 --- a/src-testing/src/nodes/utils/ArrayElementNode.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import Node from "../core/Node.js"; -import { TempNode } from "../Nodes.js"; - -export default class ArrayElementNode extends TempNode { - node: Node; - indexNode: Node; - - constructor(node: Node, indexNode: Node); -} diff --git a/src-testing/src/nodes/utils/ConvertNode.d.ts b/src-testing/src/nodes/utils/ConvertNode.d.ts deleted file mode 100644 index 7972df608..000000000 --- a/src-testing/src/nodes/utils/ConvertNode.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import Node from "../core/Node.js"; - -export default class ConvertNode extends Node { - node: Node; - convertTo: string; - constructor(node: Node, convertTo: string); -} diff --git a/src-testing/src/nodes/utils/DiscardNode.d.ts b/src-testing/src/nodes/utils/DiscardNode.d.ts deleted file mode 100644 index f18567d07..000000000 --- a/src-testing/src/nodes/utils/DiscardNode.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -import Node from "../core/Node.js"; -import CondNode from "../math/CondNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class DiscardNode extends CondNode { - constructor(condNode: Node); -} - -export const inlineDiscard: (condNode: NodeRepresentation) => ShaderNodeObject; -export const discard: (condNode: NodeRepresentation) => ShaderNodeObject; -export const Return: (condNode: NodeRepresentation) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - discard: typeof discard; - } -} diff --git a/src-testing/src/nodes/utils/EquirectUVNode.d.ts b/src-testing/src/nodes/utils/EquirectUVNode.d.ts deleted file mode 100644 index 9020033d1..000000000 --- a/src-testing/src/nodes/utils/EquirectUVNode.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Node, TempNode } from "../Nodes.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class EquirectUVNode extends TempNode { - constructor(dirNode?: ShaderNodeObject); -} - -export const equirectUV: ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/JoinNode.d.ts b/src-testing/src/nodes/utils/JoinNode.d.ts deleted file mode 100644 index 7f456bafa..000000000 --- a/src-testing/src/nodes/utils/JoinNode.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import Node from "../core/Node.js"; -import { TempNode } from "../Nodes.js"; - -/** - * This node constructs given type from elements, like vec3(a,b,c) - */ -export default class JoinNode extends TempNode { - nodes: Node[]; - constructor(nodes: Node[]); -} diff --git a/src-testing/src/nodes/utils/LoopNode.d.ts b/src-testing/src/nodes/utils/LoopNode.d.ts deleted file mode 100644 index 2fb1e1e30..000000000 --- a/src-testing/src/nodes/utils/LoopNode.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -import Node from "../core/Node.js"; -import NodeBuilder from "../core/NodeBuilder.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -declare class LoopNode extends Node { - params: unknown[]; - - constructor(params?: unknown[]); - - getProperties(builder: NodeBuilder): unknown; -} - -export default LoopNode; - -export const Loop: (...params: unknown[]) => ShaderNodeObject; -export const Continue: () => ShaderNodeObject; -export const Break: () => ShaderNodeObject; - -/** - * @deprecated loop() has been renamed to Loop() - */ -export const loop: (...params: unknown[]) => ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/MatcapUVNode.d.ts b/src-testing/src/nodes/utils/MatcapUVNode.d.ts deleted file mode 100644 index b59cf8ae6..000000000 --- a/src-testing/src/nodes/utils/MatcapUVNode.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import TempNode from "../core/TempNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class MatcapUVNode extends TempNode { - constructor(); -} - -export const matcapUV: ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/MaxMipLevelNode.d.ts b/src-testing/src/nodes/utils/MaxMipLevelNode.d.ts deleted file mode 100644 index 858624571..000000000 --- a/src-testing/src/nodes/utils/MaxMipLevelNode.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Texture } from "../../textures/Texture.js"; -import TextureNode from "../accessors/TextureNode.js"; -import UniformNode from "../core/UniformNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class MaxMipLevelNode extends UniformNode<0> { - constructor(textureNode: TextureNode); - - get textureNode(): TextureNode; - - get texture(): Texture; -} - -export const maxMipLevel: (texture: Texture) => ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/OscNode.d.ts b/src-testing/src/nodes/utils/OscNode.d.ts deleted file mode 100644 index d99abbcf4..000000000 --- a/src-testing/src/nodes/utils/OscNode.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export type OscNodeMethod = - | typeof OscNode.SINE - | typeof OscNode.SQUARE - | typeof OscNode.TRIANGLE - | typeof OscNode.SAWTOOTH; - -export default class OscNode extends Node { - static SINE: "sine"; - static SQUARE: "square"; - static TRIANGLE: "triangle"; - static SAWTOOTH: "sawtooth"; - - method: OscNodeMethod; - timeNode: Node; - - constructor(method: OscNodeMethod, timeNode?: Node); -} - -export const oscSine: (timeNode?: NodeRepresentation) => ShaderNodeObject; -export const oscSquare: (timeNode?: NodeRepresentation) => ShaderNodeObject; -export const oscTriangle: (timeNode?: NodeRepresentation) => ShaderNodeObject; -export const oscSawtooth: (timeNode?: NodeRepresentation) => ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/PackingNode.d.ts b/src-testing/src/nodes/utils/PackingNode.d.ts deleted file mode 100644 index c404b6469..000000000 --- a/src-testing/src/nodes/utils/PackingNode.d.ts +++ /dev/null @@ -1,24 +0,0 @@ -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export type PackingNodeScope = typeof PackingNode.DIRECTION_TO_COLOR | typeof PackingNode.COLOR_TO_DIRECTION; - -declare class PackingNode extends TempNode { - constructor(scope: PackingNodeScope, node: Node); - - static DIRECTION_TO_COLOR: "directionToColor"; - static COLOR_TO_DIRECTION: "colorToDirection"; -} - -export default PackingNode; - -export const directionToColor: (node: NodeRepresentation) => ShaderNodeObject; -export const colorToDirection: (node: NodeRepresentation) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - directionToColor: typeof directionToColor; - colorToDirection: typeof colorToDirection; - } -} diff --git a/src-testing/src/nodes/utils/RTTNode.d.ts b/src-testing/src/nodes/utils/RTTNode.d.ts deleted file mode 100644 index 34925d7c4..000000000 --- a/src-testing/src/nodes/utils/RTTNode.d.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { TextureDataType } from "../../constants.js"; -import { RenderTarget } from "../../core/RenderTarget.js"; -import TextureNode from "../accessors/TextureNode.js"; -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export interface RTTNodeOptions { - type: TextureDataType; -} - -declare class RTTNode extends TextureNode { - node: Node; - width: number | null; - height: number | null; - - renderTarget: RenderTarget | null; - - textureNeedsUpdate: boolean; - autoUpdate: boolean; - - pixelRatio?: number; - - constructor(node: Node, width?: number | null, height?: number | null, options?: RTTNodeOptions); - - get autoSize(): boolean; - - setSize(width: number | null, height: number | null): void; - - setPixelRatio(pixelRatio: number): void; -} - -export default RTTNode; - -export const rtt: ( - node: NodeRepresentation, - width?: number | null, - height?: number | null, - options?: RTTNodeOptions, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - toTexture: typeof rtt; - } -} diff --git a/src-testing/src/nodes/utils/ReflectorNode.d.ts b/src-testing/src/nodes/utils/ReflectorNode.d.ts deleted file mode 100644 index ac6d1e726..000000000 --- a/src-testing/src/nodes/utils/ReflectorNode.d.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { Camera } from "../../cameras/Camera.js"; -import { Object3D } from "../../core/Object3D.js"; -import { RenderTarget } from "../../core/RenderTarget.js"; -import TextureNode from "../accessors/TextureNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export interface ReflectorNodeParameters { - target?: Object3D | undefined; - resolution?: number | undefined; - generateMipmaps?: boolean | undefined; - bounces?: boolean | undefined; -} - -export default class ReflectorNode extends TextureNode { - target: Object3D; - resolution: number; - generateMipmaps: boolean; - bounces: boolean; - - virtualCameras: WeakMap; - renderTargets: WeakMap; - - constructor(parameters?: ReflectorNodeParameters); - - getTextureNode(): TextureNode; - - getVirtualCamera(camera: Camera): Camera; - - getRenderTarget(camera: Camera): RenderTarget; -} - -export const reflector: (parameters?: ReflectorNodeParameters) => ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/RemapNode.d.ts b/src-testing/src/nodes/utils/RemapNode.d.ts deleted file mode 100644 index 5039cd6fc..000000000 --- a/src-testing/src/nodes/utils/RemapNode.d.ts +++ /dev/null @@ -1,36 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class RemapNode extends Node { - node: Node; - inLowNode: Node; - inHighNode: Node; - outLowNode: Node; - outHighNode: Node; - - doClamp: boolean; - - constructor(node: Node, inLowNode: Node, inHighNode: Node, outLowNode?: Node, outHighNode?: Node); -} - -export const remap: ( - node: Node, - inLowNode: NodeRepresentation, - inHighNode: NodeRepresentation, - outLowNode?: NodeRepresentation, - outHighNode?: NodeRepresentation, -) => ShaderNodeObject; -export const remapClamp: ( - node: Node, - inLowNode: NodeRepresentation, - inHighNode: NodeRepresentation, - outLowNode?: NodeRepresentation, - outHighNode?: NodeRepresentation, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - remap: typeof remap; - remapClamp: typeof remapClamp; - } -} diff --git a/src-testing/src/nodes/utils/RotateNode.d.ts b/src-testing/src/nodes/utils/RotateNode.d.ts deleted file mode 100644 index 34ba61907..000000000 --- a/src-testing/src/nodes/utils/RotateNode.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -import Node from "../core/Node.js"; -import TempNode from "../core/TempNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class RotateNode extends TempNode { - positionNode: Node; - rotationNode: Node; - - constructor(positionNode: Node, rotationNode: Node); -} - -export const rotate: ( - positionNode: NodeRepresentation, - rotationNode: NodeRepresentation, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - rotate: typeof rotate; - } -} diff --git a/src-testing/src/nodes/utils/SplitNode.d.ts b/src-testing/src/nodes/utils/SplitNode.d.ts deleted file mode 100644 index f3966fb02..000000000 --- a/src-testing/src/nodes/utils/SplitNode.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -import Node from "../core/Node.js"; -import { SwizzleOption } from "../shadernode/ShaderNode.js"; - -/** swizzle node */ -export default class SplitNode extends Node { - node: Node; - components: string; - - /** - * @param node the input node - * @param components swizzle like string, default = "x" - */ - constructor(node: Node, components?: SwizzleOption); - getVectorLength(): number; -} diff --git a/src-testing/src/nodes/utils/SpriteSheetUVNode.d.ts b/src-testing/src/nodes/utils/SpriteSheetUVNode.d.ts deleted file mode 100644 index 111d7611f..000000000 --- a/src-testing/src/nodes/utils/SpriteSheetUVNode.d.ts +++ /dev/null @@ -1,16 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class SpriteSheetUVNode extends Node { - countNode: Node; - uvNode: Node; - frameNode: Node; - - constructor(countNode: Node, uvNode?: Node, frameNode?: Node); -} - -export const spritesheetUV: ( - countNode: NodeRepresentation, - uvNode?: NodeRepresentation, - frameNode?: NodeRepresentation, -) => ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/SpriteUtils.d.ts b/src-testing/src/nodes/utils/SpriteUtils.d.ts deleted file mode 100644 index 3952577c4..000000000 --- a/src-testing/src/nodes/utils/SpriteUtils.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const billboarding: ( - args?: { position?: NodeRepresentation | null; horizontal?: boolean; vertical?: boolean }, -) => ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/StoargeArrayElementNode.d.ts b/src-testing/src/nodes/utils/StoargeArrayElementNode.d.ts deleted file mode 100644 index 0d74282e5..000000000 --- a/src-testing/src/nodes/utils/StoargeArrayElementNode.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -import StorageBufferNode from "../accessors/StorageBufferNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; -import ArrayElementNode from "./ArrayElementNode.js"; - -export default class StorageArrayElementNode extends ArrayElementNode { - node: StorageBufferNode; - - readonly isStorageArrayElementNode: true; - - constructor(storageBufferNode: StorageBufferNode, indexNode: Node); - - get storageBufferNode(): StorageBufferNode; - set storageBufferNode(value: StorageBufferNode); -} - -export const storageElement: ( - storageBufferNode: NodeRepresentation, - indexNode: NodeRepresentation, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - storageElement: typeof storageElement; - } -} diff --git a/src-testing/src/nodes/utils/TimerNode.d.ts b/src-testing/src/nodes/utils/TimerNode.d.ts deleted file mode 100644 index 02cac2909..000000000 --- a/src-testing/src/nodes/utils/TimerNode.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -import UniformNode from "../core/UniformNode.js"; -import { ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export type TimerNodeScope = - | typeof TimerNode.LOCAL - | typeof TimerNode.GLOBAL - | typeof TimerNode.DELTA - | typeof TimerNode.FRAME; - -export default class TimerNode extends UniformNode { - static LOCAL: "local"; - static GLOBAL: "global"; - static DELTA: "delta"; - static FRAME: "frame"; - - scope: TimerNodeScope; - scale: number; - - constructor(scope?: TimerNodeScope, scale?: number, value?: number); -} - -export const timerLocal: (timeScale?: number, value?: number) => ShaderNodeObject; -export const timerGlobal: (timeScale?: number, value?: number) => ShaderNodeObject; -export const timerDelta: (timeScale?: number, value?: number) => ShaderNodeObject; -export const frameId: ShaderNodeObject; diff --git a/src-testing/src/nodes/utils/TriplanarTexturesNode.d.ts b/src-testing/src/nodes/utils/TriplanarTexturesNode.d.ts deleted file mode 100644 index 55af273e3..000000000 --- a/src-testing/src/nodes/utils/TriplanarTexturesNode.d.ts +++ /dev/null @@ -1,42 +0,0 @@ -import TextureNode from "../accessors/TextureNode.js"; -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export default class TriplanarTexturesNode extends Node { - textureXNode: TextureNode; - textureYNode: TextureNode | null; - textureZNode: TextureNode | null; - - scaleNode: ShaderNodeObject; - - positionNode: ShaderNodeObject; - normalNode: ShaderNodeObject; - - constructor( - textureXNode: Node, - textureYNode?: TextureNode | null, - textureZNode?: TextureNode | null, - scaleNode?: ShaderNodeObject, - positionNode?: ShaderNodeObject, - normalNode?: ShaderNodeObject, - ); -} - -export const triplanarTextures: ( - textureXNode: NodeRepresentation, - textureYNode?: NodeRepresentation, - textureZNode?: NodeRepresentation, - scaleNode?: NodeRepresentation, - positionNode?: NodeRepresentation, - normalNode?: NodeRepresentation, -) => ShaderNodeObject; -export const triplanarTexture: ( - texture: NodeRepresentation, - ...params: NodeRepresentation[] -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - triplanarTexture: typeof triplanarTexture; - } -} diff --git a/src-testing/src/nodes/utils/UVUtils.d.ts b/src-testing/src/nodes/utils/UVUtils.d.ts deleted file mode 100644 index 6e5b51f96..000000000 --- a/src-testing/src/nodes/utils/UVUtils.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -import OperatorNode from "../math/OperatorNode.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const rotateUV: ( - uv: NodeRepresentation, - rotation: NodeRepresentation, - center?: NodeRepresentation, -) => ShaderNodeObject; - -export const spherizeUV: ( - uv: NodeRepresentation, - strength: NodeRepresentation, - center?: NodeRepresentation, -) => ShaderNodeObject; - -declare module "../shadernode/ShaderNode.js" { - interface NodeElements { - rotateUV: typeof rotateUV; - spherizeUV: typeof spherizeUV; - } -} diff --git a/src-testing/src/nodes/utils/ViewportUtils.d.ts b/src-testing/src/nodes/utils/ViewportUtils.d.ts deleted file mode 100644 index 3e971255a..000000000 --- a/src-testing/src/nodes/utils/ViewportUtils.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -import Node from "../core/Node.js"; -import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js"; - -export const viewportSafeUV: (uv?: NodeRepresentation | null) => ShaderNodeObject; diff --git a/src-testing/src/objects/BatchedMesh.d.ts b/src-testing/src/objects/BatchedMesh.d.ts deleted file mode 100644 index 2669b5c08..000000000 --- a/src-testing/src/objects/BatchedMesh.d.ts +++ /dev/null @@ -1,197 +0,0 @@ -import { Camera } from "../cameras/Camera.js"; -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { Material } from "../materials/Material.js"; -import { Box3 } from "../math/Box3.js"; -import { Color } from "../math/Color.js"; -import { Matrix4 } from "../math/Matrix4.js"; -import { Sphere } from "../math/Sphere.js"; -import { Mesh } from "./Mesh.js"; - -/** - * A special version of {@link Mesh} with multi draw batch rendering support. Use {@link BatchedMesh} if you have to - * render a large number of objects with the same material but with different world transformations. The usage of - * {@link BatchedMesh} will help you to reduce the number of draw calls and thus improve the overall rendering - * performance in your application. - * - * If the {@link https://developer.mozilla.org/en-US/docs/Web/API/WEBGL_multi_draw WEBGL_multi_draw extension} is not - * supported then a less performant fallback is used. - * - * @example - * const box = new THREE.BoxGeometry( 1, 1, 1 ); - * const sphere = new THREE.SphereGeometry( 1, 12, 12 ); - * const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } ); - * - * // initialize and add geometries into the batched mesh - * const batchedMesh = new BatchedMesh( 10, 5000, 10000, material ); - * const boxGeometryId = batchedMesh.addGeometry( box ); - * const sphereGeometryId = batchedMesh.addGeometry( sphere ); - * - * // create instances of those geometries - * const boxInstancedId1 = batchedMesh.addInstance( boxGeometryId ); - * const boxInstancedId2 = batchedMesh.addInstance( boxGeometryId ); - * - * const sphereInstancedId1 = batchedMesh.addInstance( sphereGeometryId ); - * const sphereInstancedId2 = batchedMesh.addInstance( sphereGeometryId ); - * - * // position the geometries - * batchedMesh.setMatrixAt( boxInstancedId1, boxMatrix1 ); - * batchedMesh.setMatrixAt( boxInstancedId2, boxMatrix2 ); - * - * batchedMesh.setMatrixAt( sphereInstancedId1, sphereMatrix1 ); - * batchedMesh.setMatrixAt( sphereInstancedId2, sphereMatrix2 ); - * - * scene.add( batchedMesh ); - * - * @also Example: {@link https://threejs.org/examples/#webgl_mesh_batch WebGL / mesh / batch} - */ -declare class BatchedMesh extends Mesh { - /** - * This bounding box encloses all instances of the {@link BatchedMesh}. Can be calculated with - * {@link .computeBoundingBox()}. - * @default null - */ - boundingBox: Box3 | null; - - /** - * This bounding sphere encloses all instances of the {@link BatchedMesh}. Can be calculated with - * {@link .computeBoundingSphere()}. - * @default null - */ - boundingSphere: Sphere | null; - - customSort: ((this: this, list: Array<{ start: number; count: number; z: number }>, camera: Camera) => void) | null; - - /** - * If true then the individual objects within the {@link BatchedMesh} are frustum culled. - * @default true - */ - perObjectFrustumCulled: boolean; - - /** - * If true then the individual objects within the {@link BatchedMesh} are sorted to improve overdraw-related - * artifacts. If the material is marked as "transparent" objects are rendered back to front and if not then they are - * rendered front to back. - * @default true - */ - sortObjects: boolean; - - /** - * The maximum number of individual geometries that can be stored in the {@link BatchedMesh}. Read only. - */ - get maxInstanceCount(): number; - - /** - * Read-only flag to check if a given object is of type {@link BatchedMesh}. - */ - readonly isBatchedMesh: true; - - /** - * @param maxInstanceCount the max number of individual geometries planned to be added. - * @param maxVertexCount the max number of vertices to be used by all geometries. - * @param maxIndexCount the max number of indices to be used by all geometries. - * @param material an instance of {@link Material}. Default is a new {@link MeshBasicMaterial}. - */ - constructor(maxInstanceCount: number, maxVertexCount: number, maxIndexCount?: number, material?: Material); - - /** - * Computes the bounding box, updating {@link .boundingBox} attribute. - * Bounding boxes aren't computed by default. They need to be explicitly computed, otherwise they are `null`. - */ - computeBoundingBox(): void; - - /** - * Computes the bounding sphere, updating {@link .boundingSphere} attribute. - * Bounding spheres aren't computed by default. They need to be explicitly computed, otherwise they are `null`. - */ - computeBoundingSphere(): void; - - /** - * Frees the GPU-related resources allocated by this instance. Call this method whenever this instance is no longer - * used in your app. - */ - dispose(): this; - - /** - * Takes a sort a function that is run before render. The function takes a list of instances to sort and a camera. - * The objects in the list include a "z" field to perform a depth-ordered sort with. - */ - setCustomSort( - sortFunction: - | ((this: this, list: Array<{ start: number; count: number; z: number }>, camera: Camera) => void) - | null, - ): this; - - /** - * Get the color of the defined geometry. - * @param instanceId The id of an instance to get the color of. - * @param target The target object to copy the color in to. - */ - getColorAt(instanceId: number, target: Color): void; - - /** - * Get the local transformation matrix of the defined instance. - * @param instanceId The id of an instance to get the matrix of. - * @param target This 4x4 matrix will be set to the local transformation matrix of the defined instance. - */ - getMatrixAt(instanceId: number, target: Matrix4): Matrix4; - - /** - * Get whether the given instance is marked as "visible" or not. - * @param instanceId The id of an instance to get the visibility state of. - */ - getVisibleAt(instanceId: number): boolean; - - /** - * Sets the given color to the defined geometry instance. - * @param instanceId The id of the instance to set the color of. - * @param color The color to set the instance to. - */ - setColorAt(instanceId: number, color: Color): void; - - /** - * Sets the given local transformation matrix to the defined instance. - * @param instanceId The id of an instance to set the matrix of. - * @param matrix A 4x4 matrix representing the local transformation of a single instance. - */ - setMatrixAt(instanceId: number, matrix: Matrix4): this; - - /** - * Sets the visibility of the instance at the given index. - * @param instanceId The id of the instance to set the visibility of. - * @param visible A boolean value indicating the visibility state. - */ - setVisibleAt(instanceId: number, visible: boolean): this; - - /** - * Adds the given geometry to the {@link BatchedMesh} and returns the associated index referring to it. - * @param geometry The geometry to add into the {@link BatchedMesh}. - * @param reservedVertexRange Optional parameter specifying the amount of vertex buffer space to reserve for the - * added geometry. This is necessary if it is planned to set a new geometry at this index at a later time that is - * larger than the original geometry. Defaults to the length of the given geometry vertex buffer. - * @param reservedIndexRange Optional parameter specifying the amount of index buffer space to reserve for the added - * geometry. This is necessary if it is planned to set a new geometry at this index at a later time that is larger - * than the original geometry. Defaults to the length of the given geometry index buffer. - */ - addGeometry(geometry: BufferGeometry, reservedVertexRange?: number, reservedIndexRange?: number): number; - - /** - * Adds a new instance to the {@link BatchedMesh} using the geometry of the given geometryId and returns a new id - * referring to the new instance to be used by other functions. - * @param geometryId The id of a previously added geometry via "addGeometry" to add into the {@link BatchedMesh} to - * render. - */ - addInstance(geometryId: number): number; - - /** - * Replaces the geometry at `geometryId` with the provided geometry. Throws an error if there is not enough space - * reserved for geometry. Calling this will change all instances that are rendering that geometry. - * @param geometryId Which geometry id to replace with this geometry. - * @param geometry The geometry to substitute at the given geometry id. - */ - setGeometryAt(geometryId: number, geometry: BufferGeometry): number; - - getBoundingBoxAt(geometryId: number, target: Box3): Box3 | null; - getBoundingSphereAt(geometryId: number, target: Sphere): Sphere | null; -} - -export { BatchedMesh }; diff --git a/src-testing/src/objects/Bone.d.ts b/src-testing/src/objects/Bone.d.ts deleted file mode 100644 index 3400ea1b6..000000000 --- a/src-testing/src/objects/Bone.d.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Object3D, Object3DEventMap } from "../core/Object3D.js"; - -/** - * A {@link Bone} which is part of a {@link THREE.Skeleton | Skeleton} - * @remarks - * The skeleton in turn is used by the {@link THREE.SkinnedMesh | SkinnedMesh} - * Bones are almost identical to a blank {@link THREE.Object3D | Object3D}. - * @example - * ```typescript - * const root = new THREE.Bone(); - * const child = new THREE.Bone(); - * root.add(child); - * child.position.y = 5; - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/objects/Bone | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Bone.js | Source} - */ -export class Bone extends Object3D { - /** - * Creates a new {@link Bone}. - */ - constructor(); - - /** - * Read-only flag to check if a given object is of type {@link Bone}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isBone: true; - - /** - * @override - * @defaultValue `Bone` - */ - override readonly type: string | "Bone"; -} diff --git a/src-testing/src/objects/Group.d.ts b/src-testing/src/objects/Group.d.ts deleted file mode 100644 index bceb11e5f..000000000 --- a/src-testing/src/objects/Group.d.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { Object3D, Object3DEventMap } from "../core/Object3D.js"; - -/** - * Its purpose is to make working with groups of objects syntactically clearer. - * @remarks This is almost identical to an {@link Object3D | Object3D} - * @example - * ```typescript - * const geometry = new THREE.BoxGeometry(1, 1, 1); - * const material = new THREE.MeshBasicMaterial({ - * color: 0x00ff00 - * }); - * const cubeA = new THREE.Mesh(geometry, material); - * cubeA.position.set(100, 100, 0); - * const cubeB = new THREE.Mesh(geometry, material); - * cubeB.position.set(-100, -100, 0); - * //create a {@link Group} and add the two cubes - * //These cubes can now be rotated / scaled etc as a {@link Group} * const {@link Group} = new THREE.Group(); - * group.add(cubeA); - * group.add(cubeB); - * scene.add(group); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/objects/Group | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Group.js | Source} - */ -export class Group extends Object3D { - /** - * Creates a new {@link Group}. - */ - constructor(); - - /** - * Read-only flag to check if a given object is of type {@link Group}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isGroup: true; - - /** - * @override - * @defaultValue `Group` - */ - override readonly type: string | "Group"; -} diff --git a/src-testing/src/objects/InstancedMesh.d.ts b/src-testing/src/objects/InstancedMesh.d.ts deleted file mode 100644 index b239afb7a..000000000 --- a/src-testing/src/objects/InstancedMesh.d.ts +++ /dev/null @@ -1,179 +0,0 @@ -import { BufferAttributeJSON } from "./../core/BufferAttribute.js"; -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { InstancedBufferAttribute } from "../core/InstancedBufferAttribute.js"; -import { JSONMeta, Object3DEventMap } from "../core/Object3D.js"; -import { Material } from "../materials/Material.js"; -import { Box3 } from "../math/Box3.js"; -import { Color } from "../math/Color.js"; -import { Matrix4 } from "../math/Matrix4.js"; -import { Sphere } from "../math/Sphere.js"; -import { DataTexture } from "../textures/DataTexture.js"; -import { Mesh, MeshJSONObject } from "./Mesh.js"; - -export interface InstancedMeshJSONObject extends MeshJSONObject { - count: number; - instanceMatrix: BufferAttributeJSON; - instanceColor?: BufferAttributeJSON; -} - -export interface InstancedMeshJSON extends MeshJSONObject { - object: InstancedMeshJSONObject; -} - -export interface InstancedMeshEventMap extends Object3DEventMap { - dispose: {}; -} - -/** - * A special version of {@link THREE.Mesh | Mesh} with instanced rendering support - * @remarks - * Use {@link InstancedMesh} if you have to render a large number of objects with the same geometry and material(s) but with different world transformations - * @remarks - * The usage of {@link InstancedMesh} will help you to reduce the number of draw calls and thus improve the overall rendering performance in your application. - * @see Example: {@link https://threejs.org/examples/#webgl_instancing_dynamic | WebGL / instancing / dynamic} - * @see Example: {@link https://threejs.org/examples/#webgl_instancing_performance | WebGL / instancing / performance} - * @see Example: {@link https://threejs.org/examples/#webgl_instancing_scatter | WebGL / instancing / scatter} - * @see Example: {@link https://threejs.org/examples/#webgl_instancing_raycast | WebGL / instancing / raycast} - * @see {@link https://threejs.org/docs/index.html#api/en/objects/InstancedMesh | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/InstancedMesh.js | Source} - */ -export class InstancedMesh< - TGeometry extends BufferGeometry = BufferGeometry, - TMaterial extends Material | Material[] = Material | Material[], - TEventMap extends InstancedMeshEventMap = InstancedMeshEventMap, -> extends Mesh { - /** - * Create a new instance of {@link InstancedMesh} - * @param geometry An instance of {@link BufferGeometry}. - * @param material A single or an array of {@link Material}. Default is a new {@link MeshBasicMaterial}. - * @param count The **maximum** number of instances of this Mesh. Expects a `Integer` - */ - constructor(geometry: TGeometry | undefined, material: TMaterial | undefined, count: number); - - /** - * Read-only flag to check if a given object is of type {@link InstancedMesh}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isInstancedMesh: true; - - /** - * This bounding box encloses all instances of the {@link InstancedMesh},, which can be calculated with {@link computeBoundingBox | .computeBoundingBox()}. - * @remarks Bounding boxes aren't computed by default. They need to be explicitly computed, otherwise they are `null`. - * @defaultValue `null` - */ - boundingBox: Box3 | null; - - /** - * This bounding sphere encloses all instances of the {@link InstancedMesh}, which can be calculated with {@link computeBoundingSphere | .computeBoundingSphere()}. - * @remarks bounding spheres aren't computed by default. They need to be explicitly computed, otherwise they are `null`. - * @defaultValue `null` - */ - boundingSphere: Sphere | null; - - /** - * The number of instances. - * @remarks - * The `count` value passed into the {@link InstancedMesh | constructor} represents the **maximum** number of instances of this mesh. - * You can change the number of instances at runtime to an integer value in the range `[0, count]`. - * @remarks If you need more instances than the original `count` value, you have to create a new InstancedMesh. - * @remarks Expects a `Integer` - */ - count: number; - - /** - * Represents the colors of all instances. - * You have to set {@link InstancedBufferAttribute.needsUpdate | .instanceColor.needsUpdate()} flag to `true` if you modify instanced data via {@link setColorAt | .setColorAt()}. - * @defaultValue `null` - */ - instanceColor: InstancedBufferAttribute | null; - - /** - * Represents the local transformation of all instances. - * You have to set {@link InstancedBufferAttribute.needsUpdate | .instanceMatrix.needsUpdate()} flag to `true` if you modify instanced data via {@link setMatrixAt | .setMatrixAt()}. - */ - instanceMatrix: InstancedBufferAttribute; - - /** - * Represents the morph target weights of all instances. You have to set its {@link .needsUpdate} flag to true if - * you modify instanced data via {@link .setMorphAt}. - */ - morphTexture: DataTexture | null; - - /** - * Computes the bounding box of the instanced mesh, and updates the {@link .boundingBox} attribute. The bounding box - * is not computed by the engine; it must be computed by your app. You may need to recompute the bounding box if an - * instance is transformed via {@link .setMatrixAt()}. - */ - computeBoundingBox(): void; - - /** - * Computes the bounding sphere of the instanced mesh, and updates the {@link .boundingSphere} attribute. The engine - * automatically computes the bounding sphere when it is needed, e.g., for ray casting or view frustum culling. You - * may need to recompute the bounding sphere if an instance is transformed via [page:.setMatrixAt](). - */ - computeBoundingSphere(): void; - - /** - * Get the color of the defined instance. - * @param index The index of an instance. Values have to be in the range `[0, count]`. Expects a `Integer` - * @param color This color object will be set to the color of the defined instance. - */ - getColorAt(index: number, color: Color): void; - - /** - * Sets the given color to the defined instance - * @remarks - * Make sure you set {@link InstancedBufferAttribute.needsUpdate | .instanceColor.needsUpdate()} to `true` after updating all the colors. - * @param index The index of an instance. Values have to be in the range `[0, count]`. Expects a `Integer` - * @param color The color of a single instance. - */ - setColorAt(index: number, color: Color): void; - - /** - * Get the local transformation matrix of the defined instance. - * @param index The index of an instance Values have to be in the range `[0, count]`. Expects a `Integer` - * @param matrix This 4x4 matrix will be set to the local transformation matrix of the defined instance. - */ - getMatrixAt(index: number, matrix: Matrix4): void; - - /** - * Get the morph target weights of the defined instance. - * @param index The index of an instance. Values have to be in the range [0, count]. - * @param mesh The {@link .morphTargetInfluences} property of this mesh will be filled with the morph target weights of the defined instance. - */ - getMorphAt(index: number, mesh: Mesh): void; - - /** - * Sets the given local transformation matrix to the defined instance. - * @remarks - * Make sure you set {@link InstancedBufferAttribute.needsUpdate | .instanceMatrix.needsUpdate()} flag to `true` after updating all the matrices. - * @param index The index of an instance. Values have to be in the range `[0, count]`. Expects a `Integer` - * @param matrix A 4x4 matrix representing the local transformation of a single instance. - */ - setMatrixAt(index: number, matrix: Matrix4): void; - - /** - * Sets the morph target weights to the defined instance. Make sure you set {@link .morphTexture}{@link .needsUpdate} - * to true after updating all the influences. - * @param index The index of an instance. Values have to be in the range [0, count]. - * @param mesh A mesh with {@link .morphTargetInfluences} property containing the morph target weights of a single instance. - */ - setMorphAt(index: number, mesh: Mesh): void; - - /** - * No effect in {@link InstancedMesh}. - * @ignore - * @hidden - */ - override updateMorphTargets(): void; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): this; - - toJSON(meta?: JSONMeta): InstancedMeshJSON; -} diff --git a/src-testing/src/objects/LOD.d.ts b/src-testing/src/objects/LOD.d.ts deleted file mode 100644 index ec6657c7f..000000000 --- a/src-testing/src/objects/LOD.d.ts +++ /dev/null @@ -1,104 +0,0 @@ -import { Camera } from "../cameras/Camera.js"; -import { JSONMeta, Object3D, Object3DEventMap, Object3DJSON, Object3DJSONObject } from "../core/Object3D.js"; - -export interface LODJSONObject extends Object3DJSONObject { - autoUpdate?: boolean; - - levels: Array<{ - object: string; - distance: number; - hysteresis: number; - }>; -} - -export interface LODJSON extends Object3DJSON { - object: LODJSONObject; -} - -/** - * Every level is associated with an object, and rendering can be switched between them at the distances specified - * @remarks - * Typically you would create, say, three meshes, one for far away (low detail), one for mid range (medium detail) and one for close up (high detail). - * @example - * ```typescript - * const {@link LOD} = new THREE.LOD(); - * //Create spheres with 3 levels of detail and create new {@link LOD} levels for them - * for (let i = 0; i & lt; 3; i++) { - * const geometry = new THREE.IcosahedronGeometry(10, 3 - i) - * const mesh = new THREE.Mesh(geometry, material); - * lod.addLevel(mesh, i * 75); - * } - * scene.add(lod); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_lod | webgl / {@link LOD} } - * @see {@link https://threejs.org/docs/index.html#api/en/objects/LOD | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/LOD.js | Source} - */ -export class LOD extends Object3D { - /** - * Creates a new {@link LOD}. - */ - constructor(); - - /** - * Read-only flag to check if a given object is of type {@link LOD}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isLOD: true; - - /** - * @override - * @defaultValue `LOD` - */ - override readonly type: string | "LOD"; - - /** - * An array of level objects - */ - levels: Array<{ - /** The Object3D to display at this level. */ - object: Object3D; - /** The distance at which to display this level of detail. Expects a `Float`. */ - distance: number; - /** Threshold used to avoid flickering at LOD boundaries, as a fraction of distance. Expects a `Float`. */ - hysteresis: number; - }>; - - /** - * Whether the {@link LOD} object is updated automatically by the renderer per frame or not. - * If set to `false`, you have to call {@link update | .update()} in the render loop by yourself. - * @defaultValue `true` - */ - autoUpdate: boolean; - - /** - * Adds a mesh that will display at a certain distance and greater. Typically the further away the distance, the lower the detail on the mesh. - * - * @param object The Object3D to display at this level. - * @param distance The distance at which to display this level of detail. Expects a `Float`. Default `0.0`. - * @param hysteresis Threshold used to avoid flickering at LOD boundaries, as a fraction of distance. Expects a `Float`. Default `0.0`. - */ - addLevel(object: Object3D, distance?: number, hysteresis?: number): this; - - /** - * Get the currently active {@link LOD} level - * @remarks - * As index of the levels array. - */ - getCurrentLevel(): number; - - /** - * Get a reference to the first {@link THREE.Object3D | Object3D} (mesh) that is greater than {@link distance}. - * @param distance Expects a `Float` - */ - getObjectForDistance(distance: number): Object3D | null; - - /** - * Set the visibility of each {@link levels | level}'s {@link THREE.Object3D | object} based on distance from the {@link THREE.Camera | camera}. - * @param camera - */ - update(camera: Camera): void; - - toJSON(meta?: JSONMeta): LODJSON; -} diff --git a/src-testing/src/objects/Line.d.ts b/src-testing/src/objects/Line.d.ts deleted file mode 100644 index 2d76dec69..000000000 --- a/src-testing/src/objects/Line.d.ts +++ /dev/null @@ -1,87 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { Object3D, Object3DEventMap } from "../core/Object3D.js"; -import { Material } from "../materials/Material.js"; - -/** - * A continuous line. - * @remarks - * This is nearly the same as {@link THREE.LineSegments | LineSegments}, - * the only difference is that it is rendered using {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements | gl.LINE_STRIP} - * instead of {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements | gl.LINES} - * @example - * ```typescript - * const material = new THREE.LineBasicMaterial({ - * color: 0x0000ff - * }); - * const points = []; - * points.push(new THREE.Vector3(-10, 0, 0)); - * points.push(new THREE.Vector3(0, 10, 0)); - * points.push(new THREE.Vector3(10, 0, 0)); - * const geometry = new THREE.BufferGeometry().setFromPoints(points); - * const {@link Line} = new THREE.Line(geometry, material); - * scene.add(line); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/objects/Line | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Line.js | Source} - */ -export class Line< - TGeometry extends BufferGeometry = BufferGeometry, - TMaterial extends Material | Material[] = Material | Material[], - TEventMap extends Object3DEventMap = Object3DEventMap, -> extends Object3D { - /** - * Create a new instance of {@link Line} - * @param geometry Vertices representing the {@link Line} segment(s). Default {@link THREE.BufferGeometry | `new THREE.BufferGeometry()`}. - * @param material Material for the line. Default {@link THREE.LineBasicMaterial | `new THREE.LineBasicMaterial()`}. - */ - constructor(geometry?: TGeometry, material?: TMaterial); - - /** - * Read-only flag to check if a given object is of type {@link Line}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isLine: true; - - /** - * @override - * @defaultValue `Line` - */ - override readonly type: string | "Line"; - - /** - * Vertices representing the {@link Line} segment(s). - */ - geometry: TGeometry; - - /** - * Material for the line. - */ - material: TMaterial; - - /** - * An array of weights typically from `0-1` that specify how much of the morph is applied. - * @defaultValue `undefined`, but reset to a blank array by {@link updateMorphTargets | .updateMorphTargets()}. - */ - morphTargetInfluences?: number[] | undefined; - - /** - * A dictionary of morphTargets based on the `morphTarget.name` property. - * @defaultValue `undefined`, but reset to a blank array by {@link updateMorphTargets | .updateMorphTargets()}. - */ - morphTargetDictionary?: { [key: string]: number } | undefined; - - /** - * Computes an array of distance values which are necessary for {@link THREE.LineDashedMaterial | LineDashedMaterial} - * @remarks - * For each vertex in the geometry, the method calculates the cumulative length from the current point to the very beginning of the line. - */ - computeLineDistances(): this; - - /** - * Updates the morphTargets to have no influence on the object - * @remarks - * Resets the {@link morphTargetInfluences | .morphTargetInfluences} and {@link morphTargetDictionary | .morphTargetDictionary} properties. - */ - updateMorphTargets(): void; -} diff --git a/src-testing/src/objects/LineLoop.d.ts b/src-testing/src/objects/LineLoop.d.ts deleted file mode 100644 index 0a070a838..000000000 --- a/src-testing/src/objects/LineLoop.d.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { Object3DEventMap } from "../core/Object3D.js"; -import { Material } from "../materials/Material.js"; -import { Line } from "./Line.js"; - -/** - * A continuous line that connects back to the start. - * @remarks - * This is nearly the same as {@link THREE.Line | Line}, - * the only difference is that it is rendered using {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements | gl.LINE_LOOP} - * instead of {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements | gl.LINE_STRIP}, - * which draws a straight line to the next vertex, and connects the last vertex back to the first. - * @see {@link https://threejs.org/docs/index.html#api/en/objects/LineLoop | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/LineLoop.js | Source} - */ -export class LineLoop< - TGeometry extends BufferGeometry = BufferGeometry, - TMaterial extends Material | Material[] = Material | Material[], - TEventMap extends Object3DEventMap = Object3DEventMap, -> extends Line { - /** - * Create a new instance of {@link LineLoop} - * @param geometry List of vertices representing points on the line loop. Default {@link THREE.BufferGeometry | `new THREE.BufferGeometry()`}. - * @param material Material for the line. Default {@link THREE.LineBasicMaterial | `new THREE.LineBasicMaterial()`}. - */ - constructor(geometry?: TGeometry, material?: TMaterial); - - /** - * Read-only flag to check if a given object is of type {@link LineLoop}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isLineLoop: true; - - /** - * @override - * @defaultValue `LineLoop` - */ - override readonly type: string | "LineLoop"; -} diff --git a/src-testing/src/objects/LineSegments.d.ts b/src-testing/src/objects/LineSegments.d.ts deleted file mode 100644 index 9a8199bdc..000000000 --- a/src-testing/src/objects/LineSegments.d.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { Object3DEventMap } from "../core/Object3D.js"; -import { Material } from "../materials/Material.js"; -import { Line } from "./Line.js"; - -/** - * A series of lines drawn between pairs of vertices. - * @remarks - * This is nearly the same as {@link THREE.Line | Line}, - * the only difference is that it is rendered using {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements | gl.LINES} - * instead of {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements | gl.LINE_STRIP}. - * @see {@link https://threejs.org/docs/index.html#api/en/objects/LineSegments | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/LineSegments.js | Source} - */ -export class LineSegments< - TGeometry extends BufferGeometry = BufferGeometry, - TMaterial extends Material | Material[] = Material | Material[], - TEventMap extends Object3DEventMap = Object3DEventMap, -> extends Line { - /** - * Create a new instance of {@link LineSegments} - * @param geometry Pair(s) of vertices representing each line segment(s). Default {@link THREE.BufferGeometry | `new THREE.BufferGeometry()`}. - * @param material Material for the line. Default {@link THREE.LineBasicMaterial | `new THREE.LineBasicMaterial()`}. - */ - constructor(geometry?: TGeometry, material?: TMaterial); - - /** - * Read-only flag to check if a given object is of type {@link LineSegments}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isLineSegments: true; - - /** - * A Read-only _string_ to check if `this` object type. - * @remarks Sub-classes will update this value. - * @override - * @defaultValue `LineSegments` - */ - override readonly type: string | "LineSegments"; -} diff --git a/src-testing/src/objects/Mesh.d.ts b/src-testing/src/objects/Mesh.d.ts deleted file mode 100644 index 38dad1c73..000000000 --- a/src-testing/src/objects/Mesh.d.ts +++ /dev/null @@ -1,94 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { JSONMeta, Object3D, Object3DEventMap, Object3DJSON, Object3DJSONObject } from "../core/Object3D.js"; -import { Material } from "../materials/Material.js"; -import { Vector3 } from "../math/Vector3.js"; - -export interface MeshJSONObject extends Object3DJSONObject { - geometry: string; -} - -export interface MeshJSON extends Object3DJSON { - object: MeshJSONObject; -} - -/** - * Class representing triangular {@link https://en.wikipedia.org/wiki/Polygon_mesh | polygon mesh} based objects. - * @remarks - * Also serves as a base for other classes such as {@link THREE.SkinnedMesh | SkinnedMesh}, {@link THREE.InstancedMesh | InstancedMesh}. - * @example - * ```typescript - * const geometry = new THREE.BoxGeometry(1, 1, 1); - * const material = new THREE.MeshBasicMaterial({ - * color: 0xffff00 - * }); - * const {@link Mesh} = new THREE.Mesh(geometry, material); - * scene.add(mesh); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/objects/Mesh | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Mesh.js | Source} - */ -export class Mesh< - TGeometry extends BufferGeometry = BufferGeometry, - TMaterial extends Material | Material[] = Material | Material[], - TEventMap extends Object3DEventMap = Object3DEventMap, -> extends Object3D { - /** - * Create a new instance of {@link Mesh} - * @param geometry An instance of {@link THREE.BufferGeometry | BufferGeometry}. Default {@link THREE.BufferGeometry | `new THREE.BufferGeometry()`}. - * @param material A single or an array of {@link THREE.Material | Material}. Default {@link THREE.MeshBasicMaterial | `new THREE.MeshBasicMaterial()`}. - */ - constructor(geometry?: TGeometry, material?: TMaterial); - - /** - * Read-only flag to check if a given object is of type {@link Mesh}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isMesh: true; - - /** - * @override - * @defaultValue `Mesh` - */ - override readonly type: string | "Mesh"; - - /** - * An instance of {@link THREE.BufferGeometry | BufferGeometry} (or derived classes), defining the object's structure. - * @defaultValue {@link THREE.BufferGeometry | `new THREE.BufferGeometry()`}. - */ - geometry: TGeometry; - - /** - * An instance of material derived from the {@link THREE.Material | Material} base class or an array of materials, defining the object's appearance. - * @defaultValue {@link THREE.MeshBasicMaterial | `new THREE.MeshBasicMaterial()`}. - */ - material: TMaterial; - - /** - * An array of weights typically from `0-1` that specify how much of the morph is applied. - * @defaultValue `undefined`, _but reset to a blank array by {@link updateMorphTargets | .updateMorphTargets()}._ - */ - morphTargetInfluences?: number[] | undefined; - - /** - * A dictionary of morphTargets based on the `morphTarget.name` property. - * @defaultValue `undefined`, _but rebuilt by {@link updateMorphTargets | .updateMorphTargets()}._ - */ - morphTargetDictionary?: { [key: string]: number } | undefined; - - /** - * Updates the morphTargets to have no influence on the object - * @remarks Resets the {@link morphTargetInfluences} and {@link morphTargetDictionary} properties. - */ - updateMorphTargets(): void; - - /** - * Get the local-space position of the vertex at the given index, - * taking into account the current animation state of both morph targets and skinning. - * @param index Expects a `Integer` - * @param target - */ - getVertexPosition(index: number, target: Vector3): Vector3; - - toJSON(meta?: JSONMeta): MeshJSON; -} diff --git a/src-testing/src/objects/Points.d.ts b/src-testing/src/objects/Points.d.ts deleted file mode 100644 index 3cba2c74b..000000000 --- a/src-testing/src/objects/Points.d.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { BufferGeometry, NormalOrGLBufferAttributes } from "../core/BufferGeometry.js"; -import { Object3D, Object3DEventMap } from "../core/Object3D.js"; -import { Material } from "../materials/Material.js"; - -/** - * A class for displaying {@link Points} - * @remarks - * The {@link Points} are rendered by the {@link THREE.WebGLRenderer | WebGLRenderer} using {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements | gl.POINTS}. - * @see {@link https://threejs.org/docs/index.html#api/en/objects/Points | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Points.js | Source} - */ -export class Points< - TGeometry extends BufferGeometry = BufferGeometry, - TMaterial extends Material | Material[] = Material | Material[], - TEventMap extends Object3DEventMap = Object3DEventMap, -> extends Object3D { - /** - * Create a new instance of {@link Points} - * @param geometry An instance of {@link THREE.BufferGeometry | BufferGeometry}. Default {@link THREE.BufferGeometry | `new THREE.BufferGeometry()`}. - * @param material A single or an array of {@link THREE.Material | Material}. Default {@link THREE.PointsMaterial | `new THREE.PointsMaterial()`}. - */ - constructor(geometry?: TGeometry, material?: TMaterial); - - /** - * Read-only flag to check if a given object is of type {@link Points}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isPoints: true; - - /** - * @override - * @defaultValue `Points` - */ - override readonly type: string | "Points"; - - /** - * An array of weights typically from `0-1` that specify how much of the morph is applied. - * @defaultValue `undefined`, _but reset to a blank array by {@link updateMorphTargets | .updateMorphTargets()}._ - */ - morphTargetInfluences?: number[] | undefined; - - /** - * A dictionary of morphTargets based on the `morphTarget.name` property. - * @defaultValue `undefined`, _but rebuilt by {@link updateMorphTargets | .updateMorphTargets()}._ - */ - morphTargetDictionary?: { [key: string]: number } | undefined; - - /** - * An instance of {@link THREE.BufferGeometry | BufferGeometry} (or derived classes), defining the object's structure. - * @remarks each vertex designates the position of a particle in the system. - */ - geometry: TGeometry; - - /** - * An instance of {@link THREE.Material | Material}, defining the object's appearance. - * @defaultValue {@link THREE.PointsMaterial | `new THREE.PointsMaterial()`}, _with randomised colour_. - */ - material: TMaterial; - - /** - * Updates the morphTargets to have no influence on the object - * @remarks Resets the {@link morphTargetInfluences} and {@link morphTargetDictionary} properties. - */ - updateMorphTargets(): void; -} diff --git a/src-testing/src/objects/Skeleton.d.ts b/src-testing/src/objects/Skeleton.d.ts deleted file mode 100644 index aaeb3198b..000000000 --- a/src-testing/src/objects/Skeleton.d.ts +++ /dev/null @@ -1,120 +0,0 @@ -import { Matrix4, Matrix4Tuple } from "../math/Matrix4.js"; -import { DataTexture } from "../textures/DataTexture.js"; -import { Bone } from "./Bone.js"; - -export interface SkeletonJSON { - metadata: { version: number; type: string; generator: string }; - bones: string[]; - boneInverses: Matrix4Tuple[]; - uuid: string; -} - -/** - * Use an array of {@link Bone | bones} to create a {@link Skeleton} that can be used by a {@link THREE.SkinnedMesh | SkinnedMesh}. - * @example - * ```typescript - * // Create a simple "arm" - * const bones = []; - * const shoulder = new THREE.Bone(); - * const elbow = new THREE.Bone(); - * const hand = new THREE.Bone(); - * shoulder.add(elbow); - * elbow.add(hand); - * bones.push(shoulder); - * bones.push(elbow); - * bones.push(hand); - * shoulder.position.y = -5; - * elbow.position.y = 0; - * hand.position.y = 5; - * const armSkeleton = new THREE.Skeleton(bones); - * See the[page: SkinnedMesh] page - * for an example of usage with standard[page: BufferGeometry]. - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/objects/Skeleton | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Skeleton.js | Source} - */ -export class Skeleton { - /** - * Creates a new Skeleton. - * @param bones The array of {@link THREE.Bone | bones}. Default `[]`. - * @param boneInverses An array of {@link THREE.Matrix4 | Matrix4s}. Default `[]`. - */ - constructor(bones?: Bone[], boneInverses?: Matrix4[]); - - /** - * {@link http://en.wikipedia.org/wiki/Universally_unique_identifier | UUID} of this object instance. - * @remarks This gets automatically assigned and shouldn't be edited. - */ - uuid: string; - - /** - * The array of {@link THREE.Bone | Bones}. - * @remarks Note this is a copy of the original array, not a reference, so you can modify the original array without effecting this one. - */ - bones: Bone[]; - - /** - * An array of {@link Matrix4 | Matrix4s} that represent the inverse of the {@link THREE.Matrix4 | matrixWorld} of the individual bones. - */ - boneInverses: Matrix4[]; - - /** - * The array buffer holding the bone data when using a vertex texture. - */ - boneMatrices: Float32Array; - - /** - * The {@link THREE.DataTexture | DataTexture} holding the bone data when using a vertex texture. - */ - boneTexture: null | DataTexture; - - frame: number; - - init(): void; - - /** - * Generates the {@link boneInverses} array if not provided in the constructor. - */ - calculateInverses(): void; - - /** - * Computes an instance of {@link THREE.DataTexture | DataTexture} in order to pass the bone data more efficiently to the shader - * @remarks - * The texture is assigned to {@link boneTexture}. - */ - computeBoneTexture(): this; - - /** - * Returns the skeleton to the base pose. - */ - pose(): void; - - /** - * Updates the {@link boneMatrices} and {@link boneTexture} after changing the bones - * @remarks - * This is called automatically by the {@link THREE.WebGLRenderer | WebGLRenderer} if the {@link Skeleton} is used with a {@link THREE.SkinnedMesh | SkinnedMesh}. - */ - update(): void; - - /** - * Returns a clone of this {@link Skeleton} object. - */ - clone(): Skeleton; - - /** - * Searches through the skeleton's bone array and returns the first with a matching name. - * @param name String to match to the Bone's {@link THREE.Bone.name | .name} property. - */ - getBoneByName(name: string): undefined | Bone; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks - * Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; - - toJSON(): SkeletonJSON; - - fromJSON(json: SkeletonJSON, bones: Record): void; -} diff --git a/src-testing/src/objects/SkinnedMesh.d.ts b/src-testing/src/objects/SkinnedMesh.d.ts deleted file mode 100644 index 35149c5d1..000000000 --- a/src-testing/src/objects/SkinnedMesh.d.ts +++ /dev/null @@ -1,160 +0,0 @@ -import { BindMode } from "../constants.js"; -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { JSONMeta, Object3DEventMap } from "../core/Object3D.js"; -import { Material } from "../materials/Material.js"; -import { Box3 } from "../math/Box3.js"; -import { Matrix4, Matrix4Tuple } from "../math/Matrix4.js"; -import { Sphere } from "../math/Sphere.js"; -import { Vector3 } from "../math/Vector3.js"; -import { Mesh, MeshJSON, MeshJSONObject } from "./Mesh.js"; -import { Skeleton } from "./Skeleton.js"; - -export interface SkinnedMeshJSONObject extends MeshJSONObject { - bindMode: BindMode; - bindMatrix: Matrix4Tuple; - skeleton?: string; -} - -export interface SkinnedMeshJSON extends MeshJSON { - object: SkinnedMeshJSONObject; -} - -/** - * A mesh that has a {@link THREE.Skeleton | Skeleton} with {@link Bone | bones} that can then be used to animate the vertices of the geometry. - * @example - * ```typescript - * const geometry = new THREE.CylinderGeometry(5, 5, 5, 5, 15, 5, 30); - * // create the skin indices and skin weights manually - * // (typically a loader would read this data from a 3D model for you) - * const position = geometry.attributes.position; - * const vertex = new THREE.Vector3(); - * const skinIndices = []; - * const skinWeights = []; - * for (let i = 0; i & lt; position.count; i++) { - * vertex.fromBufferAttribute(position, i); - * // compute skinIndex and skinWeight based on some configuration data - * const y = (vertex.y + sizing.halfHeight); - * const skinIndex = Math.floor(y / sizing.segmentHeight); - * const skinWeight = (y % sizing.segmentHeight) / sizing.segmentHeight; - * skinIndices.push(skinIndex, skinIndex + 1, 0, 0); - * skinWeights.push(1 - skinWeight, skinWeight, 0, 0); - * } - * geometry.setAttribute('skinIndex', new THREE.Uint16BufferAttribute(skinIndices, 4)); - * geometry.setAttribute('skinWeight', new THREE.Float32BufferAttribute(skinWeights, 4)); - * // create skinned mesh and skeleton - * const mesh = new THREE.SkinnedMesh(geometry, material); - * const skeleton = new THREE.Skeleton(bones); - * // see example from THREE.Skeleton - * const rootBone = skeleton.bones[0]; - * mesh.add(rootBone); - * // bind the skeleton to the mesh - * mesh.bind(skeleton); - * // move the bones and manipulate the model - * skeleton.bones[0].rotation.x = -0.1; - * skeleton.bones[1].rotation.x = 0.2; - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/objects/SkinnedMesh | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/SkinnedMesh.js | Source} - */ -export class SkinnedMesh< - TGeometry extends BufferGeometry = BufferGeometry, - TMaterial extends Material | Material[] = Material | Material[], - TEventMap extends Object3DEventMap = Object3DEventMap, -> extends Mesh { - /** - * Create a new instance of {@link SkinnedMesh} - * @param geometry An instance of {@link THREE.BufferGeometry | BufferGeometry}. Default {@link THREE.BufferGeometry | `new THREE.BufferGeometry()`}. - * @param material A single or an array of {@link THREE.Material | Material}. Default {@link THREE.MeshBasicMaterial | `new THREE.MeshBasicMaterial()`}. - */ - constructor(geometry?: TGeometry, material?: TMaterial, useVertexTexture?: boolean); - - /** - * Read-only flag to check if a given object is of type {@link SkinnedMesh}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isSkinnedMesh: true; - - /** - * @override - * @defaultValue `SkinnedMesh` - */ - override readonly type: string | "SkinnedMesh"; - - /** - * Either {@link AttachedBindMode} or {@link DetachedBindMode}. {@link AttachedBindMode} means the skinned mesh - * shares the same world space as the skeleton. This is not true when using {@link DetachedBindMode} which is useful - * when sharing a skeleton across multiple skinned meshes. - * @defaultValue `AttachedBindMode` - */ - bindMode: BindMode; - - /** - * The base matrix that is used for the bound bone transforms. - */ - bindMatrix: Matrix4; - /** - * The base matrix that is used for resetting the bound bone transforms. - */ - bindMatrixInverse: Matrix4; - - /** - * The bounding box of the SkinnedMesh. Can be calculated with {@link computeBoundingBox | .computeBoundingBox()}. - * @default `null` - */ - boundingBox: Box3; - - /** - * The bounding box of the SkinnedMesh. Can be calculated with {@link computeBoundingSphere | .computeBoundingSphere()}. - * @default `null` - */ - boundingSphere: Sphere; - - /** - * {@link THREE.Skeleton | Skeleton} representing the bone hierarchy of the skinned mesh. - */ - skeleton: Skeleton; - - /** - * Bind a skeleton to the skinned mesh - * @remarks - * The bindMatrix gets saved to .bindMatrix property and the .bindMatrixInverse gets calculated. - * @param skeleton {@link THREE.Skeleton | Skeleton} created from a {@link Bone | Bones} tree. - * @param bindMatrix {@link THREE.Matrix4 | Matrix4} that represents the base transform of the skeleton. - */ - bind(skeleton: Skeleton, bindMatrix?: Matrix4): void; - - /** - * Computes the bounding box of the skinned mesh, and updates the {@link .boundingBox} attribute. The bounding box - * is not computed by the engine; it must be computed by your app. If the skinned mesh is animated, the bounding box - * should be recomputed per frame. - */ - computeBoundingBox(): void; - - /** - * Computes the bounding sphere of the skinned mesh, and updates the {@link .boundingSphere} attribute. The bounding - * sphere is automatically computed by the engine when it is needed, e.g., for ray casting and view frustum culling. - * If the skinned mesh is animated, the bounding sphere should be recomputed per frame. - */ - computeBoundingSphere(): void; - - /** - * This method sets the skinned mesh in the rest pose (resets the pose). - */ - pose(): void; - - /** - * Normalizes the skin weights. - */ - normalizeSkinWeights(): void; - - /** - * Applies the bone transform associated with the given index to the given position vector - * @remarks Returns the updated vector. - * @param index Expects a `Integer` - * @param vector - */ - applyBoneTransform(index: number, vector: Vector3): Vector3; - - toJSON(meta?: JSONMeta): SkinnedMeshJSON; -} diff --git a/src-testing/src/objects/Sprite.d.ts b/src-testing/src/objects/Sprite.d.ts deleted file mode 100644 index 427b8b414..000000000 --- a/src-testing/src/objects/Sprite.d.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { Object3D, Object3DEventMap } from "../core/Object3D.js"; -import { SpriteMaterial } from "../materials/Materials.js"; -import { Vector2 } from "../math/Vector2.js"; - -/** - * A {@link Sprite} is a plane that always faces towards the camera, generally with a partially transparent texture applied. - * @remarks Sprites do not cast shadows, setting `castShadow = true` will have no effect. - * @example - * ```typescript - * const map = new THREE.TextureLoader().load('sprite.png'); - * const material = new THREE.SpriteMaterial({ - * map: map - * }); - * const {@link Sprite} = new THREE.Sprite(material); - * scene.add(sprite); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/objects/Sprite | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Sprite.js | Source} - */ -export class Sprite extends Object3D { - /** - * Creates a new Sprite. - * @param material An instance of {@link THREE.SpriteMaterial | SpriteMaterial}. Default {@link THREE.SpriteMaterial | `new SpriteMaterial()`}, _with white color_. - */ - constructor(material?: SpriteMaterial); - - /** - * Read-only flag to check if a given object is of type {@link Sprite}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isSprite: true; - - /** - * @override - * @defaultValue `Sprite` - */ - override readonly type: string | "Sprite"; - - /** - * Whether the object gets rendered into shadow map. - * No effect in {@link Sprite}. - * @ignore - * @hidden - * @defaultValue `false` - */ - override castShadow: false; - - geometry: BufferGeometry; - - /** - * An instance of {@link THREE.SpriteMaterial | SpriteMaterial}, defining the object's appearance. - * @defaultValue {@link THREE.SpriteMaterial | `new SpriteMaterial()`}, _with white color_. - */ - material: SpriteMaterial; - - /** - * The sprite's anchor point, and the point around which the {@link Sprite} rotates. - * A value of (0.5, 0.5) corresponds to the midpoint of the sprite. - * A value of (0, 0) corresponds to the lower left corner of the sprite. - * @defaultValue {@link THREE.Vector2 | `new Vector2(0.5, 0.5)`}. - */ - center: Vector2; -} diff --git a/src-testing/src/renderers/WebGL3DRenderTarget.d.ts b/src-testing/src/renderers/WebGL3DRenderTarget.d.ts deleted file mode 100644 index 420caa97e..000000000 --- a/src-testing/src/renderers/WebGL3DRenderTarget.d.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { RenderTargetOptions } from "../core/RenderTarget.js"; -import { Data3DTexture } from "../textures/Data3DTexture.js"; -import { WebGLRenderTarget } from "./WebGLRenderTarget.js"; - -/** - * Represents a three-dimensional render target. - */ -export class WebGL3DRenderTarget extends WebGLRenderTarget { - /** - * Creates a new WebGL3DRenderTarget. - * - * @param width the width of the render target, in pixels. Default is `1`. - * @param height the height of the render target, in pixels. Default is `1`. - * @param depth the depth of the render target. Default is `1`. - * @param options optional object that holds texture parameters for an auto-generated target texture and - * depthBuffer/stencilBuffer booleans. See {@link WebGLRenderTarget} for details. - */ - constructor(width?: number, height?: number, depth?: number, options?: RenderTargetOptions); - - textures: Data3DTexture[]; - - /** - * The texture property is overwritten with an instance of {@link Data3DTexture}. - */ - get texture(): Data3DTexture; - set texture(value: Data3DTexture); - - readonly isWebGL3DRenderTarget: true; -} diff --git a/src-testing/src/renderers/WebGLArrayRenderTarget.d.ts b/src-testing/src/renderers/WebGLArrayRenderTarget.d.ts deleted file mode 100644 index 1ac617889..000000000 --- a/src-testing/src/renderers/WebGLArrayRenderTarget.d.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { RenderTargetOptions } from "../core/RenderTarget.js"; -import { DataArrayTexture } from "../textures/DataArrayTexture.js"; -import { WebGLRenderTarget } from "./WebGLRenderTarget.js"; - -/** - * This type of render target represents an array of textures. - */ -export class WebGLArrayRenderTarget extends WebGLRenderTarget { - /** - * Creates a new WebGLArrayRenderTarget. - * - * @param width the width of the render target, in pixels. Default is `1`. - * @param height the height of the render target, in pixels. Default is `1`. - * @param depth the depth/layer count of the render target. Default is `1`. - * @param options optional object that holds texture parameters for an auto-generated target texture and - * depthBuffer/stencilBuffer booleans. See {@link WebGLRenderTarget} for details. - */ - constructor(width?: number, height?: number, depth?: number, options?: RenderTargetOptions); - - textures: DataArrayTexture[]; - - /** - * The texture property is overwritten with an instance of {@link DataArrayTexture}. - */ - get texture(): DataArrayTexture; - set texture(value: DataArrayTexture); - - readonly isWebGLArrayRenderTarget: true; -} diff --git a/src-testing/src/renderers/WebGLCubeRenderTarget.d.ts b/src-testing/src/renderers/WebGLCubeRenderTarget.d.ts deleted file mode 100644 index c390adb67..000000000 --- a/src-testing/src/renderers/WebGLCubeRenderTarget.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { RenderTargetOptions } from "../core/RenderTarget.js"; -import { CubeTexture } from "../textures/CubeTexture.js"; -import { Texture } from "../textures/Texture.js"; -import { WebGLRenderer } from "./WebGLRenderer.js"; -import { WebGLRenderTarget } from "./WebGLRenderTarget.js"; - -export class WebGLCubeRenderTarget extends WebGLRenderTarget { - constructor(size?: number, options?: RenderTargetOptions); - - textures: CubeTexture[]; - - get texture(): CubeTexture; - set texture(value: CubeTexture); - - fromEquirectangularTexture(renderer: WebGLRenderer, texture: Texture): this; - - clear(renderer: WebGLRenderer, color: boolean, depth: boolean, stencil: boolean): void; -} diff --git a/src-testing/src/renderers/WebGLRenderTarget.d.ts b/src-testing/src/renderers/WebGLRenderTarget.d.ts deleted file mode 100644 index fdff12e78..000000000 --- a/src-testing/src/renderers/WebGLRenderTarget.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { RenderTarget, RenderTargetOptions } from "../core/RenderTarget.js"; -import { Texture } from "../textures/Texture.js"; - -export class WebGLRenderTarget extends RenderTarget { - constructor(width?: number, height?: number, options?: RenderTargetOptions); - - readonly isWebGLRenderTarget: true; -} diff --git a/src-testing/src/renderers/WebGLRenderer.d.ts b/src-testing/src/renderers/WebGLRenderer.d.ts deleted file mode 100644 index b53a9d3c3..000000000 --- a/src-testing/src/renderers/WebGLRenderer.d.ts +++ /dev/null @@ -1,562 +0,0 @@ -import { Camera } from "../cameras/Camera.js"; -import { ColorSpace, CullFace, ShadowMapType, ToneMapping, WebGLCoordinateSystem } from "../constants.js"; -import { TypedArray } from "../core/BufferAttribute.js"; -import { BufferGeometry } from "../core/BufferGeometry.js"; -import { Object3D } from "../core/Object3D.js"; -import { Material } from "../materials/Material.js"; -import { Box2 } from "../math/Box2.js"; -import { Box3 } from "../math/Box3.js"; -import { Color, ColorRepresentation } from "../math/Color.js"; -import { Plane } from "../math/Plane.js"; -import { Vector2 } from "../math/Vector2.js"; -import { Vector3 } from "../math/Vector3.js"; -import { Vector4 } from "../math/Vector4.js"; -import { Scene } from "../scenes/Scene.js"; -import { Data3DTexture } from "../textures/Data3DTexture.js"; -import { DataArrayTexture } from "../textures/DataArrayTexture.js"; -import { OffscreenCanvas, Texture } from "../textures/Texture.js"; -import { WebGLCapabilities } from "./webgl/WebGLCapabilities.js"; -import { WebGLExtensions } from "./webgl/WebGLExtensions.js"; -import { WebGLInfo } from "./webgl/WebGLInfo.js"; -import { WebGLProgram } from "./webgl/WebGLProgram.js"; -import { WebGLProperties } from "./webgl/WebGLProperties.js"; -import { WebGLRenderLists } from "./webgl/WebGLRenderLists.js"; -import { WebGLShadowMap } from "./webgl/WebGLShadowMap.js"; -import { WebGLState } from "./webgl/WebGLState.js"; -import { WebGLRenderTarget } from "./WebGLRenderTarget.js"; -import { WebXRManager } from "./webxr/WebXRManager.js"; - -export interface Renderer { - domElement: HTMLCanvasElement; - - render(scene: Object3D, camera: Camera): void; - setSize(width: number, height: number, updateStyle?: boolean): void; -} - -export interface WebGLRendererParameters { - /** - * A Canvas where the renderer draws its output. - */ - canvas?: HTMLCanvasElement | OffscreenCanvas | undefined; - - /** - * A WebGL Rendering Context. - * (https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext) - * Default is null - */ - context?: WebGLRenderingContext | undefined; - - /** - * shader precision. Can be "highp", "mediump" or "lowp". - */ - precision?: string | undefined; - - /** - * default is false. - */ - alpha?: boolean | undefined; - - /** - * default is true. - */ - premultipliedAlpha?: boolean | undefined; - - /** - * default is false. - */ - antialias?: boolean | undefined; - - /** - * default is false. - */ - stencil?: boolean | undefined; - - /** - * default is false. - */ - preserveDrawingBuffer?: boolean | undefined; - - /** - * Can be "high-performance", "low-power" or "default" - */ - powerPreference?: string | undefined; - - /** - * default is true. - */ - depth?: boolean | undefined; - - /** - * default is false. - */ - logarithmicDepthBuffer?: boolean | undefined; - - /** - * default is false. - */ - failIfMajorPerformanceCaveat?: boolean | undefined; -} - -export interface WebGLDebug { - /** - * Enables error checking and reporting when shader programs are being compiled. - */ - checkShaderErrors: boolean; - - /** - * A callback function that can be used for custom error reporting. The callback receives the WebGL context, an - * instance of WebGLProgram as well two instances of WebGLShader representing the vertex and fragment shader. - * Assigning a custom function disables the default error reporting. - * @default `null` - */ - onShaderError: - | (( - gl: WebGLRenderingContext, - program: WebGLProgram, - glVertexShader: WebGLShader, - glFragmentShader: WebGLShader, - ) => void) - | null; -} - -/** - * The WebGL renderer displays your beautifully crafted scenes using WebGL, if your device supports it. - * This renderer has way better performance than CanvasRenderer. - * - * see {@link https://github.com/mrdoob/three.js/blob/master/src/renderers/WebGLRenderer.js|src/renderers/WebGLRenderer.js} - */ -export class WebGLRenderer implements Renderer { - /** - * parameters is an optional object with properties defining the renderer's behavior. - * The constructor also accepts no parameters at all. - * In all cases, it will assume sane defaults when parameters are missing. - */ - constructor(parameters?: WebGLRendererParameters); - - /** - * A Canvas where the renderer draws its output. - * This is automatically created by the renderer in the constructor (if not provided already); you just need to add it to your page. - * @default document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' ) - */ - domElement: HTMLCanvasElement; - - /** - * Defines whether the renderer should automatically clear its output before rendering. - * @default true - */ - autoClear: boolean; - - /** - * If autoClear is true, defines whether the renderer should clear the color buffer. Default is true. - * @default true - */ - autoClearColor: boolean; - - /** - * If autoClear is true, defines whether the renderer should clear the depth buffer. Default is true. - * @default true - */ - autoClearDepth: boolean; - - /** - * If autoClear is true, defines whether the renderer should clear the stencil buffer. Default is true. - * @default true - */ - autoClearStencil: boolean; - - /** - * Debug configurations. - * @default { checkShaderErrors: true } - */ - debug: WebGLDebug; - - /** - * Defines whether the renderer should sort objects. Default is true. - * @default true - */ - sortObjects: boolean; - - /** - * @default [] - */ - clippingPlanes: Plane[]; - - /** - * @default false - */ - localClippingEnabled: boolean; - - extensions: WebGLExtensions; - - /** - * Color space used for output to HTMLCanvasElement. Supported values are - * {@link SRGBColorSpace} and {@link LinearSRGBColorSpace}. - * @default THREE.SRGBColorSpace. - */ - get outputColorSpace(): ColorSpace; - set outputColorSpace(colorSpace: ColorSpace); - - get coordinateSystem(): typeof WebGLCoordinateSystem; - - /** - * @default THREE.NoToneMapping - */ - toneMapping: ToneMapping; - - /** - * @default 1 - */ - toneMappingExposure: number; - - info: WebGLInfo; - - shadowMap: WebGLShadowMap; - - pixelRatio: number; - - capabilities: WebGLCapabilities; - properties: WebGLProperties; - renderLists: WebGLRenderLists; - state: WebGLState; - - xr: WebXRManager; - - /** - * Return the WebGL context. - */ - getContext(): WebGLRenderingContext | WebGL2RenderingContext; - getContextAttributes(): any; - forceContextLoss(): void; - forceContextRestore(): void; - - /** - * @deprecated Use {@link WebGLCapabilities#getMaxAnisotropy .capabilities.getMaxAnisotropy()} instead. - */ - getMaxAnisotropy(): number; - - /** - * @deprecated Use {@link WebGLCapabilities#precision .capabilities.precision} instead. - */ - getPrecision(): string; - - getPixelRatio(): number; - setPixelRatio(value: number): void; - - getDrawingBufferSize(target: Vector2): Vector2; - setDrawingBufferSize(width: number, height: number, pixelRatio: number): void; - - getSize(target: Vector2): Vector2; - - /** - * Resizes the output canvas to (width, height), and also sets the viewport to fit that size, starting in (0, 0). - */ - setSize(width: number, height: number, updateStyle?: boolean): void; - - getCurrentViewport(target: Vector4): Vector4; - - /** - * Copies the viewport into target. - */ - getViewport(target: Vector4): Vector4; - - /** - * Sets the viewport to render from (x, y) to (x + width, y + height). - * (x, y) is the lower-left corner of the region. - */ - setViewport(x: Vector4 | number, y?: number, width?: number, height?: number): void; - - /** - * Copies the scissor area into target. - */ - getScissor(target: Vector4): Vector4; - - /** - * Sets the scissor area from (x, y) to (x + width, y + height). - */ - setScissor(x: Vector4 | number, y?: number, width?: number, height?: number): void; - - /** - * Returns true if scissor test is enabled; returns false otherwise. - */ - getScissorTest(): boolean; - - /** - * Enable the scissor test. When this is enabled, only the pixels within the defined scissor area will be affected by further renderer actions. - */ - setScissorTest(enable: boolean): void; - - /** - * Sets the custom opaque sort function for the WebGLRenderLists. Pass null to use the default painterSortStable function. - */ - setOpaqueSort(method: (a: any, b: any) => number): void; - - /** - * Sets the custom transparent sort function for the WebGLRenderLists. Pass null to use the default reversePainterSortStable function. - */ - setTransparentSort(method: (a: any, b: any) => number): void; - - /** - * Returns a THREE.Color instance with the current clear color. - */ - getClearColor(target: Color): Color; - - /** - * Sets the clear color, using color for the color and alpha for the opacity. - */ - setClearColor(color: ColorRepresentation, alpha?: number): void; - - /** - * Returns a float with the current clear alpha. Ranges from 0 to 1. - */ - getClearAlpha(): number; - - setClearAlpha(alpha: number): void; - - /** - * Tells the renderer to clear its color, depth or stencil drawing buffer(s). - * Arguments default to true - */ - clear(color?: boolean, depth?: boolean, stencil?: boolean): void; - - clearColor(): void; - clearDepth(): void; - clearStencil(): void; - clearTarget(renderTarget: WebGLRenderTarget, color: boolean, depth: boolean, stencil: boolean): void; - - /** - * @deprecated Use {@link WebGLState#reset .state.reset()} instead. - */ - resetGLState(): void; - dispose(): void; - - renderBufferDirect( - camera: Camera, - scene: Scene, - geometry: BufferGeometry, - material: Material, - object: Object3D, - geometryGroup: any, - ): void; - - /** - * A build in function that can be used instead of requestAnimationFrame. For WebXR projects this function must be used. - * @param callback The function will be called every available frame. If `null` is passed it will stop any already ongoing animation. - */ - setAnimationLoop(callback: XRFrameRequestCallback | null): void; - - /** - * @deprecated Use {@link WebGLRenderer#setAnimationLoop .setAnimationLoop()} instead. - */ - animate(callback: () => void): void; - - /** - * Compiles all materials in the scene with the camera. This is useful to precompile shaders before the first - * rendering. If you want to add a 3D object to an existing scene, use the third optional parameter for applying the - * target scene. - * Note that the (target) scene's lighting should be configured before calling this method. - */ - compile: (scene: Object3D, camera: Camera, targetScene?: Scene | null) => Set; - - /** - * Asynchronous version of {@link compile}(). The method returns a Promise that resolves when the given scene can be - * rendered without unnecessary stalling due to shader compilation. - * This method makes use of the KHR_parallel_shader_compile WebGL extension. - */ - compileAsync: (scene: Object3D, camera: Camera, targetScene?: Scene | null) => Promise; - - /** - * Render a scene or an object using a camera. - * The render is done to a previously specified {@link WebGLRenderTarget#renderTarget .renderTarget} set by calling - * {@link WebGLRenderer#setRenderTarget .setRenderTarget} or to the canvas as usual. - * - * By default render buffers are cleared before rendering but you can prevent this by setting the property - * {@link WebGLRenderer#autoClear autoClear} to false. If you want to prevent only certain buffers being cleared - * you can set either the {@link WebGLRenderer#autoClearColor autoClearColor}, - * {@link WebGLRenderer#autoClearStencil autoClearStencil} or {@link WebGLRenderer#autoClearDepth autoClearDepth} - * properties to false. To forcibly clear one ore more buffers call {@link WebGLRenderer#clear .clear}. - */ - render(scene: Object3D, camera: Camera): void; - - /** - * Returns the current active cube face. - */ - getActiveCubeFace(): number; - - /** - * Returns the current active mipmap level. - */ - getActiveMipmapLevel(): number; - - /** - * Returns the current render target. If no render target is set, null is returned. - */ - getRenderTarget(): WebGLRenderTarget | null; - - /** - * @deprecated Use {@link WebGLRenderer#getRenderTarget .getRenderTarget()} instead. - */ - getCurrentRenderTarget(): WebGLRenderTarget | null; - - /** - * Sets the active render target. - * - * @param renderTarget The {@link WebGLRenderTarget renderTarget} that needs to be activated. When `null` is given, the canvas is set as the active render target instead. - * @param activeCubeFace Specifies the active cube side (PX 0, NX 1, PY 2, NY 3, PZ 4, NZ 5) of {@link WebGLCubeRenderTarget}. - * @param activeMipmapLevel Specifies the active mipmap level. - */ - setRenderTarget( - renderTarget: WebGLRenderTarget | WebGLRenderTarget | null, - activeCubeFace?: number, - activeMipmapLevel?: number, - ): void; - - readRenderTargetPixels( - renderTarget: WebGLRenderTarget | WebGLRenderTarget, - x: number, - y: number, - width: number, - height: number, - buffer: TypedArray, - activeCubeFaceIndex?: number, - ): void; - - readRenderTargetPixelsAsync( - renderTarget: WebGLRenderTarget | WebGLRenderTarget, - x: number, - y: number, - width: number, - height: number, - buffer: TypedArray, - activeCubeFaceIndex?: number, - ): Promise; - - /** - * Copies a region of the currently bound framebuffer into the selected mipmap level of the selected texture. - * This region is defined by the size of the destination texture's mip level, offset by the input position. - * - * @param texture Specifies the destination texture. - * @param position Specifies the pixel offset from which to copy out of the framebuffer. - * @param level Specifies the destination mipmap level of the texture. - */ - copyFramebufferToTexture(texture: Texture, position?: Vector2 | null, level?: number): void; - - /** - * Copies the pixels of a texture in the bounds `srcRegion` in the destination texture starting from the given - * position. - * - * @param srcTexture Specifies the source texture. - * @param dstTexture Specifies the destination texture. - * @param srcRegion Specifies the bounds - * @param dstPosition Specifies the pixel offset into the dstTexture where the copy will occur. - * @param level Specifies the destination mipmap level of the texture. - */ - copyTextureToTexture( - srcTexture: Texture, - dstTexture: Texture, - srcRegion?: Box2 | null, - dstPosition?: Vector2 | null, - level?: number, - ): void; - - /** - * Copies the pixels of a texture in the bounds `srcRegion` in the destination texture starting from the given - * position. - * - * @param srcTexture Specifies the source texture. - * @param dstTexture Specifies the destination texture. - * @param srcRegion Specifies the bounds - * @param dstPosition Specifies the pixel offset into the dstTexture where the copy will occur. - * @param level Specifies the destination mipmap level of the texture. - */ - copyTextureToTexture3D( - srcTexture: Texture, - dstTexture: Data3DTexture | DataArrayTexture, - srcRegion?: Box3 | null, - dstPosition?: Vector3 | null, - level?: number, - ): void; - - /** - * Initializes the given WebGLRenderTarget memory. Useful for initializing a render target so data can be copied - * into it using {@link WebGLRenderer.copyTextureToTexture} before it has been rendered to. - * @param target - */ - initRenderTarget(target: WebGLRenderTarget): void; - - /** - * Initializes the given texture. Can be used to preload a texture rather than waiting until first render (which can cause noticeable lags due to decode and GPU upload overhead). - * - * @param texture The texture to Initialize. - */ - initTexture(texture: Texture): void; - - /** - * Can be used to reset the internal WebGL state. - */ - resetState(): void; - - /** - * @deprecated Use {@link WebGLRenderer#xr .xr} instead. - */ - vr: boolean; - - /** - * @deprecated Use {@link WebGLShadowMap#enabled .shadowMap.enabled} instead. - */ - shadowMapEnabled: boolean; - - /** - * @deprecated Use {@link WebGLShadowMap#type .shadowMap.type} instead. - */ - shadowMapType: ShadowMapType; - - /** - * @deprecated Use {@link WebGLShadowMap#cullFace .shadowMap.cullFace} instead. - */ - shadowMapCullFace: CullFace; - - /** - * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'OES_texture_float' )} instead. - */ - supportsFloatTextures(): any; - - /** - * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'OES_texture_half_float' )} instead. - */ - supportsHalfFloatTextures(): any; - - /** - * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'OES_standard_derivatives' )} instead. - */ - supportsStandardDerivatives(): any; - - /** - * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'WEBGL_compressed_texture_s3tc' )} instead. - */ - supportsCompressedTextureS3TC(): any; - - /** - * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'WEBGL_compressed_texture_pvrtc' )} instead. - */ - supportsCompressedTexturePVRTC(): any; - - /** - * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'EXT_blend_minmax' )} instead. - */ - supportsBlendMinMax(): any; - - /** - * @deprecated Use {@link WebGLCapabilities#vertexTextures .capabilities.vertexTextures} instead. - */ - supportsVertexTextures(): any; - - /** - * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'ANGLE_instanced_arrays' )} instead. - */ - supportsInstancedArrays(): any; - - /** - * @deprecated Use {@link WebGLRenderer#setScissorTest .setScissorTest()} instead. - */ - enableScissorTest(boolean: any): any; -} diff --git a/src-testing/src/renderers/common/Animation.ts b/src-testing/src/renderers/common/Animation.ts deleted file mode 100644 index 0b00319a1..000000000 --- a/src-testing/src/renderers/common/Animation.ts +++ /dev/null @@ -1,38 +0,0 @@ -class Animation { - constructor(nodes, info) { - this.nodes = nodes; - this.info = info; - - this.animationLoop = null; - this.requestId = null; - - this._init(); - } - - _init() { - const update = (time, frame) => { - this.requestId = self.requestAnimationFrame(update); - - if (this.info.autoReset === true) this.info.reset(); - - this.nodes.nodeFrame.update(); - - this.info.frame = this.nodes.nodeFrame.frameId; - - if (this.animationLoop !== null) this.animationLoop(time, frame); - }; - - update(); - } - - dispose() { - self.cancelAnimationFrame(this.requestId); - this.requestId = null; - } - - setAnimationLoop(callback) { - this.animationLoop = callback; - } -} - -export default Animation; diff --git a/src-testing/src/renderers/common/Attributes.ts b/src-testing/src/renderers/common/Attributes.ts deleted file mode 100644 index ad7de8401..000000000 --- a/src-testing/src/renderers/common/Attributes.ts +++ /dev/null @@ -1,54 +0,0 @@ -import DataMap from './DataMap.js'; -import { AttributeType } from './Constants.js'; - -import { DynamicDrawUsage } from '../../constants.js'; - -class Attributes extends DataMap { - constructor(backend) { - super(); - - this.backend = backend; - } - - delete(attribute) { - const attributeData = super.delete(attribute); - - if (attributeData !== undefined) { - this.backend.destroyAttribute(attribute); - } - - return attributeData; - } - - update(attribute, type) { - const data = this.get(attribute); - - if (data.version === undefined) { - if (type === AttributeType.VERTEX) { - this.backend.createAttribute(attribute); - } else if (type === AttributeType.INDEX) { - this.backend.createIndexAttribute(attribute); - } else if (type === AttributeType.STORAGE) { - this.backend.createStorageAttribute(attribute); - } - - data.version = this._getBufferAttribute(attribute).version; - } else { - const bufferAttribute = this._getBufferAttribute(attribute); - - if (data.version < bufferAttribute.version || bufferAttribute.usage === DynamicDrawUsage) { - this.backend.updateAttribute(attribute); - - data.version = bufferAttribute.version; - } - } - } - - _getBufferAttribute(attribute) { - if (attribute.isInterleavedBufferAttribute) attribute = attribute.data; - - return attribute; - } -} - -export default Attributes; diff --git a/src-testing/src/renderers/common/Backend.ts b/src-testing/src/renderers/common/Backend.ts deleted file mode 100644 index 3dac46fe0..000000000 --- a/src-testing/src/renderers/common/Backend.ts +++ /dev/null @@ -1,170 +0,0 @@ -let vector2 = null; -let vector4 = null; -let color4 = null; - -import Color4 from './Color4.js'; -import { Vector2 } from '../../math/Vector2.js'; -import { Vector4 } from '../../math/Vector4.js'; -import { createCanvasElement } from '../../utils.js'; -import { REVISION } from '../../constants.js'; - -class Backend { - constructor(parameters = {}) { - this.parameters = Object.assign({}, parameters); - this.data = new WeakMap(); - this.renderer = null; - this.domElement = null; - } - - async init(renderer) { - this.renderer = renderer; - } - - // render context - - begin(/*renderContext*/) {} - - finish(/*renderContext*/) {} - - // render object - - draw(/*renderObject, info*/) {} - - // program - - createProgram(/*program*/) {} - - destroyProgram(/*program*/) {} - - // bindings - - createBindings(/*renderObject*/) {} - - updateBindings(/*renderObject*/) {} - - // pipeline - - createRenderPipeline(/*renderObject*/) {} - - createComputePipeline(/*computeNode, pipeline*/) {} - - destroyPipeline(/*pipeline*/) {} - - // cache key - - needsRenderUpdate(/*renderObject*/) {} // return Boolean ( fast test ) - - getRenderCacheKey(/*renderObject*/) {} // return String - - // node builder - - createNodeBuilder(/*renderObject*/) {} // return NodeBuilder (ADD IT) - - // textures - - createSampler(/*texture*/) {} - - createDefaultTexture(/*texture*/) {} - - createTexture(/*texture*/) {} - - copyTextureToBuffer(/*texture, x, y, width, height*/) {} - - // attributes - - createAttribute(/*attribute*/) {} - - createIndexAttribute(/*attribute*/) {} - - updateAttribute(/*attribute*/) {} - - destroyAttribute(/*attribute*/) {} - - // canvas - - getContext() {} - - updateSize() {} - - // utils - - resolveTimestampAsync(/*renderContext, type*/) {} - - hasFeatureAsync(/*name*/) {} // return Boolean - - hasFeature(/*name*/) {} // return Boolean - - getInstanceCount(renderObject) { - const { object, geometry } = renderObject; - - return geometry.isInstancedBufferGeometry ? geometry.instanceCount : object.count > 1 ? object.count : 1; - } - - getDrawingBufferSize() { - vector2 = vector2 || new Vector2(); - - return this.renderer.getDrawingBufferSize(vector2); - } - - getScissor() { - vector4 = vector4 || new Vector4(); - - return this.renderer.getScissor(vector4); - } - - setScissorTest(/*boolean*/) {} - - getClearColor() { - const renderer = this.renderer; - - color4 = color4 || new Color4(); - - renderer.getClearColor(color4); - - color4.getRGB(color4, this.renderer.currentColorSpace); - - return color4; - } - - getDomElement() { - let domElement = this.domElement; - - if (domElement === null) { - domElement = this.parameters.canvas !== undefined ? this.parameters.canvas : createCanvasElement(); - - // OffscreenCanvas does not have setAttribute, see #22811 - if ('setAttribute' in domElement) domElement.setAttribute('data-engine', `three.js r${REVISION} webgpu`); - - this.domElement = domElement; - } - - return domElement; - } - - // resource properties - - set(object, value) { - this.data.set(object, value); - } - - get(object) { - let map = this.data.get(object); - - if (map === undefined) { - map = {}; - this.data.set(object, map); - } - - return map; - } - - has(object) { - return this.data.has(object); - } - - delete(object) { - this.data.delete(object); - } -} - -export default Backend; diff --git a/src-testing/src/renderers/common/Background.ts b/src-testing/src/renderers/common/Background.ts deleted file mode 100644 index 2b163b6c7..000000000 --- a/src-testing/src/renderers/common/Background.ts +++ /dev/null @@ -1,125 +0,0 @@ -import DataMap from './DataMap.js'; -import Color4 from './Color4.js'; -import { - vec4, - context, - normalWorld, - backgroundBlurriness, - backgroundIntensity, - NodeMaterial, - modelViewProjection, -} from '../../nodes/Nodes.js'; - -import { Mesh } from '../../objects/Mesh.js'; -import { SphereGeometry } from '../../geometries/SphereGeometry.js'; -import { BackSide, LinearSRGBColorSpace } from '../../constants.js'; - -const _clearColor = /*@__PURE__*/ new Color4(); - -class Background extends DataMap { - constructor(renderer, nodes) { - super(); - - this.renderer = renderer; - this.nodes = nodes; - } - - update(scene, renderList, renderContext) { - const renderer = this.renderer; - const background = this.nodes.getBackgroundNode(scene) || scene.background; - - let forceClear = false; - - if (background === null) { - // no background settings, use clear color configuration from the renderer - - renderer._clearColor.getRGB(_clearColor, LinearSRGBColorSpace); - _clearColor.a = renderer._clearColor.a; - } else if (background.isColor === true) { - // background is an opaque color - - background.getRGB(_clearColor, LinearSRGBColorSpace); - _clearColor.a = 1; - - forceClear = true; - } else if (background.isNode === true) { - const sceneData = this.get(scene); - const backgroundNode = background; - - _clearColor.copy(renderer._clearColor); - - let backgroundMesh = sceneData.backgroundMesh; - - if (backgroundMesh === undefined) { - const backgroundMeshNode = context(vec4(backgroundNode).mul(backgroundIntensity), { - // @TODO: Add Texture2D support using node context - getUV: () => normalWorld, - getTextureLevel: () => backgroundBlurriness, - }); - - let viewProj = modelViewProjection(); - viewProj = viewProj.setZ(viewProj.w); - - const nodeMaterial = new NodeMaterial(); - nodeMaterial.name = 'Background.material'; - nodeMaterial.side = BackSide; - nodeMaterial.depthTest = false; - nodeMaterial.depthWrite = false; - nodeMaterial.fog = false; - nodeMaterial.lights = false; - nodeMaterial.vertexNode = viewProj; - nodeMaterial.colorNode = backgroundMeshNode; - - sceneData.backgroundMeshNode = backgroundMeshNode; - sceneData.backgroundMesh = backgroundMesh = new Mesh(new SphereGeometry(1, 32, 32), nodeMaterial); - backgroundMesh.frustumCulled = false; - backgroundMesh.name = 'Background.mesh'; - - backgroundMesh.onBeforeRender = function (renderer, scene, camera) { - this.matrixWorld.copyPosition(camera.matrixWorld); - }; - } - - const backgroundCacheKey = backgroundNode.getCacheKey(); - - if (sceneData.backgroundCacheKey !== backgroundCacheKey) { - sceneData.backgroundMeshNode.node = vec4(backgroundNode).mul(backgroundIntensity); - sceneData.backgroundMeshNode.needsUpdate = true; - - backgroundMesh.material.needsUpdate = true; - - sceneData.backgroundCacheKey = backgroundCacheKey; - } - - renderList.unshift(backgroundMesh, backgroundMesh.geometry, backgroundMesh.material, 0, 0, null); - } else { - console.error('THREE.Renderer: Unsupported background configuration.', background); - } - - // - - if (renderer.autoClear === true || forceClear === true) { - _clearColor.multiplyScalar(_clearColor.a); - - const clearColorValue = renderContext.clearColorValue; - - clearColorValue.r = _clearColor.r; - clearColorValue.g = _clearColor.g; - clearColorValue.b = _clearColor.b; - clearColorValue.a = _clearColor.a; - - renderContext.depthClearValue = renderer._clearDepth; - renderContext.stencilClearValue = renderer._clearStencil; - - renderContext.clearColor = renderer.autoClearColor === true; - renderContext.clearDepth = renderer.autoClearDepth === true; - renderContext.clearStencil = renderer.autoClearStencil === true; - } else { - renderContext.clearColor = false; - renderContext.clearDepth = false; - renderContext.clearStencil = false; - } - } -} - -export default Background; diff --git a/src-testing/src/renderers/common/BindGroup.ts b/src-testing/src/renderers/common/BindGroup.ts deleted file mode 100644 index 54b5f4965..000000000 --- a/src-testing/src/renderers/common/BindGroup.ts +++ /dev/null @@ -1,13 +0,0 @@ -let _id = 0; - -class BindGroup { - constructor(name = '', bindings = [], index = 0) { - this.name = name; - this.bindings = bindings; - this.index = index; - - this.id = _id++; - } -} - -export default BindGroup; diff --git a/src-testing/src/renderers/common/Binding.ts b/src-testing/src/renderers/common/Binding.ts deleted file mode 100644 index a12f3563b..000000000 --- a/src-testing/src/renderers/common/Binding.ts +++ /dev/null @@ -1,17 +0,0 @@ -class Binding { - constructor(name = '') { - this.name = name; - - this.visibility = 0; - } - - setVisibility(visibility) { - this.visibility |= visibility; - } - - clone() { - return Object.assign(new this.constructor(), this); - } -} - -export default Binding; diff --git a/src-testing/src/renderers/common/Bindings.ts b/src-testing/src/renderers/common/Bindings.ts deleted file mode 100644 index 51aa31f0d..000000000 --- a/src-testing/src/renderers/common/Bindings.ts +++ /dev/null @@ -1,161 +0,0 @@ -import DataMap from './DataMap.js'; -import { AttributeType } from './Constants.js'; - -class Bindings extends DataMap { - constructor(backend, nodes, textures, attributes, pipelines, info) { - super(); - - this.backend = backend; - this.textures = textures; - this.pipelines = pipelines; - this.attributes = attributes; - this.nodes = nodes; - this.info = info; - - this.pipelines.bindings = this; // assign bindings to pipelines - } - - getForRender(renderObject) { - const bindings = renderObject.getBindings(); - - for (const bindGroup of bindings) { - const groupData = this.get(bindGroup); - - if (groupData.bindGroup === undefined) { - // each object defines an array of bindings (ubos, textures, samplers etc.) - - this._init(bindGroup); - - this.backend.createBindings(bindGroup, bindings); - - groupData.bindGroup = bindGroup; - } - } - - return bindings; - } - - getForCompute(computeNode) { - const bindings = this.nodes.getForCompute(computeNode).bindings; - - for (const bindGroup of bindings) { - const groupData = this.get(bindGroup); - - if (groupData.bindGroup === undefined) { - this._init(bindGroup); - - this.backend.createBindings(bindGroup, bindings); - - groupData.bindGroup = bindGroup; - } - } - - return bindings; - } - - updateForCompute(computeNode) { - this._updateBindings(computeNode, this.getForCompute(computeNode)); - } - - updateForRender(renderObject) { - this._updateBindings(renderObject, this.getForRender(renderObject)); - } - - _updateBindings(object, bindings) { - for (const bindGroup of bindings) { - this._update(object, bindGroup, bindings); - } - } - - _init(bindGroup) { - for (const binding of bindGroup.bindings) { - if (binding.isSampledTexture) { - this.textures.updateTexture(binding.texture); - } else if (binding.isStorageBuffer) { - const attribute = binding.attribute; - - this.attributes.update(attribute, AttributeType.STORAGE); - } - } - } - - _update(object, bindGroup, bindings) { - const { backend } = this; - - let needsBindingsUpdate = false; - - // iterate over all bindings and check if buffer updates or a new binding group is required - - for (const binding of bindGroup.bindings) { - if (binding.isNodeUniformsGroup) { - const updated = this.nodes.updateGroup(binding); - - if (!updated) continue; - } - - if (binding.isUniformBuffer) { - const updated = binding.update(); - - if (updated) { - backend.updateBinding(binding); - } - } else if (binding.isSampler) { - binding.update(); - } else if (binding.isSampledTexture) { - const texture = binding.texture; - - if (binding.needsBindingsUpdate) needsBindingsUpdate = true; - - const updated = binding.update(); - - if (updated) { - this.textures.updateTexture(binding.texture); - } - - const textureData = backend.get(binding.texture); - - if ( - backend.isWebGPUBackend === true && - textureData.texture === undefined && - textureData.externalTexture === undefined - ) { - // TODO: Remove this once we found why updated === false isn't bound to a texture in the WebGPU backend - console.error( - 'Bindings._update: binding should be available:', - binding, - updated, - binding.texture, - binding.textureNode.value, - ); - - this.textures.updateTexture(binding.texture); - needsBindingsUpdate = true; - } - - if (texture.isStorageTexture === true) { - const textureData = this.get(texture); - - if (binding.store === true) { - textureData.needsMipmap = true; - } else if ( - texture.generateMipmaps === true && - this.textures.needsMipmaps(texture) && - textureData.needsMipmap === true - ) { - this.backend.generateMipmaps(texture); - - textureData.needsMipmap = false; - } - } - } - } - - if (needsBindingsUpdate === true) { - const pipeline = this.pipelines.getForRender(object); - - this.backend.updateBindings(bindGroup, bindings, pipeline); - } - } -} - -export default Bindings; diff --git a/src-testing/src/renderers/common/Buffer.ts b/src-testing/src/renderers/common/Buffer.ts deleted file mode 100644 index 17013c6dc..000000000 --- a/src-testing/src/renderers/common/Buffer.ts +++ /dev/null @@ -1,28 +0,0 @@ -import Binding from './Binding.js'; -import { getFloatLength } from './BufferUtils.js'; - -class Buffer extends Binding { - constructor(name, buffer = null) { - super(name); - - this.isBuffer = true; - - this.bytesPerElement = Float32Array.BYTES_PER_ELEMENT; - - this._buffer = buffer; - } - - get byteLength() { - return getFloatLength(this._buffer.byteLength); - } - - get buffer() { - return this._buffer; - } - - update() { - return true; - } -} - -export default Buffer; diff --git a/src-testing/src/renderers/common/BufferUtils.ts b/src-testing/src/renderers/common/BufferUtils.ts deleted file mode 100644 index 99ddcb48b..000000000 --- a/src-testing/src/renderers/common/BufferUtils.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { GPU_CHUNK_BYTES } from './Constants.js'; - -function getFloatLength(floatLength) { - // ensure chunk size alignment (STD140 layout) - - return floatLength + ((GPU_CHUNK_BYTES - (floatLength % GPU_CHUNK_BYTES)) % GPU_CHUNK_BYTES); -} - -function getVectorLength(count, vectorLength = 4) { - const strideLength = getStrideLength(vectorLength); - - const floatLength = strideLength * count; - - return getFloatLength(floatLength); -} - -function getStrideLength(vectorLength) { - const strideLength = 4; - - return vectorLength + ((strideLength - (vectorLength % strideLength)) % strideLength); -} - -export { getFloatLength, getVectorLength, getStrideLength }; diff --git a/src-testing/src/renderers/common/ChainMap.ts b/src-testing/src/renderers/common/ChainMap.ts deleted file mode 100644 index b17e7080f..000000000 --- a/src-testing/src/renderers/common/ChainMap.ts +++ /dev/null @@ -1,43 +0,0 @@ -export default class ChainMap { - constructor() { - this.weakMap = new WeakMap(); - } - - get(keys) { - let map = this.weakMap; - - for (let i = 0; i < keys.length; i++) { - map = map.get(keys[i]); - - if (map === undefined) return undefined; - } - - return map.get(keys[keys.length - 1]); - } - - set(keys, value) { - let map = this.weakMap; - - for (let i = 0; i < keys.length; i++) { - const key = keys[i]; - - if (map.has(key) === false) map.set(key, new WeakMap()); - - map = map.get(key); - } - - return map.set(keys[keys.length - 1], value); - } - - delete(keys) { - let map = this.weakMap; - - for (let i = 0; i < keys.length; i++) { - map = map.get(keys[i]); - - if (map === undefined) return false; - } - - return map.delete(keys[keys.length - 1]); - } -} diff --git a/src-testing/src/renderers/common/ClippingContext.ts b/src-testing/src/renderers/common/ClippingContext.ts deleted file mode 100644 index 207286052..000000000 --- a/src-testing/src/renderers/common/ClippingContext.ts +++ /dev/null @@ -1,130 +0,0 @@ -import { Matrix3 } from '../../math/Matrix3.js'; -import { Plane } from '../../math/Plane.js'; -import { Vector4 } from '../../math/Vector4.js'; - -const _plane = /*@__PURE__*/ new Plane(); - -let _clippingContextVersion = 0; - -class ClippingContext { - constructor() { - this.version = ++_clippingContextVersion; - - this.globalClippingCount = 0; - - this.localClippingCount = 0; - this.localClippingEnabled = false; - this.localClipIntersection = false; - - this.planes = []; - - this.parentVersion = 0; - this.viewNormalMatrix = new Matrix3(); - } - - projectPlanes(source, offset) { - const l = source.length; - const planes = this.planes; - - for (let i = 0; i < l; i++) { - _plane.copy(source[i]).applyMatrix4(this.viewMatrix, this.viewNormalMatrix); - - const v = planes[offset + i]; - const normal = _plane.normal; - - v.x = -normal.x; - v.y = -normal.y; - v.z = -normal.z; - v.w = _plane.constant; - } - } - - updateGlobal(renderer, camera) { - const rendererClippingPlanes = renderer.clippingPlanes; - this.viewMatrix = camera.matrixWorldInverse; - - this.viewNormalMatrix.getNormalMatrix(this.viewMatrix); - - let update = false; - - if (Array.isArray(rendererClippingPlanes) && rendererClippingPlanes.length !== 0) { - const l = rendererClippingPlanes.length; - - if (l !== this.globalClippingCount) { - const planes = []; - - for (let i = 0; i < l; i++) { - planes.push(new Vector4()); - } - - this.globalClippingCount = l; - this.planes = planes; - - update = true; - } - - this.projectPlanes(rendererClippingPlanes, 0); - } else if (this.globalClippingCount !== 0) { - this.globalClippingCount = 0; - this.planes = []; - update = true; - } - - if (renderer.localClippingEnabled !== this.localClippingEnabled) { - this.localClippingEnabled = renderer.localClippingEnabled; - update = true; - } - - if (update) this.version = _clippingContextVersion++; - } - - update(parent, material) { - let update = false; - - if (this !== parent && parent.version !== this.parentVersion) { - this.globalClippingCount = material.isShadowNodeMaterial ? 0 : parent.globalClippingCount; - this.localClippingEnabled = parent.localClippingEnabled; - this.planes = Array.from(parent.planes); - this.parentVersion = parent.version; - this.viewMatrix = parent.viewMatrix; - this.viewNormalMatrix = parent.viewNormalMatrix; - - update = true; - } - - if (this.localClippingEnabled) { - const localClippingPlanes = material.clippingPlanes; - - if (Array.isArray(localClippingPlanes) && localClippingPlanes.length !== 0) { - const l = localClippingPlanes.length; - const planes = this.planes; - const offset = this.globalClippingCount; - - if (update || l !== this.localClippingCount) { - planes.length = offset + l; - - for (let i = 0; i < l; i++) { - planes[offset + i] = new Vector4(); - } - - this.localClippingCount = l; - update = true; - } - - this.projectPlanes(localClippingPlanes, offset); - } else if (this.localClippingCount !== 0) { - this.localClippingCount = 0; - update = true; - } - - if (this.localClipIntersection !== material.clipIntersection) { - this.localClipIntersection = material.clipIntersection; - update = true; - } - } - - if (update) this.version = _clippingContextVersion++; - } -} - -export default ClippingContext; diff --git a/src-testing/src/renderers/common/Color4.ts b/src-testing/src/renderers/common/Color4.ts deleted file mode 100644 index 77caa31e0..000000000 --- a/src-testing/src/renderers/common/Color4.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { Color } from '../../math/Color.js'; - -class Color4 extends Color { - constructor(r, g, b, a = 1) { - super(r, g, b); - - this.a = a; - } - - set(r, g, b, a = 1) { - this.a = a; - - return super.set(r, g, b); - } - - copy(color) { - if (color.a !== undefined) this.a = color.a; - - return super.copy(color); - } - - clone() { - return new this.constructor(this.r, this.g, this.b, this.a); - } -} - -export default Color4; diff --git a/src-testing/src/renderers/common/ComputePipeline.ts b/src-testing/src/renderers/common/ComputePipeline.ts deleted file mode 100644 index 0fd3ca531..000000000 --- a/src-testing/src/renderers/common/ComputePipeline.ts +++ /dev/null @@ -1,13 +0,0 @@ -import Pipeline from './Pipeline.js'; - -class ComputePipeline extends Pipeline { - constructor(cacheKey, computeProgram) { - super(cacheKey); - - this.computeProgram = computeProgram; - - this.isComputePipeline = true; - } -} - -export default ComputePipeline; diff --git a/src-testing/src/renderers/common/Constants.ts b/src-testing/src/renderers/common/Constants.ts deleted file mode 100644 index 0d0c35a25..000000000 --- a/src-testing/src/renderers/common/Constants.ts +++ /dev/null @@ -1,14 +0,0 @@ -export const AttributeType = { - VERTEX: 1, - INDEX: 2, - STORAGE: 4, -}; - -// size of a chunk in bytes (STD140 layout) - -export const GPU_CHUNK_BYTES = 16; - -// @TODO: Move to src/constants.js - -export const BlendColorFactor = 211; -export const OneMinusBlendColorFactor = 212; diff --git a/src-testing/src/renderers/common/CubeRenderTarget.ts b/src-testing/src/renderers/common/CubeRenderTarget.ts deleted file mode 100644 index 44f4f3c2a..000000000 --- a/src-testing/src/renderers/common/CubeRenderTarget.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { equirectUV } from '../../nodes/utils/EquirectUVNode.js'; -import { texture as TSL_Texture } from '../../nodes/accessors/TextureNode.js'; -import { positionWorldDirection } from '../../nodes/accessors/PositionNode.js'; -import { createNodeMaterialFromType } from '../../nodes/materials/NodeMaterial.js'; - -import { WebGLCubeRenderTarget } from '../../renderers/WebGLCubeRenderTarget.js'; -import { Scene } from '../../scenes/Scene.js'; -import { CubeCamera } from '../../cameras/CubeCamera.js'; -import { BoxGeometry } from '../../geometries/BoxGeometry.js'; -import { Mesh } from '../../objects/Mesh.js'; -import { BackSide, NoBlending, LinearFilter, LinearMipmapLinearFilter } from '../../constants.js'; - -// @TODO: Consider rename WebGLCubeRenderTarget to just CubeRenderTarget - -class CubeRenderTarget extends WebGLCubeRenderTarget { - constructor(size = 1, options = {}) { - super(size, options); - - this.isCubeRenderTarget = true; - } - - fromEquirectangularTexture(renderer, texture) { - const currentMinFilter = texture.minFilter; - const currentGenerateMipmaps = texture.generateMipmaps; - - texture.generateMipmaps = true; - - this.texture.type = texture.type; - this.texture.colorSpace = texture.colorSpace; - - this.texture.generateMipmaps = texture.generateMipmaps; - this.texture.minFilter = texture.minFilter; - this.texture.magFilter = texture.magFilter; - - const geometry = new BoxGeometry(5, 5, 5); - - const uvNode = equirectUV(positionWorldDirection); - - const material = createNodeMaterialFromType('MeshBasicNodeMaterial'); - material.colorNode = TSL_Texture(texture, uvNode, 0); - material.side = BackSide; - material.blending = NoBlending; - - const mesh = new Mesh(geometry, material); - - const scene = new Scene(); - scene.add(mesh); - - // Avoid blurred poles - if (texture.minFilter === LinearMipmapLinearFilter) texture.minFilter = LinearFilter; - - const camera = new CubeCamera(1, 10, this); - camera.update(renderer, scene); - - texture.minFilter = currentMinFilter; - texture.currentGenerateMipmaps = currentGenerateMipmaps; - - mesh.geometry.dispose(); - mesh.material.dispose(); - - return this; - } -} - -export default CubeRenderTarget; diff --git a/src-testing/src/renderers/common/DataMap.ts b/src-testing/src/renderers/common/DataMap.ts deleted file mode 100644 index 006bc2950..000000000 --- a/src-testing/src/renderers/common/DataMap.ts +++ /dev/null @@ -1,38 +0,0 @@ -class DataMap { - constructor() { - this.data = new WeakMap(); - } - - get(object) { - let map = this.data.get(object); - - if (map === undefined) { - map = {}; - this.data.set(object, map); - } - - return map; - } - - delete(object) { - let map; - - if (this.data.has(object)) { - map = this.data.get(object); - - this.data.delete(object); - } - - return map; - } - - has(object) { - return this.data.has(object); - } - - dispose() { - this.data = new WeakMap(); - } -} - -export default DataMap; diff --git a/src-testing/src/renderers/common/Geometries.ts b/src-testing/src/renderers/common/Geometries.ts deleted file mode 100644 index 9072f2381..000000000 --- a/src-testing/src/renderers/common/Geometries.ts +++ /dev/null @@ -1,183 +0,0 @@ -import DataMap from './DataMap.js'; -import { AttributeType } from './Constants.js'; - -import { Uint16BufferAttribute, Uint32BufferAttribute } from '../../core/BufferAttribute.js'; - -function arrayNeedsUint32(array) { - // assumes larger values usually on last - - for (let i = array.length - 1; i >= 0; --i) { - if (array[i] >= 65535) return true; // account for PRIMITIVE_RESTART_FIXED_INDEX, #24565 - } - - return false; -} - -function getWireframeVersion(geometry) { - return geometry.index !== null ? geometry.index.version : geometry.attributes.position.version; -} - -function getWireframeIndex(geometry) { - const indices = []; - - const geometryIndex = geometry.index; - const geometryPosition = geometry.attributes.position; - - if (geometryIndex !== null) { - const array = geometryIndex.array; - - for (let i = 0, l = array.length; i < l; i += 3) { - const a = array[i + 0]; - const b = array[i + 1]; - const c = array[i + 2]; - - indices.push(a, b, b, c, c, a); - } - } else { - const array = geometryPosition.array; - - for (let i = 0, l = array.length / 3 - 1; i < l; i += 3) { - const a = i + 0; - const b = i + 1; - const c = i + 2; - - indices.push(a, b, b, c, c, a); - } - } - - const attribute = new (arrayNeedsUint32(indices) ? Uint32BufferAttribute : Uint16BufferAttribute)(indices, 1); - attribute.version = getWireframeVersion(geometry); - - return attribute; -} - -class Geometries extends DataMap { - constructor(attributes, info) { - super(); - - this.attributes = attributes; - this.info = info; - - this.wireframes = new WeakMap(); - - this.attributeCall = new WeakMap(); - } - - has(renderObject) { - const geometry = renderObject.geometry; - - return super.has(geometry) && this.get(geometry).initialized === true; - } - - updateForRender(renderObject) { - if (this.has(renderObject) === false) this.initGeometry(renderObject); - - this.updateAttributes(renderObject); - } - - initGeometry(renderObject) { - const geometry = renderObject.geometry; - const geometryData = this.get(geometry); - - geometryData.initialized = true; - - this.info.memory.geometries++; - - const onDispose = () => { - this.info.memory.geometries--; - - const index = geometry.index; - const geometryAttributes = renderObject.getAttributes(); - - if (index !== null) { - this.attributes.delete(index); - } - - for (const geometryAttribute of geometryAttributes) { - this.attributes.delete(geometryAttribute); - } - - const wireframeAttribute = this.wireframes.get(geometry); - - if (wireframeAttribute !== undefined) { - this.attributes.delete(wireframeAttribute); - } - - geometry.removeEventListener('dispose', onDispose); - }; - - geometry.addEventListener('dispose', onDispose); - } - - updateAttributes(renderObject) { - const attributes = renderObject.getAttributes(); - - for (const attribute of attributes) { - if (attribute.isStorageBufferAttribute || attribute.isStorageInstancedBufferAttribute) { - this.updateAttribute(attribute, AttributeType.STORAGE); - } else { - this.updateAttribute(attribute, AttributeType.VERTEX); - } - } - - const index = this.getIndex(renderObject); - - if (index !== null) { - this.updateAttribute(index, AttributeType.INDEX); - } - } - - updateAttribute(attribute, type) { - const callId = this.info.render.calls; - - if (!attribute.isInterleavedBufferAttribute) { - if (this.attributeCall.get(attribute) !== callId) { - this.attributes.update(attribute, type); - - this.attributeCall.set(attribute, callId); - } - } else { - if (this.attributeCall.get(attribute) === undefined) { - this.attributes.update(attribute, type); - - this.attributeCall.set(attribute, callId); - } else if (this.attributeCall.get(attribute.data) !== callId) { - this.attributes.update(attribute, type); - - this.attributeCall.set(attribute.data, callId); - - this.attributeCall.set(attribute, callId); - } - } - } - - getIndex(renderObject) { - const { geometry, material } = renderObject; - - let index = geometry.index; - - if (material.wireframe === true) { - const wireframes = this.wireframes; - - let wireframeAttribute = wireframes.get(geometry); - - if (wireframeAttribute === undefined) { - wireframeAttribute = getWireframeIndex(geometry); - - wireframes.set(geometry, wireframeAttribute); - } else if (wireframeAttribute.version !== getWireframeVersion(geometry)) { - this.attributes.delete(wireframeAttribute); - - wireframeAttribute = getWireframeIndex(geometry); - - wireframes.set(geometry, wireframeAttribute); - } - - index = wireframeAttribute; - } - - return index; - } -} - -export default Geometries; diff --git a/src-testing/src/renderers/common/Info.ts b/src-testing/src/renderers/common/Info.ts deleted file mode 100644 index 4ede75de7..000000000 --- a/src-testing/src/renderers/common/Info.ts +++ /dev/null @@ -1,95 +0,0 @@ -class Info { - constructor() { - this.autoReset = true; - - this.frame = 0; - this.calls = 0; - - this.render = { - calls: 0, - frameCalls: 0, - drawCalls: 0, - triangles: 0, - points: 0, - lines: 0, - timestamp: 0, - previousFrameCalls: 0, - timestampCalls: 0, - }; - - this.compute = { - calls: 0, - frameCalls: 0, - timestamp: 0, - previousFrameCalls: 0, - timestampCalls: 0, - }; - - this.memory = { - geometries: 0, - textures: 0, - }; - } - - update(object, count, instanceCount) { - this.render.drawCalls++; - - if (object.isMesh || object.isSprite) { - this.render.triangles += instanceCount * (count / 3); - } else if (object.isPoints) { - this.render.points += instanceCount * count; - } else if (object.isLineSegments) { - this.render.lines += instanceCount * (count / 2); - } else if (object.isLine) { - this.render.lines += instanceCount * (count - 1); - } else { - console.error('THREE.WebGPUInfo: Unknown object type.'); - } - } - - updateTimestamp(type, time) { - if (this[type].timestampCalls === 0) { - this[type].timestamp = 0; - } - - this[type].timestamp += time; - - this[type].timestampCalls++; - - if (this[type].timestampCalls >= this[type].previousFrameCalls) { - this[type].timestampCalls = 0; - } - } - - reset() { - const previousRenderFrameCalls = this.render.frameCalls; - this.render.previousFrameCalls = previousRenderFrameCalls; - - const previousComputeFrameCalls = this.compute.frameCalls; - this.compute.previousFrameCalls = previousComputeFrameCalls; - - this.render.drawCalls = 0; - this.render.frameCalls = 0; - this.compute.frameCalls = 0; - - this.render.triangles = 0; - this.render.points = 0; - this.render.lines = 0; - } - - dispose() { - this.reset(); - - this.calls = 0; - - this.render.calls = 0; - this.compute.calls = 0; - - this.render.timestamp = 0; - this.compute.timestamp = 0; - this.memory.geometries = 0; - this.memory.textures = 0; - } -} - -export default Info; diff --git a/src-testing/src/renderers/common/Pipeline.ts b/src-testing/src/renderers/common/Pipeline.ts deleted file mode 100644 index 16017455a..000000000 --- a/src-testing/src/renderers/common/Pipeline.ts +++ /dev/null @@ -1,9 +0,0 @@ -class Pipeline { - constructor(cacheKey) { - this.cacheKey = cacheKey; - - this.usedTimes = 0; - } -} - -export default Pipeline; diff --git a/src-testing/src/renderers/common/Pipelines.ts b/src-testing/src/renderers/common/Pipelines.ts deleted file mode 100644 index 68c8f223c..000000000 --- a/src-testing/src/renderers/common/Pipelines.ts +++ /dev/null @@ -1,270 +0,0 @@ -import DataMap from './DataMap.js'; -import RenderPipeline from './RenderPipeline.js'; -import ComputePipeline from './ComputePipeline.js'; -import ProgrammableStage from './ProgrammableStage.js'; - -class Pipelines extends DataMap { - constructor(backend, nodes) { - super(); - - this.backend = backend; - this.nodes = nodes; - - this.bindings = null; // set by the bindings - - this.caches = new Map(); - this.programs = { - vertex: new Map(), - fragment: new Map(), - compute: new Map(), - }; - } - - getForCompute(computeNode, bindings) { - const { backend } = this; - - const data = this.get(computeNode); - - if (this._needsComputeUpdate(computeNode)) { - const previousPipeline = data.pipeline; - - if (previousPipeline) { - previousPipeline.usedTimes--; - previousPipeline.computeProgram.usedTimes--; - } - - // get shader - - const nodeBuilderState = this.nodes.getForCompute(computeNode); - - // programmable stage - - let stageCompute = this.programs.compute.get(nodeBuilderState.computeShader); - - if (stageCompute === undefined) { - if (previousPipeline && previousPipeline.computeProgram.usedTimes === 0) - this._releaseProgram(previousPipeline.computeProgram); - - stageCompute = new ProgrammableStage( - nodeBuilderState.computeShader, - 'compute', - nodeBuilderState.transforms, - nodeBuilderState.nodeAttributes, - ); - this.programs.compute.set(nodeBuilderState.computeShader, stageCompute); - - backend.createProgram(stageCompute); - } - - // determine compute pipeline - - const cacheKey = this._getComputeCacheKey(computeNode, stageCompute); - - let pipeline = this.caches.get(cacheKey); - - if (pipeline === undefined) { - if (previousPipeline && previousPipeline.usedTimes === 0) this._releasePipeline(previousPipeline); - - pipeline = this._getComputePipeline(computeNode, stageCompute, cacheKey, bindings); - } - - // keep track of all used times - - pipeline.usedTimes++; - stageCompute.usedTimes++; - - // - - data.version = computeNode.version; - data.pipeline = pipeline; - } - - return data.pipeline; - } - - getForRender(renderObject, promises = null) { - const { backend } = this; - - const data = this.get(renderObject); - - if (this._needsRenderUpdate(renderObject)) { - const previousPipeline = data.pipeline; - - if (previousPipeline) { - previousPipeline.usedTimes--; - previousPipeline.vertexProgram.usedTimes--; - previousPipeline.fragmentProgram.usedTimes--; - } - - // get shader - - const nodeBuilderState = renderObject.getNodeBuilderState(); - - // programmable stages - - let stageVertex = this.programs.vertex.get(nodeBuilderState.vertexShader); - - if (stageVertex === undefined) { - if (previousPipeline && previousPipeline.vertexProgram.usedTimes === 0) - this._releaseProgram(previousPipeline.vertexProgram); - - stageVertex = new ProgrammableStage(nodeBuilderState.vertexShader, 'vertex'); - this.programs.vertex.set(nodeBuilderState.vertexShader, stageVertex); - - backend.createProgram(stageVertex); - } - - let stageFragment = this.programs.fragment.get(nodeBuilderState.fragmentShader); - - if (stageFragment === undefined) { - if (previousPipeline && previousPipeline.fragmentProgram.usedTimes === 0) - this._releaseProgram(previousPipeline.fragmentProgram); - - stageFragment = new ProgrammableStage(nodeBuilderState.fragmentShader, 'fragment'); - this.programs.fragment.set(nodeBuilderState.fragmentShader, stageFragment); - - backend.createProgram(stageFragment); - } - - // determine render pipeline - - const cacheKey = this._getRenderCacheKey(renderObject, stageVertex, stageFragment); - - let pipeline = this.caches.get(cacheKey); - - if (pipeline === undefined) { - if (previousPipeline && previousPipeline.usedTimes === 0) this._releasePipeline(previousPipeline); - - pipeline = this._getRenderPipeline(renderObject, stageVertex, stageFragment, cacheKey, promises); - } else { - renderObject.pipeline = pipeline; - } - - // keep track of all used times - - pipeline.usedTimes++; - stageVertex.usedTimes++; - stageFragment.usedTimes++; - - // - - data.pipeline = pipeline; - } - - return data.pipeline; - } - - delete(object) { - const pipeline = this.get(object).pipeline; - - if (pipeline) { - // pipeline - - pipeline.usedTimes--; - - if (pipeline.usedTimes === 0) this._releasePipeline(pipeline); - - // programs - - if (pipeline.isComputePipeline) { - pipeline.computeProgram.usedTimes--; - - if (pipeline.computeProgram.usedTimes === 0) this._releaseProgram(pipeline.computeProgram); - } else { - pipeline.fragmentProgram.usedTimes--; - pipeline.vertexProgram.usedTimes--; - - if (pipeline.vertexProgram.usedTimes === 0) this._releaseProgram(pipeline.vertexProgram); - if (pipeline.fragmentProgram.usedTimes === 0) this._releaseProgram(pipeline.fragmentProgram); - } - } - - return super.delete(object); - } - - dispose() { - super.dispose(); - - this.caches = new Map(); - this.programs = { - vertex: new Map(), - fragment: new Map(), - compute: new Map(), - }; - } - - updateForRender(renderObject) { - this.getForRender(renderObject); - } - - _getComputePipeline(computeNode, stageCompute, cacheKey, bindings) { - // check for existing pipeline - - cacheKey = cacheKey || this._getComputeCacheKey(computeNode, stageCompute); - - let pipeline = this.caches.get(cacheKey); - - if (pipeline === undefined) { - pipeline = new ComputePipeline(cacheKey, stageCompute); - - this.caches.set(cacheKey, pipeline); - - this.backend.createComputePipeline(pipeline, bindings); - } - - return pipeline; - } - - _getRenderPipeline(renderObject, stageVertex, stageFragment, cacheKey, promises) { - // check for existing pipeline - - cacheKey = cacheKey || this._getRenderCacheKey(renderObject, stageVertex, stageFragment); - - let pipeline = this.caches.get(cacheKey); - - if (pipeline === undefined) { - pipeline = new RenderPipeline(cacheKey, stageVertex, stageFragment); - - this.caches.set(cacheKey, pipeline); - - renderObject.pipeline = pipeline; - - this.backend.createRenderPipeline(renderObject, promises); - } - - return pipeline; - } - - _getComputeCacheKey(computeNode, stageCompute) { - return computeNode.id + ',' + stageCompute.id; - } - - _getRenderCacheKey(renderObject, stageVertex, stageFragment) { - return stageVertex.id + ',' + stageFragment.id + ',' + this.backend.getRenderCacheKey(renderObject); - } - - _releasePipeline(pipeline) { - this.caches.delete(pipeline.cacheKey); - } - - _releaseProgram(program) { - const code = program.code; - const stage = program.stage; - - this.programs[stage].delete(code); - } - - _needsComputeUpdate(computeNode) { - const data = this.get(computeNode); - - return data.pipeline === undefined || data.version !== computeNode.version; - } - - _needsRenderUpdate(renderObject) { - const data = this.get(renderObject); - - return data.pipeline === undefined || this.backend.needsRenderUpdate(renderObject); - } -} - -export default Pipelines; diff --git a/src-testing/src/renderers/common/PostProcessing.d.ts b/src-testing/src/renderers/common/PostProcessing.d.ts deleted file mode 100644 index fdd4e5bdc..000000000 --- a/src-testing/src/renderers/common/PostProcessing.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { Node } from "../../nodes/Nodes.js"; -import Renderer from "./Renderer.js"; - -declare class PostProcessing { - renderer: Renderer; - outputNode: Node; - - outputColorTransform: boolean; - - needsUpdate: boolean; - - constructor(renderer: Renderer, outputNode?: Node); - - render(): void; - - update(): void; - - renderAsync(): Promise; -} - -export default PostProcessing; diff --git a/src-testing/src/renderers/common/ProgrammableStage.ts b/src-testing/src/renderers/common/ProgrammableStage.ts deleted file mode 100644 index a684e4443..000000000 --- a/src-testing/src/renderers/common/ProgrammableStage.ts +++ /dev/null @@ -1,16 +0,0 @@ -let _id = 0; - -class ProgrammableStage { - constructor(code, type, transforms = null, attributes = null) { - this.id = _id++; - - this.code = code; - this.stage = type; - this.transforms = transforms; - this.attributes = attributes; - - this.usedTimes = 0; - } -} - -export default ProgrammableStage; diff --git a/src-testing/src/renderers/common/QuadMesh.d.ts b/src-testing/src/renderers/common/QuadMesh.d.ts deleted file mode 100644 index fb5eafb1b..000000000 --- a/src-testing/src/renderers/common/QuadMesh.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { OrthographicCamera } from "../../cameras/OrthographicCamera.js"; -import { Material } from "../../materials/Material.js"; -import { Mesh } from "../../objects/Mesh.js"; -import Renderer from "./Renderer.js"; - -export default class QuadMesh extends Mesh { - camera: OrthographicCamera; - - constructor(material?: Material | null); - - renderAsync(renderer: Renderer): Promise; - - render(renderer: Renderer): void; -} diff --git a/src-testing/src/renderers/common/RenderBundle.ts b/src-testing/src/renderers/common/RenderBundle.ts deleted file mode 100644 index e59e49378..000000000 --- a/src-testing/src/renderers/common/RenderBundle.ts +++ /dev/null @@ -1,12 +0,0 @@ -class RenderBundle { - constructor(scene, camera) { - this.scene = scene; - this.camera = camera; - } - - clone() { - return Object.assign(new this.constructor(), this); - } -} - -export default RenderBundle; diff --git a/src-testing/src/renderers/common/RenderBundles.ts b/src-testing/src/renderers/common/RenderBundles.ts deleted file mode 100644 index 291403652..000000000 --- a/src-testing/src/renderers/common/RenderBundles.ts +++ /dev/null @@ -1,28 +0,0 @@ -import ChainMap from './ChainMap.js'; -import RenderBundle from './RenderBundle.js'; - -class RenderBundles { - constructor() { - this.lists = new ChainMap(); - } - - get(scene, camera) { - const lists = this.lists; - const keys = [scene, camera]; - - let list = lists.get(keys); - - if (list === undefined) { - list = new RenderBundle(scene, camera); - lists.set(keys, list); - } - - return list; - } - - dispose() { - this.lists = new ChainMap(); - } -} - -export default RenderBundles; diff --git a/src-testing/src/renderers/common/RenderContext.ts b/src-testing/src/renderers/common/RenderContext.ts deleted file mode 100644 index 342029ead..000000000 --- a/src-testing/src/renderers/common/RenderContext.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { Vector4 } from '../../math/Vector4.js'; - -let id = 0; - -class RenderContext { - constructor() { - this.id = id++; - - this.color = true; - this.clearColor = true; - this.clearColorValue = { r: 0, g: 0, b: 0, a: 1 }; - - this.depth = true; - this.clearDepth = true; - this.clearDepthValue = 1; - - this.stencil = false; - this.clearStencil = true; - this.clearStencilValue = 1; - - this.viewport = false; - this.viewportValue = new Vector4(); - - this.scissor = false; - this.scissorValue = new Vector4(); - - this.textures = null; - this.depthTexture = null; - this.activeCubeFace = 0; - this.sampleCount = 1; - - this.width = 0; - this.height = 0; - - this.isRenderContext = true; - } - - getCacheKey() { - return getCacheKey(this); - } -} - -export function getCacheKey(renderContext) { - const { textures, activeCubeFace } = renderContext; - - let key = ''; - - for (const texture of textures) { - key += texture.id + ','; - } - - key += activeCubeFace; - - return key; -} - -export default RenderContext; diff --git a/src-testing/src/renderers/common/RenderContexts.ts b/src-testing/src/renderers/common/RenderContexts.ts deleted file mode 100644 index e77308c1d..000000000 --- a/src-testing/src/renderers/common/RenderContexts.ts +++ /dev/null @@ -1,47 +0,0 @@ -import ChainMap from './ChainMap.js'; -import RenderContext from './RenderContext.js'; - -class RenderContexts { - constructor() { - this.chainMaps = {}; - } - - get(scene, camera, renderTarget = null) { - const chainKey = [scene, camera]; - - let attachmentState; - - if (renderTarget === null) { - attachmentState = 'default'; - } else { - const format = renderTarget.texture.format; - const count = renderTarget.textures.length; - - attachmentState = `${count}:${format}:${renderTarget.samples}:${renderTarget.depthBuffer}:${renderTarget.stencilBuffer}`; - } - - const chainMap = this.getChainMap(attachmentState); - - let renderState = chainMap.get(chainKey); - - if (renderState === undefined) { - renderState = new RenderContext(); - - chainMap.set(chainKey, renderState); - } - - if (renderTarget !== null) renderState.sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples; - - return renderState; - } - - getChainMap(attachmentState) { - return this.chainMaps[attachmentState] || (this.chainMaps[attachmentState] = new ChainMap()); - } - - dispose() { - this.chainMaps = {}; - } -} - -export default RenderContexts; diff --git a/src-testing/src/renderers/common/RenderList.ts b/src-testing/src/renderers/common/RenderList.ts deleted file mode 100644 index a72a91dfd..000000000 --- a/src-testing/src/renderers/common/RenderList.ts +++ /dev/null @@ -1,145 +0,0 @@ -import { LightsNode } from '../../nodes/Nodes.js'; - -function painterSortStable(a, b) { - if (a.groupOrder !== b.groupOrder) { - return a.groupOrder - b.groupOrder; - } else if (a.renderOrder !== b.renderOrder) { - return a.renderOrder - b.renderOrder; - } else if (a.material.id !== b.material.id) { - return a.material.id - b.material.id; - } else if (a.z !== b.z) { - return a.z - b.z; - } else { - return a.id - b.id; - } -} - -function reversePainterSortStable(a, b) { - if (a.groupOrder !== b.groupOrder) { - return a.groupOrder - b.groupOrder; - } else if (a.renderOrder !== b.renderOrder) { - return a.renderOrder - b.renderOrder; - } else if (a.z !== b.z) { - return b.z - a.z; - } else { - return a.id - b.id; - } -} - -class RenderList { - constructor() { - this.renderItems = []; - this.renderItemsIndex = 0; - - this.opaque = []; - this.transparent = []; - this.bundles = []; - - this.lightsNode = new LightsNode([]); - this.lightsArray = []; - - this.occlusionQueryCount = 0; - } - - begin() { - this.renderItemsIndex = 0; - - this.opaque.length = 0; - this.transparent.length = 0; - this.bundles.length = 0; - - this.lightsArray.length = 0; - - this.occlusionQueryCount = 0; - - return this; - } - - getNextRenderItem(object, geometry, material, groupOrder, z, group) { - let renderItem = this.renderItems[this.renderItemsIndex]; - - if (renderItem === undefined) { - renderItem = { - id: object.id, - object: object, - geometry: geometry, - material: material, - groupOrder: groupOrder, - renderOrder: object.renderOrder, - z: z, - group: group, - }; - - this.renderItems[this.renderItemsIndex] = renderItem; - } else { - renderItem.id = object.id; - renderItem.object = object; - renderItem.geometry = geometry; - renderItem.material = material; - renderItem.groupOrder = groupOrder; - renderItem.renderOrder = object.renderOrder; - renderItem.z = z; - renderItem.group = group; - } - - this.renderItemsIndex++; - - return renderItem; - } - - push(object, geometry, material, groupOrder, z, group) { - const renderItem = this.getNextRenderItem(object, geometry, material, groupOrder, z, group); - - if (object.occlusionTest === true) this.occlusionQueryCount++; - - (material.transparent === true || material.transmission > 0 ? this.transparent : this.opaque).push(renderItem); - } - - unshift(object, geometry, material, groupOrder, z, group) { - const renderItem = this.getNextRenderItem(object, geometry, material, groupOrder, z, group); - - (material.transparent === true ? this.transparent : this.opaque).unshift(renderItem); - } - - pushBundle(group) { - this.bundles.push(group); - } - - pushLight(light) { - this.lightsArray.push(light); - } - - getLightsNode() { - return this.lightsNode.fromLights(this.lightsArray); - } - - sort(customOpaqueSort, customTransparentSort) { - if (this.opaque.length > 1) this.opaque.sort(customOpaqueSort || painterSortStable); - if (this.transparent.length > 1) this.transparent.sort(customTransparentSort || reversePainterSortStable); - } - - finish() { - // update lights - - this.lightsNode.fromLights(this.lightsArray); - - // Clear references from inactive renderItems in the list - - for (let i = this.renderItemsIndex, il = this.renderItems.length; i < il; i++) { - const renderItem = this.renderItems[i]; - - if (renderItem.id === null) break; - - renderItem.id = null; - renderItem.object = null; - renderItem.geometry = null; - renderItem.material = null; - renderItem.groupOrder = null; - renderItem.renderOrder = null; - renderItem.z = null; - renderItem.group = null; - } - } -} - -export default RenderList; diff --git a/src-testing/src/renderers/common/RenderLists.ts b/src-testing/src/renderers/common/RenderLists.ts deleted file mode 100644 index 3fc3134e6..000000000 --- a/src-testing/src/renderers/common/RenderLists.ts +++ /dev/null @@ -1,28 +0,0 @@ -import ChainMap from './ChainMap.js'; -import RenderList from './RenderList.js'; - -class RenderLists { - constructor() { - this.lists = new ChainMap(); - } - - get(scene, camera) { - const lists = this.lists; - const keys = [scene, camera]; - - let list = lists.get(keys); - - if (list === undefined) { - list = new RenderList(); - lists.set(keys, list); - } - - return list; - } - - dispose() { - this.lists = new ChainMap(); - } -} - -export default RenderLists; diff --git a/src-testing/src/renderers/common/RenderObject.ts b/src-testing/src/renderers/common/RenderObject.ts deleted file mode 100644 index 703e3d206..000000000 --- a/src-testing/src/renderers/common/RenderObject.ts +++ /dev/null @@ -1,231 +0,0 @@ -import ClippingContext from './ClippingContext.js'; - -let _id = 0; - -function getKeys(obj) { - const keys = Object.keys(obj); - - let proto = Object.getPrototypeOf(obj); - - while (proto) { - const descriptors = Object.getOwnPropertyDescriptors(proto); - - for (const key in descriptors) { - if (descriptors[key] !== undefined) { - const descriptor = descriptors[key]; - - if (descriptor && typeof descriptor.get === 'function') { - keys.push(key); - } - } - } - - proto = Object.getPrototypeOf(proto); - } - - return keys; -} - -export default class RenderObject { - constructor(nodes, geometries, renderer, object, material, scene, camera, lightsNode, renderContext) { - this._nodes = nodes; - this._geometries = geometries; - - this.id = _id++; - - this.renderer = renderer; - this.object = object; - this.material = material; - this.scene = scene; - this.camera = camera; - this.lightsNode = lightsNode; - this.context = renderContext; - - this.geometry = object.geometry; - this.version = material.version; - - this.drawRange = null; - - this.attributes = null; - this.pipeline = null; - this.vertexBuffers = null; - - this.updateClipping(renderContext.clippingContext); - - this.clippingContextVersion = this.clippingContext.version; - - this.initialNodesCacheKey = this.getDynamicCacheKey(); - this.initialCacheKey = this.getCacheKey(); - - this._nodeBuilderState = null; - this._bindings = null; - - this.onDispose = null; - - this.isRenderObject = true; - - this.onMaterialDispose = () => { - this.dispose(); - }; - - this.material.addEventListener('dispose', this.onMaterialDispose); - } - - updateClipping(parent) { - const material = this.material; - - let clippingContext = this.clippingContext; - - if (Array.isArray(material.clippingPlanes)) { - if (clippingContext === parent || !clippingContext) { - clippingContext = new ClippingContext(); - this.clippingContext = clippingContext; - } - - clippingContext.update(parent, material); - } else if (this.clippingContext !== parent) { - this.clippingContext = parent; - } - } - - get clippingNeedsUpdate() { - if (this.clippingContext.version === this.clippingContextVersion) return false; - - this.clippingContextVersion = this.clippingContext.version; - - return true; - } - - getNodeBuilderState() { - return this._nodeBuilderState || (this._nodeBuilderState = this._nodes.getForRender(this)); - } - - getBindings() { - return this._bindings || (this._bindings = this.getNodeBuilderState().createBindings()); - } - - getIndex() { - return this._geometries.getIndex(this); - } - - getChainArray() { - return [this.object, this.material, this.context, this.lightsNode]; - } - - getAttributes() { - if (this.attributes !== null) return this.attributes; - - const nodeAttributes = this.getNodeBuilderState().nodeAttributes; - const geometry = this.geometry; - - const attributes = []; - const vertexBuffers = new Set(); - - for (const nodeAttribute of nodeAttributes) { - const attribute = - nodeAttribute.node && nodeAttribute.node.attribute - ? nodeAttribute.node.attribute - : geometry.getAttribute(nodeAttribute.name); - - if (attribute === undefined) continue; - - attributes.push(attribute); - - const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute; - vertexBuffers.add(bufferAttribute); - } - - this.attributes = attributes; - this.vertexBuffers = Array.from(vertexBuffers.values()); - - return attributes; - } - - getVertexBuffers() { - if (this.vertexBuffers === null) this.getAttributes(); - - return this.vertexBuffers; - } - - getMaterialCacheKey() { - const { object, material } = this; - - let cacheKey = material.customProgramCacheKey(); - - for (const property of getKeys(material)) { - if (/^(is[A-Z]|_)|^(visible|version|uuid|name|opacity|userData)$/.test(property)) continue; - - const value = material[property]; - - let valueKey; - - if (value !== null) { - // some material values require a formatting - - const type = typeof value; - - if (type === 'number') { - valueKey = value !== 0 ? '1' : '0'; // Convert to on/off, important for clearcoat, transmission, etc - } else if (type === 'object') { - valueKey = '{'; - - if (value.isTexture) { - valueKey += value.mapping; - } - - valueKey += '}'; - } else { - valueKey = String(value); - } - } else { - valueKey = String(value); - } - - cacheKey += /*property + ':' +*/ valueKey + ','; - } - - cacheKey += this.clippingContextVersion + ','; - - if (object.skeleton) { - cacheKey += object.skeleton.bones.length + ','; - } - - if (object.morphTargetInfluences) { - cacheKey += object.morphTargetInfluences.length + ','; - } - - if (object.isBatchedMesh) { - cacheKey += object._matricesTexture.uuid + ','; - - if (object._colorsTexture !== null) { - cacheKey += object._colorsTexture.uuid + ','; - } - } - - if (object.count > 1) { - cacheKey += object.count + ',' + object.uuid + ','; - } - - return cacheKey; - } - - get needsUpdate() { - return this.initialNodesCacheKey !== this.getDynamicCacheKey() || this.clippingNeedsUpdate; - } - - getDynamicCacheKey() { - // Environment Nodes Cache Key - - return this.object.receiveShadow + ',' + this._nodes.getCacheKey(this.scene, this.lightsNode); - } - - getCacheKey() { - return this.getMaterialCacheKey() + ',' + this.getDynamicCacheKey(); - } - - dispose() { - this.material.removeEventListener('dispose', this.onMaterialDispose); - - this.onDispose(); - } -} diff --git a/src-testing/src/renderers/common/RenderObjects.ts b/src-testing/src/renderers/common/RenderObjects.ts deleted file mode 100644 index 76dc482e4..000000000 --- a/src-testing/src/renderers/common/RenderObjects.ts +++ /dev/null @@ -1,100 +0,0 @@ -import ChainMap from './ChainMap.js'; -import RenderObject from './RenderObject.js'; - -class RenderObjects { - constructor(renderer, nodes, geometries, pipelines, bindings, info) { - this.renderer = renderer; - this.nodes = nodes; - this.geometries = geometries; - this.pipelines = pipelines; - this.bindings = bindings; - this.info = info; - - this.chainMaps = {}; - } - - get(object, material, scene, camera, lightsNode, renderContext, passId) { - const chainMap = this.getChainMap(passId); - const chainArray = [object, material, renderContext, lightsNode]; - - let renderObject = chainMap.get(chainArray); - - if (renderObject === undefined) { - renderObject = this.createRenderObject( - this.nodes, - this.geometries, - this.renderer, - object, - material, - scene, - camera, - lightsNode, - renderContext, - passId, - ); - - chainMap.set(chainArray, renderObject); - } else { - renderObject.updateClipping(renderContext.clippingContext); - - if (renderObject.version !== material.version || renderObject.needsUpdate) { - if (renderObject.initialCacheKey !== renderObject.getCacheKey()) { - renderObject.dispose(); - - renderObject = this.get(object, material, scene, camera, lightsNode, renderContext, passId); - } else { - renderObject.version = material.version; - } - } - } - - return renderObject; - } - - getChainMap(passId = 'default') { - return this.chainMaps[passId] || (this.chainMaps[passId] = new ChainMap()); - } - - dispose() { - this.chainMaps = {}; - } - - createRenderObject( - nodes, - geometries, - renderer, - object, - material, - scene, - camera, - lightsNode, - renderContext, - passId, - ) { - const chainMap = this.getChainMap(passId); - - const renderObject = new RenderObject( - nodes, - geometries, - renderer, - object, - material, - scene, - camera, - lightsNode, - renderContext, - ); - - renderObject.onDispose = () => { - this.pipelines.delete(renderObject); - this.bindings.delete(renderObject); - this.nodes.delete(renderObject); - - chainMap.delete(renderObject.getChainArray()); - }; - - return renderObject; - } -} - -export default RenderObjects; diff --git a/src-testing/src/renderers/common/RenderPipeline.ts b/src-testing/src/renderers/common/RenderPipeline.ts deleted file mode 100644 index 0ec34b043..000000000 --- a/src-testing/src/renderers/common/RenderPipeline.ts +++ /dev/null @@ -1,12 +0,0 @@ -import Pipeline from './Pipeline.js'; - -class RenderPipeline extends Pipeline { - constructor(cacheKey, vertexProgram, fragmentProgram) { - super(cacheKey); - - this.vertexProgram = vertexProgram; - this.fragmentProgram = fragmentProgram; - } -} - -export default RenderPipeline; diff --git a/src-testing/src/renderers/common/Renderer.ts b/src-testing/src/renderers/common/Renderer.ts deleted file mode 100644 index be803038b..000000000 --- a/src-testing/src/renderers/common/Renderer.ts +++ /dev/null @@ -1,1399 +0,0 @@ -import Animation from './Animation.js'; -import RenderObjects from './RenderObjects.js'; -import Attributes from './Attributes.js'; -import Geometries from './Geometries.js'; -import Info from './Info.js'; -import Pipelines from './Pipelines.js'; -import Bindings from './Bindings.js'; -import RenderLists from './RenderLists.js'; -import RenderContexts from './RenderContexts.js'; -import Textures from './Textures.js'; -import Background from './Background.js'; -import Nodes from './nodes/Nodes.js'; -import Color4 from './Color4.js'; -import ClippingContext from './ClippingContext.js'; -import QuadMesh from './QuadMesh.js'; -import RenderBundles from './RenderBundles.js'; - -import { NodeMaterial } from '../../nodes/Nodes.js'; - -import { Scene } from '../../scenes/Scene.js'; -import { Frustum } from '../../math/Frustum.js'; -import { Matrix4 } from '../../math/Matrix4.js'; -import { Vector2 } from '../../math/Vector2.js'; -import { Vector3 } from '../../math/Vector3.js'; -import { Vector4 } from '../../math/Vector4.js'; -import { RenderTarget } from '../../core/RenderTarget.js'; -import { - DoubleSide, - BackSide, - FrontSide, - SRGBColorSpace, - NoColorSpace, - NoToneMapping, - LinearFilter, - LinearSRGBColorSpace, - HalfFloatType, - RGBAFormat, - PCFShadowMap, -} from '../../constants.js'; - -const _scene = /*@__PURE__*/ new Scene(); -const _drawingBufferSize = /*@__PURE__*/ new Vector2(); -const _screen = /*@__PURE__*/ new Vector4(); -const _frustum = /*@__PURE__*/ new Frustum(); -const _projScreenMatrix = /*@__PURE__*/ new Matrix4(); -const _vector3 = /*@__PURE__*/ new Vector3(); - -class Renderer { - constructor(backend, parameters = {}) { - this.isRenderer = true; - - // - - const { logarithmicDepthBuffer = false, alpha = true, antialias = false, samples = 0 } = parameters; - - // public - this.domElement = backend.getDomElement(); - - this.backend = backend; - - this.samples = samples || antialias === true ? 4 : 0; - - this.autoClear = true; - this.autoClearColor = true; - this.autoClearDepth = true; - this.autoClearStencil = true; - - this.alpha = alpha; - - this.logarithmicDepthBuffer = logarithmicDepthBuffer; - - this.outputColorSpace = SRGBColorSpace; - - this.toneMapping = NoToneMapping; - this.toneMappingExposure = 1.0; - - this.sortObjects = true; - - this.depth = true; - this.stencil = false; - - this.clippingPlanes = []; - - this.info = new Info(); - - // internals - - this._pixelRatio = 1; - this._width = this.domElement.width; - this._height = this.domElement.height; - - this._viewport = new Vector4(0, 0, this._width, this._height); - this._scissor = new Vector4(0, 0, this._width, this._height); - this._scissorTest = false; - - this._attributes = null; - this._geometries = null; - this._nodes = null; - this._animation = null; - this._bindings = null; - this._objects = null; - this._pipelines = null; - this._bundles = null; - this._renderLists = null; - this._renderContexts = null; - this._textures = null; - this._background = null; - - this._quad = new QuadMesh(new NodeMaterial()); - - this._currentRenderContext = null; - - this._opaqueSort = null; - this._transparentSort = null; - - this._frameBufferTarget = null; - - const alphaClear = this.alpha === true ? 0 : 1; - - this._clearColor = new Color4(0, 0, 0, alphaClear); - this._clearDepth = 1; - this._clearStencil = 0; - - this._renderTarget = null; - this._activeCubeFace = 0; - this._activeMipmapLevel = 0; - - this._mrt = null; - - this._renderObjectFunction = null; - this._currentRenderObjectFunction = null; - this._currentRenderBundle = null; - - this._handleObjectFunction = this._renderObjectDirect; - - this._initialized = false; - this._initPromise = null; - - this._compilationPromises = null; - - this.transparent = true; - this.opaque = true; - - this.shadowMap = { - enabled: false, - type: PCFShadowMap, - }; - - this.xr = { - enabled: false, - }; - - this.debug = { - checkShaderErrors: true, - onShaderError: null, - getShaderAsync: async (scene, camera, object) => { - await this.compileAsync(scene, camera); - - const renderList = this._renderLists.get(scene, camera); - const renderContext = this._renderContexts.get(scene, camera, this._renderTarget); - - const material = scene.overrideMaterial || object.material; - - const renderObject = this._objects.get( - object, - material, - scene, - camera, - renderList.lightsNode, - renderContext, - ); - - const { fragmentShader, vertexShader } = renderObject.getNodeBuilderState(); - - return { fragmentShader, vertexShader }; - }, - }; - } - - async init() { - if (this._initialized) { - throw new Error('Renderer: Backend has already been initialized.'); - } - - if (this._initPromise !== null) { - return this._initPromise; - } - - this._initPromise = new Promise(async (resolve, reject) => { - const backend = this.backend; - - try { - await backend.init(this); - } catch (error) { - reject(error); - return; - } - - this._nodes = new Nodes(this, backend); - this._animation = new Animation(this._nodes, this.info); - this._attributes = new Attributes(backend); - this._background = new Background(this, this._nodes); - this._geometries = new Geometries(this._attributes, this.info); - this._textures = new Textures(this, backend, this.info); - this._pipelines = new Pipelines(backend, this._nodes); - this._bindings = new Bindings( - backend, - this._nodes, - this._textures, - this._attributes, - this._pipelines, - this.info, - ); - this._objects = new RenderObjects( - this, - this._nodes, - this._geometries, - this._pipelines, - this._bindings, - this.info, - ); - this._renderLists = new RenderLists(); - this._bundles = new RenderBundles(); - this._renderContexts = new RenderContexts(); - - // - - this._initialized = true; - - resolve(); - }); - - return this._initPromise; - } - - get coordinateSystem() { - return this.backend.coordinateSystem; - } - - async compileAsync(scene, camera, targetScene = null) { - if (this._initialized === false) await this.init(); - - // preserve render tree - - const nodeFrame = this._nodes.nodeFrame; - - const previousRenderId = nodeFrame.renderId; - const previousRenderContext = this._currentRenderContext; - const previousRenderObjectFunction = this._currentRenderObjectFunction; - const previousCompilationPromises = this._compilationPromises; - - // - - const sceneRef = scene.isScene === true ? scene : _scene; - - if (targetScene === null) targetScene = scene; - - const renderTarget = this._renderTarget; - const renderContext = this._renderContexts.get(targetScene, camera, renderTarget); - const activeMipmapLevel = this._activeMipmapLevel; - - const compilationPromises = []; - - this._currentRenderContext = renderContext; - this._currentRenderObjectFunction = this.renderObject; - - this._handleObjectFunction = this._createObjectPipeline; - - this._compilationPromises = compilationPromises; - - nodeFrame.renderId++; - - // - - nodeFrame.update(); - - // - - renderContext.depth = this.depth; - renderContext.stencil = this.stencil; - - if (!renderContext.clippingContext) renderContext.clippingContext = new ClippingContext(); - renderContext.clippingContext.updateGlobal(this, camera); - - // - - sceneRef.onBeforeRender(this, scene, camera, renderTarget); - - // - - const renderList = this._renderLists.get(scene, camera); - renderList.begin(); - - this._projectObject(scene, camera, 0, renderList); - - // include lights from target scene - if (targetScene !== scene) { - targetScene.traverseVisible(function (object) { - if (object.isLight && object.layers.test(camera.layers)) { - renderList.pushLight(object); - } - }); - } - - renderList.finish(); - - // - - if (renderTarget !== null) { - this._textures.updateRenderTarget(renderTarget, activeMipmapLevel); - - const renderTargetData = this._textures.get(renderTarget); - - renderContext.textures = renderTargetData.textures; - renderContext.depthTexture = renderTargetData.depthTexture; - } else { - renderContext.textures = null; - renderContext.depthTexture = null; - } - - // - - this._nodes.updateScene(sceneRef); - - // - - this._background.update(sceneRef, renderList, renderContext); - - // process render lists - - const opaqueObjects = renderList.opaque; - const transparentObjects = renderList.transparent; - const lightsNode = renderList.lightsNode; - - if (this.opaque === true && opaqueObjects.length > 0) - this._renderObjects(opaqueObjects, camera, sceneRef, lightsNode); - if (this.transparent === true && transparentObjects.length > 0) - this._renderObjects(transparentObjects, camera, sceneRef, lightsNode); - - // restore render tree - - nodeFrame.renderId = previousRenderId; - - this._currentRenderContext = previousRenderContext; - this._currentRenderObjectFunction = previousRenderObjectFunction; - this._compilationPromises = previousCompilationPromises; - - this._handleObjectFunction = this._renderObjectDirect; - - // wait for all promises setup by backends awaiting compilation/linking/pipeline creation to complete - - await Promise.all(compilationPromises); - } - - async renderAsync(scene, camera) { - if (this._initialized === false) await this.init(); - - const renderContext = this._renderScene(scene, camera); - - await this.backend.resolveTimestampAsync(renderContext, 'render'); - } - - setMRT(mrt) { - this._mrt = mrt; - - return this; - } - - getMRT() { - return this._mrt; - } - - _renderBundle(bundle, sceneRef, lightsNode) { - const { object, camera, renderList } = bundle; - - const renderContext = this._currentRenderContext; - const renderContextData = this.backend.get(renderContext); - - // - - const renderBundle = this._bundles.get(object, camera); - - const renderBundleData = this.backend.get(renderBundle); - if (renderBundleData.renderContexts === undefined) renderBundleData.renderContexts = new Set(); - - // - - const renderBundleNeedsUpdate = - renderBundleData.renderContexts.has(renderContext) === false || object.needsUpdate === true; - - renderBundleData.renderContexts.add(renderContext); - - if (renderBundleNeedsUpdate) { - if (renderContextData.renderObjects === undefined || object.needsUpdate === true) { - const nodeFrame = this._nodes.nodeFrame; - - renderContextData.renderObjects = []; - renderContextData.renderBundles = []; - renderContextData.scene = sceneRef; - renderContextData.camera = camera; - renderContextData.renderId = nodeFrame.renderId; - - renderContextData.registerBundlesPhase = true; - } - - this._currentRenderBundle = renderBundle; - - const opaqueObjects = renderList.opaque; - - if (opaqueObjects.length > 0) this._renderObjects(opaqueObjects, camera, sceneRef, lightsNode); - - this._currentRenderBundle = null; - - // - - object.needsUpdate = false; - } else { - const renderContext = this._currentRenderContext; - const renderContextData = this.backend.get(renderContext); - - for (let i = 0, l = renderContextData.renderObjects.length; i < l; i++) { - const renderObject = renderContextData.renderObjects[i]; - - this._nodes.updateBefore(renderObject); - - // - - renderObject.object.modelViewMatrix.multiplyMatrices( - camera.matrixWorldInverse, - renderObject.object.matrixWorld, - ); - renderObject.object.normalMatrix.getNormalMatrix(renderObject.object.modelViewMatrix); - - this._nodes.updateForRender(renderObject); - this._bindings.updateForRender(renderObject); - - this.backend.draw(renderObject, this.info); - - this._nodes.updateAfter(renderObject); - } - } - } - - render(scene, camera) { - if (this._initialized === false) { - console.warn( - 'THREE.Renderer: .render() called before the backend is initialized. Try using .renderAsync() instead.', - ); - - return this.renderAsync(scene, camera); - } - - this._renderScene(scene, camera); - } - - _getFrameBufferTarget() { - const { currentColorSpace } = this; - - const useToneMapping = this._renderTarget === null && this.toneMapping !== NoToneMapping; - const useColorSpace = - this._renderTarget === null && - currentColorSpace !== LinearSRGBColorSpace && - currentColorSpace !== NoColorSpace; - - if (useToneMapping === false && useColorSpace === false) return null; - - const { width, height } = this.getDrawingBufferSize(_drawingBufferSize); - const { depth, stencil } = this; - - let frameBufferTarget = this._frameBufferTarget; - - if (frameBufferTarget === null) { - frameBufferTarget = new RenderTarget(width, height, { - depthBuffer: depth, - stencilBuffer: stencil, - type: HalfFloatType, // FloatType - format: RGBAFormat, - colorSpace: LinearSRGBColorSpace, - generateMipmaps: false, - minFilter: LinearFilter, - magFilter: LinearFilter, - samples: this.samples, - }); - - frameBufferTarget.isPostProcessingRenderTarget = true; - - this._frameBufferTarget = frameBufferTarget; - } - - frameBufferTarget.depthBuffer = depth; - frameBufferTarget.stencilBuffer = stencil; - frameBufferTarget.setSize(width, height); - frameBufferTarget.viewport.copy(this._viewport); - frameBufferTarget.scissor.copy(this._scissor); - frameBufferTarget.viewport.multiplyScalar(this._pixelRatio); - frameBufferTarget.scissor.multiplyScalar(this._pixelRatio); - frameBufferTarget.scissorTest = this._scissorTest; - - return frameBufferTarget; - } - - _renderScene(scene, camera, useFrameBufferTarget = true) { - const frameBufferTarget = useFrameBufferTarget ? this._getFrameBufferTarget() : null; - - // preserve render tree - - const nodeFrame = this._nodes.nodeFrame; - - const previousRenderId = nodeFrame.renderId; - const previousRenderContext = this._currentRenderContext; - const previousRenderObjectFunction = this._currentRenderObjectFunction; - - // - - const sceneRef = scene.isScene === true ? scene : _scene; - - const outputRenderTarget = this._renderTarget; - - const activeCubeFace = this._activeCubeFace; - const activeMipmapLevel = this._activeMipmapLevel; - - // - - let renderTarget; - - if (frameBufferTarget !== null) { - renderTarget = frameBufferTarget; - - this.setRenderTarget(renderTarget); - } else { - renderTarget = outputRenderTarget; - } - - // - - const renderContext = this._renderContexts.get(scene, camera, renderTarget); - - this._currentRenderContext = renderContext; - this._currentRenderObjectFunction = this._renderObjectFunction || this.renderObject; - - // - - this.info.calls++; - this.info.render.calls++; - this.info.render.frameCalls++; - - nodeFrame.renderId = this.info.calls; - - // - - const coordinateSystem = this.coordinateSystem; - - if (camera.coordinateSystem !== coordinateSystem) { - camera.coordinateSystem = coordinateSystem; - - camera.updateProjectionMatrix(); - } - - // - - if (scene.matrixWorldAutoUpdate === true) scene.updateMatrixWorld(); - - if (camera.parent === null && camera.matrixWorldAutoUpdate === true) camera.updateMatrixWorld(); - - // - - let viewport = this._viewport; - let scissor = this._scissor; - let pixelRatio = this._pixelRatio; - - if (renderTarget !== null) { - viewport = renderTarget.viewport; - scissor = renderTarget.scissor; - pixelRatio = 1; - } - - this.getDrawingBufferSize(_drawingBufferSize); - - _screen.set(0, 0, _drawingBufferSize.width, _drawingBufferSize.height); - - const minDepth = viewport.minDepth === undefined ? 0 : viewport.minDepth; - const maxDepth = viewport.maxDepth === undefined ? 1 : viewport.maxDepth; - - renderContext.viewportValue.copy(viewport).multiplyScalar(pixelRatio).floor(); - renderContext.viewportValue.width >>= activeMipmapLevel; - renderContext.viewportValue.height >>= activeMipmapLevel; - renderContext.viewportValue.minDepth = minDepth; - renderContext.viewportValue.maxDepth = maxDepth; - renderContext.viewport = renderContext.viewportValue.equals(_screen) === false; - - renderContext.scissorValue.copy(scissor).multiplyScalar(pixelRatio).floor(); - renderContext.scissor = this._scissorTest && renderContext.scissorValue.equals(_screen) === false; - renderContext.scissorValue.width >>= activeMipmapLevel; - renderContext.scissorValue.height >>= activeMipmapLevel; - - if (!renderContext.clippingContext) renderContext.clippingContext = new ClippingContext(); - renderContext.clippingContext.updateGlobal(this, camera); - - // - - sceneRef.onBeforeRender(this, scene, camera, renderTarget); - - // - - _projScreenMatrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse); - _frustum.setFromProjectionMatrix(_projScreenMatrix, coordinateSystem); - - const renderList = this._renderLists.get(scene, camera); - renderList.begin(); - - this._projectObject(scene, camera, 0, renderList); - - renderList.finish(); - - if (this.sortObjects === true) { - renderList.sort(this._opaqueSort, this._transparentSort); - } - - // - - if (renderTarget !== null) { - this._textures.updateRenderTarget(renderTarget, activeMipmapLevel); - - const renderTargetData = this._textures.get(renderTarget); - - renderContext.textures = renderTargetData.textures; - renderContext.depthTexture = renderTargetData.depthTexture; - renderContext.width = renderTargetData.width; - renderContext.height = renderTargetData.height; - renderContext.renderTarget = renderTarget; - renderContext.depth = renderTarget.depthBuffer; - renderContext.stencil = renderTarget.stencilBuffer; - } else { - renderContext.textures = null; - renderContext.depthTexture = null; - renderContext.width = this.domElement.width; - renderContext.height = this.domElement.height; - renderContext.depth = this.depth; - renderContext.stencil = this.stencil; - } - - renderContext.width >>= activeMipmapLevel; - renderContext.height >>= activeMipmapLevel; - renderContext.activeCubeFace = activeCubeFace; - renderContext.activeMipmapLevel = activeMipmapLevel; - renderContext.occlusionQueryCount = renderList.occlusionQueryCount; - - // - - this._nodes.updateScene(sceneRef); - - // - - this._background.update(sceneRef, renderList, renderContext); - - // - - this.backend.beginRender(renderContext); - - // process render lists - - const opaqueObjects = renderList.opaque; - const transparentObjects = renderList.transparent; - const bundles = renderList.bundles; - const lightsNode = renderList.lightsNode; - - if (bundles.length > 0) this._renderBundles(bundles, sceneRef, lightsNode); - if (this.opaque === true && opaqueObjects.length > 0) - this._renderObjects(opaqueObjects, camera, sceneRef, lightsNode); - if (this.transparent === true && transparentObjects.length > 0) - this._renderObjects(transparentObjects, camera, sceneRef, lightsNode); - - // finish render pass - - this.backend.finishRender(renderContext); - - // restore render tree - - nodeFrame.renderId = previousRenderId; - - this._currentRenderContext = previousRenderContext; - this._currentRenderObjectFunction = previousRenderObjectFunction; - - // - - if (frameBufferTarget !== null) { - this.setRenderTarget(outputRenderTarget, activeCubeFace, activeMipmapLevel); - - const quad = this._quad; - - if (this._nodes.hasOutputChange(renderTarget.texture)) { - quad.material.fragmentNode = this._nodes.getOutputNode(renderTarget.texture); - quad.material.needsUpdate = true; - } - - this._renderScene(quad, quad.camera, false); - } - - // - - sceneRef.onAfterRender(this, scene, camera, renderTarget); - - // - - return renderContext; - } - - getMaxAnisotropy() { - return this.backend.getMaxAnisotropy(); - } - - getActiveCubeFace() { - return this._activeCubeFace; - } - - getActiveMipmapLevel() { - return this._activeMipmapLevel; - } - - async setAnimationLoop(callback) { - if (this._initialized === false) await this.init(); - - this._animation.setAnimationLoop(callback); - } - - async getArrayBufferAsync(attribute) { - return await this.backend.getArrayBufferAsync(attribute); - } - - getContext() { - return this.backend.getContext(); - } - - getPixelRatio() { - return this._pixelRatio; - } - - getDrawingBufferSize(target) { - return target.set(this._width * this._pixelRatio, this._height * this._pixelRatio).floor(); - } - - getSize(target) { - return target.set(this._width, this._height); - } - - setPixelRatio(value = 1) { - this._pixelRatio = value; - - this.setSize(this._width, this._height, false); - } - - setDrawingBufferSize(width, height, pixelRatio) { - this._width = width; - this._height = height; - - this._pixelRatio = pixelRatio; - - this.domElement.width = Math.floor(width * pixelRatio); - this.domElement.height = Math.floor(height * pixelRatio); - - this.setViewport(0, 0, width, height); - - if (this._initialized) this.backend.updateSize(); - } - - setSize(width, height, updateStyle = true) { - this._width = width; - this._height = height; - - this.domElement.width = Math.floor(width * this._pixelRatio); - this.domElement.height = Math.floor(height * this._pixelRatio); - - if (updateStyle === true) { - this.domElement.style.width = width + 'px'; - this.domElement.style.height = height + 'px'; - } - - this.setViewport(0, 0, width, height); - - if (this._initialized) this.backend.updateSize(); - } - - setOpaqueSort(method) { - this._opaqueSort = method; - } - - setTransparentSort(method) { - this._transparentSort = method; - } - - getScissor(target) { - const scissor = this._scissor; - - target.x = scissor.x; - target.y = scissor.y; - target.width = scissor.width; - target.height = scissor.height; - - return target; - } - - setScissor(x, y, width, height) { - const scissor = this._scissor; - - if (x.isVector4) { - scissor.copy(x); - } else { - scissor.set(x, y, width, height); - } - } - - getScissorTest() { - return this._scissorTest; - } - - setScissorTest(boolean) { - this._scissorTest = boolean; - - this.backend.setScissorTest(boolean); - } - - getViewport(target) { - return target.copy(this._viewport); - } - - setViewport(x, y, width, height, minDepth = 0, maxDepth = 1) { - const viewport = this._viewport; - - if (x.isVector4) { - viewport.copy(x); - } else { - viewport.set(x, y, width, height); - } - - viewport.minDepth = minDepth; - viewport.maxDepth = maxDepth; - } - - getClearColor(target) { - return target.copy(this._clearColor); - } - - setClearColor(color, alpha = 1) { - this._clearColor.set(color); - this._clearColor.a = alpha; - } - - getClearAlpha() { - return this._clearColor.a; - } - - setClearAlpha(alpha) { - this._clearColor.a = alpha; - } - - getClearDepth() { - return this._clearDepth; - } - - setClearDepth(depth) { - this._clearDepth = depth; - } - - getClearStencil() { - return this._clearStencil; - } - - setClearStencil(stencil) { - this._clearStencil = stencil; - } - - isOccluded(object) { - const renderContext = this._currentRenderContext; - - return renderContext && this.backend.isOccluded(renderContext, object); - } - - clear(color = true, depth = true, stencil = true) { - if (this._initialized === false) { - console.warn( - 'THREE.Renderer: .clear() called before the backend is initialized. Try using .clearAsync() instead.', - ); - - return this.clearAsync(color, depth, stencil); - } - - const renderTarget = this._renderTarget || this._getFrameBufferTarget(); - - let renderTargetData = null; - - if (renderTarget !== null) { - this._textures.updateRenderTarget(renderTarget); - - renderTargetData = this._textures.get(renderTarget); - } - - this.backend.clear(color, depth, stencil, renderTargetData); - - if (renderTarget !== null && this._renderTarget === null) { - // If a color space transform or tone mapping is required, - // the clear operation clears the intermediate renderTarget texture, but does not update the screen canvas. - - const quad = this._quad; - - if (this._nodes.hasOutputChange(renderTarget.texture)) { - quad.material.fragmentNode = this._nodes.getOutputNode(renderTarget.texture); - quad.material.needsUpdate = true; - } - - this._renderScene(quad, quad.camera, false); - } - } - - clearColor() { - return this.clear(true, false, false); - } - - clearDepth() { - return this.clear(false, true, false); - } - - clearStencil() { - return this.clear(false, false, true); - } - - async clearAsync(color = true, depth = true, stencil = true) { - if (this._initialized === false) await this.init(); - - this.clear(color, depth, stencil); - } - - clearColorAsync() { - return this.clearAsync(true, false, false); - } - - clearDepthAsync() { - return this.clearAsync(false, true, false); - } - - clearStencilAsync() { - return this.clearAsync(false, false, true); - } - - get currentColorSpace() { - const renderTarget = this._renderTarget; - - if (renderTarget !== null) { - const texture = renderTarget.texture; - - return (Array.isArray(texture) ? texture[0] : texture).colorSpace; - } - - return this.outputColorSpace; - } - - dispose() { - this.info.dispose(); - - this._animation.dispose(); - this._objects.dispose(); - this._pipelines.dispose(); - this._nodes.dispose(); - this._bindings.dispose(); - this._renderLists.dispose(); - this._renderContexts.dispose(); - this._textures.dispose(); - - this.setRenderTarget(null); - this.setAnimationLoop(null); - } - - setRenderTarget(renderTarget, activeCubeFace = 0, activeMipmapLevel = 0) { - this._renderTarget = renderTarget; - this._activeCubeFace = activeCubeFace; - this._activeMipmapLevel = activeMipmapLevel; - } - - getRenderTarget() { - return this._renderTarget; - } - - setRenderObjectFunction(renderObjectFunction) { - this._renderObjectFunction = renderObjectFunction; - } - - getRenderObjectFunction() { - return this._renderObjectFunction; - } - - async computeAsync(computeNodes) { - if (this._initialized === false) await this.init(); - - const nodeFrame = this._nodes.nodeFrame; - - const previousRenderId = nodeFrame.renderId; - - // - - this.info.calls++; - this.info.compute.calls++; - this.info.compute.frameCalls++; - - nodeFrame.renderId = this.info.calls; - - // - - const backend = this.backend; - const pipelines = this._pipelines; - const bindings = this._bindings; - const nodes = this._nodes; - - const computeList = Array.isArray(computeNodes) ? computeNodes : [computeNodes]; - - if (computeList[0] === undefined || computeList[0].isComputeNode !== true) { - throw new Error('THREE.Renderer: .compute() expects a ComputeNode.'); - } - - backend.beginCompute(computeNodes); - - for (const computeNode of computeList) { - // onInit - - if (pipelines.has(computeNode) === false) { - const dispose = () => { - computeNode.removeEventListener('dispose', dispose); - - pipelines.delete(computeNode); - bindings.delete(computeNode); - nodes.delete(computeNode); - }; - - computeNode.addEventListener('dispose', dispose); - - // - - computeNode.onInit({ renderer: this }); - } - - nodes.updateForCompute(computeNode); - bindings.updateForCompute(computeNode); - - const computeBindings = bindings.getForCompute(computeNode); - const computePipeline = pipelines.getForCompute(computeNode, computeBindings); - - backend.compute(computeNodes, computeNode, computeBindings, computePipeline); - } - - backend.finishCompute(computeNodes); - - await this.backend.resolveTimestampAsync(computeNodes, 'compute'); - - // - - nodeFrame.renderId = previousRenderId; - } - - async hasFeatureAsync(name) { - if (this._initialized === false) await this.init(); - - return this.backend.hasFeature(name); - } - - hasFeature(name) { - if (this._initialized === false) { - console.warn( - 'THREE.Renderer: .hasFeature() called before the backend is initialized. Try using .hasFeatureAsync() instead.', - ); - - return false; - } - - return this.backend.hasFeature(name); - } - - copyFramebufferToTexture(framebufferTexture) { - const renderContext = this._currentRenderContext; - - this._textures.updateTexture(framebufferTexture); - - this.backend.copyFramebufferToTexture(framebufferTexture, renderContext); - } - - copyTextureToTexture(srcTexture, dstTexture, srcRegion = null, dstPosition = null, level = 0) { - this._textures.updateTexture(srcTexture); - this._textures.updateTexture(dstTexture); - - this.backend.copyTextureToTexture(srcTexture, dstTexture, srcRegion, dstPosition, level); - } - - readRenderTargetPixelsAsync(renderTarget, x, y, width, height, index = 0) { - return this.backend.copyTextureToBuffer(renderTarget.textures[index], x, y, width, height); - } - - _projectObject(object, camera, groupOrder, renderList) { - if (object.visible === false) return; - - const visible = object.layers.test(camera.layers); - - if (visible) { - if (object.isGroup) { - groupOrder = object.renderOrder; - } else if (object.isLOD) { - if (object.autoUpdate === true) object.update(camera); - } else if (object.isLight) { - renderList.pushLight(object); - } else if (object.isSprite) { - if (!object.frustumCulled || _frustum.intersectsSprite(object)) { - if (this.sortObjects === true) { - _vector3.setFromMatrixPosition(object.matrixWorld).applyMatrix4(_projScreenMatrix); - } - - const geometry = object.geometry; - const material = object.material; - - if (material.visible) { - renderList.push(object, geometry, material, groupOrder, _vector3.z, null); - } - } - } else if (object.isLineLoop) { - console.error( - 'THREE.Renderer: Objects of type THREE.LineLoop are not supported. Please use THREE.Line or THREE.LineSegments.', - ); - } else if (object.isMesh || object.isLine || object.isPoints) { - if (!object.frustumCulled || _frustum.intersectsObject(object)) { - const geometry = object.geometry; - const material = object.material; - - if (this.sortObjects === true) { - if (geometry.boundingSphere === null) geometry.computeBoundingSphere(); - - _vector3 - .copy(geometry.boundingSphere.center) - .applyMatrix4(object.matrixWorld) - .applyMatrix4(_projScreenMatrix); - } - - if (Array.isArray(material)) { - const groups = geometry.groups; - - for (let i = 0, l = groups.length; i < l; i++) { - const group = groups[i]; - const groupMaterial = material[group.materialIndex]; - - if (groupMaterial && groupMaterial.visible) { - renderList.push(object, geometry, groupMaterial, groupOrder, _vector3.z, group); - } - } - } else if (material.visible) { - renderList.push(object, geometry, material, groupOrder, _vector3.z, null); - } - } - } - } - - if (object.static === true) { - const baseRenderList = renderList; - - // replace render list - renderList = this._renderLists.get(object, camera); - - renderList.begin(); - - baseRenderList.pushBundle({ - object, - camera, - renderList, - }); - - renderList.finish(); - } - - const children = object.children; - - for (let i = 0, l = children.length; i < l; i++) { - this._projectObject(children[i], camera, groupOrder, renderList); - } - } - - _renderBundles(bundles, sceneRef, lightsNode) { - for (const bundle of bundles) { - this._renderBundle(bundle, sceneRef, lightsNode); - } - } - - _renderObjects(renderList, camera, scene, lightsNode) { - // process renderable objects - - for (let i = 0, il = renderList.length; i < il; i++) { - const renderItem = renderList[i]; - - // @TODO: Add support for multiple materials per object. This will require to extract - // the material from the renderItem object and pass it with its group data to renderObject(). - - const { object, geometry, material, group } = renderItem; - - if (camera.isArrayCamera) { - const cameras = camera.cameras; - - for (let j = 0, jl = cameras.length; j < jl; j++) { - const camera2 = cameras[j]; - - if (object.layers.test(camera2.layers)) { - const vp = camera2.viewport; - const minDepth = vp.minDepth === undefined ? 0 : vp.minDepth; - const maxDepth = vp.maxDepth === undefined ? 1 : vp.maxDepth; - - const viewportValue = this._currentRenderContext.viewportValue; - viewportValue.copy(vp).multiplyScalar(this._pixelRatio).floor(); - viewportValue.minDepth = minDepth; - viewportValue.maxDepth = maxDepth; - - this.backend.updateViewport(this._currentRenderContext); - - this._currentRenderObjectFunction( - object, - scene, - camera2, - geometry, - material, - group, - lightsNode, - ); - } - } - } else { - this._currentRenderObjectFunction(object, scene, camera, geometry, material, group, lightsNode); - } - } - } - - renderObject(object, scene, camera, geometry, material, group, lightsNode) { - let overridePositionNode; - let overrideFragmentNode; - let overrideDepthNode; - - // - - object.onBeforeRender(this, scene, camera, geometry, material, group); - - // - - if (scene.overrideMaterial !== null) { - const overrideMaterial = scene.overrideMaterial; - - if (material.positionNode && material.positionNode.isNode) { - overridePositionNode = overrideMaterial.positionNode; - overrideMaterial.positionNode = material.positionNode; - } - - if (overrideMaterial.isShadowNodeMaterial) { - overrideMaterial.side = material.shadowSide === null ? material.side : material.shadowSide; - - if (material.depthNode && material.depthNode.isNode) { - overrideDepthNode = overrideMaterial.depthNode; - overrideMaterial.depthNode = material.depthNode; - } - - if (material.shadowNode && material.shadowNode.isNode) { - overrideFragmentNode = overrideMaterial.fragmentNode; - overrideMaterial.fragmentNode = material.shadowNode; - } - - if (this.localClippingEnabled) { - if (material.clipShadows) { - if (overrideMaterial.clippingPlanes !== material.clippingPlanes) { - overrideMaterial.clippingPlanes = material.clippingPlanes; - overrideMaterial.needsUpdate = true; - } - - if (overrideMaterial.clipIntersection !== material.clipIntersection) { - overrideMaterial.clipIntersection = material.clipIntersection; - } - } else if (Array.isArray(overrideMaterial.clippingPlanes)) { - overrideMaterial.clippingPlanes = null; - overrideMaterial.needsUpdate = true; - } - } - } - - material = overrideMaterial; - } - - // - - if (material.transparent === true && material.side === DoubleSide && material.forceSinglePass === false) { - material.side = BackSide; - this._handleObjectFunction(object, material, scene, camera, lightsNode, group, 'backSide'); // create backSide pass id - - material.side = FrontSide; - this._handleObjectFunction(object, material, scene, camera, lightsNode, group); // use default pass id - - material.side = DoubleSide; - } else { - this._handleObjectFunction(object, material, scene, camera, lightsNode, group); - } - - // - - if (overridePositionNode !== undefined) { - scene.overrideMaterial.positionNode = overridePositionNode; - } - - if (overrideDepthNode !== undefined) { - scene.overrideMaterial.depthNode = overrideDepthNode; - } - - if (overrideFragmentNode !== undefined) { - scene.overrideMaterial.fragmentNode = overrideFragmentNode; - } - - // - - object.onAfterRender(this, scene, camera, geometry, material, group); - } - - _renderObjectDirect(object, material, scene, camera, lightsNode, group, passId) { - const renderObject = this._objects.get( - object, - material, - scene, - camera, - lightsNode, - this._currentRenderContext, - passId, - ); - renderObject.drawRange = group || object.geometry.drawRange; - - // - - this._nodes.updateBefore(renderObject); - - // - - object.modelViewMatrix.multiplyMatrices(camera.matrixWorldInverse, object.matrixWorld); - object.normalMatrix.getNormalMatrix(object.modelViewMatrix); - - // - - this._nodes.updateForRender(renderObject); - this._geometries.updateForRender(renderObject); - this._bindings.updateForRender(renderObject); - this._pipelines.updateForRender(renderObject); - - // - - if (this._currentRenderBundle !== null && this._currentRenderBundle.needsUpdate === true) { - const renderObjectData = this.backend.get(renderObject); - - renderObjectData.bundleEncoder = undefined; - renderObjectData.lastPipelineGPU = undefined; - } - - this.backend.draw(renderObject, this.info); - - if (this._currentRenderBundle !== null) { - const renderContextData = this.backend.get(this._currentRenderContext); - - renderContextData.renderObjects.push(renderObject); - } - - this._nodes.updateAfter(renderObject); - } - - _createObjectPipeline(object, material, scene, camera, lightsNode, passId) { - const renderObject = this._objects.get( - object, - material, - scene, - camera, - lightsNode, - this._currentRenderContext, - passId, - ); - - // - - this._nodes.updateBefore(renderObject); - - // - - this._nodes.updateForRender(renderObject); - this._geometries.updateForRender(renderObject); - this._bindings.updateForRender(renderObject); - - this._pipelines.getForRender(renderObject, this._compilationPromises); - - this._nodes.updateAfter(renderObject); - } - - get compute() { - return this.computeAsync; - } - - get compile() { - return this.compileAsync; - } -} - -export default Renderer; diff --git a/src-testing/src/renderers/common/SampledTexture.ts b/src-testing/src/renderers/common/SampledTexture.ts deleted file mode 100644 index c671cb09a..000000000 --- a/src-testing/src/renderers/common/SampledTexture.ts +++ /dev/null @@ -1,61 +0,0 @@ -import Binding from './Binding.js'; - -let _id = 0; - -class SampledTexture extends Binding { - constructor(name, texture) { - super(name); - - this.id = _id++; - - this.texture = texture; - this.version = texture ? texture.version : 0; - this.store = false; - - this.isSampledTexture = true; - } - - get needsBindingsUpdate() { - const { texture, version } = this; - - return texture.isVideoTexture ? true : version !== texture.version; // @TODO: version === 0 && texture.version > 0 ( add it just to External Textures like PNG,JPG ) - } - - update() { - const { texture, version } = this; - - if (version !== texture.version) { - this.version = texture.version; - - return true; - } - - return false; - } -} - -class SampledArrayTexture extends SampledTexture { - constructor(name, texture) { - super(name, texture); - - this.isSampledArrayTexture = true; - } -} - -class Sampled3DTexture extends SampledTexture { - constructor(name, texture) { - super(name, texture); - - this.isSampled3DTexture = true; - } -} - -class SampledCubeTexture extends SampledTexture { - constructor(name, texture) { - super(name, texture); - - this.isSampledCubeTexture = true; - } -} - -export { SampledTexture, SampledArrayTexture, Sampled3DTexture, SampledCubeTexture }; diff --git a/src-testing/src/renderers/common/Sampler.ts b/src-testing/src/renderers/common/Sampler.ts deleted file mode 100644 index 8cd20d04a..000000000 --- a/src-testing/src/renderers/common/Sampler.ts +++ /dev/null @@ -1,14 +0,0 @@ -import Binding from './Binding.js'; - -class Sampler extends Binding { - constructor(name, texture) { - super(name); - - this.texture = texture; - this.version = texture ? texture.version : 0; - - this.isSampler = true; - } -} - -export default Sampler; diff --git a/src-testing/src/renderers/common/StorageBuffer.ts b/src-testing/src/renderers/common/StorageBuffer.ts deleted file mode 100644 index ef5d3e464..000000000 --- a/src-testing/src/renderers/common/StorageBuffer.ts +++ /dev/null @@ -1,13 +0,0 @@ -import Buffer from './Buffer.js'; - -class StorageBuffer extends Buffer { - constructor(name, attribute) { - super(name, attribute ? attribute.array : null); - - this.attribute = attribute; - - this.isStorageBuffer = true; - } -} - -export default StorageBuffer; diff --git a/src-testing/src/renderers/common/StorageBufferAttribute.d.ts b/src-testing/src/renderers/common/StorageBufferAttribute.d.ts deleted file mode 100644 index 2a864f54a..000000000 --- a/src-testing/src/renderers/common/StorageBufferAttribute.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { BufferAttribute, TypedArray } from "../../core/BufferAttribute.js"; - -export default class StorageBufferAttribute extends BufferAttribute { - readonly isStorageBufferAttribute: true; - - constructor(array: TypedArray, itemSize: number); -} diff --git a/src-testing/src/renderers/common/StorageInstancedBufferAttribute.d.ts b/src-testing/src/renderers/common/StorageInstancedBufferAttribute.d.ts deleted file mode 100644 index 3f01891e8..000000000 --- a/src-testing/src/renderers/common/StorageInstancedBufferAttribute.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { TypedArray } from "../../core/BufferAttribute.js"; -import { InstancedBufferAttribute } from "../../core/InstancedBufferAttribute.js"; - -export default class StorageInstancedBufferAttribute extends InstancedBufferAttribute { - readonly isStorageInstancedBufferAttribute: true; - - constructor(array: TypedArray | number, itemSize: number); -} diff --git a/src-testing/src/renderers/common/StorageTexture.d.ts b/src-testing/src/renderers/common/StorageTexture.d.ts deleted file mode 100644 index 435ef9ba3..000000000 --- a/src-testing/src/renderers/common/StorageTexture.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { Texture } from "../../textures/Texture.js"; - -export default class StorageTexture extends Texture { - constructor(width?: number, height?: number); -} diff --git a/src-testing/src/renderers/common/Textures.ts b/src-testing/src/renderers/common/Textures.ts deleted file mode 100644 index 57507087a..000000000 --- a/src-testing/src/renderers/common/Textures.ts +++ /dev/null @@ -1,290 +0,0 @@ -import DataMap from './DataMap.js'; - -import { Vector3 } from '../../math/Vector3.js'; -import { DepthTexture } from '../../textures/DepthTexture.js'; -import { - DepthStencilFormat, - DepthFormat, - UnsignedIntType, - UnsignedInt248Type, - LinearFilter, - NearestFilter, - EquirectangularReflectionMapping, - EquirectangularRefractionMapping, - CubeReflectionMapping, - CubeRefractionMapping, - UnsignedByteType, -} from '../../constants.js'; - -const _size = /*@__PURE__*/ new Vector3(); - -class Textures extends DataMap { - constructor(renderer, backend, info) { - super(); - - this.renderer = renderer; - this.backend = backend; - this.info = info; - } - - updateRenderTarget(renderTarget, activeMipmapLevel = 0) { - const renderTargetData = this.get(renderTarget); - - const sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples; - const depthTextureMips = renderTargetData.depthTextureMips || (renderTargetData.depthTextureMips = {}); - - const texture = renderTarget.texture; - const textures = renderTarget.textures; - - const size = this.getSize(texture); - - const mipWidth = size.width >> activeMipmapLevel; - const mipHeight = size.height >> activeMipmapLevel; - - let depthTexture = renderTarget.depthTexture || depthTextureMips[activeMipmapLevel]; - let textureNeedsUpdate = false; - - if (depthTexture === undefined) { - depthTexture = new DepthTexture(); - depthTexture.format = renderTarget.stencilBuffer ? DepthStencilFormat : DepthFormat; - depthTexture.type = renderTarget.stencilBuffer ? UnsignedInt248Type : UnsignedIntType; // FloatType - depthTexture.image.width = mipWidth; - depthTexture.image.height = mipHeight; - - depthTextureMips[activeMipmapLevel] = depthTexture; - } - - if (renderTargetData.width !== size.width || size.height !== renderTargetData.height) { - textureNeedsUpdate = true; - depthTexture.needsUpdate = true; - - depthTexture.image.width = mipWidth; - depthTexture.image.height = mipHeight; - } - - renderTargetData.width = size.width; - renderTargetData.height = size.height; - renderTargetData.textures = textures; - renderTargetData.depthTexture = depthTexture; - renderTargetData.depth = renderTarget.depthBuffer; - renderTargetData.stencil = renderTarget.stencilBuffer; - renderTargetData.renderTarget = renderTarget; - - if (renderTargetData.sampleCount !== sampleCount) { - textureNeedsUpdate = true; - depthTexture.needsUpdate = true; - - renderTargetData.sampleCount = sampleCount; - } - - // - - const options = { sampleCount }; - - for (let i = 0; i < textures.length; i++) { - const texture = textures[i]; - - if (textureNeedsUpdate) texture.needsUpdate = true; - - this.updateTexture(texture, options); - } - - this.updateTexture(depthTexture, options); - - // dispose handler - - if (renderTargetData.initialized !== true) { - renderTargetData.initialized = true; - - // dispose - - const onDispose = () => { - renderTarget.removeEventListener('dispose', onDispose); - - if (textures !== undefined) { - for (let i = 0; i < textures.length; i++) { - this._destroyTexture(textures[i]); - } - } else { - this._destroyTexture(texture); - } - - this._destroyTexture(depthTexture); - - this.delete(renderTarget); - }; - - renderTarget.addEventListener('dispose', onDispose); - } - } - - updateTexture(texture, options = {}) { - const textureData = this.get(texture); - if (textureData.initialized === true && textureData.version === texture.version) return; - - const isRenderTarget = texture.isRenderTargetTexture || texture.isDepthTexture || texture.isFramebufferTexture; - const backend = this.backend; - - if (isRenderTarget && textureData.initialized === true) { - // it's an update - - backend.destroySampler(texture); - backend.destroyTexture(texture); - } - - // - - if (texture.isFramebufferTexture) { - const renderer = this.renderer; - const renderTarget = renderer.getRenderTarget(); - - if (renderTarget) { - texture.type = renderTarget.texture.type; - } else { - texture.type = UnsignedByteType; - } - } - - // - - const { width, height, depth } = this.getSize(texture); - - options.width = width; - options.height = height; - options.depth = depth; - options.needsMipmaps = this.needsMipmaps(texture); - options.levels = options.needsMipmaps ? this.getMipLevels(texture, width, height) : 1; - - // - - if (isRenderTarget || texture.isStorageTexture === true) { - backend.createSampler(texture); - backend.createTexture(texture, options); - } else { - const needsCreate = textureData.initialized !== true; - - if (needsCreate) backend.createSampler(texture); - - if (texture.version > 0) { - const image = texture.image; - - if (image === undefined) { - console.warn('THREE.Renderer: Texture marked for update but image is undefined.'); - } else if (image.complete === false) { - console.warn('THREE.Renderer: Texture marked for update but image is incomplete.'); - } else { - if (texture.images) { - const images = []; - - for (const image of texture.images) { - images.push(image); - } - - options.images = images; - } else { - options.image = image; - } - - if (textureData.isDefaultTexture === undefined || textureData.isDefaultTexture === true) { - backend.createTexture(texture, options); - - textureData.isDefaultTexture = false; - } - - if (texture.source.dataReady === true) backend.updateTexture(texture, options); - - if (options.needsMipmaps && texture.mipmaps.length === 0) backend.generateMipmaps(texture); - } - } else { - // async update - - backend.createDefaultTexture(texture); - - textureData.isDefaultTexture = true; - } - } - - // dispose handler - - if (textureData.initialized !== true) { - textureData.initialized = true; - - // - - this.info.memory.textures++; - - // dispose - - const onDispose = () => { - texture.removeEventListener('dispose', onDispose); - - this._destroyTexture(texture); - - this.info.memory.textures--; - }; - - texture.addEventListener('dispose', onDispose); - } - - // - - textureData.version = texture.version; - } - - getSize(texture, target = _size) { - let image = texture.images ? texture.images[0] : texture.image; - - if (image) { - if (image.image !== undefined) image = image.image; - - target.width = image.width; - target.height = image.height; - target.depth = texture.isCubeTexture ? 6 : image.depth || 1; - } else { - target.width = target.height = target.depth = 1; - } - - return target; - } - - getMipLevels(texture, width, height) { - let mipLevelCount; - - if (texture.isCompressedTexture) { - mipLevelCount = texture.mipmaps.length; - } else { - mipLevelCount = Math.floor(Math.log2(Math.max(width, height))) + 1; - } - - return mipLevelCount; - } - - needsMipmaps(texture) { - if (this.isEnvironmentTexture(texture)) return true; - - return ( - texture.isCompressedTexture === true || - (texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter) - ); - } - - isEnvironmentTexture(texture) { - const mapping = texture.mapping; - - return ( - mapping === EquirectangularReflectionMapping || - mapping === EquirectangularRefractionMapping || - mapping === CubeReflectionMapping || - mapping === CubeRefractionMapping - ); - } - - _destroyTexture(texture) { - this.backend.destroySampler(texture); - this.backend.destroyTexture(texture); - - this.delete(texture); - } -} - -export default Textures; diff --git a/src-testing/src/renderers/common/Uniform.ts b/src-testing/src/renderers/common/Uniform.ts deleted file mode 100644 index 80c131494..000000000 --- a/src-testing/src/renderers/common/Uniform.ts +++ /dev/null @@ -1,105 +0,0 @@ -import { Color } from '../../math/Color.js'; -import { Matrix3 } from '../../math/Matrix3.js'; -import { Matrix4 } from '../../math/Matrix4.js'; -import { Vector2 } from '../../math/Vector2.js'; -import { Vector3 } from '../../math/Vector3.js'; -import { Vector4 } from '../../math/Vector4.js'; - -class Uniform { - constructor(name, value) { - this.name = name; - this.value = value; - - this.boundary = 0; // used to build the uniform buffer according to the STD140 layout - this.itemSize = 0; - - this.offset = 0; // this property is set by WebGPUUniformsGroup and marks the start position in the uniform buffer - } - - setValue(value) { - this.value = value; - } - - getValue() { - return this.value; - } -} - -class NumberUniform extends Uniform { - constructor(name, value = 0) { - super(name, value); - - this.isNumberUniform = true; - - this.boundary = 4; - this.itemSize = 1; - } -} - -class Vector2Uniform extends Uniform { - constructor(name, value = new Vector2()) { - super(name, value); - - this.isVector2Uniform = true; - - this.boundary = 8; - this.itemSize = 2; - } -} - -class Vector3Uniform extends Uniform { - constructor(name, value = new Vector3()) { - super(name, value); - - this.isVector3Uniform = true; - - this.boundary = 16; - this.itemSize = 3; - } -} - -class Vector4Uniform extends Uniform { - constructor(name, value = new Vector4()) { - super(name, value); - - this.isVector4Uniform = true; - - this.boundary = 16; - this.itemSize = 4; - } -} - -class ColorUniform extends Uniform { - constructor(name, value = new Color()) { - super(name, value); - - this.isColorUniform = true; - - this.boundary = 16; - this.itemSize = 3; - } -} - -class Matrix3Uniform extends Uniform { - constructor(name, value = new Matrix3()) { - super(name, value); - - this.isMatrix3Uniform = true; - - this.boundary = 48; - this.itemSize = 12; - } -} - -class Matrix4Uniform extends Uniform { - constructor(name, value = new Matrix4()) { - super(name, value); - - this.isMatrix4Uniform = true; - - this.boundary = 64; - this.itemSize = 16; - } -} - -export { NumberUniform, Vector2Uniform, Vector3Uniform, Vector4Uniform, ColorUniform, Matrix3Uniform, Matrix4Uniform }; diff --git a/src-testing/src/renderers/common/UniformBuffer.ts b/src-testing/src/renderers/common/UniformBuffer.ts deleted file mode 100644 index 28aac0d7e..000000000 --- a/src-testing/src/renderers/common/UniformBuffer.ts +++ /dev/null @@ -1,11 +0,0 @@ -import Buffer from './Buffer.js'; - -class UniformBuffer extends Buffer { - constructor(name, buffer = null) { - super(name, buffer); - - this.isUniformBuffer = true; - } -} - -export default UniformBuffer; diff --git a/src-testing/src/renderers/common/UniformsGroup.ts b/src-testing/src/renderers/common/UniformsGroup.ts deleted file mode 100644 index e2b62671a..000000000 --- a/src-testing/src/renderers/common/UniformsGroup.ts +++ /dev/null @@ -1,277 +0,0 @@ -import UniformBuffer from './UniformBuffer.js'; -import { GPU_CHUNK_BYTES } from './Constants.js'; - -class UniformsGroup extends UniformBuffer { - constructor(name) { - super(name); - - this.isUniformsGroup = true; - - this._values = null; - - // the order of uniforms in this array must match the order of uniforms in the shader - - this.uniforms = []; - } - - addUniform(uniform) { - this.uniforms.push(uniform); - - return this; - } - - removeUniform(uniform) { - const index = this.uniforms.indexOf(uniform); - - if (index !== -1) { - this.uniforms.splice(index, 1); - } - - return this; - } - - get values() { - if (this._values === null) { - this._values = Array.from(this.buffer); - } - - return this._values; - } - - get buffer() { - let buffer = this._buffer; - - if (buffer === null) { - const byteLength = this.byteLength; - - buffer = new Float32Array(new ArrayBuffer(byteLength)); - - this._buffer = buffer; - } - - return buffer; - } - - get byteLength() { - let offset = 0; // global buffer offset in bytes - - for (let i = 0, l = this.uniforms.length; i < l; i++) { - const uniform = this.uniforms[i]; - - const { boundary, itemSize } = uniform; - - // offset within a single chunk in bytes - - const chunkOffset = offset % GPU_CHUNK_BYTES; - const remainingSizeInChunk = GPU_CHUNK_BYTES - chunkOffset; - - // conformance tests - - if (chunkOffset !== 0 && remainingSizeInChunk - boundary < 0) { - // check for chunk overflow - - offset += GPU_CHUNK_BYTES - chunkOffset; - } else if (chunkOffset % boundary !== 0) { - // check for correct alignment - - offset += chunkOffset % boundary; - } - - uniform.offset = offset / this.bytesPerElement; - - offset += itemSize * this.bytesPerElement; - } - - return Math.ceil(offset / GPU_CHUNK_BYTES) * GPU_CHUNK_BYTES; - } - - update() { - let updated = false; - - for (const uniform of this.uniforms) { - if (this.updateByType(uniform) === true) { - updated = true; - } - } - - return updated; - } - - updateByType(uniform) { - if (uniform.isNumberUniform) return this.updateNumber(uniform); - if (uniform.isVector2Uniform) return this.updateVector2(uniform); - if (uniform.isVector3Uniform) return this.updateVector3(uniform); - if (uniform.isVector4Uniform) return this.updateVector4(uniform); - if (uniform.isColorUniform) return this.updateColor(uniform); - if (uniform.isMatrix3Uniform) return this.updateMatrix3(uniform); - if (uniform.isMatrix4Uniform) return this.updateMatrix4(uniform); - - console.error('THREE.WebGPUUniformsGroup: Unsupported uniform type.', uniform); - } - - updateNumber(uniform) { - let updated = false; - - const a = this.values; - const v = uniform.getValue(); - const offset = uniform.offset; - - if (a[offset] !== v) { - const b = this.buffer; - - b[offset] = a[offset] = v; - updated = true; - } - - return updated; - } - - updateVector2(uniform) { - let updated = false; - - const a = this.values; - const v = uniform.getValue(); - const offset = uniform.offset; - - if (a[offset + 0] !== v.x || a[offset + 1] !== v.y) { - const b = this.buffer; - - b[offset + 0] = a[offset + 0] = v.x; - b[offset + 1] = a[offset + 1] = v.y; - - updated = true; - } - - return updated; - } - - updateVector3(uniform) { - let updated = false; - - const a = this.values; - const v = uniform.getValue(); - const offset = uniform.offset; - - if (a[offset + 0] !== v.x || a[offset + 1] !== v.y || a[offset + 2] !== v.z) { - const b = this.buffer; - - b[offset + 0] = a[offset + 0] = v.x; - b[offset + 1] = a[offset + 1] = v.y; - b[offset + 2] = a[offset + 2] = v.z; - - updated = true; - } - - return updated; - } - - updateVector4(uniform) { - let updated = false; - - const a = this.values; - const v = uniform.getValue(); - const offset = uniform.offset; - - if (a[offset + 0] !== v.x || a[offset + 1] !== v.y || a[offset + 2] !== v.z || a[offset + 4] !== v.w) { - const b = this.buffer; - - b[offset + 0] = a[offset + 0] = v.x; - b[offset + 1] = a[offset + 1] = v.y; - b[offset + 2] = a[offset + 2] = v.z; - b[offset + 3] = a[offset + 3] = v.w; - - updated = true; - } - - return updated; - } - - updateColor(uniform) { - let updated = false; - - const a = this.values; - const c = uniform.getValue(); - const offset = uniform.offset; - - if (a[offset + 0] !== c.r || a[offset + 1] !== c.g || a[offset + 2] !== c.b) { - const b = this.buffer; - - b[offset + 0] = a[offset + 0] = c.r; - b[offset + 1] = a[offset + 1] = c.g; - b[offset + 2] = a[offset + 2] = c.b; - - updated = true; - } - - return updated; - } - - updateMatrix3(uniform) { - let updated = false; - - const a = this.values; - const e = uniform.getValue().elements; - const offset = uniform.offset; - - if ( - a[offset + 0] !== e[0] || - a[offset + 1] !== e[1] || - a[offset + 2] !== e[2] || - a[offset + 4] !== e[3] || - a[offset + 5] !== e[4] || - a[offset + 6] !== e[5] || - a[offset + 8] !== e[6] || - a[offset + 9] !== e[7] || - a[offset + 10] !== e[8] - ) { - const b = this.buffer; - - b[offset + 0] = a[offset + 0] = e[0]; - b[offset + 1] = a[offset + 1] = e[1]; - b[offset + 2] = a[offset + 2] = e[2]; - b[offset + 4] = a[offset + 4] = e[3]; - b[offset + 5] = a[offset + 5] = e[4]; - b[offset + 6] = a[offset + 6] = e[5]; - b[offset + 8] = a[offset + 8] = e[6]; - b[offset + 9] = a[offset + 9] = e[7]; - b[offset + 10] = a[offset + 10] = e[8]; - - updated = true; - } - - return updated; - } - - updateMatrix4(uniform) { - let updated = false; - - const a = this.values; - const e = uniform.getValue().elements; - const offset = uniform.offset; - - if (arraysEqual(a, e, offset) === false) { - const b = this.buffer; - b.set(e, offset); - setArray(a, e, offset); - updated = true; - } - - return updated; - } -} - -function setArray(a, b, offset) { - for (let i = 0, l = b.length; i < l; i++) { - a[offset + i] = b[i]; - } -} - -function arraysEqual(a, b, offset) { - for (let i = 0, l = b.length; i < l; i++) { - if (a[offset + i] !== b[i]) return false; - } - - return true; -} - -export default UniformsGroup; diff --git a/src-testing/src/renderers/common/extras/PMREMGenerator.ts b/src-testing/src/renderers/common/extras/PMREMGenerator.ts deleted file mode 100644 index e45588271..000000000 --- a/src-testing/src/renderers/common/extras/PMREMGenerator.ts +++ /dev/null @@ -1,660 +0,0 @@ -import NodeMaterial from '../../../nodes/materials/NodeMaterial.js'; -import { getDirection, blur } from '../../../nodes/pmrem/PMREMUtils.js'; -import { equirectUV } from '../../../nodes/utils/EquirectUVNode.js'; -import { uniform } from '../../../nodes/core/UniformNode.js'; -import { uniformArray } from '../../../nodes/accessors/UniformArrayNode.js'; -import { texture } from '../../../nodes/accessors/TextureNode.js'; -import { cubeTexture } from '../../../nodes/accessors/CubeTextureNode.js'; -import { float, vec3 } from '../../../nodes/shadernode/ShaderNode.js'; -import { uv } from '../../../nodes/accessors/UVNode.js'; -import { attribute } from '../../../nodes/core/AttributeNode.js'; - -import { OrthographicCamera } from '../../../cameras/OrthographicCamera.js'; -import { Color } from '../../../math/Color.js'; -import { Vector3 } from '../../../math/Vector3.js'; -import { BufferGeometry } from '../../../core/BufferGeometry.js'; -import { BufferAttribute } from '../../../core/BufferAttribute.js'; -import { RenderTarget } from '../../../core/RenderTarget.js'; -import { Mesh } from '../../../objects/Mesh.js'; -import { PerspectiveCamera } from '../../../cameras/PerspectiveCamera.js'; -import { MeshBasicMaterial } from '../../../materials/MeshBasicMaterial.js'; -import { BoxGeometry } from '../../../geometries/BoxGeometry.js'; -import { - CubeReflectionMapping, - CubeRefractionMapping, - CubeUVReflectionMapping, - LinearFilter, - NoBlending, - RGBAFormat, - HalfFloatType, - BackSide, - LinearSRGBColorSpace, -} from '../../../constants.js'; - -const LOD_MIN = 4; - -// The standard deviations (radians) associated with the extra mips. These are -// chosen to approximate a Trowbridge-Reitz distribution function times the -// geometric shadowing function. These sigma values squared must match the -// variance #defines in cube_uv_reflection_fragment.glsl.js. -const EXTRA_LOD_SIGMA = [0.125, 0.215, 0.35, 0.446, 0.526, 0.582]; - -// The maximum length of the blur for loop. Smaller sigmas will use fewer -// samples and exit early, but not recompile the shader. -const MAX_SAMPLES = 20; - -const _flatCamera = /*@__PURE__*/ new OrthographicCamera(-1, 1, 1, -1, 0, 1); -const _cubeCamera = /*@__PURE__*/ new PerspectiveCamera(90, 1); -const _clearColor = /*@__PURE__*/ new Color(); -let _oldTarget = null; -let _oldActiveCubeFace = 0; -let _oldActiveMipmapLevel = 0; - -// Golden Ratio -const PHI = (1 + Math.sqrt(5)) / 2; -const INV_PHI = 1 / PHI; - -// Vertices of a dodecahedron (except the opposites, which represent the -// same axis), used as axis directions evenly spread on a sphere. -const _axisDirections = [ - /*@__PURE__*/ new Vector3(-PHI, INV_PHI, 0), - /*@__PURE__*/ new Vector3(PHI, INV_PHI, 0), - /*@__PURE__*/ new Vector3(-INV_PHI, 0, PHI), - /*@__PURE__*/ new Vector3(INV_PHI, 0, PHI), - /*@__PURE__*/ new Vector3(0, PHI, -INV_PHI), - /*@__PURE__*/ new Vector3(0, PHI, INV_PHI), - /*@__PURE__*/ new Vector3(-1, 1, -1), - /*@__PURE__*/ new Vector3(1, 1, -1), - /*@__PURE__*/ new Vector3(-1, 1, 1), - /*@__PURE__*/ new Vector3(1, 1, 1), -]; - -// - -// WebGPU Face indices -const _faceLib = [3, 1, 5, 0, 4, 2]; - -const direction = getDirection(uv(), attribute('faceIndex')).normalize(); -const outputDirection = vec3(direction.x, direction.y.negate(), direction.z); - -/** - * This class generates a Prefiltered, Mipmapped Radiance Environment Map - * (PMREM) from a cubeMap environment texture. This allows different levels of - * blur to be quickly accessed based on material roughness. It is packed into a - * special CubeUV format that allows us to perform custom interpolation so that - * we can support nonlinear formats such as RGBE. Unlike a traditional mipmap - * chain, it only goes down to the LOD_MIN level (above), and then creates extra - * even more filtered 'mips' at the same LOD_MIN resolution, associated with - * higher roughness levels. In this way we maintain resolution to smoothly - * interpolate diffuse lighting while limiting sampling computation. - * - * Paper: Fast, Accurate Image-Based Lighting - * https://drive.google.com/file/d/15y8r_UpKlU9SvV4ILb0C3qCPecS8pvLz/view - */ - -class PMREMGenerator { - constructor(renderer) { - this._renderer = renderer; - this._pingPongRenderTarget = null; - - this._lodMax = 0; - this._cubeSize = 0; - this._lodPlanes = []; - this._sizeLods = []; - this._sigmas = []; - this._lodMeshes = []; - - this._blurMaterial = null; - this._cubemapMaterial = null; - this._equirectMaterial = null; - this._backgroundBox = null; - } - - /** - * Generates a PMREM from a supplied Scene, which can be faster than using an - * image if networking bandwidth is low. Optional sigma specifies a blur radius - * in radians to be applied to the scene before PMREM generation. Optional near - * and far planes ensure the scene is rendered in its entirety (the cubeCamera - * is placed at the origin). - */ - fromScene(scene, sigma = 0, near = 0.1, far = 100) { - _oldTarget = this._renderer.getRenderTarget(); - _oldActiveCubeFace = this._renderer.getActiveCubeFace(); - _oldActiveMipmapLevel = this._renderer.getActiveMipmapLevel(); - - this._setSize(256); - - const cubeUVRenderTarget = this._allocateTargets(); - cubeUVRenderTarget.depthBuffer = true; - - this._sceneToCubeUV(scene, near, far, cubeUVRenderTarget); - - if (sigma > 0) { - this._blur(cubeUVRenderTarget, 0, 0, sigma); - } - - this._applyPMREM(cubeUVRenderTarget); - - this._cleanup(cubeUVRenderTarget); - - return cubeUVRenderTarget; - } - - /** - * Generates a PMREM from an equirectangular texture, which can be either LDR - * or HDR. The ideal input image size is 1k (1024 x 512), - * as this matches best with the 256 x 256 cubemap output. - */ - fromEquirectangular(equirectangular, renderTarget = null) { - return this._fromTexture(equirectangular, renderTarget); - } - - /** - * Generates a PMREM from an cubemap texture, which can be either LDR - * or HDR. The ideal input cube size is 256 x 256, - * as this matches best with the 256 x 256 cubemap output. - */ - fromCubemap(cubemap, renderTarget = null) { - return this._fromTexture(cubemap, renderTarget); - } - - /** - * Pre-compiles the cubemap shader. You can get faster start-up by invoking this method during - * your texture's network fetch for increased concurrency. - */ - compileCubemapShader() { - if (this._cubemapMaterial === null) { - this._cubemapMaterial = _getCubemapMaterial(); - this._compileMaterial(this._cubemapMaterial); - } - } - - /** - * Pre-compiles the equirectangular shader. You can get faster start-up by invoking this method during - * your texture's network fetch for increased concurrency. - */ - compileEquirectangularShader() { - if (this._equirectMaterial === null) { - this._equirectMaterial = _getEquirectMaterial(); - this._compileMaterial(this._equirectMaterial); - } - } - - /** - * Disposes of the PMREMGenerator's internal memory. Note that PMREMGenerator is a static class, - * so you should not need more than one PMREMGenerator object. If you do, calling dispose() on - * one of them will cause any others to also become unusable. - */ - dispose() { - this._dispose(); - - if (this._cubemapMaterial !== null) this._cubemapMaterial.dispose(); - if (this._equirectMaterial !== null) this._equirectMaterial.dispose(); - if (this._backgroundBox !== null) { - this._backgroundBox.geometry.dispose(); - this._backgroundBox.material.dispose(); - } - } - - // private interface - - _setSize(cubeSize) { - this._lodMax = Math.floor(Math.log2(cubeSize)); - this._cubeSize = Math.pow(2, this._lodMax); - } - - _dispose() { - if (this._blurMaterial !== null) this._blurMaterial.dispose(); - - if (this._pingPongRenderTarget !== null) this._pingPongRenderTarget.dispose(); - - for (let i = 0; i < this._lodPlanes.length; i++) { - this._lodPlanes[i].dispose(); - } - } - - _cleanup(outputTarget) { - this._renderer.setRenderTarget(_oldTarget, _oldActiveCubeFace, _oldActiveMipmapLevel); - outputTarget.scissorTest = false; - _setViewport(outputTarget, 0, 0, outputTarget.width, outputTarget.height); - } - - _fromTexture(texture, renderTarget) { - if (texture.mapping === CubeReflectionMapping || texture.mapping === CubeRefractionMapping) { - this._setSize(texture.image.length === 0 ? 16 : texture.image[0].width || texture.image[0].image.width); - } else { - // Equirectangular - - this._setSize(texture.image.width / 4); - } - - _oldTarget = this._renderer.getRenderTarget(); - _oldActiveCubeFace = this._renderer.getActiveCubeFace(); - _oldActiveMipmapLevel = this._renderer.getActiveMipmapLevel(); - - const cubeUVRenderTarget = renderTarget || this._allocateTargets(); - this._textureToCubeUV(texture, cubeUVRenderTarget); - this._applyPMREM(cubeUVRenderTarget); - this._cleanup(cubeUVRenderTarget); - - return cubeUVRenderTarget; - } - - _allocateTargets() { - const width = 3 * Math.max(this._cubeSize, 16 * 7); - const height = 4 * this._cubeSize; - - const params = { - magFilter: LinearFilter, - minFilter: LinearFilter, - generateMipmaps: false, - type: HalfFloatType, - format: RGBAFormat, - colorSpace: LinearSRGBColorSpace, - //depthBuffer: false - }; - - const cubeUVRenderTarget = _createRenderTarget(width, height, params); - - if ( - this._pingPongRenderTarget === null || - this._pingPongRenderTarget.width !== width || - this._pingPongRenderTarget.height !== height - ) { - if (this._pingPongRenderTarget !== null) { - this._dispose(); - } - - this._pingPongRenderTarget = _createRenderTarget(width, height, params); - - const { _lodMax } = this; - ({ - sizeLods: this._sizeLods, - lodPlanes: this._lodPlanes, - sigmas: this._sigmas, - lodMeshes: this._lodMeshes, - } = _createPlanes(_lodMax)); - - this._blurMaterial = _getBlurShader(_lodMax, width, height); - } - - return cubeUVRenderTarget; - } - - _compileMaterial(material) { - const tmpMesh = this._lodMeshes[0]; - tmpMesh.material = material; - - this._renderer.compile(tmpMesh, _flatCamera); - } - - _sceneToCubeUV(scene, near, far, cubeUVRenderTarget) { - const cubeCamera = _cubeCamera; - cubeCamera.near = near; - cubeCamera.far = far; - - // px, py, pz, nx, ny, nz - const upSign = [-1, 1, -1, -1, -1, -1]; - const forwardSign = [1, 1, 1, -1, -1, -1]; - - const renderer = this._renderer; - - const originalAutoClear = renderer.autoClear; - - renderer.getClearColor(_clearColor); - - renderer.autoClear = false; - - let backgroundBox = this._backgroundBox; - - if (backgroundBox === null) { - const backgroundMaterial = new MeshBasicMaterial({ - name: 'PMREM.Background', - side: BackSide, - depthWrite: false, - depthTest: false, - }); - - backgroundBox = new Mesh(new BoxGeometry(), backgroundMaterial); - } - - let useSolidColor = false; - const background = scene.background; - - if (background) { - if (background.isColor) { - backgroundBox.material.color.copy(background); - scene.background = null; - useSolidColor = true; - } - } else { - backgroundBox.material.color.copy(_clearColor); - useSolidColor = true; - } - - renderer.setRenderTarget(cubeUVRenderTarget); - - renderer.clear(); - - if (useSolidColor) { - renderer.render(backgroundBox, cubeCamera); - } - - for (let i = 0; i < 6; i++) { - const col = i % 3; - - if (col === 0) { - cubeCamera.up.set(0, upSign[i], 0); - cubeCamera.lookAt(forwardSign[i], 0, 0); - } else if (col === 1) { - cubeCamera.up.set(0, 0, upSign[i]); - cubeCamera.lookAt(0, forwardSign[i], 0); - } else { - cubeCamera.up.set(0, upSign[i], 0); - cubeCamera.lookAt(0, 0, forwardSign[i]); - } - - const size = this._cubeSize; - - _setViewport(cubeUVRenderTarget, col * size, i > 2 ? size : 0, size, size); - - renderer.render(scene, cubeCamera); - } - - renderer.autoClear = originalAutoClear; - scene.background = background; - } - - _textureToCubeUV(texture, cubeUVRenderTarget) { - const renderer = this._renderer; - - const isCubeTexture = texture.mapping === CubeReflectionMapping || texture.mapping === CubeRefractionMapping; - - if (isCubeTexture) { - if (this._cubemapMaterial === null) { - this._cubemapMaterial = _getCubemapMaterial(texture); - } - } else { - if (this._equirectMaterial === null) { - this._equirectMaterial = _getEquirectMaterial(texture); - } - } - - const material = isCubeTexture ? this._cubemapMaterial : this._equirectMaterial; - material.fragmentNode.value = texture; - - const mesh = this._lodMeshes[0]; - mesh.material = material; - - const size = this._cubeSize; - - _setViewport(cubeUVRenderTarget, 0, 0, 3 * size, 2 * size); - - renderer.setRenderTarget(cubeUVRenderTarget); - renderer.render(mesh, _flatCamera); - } - - _applyPMREM(cubeUVRenderTarget) { - const renderer = this._renderer; - const autoClear = renderer.autoClear; - renderer.autoClear = false; - const n = this._lodPlanes.length; - - for (let i = 1; i < n; i++) { - const sigma = Math.sqrt(this._sigmas[i] * this._sigmas[i] - this._sigmas[i - 1] * this._sigmas[i - 1]); - - const poleAxis = _axisDirections[(n - i - 1) % _axisDirections.length]; - - this._blur(cubeUVRenderTarget, i - 1, i, sigma, poleAxis); - } - - renderer.autoClear = autoClear; - } - - /** - * This is a two-pass Gaussian blur for a cubemap. Normally this is done - * vertically and horizontally, but this breaks down on a cube. Here we apply - * the blur latitudinally (around the poles), and then longitudinally (towards - * the poles) to approximate the orthogonally-separable blur. It is least - * accurate at the poles, but still does a decent job. - */ - _blur(cubeUVRenderTarget, lodIn, lodOut, sigma, poleAxis) { - const pingPongRenderTarget = this._pingPongRenderTarget; - - this._halfBlur(cubeUVRenderTarget, pingPongRenderTarget, lodIn, lodOut, sigma, 'latitudinal', poleAxis); - - this._halfBlur(pingPongRenderTarget, cubeUVRenderTarget, lodOut, lodOut, sigma, 'longitudinal', poleAxis); - } - - _halfBlur(targetIn, targetOut, lodIn, lodOut, sigmaRadians, direction, poleAxis) { - const renderer = this._renderer; - const blurMaterial = this._blurMaterial; - - if (direction !== 'latitudinal' && direction !== 'longitudinal') { - console.error('blur direction must be either latitudinal or longitudinal!'); - } - - // Number of standard deviations at which to cut off the discrete approximation. - const STANDARD_DEVIATIONS = 3; - - const blurMesh = this._lodMeshes[lodOut]; - blurMesh.material = blurMaterial; - - const blurUniforms = blurMaterial.uniforms; - - const pixels = this._sizeLods[lodIn] - 1; - const radiansPerPixel = isFinite(sigmaRadians) ? Math.PI / (2 * pixels) : (2 * Math.PI) / (2 * MAX_SAMPLES - 1); - const sigmaPixels = sigmaRadians / radiansPerPixel; - const samples = isFinite(sigmaRadians) ? 1 + Math.floor(STANDARD_DEVIATIONS * sigmaPixels) : MAX_SAMPLES; - - if (samples > MAX_SAMPLES) { - console.warn( - `sigmaRadians, ${sigmaRadians}, is too large and will clip, as it requested ${ - samples - } samples when the maximum is set to ${MAX_SAMPLES}`, - ); - } - - const weights = []; - let sum = 0; - - for (let i = 0; i < MAX_SAMPLES; ++i) { - const x = i / sigmaPixels; - const weight = Math.exp((-x * x) / 2); - weights.push(weight); - - if (i === 0) { - sum += weight; - } else if (i < samples) { - sum += 2 * weight; - } - } - - for (let i = 0; i < weights.length; i++) { - weights[i] = weights[i] / sum; - } - - targetIn.texture.frame = (targetIn.texture.frame || 0) + 1; - - blurUniforms.envMap.value = targetIn.texture; - blurUniforms.samples.value = samples; - blurUniforms.weights.array = weights; - blurUniforms.latitudinal.value = direction === 'latitudinal' ? 1 : 0; - - if (poleAxis) { - blurUniforms.poleAxis.value = poleAxis; - } - - const { _lodMax } = this; - blurUniforms.dTheta.value = radiansPerPixel; - blurUniforms.mipInt.value = _lodMax - lodIn; - - const outputSize = this._sizeLods[lodOut]; - const x = 3 * outputSize * (lodOut > _lodMax - LOD_MIN ? lodOut - _lodMax + LOD_MIN : 0); - const y = 4 * (this._cubeSize - outputSize); - - _setViewport(targetOut, x, y, 3 * outputSize, 2 * outputSize); - renderer.setRenderTarget(targetOut); - renderer.render(blurMesh, _flatCamera); - } -} - -function _createPlanes(lodMax) { - const lodPlanes = []; - const sizeLods = []; - const sigmas = []; - const lodMeshes = []; - - let lod = lodMax; - - const totalLods = lodMax - LOD_MIN + 1 + EXTRA_LOD_SIGMA.length; - - for (let i = 0; i < totalLods; i++) { - const sizeLod = Math.pow(2, lod); - sizeLods.push(sizeLod); - let sigma = 1.0 / sizeLod; - - if (i > lodMax - LOD_MIN) { - sigma = EXTRA_LOD_SIGMA[i - lodMax + LOD_MIN - 1]; - } else if (i === 0) { - sigma = 0; - } - - sigmas.push(sigma); - - const texelSize = 1.0 / (sizeLod - 2); - const min = -texelSize; - const max = 1 + texelSize; - const uv1 = [min, min, max, min, max, max, min, min, max, max, min, max]; - - const cubeFaces = 6; - const vertices = 6; - const positionSize = 3; - const uvSize = 2; - const faceIndexSize = 1; - - const position = new Float32Array(positionSize * vertices * cubeFaces); - const uv = new Float32Array(uvSize * vertices * cubeFaces); - const faceIndex = new Float32Array(faceIndexSize * vertices * cubeFaces); - - for (let face = 0; face < cubeFaces; face++) { - const x = ((face % 3) * 2) / 3 - 1; - const y = face > 2 ? 0 : -1; - const coordinates = [ - x, - y, - 0, - x + 2 / 3, - y, - 0, - x + 2 / 3, - y + 1, - 0, - x, - y, - 0, - x + 2 / 3, - y + 1, - 0, - x, - y + 1, - 0, - ]; - - const faceIdx = _faceLib[face]; - position.set(coordinates, positionSize * vertices * faceIdx); - uv.set(uv1, uvSize * vertices * faceIdx); - const fill = [faceIdx, faceIdx, faceIdx, faceIdx, faceIdx, faceIdx]; - faceIndex.set(fill, faceIndexSize * vertices * faceIdx); - } - - const planes = new BufferGeometry(); - planes.setAttribute('position', new BufferAttribute(position, positionSize)); - planes.setAttribute('uv', new BufferAttribute(uv, uvSize)); - planes.setAttribute('faceIndex', new BufferAttribute(faceIndex, faceIndexSize)); - lodPlanes.push(planes); - lodMeshes.push(new Mesh(planes, null)); - - if (lod > LOD_MIN) { - lod--; - } - } - - return { lodPlanes, sizeLods, sigmas, lodMeshes }; -} - -function _createRenderTarget(width, height, params) { - const cubeUVRenderTarget = new RenderTarget(width, height, params); - cubeUVRenderTarget.texture.mapping = CubeUVReflectionMapping; - cubeUVRenderTarget.texture.name = 'PMREM.cubeUv'; - cubeUVRenderTarget.texture.isPMREMTexture = true; - cubeUVRenderTarget.scissorTest = true; - return cubeUVRenderTarget; -} - -function _setViewport(target, x, y, width, height) { - const viewY = target.height - height - y; - - target.viewport.set(x, viewY, width, height); - target.scissor.set(x, viewY, width, height); -} - -function _getMaterial() { - const material = new NodeMaterial(); - material.depthTest = false; - material.depthWrite = false; - material.blending = NoBlending; - - return material; -} - -function _getBlurShader(lodMax, width, height) { - const weights = uniformArray(new Array(MAX_SAMPLES).fill(0)); - const poleAxis = uniform(new Vector3(0, 1, 0)); - const dTheta = uniform(0); - const n = float(MAX_SAMPLES); - const latitudinal = uniform(0); // false, bool - const samples = uniform(1); // int - const envMap = texture(null); - const mipInt = uniform(0); // int - const CUBEUV_TEXEL_WIDTH = float(1 / width); - const CUBEUV_TEXEL_HEIGHT = float(1 / height); - const CUBEUV_MAX_MIP = float(lodMax); - - const materialUniforms = { - n, - latitudinal, - weights, - poleAxis, - outputDirection, - dTheta, - samples, - envMap, - mipInt, - CUBEUV_TEXEL_WIDTH, - CUBEUV_TEXEL_HEIGHT, - CUBEUV_MAX_MIP, - }; - - const material = _getMaterial(); - material.uniforms = materialUniforms; // TODO: Move to outside of the material - material.fragmentNode = blur({ ...materialUniforms, latitudinal: latitudinal.equal(1) }); - - return material; -} - -function _getCubemapMaterial(envTexture) { - const material = _getMaterial(); - material.fragmentNode = cubeTexture(envTexture, outputDirection); - - return material; -} - -function _getEquirectMaterial(envTexture) { - const material = _getMaterial(); - material.fragmentNode = texture(envTexture, equirectUV(outputDirection), 0); - - return material; -} - -export default PMREMGenerator; diff --git a/src-testing/src/renderers/common/nodes/NodeBuilderState.ts b/src-testing/src/renderers/common/nodes/NodeBuilderState.ts deleted file mode 100644 index 7516dbdc1..000000000 --- a/src-testing/src/renderers/common/nodes/NodeBuilderState.ts +++ /dev/null @@ -1,55 +0,0 @@ -import BindGroup from '../BindGroup.js'; - -class NodeBuilderState { - constructor( - vertexShader, - fragmentShader, - computeShader, - nodeAttributes, - bindings, - updateNodes, - updateBeforeNodes, - updateAfterNodes, - instanceBindGroups = true, - transforms = [], - ) { - this.vertexShader = vertexShader; - this.fragmentShader = fragmentShader; - this.computeShader = computeShader; - this.transforms = transforms; - - this.nodeAttributes = nodeAttributes; - this.bindings = bindings; - - this.updateNodes = updateNodes; - this.updateBeforeNodes = updateBeforeNodes; - this.updateAfterNodes = updateAfterNodes; - - this.instanceBindGroups = instanceBindGroups; - - this.usedTimes = 0; - } - - createBindings() { - const bindings = []; - - for (const instanceGroup of this.bindings) { - const shared = this.instanceBindGroups && instanceGroup.bindings[0].groupNode.shared; - - if (shared !== true) { - const bindingsGroup = new BindGroup(instanceGroup.name, [], instanceGroup.index); - bindings.push(bindingsGroup); - - for (const instanceBinding of instanceGroup.bindings) { - bindingsGroup.bindings.push(instanceBinding.clone()); - } - } else { - bindings.push(instanceGroup); - } - } - - return bindings; - } -} - -export default NodeBuilderState; diff --git a/src-testing/src/renderers/common/nodes/NodeSampledTexture.d.ts b/src-testing/src/renderers/common/nodes/NodeSampledTexture.d.ts deleted file mode 100644 index 12a2a4aa7..000000000 --- a/src-testing/src/renderers/common/nodes/NodeSampledTexture.d.ts +++ /dev/null @@ -1,29 +0,0 @@ -import TextureNode from "../../../nodes/accessors/TextureNode.js"; -import UniformGroupNode from "../../../nodes/core/UniformGroupNode.js"; -import { SampledTexture } from "../SampledTexture.js"; - -type GPUStorageTextureAccess = "read-only" | "read-write" | "write-only"; - -declare class NodeSampledTexture extends SampledTexture { - textureNode: TextureNode | undefined; - groupNode: UniformGroupNode; - access: "read-write" | "read-only" | "write-only"; - constructor( - name: string, - textureNode: TextureNode | undefined, - groupNode: UniformGroupNode, - access: GPUStorageTextureAccess | null, - ); - get needsBindingsUpdate(): boolean; - update(): boolean; -} - -declare class NodeSampledCubeTexture extends NodeSampledTexture { - readonly isSampledCubeTexture: true; -} - -declare class NodeSampledTexture3D extends NodeSampledTexture { - readonly isSampledTexture3D = true; -} - -export { NodeSampledCubeTexture, NodeSampledTexture, NodeSampledTexture3D }; diff --git a/src-testing/src/renderers/common/nodes/NodeSampler.d.ts b/src-testing/src/renderers/common/nodes/NodeSampler.d.ts deleted file mode 100644 index 60db177d5..000000000 --- a/src-testing/src/renderers/common/nodes/NodeSampler.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -import TextureNode from "../../../nodes/accessors/TextureNode.js"; -import UniformGroupNode from "../../../nodes/core/UniformGroupNode.js"; -import Sampler from "../Sampler.js"; - -declare class NodeSampler extends Sampler { - textureNode: TextureNode | undefined; - groupNode: UniformGroupNode; - constructor(name: string, textureNode: TextureNode | undefined, groupNode: UniformGroupNode); - update(): void; -} - -export default NodeSampler; diff --git a/src-testing/src/renderers/common/nodes/NodeUniform.ts b/src-testing/src/renderers/common/nodes/NodeUniform.ts deleted file mode 100644 index 659f5a82f..000000000 --- a/src-testing/src/renderers/common/nodes/NodeUniform.ts +++ /dev/null @@ -1,103 +0,0 @@ -import { - NumberUniform, - Vector2Uniform, - Vector3Uniform, - Vector4Uniform, - ColorUniform, - Matrix3Uniform, - Matrix4Uniform, -} from '../Uniform.js'; - -class NumberNodeUniform extends NumberUniform { - constructor(nodeUniform) { - super(nodeUniform.name, nodeUniform.value); - - this.nodeUniform = nodeUniform; - } - - getValue() { - return this.nodeUniform.value; - } -} - -class Vector2NodeUniform extends Vector2Uniform { - constructor(nodeUniform) { - super(nodeUniform.name, nodeUniform.value); - - this.nodeUniform = nodeUniform; - } - - getValue() { - return this.nodeUniform.value; - } -} - -class Vector3NodeUniform extends Vector3Uniform { - constructor(nodeUniform) { - super(nodeUniform.name, nodeUniform.value); - - this.nodeUniform = nodeUniform; - } - - getValue() { - return this.nodeUniform.value; - } -} - -class Vector4NodeUniform extends Vector4Uniform { - constructor(nodeUniform) { - super(nodeUniform.name, nodeUniform.value); - - this.nodeUniform = nodeUniform; - } - - getValue() { - return this.nodeUniform.value; - } -} - -class ColorNodeUniform extends ColorUniform { - constructor(nodeUniform) { - super(nodeUniform.name, nodeUniform.value); - - this.nodeUniform = nodeUniform; - } - - getValue() { - return this.nodeUniform.value; - } -} - -class Matrix3NodeUniform extends Matrix3Uniform { - constructor(nodeUniform) { - super(nodeUniform.name, nodeUniform.value); - - this.nodeUniform = nodeUniform; - } - - getValue() { - return this.nodeUniform.value; - } -} - -class Matrix4NodeUniform extends Matrix4Uniform { - constructor(nodeUniform) { - super(nodeUniform.name, nodeUniform.value); - - this.nodeUniform = nodeUniform; - } - - getValue() { - return this.nodeUniform.value; - } -} - -export { - NumberNodeUniform, - Vector2NodeUniform, - Vector3NodeUniform, - Vector4NodeUniform, - ColorNodeUniform, - Matrix3NodeUniform, - Matrix4NodeUniform, -}; diff --git a/src-testing/src/renderers/common/nodes/NodeUniformsGroup.ts b/src-testing/src/renderers/common/nodes/NodeUniformsGroup.ts deleted file mode 100644 index d2d92cb20..000000000 --- a/src-testing/src/renderers/common/nodes/NodeUniformsGroup.ts +++ /dev/null @@ -1,30 +0,0 @@ -import UniformsGroup from '../UniformsGroup.js'; - -let _id = 0; - -class NodeUniformsGroup extends UniformsGroup { - constructor(name, groupNode) { - super(name); - - this.id = _id++; - this.groupNode = groupNode; - - this.isNodeUniformsGroup = true; - } - - getNodes() { - const nodes = []; - - for (const uniform of this.uniforms) { - const node = uniform.nodeUniform.node; - - if (!node) throw new Error('NodeUniformsGroup: Uniform has no node.'); - - nodes.push(node); - } - - return nodes; - } -} - -export default NodeUniformsGroup; diff --git a/src-testing/src/renderers/common/nodes/Nodes.ts b/src-testing/src/renderers/common/nodes/Nodes.ts deleted file mode 100644 index a162ac892..000000000 --- a/src-testing/src/renderers/common/nodes/Nodes.ts +++ /dev/null @@ -1,399 +0,0 @@ -import DataMap from '../DataMap.js'; -import ChainMap from '../ChainMap.js'; -import NodeBuilderState from './NodeBuilderState.js'; -import { - NodeFrame, - objectGroup, - renderGroup, - frameGroup, - cubeTexture, - texture, - rangeFog, - densityFog, - reference, - viewportBottomLeft, - normalWorld, - pmremTexture, - viewportTopLeft, -} from '../../../nodes/Nodes.js'; - -import { EquirectangularReflectionMapping, EquirectangularRefractionMapping } from '../../../constants.js'; - -const outputNodeMap = new WeakMap(); - -class Nodes extends DataMap { - constructor(renderer, backend) { - super(); - - this.renderer = renderer; - this.backend = backend; - this.nodeFrame = new NodeFrame(); - this.nodeBuilderCache = new Map(); - this.callHashCache = new ChainMap(); - this.groupsData = new ChainMap(); - } - - updateGroup(nodeUniformsGroup) { - const groupNode = nodeUniformsGroup.groupNode; - const name = groupNode.name; - - // objectGroup is every updated - - if (name === objectGroup.name) return true; - - // renderGroup is updated once per render/compute call - - if (name === renderGroup.name) { - const uniformsGroupData = this.get(nodeUniformsGroup); - const renderId = this.nodeFrame.renderId; - - if (uniformsGroupData.renderId !== renderId) { - uniformsGroupData.renderId = renderId; - - return true; - } - - return false; - } - - // frameGroup is updated once per frame - - if (name === frameGroup.name) { - const uniformsGroupData = this.get(nodeUniformsGroup); - const frameId = this.nodeFrame.frameId; - - if (uniformsGroupData.frameId !== frameId) { - uniformsGroupData.frameId = frameId; - - return true; - } - - return false; - } - - // other groups are updated just when groupNode.needsUpdate is true - - const groupChain = [groupNode, nodeUniformsGroup]; - - let groupData = this.groupsData.get(groupChain); - if (groupData === undefined) this.groupsData.set(groupChain, (groupData = {})); - - if (groupData.version !== groupNode.version) { - groupData.version = groupNode.version; - - return true; - } - - return false; - } - - getForRenderCacheKey(renderObject) { - return renderObject.initialCacheKey; - } - - getForRender(renderObject) { - const renderObjectData = this.get(renderObject); - - let nodeBuilderState = renderObjectData.nodeBuilderState; - - if (nodeBuilderState === undefined) { - const { nodeBuilderCache } = this; - - const cacheKey = this.getForRenderCacheKey(renderObject); - - nodeBuilderState = nodeBuilderCache.get(cacheKey); - - if (nodeBuilderState === undefined) { - const nodeBuilder = this.backend.createNodeBuilder(renderObject.object, this.renderer); - nodeBuilder.scene = renderObject.scene; - nodeBuilder.material = renderObject.material; - nodeBuilder.camera = renderObject.camera; - nodeBuilder.context.material = renderObject.material; - nodeBuilder.lightsNode = renderObject.lightsNode; - nodeBuilder.environmentNode = this.getEnvironmentNode(renderObject.scene); - nodeBuilder.fogNode = this.getFogNode(renderObject.scene); - nodeBuilder.clippingContext = renderObject.clippingContext; - nodeBuilder.build(); - - nodeBuilderState = this._createNodeBuilderState(nodeBuilder); - - nodeBuilderCache.set(cacheKey, nodeBuilderState); - } - - nodeBuilderState.usedTimes++; - - renderObjectData.nodeBuilderState = nodeBuilderState; - } - - return nodeBuilderState; - } - - delete(object) { - if (object.isRenderObject) { - const nodeBuilderState = this.get(object).nodeBuilderState; - nodeBuilderState.usedTimes--; - - if (nodeBuilderState.usedTimes === 0) { - this.nodeBuilderCache.delete(this.getForRenderCacheKey(object)); - } - } - - return super.delete(object); - } - - getForCompute(computeNode) { - const computeData = this.get(computeNode); - - let nodeBuilderState = computeData.nodeBuilderState; - - if (nodeBuilderState === undefined) { - const nodeBuilder = this.backend.createNodeBuilder(computeNode, this.renderer); - nodeBuilder.build(); - - nodeBuilderState = this._createNodeBuilderState(nodeBuilder); - - computeData.nodeBuilderState = nodeBuilderState; - } - - return nodeBuilderState; - } - - _createNodeBuilderState(nodeBuilder) { - return new NodeBuilderState( - nodeBuilder.vertexShader, - nodeBuilder.fragmentShader, - nodeBuilder.computeShader, - nodeBuilder.getAttributesArray(), - nodeBuilder.getBindings(), - nodeBuilder.updateNodes, - nodeBuilder.updateBeforeNodes, - nodeBuilder.updateAfterNodes, - nodeBuilder.instanceBindGroups, - nodeBuilder.transforms, - ); - } - - getEnvironmentNode(scene) { - return scene.environmentNode || this.get(scene).environmentNode || null; - } - - getBackgroundNode(scene) { - return scene.backgroundNode || this.get(scene).backgroundNode || null; - } - - getFogNode(scene) { - return scene.fogNode || this.get(scene).fogNode || null; - } - - getCacheKey(scene, lightsNode) { - const chain = [scene, lightsNode]; - const callId = this.renderer.info.calls; - - let cacheKeyData = this.callHashCache.get(chain); - - if (cacheKeyData === undefined || cacheKeyData.callId !== callId) { - const environmentNode = this.getEnvironmentNode(scene); - const fogNode = this.getFogNode(scene); - - const cacheKey = []; - - if (lightsNode) cacheKey.push(lightsNode.getCacheKey(true)); - if (environmentNode) cacheKey.push(environmentNode.getCacheKey()); - if (fogNode) cacheKey.push(fogNode.getCacheKey()); - - cacheKeyData = { - callId, - cacheKey: cacheKey.join(','), - }; - - this.callHashCache.set(chain, cacheKeyData); - } - - return cacheKeyData.cacheKey; - } - - updateScene(scene) { - this.updateEnvironment(scene); - this.updateFog(scene); - this.updateBackground(scene); - } - - get isToneMappingState() { - return this.renderer.getRenderTarget() ? false : true; - } - - updateBackground(scene) { - const sceneData = this.get(scene); - const background = scene.background; - - if (background) { - if (sceneData.background !== background) { - let backgroundNode = null; - - if ( - background.isCubeTexture === true || - background.mapping === EquirectangularReflectionMapping || - background.mapping === EquirectangularRefractionMapping - ) { - backgroundNode = pmremTexture(background, normalWorld); - } else if (background.isTexture === true) { - backgroundNode = texture(background, viewportBottomLeft).setUpdateMatrix(true); - } else if (background.isColor !== true) { - console.error('WebGPUNodes: Unsupported background configuration.', background); - } - - sceneData.backgroundNode = backgroundNode; - sceneData.background = background; - } - } else if (sceneData.backgroundNode) { - delete sceneData.backgroundNode; - delete sceneData.background; - } - } - - updateFog(scene) { - const sceneData = this.get(scene); - const fog = scene.fog; - - if (fog) { - if (sceneData.fog !== fog) { - let fogNode = null; - - if (fog.isFogExp2) { - fogNode = densityFog(reference('color', 'color', fog), reference('density', 'float', fog)); - } else if (fog.isFog) { - fogNode = rangeFog( - reference('color', 'color', fog), - reference('near', 'float', fog), - reference('far', 'float', fog), - ); - } else { - console.error('WebGPUNodes: Unsupported fog configuration.', fog); - } - - sceneData.fogNode = fogNode; - sceneData.fog = fog; - } - } else { - delete sceneData.fogNode; - delete sceneData.fog; - } - } - - updateEnvironment(scene) { - const sceneData = this.get(scene); - const environment = scene.environment; - - if (environment) { - if (sceneData.environment !== environment) { - let environmentNode = null; - - if (environment.isCubeTexture === true) { - environmentNode = cubeTexture(environment); - } else if (environment.isTexture === true) { - environmentNode = texture(environment); - } else { - console.error('Nodes: Unsupported environment configuration.', environment); - } - - sceneData.environmentNode = environmentNode; - sceneData.environment = environment; - } - } else if (sceneData.environmentNode) { - delete sceneData.environmentNode; - delete sceneData.environment; - } - } - - getNodeFrame(renderer = this.renderer, scene = null, object = null, camera = null, material = null) { - const nodeFrame = this.nodeFrame; - nodeFrame.renderer = renderer; - nodeFrame.scene = scene; - nodeFrame.object = object; - nodeFrame.camera = camera; - nodeFrame.material = material; - - return nodeFrame; - } - - getNodeFrameForRender(renderObject) { - return this.getNodeFrame( - renderObject.renderer, - renderObject.scene, - renderObject.object, - renderObject.camera, - renderObject.material, - ); - } - - getOutputCacheKey() { - const renderer = this.renderer; - - return renderer.toneMapping + ',' + renderer.currentColorSpace; - } - - hasOutputChange(outputTarget) { - const cacheKey = outputNodeMap.get(outputTarget); - - return cacheKey !== this.getOutputCacheKey(); - } - - getOutputNode(outputTexture) { - const renderer = this.renderer; - const cacheKey = this.getOutputCacheKey(); - - const output = texture(outputTexture, viewportTopLeft).renderOutput( - renderer.toneMapping, - renderer.currentColorSpace, - ); - - outputNodeMap.set(outputTexture, cacheKey); - - return output; - } - - updateBefore(renderObject) { - const nodeFrame = this.getNodeFrameForRender(renderObject); - const nodeBuilder = renderObject.getNodeBuilderState(); - - for (const node of nodeBuilder.updateBeforeNodes) { - nodeFrame.updateBeforeNode(node); - } - } - - updateAfter(renderObject) { - const nodeFrame = this.getNodeFrameForRender(renderObject); - const nodeBuilder = renderObject.getNodeBuilderState(); - - for (const node of nodeBuilder.updateAfterNodes) { - nodeFrame.updateAfterNode(node); - } - } - - updateForCompute(computeNode) { - const nodeFrame = this.getNodeFrame(); - const nodeBuilder = this.getForCompute(computeNode); - - for (const node of nodeBuilder.updateNodes) { - nodeFrame.updateNode(node); - } - } - - updateForRender(renderObject) { - const nodeFrame = this.getNodeFrameForRender(renderObject); - const nodeBuilder = renderObject.getNodeBuilderState(); - - for (const node of nodeBuilder.updateNodes) { - nodeFrame.updateNode(node); - } - } - - dispose() { - super.dispose(); - - this.nodeFrame = new NodeFrame(); - this.nodeBuilderCache = new Map(); - } -} - -export default Nodes; diff --git a/src-testing/src/renderers/shaders/ShaderChunk.d.ts b/src-testing/src/renderers/shaders/ShaderChunk.d.ts deleted file mode 100644 index caf4e39bb..000000000 --- a/src-testing/src/renderers/shaders/ShaderChunk.d.ts +++ /dev/null @@ -1,143 +0,0 @@ -// Renderers / Shaders ///////////////////////////////////////////////////////////////////// -export const ShaderChunk: { - alphahash_fragment: string; - alphahash_pars_fragment: string; - alphamap_fragment: string; - alphamap_pars_fragment: string; - alphatest_fragment: string; - alphatest_pars_fragment: string; - aomap_fragment: string; - aomap_pars_fragment: string; - batching_pars_vertex: string; - begin_vertex: string; - beginnormal_vertex: string; - bsdfs: string; - iridescence_fragment: string; - bumpmap_pars_fragment: string; - clipping_planes_fragment: string; - clipping_planes_pars_fragment: string; - clipping_planes_pars_vertex: string; - clipping_planes_vertex: string; - color_fragment: string; - color_pars_fragment: string; - color_pars_vertex: string; - color_vertex: string; - common: string; - cube_uv_reflection_fragment: string; - defaultnormal_vertex: string; - displacementmap_pars_vertex: string; - displacementmap_vertex: string; - emissivemap_fragment: string; - emissivemap_pars_fragment: string; - colorspace_fragment: string; - colorspace_pars_fragment: string; - envmap_fragment: string; - envmap_common_pars_fragment: string; - envmap_pars_fragment: string; - envmap_pars_vertex: string; - envmap_physical_pars_fragment: string; - envmap_vertex: string; - fog_vertex: string; - fog_pars_vertex: string; - fog_fragment: string; - fog_pars_fragment: string; - gradientmap_pars_fragment: string; - lightmap_pars_fragment: string; - lights_lambert_fragment: string; - lights_lambert_pars_fragment: string; - lights_pars_begin: string; - lights_toon_fragment: string; - lights_toon_pars_fragment: string; - lights_phong_fragment: string; - lights_phong_pars_fragment: string; - lights_physical_fragment: string; - lights_physical_pars_fragment: string; - lights_fragment_begin: string; - lights_fragment_maps: string; - lights_fragment_end: string; - logdepthbuf_fragment: string; - logdepthbuf_pars_fragment: string; - logdepthbuf_pars_vertex: string; - logdepthbuf_vertex: string; - map_fragment: string; - map_pars_fragment: string; - map_particle_fragment: string; - map_particle_pars_fragment: string; - metalnessmap_fragment: string; - metalnessmap_pars_fragment: string; - morphcolor_vertex: string; - morphnormal_vertex: string; - morphtarget_pars_vertex: string; - morphtarget_vertex: string; - normal_fragment_begin: string; - normal_fragment_maps: string; - normal_pars_fragment: string; - normal_pars_vertex: string; - normal_vertex: string; - normalmap_pars_fragment: string; - clearcoat_normal_fragment_begin: string; - clearcoat_normal_fragment_maps: string; - clearcoat_pars_fragment: string; - iridescence_pars_fragment: string; - opaque_fragment: string; - packing: string; - premultiplied_alpha_fragment: string; - project_vertex: string; - dithering_fragment: string; - dithering_pars_fragment: string; - roughnessmap_fragment: string; - roughnessmap_pars_fragment: string; - shadowmap_pars_fragment: string; - shadowmap_pars_vertex: string; - shadowmap_vertex: string; - shadowmask_pars_fragment: string; - skinbase_vertex: string; - skinning_pars_vertex: string; - skinning_vertex: string; - skinnormal_vertex: string; - specularmap_fragment: string; - specularmap_pars_fragment: string; - tonemapping_fragment: string; - tonemapping_pars_fragment: string; - transmission_fragment: string; - transmission_pars_fragment: string; - uv_pars_fragment: string; - uv_pars_vertex: string; - uv_vertex: string; - worldpos_vertex: string; - - background_vert: string; - background_frag: string; - backgroundCube_vert: string; - backgroundCube_frag: string; - cube_vert: string; - cube_frag: string; - depth_vert: string; - depth_frag: string; - distanceRGBA_vert: string; - distanceRGBA_frag: string; - equirect_vert: string; - equirect_frag: string; - linedashed_vert: string; - linedashed_frag: string; - meshbasic_vert: string; - meshbasic_frag: string; - meshlambert_vert: string; - meshlambert_frag: string; - meshmatcap_vert: string; - meshmatcap_frag: string; - meshnormal_vert: string; - meshnormal_frag: string; - meshphong_vert: string; - meshphong_frag: string; - meshphysical_vert: string; - meshphysical_frag: string; - meshtoon_vert: string; - meshtoon_frag: string; - points_vert: string; - points_frag: string; - shadow_vert: string; - shadow_frag: string; - sprite_vert: string; - sprite_frag: string; -}; diff --git a/src-testing/src/renderers/shaders/ShaderLib.d.ts b/src-testing/src/renderers/shaders/ShaderLib.d.ts deleted file mode 100644 index 9a52c4dcd..000000000 --- a/src-testing/src/renderers/shaders/ShaderLib.d.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { IUniform } from "./UniformsLib.js"; - -export interface ShaderLibShader { - uniforms: { [uniform: string]: IUniform }; - vertexShader: string; - fragmentShader: string; -} - -declare const ShaderLib: { - [name: string]: ShaderLibShader; - basic: ShaderLibShader; - lambert: ShaderLibShader; - phong: ShaderLibShader; - standard: ShaderLibShader; - matcap: ShaderLibShader; - points: ShaderLibShader; - dashed: ShaderLibShader; - depth: ShaderLibShader; - normal: ShaderLibShader; - sprite: ShaderLibShader; - background: ShaderLibShader; - cube: ShaderLibShader; - equirect: ShaderLibShader; - distanceRGBA: ShaderLibShader; - shadow: ShaderLibShader; - physical: ShaderLibShader; -}; - -export { ShaderLib }; diff --git a/src-testing/src/renderers/shaders/UniformsLib.d.ts b/src-testing/src/renderers/shaders/UniformsLib.d.ts deleted file mode 100644 index cb0d808bd..000000000 --- a/src-testing/src/renderers/shaders/UniformsLib.d.ts +++ /dev/null @@ -1,189 +0,0 @@ -import { Color } from "../../math/Color.js"; -import { Matrix3 } from "../../math/Matrix3.js"; -import { Vector2 } from "../../math/Vector2.js"; - -// eslint-disable-next-line @typescript-eslint/naming-convention -export interface IUniform { - value: TValue; -} - -export const UniformsLib: { - common: { - diffuse: IUniform; - opacity: IUniform; - map: IUniform; - mapTransform: IUniform; - alphaMap: IUniform; - alphaMapTransform: IUniform; - alphaTest: IUniform; - }; - specularmap: { - specularMap: IUniform; - specularMapTransform: IUniform; - }; - envmap: { - envMap: IUniform; - envMapRotation: IUniform; - flipEnvMap: IUniform; - reflectivity: IUniform; - ior: IUniform; - refractRatio: IUniform; - }; - aomap: { - aoMap: IUniform; - aoMapIntensity: IUniform; - aoMapTransform: IUniform; - }; - lightmap: { - lightMap: IUniform; - lightMapIntensity: IUniform; - lightMapTransform: IUniform; - }; - bumpmap: { - bumpMap: IUniform; - bumpMapTransform: IUniform; - bumpScale: IUniform; - }; - normalmap: { - normalMap: IUniform; - normalMapTransform: IUniform; - normalScale: IUniform; - }; - displacementmap: { - displacementMap: IUniform; - displacementMapTransform: IUniform; - displacementScale: IUniform; - displacementBias: IUniform; - }; - emissivemap: { - emissiveMap: IUniform; - emissiveMapTransform: IUniform; - }; - metalnessmap: { - metalnessMap: IUniform; - metalnessMapTransform: IUniform; - }; - roughnessmap: { - roughnessMap: IUniform; - roughnessMapTransform: IUniform; - }; - gradientmap: { - gradientMap: IUniform; - }; - fog: { - fogDensity: IUniform; - fogNear: IUniform; - fogFar: IUniform; - fogColor: IUniform; - }; - lights: { - ambientLightColor: IUniform; - lightProbe: IUniform; - directionalLights: { - value: unknown[]; - properties: { - direction: {}; - color: {}; - }; - }; - directionalLightShadows: { - value: unknown[]; - properties: { - shadowIntensity: number; - shadowBias: {}; - shadowNormalBias: {}; - shadowRadius: {}; - shadowMapSize: {}; - }; - }; - directionalShadowMap: IUniform; - directionalShadowMatrix: IUniform; - spotLights: { - value: unknown[]; - properties: { - color: {}; - position: {}; - direction: {}; - distance: {}; - coneCos: {}; - penumbraCos: {}; - decay: {}; - }; - }; - spotLightShadows: { - value: unknown[]; - properties: { - shadowIntensity: number; - shadowBias: {}; - shadowNormalBias: {}; - shadowRadius: {}; - shadowMapSize: {}; - }; - }; - spotLightMap: IUniform; - spotShadowMap: IUniform; - spotLightMatrix: IUniform; - pointLights: { - value: unknown[]; - properties: { - color: {}; - position: {}; - decay: {}; - distance: {}; - }; - }; - pointLightShadows: { - value: unknown[]; - properties: { - shadowIntensity: number; - shadowBias: {}; - shadowNormalBias: {}; - shadowRadius: {}; - shadowMapSize: {}; - shadowCameraNear: {}; - shadowCameraFar: {}; - }; - }; - pointShadowMap: IUniform; - pointShadowMatrix: IUniform; - hemisphereLights: { - value: unknown[]; - properties: { - direction: {}; - skycolor: {}; - groundColor: {}; - }; - }; - rectAreaLights: { - value: unknown[]; - properties: { - color: {}; - position: {}; - width: {}; - height: {}; - }; - }; - ltc_1: IUniform; - ltc_2: IUniform; - }; - points: { - diffuse: IUniform; - opacity: IUniform; - size: IUniform; - scale: IUniform; - map: IUniform; - alphaMap: IUniform; - alphaTest: IUniform; - uvTransform: IUniform; - }; - sprite: { - diffuse: IUniform; - opacity: IUniform; - center: IUniform; - rotation: IUniform; - map: IUniform; - mapTransform: IUniform; - alphaMap: IUniform; - alphaTest: IUniform; - }; -}; diff --git a/src-testing/src/renderers/shaders/UniformsUtils.d.ts b/src-testing/src/renderers/shaders/UniformsUtils.d.ts deleted file mode 100644 index fe5178d55..000000000 --- a/src-testing/src/renderers/shaders/UniformsUtils.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { UniformsGroup } from "../../core/UniformsGroup.js"; -import { IUniform } from "./UniformsLib.js"; - -export function cloneUniforms(uniformsSrc: T): T; -export function mergeUniforms(uniforms: Array<{ [uniform: string]: IUniform }>): { [uniform: string]: IUniform }; - -export function cloneUniformsGroups(src: UniformsGroup[]): UniformsGroup[]; - -declare const UniformsUtils: { - clone: typeof cloneUniforms; - merge: typeof mergeUniforms; -}; - -export { UniformsUtils }; diff --git a/src-testing/src/renderers/webgl-fallback/WebGLBackend.ts b/src-testing/src/renderers/webgl-fallback/WebGLBackend.ts deleted file mode 100644 index 35e79b78e..000000000 --- a/src-testing/src/renderers/webgl-fallback/WebGLBackend.ts +++ /dev/null @@ -1,1280 +0,0 @@ -import GLSLNodeBuilder from './nodes/GLSLNodeBuilder.js'; -import Backend from '../common/Backend.js'; -import { getCacheKey } from '../common/RenderContext.js'; - -import WebGLAttributeUtils from './utils/WebGLAttributeUtils.js'; -import WebGLState from './utils/WebGLState.js'; -import WebGLUtils from './utils/WebGLUtils.js'; -import WebGLTextureUtils from './utils/WebGLTextureUtils.js'; -import WebGLExtensions from './utils/WebGLExtensions.js'; -import WebGLCapabilities from './utils/WebGLCapabilities.js'; -import { GLFeatureName } from './utils/WebGLConstants.js'; -import { WebGLBufferRenderer } from './WebGLBufferRenderer.js'; - -import { warnOnce } from '../../utils.js'; -import { WebGLCoordinateSystem } from '../../constants.js'; - -// - -class WebGLBackend extends Backend { - constructor(parameters = {}) { - super(parameters); - - this.isWebGLBackend = true; - } - - init(renderer) { - super.init(renderer); - - // - - const parameters = this.parameters; - - const glContext = - parameters.context !== undefined ? parameters.context : renderer.domElement.getContext('webgl2'); - - this.gl = glContext; - - this.extensions = new WebGLExtensions(this); - this.capabilities = new WebGLCapabilities(this); - this.attributeUtils = new WebGLAttributeUtils(this); - this.textureUtils = new WebGLTextureUtils(this); - this.bufferRenderer = new WebGLBufferRenderer(this); - - this.state = new WebGLState(this); - this.utils = new WebGLUtils(this); - - this.vaoCache = {}; - this.transformFeedbackCache = {}; - this.discard = false; - this.trackTimestamp = parameters.trackTimestamp === true; - - this.extensions.get('EXT_color_buffer_float'); - this.extensions.get('WEBGL_multi_draw'); - - this.disjoint = this.extensions.get('EXT_disjoint_timer_query_webgl2'); - this.parallel = this.extensions.get('KHR_parallel_shader_compile'); - this._currentContext = null; - } - - get coordinateSystem() { - return WebGLCoordinateSystem; - } - - async getArrayBufferAsync(attribute) { - return await this.attributeUtils.getArrayBufferAsync(attribute); - } - - initTimestampQuery(renderContext) { - if (!this.disjoint || !this.trackTimestamp) return; - - const renderContextData = this.get(renderContext); - - if (this.queryRunning) { - if (!renderContextData.queryQueue) renderContextData.queryQueue = []; - renderContextData.queryQueue.push(renderContext); - return; - } - - if (renderContextData.activeQuery) { - this.gl.endQuery(this.disjoint.TIME_ELAPSED_EXT); - renderContextData.activeQuery = null; - } - - renderContextData.activeQuery = this.gl.createQuery(); - - if (renderContextData.activeQuery !== null) { - this.gl.beginQuery(this.disjoint.TIME_ELAPSED_EXT, renderContextData.activeQuery); - this.queryRunning = true; - } - } - - // timestamp utils - - prepareTimestampBuffer(renderContext) { - if (!this.disjoint || !this.trackTimestamp) return; - - const renderContextData = this.get(renderContext); - - if (renderContextData.activeQuery) { - this.gl.endQuery(this.disjoint.TIME_ELAPSED_EXT); - - if (!renderContextData.gpuQueries) renderContextData.gpuQueries = []; - renderContextData.gpuQueries.push({ query: renderContextData.activeQuery }); - renderContextData.activeQuery = null; - this.queryRunning = false; - - if (renderContextData.queryQueue && renderContextData.queryQueue.length > 0) { - const nextRenderContext = renderContextData.queryQueue.shift(); - this.initTimestampQuery(nextRenderContext); - } - } - } - - async resolveTimestampAsync(renderContext, type = 'render') { - if (!this.disjoint || !this.trackTimestamp) return; - - const renderContextData = this.get(renderContext); - - if (!renderContextData.gpuQueries) renderContextData.gpuQueries = []; - - for (let i = 0; i < renderContextData.gpuQueries.length; i++) { - const queryInfo = renderContextData.gpuQueries[i]; - const available = this.gl.getQueryParameter(queryInfo.query, this.gl.QUERY_RESULT_AVAILABLE); - const disjoint = this.gl.getParameter(this.disjoint.GPU_DISJOINT_EXT); - - if (available && !disjoint) { - const elapsed = this.gl.getQueryParameter(queryInfo.query, this.gl.QUERY_RESULT); - const duration = Number(elapsed) / 1000000; // Convert nanoseconds to milliseconds - this.gl.deleteQuery(queryInfo.query); - renderContextData.gpuQueries.splice(i, 1); // Remove the processed query - i--; - this.renderer.info.updateTimestamp(type, duration); - } - } - } - - getContext() { - return this.gl; - } - - beginRender(renderContext) { - const { gl } = this; - const renderContextData = this.get(renderContext); - - // - - // - - this.initTimestampQuery(renderContext); - - renderContextData.previousContext = this._currentContext; - this._currentContext = renderContext; - - this._setFramebuffer(renderContext); - - this.clear( - renderContext.clearColor, - renderContext.clearDepth, - renderContext.clearStencil, - renderContext, - false, - ); - - // - if (renderContext.viewport) { - this.updateViewport(renderContext); - } else { - gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); - } - - if (renderContext.scissor) { - const { x, y, width, height } = renderContext.scissorValue; - - gl.scissor(x, y, width, height); - } - - const occlusionQueryCount = renderContext.occlusionQueryCount; - - if (occlusionQueryCount > 0) { - // Get a reference to the array of objects with queries. The renderContextData property - // can be changed by another render pass before the async reading of all previous queries complete - renderContextData.currentOcclusionQueries = renderContextData.occlusionQueries; - renderContextData.currentOcclusionQueryObjects = renderContextData.occlusionQueryObjects; - - renderContextData.lastOcclusionObject = null; - renderContextData.occlusionQueries = new Array(occlusionQueryCount); - renderContextData.occlusionQueryObjects = new Array(occlusionQueryCount); - renderContextData.occlusionQueryIndex = 0; - } - } - - finishRender(renderContext) { - const { gl, state } = this; - const renderContextData = this.get(renderContext); - const previousContext = renderContextData.previousContext; - - const textures = renderContext.textures; - - if (textures !== null) { - for (let i = 0; i < textures.length; i++) { - const texture = textures[i]; - - if (texture.generateMipmaps) { - this.generateMipmaps(texture); - } - } - } - - this._currentContext = previousContext; - - if (renderContext.textures !== null && renderContext.renderTarget) { - const renderTargetContextData = this.get(renderContext.renderTarget); - - const { samples } = renderContext.renderTarget; - - if (samples > 0) { - const fb = renderTargetContextData.framebuffers[renderContext.getCacheKey()]; - - const mask = gl.COLOR_BUFFER_BIT; - - const msaaFrameBuffer = renderTargetContextData.msaaFrameBuffer; - - const textures = renderContext.textures; - - state.bindFramebuffer(gl.READ_FRAMEBUFFER, msaaFrameBuffer); - state.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fb); - - for (let i = 0; i < textures.length; i++) { - // TODO Add support for MRT - - gl.blitFramebuffer( - 0, - 0, - renderContext.width, - renderContext.height, - 0, - 0, - renderContext.width, - renderContext.height, - mask, - gl.NEAREST, - ); - - gl.invalidateFramebuffer(gl.READ_FRAMEBUFFER, renderTargetContextData.invalidationArray); - } - } - } - - if (previousContext !== null) { - this._setFramebuffer(previousContext); - - if (previousContext.viewport) { - this.updateViewport(previousContext); - } else { - const gl = this.gl; - - gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); - } - } - - const occlusionQueryCount = renderContext.occlusionQueryCount; - - if (occlusionQueryCount > 0) { - const renderContextData = this.get(renderContext); - - if (occlusionQueryCount > renderContextData.occlusionQueryIndex) { - const { gl } = this; - - gl.endQuery(gl.ANY_SAMPLES_PASSED); - } - - this.resolveOccludedAsync(renderContext); - } - - this.prepareTimestampBuffer(renderContext); - } - - resolveOccludedAsync(renderContext) { - const renderContextData = this.get(renderContext); - - // handle occlusion query results - - const { currentOcclusionQueries, currentOcclusionQueryObjects } = renderContextData; - - if (currentOcclusionQueries && currentOcclusionQueryObjects) { - const occluded = new WeakSet(); - const { gl } = this; - - renderContextData.currentOcclusionQueryObjects = null; - renderContextData.currentOcclusionQueries = null; - - const check = () => { - let completed = 0; - - // check all queries and requeue as appropriate - for (let i = 0; i < currentOcclusionQueries.length; i++) { - const query = currentOcclusionQueries[i]; - - if (query === null) continue; - - if (gl.getQueryParameter(query, gl.QUERY_RESULT_AVAILABLE)) { - if (gl.getQueryParameter(query, gl.QUERY_RESULT) > 0) - occluded.add(currentOcclusionQueryObjects[i]); - - currentOcclusionQueries[i] = null; - gl.deleteQuery(query); - - completed++; - } - } - - if (completed < currentOcclusionQueries.length) { - requestAnimationFrame(check); - } else { - renderContextData.occluded = occluded; - } - }; - - check(); - } - } - - isOccluded(renderContext, object) { - const renderContextData = this.get(renderContext); - - return renderContextData.occluded && renderContextData.occluded.has(object); - } - - updateViewport(renderContext) { - const gl = this.gl; - const { x, y, width, height } = renderContext.viewportValue; - - gl.viewport(x, y, width, height); - } - - setScissorTest(boolean) { - const gl = this.gl; - - if (boolean) { - gl.enable(gl.SCISSOR_TEST); - } else { - gl.disable(gl.SCISSOR_TEST); - } - } - - clear(color, depth, stencil, descriptor = null, setFrameBuffer = true) { - const { gl } = this; - - if (descriptor === null) { - descriptor = { - textures: null, - clearColorValue: this.getClearColor(), - }; - } - - // - - let clear = 0; - - if (color) clear |= gl.COLOR_BUFFER_BIT; - if (depth) clear |= gl.DEPTH_BUFFER_BIT; - if (stencil) clear |= gl.STENCIL_BUFFER_BIT; - - if (clear !== 0) { - const clearColor = descriptor.clearColorValue || this.getClearColor(); - - if (depth) this.state.setDepthMask(true); - - if (descriptor.textures === null) { - gl.clearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a); - gl.clear(clear); - } else { - if (setFrameBuffer) this._setFramebuffer(descriptor); - - if (color) { - for (let i = 0; i < descriptor.textures.length; i++) { - gl.clearBufferfv(gl.COLOR, i, [clearColor.r, clearColor.g, clearColor.b, clearColor.a]); - } - } - - if (depth && stencil) { - gl.clearBufferfi(gl.DEPTH_STENCIL, 0, 1, 0); - } else if (depth) { - gl.clearBufferfv(gl.DEPTH, 0, [1.0]); - } else if (stencil) { - gl.clearBufferiv(gl.STENCIL, 0, [0]); - } - } - } - } - - beginCompute(computeGroup) { - const gl = this.gl; - - gl.bindFramebuffer(gl.FRAMEBUFFER, null); - this.initTimestampQuery(computeGroup); - } - - compute(computeGroup, computeNode, bindings, pipeline) { - const gl = this.gl; - - if (!this.discard) { - // required here to handle async behaviour of render.compute() - gl.enable(gl.RASTERIZER_DISCARD); - this.discard = true; - } - - const { programGPU, transformBuffers, attributes } = this.get(pipeline); - - const vaoKey = this._getVaoKey(null, attributes); - - const vaoGPU = this.vaoCache[vaoKey]; - - if (vaoGPU === undefined) { - this._createVao(null, attributes); - } else { - gl.bindVertexArray(vaoGPU); - } - - gl.useProgram(programGPU); - - this._bindUniforms(bindings); - - const transformFeedbackGPU = this._getTransformFeedback(transformBuffers); - - gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, transformFeedbackGPU); - gl.beginTransformFeedback(gl.POINTS); - - if (attributes[0].isStorageInstancedBufferAttribute) { - gl.drawArraysInstanced(gl.POINTS, 0, 1, computeNode.count); - } else { - gl.drawArrays(gl.POINTS, 0, computeNode.count); - } - - gl.endTransformFeedback(); - gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, null); - - // switch active buffers - - for (let i = 0; i < transformBuffers.length; i++) { - const dualAttributeData = transformBuffers[i]; - - if (dualAttributeData.pbo) { - this.textureUtils.copyBufferToTexture(dualAttributeData.transformBuffer, dualAttributeData.pbo); - } - - dualAttributeData.switchBuffers(); - } - } - - finishCompute(computeGroup) { - const gl = this.gl; - - this.discard = false; - - gl.disable(gl.RASTERIZER_DISCARD); - - this.prepareTimestampBuffer(computeGroup); - } - - draw(renderObject /*, info*/) { - const { object, pipeline, material, context } = renderObject; - const { programGPU } = this.get(pipeline); - - const { gl, state } = this; - - const contextData = this.get(context); - - // - - this._bindUniforms(renderObject.getBindings()); - - const frontFaceCW = object.isMesh && object.matrixWorld.determinant() < 0; - - state.setMaterial(material, frontFaceCW); - - gl.useProgram(programGPU); - - // - - let vaoGPU = renderObject.staticVao; - - if (vaoGPU === undefined) { - const vaoKey = this._getVaoKey(renderObject.getIndex(), renderObject.getAttributes()); - - vaoGPU = this.vaoCache[vaoKey]; - - if (vaoGPU === undefined) { - let staticVao; - - ({ vaoGPU, staticVao } = this._createVao(renderObject.getIndex(), renderObject.getAttributes())); - - if (staticVao) renderObject.staticVao = vaoGPU; - } - } - - gl.bindVertexArray(vaoGPU); - - // - - const index = renderObject.getIndex(); - - const geometry = renderObject.geometry; - const drawRange = renderObject.drawRange; - const firstVertex = drawRange.start; - - // - - const lastObject = contextData.lastOcclusionObject; - - if (lastObject !== object && lastObject !== undefined) { - if (lastObject !== null && lastObject.occlusionTest === true) { - gl.endQuery(gl.ANY_SAMPLES_PASSED); - - contextData.occlusionQueryIndex++; - } - - if (object.occlusionTest === true) { - const query = gl.createQuery(); - - gl.beginQuery(gl.ANY_SAMPLES_PASSED, query); - - contextData.occlusionQueries[contextData.occlusionQueryIndex] = query; - contextData.occlusionQueryObjects[contextData.occlusionQueryIndex] = object; - } - - contextData.lastOcclusionObject = object; - } - - // - - const renderer = this.bufferRenderer; - - if (object.isPoints) renderer.mode = gl.POINTS; - else if (object.isLineSegments) renderer.mode = gl.LINES; - else if (object.isLine) renderer.mode = gl.LINE_STRIP; - else if (object.isLineLoop) renderer.mode = gl.LINE_LOOP; - else { - if (material.wireframe === true) { - state.setLineWidth(material.wireframeLinewidth * this.renderer.getPixelRatio()); - renderer.mode = gl.LINES; - } else { - renderer.mode = gl.TRIANGLES; - } - } - - // - - let count; - - renderer.object = object; - - if (index !== null) { - const indexData = this.get(index); - const indexCount = drawRange.count !== Infinity ? drawRange.count : index.count; - - renderer.index = index.count; - renderer.type = indexData.type; - - count = indexCount; - } else { - renderer.index = 0; - - const vertexCount = drawRange.count !== Infinity ? drawRange.count : geometry.attributes.position.count; - - count = vertexCount; - } - - const instanceCount = this.getInstanceCount(renderObject); - - if (object.isBatchedMesh) { - if (object._multiDrawInstances !== null) { - renderer.renderMultiDrawInstances( - object._multiDrawStarts, - object._multiDrawCounts, - object._multiDrawCount, - object._multiDrawInstances, - ); - } else if (!this.hasFeature('WEBGL_multi_draw')) { - warnOnce('THREE.WebGLRenderer: WEBGL_multi_draw not supported.'); - } else { - renderer.renderMultiDraw(object._multiDrawStarts, object._multiDrawCounts, object._multiDrawCount); - } - } else if (instanceCount > 1) { - renderer.renderInstances(firstVertex, count, instanceCount); - } else { - renderer.render(firstVertex, count); - } - // - - gl.bindVertexArray(null); - } - - needsRenderUpdate(/*renderObject*/) { - return false; - } - - getRenderCacheKey(renderObject) { - return renderObject.id; - } - - // textures - - createDefaultTexture(texture) { - this.textureUtils.createDefaultTexture(texture); - } - - createTexture(texture, options) { - this.textureUtils.createTexture(texture, options); - } - - updateTexture(texture, options) { - this.textureUtils.updateTexture(texture, options); - } - - generateMipmaps(texture) { - this.textureUtils.generateMipmaps(texture); - } - - destroyTexture(texture) { - this.textureUtils.destroyTexture(texture); - } - - copyTextureToBuffer(texture, x, y, width, height) { - return this.textureUtils.copyTextureToBuffer(texture, x, y, width, height); - } - - createSampler(/*texture*/) { - //console.warn( 'Abstract class.' ); - } - - destroySampler() {} - - // node builder - - createNodeBuilder(object, renderer) { - return new GLSLNodeBuilder(object, renderer); - } - - // program - - createProgram(program) { - const gl = this.gl; - const { stage, code } = program; - - const shader = stage === 'fragment' ? gl.createShader(gl.FRAGMENT_SHADER) : gl.createShader(gl.VERTEX_SHADER); - - gl.shaderSource(shader, code); - gl.compileShader(shader); - - this.set(program, { - shaderGPU: shader, - }); - } - - destroyProgram(/*program*/) { - console.warn('Abstract class.'); - } - - createRenderPipeline(renderObject, promises) { - const gl = this.gl; - const pipeline = renderObject.pipeline; - - // Program - - const { fragmentProgram, vertexProgram } = pipeline; - - const programGPU = gl.createProgram(); - - const fragmentShader = this.get(fragmentProgram).shaderGPU; - const vertexShader = this.get(vertexProgram).shaderGPU; - - gl.attachShader(programGPU, fragmentShader); - gl.attachShader(programGPU, vertexShader); - gl.linkProgram(programGPU); - - this.set(pipeline, { - programGPU, - fragmentShader, - vertexShader, - }); - - if (promises !== null && this.parallel) { - const p = new Promise((resolve /*, reject*/) => { - const parallel = this.parallel; - const checkStatus = () => { - if (gl.getProgramParameter(programGPU, parallel.COMPLETION_STATUS_KHR)) { - this._completeCompile(renderObject, pipeline); - resolve(); - } else { - requestAnimationFrame(checkStatus); - } - }; - - checkStatus(); - }); - - promises.push(p); - - return; - } - - this._completeCompile(renderObject, pipeline); - } - - _handleSource(string, errorLine) { - const lines = string.split('\n'); - const lines2 = []; - - const from = Math.max(errorLine - 6, 0); - const to = Math.min(errorLine + 6, lines.length); - - for (let i = from; i < to; i++) { - const line = i + 1; - lines2.push(`${line === errorLine ? '>' : ' '} ${line}: ${lines[i]}`); - } - - return lines2.join('\n'); - } - - _getShaderErrors(gl, shader, type) { - const status = gl.getShaderParameter(shader, gl.COMPILE_STATUS); - const errors = gl.getShaderInfoLog(shader).trim(); - - if (status && errors === '') return ''; - - const errorMatches = /ERROR: 0:(\d+)/.exec(errors); - if (errorMatches) { - const errorLine = parseInt(errorMatches[1]); - return ( - type.toUpperCase() + - '\n\n' + - errors + - '\n\n' + - this._handleSource(gl.getShaderSource(shader), errorLine) - ); - } else { - return errors; - } - } - - _logProgramError(programGPU, glFragmentShader, glVertexShader) { - if (this.renderer.debug.checkShaderErrors) { - const gl = this.gl; - - const programLog = gl.getProgramInfoLog(programGPU).trim(); - - if (gl.getProgramParameter(programGPU, gl.LINK_STATUS) === false) { - if (typeof this.renderer.debug.onShaderError === 'function') { - this.renderer.debug.onShaderError(gl, programGPU, glVertexShader, glFragmentShader); - } else { - // default error reporting - - const vertexErrors = this._getShaderErrors(gl, glVertexShader, 'vertex'); - const fragmentErrors = this._getShaderErrors(gl, glFragmentShader, 'fragment'); - - console.error( - 'THREE.WebGLProgram: Shader Error ' + - gl.getError() + - ' - ' + - 'VALIDATE_STATUS ' + - gl.getProgramParameter(programGPU, gl.VALIDATE_STATUS) + - '\n\n' + - 'Program Info Log: ' + - programLog + - '\n' + - vertexErrors + - '\n' + - fragmentErrors, - ); - } - } else if (programLog !== '') { - console.warn('THREE.WebGLProgram: Program Info Log:', programLog); - } - } - } - - _completeCompile(renderObject, pipeline) { - const gl = this.gl; - const pipelineData = this.get(pipeline); - const { programGPU, fragmentShader, vertexShader } = pipelineData; - - if (gl.getProgramParameter(programGPU, gl.LINK_STATUS) === false) { - this._logProgramError(programGPU, fragmentShader, vertexShader); - } - - gl.useProgram(programGPU); - - // Bindings - - const bindings = renderObject.getBindings(); - - this._setupBindings(bindings, programGPU); - - // - - this.set(pipeline, { - programGPU, - }); - } - - createComputePipeline(computePipeline, bindings) { - const gl = this.gl; - - // Program - - const fragmentProgram = { - stage: 'fragment', - code: '#version 300 es\nprecision highp float;\nvoid main() {}', - }; - - this.createProgram(fragmentProgram); - - const { computeProgram } = computePipeline; - - const programGPU = gl.createProgram(); - - const fragmentShader = this.get(fragmentProgram).shaderGPU; - const vertexShader = this.get(computeProgram).shaderGPU; - - const transforms = computeProgram.transforms; - - const transformVaryingNames = []; - const transformAttributeNodes = []; - - for (let i = 0; i < transforms.length; i++) { - const transform = transforms[i]; - - transformVaryingNames.push(transform.varyingName); - transformAttributeNodes.push(transform.attributeNode); - } - - gl.attachShader(programGPU, fragmentShader); - gl.attachShader(programGPU, vertexShader); - - gl.transformFeedbackVaryings(programGPU, transformVaryingNames, gl.SEPARATE_ATTRIBS); - - gl.linkProgram(programGPU); - - if (gl.getProgramParameter(programGPU, gl.LINK_STATUS) === false) { - this._logProgramError(programGPU, fragmentShader, vertexShader); - } - - gl.useProgram(programGPU); - - // Bindings - - this.createBindings(null, bindings); - - this._setupBindings(bindings, programGPU); - - const attributeNodes = computeProgram.attributes; - const attributes = []; - const transformBuffers = []; - - for (let i = 0; i < attributeNodes.length; i++) { - const attribute = attributeNodes[i].node.attribute; - - attributes.push(attribute); - - if (!this.has(attribute)) this.attributeUtils.createAttribute(attribute, gl.ARRAY_BUFFER); - } - - for (let i = 0; i < transformAttributeNodes.length; i++) { - const attribute = transformAttributeNodes[i].attribute; - - if (!this.has(attribute)) this.attributeUtils.createAttribute(attribute, gl.ARRAY_BUFFER); - - const attributeData = this.get(attribute); - - transformBuffers.push(attributeData); - } - - // - - this.set(computePipeline, { - programGPU, - transformBuffers, - attributes, - }); - } - - createBindings(bindGroup, bindings) { - this.updateBindings(bindGroup, bindings); - } - - updateBindings(bindGroup, bindings) { - const { gl } = this; - - let groupIndex = 0; - let textureIndex = 0; - - for (const bindGroup of bindings) { - for (const binding of bindGroup.bindings) { - if (binding.isUniformsGroup || binding.isUniformBuffer) { - const bufferGPU = gl.createBuffer(); - const data = binding.buffer; - - gl.bindBuffer(gl.UNIFORM_BUFFER, bufferGPU); - gl.bufferData(gl.UNIFORM_BUFFER, data, gl.DYNAMIC_DRAW); - gl.bindBufferBase(gl.UNIFORM_BUFFER, groupIndex, bufferGPU); - - this.set(binding, { - index: groupIndex++, - bufferGPU, - }); - } else if (binding.isSampledTexture) { - const { textureGPU, glTextureType } = this.get(binding.texture); - - this.set(binding, { - index: textureIndex++, - textureGPU, - glTextureType, - }); - } - } - } - } - - updateBinding(binding) { - const gl = this.gl; - - if (binding.isUniformsGroup || binding.isUniformBuffer) { - const bindingData = this.get(binding); - const bufferGPU = bindingData.bufferGPU; - const data = binding.buffer; - - gl.bindBuffer(gl.UNIFORM_BUFFER, bufferGPU); - gl.bufferData(gl.UNIFORM_BUFFER, data, gl.DYNAMIC_DRAW); - } - } - - // attributes - - createIndexAttribute(attribute) { - const gl = this.gl; - - this.attributeUtils.createAttribute(attribute, gl.ELEMENT_ARRAY_BUFFER); - } - - createAttribute(attribute) { - if (this.has(attribute)) return; - - const gl = this.gl; - - this.attributeUtils.createAttribute(attribute, gl.ARRAY_BUFFER); - } - - createStorageAttribute(attribute) { - if (this.has(attribute)) return; - - const gl = this.gl; - - this.attributeUtils.createAttribute(attribute, gl.ARRAY_BUFFER); - } - - updateAttribute(attribute) { - this.attributeUtils.updateAttribute(attribute); - } - - destroyAttribute(attribute) { - this.attributeUtils.destroyAttribute(attribute); - } - - updateSize() { - //console.warn( 'Abstract class.' ); - } - - hasFeature(name) { - const keysMatching = Object.keys(GLFeatureName).filter(key => GLFeatureName[key] === name); - - const extensions = this.extensions; - - for (let i = 0; i < keysMatching.length; i++) { - if (extensions.has(keysMatching[i])) return true; - } - - return false; - } - - getMaxAnisotropy() { - return this.capabilities.getMaxAnisotropy(); - } - - copyTextureToTexture(position, srcTexture, dstTexture, level) { - this.textureUtils.copyTextureToTexture(position, srcTexture, dstTexture, level); - } - - copyFramebufferToTexture(texture, renderContext) { - this.textureUtils.copyFramebufferToTexture(texture, renderContext); - } - - _setFramebuffer(descriptor) { - const { gl, state } = this; - - let currentFrameBuffer = null; - - if (descriptor.textures !== null) { - const renderTarget = descriptor.renderTarget; - const renderTargetContextData = this.get(renderTarget); - const { samples, depthBuffer, stencilBuffer } = renderTarget; - - const isCube = renderTarget.isWebGLCubeRenderTarget === true; - - let msaaFb = renderTargetContextData.msaaFrameBuffer; - let depthRenderbuffer = renderTargetContextData.depthRenderbuffer; - - const cacheKey = getCacheKey(descriptor); - - let fb; - - if (isCube) { - renderTargetContextData.cubeFramebuffers || (renderTargetContextData.cubeFramebuffers = {}); - - fb = renderTargetContextData.cubeFramebuffers[cacheKey]; - } else { - renderTargetContextData.framebuffers || (renderTargetContextData.framebuffers = {}); - - fb = renderTargetContextData.framebuffers[cacheKey]; - } - - if (fb === undefined) { - fb = gl.createFramebuffer(); - - state.bindFramebuffer(gl.FRAMEBUFFER, fb); - - const textures = descriptor.textures; - - if (isCube) { - renderTargetContextData.cubeFramebuffers[cacheKey] = fb; - - const { textureGPU } = this.get(textures[0]); - - const cubeFace = this.renderer._activeCubeFace; - - gl.framebufferTexture2D( - gl.FRAMEBUFFER, - gl.COLOR_ATTACHMENT0, - gl.TEXTURE_CUBE_MAP_POSITIVE_X + cubeFace, - textureGPU, - 0, - ); - } else { - renderTargetContextData.framebuffers[cacheKey] = fb; - - for (let i = 0; i < textures.length; i++) { - const texture = textures[i]; - const textureData = this.get(texture); - textureData.renderTarget = descriptor.renderTarget; - - const attachment = gl.COLOR_ATTACHMENT0 + i; - - gl.framebufferTexture2D(gl.FRAMEBUFFER, attachment, gl.TEXTURE_2D, textureData.textureGPU, 0); - } - - state.drawBuffers(descriptor, fb); - } - - if (descriptor.depthTexture !== null) { - const textureData = this.get(descriptor.depthTexture); - const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT; - - gl.framebufferTexture2D(gl.FRAMEBUFFER, depthStyle, gl.TEXTURE_2D, textureData.textureGPU, 0); - } - } - - if (samples > 0) { - if (msaaFb === undefined) { - const invalidationArray = []; - - msaaFb = gl.createFramebuffer(); - - state.bindFramebuffer(gl.FRAMEBUFFER, msaaFb); - - const msaaRenderbuffers = []; - - const textures = descriptor.textures; - - for (let i = 0; i < textures.length; i++) { - msaaRenderbuffers[i] = gl.createRenderbuffer(); - - gl.bindRenderbuffer(gl.RENDERBUFFER, msaaRenderbuffers[i]); - - invalidationArray.push(gl.COLOR_ATTACHMENT0 + i); - - if (depthBuffer) { - const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT; - invalidationArray.push(depthStyle); - } - - const texture = descriptor.textures[i]; - const textureData = this.get(texture); - - gl.renderbufferStorageMultisample( - gl.RENDERBUFFER, - samples, - textureData.glInternalFormat, - descriptor.width, - descriptor.height, - ); - gl.framebufferRenderbuffer( - gl.FRAMEBUFFER, - gl.COLOR_ATTACHMENT0 + i, - gl.RENDERBUFFER, - msaaRenderbuffers[i], - ); - } - - renderTargetContextData.msaaFrameBuffer = msaaFb; - renderTargetContextData.msaaRenderbuffers = msaaRenderbuffers; - - if (depthRenderbuffer === undefined) { - depthRenderbuffer = gl.createRenderbuffer(); - this.textureUtils.setupRenderBufferStorage(depthRenderbuffer, descriptor); - - renderTargetContextData.depthRenderbuffer = depthRenderbuffer; - - const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT; - invalidationArray.push(depthStyle); - } - - renderTargetContextData.invalidationArray = invalidationArray; - } - - currentFrameBuffer = renderTargetContextData.msaaFrameBuffer; - } else { - currentFrameBuffer = fb; - } - } - - state.bindFramebuffer(gl.FRAMEBUFFER, currentFrameBuffer); - } - - _getVaoKey(index, attributes) { - let key = []; - - if (index !== null) { - const indexData = this.get(index); - - key += ':' + indexData.id; - } - - for (let i = 0; i < attributes.length; i++) { - const attributeData = this.get(attributes[i]); - - key += ':' + attributeData.id; - } - - return key; - } - - _createVao(index, attributes) { - const { gl } = this; - - const vaoGPU = gl.createVertexArray(); - let key = ''; - - let staticVao = true; - - gl.bindVertexArray(vaoGPU); - - if (index !== null) { - const indexData = this.get(index); - - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexData.bufferGPU); - - key += ':' + indexData.id; - } - - for (let i = 0; i < attributes.length; i++) { - const attribute = attributes[i]; - const attributeData = this.get(attribute); - - key += ':' + attributeData.id; - - gl.bindBuffer(gl.ARRAY_BUFFER, attributeData.bufferGPU); - gl.enableVertexAttribArray(i); - - if (attribute.isStorageBufferAttribute || attribute.isStorageInstancedBufferAttribute) staticVao = false; - - let stride, offset; - - if (attribute.isInterleavedBufferAttribute === true) { - stride = attribute.data.stride * attributeData.bytesPerElement; - offset = attribute.offset * attributeData.bytesPerElement; - } else { - stride = 0; - offset = 0; - } - - if (attributeData.isInteger) { - gl.vertexAttribIPointer(i, attribute.itemSize, attributeData.type, stride, offset); - } else { - gl.vertexAttribPointer(i, attribute.itemSize, attributeData.type, attribute.normalized, stride, offset); - } - - if (attribute.isInstancedBufferAttribute && !attribute.isInterleavedBufferAttribute) { - gl.vertexAttribDivisor(i, attribute.meshPerAttribute); - } else if (attribute.isInterleavedBufferAttribute && attribute.data.isInstancedInterleavedBuffer) { - gl.vertexAttribDivisor(i, attribute.data.meshPerAttribute); - } - } - - gl.bindBuffer(gl.ARRAY_BUFFER, null); - - this.vaoCache[key] = vaoGPU; - - return { vaoGPU, staticVao }; - } - - _getTransformFeedback(transformBuffers) { - let key = ''; - - for (let i = 0; i < transformBuffers.length; i++) { - key += ':' + transformBuffers[i].id; - } - - let transformFeedbackGPU = this.transformFeedbackCache[key]; - - if (transformFeedbackGPU !== undefined) { - return transformFeedbackGPU; - } - - const gl = this.gl; - - transformFeedbackGPU = gl.createTransformFeedback(); - - gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, transformFeedbackGPU); - - for (let i = 0; i < transformBuffers.length; i++) { - const attributeData = transformBuffers[i]; - - gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, i, attributeData.transformBuffer); - } - - gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, null); - - this.transformFeedbackCache[key] = transformFeedbackGPU; - - return transformFeedbackGPU; - } - - _setupBindings(bindings, programGPU) { - const gl = this.gl; - - for (const bindGroup of bindings) { - for (const binding of bindGroup.bindings) { - const bindingData = this.get(binding); - const index = bindingData.index; - - if (binding.isUniformsGroup || binding.isUniformBuffer) { - const location = gl.getUniformBlockIndex(programGPU, binding.name); - gl.uniformBlockBinding(programGPU, location, index); - } else if (binding.isSampledTexture) { - const location = gl.getUniformLocation(programGPU, binding.name); - gl.uniform1i(location, index); - } - } - } - } - - _bindUniforms(bindings) { - const { gl, state } = this; - - for (const bindGroup of bindings) { - for (const binding of bindGroup.bindings) { - const bindingData = this.get(binding); - const index = bindingData.index; - - if (binding.isUniformsGroup || binding.isUniformBuffer) { - gl.bindBufferBase(gl.UNIFORM_BUFFER, index, bindingData.bufferGPU); - } else if (binding.isSampledTexture) { - state.bindTexture(bindingData.glTextureType, bindingData.textureGPU, gl.TEXTURE0 + index); - } - } - } - } -} - -export default WebGLBackend; diff --git a/src-testing/src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.ts b/src-testing/src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.ts deleted file mode 100644 index ec5211b5c..000000000 --- a/src-testing/src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.ts +++ /dev/null @@ -1,794 +0,0 @@ -import { MathNode, GLSLNodeParser, NodeBuilder, TextureNode, vectorComponents } from '../../../nodes/Nodes.js'; - -import NodeUniformBuffer from '../../common/nodes/NodeUniformBuffer.js'; -import NodeUniformsGroup from '../../common/nodes/NodeUniformsGroup.js'; - -import { - NodeSampledTexture, - NodeSampledCubeTexture, - NodeSampledTexture3D, -} from '../../common/nodes/NodeSampledTexture.js'; - -import { - ByteType, - ShortType, - RGBAIntegerFormat, - RGBIntegerFormat, - RedIntegerFormat, - RGIntegerFormat, - UnsignedByteType, - UnsignedIntType, - UnsignedShortType, - RedFormat, - RGFormat, - IntType, - RGBFormat, - RGBAFormat, - FloatType, -} from '../../../constants.js'; -import { DataTexture } from '../../../textures/DataTexture.js'; - -const glslMethods = { - [MathNode.ATAN2]: 'atan', - textureDimensions: 'textureSize', - equals: 'equal', -}; - -const precisionLib = { - low: 'lowp', - medium: 'mediump', - high: 'highp', -}; - -const supports = { - swizzleAssign: true, - storageBuffer: false, -}; - -const defaultPrecisions = ` -precision highp float; -precision highp int; -precision highp sampler2D; -precision highp sampler3D; -precision highp samplerCube; -precision highp sampler2DArray; - -precision highp usampler2D; -precision highp usampler3D; -precision highp usamplerCube; -precision highp usampler2DArray; - -precision highp isampler2D; -precision highp isampler3D; -precision highp isamplerCube; -precision highp isampler2DArray; - -precision lowp sampler2DShadow; -`; - -class GLSLNodeBuilder extends NodeBuilder { - constructor(object, renderer) { - super(object, renderer, new GLSLNodeParser()); - - this.uniformGroups = {}; - this.transforms = []; - this.extensions = {}; - - this.instanceBindGroups = false; - - this.useComparisonMethod = true; - } - - getMethod(method) { - return glslMethods[method] || method; - } - - getOutputStructName() { - return ''; - } - - buildFunctionCode(shaderNode) { - const layout = shaderNode.layout; - const flowData = this.flowShaderNode(shaderNode); - - const parameters = []; - - for (const input of layout.inputs) { - parameters.push(this.getType(input.type) + ' ' + input.name); - } - - // - - const code = `${this.getType(layout.type)} ${layout.name}( ${parameters.join(', ')} ) { - - ${flowData.vars} - -${flowData.code} - return ${flowData.result}; - -}`; - - // - - return code; - } - - setupPBO(storageBufferNode) { - const attribute = storageBufferNode.value; - - if (attribute.pbo === undefined) { - const originalArray = attribute.array; - const numElements = attribute.count * attribute.itemSize; - - const { itemSize } = attribute; - - const isInteger = attribute.array.constructor.name.toLowerCase().includes('int'); - - let format = isInteger ? RedIntegerFormat : RedFormat; - - if (itemSize === 2) { - format = isInteger ? RGIntegerFormat : RGFormat; - } else if (itemSize === 3) { - format = isInteger ? RGBIntegerFormat : RGBFormat; - } else if (itemSize === 4) { - format = isInteger ? RGBAIntegerFormat : RGBAFormat; - } - - const typeMap = { - Float32Array: FloatType, - Uint8Array: UnsignedByteType, - Uint16Array: UnsignedShortType, - Uint32Array: UnsignedIntType, - Int8Array: ByteType, - Int16Array: ShortType, - Int32Array: IntType, - Uint8ClampedArray: UnsignedByteType, - }; - - const width = Math.pow(2, Math.ceil(Math.log2(Math.sqrt(numElements / itemSize)))); - let height = Math.ceil(numElements / itemSize / width); - if (width * height * itemSize < numElements) height++; // Ensure enough space - - const newSize = width * height * itemSize; - - const newArray = new originalArray.constructor(newSize); - - newArray.set(originalArray, 0); - - attribute.array = newArray; - - const pboTexture = new DataTexture( - attribute.array, - width, - height, - format, - typeMap[attribute.array.constructor.name] || FloatType, - ); - pboTexture.needsUpdate = true; - pboTexture.isPBOTexture = true; - - const pbo = new TextureNode(pboTexture, null, null); - pbo.setPrecision('high'); - - attribute.pboNode = pbo; - attribute.pbo = pbo.value; - - this.getUniformFromNode(attribute.pboNode, 'texture', this.shaderStage, this.context.label); - } - } - - getPropertyName(node, shaderStage = this.shaderStage) { - if (node.isNodeUniform && node.node.isTextureNode !== true && node.node.isBufferNode !== true) { - return shaderStage.charAt(0) + '_' + node.name; - } - - return super.getPropertyName(node, shaderStage); - } - - generatePBO(storageArrayElementNode) { - const { node, indexNode } = storageArrayElementNode; - const attribute = node.value; - - if (this.renderer.backend.has(attribute)) { - const attributeData = this.renderer.backend.get(attribute); - attributeData.pbo = attribute.pbo; - } - - const nodeUniform = this.getUniformFromNode(attribute.pboNode, 'texture', this.shaderStage, this.context.label); - const textureName = this.getPropertyName(nodeUniform); - - indexNode.increaseUsage(this); // force cache generate to be used as index in x,y - const indexSnippet = indexNode.build(this, 'uint'); - - const elementNodeData = this.getDataFromNode(storageArrayElementNode); - - let propertyName = elementNodeData.propertyName; - - if (propertyName === undefined) { - // property element - - const nodeVar = this.getVarFromNode(storageArrayElementNode); - - propertyName = this.getPropertyName(nodeVar); - - // property size - - const bufferNodeData = this.getDataFromNode(node); - - let propertySizeName = bufferNodeData.propertySizeName; - - if (propertySizeName === undefined) { - propertySizeName = propertyName + 'Size'; - - this.getVarFromNode(node, propertySizeName, 'uint'); - - this.addLineFlowCode(`${propertySizeName} = uint( textureSize( ${textureName}, 0 ).x )`); - - bufferNodeData.propertySizeName = propertySizeName; - } - - // - - const { itemSize } = attribute; - - const channel = '.' + vectorComponents.join('').slice(0, itemSize); - const uvSnippet = `ivec2(${indexSnippet} % ${propertySizeName}, ${indexSnippet} / ${propertySizeName})`; - - const snippet = this.generateTextureLoad(null, textureName, uvSnippet, null, '0'); - - // - - let prefix = 'vec4'; - - if (attribute.pbo.type === UnsignedIntType) { - prefix = 'uvec4'; - } else if (attribute.pbo.type === IntType) { - prefix = 'ivec4'; - } - - this.addLineFlowCode(`${propertyName} = ${prefix}(${snippet})${channel}`); - - elementNodeData.propertyName = propertyName; - } - - return propertyName; - } - - generateTextureLoad(texture, textureProperty, uvIndexSnippet, depthSnippet, levelSnippet = '0') { - if (depthSnippet) { - return `texelFetch( ${textureProperty}, ivec3( ${uvIndexSnippet}, ${depthSnippet} ), ${levelSnippet} )`; - } else { - return `texelFetch( ${textureProperty}, ${uvIndexSnippet}, ${levelSnippet} )`; - } - } - - generateTexture(texture, textureProperty, uvSnippet, depthSnippet) { - if (texture.isDepthTexture) { - return `texture( ${textureProperty}, ${uvSnippet} ).x`; - } else { - if (depthSnippet) uvSnippet = `vec3( ${uvSnippet}, ${depthSnippet} )`; - - return `texture( ${textureProperty}, ${uvSnippet} )`; - } - } - - generateTextureLevel(texture, textureProperty, uvSnippet, levelSnippet) { - return `textureLod( ${textureProperty}, ${uvSnippet}, ${levelSnippet} )`; - } - - generateTextureBias(texture, textureProperty, uvSnippet, biasSnippet) { - return `texture( ${textureProperty}, ${uvSnippet}, ${biasSnippet} )`; - } - - generateTextureGrad(texture, textureProperty, uvSnippet, gradSnippet) { - return `textureGrad( ${textureProperty}, ${uvSnippet}, ${gradSnippet[0]}, ${gradSnippet[1]} )`; - } - - generateTextureCompare( - texture, - textureProperty, - uvSnippet, - compareSnippet, - depthSnippet, - shaderStage = this.shaderStage, - ) { - if (shaderStage === 'fragment') { - return `texture( ${textureProperty}, vec3( ${uvSnippet}, ${compareSnippet} ) )`; - } else { - console.error( - `WebGPURenderer: THREE.DepthTexture.compareFunction() does not support ${shaderStage} shader.`, - ); - } - } - - getVars(shaderStage) { - const snippets = []; - - const vars = this.vars[shaderStage]; - - if (vars !== undefined) { - for (const variable of vars) { - snippets.push(`${this.getVar(variable.type, variable.name)};`); - } - } - - return snippets.join('\n\t'); - } - - getUniforms(shaderStage) { - const uniforms = this.uniforms[shaderStage]; - - const bindingSnippets = []; - const uniformGroups = {}; - - for (const uniform of uniforms) { - let snippet = null; - let group = false; - - if (uniform.type === 'texture') { - const texture = uniform.node.value; - - let typePrefix = ''; - - if (texture.isDataTexture === true) { - if (texture.type === UnsignedIntType) { - typePrefix = 'u'; - } else if (texture.type === IntType) { - typePrefix = 'i'; - } - } - - if (texture.compareFunction) { - snippet = `sampler2DShadow ${uniform.name};`; - } else if (texture.isDataArrayTexture === true) { - snippet = `${typePrefix}sampler2DArray ${uniform.name};`; - } else { - snippet = `${typePrefix}sampler2D ${uniform.name};`; - } - } else if (uniform.type === 'cubeTexture') { - snippet = `samplerCube ${uniform.name};`; - } else if (uniform.type === 'texture3D') { - snippet = `sampler3D ${uniform.name};`; - } else if (uniform.type === 'buffer') { - const bufferNode = uniform.node; - const bufferType = this.getType(bufferNode.bufferType); - const bufferCount = bufferNode.bufferCount; - - const bufferCountSnippet = bufferCount > 0 ? bufferCount : ''; - snippet = `${bufferNode.name} {\n\t${bufferType} ${uniform.name}[${bufferCountSnippet}];\n};\n`; - } else { - const vectorType = this.getVectorType(uniform.type); - - snippet = `${vectorType} ${this.getPropertyName(uniform, shaderStage)};`; - - group = true; - } - - const precision = uniform.node.precision; - - if (precision !== null) { - snippet = precisionLib[precision] + ' ' + snippet; - } - - if (group) { - snippet = '\t' + snippet; - - const groupName = uniform.groupNode.name; - const groupSnippets = uniformGroups[groupName] || (uniformGroups[groupName] = []); - - groupSnippets.push(snippet); - } else { - snippet = 'uniform ' + snippet; - - bindingSnippets.push(snippet); - } - } - - let output = ''; - - for (const name in uniformGroups) { - const groupSnippets = uniformGroups[name]; - - output += this._getGLSLUniformStruct(shaderStage + '_' + name, groupSnippets.join('\n')) + '\n'; - } - - output += bindingSnippets.join('\n'); - - return output; - } - - getTypeFromAttribute(attribute) { - let nodeType = super.getTypeFromAttribute(attribute); - - if (/^[iu]/.test(nodeType) && attribute.gpuType !== IntType) { - let dataAttribute = attribute; - - if (attribute.isInterleavedBufferAttribute) dataAttribute = attribute.data; - - const array = dataAttribute.array; - - if ((array instanceof Uint32Array || array instanceof Int32Array) === false) { - nodeType = nodeType.slice(1); - } - } - - return nodeType; - } - - getAttributes(shaderStage) { - let snippet = ''; - - if (shaderStage === 'vertex' || shaderStage === 'compute') { - const attributes = this.getAttributesArray(); - - let location = 0; - - for (const attribute of attributes) { - snippet += `layout( location = ${location++} ) in ${attribute.type} ${attribute.name};\n`; - } - } - - return snippet; - } - - getStructMembers(struct) { - const snippets = []; - const members = struct.getMemberTypes(); - - for (let i = 0; i < members.length; i++) { - const member = members[i]; - snippets.push(`layout( location = ${i} ) out ${member} m${i};`); - } - - return snippets.join('\n'); - } - - getStructs(shaderStage) { - const snippets = []; - const structs = this.structs[shaderStage]; - - if (structs.length === 0) { - return 'layout( location = 0 ) out vec4 fragColor;\n'; - } - - for (let index = 0, length = structs.length; index < length; index++) { - const struct = structs[index]; - - let snippet = '\n'; - snippet += this.getStructMembers(struct); - snippet += '\n'; - - snippets.push(snippet); - } - - return snippets.join('\n\n'); - } - - getVaryings(shaderStage) { - let snippet = ''; - - const varyings = this.varyings; - - if (shaderStage === 'vertex' || shaderStage === 'compute') { - for (const varying of varyings) { - if (shaderStage === 'compute') varying.needsInterpolation = true; - const type = varying.type; - const flat = type.includes('int') || type.includes('uv') || type.includes('iv') ? 'flat ' : ''; - - snippet += `${flat}${varying.needsInterpolation ? 'out' : '/*out*/'} ${type} ${varying.name};\n`; - } - } else if (shaderStage === 'fragment') { - for (const varying of varyings) { - if (varying.needsInterpolation) { - const type = varying.type; - const flat = type.includes('int') || type.includes('uv') || type.includes('iv') ? 'flat ' : ''; - - snippet += `${flat}in ${type} ${varying.name};\n`; - } - } - } - - return snippet; - } - - getVertexIndex() { - return 'uint( gl_VertexID )'; - } - - getInstanceIndex() { - return 'uint( gl_InstanceID )'; - } - - getDrawIndex() { - const extensions = this.renderer.backend.extensions; - - if (extensions.has('WEBGL_multi_draw')) { - return 'uint( gl_DrawID )'; - } - - return null; - } - - getFrontFacing() { - return 'gl_FrontFacing'; - } - - getFragCoord() { - return 'gl_FragCoord'; - } - - getFragDepth() { - return 'gl_FragDepth'; - } - - enableExtension(name, behavior, shaderStage = this.shaderStage) { - const map = this.extensions[shaderStage] || (this.extensions[shaderStage] = new Map()); - - if (map.has(name) === false) { - map.set(name, { - name, - behavior, - }); - } - } - - getExtensions(shaderStage) { - const snippets = []; - - if (shaderStage === 'vertex') { - const ext = this.renderer.backend.extensions; - const isBatchedMesh = this.object.isBatchedMesh; - - if (isBatchedMesh && ext.has('WEBGL_multi_draw')) { - this.enableExtension('GL_ANGLE_multi_draw', 'require'); - } - } - - const extensions = this.extensions[shaderStage]; - - if (extensions !== undefined) { - for (const { name, behavior } of extensions.values()) { - snippets.push(`#extension ${name} : ${behavior}`); - } - } - - return snippets.join('\n'); - } - - isAvailable(name) { - let result = supports[name]; - - if (result === undefined) { - if (name === 'float32Filterable') { - const extensions = this.renderer.backend.extensions; - - if (extensions.has('OES_texture_float_linear')) { - extensions.get('OES_texture_float_linear'); - result = true; - } else { - result = false; - } - } - - supports[name] = result; - } - - return result; - } - - isFlipY() { - return true; - } - - registerTransform(varyingName, attributeNode) { - this.transforms.push({ varyingName, attributeNode }); - } - - getTransforms(/* shaderStage */) { - const transforms = this.transforms; - - let snippet = ''; - - for (let i = 0; i < transforms.length; i++) { - const transform = transforms[i]; - - const attributeName = this.getPropertyName(transform.attributeNode); - - snippet += `${transform.varyingName} = ${attributeName};\n\t`; - } - - return snippet; - } - - _getGLSLUniformStruct(name, vars) { - return ` -layout( std140 ) uniform ${name} { -${vars} -};`; - } - - _getGLSLVertexCode(shaderData) { - return `#version 300 es - -// extensions -${shaderData.extensions} - -// precision -${defaultPrecisions} - -// uniforms -${shaderData.uniforms} - -// varyings -${shaderData.varyings} - -// attributes -${shaderData.attributes} - -// codes -${shaderData.codes} - -void main() { - - // vars - ${shaderData.vars} - - // transforms - ${shaderData.transforms} - - // flow - ${shaderData.flow} - - gl_PointSize = 1.0; - -} -`; - } - - _getGLSLFragmentCode(shaderData) { - return `#version 300 es - -${this.getSignature()} - -// precision -${defaultPrecisions} - -// uniforms -${shaderData.uniforms} - -// varyings -${shaderData.varyings} - -// codes -${shaderData.codes} - -${shaderData.structs} - -void main() { - - // vars - ${shaderData.vars} - - // flow - ${shaderData.flow} - -} -`; - } - - buildCode() { - const shadersData = this.material !== null ? { fragment: {}, vertex: {} } : { compute: {} }; - - for (const shaderStage in shadersData) { - let flow = '// code\n\n'; - flow += this.flowCode[shaderStage]; - - const flowNodes = this.flowNodes[shaderStage]; - const mainNode = flowNodes[flowNodes.length - 1]; - - for (const node of flowNodes) { - const flowSlotData = this.getFlowData(node /*, shaderStage*/); - const slotName = node.name; - - if (slotName) { - if (flow.length > 0) flow += '\n'; - - flow += `\t// flow -> ${slotName}\n\t`; - } - - flow += `${flowSlotData.code}\n\t`; - - if (node === mainNode && shaderStage !== 'compute') { - flow += '// result\n\t'; - - if (shaderStage === 'vertex') { - flow += 'gl_Position = '; - flow += `${flowSlotData.result};`; - } else if (shaderStage === 'fragment') { - if (!node.outputNode.isOutputStructNode) { - flow += 'fragColor = '; - flow += `${flowSlotData.result};`; - } - } - } - } - - const stageData = shadersData[shaderStage]; - - stageData.extensions = this.getExtensions(shaderStage); - stageData.uniforms = this.getUniforms(shaderStage); - stageData.attributes = this.getAttributes(shaderStage); - stageData.varyings = this.getVaryings(shaderStage); - stageData.vars = this.getVars(shaderStage); - stageData.structs = this.getStructs(shaderStage); - stageData.codes = this.getCodes(shaderStage); - stageData.transforms = this.getTransforms(shaderStage); - stageData.flow = flow; - } - - if (this.material !== null) { - this.vertexShader = this._getGLSLVertexCode(shadersData.vertex); - this.fragmentShader = this._getGLSLFragmentCode(shadersData.fragment); - } else { - this.computeShader = this._getGLSLVertexCode(shadersData.compute); - } - } - - getUniformFromNode(node, type, shaderStage, name = null) { - const uniformNode = super.getUniformFromNode(node, type, shaderStage, name); - const nodeData = this.getDataFromNode(node, shaderStage, this.globalCache); - - let uniformGPU = nodeData.uniformGPU; - - if (uniformGPU === undefined) { - const group = node.groupNode; - const groupName = group.name; - - const bindings = this.getBindGroupArray(groupName, shaderStage); - - if (type === 'texture') { - uniformGPU = new NodeSampledTexture(uniformNode.name, uniformNode.node, group); - bindings.push(uniformGPU); - } else if (type === 'cubeTexture') { - uniformGPU = new NodeSampledCubeTexture(uniformNode.name, uniformNode.node, group); - bindings.push(uniformGPU); - } else if (type === 'texture3D') { - uniformGPU = new NodeSampledTexture3D(uniformNode.name, uniformNode.node, group); - bindings.push(uniformGPU); - } else if (type === 'buffer') { - node.name = `NodeBuffer_${node.id}`; - uniformNode.name = `buffer${node.id}`; - - const buffer = new NodeUniformBuffer(node, group); - buffer.name = node.name; - - bindings.push(buffer); - - uniformGPU = buffer; - } else { - const uniformsStage = this.uniformGroups[shaderStage] || (this.uniformGroups[shaderStage] = {}); - - let uniformsGroup = uniformsStage[groupName]; - - if (uniformsGroup === undefined) { - uniformsGroup = new NodeUniformsGroup(shaderStage + '_' + groupName, group); - //uniformsGroup.setVisibility( gpuShaderStageLib[ shaderStage ] ); - - uniformsStage[groupName] = uniformsGroup; - - bindings.push(uniformsGroup); - } - - uniformGPU = this.getNodeUniform(uniformNode, type); - - uniformsGroup.addUniform(uniformGPU); - } - - nodeData.uniformGPU = uniformGPU; - } - - return uniformNode; - } -} - -export default GLSLNodeBuilder; diff --git a/src-testing/src/renderers/webgl/WebGLAttributes.d.ts b/src-testing/src/renderers/webgl/WebGLAttributes.d.ts deleted file mode 100644 index 8f4a9757b..000000000 --- a/src-testing/src/renderers/webgl/WebGLAttributes.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { BufferAttribute } from "../../core/BufferAttribute.js"; -import { GLBufferAttribute } from "../../core/GLBufferAttribute.js"; -import { InterleavedBufferAttribute } from "../../core/InterleavedBufferAttribute.js"; - -export class WebGLAttributes { - constructor(gl: WebGLRenderingContext | WebGL2RenderingContext); - - get(attribute: BufferAttribute | InterleavedBufferAttribute | GLBufferAttribute): - | { - buffer: WebGLBuffer; - type: number; - bytesPerElement: number; - version: number; - size: number; - } - | undefined; - - remove(attribute: BufferAttribute | InterleavedBufferAttribute | GLBufferAttribute): void; - - update(attribute: BufferAttribute | InterleavedBufferAttribute | GLBufferAttribute, bufferType: number): void; -} diff --git a/src-testing/src/renderers/webgl/WebGLBindingStates.d.ts b/src-testing/src/renderers/webgl/WebGLBindingStates.d.ts deleted file mode 100644 index 0b96de770..000000000 --- a/src-testing/src/renderers/webgl/WebGLBindingStates.d.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { BufferAttribute } from "../../core/BufferAttribute.js"; -import { BufferGeometry } from "../../core/BufferGeometry.js"; -import { Object3D } from "../../core/Object3D.js"; -import { Material } from "../../materials/Material.js"; -import { WebGLAttributes } from "./WebGLAttributes.js"; -import { WebGLProgram } from "./WebGLProgram.js"; - -export class WebGLBindingStates { - constructor(gl: WebGLRenderingContext, attributes: WebGLAttributes); - - setup( - object: Object3D, - material: Material, - program: WebGLProgram, - geometry: BufferGeometry, - index: BufferAttribute, - ): void; - reset(): void; - resetDefaultState(): void; - dispose(): void; - releaseStatesOfGeometry(): void; - releaseStatesOfProgram(): void; - initAttributes(): void; - enableAttribute(attribute: number): void; - disableUnusedAttributes(): void; -} diff --git a/src-testing/src/renderers/webgl/WebGLBufferRenderer.d.ts b/src-testing/src/renderers/webgl/WebGLBufferRenderer.d.ts deleted file mode 100644 index c75f74745..000000000 --- a/src-testing/src/renderers/webgl/WebGLBufferRenderer.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { WebGLExtensions } from "./WebGLExtensions.js"; -import { WebGLInfo } from "./WebGLInfo.js"; - -export class WebGLBufferRenderer { - constructor( - gl: WebGLRenderingContext, - extensions: WebGLExtensions, - info: WebGLInfo, - ); - - setMode: (value: any) => void; - render: (start: any, count: number) => void; - renderInstances: (start: any, count: number, primcount: number) => void; - renderMultiDraw: (starts: Int32Array, counts: Int32Array, drawCount: number) => void; - renderMultiDrawInstances: ( - starts: Int32Array, - counts: Int32Array, - drawCount: number, - primcount: Int32Array, - ) => void; -} diff --git a/src-testing/src/renderers/webgl/WebGLCapabilities.d.ts b/src-testing/src/renderers/webgl/WebGLCapabilities.d.ts deleted file mode 100644 index c0f3aae41..000000000 --- a/src-testing/src/renderers/webgl/WebGLCapabilities.d.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { PixelFormat, TextureDataType } from "../../constants.js"; - -export interface WebGLCapabilitiesParameters { - precision?: string | undefined; - logarithmicDepthBuffer?: boolean | undefined; -} - -export class WebGLCapabilities { - constructor(gl: WebGLRenderingContext, extensions: any, parameters: WebGLCapabilitiesParameters); - - readonly isWebGL2: boolean; - - getMaxAnisotropy: () => number; - getMaxPrecision: (precision: string) => string; - - textureFormatReadable: (textureFormat: PixelFormat) => boolean; - textureTypeReadable: (textureType: TextureDataType) => boolean; - - precision: string; - logarithmicDepthBuffer: boolean; - - maxTextures: number; - maxVertexTextures: number; - maxTextureSize: number; - maxCubemapSize: number; - - maxAttributes: number; - maxVertexUniforms: number; - maxVaryings: number; - maxFragmentUniforms: number; - - vertexTextures: boolean; - - maxSamples: number; -} diff --git a/src-testing/src/renderers/webgl/WebGLClipping.d.ts b/src-testing/src/renderers/webgl/WebGLClipping.d.ts deleted file mode 100644 index 167a16b35..000000000 --- a/src-testing/src/renderers/webgl/WebGLClipping.d.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Camera } from "../../cameras/Camera.js"; -import { Material } from "../../materials/Material.js"; -import { Plane } from "../../math/Plane.js"; -import { WebGLProperties } from "./WebGLProperties.js"; - -export class WebGLClipping { - constructor(properties: WebGLProperties); - - uniform: { value: any; needsUpdate: boolean }; - - /** - * @default 0 - */ - numPlanes: number; - - /** - * @default 0 - */ - numIntersection: number; - - init(planes: any[], enableLocalClipping: boolean): boolean; - beginShadows(): void; - endShadows(): void; - setGlobalState(planes: Plane[], camera: Camera): void; - setState(material: Material, camera: Camera, useCache: boolean): void; -} diff --git a/src-testing/src/renderers/webgl/WebGLCubeMaps.d.ts b/src-testing/src/renderers/webgl/WebGLCubeMaps.d.ts deleted file mode 100644 index 32cda3f61..000000000 --- a/src-testing/src/renderers/webgl/WebGLCubeMaps.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { WebGLRenderer } from "../WebGLRenderer.js"; - -export class WebGLCubeMaps { - constructor(renderer: WebGLRenderer); - - get(texture: any): any; - dispose(): void; -} diff --git a/src-testing/src/renderers/webgl/WebGLCubeUVMaps.d.ts b/src-testing/src/renderers/webgl/WebGLCubeUVMaps.d.ts deleted file mode 100644 index e94246325..000000000 --- a/src-testing/src/renderers/webgl/WebGLCubeUVMaps.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Texture } from "../../textures/Texture.js"; -import { WebGLRenderer } from "../WebGLRenderer.js"; - -export class WebGLCubeUVMaps { - constructor(renderer: WebGLRenderer); - - get(texture: T): T extends Texture ? Texture : T; - dispose(): void; -} diff --git a/src-testing/src/renderers/webgl/WebGLExtensions.d.ts b/src-testing/src/renderers/webgl/WebGLExtensions.d.ts deleted file mode 100644 index 0f0c0bf4b..000000000 --- a/src-testing/src/renderers/webgl/WebGLExtensions.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -export class WebGLExtensions { - constructor(gl: WebGLRenderingContext); - - has(name: string): boolean; - init(): void; - get(name: string): any; -} diff --git a/src-testing/src/renderers/webgl/WebGLGeometries.d.ts b/src-testing/src/renderers/webgl/WebGLGeometries.d.ts deleted file mode 100644 index 422249f37..000000000 --- a/src-testing/src/renderers/webgl/WebGLGeometries.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { BufferAttribute } from "../../core/BufferAttribute.js"; -import { BufferGeometry } from "../../core/BufferGeometry.js"; -import { Object3D } from "../../core/Object3D.js"; -import { WebGLAttributes } from "./WebGLAttributes.js"; -import { WebGLInfo } from "./WebGLInfo.js"; - -export class WebGLGeometries { - constructor(gl: WebGLRenderingContext, attributes: WebGLAttributes, info: WebGLInfo); - - get(object: Object3D, geometry: BufferGeometry): BufferGeometry; - update(geometry: BufferGeometry): void; - getWireframeAttribute(geometry: BufferGeometry): BufferAttribute; -} diff --git a/src-testing/src/renderers/webgl/WebGLIndexedBufferRenderer.d.ts b/src-testing/src/renderers/webgl/WebGLIndexedBufferRenderer.d.ts deleted file mode 100644 index 8e016e847..000000000 --- a/src-testing/src/renderers/webgl/WebGLIndexedBufferRenderer.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -export class WebGLIndexedBufferRenderer { - constructor(gl: WebGLRenderingContext, extensions: any, info: any); - - setMode: (value: any) => void; - setIndex: (index: any) => void; - render: (start: any, count: number) => void; - renderInstances: (start: any, count: number, primcount: number) => void; - renderMultiDraw: (starts: Int32Array, counts: Int32Array, drawCount: number) => void; - renderMultiDrawInstances: ( - starts: Int32Array, - counts: Int32Array, - drawCount: number, - primcount: Int32Array, - ) => void; -} diff --git a/src-testing/src/renderers/webgl/WebGLInfo.d.ts b/src-testing/src/renderers/webgl/WebGLInfo.d.ts deleted file mode 100644 index 952ff6f9f..000000000 --- a/src-testing/src/renderers/webgl/WebGLInfo.d.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { WebGLProgram } from "./WebGLProgram.js"; - -/** - * An object with a series of statistical information about the graphics board memory and the rendering process. - */ -export class WebGLInfo { - constructor(gl: WebGLRenderingContext); - - /** - * @default true - */ - autoReset: boolean; - - /** - * @default { geometries: 0, textures: 0 } - */ - memory: { - geometries: number; - textures: number; - }; - - /** - * @default null - */ - programs: WebGLProgram[] | null; - - /** - * @default { frame: 0, calls: 0, triangles: 0, points: 0, lines: 0 } - */ - render: { - calls: number; - frame: number; - lines: number; - points: number; - triangles: number; - }; - update(count: number, mode: number, instanceCount: number): void; - reset(): void; -} diff --git a/src-testing/src/renderers/webgl/WebGLLights.d.ts b/src-testing/src/renderers/webgl/WebGLLights.d.ts deleted file mode 100644 index 4c01422e8..000000000 --- a/src-testing/src/renderers/webgl/WebGLLights.d.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { WebGLExtensions } from "./WebGLExtensions.js"; - -export interface WebGLLightsState { - version: number; - - hash: { - directionalLength: number; - pointLength: number; - spotLength: number; - rectAreaLength: number; - hemiLength: number; - - numDirectionalShadows: number; - numPointShadows: number; - numSpotShadows: number; - numSpotMaps: number; - - numLightProbes: number; - }; - - ambient: number[]; - probe: any[]; - directional: any[]; - directionalShadow: any[]; - directionalShadowMap: any[]; - directionalShadowMatrix: any[]; - spot: any[]; - spotShadow: any[]; - spotShadowMap: any[]; - spotShadowMatrix: any[]; - rectArea: any[]; - point: any[]; - pointShadow: any[]; - pointShadowMap: any[]; - pointShadowMatrix: any[]; - hemi: any[]; - numSpotLightShadowsWithMaps: number; - numLightProbes: number; -} - -export class WebGLLights { - constructor(extensions: WebGLExtensions); - - state: WebGLLightsState; - - get(light: any): any; - setup(lights: any): void; - setupView(lights: any, camera: any): void; -} diff --git a/src-testing/src/renderers/webgl/WebGLObjects.d.ts b/src-testing/src/renderers/webgl/WebGLObjects.d.ts deleted file mode 100644 index aeb0356f7..000000000 --- a/src-testing/src/renderers/webgl/WebGLObjects.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -export class WebGLObjects { - constructor(gl: WebGLRenderingContext, geometries: any, attributes: any, info: any); - - update(object: any): any; - dispose(): void; -} diff --git a/src-testing/src/renderers/webgl/WebGLProgram.d.ts b/src-testing/src/renderers/webgl/WebGLProgram.d.ts deleted file mode 100644 index b3e5d6fd8..000000000 --- a/src-testing/src/renderers/webgl/WebGLProgram.d.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { WebGLRenderer } from "../WebGLRenderer.js"; -import { WebGLUniforms } from "./WebGLUniforms.js"; - -export class WebGLProgram { - constructor(renderer: WebGLRenderer, cacheKey: string, parameters: object); - - name: string; - id: number; - cacheKey: string; // unique identifier for this program, used for looking up compiled programs from cache. - - /** - * @default 1 - */ - usedTimes: number; - program: any; - vertexShader: WebGLShader; - fragmentShader: WebGLShader; - /** - * @deprecated Use {@link WebGLProgram#getUniforms getUniforms()} instead. - */ - uniforms: any; - /** - * @deprecated Use {@link WebGLProgram#getAttributes getAttributes()} instead. - */ - attributes: any; - - getUniforms(): WebGLUniforms; - getAttributes(): any; - destroy(): void; -} diff --git a/src-testing/src/renderers/webgl/WebGLPrograms.d.ts b/src-testing/src/renderers/webgl/WebGLPrograms.d.ts deleted file mode 100644 index cee01169b..000000000 --- a/src-testing/src/renderers/webgl/WebGLPrograms.d.ts +++ /dev/null @@ -1,239 +0,0 @@ -import { - ColorSpace, - Combine, - DepthPackingStrategies, - GLSLVersion, - Mapping, - ShadowMapType, - ToneMapping, -} from "../../constants.js"; -import { Object3D } from "../../core/Object3D.js"; -import { Light } from "../../lights/Light.js"; -import { Material } from "../../materials/Material.js"; -import { Scene } from "../../scenes/Scene.js"; -import { IUniform } from "../shaders/UniformsLib.js"; -import { WebGLRenderer } from "../WebGLRenderer.js"; -import { WebGLBindingStates } from "./WebGLBindingStates.js"; -import { WebGLCapabilities } from "./WebGLCapabilities.js"; -import { WebGLClipping } from "./WebGLClipping.js"; -import { WebGLCubeMaps } from "./WebGLCubeMaps.js"; -import { WebGLExtensions } from "./WebGLExtensions.js"; -import { WebGLLightsState } from "./WebGLLights.js"; -import { WebGLProgram } from "./WebGLProgram.js"; - -export interface WebGLProgramParameters { - shaderID: string; - shaderType: string; - shaderName: string; - - vertexShader: string; - fragmentShader: string; - defines: { [define: string]: string | number | boolean } | undefined; - - customVertexShaderID: string | undefined; - customFragmentShaderID: string | undefined; - - isRawShaderMaterial: boolean; - glslVersion: GLSLVersion | null | undefined; - - precision: "lowp" | "mediump" | "highp"; - - batching: boolean; - batchingColor: boolean; - instancing: boolean; - instancingColor: boolean; - instancingMorph: boolean; - - supportsVertexTextures: boolean; - outputColorSpace: ColorSpace; - alphaToCoverage: boolean; - - map: boolean; - matcap: boolean; - envMap: boolean; - envMapMode: Mapping | false; - envMapCubeUVHeight: number | null; - aoMap: boolean; - lightMap: boolean; - bumpMap: boolean; - normalMap: boolean; - displacementMap: boolean; - emissiveMap: boolean; - - normalMapObjectSpace: boolean; - normalMapTangentSpace: boolean; - - metalnessMap: boolean; - roughnessMap: boolean; - - anisotropy: boolean; - anisotropyMap: boolean; - - clearcoat: boolean; - clearcoatMap: boolean; - clearcoatNormalMap: boolean; - clearcoatRoughnessMap: boolean; - - dispersion: boolean; - - iridescence: boolean; - iridescenceMap: boolean; - iridescenceThicknessMap: boolean; - - sheen: boolean; - sheenColorMap: boolean; - sheenRoughnessMap: boolean; - - specularMap: boolean; - specularColorMap: boolean; - specularIntensityMap: boolean; - - transmission: boolean; - transmissionMap: boolean; - thicknessMap: boolean; - - gradientMap: boolean; - - opaque: boolean; - - alphaMap: boolean; - alphaTest: boolean; - alphaHash: boolean; - - combine: Combine | undefined; - - // - - mapUv: string | false; - aoMapUv: string | false; - lightMapUv: string | false; - bumpMapUv: string | false; - normalMapUv: string | false; - displacementMapUv: string | false; - emissiveMapUv: string | false; - - metalnessMapUv: string | false; - roughnessMapUv: string | false; - - anisotropyMapUv: string | false; - - clearcoatMapUv: string | false; - clearcoatNormalMapUv: string | false; - clearcoatRoughnessMapUv: string | false; - - iridescenceMapUv: string | false; - iridescenceThicknessMapUv: string | false; - - sheenColorMapUv: string | false; - sheenRoughnessMapUv: string | false; - - specularMapUv: string | false; - specularColorMapUv: string | false; - specularIntensityMapUv: string | false; - - transmissionMapUv: string | false; - thicknessMapUv: string | false; - - alphaMapUv: string | false; - - // - - vertexTangents: boolean; - vertexColors: boolean; - vertexAlphas: boolean; - vertexUv1s: boolean; - vertexUv2s: boolean; - vertexUv3s: boolean; - - pointsUvs: boolean; - - fog: boolean; - useFog: boolean; - fogExp2: boolean; - - flatShading: boolean; - - sizeAttenuation: boolean; - logarithmicDepthBuffer: boolean; - - skinning: boolean; - - morphTargets: boolean; - morphNormals: boolean; - morphColors: boolean; - morphTargetsCount: number; - morphTextureStride: number; - - numDirLights: number; - numPointLights: number; - numSpotLights: number; - numSpotLightMaps: number; - numRectAreaLights: number; - numHemiLights: number; - - numDirLightShadows: number; - numPointLightShadows: number; - numSpotLightShadows: number; - numSpotLightShadowsWithMaps: number; - - numLightProbes: number; - - numClippingPlanes: number; - numClipIntersection: number; - - dithering: boolean; - - shadowMapEnabled: boolean; - shadowMapType: ShadowMapType; - - toneMapping: ToneMapping; - - decodeVideoTexture: boolean; - - premultipliedAlpha: boolean; - - doubleSided: boolean; - flipSided: boolean; - - useDepthPacking: boolean; - depthPacking: DepthPackingStrategies | 0; - - index0AttributeName: string | undefined; - - extensionClipCullDistance: boolean; - extensionMultiDraw: boolean; - - rendererExtensionParallelShaderCompile: boolean; - - customProgramCacheKey: string; -} - -export interface WebGLProgramParametersWithUniforms extends WebGLProgramParameters { - uniforms: { [uniform: string]: IUniform }; -} - -export class WebGLPrograms { - constructor( - renderer: WebGLRenderer, - cubemaps: WebGLCubeMaps, - extensions: WebGLExtensions, - capabilities: WebGLCapabilities, - bindingStates: WebGLBindingStates, - clipping: WebGLClipping, - ); - - programs: WebGLProgram[]; - - getParameters( - material: Material, - lights: WebGLLightsState, - shadows: Light[], - scene: Scene, - object: Object3D, - ): WebGLProgramParameters; - - getProgramCacheKey(parameters: WebGLProgramParameters): string; - getUniforms(material: Material): { [uniform: string]: IUniform }; - acquireProgram(parameters: WebGLProgramParametersWithUniforms, cacheKey: string): WebGLProgram; - releaseProgram(program: WebGLProgram): void; -} diff --git a/src-testing/src/renderers/webgl/WebGLProperties.d.ts b/src-testing/src/renderers/webgl/WebGLProperties.d.ts deleted file mode 100644 index adcf01ec9..000000000 --- a/src-testing/src/renderers/webgl/WebGLProperties.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -export class WebGLProperties { - constructor(); - - has: (object: unknown) => boolean; - get: (object: unknown) => unknown; - remove: (object: unknown) => void; - update: (object: unknown, key: unknown, value: unknown) => unknown; - dispose: () => void; -} diff --git a/src-testing/src/renderers/webgl/WebGLRenderLists.d.ts b/src-testing/src/renderers/webgl/WebGLRenderLists.d.ts deleted file mode 100644 index 0f16a4287..000000000 --- a/src-testing/src/renderers/webgl/WebGLRenderLists.d.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { Camera } from "../../cameras/Camera.js"; -import { BufferGeometry } from "../../core/BufferGeometry.js"; -import { Object3D } from "../../core/Object3D.js"; -import { Material } from "../../materials/Material.js"; -import { Group } from "../../objects/Group.js"; -import { Scene } from "../../scenes/Scene.js"; -import { WebGLProgram } from "./WebGLProgram.js"; -import { WebGLProperties } from "./WebGLProperties.js"; - -export interface RenderItem { - id: number; - object: Object3D; - geometry: BufferGeometry | null; - material: Material; - program: WebGLProgram; - groupOrder: number; - renderOrder: number; - z: number; - group: Group | null; -} - -export class WebGLRenderList { - constructor(properties: WebGLProperties); - - /** - * @default [] - */ - opaque: RenderItem[]; - - /** - * @default [] - */ - transparent: RenderItem[]; - - /** - * @default [] - */ - transmissive: RenderItem[]; - - init(): void; - push( - object: Object3D, - geometry: BufferGeometry | null, - material: Material, - groupOrder: number, - z: number, - group: Group | null, - ): void; - unshift( - object: Object3D, - geometry: BufferGeometry | null, - material: Material, - groupOrder: number, - z: number, - group: Group | null, - ): void; - sort(opaqueSort: (a: any, b: any) => number, transparentSort: (a: any, b: any) => number): void; - finish(): void; -} - -export class WebGLRenderLists { - constructor(properties: WebGLProperties); - - dispose(): void; - get(scene: Scene, renderCallDepth: number): WebGLRenderList; -} diff --git a/src-testing/src/renderers/webgl/WebGLShader.d.ts b/src-testing/src/renderers/webgl/WebGLShader.d.ts deleted file mode 100644 index 5704fb88e..000000000 --- a/src-testing/src/renderers/webgl/WebGLShader.d.ts +++ /dev/null @@ -1 +0,0 @@ -export function WebGLShader(gl: WebGLRenderingContext, type: string, string: string): WebGLShader; diff --git a/src-testing/src/renderers/webgl/WebGLShadowMap.d.ts b/src-testing/src/renderers/webgl/WebGLShadowMap.d.ts deleted file mode 100644 index 8368fdee7..000000000 --- a/src-testing/src/renderers/webgl/WebGLShadowMap.d.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { Camera } from "../../cameras/Camera.js"; -import { ShadowMapType } from "../../constants.js"; -import { Light } from "../../lights/Light.js"; -import { Scene } from "../../scenes/Scene.js"; -import { WebGLRenderer } from "../WebGLRenderer.js"; -import { WebGLCapabilities } from "./WebGLCapabilities.js"; -import { WebGLObjects } from "./WebGLObjects.js"; - -export class WebGLShadowMap { - constructor(_renderer: WebGLRenderer, _objects: WebGLObjects, _capabilities: WebGLCapabilities); - - /** - * @default false - */ - enabled: boolean; - - /** - * @default true - */ - autoUpdate: boolean; - - /** - * @default false - */ - needsUpdate: boolean; - - /** - * @default THREE.PCFShadowMap - */ - type: ShadowMapType; - - render(shadowsArray: Light[], scene: Scene, camera: Camera): void; - - /** - * @deprecated Use {@link Material#shadowSide} instead. - */ - cullFace: any; -} diff --git a/src-testing/src/renderers/webgl/WebGLState.d.ts b/src-testing/src/renderers/webgl/WebGLState.d.ts deleted file mode 100644 index 13d4850e9..000000000 --- a/src-testing/src/renderers/webgl/WebGLState.d.ts +++ /dev/null @@ -1,116 +0,0 @@ -import { - Blending, - BlendingDstFactor, - BlendingEquation, - BlendingSrcFactor, - CullFace, - DepthModes, -} from "../../constants.js"; -import { Material } from "../../materials/Material.js"; -import { Vector4 } from "../../math/Vector4.js"; -import { WebGLRenderTarget } from "../WebGLRenderTarget.js"; - -export class WebGLColorBuffer { - constructor(); - - setMask(colorMask: boolean): void; - setLocked(lock: boolean): void; - setClear(r: number, g: number, b: number, a: number, premultipliedAlpha: boolean): void; - reset(): void; -} - -export class WebGLDepthBuffer { - constructor(); - - setTest(depthTest: boolean): void; - setMask(depthMask: boolean): void; - setFunc(depthFunc: DepthModes): void; - setLocked(lock: boolean): void; - setClear(depth: number): void; - reset(): void; -} - -export class WebGLStencilBuffer { - constructor(); - - setTest(stencilTest: boolean): void; - setMask(stencilMask: number): void; - setFunc(stencilFunc: number, stencilRef: number, stencilMask: number): void; - setOp(stencilFail: number, stencilZFail: number, stencilZPass: number): void; - setLocked(lock: boolean): void; - setClear(stencil: number): void; - reset(): void; -} - -export class WebGLState { - constructor(gl: WebGLRenderingContext); - - buffers: { - color: WebGLColorBuffer; - depth: WebGLDepthBuffer; - stencil: WebGLStencilBuffer; - }; - - enable(id: number): void; - disable(id: number): void; - bindFramebuffer(target: number, framebuffer: WebGLFramebuffer | null): void; - drawBuffers(renderTarget: WebGLRenderTarget | null, framebuffer: WebGLFramebuffer | null): void; - useProgram(program: any): boolean; - setBlending( - blending: Blending, - blendEquation?: BlendingEquation, - blendSrc?: BlendingSrcFactor, - blendDst?: BlendingDstFactor, - blendEquationAlpha?: BlendingEquation, - blendSrcAlpha?: BlendingSrcFactor, - blendDstAlpha?: BlendingDstFactor, - premultiplyAlpha?: boolean, - ): void; - setMaterial(material: Material, frontFaceCW: boolean): void; - setFlipSided(flipSided: boolean): void; - setCullFace(cullFace: CullFace): void; - setLineWidth(width: number): void; - setPolygonOffset(polygonoffset: boolean, factor?: number, units?: number): void; - setScissorTest(scissorTest: boolean): void; - activeTexture(webglSlot: number): void; - bindTexture(webglType: number, webglTexture: any): void; - unbindTexture(): void; - // Same interface as https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/compressedTexImage2D - compressedTexImage2D( - target: number, - level: number, - internalformat: number, - width: number, - height: number, - border: number, - data: ArrayBufferView, - ): void; - // Same interface as https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texImage2D - texImage2D( - target: number, - level: number, - internalformat: number, - width: number, - height: number, - border: number, - format: number, - type: number, - pixels: ArrayBufferView | null, - ): void; - texImage2D(target: number, level: number, internalformat: number, format: number, type: number, source: any): void; - texImage3D( - target: number, - level: number, - internalformat: number, - width: number, - height: number, - depth: number, - border: number, - format: number, - type: number, - pixels: any, - ): void; - scissor(scissor: Vector4): void; - viewport(viewport: Vector4): void; - reset(): void; -} diff --git a/src-testing/src/renderers/webgl/WebGLTextures.d.ts b/src-testing/src/renderers/webgl/WebGLTextures.d.ts deleted file mode 100644 index 031866dee..000000000 --- a/src-testing/src/renderers/webgl/WebGLTextures.d.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { WebGLCapabilities } from "./WebGLCapabilities.js"; -import { WebGLExtensions } from "./WebGLExtensions.js"; -import { WebGLInfo } from "./WebGLInfo.js"; -import { WebGLProperties } from "./WebGLProperties.js"; -import { WebGLState } from "./WebGLState.js"; -import { WebGLUtils } from "./WebGLUtils.js"; - -export class WebGLTextures { - constructor( - gl: WebGLRenderingContext, - extensions: WebGLExtensions, - state: WebGLState, - properties: WebGLProperties, - capabilities: WebGLCapabilities, - utils: WebGLUtils, - info: WebGLInfo, - ); - - allocateTextureUnit(): void; - resetTextureUnits(): void; - setTexture2D(texture: any, slot: number): void; - setTexture2DArray(texture: any, slot: number): void; - setTexture3D(texture: any, slot: number): void; - setTextureCube(texture: any, slot: number): void; - setupRenderTarget(renderTarget: any): void; - updateRenderTargetMipmap(renderTarget: any): void; - updateMultisampleRenderTarget(renderTarget: any): void; - safeSetTexture2D(texture: any, slot: number): void; - safeSetTextureCube(texture: any, slot: number): void; -} diff --git a/src-testing/src/renderers/webgl/WebGLUniforms.d.ts b/src-testing/src/renderers/webgl/WebGLUniforms.d.ts deleted file mode 100644 index 925b5e4bf..000000000 --- a/src-testing/src/renderers/webgl/WebGLUniforms.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { WebGLProgram } from "./WebGLProgram.js"; -import { WebGLTextures } from "./WebGLTextures.js"; - -export class WebGLUniforms { - constructor(gl: WebGLRenderingContext, program: WebGLProgram); - - setValue(gl: WebGLRenderingContext, name: string, value: any, textures: WebGLTextures): void; - setOptional(gl: WebGLRenderingContext, object: any, name: string): void; - - static upload(gl: WebGLRenderingContext, seq: any, values: any[], textures: WebGLTextures): void; - static seqWithValue(seq: any, values: any[]): any[]; -} diff --git a/src-testing/src/renderers/webgl/WebGLUniformsGroups.d.ts b/src-testing/src/renderers/webgl/WebGLUniformsGroups.d.ts deleted file mode 100644 index a5e052c1f..000000000 --- a/src-testing/src/renderers/webgl/WebGLUniformsGroups.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { UniformsGroup } from "../../core/UniformsGroup.js"; - -import { WebGLCapabilities } from "./WebGLCapabilities.js"; -import { WebGLInfo } from "./WebGLInfo.js"; -import { WebGLProgram } from "./WebGLProgram.js"; -import { WebGLState } from "./WebGLState.js"; - -export function WebGLUniformsGroups( - gl: WebGLRenderingContext, - info: WebGLInfo, - capabilities: WebGLCapabilities, - state: WebGLState, -): { - dispose: () => void; - update: (uniformsGroup: UniformsGroup, program: WebGLProgram) => void; - bind: (uniformsGroup: UniformsGroup, program: WebGLProgram) => void; -}; diff --git a/src-testing/src/renderers/webgl/WebGLUtils.d.ts b/src-testing/src/renderers/webgl/WebGLUtils.d.ts deleted file mode 100644 index b70a6e449..000000000 --- a/src-testing/src/renderers/webgl/WebGLUtils.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { ColorSpace, CompressedPixelFormat, PixelFormat, TextureDataType } from "../../constants.js"; -import { WebGLExtensions } from "./WebGLExtensions.js"; - -export class WebGLUtils { - constructor( - gl: WebGLRenderingContext | WebGL2RenderingContext, - extensions: WebGLExtensions, - ); - - convert(p: PixelFormat | CompressedPixelFormat | TextureDataType, colorSpace?: ColorSpace): number | null; -} diff --git a/src-testing/src/renderers/webgpu/WebGPUBackend.ts b/src-testing/src/renderers/webgpu/WebGPUBackend.ts deleted file mode 100644 index 2b2aeefcd..000000000 --- a/src-testing/src/renderers/webgpu/WebGPUBackend.ts +++ /dev/null @@ -1,1249 +0,0 @@ -/*// debugger tools -import 'https://greggman.github.io/webgpu-avoid-redundant-state-setting/webgpu-check-redundant-state-setting.js'; -//*/ - -import { - GPUFeatureName, - GPUTextureFormat, - GPULoadOp, - GPUStoreOp, - GPUIndexFormat, - GPUTextureViewDimension, -} from './utils/WebGPUConstants.js'; - -import WGSLNodeBuilder from './nodes/WGSLNodeBuilder.js'; -import Backend from '../common/Backend.js'; - -import WebGPUUtils from './utils/WebGPUUtils.js'; -import WebGPUAttributeUtils from './utils/WebGPUAttributeUtils.js'; -import WebGPUBindingUtils from './utils/WebGPUBindingUtils.js'; -import WebGPUPipelineUtils from './utils/WebGPUPipelineUtils.js'; -import WebGPUTextureUtils from './utils/WebGPUTextureUtils.js'; - -import { WebGPUCoordinateSystem } from '../../constants.js'; - -// - -class WebGPUBackend extends Backend { - constructor(parameters = {}) { - super(parameters); - - this.isWebGPUBackend = true; - - // some parameters require default values other than "undefined" - this.parameters.alpha = parameters.alpha === undefined ? true : parameters.alpha; - - this.parameters.requiredLimits = parameters.requiredLimits === undefined ? {} : parameters.requiredLimits; - - this.trackTimestamp = parameters.trackTimestamp === true; - - this.device = null; - this.context = null; - this.colorBuffer = null; - this.defaultRenderPassdescriptor = null; - - this.utils = new WebGPUUtils(this); - this.attributeUtils = new WebGPUAttributeUtils(this); - this.bindingUtils = new WebGPUBindingUtils(this); - this.pipelineUtils = new WebGPUPipelineUtils(this); - this.textureUtils = new WebGPUTextureUtils(this); - this.occludedResolveCache = new Map(); - } - - async init(renderer) { - await super.init(renderer); - - // - - const parameters = this.parameters; - - // create the device if it is not passed with parameters - - let device; - - if (parameters.device === undefined) { - const adapterOptions = { - powerPreference: parameters.powerPreference, - }; - - const adapter = await navigator.gpu.requestAdapter(adapterOptions); - - if (adapter === null) { - throw new Error('WebGPUBackend: Unable to create WebGPU adapter.'); - } - - // feature support - - const features = Object.values(GPUFeatureName); - - const supportedFeatures = []; - - for (const name of features) { - if (adapter.features.has(name)) { - supportedFeatures.push(name); - } - } - - const deviceDescriptor = { - requiredFeatures: supportedFeatures, - requiredLimits: parameters.requiredLimits, - }; - - device = await adapter.requestDevice(deviceDescriptor); - } else { - device = parameters.device; - } - - const context = - parameters.context !== undefined ? parameters.context : renderer.domElement.getContext('webgpu'); - - this.device = device; - this.context = context; - - const alphaMode = parameters.alpha ? 'premultiplied' : 'opaque'; - - this.context.configure({ - device: this.device, - format: GPUTextureFormat.BGRA8Unorm, - usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC, - alphaMode: alphaMode, - }); - - this.updateSize(); - } - - get coordinateSystem() { - return WebGPUCoordinateSystem; - } - - async getArrayBufferAsync(attribute) { - return await this.attributeUtils.getArrayBufferAsync(attribute); - } - - getContext() { - return this.context; - } - - _getDefaultRenderPassDescriptor() { - let descriptor = this.defaultRenderPassdescriptor; - - if (descriptor === null) { - const renderer = this.renderer; - - descriptor = { - colorAttachments: [ - { - view: null, - }, - ], - depthStencilAttachment: { - view: this.textureUtils.getDepthBuffer(renderer.depth, renderer.stencil).createView(), - }, - }; - - const colorAttachment = descriptor.colorAttachments[0]; - - if (this.renderer.samples > 0) { - colorAttachment.view = this.colorBuffer.createView(); - } else { - colorAttachment.resolveTarget = undefined; - } - - this.defaultRenderPassdescriptor = descriptor; - } - - const colorAttachment = descriptor.colorAttachments[0]; - - if (this.renderer.samples > 0) { - colorAttachment.resolveTarget = this.context.getCurrentTexture().createView(); - } else { - colorAttachment.view = this.context.getCurrentTexture().createView(); - } - - return descriptor; - } - - _getRenderPassDescriptor(renderContext) { - const renderTarget = renderContext.renderTarget; - const renderTargetData = this.get(renderTarget); - - let descriptors = renderTargetData.descriptors; - - if ( - descriptors === undefined || - renderTargetData.width !== renderTarget.width || - renderTargetData.height !== renderTarget.height || - renderTargetData.activeMipmapLevel !== renderTarget.activeMipmapLevel || - renderTargetData.samples !== renderTarget.samples - ) { - descriptors = {}; - - renderTargetData.descriptors = descriptors; - - // dispose - - const onDispose = () => { - renderTarget.removeEventListener('dispose', onDispose); - - this.delete(renderTarget); - }; - - renderTarget.addEventListener('dispose', onDispose); - } - - const cacheKey = renderContext.getCacheKey(); - - let descriptor = descriptors[cacheKey]; - - if (descriptor === undefined) { - const textures = renderContext.textures; - const colorAttachments = []; - - for (let i = 0; i < textures.length; i++) { - const textureData = this.get(textures[i]); - - const textureView = textureData.texture.createView({ - baseMipLevel: renderContext.activeMipmapLevel, - mipLevelCount: 1, - baseArrayLayer: renderContext.activeCubeFace, - dimension: GPUTextureViewDimension.TwoD, - }); - - let view, resolveTarget; - - if (textureData.msaaTexture !== undefined) { - view = textureData.msaaTexture.createView(); - resolveTarget = textureView; - } else { - view = textureView; - resolveTarget = undefined; - } - - colorAttachments.push({ - view, - resolveTarget, - loadOp: GPULoadOp.Load, - storeOp: GPUStoreOp.Store, - }); - } - - const depthTextureData = this.get(renderContext.depthTexture); - - const depthStencilAttachment = { - view: depthTextureData.texture.createView(), - }; - - descriptor = { - colorAttachments, - depthStencilAttachment, - }; - - descriptors[cacheKey] = descriptor; - - renderTargetData.width = renderTarget.width; - renderTargetData.height = renderTarget.height; - renderTargetData.samples = renderTarget.samples; - renderTargetData.activeMipmapLevel = renderTarget.activeMipmapLevel; - } - - return descriptor; - } - - beginRender(renderContext) { - const renderContextData = this.get(renderContext); - - const device = this.device; - const occlusionQueryCount = renderContext.occlusionQueryCount; - - let occlusionQuerySet; - - if (occlusionQueryCount > 0) { - if (renderContextData.currentOcclusionQuerySet) renderContextData.currentOcclusionQuerySet.destroy(); - if (renderContextData.currentOcclusionQueryBuffer) renderContextData.currentOcclusionQueryBuffer.destroy(); - - // Get a reference to the array of objects with queries. The renderContextData property - // can be changed by another render pass before the buffer.mapAsyc() completes. - renderContextData.currentOcclusionQuerySet = renderContextData.occlusionQuerySet; - renderContextData.currentOcclusionQueryBuffer = renderContextData.occlusionQueryBuffer; - renderContextData.currentOcclusionQueryObjects = renderContextData.occlusionQueryObjects; - - // - - occlusionQuerySet = device.createQuerySet({ type: 'occlusion', count: occlusionQueryCount }); - - renderContextData.occlusionQuerySet = occlusionQuerySet; - renderContextData.occlusionQueryIndex = 0; - renderContextData.occlusionQueryObjects = new Array(occlusionQueryCount); - - renderContextData.lastOcclusionObject = null; - } - - let descriptor; - - if (renderContext.textures === null) { - descriptor = this._getDefaultRenderPassDescriptor(); - } else { - descriptor = this._getRenderPassDescriptor(renderContext); - } - - this.initTimestampQuery(renderContext, descriptor); - - descriptor.occlusionQuerySet = occlusionQuerySet; - - const depthStencilAttachment = descriptor.depthStencilAttachment; - - if (renderContext.textures !== null) { - const colorAttachments = descriptor.colorAttachments; - - for (let i = 0; i < colorAttachments.length; i++) { - const colorAttachment = colorAttachments[i]; - - if (renderContext.clearColor) { - colorAttachment.clearValue = renderContext.clearColorValue; - colorAttachment.loadOp = GPULoadOp.Clear; - colorAttachment.storeOp = GPUStoreOp.Store; - } else { - colorAttachment.loadOp = GPULoadOp.Load; - colorAttachment.storeOp = GPUStoreOp.Store; - } - } - } else { - const colorAttachment = descriptor.colorAttachments[0]; - - if (renderContext.clearColor) { - colorAttachment.clearValue = renderContext.clearColorValue; - colorAttachment.loadOp = GPULoadOp.Clear; - colorAttachment.storeOp = GPUStoreOp.Store; - } else { - colorAttachment.loadOp = GPULoadOp.Load; - colorAttachment.storeOp = GPUStoreOp.Store; - } - } - - // - - if (renderContext.depth) { - if (renderContext.clearDepth) { - depthStencilAttachment.depthClearValue = renderContext.clearDepthValue; - depthStencilAttachment.depthLoadOp = GPULoadOp.Clear; - depthStencilAttachment.depthStoreOp = GPUStoreOp.Store; - } else { - depthStencilAttachment.depthLoadOp = GPULoadOp.Load; - depthStencilAttachment.depthStoreOp = GPUStoreOp.Store; - } - } - - if (renderContext.stencil) { - if (renderContext.clearStencil) { - depthStencilAttachment.stencilClearValue = renderContext.clearStencilValue; - depthStencilAttachment.stencilLoadOp = GPULoadOp.Clear; - depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store; - } else { - depthStencilAttachment.stencilLoadOp = GPULoadOp.Load; - depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store; - } - } - - // - - const encoder = device.createCommandEncoder({ label: 'renderContext_' + renderContext.id }); - const currentPass = encoder.beginRenderPass(descriptor); - - // - - renderContextData.descriptor = descriptor; - renderContextData.encoder = encoder; - renderContextData.currentPass = currentPass; - renderContextData.currentSets = { attributes: {} }; - - // - - if (renderContext.viewport) { - this.updateViewport(renderContext); - } - - if (renderContext.scissor) { - const { x, y, width, height } = renderContext.scissorValue; - - currentPass.setScissorRect(x, renderContext.height - height - y, width, height); - } - } - - finishRender(renderContext) { - const renderContextData = this.get(renderContext); - const occlusionQueryCount = renderContext.occlusionQueryCount; - - if (renderContextData.renderBundles !== undefined && renderContextData.renderBundles.length > 0) { - renderContextData.registerBundlesPhase = false; - renderContextData.currentPass.executeBundles(renderContextData.renderBundles); - } - - if (occlusionQueryCount > renderContextData.occlusionQueryIndex) { - renderContextData.currentPass.endOcclusionQuery(); - } - - renderContextData.currentPass.end(); - - if (occlusionQueryCount > 0) { - const bufferSize = occlusionQueryCount * 8; // 8 byte entries for query results - - // - - let queryResolveBuffer = this.occludedResolveCache.get(bufferSize); - - if (queryResolveBuffer === undefined) { - queryResolveBuffer = this.device.createBuffer({ - size: bufferSize, - usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC, - }); - - this.occludedResolveCache.set(bufferSize, queryResolveBuffer); - } - - // - - const readBuffer = this.device.createBuffer({ - size: bufferSize, - usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ, - }); - - // two buffers required here - WebGPU doesn't allow usage of QUERY_RESOLVE & MAP_READ to be combined - renderContextData.encoder.resolveQuerySet( - renderContextData.occlusionQuerySet, - 0, - occlusionQueryCount, - queryResolveBuffer, - 0, - ); - renderContextData.encoder.copyBufferToBuffer(queryResolveBuffer, 0, readBuffer, 0, bufferSize); - - renderContextData.occlusionQueryBuffer = readBuffer; - - // - - this.resolveOccludedAsync(renderContext); - } - - this.prepareTimestampBuffer(renderContext, renderContextData.encoder); - - this.device.queue.submit([renderContextData.encoder.finish()]); - - // - - if (renderContext.textures !== null) { - const textures = renderContext.textures; - - for (let i = 0; i < textures.length; i++) { - const texture = textures[i]; - - if (texture.generateMipmaps === true) { - this.textureUtils.generateMipmaps(texture); - } - } - } - } - - isOccluded(renderContext, object) { - const renderContextData = this.get(renderContext); - - return renderContextData.occluded && renderContextData.occluded.has(object); - } - - async resolveOccludedAsync(renderContext) { - const renderContextData = this.get(renderContext); - - // handle occlusion query results - - const { currentOcclusionQueryBuffer, currentOcclusionQueryObjects } = renderContextData; - - if (currentOcclusionQueryBuffer && currentOcclusionQueryObjects) { - const occluded = new WeakSet(); - - renderContextData.currentOcclusionQueryObjects = null; - renderContextData.currentOcclusionQueryBuffer = null; - - await currentOcclusionQueryBuffer.mapAsync(GPUMapMode.READ); - - const buffer = currentOcclusionQueryBuffer.getMappedRange(); - const results = new BigUint64Array(buffer); - - for (let i = 0; i < currentOcclusionQueryObjects.length; i++) { - if (results[i] !== BigInt(0)) { - occluded.add(currentOcclusionQueryObjects[i]); - } - } - - currentOcclusionQueryBuffer.destroy(); - - renderContextData.occluded = occluded; - } - } - - updateViewport(renderContext) { - const { currentPass } = this.get(renderContext); - const { x, y, width, height, minDepth, maxDepth } = renderContext.viewportValue; - - currentPass.setViewport(x, renderContext.height - height - y, width, height, minDepth, maxDepth); - } - - clear(color, depth, stencil, renderTargetData = null) { - const device = this.device; - const renderer = this.renderer; - - let colorAttachments = []; - - let depthStencilAttachment; - let clearValue; - - let supportsDepth; - let supportsStencil; - - if (color) { - const clearColor = this.getClearColor(); - - clearValue = { r: clearColor.r, g: clearColor.g, b: clearColor.b, a: clearColor.a }; - } - - if (renderTargetData === null) { - supportsDepth = renderer.depth; - supportsStencil = renderer.stencil; - - const descriptor = this._getDefaultRenderPassDescriptor(); - - if (color) { - colorAttachments = descriptor.colorAttachments; - - const colorAttachment = colorAttachments[0]; - - colorAttachment.clearValue = clearValue; - colorAttachment.loadOp = GPULoadOp.Clear; - colorAttachment.storeOp = GPUStoreOp.Store; - } - - if (supportsDepth || supportsStencil) { - depthStencilAttachment = descriptor.depthStencilAttachment; - } - } else { - supportsDepth = renderTargetData.depth; - supportsStencil = renderTargetData.stencil; - - if (color) { - for (const texture of renderTargetData.textures) { - const textureData = this.get(texture); - const textureView = textureData.texture.createView(); - - let view, resolveTarget; - - if (textureData.msaaTexture !== undefined) { - view = textureData.msaaTexture.createView(); - resolveTarget = textureView; - } else { - view = textureView; - resolveTarget = undefined; - } - - colorAttachments.push({ - view, - resolveTarget, - clearValue, - loadOp: GPULoadOp.Clear, - storeOp: GPUStoreOp.Store, - }); - } - } - - if (supportsDepth || supportsStencil) { - const depthTextureData = this.get(renderTargetData.depthTexture); - - depthStencilAttachment = { - view: depthTextureData.texture.createView(), - }; - } - } - - // - - if (supportsDepth) { - if (depth) { - depthStencilAttachment.depthLoadOp = GPULoadOp.Clear; - depthStencilAttachment.depthClearValue = renderer.getClearDepth(); - depthStencilAttachment.depthStoreOp = GPUStoreOp.Store; - } else { - depthStencilAttachment.depthLoadOp = GPULoadOp.Load; - depthStencilAttachment.depthStoreOp = GPUStoreOp.Store; - } - } - - // - - if (supportsStencil) { - if (stencil) { - depthStencilAttachment.stencilLoadOp = GPULoadOp.Clear; - depthStencilAttachment.stencilClearValue = renderer.getClearStencil(); - depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store; - } else { - depthStencilAttachment.stencilLoadOp = GPULoadOp.Load; - depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store; - } - } - - // - - const encoder = device.createCommandEncoder({}); - const currentPass = encoder.beginRenderPass({ - colorAttachments, - depthStencilAttachment, - }); - - currentPass.end(); - - device.queue.submit([encoder.finish()]); - } - - // compute - - beginCompute(computeGroup) { - const groupGPU = this.get(computeGroup); - - const descriptor = {}; - - this.initTimestampQuery(computeGroup, descriptor); - - groupGPU.cmdEncoderGPU = this.device.createCommandEncoder(); - - groupGPU.passEncoderGPU = groupGPU.cmdEncoderGPU.beginComputePass(descriptor); - } - - compute(computeGroup, computeNode, bindings, pipeline) { - const { passEncoderGPU } = this.get(computeGroup); - - // pipeline - - const pipelineGPU = this.get(pipeline).pipeline; - passEncoderGPU.setPipeline(pipelineGPU); - - // bind groups - - for (let i = 0, l = bindings.length; i < l; i++) { - const bindGroup = bindings[i]; - const bindingsData = this.get(bindGroup); - - passEncoderGPU.setBindGroup(i, bindingsData.group); - } - - const maxComputeWorkgroupsPerDimension = this.device.limits.maxComputeWorkgroupsPerDimension; - - const computeNodeData = this.get(computeNode); - - if (computeNodeData.dispatchSize === undefined) computeNodeData.dispatchSize = { x: 0, y: 1, z: 1 }; - - const { dispatchSize } = computeNodeData; - - if (computeNode.dispatchCount > maxComputeWorkgroupsPerDimension) { - dispatchSize.x = Math.min(computeNode.dispatchCount, maxComputeWorkgroupsPerDimension); - dispatchSize.y = Math.ceil(computeNode.dispatchCount / maxComputeWorkgroupsPerDimension); - } else { - dispatchSize.x = computeNode.dispatchCount; - } - - passEncoderGPU.dispatchWorkgroups(dispatchSize.x, dispatchSize.y, dispatchSize.z); - } - - finishCompute(computeGroup) { - const groupData = this.get(computeGroup); - - groupData.passEncoderGPU.end(); - - this.prepareTimestampBuffer(computeGroup, groupData.cmdEncoderGPU); - - this.device.queue.submit([groupData.cmdEncoderGPU.finish()]); - } - - // render object - - draw(renderObject, info) { - const { object, geometry, context, pipeline } = renderObject; - - const bindings = renderObject.getBindings(); - const contextData = this.get(context); - const pipelineGPU = this.get(pipeline).pipeline; - const currentSets = contextData.currentSets; - - const renderObjectData = this.get(renderObject); - - const { bundleEncoder, renderBundle, lastPipelineGPU } = renderObjectData; - - const renderContextData = this.get(context); - - if ( - renderContextData.registerBundlesPhase === true && - bundleEncoder !== undefined && - lastPipelineGPU === pipelineGPU - ) { - renderContextData.renderBundles.push(renderBundle); - return; - } - - const passEncoderGPU = this.renderer._currentRenderBundle - ? this.createBundleEncoder(context, renderObject) - : contextData.currentPass; - - // pipeline - - if (currentSets.pipeline !== pipelineGPU) { - passEncoderGPU.setPipeline(pipelineGPU); - - currentSets.pipeline = pipelineGPU; - } - - // bind groups - - for (let i = 0, l = bindings.length; i < l; i++) { - const bindGroup = bindings[i]; - const bindingsData = this.get(bindGroup); - - passEncoderGPU.setBindGroup(bindGroup.index, bindingsData.group); - } - - // attributes - - const index = renderObject.getIndex(); - - const hasIndex = index !== null; - - // index - - if (hasIndex === true) { - if (currentSets.index !== index) { - const buffer = this.get(index).buffer; - const indexFormat = index.array instanceof Uint16Array ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32; - - passEncoderGPU.setIndexBuffer(buffer, indexFormat); - - currentSets.index = index; - } - } - - // vertex buffers - - const vertexBuffers = renderObject.getVertexBuffers(); - - for (let i = 0, l = vertexBuffers.length; i < l; i++) { - const vertexBuffer = vertexBuffers[i]; - - if (currentSets.attributes[i] !== vertexBuffer) { - const buffer = this.get(vertexBuffer).buffer; - passEncoderGPU.setVertexBuffer(i, buffer); - - currentSets.attributes[i] = vertexBuffer; - } - } - - // occlusion queries - handle multiple consecutive draw calls for an object - - if (contextData.occlusionQuerySet !== undefined) { - const lastObject = contextData.lastOcclusionObject; - - if (lastObject !== object) { - if (lastObject !== null && lastObject.occlusionTest === true) { - passEncoderGPU.endOcclusionQuery(); - contextData.occlusionQueryIndex++; - } - - if (object.occlusionTest === true) { - passEncoderGPU.beginOcclusionQuery(contextData.occlusionQueryIndex); - contextData.occlusionQueryObjects[contextData.occlusionQueryIndex] = object; - } - - contextData.lastOcclusionObject = object; - } - } - - // draw - - const drawRange = renderObject.drawRange; - const firstVertex = drawRange.start; - - const instanceCount = this.getInstanceCount(renderObject); - if (instanceCount === 0) return; - - if (object.isBatchedMesh === true) { - const starts = object._multiDrawStarts; - const counts = object._multiDrawCounts; - const drawCount = object._multiDrawCount; - const drawInstances = object._multiDrawInstances; - - const bytesPerElement = index.bytesPerElement || 1; - - for (let i = 0; i < drawCount; i++) { - const count = drawInstances ? drawInstances[i] : 1; - const firstInstance = count > 1 ? 0 : i; - - passEncoderGPU.drawIndexed(counts[i] / bytesPerElement, count, starts[i] / 4, 0, firstInstance); - } - } else if (hasIndex === true) { - const indexCount = drawRange.count !== Infinity ? drawRange.count : index.count; - - passEncoderGPU.drawIndexed(indexCount, instanceCount, firstVertex, 0, 0); - - info.update(object, indexCount, instanceCount); - } else { - const positionAttribute = geometry.attributes.position; - const vertexCount = drawRange.count !== Infinity ? drawRange.count : positionAttribute.count; - - passEncoderGPU.draw(vertexCount, instanceCount, firstVertex, 0); - - info.update(object, vertexCount, instanceCount); - } - - if (this.renderer._currentRenderBundle) { - const renderBundle = passEncoderGPU.finish(); - renderObjectData.lastPipelineGPU = pipelineGPU; - renderObjectData.renderBundle = renderBundle; - renderObjectData.bundleEncoder = passEncoderGPU; - } - } - - // cache key - - needsRenderUpdate(renderObject) { - const data = this.get(renderObject); - - const { object, material } = renderObject; - - const utils = this.utils; - - const sampleCount = utils.getSampleCountRenderContext(renderObject.context); - const colorSpace = utils.getCurrentColorSpace(renderObject.context); - const colorFormat = utils.getCurrentColorFormat(renderObject.context); - const depthStencilFormat = utils.getCurrentDepthStencilFormat(renderObject.context); - const primitiveTopology = utils.getPrimitiveTopology(object, material); - - let needsUpdate = false; - - if ( - data.material !== material || - data.materialVersion !== material.version || - data.transparent !== material.transparent || - data.blending !== material.blending || - data.premultipliedAlpha !== material.premultipliedAlpha || - data.blendSrc !== material.blendSrc || - data.blendDst !== material.blendDst || - data.blendEquation !== material.blendEquation || - data.blendSrcAlpha !== material.blendSrcAlpha || - data.blendDstAlpha !== material.blendDstAlpha || - data.blendEquationAlpha !== material.blendEquationAlpha || - data.colorWrite !== material.colorWrite || - data.depthWrite !== material.depthWrite || - data.depthTest !== material.depthTest || - data.depthFunc !== material.depthFunc || - data.stencilWrite !== material.stencilWrite || - data.stencilFunc !== material.stencilFunc || - data.stencilFail !== material.stencilFail || - data.stencilZFail !== material.stencilZFail || - data.stencilZPass !== material.stencilZPass || - data.stencilFuncMask !== material.stencilFuncMask || - data.stencilWriteMask !== material.stencilWriteMask || - data.side !== material.side || - data.alphaToCoverage !== material.alphaToCoverage || - data.sampleCount !== sampleCount || - data.colorSpace !== colorSpace || - data.colorFormat !== colorFormat || - data.depthStencilFormat !== depthStencilFormat || - data.primitiveTopology !== primitiveTopology || - data.clippingContextVersion !== renderObject.clippingContextVersion - ) { - data.material = material; - data.materialVersion = material.version; - data.transparent = material.transparent; - data.blending = material.blending; - data.premultipliedAlpha = material.premultipliedAlpha; - data.blendSrc = material.blendSrc; - data.blendDst = material.blendDst; - data.blendEquation = material.blendEquation; - data.blendSrcAlpha = material.blendSrcAlpha; - data.blendDstAlpha = material.blendDstAlpha; - data.blendEquationAlpha = material.blendEquationAlpha; - data.colorWrite = material.colorWrite; - data.depthWrite = material.depthWrite; - data.depthTest = material.depthTest; - data.depthFunc = material.depthFunc; - data.stencilWrite = material.stencilWrite; - data.stencilFunc = material.stencilFunc; - data.stencilFail = material.stencilFail; - data.stencilZFail = material.stencilZFail; - data.stencilZPass = material.stencilZPass; - data.stencilFuncMask = material.stencilFuncMask; - data.stencilWriteMask = material.stencilWriteMask; - data.side = material.side; - data.alphaToCoverage = material.alphaToCoverage; - data.sampleCount = sampleCount; - data.colorSpace = colorSpace; - data.colorFormat = colorFormat; - data.depthStencilFormat = depthStencilFormat; - data.primitiveTopology = primitiveTopology; - data.clippingContextVersion = renderObject.clippingContextVersion; - - needsUpdate = true; - } - - return needsUpdate; - } - - getRenderCacheKey(renderObject) { - const { object, material } = renderObject; - - const utils = this.utils; - const renderContext = renderObject.context; - - return [ - material.transparent, - material.blending, - material.premultipliedAlpha, - material.blendSrc, - material.blendDst, - material.blendEquation, - material.blendSrcAlpha, - material.blendDstAlpha, - material.blendEquationAlpha, - material.colorWrite, - material.depthWrite, - material.depthTest, - material.depthFunc, - material.stencilWrite, - material.stencilFunc, - material.stencilFail, - material.stencilZFail, - material.stencilZPass, - material.stencilFuncMask, - material.stencilWriteMask, - material.side, - utils.getSampleCountRenderContext(renderContext), - utils.getCurrentColorSpace(renderContext), - utils.getCurrentColorFormat(renderContext), - utils.getCurrentDepthStencilFormat(renderContext), - utils.getPrimitiveTopology(object, material), - renderObject.clippingContextVersion, - ].join(); - } - - // textures - - createSampler(texture) { - this.textureUtils.createSampler(texture); - } - - destroySampler(texture) { - this.textureUtils.destroySampler(texture); - } - - createDefaultTexture(texture) { - this.textureUtils.createDefaultTexture(texture); - } - - createTexture(texture, options) { - this.textureUtils.createTexture(texture, options); - } - - updateTexture(texture, options) { - this.textureUtils.updateTexture(texture, options); - } - - generateMipmaps(texture) { - this.textureUtils.generateMipmaps(texture); - } - - destroyTexture(texture) { - this.textureUtils.destroyTexture(texture); - } - - copyTextureToBuffer(texture, x, y, width, height) { - return this.textureUtils.copyTextureToBuffer(texture, x, y, width, height); - } - - initTimestampQuery(renderContext, descriptor) { - if (!this.hasFeature(GPUFeatureName.TimestampQuery) || !this.trackTimestamp) return; - - const renderContextData = this.get(renderContext); - - if (!renderContextData.timeStampQuerySet) { - // Create a GPUQuerySet which holds 2 timestamp query results: one for the - // beginning and one for the end of compute pass execution. - const timeStampQuerySet = this.device.createQuerySet({ type: 'timestamp', count: 2 }); - - const timestampWrites = { - querySet: timeStampQuerySet, - beginningOfPassWriteIndex: 0, // Write timestamp in index 0 when pass begins. - endOfPassWriteIndex: 1, // Write timestamp in index 1 when pass ends. - }; - - Object.assign(descriptor, { - timestampWrites, - }); - - renderContextData.timeStampQuerySet = timeStampQuerySet; - } - } - - // timestamp utils - - prepareTimestampBuffer(renderContext, encoder) { - if (!this.hasFeature(GPUFeatureName.TimestampQuery) || !this.trackTimestamp) return; - - const renderContextData = this.get(renderContext); - - const size = 2 * BigInt64Array.BYTES_PER_ELEMENT; - - if (renderContextData.currentTimestampQueryBuffers === undefined) { - renderContextData.currentTimestampQueryBuffers = { - resolveBuffer: this.device.createBuffer({ - label: 'timestamp resolve buffer', - size: size, - usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC, - }), - resultBuffer: this.device.createBuffer({ - label: 'timestamp result buffer', - size: size, - usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ, - }), - isMappingPending: false, - }; - } - - const { resolveBuffer, resultBuffer, isMappingPending } = renderContextData.currentTimestampQueryBuffers; - - if (isMappingPending === true) return; - - encoder.resolveQuerySet(renderContextData.timeStampQuerySet, 0, 2, resolveBuffer, 0); - encoder.copyBufferToBuffer(resolveBuffer, 0, resultBuffer, 0, size); - } - - async resolveTimestampAsync(renderContext, type = 'render') { - if (!this.hasFeature(GPUFeatureName.TimestampQuery) || !this.trackTimestamp) return; - - const renderContextData = this.get(renderContext); - - if (renderContextData.currentTimestampQueryBuffers === undefined) return; - - const { resultBuffer, isMappingPending } = renderContextData.currentTimestampQueryBuffers; - - if (isMappingPending === true) return; - - renderContextData.currentTimestampQueryBuffers.isMappingPending = true; - - resultBuffer.mapAsync(GPUMapMode.READ).then(() => { - const times = new BigUint64Array(resultBuffer.getMappedRange()); - const duration = Number(times[1] - times[0]) / 1000000; - - this.renderer.info.updateTimestamp(type, duration); - - resultBuffer.unmap(); - - renderContextData.currentTimestampQueryBuffers.isMappingPending = false; - }); - } - - // node builder - - createNodeBuilder(object, renderer) { - return new WGSLNodeBuilder(object, renderer); - } - - // program - - createProgram(program) { - const programGPU = this.get(program); - - programGPU.module = { - module: this.device.createShaderModule({ code: program.code, label: program.stage }), - entryPoint: 'main', - }; - } - - destroyProgram(program) { - this.delete(program); - } - - // pipelines - - createRenderPipeline(renderObject, promises) { - this.pipelineUtils.createRenderPipeline(renderObject, promises); - } - - createComputePipeline(computePipeline, bindings) { - this.pipelineUtils.createComputePipeline(computePipeline, bindings); - } - - createBundleEncoder(renderContext, renderObject) { - return this.pipelineUtils.createBundleEncoder(renderContext, renderObject); - } - - // bindings - - createBindings(bindGroup) { - this.bindingUtils.createBindings(bindGroup); - } - - updateBindings(bindGroup) { - this.bindingUtils.createBindings(bindGroup); - } - - updateBinding(binding) { - this.bindingUtils.updateBinding(binding); - } - - // attributes - - createIndexAttribute(attribute) { - this.attributeUtils.createAttribute( - attribute, - GPUBufferUsage.INDEX | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, - ); - } - - createAttribute(attribute) { - this.attributeUtils.createAttribute( - attribute, - GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, - ); - } - - createStorageAttribute(attribute) { - this.attributeUtils.createAttribute( - attribute, - GPUBufferUsage.STORAGE | GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, - ); - } - - updateAttribute(attribute) { - this.attributeUtils.updateAttribute(attribute); - } - - destroyAttribute(attribute) { - this.attributeUtils.destroyAttribute(attribute); - } - - // canvas - - updateSize() { - this.colorBuffer = this.textureUtils.getColorBuffer(); - this.defaultRenderPassdescriptor = null; - } - - // utils public - - getMaxAnisotropy() { - return 16; - } - - hasFeature(name) { - return this.device.features.has(name); - } - - copyTextureToTexture(srcTexture, dstTexture, srcRegion = null, dstPosition = null, level = 0) { - let dstX = 0; - let dstY = 0; - - let srcX = 0; - let srcY = 0; - - let srcWidth = srcTexture.image.width; - let srcHeight = srcTexture.image.height; - - if (srcRegion !== null) { - srcX = srcRegion.x; - srcY = srcRegion.y; - srcWidth = srcRegion.width; - srcHeight = srcRegion.height; - } - - if (dstPosition !== null) { - dstX = dstPosition.x; - dstY = dstPosition.y; - } - - const encoder = this.device.createCommandEncoder({ - label: 'copyTextureToTexture_' + srcTexture.id + '_' + dstTexture.id, - }); - - const sourceGPU = this.get(srcTexture).texture; - const destinationGPU = this.get(dstTexture).texture; - - encoder.copyTextureToTexture( - { - texture: sourceGPU, - mipLevel: level, - origin: { x: srcX, y: srcY, z: 0 }, - }, - { - texture: destinationGPU, - mipLevel: level, - origin: { x: dstX, y: dstY, z: 0 }, - }, - [srcWidth, srcHeight], - ); - - this.device.queue.submit([encoder.finish()]); - } - - copyFramebufferToTexture(texture, renderContext) { - const renderContextData = this.get(renderContext); - - const { encoder, descriptor } = renderContextData; - - let sourceGPU = null; - - if (renderContext.renderTarget) { - if (texture.isDepthTexture) { - sourceGPU = this.get(renderContext.depthTexture).texture; - } else { - sourceGPU = this.get(renderContext.textures[0]).texture; - } - } else { - if (texture.isDepthTexture) { - sourceGPU = this.textureUtils.getDepthBuffer(renderContext.depth, renderContext.stencil); - } else { - sourceGPU = this.context.getCurrentTexture(); - } - } - - const destinationGPU = this.get(texture).texture; - - if (sourceGPU.format !== destinationGPU.format) { - console.error( - 'WebGPUBackend: copyFramebufferToTexture: Source and destination formats do not match.', - sourceGPU.format, - destinationGPU.format, - ); - - return; - } - - renderContextData.currentPass.end(); - - encoder.copyTextureToTexture( - { - texture: sourceGPU, - origin: { x: 0, y: 0, z: 0 }, - }, - { - texture: destinationGPU, - }, - [texture.image.width, texture.image.height], - ); - - if (texture.generateMipmaps) this.textureUtils.generateMipmaps(texture); - - for (let i = 0; i < descriptor.colorAttachments.length; i++) { - descriptor.colorAttachments[i].loadOp = GPULoadOp.Load; - } - - if (renderContext.depth) descriptor.depthStencilAttachment.depthLoadOp = GPULoadOp.Load; - if (renderContext.stencil) descriptor.depthStencilAttachment.stencilLoadOp = GPULoadOp.Load; - - renderContextData.currentPass = encoder.beginRenderPass(descriptor); - renderContextData.currentSets = { attributes: {} }; - } -} - -export default WebGPUBackend; diff --git a/src-testing/src/renderers/webgpu/WebGPURenderer.ts b/src-testing/src/renderers/webgpu/WebGPURenderer.ts deleted file mode 100644 index 7a25c2677..000000000 --- a/src-testing/src/renderers/webgpu/WebGPURenderer.ts +++ /dev/null @@ -1,43 +0,0 @@ -import WebGPU from '../../../examples/jsm/capabilities/WebGPU.js'; - -import Renderer from '../common/Renderer.js'; -import WebGLBackend from '../webgl-fallback/WebGLBackend.js'; -import WebGPUBackend from './WebGPUBackend.js'; -/* -const debugHandler = { - - get: function ( target, name ) { - - // Add |update - if ( /^(create|destroy)/.test( name ) ) console.log( 'WebGPUBackend.' + name ); - - return target[ name ]; - - } - -}; -*/ -class WebGPURenderer extends Renderer { - constructor(parameters = {}) { - let BackendClass; - - if (parameters.forceWebGL) { - BackendClass = WebGLBackend; - } else if (WebGPU.isAvailable()) { - BackendClass = WebGPUBackend; - } else { - BackendClass = WebGLBackend; - - console.warn('THREE.WebGPURenderer: WebGPU is not available, running under WebGL2 backend.'); - } - - const backend = new BackendClass(parameters); - - //super( new Proxy( backend, debugHandler ) ); - super(backend, parameters); - - this.isWebGPURenderer = true; - } -} - -export default WebGPURenderer; diff --git a/src-testing/src/renderers/webgpu/nodes/WGSLNodeBuilder.ts b/src-testing/src/renderers/webgpu/nodes/WGSLNodeBuilder.ts deleted file mode 100644 index 1d808f4be..000000000 --- a/src-testing/src/renderers/webgpu/nodes/WGSLNodeBuilder.ts +++ /dev/null @@ -1,1114 +0,0 @@ -import NodeUniformsGroup from '../../common/nodes/NodeUniformsGroup.js'; - -import NodeSampler from '../../common/nodes/NodeSampler.js'; -import { - NodeSampledTexture, - NodeSampledCubeTexture, - NodeSampledTexture3D, -} from '../../common/nodes/NodeSampledTexture.js'; - -import NodeUniformBuffer from '../../common/nodes/NodeUniformBuffer.js'; -import NodeStorageBuffer from '../../common/nodes/NodeStorageBuffer.js'; - -import { NodeBuilder, CodeNode } from '../../../nodes/Nodes.js'; - -import { getFormat } from '../utils/WebGPUTextureUtils.js'; - -import WGSLNodeParser from './WGSLNodeParser.js'; -import { GPUBufferBindingType, GPUStorageTextureAccess } from '../utils/WebGPUConstants.js'; - -import { NoColorSpace, FloatType } from '../../../constants.js'; - -// GPUShaderStage is not defined in browsers not supporting WebGPU -const GPUShaderStage = self.GPUShaderStage; - -const gpuShaderStageLib = { - vertex: GPUShaderStage ? GPUShaderStage.VERTEX : 1, - fragment: GPUShaderStage ? GPUShaderStage.FRAGMENT : 2, - compute: GPUShaderStage ? GPUShaderStage.COMPUTE : 4, -}; - -const supports = { - instance: true, - swizzleAssign: false, - storageBuffer: true, -}; - -const wgslFnOpLib = { - '^^': 'tsl_xor', -}; - -const wgslTypeLib = { - float: 'f32', - int: 'i32', - uint: 'u32', - bool: 'bool', - color: 'vec3', - - vec2: 'vec2', - ivec2: 'vec2', - uvec2: 'vec2', - bvec2: 'vec2', - - vec3: 'vec3', - ivec3: 'vec3', - uvec3: 'vec3', - bvec3: 'vec3', - - vec4: 'vec4', - ivec4: 'vec4', - uvec4: 'vec4', - bvec4: 'vec4', - - mat2: 'mat2x2', - imat2: 'mat2x2', - umat2: 'mat2x2', - bmat2: 'mat2x2', - - mat3: 'mat3x3', - imat3: 'mat3x3', - umat3: 'mat3x3', - bmat3: 'mat3x3', - - mat4: 'mat4x4', - imat4: 'mat4x4', - umat4: 'mat4x4', - bmat4: 'mat4x4', -}; - -const wgslPolyfill = { - tsl_xor: new CodeNode('fn tsl_xor( a : bool, b : bool ) -> bool { return ( a || b ) && !( a && b ); }'), - mod_float: new CodeNode('fn tsl_mod_float( x : f32, y : f32 ) -> f32 { return x - y * floor( x / y ); }'), - mod_vec2: new CodeNode('fn tsl_mod_vec2( x : vec2f, y : vec2f ) -> vec2f { return x - y * floor( x / y ); }'), - mod_vec3: new CodeNode('fn tsl_mod_vec3( x : vec3f, y : vec3f ) -> vec3f { return x - y * floor( x / y ); }'), - mod_vec4: new CodeNode('fn tsl_mod_vec4( x : vec4f, y : vec4f ) -> vec4f { return x - y * floor( x / y ); }'), - equals_bool: new CodeNode('fn tsl_equals_bool( a : bool, b : bool ) -> bool { return a == b; }'), - equals_bvec2: new CodeNode( - 'fn tsl_equals_bvec2( a : vec2f, b : vec2f ) -> vec2 { return vec2( a.x == b.x, a.y == b.y ); }', - ), - equals_bvec3: new CodeNode( - 'fn tsl_equals_bvec3( a : vec3f, b : vec3f ) -> vec3 { return vec3( a.x == b.x, a.y == b.y, a.z == b.z ); }', - ), - equals_bvec4: new CodeNode( - 'fn tsl_equals_bvec4( a : vec4f, b : vec4f ) -> vec4 { return vec4( a.x == b.x, a.y == b.y, a.z == b.z, a.w == b.w ); }', - ), - repeatWrapping: new CodeNode(` -fn tsl_repeatWrapping( uv : vec2, dimension : vec2 ) -> vec2 { - - let uvScaled = vec2( uv * vec2( dimension ) ); - - return ( ( uvScaled % dimension ) + dimension ) % dimension; - -} -`), - biquadraticTexture: new CodeNode(` -fn tsl_biquadraticTexture( map : texture_2d, coord : vec2f, level : i32 ) -> vec4f { - - let res = vec2f( textureDimensions( map, level ) ); - - let uvScaled = coord * res; - let uvWrapping = ( ( uvScaled % res ) + res ) % res; - - // https://www.shadertoy.com/view/WtyXRy - - let uv = uvWrapping - 0.5; - let iuv = floor( uv ); - let f = fract( uv ); - - let rg1 = textureLoad( map, vec2i( iuv + vec2( 0.5, 0.5 ) ), level ); - let rg2 = textureLoad( map, vec2i( iuv + vec2( 1.5, 0.5 ) ), level ); - let rg3 = textureLoad( map, vec2i( iuv + vec2( 0.5, 1.5 ) ), level ); - let rg4 = textureLoad( map, vec2i( iuv + vec2( 1.5, 1.5 ) ), level ); - - return mix( mix( rg1, rg2, f.x ), mix( rg3, rg4, f.x ), f.y ); - -} -`), -}; - -const wgslMethods = { - dFdx: 'dpdx', - dFdy: '- dpdy', - mod_float: 'tsl_mod_float', - mod_vec2: 'tsl_mod_vec2', - mod_vec3: 'tsl_mod_vec3', - mod_vec4: 'tsl_mod_vec4', - equals_bool: 'tsl_equals_bool', - equals_bvec2: 'tsl_equals_bvec2', - equals_bvec3: 'tsl_equals_bvec3', - equals_bvec4: 'tsl_equals_bvec4', - inversesqrt: 'inverseSqrt', - bitcast: 'bitcast', -}; - -// WebGPU issue: does not support pow() with negative base on Windows - -if (/Windows/g.test(navigator.userAgent)) { - wgslPolyfill.pow_float = new CodeNode( - 'fn tsl_pow_float( a : f32, b : f32 ) -> f32 { return select( -pow( -a, b ), pow( a, b ), a > 0.0 ); }', - ); - wgslPolyfill.pow_vec2 = new CodeNode( - 'fn tsl_pow_vec2( a : vec2f, b : vec2f ) -> vec2f { return vec2f( tsl_pow_float( a.x, b.x ), tsl_pow_float( a.y, b.y ) ); }', - [wgslPolyfill.pow_float], - ); - wgslPolyfill.pow_vec3 = new CodeNode( - 'fn tsl_pow_vec3( a : vec3f, b : vec3f ) -> vec3f { return vec3f( tsl_pow_float( a.x, b.x ), tsl_pow_float( a.y, b.y ), tsl_pow_float( a.z, b.z ) ); }', - [wgslPolyfill.pow_float], - ); - wgslPolyfill.pow_vec4 = new CodeNode( - 'fn tsl_pow_vec4( a : vec4f, b : vec4f ) -> vec4f { return vec4f( tsl_pow_float( a.x, b.x ), tsl_pow_float( a.y, b.y ), tsl_pow_float( a.z, b.z ), tsl_pow_float( a.w, b.w ) ); }', - [wgslPolyfill.pow_float], - ); - - wgslMethods.pow_float = 'tsl_pow_float'; - wgslMethods.pow_vec2 = 'tsl_pow_vec2'; - wgslMethods.pow_vec3 = 'tsl_pow_vec3'; - wgslMethods.pow_vec4 = 'tsl_pow_vec4'; -} - -// - -class WGSLNodeBuilder extends NodeBuilder { - constructor(object, renderer) { - super(object, renderer, new WGSLNodeParser()); - - this.uniformGroups = {}; - - this.builtins = {}; - - this.directives = {}; - } - - needsColorSpaceToLinear(texture) { - return texture.isVideoTexture === true && texture.colorSpace !== NoColorSpace; - } - - _generateTextureSample(texture, textureProperty, uvSnippet, depthSnippet, shaderStage = this.shaderStage) { - if (shaderStage === 'fragment') { - if (depthSnippet) { - return `textureSample( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet}, ${depthSnippet} )`; - } else { - return `textureSample( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet} )`; - } - } else if (this.isFilteredTexture(texture)) { - return this.generateFilteredTexture(texture, textureProperty, uvSnippet); - } else { - return this.generateTextureLod(texture, textureProperty, uvSnippet, '0'); - } - } - - _generateVideoSample(textureProperty, uvSnippet, shaderStage = this.shaderStage) { - if (shaderStage === 'fragment') { - return `textureSampleBaseClampToEdge( ${textureProperty}, ${textureProperty}_sampler, vec2( ${uvSnippet}.x, 1.0 - ${uvSnippet}.y ) )`; - } else { - console.error(`WebGPURenderer: THREE.VideoTexture does not support ${shaderStage} shader.`); - } - } - - _generateTextureSampleLevel( - texture, - textureProperty, - uvSnippet, - levelSnippet, - depthSnippet, - shaderStage = this.shaderStage, - ) { - if (shaderStage === 'fragment' && this.isUnfilterable(texture) === false) { - return `textureSampleLevel( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet}, ${levelSnippet} )`; - } else if (this.isFilteredTexture(texture)) { - return this.generateFilteredTexture(texture, textureProperty, uvSnippet, levelSnippet); - } else { - return this.generateTextureLod(texture, textureProperty, uvSnippet, levelSnippet); - } - } - - generateFilteredTexture(texture, textureProperty, uvSnippet, levelSnippet = '0') { - this._include('biquadraticTexture'); - - return `tsl_biquadraticTexture( ${textureProperty}, ${uvSnippet}, i32( ${levelSnippet} ) )`; - } - - generateTextureLod(texture, textureProperty, uvSnippet, levelSnippet = '0') { - this._include('repeatWrapping'); - - const dimension = - texture.isMultisampleRenderTargetTexture === true - ? `textureDimensions( ${textureProperty} )` - : `textureDimensions( ${textureProperty}, 0 )`; - - return `textureLoad( ${textureProperty}, tsl_repeatWrapping( ${uvSnippet}, ${dimension} ), i32( ${levelSnippet} ) )`; - } - - generateTextureLoad(texture, textureProperty, uvIndexSnippet, depthSnippet, levelSnippet = '0u') { - if (depthSnippet) { - return `textureLoad( ${textureProperty}, ${uvIndexSnippet}, ${depthSnippet}, ${levelSnippet} )`; - } else { - return `textureLoad( ${textureProperty}, ${uvIndexSnippet}, ${levelSnippet} )`; - } - } - - generateTextureStore(texture, textureProperty, uvIndexSnippet, valueSnippet) { - return `textureStore( ${textureProperty}, ${uvIndexSnippet}, ${valueSnippet} )`; - } - - isUnfilterable(texture) { - return ( - this.getComponentTypeFromTexture(texture) !== 'float' || - (texture.isDataTexture === true && texture.type === FloatType) || - texture.isMultisampleRenderTargetTexture === true - ); - } - - generateTexture(texture, textureProperty, uvSnippet, depthSnippet, shaderStage = this.shaderStage) { - let snippet = null; - - if (texture.isVideoTexture === true) { - snippet = this._generateVideoSample(textureProperty, uvSnippet, shaderStage); - } else if (this.isUnfilterable(texture)) { - snippet = this.generateTextureLod(texture, textureProperty, uvSnippet, '0', depthSnippet, shaderStage); - } else { - snippet = this._generateTextureSample(texture, textureProperty, uvSnippet, depthSnippet, shaderStage); - } - - return snippet; - } - - generateTextureGrad( - texture, - textureProperty, - uvSnippet, - gradSnippet, - depthSnippet, - shaderStage = this.shaderStage, - ) { - if (shaderStage === 'fragment') { - // TODO handle i32 or u32 --> uvSnippet, array_index: A, ddx, ddy - return `textureSampleGrad( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet}, ${gradSnippet[0]}, ${gradSnippet[1]} )`; - } else { - console.error(`WebGPURenderer: THREE.TextureNode.gradient() does not support ${shaderStage} shader.`); - } - } - - generateTextureCompare( - texture, - textureProperty, - uvSnippet, - compareSnippet, - depthSnippet, - shaderStage = this.shaderStage, - ) { - if (shaderStage === 'fragment') { - return `textureSampleCompare( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet}, ${compareSnippet} )`; - } else { - console.error( - `WebGPURenderer: THREE.DepthTexture.compareFunction() does not support ${shaderStage} shader.`, - ); - } - } - - generateTextureLevel( - texture, - textureProperty, - uvSnippet, - levelSnippet, - depthSnippet, - shaderStage = this.shaderStage, - ) { - let snippet = null; - - if (texture.isVideoTexture === true) { - snippet = this._generateVideoSample(textureProperty, uvSnippet, shaderStage); - } else { - snippet = this._generateTextureSampleLevel( - texture, - textureProperty, - uvSnippet, - levelSnippet, - depthSnippet, - shaderStage, - ); - } - - return snippet; - } - - generateTextureBias( - texture, - textureProperty, - uvSnippet, - biasSnippet, - depthSnippet, - shaderStage = this.shaderStage, - ) { - if (shaderStage === 'fragment') { - return `textureSampleBias( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet}, ${biasSnippet} )`; - } else { - console.error(`WebGPURenderer: THREE.TextureNode.biasNode does not support ${shaderStage} shader.`); - } - } - - getPropertyName(node, shaderStage = this.shaderStage) { - if (node.isNodeVarying === true && node.needsInterpolation === true) { - if (shaderStage === 'vertex') { - return `varyings.${node.name}`; - } - } else if (node.isNodeUniform === true) { - const name = node.name; - const type = node.type; - - if (type === 'texture' || type === 'cubeTexture' || type === 'storageTexture' || type === 'texture3D') { - return name; - } else if (type === 'buffer' || type === 'storageBuffer') { - return `NodeBuffer_${node.id}.${name}`; - } else { - return node.groupNode.name + '.' + name; - } - } - - return super.getPropertyName(node); - } - - getOutputStructName() { - return 'output'; - } - - _getUniformGroupCount(shaderStage) { - return Object.keys(this.uniforms[shaderStage]).length; - } - - getFunctionOperator(op) { - const fnOp = wgslFnOpLib[op]; - - if (fnOp !== undefined) { - this._include(fnOp); - - return fnOp; - } - - return null; - } - - getStorageAccess(node) { - if (node.isStorageTextureNode) { - switch (node.access) { - case GPUStorageTextureAccess.ReadOnly: - return 'read'; - - case GPUStorageTextureAccess.WriteOnly: - return 'write'; - - default: - return 'read_write'; - } - } else { - switch (node.access) { - case GPUBufferBindingType.Storage: - return 'read_write'; - - case GPUBufferBindingType.ReadOnlyStorage: - return 'read'; - - default: - return 'write'; - } - } - } - - getUniformFromNode(node, type, shaderStage, name = null) { - const uniformNode = super.getUniformFromNode(node, type, shaderStage, name); - const nodeData = this.getDataFromNode(node, shaderStage, this.globalCache); - - if (nodeData.uniformGPU === undefined) { - let uniformGPU; - - const group = node.groupNode; - const groupName = group.name; - - const bindings = this.getBindGroupArray(groupName, shaderStage); - - if (type === 'texture' || type === 'cubeTexture' || type === 'storageTexture' || type === 'texture3D') { - let texture = null; - - if (type === 'texture' || type === 'storageTexture') { - texture = new NodeSampledTexture( - uniformNode.name, - uniformNode.node, - group, - node.access ? node.access : null, - ); - } else if (type === 'cubeTexture') { - texture = new NodeSampledCubeTexture( - uniformNode.name, - uniformNode.node, - group, - node.access ? node.access : null, - ); - } else if (type === 'texture3D') { - texture = new NodeSampledTexture3D( - uniformNode.name, - uniformNode.node, - group, - node.access ? node.access : null, - ); - } - - texture.store = node.isStorageTextureNode === true; - texture.setVisibility(gpuShaderStageLib[shaderStage]); - - if ( - shaderStage === 'fragment' && - this.isUnfilterable(node.value) === false && - texture.store === false - ) { - const sampler = new NodeSampler(`${uniformNode.name}_sampler`, uniformNode.node, group); - sampler.setVisibility(gpuShaderStageLib[shaderStage]); - - bindings.push(sampler, texture); - - uniformGPU = [sampler, texture]; - } else { - bindings.push(texture); - - uniformGPU = [texture]; - } - } else if (type === 'buffer' || type === 'storageBuffer') { - const bufferClass = type === 'storageBuffer' ? NodeStorageBuffer : NodeUniformBuffer; - const buffer = new bufferClass(node, group); - buffer.setVisibility(gpuShaderStageLib[shaderStage]); - - bindings.push(buffer); - - uniformGPU = buffer; - } else { - const uniformsStage = this.uniformGroups[shaderStage] || (this.uniformGroups[shaderStage] = {}); - - let uniformsGroup = uniformsStage[groupName]; - - if (uniformsGroup === undefined) { - uniformsGroup = new NodeUniformsGroup(groupName, group); - uniformsGroup.setVisibility(gpuShaderStageLib[shaderStage]); - - uniformsStage[groupName] = uniformsGroup; - - bindings.push(uniformsGroup); - } - - uniformGPU = this.getNodeUniform(uniformNode, type); - - uniformsGroup.addUniform(uniformGPU); - } - - nodeData.uniformGPU = uniformGPU; - } - - return uniformNode; - } - - getBuiltin(name, property, type, shaderStage = this.shaderStage) { - const map = this.builtins[shaderStage] || (this.builtins[shaderStage] = new Map()); - - if (map.has(name) === false) { - map.set(name, { - name, - property, - type, - }); - } - - return property; - } - - getVertexIndex() { - if (this.shaderStage === 'vertex') { - return this.getBuiltin('vertex_index', 'vertexIndex', 'u32', 'attribute'); - } - - return 'vertexIndex'; - } - - buildFunctionCode(shaderNode) { - const layout = shaderNode.layout; - const flowData = this.flowShaderNode(shaderNode); - - const parameters = []; - - for (const input of layout.inputs) { - parameters.push(input.name + ' : ' + this.getType(input.type)); - } - - // - - const code = `fn ${layout.name}( ${parameters.join(', ')} ) -> ${this.getType(layout.type)} { -${flowData.vars} -${flowData.code} - return ${flowData.result}; - -}`; - - // - - return code; - } - - getInstanceIndex() { - if (this.shaderStage === 'vertex') { - return this.getBuiltin('instance_index', 'instanceIndex', 'u32', 'attribute'); - } - - return 'instanceIndex'; - } - - getSubgroupSize() { - this.enableSubGroups(); - - return this.getBuiltin('subgroup_size', 'subgroupSize', 'u32', 'attribute'); - } - - getSubgroupIndex() { - this.enableSubGroups(); - - return this.getBuiltin('subgroup_invocation_id', 'subgroupIndex', 'u32', 'attribute'); - } - - getDrawIndex() { - return null; - } - - getFrontFacing() { - return this.getBuiltin('front_facing', 'isFront', 'bool'); - } - - getFragCoord() { - return this.getBuiltin('position', 'fragCoord', 'vec4') + '.xyz'; - } - - getFragDepth() { - return 'output.' + this.getBuiltin('frag_depth', 'depth', 'f32', 'output'); - } - - isFlipY() { - return false; - } - - enableDirective(name, shaderStage = this.shaderStage) { - const stage = this.directives[shaderStage] || (this.directives[shaderStage] = new Set()); - stage.add(name); - } - - getDirectives(shaderStage) { - const snippets = []; - const directives = this.directives[shaderStage]; - - if (directives !== undefined) { - for (const directive of directives) { - snippets.push(`enable ${directive};`); - } - } - - return snippets.join('\n'); - } - - enableSubGroups() { - this.enableDirective('subgroups'); - } - - enableSubgroupsF16() { - this.enableDirective('subgroups-f16'); - } - - enableClipDistances() { - this.enableDirective('clip_distances'); - } - - enableShaderF16() { - this.enableDirective('f16'); - } - - enableDualSourceBlending() { - this.enableDirective('dual_source_blending'); - } - - getBuiltins(shaderStage) { - const snippets = []; - const builtins = this.builtins[shaderStage]; - - if (builtins !== undefined) { - for (const { name, property, type } of builtins.values()) { - snippets.push(`@builtin( ${name} ) ${property} : ${type}`); - } - } - - return snippets.join(',\n\t'); - } - - getAttributes(shaderStage) { - const snippets = []; - - if (shaderStage === 'compute') { - this.getBuiltin('global_invocation_id', 'id', 'vec3', 'attribute'); - this.getBuiltin('workgroup_id', 'workgroupId', 'vec3', 'attribute'); - this.getBuiltin('local_invocation_id', 'localId', 'vec3', 'attribute'); - this.getBuiltin('num_workgroups', 'numWorkgroups', 'vec3', 'attribute'); - } - - if (shaderStage === 'vertex' || shaderStage === 'compute') { - const builtins = this.getBuiltins('attribute'); - - if (builtins) snippets.push(builtins); - - const attributes = this.getAttributesArray(); - - for (let index = 0, length = attributes.length; index < length; index++) { - const attribute = attributes[index]; - const name = attribute.name; - const type = this.getType(attribute.type); - - snippets.push(`@location( ${index} ) ${name} : ${type}`); - } - } - - return snippets.join(',\n\t'); - } - - getStructMembers(struct) { - const snippets = []; - const members = struct.getMemberTypes(); - - for (let i = 0; i < members.length; i++) { - const member = members[i]; - snippets.push(`\t@location( ${i} ) m${i} : ${member}`); - } - - const builtins = this.getBuiltins('output'); - - if (builtins) snippets.push(builtins); - - return snippets.join(',\n'); - } - - getStructs(shaderStage) { - const snippets = []; - const structs = this.structs[shaderStage]; - - for (let index = 0, length = structs.length; index < length; index++) { - const struct = structs[index]; - const name = struct.name; - - let snippet = `\struct ${name} {\n`; - snippet += this.getStructMembers(struct); - snippet += '\n}'; - - snippets.push(snippet); - - snippets.push(`\nvar output : ${name};\n\n`); - } - - return snippets.join('\n\n'); - } - - getVar(type, name) { - return `var ${name} : ${this.getType(type)}`; - } - - getVars(shaderStage) { - const snippets = []; - const vars = this.vars[shaderStage]; - - if (vars !== undefined) { - for (const variable of vars) { - snippets.push(`\t${this.getVar(variable.type, variable.name)};`); - } - } - - return `\n${snippets.join('\n')}\n`; - } - - getVaryings(shaderStage) { - const snippets = []; - - if (shaderStage === 'vertex') { - this.getBuiltin('position', 'Vertex', 'vec4', 'vertex'); - } - - if (shaderStage === 'vertex' || shaderStage === 'fragment') { - const varyings = this.varyings; - const vars = this.vars[shaderStage]; - - for (let index = 0; index < varyings.length; index++) { - const varying = varyings[index]; - - if (varying.needsInterpolation) { - let attributesSnippet = `@location( ${index} )`; - - if (/^(int|uint|ivec|uvec)/.test(varying.type)) { - attributesSnippet += ' @interpolate( flat )'; - } - - snippets.push(`${attributesSnippet} ${varying.name} : ${this.getType(varying.type)}`); - } else if (shaderStage === 'vertex' && vars.includes(varying) === false) { - vars.push(varying); - } - } - } - - const builtins = this.getBuiltins(shaderStage); - - if (builtins) snippets.push(builtins); - - const code = snippets.join(',\n\t'); - - return shaderStage === 'vertex' ? this._getWGSLStruct('VaryingsStruct', '\t' + code) : code; - } - - getUniforms(shaderStage) { - const uniforms = this.uniforms[shaderStage]; - - const bindingSnippets = []; - const bufferSnippets = []; - const structSnippets = []; - const uniformGroups = {}; - - for (const uniform of uniforms) { - const groundName = uniform.groupNode.name; - const uniformIndexes = this.bindingsIndexes[groundName]; - - if ( - uniform.type === 'texture' || - uniform.type === 'cubeTexture' || - uniform.type === 'storageTexture' || - uniform.type === 'texture3D' - ) { - const texture = uniform.node.value; - - if ( - shaderStage === 'fragment' && - this.isUnfilterable(texture) === false && - uniform.node.isStorageTextureNode !== true - ) { - if (texture.isDepthTexture === true && texture.compareFunction !== null) { - bindingSnippets.push( - `@binding( ${uniformIndexes.binding++} ) @group( ${uniformIndexes.group} ) var ${uniform.name}_sampler : sampler_comparison;`, - ); - } else { - bindingSnippets.push( - `@binding( ${uniformIndexes.binding++} ) @group( ${uniformIndexes.group} ) var ${uniform.name}_sampler : sampler;`, - ); - } - } - - let textureType; - - let multisampled = ''; - - if (texture.isMultisampleRenderTargetTexture === true) { - multisampled = '_multisampled'; - } - - if (texture.isCubeTexture === true) { - textureType = 'texture_cube'; - } else if (texture.isDataArrayTexture === true) { - textureType = 'texture_2d_array'; - } else if (texture.isDepthTexture === true) { - textureType = `texture_depth${multisampled}_2d`; - } else if (texture.isVideoTexture === true) { - textureType = 'texture_external'; - } else if (texture.isData3DTexture === true) { - textureType = 'texture_3d'; - } else if (uniform.node.isStorageTextureNode === true) { - const format = getFormat(texture); - const access = this.getStorageAccess(uniform.node); - - textureType = `texture_storage_2d<${format}, ${access}>`; - } else { - const componentPrefix = this.getComponentTypeFromTexture(texture).charAt(0); - - textureType = `texture${multisampled}_2d<${componentPrefix}32>`; - } - - bindingSnippets.push( - `@binding( ${uniformIndexes.binding++} ) @group( ${uniformIndexes.group} ) var ${uniform.name} : ${textureType};`, - ); - } else if (uniform.type === 'buffer' || uniform.type === 'storageBuffer') { - const bufferNode = uniform.node; - const bufferType = this.getType(bufferNode.bufferType); - const bufferCount = bufferNode.bufferCount; - - const bufferCountSnippet = bufferCount > 0 ? ', ' + bufferCount : ''; - const bufferSnippet = `\t${uniform.name} : array< ${bufferType}${bufferCountSnippet} >\n`; - const bufferAccessMode = bufferNode.isStorageBufferNode - ? `storage, ${this.getStorageAccess(bufferNode)}` - : 'uniform'; - - bufferSnippets.push( - this._getWGSLStructBinding( - 'NodeBuffer_' + bufferNode.id, - bufferSnippet, - bufferAccessMode, - uniformIndexes.binding++, - uniformIndexes.group, - ), - ); - } else { - const vectorType = this.getType(this.getVectorType(uniform.type)); - const groupName = uniform.groupNode.name; - - const group = - uniformGroups[groupName] || - (uniformGroups[groupName] = { - index: uniformIndexes.binding++, - id: uniformIndexes.group, - snippets: [], - }); - - group.snippets.push(`\t${uniform.name} : ${vectorType}`); - } - } - - for (const name in uniformGroups) { - const group = uniformGroups[name]; - - structSnippets.push( - this._getWGSLStructBinding(name, group.snippets.join(',\n'), 'uniform', group.index, group.id), - ); - } - - let code = bindingSnippets.join('\n'); - code += bufferSnippets.join('\n'); - code += structSnippets.join('\n'); - - return code; - } - - buildCode() { - const shadersData = this.material !== null ? { fragment: {}, vertex: {} } : { compute: {} }; - - for (const shaderStage in shadersData) { - const stageData = shadersData[shaderStage]; - stageData.uniforms = this.getUniforms(shaderStage); - stageData.attributes = this.getAttributes(shaderStage); - stageData.varyings = this.getVaryings(shaderStage); - stageData.structs = this.getStructs(shaderStage); - stageData.vars = this.getVars(shaderStage); - stageData.codes = this.getCodes(shaderStage); - stageData.directives = this.getDirectives(shaderStage); - - // - - let flow = '// code\n\n'; - flow += this.flowCode[shaderStage]; - - const flowNodes = this.flowNodes[shaderStage]; - const mainNode = flowNodes[flowNodes.length - 1]; - - const outputNode = mainNode.outputNode; - const isOutputStruct = outputNode !== undefined && outputNode.isOutputStructNode === true; - - for (const node of flowNodes) { - const flowSlotData = this.getFlowData(node /*, shaderStage*/); - const slotName = node.name; - - if (slotName) { - if (flow.length > 0) flow += '\n'; - - flow += `\t// flow -> ${slotName}\n\t`; - } - - flow += `${flowSlotData.code}\n\t`; - - if (node === mainNode && shaderStage !== 'compute') { - flow += '// result\n\n\t'; - - if (shaderStage === 'vertex') { - flow += `varyings.Vertex = ${flowSlotData.result};`; - } else if (shaderStage === 'fragment') { - if (isOutputStruct) { - stageData.returnType = outputNode.nodeType; - - flow += `return ${flowSlotData.result};`; - } else { - let structSnippet = '\t@location(0) color: vec4'; - - const builtins = this.getBuiltins('output'); - - if (builtins) structSnippet += ',\n\t' + builtins; - - stageData.returnType = 'OutputStruct'; - stageData.structs += this._getWGSLStruct('OutputStruct', structSnippet); - stageData.structs += '\nvar output : OutputStruct;\n\n'; - - flow += `output.color = ${flowSlotData.result};\n\n\treturn output;`; - } - } - } - } - - stageData.flow = flow; - } - - if (this.material !== null) { - this.vertexShader = this._getWGSLVertexCode(shadersData.vertex); - this.fragmentShader = this._getWGSLFragmentCode(shadersData.fragment); - } else { - this.computeShader = this._getWGSLComputeCode( - shadersData.compute, - (this.object.workgroupSize || [64]).join(', '), - ); - } - } - - getMethod(method, output = null) { - let wgslMethod; - - if (output !== null) { - wgslMethod = this._getWGSLMethod(method + '_' + output); - } - - if (wgslMethod === undefined) { - wgslMethod = this._getWGSLMethod(method); - } - - return wgslMethod || method; - } - - getType(type) { - return wgslTypeLib[type] || type; - } - - isAvailable(name) { - let result = supports[name]; - - if (result === undefined) { - if (name === 'float32Filterable') { - result = this.renderer.hasFeature('float32-filterable'); - } - - supports[name] = result; - } - - return result; - } - - _getWGSLMethod(method) { - if (wgslPolyfill[method] !== undefined) { - this._include(method); - } - - return wgslMethods[method]; - } - - _include(name) { - const codeNode = wgslPolyfill[name]; - codeNode.build(this); - - if (this.currentFunctionNode !== null) { - this.currentFunctionNode.includes.push(codeNode); - } - - return codeNode; - } - - _getWGSLVertexCode(shaderData) { - return `${this.getSignature()} -// directives -${shaderData.directives} - -// uniforms -${shaderData.uniforms} - -// varyings -${shaderData.varyings} -var varyings : VaryingsStruct; - -// codes -${shaderData.codes} - -@vertex -fn main( ${shaderData.attributes} ) -> VaryingsStruct { - - // vars - ${shaderData.vars} - - // flow - ${shaderData.flow} - - return varyings; - -} -`; - } - - _getWGSLFragmentCode(shaderData) { - return `${this.getSignature()} - -diagnostic( off, derivative_uniformity ); - -// uniforms -${shaderData.uniforms} - -// structs -${shaderData.structs} - -// codes -${shaderData.codes} - -@fragment -fn main( ${shaderData.varyings} ) -> ${shaderData.returnType} { - - // vars - ${shaderData.vars} - - // flow - ${shaderData.flow} - -} -`; - } - - _getWGSLComputeCode(shaderData, workgroupSize) { - return `${this.getSignature()} -// directives -${shaderData.directives} - -// system -var instanceIndex : u32; - -// uniforms -${shaderData.uniforms} - -// codes -${shaderData.codes} - -@compute @workgroup_size( ${workgroupSize} ) -fn main( ${shaderData.attributes} ) { - - // system - instanceIndex = id.x + id.y * numWorkgroups.x * u32(${workgroupSize}) + id.z * numWorkgroups.x * numWorkgroups.y * u32(${workgroupSize}); - - // vars - ${shaderData.vars} - - // flow - ${shaderData.flow} - -} -`; - } - - _getWGSLStruct(name, vars) { - return ` -struct ${name} { -${vars} -};`; - } - - _getWGSLStructBinding(name, vars, access, binding = 0, group = 0) { - const structName = name + 'Struct'; - const structSnippet = this._getWGSLStruct(structName, vars); - - return `${structSnippet} -@binding( ${binding} ) @group( ${group} ) -var<${access}> ${name} : ${structName};`; - } -} - -export default WGSLNodeBuilder; diff --git a/src-testing/src/renderers/webgpu/nodes/WGSLNodeFunction.ts b/src-testing/src/renderers/webgpu/nodes/WGSLNodeFunction.ts deleted file mode 100644 index beab7253d..000000000 --- a/src-testing/src/renderers/webgpu/nodes/WGSLNodeFunction.ts +++ /dev/null @@ -1,140 +0,0 @@ -import NodeFunction from '../../../nodes/core/NodeFunction.js'; -import NodeFunctionInput from '../../../nodes/core/NodeFunctionInput.js'; - -const declarationRegexp = /^[fn]*\s*([a-z_0-9]+)?\s*\(([\s\S]*?)\)\s*[\-\>]*\s*([a-z_0-9]+(?:<[\s\S]+?>)?)/i; -const propertiesRegexp = /([a-z_0-9]+)\s*:\s*([a-z_0-9]+(?:<[\s\S]+?>)?)/gi; - -const wgslTypeLib = { - f32: 'float', - i32: 'int', - u32: 'uint', - bool: 'bool', - - 'vec2': 'vec2', - 'vec2': 'ivec2', - 'vec2': 'uvec2', - 'vec2': 'bvec2', - - vec2f: 'vec2', - vec2i: 'ivec2', - vec2u: 'uvec2', - vec2b: 'bvec2', - - 'vec3': 'vec3', - 'vec3': 'ivec3', - 'vec3': 'uvec3', - 'vec3': 'bvec3', - - vec3f: 'vec3', - vec3i: 'ivec3', - vec3u: 'uvec3', - vec3b: 'bvec3', - - 'vec4': 'vec4', - 'vec4': 'ivec4', - 'vec4': 'uvec4', - 'vec4': 'bvec4', - - vec4f: 'vec4', - vec4i: 'ivec4', - vec4u: 'uvec4', - vec4b: 'bvec4', - - 'mat2x2': 'mat2', - mat2x2f: 'mat2', - - 'mat3x3': 'mat3', - mat3x3f: 'mat3', - - 'mat4x4': 'mat4', - mat4x4f: 'mat4', - - sampler: 'sampler', - - texture_1d: 'texture', - - texture_2d: 'texture', - texture_2d_array: 'texture', - texture_multisampled_2d: 'cubeTexture', - - texture_depth_2d: 'depthTexture', - - texture_3d: 'texture3D', - - texture_cube: 'cubeTexture', - texture_cube_array: 'cubeTexture', - - texture_storage_1d: 'storageTexture', - texture_storage_2d: 'storageTexture', - texture_storage_2d_array: 'storageTexture', - texture_storage_3d: 'storageTexture', -}; - -const parse = source => { - source = source.trim(); - - const declaration = source.match(declarationRegexp); - - if (declaration !== null && declaration.length === 4) { - const inputsCode = declaration[2]; - const propsMatches = []; - let match = null; - - while ((match = propertiesRegexp.exec(inputsCode)) !== null) { - propsMatches.push({ name: match[1], type: match[2] }); - } - - // Process matches to correctly pair names and types - const inputs = []; - for (let i = 0; i < propsMatches.length; i++) { - const { name, type } = propsMatches[i]; - - let resolvedType = type; - - if (resolvedType.startsWith('texture')) { - resolvedType = type.split('<')[0]; - } - - resolvedType = wgslTypeLib[resolvedType] || resolvedType; - - inputs.push(new NodeFunctionInput(resolvedType, name)); - } - - const blockCode = source.substring(declaration[0].length); - const outputType = declaration[3] || 'void'; - - const name = declaration[1] !== undefined ? declaration[1] : ''; - const type = wgslTypeLib[outputType] || outputType; - - return { - type, - inputs, - name, - inputsCode, - blockCode, - outputType, - }; - } else { - throw new Error('FunctionNode: Function is not a WGSL code.'); - } -}; - -class WGSLNodeFunction extends NodeFunction { - constructor(source) { - const { type, inputs, name, inputsCode, blockCode, outputType } = parse(source); - - super(type, inputs, name); - - this.inputsCode = inputsCode; - this.blockCode = blockCode; - this.outputType = outputType; - } - - getCode(name = this.name) { - const outputType = this.outputType !== 'void' ? '-> ' + this.outputType : ''; - - return `fn ${name} ( ${this.inputsCode.trim()} ) ${outputType}` + this.blockCode; - } -} - -export default WGSLNodeFunction; diff --git a/src-testing/src/renderers/webgpu/nodes/WGSLNodeParser.ts b/src-testing/src/renderers/webgpu/nodes/WGSLNodeParser.ts deleted file mode 100644 index c32133df4..000000000 --- a/src-testing/src/renderers/webgpu/nodes/WGSLNodeParser.ts +++ /dev/null @@ -1,10 +0,0 @@ -import NodeParser from '../../../nodes/core/NodeParser.js'; -import WGSLNodeFunction from './WGSLNodeFunction.js'; - -class WGSLNodeParser extends NodeParser { - parseFunction(source) { - return new WGSLNodeFunction(source); - } -} - -export default WGSLNodeParser; diff --git a/src-testing/src/renderers/webgpu/utils/WebGPUConstants.d.ts b/src-testing/src/renderers/webgpu/utils/WebGPUConstants.d.ts deleted file mode 100644 index baa36f901..000000000 --- a/src-testing/src/renderers/webgpu/utils/WebGPUConstants.d.ts +++ /dev/null @@ -1,328 +0,0 @@ -export enum GPUPrimitiveTopology { - PointList = "point-list", - LineList = "line-list", - LineStrip = "line-strip", - TriangleList = "triangle-list", - TriangleStrip = "triangle-strip", -} - -export enum GPUCompareFunction { - Never = "never", - Less = "less", - Equal = "equal", - LessEqual = "less-equal", - Greater = "greater", - NotEqual = "not-equal", - GreaterEqual = "greater-equal", - Always = "always", -} - -export enum GPUStoreOp { - Store = "store", - Discard = "discard", -} - -export enum GPULoadOp { - Load = "load", - Clear = "clear", -} - -export enum GPUFrontFace { - CCW = "ccw", - CW = "cw", -} - -export enum GPUCullMode { - None = "none", - Front = "front", - Back = "back", -} - -export enum GPUIndexFormat { - Uint16 = "uint16", - Uint32 = "uint32", -} - -export enum GPUVertexFormat { - Uint8x2 = "uint8x2", - Uint8x4 = "uint8x4", - Sint8x2 = "sint8x2", - Sint8x4 = "sint8x4", - Unorm8x2 = "unorm8x2", - Unorm8x4 = "unorm8x4", - Snorm8x2 = "snorm8x2", - Snorm8x4 = "snorm8x4", - Uint16x2 = "uint16x2", - Uint16x4 = "uint16x4", - Sint16x2 = "sint16x2", - Sint16x4 = "sint16x4", - Unorm16x2 = "unorm16x2", - Unorm16x4 = "unorm16x4", - Snorm16x2 = "snorm16x2", - Snorm16x4 = "snorm16x4", - Float16x2 = "float16x2", - Float16x4 = "float16x4", - Float32 = "float32", - Float32x2 = "float32x2", - Float32x3 = "float32x3", - Float32x4 = "float32x4", - Uint32 = "uint32", - Uint32x2 = "uint32x2", - Uint32x3 = "uint32x3", - Uint32x4 = "uint32x4", - Sint32 = "sint32", - Sint32x2 = "sint32x2", - Sint32x3 = "sint32x3", - Sint32x4 = "sint32x4", -} - -export enum GPUTextureFormat { - // 8-bit formats - - R8Unorm = "r8unorm", - R8Snorm = "r8snorm", - R8Uint = "r8uint", - R8Sint = "r8sint", - - // 16-bit formats - - R16Uint = "r16uint", - R16Sint = "r16sint", - R16Float = "r16float", - RG8Unorm = "rg8unorm", - RG8Snorm = "rg8snorm", - RG8Uint = "rg8uint", - RG8Sint = "rg8sint", - - // 32-bit formats - - R32Uint = "r32uint", - R32Sint = "r32sint", - R32Float = "r32float", - RG16Uint = "rg16uint", - RG16Sint = "rg16sint", - RG16Float = "rg16float", - RGBA8Unorm = "rgba8unorm", - RGBA8UnormSRGB = "rgba8unorm-srgb", - RGBA8Snorm = "rgba8snorm", - RGBA8Uint = "rgba8uint", - RGBA8Sint = "rgba8sint", - BGRA8Unorm = "bgra8unorm", - BGRA8UnormSRGB = "bgra8unorm-srgb", - // Packed 32-bit formats - RGB9E5UFloat = "rgb9e5ufloat", - RGB10A2Unorm = "rgb10a2unorm", - RG11B10uFloat = "rgb10a2unorm", - - // 64-bit formats - - RG32Uint = "rg32uint", - RG32Sint = "rg32sint", - RG32Float = "rg32float", - RGBA16Uint = "rgba16uint", - RGBA16Sint = "rgba16sint", - RGBA16Float = "rgba16float", - - // 128-bit formats - - RGBA32Uint = "rgba32uint", - RGBA32Sint = "rgba32sint", - RGBA32Float = "rgba32float", - - // Depth and stencil formats - - Stencil8 = "stencil8", - Depth16Unorm = "depth16unorm", - Depth24Plus = "depth24plus", - Depth24PlusStencil8 = "depth24plus-stencil8", - Depth32Float = "depth32float", - - // 'depth32float-stencil8' extension - - Depth32FloatStencil8 = "depth32float-stencil8", - - // BC compressed formats usable if 'texture-compression-bc' is both - // supported by the device/user agent and enabled in requestDevice. - - BC1RGBAUnorm = "bc1-rgba-unorm", - BC1RGBAUnormSRGB = "bc1-rgba-unorm-srgb", - BC2RGBAUnorm = "bc2-rgba-unorm", - BC2RGBAUnormSRGB = "bc2-rgba-unorm-srgb", - BC3RGBAUnorm = "bc3-rgba-unorm", - BC3RGBAUnormSRGB = "bc3-rgba-unorm-srgb", - BC4RUnorm = "bc4-r-unorm", - BC4RSnorm = "bc4-r-snorm", - BC5RGUnorm = "bc5-rg-unorm", - BC5RGSnorm = "bc5-rg-snorm", - BC6HRGBUFloat = "bc6h-rgb-ufloat", - BC6HRGBFloat = "bc6h-rgb-float", - BC7RGBAUnorm = "bc7-rgba-unorm", - BC7RGBAUnormSRGB = "bc7-rgba-srgb", - - // ETC2 compressed formats usable if 'texture-compression-etc2' is both - // supported by the device/user agent and enabled in requestDevice. - - ETC2RGB8Unorm = "etc2-rgb8unorm", - ETC2RGB8UnormSRGB = "etc2-rgb8unorm-srgb", - ETC2RGB8A1Unorm = "etc2-rgb8a1unorm", - ETC2RGB8A1UnormSRGB = "etc2-rgb8a1unorm-srgb", - ETC2RGBA8Unorm = "etc2-rgba8unorm", - ETC2RGBA8UnormSRGB = "etc2-rgba8unorm-srgb", - EACR11Unorm = "eac-r11unorm", - EACR11Snorm = "eac-r11snorm", - EACRG11Unorm = "eac-rg11unorm", - EACRG11Snorm = "eac-rg11snorm", - - // ASTC compressed formats usable if 'texture-compression-astc' is both - // supported by the device/user agent and enabled in requestDevice. - - ASTC4x4Unorm = "astc-4x4-unorm", - ASTC4x4UnormSRGB = "astc-4x4-unorm-srgb", - ASTC5x4Unorm = "astc-5x4-unorm", - ASTC5x4UnormSRGB = "astc-5x4-unorm-srgb", - ASTC5x5Unorm = "astc-5x5-unorm", - ASTC5x5UnormSRGB = "astc-5x5-unorm-srgb", - ASTC6x5Unorm = "astc-6x5-unorm", - ASTC6x5UnormSRGB = "astc-6x5-unorm-srgb", - ASTC6x6Unorm = "astc-6x6-unorm", - ASTC6x6UnormSRGB = "astc-6x6-unorm-srgb", - ASTC8x5Unorm = "astc-8x5-unorm", - ASTC8x5UnormSRGB = "astc-8x5-unorm-srgb", - ASTC8x6Unorm = "astc-8x6-unorm", - ASTC8x6UnormSRGB = "astc-8x6-unorm-srgb", - ASTC8x8Unorm = "astc-8x8-unorm", - ASTC8x8UnormSRGB = "astc-8x8-unorm-srgb", - ASTC10x5Unorm = "astc-10x5-unorm", - ASTC10x5UnormSRGB = "astc-10x5-unorm-srgb", - ASTC10x6Unorm = "astc-10x6-unorm", - ASTC10x6UnormSRGB = "astc-10x6-unorm-srgb", - ASTC10x8Unorm = "astc-10x8-unorm", - ASTC10x8UnormSRGB = "astc-10x8-unorm-srgb", - ASTC10x10Unorm = "astc-10x10-unorm", - ASTC10x10UnormSRGB = "astc-10x10-unorm-srgb", - ASTC12x10Unorm = "astc-12x10-unorm", - ASTC12x10UnormSRGB = "astc-12x10-unorm-srgb", - ASTC12x12Unorm = "astc-12x12-unorm", - ASTC12x12UnormSRGB = "astc-12x12-unorm-srgb", -} - -export enum GPUAddressMode { - ClampToEdge = "clamp-to-edge", - Repeat = "repeat", - MirrorRepeat = "mirror-repeat", -} - -export enum GPUFilterMode { - Linear = "linear", - Nearest = "nearest", -} - -export enum GPUBlendFactor { - Zero = "zero", - One = "one", - Src = "src", - OneMinusSrc = "one-minus-src", - SrcAlpha = "src-alpha", - OneMinusSrcAlpha = "one-minus-src-alpha", - Dst = "dst", - OneMinusDstColor = "one-minus-dst", - DstAlpha = "dst-alpha", - OneMinusDstAlpha = "one-minus-dst-alpha", - SrcAlphaSaturated = "src-alpha-saturated", - Constant = "constant", - OneMinusConstant = "one-minus-constant", -} - -export enum GPUBlendOperation { - Add = "add", - Subtract = "subtract", - ReverseSubtract = "reverse-subtract", - Min = "min", - Max = "max", -} - -export enum GPUColorWriteFlags { - None = 0, - Red = 0x1, - Green = 0x2, - Blue = 0x4, - Alpha = 0x8, - All = 0xF, -} - -export enum GPUStencilOperation { - Keep = "keep", - Zero = "zero", - Replace = "replace", - Invert = "invert", - IncrementClamp = "increment-clamp", - DecrementClamp = "decrement-clamp", - IncrementWrap = "increment-wrap", - DecrementWrap = "decrement-wrap", -} - -export enum GPUBufferBindingType { - Uniform = "uniform", - Storage = "storage", - ReadOnlyStorage = "read-only-storage", -} - -export enum GPUStorageTextureAccess { - WriteOnly = "write-only", - ReadOnly = "read-only", - ReadWrite = "read-write", -} - -export enum GPUSamplerBindingType { - Filtering = "filtering", - NonFiltering = "non-filtering", - Comparison = "comparison", -} - -export enum GPUTextureSampleType { - Float = "float", - UnfilterableFloat = "unfilterable-float", - Depth = "depth", - SInt = "sint", - UInt = "uint", -} - -export enum GPUTextureDimension { - OneD = "1d", - TwoD = "2d", - ThreeD = "3d", -} - -export enum GPUTextureViewDimension { - OneD = "1d", - TwoD = "2d", - TwoDArray = "2d-array", - Cube = "cube", - CubeArray = "cube-array", - ThreeD = "3d", -} - -export enum GPUTextureAspect { - All = "all", - StencilOnly = "stencil-only", - DepthOnly = "depth-only", -} - -export enum GPUInputStepMode { - Vertex = "vertex", - Instance = "instance", -} - -export enum GPUFeatureName { - DepthClipControl = "depth-clip-control", - Depth32FloatStencil8 = "depth32float-stencil8", - TextureCompressionBC = "texture-compression-bc", - TextureCompressionETC2 = "texture-compression-etc2", - TextureCompressionASTC = "texture-compression-astc", - TimestampQuery = "timestamp-query", - IndirectFirstInstance = "indirect-first-instance", - ShaderF16 = "shader-f16", - RG11B10UFloat = "rg11b10ufloat-renderable", - BGRA8UNormStorage = "bgra8unorm-storage", - Float32Filterable = "float32-filterable", -} diff --git a/src-testing/src/renderers/webxr/WebXRController.d.ts b/src-testing/src/renderers/webxr/WebXRController.d.ts deleted file mode 100644 index 956a036b4..000000000 --- a/src-testing/src/renderers/webxr/WebXRController.d.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { Object3DEventMap } from "../../core/Object3D.js"; -import { Vector3 } from "../../math/Vector3.js"; -import { Group } from "../../objects/Group.js"; - -export type XRControllerEventType = XRSessionEventType | XRInputSourceEventType | "disconnected" | "connected"; - -export class XRJointSpace extends Group { - readonly jointRadius: number | undefined; -} - -export type XRHandJoints = Record; - -export interface XRHandInputState { - pinching: boolean; -} - -export interface WebXRSpaceEventMap extends Object3DEventMap { - select: { data: XRInputSource }; - selectstart: { data: XRInputSource }; - selectend: { data: XRInputSource }; - squeeze: { data: XRInputSource }; - squeezestart: { data: XRInputSource }; - squeezeend: { data: XRInputSource }; - - connected: { data: XRInputSource }; - disconnected: { data: XRInputSource }; - - pinchend: { handedness: XRHandedness; target: WebXRController }; // This Event break the THREE.EventDispatcher contract, replacing the target to the wrong instance. - pinchstart: { handedness: XRHandedness; target: WebXRController }; // This Event break the THREE.EventDispatcher contract, replacing the target to the wrong instance. - - move: {}; -} - -export class XRHandSpace extends Group { - readonly joints: Partial; - readonly inputState: XRHandInputState; -} - -export class XRTargetRaySpace extends Group { - hasLinearVelocity: boolean; - readonly linearVelocity: Vector3; - hasAngularVelocity: boolean; - readonly angularVelocity: Vector3; -} - -export class XRGripSpace extends Group { - hasLinearVelocity: boolean; - readonly linearVelocity: Vector3; - hasAngularVelocity: boolean; - readonly angularVelocity: Vector3; -} - -export class WebXRController { - constructor(); - - getHandSpace(): XRHandSpace; - getTargetRaySpace(): XRTargetRaySpace; - getGripSpace(): XRGripSpace; - dispatchEvent(event: { type: XRControllerEventType; data?: XRInputSource }): this; - connect(inputSource: XRInputSource): this; - disconnect(inputSource: XRInputSource): this; - update(inputSource: XRInputSource, frame: XRFrame, referenceSpace: XRReferenceSpace): this; -} diff --git a/src-testing/src/renderers/webxr/WebXRDepthSensing.d.ts b/src-testing/src/renderers/webxr/WebXRDepthSensing.d.ts deleted file mode 100644 index 23914f679..000000000 --- a/src-testing/src/renderers/webxr/WebXRDepthSensing.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Mesh } from "../../objects/Mesh.js"; -import { Texture } from "../../textures/Texture.js"; -import { WebGLRenderer } from "../WebGLRenderer.js"; -import { WebXRArrayCamera } from "./WebXRManager.js"; - -export class WebXRDepthSensing { - texture: Texture | null; - mesh: Mesh | null; - - depthNear: number; - depthFar: number; - - constructor(); - - init(renderer: WebGLRenderer, depthData: XRWebGLDepthInformation, renderState: XRRenderState): void; - - getMesh(cameraXR: WebXRArrayCamera): Mesh | null; - - reset(): void; - - getDepthTexture(): Texture | null; -} diff --git a/src-testing/src/renderers/webxr/WebXRManager.d.ts b/src-testing/src/renderers/webxr/WebXRManager.d.ts deleted file mode 100644 index 4b2073101..000000000 --- a/src-testing/src/renderers/webxr/WebXRManager.d.ts +++ /dev/null @@ -1,85 +0,0 @@ -/// - -import { ArrayCamera } from "../../cameras/ArrayCamera.js"; -import { PerspectiveCamera } from "../../cameras/PerspectiveCamera.js"; -import { EventDispatcher } from "../../core/EventDispatcher.js"; -import { Vector4 } from "../../math/Vector4.js"; -import { Mesh } from "../../objects/Mesh.js"; -import { Texture } from "../../textures/Texture.js"; -import { WebGLRenderer } from "../WebGLRenderer.js"; -import { XRGripSpace, XRHandSpace, XRTargetRaySpace } from "./WebXRController.js"; - -export type WebXRCamera = PerspectiveCamera & { viewport: Vector4 }; -export type WebXRArrayCamera = Omit & { cameras: [WebXRCamera, WebXRCamera] }; - -export interface WebXRManagerEventMap { - sessionstart: {}; - sessionend: {}; - planeadded: { data: XRPlane }; - planeremoved: { data: XRPlane }; - planechanged: { data: XRPlane }; - planesdetected: { data: XRPlaneSet }; -} - -export class WebXRManager extends EventDispatcher { - /** - * @default true - */ - cameraAutoUpdate: boolean; - - /** - * @default false - */ - enabled: boolean; - - /** - * @default false - */ - isPresenting: boolean; - - constructor(renderer: WebGLRenderer, gl: WebGLRenderingContext); - - getController: (index: number) => XRTargetRaySpace; - - getControllerGrip: (index: number) => XRGripSpace; - - getHand: (index: number) => XRHandSpace; - - setFramebufferScaleFactor: (value: number) => void; - - setReferenceSpaceType: (value: XRReferenceSpaceType) => void; - - getReferenceSpace: () => XRReferenceSpace | null; - - setReferenceSpace: (value: XRReferenceSpace) => void; - - getBaseLayer: () => XRWebGLLayer | XRProjectionLayer; - - getBinding: () => XRWebGLBinding; - - getFrame: () => XRFrame; - - getSession: () => XRSession | null; - - setSession: (value: XRSession | null) => Promise; - - getEnvironmentBlendMode: () => XREnvironmentBlendMode | undefined; - - getDepthTexture: () => Texture | null; - - updateCamera: (camera: PerspectiveCamera) => void; - - getCamera: () => WebXRArrayCamera; - - getFoveation: () => number | undefined; - - setFoveation: (value: number) => void; - - hasDepthSensing: () => boolean; - - getDepthSensingMesh: () => Mesh | null; - - setAnimationLoop: (callback: XRFrameRequestCallback | null) => void; - - dispose: () => void; -} diff --git a/src-testing/src/scenes/Fog.d.ts b/src-testing/src/scenes/Fog.d.ts deleted file mode 100644 index fc0f18019..000000000 --- a/src-testing/src/scenes/Fog.d.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { Color, ColorRepresentation } from "../math/Color.js"; - -export interface FogJSON { - type: string; - name: string; - color: number; - near: number; - far: number; -} - -/** - * This class contains the parameters that define linear fog, i.e., that grows linearly denser with the distance. - * @example - * ```typescript - * const scene = new THREE.Scene(); - * scene.fog = new THREE.Fog(0xcccccc, 10, 15); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/scenes/Fog | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/scenes/Fog.js | Source} - */ -export class Fog { - /** - * The color parameter is passed to the {@link THREE.Color | Color} constructor to set the color property - * @remarks - * Color can be a hexadecimal integer or a CSS-style string. - * @param color - * @param near Expects a `Float` - * @param far Expects a `Float` - */ - constructor(color: ColorRepresentation, near?: number, far?: number); - - /** - * Read-only flag to check if a given object is of type {@link Fog}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isFog: true; - - /** - * Optional name of the object - * @remarks _(doesn't need to be unique)_. - * @defaultValue `""` - */ - name: string; - - /** - * Fog color. - * @remarks If set to black, far away objects will be rendered black. - */ - color: Color; - - /** - * The minimum distance to start applying fog. - * @remarks Objects that are less than **near** units from the active camera won't be affected by fog. - * @defaultValue `1` - * @remarks Expects a `Float` - */ - near: number; - - /** - * The maximum distance at which fog stops being calculated and applied. - * @remarks Objects that are more than **far** units away from the active camera won't be affected by fog. - * @defaultValue `1000` - * @remarks Expects a `Float` - */ - far: number; - - /** - * Returns a new {@link Fog} instance with the same parameters as this one. - */ - clone(): Fog; - - /** - * Return {@link Fog} data in JSON format. - */ - toJSON(): FogJSON; -} diff --git a/src-testing/src/scenes/FogExp2.d.ts b/src-testing/src/scenes/FogExp2.d.ts deleted file mode 100644 index af00981e6..000000000 --- a/src-testing/src/scenes/FogExp2.d.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { Color, ColorRepresentation } from "../math/Color.js"; - -export interface FogExp2JSON { - type: string; - name: string; - color: number; - density: number; -} - -/** - * This class contains the parameters that define exponential squared fog, which gives a clear view near the camera and a faster than exponentially densening fog farther from the camera. - * @example - * ```typescript - * const scene = new THREE.Scene(); - * scene.fog = new THREE.FogExp2(0xcccccc, 0.002); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_geometry_terrain | webgl geometry terrain} - * @see {@link https://threejs.org/docs/index.html#api/en/scenes/FogExp2 | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/scenes/FogExp2.js | Source} - */ -export class FogExp2 { - /** - * The color parameter is passed to the {@link THREE.Color | Color} constructor to set the color property - * @remarks Color can be a hexadecimal integer or a CSS-style string. - * @param color - * @param density Expects a `Float` - */ - constructor(color: ColorRepresentation, density?: number); - - /** - * Read-only flag to check if a given object is of type {@link FogExp2}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isFogExp2: true; - - /** - * Optional name of the object - * @remarks _(doesn't need to be unique)_. - * @defaultValue `""` - */ - name: string; - - /** - * Fog color. - * @remarks If set to black, far away objects will be rendered black. - */ - color: Color; - - /** - * Defines how fast the fog will grow dense. - * @defaultValue `0.00025` - * @remarks Expects a `Float` - */ - density: number; - - /** - * Returns a new {@link FogExp2} instance with the same parameters as this one. - */ - clone(): FogExp2; - - /** - * Return {@link FogExp2} data in JSON format. - */ - toJSON(): FogExp2JSON; -} diff --git a/src-testing/src/scenes/Scene.d.ts b/src-testing/src/scenes/Scene.d.ts deleted file mode 100644 index c2f43afd7..000000000 --- a/src-testing/src/scenes/Scene.d.ts +++ /dev/null @@ -1,118 +0,0 @@ -import { JSONMeta, Object3D, Object3DJSON, Object3DJSONObject } from "../core/Object3D.js"; -import { Material } from "../materials/Material.js"; -import { Color } from "../math/Color.js"; -import { Euler, EulerTuple } from "../math/Euler.js"; -import { CubeTexture } from "../textures/CubeTexture.js"; -import { Texture } from "../textures/Texture.js"; -import { Fog, FogJSON } from "./Fog.js"; -import { FogExp2, FogExp2JSON } from "./FogExp2.js"; - -export interface SceneJSONObject extends Object3DJSONObject { - fog?: FogJSON | FogExp2JSON; - - backgroundBlurriness?: number; - backgroundIntensity?: number; - backgroundRotation: EulerTuple; - - environmentIntensity?: number; - environmentRotation: EulerTuple; -} - -export interface SceneJSON extends Object3DJSON { - object: SceneJSONObject; -} - -/** - * Scenes allow you to set up what and where is to be rendered by three.js - * @remarks - * This is where you place objects, lights and cameras. - * @see Example: {@link https://threejs.org/examples/#webgl_multiple_scenes_comparison | webgl multiple scenes comparison} - * @see {@link https://threejs.org/docs/index.html#manual/en/introduction/Creating-a-scene | Manual: Creating a scene} - * @see {@link https://threejs.org/docs/index.html#api/en/scenes/Scene | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/scenes/Scene.js | Source} - */ -export class Scene extends Object3D { - /** - * Create a new {@link Scene} object. - */ - constructor(); - - /** - * Read-only flag to check if a given object is of type {@link Scene}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isScene: true; - - /** - * @defaultValue `Scene` - */ - type: "Scene"; - - /** - * A {@link Fog | fog} instance defining the type of fog that affects everything rendered in the scene. - * @defaultValue `null` - */ - fog: Fog | FogExp2 | null; - - /** - * Sets the blurriness of the background. Only influences environment maps assigned to {@link THREE.Scene.background | Scene.background}. - * @defaultValue `0` - * @remarks Expects a `Float` between `0` and `1`. - */ - backgroundBlurriness: number; - - /** - * Attenuates the color of the background. Only applies to background textures. - * @defaultValue `1` - * @remarks Expects a `Float` - */ - backgroundIntensity: number; - - /** - * Forces everything in the {@link Scene} to be rendered with the defined material. - * @defaultValue `null` - */ - overrideMaterial: Material | null; - - /** - * Defines the background of the scene. - * @remarks Valid inputs are: - * - A {@link THREE.Color | Color} for defining a uniform colored background. - * - A {@link THREE.Texture | Texture} for defining a (flat) textured background. - * - Texture cubes ({@link THREE.CubeTexture | CubeTexture}) or equirectangular textures for defining a skybox. - * @defaultValue `null` - */ - background: Color | Texture | CubeTexture | null; - - /** - * The rotation of the background in radians. Only influences environment maps assigned to {@link .background}. - * Default is `(0,0,0)`. - */ - backgroundRotation: Euler; - - /** - * Sets the environment map for all physical materials in the scene. - * However, it's not possible to overwrite an existing texture assigned to {@link THREE.MeshStandardMaterial.envMap | MeshStandardMaterial.envMap}. - * @defaultValue `null` - */ - environment: Texture | null; - - /** - * Attenuates the color of the environment. Only influences environment maps assigned to {@link Scene.environment}. - * @default 1 - */ - environmentIntensity: number; - - /** - * The rotation of the environment map in radians. Only influences physical materials in the scene when - * {@link .environment} is used. Default is `(0,0,0)`. - */ - environmentRotation: Euler; - - /** - * Convert the {@link Scene} to three.js {@link https://github.com/mrdoob/three.js/wiki/JSON-Object-Scene-format-4 | JSON Object/Scene format}. - * @param meta Object containing metadata such as textures or images for the scene. - */ - toJSON(meta?: JSONMeta): SceneJSON; -} diff --git a/src-testing/src/textures/CanvasTexture.d.ts b/src-testing/src/textures/CanvasTexture.d.ts deleted file mode 100644 index 6445338fa..000000000 --- a/src-testing/src/textures/CanvasTexture.d.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { - MagnificationTextureFilter, - Mapping, - MinificationTextureFilter, - PixelFormat, - TextureDataType, - Wrapping, -} from "../constants.js"; -import { OffscreenCanvas, Texture } from "./Texture.js"; - -/** - * Creates a texture from a {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas | canvas element}. - * @remarks - * This is almost the same as the base {@link Texture | Texture} class, - * except that it sets {@link Texture.needsUpdate | needsUpdate} to `true` immediately. - * @see {@link THREE.Texture | Texture} - * @see {@link https://threejs.org/docs/index.html#api/en/textures/CanvasTexture | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/CanvasTexture.js | Source} - */ -export class CanvasTexture extends Texture { - /** - * This creates a new {@link THREE.CanvasTexture | CanvasTexture} object. - * @param canvas The HTML canvas element from which to load the texture. - * @param mapping See {@link Texture.mapping | .mapping}. Default {@link THREE.Texture.DEFAULT_MAPPING} - * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping} - * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping} - * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.LinearFilter} - * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.LinearMipmapLinearFilter} - * @param format See {@link Texture.format | .format}. Default {@link THREE.RGBAFormat} - * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} - * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY} - */ - constructor( - canvas: TexImageSource | OffscreenCanvas, - mapping?: Mapping, - wrapS?: Wrapping, - wrapT?: Wrapping, - magFilter?: MagnificationTextureFilter, - minFilter?: MinificationTextureFilter, - format?: PixelFormat, - type?: TextureDataType, - anisotropy?: number, - ); - - /** - * Read-only flag to check if a given object is of type {@link CanvasTexture}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isCanvasTexture: true; -} diff --git a/src-testing/src/textures/CompressedArrayTexture.d.ts b/src-testing/src/textures/CompressedArrayTexture.d.ts deleted file mode 100644 index e46c3d478..000000000 --- a/src-testing/src/textures/CompressedArrayTexture.d.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { CompressedPixelFormat, TextureDataType, Wrapping } from "../constants.js"; -import { CompressedTexture, CompressedTextureMipmap } from "./CompressedTexture.js"; - -/** - * Creates an texture 2D array based on data in compressed form, for example from a - * {@link https://en.wikipedia.org/wiki/DirectDraw_Surface | DDS} file. - * @remarks For use with the {@link THREE.CompressedTextureLoader | CompressedTextureLoader}. - * @see {@link https://threejs.org/docs/index.html#api/en/textures/CompressedArrayTexture | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/CompressedArrayTexture.js | Source} - */ -export class CompressedArrayTexture extends CompressedTexture { - /** - * Read-only flag to check if a given object is of type {@link CompressedArrayTexture}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isCompressedArrayTexture: true; - - /** - * Overridden with a object containing width and height. - * @override - */ - get image(): { width: number; height: number; depth: number }; - set image(value: { width: number; height: number; depth: number }); - - /** - * This defines how the texture is wrapped in the depth direction. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - * @defaultValue {@link THREE.ClampToEdgeWrapping} - */ - wrapR: Wrapping; - - /** - * A set of all layers which need to be updated in the texture. See {@link CompressedArrayTexture.addLayerUpdate}. - */ - layerUpdates: Set; - - /** - * Create a new instance of {@link CompressedArrayTexture} - * @param mipmaps The mipmaps array should contain objects with data, width and height. The mipmaps should be of the - * correct format and type. - * @param width The width of the biggest mipmap. - * @param height The height of the biggest mipmap. - * @param depth The number of layers of the 2D array texture - * @param format The format used in the mipmaps. See {@link THREE.CompressedPixelFormat}. - * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} - */ - constructor( - mipmaps: CompressedTextureMipmap[], - width: number, - height: number, - depth: number, - format: CompressedPixelFormat, - type?: TextureDataType, - ); - - /** - * Describes that a specific layer of the texture needs to be updated. Normally when {@link Texture.needsUpdate} is - * set to true, the entire compressed texture array is sent to the GPU. Marking specific layers will only transmit - * subsets of all mipmaps associated with a specific depth in the array which is often much more performant. - */ - addLayerUpdate(layerIndex: number): void; - - /** - * Resets the layer updates registry. See {@link CompressedArrayTexture.addLayerUpdate}. - */ - clearLayoutUpdates(): void; -} diff --git a/src-testing/src/textures/CompressedCubeTexture.d.ts b/src-testing/src/textures/CompressedCubeTexture.d.ts deleted file mode 100644 index 9c72145a0..000000000 --- a/src-testing/src/textures/CompressedCubeTexture.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { CompressedPixelFormat, TextureDataType } from "../constants.js"; -import { CompressedTexture } from "./CompressedTexture.js"; - -export class CompressedCubeTexture extends CompressedTexture { - readonly isCompressedCubeTexture: true; - readonly isCubeTexture: true; - - constructor( - images: Array<{ width: number; height: number }>, - format?: CompressedPixelFormat, - type?: TextureDataType, - ); -} diff --git a/src-testing/src/textures/CompressedTexture.d.ts b/src-testing/src/textures/CompressedTexture.d.ts deleted file mode 100644 index 21f5427c7..000000000 --- a/src-testing/src/textures/CompressedTexture.d.ts +++ /dev/null @@ -1,95 +0,0 @@ -import { - ColorSpace, - CompressedPixelFormat, - MagnificationTextureFilter, - Mapping, - MinificationTextureFilter, - TextureDataType, - Wrapping, -} from "../constants.js"; -import { TypedArray } from "../core/BufferAttribute.js"; -import { Texture } from "./Texture.js"; - -export interface CompressedTextureMipmap { - data: TypedArray; - width: number; - height: number; -} - -/** - * Creates a texture based on data in compressed form, for example from a {@link https://en.wikipedia.org/wiki/DirectDraw_Surface | DDS} file. - * @remarks For use with the {@link THREE.CompressedTextureLoader | CompressedTextureLoader}. - * @see {@link https://threejs.org/docs/index.html#api/en/textures/CompressedTexture | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/CompressedTexture.js | Source} - */ -export class CompressedTexture extends Texture { - /** - * This creates a new {@link THREE.CompressedTexture | CompressedTexture} object. - * @param mipmaps The mipmaps array should contain objects with data, width and height. The mipmaps should be of the - * correct format and type. - * @param width The width of the biggest mipmap. - * @param height The height of the biggest mipmap. - * @param format The format used in the mipmaps. See {@link THREE.CompressedPixelFormat}. - * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} - * @param mapping See {@link Texture.mapping | .mapping}. Default {@link THREE.Texture.DEFAULT_MAPPING} - * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping} - * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping} - * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.LinearFilter} - * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.LinearMipmapLinearFilter} - * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY} - * @param colorSpace See {@link Texture.colorSpace .colorSpace}. Default {@link NoColorSpace} - */ - constructor( - mipmaps?: CompressedTextureMipmap[], - width?: number, - height?: number, - format?: CompressedPixelFormat, - type?: TextureDataType, - mapping?: Mapping, - wrapS?: Wrapping, - wrapT?: Wrapping, - magFilter?: MagnificationTextureFilter, - minFilter?: MinificationTextureFilter, - anisotropy?: number, - colorSpace?: ColorSpace, - ); - - /** - * Read-only flag to check if a given object is of type {@link CompressedTexture}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isCompressedTexture: true; - - /** - * Overridden with a object containing width and height. - * @override - */ - get image(): { width: number; height: number }; - set image(value: { width: number; height: number }); - - /** - * The mipmaps array should contain objects with data, width and height. The mipmaps should be of the correct - * format and type. - */ - mipmaps: CompressedTextureMipmap[] | undefined; - - /** - * @override - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - * @see {@link THREE.CompressedPixelFormat} - */ - format: CompressedPixelFormat; - - /** - * @override No flipping for cube textures. (also flipping doesn't work for compressed textures) - * @defaultValue `false` - */ - flipY: boolean; - - /** - * @override Can't generate mipmaps for compressed textures. mips must be embedded in DDS files - * @defaultValue `false` - */ - generateMipmaps: boolean; -} diff --git a/src-testing/src/textures/CubeTexture.d.ts b/src-testing/src/textures/CubeTexture.d.ts deleted file mode 100644 index 1a4def7dd..000000000 --- a/src-testing/src/textures/CubeTexture.d.ts +++ /dev/null @@ -1,90 +0,0 @@ -import { - ColorSpace, - CubeTextureMapping, - MagnificationTextureFilter, - MinificationTextureFilter, - PixelFormat, - TextureDataType, - Wrapping, -} from "../constants.js"; -import { Texture } from "./Texture.js"; - -/** - * Creates a cube texture made up of six images. - * @remarks - * {@link CubeTexture} is almost equivalent in functionality and usage to {@link Texture}. - * The only differences are that the images are an array of _6_ images as opposed to a single image, - * and the mapping options are {@link THREE.CubeReflectionMapping} (default) or {@link THREE.CubeRefractionMapping} - * @example - * ```typescript - * const loader = new THREE.CubeTextureLoader(); - * loader.setPath('textures/cube/pisa/'); - * const textureCube = loader.load(['px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png']); - * const material = new THREE.MeshBasicMaterial({ - * color: 0xffffff, - * envMap: textureCube - * }); - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/textures/CubeTexture | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/CubeTexture.js | Source} - */ -export class CubeTexture extends Texture { - /** - * This creates a new {@link THREE.CubeTexture | CubeTexture} object. - * @param images - * @param mapping See {@link CubeTexture.mapping | .mapping}. Default {@link THREE.CubeReflectionMapping} - * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping} - * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping} - * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.LinearFilter} - * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.LinearMipmapLinearFilter} - * @param format See {@link Texture.format | .format}. Default {@link THREE.RGBAFormat} - * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} - * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY} - * @param colorSpace See {@link Texture.colorSpace | .colorSpace}. Default {@link NoColorSpace} - */ - constructor( - images?: any[], // HTMLImageElement or HTMLCanvasElement - mapping?: CubeTextureMapping, - wrapS?: Wrapping, - wrapT?: Wrapping, - magFilter?: MagnificationTextureFilter, - minFilter?: MinificationTextureFilter, - format?: PixelFormat, - type?: TextureDataType, - anisotropy?: number, - colorSpace?: ColorSpace, - ); - - /** - * Read-only flag to check if a given object is of type {@link CubeTexture}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isCubeTexture: true; - - /** - * An image object, typically created using the {@link THREE.CubeTextureLoader.load | CubeTextureLoader.load()} method. - * @see {@link Texture.image} - */ - get image(): any; - set image(data: any); - - /** - * An image object, typically created using the {@link THREE.CubeTextureLoader.load | CubeTextureLoader.load()} method. - * @see {@link Texture.image} - */ - get images(): any; - set images(data: any); - - /** - * @inheritDoc - * @defaultValue {@link THREE.CubeReflectionMapping} - */ - mapping: CubeTextureMapping; - - /** - * @inheritDoc - * @defaultValue `false` - */ - flipY: boolean; -} diff --git a/src-testing/src/textures/Data3DTexture.d.ts b/src-testing/src/textures/Data3DTexture.d.ts deleted file mode 100644 index 9e5986eed..000000000 --- a/src-testing/src/textures/Data3DTexture.d.ts +++ /dev/null @@ -1,96 +0,0 @@ -import { MagnificationTextureFilter, MinificationTextureFilter, Wrapping } from "../constants.js"; -import { Texture } from "./Texture.js"; -import { Texture3DImageData } from "./types.js"; - -/** - * Creates a three-dimensional texture from raw data, with parameters to divide it into width, height, and depth - * @example - * ```typescript - * This creates a[name] with repeating data, 0 to 255 - * // create a buffer with some data - * const sizeX = 64; - * const sizeY = 64; - * const sizeZ = 64; - * const data = new Uint8Array(sizeX * sizeY * sizeZ); - * let i = 0; - * for (let z = 0; z & lt; sizeZ; z++) { - * for (let y = 0; y & lt; sizeY; y++) { - * for (let x = 0; x & lt; sizeX; x++) { - * data[i] = i % 256; - * i++; - * } - * } - * } - * // use the buffer to create the texture - * const texture = new THREE.Data3DTexture(data, sizeX, sizeY, sizeZ); - * texture.needsUpdate = true; - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl2_materials_texture3d | WebGL2 / materials / texture3d} - * @see Example: {@link https://threejs.org/examples/#webgl2_materials_texture3d_partialupdate | WebGL2 / materials / texture3d / partialupdate} - * @see Example: {@link https://threejs.org/examples/#webgl2_volume_cloud | WebGL2 / volume / cloud} - * @see Example: {@link https://threejs.org/examples/#webgl2_volume_perlin | WebGL2 / volume / perlin} - * @see {@link https://threejs.org/docs/index.html#api/en/textures/Data3DTexture | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/Data3DTexture.js | Source} - */ -export class Data3DTexture extends Texture { - /** - * Create a new instance of {@link Data3DTexture} - * @param data {@link https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView | ArrayBufferView} of the texture. Default `null`. - * @param width Width of the texture. Default `1`. - * @param height Height of the texture. Default `1`. - * @param depth Depth of the texture. Default `1`. - */ - constructor(data?: BufferSource | null, width?: number, height?: number, depth?: number); - - /** - * Read-only flag to check if a given object is of type {@link Data3DTexture}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isData3DTexture: true; - - /** - * Overridden with a record type holding data, width and height and depth. - * @override - */ - get image(): Texture3DImageData; - set image(data: Texture3DImageData); - - /** - * @override - * @defaultValue {@link THREE.NearestFilter} - */ - magFilter: MagnificationTextureFilter; - - /** - * @override - * @defaultValue {@link THREE.NearestFilter} - */ - minFilter: MinificationTextureFilter; - - /** - * @override - * @defaultValue {@link THREE.ClampToEdgeWrapping} - */ - wrapR: Wrapping; - - /** - * @override - * @defaultValue `false` - */ - flipY: boolean; - - /** - * @override - * @defaultValue `false` - */ - generateMipmaps: boolean; - - /** - * @override - * @defaultValue `1` - */ - unpackAlignment: number; -} - -export {}; diff --git a/src-testing/src/textures/DataArrayTexture.d.ts b/src-testing/src/textures/DataArrayTexture.d.ts deleted file mode 100644 index d3a82c1e8..000000000 --- a/src-testing/src/textures/DataArrayTexture.d.ts +++ /dev/null @@ -1,123 +0,0 @@ -import { MagnificationTextureFilter, MinificationTextureFilter } from "../constants.js"; -import { Texture } from "./Texture.js"; -import { Texture3DImageData } from "./types.js"; - -/** - * Creates an array of textures directly from raw data, width and height and depth - * @example - * ```typescript - * This creates a[name] where each texture has a different color. - * // create a buffer with color data - * const width = 512; - * const height = 512; - * const depth = 100; - * const size = width * height; - * const data = new Uint8Array(4 * size * depth); - * for (let i = 0; i & lt; depth; i++) { - * const color = new THREE.Color(Math.random(), Math.random(), Math.random()); - * const r = Math.floor(color.r * 255); - * const g = Math.floor(color.g * 255); - * const b = Math.floor(color.b * 255); - * for (let j = 0; j & lt; size; j++) { - * const stride = (i * size + j) * 4; - * data[stride] = r; - * data[stride + 1] = g; - * data[stride + 2] = b; - * data[stride + 3] = 255; - * } - * } - * // used the buffer to create a [name] - * const texture = new THREE.DataArrayTexture(data, width, height, depth); - * texture.needsUpdate = true; - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl2_materials_texture2darray | WebGL2 / materials / texture2darray} - * @see Example: {@link https://threejs.org/examples/#webgl2_rendertarget_texture2darray | WebGL2 / rendertarget / texture2darray} - * @see {@link https://threejs.org/docs/index.html#api/en/textures/DataArrayTexture | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/DataArrayTexture.js | Source} - */ -export class DataArrayTexture extends Texture { - /** - * Read-only flag to check if a given object is of type {@link DataArrayTexture}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isDataArrayTexture: true; - - /** - * Overridden with a record type holding data, width and height and depth. - * @override - */ - get image(): Texture3DImageData; - set image(data: Texture3DImageData); - - /** - * @override - * @defaultValue {@link THREE.NearestFilter} - */ - magFilter: MagnificationTextureFilter; - - /** - * @override - * @defaultValue {@link THREE.NearestFilter} - */ - minFilter: MinificationTextureFilter; - - /** - * @override - * @defaultValue {@link THREE.ClampToEdgeWrapping} - */ - wrapR: boolean; - - /** - * @override - * @defaultValue `false` - */ - generateMipmaps: boolean; - - /** - * @override - * @defaultValue `false` - */ - flipY: boolean; - - /** - * @override - * @defaultValue `1` - */ - unpackAlignment: number; - - /** - * A set of all layers which need to be updated in the texture. See {@link DataArrayTexture.addLayerUpdate}. - */ - layerUpdates: Set; - - /** - * This creates a new {@link THREE.DataArrayTexture | DataArrayTexture} object. - * @remarks The interpretation of the data depends on {@link format} and {@link type}. - * @remarks If the {@link type} is {@link THREE.UnsignedByteType}, a {@link Uint8Array} will be useful for addressing the texel data - * @remarks If the {@link format} is {@link THREE.RGBAFormat}, data needs four values for one texel; Red, Green, Blue and Alpha (typically the opacity). - * @remarks For the packed {@link type | types}, {@link THREE.UnsignedShort4444Type} and {@link THREE.UnsignedShort5551Type} - * all color components of one texel can be addressed as bitfields within an integer element of a {@link Uint16Array}. - * @remarks In order to use the {@link type | types} {@link THREE.FloatType} and {@link THREE.HalfFloatType}, - * the WebGL implementation must support the respective extensions _OES_texture_float_ and _OES_texture_half_float_ - * @remarks In order to use {@link THREE.LinearFilter} for component-wise, bilinear interpolation of the texels based on these types, - * the WebGL extensions _OES_texture_float_linear_ or _OES_texture_half_float_linear_ must also be present. - * @param data {@link https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView | ArrayBufferView} of the texture. Default `null`. - * @param width Width of the texture. Default `1`. - * @param height Height of the texture. Default `1`. - * @param depth Depth of the texture. Default `1`. - */ - constructor(data?: BufferSource | null, width?: number, height?: number, depth?: number); - - /** - * Describes that a specific layer of the texture needs to be updated. Normally when {@link Texture.needsUpdate} is - * set to true, the entire compressed texture array is sent to the GPU. Marking specific layers will only transmit - * subsets of all mipmaps associated with a specific depth in the array which is often much more performant. - */ - addLayerUpdate(layerIndex: number): void; - - /** - * Resets the layer updates registry. See {@link DataArrayTexture.addLayerUpdate}. - */ - clearLayoutUpdates(): void; -} diff --git a/src-testing/src/textures/DataTexture.d.ts b/src-testing/src/textures/DataTexture.d.ts deleted file mode 100644 index e679414cf..000000000 --- a/src-testing/src/textures/DataTexture.d.ts +++ /dev/null @@ -1,113 +0,0 @@ -import { - ColorSpace, - MagnificationTextureFilter, - Mapping, - MinificationTextureFilter, - PixelFormat, - TextureDataType, - Wrapping, -} from "../constants.js"; -import { Texture } from "./Texture.js"; -import { TextureImageData } from "./types.js"; - -/** - * Creates a texture directly from raw data, width and height. - * @example - * ```typescript - * // create a buffer with color data - * const width = 512; - * const height = 512; - * const size = width * height; - * const data = new Uint8Array(4 * size); - * const color = new THREE.Color(0xffffff); - * const r = Math.floor(color.r * 255); - * const g = Math.floor(color.g * 255); - * const b = Math.floor(color.b * 255); - * for (let i = 0; i & lt; size; i++) { - * const stride = i * 4; - * data[stride] = r; - * data[stride + 1] = g; - * data[stride + 2] = b; - * data[stride + 3] = 255; - * } - * // used the buffer to create a [name] - * const texture = new THREE.DataTexture(data, width, height); - * texture.needsUpdate = true; - * ``` - * @see {@link https://threejs.org/docs/index.html#api/en/textures/DataTexture | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/DataTexture.js | Source} - */ -export class DataTexture extends Texture { - /** - * @param data {@link https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView | ArrayBufferView} of the texture. Default `null`. - * @param width Width of the texture. Default `1`. - * @param height Height of the texture. Default `1`. - * @param format See {@link Texture.format | .format}. Default {@link THREE.RGBAFormat} - * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} - * @param mapping See {@link Texture.mapping | .mapping}. Default {@link THREE.Texture.DEFAULT_MAPPING} - * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping} - * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping} - * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.NearestFilter} - * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.NearestFilter} - * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY} - * @param colorSpace See {@link Texture.colorSpace | .colorSpace}. Default {@link NoColorSpace} - */ - constructor( - data?: BufferSource | null, - width?: number, - height?: number, - format?: PixelFormat, - type?: TextureDataType, - mapping?: Mapping, - wrapS?: Wrapping, - wrapT?: Wrapping, - magFilter?: MagnificationTextureFilter, - minFilter?: MinificationTextureFilter, - anisotropy?: number, - colorSpace?: ColorSpace, - ); - - /** - * Read-only flag to check if a given object is of type {@link DataTexture}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isDataTexture: true; - - /** - * Overridden with a record type holding data, width and height and depth. - * @override - */ - get image(): TextureImageData; - set image(value: TextureImageData); - - /** - * @override - * @defaultValue {@link THREE.NearestFilter} - */ - magFilter: MagnificationTextureFilter; - - /** - * @override - * @defaultValue {@link THREE.NearestFilter} - */ - minFilter: MinificationTextureFilter; - - /** - * @override - * @defaultValue `false` - */ - flipY: boolean; - - /** - * @override - * @defaultValue `false` - */ - generateMipmaps: boolean; - - /** - * @override - * @defaultValue `1` - */ - unpackAlignment: number; -} diff --git a/src-testing/src/textures/DepthTexture.d.ts b/src-testing/src/textures/DepthTexture.d.ts deleted file mode 100644 index f524e91cc..000000000 --- a/src-testing/src/textures/DepthTexture.d.ts +++ /dev/null @@ -1,104 +0,0 @@ -import { - DepthTexturePixelFormat, - MagnificationTextureFilter, - Mapping, - MinificationTextureFilter, - TextureComparisonFunction, - TextureDataType, - Wrapping, -} from "../constants.js"; -import { Texture } from "./Texture.js"; - -/** - * This class can be used to automatically save the depth information of a rendering into a texture - * @see Example: {@link https://threejs.org/examples/#webgl_depth_texture | depth / texture} - * @see {@link https://threejs.org/docs/index.html#api/en/textures/DepthTexture | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/DepthTexture.js | Source} - */ -export class DepthTexture extends Texture { - /** - * Create a new instance of {@link DepthTexture} - * @param width Width of the texture. - * @param height Height of the texture. - * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} or {@link THREE.UnsignedInt248Type} - * @param mapping See {@link Texture.mapping | .mapping}. Default {@link THREE.Texture.DEFAULT_MAPPING} - * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping} - * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping} - * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.NearestFilter} - * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.NearestFilter} - * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY} - * @param format See {@link DepthTexture.format | .format}. Default {@link THREE.DepthFormat} - */ - constructor( - width: number, - height: number, - type?: TextureDataType, - mapping?: Mapping, - wrapS?: Wrapping, - wrapT?: Wrapping, - magFilter?: MagnificationTextureFilter, - minFilter?: MinificationTextureFilter, - anisotropy?: number, - format?: DepthTexturePixelFormat, - ); - - /** - * Read-only flag to check if a given object is of type {@link DepthTexture}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isDepthTexture: true; - - /** - * Overridden with a record type holding width and height. - * @override - */ - get image(): { width: number; height: number }; - set image(value: { width: number; height: number }); - - /** - * @override - * @defaultValue `false` - */ - flipY: boolean; - - /** - * @override - * @defaultValue {@link THREE.NearestFilter} - */ - magFilter: MagnificationTextureFilter; - - /** - * @override - * @defaultValue {@link THREE.NearestFilter} - */ - minFilter: MinificationTextureFilter; - - /** - * @override Depth textures do not use mipmaps. - * @defaultValue `false` - */ - generateMipmaps: boolean; - - /** - * @override - * @see {@link Texture.format | Texture.format} - * @defaultValue {@link THREE.DepthFormat}. - */ - format: DepthTexturePixelFormat; - - /** - * @override - * @defaultValue {@link THREE.UnsignedByteType} when {@link format | .format} === {@link THREE.DepthFormat} - * @defaultValue {@link THREE.UnsignedInt248Type} when {@link format | .format} === {@link THREE.DepthStencilFormat} - */ - type: TextureDataType; - - /** - * This is used to define the comparison function used when comparing texels in the depth texture to the value in - * the depth buffer. Default is `null` which means comparison is disabled. - * - * See {@link THREE.TextureComparisonFunction} for functions. - */ - compareFunction: TextureComparisonFunction | null; -} diff --git a/src-testing/src/textures/FramebufferTexture.d.ts b/src-testing/src/textures/FramebufferTexture.d.ts deleted file mode 100644 index ad54c5175..000000000 --- a/src-testing/src/textures/FramebufferTexture.d.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { MagnificationTextureFilter, MinificationTextureFilter } from "../constants.js"; -import { Texture } from "./Texture.js"; - -/** - * This class can only be used in combination with {@link THREE.WebGLRenderer.copyFramebufferToTexture | WebGLRenderer.copyFramebufferToTexture()}. - * @example - * ```typescript - * const pixelRatio = window.devicePixelRatio; - * const textureSize = 128 * pixelRatio; - * - * // instantiate a framebuffer texture - * const frameTexture = new FramebufferTexture( textureSize, textureSize, RGBAFormat ); - * - * // calculate start position for copying part of the frame data - * const vector = new Vector2(); - * vector.x = ( window.innerWidth * pixelRatio / 2 ) - ( textureSize / 2 ); - * vector.y = ( window.innerHeight * pixelRatio / 2 ) - ( textureSize / 2 ); - * - * // render the scene - * renderer.clear(); - * renderer.render( scene, camera ); - * - * // copy part of the rendered frame into the framebuffer texture - * renderer.copyFramebufferToTexture( frameTexture, vector ); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_framebuffer_texture | webgl_framebuffer_texture} - * @see {@link https://threejs.org/docs/index.html#api/en/textures/FramebufferTexture | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/FramebufferTexture.js | Source} - */ -export class FramebufferTexture extends Texture { - /** - * Create a new instance of {@link FramebufferTexture} - * @param width The width of the texture. - * @param height The height of the texture. - */ - constructor(width: number, height: number); - - /** - * Read-only flag to check if a given object is of type {@link FramebufferTexture}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isFramebufferTexture: true; - - /** - * @override - * @defaultValue {@link THREE.NearestFilter} - */ - magFilter: MagnificationTextureFilter; - - /** - * @override - * @defaultValue {@link THREE.NearestFilter} - */ - minFilter: MinificationTextureFilter; - - /** - * @override - * @defaultValue `false` - */ - generateMipmaps: boolean; -} diff --git a/src-testing/src/textures/Source.d.ts b/src-testing/src/textures/Source.d.ts deleted file mode 100644 index 404d1d8a1..000000000 --- a/src-testing/src/textures/Source.d.ts +++ /dev/null @@ -1,75 +0,0 @@ -export type SerializedImage = - | string - | { - data: number[]; - width: number; - height: number; - type: string; - }; - -export class SourceJSON { - uuid: string; - url: SerializedImage | SerializedImage[]; -} - -/** - * Represents the data {@link Source} of a texture. - * @see {@link https://threejs.org/docs/index.html#api/en/textures/Source | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/Source.js | Source} - */ -export class Source { - /** - * Create a new instance of {@link Source} - * @param data The data definition of a texture. Default `null` - */ - constructor(data: any); - - /** - * Flag to check if a given object is of type {@link Source}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isSource: true; - - readonly id: number; - - /** - * The actual data of a texture. - * @remarks The type of this property depends on the texture that uses this instance. - */ - data: any; - - /** - * This property is only relevant when {@link .needsUpdate} is set to `true` and provides more control on how - * texture data should be processed. - * When `dataReady` is set to `false`, the engine performs the memory allocation (if necessary) but does not - * transfer the data into the GPU memory. - * @default true - */ - dataReady: boolean; - - /** - * When the property is set to `true`, the engine allocates the memory for the texture (if necessary) and triggers - * the actual texture upload to the GPU next time the source is used. - */ - set needsUpdate(value: boolean); - - /** - * {@link http://en.wikipedia.org/wiki/Universally_unique_identifier | UUID} of this object instance. - * @remarks This gets automatically assigned and shouldn't be edited. - */ - uuid: string; - - /** - * This starts at `0` and counts how many times {@link needsUpdate | .needsUpdate} is set to `true`. - * @remarks Expects a `Integer` - * @defaultValue `0` - */ - version: number; - - /** - * Convert the data {@link Source} to three.js {@link https://github.com/mrdoob/three.js/wiki/JSON-Object-Scene-format-4 | JSON Object/Scene format}. - * @param meta Optional object containing metadata. - */ - toJSON(meta?: string | {}): SourceJSON; -} diff --git a/src-testing/src/textures/Texture.d.ts b/src-testing/src/textures/Texture.d.ts deleted file mode 100644 index 48a178966..000000000 --- a/src-testing/src/textures/Texture.d.ts +++ /dev/null @@ -1,477 +0,0 @@ -import { - AnyMapping, - AnyPixelFormat, - ColorSpace, - MagnificationTextureFilter, - Mapping, - MinificationTextureFilter, - PixelFormat, - PixelFormatGPU, - TextureDataType, - Wrapping, -} from "../constants.js"; -import { EventDispatcher } from "../core/EventDispatcher.js"; -import { Matrix3 } from "../math/Matrix3.js"; -import { Vector2 } from "../math/Vector2.js"; -import { CompressedTextureMipmap } from "./CompressedTexture.js"; -import { CubeTexture } from "./CubeTexture.js"; -import { Source } from "./Source.js"; - -export interface TextureJSON { - metadata: { version: number; type: string; generator: string }; - - uuid: string; - name: string; - - image: string; - - mapping: AnyMapping; - channel: number; - - repeat: [x: number, y: number]; - offset: [x: number, y: number]; - center: [x: number, y: number]; - rotation: number; - - wrap: [wrapS: number, wrapT: number]; - - format: AnyPixelFormat; - internalFormat: PixelFormatGPU | null; - type: TextureDataType; - colorSpace: ColorSpace; - - minFilter: MinificationTextureFilter; - magFilter: MagnificationTextureFilter; - anisotropy: number; - - flipY: boolean; - - generateMipmaps: boolean; - premultiplyAlpha: boolean; - unpackAlignment: number; - - userData?: Record; -} - -/** Shim for OffscreenCanvas. */ -// eslint-disable-next-line @typescript-eslint/no-empty-interface -export interface OffscreenCanvas extends EventTarget {} - -/** - * Create a {@link Texture} to apply to a surface or as a reflection or refraction map. - * @remarks - * After the initial use of a texture, its **dimensions**, {@link format}, and {@link type} cannot be changed - * Instead, call {@link dispose | .dispose()} on the {@link Texture} and instantiate a new {@link Texture}. - * @example - * ```typescript - * // load a texture, set wrap mode to repeat - * const texture = new THREE.TextureLoader().load("textures/water.jpg"); - * texture.wrapS = THREE.RepeatWrapping; - * texture.wrapT = THREE.RepeatWrapping; - * texture.repeat.set(4, 4); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_materials_texture_filters | webgl materials texture filters} - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - * @see {@link https://threejs.org/docs/index.html#api/en/textures/Texture | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/Textures/Texture.js | Source} - */ -export class Texture extends EventDispatcher<{ dispose: {} }> { - /** - * This creates a new {@link THREE.Texture | Texture} object. - * @param image See {@link Texture.image | .image}. Default {@link THREE.Texture.DEFAULT_IMAGE} - * @param mapping See {@link Texture.mapping | .mapping}. Default {@link THREE.Texture.DEFAULT_MAPPING} - * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping} - * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping} - * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.LinearFilter} - * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.LinearMipmapLinearFilter} - * @param format See {@link Texture.format | .format}. Default {@link THREE.RGBAFormat} - * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} - * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY} - * @param colorSpace See {@link Texture.colorSpace | .colorSpace}. Default {@link THREE.NoColorSpace} - */ - constructor( - image?: TexImageSource | OffscreenCanvas, - mapping?: Mapping, - wrapS?: Wrapping, - wrapT?: Wrapping, - magFilter?: MagnificationTextureFilter, - minFilter?: MinificationTextureFilter, - format?: PixelFormat, - type?: TextureDataType, - anisotropy?: number, - colorSpace?: ColorSpace, - ); - - /** - * @deprecated - */ - constructor( - image: TexImageSource | OffscreenCanvas, - mapping: Mapping, - wrapS: Wrapping, - wrapT: Wrapping, - magFilter: MagnificationTextureFilter, - minFilter: MinificationTextureFilter, - format: PixelFormat, - type: TextureDataType, - anisotropy: number, - ); - - /** - * Read-only flag to check if a given object is of type {@link Texture}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isTexture: true; - - /** - * Unique number for this {@link Texture} instance. - * @remarks Note that ids are assigned in chronological order: 1, 2, 3, ..., incrementing by one for each new object. - * @remarks Expects a `Integer` - */ - readonly id: number; - - /** - * {@link http://en.wikipedia.org/wiki/Universally_unique_identifier | UUID} of this object instance. - * @remarks This gets automatically assigned and shouldn't be edited. - */ - uuid: string; - - /** - * Optional name of the object - * @remarks _(doesn't need to be unique)_. - * @defaultValue `""` - */ - name: string; - - /** - * The data definition of a texture. A reference to the data source can be shared across textures. - * This is often useful in context of spritesheets where multiple textures render the same data - * but with different {@link Texture} transformations. - */ - source: Source; - - /** - * An image object, typically created using the {@link THREE.TextureLoader.load | TextureLoader.load()} method. - * @remarks This can be any image (e.g., PNG, JPG, GIF, DDS) or video (e.g., MP4, OGG/OGV) type supported by three.js. - * @remarks To use video as a {@link Texture} you need to have a playing HTML5 video element as a source - * for your {@link Texture} image and continuously update this {@link Texture} - * as long as video is playing - the {@link THREE.VideoTexture | VideoTexture} class handles this automatically. - */ - get image(): any; - set image(data: any); - - /** - * Array of user-specified mipmaps - * @defaultValue `[]` - */ - mipmaps: CompressedTextureMipmap[] | CubeTexture[] | HTMLCanvasElement[] | undefined; - - /** - * How the image is applied to the object. - * @remarks All {@link Texture} types except {@link THREE.CubeTexture} expect the _values_ be {@link THREE.Mapping} - * @remarks {@link CubeTexture} expect the _values_ be {@link THREE.CubeTextureMapping} - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - * @defaultValue _value of_ {@link THREE.Texture.DEFAULT_MAPPING} - */ - mapping: AnyMapping; - - /** - * Lets you select the uv attribute to map the texture to. `0` for `uv`, `1` for `uv1`, `2` for `uv2` and `3` for - * `uv3`. - */ - channel: number; - - /** - * This defines how the {@link Texture} is wrapped *horizontally* and corresponds to **U** in UV mapping. - * @remarks for **WEBGL1** - tiling of images in textures only functions if image dimensions are powers of two - * (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, ...) in terms of pixels. - * Individual dimensions need not be equal, but each must be a power of two. This is a limitation of WebGL1, not three.js. - * **WEBGL2** does not have this limitation. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - * @see {@link wrapT} - * @see {@link repeat} - * @defaultValue {@link THREE.ClampToEdgeWrapping} - */ - wrapS: Wrapping; - - /** - * This defines how the {@link Texture} is wrapped *vertically* and corresponds to **V** in UV mapping. - * @remarks for **WEBGL1** - tiling of images in textures only functions if image dimensions are powers of two - * (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, ...) in terms of pixels. - * Individual dimensions need not be equal, but each must be a power of two. This is a limitation of WebGL1, not three.js. - * **WEBGL2** does not have this limitation. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - * @see {@link wrapS} - * @see {@link repeat} - * @defaultValue {@link THREE.ClampToEdgeWrapping} - */ - wrapT: Wrapping; - - /** - * How the {@link Texture} is sampled when a texel covers more than one pixel. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - * @see {@link minFilter} - * @see {@link THREE.MagnificationTextureFilter} - * @defaultValue {@link THREE.LinearFilter} - */ - magFilter: MagnificationTextureFilter; - - /** - * How the {@link Texture} is sampled when a texel covers less than one pixel. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - * @see {@link magFilter} - * @see {@link THREE.MinificationTextureFilter} - * @defaultValue {@link THREE.LinearMipmapLinearFilter} - */ - minFilter: MinificationTextureFilter; - - /** - * The number of samples taken along the axis through the pixel that has the highest density of texels. - * @remarks A higher value gives a less blurry result than a basic mipmap, at the cost of more {@link Texture} samples being used. - * @remarks Use {@link THREE.WebGLCapabilities.getMaxAnisotropy() | renderer.capabilities.getMaxAnisotropy()} to find the maximum valid anisotropy value for the GPU; - * @remarks This value is usually a power of 2. - * @default _value of_ {@link THREE.Texture.DEFAULT_ANISOTROPY}. That is normally `1`. - */ - anisotropy: number; - - /** - * These define how elements of a 2D texture, or texels, are read by shaders. - * @remarks All {@link Texture} types except {@link THREE.DepthTexture} and {@link THREE.CompressedPixelFormat} expect the _values_ be {@link THREE.PixelFormat} - * @remarks {@link DepthTexture} expect the _values_ be {@link THREE.CubeTextureMapping} - * @remarks {@link CompressedPixelFormat} expect the _values_ be {@link THREE.CubeTextureMapping} - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - * @see {@link THREE.PixelFormat} - * @defaultValue {@link THREE.RGBAFormat}. - */ - format: AnyPixelFormat; - - /** - * This must correspond to the {@link Texture.format | .format}. - * @remarks {@link THREE.UnsignedByteType}, is the type most used by Texture formats. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - * @see {@link THREE.TextureDataType} - * @defaultValue {@link THREE.UnsignedByteType} - */ - type: TextureDataType; - - /** - * The GPU Pixel Format allows the developer to specify how the data is going to be stored on the GPU. - * @remarks Compatible only with {@link WebGL2RenderingContext | WebGL 2 Rendering Context}. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - * @defaultValue The default value is obtained using a combination of {@link Texture.format | .format} and {@link Texture.type | .type}. - */ - internalFormat: PixelFormatGPU | null; - - /** - * The uv-transform matrix for the texture. - * @remarks - * When {@link Texture.matrixAutoUpdate | .matrixAutoUpdate} property is `true`. - * Will be updated by the renderer from the properties: - * - {@link Texture.offset | .offset} - * - {@link Texture.repeat | .repeat} - * - {@link Texture.rotation | .rotation} - * - {@link Texture.center | .center} - * @remarks - * When {@link Texture.matrixAutoUpdate | .matrixAutoUpdate} property is `false`. - * This matrix may be set manually. - * @see {@link matrixAutoUpdate | .matrixAutoUpdate} - * @defaultValue `new THREE.Matrix3()` - */ - matrix: Matrix3; - - /** - * Whether is to update the texture's uv-transform {@link matrix | .matrix}. - * @remarks Set this to `false` if you are specifying the uv-transform {@link matrix} directly. - * @see {@link matrix | .matrix} - * @defaultValue `true` - */ - matrixAutoUpdate: boolean; - - /** - * How much a single repetition of the texture is offset from the beginning, in each direction **U** and **V**. - * @remarks Typical range is `0.0` to `1.0`. - * @defaultValue `new THREE.Vector2(0, 0)` - */ - offset: Vector2; - - /** - * How many times the texture is repeated across the surface, in each direction **U** and **V**. - * @remarks - * If repeat is set greater than `1` in either direction, the corresponding *Wrap* parameter should - * also be set to {@link THREE.RepeatWrapping} or {@link THREE.MirroredRepeatWrapping} to achieve the desired tiling effect. - * @see {@link wrapS} - * @see {@link wrapT} - * @defaultValue `new THREE.Vector2( 1, 1 )` - */ - repeat: Vector2; - - /** - * The point around which rotation occurs. - * @remarks A value of `(0.5, 0.5)` corresponds to the center of the texture. - * @defaultValue `new THREE.Vector2( 0, 0 )`, _lower left._ - */ - center: Vector2; - - /** - * How much the texture is rotated around the center point, in radians. - * @remarks Positive values are counter-clockwise. - * @defaultValue `0` - */ - rotation: number; - - /** - * Whether to generate mipmaps, _(if possible)_ for a texture. - * @remarks Set this to false if you are creating mipmaps manually. - * @defaultValue true - */ - generateMipmaps: boolean; - - /** - * If set to `true`, the alpha channel, if present, is multiplied into the color channels when the texture is uploaded to the GPU. - * @remarks - * Note that this property has no effect for {@link https://developer.mozilla.org/en-US/docs/Web/API/ImageBitmap | ImageBitmap}. - * You need to configure on bitmap creation instead. See {@link THREE.ImageBitmapLoader | ImageBitmapLoader}. - * @see {@link THREE.ImageBitmapLoader | ImageBitmapLoader}. - * @defaultValue `false` - */ - premultiplyAlpha: boolean; - - /** - * If set to `true`, the texture is flipped along the vertical axis when uploaded to the GPU. - * @remarks - * Note that this property has no effect for {@link https://developer.mozilla.org/en-US/docs/Web/API/ImageBitmap | ImageBitmap}. - * You need to configure on bitmap creation instead. See {@link THREE.ImageBitmapLoader | ImageBitmapLoader}. - * @see {@link THREE.ImageBitmapLoader | ImageBitmapLoader}. - * @defaultValue `true` - */ - flipY: boolean; - - /** - * Specifies the alignment requirements for the start of each pixel row in memory. - * @remarks - * The allowable values are: - * - `1` (byte-alignment) - * - `2` (rows aligned to even-numbered bytes) - * - `4` (word-alignment) - * - `8` (rows start on double-word boundaries). - * @see {@link http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml | glPixelStorei} for more information. - * @defaultValue `4` - */ - unpackAlignment: number; // TODO Fix typing to only allow the expected values. - - /** - * The {@link Textures | {@link Texture} constants} page for details of other color spaces. - * @remarks - * Textures containing color data should be annotated with {@link SRGBColorSpace THREE.SRGBColorSpace} or - * {@link LinearSRGBColorSpace THREE.LinearSRGBColorSpace}. - * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants} - * @see {@link THREE.TextureDataType} - * @defaultValue {@link THREE.NoColorSpace} - */ - colorSpace: ColorSpace; - - /** - * Indicates whether a texture belongs to a render target or not - * @defaultValue `false` - */ - isRenderTargetTexture: boolean; - - /** - * An object that can be used to store custom data about the texture. - * @remarks It should not hold references to functions as these will not be cloned. - * @defaultValue `{}` - */ - userData: Record; - - /** - * This starts at `0` and counts how many times {@link needsUpdate | .needsUpdate} is set to `true`. - * @remarks Expects a `Integer` - * @defaultValue `0` - */ - version: number; - - /** - * Indicates whether this texture should be processed by PMREMGenerator or not (only relevant for render target - * textures) - */ - pmremVersion: number; - - /** - * Set this to `true` to trigger an update next time the texture is used. Particularly important for setting the wrap mode. - */ - set needsUpdate(value: boolean); - - /** - * Indicates whether this texture should be processed by {@link THREE.PMREMGenerator} or not. - * @remarks Only relevant for render target textures. - * @defaultValue `false` - */ - set needsPMREMUpdate(value: boolean); - - /** - * The Global default value for {@link anisotropy | .anisotropy}. - * @defaultValue `1`. - */ - static DEFAULT_ANISOTROPY: number; - - /** - * The Global default value for {@link Texture.image | .image}. - * @defaultValue `null`. - */ - static DEFAULT_IMAGE: any; - - /** - * The Global default value for {@link mapping | .mapping}. - * @defaultValue {@link THREE.UVMapping} - */ - static DEFAULT_MAPPING: Mapping; - - /** - * A callback function, called when the texture is updated _(e.g., when needsUpdate has been set to true and then the texture is used)_. - */ - onUpdate: () => void; - - /** - * Transform the **UV** based on the value of this texture's - * {@link offset | .offset}, - * {@link repeat | .repeat}, - * {@link wrapS | .wrapS}, - * {@link wrapT | .wrapT} and - * {@link flipY | .flipY} properties. - * @param uv - */ - transformUv(uv: Vector2): Vector2; - - /** - * Update the texture's **UV-transform** {@link matrix | .matrix} from the texture properties - * {@link offset | .offset}, - * {@link repeat | .repeat}, - * {@link rotation | .rotation} and - * {@link center | .center}. - */ - updateMatrix(): void; - - /** - * Make copy of the texture - * @remarks Note this is not a **"deep copy"**, the image is shared - * @remarks - * Besides, cloning a texture does not automatically mark it for a texture upload - * You have to set {@link needsUpdate | .needsUpdate} to `true` as soon as it's image property (the data source) is fully loaded or ready. - */ - clone(): this; - - copy(source: Texture): this; - - /** - * Convert the texture to three.js {@link https://github.com/mrdoob/three.js/wiki/JSON-Object-Scene-format-4 | JSON Object/Scene format}. - * @param meta Optional object containing metadata. - */ - toJSON(meta?: string | {}): TextureJSON; - - /** - * Frees the GPU-related resources allocated by this instance - * @remarks Call this method whenever this instance is no longer used in your app. - */ - dispose(): void; -} diff --git a/src-testing/src/textures/VideoTexture.d.ts b/src-testing/src/textures/VideoTexture.d.ts deleted file mode 100644 index 31dc5d456..000000000 --- a/src-testing/src/textures/VideoTexture.d.ts +++ /dev/null @@ -1,90 +0,0 @@ -import { - MagnificationTextureFilter, - Mapping, - MinificationTextureFilter, - PixelFormat, - TextureDataType, - Wrapping, -} from "../constants.js"; -import { Texture } from "./Texture.js"; - -/** - * Creates a texture for use with a video. - * @remarks - * Note: After the initial use of a texture, the video cannot be changed - * Instead, call {@link dispose | .dispose()} on the texture and instantiate a new one. - * @example - * ```typescript - * // assuming you have created a HTML video element with id="video" - * const video = document.getElementById('video'); - * const texture = new THREE.VideoTexture(video); - * ``` - * @see Example: {@link https://threejs.org/examples/#webgl_materials_video | materials / video} - * @see Example: {@link https://threejs.org/examples/#webgl_materials_video_webcam | materials / video / webcam} - * @see Example: {@link https://threejs.org/examples/#webgl_video_kinect | video / kinect} - * @see Example: {@link https://threejs.org/examples/#webgl_video_panorama_equirectangular | video / panorama / equirectangular} - * @see Example: {@link https://threejs.org/examples/#webxr_vr_video | vr / video} - * @see {@link https://threejs.org/docs/index.html#api/en/textures/VideoTexture | Official Documentation} - * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/VideoTexture.js | Source} - */ -export class VideoTexture extends Texture { - /** - * Create a new instance of {@link VideoTexture} - * @param video The video element to use as the texture. - * @param mapping See {@link Texture.mapping | .mapping}. Default {@link THREE.Texture.DEFAULT_MAPPING} - * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping} - * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping} - * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.LinearFilter} - * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.LinearFilter} - * @param format See {@link Texture.format | .format}. Default {@link THREE.RGBAFormat} - * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType} - * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY} - */ - constructor( - video: HTMLVideoElement, - mapping?: Mapping, - wrapS?: Wrapping, - wrapT?: Wrapping, - magFilter?: MagnificationTextureFilter, - minFilter?: MinificationTextureFilter, - format?: PixelFormat, - type?: TextureDataType, - anisotropy?: number, - ); - - /** - * Read-only flag to check if a given object is of type {@link VideoTexture}. - * @remarks This is a _constant_ value - * @defaultValue `true` - */ - readonly isVideoTexture: true; - - /** - * @override - * @defaultValue {@link THREE.LinearFilter} - */ - magFilter: MagnificationTextureFilter; - - /** - * @override - * @defaultValue {@link THREE.LinearFilter} - */ - minFilter: MinificationTextureFilter; - - /** - * @override - * @defaultValue `false` - */ - generateMipmaps: boolean; - - /** - * @override - * You will **not** need to set this manually here as it is handled by the {@link update | update()} method. - */ - set needsUpdate(value: boolean); - - /** - * This is called automatically and sets {@link needsUpdate | .needsUpdate } to `true` every time a new frame is available. - */ - update(): void; -} diff --git a/src-testing/src/textures/types.d.ts b/src-testing/src/textures/types.d.ts deleted file mode 100644 index 86b7f2fd2..000000000 --- a/src-testing/src/textures/types.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -export interface TextureImageData { - data: Uint8Array | Uint8ClampedArray; - height: number; - width: number; -} - -export interface Texture3DImageData extends TextureImageData { - depth: number; -} diff --git a/src-testing/src/utils.d.ts b/src-testing/src/utils.d.ts deleted file mode 100644 index 3fda1cfda..000000000 --- a/src-testing/src/utils.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function createCanvasElement(): HTMLCanvasElement; - -export function probeAsync(gl: WebGLRenderingContext, sync: WebGLSync, interval: number): Promise; From 404092259edbe22fd6f75e87842193ca4ceafea0 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Sat, 24 Aug 2024 15:41:22 -0400 Subject: [PATCH 5/7] Update declarations --- types/three/src/renderers/common/RenderContext.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/three/src/renderers/common/RenderContext.d.ts b/types/three/src/renderers/common/RenderContext.d.ts index f86412f22..84662b77d 100644 --- a/types/three/src/renderers/common/RenderContext.d.ts +++ b/types/three/src/renderers/common/RenderContext.d.ts @@ -37,5 +37,7 @@ declare class RenderContext { activeMipmapLevel?: number | undefined; occlusionQueryCount?: number | undefined; constructor(); + getCacheKey(): string; } +export declare function getCacheKey(renderContext: RenderContext): string; export default RenderContext; From 19eaaaf26c0cc3819beb6c5da3aa531cb76eb469 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Sat, 24 Aug 2024 15:42:30 -0400 Subject: [PATCH 6/7] Add examples --- examples-testing/examples/css2d_label.ts | 186 ++++ examples-testing/examples/css3d_molecules.ts | 353 ++++++++ .../examples/css3d_orthographic.ts | 208 +++++ .../examples/css3d_periodictable.ts | 793 ++++++++++++++++++ examples-testing/examples/css3d_sandbox.ts | 180 ++++ examples-testing/examples/css3d_sprites.ts | 157 ++++ examples-testing/examples/css3d_youtube.ts | 79 ++ examples-testing/examples/games_fps.ts | 372 ++++++++ .../examples/misc_animation_groups.ts | 125 +++ .../examples/misc_animation_keys.ts | 129 +++ .../examples/misc_boxselection.ts | 137 +++ .../examples/misc_controls_arcball.ts | 210 +++++ .../examples/misc_controls_drag.ts | 153 ++++ .../examples/misc_controls_fly.ts | 214 +++++ .../examples/misc_controls_map.ts | 98 +++ .../examples/misc_controls_orbit.ts | 89 ++ .../examples/misc_controls_pointerlock.ts | 245 ++++++ .../examples/misc_controls_trackball.ts | 134 +++ .../examples/misc_controls_transform.ts | 181 ++++ .../examples/misc_exporter_draco.ts | 117 +++ .../examples/misc_exporter_exr.ts | 158 ++++ .../examples/misc_exporter_gltf.ts | 507 +++++++++++ .../examples/misc_exporter_obj.ts | 192 +++++ .../examples/misc_exporter_ply.ts | 156 ++++ .../examples/misc_exporter_stl.ts | 129 +++ .../examples/misc_exporter_usdz.ts | 129 +++ examples-testing/examples/misc_lookat.ts | 95 +++ examples-testing/examples/misc_uv_tests.ts | 44 + .../examples/physics_ammo_instancing.ts | 119 +++ .../examples/physics_jolt_instancing.ts | 119 +++ .../examples/physics_rapier_instancing.ts | 119 +++ examples-testing/examples/svg_lines.ts | 87 ++ examples-testing/examples/svg_sandbox.ts | 212 +++++ .../examples/webaudio_orientation.ts | 141 ++++ examples-testing/examples/webaudio_sandbox.ts | 222 +++++ examples-testing/examples/webaudio_timing.ts | 152 ++++ .../examples/webaudio_visualizer.ts | 86 ++ .../examples/webgl_animation_keyframes.ts | 80 ++ .../examples/webgl_animation_multiple.ts | 197 +++++ .../webgl_animation_skinning_morph.ts | 187 +++++ .../examples/webgl_buffergeometry.ts | 178 ++++ ...webgl_buffergeometry_attributes_integer.ts | 113 +++ .../webgl_buffergeometry_attributes_none.ts | 56 ++ ...fergeometry_custom_attributes_particles.ts | 103 +++ .../webgl_buffergeometry_drawrange.ts | 239 ++++++ .../webgl_buffergeometry_glbufferattribute.ts | 139 +++ .../examples/webgl_buffergeometry_indexed.ts | 137 +++ .../webgl_buffergeometry_instancing.ts | 138 +++ ...gl_buffergeometry_instancing_billboards.ts | 86 ++ ...l_buffergeometry_instancing_interleaved.ts | 152 ++++ .../examples/webgl_buffergeometry_lines.ts | 118 +++ .../webgl_buffergeometry_lines_indexed.ts | 179 ++++ .../examples/webgl_buffergeometry_points.ts | 109 +++ ...webgl_buffergeometry_points_interleaved.ts | 122 +++ .../webgl_buffergeometry_rawshader.ts | 97 +++ .../webgl_buffergeometry_selective_draw.ts | 150 ++++ .../examples/webgl_buffergeometry_uint.ts | 177 ++++ examples-testing/examples/webgl_camera.ts | 218 +++++ .../examples/webgl_camera_array.ts | 104 +++ .../webgl_camera_logarithmicdepthbuffer.ts | 248 ++++++ .../examples/webgl_clipculldistance.ts | 110 +++ examples-testing/examples/webgl_clipping.ts | 195 +++++ .../examples/webgl_clipping_advanced.ts | 355 ++++++++ .../examples/webgl_clipping_intersection.ts | 137 +++ .../examples/webgl_clipping_stencil.ts | 260 ++++++ .../examples/webgl_custom_attributes.ts | 100 +++ .../examples/webgl_custom_attributes_lines.ts | 121 +++ .../webgl_custom_attributes_points.ts | 117 +++ .../webgl_custom_attributes_points2.ts | 193 +++++ .../webgl_custom_attributes_points3.ts | 200 +++++ examples-testing/examples/webgl_decals.ts | 237 ++++++ .../examples/webgl_effects_anaglyph.ts | 114 +++ .../examples/webgl_effects_ascii.ts | 81 ++ .../examples/webgl_effects_parallaxbarrier.ts | 110 +++ .../examples/webgl_effects_peppersghost.ts | 85 ++ .../examples/webgl_effects_stereo.ts | 98 +++ .../examples/webgl_framebuffer_texture.ts | 151 ++++ .../examples/webgl_furnace_test.ts | 96 +++ examples-testing/examples/webgl_geometries.ts | 137 +++ .../examples/webgl_geometries_parametric.ts | 124 +++ .../examples/webgl_geometry_colors.ts | 176 ++++ .../webgl_geometry_colors_lookuptable.ts | 148 ++++ .../examples/webgl_geometry_convex.ts | 117 +++ .../examples/webgl_geometry_cube.ts | 46 + .../examples/webgl_geometry_dynamic.ts | 97 +++ .../examples/webgl_geometry_extrude_shapes.ts | 149 ++++ .../webgl_geometry_extrude_splines.ts | 310 +++++++ .../examples/webgl_geometry_minecraft.ts | 183 ++++ .../examples/webgl_geometry_nurbs.ts | 298 +++++++ .../examples/webgl_geometry_sdf.ts | 164 ++++ .../examples/webgl_geometry_shapes.ts | 363 ++++++++ .../examples/webgl_geometry_teapot.ts | 180 ++++ .../examples/webgl_geometry_terrain.ts | 173 ++++ .../webgl_geometry_terrain_raycast.ts | 206 +++++ .../examples/webgl_geometry_text.ts | 312 +++++++ .../examples/webgl_geometry_text_shapes.ts | 112 +++ .../examples/webgl_geometry_text_stroke.ts | 116 +++ .../examples/webgl_gpgpu_birds.ts | 313 +++++++ .../examples/webgl_gpgpu_birds_gltf.ts | 415 +++++++++ .../examples/webgl_gpgpu_protoplanet.ts | 280 +++++++ .../examples/webgl_gpgpu_water.ts | 397 +++++++++ examples-testing/examples/webgl_helpers.ts | 117 +++ .../examples/webgl_instancing_dynamic.ts | 103 +++ .../examples/webgl_instancing_morph.ts | 147 ++++ .../examples/webgl_instancing_performance.ts | 262 ++++++ .../examples/webgl_instancing_raycast.ts | 116 +++ .../examples/webgl_instancing_scatter.ts | 257 ++++++ .../webgl_interactive_buffergeometry.ts | 244 ++++++ .../examples/webgl_interactive_cubes.ts | 114 +++ .../examples/webgl_interactive_cubes_gpu.ts | 229 +++++ .../examples/webgl_interactive_cubes_ortho.ts | 129 +++ .../examples/webgl_interactive_lines.ts | 160 ++++ .../examples/webgl_interactive_points.ts | 143 ++++ .../webgl_interactive_raycasting_points.ts | 220 +++++ .../webgl_interactive_voxelpainter.ts | 158 ++++ examples-testing/examples/webgl_layers.ts | 125 +++ examples-testing/examples/webgl_lensflares.ts | 137 +++ examples-testing/examples/webgl_lightprobe.ts | 133 +++ .../examples/webgl_lightprobe_cubecamera.ts | 83 ++ .../examples/webgl_lights_hemisphere.ts | 188 +++++ .../examples/webgl_lights_physical.ts | 237 ++++++ .../examples/webgl_lights_pointlights.ts | 100 +++ .../examples/webgl_lights_rectarealight.ts | 79 ++ .../examples/webgl_lights_spotlight.ts | 183 ++++ .../examples/webgl_lights_spotlights.ts | 133 +++ .../examples/webgl_lines_colors.ts | 181 ++++ .../examples/webgl_lines_dashed.ts | 186 ++++ examples-testing/examples/webgl_lines_fat.ts | 251 ++++++ .../examples/webgl_lines_fat_raycasting.ts | 294 +++++++ .../examples/webgl_lines_fat_wireframe.ts | 210 +++++ examples-testing/examples/webgl_loader_3dm.ts | 95 +++ examples-testing/examples/webgl_loader_3ds.ts | 62 ++ examples-testing/examples/webgl_loader_3mf.ts | 105 +++ .../examples/webgl_loader_3mf_materials.ts | 106 +++ examples-testing/examples/webgl_loader_amf.ts | 62 ++ examples-testing/examples/webgl_loader_bvh.ts | 61 ++ .../examples/webgl_loader_collada.ts | 83 ++ .../examples/webgl_loader_collada_skinning.ts | 97 +++ .../examples/webgl_loader_draco.ts | 85 ++ examples-testing/examples/webgl_loader_fbx.ts | 162 ++++ .../examples/webgl_loader_fbx_nurbs.ts | 61 ++ .../examples/webgl_loader_gcode.ts | 49 ++ .../examples/webgl_loader_gltf.ts | 74 ++ .../examples/webgl_loader_gltf_anisotropy.ts | 68 ++ .../examples/webgl_loader_gltf_avif.ts | 61 ++ .../examples/webgl_loader_gltf_compressed.ts | 83 ++ .../examples/webgl_loader_gltf_dispersion.ts | 66 ++ .../examples/webgl_loader_gltf_instancing.ts | 69 ++ .../examples/webgl_loader_gltf_iridescence.ts | 66 ++ .../examples/webgl_loader_gltf_lights.ts | 83 ++ .../examples/webgl_loader_gltf_sheen.ts | 72 ++ .../webgl_loader_gltf_transmission.ts | 80 ++ .../examples/webgl_loader_imagebitmap.ts | 109 +++ examples-testing/examples/webgl_loader_kmz.ts | 59 ++ examples-testing/examples/webgl_loader_lwo.ts | 69 ++ .../examples/webgl_loader_md2_control.ts | 289 +++++++ examples-testing/examples/webgl_loader_mdd.ts | 62 ++ examples-testing/examples/webgl_loader_obj.ts | 98 +++ .../examples/webgl_loader_obj_mtl.ts | 82 ++ examples-testing/examples/webgl_loader_pcd.ts | 65 ++ examples-testing/examples/webgl_loader_pdb.ts | 208 +++++ examples-testing/examples/webgl_loader_ply.ts | 146 ++++ examples-testing/examples/webgl_loader_svg.ts | 193 +++++ .../examples/webgl_loader_texture_dds.ts | 207 +++++ .../examples/webgl_loader_texture_ktx.ts | 137 +++ .../examples/webgl_loader_texture_logluv.ts | 75 ++ .../examples/webgl_loader_texture_rgbm.ts | 75 ++ .../examples/webgl_loader_texture_tga.ts | 90 ++ .../examples/webgl_loader_texture_tiff.ts | 87 ++ .../examples/webgl_loader_texture_ultrahdr.ts | 101 +++ .../examples/webgl_loader_tilt.ts | 54 ++ examples-testing/examples/webgl_loader_ttf.ts | 231 +++++ .../examples/webgl_loader_usdz.ts | 68 ++ examples-testing/examples/webgl_loader_vox.ts | 104 +++ .../examples/webgl_loader_vrml.ts | 118 +++ examples-testing/examples/webgl_loader_vtk.ts | 123 +++ examples-testing/examples/webgl_loader_xyz.ts | 62 ++ examples-testing/examples/webgl_lod.ts | 88 ++ .../examples/webgl_marchingcubes.ts | 311 +++++++ .../examples/webgl_materials_alphahash.ts | 178 ++++ .../examples/webgl_materials_blending.ts | 147 ++++ .../webgl_materials_blending_custom.ts | 214 +++++ .../examples/webgl_materials_bumpmap.ts | 140 ++++ .../examples/webgl_materials_car.ts | 167 ++++ .../examples/webgl_materials_cubemap.ts | 115 +++ .../webgl_materials_cubemap_dynamic.ts | 115 +++ .../webgl_materials_cubemap_mipmaps.ts | 119 +++ .../webgl_materials_cubemap_refraction.ts | 126 +++ ...bgl_materials_cubemap_render_to_mipmaps.ts | 183 ++++ .../examples/webgl_materials_curvature.ts | 252 ++++++ .../webgl_materials_displacementmap.ts | 224 +++++ .../examples/webgl_materials_envmaps.ts | 131 +++ .../examples/webgl_materials_envmaps_exr.ts | 153 ++++ ...webgl_materials_envmaps_groundprojected.ts | 150 ++++ .../examples/webgl_materials_envmaps_hdr.ts | 176 ++++ .../examples/webgl_materials_modified.ts | 115 +++ .../webgl_materials_normalmap_object_space.ts | 82 ++ .../webgl_materials_physical_clearcoat.ts | 208 +++++ .../webgl_materials_physical_transmission.ts | 182 ++++ ...l_materials_physical_transmission_alpha.ts | 192 +++++ .../webgl_materials_texture_anisotropy.ts | 143 ++++ .../webgl_materials_texture_canvas.ts | 92 ++ .../webgl_materials_texture_filters.ts | 164 ++++ .../webgl_materials_texture_manualmipmap.ts | 175 ++++ .../webgl_materials_texture_partialupdate.ts | 100 +++ .../webgl_materials_texture_rotation.ts | 113 +++ .../examples/webgl_materials_toon.ts | 152 ++++ .../examples/webgl_materials_video.ts | 208 +++++ .../examples/webgl_materials_video_webcam.ts | 79 ++ .../examples/webgl_materials_wireframe.ts | 107 +++ examples-testing/examples/webgl_math_obb.ts | 189 +++++ .../webgl_math_orientation_transform.ts | 95 +++ examples-testing/examples/webgl_mesh_batch.ts | 305 +++++++ examples-testing/examples/webgl_mirror.ts | 168 ++++ .../examples/webgl_modifier_edgesplit.ts | 136 +++ .../examples/webgl_modifier_simplifier.ts | 77 ++ .../examples/webgl_modifier_tessellation.ts | 142 ++++ .../examples/webgl_morphtargets.ts | 120 +++ .../examples/webgl_morphtargets_face.ts | 105 +++ .../examples/webgl_morphtargets_horse.ts | 100 +++ .../examples/webgl_morphtargets_sphere.ts | 105 +++ .../examples/webgl_multiple_elements.ts | 139 +++ .../examples/webgl_multiple_rendertargets.ts | 133 +++ .../webgl_multiple_scenes_comparison.ts | 98 +++ .../examples/webgl_multiple_views.ts | 237 ++++++ .../webgl_multisampled_renderbuffers.ts | 133 +++ .../examples/webgl_panorama_cube.ts | 83 ++ .../webgl_panorama_equirectangular.ts | 142 ++++ examples-testing/examples/webgl_pmrem_test.ts | 141 ++++ .../examples/webgl_points_billboards.ts | 120 +++ .../examples/webgl_points_sprites.ts | 167 ++++ .../examples/webgl_points_waves.ts | 145 ++++ examples-testing/examples/webgl_portal.ts | 218 +++++ .../examples/webgl_postprocessing.ts | 86 ++ .../examples/webgl_postprocessing_advanced.ts | 304 +++++++ .../webgl_postprocessing_afterimage.ts | 72 ++ .../webgl_postprocessing_backgrounds.ts | 214 +++++ .../examples/webgl_postprocessing_fxaa.ts | 132 +++ .../examples/webgl_postprocessing_glitch.ts | 97 +++ .../examples/webgl_postprocessing_godrays.ts | 347 ++++++++ .../examples/webgl_postprocessing_gtao.ts | 215 +++++ .../examples/webgl_postprocessing_masking.ts | 100 +++ .../webgl_postprocessing_material_ao.ts | 277 ++++++ .../examples/webgl_postprocessing_outline.ts | 282 +++++++ .../examples/webgl_postprocessing_pixel.ts | 228 +++++ .../webgl_postprocessing_procedural.ts | 77 ++ .../webgl_postprocessing_rgb_halftone.ts | 167 ++++ .../examples/webgl_postprocessing_sao.ts | 137 +++ .../examples/webgl_postprocessing_smaa.ts | 109 +++ .../examples/webgl_postprocessing_sobel.ts | 111 +++ .../examples/webgl_postprocessing_ssaa.ts | 206 +++++ .../examples/webgl_postprocessing_ssao.ts | 118 +++ .../examples/webgl_postprocessing_ssr.ts | 261 ++++++ .../examples/webgl_postprocessing_taa.ts | 139 +++ .../webgl_postprocessing_transition.ts | 211 +++++ .../webgl_postprocessing_unreal_bloom.ts | 136 +++ ...l_postprocessing_unreal_bloom_selective.ts | 195 +++++ .../examples/webgl_raycaster_sprite.ts | 103 +++ .../examples/webgl_raycaster_texture.ts | 286 +++++++ .../examples/webgl_raymarching_reflect.ts | 95 +++ .../examples/webgl_read_float_buffer.ts | 153 ++++ examples-testing/examples/webgl_refraction.ts | 135 +++ examples-testing/examples/webgl_rtt.ts | 171 ++++ examples-testing/examples/webgl_shader.ts | 50 ++ .../examples/webgl_shader_lava.ts | 101 +++ .../examples/webgl_shaders_ocean.ts | 169 ++++ .../examples/webgl_shaders_sky.ts | 103 +++ .../examples/webgl_shadow_contact.ts | 272 ++++++ examples-testing/examples/webgl_shadowmap.ts | 311 +++++++ .../examples/webgl_shadowmap_csm.ts | 253 ++++++ .../examples/webgl_shadowmap_pcss.ts | 161 ++++ .../examples/webgl_shadowmap_performance.ts | 281 +++++++ .../examples/webgl_shadowmap_pointlight.ts | 139 +++ .../examples/webgl_shadowmap_progressive.ts | 204 +++++ .../examples/webgl_shadowmap_viewer.ts | 178 ++++ .../examples/webgl_shadowmap_vsm.ts | 200 +++++ examples-testing/examples/webgl_shadowmesh.ts | 250 ++++++ examples-testing/examples/webgl_simple_gi.ts | 172 ++++ examples-testing/examples/webgl_sprites.ts | 187 +++++ .../examples/webgl_test_memory.ts | 65 ++ .../examples/webgl_test_memory2.ts | 81 ++ .../examples/webgl_test_wide_gamut.ts | 118 +++ .../webgl_texture2darray_compressed.ts | 88 ++ .../webgl_texture2darray_layerupdate.ts | 124 +++ examples-testing/examples/webgl_texture3d.ts | 128 +++ .../examples/webgl_texture3d_partialupdate.ts | 326 +++++++ .../examples/webgl_tonemapping.ts | 163 ++++ examples-testing/examples/webgl_ubo.ts | 137 +++ examples-testing/examples/webgl_ubo_arrays.ts | 171 ++++ .../examples/webgl_video_kinect.ts | 113 +++ .../webgl_video_panorama_equirectangular.ts | 95 +++ .../examples/webgl_volume_cloud.ts | 279 ++++++ .../examples/webgl_volume_instancing.ts | 192 +++++ .../examples/webgl_volume_perlin.ts | 208 +++++ examples-testing/examples/webgl_water.ts | 162 ++++ .../examples/webgl_water_flowmap.ts | 100 +++ .../examples/webgpu_backdrop_area.ts | 162 ++++ .../webgpu_camera_logarithmicdepthbuffer.ts | 245 ++++++ examples-testing/examples/webgpu_clearcoat.ts | 205 +++++ examples-testing/examples/webgpu_clipping.ts | 207 +++++ .../examples/webgpu_custom_fog_background.ts | 93 ++ .../examples/webgpu_instancing_morph.ts | 148 ++++ .../examples/webgpu_lights_ies_spotlight.ts | 117 +++ .../examples/webgpu_lights_rectarealight.ts | 79 ++ .../examples/webgpu_loader_gltf.ts | 71 ++ .../examples/webgpu_loader_gltf_anisotropy.ts | 65 ++ .../examples/webgpu_loader_gltf_compressed.ts | 67 ++ .../examples/webgpu_loader_gltf_dispersion.ts | 63 ++ .../webgpu_loader_gltf_iridescence.ts | 70 ++ .../examples/webgpu_loader_gltf_sheen.ts | 81 ++ .../webgpu_loader_gltf_transmission.ts | 80 ++ .../examples/webgpu_materials_basic.ts | 137 +++ .../webgpu_materials_displacementmap.ts | 224 +++++ .../examples/webgpu_materials_lightmap.ts | 94 +++ .../examples/webgpu_materials_toon.ts | 148 ++++ .../examples/webgpu_materials_transmission.ts | 168 ++++ .../examples/webgpu_materials_video.ts | 184 ++++ .../examples/webgpu_mesh_batch.ts | 271 ++++++ .../examples/webgpu_morphtargets.ts | 121 +++ .../examples/webgpu_morphtargets_face.ts | 102 +++ examples-testing/examples/webgpu_mrt.ts | 119 +++ .../webgpu_multiple_rendertargets_readback.ts | 156 ++++ examples-testing/examples/webgpu_ocean.ts | 161 ++++ .../examples/webgpu_parallax_uv.ts | 112 +++ .../examples/webgpu_postprocessing.ts | 77 ++ .../examples/webgpu_postprocessing_3dlut.ts | 139 +++ .../webgpu_postprocessing_afterimage.ts | 70 ++ .../examples/webgpu_postprocessing_ao.ts | 204 +++++ .../examples/webgpu_postprocessing_bloom.ts | 127 +++ .../webgpu_postprocessing_bloom_emissive.ts | 101 +++ .../webgpu_postprocessing_bloom_selective.ts | 122 +++ .../webgpu_postprocessing_difference.ts | 92 ++ .../examples/webgpu_postprocessing_dof.ts | 159 ++++ .../examples/webgpu_postprocessing_fxaa.ts | 122 +++ .../examples/webgpu_postprocessing_masking.ts | 85 ++ .../examples/webgpu_postprocessing_pixel.ts | 234 ++++++ .../examples/webgpu_postprocessing_sobel.ts | 89 ++ .../webgpu_postprocessing_transition.ts | 200 +++++ .../examples/webgpu_procedural_texture.ts | 74 ++ .../examples/webgpu_refraction.ts | 141 ++++ examples-testing/examples/webgpu_sky.ts | 95 +++ .../examples/webgpu_textures_anisotropy.ts | 155 ++++ .../examples/webgpu_textures_partialupdate.ts | 103 +++ .../examples/webgpu_tsl_coffee_smoke.ts | 132 +++ .../examples/webgpu_tsl_vfx_flames.ts | 203 +++++ .../examples/webgpu_video_panorama.ts | 99 +++ examples-testing/examples/webgpu_water.ts | 171 ++++ examples-testing/examples/webxr_ar_cones.ts | 66 ++ examples-testing/examples/webxr_ar_hittest.ts | 115 +++ .../examples/webxr_ar_lighting.ts | 124 +++ .../examples/webxr_ar_plane_detection.ts | 46 + .../examples/webxr_vr_handinput.ts | 126 +++ .../examples/webxr_vr_panorama.ts | 92 ++ .../examples/webxr_vr_panorama_depth.ts | 90 ++ .../examples/webxr_vr_rollercoaster.ts | 211 +++++ examples-testing/examples/webxr_vr_sandbox.ts | 192 +++++ examples-testing/examples/webxr_vr_video.ts | 92 ++ .../examples/webxr_xr_controls_transform.ts | 210 +++++ .../webxr_xr_dragging_custom_depth.ts | 395 +++++++++ 359 files changed, 54958 insertions(+) create mode 100644 examples-testing/examples/css2d_label.ts create mode 100644 examples-testing/examples/css3d_molecules.ts create mode 100644 examples-testing/examples/css3d_orthographic.ts create mode 100644 examples-testing/examples/css3d_periodictable.ts create mode 100644 examples-testing/examples/css3d_sandbox.ts create mode 100644 examples-testing/examples/css3d_sprites.ts create mode 100644 examples-testing/examples/css3d_youtube.ts create mode 100644 examples-testing/examples/games_fps.ts create mode 100644 examples-testing/examples/misc_animation_groups.ts create mode 100644 examples-testing/examples/misc_animation_keys.ts create mode 100644 examples-testing/examples/misc_boxselection.ts create mode 100644 examples-testing/examples/misc_controls_arcball.ts create mode 100644 examples-testing/examples/misc_controls_drag.ts create mode 100644 examples-testing/examples/misc_controls_fly.ts create mode 100644 examples-testing/examples/misc_controls_map.ts create mode 100644 examples-testing/examples/misc_controls_orbit.ts create mode 100644 examples-testing/examples/misc_controls_pointerlock.ts create mode 100644 examples-testing/examples/misc_controls_trackball.ts create mode 100644 examples-testing/examples/misc_controls_transform.ts create mode 100644 examples-testing/examples/misc_exporter_draco.ts create mode 100644 examples-testing/examples/misc_exporter_exr.ts create mode 100644 examples-testing/examples/misc_exporter_gltf.ts create mode 100644 examples-testing/examples/misc_exporter_obj.ts create mode 100644 examples-testing/examples/misc_exporter_ply.ts create mode 100644 examples-testing/examples/misc_exporter_stl.ts create mode 100644 examples-testing/examples/misc_exporter_usdz.ts create mode 100644 examples-testing/examples/misc_lookat.ts create mode 100644 examples-testing/examples/misc_uv_tests.ts create mode 100644 examples-testing/examples/physics_ammo_instancing.ts create mode 100644 examples-testing/examples/physics_jolt_instancing.ts create mode 100644 examples-testing/examples/physics_rapier_instancing.ts create mode 100644 examples-testing/examples/svg_lines.ts create mode 100644 examples-testing/examples/svg_sandbox.ts create mode 100644 examples-testing/examples/webaudio_orientation.ts create mode 100644 examples-testing/examples/webaudio_sandbox.ts create mode 100644 examples-testing/examples/webaudio_timing.ts create mode 100644 examples-testing/examples/webaudio_visualizer.ts create mode 100644 examples-testing/examples/webgl_animation_keyframes.ts create mode 100644 examples-testing/examples/webgl_animation_multiple.ts create mode 100644 examples-testing/examples/webgl_animation_skinning_morph.ts create mode 100644 examples-testing/examples/webgl_buffergeometry.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_attributes_integer.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_attributes_none.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_custom_attributes_particles.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_drawrange.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_glbufferattribute.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_indexed.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_instancing.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_instancing_billboards.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_instancing_interleaved.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_lines.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_lines_indexed.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_points.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_points_interleaved.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_rawshader.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_selective_draw.ts create mode 100644 examples-testing/examples/webgl_buffergeometry_uint.ts create mode 100644 examples-testing/examples/webgl_camera.ts create mode 100644 examples-testing/examples/webgl_camera_array.ts create mode 100644 examples-testing/examples/webgl_camera_logarithmicdepthbuffer.ts create mode 100644 examples-testing/examples/webgl_clipculldistance.ts create mode 100644 examples-testing/examples/webgl_clipping.ts create mode 100644 examples-testing/examples/webgl_clipping_advanced.ts create mode 100644 examples-testing/examples/webgl_clipping_intersection.ts create mode 100644 examples-testing/examples/webgl_clipping_stencil.ts create mode 100644 examples-testing/examples/webgl_custom_attributes.ts create mode 100644 examples-testing/examples/webgl_custom_attributes_lines.ts create mode 100644 examples-testing/examples/webgl_custom_attributes_points.ts create mode 100644 examples-testing/examples/webgl_custom_attributes_points2.ts create mode 100644 examples-testing/examples/webgl_custom_attributes_points3.ts create mode 100644 examples-testing/examples/webgl_decals.ts create mode 100644 examples-testing/examples/webgl_effects_anaglyph.ts create mode 100644 examples-testing/examples/webgl_effects_ascii.ts create mode 100644 examples-testing/examples/webgl_effects_parallaxbarrier.ts create mode 100644 examples-testing/examples/webgl_effects_peppersghost.ts create mode 100644 examples-testing/examples/webgl_effects_stereo.ts create mode 100644 examples-testing/examples/webgl_framebuffer_texture.ts create mode 100644 examples-testing/examples/webgl_furnace_test.ts create mode 100644 examples-testing/examples/webgl_geometries.ts create mode 100644 examples-testing/examples/webgl_geometries_parametric.ts create mode 100644 examples-testing/examples/webgl_geometry_colors.ts create mode 100644 examples-testing/examples/webgl_geometry_colors_lookuptable.ts create mode 100644 examples-testing/examples/webgl_geometry_convex.ts create mode 100644 examples-testing/examples/webgl_geometry_cube.ts create mode 100644 examples-testing/examples/webgl_geometry_dynamic.ts create mode 100644 examples-testing/examples/webgl_geometry_extrude_shapes.ts create mode 100644 examples-testing/examples/webgl_geometry_extrude_splines.ts create mode 100644 examples-testing/examples/webgl_geometry_minecraft.ts create mode 100644 examples-testing/examples/webgl_geometry_nurbs.ts create mode 100644 examples-testing/examples/webgl_geometry_sdf.ts create mode 100644 examples-testing/examples/webgl_geometry_shapes.ts create mode 100644 examples-testing/examples/webgl_geometry_teapot.ts create mode 100644 examples-testing/examples/webgl_geometry_terrain.ts create mode 100644 examples-testing/examples/webgl_geometry_terrain_raycast.ts create mode 100644 examples-testing/examples/webgl_geometry_text.ts create mode 100644 examples-testing/examples/webgl_geometry_text_shapes.ts create mode 100644 examples-testing/examples/webgl_geometry_text_stroke.ts create mode 100644 examples-testing/examples/webgl_gpgpu_birds.ts create mode 100644 examples-testing/examples/webgl_gpgpu_birds_gltf.ts create mode 100644 examples-testing/examples/webgl_gpgpu_protoplanet.ts create mode 100644 examples-testing/examples/webgl_gpgpu_water.ts create mode 100644 examples-testing/examples/webgl_helpers.ts create mode 100644 examples-testing/examples/webgl_instancing_dynamic.ts create mode 100644 examples-testing/examples/webgl_instancing_morph.ts create mode 100644 examples-testing/examples/webgl_instancing_performance.ts create mode 100644 examples-testing/examples/webgl_instancing_raycast.ts create mode 100644 examples-testing/examples/webgl_instancing_scatter.ts create mode 100644 examples-testing/examples/webgl_interactive_buffergeometry.ts create mode 100644 examples-testing/examples/webgl_interactive_cubes.ts create mode 100644 examples-testing/examples/webgl_interactive_cubes_gpu.ts create mode 100644 examples-testing/examples/webgl_interactive_cubes_ortho.ts create mode 100644 examples-testing/examples/webgl_interactive_lines.ts create mode 100644 examples-testing/examples/webgl_interactive_points.ts create mode 100644 examples-testing/examples/webgl_interactive_raycasting_points.ts create mode 100644 examples-testing/examples/webgl_interactive_voxelpainter.ts create mode 100644 examples-testing/examples/webgl_layers.ts create mode 100644 examples-testing/examples/webgl_lensflares.ts create mode 100644 examples-testing/examples/webgl_lightprobe.ts create mode 100644 examples-testing/examples/webgl_lightprobe_cubecamera.ts create mode 100644 examples-testing/examples/webgl_lights_hemisphere.ts create mode 100644 examples-testing/examples/webgl_lights_physical.ts create mode 100644 examples-testing/examples/webgl_lights_pointlights.ts create mode 100644 examples-testing/examples/webgl_lights_rectarealight.ts create mode 100644 examples-testing/examples/webgl_lights_spotlight.ts create mode 100644 examples-testing/examples/webgl_lights_spotlights.ts create mode 100644 examples-testing/examples/webgl_lines_colors.ts create mode 100644 examples-testing/examples/webgl_lines_dashed.ts create mode 100644 examples-testing/examples/webgl_lines_fat.ts create mode 100644 examples-testing/examples/webgl_lines_fat_raycasting.ts create mode 100644 examples-testing/examples/webgl_lines_fat_wireframe.ts create mode 100644 examples-testing/examples/webgl_loader_3dm.ts create mode 100644 examples-testing/examples/webgl_loader_3ds.ts create mode 100644 examples-testing/examples/webgl_loader_3mf.ts create mode 100644 examples-testing/examples/webgl_loader_3mf_materials.ts create mode 100644 examples-testing/examples/webgl_loader_amf.ts create mode 100644 examples-testing/examples/webgl_loader_bvh.ts create mode 100644 examples-testing/examples/webgl_loader_collada.ts create mode 100644 examples-testing/examples/webgl_loader_collada_skinning.ts create mode 100644 examples-testing/examples/webgl_loader_draco.ts create mode 100644 examples-testing/examples/webgl_loader_fbx.ts create mode 100644 examples-testing/examples/webgl_loader_fbx_nurbs.ts create mode 100644 examples-testing/examples/webgl_loader_gcode.ts create mode 100644 examples-testing/examples/webgl_loader_gltf.ts create mode 100644 examples-testing/examples/webgl_loader_gltf_anisotropy.ts create mode 100644 examples-testing/examples/webgl_loader_gltf_avif.ts create mode 100644 examples-testing/examples/webgl_loader_gltf_compressed.ts create mode 100644 examples-testing/examples/webgl_loader_gltf_dispersion.ts create mode 100644 examples-testing/examples/webgl_loader_gltf_instancing.ts create mode 100644 examples-testing/examples/webgl_loader_gltf_iridescence.ts create mode 100644 examples-testing/examples/webgl_loader_gltf_lights.ts create mode 100644 examples-testing/examples/webgl_loader_gltf_sheen.ts create mode 100644 examples-testing/examples/webgl_loader_gltf_transmission.ts create mode 100644 examples-testing/examples/webgl_loader_imagebitmap.ts create mode 100644 examples-testing/examples/webgl_loader_kmz.ts create mode 100644 examples-testing/examples/webgl_loader_lwo.ts create mode 100644 examples-testing/examples/webgl_loader_md2_control.ts create mode 100644 examples-testing/examples/webgl_loader_mdd.ts create mode 100644 examples-testing/examples/webgl_loader_obj.ts create mode 100644 examples-testing/examples/webgl_loader_obj_mtl.ts create mode 100644 examples-testing/examples/webgl_loader_pcd.ts create mode 100644 examples-testing/examples/webgl_loader_pdb.ts create mode 100644 examples-testing/examples/webgl_loader_ply.ts create mode 100644 examples-testing/examples/webgl_loader_svg.ts create mode 100644 examples-testing/examples/webgl_loader_texture_dds.ts create mode 100644 examples-testing/examples/webgl_loader_texture_ktx.ts create mode 100644 examples-testing/examples/webgl_loader_texture_logluv.ts create mode 100644 examples-testing/examples/webgl_loader_texture_rgbm.ts create mode 100644 examples-testing/examples/webgl_loader_texture_tga.ts create mode 100644 examples-testing/examples/webgl_loader_texture_tiff.ts create mode 100644 examples-testing/examples/webgl_loader_texture_ultrahdr.ts create mode 100644 examples-testing/examples/webgl_loader_tilt.ts create mode 100644 examples-testing/examples/webgl_loader_ttf.ts create mode 100644 examples-testing/examples/webgl_loader_usdz.ts create mode 100644 examples-testing/examples/webgl_loader_vox.ts create mode 100644 examples-testing/examples/webgl_loader_vrml.ts create mode 100644 examples-testing/examples/webgl_loader_vtk.ts create mode 100644 examples-testing/examples/webgl_loader_xyz.ts create mode 100644 examples-testing/examples/webgl_lod.ts create mode 100644 examples-testing/examples/webgl_marchingcubes.ts create mode 100644 examples-testing/examples/webgl_materials_alphahash.ts create mode 100644 examples-testing/examples/webgl_materials_blending.ts create mode 100644 examples-testing/examples/webgl_materials_blending_custom.ts create mode 100644 examples-testing/examples/webgl_materials_bumpmap.ts create mode 100644 examples-testing/examples/webgl_materials_car.ts create mode 100644 examples-testing/examples/webgl_materials_cubemap.ts create mode 100644 examples-testing/examples/webgl_materials_cubemap_dynamic.ts create mode 100644 examples-testing/examples/webgl_materials_cubemap_mipmaps.ts create mode 100644 examples-testing/examples/webgl_materials_cubemap_refraction.ts create mode 100644 examples-testing/examples/webgl_materials_cubemap_render_to_mipmaps.ts create mode 100644 examples-testing/examples/webgl_materials_curvature.ts create mode 100644 examples-testing/examples/webgl_materials_displacementmap.ts create mode 100644 examples-testing/examples/webgl_materials_envmaps.ts create mode 100644 examples-testing/examples/webgl_materials_envmaps_exr.ts create mode 100644 examples-testing/examples/webgl_materials_envmaps_groundprojected.ts create mode 100644 examples-testing/examples/webgl_materials_envmaps_hdr.ts create mode 100644 examples-testing/examples/webgl_materials_modified.ts create mode 100644 examples-testing/examples/webgl_materials_normalmap_object_space.ts create mode 100644 examples-testing/examples/webgl_materials_physical_clearcoat.ts create mode 100644 examples-testing/examples/webgl_materials_physical_transmission.ts create mode 100644 examples-testing/examples/webgl_materials_physical_transmission_alpha.ts create mode 100644 examples-testing/examples/webgl_materials_texture_anisotropy.ts create mode 100644 examples-testing/examples/webgl_materials_texture_canvas.ts create mode 100644 examples-testing/examples/webgl_materials_texture_filters.ts create mode 100644 examples-testing/examples/webgl_materials_texture_manualmipmap.ts create mode 100644 examples-testing/examples/webgl_materials_texture_partialupdate.ts create mode 100644 examples-testing/examples/webgl_materials_texture_rotation.ts create mode 100644 examples-testing/examples/webgl_materials_toon.ts create mode 100644 examples-testing/examples/webgl_materials_video.ts create mode 100644 examples-testing/examples/webgl_materials_video_webcam.ts create mode 100644 examples-testing/examples/webgl_materials_wireframe.ts create mode 100644 examples-testing/examples/webgl_math_obb.ts create mode 100644 examples-testing/examples/webgl_math_orientation_transform.ts create mode 100644 examples-testing/examples/webgl_mesh_batch.ts create mode 100644 examples-testing/examples/webgl_mirror.ts create mode 100644 examples-testing/examples/webgl_modifier_edgesplit.ts create mode 100644 examples-testing/examples/webgl_modifier_simplifier.ts create mode 100644 examples-testing/examples/webgl_modifier_tessellation.ts create mode 100644 examples-testing/examples/webgl_morphtargets.ts create mode 100644 examples-testing/examples/webgl_morphtargets_face.ts create mode 100644 examples-testing/examples/webgl_morphtargets_horse.ts create mode 100644 examples-testing/examples/webgl_morphtargets_sphere.ts create mode 100644 examples-testing/examples/webgl_multiple_elements.ts create mode 100644 examples-testing/examples/webgl_multiple_rendertargets.ts create mode 100644 examples-testing/examples/webgl_multiple_scenes_comparison.ts create mode 100644 examples-testing/examples/webgl_multiple_views.ts create mode 100644 examples-testing/examples/webgl_multisampled_renderbuffers.ts create mode 100644 examples-testing/examples/webgl_panorama_cube.ts create mode 100644 examples-testing/examples/webgl_panorama_equirectangular.ts create mode 100644 examples-testing/examples/webgl_pmrem_test.ts create mode 100644 examples-testing/examples/webgl_points_billboards.ts create mode 100644 examples-testing/examples/webgl_points_sprites.ts create mode 100644 examples-testing/examples/webgl_points_waves.ts create mode 100644 examples-testing/examples/webgl_portal.ts create mode 100644 examples-testing/examples/webgl_postprocessing.ts create mode 100644 examples-testing/examples/webgl_postprocessing_advanced.ts create mode 100644 examples-testing/examples/webgl_postprocessing_afterimage.ts create mode 100644 examples-testing/examples/webgl_postprocessing_backgrounds.ts create mode 100644 examples-testing/examples/webgl_postprocessing_fxaa.ts create mode 100644 examples-testing/examples/webgl_postprocessing_glitch.ts create mode 100644 examples-testing/examples/webgl_postprocessing_godrays.ts create mode 100644 examples-testing/examples/webgl_postprocessing_gtao.ts create mode 100644 examples-testing/examples/webgl_postprocessing_masking.ts create mode 100644 examples-testing/examples/webgl_postprocessing_material_ao.ts create mode 100644 examples-testing/examples/webgl_postprocessing_outline.ts create mode 100644 examples-testing/examples/webgl_postprocessing_pixel.ts create mode 100644 examples-testing/examples/webgl_postprocessing_procedural.ts create mode 100644 examples-testing/examples/webgl_postprocessing_rgb_halftone.ts create mode 100644 examples-testing/examples/webgl_postprocessing_sao.ts create mode 100644 examples-testing/examples/webgl_postprocessing_smaa.ts create mode 100644 examples-testing/examples/webgl_postprocessing_sobel.ts create mode 100644 examples-testing/examples/webgl_postprocessing_ssaa.ts create mode 100644 examples-testing/examples/webgl_postprocessing_ssao.ts create mode 100644 examples-testing/examples/webgl_postprocessing_ssr.ts create mode 100644 examples-testing/examples/webgl_postprocessing_taa.ts create mode 100644 examples-testing/examples/webgl_postprocessing_transition.ts create mode 100644 examples-testing/examples/webgl_postprocessing_unreal_bloom.ts create mode 100644 examples-testing/examples/webgl_postprocessing_unreal_bloom_selective.ts create mode 100644 examples-testing/examples/webgl_raycaster_sprite.ts create mode 100644 examples-testing/examples/webgl_raycaster_texture.ts create mode 100644 examples-testing/examples/webgl_raymarching_reflect.ts create mode 100644 examples-testing/examples/webgl_read_float_buffer.ts create mode 100644 examples-testing/examples/webgl_refraction.ts create mode 100644 examples-testing/examples/webgl_rtt.ts create mode 100644 examples-testing/examples/webgl_shader.ts create mode 100644 examples-testing/examples/webgl_shader_lava.ts create mode 100644 examples-testing/examples/webgl_shaders_ocean.ts create mode 100644 examples-testing/examples/webgl_shaders_sky.ts create mode 100644 examples-testing/examples/webgl_shadow_contact.ts create mode 100644 examples-testing/examples/webgl_shadowmap.ts create mode 100644 examples-testing/examples/webgl_shadowmap_csm.ts create mode 100644 examples-testing/examples/webgl_shadowmap_pcss.ts create mode 100644 examples-testing/examples/webgl_shadowmap_performance.ts create mode 100644 examples-testing/examples/webgl_shadowmap_pointlight.ts create mode 100644 examples-testing/examples/webgl_shadowmap_progressive.ts create mode 100644 examples-testing/examples/webgl_shadowmap_viewer.ts create mode 100644 examples-testing/examples/webgl_shadowmap_vsm.ts create mode 100644 examples-testing/examples/webgl_shadowmesh.ts create mode 100644 examples-testing/examples/webgl_simple_gi.ts create mode 100644 examples-testing/examples/webgl_sprites.ts create mode 100644 examples-testing/examples/webgl_test_memory.ts create mode 100644 examples-testing/examples/webgl_test_memory2.ts create mode 100644 examples-testing/examples/webgl_test_wide_gamut.ts create mode 100644 examples-testing/examples/webgl_texture2darray_compressed.ts create mode 100644 examples-testing/examples/webgl_texture2darray_layerupdate.ts create mode 100644 examples-testing/examples/webgl_texture3d.ts create mode 100644 examples-testing/examples/webgl_texture3d_partialupdate.ts create mode 100644 examples-testing/examples/webgl_tonemapping.ts create mode 100644 examples-testing/examples/webgl_ubo.ts create mode 100644 examples-testing/examples/webgl_ubo_arrays.ts create mode 100644 examples-testing/examples/webgl_video_kinect.ts create mode 100644 examples-testing/examples/webgl_video_panorama_equirectangular.ts create mode 100644 examples-testing/examples/webgl_volume_cloud.ts create mode 100644 examples-testing/examples/webgl_volume_instancing.ts create mode 100644 examples-testing/examples/webgl_volume_perlin.ts create mode 100644 examples-testing/examples/webgl_water.ts create mode 100644 examples-testing/examples/webgl_water_flowmap.ts create mode 100644 examples-testing/examples/webgpu_backdrop_area.ts create mode 100644 examples-testing/examples/webgpu_camera_logarithmicdepthbuffer.ts create mode 100644 examples-testing/examples/webgpu_clearcoat.ts create mode 100644 examples-testing/examples/webgpu_clipping.ts create mode 100644 examples-testing/examples/webgpu_custom_fog_background.ts create mode 100644 examples-testing/examples/webgpu_instancing_morph.ts create mode 100644 examples-testing/examples/webgpu_lights_ies_spotlight.ts create mode 100644 examples-testing/examples/webgpu_lights_rectarealight.ts create mode 100644 examples-testing/examples/webgpu_loader_gltf.ts create mode 100644 examples-testing/examples/webgpu_loader_gltf_anisotropy.ts create mode 100644 examples-testing/examples/webgpu_loader_gltf_compressed.ts create mode 100644 examples-testing/examples/webgpu_loader_gltf_dispersion.ts create mode 100644 examples-testing/examples/webgpu_loader_gltf_iridescence.ts create mode 100644 examples-testing/examples/webgpu_loader_gltf_sheen.ts create mode 100644 examples-testing/examples/webgpu_loader_gltf_transmission.ts create mode 100644 examples-testing/examples/webgpu_materials_basic.ts create mode 100644 examples-testing/examples/webgpu_materials_displacementmap.ts create mode 100644 examples-testing/examples/webgpu_materials_lightmap.ts create mode 100644 examples-testing/examples/webgpu_materials_toon.ts create mode 100644 examples-testing/examples/webgpu_materials_transmission.ts create mode 100644 examples-testing/examples/webgpu_materials_video.ts create mode 100644 examples-testing/examples/webgpu_mesh_batch.ts create mode 100644 examples-testing/examples/webgpu_morphtargets.ts create mode 100644 examples-testing/examples/webgpu_morphtargets_face.ts create mode 100644 examples-testing/examples/webgpu_mrt.ts create mode 100644 examples-testing/examples/webgpu_multiple_rendertargets_readback.ts create mode 100644 examples-testing/examples/webgpu_ocean.ts create mode 100644 examples-testing/examples/webgpu_parallax_uv.ts create mode 100644 examples-testing/examples/webgpu_postprocessing.ts create mode 100644 examples-testing/examples/webgpu_postprocessing_3dlut.ts create mode 100644 examples-testing/examples/webgpu_postprocessing_afterimage.ts create mode 100644 examples-testing/examples/webgpu_postprocessing_ao.ts create mode 100644 examples-testing/examples/webgpu_postprocessing_bloom.ts create mode 100644 examples-testing/examples/webgpu_postprocessing_bloom_emissive.ts create mode 100644 examples-testing/examples/webgpu_postprocessing_bloom_selective.ts create mode 100644 examples-testing/examples/webgpu_postprocessing_difference.ts create mode 100644 examples-testing/examples/webgpu_postprocessing_dof.ts create mode 100644 examples-testing/examples/webgpu_postprocessing_fxaa.ts create mode 100644 examples-testing/examples/webgpu_postprocessing_masking.ts create mode 100644 examples-testing/examples/webgpu_postprocessing_pixel.ts create mode 100644 examples-testing/examples/webgpu_postprocessing_sobel.ts create mode 100644 examples-testing/examples/webgpu_postprocessing_transition.ts create mode 100644 examples-testing/examples/webgpu_procedural_texture.ts create mode 100644 examples-testing/examples/webgpu_refraction.ts create mode 100644 examples-testing/examples/webgpu_sky.ts create mode 100644 examples-testing/examples/webgpu_textures_anisotropy.ts create mode 100644 examples-testing/examples/webgpu_textures_partialupdate.ts create mode 100644 examples-testing/examples/webgpu_tsl_coffee_smoke.ts create mode 100644 examples-testing/examples/webgpu_tsl_vfx_flames.ts create mode 100644 examples-testing/examples/webgpu_video_panorama.ts create mode 100644 examples-testing/examples/webgpu_water.ts create mode 100644 examples-testing/examples/webxr_ar_cones.ts create mode 100644 examples-testing/examples/webxr_ar_hittest.ts create mode 100644 examples-testing/examples/webxr_ar_lighting.ts create mode 100644 examples-testing/examples/webxr_ar_plane_detection.ts create mode 100644 examples-testing/examples/webxr_vr_handinput.ts create mode 100644 examples-testing/examples/webxr_vr_panorama.ts create mode 100644 examples-testing/examples/webxr_vr_panorama_depth.ts create mode 100644 examples-testing/examples/webxr_vr_rollercoaster.ts create mode 100644 examples-testing/examples/webxr_vr_sandbox.ts create mode 100644 examples-testing/examples/webxr_vr_video.ts create mode 100644 examples-testing/examples/webxr_xr_controls_transform.ts create mode 100644 examples-testing/examples/webxr_xr_dragging_custom_depth.ts diff --git a/examples-testing/examples/css2d_label.ts b/examples-testing/examples/css2d_label.ts new file mode 100644 index 000000000..48a2d1f05 --- /dev/null +++ b/examples-testing/examples/css2d_label.ts @@ -0,0 +1,186 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { CSS2DRenderer, CSS2DObject } from 'three/addons/renderers/CSS2DRenderer.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let gui; + +let camera, scene, renderer, labelRenderer; + +const layers = { + 'Toggle Name': function () { + camera.layers.toggle(0); + }, + 'Toggle Mass': function () { + camera.layers.toggle(1); + }, + 'Enable All': function () { + camera.layers.enableAll(); + }, + + 'Disable All': function () { + camera.layers.disableAll(); + }, +}; + +const clock = new THREE.Clock(); +const textureLoader = new THREE.TextureLoader(); + +let moon; + +init(); +animate(); + +function init() { + const EARTH_RADIUS = 1; + const MOON_RADIUS = 0.27; + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200); + camera.position.set(10, 5, 20); + camera.layers.enableAll(); + + scene = new THREE.Scene(); + + const dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.position.set(0, 0, 1); + dirLight.layers.enableAll(); + scene.add(dirLight); + + const axesHelper = new THREE.AxesHelper(5); + axesHelper.layers.enableAll(); + scene.add(axesHelper); + + // + + const earthGeometry = new THREE.SphereGeometry(EARTH_RADIUS, 16, 16); + const earthMaterial = new THREE.MeshPhongMaterial({ + specular: 0x333333, + shininess: 5, + map: textureLoader.load('textures/planets/earth_atmos_2048.jpg'), + specularMap: textureLoader.load('textures/planets/earth_specular_2048.jpg'), + normalMap: textureLoader.load('textures/planets/earth_normal_2048.jpg'), + normalScale: new THREE.Vector2(0.85, 0.85), + }); + earthMaterial.map.colorSpace = THREE.SRGBColorSpace; + const earth = new THREE.Mesh(earthGeometry, earthMaterial); + scene.add(earth); + + const moonGeometry = new THREE.SphereGeometry(MOON_RADIUS, 16, 16); + const moonMaterial = new THREE.MeshPhongMaterial({ + shininess: 5, + map: textureLoader.load('textures/planets/moon_1024.jpg'), + }); + moonMaterial.map.colorSpace = THREE.SRGBColorSpace; + moon = new THREE.Mesh(moonGeometry, moonMaterial); + scene.add(moon); + + // + + earth.layers.enableAll(); + moon.layers.enableAll(); + + const earthDiv = document.createElement('div'); + earthDiv.className = 'label'; + earthDiv.textContent = 'Earth'; + earthDiv.style.backgroundColor = 'transparent'; + + const earthLabel = new CSS2DObject(earthDiv); + earthLabel.position.set(1.5 * EARTH_RADIUS, 0, 0); + earthLabel.center.set(0, 1); + earth.add(earthLabel); + earthLabel.layers.set(0); + + const earthMassDiv = document.createElement('div'); + earthMassDiv.className = 'label'; + earthMassDiv.textContent = '5.97237e24 kg'; + earthMassDiv.style.backgroundColor = 'transparent'; + + const earthMassLabel = new CSS2DObject(earthMassDiv); + earthMassLabel.position.set(1.5 * EARTH_RADIUS, 0, 0); + earthMassLabel.center.set(0, 0); + earth.add(earthMassLabel); + earthMassLabel.layers.set(1); + + const moonDiv = document.createElement('div'); + moonDiv.className = 'label'; + moonDiv.textContent = 'Moon'; + moonDiv.style.backgroundColor = 'transparent'; + + const moonLabel = new CSS2DObject(moonDiv); + moonLabel.position.set(1.5 * MOON_RADIUS, 0, 0); + moonLabel.center.set(0, 1); + moon.add(moonLabel); + moonLabel.layers.set(0); + + const moonMassDiv = document.createElement('div'); + moonMassDiv.className = 'label'; + moonMassDiv.textContent = '7.342e22 kg'; + moonMassDiv.style.backgroundColor = 'transparent'; + + const moonMassLabel = new CSS2DObject(moonMassDiv); + moonMassLabel.position.set(1.5 * MOON_RADIUS, 0, 0); + moonMassLabel.center.set(0, 0); + moon.add(moonMassLabel); + moonMassLabel.layers.set(1); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + labelRenderer = new CSS2DRenderer(); + labelRenderer.setSize(window.innerWidth, window.innerHeight); + labelRenderer.domElement.style.position = 'absolute'; + labelRenderer.domElement.style.top = '0px'; + document.body.appendChild(labelRenderer.domElement); + + const controls = new OrbitControls(camera, labelRenderer.domElement); + controls.minDistance = 5; + controls.maxDistance = 100; + + // + + window.addEventListener('resize', onWindowResize); + + initGui(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + labelRenderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + requestAnimationFrame(animate); + + const elapsed = clock.getElapsedTime(); + + moon.position.set(Math.sin(elapsed) * 5, 0, Math.cos(elapsed) * 5); + + renderer.render(scene, camera); + labelRenderer.render(scene, camera); +} + +// + +function initGui() { + gui = new GUI(); + + gui.title('Camera Layers'); + + gui.add(layers, 'Toggle Name'); + gui.add(layers, 'Toggle Mass'); + gui.add(layers, 'Enable All'); + gui.add(layers, 'Disable All'); + + gui.open(); +} diff --git a/examples-testing/examples/css3d_molecules.ts b/examples-testing/examples/css3d_molecules.ts new file mode 100644 index 000000000..538472607 --- /dev/null +++ b/examples-testing/examples/css3d_molecules.ts @@ -0,0 +1,353 @@ +import * as THREE from 'three'; + +import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; +import { PDBLoader } from 'three/addons/loaders/PDBLoader.js'; +import { CSS3DRenderer, CSS3DObject, CSS3DSprite } from 'three/addons/renderers/CSS3DRenderer.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer; +let controls; +let root; + +const objects = []; +const tmpVec1 = new THREE.Vector3(); +const tmpVec2 = new THREE.Vector3(); +const tmpVec3 = new THREE.Vector3(); +const tmpVec4 = new THREE.Vector3(); +const offset = new THREE.Vector3(); + +const VIZ_TYPE = { + Atoms: 0, + Bonds: 1, + 'Atoms + Bonds': 2, +}; + +const MOLECULES = { + Ethanol: 'ethanol.pdb', + Aspirin: 'aspirin.pdb', + Caffeine: 'caffeine.pdb', + Nicotine: 'nicotine.pdb', + LSD: 'lsd.pdb', + Cocaine: 'cocaine.pdb', + Cholesterol: 'cholesterol.pdb', + Lycopene: 'lycopene.pdb', + Glucose: 'glucose.pdb', + 'Aluminium oxide': 'Al2O3.pdb', + Cubane: 'cubane.pdb', + Copper: 'cu.pdb', + Fluorite: 'caf2.pdb', + Salt: 'nacl.pdb', + 'YBCO superconductor': 'ybco.pdb', + Buckyball: 'buckyball.pdb', + // 'Diamond': 'diamond.pdb', + Graphite: 'graphite.pdb', +}; + +const params = { + vizType: 2, + molecule: 'caffeine.pdb', +}; + +const loader = new PDBLoader(); +const colorSpriteMap = {}; +const baseSprite = document.createElement('img'); + +init(); +animate(); + +function init() { + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 5000); + camera.position.z = 1000; + + scene = new THREE.Scene(); + + root = new THREE.Object3D(); + scene.add(root); + + // + + renderer = new CSS3DRenderer(); + renderer.setSize(window.innerWidth, window.innerHeight); + document.getElementById('container').appendChild(renderer.domElement); + + // + + controls = new TrackballControls(camera, renderer.domElement); + controls.rotateSpeed = 0.5; + + // + + baseSprite.onload = function () { + loadMolecule(params.molecule); + }; + + baseSprite.src = 'textures/sprites/ball.png'; + + // + + window.addEventListener('resize', onWindowResize); + + // + + const gui = new GUI(); + + gui.add(params, 'vizType', VIZ_TYPE).onChange(changeVizType); + gui.add(params, 'molecule', MOLECULES).onChange(loadMolecule); + gui.open(); +} + +function changeVizType(value) { + if (value === 0) showAtoms(); + else if (value === 1) showBonds(); + else showAtomsBonds(); +} + +// + +function showAtoms() { + for (let i = 0; i < objects.length; i++) { + const object = objects[i]; + + if (object instanceof CSS3DSprite) { + object.element.style.display = ''; + object.visible = true; + } else { + object.element.style.display = 'none'; + object.visible = false; + } + } +} + +function showBonds() { + for (let i = 0; i < objects.length; i++) { + const object = objects[i]; + + if (object instanceof CSS3DSprite) { + object.element.style.display = 'none'; + object.visible = false; + } else { + object.element.style.display = ''; + object.element.style.height = object.userData.bondLengthFull; + object.visible = true; + } + } +} + +function showAtomsBonds() { + for (let i = 0; i < objects.length; i++) { + const object = objects[i]; + + object.element.style.display = ''; + object.visible = true; + + if (!(object instanceof CSS3DSprite)) { + object.element.style.height = object.userData.bondLengthShort; + } + } +} + +// + +function colorify(ctx, width, height, color) { + const r = color.r, + g = color.g, + b = color.b; + + const imageData = ctx.getImageData(0, 0, width, height); + const data = imageData.data; + + for (let i = 0, l = data.length; i < l; i += 4) { + data[i + 0] *= r; + data[i + 1] *= g; + data[i + 2] *= b; + } + + ctx.putImageData(imageData, 0, 0); +} + +function imageToCanvas(image) { + const width = image.width; + const height = image.height; + + const canvas = document.createElement('canvas'); + + canvas.width = width; + canvas.height = height; + + const context = canvas.getContext('2d'); + context.drawImage(image, 0, 0, width, height); + + return canvas; +} + +// + +function loadMolecule(model) { + const url = 'models/pdb/' + model; + + for (let i = 0; i < objects.length; i++) { + const object = objects[i]; + object.parent.remove(object); + } + + objects.length = 0; + + loader.load(url, function (pdb) { + const geometryAtoms = pdb.geometryAtoms; + const geometryBonds = pdb.geometryBonds; + const json = pdb.json; + + geometryAtoms.computeBoundingBox(); + geometryAtoms.boundingBox.getCenter(offset).negate(); + + geometryAtoms.translate(offset.x, offset.y, offset.z); + geometryBonds.translate(offset.x, offset.y, offset.z); + + const positionAtoms = geometryAtoms.getAttribute('position'); + const colorAtoms = geometryAtoms.getAttribute('color'); + + const position = new THREE.Vector3(); + const color = new THREE.Color(); + + for (let i = 0; i < positionAtoms.count; i++) { + position.fromBufferAttribute(positionAtoms, i); + color.fromBufferAttribute(colorAtoms, i); + + const atomJSON = json.atoms[i]; + const element = atomJSON[4]; + + if (!colorSpriteMap[element]) { + const canvas = imageToCanvas(baseSprite); + const context = canvas.getContext('2d'); + + colorify(context, canvas.width, canvas.height, color); + + const dataUrl = canvas.toDataURL(); + + colorSpriteMap[element] = dataUrl; + } + + const colorSprite = colorSpriteMap[element]; + + const atom = document.createElement('img'); + atom.src = colorSprite; + + const object = new CSS3DSprite(atom); + object.position.copy(position); + object.position.multiplyScalar(75); + + object.matrixAutoUpdate = false; + object.updateMatrix(); + + root.add(object); + + objects.push(object); + } + + const positionBonds = geometryBonds.getAttribute('position'); + + const start = new THREE.Vector3(); + const end = new THREE.Vector3(); + + for (let i = 0; i < positionBonds.count; i += 2) { + start.fromBufferAttribute(positionBonds, i); + end.fromBufferAttribute(positionBonds, i + 1); + + start.multiplyScalar(75); + end.multiplyScalar(75); + + tmpVec1.subVectors(end, start); + const bondLength = tmpVec1.length() - 50; + + // + + let bond = document.createElement('div'); + bond.className = 'bond'; + bond.style.height = bondLength + 'px'; + + let object = new CSS3DObject(bond); + object.position.copy(start); + object.position.lerp(end, 0.5); + + object.userData.bondLengthShort = bondLength + 'px'; + object.userData.bondLengthFull = bondLength + 55 + 'px'; + + // + + const axis = tmpVec2.set(0, 1, 0).cross(tmpVec1); + const radians = Math.acos(tmpVec3.set(0, 1, 0).dot(tmpVec4.copy(tmpVec1).normalize())); + + const objMatrix = new THREE.Matrix4().makeRotationAxis(axis.normalize(), radians); + object.matrix.copy(objMatrix); + object.quaternion.setFromRotationMatrix(object.matrix); + + object.matrixAutoUpdate = false; + object.updateMatrix(); + + root.add(object); + + objects.push(object); + + // + + const joint = new THREE.Object3D(); + joint.position.copy(start); + joint.position.lerp(end, 0.5); + + joint.matrix.copy(objMatrix); + joint.quaternion.setFromRotationMatrix(joint.matrix); + + joint.matrixAutoUpdate = false; + joint.updateMatrix(); + + bond = document.createElement('div'); + bond.className = 'bond'; + bond.style.height = bondLength + 'px'; + + object = new CSS3DObject(bond); + object.rotation.y = Math.PI / 2; + + object.matrixAutoUpdate = false; + object.updateMatrix(); + + object.userData.bondLengthShort = bondLength + 'px'; + object.userData.bondLengthFull = bondLength + 55 + 'px'; + + object.userData.joint = joint; + + joint.add(object); + root.add(joint); + + objects.push(object); + } + + //console.log( "CSS3DObjects:", objects.length ); + + changeVizType(params.vizType); + }); +} + +// + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + requestAnimationFrame(animate); + controls.update(); + + const time = Date.now() * 0.0004; + + root.rotation.x = time; + root.rotation.y = time * 0.7; + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/css3d_orthographic.ts b/examples-testing/examples/css3d_orthographic.ts new file mode 100644 index 000000000..4aabbed08 --- /dev/null +++ b/examples-testing/examples/css3d_orthographic.ts @@ -0,0 +1,208 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { CSS3DRenderer, CSS3DObject } from 'three/addons/renderers/CSS3DRenderer.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer; + +let scene2, renderer2; + +const frustumSize = 500; + +init(); +animate(); + +function init() { + const aspect = window.innerWidth / window.innerHeight; + camera = new THREE.OrthographicCamera( + (frustumSize * aspect) / -2, + (frustumSize * aspect) / 2, + frustumSize / 2, + frustumSize / -2, + 1, + 1000, + ); + + camera.position.set(-200, 200, 200); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + scene2 = new THREE.Scene(); + + const material = new THREE.MeshBasicMaterial({ + color: 0x000000, + wireframe: true, + wireframeLinewidth: 1, + side: THREE.DoubleSide, + }); + + // left + createPlane( + 100, + 100, + 'chocolate', + new THREE.Vector3(-50, 0, 0), + new THREE.Euler(0, -90 * THREE.MathUtils.DEG2RAD, 0), + ); + // right + createPlane(100, 100, 'saddlebrown', new THREE.Vector3(0, 0, 50), new THREE.Euler(0, 0, 0)); + // top + createPlane( + 100, + 100, + 'yellowgreen', + new THREE.Vector3(0, 50, 0), + new THREE.Euler(-90 * THREE.MathUtils.DEG2RAD, 0, 0), + ); + // bottom + createPlane( + 300, + 300, + 'seagreen', + new THREE.Vector3(0, -50, 0), + new THREE.Euler(-90 * THREE.MathUtils.DEG2RAD, 0, 0), + ); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + renderer2 = new CSS3DRenderer(); + renderer2.setSize(window.innerWidth, window.innerHeight); + renderer2.domElement.style.position = 'absolute'; + renderer2.domElement.style.top = 0; + document.body.appendChild(renderer2.domElement); + + const controls = new OrbitControls(camera, renderer2.domElement); + controls.minZoom = 0.5; + controls.maxZoom = 2; + + function createPlane(width, height, cssColor, pos, rot) { + const element = document.createElement('div'); + element.style.width = width + 'px'; + element.style.height = height + 'px'; + element.style.opacity = 0.75; + element.style.background = cssColor; + + const object = new CSS3DObject(element); + object.position.copy(pos); + object.rotation.copy(rot); + scene2.add(object); + + const geometry = new THREE.PlaneGeometry(width, height); + const mesh = new THREE.Mesh(geometry, material); + mesh.position.copy(object.position); + mesh.rotation.copy(object.rotation); + scene.add(mesh); + } + + window.addEventListener('resize', onWindowResize); + createPanel(); +} + +function onWindowResize() { + const aspect = window.innerWidth / window.innerHeight; + + camera.left = (-frustumSize * aspect) / 2; + camera.right = (frustumSize * aspect) / 2; + camera.top = frustumSize / 2; + camera.bottom = -frustumSize / 2; + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + renderer2.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + requestAnimationFrame(animate); + + renderer.render(scene, camera); + renderer2.render(scene2, camera); +} + +function createPanel() { + const panel = new GUI(); + const folder1 = panel.addFolder('camera setViewOffset').close(); + + const settings = { + setViewOffset() { + folder1.children[1].enable().setValue(window.innerWidth); + folder1.children[2].enable().setValue(window.innerHeight); + folder1.children[3].enable().setValue(0); + folder1.children[4].enable().setValue(0); + folder1.children[5].enable().setValue(window.innerWidth); + folder1.children[6].enable().setValue(window.innerHeight); + }, + fullWidth: 0, + fullHeight: 0, + offsetX: 0, + offsetY: 0, + width: 0, + height: 0, + clearViewOffset() { + folder1.children[1].setValue(0).disable(); + folder1.children[2].setValue(0).disable(); + folder1.children[3].setValue(0).disable(); + folder1.children[4].setValue(0).disable(); + folder1.children[5].setValue(0).disable(); + folder1.children[6].setValue(0).disable(); + camera.clearViewOffset(); + }, + }; + + folder1.add(settings, 'setViewOffset'); + folder1 + .add(settings, 'fullWidth', window.screen.width / 4, window.screen.width * 2, 1) + .onChange(val => updateCameraViewOffset({ fullWidth: val })) + .disable(); + folder1 + .add(settings, 'fullHeight', window.screen.height / 4, window.screen.height * 2, 1) + .onChange(val => updateCameraViewOffset({ fullHeight: val })) + .disable(); + folder1 + .add(settings, 'offsetX', 0, 256, 1) + .onChange(val => updateCameraViewOffset({ x: val })) + .disable(); + folder1 + .add(settings, 'offsetY', 0, 256, 1) + .onChange(val => updateCameraViewOffset({ y: val })) + .disable(); + folder1 + .add(settings, 'width', window.screen.width / 4, window.screen.width * 2, 1) + .onChange(val => updateCameraViewOffset({ width: val })) + .disable(); + folder1 + .add(settings, 'height', window.screen.height / 4, window.screen.height * 2, 1) + .onChange(val => updateCameraViewOffset({ height: val })) + .disable(); + folder1.add(settings, 'clearViewOffset'); +} + +function updateCameraViewOffset({ fullWidth, fullHeight, x, y, width, height }) { + if (!camera.view) { + camera.setViewOffset( + fullWidth || window.innerWidth, + fullHeight || window.innerHeight, + x || 0, + y || 0, + width || window.innerWidth, + height || window.innerHeight, + ); + } else { + camera.setViewOffset( + fullWidth || camera.view.fullWidth, + fullHeight || camera.view.fullHeight, + x || camera.view.offsetX, + y || camera.view.offsetY, + width || camera.view.width, + height || camera.view.height, + ); + } +} diff --git a/examples-testing/examples/css3d_periodictable.ts b/examples-testing/examples/css3d_periodictable.ts new file mode 100644 index 000000000..e3a33f796 --- /dev/null +++ b/examples-testing/examples/css3d_periodictable.ts @@ -0,0 +1,793 @@ +import * as THREE from 'three'; + +import TWEEN from 'three/addons/libs/tween.module.js'; +import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; +import { CSS3DRenderer, CSS3DObject } from 'three/addons/renderers/CSS3DRenderer.js'; + +const table = [ + 'H', + 'Hydrogen', + '1.00794', + 1, + 1, + 'He', + 'Helium', + '4.002602', + 18, + 1, + 'Li', + 'Lithium', + '6.941', + 1, + 2, + 'Be', + 'Beryllium', + '9.012182', + 2, + 2, + 'B', + 'Boron', + '10.811', + 13, + 2, + 'C', + 'Carbon', + '12.0107', + 14, + 2, + 'N', + 'Nitrogen', + '14.0067', + 15, + 2, + 'O', + 'Oxygen', + '15.9994', + 16, + 2, + 'F', + 'Fluorine', + '18.9984032', + 17, + 2, + 'Ne', + 'Neon', + '20.1797', + 18, + 2, + 'Na', + 'Sodium', + '22.98976...', + 1, + 3, + 'Mg', + 'Magnesium', + '24.305', + 2, + 3, + 'Al', + 'Aluminium', + '26.9815386', + 13, + 3, + 'Si', + 'Silicon', + '28.0855', + 14, + 3, + 'P', + 'Phosphorus', + '30.973762', + 15, + 3, + 'S', + 'Sulfur', + '32.065', + 16, + 3, + 'Cl', + 'Chlorine', + '35.453', + 17, + 3, + 'Ar', + 'Argon', + '39.948', + 18, + 3, + 'K', + 'Potassium', + '39.948', + 1, + 4, + 'Ca', + 'Calcium', + '40.078', + 2, + 4, + 'Sc', + 'Scandium', + '44.955912', + 3, + 4, + 'Ti', + 'Titanium', + '47.867', + 4, + 4, + 'V', + 'Vanadium', + '50.9415', + 5, + 4, + 'Cr', + 'Chromium', + '51.9961', + 6, + 4, + 'Mn', + 'Manganese', + '54.938045', + 7, + 4, + 'Fe', + 'Iron', + '55.845', + 8, + 4, + 'Co', + 'Cobalt', + '58.933195', + 9, + 4, + 'Ni', + 'Nickel', + '58.6934', + 10, + 4, + 'Cu', + 'Copper', + '63.546', + 11, + 4, + 'Zn', + 'Zinc', + '65.38', + 12, + 4, + 'Ga', + 'Gallium', + '69.723', + 13, + 4, + 'Ge', + 'Germanium', + '72.63', + 14, + 4, + 'As', + 'Arsenic', + '74.9216', + 15, + 4, + 'Se', + 'Selenium', + '78.96', + 16, + 4, + 'Br', + 'Bromine', + '79.904', + 17, + 4, + 'Kr', + 'Krypton', + '83.798', + 18, + 4, + 'Rb', + 'Rubidium', + '85.4678', + 1, + 5, + 'Sr', + 'Strontium', + '87.62', + 2, + 5, + 'Y', + 'Yttrium', + '88.90585', + 3, + 5, + 'Zr', + 'Zirconium', + '91.224', + 4, + 5, + 'Nb', + 'Niobium', + '92.90628', + 5, + 5, + 'Mo', + 'Molybdenum', + '95.96', + 6, + 5, + 'Tc', + 'Technetium', + '(98)', + 7, + 5, + 'Ru', + 'Ruthenium', + '101.07', + 8, + 5, + 'Rh', + 'Rhodium', + '102.9055', + 9, + 5, + 'Pd', + 'Palladium', + '106.42', + 10, + 5, + 'Ag', + 'Silver', + '107.8682', + 11, + 5, + 'Cd', + 'Cadmium', + '112.411', + 12, + 5, + 'In', + 'Indium', + '114.818', + 13, + 5, + 'Sn', + 'Tin', + '118.71', + 14, + 5, + 'Sb', + 'Antimony', + '121.76', + 15, + 5, + 'Te', + 'Tellurium', + '127.6', + 16, + 5, + 'I', + 'Iodine', + '126.90447', + 17, + 5, + 'Xe', + 'Xenon', + '131.293', + 18, + 5, + 'Cs', + 'Caesium', + '132.9054', + 1, + 6, + 'Ba', + 'Barium', + '132.9054', + 2, + 6, + 'La', + 'Lanthanum', + '138.90547', + 4, + 9, + 'Ce', + 'Cerium', + '140.116', + 5, + 9, + 'Pr', + 'Praseodymium', + '140.90765', + 6, + 9, + 'Nd', + 'Neodymium', + '144.242', + 7, + 9, + 'Pm', + 'Promethium', + '(145)', + 8, + 9, + 'Sm', + 'Samarium', + '150.36', + 9, + 9, + 'Eu', + 'Europium', + '151.964', + 10, + 9, + 'Gd', + 'Gadolinium', + '157.25', + 11, + 9, + 'Tb', + 'Terbium', + '158.92535', + 12, + 9, + 'Dy', + 'Dysprosium', + '162.5', + 13, + 9, + 'Ho', + 'Holmium', + '164.93032', + 14, + 9, + 'Er', + 'Erbium', + '167.259', + 15, + 9, + 'Tm', + 'Thulium', + '168.93421', + 16, + 9, + 'Yb', + 'Ytterbium', + '173.054', + 17, + 9, + 'Lu', + 'Lutetium', + '174.9668', + 18, + 9, + 'Hf', + 'Hafnium', + '178.49', + 4, + 6, + 'Ta', + 'Tantalum', + '180.94788', + 5, + 6, + 'W', + 'Tungsten', + '183.84', + 6, + 6, + 'Re', + 'Rhenium', + '186.207', + 7, + 6, + 'Os', + 'Osmium', + '190.23', + 8, + 6, + 'Ir', + 'Iridium', + '192.217', + 9, + 6, + 'Pt', + 'Platinum', + '195.084', + 10, + 6, + 'Au', + 'Gold', + '196.966569', + 11, + 6, + 'Hg', + 'Mercury', + '200.59', + 12, + 6, + 'Tl', + 'Thallium', + '204.3833', + 13, + 6, + 'Pb', + 'Lead', + '207.2', + 14, + 6, + 'Bi', + 'Bismuth', + '208.9804', + 15, + 6, + 'Po', + 'Polonium', + '(209)', + 16, + 6, + 'At', + 'Astatine', + '(210)', + 17, + 6, + 'Rn', + 'Radon', + '(222)', + 18, + 6, + 'Fr', + 'Francium', + '(223)', + 1, + 7, + 'Ra', + 'Radium', + '(226)', + 2, + 7, + 'Ac', + 'Actinium', + '(227)', + 4, + 10, + 'Th', + 'Thorium', + '232.03806', + 5, + 10, + 'Pa', + 'Protactinium', + '231.0588', + 6, + 10, + 'U', + 'Uranium', + '238.02891', + 7, + 10, + 'Np', + 'Neptunium', + '(237)', + 8, + 10, + 'Pu', + 'Plutonium', + '(244)', + 9, + 10, + 'Am', + 'Americium', + '(243)', + 10, + 10, + 'Cm', + 'Curium', + '(247)', + 11, + 10, + 'Bk', + 'Berkelium', + '(247)', + 12, + 10, + 'Cf', + 'Californium', + '(251)', + 13, + 10, + 'Es', + 'Einstenium', + '(252)', + 14, + 10, + 'Fm', + 'Fermium', + '(257)', + 15, + 10, + 'Md', + 'Mendelevium', + '(258)', + 16, + 10, + 'No', + 'Nobelium', + '(259)', + 17, + 10, + 'Lr', + 'Lawrencium', + '(262)', + 18, + 10, + 'Rf', + 'Rutherfordium', + '(267)', + 4, + 7, + 'Db', + 'Dubnium', + '(268)', + 5, + 7, + 'Sg', + 'Seaborgium', + '(271)', + 6, + 7, + 'Bh', + 'Bohrium', + '(272)', + 7, + 7, + 'Hs', + 'Hassium', + '(270)', + 8, + 7, + 'Mt', + 'Meitnerium', + '(276)', + 9, + 7, + 'Ds', + 'Darmstadium', + '(281)', + 10, + 7, + 'Rg', + 'Roentgenium', + '(280)', + 11, + 7, + 'Cn', + 'Copernicium', + '(285)', + 12, + 7, + 'Nh', + 'Nihonium', + '(286)', + 13, + 7, + 'Fl', + 'Flerovium', + '(289)', + 14, + 7, + 'Mc', + 'Moscovium', + '(290)', + 15, + 7, + 'Lv', + 'Livermorium', + '(293)', + 16, + 7, + 'Ts', + 'Tennessine', + '(294)', + 17, + 7, + 'Og', + 'Oganesson', + '(294)', + 18, + 7, +]; + +let camera, scene, renderer; +let controls; + +const objects = []; +const targets = { table: [], sphere: [], helix: [], grid: [] }; + +init(); +animate(); + +function init() { + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 3000; + + scene = new THREE.Scene(); + + // table + + for (let i = 0; i < table.length; i += 5) { + const element = document.createElement('div'); + element.className = 'element'; + element.style.backgroundColor = 'rgba(0,127,127,' + (Math.random() * 0.5 + 0.25) + ')'; + + const number = document.createElement('div'); + number.className = 'number'; + number.textContent = i / 5 + 1; + element.appendChild(number); + + const symbol = document.createElement('div'); + symbol.className = 'symbol'; + symbol.textContent = table[i]; + element.appendChild(symbol); + + const details = document.createElement('div'); + details.className = 'details'; + details.innerHTML = table[i + 1] + '
' + table[i + 2]; + element.appendChild(details); + + const objectCSS = new CSS3DObject(element); + objectCSS.position.x = Math.random() * 4000 - 2000; + objectCSS.position.y = Math.random() * 4000 - 2000; + objectCSS.position.z = Math.random() * 4000 - 2000; + scene.add(objectCSS); + + objects.push(objectCSS); + + // + + const object = new THREE.Object3D(); + object.position.x = table[i + 3] * 140 - 1330; + object.position.y = -(table[i + 4] * 180) + 990; + + targets.table.push(object); + } + + // sphere + + const vector = new THREE.Vector3(); + + for (let i = 0, l = objects.length; i < l; i++) { + const phi = Math.acos(-1 + (2 * i) / l); + const theta = Math.sqrt(l * Math.PI) * phi; + + const object = new THREE.Object3D(); + + object.position.setFromSphericalCoords(800, phi, theta); + + vector.copy(object.position).multiplyScalar(2); + + object.lookAt(vector); + + targets.sphere.push(object); + } + + // helix + + for (let i = 0, l = objects.length; i < l; i++) { + const theta = i * 0.175 + Math.PI; + const y = -(i * 8) + 450; + + const object = new THREE.Object3D(); + + object.position.setFromCylindricalCoords(900, theta, y); + + vector.x = object.position.x * 2; + vector.y = object.position.y; + vector.z = object.position.z * 2; + + object.lookAt(vector); + + targets.helix.push(object); + } + + // grid + + for (let i = 0; i < objects.length; i++) { + const object = new THREE.Object3D(); + + object.position.x = (i % 5) * 400 - 800; + object.position.y = -(Math.floor(i / 5) % 5) * 400 + 800; + object.position.z = Math.floor(i / 25) * 1000 - 2000; + + targets.grid.push(object); + } + + // + + renderer = new CSS3DRenderer(); + renderer.setSize(window.innerWidth, window.innerHeight); + document.getElementById('container').appendChild(renderer.domElement); + + // + + controls = new TrackballControls(camera, renderer.domElement); + controls.minDistance = 500; + controls.maxDistance = 6000; + controls.addEventListener('change', render); + + const buttonTable = document.getElementById('table'); + buttonTable.addEventListener('click', function () { + transform(targets.table, 2000); + }); + + const buttonSphere = document.getElementById('sphere'); + buttonSphere.addEventListener('click', function () { + transform(targets.sphere, 2000); + }); + + const buttonHelix = document.getElementById('helix'); + buttonHelix.addEventListener('click', function () { + transform(targets.helix, 2000); + }); + + const buttonGrid = document.getElementById('grid'); + buttonGrid.addEventListener('click', function () { + transform(targets.grid, 2000); + }); + + transform(targets.table, 2000); + + // + + window.addEventListener('resize', onWindowResize); +} + +function transform(targets, duration) { + TWEEN.removeAll(); + + for (let i = 0; i < objects.length; i++) { + const object = objects[i]; + const target = targets[i]; + + new TWEEN.Tween(object.position) + .to( + { x: target.position.x, y: target.position.y, z: target.position.z }, + Math.random() * duration + duration, + ) + .easing(TWEEN.Easing.Exponential.InOut) + .start(); + + new TWEEN.Tween(object.rotation) + .to( + { x: target.rotation.x, y: target.rotation.y, z: target.rotation.z }, + Math.random() * duration + duration, + ) + .easing(TWEEN.Easing.Exponential.InOut) + .start(); + } + + new TWEEN.Tween(this) + .to({}, duration * 2) + .onUpdate(render) + .start(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function animate() { + requestAnimationFrame(animate); + + TWEEN.update(); + + controls.update(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/css3d_sandbox.ts b/examples-testing/examples/css3d_sandbox.ts new file mode 100644 index 000000000..1088b84b1 --- /dev/null +++ b/examples-testing/examples/css3d_sandbox.ts @@ -0,0 +1,180 @@ +import * as THREE from 'three'; + +import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; +import { CSS3DRenderer, CSS3DObject } from 'three/addons/renderers/CSS3DRenderer.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer; + +let scene2, renderer2; + +let controls; + +init(); +animate(); + +function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(200, 200, 200); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + scene2 = new THREE.Scene(); + + const material = new THREE.MeshBasicMaterial({ + color: 0x000000, + wireframe: true, + wireframeLinewidth: 1, + side: THREE.DoubleSide, + }); + + // + + for (let i = 0; i < 10; i++) { + const element = document.createElement('div'); + element.style.width = '100px'; + element.style.height = '100px'; + element.style.opacity = i < 5 ? 0.5 : 1; + element.style.background = new THREE.Color(Math.random() * 0xffffff).getStyle(); + + const object = new CSS3DObject(element); + object.position.x = Math.random() * 200 - 100; + object.position.y = Math.random() * 200 - 100; + object.position.z = Math.random() * 200 - 100; + object.rotation.x = Math.random(); + object.rotation.y = Math.random(); + object.rotation.z = Math.random(); + object.scale.x = Math.random() + 0.5; + object.scale.y = Math.random() + 0.5; + scene2.add(object); + + const geometry = new THREE.PlaneGeometry(100, 100); + const mesh = new THREE.Mesh(geometry, material); + mesh.position.copy(object.position); + mesh.rotation.copy(object.rotation); + mesh.scale.copy(object.scale); + scene.add(mesh); + } + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + renderer2 = new CSS3DRenderer(); + renderer2.setSize(window.innerWidth, window.innerHeight); + renderer2.domElement.style.position = 'absolute'; + renderer2.domElement.style.top = 0; + document.body.appendChild(renderer2.domElement); + + controls = new TrackballControls(camera, renderer2.domElement); + + window.addEventListener('resize', onWindowResize); + createPanel(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + renderer2.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + requestAnimationFrame(animate); + + controls.update(); + + renderer.render(scene, camera); + renderer2.render(scene2, camera); +} + +function createPanel() { + const panel = new GUI(); + const folder1 = panel.addFolder('camera setViewOffset').close(); + + const settings = { + setViewOffset() { + folder1.children[1].enable().setValue(window.innerWidth); + folder1.children[2].enable().setValue(window.innerHeight); + folder1.children[3].enable().setValue(0); + folder1.children[4].enable().setValue(0); + folder1.children[5].enable().setValue(window.innerWidth); + folder1.children[6].enable().setValue(window.innerHeight); + }, + fullWidth: 0, + fullHeight: 0, + offsetX: 0, + offsetY: 0, + width: 0, + height: 0, + clearViewOffset() { + folder1.children[1].setValue(0).disable(); + folder1.children[2].setValue(0).disable(); + folder1.children[3].setValue(0).disable(); + folder1.children[4].setValue(0).disable(); + folder1.children[5].setValue(0).disable(); + folder1.children[6].setValue(0).disable(); + camera.clearViewOffset(); + }, + }; + + folder1.add(settings, 'setViewOffset'); + folder1 + .add(settings, 'fullWidth', window.screen.width / 4, window.screen.width * 2, 1) + .onChange(val => updateCameraViewOffset({ fullWidth: val })) + .disable(); + folder1 + .add(settings, 'fullHeight', window.screen.height / 4, window.screen.height * 2, 1) + .onChange(val => updateCameraViewOffset({ fullHeight: val })) + .disable(); + folder1 + .add(settings, 'offsetX', 0, 256, 1) + .onChange(val => updateCameraViewOffset({ x: val })) + .disable(); + folder1 + .add(settings, 'offsetY', 0, 256, 1) + .onChange(val => updateCameraViewOffset({ y: val })) + .disable(); + folder1 + .add(settings, 'width', window.screen.width / 4, window.screen.width * 2, 1) + .onChange(val => updateCameraViewOffset({ width: val })) + .disable(); + folder1 + .add(settings, 'height', window.screen.height / 4, window.screen.height * 2, 1) + .onChange(val => updateCameraViewOffset({ height: val })) + .disable(); + folder1.add(settings, 'clearViewOffset'); +} + +function updateCameraViewOffset({ fullWidth, fullHeight, x, y, width, height }) { + if (!camera.view) { + camera.setViewOffset( + fullWidth || window.innerWidth, + fullHeight || window.innerHeight, + x || 0, + y || 0, + width || window.innerWidth, + height || window.innerHeight, + ); + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + } else { + camera.setViewOffset( + fullWidth || camera.view.fullWidth, + fullHeight || camera.view.fullHeight, + x || camera.view.offsetX, + y || camera.view.offsetY, + width || camera.view.width, + height || camera.view.height, + ); + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + } +} diff --git a/examples-testing/examples/css3d_sprites.ts b/examples-testing/examples/css3d_sprites.ts new file mode 100644 index 000000000..dfe24e79d --- /dev/null +++ b/examples-testing/examples/css3d_sprites.ts @@ -0,0 +1,157 @@ +import * as THREE from 'three'; + +import TWEEN from 'three/addons/libs/tween.module.js'; +import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; +import { CSS3DRenderer, CSS3DSprite } from 'three/addons/renderers/CSS3DRenderer.js'; + +let camera, scene, renderer; +let controls; + +const particlesTotal = 512; +const positions = []; +const objects = []; +let current = 0; + +init(); +animate(); + +function init() { + camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 5000); + camera.position.set(600, 400, 1500); + camera.lookAt(0, 0, 0); + + scene = new THREE.Scene(); + + const image = document.createElement('img'); + image.addEventListener('load', function () { + for (let i = 0; i < particlesTotal; i++) { + const object = new CSS3DSprite(image.cloneNode()); + (object.position.x = Math.random() * 4000 - 2000), + (object.position.y = Math.random() * 4000 - 2000), + (object.position.z = Math.random() * 4000 - 2000); + scene.add(object); + + objects.push(object); + } + + transition(); + }); + image.src = 'textures/sprite.png'; + + // Plane + + const amountX = 16; + const amountZ = 32; + const separationPlane = 150; + const offsetX = ((amountX - 1) * separationPlane) / 2; + const offsetZ = ((amountZ - 1) * separationPlane) / 2; + + for (let i = 0; i < particlesTotal; i++) { + const x = (i % amountX) * separationPlane; + const z = Math.floor(i / amountX) * separationPlane; + const y = (Math.sin(x * 0.5) + Math.sin(z * 0.5)) * 200; + + positions.push(x - offsetX, y, z - offsetZ); + } + + // Cube + + const amount = 8; + const separationCube = 150; + const offset = ((amount - 1) * separationCube) / 2; + + for (let i = 0; i < particlesTotal; i++) { + const x = (i % amount) * separationCube; + const y = Math.floor((i / amount) % amount) * separationCube; + const z = Math.floor(i / (amount * amount)) * separationCube; + + positions.push(x - offset, y - offset, z - offset); + } + + // Random + + for (let i = 0; i < particlesTotal; i++) { + positions.push(Math.random() * 4000 - 2000, Math.random() * 4000 - 2000, Math.random() * 4000 - 2000); + } + + // Sphere + + const radius = 750; + + for (let i = 0; i < particlesTotal; i++) { + const phi = Math.acos(-1 + (2 * i) / particlesTotal); + const theta = Math.sqrt(particlesTotal * Math.PI) * phi; + + positions.push( + radius * Math.cos(theta) * Math.sin(phi), + radius * Math.sin(theta) * Math.sin(phi), + radius * Math.cos(phi), + ); + } + + // + + renderer = new CSS3DRenderer(); + renderer.setSize(window.innerWidth, window.innerHeight); + document.getElementById('container').appendChild(renderer.domElement); + + // + + controls = new TrackballControls(camera, renderer.domElement); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function transition() { + const offset = current * particlesTotal * 3; + const duration = 2000; + + for (let i = 0, j = offset; i < particlesTotal; i++, j += 3) { + const object = objects[i]; + + new TWEEN.Tween(object.position) + .to( + { + x: positions[j], + y: positions[j + 1], + z: positions[j + 2], + }, + Math.random() * duration + duration, + ) + .easing(TWEEN.Easing.Exponential.InOut) + .start(); + } + + new TWEEN.Tween(this) + .to({}, duration * 3) + .onComplete(transition) + .start(); + + current = (current + 1) % 4; +} + +function animate() { + requestAnimationFrame(animate); + + TWEEN.update(); + controls.update(); + + const time = performance.now(); + + for (let i = 0, l = objects.length; i < l; i++) { + const object = objects[i]; + const scale = Math.sin((Math.floor(object.position.x) + time) * 0.002) * 0.3 + 1; + object.scale.set(scale, scale, scale); + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/css3d_youtube.ts b/examples-testing/examples/css3d_youtube.ts new file mode 100644 index 000000000..62652f87f --- /dev/null +++ b/examples-testing/examples/css3d_youtube.ts @@ -0,0 +1,79 @@ +import * as THREE from 'three'; + +import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; +import { CSS3DRenderer, CSS3DObject } from 'three/addons/renderers/CSS3DRenderer.js'; + +let camera, scene, renderer; +let controls; + +function Element(id, x, y, z, ry) { + const div = document.createElement('div'); + div.style.width = '480px'; + div.style.height = '360px'; + div.style.backgroundColor = '#000'; + + const iframe = document.createElement('iframe'); + iframe.style.width = '480px'; + iframe.style.height = '360px'; + iframe.style.border = '0px'; + iframe.src = ['https://www.youtube.com/embed/', id, '?rel=0'].join(''); + div.appendChild(iframe); + + const object = new CSS3DObject(div); + object.position.set(x, y, z); + object.rotation.y = ry; + + return object; +} + +init(); +animate(); + +function init() { + const container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 5000); + camera.position.set(500, 350, 750); + + scene = new THREE.Scene(); + + renderer = new CSS3DRenderer(); + renderer.setSize(window.innerWidth, window.innerHeight); + container.appendChild(renderer.domElement); + + const group = new THREE.Group(); + group.add(new Element('SJOz3qjfQXU', 0, 0, 240, 0)); + group.add(new Element('Y2-xZ-1HE-Q', 240, 0, 0, Math.PI / 2)); + group.add(new Element('IrydklNpcFI', 0, 0, -240, Math.PI)); + group.add(new Element('9ubytEsCaS0', -240, 0, 0, -Math.PI / 2)); + scene.add(group); + + controls = new TrackballControls(camera, renderer.domElement); + controls.rotateSpeed = 4; + + window.addEventListener('resize', onWindowResize); + + // Block iframe events when dragging camera + + const blocker = document.getElementById('blocker'); + blocker.style.display = 'none'; + + controls.addEventListener('start', function () { + blocker.style.display = ''; + }); + controls.addEventListener('end', function () { + blocker.style.display = 'none'; + }); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + requestAnimationFrame(animate); + controls.update(); + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/games_fps.ts b/examples-testing/examples/games_fps.ts new file mode 100644 index 000000000..4c459f9bc --- /dev/null +++ b/examples-testing/examples/games_fps.ts @@ -0,0 +1,372 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +import { Octree } from 'three/addons/math/Octree.js'; +import { OctreeHelper } from 'three/addons/helpers/OctreeHelper.js'; + +import { Capsule } from 'three/addons/math/Capsule.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +const clock = new THREE.Clock(); + +const scene = new THREE.Scene(); +scene.background = new THREE.Color(0x88ccee); +scene.fog = new THREE.Fog(0x88ccee, 0, 50); + +const camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 1000); +camera.rotation.order = 'YXZ'; + +const fillLight1 = new THREE.HemisphereLight(0x8dc1de, 0x00668d, 1.5); +fillLight1.position.set(2, 1, 1); +scene.add(fillLight1); + +const directionalLight = new THREE.DirectionalLight(0xffffff, 2.5); +directionalLight.position.set(-5, 25, -1); +directionalLight.castShadow = true; +directionalLight.shadow.camera.near = 0.01; +directionalLight.shadow.camera.far = 500; +directionalLight.shadow.camera.right = 30; +directionalLight.shadow.camera.left = -30; +directionalLight.shadow.camera.top = 30; +directionalLight.shadow.camera.bottom = -30; +directionalLight.shadow.mapSize.width = 1024; +directionalLight.shadow.mapSize.height = 1024; +directionalLight.shadow.radius = 4; +directionalLight.shadow.bias = -0.00006; +scene.add(directionalLight); + +const container = document.getElementById('container'); + +const renderer = new THREE.WebGLRenderer({ antialias: true }); +renderer.setPixelRatio(window.devicePixelRatio); +renderer.setSize(window.innerWidth, window.innerHeight); +renderer.setAnimationLoop(animate); +renderer.shadowMap.enabled = true; +renderer.shadowMap.type = THREE.VSMShadowMap; +renderer.toneMapping = THREE.ACESFilmicToneMapping; +container.appendChild(renderer.domElement); + +const stats = new Stats(); +stats.domElement.style.position = 'absolute'; +stats.domElement.style.top = '0px'; +container.appendChild(stats.domElement); + +const GRAVITY = 30; + +const NUM_SPHERES = 100; +const SPHERE_RADIUS = 0.2; + +const STEPS_PER_FRAME = 5; + +const sphereGeometry = new THREE.IcosahedronGeometry(SPHERE_RADIUS, 5); +const sphereMaterial = new THREE.MeshLambertMaterial({ color: 0xdede8d }); + +const spheres = []; +let sphereIdx = 0; + +for (let i = 0; i < NUM_SPHERES; i++) { + const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); + sphere.castShadow = true; + sphere.receiveShadow = true; + + scene.add(sphere); + + spheres.push({ + mesh: sphere, + collider: new THREE.Sphere(new THREE.Vector3(0, -100, 0), SPHERE_RADIUS), + velocity: new THREE.Vector3(), + }); +} + +const worldOctree = new Octree(); + +const playerCollider = new Capsule(new THREE.Vector3(0, 0.35, 0), new THREE.Vector3(0, 1, 0), 0.35); + +const playerVelocity = new THREE.Vector3(); +const playerDirection = new THREE.Vector3(); + +let playerOnFloor = false; +let mouseTime = 0; + +const keyStates = {}; + +const vector1 = new THREE.Vector3(); +const vector2 = new THREE.Vector3(); +const vector3 = new THREE.Vector3(); + +document.addEventListener('keydown', event => { + keyStates[event.code] = true; +}); + +document.addEventListener('keyup', event => { + keyStates[event.code] = false; +}); + +container.addEventListener('mousedown', () => { + document.body.requestPointerLock(); + + mouseTime = performance.now(); +}); + +document.addEventListener('mouseup', () => { + if (document.pointerLockElement !== null) throwBall(); +}); + +document.body.addEventListener('mousemove', event => { + if (document.pointerLockElement === document.body) { + camera.rotation.y -= event.movementX / 500; + camera.rotation.x -= event.movementY / 500; + } +}); + +window.addEventListener('resize', onWindowResize); + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function throwBall() { + const sphere = spheres[sphereIdx]; + + camera.getWorldDirection(playerDirection); + + sphere.collider.center.copy(playerCollider.end).addScaledVector(playerDirection, playerCollider.radius * 1.5); + + // throw the ball with more force if we hold the button longer, and if we move forward + + const impulse = 15 + 30 * (1 - Math.exp((mouseTime - performance.now()) * 0.001)); + + sphere.velocity.copy(playerDirection).multiplyScalar(impulse); + sphere.velocity.addScaledVector(playerVelocity, 2); + + sphereIdx = (sphereIdx + 1) % spheres.length; +} + +function playerCollisions() { + const result = worldOctree.capsuleIntersect(playerCollider); + + playerOnFloor = false; + + if (result) { + playerOnFloor = result.normal.y > 0; + + if (!playerOnFloor) { + playerVelocity.addScaledVector(result.normal, -result.normal.dot(playerVelocity)); + } + + if (result.depth >= 1e-10) { + playerCollider.translate(result.normal.multiplyScalar(result.depth)); + } + } +} + +function updatePlayer(deltaTime) { + let damping = Math.exp(-4 * deltaTime) - 1; + + if (!playerOnFloor) { + playerVelocity.y -= GRAVITY * deltaTime; + + // small air resistance + damping *= 0.1; + } + + playerVelocity.addScaledVector(playerVelocity, damping); + + const deltaPosition = playerVelocity.clone().multiplyScalar(deltaTime); + playerCollider.translate(deltaPosition); + + playerCollisions(); + + camera.position.copy(playerCollider.end); +} + +function playerSphereCollision(sphere) { + const center = vector1.addVectors(playerCollider.start, playerCollider.end).multiplyScalar(0.5); + + const sphere_center = sphere.collider.center; + + const r = playerCollider.radius + sphere.collider.radius; + const r2 = r * r; + + // approximation: player = 3 spheres + + for (const point of [playerCollider.start, playerCollider.end, center]) { + const d2 = point.distanceToSquared(sphere_center); + + if (d2 < r2) { + const normal = vector1.subVectors(point, sphere_center).normalize(); + const v1 = vector2.copy(normal).multiplyScalar(normal.dot(playerVelocity)); + const v2 = vector3.copy(normal).multiplyScalar(normal.dot(sphere.velocity)); + + playerVelocity.add(v2).sub(v1); + sphere.velocity.add(v1).sub(v2); + + const d = (r - Math.sqrt(d2)) / 2; + sphere_center.addScaledVector(normal, -d); + } + } +} + +function spheresCollisions() { + for (let i = 0, length = spheres.length; i < length; i++) { + const s1 = spheres[i]; + + for (let j = i + 1; j < length; j++) { + const s2 = spheres[j]; + + const d2 = s1.collider.center.distanceToSquared(s2.collider.center); + const r = s1.collider.radius + s2.collider.radius; + const r2 = r * r; + + if (d2 < r2) { + const normal = vector1.subVectors(s1.collider.center, s2.collider.center).normalize(); + const v1 = vector2.copy(normal).multiplyScalar(normal.dot(s1.velocity)); + const v2 = vector3.copy(normal).multiplyScalar(normal.dot(s2.velocity)); + + s1.velocity.add(v2).sub(v1); + s2.velocity.add(v1).sub(v2); + + const d = (r - Math.sqrt(d2)) / 2; + + s1.collider.center.addScaledVector(normal, d); + s2.collider.center.addScaledVector(normal, -d); + } + } + } +} + +function updateSpheres(deltaTime) { + spheres.forEach(sphere => { + sphere.collider.center.addScaledVector(sphere.velocity, deltaTime); + + const result = worldOctree.sphereIntersect(sphere.collider); + + if (result) { + sphere.velocity.addScaledVector(result.normal, -result.normal.dot(sphere.velocity) * 1.5); + sphere.collider.center.add(result.normal.multiplyScalar(result.depth)); + } else { + sphere.velocity.y -= GRAVITY * deltaTime; + } + + const damping = Math.exp(-1.5 * deltaTime) - 1; + sphere.velocity.addScaledVector(sphere.velocity, damping); + + playerSphereCollision(sphere); + }); + + spheresCollisions(); + + for (const sphere of spheres) { + sphere.mesh.position.copy(sphere.collider.center); + } +} + +function getForwardVector() { + camera.getWorldDirection(playerDirection); + playerDirection.y = 0; + playerDirection.normalize(); + + return playerDirection; +} + +function getSideVector() { + camera.getWorldDirection(playerDirection); + playerDirection.y = 0; + playerDirection.normalize(); + playerDirection.cross(camera.up); + + return playerDirection; +} + +function controls(deltaTime) { + // gives a bit of air control + const speedDelta = deltaTime * (playerOnFloor ? 25 : 8); + + if (keyStates['KeyW']) { + playerVelocity.add(getForwardVector().multiplyScalar(speedDelta)); + } + + if (keyStates['KeyS']) { + playerVelocity.add(getForwardVector().multiplyScalar(-speedDelta)); + } + + if (keyStates['KeyA']) { + playerVelocity.add(getSideVector().multiplyScalar(-speedDelta)); + } + + if (keyStates['KeyD']) { + playerVelocity.add(getSideVector().multiplyScalar(speedDelta)); + } + + if (playerOnFloor) { + if (keyStates['Space']) { + playerVelocity.y = 15; + } + } +} + +const loader = new GLTFLoader().setPath('./models/gltf/'); + +loader.load('collision-world.glb', gltf => { + scene.add(gltf.scene); + + worldOctree.fromGraphNode(gltf.scene); + + gltf.scene.traverse(child => { + if (child.isMesh) { + child.castShadow = true; + child.receiveShadow = true; + + if (child.material.map) { + child.material.map.anisotropy = 4; + } + } + }); + + const helper = new OctreeHelper(worldOctree); + helper.visible = false; + scene.add(helper); + + const gui = new GUI({ width: 200 }); + gui.add({ debug: false }, 'debug').onChange(function (value) { + helper.visible = value; + }); +}); + +function teleportPlayerIfOob() { + if (camera.position.y <= -25) { + playerCollider.start.set(0, 0.35, 0); + playerCollider.end.set(0, 1, 0); + playerCollider.radius = 0.35; + camera.position.copy(playerCollider.end); + camera.rotation.set(0, 0, 0); + } +} + +function animate() { + const deltaTime = Math.min(0.05, clock.getDelta()) / STEPS_PER_FRAME; + + // we look for collisions in substeps to mitigate the risk of + // an object traversing another too quickly for detection. + + for (let i = 0; i < STEPS_PER_FRAME; i++) { + controls(deltaTime); + + updatePlayer(deltaTime); + + updateSpheres(deltaTime); + + teleportPlayerIfOob(); + } + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/misc_animation_groups.ts b/examples-testing/examples/misc_animation_groups.ts new file mode 100644 index 000000000..33fc41997 --- /dev/null +++ b/examples-testing/examples/misc_animation_groups.ts @@ -0,0 +1,125 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let stats, clock; +let scene, camera, renderer, mixer; + +init(); + +function init() { + scene = new THREE.Scene(); + + // + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(50, 50, 100); + camera.lookAt(scene.position); + + // all objects of this animation group share a common animation state + + const animationGroup = new THREE.AnimationObjectGroup(); + + // + + const geometry = new THREE.BoxGeometry(5, 5, 5); + const material = new THREE.MeshBasicMaterial({ transparent: true }); + + // + + for (let i = 0; i < 5; i++) { + for (let j = 0; j < 5; j++) { + const mesh = new THREE.Mesh(geometry, material); + + mesh.position.x = 32 - 16 * i; + mesh.position.y = 0; + mesh.position.z = 32 - 16 * j; + + scene.add(mesh); + animationGroup.add(mesh); + } + } + + // create some keyframe tracks + + const xAxis = new THREE.Vector3(1, 0, 0); + const qInitial = new THREE.Quaternion().setFromAxisAngle(xAxis, 0); + const qFinal = new THREE.Quaternion().setFromAxisAngle(xAxis, Math.PI); + const quaternionKF = new THREE.QuaternionKeyframeTrack( + '.quaternion', + [0, 1, 2], + [ + qInitial.x, + qInitial.y, + qInitial.z, + qInitial.w, + qFinal.x, + qFinal.y, + qFinal.z, + qFinal.w, + qInitial.x, + qInitial.y, + qInitial.z, + qInitial.w, + ], + ); + + const colorKF = new THREE.ColorKeyframeTrack( + '.material.color', + [0, 1, 2], + [1, 0, 0, 0, 1, 0, 0, 0, 1], + THREE.InterpolateDiscrete, + ); + const opacityKF = new THREE.NumberKeyframeTrack('.material.opacity', [0, 1, 2], [1, 0, 1]); + + // create clip + + const clip = new THREE.AnimationClip('default', 3, [quaternionKF, colorKF, opacityKF]); + + // apply the animation group to the mixer as the root object + + mixer = new THREE.AnimationMixer(animationGroup); + + const clipAction = mixer.clipAction(clip); + clipAction.play(); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + clock = new THREE.Clock(); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const delta = clock.getDelta(); + + if (mixer) { + mixer.update(delta); + } + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/misc_animation_keys.ts b/examples-testing/examples/misc_animation_keys.ts new file mode 100644 index 000000000..e2f141f91 --- /dev/null +++ b/examples-testing/examples/misc_animation_keys.ts @@ -0,0 +1,129 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let stats, clock; +let scene, camera, renderer, mixer; + +init(); + +function init() { + scene = new THREE.Scene(); + + // + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(25, 25, 50); + camera.lookAt(scene.position); + + // + + const axesHelper = new THREE.AxesHelper(10); + scene.add(axesHelper); + + // + + const geometry = new THREE.BoxGeometry(5, 5, 5); + const material = new THREE.MeshBasicMaterial({ color: 0xffffff, transparent: true }); + const mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // create a keyframe track (i.e. a timed sequence of keyframes) for each animated property + // Note: the keyframe track type should correspond to the type of the property being animated + + // POSITION + const positionKF = new THREE.VectorKeyframeTrack('.position', [0, 1, 2], [0, 0, 0, 30, 0, 0, 0, 0, 0]); + + // SCALE + const scaleKF = new THREE.VectorKeyframeTrack('.scale', [0, 1, 2], [1, 1, 1, 2, 2, 2, 1, 1, 1]); + + // ROTATION + // Rotation should be performed using quaternions, using a THREE.QuaternionKeyframeTrack + // Interpolating Euler angles (.rotation property) can be problematic and is currently not supported + + // set up rotation about x axis + const xAxis = new THREE.Vector3(1, 0, 0); + + const qInitial = new THREE.Quaternion().setFromAxisAngle(xAxis, 0); + const qFinal = new THREE.Quaternion().setFromAxisAngle(xAxis, Math.PI); + const quaternionKF = new THREE.QuaternionKeyframeTrack( + '.quaternion', + [0, 1, 2], + [ + qInitial.x, + qInitial.y, + qInitial.z, + qInitial.w, + qFinal.x, + qFinal.y, + qFinal.z, + qFinal.w, + qInitial.x, + qInitial.y, + qInitial.z, + qInitial.w, + ], + ); + + // COLOR + const colorKF = new THREE.ColorKeyframeTrack( + '.material.color', + [0, 1, 2], + [1, 0, 0, 0, 1, 0, 0, 0, 1], + THREE.InterpolateDiscrete, + ); + + // OPACITY + const opacityKF = new THREE.NumberKeyframeTrack('.material.opacity', [0, 1, 2], [1, 0, 1]); + + // create an animation sequence with the tracks + // If a negative time value is passed, the duration will be calculated from the times of the passed tracks array + const clip = new THREE.AnimationClip('Action', 3, [scaleKF, positionKF, quaternionKF, colorKF, opacityKF]); + + // setup the THREE.AnimationMixer + mixer = new THREE.AnimationMixer(mesh); + + // create a ClipAction and set it to play + const clipAction = mixer.clipAction(clip); + clipAction.play(); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + clock = new THREE.Clock(); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const delta = clock.getDelta(); + + if (mixer) { + mixer.update(delta); + } + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/misc_boxselection.ts b/examples-testing/examples/misc_boxselection.ts new file mode 100644 index 000000000..e7079c405 --- /dev/null +++ b/examples-testing/examples/misc_boxselection.ts @@ -0,0 +1,137 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { SelectionBox } from 'three/addons/interactive/SelectionBox.js'; +import { SelectionHelper } from 'three/addons/interactive/SelectionHelper.js'; + +let container, stats; +let camera, scene, renderer; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 500); + camera.position.z = 50; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + scene.add(new THREE.AmbientLight(0xaaaaaa)); + + const light = new THREE.SpotLight(0xffffff, 10000); + light.position.set(0, 25, 50); + light.angle = Math.PI / 5; + + light.castShadow = true; + light.shadow.camera.near = 10; + light.shadow.camera.far = 100; + light.shadow.mapSize.width = 1024; + light.shadow.mapSize.height = 1024; + + scene.add(light); + + const geometry = new THREE.BoxGeometry(); + + for (let i = 0; i < 200; i++) { + const object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff })); + + object.position.x = Math.random() * 80 - 40; + object.position.y = Math.random() * 45 - 25; + object.position.z = Math.random() * 45 - 25; + + object.rotation.x = Math.random() * 2 * Math.PI; + object.rotation.y = Math.random() * 2 * Math.PI; + object.rotation.z = Math.random() * 2 * Math.PI; + + object.scale.x = Math.random() * 2 + 1; + object.scale.y = Math.random() * 2 + 1; + object.scale.z = Math.random() * 2 + 1; + + object.castShadow = true; + object.receiveShadow = true; + + scene.add(object); + } + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + renderer.shadowMap.type = THREE.PCFShadowMap; + + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + renderer.render(scene, camera); + + stats.update(); +} + +const selectionBox = new SelectionBox(camera, scene); +const helper = new SelectionHelper(renderer, 'selectBox'); + +document.addEventListener('pointerdown', function (event) { + for (const item of selectionBox.collection) { + item.material.emissive.set(0x000000); + } + + selectionBox.startPoint.set( + (event.clientX / window.innerWidth) * 2 - 1, + -(event.clientY / window.innerHeight) * 2 + 1, + 0.5, + ); +}); + +document.addEventListener('pointermove', function (event) { + if (helper.isDown) { + for (let i = 0; i < selectionBox.collection.length; i++) { + selectionBox.collection[i].material.emissive.set(0x000000); + } + + selectionBox.endPoint.set( + (event.clientX / window.innerWidth) * 2 - 1, + -(event.clientY / window.innerHeight) * 2 + 1, + 0.5, + ); + + const allSelected = selectionBox.select(); + + for (let i = 0; i < allSelected.length; i++) { + allSelected[i].material.emissive.set(0xffffff); + } + } +}); + +document.addEventListener('pointerup', function (event) { + selectionBox.endPoint.set( + (event.clientX / window.innerWidth) * 2 - 1, + -(event.clientY / window.innerHeight) * 2 + 1, + 0.5, + ); + + const allSelected = selectionBox.select(); + + for (let i = 0; i < allSelected.length; i++) { + allSelected[i].material.emissive.set(0xffffff); + } +}); diff --git a/examples-testing/examples/misc_controls_arcball.ts b/examples-testing/examples/misc_controls_arcball.ts new file mode 100644 index 000000000..fbef33189 --- /dev/null +++ b/examples-testing/examples/misc_controls_arcball.ts @@ -0,0 +1,210 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { ArcballControls } from 'three/addons/controls/ArcballControls.js'; + +import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +const cameras = ['Orthographic', 'Perspective']; +const cameraType = { type: 'Perspective' }; + +const perspectiveDistance = 2.5; +const orthographicDistance = 120; +let camera, controls, scene, renderer, gui; +let folderOptions, folderAnimations; + +const arcballGui = { + gizmoVisible: true, + + setArcballControls: function () { + controls = new ArcballControls(camera, renderer.domElement, scene); + controls.addEventListener('change', render); + + this.gizmoVisible = true; + + this.populateGui(); + }, + + populateGui: function () { + folderOptions.add(controls, 'enabled').name('Enable controls'); + folderOptions.add(controls, 'enableGrid').name('Enable Grid'); + folderOptions.add(controls, 'enableRotate').name('Enable rotate'); + folderOptions.add(controls, 'enablePan').name('Enable pan'); + folderOptions.add(controls, 'enableZoom').name('Enable zoom'); + folderOptions.add(controls, 'cursorZoom').name('Cursor zoom'); + folderOptions.add(controls, 'adjustNearFar').name('adjust near/far'); + folderOptions.add(controls, 'scaleFactor', 1.1, 10, 0.1).name('Scale factor'); + folderOptions.add(controls, 'minDistance', 0, 50, 0.5).name('Min distance'); + folderOptions.add(controls, 'maxDistance', 0, 50, 0.5).name('Max distance'); + folderOptions.add(controls, 'minZoom', 0, 50, 0.5).name('Min zoom'); + folderOptions.add(controls, 'maxZoom', 0, 50, 0.5).name('Max zoom'); + folderOptions + .add(arcballGui, 'gizmoVisible') + .name('Show gizmos') + .onChange(function () { + controls.setGizmosVisible(arcballGui.gizmoVisible); + }); + folderOptions.add(controls, 'copyState').name('Copy state(ctrl+c)'); + folderOptions.add(controls, 'pasteState').name('Paste state(ctrl+v)'); + folderOptions.add(controls, 'reset').name('Reset'); + folderAnimations.add(controls, 'enableAnimations').name('Enable anim.'); + folderAnimations.add(controls, 'dampingFactor', 0, 100, 1).name('Damping'); + folderAnimations.add(controls, 'wMax', 0, 100, 1).name('Angular spd'); + }, +}; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.toneMapping = THREE.ReinhardToneMapping; + renderer.toneMappingExposure = 3; + renderer.domElement.style.background = 'linear-gradient( 180deg, rgba( 0,0,0,1 ) 0%, rgba( 128,128,255,1 ) 100% )'; + container.appendChild(renderer.domElement); + + // + + scene = new THREE.Scene(); + + camera = makePerspectiveCamera(); + camera.position.set(0, 0, perspectiveDistance); + + const material = new THREE.MeshStandardMaterial(); + + new OBJLoader().setPath('models/obj/cerberus/').load('Cerberus.obj', function (group) { + const textureLoader = new THREE.TextureLoader().setPath('models/obj/cerberus/'); + + material.roughness = 1; + material.metalness = 1; + + const diffuseMap = textureLoader.load('Cerberus_A.jpg', render); + diffuseMap.colorSpace = THREE.SRGBColorSpace; + material.map = diffuseMap; + + material.metalnessMap = material.roughnessMap = textureLoader.load('Cerberus_RM.jpg', render); + material.normalMap = textureLoader.load('Cerberus_N.jpg', render); + + material.map.wrapS = THREE.RepeatWrapping; + material.roughnessMap.wrapS = THREE.RepeatWrapping; + material.metalnessMap.wrapS = THREE.RepeatWrapping; + material.normalMap.wrapS = THREE.RepeatWrapping; + + group.traverse(function (child) { + if (child.isMesh) { + child.material = material; + } + }); + + group.rotation.y = Math.PI / 2; + group.position.x += 0.25; + scene.add(group); + render(); + + new RGBELoader().setPath('textures/equirectangular/').load('venice_sunset_1k.hdr', function (hdrEquirect) { + hdrEquirect.mapping = THREE.EquirectangularReflectionMapping; + + scene.environment = hdrEquirect; + + render(); + }); + + window.addEventListener('keydown', onKeyDown); + window.addEventListener('resize', onWindowResize); + + // + + gui = new GUI(); + gui.add(cameraType, 'type', cameras) + .name('Choose Camera') + .onChange(function () { + setCamera(cameraType.type); + }); + + folderOptions = gui.addFolder('Arcball parameters'); + folderAnimations = folderOptions.addFolder('Animations'); + + arcballGui.setArcballControls(); + + render(); + }); +} + +function makeOrthographicCamera() { + const halfFovV = THREE.MathUtils.DEG2RAD * 45 * 0.5; + const halfFovH = Math.atan((window.innerWidth / window.innerHeight) * Math.tan(halfFovV)); + + const halfW = perspectiveDistance * Math.tan(halfFovH); + const halfH = perspectiveDistance * Math.tan(halfFovV); + const near = 0.01; + const far = 2000; + const newCamera = new THREE.OrthographicCamera(-halfW, halfW, halfH, -halfH, near, far); + return newCamera; +} + +function makePerspectiveCamera() { + const fov = 45; + const aspect = window.innerWidth / window.innerHeight; + const near = 0.01; + const far = 2000; + const newCamera = new THREE.PerspectiveCamera(fov, aspect, near, far); + return newCamera; +} + +function onWindowResize() { + if (camera.type == 'OrthographicCamera') { + const halfFovV = THREE.MathUtils.DEG2RAD * 45 * 0.5; + const halfFovH = Math.atan((window.innerWidth / window.innerHeight) * Math.tan(halfFovV)); + + const halfW = perspectiveDistance * Math.tan(halfFovH); + const halfH = perspectiveDistance * Math.tan(halfFovV); + camera.left = -halfW; + camera.right = halfW; + camera.top = halfH; + camera.bottom = -halfH; + } else if (camera.type == 'PerspectiveCamera') { + camera.aspect = window.innerWidth / window.innerHeight; + } + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, camera); +} + +function onKeyDown(event) { + if (event.key === 'c') { + if (event.ctrlKey || event.metaKey) { + controls.copyState(); + } + } else if (event.key === 'v') { + if (event.ctrlKey || event.metaKey) { + controls.pasteState(); + } + } +} + +function setCamera(type) { + if (type == 'Orthographic') { + camera = makeOrthographicCamera(); + camera.position.set(0, 0, orthographicDistance); + } else if (type == 'Perspective') { + camera = makePerspectiveCamera(); + camera.position.set(0, 0, perspectiveDistance); + } + + controls.setCamera(camera); + + render(); +} diff --git a/examples-testing/examples/misc_controls_drag.ts b/examples-testing/examples/misc_controls_drag.ts new file mode 100644 index 000000000..eb7e7ca0f --- /dev/null +++ b/examples-testing/examples/misc_controls_drag.ts @@ -0,0 +1,153 @@ +import * as THREE from 'three'; + +import { DragControls } from 'three/addons/controls/DragControls.js'; + +let container; +let camera, scene, renderer; +let controls, group; +let enableSelection = false; + +const objects = []; + +const mouse = new THREE.Vector2(), + raycaster = new THREE.Raycaster(); + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 500); + camera.position.z = 25; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + scene.add(new THREE.AmbientLight(0xaaaaaa)); + + const light = new THREE.SpotLight(0xffffff, 10000); + light.position.set(0, 25, 50); + light.angle = Math.PI / 9; + + light.castShadow = true; + light.shadow.camera.near = 10; + light.shadow.camera.far = 100; + light.shadow.mapSize.width = 1024; + light.shadow.mapSize.height = 1024; + + scene.add(light); + + group = new THREE.Group(); + scene.add(group); + + const geometry = new THREE.BoxGeometry(); + + for (let i = 0; i < 200; i++) { + const object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff })); + + object.position.x = Math.random() * 30 - 15; + object.position.y = Math.random() * 15 - 7.5; + object.position.z = Math.random() * 20 - 10; + + object.rotation.x = Math.random() * 2 * Math.PI; + object.rotation.y = Math.random() * 2 * Math.PI; + object.rotation.z = Math.random() * 2 * Math.PI; + + object.scale.x = Math.random() * 2 + 1; + object.scale.y = Math.random() * 2 + 1; + object.scale.z = Math.random() * 2 + 1; + + object.castShadow = true; + object.receiveShadow = true; + + scene.add(object); + + objects.push(object); + } + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.shadowMap.enabled = true; + renderer.shadowMap.type = THREE.PCFShadowMap; + + container.appendChild(renderer.domElement); + + controls = new DragControls([...objects], camera, renderer.domElement); + controls.rotateSpeed = 2; + controls.addEventListener('drag', render); + + // + + window.addEventListener('resize', onWindowResize); + + document.addEventListener('click', onClick); + window.addEventListener('keydown', onKeyDown); + window.addEventListener('keyup', onKeyUp); + + render(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function onKeyDown(event) { + enableSelection = event.keyCode === 16 ? true : false; + + if (event.keyCode === 77) { + controls.mode = controls.mode === 'translate' ? 'rotate' : 'translate'; + } +} + +function onKeyUp() { + enableSelection = false; +} + +function onClick(event) { + event.preventDefault(); + + if (enableSelection === true) { + const draggableObjects = controls.getObjects(); + draggableObjects.length = 0; + + mouse.x = (event.clientX / window.innerWidth) * 2 - 1; + mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; + + raycaster.setFromCamera(mouse, camera); + + const intersections = raycaster.intersectObjects(objects, true); + + if (intersections.length > 0) { + const object = intersections[0].object; + + if (group.children.includes(object) === true) { + object.material.emissive.set(0x000000); + scene.attach(object); + } else { + object.material.emissive.set(0xaaaaaa); + group.attach(object); + } + + controls.transformGroup = true; + draggableObjects.push(group); + } + + if (group.children.length === 0) { + controls.transformGroup = false; + draggableObjects.push(...objects); + } + } + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/misc_controls_fly.ts b/examples-testing/examples/misc_controls_fly.ts new file mode 100644 index 000000000..4e58a36ec --- /dev/null +++ b/examples-testing/examples/misc_controls_fly.ts @@ -0,0 +1,214 @@ +import * as THREE from 'three'; +import { pass } from 'three/tsl'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { FlyControls } from 'three/addons/controls/FlyControls.js'; + +const radius = 6371; +const tilt = 0.41; +const rotationSpeed = 0.02; + +const cloudsScale = 1.005; +const moonScale = 0.23; + +const MARGIN = 0; +let SCREEN_HEIGHT = window.innerHeight - MARGIN * 2; +let SCREEN_WIDTH = window.innerWidth; + +let camera, controls, scene, renderer, stats; +let geometry, meshPlanet, meshClouds, meshMoon; +let dirLight; + +let postProcessing; + +const textureLoader = new THREE.TextureLoader(); + +let d, dPlanet, dMoon; +const dMoonVec = new THREE.Vector3(); + +const clock = new THREE.Clock(); + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(25, SCREEN_WIDTH / SCREEN_HEIGHT, 50, 1e7); + camera.position.z = radius * 5; + + scene = new THREE.Scene(); + scene.fog = new THREE.FogExp2(0x000000, 0.00000025); + + dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.position.set(-1, 0, 1).normalize(); + scene.add(dirLight); + + const materialNormalMap = new THREE.MeshPhongMaterial({ + specular: 0x7c7c7c, + shininess: 15, + map: textureLoader.load('textures/planets/earth_atmos_2048.jpg'), + specularMap: textureLoader.load('textures/planets/earth_specular_2048.jpg'), + normalMap: textureLoader.load('textures/planets/earth_normal_2048.jpg'), + + // y scale is negated to compensate for normal map handedness. + normalScale: new THREE.Vector2(0.85, -0.85), + }); + materialNormalMap.map.colorSpace = THREE.SRGBColorSpace; + + // planet + + geometry = new THREE.SphereGeometry(radius, 100, 50); + + meshPlanet = new THREE.Mesh(geometry, materialNormalMap); + meshPlanet.rotation.y = 0; + meshPlanet.rotation.z = tilt; + scene.add(meshPlanet); + + // clouds + + const materialClouds = new THREE.MeshLambertMaterial({ + map: textureLoader.load('textures/planets/earth_clouds_1024.png'), + transparent: true, + }); + materialClouds.map.colorSpace = THREE.SRGBColorSpace; + + meshClouds = new THREE.Mesh(geometry, materialClouds); + meshClouds.scale.set(cloudsScale, cloudsScale, cloudsScale); + meshClouds.rotation.z = tilt; + scene.add(meshClouds); + + // moon + + const materialMoon = new THREE.MeshPhongMaterial({ + map: textureLoader.load('textures/planets/moon_1024.jpg'), + }); + materialMoon.map.colorSpace = THREE.SRGBColorSpace; + + meshMoon = new THREE.Mesh(geometry, materialMoon); + meshMoon.position.set(radius * 5, 0, 0); + meshMoon.scale.set(moonScale, moonScale, moonScale); + scene.add(meshMoon); + + // stars + + const r = radius, + starsGeometry = [new THREE.BufferGeometry(), new THREE.BufferGeometry()]; + + const vertices1 = []; + const vertices2 = []; + + const vertex = new THREE.Vector3(); + + for (let i = 0; i < 250; i++) { + vertex.x = Math.random() * 2 - 1; + vertex.y = Math.random() * 2 - 1; + vertex.z = Math.random() * 2 - 1; + vertex.multiplyScalar(r); + + vertices1.push(vertex.x, vertex.y, vertex.z); + } + + for (let i = 0; i < 1500; i++) { + vertex.x = Math.random() * 2 - 1; + vertex.y = Math.random() * 2 - 1; + vertex.z = Math.random() * 2 - 1; + vertex.multiplyScalar(r); + + vertices2.push(vertex.x, vertex.y, vertex.z); + } + + starsGeometry[0].setAttribute('position', new THREE.Float32BufferAttribute(vertices1, 3)); + starsGeometry[1].setAttribute('position', new THREE.Float32BufferAttribute(vertices2, 3)); + + const starsMaterials = [ + new THREE.PointsMaterial({ color: 0x9c9c9c }), + new THREE.PointsMaterial({ color: 0x838383 }), + new THREE.PointsMaterial({ color: 0x5a5a5a }), + ]; + + for (let i = 10; i < 30; i++) { + const stars = new THREE.Points(starsGeometry[i % 2], starsMaterials[i % 3]); + + stars.rotation.x = Math.random() * 6; + stars.rotation.y = Math.random() * 6; + stars.rotation.z = Math.random() * 6; + stars.scale.setScalar(i * 10); + + stars.matrixAutoUpdate = false; + stars.updateMatrix(); + + scene.add(stars); + } + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + controls = new FlyControls(camera, renderer.domElement); + + controls.movementSpeed = 1000; + controls.domElement = renderer.domElement; + controls.rollSpeed = Math.PI / 24; + controls.autoForward = false; + controls.dragToLook = false; + + // + + stats = new Stats(); + document.body.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); + + // postprocessing + + postProcessing = new THREE.PostProcessing(renderer); + + const scenePass = pass(scene, camera); + const scenePassColor = scenePass.getTextureNode(); + + postProcessing.outputNode = scenePassColor.film(); +} + +function onWindowResize() { + SCREEN_HEIGHT = window.innerHeight; + SCREEN_WIDTH = window.innerWidth; + + camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT; + camera.updateProjectionMatrix(); + + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + // rotate the planet and clouds + + const delta = clock.getDelta(); + + meshPlanet.rotation.y += rotationSpeed * delta; + meshClouds.rotation.y += 1.25 * rotationSpeed * delta; + + // slow down as we approach the surface + + dPlanet = camera.position.length(); + + dMoonVec.subVectors(camera.position, meshMoon.position); + dMoon = dMoonVec.length(); + + if (dMoon < dPlanet) { + d = dMoon - radius * moonScale * 1.01; + } else { + d = dPlanet - radius * 1.01; + } + + controls.movementSpeed = 0.33 * d; + controls.update(delta); + + postProcessing.render(); +} diff --git a/examples-testing/examples/misc_controls_map.ts b/examples-testing/examples/misc_controls_map.ts new file mode 100644 index 000000000..2f52190cf --- /dev/null +++ b/examples-testing/examples/misc_controls_map.ts @@ -0,0 +1,98 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { MapControls } from 'three/addons/controls/MapControls.js'; + +let camera, controls, scene, renderer; + +init(); +//render(); // remove when using animation loop + +function init() { + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xcccccc); + scene.fog = new THREE.FogExp2(0xcccccc, 0.002); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 200, -400); + + // controls + + controls = new MapControls(camera, renderer.domElement); + + //controls.addEventListener( 'change', render ); // call this only in static scenes (i.e., if there is no animation loop) + + controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled + controls.dampingFactor = 0.05; + + controls.screenSpacePanning = false; + + controls.minDistance = 100; + controls.maxDistance = 500; + + controls.maxPolarAngle = Math.PI / 2; + + // world + + const geometry = new THREE.BoxGeometry(); + geometry.translate(0, 0.5, 0); + const material = new THREE.MeshPhongMaterial({ color: 0xeeeeee, flatShading: true }); + + for (let i = 0; i < 500; i++) { + const mesh = new THREE.Mesh(geometry, material); + mesh.position.x = Math.random() * 1600 - 800; + mesh.position.y = 0; + mesh.position.z = Math.random() * 1600 - 800; + mesh.scale.x = 20; + mesh.scale.y = Math.random() * 80 + 10; + mesh.scale.z = 20; + mesh.updateMatrix(); + mesh.matrixAutoUpdate = false; + scene.add(mesh); + } + + // lights + + const dirLight1 = new THREE.DirectionalLight(0xffffff, 3); + dirLight1.position.set(1, 1, 1); + scene.add(dirLight1); + + const dirLight2 = new THREE.DirectionalLight(0x002288, 3); + dirLight2.position.set(-1, -1, -1); + scene.add(dirLight2); + + const ambientLight = new THREE.AmbientLight(0x555555); + scene.add(ambientLight); + + // + + window.addEventListener('resize', onWindowResize); + + const gui = new GUI(); + gui.add(controls, 'zoomToCursor'); + gui.add(controls, 'screenSpacePanning'); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + controls.update(); // only required if controls.enableDamping = true, or if controls.autoRotate = true + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/misc_controls_orbit.ts b/examples-testing/examples/misc_controls_orbit.ts new file mode 100644 index 000000000..186e216cb --- /dev/null +++ b/examples-testing/examples/misc_controls_orbit.ts @@ -0,0 +1,89 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, controls, scene, renderer; + +init(); +//render(); // remove when using animation loop + +function init() { + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xcccccc); + scene.fog = new THREE.FogExp2(0xcccccc, 0.002); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(400, 200, 0); + + // controls + + controls = new OrbitControls(camera, renderer.domElement); + controls.listenToKeyEvents(window); // optional + + //controls.addEventListener( 'change', render ); // call this only in static scenes (i.e., if there is no animation loop) + + controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled + controls.dampingFactor = 0.05; + + controls.screenSpacePanning = false; + + controls.minDistance = 100; + controls.maxDistance = 500; + + controls.maxPolarAngle = Math.PI / 2; + + // world + + const geometry = new THREE.ConeGeometry(10, 30, 4, 1); + const material = new THREE.MeshPhongMaterial({ color: 0xffffff, flatShading: true }); + + for (let i = 0; i < 500; i++) { + const mesh = new THREE.Mesh(geometry, material); + mesh.position.x = Math.random() * 1600 - 800; + mesh.position.y = 0; + mesh.position.z = Math.random() * 1600 - 800; + mesh.updateMatrix(); + mesh.matrixAutoUpdate = false; + scene.add(mesh); + } + + // lights + + const dirLight1 = new THREE.DirectionalLight(0xffffff, 3); + dirLight1.position.set(1, 1, 1); + scene.add(dirLight1); + + const dirLight2 = new THREE.DirectionalLight(0x002288, 3); + dirLight2.position.set(-1, -1, -1); + scene.add(dirLight2); + + const ambientLight = new THREE.AmbientLight(0x555555); + scene.add(ambientLight); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + controls.update(); // only required if controls.enableDamping = true, or if controls.autoRotate = true + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/misc_controls_pointerlock.ts b/examples-testing/examples/misc_controls_pointerlock.ts new file mode 100644 index 000000000..66b061671 --- /dev/null +++ b/examples-testing/examples/misc_controls_pointerlock.ts @@ -0,0 +1,245 @@ +import * as THREE from 'three'; + +import { PointerLockControls } from 'three/addons/controls/PointerLockControls.js'; + +let camera, scene, renderer, controls; + +const objects = []; + +let raycaster; + +let moveForward = false; +let moveBackward = false; +let moveLeft = false; +let moveRight = false; +let canJump = false; + +let prevTime = performance.now(); +const velocity = new THREE.Vector3(); +const direction = new THREE.Vector3(); +const vertex = new THREE.Vector3(); +const color = new THREE.Color(); + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.y = 10; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + scene.fog = new THREE.Fog(0xffffff, 0, 750); + + const light = new THREE.HemisphereLight(0xeeeeff, 0x777788, 2.5); + light.position.set(0.5, 1, 0.75); + scene.add(light); + + controls = new PointerLockControls(camera, document.body); + + const blocker = document.getElementById('blocker'); + const instructions = document.getElementById('instructions'); + + instructions.addEventListener('click', function () { + controls.lock(); + }); + + controls.addEventListener('lock', function () { + instructions.style.display = 'none'; + blocker.style.display = 'none'; + }); + + controls.addEventListener('unlock', function () { + blocker.style.display = 'block'; + instructions.style.display = ''; + }); + + scene.add(controls.getObject()); + + const onKeyDown = function (event) { + switch (event.code) { + case 'ArrowUp': + case 'KeyW': + moveForward = true; + break; + + case 'ArrowLeft': + case 'KeyA': + moveLeft = true; + break; + + case 'ArrowDown': + case 'KeyS': + moveBackward = true; + break; + + case 'ArrowRight': + case 'KeyD': + moveRight = true; + break; + + case 'Space': + if (canJump === true) velocity.y += 350; + canJump = false; + break; + } + }; + + const onKeyUp = function (event) { + switch (event.code) { + case 'ArrowUp': + case 'KeyW': + moveForward = false; + break; + + case 'ArrowLeft': + case 'KeyA': + moveLeft = false; + break; + + case 'ArrowDown': + case 'KeyS': + moveBackward = false; + break; + + case 'ArrowRight': + case 'KeyD': + moveRight = false; + break; + } + }; + + document.addEventListener('keydown', onKeyDown); + document.addEventListener('keyup', onKeyUp); + + raycaster = new THREE.Raycaster(new THREE.Vector3(), new THREE.Vector3(0, -1, 0), 0, 10); + + // floor + + let floorGeometry = new THREE.PlaneGeometry(2000, 2000, 100, 100); + floorGeometry.rotateX(-Math.PI / 2); + + // vertex displacement + + let position = floorGeometry.attributes.position; + + for (let i = 0, l = position.count; i < l; i++) { + vertex.fromBufferAttribute(position, i); + + vertex.x += Math.random() * 20 - 10; + vertex.y += Math.random() * 2; + vertex.z += Math.random() * 20 - 10; + + position.setXYZ(i, vertex.x, vertex.y, vertex.z); + } + + floorGeometry = floorGeometry.toNonIndexed(); // ensure each face has unique vertices + + position = floorGeometry.attributes.position; + const colorsFloor = []; + + for (let i = 0, l = position.count; i < l; i++) { + color.setHSL(Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75, THREE.SRGBColorSpace); + colorsFloor.push(color.r, color.g, color.b); + } + + floorGeometry.setAttribute('color', new THREE.Float32BufferAttribute(colorsFloor, 3)); + + const floorMaterial = new THREE.MeshBasicMaterial({ vertexColors: true }); + + const floor = new THREE.Mesh(floorGeometry, floorMaterial); + scene.add(floor); + + // objects + + const boxGeometry = new THREE.BoxGeometry(20, 20, 20).toNonIndexed(); + + position = boxGeometry.attributes.position; + const colorsBox = []; + + for (let i = 0, l = position.count; i < l; i++) { + color.setHSL(Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75, THREE.SRGBColorSpace); + colorsBox.push(color.r, color.g, color.b); + } + + boxGeometry.setAttribute('color', new THREE.Float32BufferAttribute(colorsBox, 3)); + + for (let i = 0; i < 500; i++) { + const boxMaterial = new THREE.MeshPhongMaterial({ specular: 0xffffff, flatShading: true, vertexColors: true }); + boxMaterial.color.setHSL(Math.random() * 0.2 + 0.5, 0.75, Math.random() * 0.25 + 0.75, THREE.SRGBColorSpace); + + const box = new THREE.Mesh(boxGeometry, boxMaterial); + box.position.x = Math.floor(Math.random() * 20 - 10) * 20; + box.position.y = Math.floor(Math.random() * 20) * 20 + 10; + box.position.z = Math.floor(Math.random() * 20 - 10) * 20; + + scene.add(box); + objects.push(box); + } + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const time = performance.now(); + + if (controls.isLocked === true) { + raycaster.ray.origin.copy(controls.getObject().position); + raycaster.ray.origin.y -= 10; + + const intersections = raycaster.intersectObjects(objects, false); + + const onObject = intersections.length > 0; + + const delta = (time - prevTime) / 1000; + + velocity.x -= velocity.x * 10.0 * delta; + velocity.z -= velocity.z * 10.0 * delta; + + velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass + + direction.z = Number(moveForward) - Number(moveBackward); + direction.x = Number(moveRight) - Number(moveLeft); + direction.normalize(); // this ensures consistent movements in all directions + + if (moveForward || moveBackward) velocity.z -= direction.z * 400.0 * delta; + if (moveLeft || moveRight) velocity.x -= direction.x * 400.0 * delta; + + if (onObject === true) { + velocity.y = Math.max(0, velocity.y); + canJump = true; + } + + controls.moveRight(-velocity.x * delta); + controls.moveForward(-velocity.z * delta); + + controls.getObject().position.y += velocity.y * delta; // new behavior + + if (controls.getObject().position.y < 10) { + velocity.y = 0; + controls.getObject().position.y = 10; + + canJump = true; + } + } + + prevTime = time; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/misc_controls_trackball.ts b/examples-testing/examples/misc_controls_trackball.ts new file mode 100644 index 000000000..b6479e9f6 --- /dev/null +++ b/examples-testing/examples/misc_controls_trackball.ts @@ -0,0 +1,134 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; + +let perspectiveCamera, orthographicCamera, controls, scene, renderer, stats; + +const params = { + orthographicCamera: false, +}; + +const frustumSize = 400; + +init(); + +function init() { + const aspect = window.innerWidth / window.innerHeight; + + perspectiveCamera = new THREE.PerspectiveCamera(60, aspect, 1, 1000); + perspectiveCamera.position.z = 500; + + orthographicCamera = new THREE.OrthographicCamera( + (frustumSize * aspect) / -2, + (frustumSize * aspect) / 2, + frustumSize / 2, + frustumSize / -2, + 1, + 1000, + ); + orthographicCamera.position.z = 500; + + // world + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xcccccc); + scene.fog = new THREE.FogExp2(0xcccccc, 0.002); + + const geometry = new THREE.ConeGeometry(10, 30, 4, 1); + const material = new THREE.MeshPhongMaterial({ color: 0xffffff, flatShading: true }); + + for (let i = 0; i < 500; i++) { + const mesh = new THREE.Mesh(geometry, material); + mesh.position.x = (Math.random() - 0.5) * 1000; + mesh.position.y = (Math.random() - 0.5) * 1000; + mesh.position.z = (Math.random() - 0.5) * 1000; + mesh.updateMatrix(); + mesh.matrixAutoUpdate = false; + scene.add(mesh); + } + + // lights + + const dirLight1 = new THREE.DirectionalLight(0xffffff, 3); + dirLight1.position.set(1, 1, 1); + scene.add(dirLight1); + + const dirLight2 = new THREE.DirectionalLight(0x002288, 3); + dirLight2.position.set(-1, -1, -1); + scene.add(dirLight2); + + const ambientLight = new THREE.AmbientLight(0x555555); + scene.add(ambientLight); + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + const gui = new GUI(); + gui.add(params, 'orthographicCamera') + .name('use orthographic') + .onChange(function (value) { + controls.dispose(); + + createControls(value ? orthographicCamera : perspectiveCamera); + }); + + // + + window.addEventListener('resize', onWindowResize); + + createControls(perspectiveCamera); +} + +function createControls(camera) { + controls = new TrackballControls(camera, renderer.domElement); + + controls.rotateSpeed = 1.0; + controls.zoomSpeed = 1.2; + controls.panSpeed = 0.8; + + controls.keys = ['KeyA', 'KeyS', 'KeyD']; +} + +function onWindowResize() { + const aspect = window.innerWidth / window.innerHeight; + + perspectiveCamera.aspect = aspect; + perspectiveCamera.updateProjectionMatrix(); + + orthographicCamera.left = (-frustumSize * aspect) / 2; + orthographicCamera.right = (frustumSize * aspect) / 2; + orthographicCamera.top = frustumSize / 2; + orthographicCamera.bottom = -frustumSize / 2; + orthographicCamera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + controls.handleResize(); +} + +function animate() { + controls.update(); + + render(); + + stats.update(); +} + +function render() { + const camera = params.orthographicCamera ? orthographicCamera : perspectiveCamera; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/misc_controls_transform.ts b/examples-testing/examples/misc_controls_transform.ts new file mode 100644 index 000000000..9d14bf7ee --- /dev/null +++ b/examples-testing/examples/misc_controls_transform.ts @@ -0,0 +1,181 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { TransformControls } from 'three/addons/controls/TransformControls.js'; + +let cameraPersp, cameraOrtho, currentCamera; +let scene, renderer, control, orbit; + +init(); +render(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + const aspect = window.innerWidth / window.innerHeight; + + const frustumSize = 5; + + cameraPersp = new THREE.PerspectiveCamera(50, aspect, 0.1, 100); + cameraOrtho = new THREE.OrthographicCamera( + -frustumSize * aspect, + frustumSize * aspect, + frustumSize, + -frustumSize, + 0.1, + 100, + ); + currentCamera = cameraPersp; + + currentCamera.position.set(5, 2.5, 5); + + scene = new THREE.Scene(); + scene.add(new THREE.GridHelper(5, 10, 0x888888, 0x444444)); + + const ambientLight = new THREE.AmbientLight(0xffffff); + scene.add(ambientLight); + + const light = new THREE.DirectionalLight(0xffffff, 4); + light.position.set(1, 1, 1); + scene.add(light); + + const texture = new THREE.TextureLoader().load('textures/crate.gif', render); + texture.colorSpace = THREE.SRGBColorSpace; + texture.anisotropy = renderer.capabilities.getMaxAnisotropy(); + + const geometry = new THREE.BoxGeometry(); + const material = new THREE.MeshLambertMaterial({ map: texture }); + + orbit = new OrbitControls(currentCamera, renderer.domElement); + orbit.update(); + orbit.addEventListener('change', render); + + control = new TransformControls(currentCamera, renderer.domElement); + control.addEventListener('change', render); + + control.addEventListener('dragging-changed', function (event) { + orbit.enabled = !event.value; + }); + + const mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + control.attach(mesh); + scene.add(control); + + window.addEventListener('resize', onWindowResize); + + window.addEventListener('keydown', function (event) { + switch (event.key) { + case 'q': + control.setSpace(control.space === 'local' ? 'world' : 'local'); + break; + + case 'Shift': + control.setTranslationSnap(1); + control.setRotationSnap(THREE.MathUtils.degToRad(15)); + control.setScaleSnap(0.25); + break; + + case 'w': + control.setMode('translate'); + break; + + case 'e': + control.setMode('rotate'); + break; + + case 'r': + control.setMode('scale'); + break; + + case 'c': + const position = currentCamera.position.clone(); + + currentCamera = currentCamera.isPerspectiveCamera ? cameraOrtho : cameraPersp; + currentCamera.position.copy(position); + + orbit.object = currentCamera; + control.camera = currentCamera; + + currentCamera.lookAt(orbit.target.x, orbit.target.y, orbit.target.z); + onWindowResize(); + break; + + case 'v': + const randomFoV = Math.random() + 0.1; + const randomZoom = Math.random() + 0.1; + + cameraPersp.fov = randomFoV * 160; + cameraOrtho.bottom = -randomFoV * 500; + cameraOrtho.top = randomFoV * 500; + + cameraPersp.zoom = randomZoom * 5; + cameraOrtho.zoom = randomZoom * 5; + onWindowResize(); + break; + + case '+': + case '=': + control.setSize(control.size + 0.1); + break; + + case '-': + case '_': + control.setSize(Math.max(control.size - 0.1, 0.1)); + break; + + case 'x': + control.showX = !control.showX; + break; + + case 'y': + control.showY = !control.showY; + break; + + case 'z': + control.showZ = !control.showZ; + break; + + case ' ': + control.enabled = !control.enabled; + break; + + case 'Escape': + control.reset(); + break; + } + }); + + window.addEventListener('keyup', function (event) { + switch (event.key) { + case 'Shift': + control.setTranslationSnap(null); + control.setRotationSnap(null); + control.setScaleSnap(null); + break; + } + }); +} + +function onWindowResize() { + const aspect = window.innerWidth / window.innerHeight; + + cameraPersp.aspect = aspect; + cameraPersp.updateProjectionMatrix(); + + cameraOrtho.left = cameraOrtho.bottom * aspect; + cameraOrtho.right = cameraOrtho.top * aspect; + cameraOrtho.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, currentCamera); +} diff --git a/examples-testing/examples/misc_exporter_draco.ts b/examples-testing/examples/misc_exporter_draco.ts new file mode 100644 index 000000000..40a62fb18 --- /dev/null +++ b/examples-testing/examples/misc_exporter_draco.ts @@ -0,0 +1,117 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { DRACOExporter } from 'three/addons/exporters/DRACOExporter.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let scene, camera, renderer, exporter, mesh; + +const params = { + export: exportFile, +}; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(4, 2, 4); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xa0a0a0); + scene.fog = new THREE.Fog(0xa0a0a0, 4, 20); + + exporter = new DRACOExporter(); + + // + + const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 3); + hemiLight.position.set(0, 20, 0); + scene.add(hemiLight); + + const directionalLight = new THREE.DirectionalLight(0xffffff, 3); + directionalLight.position.set(0, 20, 10); + directionalLight.castShadow = true; + directionalLight.shadow.camera.top = 2; + directionalLight.shadow.camera.bottom = -2; + directionalLight.shadow.camera.left = -2; + directionalLight.shadow.camera.right = 2; + scene.add(directionalLight); + + // ground + + const ground = new THREE.Mesh( + new THREE.PlaneGeometry(40, 40), + new THREE.MeshPhongMaterial({ color: 0xbbbbbb, depthWrite: false }), + ); + ground.rotation.x = -Math.PI / 2; + ground.receiveShadow = true; + scene.add(ground); + + const grid = new THREE.GridHelper(40, 20, 0x000000, 0x000000); + grid.material.opacity = 0.2; + grid.material.transparent = true; + scene.add(grid); + + // export mesh + + const geometry = new THREE.TorusKnotGeometry(0.75, 0.2, 200, 30); + const material = new THREE.MeshPhongMaterial({ color: 0x00ff00 }); + mesh = new THREE.Mesh(geometry, material); + mesh.castShadow = true; + mesh.position.y = 1.5; + scene.add(mesh); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + document.body.appendChild(renderer.domElement); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 1.5, 0); + controls.update(); + + // + + window.addEventListener('resize', onWindowResize); + + const gui = new GUI(); + + gui.add(params, 'export').name('Export DRC'); + gui.open(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} + +function exportFile() { + const result = exporter.parse(mesh); + saveArrayBuffer(result, 'file.drc'); +} + +const link = document.createElement('a'); +link.style.display = 'none'; +document.body.appendChild(link); + +function save(blob, filename) { + link.href = URL.createObjectURL(blob); + link.download = filename; + link.click(); +} + +function saveArrayBuffer(buffer, filename) { + save(new Blob([buffer], { type: 'application/octet-stream' }), filename); +} diff --git a/examples-testing/examples/misc_exporter_exr.ts b/examples-testing/examples/misc_exporter_exr.ts new file mode 100644 index 000000000..c239a65fa --- /dev/null +++ b/examples-testing/examples/misc_exporter_exr.ts @@ -0,0 +1,158 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { EXRExporter, ZIP_COMPRESSION, ZIPS_COMPRESSION, NO_COMPRESSION } from 'three/addons/exporters/EXRExporter.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let scene, camera, renderer, exporter, mesh, controls, renderTarget, dataTexture; + +const params = { + target: 'pmrem', + type: 'HalfFloatType', + compression: 'ZIP', + export: exportFile, +}; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(10, 0, 0); + + scene = new THREE.Scene(); + + exporter = new EXRExporter(); + const rgbeloader = new RGBELoader(); + + // + + const pmremGenerator = new THREE.PMREMGenerator(renderer); + pmremGenerator.compileEquirectangularShader(); + + rgbeloader.load('textures/equirectangular/san_giuseppe_bridge_2k.hdr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + + renderTarget = pmremGenerator.fromEquirectangular(texture); + scene.background = renderTarget.texture; + }); + + createDataTexture(); + + // + + controls = new OrbitControls(camera, renderer.domElement); + controls.enableDamping = true; + controls.rotateSpeed = -0.25; // negative, to track mouse pointer + + // + + window.addEventListener('resize', onWindowResize); + + const gui = new GUI(); + + const input = gui.addFolder('Input'); + input.add(params, 'target').options(['pmrem', 'data-texture']).onChange(swapScene); + + const options = gui.addFolder('Output Options'); + options.add(params, 'type').options(['FloatType', 'HalfFloatType']); + options.add(params, 'compression').options(['ZIP', 'ZIPS', 'NONE']); + + gui.add(params, 'export').name('Export EXR'); + gui.open(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + controls.update(); + renderer.render(scene, camera); +} + +function createDataTexture() { + const normal = new THREE.Vector3(); + const coord = new THREE.Vector2(); + const size = 800, + radius = 320, + factor = (Math.PI * 0.5) / radius; + const data = new Float32Array(4 * size * size); + + for (let i = 0; i < size; i++) { + for (let j = 0; j < size; j++) { + const idx = i * size * 4 + j * 4; + coord.set(j, i).subScalar(size / 2); + + if (coord.length() < radius) + normal.set(Math.sin(coord.x * factor), Math.sin(coord.y * factor), Math.cos(coord.x * factor)); + else normal.set(0, 0, 1); + + data[idx + 0] = 0.5 + 0.5 * normal.x; + data[idx + 1] = 0.5 + 0.5 * normal.y; + data[idx + 2] = 0.5 + 0.5 * normal.z; + data[idx + 3] = 1; + } + } + + dataTexture = new THREE.DataTexture(data, size, size, THREE.RGBAFormat, THREE.FloatType); + dataTexture.needsUpdate = true; + + const material = new THREE.MeshBasicMaterial({ map: dataTexture }); + const quad = new THREE.PlaneGeometry(50, 50); + mesh = new THREE.Mesh(quad, material); + mesh.visible = false; + + scene.add(mesh); +} + +function swapScene() { + if (params.target == 'pmrem') { + camera.position.set(10, 0, 0); + controls.enabled = true; + scene.background = renderTarget.texture; + mesh.visible = false; + } else { + camera.position.set(0, 0, 70); + controls.enabled = false; + scene.background = new THREE.Color(0, 0, 0); + mesh.visible = true; + } +} + +function exportFile() { + let result, exportType, exportCompression; + + if (params.type == 'HalfFloatType') exportType = THREE.HalfFloatType; + else exportType = THREE.FloatType; + + if (params.compression == 'ZIP') exportCompression = ZIP_COMPRESSION; + else if (params.compression == 'ZIPS') exportCompression = ZIPS_COMPRESSION; + else exportCompression = NO_COMPRESSION; + + if (params.target == 'pmrem') + result = exporter.parse(renderer, renderTarget, { type: exportType, compression: exportCompression }); + else result = exporter.parse(dataTexture, { type: exportType, compression: exportCompression }); + + saveArrayBuffer(result, params.target + '.exr'); +} + +function saveArrayBuffer(buffer, filename) { + const blob = new Blob([buffer], { type: 'image/x-exr' }); + const link = document.createElement('a'); + + link.href = URL.createObjectURL(blob); + link.download = filename; + link.click(); +} diff --git a/examples-testing/examples/misc_exporter_gltf.ts b/examples-testing/examples/misc_exporter_gltf.ts new file mode 100644 index 000000000..e4172b852 --- /dev/null +++ b/examples-testing/examples/misc_exporter_gltf.ts @@ -0,0 +1,507 @@ +import * as THREE from 'three'; + +import { GLTFExporter } from 'three/addons/exporters/GLTFExporter.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js'; +import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +function exportGLTF(input) { + const gltfExporter = new GLTFExporter(); + + const options = { + trs: params.trs, + onlyVisible: params.onlyVisible, + binary: params.binary, + maxTextureSize: params.maxTextureSize, + }; + gltfExporter.parse( + input, + function (result) { + if (result instanceof ArrayBuffer) { + saveArrayBuffer(result, 'scene.glb'); + } else { + const output = JSON.stringify(result, null, 2); + console.log(output); + saveString(output, 'scene.gltf'); + } + }, + function (error) { + console.log('An error happened during parsing', error); + }, + options, + ); +} + +const link = document.createElement('a'); +link.style.display = 'none'; +document.body.appendChild(link); // Firefox workaround, see #6594 + +function save(blob, filename) { + link.href = URL.createObjectURL(blob); + link.download = filename; + link.click(); + + // URL.revokeObjectURL( url ); breaks Firefox... +} + +function saveString(text, filename) { + save(new Blob([text], { type: 'text/plain' }), filename); +} + +function saveArrayBuffer(buffer, filename) { + save(new Blob([buffer], { type: 'application/octet-stream' }), filename); +} + +let container; + +let camera, object, object2, material, geometry, scene1, scene2, renderer; +let gridHelper, sphere, model, coffeemat; + +const params = { + trs: false, + onlyVisible: true, + binary: false, + maxTextureSize: 4096, + exportScene1: exportScene1, + exportScenes: exportScenes, + exportSphere: exportSphere, + exportModel: exportModel, + exportObjects: exportObjects, + exportSceneObject: exportSceneObject, + exportCompressedObject: exportCompressedObject, +}; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + // Make linear gradient texture + + const data = new Uint8ClampedArray(100 * 100 * 4); + + for (let y = 0; y < 100; y++) { + for (let x = 0; x < 100; x++) { + const stride = 4 * (100 * y + x); + + data[stride] = Math.round((255 * y) / 99); + data[stride + 1] = Math.round(255 - (255 * y) / 99); + data[stride + 2] = 0; + data[stride + 3] = 255; + } + } + + const gradientTexture = new THREE.DataTexture(data, 100, 100, THREE.RGBAFormat); + gradientTexture.minFilter = THREE.LinearFilter; + gradientTexture.magFilter = THREE.LinearFilter; + gradientTexture.needsUpdate = true; + + scene1 = new THREE.Scene(); + scene1.name = 'Scene1'; + + // --------------------------------------------------------------------- + // Perspective Camera + // --------------------------------------------------------------------- + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000); + camera.position.set(600, 400, 0); + + camera.name = 'PerspectiveCamera'; + scene1.add(camera); + + // --------------------------------------------------------------------- + // Ambient light + // --------------------------------------------------------------------- + const ambientLight = new THREE.AmbientLight(0xcccccc); + ambientLight.name = 'AmbientLight'; + scene1.add(ambientLight); + + // --------------------------------------------------------------------- + // DirectLight + // --------------------------------------------------------------------- + const dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.target.position.set(0, 0, -1); + dirLight.add(dirLight.target); + dirLight.lookAt(-1, -1, 0); + dirLight.name = 'DirectionalLight'; + scene1.add(dirLight); + + // --------------------------------------------------------------------- + // Grid + // --------------------------------------------------------------------- + gridHelper = new THREE.GridHelper(2000, 20, 0xc1c1c1, 0x8d8d8d); + gridHelper.position.y = -50; + gridHelper.name = 'Grid'; + scene1.add(gridHelper); + + // --------------------------------------------------------------------- + // Axes + // --------------------------------------------------------------------- + const axes = new THREE.AxesHelper(500); + axes.name = 'AxesHelper'; + scene1.add(axes); + + // --------------------------------------------------------------------- + // Simple geometry with basic material + // --------------------------------------------------------------------- + // Icosahedron + const mapGrid = new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg'); + mapGrid.wrapS = mapGrid.wrapT = THREE.RepeatWrapping; + mapGrid.colorSpace = THREE.SRGBColorSpace; + material = new THREE.MeshBasicMaterial({ + color: 0xffffff, + map: mapGrid, + }); + + object = new THREE.Mesh(new THREE.IcosahedronGeometry(75, 0), material); + object.position.set(-200, 0, 200); + object.name = 'Icosahedron'; + scene1.add(object); + + // Octahedron + material = new THREE.MeshBasicMaterial({ + color: 0x0000ff, + wireframe: true, + }); + object = new THREE.Mesh(new THREE.OctahedronGeometry(75, 1), material); + object.position.set(0, 0, 200); + object.name = 'Octahedron'; + scene1.add(object); + + // Tetrahedron + material = new THREE.MeshBasicMaterial({ + color: 0xff0000, + transparent: true, + opacity: 0.5, + }); + + object = new THREE.Mesh(new THREE.TetrahedronGeometry(75, 0), material); + object.position.set(200, 0, 200); + object.name = 'Tetrahedron'; + scene1.add(object); + + // --------------------------------------------------------------------- + // Buffered geometry primitives + // --------------------------------------------------------------------- + // Sphere + material = new THREE.MeshStandardMaterial({ + color: 0xffff00, + metalness: 0.5, + roughness: 1.0, + flatShading: true, + }); + material.map = gradientTexture; + material.bumpMap = mapGrid; + sphere = new THREE.Mesh(new THREE.SphereGeometry(70, 10, 10), material); + sphere.position.set(0, 0, 0); + sphere.name = 'Sphere'; + scene1.add(sphere); + + // Cylinder + material = new THREE.MeshStandardMaterial({ + color: 0xff00ff, + flatShading: true, + }); + object = new THREE.Mesh(new THREE.CylinderGeometry(10, 80, 100), material); + object.position.set(200, 0, 0); + object.name = 'Cylinder'; + scene1.add(object); + + // TorusKnot + material = new THREE.MeshStandardMaterial({ + color: 0xff0000, + roughness: 1, + }); + object = new THREE.Mesh(new THREE.TorusKnotGeometry(50, 15, 40, 10), material); + object.position.set(-200, 0, 0); + object.name = 'Cylinder'; + scene1.add(object); + + // --------------------------------------------------------------------- + // Hierarchy + // --------------------------------------------------------------------- + const mapWood = new THREE.TextureLoader().load('textures/hardwood2_diffuse.jpg'); + material = new THREE.MeshStandardMaterial({ map: mapWood, side: THREE.DoubleSide }); + + object = new THREE.Mesh(new THREE.BoxGeometry(40, 100, 100), material); + object.position.set(-200, 0, 400); + object.name = 'Cube'; + scene1.add(object); + + object2 = new THREE.Mesh(new THREE.BoxGeometry(40, 40, 40, 2, 2, 2), material); + object2.position.set(0, 0, 50); + object2.rotation.set(0, 45, 0); + object2.name = 'SubCube'; + object.add(object2); + + // --------------------------------------------------------------------- + // Groups + // --------------------------------------------------------------------- + const group1 = new THREE.Group(); + group1.name = 'Group'; + scene1.add(group1); + + const group2 = new THREE.Group(); + group2.name = 'subGroup'; + group2.position.set(0, 50, 0); + group1.add(group2); + + object2 = new THREE.Mesh(new THREE.BoxGeometry(30, 30, 30), material); + object2.name = 'Cube in group'; + object2.position.set(0, 0, 400); + group2.add(object2); + + // --------------------------------------------------------------------- + // THREE.Line Strip + // --------------------------------------------------------------------- + geometry = new THREE.BufferGeometry(); + let numPoints = 100; + let positions = new Float32Array(numPoints * 3); + + for (let i = 0; i < numPoints; i++) { + positions[i * 3] = i; + positions[i * 3 + 1] = Math.sin(i / 2) * 20; + positions[i * 3 + 2] = 0; + } + + geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); + object = new THREE.Line(geometry, new THREE.LineBasicMaterial({ color: 0xffff00 })); + object.position.set(-50, 0, -200); + scene1.add(object); + + // --------------------------------------------------------------------- + // THREE.Line Loop + // --------------------------------------------------------------------- + geometry = new THREE.BufferGeometry(); + numPoints = 5; + const radius = 70; + positions = new Float32Array(numPoints * 3); + + for (let i = 0; i < numPoints; i++) { + const s = (i * Math.PI * 2) / numPoints; + positions[i * 3] = radius * Math.sin(s); + positions[i * 3 + 1] = radius * Math.cos(s); + positions[i * 3 + 2] = 0; + } + + geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); + object = new THREE.LineLoop(geometry, new THREE.LineBasicMaterial({ color: 0xffff00 })); + object.position.set(0, 0, -200); + + scene1.add(object); + + // --------------------------------------------------------------------- + // THREE.Points + // --------------------------------------------------------------------- + numPoints = 100; + const pointsArray = new Float32Array(numPoints * 3); + for (let i = 0; i < numPoints; i++) { + pointsArray[3 * i] = -50 + Math.random() * 100; + pointsArray[3 * i + 1] = Math.random() * 100; + pointsArray[3 * i + 2] = -50 + Math.random() * 100; + } + + const pointsGeo = new THREE.BufferGeometry(); + pointsGeo.setAttribute('position', new THREE.BufferAttribute(pointsArray, 3)); + + const pointsMaterial = new THREE.PointsMaterial({ color: 0xffff00, size: 5 }); + const pointCloud = new THREE.Points(pointsGeo, pointsMaterial); + pointCloud.name = 'Points'; + pointCloud.position.set(-200, 0, -200); + scene1.add(pointCloud); + + // --------------------------------------------------------------------- + // Ortho camera + // --------------------------------------------------------------------- + const cameraOrtho = new THREE.OrthographicCamera( + window.innerWidth / -2, + window.innerWidth / 2, + window.innerHeight / 2, + window.innerHeight / -2, + 0.1, + 10, + ); + scene1.add(cameraOrtho); + cameraOrtho.name = 'OrthographicCamera'; + + material = new THREE.MeshLambertMaterial({ + color: 0xffff00, + side: THREE.DoubleSide, + }); + + object = new THREE.Mesh(new THREE.CircleGeometry(50, 20, 0, Math.PI * 2), material); + object.position.set(200, 0, -400); + scene1.add(object); + + object = new THREE.Mesh(new THREE.RingGeometry(10, 50, 20, 5, 0, Math.PI * 2), material); + object.position.set(0, 0, -400); + scene1.add(object); + + object = new THREE.Mesh(new THREE.CylinderGeometry(25, 75, 100, 40, 5), material); + object.position.set(-200, 0, -400); + scene1.add(object); + + // + const points = []; + + for (let i = 0; i < 50; i++) { + points.push(new THREE.Vector2(Math.sin(i * 0.2) * Math.sin(i * 0.1) * 15 + 50, (i - 5) * 2)); + } + + object = new THREE.Mesh(new THREE.LatheGeometry(points, 20), material); + object.position.set(200, 0, 400); + scene1.add(object); + + // --------------------------------------------------------------------- + // Big red box hidden just for testing `onlyVisible` option + // --------------------------------------------------------------------- + material = new THREE.MeshBasicMaterial({ + color: 0xff0000, + }); + object = new THREE.Mesh(new THREE.BoxGeometry(200, 200, 200), material); + object.position.set(0, 0, 0); + object.name = 'CubeHidden'; + object.visible = false; + scene1.add(object); + + // --------------------------------------------------------------------- + // Model requiring KHR_mesh_quantization + // --------------------------------------------------------------------- + const loader = new GLTFLoader(); + loader.load('models/gltf/ShaderBall.glb', function (gltf) { + model = gltf.scene; + model.scale.setScalar(50); + model.position.set(200, -40, -200); + scene1.add(model); + }); + + // --------------------------------------------------------------------- + // Model requiring KHR_mesh_quantization + // --------------------------------------------------------------------- + + material = new THREE.MeshBasicMaterial({ + color: 0xffffff, + }); + object = new THREE.InstancedMesh(new THREE.BoxGeometry(10, 10, 10, 2, 2, 2), material, 50); + const matrix = new THREE.Matrix4(); + const color = new THREE.Color(); + for (let i = 0; i < 50; i++) { + matrix.setPosition(Math.random() * 100 - 50, Math.random() * 100 - 50, Math.random() * 100 - 50); + object.setMatrixAt(i, matrix); + object.setColorAt(i, color.setHSL(i / 50, 1, 0.5)); + } + + object.position.set(400, 0, 200); + scene1.add(object); + + // --------------------------------------------------------------------- + // 2nd THREE.Scene + // --------------------------------------------------------------------- + scene2 = new THREE.Scene(); + object = new THREE.Mesh(new THREE.BoxGeometry(100, 100, 100), material); + object.position.set(0, 0, 0); + object.name = 'Cube2ndScene'; + scene2.name = 'Scene2'; + scene2.add(object); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1; + + container.appendChild(renderer.domElement); + + // + + window.addEventListener('resize', onWindowResize); + + // --------------------------------------------------------------------- + // Exporting compressed textures and meshes (KTX2 / Draco / Meshopt) + // --------------------------------------------------------------------- + const ktx2Loader = new KTX2Loader().setTranscoderPath('jsm/libs/basis/').detectSupport(renderer); + + const gltfLoader = new GLTFLoader().setPath('models/gltf/'); + gltfLoader.setKTX2Loader(ktx2Loader); + gltfLoader.setMeshoptDecoder(MeshoptDecoder); + gltfLoader.load('coffeemat.glb', function (gltf) { + gltf.scene.position.x = 400; + gltf.scene.position.z = -200; + + scene1.add(gltf.scene); + + coffeemat = gltf.scene; + }); + + // + + const gui = new GUI(); + + let h = gui.addFolder('Settings'); + h.add(params, 'trs').name('Use TRS'); + h.add(params, 'onlyVisible').name('Only Visible Objects'); + h.add(params, 'binary').name('Binary (GLB)'); + h.add(params, 'maxTextureSize', 2, 8192).name('Max Texture Size').step(1); + + h = gui.addFolder('Export'); + h.add(params, 'exportScene1').name('Export Scene 1'); + h.add(params, 'exportScenes').name('Export Scene 1 and 2'); + h.add(params, 'exportSphere').name('Export Sphere'); + h.add(params, 'exportModel').name('Export Model'); + h.add(params, 'exportObjects').name('Export Sphere With Grid'); + h.add(params, 'exportSceneObject').name('Export Scene 1 and Object'); + h.add(params, 'exportCompressedObject').name('Export Coffeemat (from compressed data)'); + + gui.open(); +} + +function exportScene1() { + exportGLTF(scene1); +} + +function exportScenes() { + exportGLTF([scene1, scene2]); +} + +function exportSphere() { + exportGLTF(sphere); +} + +function exportModel() { + exportGLTF(model); +} + +function exportObjects() { + exportGLTF([sphere, gridHelper]); +} + +function exportSceneObject() { + exportGLTF([scene1, gridHelper]); +} + +function exportCompressedObject() { + exportGLTF([coffeemat]); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const timer = Date.now() * 0.0001; + + camera.position.x = Math.cos(timer) * 800; + camera.position.z = Math.sin(timer) * 800; + + camera.lookAt(scene1.position); + renderer.render(scene1, camera); +} diff --git a/examples-testing/examples/misc_exporter_obj.ts b/examples-testing/examples/misc_exporter_obj.ts new file mode 100644 index 000000000..025034daf --- /dev/null +++ b/examples-testing/examples/misc_exporter_obj.ts @@ -0,0 +1,192 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { OBJExporter } from 'three/addons/exporters/OBJExporter.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer; + +const params = { + addTriangle: addTriangle, + addCube: addCube, + addCylinder: addCylinder, + addMultiple: addMultiple, + addTransformed: addTransformed, + addPoints: addPoints, + exportToObj: exportToObj, +}; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 0, 400); + + scene = new THREE.Scene(); + + const ambientLight = new THREE.AmbientLight(0xffffff); + scene.add(ambientLight); + + const directionalLight = new THREE.DirectionalLight(0xffffff, 2.5); + directionalLight.position.set(0, 1, 1); + scene.add(directionalLight); + + const gui = new GUI(); + + let h = gui.addFolder('Geometry Selection'); + h.add(params, 'addTriangle').name('Triangle'); + h.add(params, 'addCube').name('Cube'); + h.add(params, 'addCylinder').name('Cylinder'); + h.add(params, 'addMultiple').name('Multiple objects'); + h.add(params, 'addTransformed').name('Transformed objects'); + h.add(params, 'addPoints').name('Point Cloud'); + + h = gui.addFolder('Export'); + h.add(params, 'exportToObj').name('Export OBJ'); + + gui.open(); + + addGeometry(1); + + window.addEventListener('resize', onWindowResize); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.enablePan = false; +} + +function exportToObj() { + const exporter = new OBJExporter(); + const result = exporter.parse(scene); + saveString(result, 'object.obj'); +} + +function addGeometry(type) { + for (let i = 0; i < scene.children.length; i++) { + const child = scene.children[i]; + + if (child.isMesh || child.isPoints) { + child.geometry.dispose(); + scene.remove(child); + i--; + } + } + + if (type === 1) { + const material = new THREE.MeshLambertMaterial({ color: 0x00cc00 }); + const geometry = generateTriangleGeometry(); + + scene.add(new THREE.Mesh(geometry, material)); + } else if (type === 2) { + const material = new THREE.MeshLambertMaterial({ color: 0x00cc00 }); + const geometry = new THREE.BoxGeometry(100, 100, 100); + scene.add(new THREE.Mesh(geometry, material)); + } else if (type === 3) { + const material = new THREE.MeshLambertMaterial({ color: 0x00cc00 }); + const geometry = new THREE.CylinderGeometry(50, 50, 100, 30, 1); + scene.add(new THREE.Mesh(geometry, material)); + } else if (type === 4 || type === 5) { + const material = new THREE.MeshLambertMaterial({ color: 0x00cc00 }); + const geometry = generateTriangleGeometry(); + + const mesh = new THREE.Mesh(geometry, material); + mesh.position.x = -200; + scene.add(mesh); + + const geometry2 = new THREE.BoxGeometry(100, 100, 100); + const mesh2 = new THREE.Mesh(geometry2, material); + scene.add(mesh2); + + const geometry3 = new THREE.CylinderGeometry(50, 50, 100, 30, 1); + const mesh3 = new THREE.Mesh(geometry3, material); + mesh3.position.x = 200; + scene.add(mesh3); + + if (type === 5) { + mesh.rotation.y = Math.PI / 4.0; + mesh2.rotation.y = Math.PI / 4.0; + mesh3.rotation.y = Math.PI / 4.0; + } + } else if (type === 6) { + const points = [0, 0, 0, 100, 0, 0, 100, 100, 0, 0, 100, 0]; + const colors = [0.5, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0, 0.5, 0]; + + const geometry = new THREE.BufferGeometry(); + geometry.setAttribute('position', new THREE.Float32BufferAttribute(points, 3)); + geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); + + const material = new THREE.PointsMaterial({ size: 10, vertexColors: true }); + + const pointCloud = new THREE.Points(geometry, material); + pointCloud.name = 'point cloud'; + scene.add(pointCloud); + } +} + +function addTriangle() { + addGeometry(1); +} + +function addCube() { + addGeometry(2); +} + +function addCylinder() { + addGeometry(3); +} + +function addMultiple() { + addGeometry(4); +} + +function addTransformed() { + addGeometry(5); +} + +function addPoints() { + addGeometry(6); +} + +const link = document.createElement('a'); +link.style.display = 'none'; +document.body.appendChild(link); + +function save(blob, filename) { + link.href = URL.createObjectURL(blob); + link.download = filename; + link.click(); +} + +function saveString(text, filename) { + save(new Blob([text], { type: 'text/plain' }), filename); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} + +function generateTriangleGeometry() { + const geometry = new THREE.BufferGeometry(); + const vertices = []; + + vertices.push(-50, -50, 0); + vertices.push(50, -50, 0); + vertices.push(50, 50, 0); + + geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); + geometry.computeVertexNormals(); + + return geometry; +} diff --git a/examples-testing/examples/misc_exporter_ply.ts b/examples-testing/examples/misc_exporter_ply.ts new file mode 100644 index 000000000..b7e324688 --- /dev/null +++ b/examples-testing/examples/misc_exporter_ply.ts @@ -0,0 +1,156 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { PLYExporter } from 'three/addons/exporters/PLYExporter.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let scene, camera, renderer, exporter, mesh; + +const params = { + exportASCII: exportASCII, + exportBinaryBigEndian: exportBinaryBigEndian, + exportBinaryLittleEndian: exportBinaryLittleEndian, +}; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(4, 2, 4); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xa0a0a0); + scene.fog = new THREE.Fog(0xa0a0a0, 4, 20); + + exporter = new PLYExporter(); + + // + + const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 3); + hemiLight.position.set(0, 20, 0); + scene.add(hemiLight); + + const directionalLight = new THREE.DirectionalLight(0xffffff, 3); + directionalLight.position.set(0, 20, 10); + directionalLight.castShadow = true; + directionalLight.shadow.camera.top = 2; + directionalLight.shadow.camera.bottom = -2; + directionalLight.shadow.camera.left = -2; + directionalLight.shadow.camera.right = 2; + scene.add(directionalLight); + + // ground + + const ground = new THREE.Mesh( + new THREE.PlaneGeometry(40, 40), + new THREE.MeshPhongMaterial({ color: 0xcbcbcb, depthWrite: false }), + ); + ground.rotation.x = -Math.PI / 2; + ground.receiveShadow = true; + scene.add(ground); + + const grid = new THREE.GridHelper(40, 20, 0x000000, 0x000000); + grid.material.opacity = 0.2; + grid.material.transparent = true; + scene.add(grid); + + // export mesh + + const geometry = new THREE.BoxGeometry(); + const material = new THREE.MeshPhongMaterial({ vertexColors: true }); + + // color vertices based on vertex positions + const colors = geometry.getAttribute('position').array.slice(); + for (let i = 0, l = colors.length; i < l; i++) { + if (colors[i] > 0) colors[i] = 0.5; + else colors[i] = 0; + } + + geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3, false)); + + mesh = new THREE.Mesh(geometry, material); + mesh.castShadow = true; + mesh.position.y = 0.5; + scene.add(mesh); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + document.body.appendChild(renderer.domElement); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 0.5, 0); + controls.update(); + + // + + window.addEventListener('resize', onWindowResize); + + const gui = new GUI(); + + gui.add(params, 'exportASCII').name('Export PLY (ASCII)'); + gui.add(params, 'exportBinaryBigEndian').name('Export PLY (Binary BE)'); + gui.add(params, 'exportBinaryLittleEndian').name('Export PLY (Binary LE)'); + gui.open(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} + +function exportASCII() { + exporter.parse(mesh, function (result) { + saveString(result, 'box.ply'); + }); +} + +function exportBinaryBigEndian() { + exporter.parse( + mesh, + function (result) { + saveArrayBuffer(result, 'box.ply'); + }, + { binary: true }, + ); +} + +function exportBinaryLittleEndian() { + exporter.parse( + mesh, + function (result) { + saveArrayBuffer(result, 'box.ply'); + }, + { binary: true, littleEndian: true }, + ); +} + +const link = document.createElement('a'); +link.style.display = 'none'; +document.body.appendChild(link); + +function save(blob, filename) { + link.href = URL.createObjectURL(blob); + link.download = filename; + link.click(); +} + +function saveString(text, filename) { + save(new Blob([text], { type: 'text/plain' }), filename); +} + +function saveArrayBuffer(buffer, filename) { + save(new Blob([buffer], { type: 'application/octet-stream' }), filename); +} diff --git a/examples-testing/examples/misc_exporter_stl.ts b/examples-testing/examples/misc_exporter_stl.ts new file mode 100644 index 000000000..ff6d6e2b5 --- /dev/null +++ b/examples-testing/examples/misc_exporter_stl.ts @@ -0,0 +1,129 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { STLExporter } from 'three/addons/exporters/STLExporter.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let scene, camera, renderer, exporter, mesh; + +const params = { + exportASCII: exportASCII, + exportBinary: exportBinary, +}; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(4, 2, 4); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xa0a0a0); + scene.fog = new THREE.Fog(0xa0a0a0, 4, 20); + + exporter = new STLExporter(); + + // + + const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 3); + hemiLight.position.set(0, 20, 0); + scene.add(hemiLight); + + const directionalLight = new THREE.DirectionalLight(0xffffff, 3); + directionalLight.position.set(0, 20, 10); + directionalLight.castShadow = true; + directionalLight.shadow.camera.top = 2; + directionalLight.shadow.camera.bottom = -2; + directionalLight.shadow.camera.left = -2; + directionalLight.shadow.camera.right = 2; + scene.add(directionalLight); + + // ground + + const ground = new THREE.Mesh( + new THREE.PlaneGeometry(40, 40), + new THREE.MeshPhongMaterial({ color: 0xbbbbbb, depthWrite: false }), + ); + ground.rotation.x = -Math.PI / 2; + ground.receiveShadow = true; + scene.add(ground); + + const grid = new THREE.GridHelper(40, 20, 0x000000, 0x000000); + grid.material.opacity = 0.2; + grid.material.transparent = true; + scene.add(grid); + + // export mesh + + const geometry = new THREE.BoxGeometry(); + const material = new THREE.MeshPhongMaterial({ color: 0x00ff00 }); + + mesh = new THREE.Mesh(geometry, material); + mesh.castShadow = true; + mesh.position.y = 0.5; + scene.add(mesh); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + document.body.appendChild(renderer.domElement); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 0.5, 0); + controls.update(); + + // + + window.addEventListener('resize', onWindowResize); + + const gui = new GUI(); + + gui.add(params, 'exportASCII').name('Export STL (ASCII)'); + gui.add(params, 'exportBinary').name('Export STL (Binary)'); + gui.open(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} + +function exportASCII() { + const result = exporter.parse(mesh); + saveString(result, 'box.stl'); +} + +function exportBinary() { + const result = exporter.parse(mesh, { binary: true }); + saveArrayBuffer(result, 'box.stl'); +} + +const link = document.createElement('a'); +link.style.display = 'none'; +document.body.appendChild(link); + +function save(blob, filename) { + link.href = URL.createObjectURL(blob); + link.download = filename; + link.click(); +} + +function saveString(text, filename) { + save(new Blob([text], { type: 'text/plain' }), filename); +} + +function saveArrayBuffer(buffer, filename) { + save(new Blob([buffer], { type: 'application/octet-stream' }), filename); +} diff --git a/examples-testing/examples/misc_exporter_usdz.ts b/examples-testing/examples/misc_exporter_usdz.ts new file mode 100644 index 000000000..9a14919ba --- /dev/null +++ b/examples-testing/examples/misc_exporter_usdz.ts @@ -0,0 +1,129 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { USDZExporter } from 'three/addons/exporters/USDZExporter.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer; + +const params = { + exportUSDZ: exportUSDZ, +}; + +init(); +render(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + document.body.appendChild(renderer.domElement); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); + camera.position.set(-2.5, 0.6, 3.0); + + const pmremGenerator = new THREE.PMREMGenerator(renderer); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + scene.environment = pmremGenerator.fromScene(new RoomEnvironment(), 0.04).texture; + + const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); + loader.load('DamagedHelmet.gltf', async function (gltf) { + scene.add(gltf.scene); + + const shadowMesh = createSpotShadowMesh(); + shadowMesh.position.y = -1.1; + shadowMesh.position.z = -0.25; + shadowMesh.scale.setScalar(2); + scene.add(shadowMesh); + + render(); + + // USDZ + + const exporter = new USDZExporter(); + const arraybuffer = await exporter.parseAsync(gltf.scene); + const blob = new Blob([arraybuffer], { type: 'application/octet-stream' }); + + const link = document.getElementById('link'); + link.href = URL.createObjectURL(blob); + }); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.minDistance = 2; + controls.maxDistance = 10; + controls.target.set(0, -0.15, -0.2); + controls.update(); + + window.addEventListener('resize', onWindowResize); + + const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent); + + if (isIOS === false) { + const gui = new GUI(); + + gui.add(params, 'exportUSDZ').name('Export USDZ'); + gui.open(); + } +} + +function createSpotShadowMesh() { + const canvas = document.createElement('canvas'); + canvas.width = 128; + canvas.height = 128; + + const context = canvas.getContext('2d'); + const gradient = context.createRadialGradient( + canvas.width / 2, + canvas.height / 2, + 0, + canvas.width / 2, + canvas.height / 2, + canvas.width / 2, + ); + gradient.addColorStop(0.1, 'rgba(130,130,130,1)'); + gradient.addColorStop(1, 'rgba(255,255,255,1)'); + + context.fillStyle = gradient; + context.fillRect(0, 0, canvas.width, canvas.height); + + const shadowTexture = new THREE.CanvasTexture(canvas); + + const geometry = new THREE.PlaneGeometry(); + const material = new THREE.MeshBasicMaterial({ + map: shadowTexture, + blending: THREE.MultiplyBlending, + toneMapped: false, + }); + + const mesh = new THREE.Mesh(geometry, material); + mesh.rotation.x = -Math.PI / 2; + + return mesh; +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function exportUSDZ() { + const link = document.getElementById('link'); + link.click(); +} + +// + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/misc_lookat.ts b/examples-testing/examples/misc_lookat.ts new file mode 100644 index 000000000..280b6e2d8 --- /dev/null +++ b/examples-testing/examples/misc_lookat.ts @@ -0,0 +1,95 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let camera, scene, renderer, stats; + +let sphere; + +let mouseX = 0, + mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +document.addEventListener('mousemove', onDocumentMouseMove); + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 15000); + camera.position.z = 3200; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + + sphere = new THREE.Mesh(new THREE.SphereGeometry(100, 20, 20), new THREE.MeshNormalMaterial()); + scene.add(sphere); + + const geometry = new THREE.CylinderGeometry(0, 10, 100, 12); + geometry.rotateX(Math.PI / 2); + + const material = new THREE.MeshNormalMaterial(); + + for (let i = 0; i < 1000; i++) { + const mesh = new THREE.Mesh(geometry, material); + mesh.position.x = Math.random() * 4000 - 2000; + mesh.position.y = Math.random() * 4000 - 2000; + mesh.position.z = Math.random() * 4000 - 2000; + mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 4 + 2; + scene.add(mesh); + } + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onDocumentMouseMove(event) { + mouseX = (event.clientX - windowHalfX) * 10; + mouseY = (event.clientY - windowHalfY) * 10; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + const time = Date.now() * 0.0005; + + sphere.position.x = Math.sin(time * 0.7) * 2000; + sphere.position.y = Math.cos(time * 0.5) * 2000; + sphere.position.z = Math.cos(time * 0.3) * 2000; + + for (let i = 1, l = scene.children.length; i < l; i++) { + scene.children[i].lookAt(sphere.position); + } + + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-mouseY - camera.position.y) * 0.05; + camera.lookAt(scene.position); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/misc_uv_tests.ts b/examples-testing/examples/misc_uv_tests.ts new file mode 100644 index 000000000..4f782d45f --- /dev/null +++ b/examples-testing/examples/misc_uv_tests.ts @@ -0,0 +1,44 @@ +import * as THREE from 'three'; + +import { UVsDebug } from 'three/addons/utils/UVsDebug.js'; + +/* + * This is to help debug UVs problems in geometry, + * as well as allow a new user to visualize what UVs are about. + */ + +function test(name, geometry) { + const d = document.createElement('div'); + + d.innerHTML = '

' + name + '

'; + + d.appendChild(UVsDebug(geometry)); + + document.body.appendChild(d); +} + +const points = []; + +for (let i = 0; i < 10; i++) { + points.push(new THREE.Vector2(Math.sin(i * 0.2) * 15 + 50, (i - 5) * 2)); +} + +// + +test('new THREE.PlaneGeometry( 100, 100, 4, 4 )', new THREE.PlaneGeometry(100, 100, 4, 4)); + +test('new THREE.SphereGeometry( 75, 12, 6 )', new THREE.SphereGeometry(75, 12, 6)); + +test('new THREE.IcosahedronGeometry( 30, 1 )', new THREE.IcosahedronGeometry(30, 1)); + +test('new THREE.OctahedronGeometry( 30, 2 )', new THREE.OctahedronGeometry(30, 2)); + +test('new THREE.CylinderGeometry( 25, 75, 100, 10, 5 )', new THREE.CylinderGeometry(25, 75, 100, 10, 5)); + +test('new THREE.BoxGeometry( 100, 100, 100, 4, 4, 4 )', new THREE.BoxGeometry(100, 100, 100, 4, 4, 4)); + +test('new THREE.LatheGeometry( points, 8 )', new THREE.LatheGeometry(points, 8)); + +test('new THREE.TorusGeometry( 50, 20, 8, 8 )', new THREE.TorusGeometry(50, 20, 8, 8)); + +test('new THREE.TorusKnotGeometry( 50, 10, 12, 6 )', new THREE.TorusKnotGeometry(50, 10, 12, 6)); diff --git a/examples-testing/examples/physics_ammo_instancing.ts b/examples-testing/examples/physics_ammo_instancing.ts new file mode 100644 index 000000000..265c254c8 --- /dev/null +++ b/examples-testing/examples/physics_ammo_instancing.ts @@ -0,0 +1,119 @@ +import * as THREE from 'three'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { AmmoPhysics } from 'three/addons/physics/AmmoPhysics.js'; +import Stats from 'three/addons/libs/stats.module.js'; + +let camera, scene, renderer, stats; +let physics, position; + +let boxes, spheres; + +init(); + +async function init() { + physics = await AmmoPhysics(); + position = new THREE.Vector3(); + + // + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(-1, 1.5, 2); + camera.lookAt(0, 0.5, 0); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x666666); + + const hemiLight = new THREE.HemisphereLight(); + scene.add(hemiLight); + + const dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.position.set(5, 5, 5); + dirLight.castShadow = true; + dirLight.shadow.camera.zoom = 2; + scene.add(dirLight); + + const floor = new THREE.Mesh(new THREE.BoxGeometry(10, 5, 10), new THREE.ShadowMaterial({ color: 0x444444 })); + floor.position.y = -2.5; + floor.receiveShadow = true; + floor.userData.physics = { mass: 0 }; + scene.add(floor); + + // + + const material = new THREE.MeshLambertMaterial(); + + const matrix = new THREE.Matrix4(); + const color = new THREE.Color(); + + // Boxes + + const geometryBox = new THREE.BoxGeometry(0.075, 0.075, 0.075); + boxes = new THREE.InstancedMesh(geometryBox, material, 400); + boxes.instanceMatrix.setUsage(THREE.DynamicDrawUsage); // will be updated every frame + boxes.castShadow = true; + boxes.receiveShadow = true; + boxes.userData.physics = { mass: 1 }; + scene.add(boxes); + + for (let i = 0; i < boxes.count; i++) { + matrix.setPosition(Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5); + boxes.setMatrixAt(i, matrix); + boxes.setColorAt(i, color.setHex(0xffffff * Math.random())); + } + + // Spheres + + const geometrySphere = new THREE.IcosahedronGeometry(0.05, 4); + spheres = new THREE.InstancedMesh(geometrySphere, material, 400); + spheres.instanceMatrix.setUsage(THREE.DynamicDrawUsage); // will be updated every frame + spheres.castShadow = true; + spheres.receiveShadow = true; + spheres.userData.physics = { mass: 1 }; + scene.add(spheres); + + for (let i = 0; i < spheres.count; i++) { + matrix.setPosition(Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5); + spheres.setMatrixAt(i, matrix); + spheres.setColorAt(i, color.setHex(0xffffff * Math.random())); + } + + physics.addScene(scene); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.y = 0.5; + controls.update(); + + setInterval(() => { + let index = Math.floor(Math.random() * boxes.count); + + position.set(0, Math.random() + 1, 0); + physics.setMeshPosition(boxes, position, index); + + // + + index = Math.floor(Math.random() * spheres.count); + + position.set(0, Math.random() + 1, 0); + physics.setMeshPosition(spheres, position, index); + }, 1000 / 60); +} + +function animate() { + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/physics_jolt_instancing.ts b/examples-testing/examples/physics_jolt_instancing.ts new file mode 100644 index 000000000..022263c0d --- /dev/null +++ b/examples-testing/examples/physics_jolt_instancing.ts @@ -0,0 +1,119 @@ +import * as THREE from 'three'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { JoltPhysics } from 'three/addons/physics/JoltPhysics.js'; +import Stats from 'three/addons/libs/stats.module.js'; + +let camera, scene, renderer, stats; +let physics, position; + +let boxes, spheres; + +init(); + +async function init() { + physics = await JoltPhysics(); + position = new THREE.Vector3(); + + // + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(-1, 1.5, 2); + camera.lookAt(0, 0.5, 0); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x666666); + + const hemiLight = new THREE.HemisphereLight(); + scene.add(hemiLight); + + const dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.position.set(5, 5, 5); + dirLight.castShadow = true; + dirLight.shadow.camera.zoom = 2; + scene.add(dirLight); + + const floor = new THREE.Mesh(new THREE.BoxGeometry(10, 5, 10), new THREE.ShadowMaterial({ color: 0x444444 })); + floor.position.y = -2.5; + floor.receiveShadow = true; + floor.userData.physics = { mass: 0 }; + scene.add(floor); + + // + + const material = new THREE.MeshLambertMaterial(); + + const matrix = new THREE.Matrix4(); + const color = new THREE.Color(); + + // Boxes + + const geometryBox = new THREE.BoxGeometry(0.075, 0.075, 0.075); + boxes = new THREE.InstancedMesh(geometryBox, material, 400); + boxes.instanceMatrix.setUsage(THREE.DynamicDrawUsage); // will be updated every frame + boxes.castShadow = true; + boxes.receiveShadow = true; + boxes.userData.physics = { mass: 1 }; + scene.add(boxes); + + for (let i = 0; i < boxes.count; i++) { + matrix.setPosition(Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5); + boxes.setMatrixAt(i, matrix); + boxes.setColorAt(i, color.setHex(0xffffff * Math.random())); + } + + // Spheres + + const geometrySphere = new THREE.IcosahedronGeometry(0.05, 4); + spheres = new THREE.InstancedMesh(geometrySphere, material, 400); + spheres.instanceMatrix.setUsage(THREE.DynamicDrawUsage); // will be updated every frame + spheres.castShadow = true; + spheres.receiveShadow = true; + spheres.userData.physics = { mass: 1 }; + scene.add(spheres); + + for (let i = 0; i < spheres.count; i++) { + matrix.setPosition(Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5); + spheres.setMatrixAt(i, matrix); + spheres.setColorAt(i, color.setHex(0xffffff * Math.random())); + } + + physics.addScene(scene); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.y = 0.5; + controls.update(); + + setInterval(() => { + let index = Math.floor(Math.random() * boxes.count); + + position.set(0, Math.random() + 1, 0); + physics.setMeshPosition(boxes, position, index); + + // + + index = Math.floor(Math.random() * spheres.count); + + position.set(0, Math.random() + 1, 0); + physics.setMeshPosition(spheres, position, index); + }, 1000 / 60); +} + +function animate() { + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/physics_rapier_instancing.ts b/examples-testing/examples/physics_rapier_instancing.ts new file mode 100644 index 000000000..f23cf7667 --- /dev/null +++ b/examples-testing/examples/physics_rapier_instancing.ts @@ -0,0 +1,119 @@ +import * as THREE from 'three'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { RapierPhysics } from 'three/addons/physics/RapierPhysics.js'; +import Stats from 'three/addons/libs/stats.module.js'; + +let camera, scene, renderer, stats; +let physics, position; + +let boxes, spheres; + +init(); + +async function init() { + physics = await RapierPhysics(); + position = new THREE.Vector3(); + + // + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(-1, 1.5, 2); + camera.lookAt(0, 0.5, 0); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x666666); + + const hemiLight = new THREE.HemisphereLight(); + scene.add(hemiLight); + + const dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.position.set(5, 5, 5); + dirLight.castShadow = true; + dirLight.shadow.camera.zoom = 2; + scene.add(dirLight); + + const floor = new THREE.Mesh(new THREE.BoxGeometry(10, 5, 10), new THREE.ShadowMaterial({ color: 0x444444 })); + floor.position.y = -2.5; + floor.receiveShadow = true; + floor.userData.physics = { mass: 0 }; + scene.add(floor); + + // + + const material = new THREE.MeshLambertMaterial(); + + const matrix = new THREE.Matrix4(); + const color = new THREE.Color(); + + // Boxes + + const geometryBox = new THREE.BoxGeometry(0.075, 0.075, 0.075); + boxes = new THREE.InstancedMesh(geometryBox, material, 400); + boxes.instanceMatrix.setUsage(THREE.DynamicDrawUsage); // will be updated every frame + boxes.castShadow = true; + boxes.receiveShadow = true; + boxes.userData.physics = { mass: 1 }; + scene.add(boxes); + + for (let i = 0; i < boxes.count; i++) { + matrix.setPosition(Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5); + boxes.setMatrixAt(i, matrix); + boxes.setColorAt(i, color.setHex(0xffffff * Math.random())); + } + + // Spheres + + const geometrySphere = new THREE.IcosahedronGeometry(0.05, 4); + spheres = new THREE.InstancedMesh(geometrySphere, material, 400); + spheres.instanceMatrix.setUsage(THREE.DynamicDrawUsage); // will be updated every frame + spheres.castShadow = true; + spheres.receiveShadow = true; + spheres.userData.physics = { mass: 1 }; + scene.add(spheres); + + for (let i = 0; i < spheres.count; i++) { + matrix.setPosition(Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5); + spheres.setMatrixAt(i, matrix); + spheres.setColorAt(i, color.setHex(0xffffff * Math.random())); + } + + physics.addScene(scene); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.y = 0.5; + controls.update(); + + setInterval(() => { + let index = Math.floor(Math.random() * boxes.count); + + position.set(0, Math.random() + 1, 0); + physics.setMeshPosition(boxes, position, index); + + // + + index = Math.floor(Math.random() * spheres.count); + + position.set(0, Math.random() + 1, 0); + physics.setMeshPosition(spheres, position, index); + }, 1000 / 60); +} + +function animate() { + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/svg_lines.ts b/examples-testing/examples/svg_lines.ts new file mode 100644 index 000000000..99b74c405 --- /dev/null +++ b/examples-testing/examples/svg_lines.ts @@ -0,0 +1,87 @@ +import * as THREE from 'three'; + +import { SVGRenderer } from 'three/addons/renderers/SVGRenderer.js'; + +THREE.ColorManagement.enabled = false; + +let camera, scene, renderer; + +init(); +animate(); + +function init() { + camera = new THREE.PerspectiveCamera(33, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.z = 10; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0, 0, 0); + + renderer = new SVGRenderer(); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + // + + const vertices = []; + const divisions = 50; + + for (let i = 0; i <= divisions; i++) { + const v = (i / divisions) * (Math.PI * 2); + + const x = Math.sin(v); + const z = Math.cos(v); + + vertices.push(x, 0, z); + } + + const geometry = new THREE.BufferGeometry(); + geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); + + // + + for (let i = 1; i <= 3; i++) { + const material = new THREE.LineBasicMaterial({ + color: Math.random() * 0xffffff, + linewidth: 10, + }); + const line = new THREE.Line(geometry, material); + line.scale.setScalar(i / 3); + scene.add(line); + } + + const material = new THREE.LineDashedMaterial({ + color: 'blue', + linewidth: 1, + dashSize: 10, + gapSize: 10, + }); + const line = new THREE.Line(geometry, material); + line.scale.setScalar(2); + scene.add(line); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + let count = 0; + const time = performance.now() / 1000; + + scene.traverse(function (child) { + child.rotation.x = count + time / 3; + child.rotation.z = count + time / 4; + + count++; + }); + + renderer.render(scene, camera); + requestAnimationFrame(animate); +} diff --git a/examples-testing/examples/svg_sandbox.ts b/examples-testing/examples/svg_sandbox.ts new file mode 100644 index 000000000..e6be8386c --- /dev/null +++ b/examples-testing/examples/svg_sandbox.ts @@ -0,0 +1,212 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { SVGRenderer, SVGObject } from 'three/addons/renderers/SVGRenderer.js'; + +THREE.ColorManagement.enabled = false; + +let camera, scene, renderer, stats; + +let group; + +init(); +animate(); + +function init() { + camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 500; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + // QRCODE + + const loader = new THREE.BufferGeometryLoader(); + loader.load('models/json/QRCode_buffergeometry.json', function (geometry) { + mesh = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ vertexColors: true })); + mesh.scale.x = mesh.scale.y = mesh.scale.z = 2; + scene.add(mesh); + }); + + // CUBES + + const boxGeometry = new THREE.BoxGeometry(100, 100, 100); + + let mesh = new THREE.Mesh( + boxGeometry, + new THREE.MeshBasicMaterial({ color: 0x0000ff, opacity: 0.5, transparent: true }), + ); + mesh.position.x = 500; + mesh.rotation.x = Math.random(); + mesh.rotation.y = Math.random(); + mesh.scale.x = mesh.scale.y = mesh.scale.z = 2; + scene.add(mesh); + + mesh = new THREE.Mesh(boxGeometry, new THREE.MeshBasicMaterial({ color: Math.random() * 0xffffff })); + mesh.position.x = 500; + mesh.position.y = 500; + mesh.rotation.x = Math.random(); + mesh.rotation.y = Math.random(); + mesh.scale.x = mesh.scale.y = mesh.scale.z = 2; + scene.add(mesh); + + // PLANE + + mesh = new THREE.Mesh( + new THREE.PlaneGeometry(100, 100), + new THREE.MeshBasicMaterial({ color: Math.random() * 0xffffff, side: THREE.DoubleSide }), + ); + mesh.position.y = -500; + mesh.scale.x = mesh.scale.y = mesh.scale.z = 2; + scene.add(mesh); + + // CYLINDER + + mesh = new THREE.Mesh( + new THREE.CylinderGeometry(20, 100, 200, 10), + new THREE.MeshBasicMaterial({ color: Math.random() * 0xffffff }), + ); + mesh.position.x = -500; + mesh.rotation.x = -Math.PI / 2; + mesh.scale.x = mesh.scale.y = mesh.scale.z = 2; + scene.add(mesh); + + // POLYFIELD + + const geometry = new THREE.BufferGeometry(); + const material = new THREE.MeshBasicMaterial({ vertexColors: true, side: THREE.DoubleSide }); + + const v = new THREE.Vector3(); + const v0 = new THREE.Vector3(); + const v1 = new THREE.Vector3(); + const v2 = new THREE.Vector3(); + const color = new THREE.Color(); + + const vertices = []; + const colors = []; + + for (let i = 0; i < 100; i++) { + v.set(Math.random() * 1000 - 500, Math.random() * 1000 - 500, Math.random() * 1000 - 500); + + v0.set(Math.random() * 100 - 50, Math.random() * 100 - 50, Math.random() * 100 - 50); + + v1.set(Math.random() * 100 - 50, Math.random() * 100 - 50, Math.random() * 100 - 50); + + v2.set(Math.random() * 100 - 50, Math.random() * 100 - 50, Math.random() * 100 - 50); + + v0.add(v); + v1.add(v); + v2.add(v); + + color.setHex(Math.random() * 0xffffff); + + // create a single triangle + + vertices.push(v0.x, v0.y, v0.z); + vertices.push(v1.x, v1.y, v1.z); + vertices.push(v2.x, v2.y, v2.z); + + colors.push(color.r, color.g, color.b); + colors.push(color.r, color.g, color.b); + colors.push(color.r, color.g, color.b); + } + + geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); + geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); + + group = new THREE.Mesh(geometry, material); + group.scale.set(2, 2, 2); + scene.add(group); + + // SPRITES + + for (let i = 0; i < 50; i++) { + const material = new THREE.SpriteMaterial({ color: Math.random() * 0xffffff }); + const sprite = new THREE.Sprite(material); + sprite.position.x = Math.random() * 1000 - 500; + sprite.position.y = Math.random() * 1000 - 500; + sprite.position.z = Math.random() * 1000 - 500; + sprite.scale.set(64, 64, 1); + scene.add(sprite); + } + + // CUSTOM + + const node = document.createElementNS('http://www.w3.org/2000/svg', 'circle'); + node.setAttribute('stroke', 'black'); + node.setAttribute('fill', 'red'); + node.setAttribute('r', '40'); + + for (let i = 0; i < 50; i++) { + const object = new SVGObject(node.cloneNode()); + object.position.x = Math.random() * 1000 - 500; + object.position.y = Math.random() * 1000 - 500; + object.position.z = Math.random() * 1000 - 500; + scene.add(object); + } + + // CUSTOM FROM FILE + + const fileLoader = new THREE.FileLoader(); + fileLoader.load('models/svg/hexagon.svg', function (svg) { + const node = document.createElementNS('http://www.w3.org/2000/svg', 'g'); + const parser = new DOMParser(); + const doc = parser.parseFromString(svg, 'image/svg+xml'); + + node.appendChild(doc.documentElement); + + const object = new SVGObject(node); + object.position.x = 500; + scene.add(object); + }); + + // LIGHTS + + const ambient = new THREE.AmbientLight(0x80ffff); + scene.add(ambient); + + const directional = new THREE.DirectionalLight(0xffff00); + directional.position.set(-1, 0.5, 0); + scene.add(directional); + + renderer = new SVGRenderer(); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setQuality('low'); + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + requestAnimationFrame(animate); + + render(); + stats.update(); +} + +function render() { + const time = Date.now() * 0.0002; + + camera.position.x = Math.sin(time) * 500; + camera.position.z = Math.cos(time) * 500; + camera.lookAt(scene.position); + + group.rotation.x += 0.01; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webaudio_orientation.ts b/examples-testing/examples/webaudio_orientation.ts new file mode 100644 index 000000000..7baaa88a0 --- /dev/null +++ b/examples-testing/examples/webaudio_orientation.ts @@ -0,0 +1,141 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { PositionalAudioHelper } from 'three/addons/helpers/PositionalAudioHelper.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +let scene, camera, renderer; + +const startButton = document.getElementById('startButton'); +startButton.addEventListener('click', init); + +function init() { + const overlay = document.getElementById('overlay'); + overlay.remove(); + + const container = document.getElementById('container'); + + // + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(3, 2, 3); + + const reflectionCube = new THREE.CubeTextureLoader() + .setPath('textures/cube/SwedishRoyalCastle/') + .load(['px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg']); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xa0a0a0); + scene.fog = new THREE.Fog(0xa0a0a0, 2, 20); + + // + + const hemiLight = new THREE.HemisphereLight(0xffffff, 0x8d8d8d, 3); + hemiLight.position.set(0, 20, 0); + scene.add(hemiLight); + + const dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.position.set(5, 5, 0); + dirLight.castShadow = true; + dirLight.shadow.camera.top = 1; + dirLight.shadow.camera.bottom = -1; + dirLight.shadow.camera.left = -1; + dirLight.shadow.camera.right = 1; + dirLight.shadow.camera.near = 0.1; + dirLight.shadow.camera.far = 20; + scene.add(dirLight); + + // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) ); + + // + + const mesh = new THREE.Mesh( + new THREE.PlaneGeometry(50, 50), + new THREE.MeshPhongMaterial({ color: 0xcbcbcb, depthWrite: false }), + ); + mesh.rotation.x = -Math.PI / 2; + mesh.receiveShadow = true; + scene.add(mesh); + + const grid = new THREE.GridHelper(50, 50, 0xc1c1c1, 0xc1c1c1); + scene.add(grid); + + // + + const listener = new THREE.AudioListener(); + camera.add(listener); + + const audioElement = document.getElementById('music'); + audioElement.play(); + + const positionalAudio = new THREE.PositionalAudio(listener); + positionalAudio.setMediaElementSource(audioElement); + positionalAudio.setRefDistance(1); + positionalAudio.setDirectionalCone(180, 230, 0.1); + + const helper = new PositionalAudioHelper(positionalAudio, 0.1); + positionalAudio.add(helper); + + // + + const gltfLoader = new GLTFLoader(); + gltfLoader.load('models/gltf/BoomBox.glb', function (gltf) { + const boomBox = gltf.scene; + boomBox.position.set(0, 0.2, 0); + boomBox.scale.set(20, 20, 20); + + boomBox.traverse(function (object) { + if (object.isMesh) { + object.material.envMap = reflectionCube; + object.geometry.rotateY(-Math.PI); + object.castShadow = true; + } + }); + + boomBox.add(positionalAudio); + scene.add(boomBox); + + renderer.setAnimationLoop(animate); + }); + + // sound is damped behind this wall + + const wallGeometry = new THREE.BoxGeometry(2, 1, 0.1); + const wallMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000, transparent: true, opacity: 0.5 }); + + const wall = new THREE.Mesh(wallGeometry, wallMaterial); + wall.position.set(0, 0.5, -0.5); + scene.add(wall); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.shadowMap.enabled = true; + container.appendChild(renderer.domElement); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 0.1, 0); + controls.update(); + controls.minDistance = 0.5; + controls.maxDistance = 10; + controls.maxPolarAngle = 0.5 * Math.PI; + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webaudio_sandbox.ts b/examples-testing/examples/webaudio_sandbox.ts new file mode 100644 index 000000000..d67d0d552 --- /dev/null +++ b/examples-testing/examples/webaudio_sandbox.ts @@ -0,0 +1,222 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js'; + +let camera, controls, scene, renderer, light; + +let material1, material2, material3; + +let analyser1, analyser2, analyser3; + +const clock = new THREE.Clock(); + +const startButton = document.getElementById('startButton'); +startButton.addEventListener('click', init); + +function init() { + const overlay = document.getElementById('overlay'); + overlay.remove(); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.set(0, 25, 0); + + const listener = new THREE.AudioListener(); + camera.add(listener); + + scene = new THREE.Scene(); + scene.fog = new THREE.FogExp2(0x000000, 0.0025); + + light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0, 0.5, 1).normalize(); + scene.add(light); + + const sphere = new THREE.SphereGeometry(20, 32, 16); + + material1 = new THREE.MeshPhongMaterial({ color: 0xffaa00, flatShading: true, shininess: 0 }); + material2 = new THREE.MeshPhongMaterial({ color: 0xff2200, flatShading: true, shininess: 0 }); + material3 = new THREE.MeshPhongMaterial({ color: 0x6622aa, flatShading: true, shininess: 0 }); + + // sound spheres + + const mesh1 = new THREE.Mesh(sphere, material1); + mesh1.position.set(-250, 30, 0); + scene.add(mesh1); + + const sound1 = new THREE.PositionalAudio(listener); + const songElement = document.getElementById('song'); + sound1.setMediaElementSource(songElement); + sound1.setRefDistance(20); + songElement.play(); + mesh1.add(sound1); + + // + + const mesh2 = new THREE.Mesh(sphere, material2); + mesh2.position.set(250, 30, 0); + scene.add(mesh2); + + const sound2 = new THREE.PositionalAudio(listener); + const skullbeatzElement = document.getElementById('skullbeatz'); + sound2.setMediaElementSource(skullbeatzElement); + sound2.setRefDistance(20); + skullbeatzElement.play(); + mesh2.add(sound2); + + // + + const mesh3 = new THREE.Mesh(sphere, material3); + mesh3.position.set(0, 30, -250); + scene.add(mesh3); + + const sound3 = new THREE.PositionalAudio(listener); + const oscillator = listener.context.createOscillator(); + oscillator.type = 'sine'; + oscillator.frequency.setValueAtTime(144, sound3.context.currentTime); + oscillator.start(0); + sound3.setNodeSource(oscillator); + sound3.setRefDistance(20); + sound3.setVolume(0.5); + mesh3.add(sound3); + + // analysers + + analyser1 = new THREE.AudioAnalyser(sound1, 32); + analyser2 = new THREE.AudioAnalyser(sound2, 32); + analyser3 = new THREE.AudioAnalyser(sound3, 32); + + // global ambient audio + + const sound4 = new THREE.Audio(listener); + const utopiaElement = document.getElementById('utopia'); + sound4.setMediaElementSource(utopiaElement); + sound4.setVolume(0.5); + utopiaElement.play(); + + // ground + + const helper = new THREE.GridHelper(1000, 10, 0x444444, 0x444444); + helper.position.y = 0.1; + scene.add(helper); + + // + + const SoundControls = function () { + this.master = listener.getMasterVolume(); + this.firstSphere = sound1.getVolume(); + this.secondSphere = sound2.getVolume(); + this.thirdSphere = sound3.getVolume(); + this.Ambient = sound4.getVolume(); + }; + + const GeneratorControls = function () { + this.frequency = oscillator.frequency.value; + this.wavetype = oscillator.type; + }; + + const gui = new GUI(); + const soundControls = new SoundControls(); + const generatorControls = new GeneratorControls(); + const volumeFolder = gui.addFolder('sound volume'); + const generatorFolder = gui.addFolder('sound generator'); + + volumeFolder + .add(soundControls, 'master') + .min(0.0) + .max(1.0) + .step(0.01) + .onChange(function () { + listener.setMasterVolume(soundControls.master); + }); + volumeFolder + .add(soundControls, 'firstSphere') + .min(0.0) + .max(1.0) + .step(0.01) + .onChange(function () { + sound1.setVolume(soundControls.firstSphere); + }); + volumeFolder + .add(soundControls, 'secondSphere') + .min(0.0) + .max(1.0) + .step(0.01) + .onChange(function () { + sound2.setVolume(soundControls.secondSphere); + }); + + volumeFolder + .add(soundControls, 'thirdSphere') + .min(0.0) + .max(1.0) + .step(0.01) + .onChange(function () { + sound3.setVolume(soundControls.thirdSphere); + }); + volumeFolder + .add(soundControls, 'Ambient') + .min(0.0) + .max(1.0) + .step(0.01) + .onChange(function () { + sound4.setVolume(soundControls.Ambient); + }); + volumeFolder.open(); + generatorFolder + .add(generatorControls, 'frequency') + .min(50.0) + .max(5000.0) + .step(1.0) + .onChange(function () { + oscillator.frequency.setValueAtTime(generatorControls.frequency, listener.context.currentTime); + }); + generatorFolder + .add(generatorControls, 'wavetype', ['sine', 'square', 'sawtooth', 'triangle']) + .onChange(function () { + oscillator.type = generatorControls.wavetype; + }); + + generatorFolder.open(); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + controls = new FirstPersonControls(camera, renderer.domElement); + + controls.movementSpeed = 70; + controls.lookSpeed = 0.05; + controls.lookVertical = false; + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + controls.handleResize(); +} + +function animate() { + const delta = clock.getDelta(); + + controls.update(delta); + + material1.emissive.b = analyser1.getAverageFrequency() / 256; + material2.emissive.b = analyser2.getAverageFrequency() / 256; + material3.emissive.b = analyser3.getAverageFrequency() / 256; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webaudio_timing.ts b/examples-testing/examples/webaudio_timing.ts new file mode 100644 index 000000000..9e17bcbcd --- /dev/null +++ b/examples-testing/examples/webaudio_timing.ts @@ -0,0 +1,152 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let scene, camera, renderer, clock; + +const objects = []; + +const speed = 2.5; +const height = 3; +const offset = 0.5; + +const startButton = document.getElementById('startButton'); +startButton.addEventListener('click', init); + +function init() { + const overlay = document.getElementById('overlay'); + overlay.remove(); + + const container = document.getElementById('container'); + + scene = new THREE.Scene(); + + clock = new THREE.Clock(); + + // + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(7, 3, 7); + + // lights + + const ambientLight = new THREE.AmbientLight(0xcccccc); + scene.add(ambientLight); + + const directionalLight = new THREE.DirectionalLight(0xffffff, 2.5); + directionalLight.position.set(0, 5, 5); + scene.add(directionalLight); + + const d = 5; + directionalLight.castShadow = true; + directionalLight.shadow.camera.left = -d; + directionalLight.shadow.camera.right = d; + directionalLight.shadow.camera.top = d; + directionalLight.shadow.camera.bottom = -d; + + directionalLight.shadow.camera.near = 1; + directionalLight.shadow.camera.far = 20; + + directionalLight.shadow.mapSize.x = 1024; + directionalLight.shadow.mapSize.y = 1024; + + // audio + + const audioLoader = new THREE.AudioLoader(); + + const listener = new THREE.AudioListener(); + camera.add(listener); + + // floor + + const floorGeometry = new THREE.PlaneGeometry(10, 10); + const floorMaterial = new THREE.MeshLambertMaterial({ color: 0x4676b6 }); + + const floor = new THREE.Mesh(floorGeometry, floorMaterial); + floor.rotation.x = Math.PI * -0.5; + floor.receiveShadow = true; + scene.add(floor); + + // objects + + const count = 5; + const radius = 3; + + const ballGeometry = new THREE.SphereGeometry(0.3, 32, 16); + ballGeometry.translate(0, 0.3, 0); + const ballMaterial = new THREE.MeshLambertMaterial({ color: 0xcccccc }); + + // create objects when audio buffer is loaded + + audioLoader.load('sounds/ping_pong.mp3', function (buffer) { + for (let i = 0; i < count; i++) { + const s = (i / count) * Math.PI * 2; + + const ball = new THREE.Mesh(ballGeometry, ballMaterial); + ball.castShadow = true; + ball.userData.down = false; + + ball.position.x = radius * Math.cos(s); + ball.position.z = radius * Math.sin(s); + + const audio = new THREE.PositionalAudio(listener); + audio.setBuffer(buffer); + ball.add(audio); + + scene.add(ball); + objects.push(ball); + } + + renderer.setAnimationLoop(animate); + }); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.shadowMap.enabled = true; + container.appendChild(renderer.domElement); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 1; + controls.maxDistance = 25; + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const time = clock.getElapsedTime(); + + for (let i = 0; i < objects.length; i++) { + const ball = objects[i]; + + const previousHeight = ball.position.y; + ball.position.y = Math.abs(Math.sin(i * offset + time * speed) * height); + + if (ball.position.y < previousHeight) { + ball.userData.down = true; + } else { + if (ball.userData.down === true) { + // ball changed direction from down to up + + const audio = ball.children[0]; + audio.play(); // play audio with perfect timing when ball hits the surface + ball.userData.down = false; + } + } + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webaudio_visualizer.ts b/examples-testing/examples/webaudio_visualizer.ts new file mode 100644 index 000000000..a3f58cb36 --- /dev/null +++ b/examples-testing/examples/webaudio_visualizer.ts @@ -0,0 +1,86 @@ +import * as THREE from 'three'; + +let scene, camera, renderer, analyser, uniforms; + +const startButton = document.getElementById('startButton'); +startButton.addEventListener('click', init); + +function init() { + const fftSize = 128; + + // + + const overlay = document.getElementById('overlay'); + overlay.remove(); + + // + + const container = document.getElementById('container'); + + scene = new THREE.Scene(); + + camera = new THREE.Camera(); + + // + + const listener = new THREE.AudioListener(); + + const audio = new THREE.Audio(listener); + const file = './sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3'; + + if (/(iPad|iPhone|iPod)/g.test(navigator.userAgent)) { + const loader = new THREE.AudioLoader(); + loader.load(file, function (buffer) { + audio.setBuffer(buffer); + audio.play(); + }); + } else { + const mediaElement = new Audio(file); + mediaElement.play(); + + audio.setMediaElementSource(mediaElement); + } + + analyser = new THREE.AudioAnalyser(audio, fftSize); + + // + + uniforms = { + tAudioData: { value: new THREE.DataTexture(analyser.data, fftSize / 2, 1, THREE.RedFormat) }, + }; + + const material = new THREE.ShaderMaterial({ + uniforms: uniforms, + vertexShader: document.getElementById('vertexShader').textContent, + fragmentShader: document.getElementById('fragmentShader').textContent, + }); + + const geometry = new THREE.PlaneGeometry(1, 1); + + const mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + analyser.getFrequencyData(); + + uniforms.tAudioData.value.needsUpdate = true; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_animation_keyframes.ts b/examples-testing/examples/webgl_animation_keyframes.ts new file mode 100644 index 000000000..88048f24c --- /dev/null +++ b/examples-testing/examples/webgl_animation_keyframes.ts @@ -0,0 +1,80 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; + +let mixer; + +const clock = new THREE.Clock(); +const container = document.getElementById('container'); + +const stats = new Stats(); +container.appendChild(stats.dom); + +const renderer = new THREE.WebGLRenderer({ antialias: true }); +renderer.setPixelRatio(window.devicePixelRatio); +renderer.setSize(window.innerWidth, window.innerHeight); +container.appendChild(renderer.domElement); + +const pmremGenerator = new THREE.PMREMGenerator(renderer); + +const scene = new THREE.Scene(); +scene.background = new THREE.Color(0xbfe3dd); +scene.environment = pmremGenerator.fromScene(new RoomEnvironment(), 0.04).texture; + +const camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 100); +camera.position.set(5, 2, 8); + +const controls = new OrbitControls(camera, renderer.domElement); +controls.target.set(0, 0.5, 0); +controls.update(); +controls.enablePan = false; +controls.enableDamping = true; + +const dracoLoader = new DRACOLoader(); +dracoLoader.setDecoderPath('jsm/libs/draco/gltf/'); + +const loader = new GLTFLoader(); +loader.setDRACOLoader(dracoLoader); +loader.load( + 'models/gltf/LittlestTokyo.glb', + function (gltf) { + const model = gltf.scene; + model.position.set(1, 1, 0); + model.scale.set(0.01, 0.01, 0.01); + scene.add(model); + + mixer = new THREE.AnimationMixer(model); + mixer.clipAction(gltf.animations[0]).play(); + + renderer.setAnimationLoop(animate); + }, + undefined, + function (e) { + console.error(e); + }, +); + +window.onresize = function () { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +}; + +function animate() { + const delta = clock.getDelta(); + + mixer.update(delta); + + controls.update(); + + stats.update(); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_animation_multiple.ts b/examples-testing/examples/webgl_animation_multiple.ts new file mode 100644 index 000000000..152c65067 --- /dev/null +++ b/examples-testing/examples/webgl_animation_multiple.ts @@ -0,0 +1,197 @@ +import * as THREE from 'three'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import * as SkeletonUtils from 'three/addons/utils/SkeletonUtils.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, clock; +let model, animations; + +const mixers = [], + objects = []; + +const params = { + sharedSkeleton: false, +}; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(2, 3, -6); + camera.lookAt(0, 1, 0); + + clock = new THREE.Clock(); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xa0a0a0); + scene.fog = new THREE.Fog(0xa0a0a0, 10, 50); + + const hemiLight = new THREE.HemisphereLight(0xffffff, 0x8d8d8d, 3); + hemiLight.position.set(0, 20, 0); + scene.add(hemiLight); + + const dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.position.set(-3, 10, -10); + dirLight.castShadow = true; + dirLight.shadow.camera.top = 4; + dirLight.shadow.camera.bottom = -4; + dirLight.shadow.camera.left = -4; + dirLight.shadow.camera.right = 4; + dirLight.shadow.camera.near = 0.1; + dirLight.shadow.camera.far = 40; + scene.add(dirLight); + + // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) ); + + // ground + + const mesh = new THREE.Mesh( + new THREE.PlaneGeometry(200, 200), + new THREE.MeshPhongMaterial({ color: 0xcbcbcb, depthWrite: false }), + ); + mesh.rotation.x = -Math.PI / 2; + mesh.receiveShadow = true; + scene.add(mesh); + + const loader = new GLTFLoader(); + loader.load('models/gltf/Soldier.glb', function (gltf) { + model = gltf.scene; + animations = gltf.animations; + + model.traverse(function (object) { + if (object.isMesh) object.castShadow = true; + }); + + setupDefaultScene(); + }); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + document.body.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); + + const gui = new GUI(); + + gui.add(params, 'sharedSkeleton').onChange(function () { + clearScene(); + + if (params.sharedSkeleton === true) { + setupSharedSkeletonScene(); + } else { + setupDefaultScene(); + } + }); + gui.open(); +} + +function clearScene() { + for (const mixer of mixers) { + mixer.stopAllAction(); + } + + mixers.length = 0; + + // + + for (const object of objects) { + scene.remove(object); + + scene.traverse(function (child) { + if (child.isSkinnedMesh) child.skeleton.dispose(); + }); + } +} + +function setupDefaultScene() { + // three cloned models with independent skeletons. + // each model can have its own animation state + + const model1 = SkeletonUtils.clone(model); + const model2 = SkeletonUtils.clone(model); + const model3 = SkeletonUtils.clone(model); + + model1.position.x = -2; + model2.position.x = 0; + model3.position.x = 2; + + const mixer1 = new THREE.AnimationMixer(model1); + const mixer2 = new THREE.AnimationMixer(model2); + const mixer3 = new THREE.AnimationMixer(model3); + + mixer1.clipAction(animations[0]).play(); // idle + mixer2.clipAction(animations[1]).play(); // run + mixer3.clipAction(animations[3]).play(); // walk + + scene.add(model1, model2, model3); + + objects.push(model1, model2, model3); + mixers.push(mixer1, mixer2, mixer3); +} + +function setupSharedSkeletonScene() { + // three cloned models with a single shared skeleton. + // all models share the same animation state + + const sharedModel = SkeletonUtils.clone(model); + const shareSkinnedMesh = sharedModel.getObjectByName('vanguard_Mesh'); + const sharedSkeleton = shareSkinnedMesh.skeleton; + const sharedParentBone = sharedModel.getObjectByName('mixamorigHips'); + scene.add(sharedParentBone); // the bones need to be in the scene for the animation to work + + const model1 = shareSkinnedMesh.clone(); + const model2 = shareSkinnedMesh.clone(); + const model3 = shareSkinnedMesh.clone(); + + model1.bindMode = THREE.DetachedBindMode; + model2.bindMode = THREE.DetachedBindMode; + model3.bindMode = THREE.DetachedBindMode; + + const identity = new THREE.Matrix4(); + + model1.bind(sharedSkeleton, identity); + model2.bind(sharedSkeleton, identity); + model3.bind(sharedSkeleton, identity); + + model1.position.x = -2; + model2.position.x = 0; + model3.position.x = 2; + + // apply transformation from the glTF asset + + model1.scale.setScalar(0.01); + model1.rotation.x = -Math.PI * 0.5; + model2.scale.setScalar(0.01); + model2.rotation.x = -Math.PI * 0.5; + model3.scale.setScalar(0.01); + model3.rotation.x = -Math.PI * 0.5; + + // + + const mixer = new THREE.AnimationMixer(sharedParentBone); + mixer.clipAction(animations[1]).play(); + + scene.add(sharedParentBone, model1, model2, model3); + + objects.push(sharedParentBone, model1, model2, model3); + mixers.push(mixer); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const delta = clock.getDelta(); + + for (const mixer of mixers) mixer.update(delta); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_animation_skinning_morph.ts b/examples-testing/examples/webgl_animation_skinning_morph.ts new file mode 100644 index 000000000..f05369aa9 --- /dev/null +++ b/examples-testing/examples/webgl_animation_skinning_morph.ts @@ -0,0 +1,187 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +let container, stats, clock, gui, mixer, actions, activeAction, previousAction; +let camera, scene, renderer, model, face; + +const api = { state: 'Walking' }; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 100); + camera.position.set(-5, 3, 10); + camera.lookAt(0, 2, 0); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xe0e0e0); + scene.fog = new THREE.Fog(0xe0e0e0, 20, 100); + + clock = new THREE.Clock(); + + // lights + + const hemiLight = new THREE.HemisphereLight(0xffffff, 0x8d8d8d, 3); + hemiLight.position.set(0, 20, 0); + scene.add(hemiLight); + + const dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.position.set(0, 20, 10); + scene.add(dirLight); + + // ground + + const mesh = new THREE.Mesh( + new THREE.PlaneGeometry(2000, 2000), + new THREE.MeshPhongMaterial({ color: 0xcbcbcb, depthWrite: false }), + ); + mesh.rotation.x = -Math.PI / 2; + scene.add(mesh); + + const grid = new THREE.GridHelper(200, 40, 0x000000, 0x000000); + grid.material.opacity = 0.2; + grid.material.transparent = true; + scene.add(grid); + + // model + + const loader = new GLTFLoader(); + loader.load( + 'models/gltf/RobotExpressive/RobotExpressive.glb', + function (gltf) { + model = gltf.scene; + scene.add(model); + + createGUI(model, gltf.animations); + }, + undefined, + function (e) { + console.error(e); + }, + ); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); + + // stats + stats = new Stats(); + container.appendChild(stats.dom); +} + +function createGUI(model, animations) { + const states = ['Idle', 'Walking', 'Running', 'Dance', 'Death', 'Sitting', 'Standing']; + const emotes = ['Jump', 'Yes', 'No', 'Wave', 'Punch', 'ThumbsUp']; + + gui = new GUI(); + + mixer = new THREE.AnimationMixer(model); + + actions = {}; + + for (let i = 0; i < animations.length; i++) { + const clip = animations[i]; + const action = mixer.clipAction(clip); + actions[clip.name] = action; + + if (emotes.indexOf(clip.name) >= 0 || states.indexOf(clip.name) >= 4) { + action.clampWhenFinished = true; + action.loop = THREE.LoopOnce; + } + } + + // states + + const statesFolder = gui.addFolder('States'); + + const clipCtrl = statesFolder.add(api, 'state').options(states); + + clipCtrl.onChange(function () { + fadeToAction(api.state, 0.5); + }); + + statesFolder.open(); + + // emotes + + const emoteFolder = gui.addFolder('Emotes'); + + function createEmoteCallback(name) { + api[name] = function () { + fadeToAction(name, 0.2); + + mixer.addEventListener('finished', restoreState); + }; + + emoteFolder.add(api, name); + } + + function restoreState() { + mixer.removeEventListener('finished', restoreState); + + fadeToAction(api.state, 0.2); + } + + for (let i = 0; i < emotes.length; i++) { + createEmoteCallback(emotes[i]); + } + + emoteFolder.open(); + + // expressions + + face = model.getObjectByName('Head_4'); + + const expressions = Object.keys(face.morphTargetDictionary); + const expressionFolder = gui.addFolder('Expressions'); + + for (let i = 0; i < expressions.length; i++) { + expressionFolder.add(face.morphTargetInfluences, i, 0, 1, 0.01).name(expressions[i]); + } + + activeAction = actions['Walking']; + activeAction.play(); + + expressionFolder.open(); +} + +function fadeToAction(name, duration) { + previousAction = activeAction; + activeAction = actions[name]; + + if (previousAction !== activeAction) { + previousAction.fadeOut(duration); + } + + activeAction.reset().setEffectiveTimeScale(1).setEffectiveWeight(1).fadeIn(duration).play(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const dt = clock.getDelta(); + + if (mixer) mixer.update(dt); + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_buffergeometry.ts b/examples-testing/examples/webgl_buffergeometry.ts new file mode 100644 index 000000000..28b2c96a4 --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry.ts @@ -0,0 +1,178 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; + +let camera, scene, renderer; + +let mesh; + +init(); +animate(); + +function init() { + container = document.getElementById('container'); + + // + + camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 3500); + camera.position.z = 2750; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x050505); + scene.fog = new THREE.Fog(0x050505, 2000, 3500); + + // + + scene.add(new THREE.AmbientLight(0xcccccc)); + + const light1 = new THREE.DirectionalLight(0xffffff, 1.5); + light1.position.set(1, 1, 1); + scene.add(light1); + + const light2 = new THREE.DirectionalLight(0xffffff, 4.5); + light2.position.set(0, -1, 0); + scene.add(light2); + + // + + const triangles = 160000; + + const geometry = new THREE.BufferGeometry(); + + const positions = []; + const normals = []; + const colors = []; + + const color = new THREE.Color(); + + const n = 800, + n2 = n / 2; // triangles spread in the cube + const d = 12, + d2 = d / 2; // individual triangle size + + const pA = new THREE.Vector3(); + const pB = new THREE.Vector3(); + const pC = new THREE.Vector3(); + + const cb = new THREE.Vector3(); + const ab = new THREE.Vector3(); + + for (let i = 0; i < triangles; i++) { + // positions + + const x = Math.random() * n - n2; + const y = Math.random() * n - n2; + const z = Math.random() * n - n2; + + const ax = x + Math.random() * d - d2; + const ay = y + Math.random() * d - d2; + const az = z + Math.random() * d - d2; + + const bx = x + Math.random() * d - d2; + const by = y + Math.random() * d - d2; + const bz = z + Math.random() * d - d2; + + const cx = x + Math.random() * d - d2; + const cy = y + Math.random() * d - d2; + const cz = z + Math.random() * d - d2; + + positions.push(ax, ay, az); + positions.push(bx, by, bz); + positions.push(cx, cy, cz); + + // flat face normals + + pA.set(ax, ay, az); + pB.set(bx, by, bz); + pC.set(cx, cy, cz); + + cb.subVectors(pC, pB); + ab.subVectors(pA, pB); + cb.cross(ab); + + cb.normalize(); + + const nx = cb.x; + const ny = cb.y; + const nz = cb.z; + + normals.push(nx, ny, nz); + normals.push(nx, ny, nz); + normals.push(nx, ny, nz); + + // colors + + const vx = x / n + 0.5; + const vy = y / n + 0.5; + const vz = z / n + 0.5; + + color.setRGB(vx, vy, vz); + + const alpha = Math.random(); + + colors.push(color.r, color.g, color.b, alpha); + colors.push(color.r, color.g, color.b, alpha); + colors.push(color.r, color.g, color.b, alpha); + } + + function disposeArray() { + this.array = null; + } + + geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3).onUpload(disposeArray)); + geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3).onUpload(disposeArray)); + geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 4).onUpload(disposeArray)); + + geometry.computeBoundingSphere(); + + const material = new THREE.MeshPhongMaterial({ + color: 0xd5d5d5, + specular: 0xffffff, + shininess: 250, + side: THREE.DoubleSide, + vertexColors: true, + transparent: true, + }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const time = Date.now() * 0.001; + + mesh.rotation.x = time * 0.25; + mesh.rotation.y = time * 0.5; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_buffergeometry_attributes_integer.ts b/examples-testing/examples/webgl_buffergeometry_attributes_integer.ts new file mode 100644 index 000000000..96926c2c3 --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_attributes_integer.ts @@ -0,0 +1,113 @@ +import * as THREE from 'three'; + +let camera, scene, renderer, mesh; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 3500); + camera.position.z = 2500; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x050505); + scene.fog = new THREE.Fog(0x050505, 2000, 3500); + + // geometry + + const triangles = 10000; + + const geometry = new THREE.BufferGeometry(); + + const positions = []; + const uvs = []; + const textureIndices = []; + + const n = 800, + n2 = n / 2; // triangles spread in the cube + const d = 50, + d2 = d / 2; // individual triangle size + + for (let i = 0; i < triangles; i++) { + // positions + + const x = Math.random() * n - n2; + const y = Math.random() * n - n2; + const z = Math.random() * n - n2; + + const ax = x + Math.random() * d - d2; + const ay = y + Math.random() * d - d2; + const az = z + Math.random() * d - d2; + + const bx = x + Math.random() * d - d2; + const by = y + Math.random() * d - d2; + const bz = z + Math.random() * d - d2; + + const cx = x + Math.random() * d - d2; + const cy = y + Math.random() * d - d2; + const cz = z + Math.random() * d - d2; + + positions.push(ax, ay, az); + positions.push(bx, by, bz); + positions.push(cx, cy, cz); + + // uvs + + uvs.push(0, 0); + uvs.push(0.5, 1); + uvs.push(1, 0); + + // texture indices + + const t = i % 3; + textureIndices.push(t, t, t); + } + + geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); + geometry.setAttribute('uv', new THREE.Float32BufferAttribute(uvs, 2)); + geometry.setAttribute('textureIndex', new THREE.Int16BufferAttribute(textureIndices, 1)); + geometry.attributes.textureIndex.gpuType = THREE.IntType; + + geometry.computeBoundingSphere(); + + // material + + const loader = new THREE.TextureLoader(); + + const map1 = loader.load('textures/crate.gif'); + const map2 = loader.load('textures/floors/FloorsCheckerboard_S_Diffuse.jpg'); + const map3 = loader.load('textures/terrain/grasslight-big.jpg'); + + const material = new THREE.ShaderMaterial({ + uniforms: { + uTextures: { + value: [map1, map2, map3], + }, + }, + vertexShader: document.getElementById('vertexShader').textContent, + fragmentShader: document.getElementById('fragmentShader').textContent, + side: THREE.DoubleSide, + glslVersion: THREE.GLSL3, + }); + + // mesh + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); +} + +function animate() { + const time = Date.now() * 0.001; + + mesh.rotation.x = time * 0.25; + mesh.rotation.y = time * 0.5; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_buffergeometry_attributes_none.ts b/examples-testing/examples/webgl_buffergeometry_attributes_none.ts new file mode 100644 index 000000000..a1424e871 --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_attributes_none.ts @@ -0,0 +1,56 @@ +import * as THREE from 'three'; + +let camera, scene, renderer, mesh; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 3500); + camera.position.z = 4; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x050505); + scene.fog = new THREE.Fog(0x050505, 2000, 3500); + + // geometry + + const triangleCount = 10000; + const vertexCountPerTriangle = 3; + const vertexCount = triangleCount * vertexCountPerTriangle; + + const geometry = new THREE.BufferGeometry(); + geometry.setDrawRange(0, vertexCount); + + // material + + const material = new THREE.RawShaderMaterial({ + uniforms: { + seed: { value: 42 }, + }, + vertexShader: document.getElementById('vertexShader').textContent, + fragmentShader: document.getElementById('fragmentShader').textContent, + side: THREE.DoubleSide, + glslVersion: THREE.GLSL3, + }); + + // mesh + + mesh = new THREE.Mesh(geometry, material); + mesh.frustumCulled = false; + scene.add(mesh); + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); +} + +function animate(time) { + mesh.rotation.x = (time / 1000.0) * 0.25; + mesh.rotation.y = (time / 1000.0) * 0.5; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_buffergeometry_custom_attributes_particles.ts b/examples-testing/examples/webgl_buffergeometry_custom_attributes_particles.ts new file mode 100644 index 000000000..0dffa65cc --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_custom_attributes_particles.ts @@ -0,0 +1,103 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let renderer, scene, camera, stats; + +let particleSystem, uniforms, geometry; + +const particles = 100000; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 300; + + scene = new THREE.Scene(); + + uniforms = { + pointTexture: { value: new THREE.TextureLoader().load('textures/sprites/spark1.png') }, + }; + + const shaderMaterial = new THREE.ShaderMaterial({ + uniforms: uniforms, + vertexShader: document.getElementById('vertexshader').textContent, + fragmentShader: document.getElementById('fragmentshader').textContent, + + blending: THREE.AdditiveBlending, + depthTest: false, + transparent: true, + vertexColors: true, + }); + + const radius = 200; + + geometry = new THREE.BufferGeometry(); + + const positions = []; + const colors = []; + const sizes = []; + + const color = new THREE.Color(); + + for (let i = 0; i < particles; i++) { + positions.push((Math.random() * 2 - 1) * radius); + positions.push((Math.random() * 2 - 1) * radius); + positions.push((Math.random() * 2 - 1) * radius); + + color.setHSL(i / particles, 1.0, 0.5); + + colors.push(color.r, color.g, color.b); + + sizes.push(20); + } + + geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); + geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); + geometry.setAttribute('size', new THREE.Float32BufferAttribute(sizes, 1).setUsage(THREE.DynamicDrawUsage)); + + particleSystem = new THREE.Points(geometry, shaderMaterial); + + scene.add(particleSystem); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + + const container = document.getElementById('container'); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const time = Date.now() * 0.005; + + particleSystem.rotation.z = 0.01 * time; + + const sizes = geometry.attributes.size.array; + + for (let i = 0; i < particles; i++) { + sizes[i] = 10 * (1 + Math.sin(0.1 * i + time)); + } + + geometry.attributes.size.needsUpdate = true; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_buffergeometry_drawrange.ts b/examples-testing/examples/webgl_buffergeometry_drawrange.ts new file mode 100644 index 000000000..142ff43bf --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_drawrange.ts @@ -0,0 +1,239 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let group; +let container, stats; +const particlesData = []; +let camera, scene, renderer; +let positions, colors; +let particles; +let pointCloud; +let particlePositions; +let linesMesh; + +const maxParticleCount = 1000; +let particleCount = 500; +const r = 800; +const rHalf = r / 2; + +const effectController = { + showDots: true, + showLines: true, + minDistance: 150, + limitConnections: false, + maxConnections: 20, + particleCount: 500, +}; + +init(); + +function initGUI() { + const gui = new GUI(); + + gui.add(effectController, 'showDots').onChange(function (value) { + pointCloud.visible = value; + }); + gui.add(effectController, 'showLines').onChange(function (value) { + linesMesh.visible = value; + }); + gui.add(effectController, 'minDistance', 10, 300); + gui.add(effectController, 'limitConnections'); + gui.add(effectController, 'maxConnections', 0, 30, 1); + gui.add(effectController, 'particleCount', 0, maxParticleCount, 1).onChange(function (value) { + particleCount = value; + particles.setDrawRange(0, particleCount); + }); +} + +function init() { + initGUI(); + + container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 4000); + camera.position.z = 1750; + + const controls = new OrbitControls(camera, container); + controls.minDistance = 1000; + controls.maxDistance = 3000; + + scene = new THREE.Scene(); + + group = new THREE.Group(); + scene.add(group); + + const helper = new THREE.BoxHelper(new THREE.Mesh(new THREE.BoxGeometry(r, r, r))); + helper.material.color.setHex(0x474747); + helper.material.blending = THREE.AdditiveBlending; + helper.material.transparent = true; + group.add(helper); + + const segments = maxParticleCount * maxParticleCount; + + positions = new Float32Array(segments * 3); + colors = new Float32Array(segments * 3); + + const pMaterial = new THREE.PointsMaterial({ + color: 0xffffff, + size: 3, + blending: THREE.AdditiveBlending, + transparent: true, + sizeAttenuation: false, + }); + + particles = new THREE.BufferGeometry(); + particlePositions = new Float32Array(maxParticleCount * 3); + + for (let i = 0; i < maxParticleCount; i++) { + const x = Math.random() * r - r / 2; + const y = Math.random() * r - r / 2; + const z = Math.random() * r - r / 2; + + particlePositions[i * 3] = x; + particlePositions[i * 3 + 1] = y; + particlePositions[i * 3 + 2] = z; + + // add it to the geometry + particlesData.push({ + velocity: new THREE.Vector3(-1 + Math.random() * 2, -1 + Math.random() * 2, -1 + Math.random() * 2), + numConnections: 0, + }); + } + + particles.setDrawRange(0, particleCount); + particles.setAttribute( + 'position', + new THREE.BufferAttribute(particlePositions, 3).setUsage(THREE.DynamicDrawUsage), + ); + + // create the particle system + pointCloud = new THREE.Points(particles, pMaterial); + group.add(pointCloud); + + const geometry = new THREE.BufferGeometry(); + + geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3).setUsage(THREE.DynamicDrawUsage)); + geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3).setUsage(THREE.DynamicDrawUsage)); + + geometry.computeBoundingSphere(); + + geometry.setDrawRange(0, 0); + + const material = new THREE.LineBasicMaterial({ + vertexColors: true, + blending: THREE.AdditiveBlending, + transparent: true, + }); + + linesMesh = new THREE.LineSegments(geometry, material); + group.add(linesMesh); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + let vertexpos = 0; + let colorpos = 0; + let numConnected = 0; + + for (let i = 0; i < particleCount; i++) particlesData[i].numConnections = 0; + + for (let i = 0; i < particleCount; i++) { + // get the particle + const particleData = particlesData[i]; + + particlePositions[i * 3] += particleData.velocity.x; + particlePositions[i * 3 + 1] += particleData.velocity.y; + particlePositions[i * 3 + 2] += particleData.velocity.z; + + if (particlePositions[i * 3 + 1] < -rHalf || particlePositions[i * 3 + 1] > rHalf) + particleData.velocity.y = -particleData.velocity.y; + + if (particlePositions[i * 3] < -rHalf || particlePositions[i * 3] > rHalf) + particleData.velocity.x = -particleData.velocity.x; + + if (particlePositions[i * 3 + 2] < -rHalf || particlePositions[i * 3 + 2] > rHalf) + particleData.velocity.z = -particleData.velocity.z; + + if (effectController.limitConnections && particleData.numConnections >= effectController.maxConnections) + continue; + + // Check collision + for (let j = i + 1; j < particleCount; j++) { + const particleDataB = particlesData[j]; + if (effectController.limitConnections && particleDataB.numConnections >= effectController.maxConnections) + continue; + + const dx = particlePositions[i * 3] - particlePositions[j * 3]; + const dy = particlePositions[i * 3 + 1] - particlePositions[j * 3 + 1]; + const dz = particlePositions[i * 3 + 2] - particlePositions[j * 3 + 2]; + const dist = Math.sqrt(dx * dx + dy * dy + dz * dz); + + if (dist < effectController.minDistance) { + particleData.numConnections++; + particleDataB.numConnections++; + + const alpha = 1.0 - dist / effectController.minDistance; + + positions[vertexpos++] = particlePositions[i * 3]; + positions[vertexpos++] = particlePositions[i * 3 + 1]; + positions[vertexpos++] = particlePositions[i * 3 + 2]; + + positions[vertexpos++] = particlePositions[j * 3]; + positions[vertexpos++] = particlePositions[j * 3 + 1]; + positions[vertexpos++] = particlePositions[j * 3 + 2]; + + colors[colorpos++] = alpha; + colors[colorpos++] = alpha; + colors[colorpos++] = alpha; + + colors[colorpos++] = alpha; + colors[colorpos++] = alpha; + colors[colorpos++] = alpha; + + numConnected++; + } + } + } + + linesMesh.geometry.setDrawRange(0, numConnected * 2); + linesMesh.geometry.attributes.position.needsUpdate = true; + linesMesh.geometry.attributes.color.needsUpdate = true; + + pointCloud.geometry.attributes.position.needsUpdate = true; + + render(); + + stats.update(); +} + +function render() { + const time = Date.now() * 0.001; + + group.rotation.y = time * 0.1; + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_buffergeometry_glbufferattribute.ts b/examples-testing/examples/webgl_buffergeometry_glbufferattribute.ts new file mode 100644 index 000000000..aea462cf4 --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_glbufferattribute.ts @@ -0,0 +1,139 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; + +let camera, scene, renderer; + +let points; + +const particles = 300000; +let drawCount = 10000; + +init(); +animate(); + +function init() { + container = document.getElementById('container'); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + + container.appendChild(renderer.domElement); + + // + + camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 5, 3500); + camera.position.z = 2750; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x050505); + scene.fog = new THREE.Fog(0x050505, 2000, 3500); + + // + + const geometry = new THREE.BufferGeometry(); + + const positions = []; + const positions2 = []; + const colors = []; + + const color = new THREE.Color(); + + const n = 1000, + n2 = n / 2; // particles spread in the cube + + for (let i = 0; i < particles; i++) { + // positions + + const x = Math.random() * n - n2; + const y = Math.random() * n - n2; + const z = Math.random() * n - n2; + + positions.push(x, y, z); + positions2.push(z * 0.3, x * 0.3, y * 0.3); + + // colors + + const vx = x / n + 0.5; + const vy = y / n + 0.5; + const vz = z / n + 0.5; + + color.setRGB(vx, vy, vz, THREE.SRGBColorSpace); + + colors.push(color.r, color.g, color.b); + } + + const gl = renderer.getContext(); + + const pos = gl.createBuffer(); + gl.bindBuffer(gl.ARRAY_BUFFER, pos); + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW); + + const pos2 = gl.createBuffer(); + gl.bindBuffer(gl.ARRAY_BUFFER, pos2); + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions2), gl.STATIC_DRAW); + + const rgb = gl.createBuffer(); + gl.bindBuffer(gl.ARRAY_BUFFER, rgb); + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW); + + const posAttr1 = new THREE.GLBufferAttribute(pos, gl.FLOAT, 3, 4, particles); + const posAttr2 = new THREE.GLBufferAttribute(pos2, gl.FLOAT, 3, 4, particles); + geometry.setAttribute('position', posAttr1); + + setInterval(function () { + const attr = geometry.getAttribute('position'); + + geometry.setAttribute('position', attr === posAttr1 ? posAttr2 : posAttr1); + }, 2000); + + geometry.setAttribute('color', new THREE.GLBufferAttribute(rgb, gl.FLOAT, 3, 4, particles)); + + // + + const material = new THREE.PointsMaterial({ size: 15, vertexColors: true }); + + points = new THREE.Points(geometry, material); + + geometry.boundingSphere = new THREE.Sphere().set(new THREE.Vector3(), 500); + + scene.add(points); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + drawCount = (Math.max(5000, drawCount) + Math.floor(500 * Math.random())) % particles; + points.geometry.setDrawRange(0, drawCount); + + const time = Date.now() * 0.001; + + points.rotation.x = time * 0.1; + points.rotation.y = time * 0.2; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_buffergeometry_indexed.ts b/examples-testing/examples/webgl_buffergeometry_indexed.ts new file mode 100644 index 000000000..a2f9f3795 --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_indexed.ts @@ -0,0 +1,137 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, stats; + +let mesh; + +init(); + +function init() { + // + + camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 3500); + camera.position.z = 64; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x050505); + + // + + const light = new THREE.HemisphereLight(); + light.intensity = 3; + scene.add(light); + + // + + const geometry = new THREE.BufferGeometry(); + + const indices = []; + + const vertices = []; + const normals = []; + const colors = []; + + const size = 20; + const segments = 10; + + const halfSize = size / 2; + const segmentSize = size / segments; + + const _color = new THREE.Color(); + + // generate vertices, normals and color data for a simple grid geometry + + for (let i = 0; i <= segments; i++) { + const y = i * segmentSize - halfSize; + + for (let j = 0; j <= segments; j++) { + const x = j * segmentSize - halfSize; + + vertices.push(x, -y, 0); + normals.push(0, 0, 1); + + const r = x / size + 0.5; + const g = y / size + 0.5; + + _color.setRGB(r, g, 1, THREE.SRGBColorSpace); + + colors.push(_color.r, _color.g, _color.b); + } + } + + // generate indices (data for element array buffer) + + for (let i = 0; i < segments; i++) { + for (let j = 0; j < segments; j++) { + const a = i * (segments + 1) + (j + 1); + const b = i * (segments + 1) + j; + const c = (i + 1) * (segments + 1) + j; + const d = (i + 1) * (segments + 1) + (j + 1); + + // generate two faces (triangles) per iteration + + indices.push(a, b, d); // face one + indices.push(b, c, d); // face two + } + } + + // + + geometry.setIndex(indices); + geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); + geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3)); + geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); + + const material = new THREE.MeshPhongMaterial({ + side: THREE.DoubleSide, + vertexColors: true, + }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + const gui = new GUI(); + gui.add(material, 'wireframe'); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const time = Date.now() * 0.001; + + mesh.rotation.x = time * 0.25; + mesh.rotation.y = time * 0.5; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_buffergeometry_instancing.ts b/examples-testing/examples/webgl_buffergeometry_instancing.ts new file mode 100644 index 000000000..a5b90ae6e --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_instancing.ts @@ -0,0 +1,138 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let container, stats; + +let camera, scene, renderer; + +init(); + +function init() { + container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10); + camera.position.z = 2; + + scene = new THREE.Scene(); + + // geometry + + const vector = new THREE.Vector4(); + + const instances = 50000; + + const positions = []; + const offsets = []; + const colors = []; + const orientationsStart = []; + const orientationsEnd = []; + + positions.push(0.025, -0.025, 0); + positions.push(-0.025, 0.025, 0); + positions.push(0, 0, 0.025); + + // instanced attributes + + for (let i = 0; i < instances; i++) { + // offsets + + offsets.push(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5); + + // colors + + colors.push(Math.random(), Math.random(), Math.random(), Math.random()); + + // orientation start + + vector.set(Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1); + vector.normalize(); + + orientationsStart.push(vector.x, vector.y, vector.z, vector.w); + + // orientation end + + vector.set(Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1); + vector.normalize(); + + orientationsEnd.push(vector.x, vector.y, vector.z, vector.w); + } + + const geometry = new THREE.InstancedBufferGeometry(); + geometry.instanceCount = instances; // set so its initalized for dat.GUI, will be set in first draw otherwise + + geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); + + geometry.setAttribute('offset', new THREE.InstancedBufferAttribute(new Float32Array(offsets), 3)); + geometry.setAttribute('color', new THREE.InstancedBufferAttribute(new Float32Array(colors), 4)); + geometry.setAttribute( + 'orientationStart', + new THREE.InstancedBufferAttribute(new Float32Array(orientationsStart), 4), + ); + geometry.setAttribute('orientationEnd', new THREE.InstancedBufferAttribute(new Float32Array(orientationsEnd), 4)); + + // material + + const material = new THREE.RawShaderMaterial({ + uniforms: { + time: { value: 1.0 }, + sineTime: { value: 1.0 }, + }, + vertexShader: document.getElementById('vertexShader').textContent, + fragmentShader: document.getElementById('fragmentShader').textContent, + side: THREE.DoubleSide, + forceSinglePass: true, + transparent: true, + }); + + // + + const mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + const gui = new GUI({ width: 350 }); + gui.add(geometry, 'instanceCount', 0, instances); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const time = performance.now(); + + const object = scene.children[0]; + + object.rotation.y = time * 0.0005; + object.material.uniforms['time'].value = time * 0.005; + object.material.uniforms['sineTime'].value = Math.sin(object.material.uniforms['time'].value * 0.05); + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_buffergeometry_instancing_billboards.ts b/examples-testing/examples/webgl_buffergeometry_instancing_billboards.ts new file mode 100644 index 000000000..2158dff39 --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_instancing_billboards.ts @@ -0,0 +1,86 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; + +let camera, scene, renderer; +let geometry, material, mesh; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 5000); + camera.position.z = 1400; + + scene = new THREE.Scene(); + + const circleGeometry = new THREE.CircleGeometry(1, 6); + + geometry = new THREE.InstancedBufferGeometry(); + geometry.index = circleGeometry.index; + geometry.attributes = circleGeometry.attributes; + + const particleCount = 75000; + + const translateArray = new Float32Array(particleCount * 3); + + for (let i = 0, i3 = 0, l = particleCount; i < l; i++, i3 += 3) { + translateArray[i3 + 0] = Math.random() * 2 - 1; + translateArray[i3 + 1] = Math.random() * 2 - 1; + translateArray[i3 + 2] = Math.random() * 2 - 1; + } + + geometry.setAttribute('translate', new THREE.InstancedBufferAttribute(translateArray, 3)); + + material = new THREE.RawShaderMaterial({ + uniforms: { + map: { value: new THREE.TextureLoader().load('textures/sprites/circle.png') }, + time: { value: 0.0 }, + }, + vertexShader: document.getElementById('vshader').textContent, + fragmentShader: document.getElementById('fshader').textContent, + depthTest: true, + depthWrite: true, + }); + + mesh = new THREE.Mesh(geometry, material); + mesh.scale.set(500, 500, 500); + scene.add(mesh); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); + + return true; +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const time = performance.now() * 0.0005; + + material.uniforms['time'].value = time; + + mesh.rotation.x = time * 0.2; + mesh.rotation.y = time * 0.4; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_buffergeometry_instancing_interleaved.ts b/examples-testing/examples/webgl_buffergeometry_instancing_interleaved.ts new file mode 100644 index 000000000..bef2c264d --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_instancing_interleaved.ts @@ -0,0 +1,152 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; +let camera, scene, renderer, mesh; + +const instances = 5000; +let lastTime = 0; + +const moveQ = new THREE.Quaternion(0.5, 0.5, 0.5, 0.0).normalize(); +const tmpQ = new THREE.Quaternion(); +const tmpM = new THREE.Matrix4(); +const currentM = new THREE.Matrix4(); + +init(); + +function init() { + container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x101010); + + // geometry + + const geometry = new THREE.InstancedBufferGeometry(); + + // per mesh data x,y,z,w,u,v,s,t for 4-element alignment + // only use x,y,z and u,v; but x, y, z, nx, ny, nz, u, v would be a good layout + const vertexBuffer = new THREE.InterleavedBuffer( + new Float32Array([ + // Front + -1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, -1, -1, 1, 0, 0, 1, 0, 0, 1, -1, 1, 0, 1, 1, 0, 0, + // Back + 1, 1, -1, 0, 1, 0, 0, 0, -1, 1, -1, 0, 0, 0, 0, 0, 1, -1, -1, 0, 1, 1, 0, 0, -1, -1, -1, 0, 0, 1, 0, 0, + // Left + -1, 1, -1, 0, 1, 1, 0, 0, -1, 1, 1, 0, 1, 0, 0, 0, -1, -1, -1, 0, 0, 1, 0, 0, -1, -1, 1, 0, 0, 0, 0, 0, + // Right + 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, -1, 0, 1, 1, 0, 0, 1, -1, 1, 0, 0, 0, 0, 0, 1, -1, -1, 0, 0, 1, 0, 0, + // Top + -1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, -1, 1, -1, 0, 0, 1, 0, 0, 1, 1, -1, 0, 1, 1, 0, 0, + // Bottom + 1, -1, 1, 0, 1, 0, 0, 0, -1, -1, 1, 0, 0, 0, 0, 0, 1, -1, -1, 0, 1, 1, 0, 0, -1, -1, -1, 0, 0, 1, 0, 0, + ]), + 8, + ); + + // Use vertexBuffer, starting at offset 0, 3 items in position attribute + const positions = new THREE.InterleavedBufferAttribute(vertexBuffer, 3, 0); + geometry.setAttribute('position', positions); + // Use vertexBuffer, starting at offset 4, 2 items in uv attribute + const uvs = new THREE.InterleavedBufferAttribute(vertexBuffer, 2, 4); + geometry.setAttribute('uv', uvs); + + const indices = new Uint16Array([ + 0, 2, 1, 2, 3, 1, 4, 6, 5, 6, 7, 5, 8, 10, 9, 10, 11, 9, 12, 14, 13, 14, 15, 13, 16, 17, 18, 18, 17, 19, 20, 21, + 22, 22, 21, 23, + ]); + + geometry.setIndex(new THREE.BufferAttribute(indices, 1)); + + // material + + const material = new THREE.MeshBasicMaterial(); + material.map = new THREE.TextureLoader().load('textures/crate.gif'); + material.map.colorSpace = THREE.SRGBColorSpace; + material.map.flipY = false; + + // per instance data + + const matrix = new THREE.Matrix4(); + const offset = new THREE.Vector3(); + const orientation = new THREE.Quaternion(); + const scale = new THREE.Vector3(1, 1, 1); + let x, y, z, w; + + mesh = new THREE.InstancedMesh(geometry, material, instances); + + for (let i = 0; i < instances; i++) { + // offsets + + x = Math.random() * 100 - 50; + y = Math.random() * 100 - 50; + z = Math.random() * 100 - 50; + + offset.set(x, y, z).normalize(); + offset.multiplyScalar(5); // move out at least 5 units from center in current direction + offset.set(x + offset.x, y + offset.y, z + offset.z); + + // orientations + + x = Math.random() * 2 - 1; + y = Math.random() * 2 - 1; + z = Math.random() * 2 - 1; + w = Math.random() * 2 - 1; + + orientation.set(x, y, z, w).normalize(); + + matrix.compose(offset, orientation, scale); + + mesh.setMatrixAt(i, matrix); + } + + scene.add(mesh); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const time = performance.now(); + + mesh.rotation.y = time * 0.00005; + + const delta = (time - lastTime) / 5000; + tmpQ.set(moveQ.x * delta, moveQ.y * delta, moveQ.z * delta, 1).normalize(); + tmpM.makeRotationFromQuaternion(tmpQ); + + for (let i = 0, il = instances; i < il; i++) { + mesh.getMatrixAt(i, currentM); + currentM.multiply(tmpM); + mesh.setMatrixAt(i, currentM); + } + + mesh.instanceMatrix.needsUpdate = true; + mesh.computeBoundingSphere(); + + lastTime = time; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_buffergeometry_lines.ts b/examples-testing/examples/webgl_buffergeometry_lines.ts new file mode 100644 index 000000000..1aaa5ca4a --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_lines.ts @@ -0,0 +1,118 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats, clock; + +let camera, scene, renderer; + +let line; + +const segments = 10000; +const r = 800; +let t = 0; + +init(); + +function init() { + container = document.getElementById('container'); + + // + + camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 4000); + camera.position.z = 2750; + + scene = new THREE.Scene(); + + clock = new THREE.Clock(); + + const geometry = new THREE.BufferGeometry(); + const material = new THREE.LineBasicMaterial({ vertexColors: true }); + + const positions = []; + const colors = []; + + for (let i = 0; i < segments; i++) { + const x = Math.random() * r - r / 2; + const y = Math.random() * r - r / 2; + const z = Math.random() * r - r / 2; + + // positions + + positions.push(x, y, z); + + // colors + + colors.push(x / r + 0.5); + colors.push(y / r + 0.5); + colors.push(z / r + 0.5); + } + + geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); + geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); + generateMorphTargets(geometry); + + geometry.computeBoundingSphere(); + + line = new THREE.Line(geometry, material); + scene.add(line); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + + container.appendChild(renderer.domElement); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const delta = clock.getDelta(); + const time = clock.getElapsedTime(); + + line.rotation.x = time * 0.25; + line.rotation.y = time * 0.5; + + t += delta * 0.5; + line.morphTargetInfluences[0] = Math.abs(Math.sin(t)); + + renderer.render(scene, camera); + + stats.update(); +} + +function generateMorphTargets(geometry) { + const data = []; + + for (let i = 0; i < segments; i++) { + const x = Math.random() * r - r / 2; + const y = Math.random() * r - r / 2; + const z = Math.random() * r - r / 2; + + data.push(x, y, z); + } + + const morphTarget = new THREE.Float32BufferAttribute(data, 3); + morphTarget.name = 'target1'; + + geometry.morphAttributes.position = [morphTarget]; +} diff --git a/examples-testing/examples/webgl_buffergeometry_lines_indexed.ts b/examples-testing/examples/webgl_buffergeometry_lines_indexed.ts new file mode 100644 index 000000000..58296087e --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_lines_indexed.ts @@ -0,0 +1,179 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; + +let camera, scene, renderer; + +let parent_node; + +init(); + +function init() { + container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 9000; + + scene = new THREE.Scene(); + + const geometry = new THREE.BufferGeometry(); + const material = new THREE.LineBasicMaterial({ vertexColors: true }); + + const indices = []; + const positions = []; + const colors = []; + + let next_positions_index = 0; + + // + + const iteration_count = 4; + const rangle = (60 * Math.PI) / 180.0; + + function add_vertex(v) { + positions.push(v.x, v.y, v.z); + colors.push(Math.random() * 0.5 + 0.5, Math.random() * 0.5 + 0.5, 1); + + return next_positions_index++; + } + + // simple Koch curve + + function snowflake_iteration(p0, p4, depth) { + if (--depth < 0) { + const i = next_positions_index - 1; // p0 already there + add_vertex(p4); + indices.push(i, i + 1); + + return; + } + + const v = p4.clone().sub(p0); + const v_tier = v.clone().multiplyScalar(1 / 3); + const p1 = p0.clone().add(v_tier); + + const angle = Math.atan2(v.y, v.x) + rangle; + const length = v_tier.length(); + const p2 = p1.clone(); + p2.x += Math.cos(angle) * length; + p2.y += Math.sin(angle) * length; + + const p3 = p0.clone().add(v_tier).add(v_tier); + + snowflake_iteration(p0, p1, depth); + snowflake_iteration(p1, p2, depth); + snowflake_iteration(p2, p3, depth); + snowflake_iteration(p3, p4, depth); + } + + function snowflake(points, loop, x_offset) { + for (let iteration = 0; iteration != iteration_count; iteration++) { + add_vertex(points[0]); + + for (let p_index = 0, p_count = points.length - 1; p_index != p_count; p_index++) { + snowflake_iteration(points[p_index], points[p_index + 1], iteration); + } + + if (loop) snowflake_iteration(points[points.length - 1], points[0], iteration); + + // translate input curve for next iteration + + for (let p_index = 0, p_count = points.length; p_index != p_count; p_index++) { + points[p_index].x += x_offset; + } + } + } + + let y = 0; + + snowflake([new THREE.Vector3(0, y, 0), new THREE.Vector3(500, y, 0)], false, 600); + + y += 600; + snowflake( + [new THREE.Vector3(0, y, 0), new THREE.Vector3(250, y + 400, 0), new THREE.Vector3(500, y, 0)], + true, + 600, + ); + + y += 600; + snowflake( + [ + new THREE.Vector3(0, y, 0), + new THREE.Vector3(500, y, 0), + new THREE.Vector3(500, y + 500, 0), + new THREE.Vector3(0, y + 500, 0), + ], + true, + 600, + ); + + y += 1000; + snowflake( + [ + new THREE.Vector3(250, y, 0), + new THREE.Vector3(500, y, 0), + new THREE.Vector3(250, y, 0), + new THREE.Vector3(250, y + 250, 0), + new THREE.Vector3(250, y, 0), + new THREE.Vector3(0, y, 0), + new THREE.Vector3(250, y, 0), + new THREE.Vector3(250, y - 250, 0), + new THREE.Vector3(250, y, 0), + ], + false, + 600, + ); + + // + + geometry.setIndex(indices); + geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); + geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); + geometry.computeBoundingSphere(); + + const lineSegments = new THREE.LineSegments(geometry, material); + lineSegments.position.x -= 1200; + lineSegments.position.y -= 1200; + + parent_node = new THREE.Object3D(); + parent_node.add(lineSegments); + + scene.add(parent_node); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + + container.appendChild(renderer.domElement); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const time = Date.now() * 0.001; + + parent_node.rotation.z = time * 0.5; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_buffergeometry_points.ts b/examples-testing/examples/webgl_buffergeometry_points.ts new file mode 100644 index 000000000..4547d9d08 --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_points.ts @@ -0,0 +1,109 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; + +let camera, scene, renderer; + +let points; + +init(); +animate(); + +function init() { + container = document.getElementById('container'); + + // + + camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 5, 3500); + camera.position.z = 2750; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x050505); + scene.fog = new THREE.Fog(0x050505, 2000, 3500); + + // + + const particles = 500000; + + const geometry = new THREE.BufferGeometry(); + + const positions = []; + const colors = []; + + const color = new THREE.Color(); + + const n = 1000, + n2 = n / 2; // particles spread in the cube + + for (let i = 0; i < particles; i++) { + // positions + + const x = Math.random() * n - n2; + const y = Math.random() * n - n2; + const z = Math.random() * n - n2; + + positions.push(x, y, z); + + // colors + + const vx = x / n + 0.5; + const vy = y / n + 0.5; + const vz = z / n + 0.5; + + color.setRGB(vx, vy, vz, THREE.SRGBColorSpace); + + colors.push(color.r, color.g, color.b); + } + + geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); + geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); + + geometry.computeBoundingSphere(); + + // + + const material = new THREE.PointsMaterial({ size: 15, vertexColors: true }); + + points = new THREE.Points(geometry, material); + scene.add(points); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + + container.appendChild(renderer.domElement); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const time = Date.now() * 0.001; + + points.rotation.x = time * 0.25; + points.rotation.y = time * 0.5; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_buffergeometry_points_interleaved.ts b/examples-testing/examples/webgl_buffergeometry_points_interleaved.ts new file mode 100644 index 000000000..93eed992e --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_points_interleaved.ts @@ -0,0 +1,122 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; + +let camera, scene, renderer; + +let points; + +init(); + +function init() { + container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 5, 3500); + camera.position.z = 2750; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x050505); + scene.fog = new THREE.Fog(0x050505, 2000, 3500); + + // + + const particles = 500000; + + const geometry = new THREE.BufferGeometry(); + + // create a generic buffer of binary data (a single particle has 16 bytes of data) + + const arrayBuffer = new ArrayBuffer(particles * 16); + + // the following typed arrays share the same buffer + + const interleavedFloat32Buffer = new Float32Array(arrayBuffer); + const interleavedUint8Buffer = new Uint8Array(arrayBuffer); + + // + + const color = new THREE.Color(); + + const n = 1000, + n2 = n / 2; // particles spread in the cube + + for (let i = 0; i < interleavedFloat32Buffer.length; i += 4) { + // position (first 12 bytes) + + const x = Math.random() * n - n2; + const y = Math.random() * n - n2; + const z = Math.random() * n - n2; + + interleavedFloat32Buffer[i + 0] = x; + interleavedFloat32Buffer[i + 1] = y; + interleavedFloat32Buffer[i + 2] = z; + + // color (last 4 bytes) + + const vx = x / n + 0.5; + const vy = y / n + 0.5; + const vz = z / n + 0.5; + + color.setRGB(vx, vy, vz, THREE.SRGBColorSpace); + + const j = (i + 3) * 4; + + interleavedUint8Buffer[j + 0] = color.r * 255; + interleavedUint8Buffer[j + 1] = color.g * 255; + interleavedUint8Buffer[j + 2] = color.b * 255; + interleavedUint8Buffer[j + 3] = 0; // not needed + } + + const interleavedBuffer32 = new THREE.InterleavedBuffer(interleavedFloat32Buffer, 4); + const interleavedBuffer8 = new THREE.InterleavedBuffer(interleavedUint8Buffer, 16); + + geometry.setAttribute('position', new THREE.InterleavedBufferAttribute(interleavedBuffer32, 3, 0, false)); + geometry.setAttribute('color', new THREE.InterleavedBufferAttribute(interleavedBuffer8, 3, 12, true)); + + // + + const material = new THREE.PointsMaterial({ size: 15, vertexColors: true }); + + points = new THREE.Points(geometry, material); + scene.add(points); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + + container.appendChild(renderer.domElement); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const time = Date.now() * 0.001; + + points.rotation.x = time * 0.25; + points.rotation.y = time * 0.5; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_buffergeometry_rawshader.ts b/examples-testing/examples/webgl_buffergeometry_rawshader.ts new file mode 100644 index 000000000..5bc113dc3 --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_rawshader.ts @@ -0,0 +1,97 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; + +let camera, scene, renderer; + +init(); + +function init() { + container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10); + camera.position.z = 2; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x101010); + + // geometry + // nr of triangles with 3 vertices per triangle + const vertexCount = 200 * 3; + + const geometry = new THREE.BufferGeometry(); + + const positions = []; + const colors = []; + + for (let i = 0; i < vertexCount; i++) { + // adding x,y,z + positions.push(Math.random() - 0.5); + positions.push(Math.random() - 0.5); + positions.push(Math.random() - 0.5); + + // adding r,g,b,a + colors.push(Math.random() * 255); + colors.push(Math.random() * 255); + colors.push(Math.random() * 255); + colors.push(Math.random() * 255); + } + + const positionAttribute = new THREE.Float32BufferAttribute(positions, 3); + const colorAttribute = new THREE.Uint8BufferAttribute(colors, 4); + + colorAttribute.normalized = true; // this will map the buffer values to 0.0f - +1.0f in the shader + + geometry.setAttribute('position', positionAttribute); + geometry.setAttribute('color', colorAttribute); + + // material + + const material = new THREE.RawShaderMaterial({ + uniforms: { + time: { value: 1.0 }, + }, + vertexShader: document.getElementById('vertexShader').textContent, + fragmentShader: document.getElementById('fragmentShader').textContent, + side: THREE.DoubleSide, + transparent: true, + }); + + const mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const time = performance.now(); + + const object = scene.children[0]; + + object.rotation.y = time * 0.0005; + object.material.uniforms.time.value = time * 0.005; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_buffergeometry_selective_draw.ts b/examples-testing/examples/webgl_buffergeometry_selective_draw.ts new file mode 100644 index 000000000..d07176c51 --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_selective_draw.ts @@ -0,0 +1,150 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let camera, scene, renderer, stats; +let geometry, mesh; +const numLat = 100; +const numLng = 200; +let numLinesCulled = 0; + +init(); + +function init() { + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 10); + camera.position.z = 3.5; + + stats = new Stats(); + document.body.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); + + addLines(1.0); + + const hideLinesButton = document.getElementById('hideLines'); + hideLinesButton.addEventListener('click', hideLines); + + const showAllLinesButton = document.getElementById('showAllLines'); + showAllLinesButton.addEventListener('click', showAllLines); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); +} + +function addLines(radius) { + geometry = new THREE.BufferGeometry(); + const linePositions = new Float32Array(numLat * numLng * 3 * 2); + const lineColors = new Float32Array(numLat * numLng * 3 * 2); + const visible = new Float32Array(numLat * numLng * 2); + + for (let i = 0; i < numLat; ++i) { + for (let j = 0; j < numLng; ++j) { + const lat = (Math.random() * Math.PI) / 50.0 + (i / numLat) * Math.PI; + const lng = (Math.random() * Math.PI) / 50.0 + (j / numLng) * 2 * Math.PI; + + const index = i * numLng + j; + + linePositions[index * 6 + 0] = 0; + linePositions[index * 6 + 1] = 0; + linePositions[index * 6 + 2] = 0; + linePositions[index * 6 + 3] = radius * Math.sin(lat) * Math.cos(lng); + linePositions[index * 6 + 4] = radius * Math.cos(lat); + linePositions[index * 6 + 5] = radius * Math.sin(lat) * Math.sin(lng); + + const color = new THREE.Color(0xffffff); + + color.setHSL(lat / Math.PI, 1.0, 0.2); + lineColors[index * 6 + 0] = color.r; + lineColors[index * 6 + 1] = color.g; + lineColors[index * 6 + 2] = color.b; + + color.setHSL(lat / Math.PI, 1.0, 0.7); + lineColors[index * 6 + 3] = color.r; + lineColors[index * 6 + 4] = color.g; + lineColors[index * 6 + 5] = color.b; + + // non-0 is visible + visible[index * 2 + 0] = 1.0; + visible[index * 2 + 1] = 1.0; + } + } + + geometry.setAttribute('position', new THREE.BufferAttribute(linePositions, 3)); + geometry.setAttribute('vertColor', new THREE.BufferAttribute(lineColors, 3)); + geometry.setAttribute('visible', new THREE.BufferAttribute(visible, 1)); + + geometry.computeBoundingSphere(); + + const shaderMaterial = new THREE.ShaderMaterial({ + vertexShader: document.getElementById('vertexshader').textContent, + fragmentShader: document.getElementById('fragmentshader').textContent, + }); + + mesh = new THREE.LineSegments(geometry, shaderMaterial); + scene.add(mesh); + + updateCount(); +} + +function updateCount() { + const str = + '1 draw call, ' + + numLat * numLng + + ' lines, ' + + numLinesCulled + + ' culled (author)'; + document.getElementById('title').innerHTML = str.replace(/\B(?=(\d{3})+(?!\d))/g, ','); +} + +function hideLines() { + for (let i = 0; i < geometry.attributes.visible.array.length; i += 2) { + if (Math.random() > 0.75) { + if (geometry.attributes.visible.array[i + 0]) { + ++numLinesCulled; + } + + geometry.attributes.visible.array[i + 0] = 0; + geometry.attributes.visible.array[i + 1] = 0; + } + } + + geometry.attributes.visible.needsUpdate = true; + + updateCount(); +} + +function showAllLines() { + numLinesCulled = 0; + + for (let i = 0; i < geometry.attributes.visible.array.length; i += 2) { + geometry.attributes.visible.array[i + 0] = 1; + geometry.attributes.visible.array[i + 1] = 1; + } + + geometry.attributes.visible.needsUpdate = true; + + updateCount(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const time = Date.now() * 0.001; + + mesh.rotation.x = time * 0.25; + mesh.rotation.y = time * 0.5; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_buffergeometry_uint.ts b/examples-testing/examples/webgl_buffergeometry_uint.ts new file mode 100644 index 000000000..0b8df6ec7 --- /dev/null +++ b/examples-testing/examples/webgl_buffergeometry_uint.ts @@ -0,0 +1,177 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; + +let camera, scene, renderer; + +let mesh; + +init(); + +function init() { + container = document.getElementById('container'); + + // + + camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 3500); + camera.position.z = 2750; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x050505); + scene.fog = new THREE.Fog(0x050505, 2000, 3500); + + // + + scene.add(new THREE.AmbientLight(0xcccccc)); + + const light1 = new THREE.DirectionalLight(0xffffff, 1.5); + light1.position.set(1, 1, 1); + scene.add(light1); + + const light2 = new THREE.DirectionalLight(0xffffff, 4.5); + light2.position.set(0, -1, 0); + scene.add(light2); + + // + + const triangles = 500000; + + const geometry = new THREE.BufferGeometry(); + + const positions = []; + const normals = []; + const colors = []; + + const color = new THREE.Color(); + + const n = 800, + n2 = n / 2; // triangles spread in the cube + const d = 12, + d2 = d / 2; // individual triangle size + + const pA = new THREE.Vector3(); + const pB = new THREE.Vector3(); + const pC = new THREE.Vector3(); + + const cb = new THREE.Vector3(); + const ab = new THREE.Vector3(); + + for (let i = 0; i < triangles; i++) { + // positions + + const x = Math.random() * n - n2; + const y = Math.random() * n - n2; + const z = Math.random() * n - n2; + + const ax = x + Math.random() * d - d2; + const ay = y + Math.random() * d - d2; + const az = z + Math.random() * d - d2; + + const bx = x + Math.random() * d - d2; + const by = y + Math.random() * d - d2; + const bz = z + Math.random() * d - d2; + + const cx = x + Math.random() * d - d2; + const cy = y + Math.random() * d - d2; + const cz = z + Math.random() * d - d2; + + positions.push(ax, ay, az); + positions.push(bx, by, bz); + positions.push(cx, cy, cz); + + // flat face normals + + pA.set(ax, ay, az); + pB.set(bx, by, bz); + pC.set(cx, cy, cz); + + cb.subVectors(pC, pB); + ab.subVectors(pA, pB); + cb.cross(ab); + + cb.normalize(); + + const nx = cb.x; + const ny = cb.y; + const nz = cb.z; + + normals.push(nx * 32767, ny * 32767, nz * 32767); + normals.push(nx * 32767, ny * 32767, nz * 32767); + normals.push(nx * 32767, ny * 32767, nz * 32767); + + // colors + + const vx = x / n + 0.5; + const vy = y / n + 0.5; + const vz = z / n + 0.5; + + color.setRGB(vx, vy, vz); + + colors.push(color.r * 255, color.g * 255, color.b * 255); + colors.push(color.r * 255, color.g * 255, color.b * 255); + colors.push(color.r * 255, color.g * 255, color.b * 255); + } + + const positionAttribute = new THREE.Float32BufferAttribute(positions, 3); + const normalAttribute = new THREE.Int16BufferAttribute(normals, 3); + const colorAttribute = new THREE.Uint8BufferAttribute(colors, 3); + + normalAttribute.normalized = true; // this will map the buffer values to 0.0f - +1.0f in the shader + colorAttribute.normalized = true; + + geometry.setAttribute('position', positionAttribute); + geometry.setAttribute('normal', normalAttribute); + geometry.setAttribute('color', colorAttribute); + + geometry.computeBoundingSphere(); + + const material = new THREE.MeshPhongMaterial({ + color: 0xd5d5d5, + specular: 0xffffff, + shininess: 250, + side: THREE.DoubleSide, + vertexColors: true, + }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const time = Date.now() * 0.001; + + mesh.rotation.x = time * 0.25; + mesh.rotation.y = time * 0.5; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_camera.ts b/examples-testing/examples/webgl_camera.ts new file mode 100644 index 000000000..f3d663603 --- /dev/null +++ b/examples-testing/examples/webgl_camera.ts @@ -0,0 +1,218 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let SCREEN_WIDTH = window.innerWidth; +let SCREEN_HEIGHT = window.innerHeight; +let aspect = SCREEN_WIDTH / SCREEN_HEIGHT; + +let container, stats; +let camera, scene, renderer, mesh; +let cameraRig, activeCamera, activeHelper; +let cameraPerspective, cameraOrtho; +let cameraPerspectiveHelper, cameraOrthoHelper; +const frustumSize = 600; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + scene = new THREE.Scene(); + + // + + camera = new THREE.PerspectiveCamera(50, 0.5 * aspect, 1, 10000); + camera.position.z = 2500; + + cameraPerspective = new THREE.PerspectiveCamera(50, 0.5 * aspect, 150, 1000); + + cameraPerspectiveHelper = new THREE.CameraHelper(cameraPerspective); + scene.add(cameraPerspectiveHelper); + + // + cameraOrtho = new THREE.OrthographicCamera( + (0.5 * frustumSize * aspect) / -2, + (0.5 * frustumSize * aspect) / 2, + frustumSize / 2, + frustumSize / -2, + 150, + 1000, + ); + + cameraOrthoHelper = new THREE.CameraHelper(cameraOrtho); + scene.add(cameraOrthoHelper); + + // + + activeCamera = cameraPerspective; + activeHelper = cameraPerspectiveHelper; + + // counteract different front orientation of cameras vs rig + + cameraOrtho.rotation.y = Math.PI; + cameraPerspective.rotation.y = Math.PI; + + cameraRig = new THREE.Group(); + + cameraRig.add(cameraPerspective); + cameraRig.add(cameraOrtho); + + scene.add(cameraRig); + + // + + mesh = new THREE.Mesh( + new THREE.SphereGeometry(100, 16, 8), + new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: true }), + ); + scene.add(mesh); + + const mesh2 = new THREE.Mesh( + new THREE.SphereGeometry(50, 16, 8), + new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: true }), + ); + mesh2.position.y = 150; + mesh.add(mesh2); + + const mesh3 = new THREE.Mesh( + new THREE.SphereGeometry(5, 16, 8), + new THREE.MeshBasicMaterial({ color: 0x0000ff, wireframe: true }), + ); + mesh3.position.z = 150; + cameraRig.add(mesh3); + + // + + const geometry = new THREE.BufferGeometry(); + const vertices = []; + + for (let i = 0; i < 10000; i++) { + vertices.push(THREE.MathUtils.randFloatSpread(2000)); // x + vertices.push(THREE.MathUtils.randFloatSpread(2000)); // y + vertices.push(THREE.MathUtils.randFloatSpread(2000)); // z + } + + geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); + + const particles = new THREE.Points(geometry, new THREE.PointsMaterial({ color: 0x888888 })); + scene.add(particles); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + renderer.setScissorTest(true); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); + document.addEventListener('keydown', onKeyDown); +} + +// + +function onKeyDown(event) { + switch (event.keyCode) { + case 79 /*O*/: + activeCamera = cameraOrtho; + activeHelper = cameraOrthoHelper; + + break; + + case 80 /*P*/: + activeCamera = cameraPerspective; + activeHelper = cameraPerspectiveHelper; + + break; + } +} + +// + +function onWindowResize() { + SCREEN_WIDTH = window.innerWidth; + SCREEN_HEIGHT = window.innerHeight; + aspect = SCREEN_WIDTH / SCREEN_HEIGHT; + + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + + camera.aspect = 0.5 * aspect; + camera.updateProjectionMatrix(); + + cameraPerspective.aspect = 0.5 * aspect; + cameraPerspective.updateProjectionMatrix(); + + cameraOrtho.left = (-0.5 * frustumSize * aspect) / 2; + cameraOrtho.right = (0.5 * frustumSize * aspect) / 2; + cameraOrtho.top = frustumSize / 2; + cameraOrtho.bottom = -frustumSize / 2; + cameraOrtho.updateProjectionMatrix(); +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + const r = Date.now() * 0.0005; + + mesh.position.x = 700 * Math.cos(r); + mesh.position.z = 700 * Math.sin(r); + mesh.position.y = 700 * Math.sin(r); + + mesh.children[0].position.x = 70 * Math.cos(2 * r); + mesh.children[0].position.z = 70 * Math.sin(r); + + if (activeCamera === cameraPerspective) { + cameraPerspective.fov = 35 + 30 * Math.sin(0.5 * r); + cameraPerspective.far = mesh.position.length(); + cameraPerspective.updateProjectionMatrix(); + + cameraPerspectiveHelper.update(); + cameraPerspectiveHelper.visible = true; + + cameraOrthoHelper.visible = false; + } else { + cameraOrtho.far = mesh.position.length(); + cameraOrtho.updateProjectionMatrix(); + + cameraOrthoHelper.update(); + cameraOrthoHelper.visible = true; + + cameraPerspectiveHelper.visible = false; + } + + cameraRig.lookAt(mesh.position); + + // + + activeHelper.visible = false; + + renderer.setClearColor(0x000000, 1); + renderer.setScissor(0, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT); + renderer.setViewport(0, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT); + renderer.render(scene, activeCamera); + + // + + activeHelper.visible = true; + + renderer.setClearColor(0x111111, 1); + renderer.setScissor(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT); + renderer.setViewport(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT); + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_camera_array.ts b/examples-testing/examples/webgl_camera_array.ts new file mode 100644 index 000000000..8b10e27cb --- /dev/null +++ b/examples-testing/examples/webgl_camera_array.ts @@ -0,0 +1,104 @@ +import * as THREE from 'three'; + +let camera, scene, renderer; +let mesh; +const AMOUNT = 6; + +init(); + +function init() { + const ASPECT_RATIO = window.innerWidth / window.innerHeight; + + const WIDTH = (window.innerWidth / AMOUNT) * window.devicePixelRatio; + const HEIGHT = (window.innerHeight / AMOUNT) * window.devicePixelRatio; + + const cameras = []; + + for (let y = 0; y < AMOUNT; y++) { + for (let x = 0; x < AMOUNT; x++) { + const subcamera = new THREE.PerspectiveCamera(40, ASPECT_RATIO, 0.1, 10); + subcamera.viewport = new THREE.Vector4( + Math.floor(x * WIDTH), + Math.floor(y * HEIGHT), + Math.ceil(WIDTH), + Math.ceil(HEIGHT), + ); + subcamera.position.x = x / AMOUNT - 0.5; + subcamera.position.y = 0.5 - y / AMOUNT; + subcamera.position.z = 1.5; + subcamera.position.multiplyScalar(2); + subcamera.lookAt(0, 0, 0); + subcamera.updateMatrixWorld(); + cameras.push(subcamera); + } + } + + camera = new THREE.ArrayCamera(cameras); + camera.position.z = 3; + + scene = new THREE.Scene(); + + scene.add(new THREE.AmbientLight(0x999999)); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0.5, 0.5, 1); + light.castShadow = true; + light.shadow.camera.zoom = 4; // tighter shadow map + scene.add(light); + + const geometryBackground = new THREE.PlaneGeometry(100, 100); + const materialBackground = new THREE.MeshPhongMaterial({ color: 0x000066 }); + + const background = new THREE.Mesh(geometryBackground, materialBackground); + background.receiveShadow = true; + background.position.set(0, 0, -1); + scene.add(background); + + const geometryCylinder = new THREE.CylinderGeometry(0.5, 0.5, 1, 32); + const materialCylinder = new THREE.MeshPhongMaterial({ color: 0xff0000 }); + + mesh = new THREE.Mesh(geometryCylinder, materialCylinder); + mesh.castShadow = true; + mesh.receiveShadow = true; + scene.add(mesh); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + document.body.appendChild(renderer.domElement); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const ASPECT_RATIO = window.innerWidth / window.innerHeight; + const WIDTH = (window.innerWidth / AMOUNT) * window.devicePixelRatio; + const HEIGHT = (window.innerHeight / AMOUNT) * window.devicePixelRatio; + + camera.aspect = ASPECT_RATIO; + camera.updateProjectionMatrix(); + + for (let y = 0; y < AMOUNT; y++) { + for (let x = 0; x < AMOUNT; x++) { + const subcamera = camera.cameras[AMOUNT * y + x]; + + subcamera.viewport.set(Math.floor(x * WIDTH), Math.floor(y * HEIGHT), Math.ceil(WIDTH), Math.ceil(HEIGHT)); + + subcamera.aspect = ASPECT_RATIO; + subcamera.updateProjectionMatrix(); + } + } + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + mesh.rotation.x += 0.005; + mesh.rotation.z += 0.01; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_camera_logarithmicdepthbuffer.ts b/examples-testing/examples/webgl_camera_logarithmicdepthbuffer.ts new file mode 100644 index 000000000..f1d440004 --- /dev/null +++ b/examples-testing/examples/webgl_camera_logarithmicdepthbuffer.ts @@ -0,0 +1,248 @@ +import * as THREE from 'three'; + +import { FontLoader } from 'three/addons/loaders/FontLoader.js'; +import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; + +import Stats from 'three/addons/libs/stats.module.js'; + +// 1 micrometer to 100 billion light years in one scene, with 1 unit = 1 meter? preposterous! and yet... +const NEAR = 1e-6, + FAR = 1e27; +let SCREEN_WIDTH = window.innerWidth; +let SCREEN_HEIGHT = window.innerHeight; +let screensplit = 0.25, + screensplit_right = 0; +const mouse = [0.5, 0.5]; +let zoompos = -100, + minzoomspeed = 0.015; +let zoomspeed = minzoomspeed; + +let container, border, stats; +const objects = {}; + +// Generate a number of text labels, from 1µm in size up to 100,000,000 light years +// Try to use some descriptive real-world examples of objects at each scale + +const labeldata = [ + { size: 0.01, scale: 0.0001, label: 'microscopic (1µm)' }, // FIXME - triangulating text fails at this size, so we scale instead + { size: 0.01, scale: 0.1, label: 'minuscule (1mm)' }, + { size: 0.01, scale: 1.0, label: 'tiny (1cm)' }, + { size: 1, scale: 1.0, label: 'child-sized (1m)' }, + { size: 10, scale: 1.0, label: 'tree-sized (10m)' }, + { size: 100, scale: 1.0, label: 'building-sized (100m)' }, + { size: 1000, scale: 1.0, label: 'medium (1km)' }, + { size: 10000, scale: 1.0, label: 'city-sized (10km)' }, + { size: 3400000, scale: 1.0, label: 'moon-sized (3,400 Km)' }, + { size: 12000000, scale: 1.0, label: 'planet-sized (12,000 km)' }, + { size: 1400000000, scale: 1.0, label: 'sun-sized (1,400,000 km)' }, + { size: 7.47e12, scale: 1.0, label: 'solar system-sized (50Au)' }, + { size: 9.4605284e15, scale: 1.0, label: 'gargantuan (1 light year)' }, + { size: 3.08567758e16, scale: 1.0, label: 'ludicrous (1 parsec)' }, + { size: 1e19, scale: 1.0, label: 'mind boggling (1000 light years)' }, +]; + +init(); + +function init() { + container = document.getElementById('container'); + + const loader = new FontLoader(); + loader.load('fonts/helvetiker_regular.typeface.json', function (font) { + const scene = initScene(font); + + // Initialize two copies of the same scene, one with normal z-buffer and one with logarithmic z-buffer + objects.normal = initView(scene, 'normal', false); + objects.logzbuf = initView(scene, 'logzbuf', true); + + animate(); + }); + + stats = new Stats(); + container.appendChild(stats.dom); + + // Resize border allows the user to easily compare effects of logarithmic depth buffer over the whole scene + border = document.getElementById('renderer_border'); + border.addEventListener('pointerdown', onBorderPointerDown); + + window.addEventListener('mousemove', onMouseMove); + window.addEventListener('resize', onWindowResize); + window.addEventListener('wheel', onMouseWheel); +} + +function initView(scene, name, logDepthBuf) { + const framecontainer = document.getElementById('container_' + name); + + const camera = new THREE.PerspectiveCamera(50, (screensplit * SCREEN_WIDTH) / SCREEN_HEIGHT, NEAR, FAR); + scene.add(camera); + + const renderer = new THREE.WebGLRenderer({ antialias: true, logarithmicDepthBuffer: logDepthBuf }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(SCREEN_WIDTH / 2, SCREEN_HEIGHT); + renderer.domElement.style.position = 'relative'; + renderer.domElement.id = 'renderer_' + name; + framecontainer.appendChild(renderer.domElement); + + return { container: framecontainer, renderer: renderer, scene: scene, camera: camera }; +} + +function initScene(font) { + const scene = new THREE.Scene(); + + scene.add(new THREE.AmbientLight(0x777777)); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(100, 100, 100); + scene.add(light); + + const materialargs = { + color: 0xffffff, + specular: 0x050505, + shininess: 50, + emissive: 0x000000, + }; + + const geometry = new THREE.SphereGeometry(0.5, 24, 12); + + for (let i = 0; i < labeldata.length; i++) { + const scale = labeldata[i].scale || 1; + + const labelgeo = new TextGeometry(labeldata[i].label, { + font: font, + size: labeldata[i].size, + depth: labeldata[i].size / 2, + }); + + labelgeo.computeBoundingSphere(); + + // center text + labelgeo.translate(-labelgeo.boundingSphere.radius, 0, 0); + + materialargs.color = new THREE.Color().setHSL(Math.random(), 0.5, 0.5); + + const material = new THREE.MeshPhongMaterial(materialargs); + + const group = new THREE.Group(); + group.position.z = -labeldata[i].size * scale; + scene.add(group); + + const textmesh = new THREE.Mesh(labelgeo, material); + textmesh.scale.set(scale, scale, scale); + textmesh.position.z = -labeldata[i].size * scale; + textmesh.position.y = (labeldata[i].size / 4) * scale; + group.add(textmesh); + + const dotmesh = new THREE.Mesh(geometry, material); + dotmesh.position.y = (-labeldata[i].size / 4) * scale; + dotmesh.scale.multiplyScalar(labeldata[i].size * scale); + group.add(dotmesh); + } + + return scene; +} + +function updateRendererSizes() { + // Recalculate size for both renderers when screen size or split location changes + + SCREEN_WIDTH = window.innerWidth; + SCREEN_HEIGHT = window.innerHeight; + + screensplit_right = 1 - screensplit; + + objects.normal.renderer.setSize(screensplit * SCREEN_WIDTH, SCREEN_HEIGHT); + objects.normal.camera.aspect = (screensplit * SCREEN_WIDTH) / SCREEN_HEIGHT; + objects.normal.camera.updateProjectionMatrix(); + objects.normal.camera.setViewOffset(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, SCREEN_WIDTH * screensplit, SCREEN_HEIGHT); + objects.normal.container.style.width = screensplit * 100 + '%'; + + objects.logzbuf.renderer.setSize(screensplit_right * SCREEN_WIDTH, SCREEN_HEIGHT); + objects.logzbuf.camera.aspect = (screensplit_right * SCREEN_WIDTH) / SCREEN_HEIGHT; + objects.logzbuf.camera.updateProjectionMatrix(); + objects.logzbuf.camera.setViewOffset( + SCREEN_WIDTH, + SCREEN_HEIGHT, + SCREEN_WIDTH * screensplit, + 0, + SCREEN_WIDTH * screensplit_right, + SCREEN_HEIGHT, + ); + objects.logzbuf.container.style.width = screensplit_right * 100 + '%'; + + border.style.left = screensplit * 100 + '%'; +} + +function animate() { + requestAnimationFrame(animate); + render(); +} + +function render() { + // Put some limits on zooming + const minzoom = labeldata[0].size * labeldata[0].scale * 1; + const maxzoom = labeldata[labeldata.length - 1].size * labeldata[labeldata.length - 1].scale * 100; + let damping = Math.abs(zoomspeed) > minzoomspeed ? 0.95 : 1.0; + + // Zoom out faster the further out you go + const zoom = THREE.MathUtils.clamp(Math.pow(Math.E, zoompos), minzoom, maxzoom); + zoompos = Math.log(zoom); + + // Slow down quickly at the zoom limits + if ((zoom == minzoom && zoomspeed < 0) || (zoom == maxzoom && zoomspeed > 0)) { + damping = 0.85; + } + + zoompos += zoomspeed; + zoomspeed *= damping; + + objects.normal.camera.position.x = Math.sin(0.5 * Math.PI * (mouse[0] - 0.5)) * zoom; + objects.normal.camera.position.y = Math.sin(0.25 * Math.PI * (mouse[1] - 0.5)) * zoom; + objects.normal.camera.position.z = Math.cos(0.5 * Math.PI * (mouse[0] - 0.5)) * zoom; + objects.normal.camera.lookAt(objects.normal.scene.position); + + // Clone camera settings across both scenes + objects.logzbuf.camera.position.copy(objects.normal.camera.position); + objects.logzbuf.camera.quaternion.copy(objects.normal.camera.quaternion); + + // Update renderer sizes if the split has changed + if (screensplit_right != 1 - screensplit) { + updateRendererSizes(); + } + + objects.normal.renderer.render(objects.normal.scene, objects.normal.camera); + objects.logzbuf.renderer.render(objects.logzbuf.scene, objects.logzbuf.camera); + + stats.update(); +} + +function onWindowResize() { + updateRendererSizes(); +} + +function onBorderPointerDown() { + // activate draggable window resizing bar + window.addEventListener('pointermove', onBorderPointerMove); + window.addEventListener('pointerup', onBorderPointerUp); +} + +function onBorderPointerMove(ev) { + screensplit = Math.max(0, Math.min(1, ev.clientX / window.innerWidth)); +} + +function onBorderPointerUp() { + window.removeEventListener('pointermove', onBorderPointerMove); + window.removeEventListener('pointerup', onBorderPointerUp); +} + +function onMouseMove(ev) { + mouse[0] = ev.clientX / window.innerWidth; + mouse[1] = ev.clientY / window.innerHeight; +} + +function onMouseWheel(ev) { + const amount = ev.deltaY; + if (amount === 0) return; + const dir = amount / Math.abs(amount); + zoomspeed = dir / 10; + + // Slow down default zoom speed after user starts zooming, to give them more control + minzoomspeed = 0.001; +} diff --git a/examples-testing/examples/webgl_clipculldistance.ts b/examples-testing/examples/webgl_clipculldistance.ts new file mode 100644 index 000000000..a5fb54d47 --- /dev/null +++ b/examples-testing/examples/webgl_clipculldistance.ts @@ -0,0 +1,110 @@ +import * as THREE from 'three'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import Stats from 'three/addons/libs/stats.module.js'; + +let camera, controls, clock, scene, renderer, stats; + +let material; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10); + camera.position.z = 2; + + scene = new THREE.Scene(); + + clock = new THREE.Clock(); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + if (renderer.extensions.has('WEBGL_clip_cull_distance') === false) { + document.getElementById('notSupported').style.display = ''; + return; + } + + const ext = renderer.getContext().getExtension('WEBGL_clip_cull_distance'); + const gl = renderer.getContext(); + + gl.enable(ext.CLIP_DISTANCE0_WEBGL); + + // geometry + + const vertexCount = 200 * 3; + + const geometry = new THREE.BufferGeometry(); + + const positions = []; + const colors = []; + + for (let i = 0; i < vertexCount; i++) { + // adding x,y,z + positions.push(Math.random() - 0.5); + positions.push(Math.random() - 0.5); + positions.push(Math.random() - 0.5); + + // adding r,g,b,a + colors.push(Math.random() * 255); + colors.push(Math.random() * 255); + colors.push(Math.random() * 255); + colors.push(Math.random() * 255); + } + + const positionAttribute = new THREE.Float32BufferAttribute(positions, 3); + const colorAttribute = new THREE.Uint8BufferAttribute(colors, 4); + colorAttribute.normalized = true; + + geometry.setAttribute('position', positionAttribute); + geometry.setAttribute('color', colorAttribute); + + // material + + material = new THREE.ShaderMaterial({ + uniforms: { + time: { value: 1.0 }, + }, + vertexShader: document.getElementById('vertexShader').textContent, + fragmentShader: document.getElementById('fragmentShader').textContent, + side: THREE.DoubleSide, + transparent: true, + vertexColors: true, + }); + + material.extensions.clipCullDistance = true; + + const mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + controls = new OrbitControls(camera, renderer.domElement); + + // + + stats = new Stats(); + document.body.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + controls.update(); + stats.update(); + + material.uniforms.time.value = clock.getElapsedTime(); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_clipping.ts b/examples-testing/examples/webgl_clipping.ts new file mode 100644 index 000000000..cde10c7d1 --- /dev/null +++ b/examples-testing/examples/webgl_clipping.ts @@ -0,0 +1,195 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer, startTime, object, stats; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(36, window.innerWidth / window.innerHeight, 0.25, 16); + + camera.position.set(0, 1.3, 3); + + scene = new THREE.Scene(); + + // Lights + + scene.add(new THREE.AmbientLight(0xcccccc)); + + const spotLight = new THREE.SpotLight(0xffffff, 60); + spotLight.angle = Math.PI / 5; + spotLight.penumbra = 0.2; + spotLight.position.set(2, 3, 3); + spotLight.castShadow = true; + spotLight.shadow.camera.near = 3; + spotLight.shadow.camera.far = 10; + spotLight.shadow.mapSize.width = 1024; + spotLight.shadow.mapSize.height = 1024; + scene.add(spotLight); + + const dirLight = new THREE.DirectionalLight(0x55505a, 3); + dirLight.position.set(0, 3, 0); + dirLight.castShadow = true; + dirLight.shadow.camera.near = 1; + dirLight.shadow.camera.far = 10; + + dirLight.shadow.camera.right = 1; + dirLight.shadow.camera.left = -1; + dirLight.shadow.camera.top = 1; + dirLight.shadow.camera.bottom = -1; + + dirLight.shadow.mapSize.width = 1024; + dirLight.shadow.mapSize.height = 1024; + scene.add(dirLight); + + // ***** Clipping planes: ***** + + const localPlane = new THREE.Plane(new THREE.Vector3(0, -1, 0), 0.8); + const globalPlane = new THREE.Plane(new THREE.Vector3(-1, 0, 0), 0.1); + + // Geometry + + const material = new THREE.MeshPhongMaterial({ + color: 0x80ee10, + shininess: 100, + side: THREE.DoubleSide, + + // ***** Clipping setup (material): ***** + clippingPlanes: [localPlane], + clipShadows: true, + + alphaToCoverage: true, + }); + + const geometry = new THREE.TorusKnotGeometry(0.4, 0.08, 95, 20); + + object = new THREE.Mesh(geometry, material); + object.castShadow = true; + scene.add(object); + + const ground = new THREE.Mesh( + new THREE.PlaneGeometry(9, 9, 1, 1), + new THREE.MeshPhongMaterial({ color: 0xa0adaf, shininess: 150 }), + ); + + ground.rotation.x = -Math.PI / 2; // rotates X/Y to X/Z + ground.receiveShadow = true; + scene.add(ground); + + // Renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + document.body.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); + + // ***** Clipping setup (renderer): ***** + const globalPlanes = [globalPlane], + Empty = Object.freeze([]); + renderer.clippingPlanes = Empty; // GUI sets it to globalPlanes + renderer.localClippingEnabled = true; + + // Stats + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // Controls + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 1, 0); + controls.update(); + + // GUI + + const gui = new GUI(), + props = { + alphaToCoverage: true, + }, + folderLocal = gui.addFolder('Local Clipping'), + propsLocal = { + get Enabled() { + return renderer.localClippingEnabled; + }, + set Enabled(v) { + renderer.localClippingEnabled = v; + }, + + get Shadows() { + return material.clipShadows; + }, + set Shadows(v) { + material.clipShadows = v; + }, + + get Plane() { + return localPlane.constant; + }, + set Plane(v) { + localPlane.constant = v; + }, + }, + folderGlobal = gui.addFolder('Global Clipping'), + propsGlobal = { + get Enabled() { + return renderer.clippingPlanes !== Empty; + }, + set Enabled(v) { + renderer.clippingPlanes = v ? globalPlanes : Empty; + }, + + get Plane() { + return globalPlane.constant; + }, + set Plane(v) { + globalPlane.constant = v; + }, + }; + + gui.add(props, 'alphaToCoverage').onChange(function (value) { + ground.material.alphaToCoverage = value; + ground.material.needsUpdate = true; + + material.alphaToCoverage = value; + material.needsUpdate = true; + }); + folderLocal.add(propsLocal, 'Enabled'); + folderLocal.add(propsLocal, 'Shadows'); + folderLocal.add(propsLocal, 'Plane', 0.3, 1.25); + + folderGlobal.add(propsGlobal, 'Enabled'); + folderGlobal.add(propsGlobal, 'Plane', -0.4, 3); + + // Start + + startTime = Date.now(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const currentTime = Date.now(); + const time = (currentTime - startTime) / 1000; + + object.position.y = 0.8; + object.rotation.x = time * 0.5; + object.rotation.y = time * 0.2; + object.scale.setScalar(Math.cos(time) * 0.125 + 0.875); + + stats.begin(); + renderer.render(scene, camera); + stats.end(); +} diff --git a/examples-testing/examples/webgl_clipping_advanced.ts b/examples-testing/examples/webgl_clipping_advanced.ts new file mode 100644 index 000000000..614d710da --- /dev/null +++ b/examples-testing/examples/webgl_clipping_advanced.ts @@ -0,0 +1,355 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +function planesFromMesh(vertices, indices) { + // creates a clipping volume from a convex triangular mesh + // specified by the arrays 'vertices' and 'indices' + + const n = indices.length / 3, + result = new Array(n); + + for (let i = 0, j = 0; i < n; ++i, j += 3) { + const a = vertices[indices[j]], + b = vertices[indices[j + 1]], + c = vertices[indices[j + 2]]; + + result[i] = new THREE.Plane().setFromCoplanarPoints(a, b, c); + } + + return result; +} + +function createPlanes(n) { + // creates an array of n uninitialized plane objects + + const result = new Array(n); + + for (let i = 0; i !== n; ++i) result[i] = new THREE.Plane(); + + return result; +} + +function assignTransformedPlanes(planesOut, planesIn, matrix) { + // sets an array of existing planes to transformed 'planesIn' + + for (let i = 0, n = planesIn.length; i !== n; ++i) planesOut[i].copy(planesIn[i]).applyMatrix4(matrix); +} + +function cylindricalPlanes(n, innerRadius) { + const result = createPlanes(n); + + for (let i = 0; i !== n; ++i) { + const plane = result[i], + angle = (i * Math.PI * 2) / n; + + plane.normal.set(Math.cos(angle), 0, Math.sin(angle)); + + plane.constant = innerRadius; + } + + return result; +} + +const planeToMatrix = (function () { + // creates a matrix that aligns X/Y to a given plane + + // temporaries: + const xAxis = new THREE.Vector3(), + yAxis = new THREE.Vector3(), + trans = new THREE.Vector3(); + + return function planeToMatrix(plane) { + const zAxis = plane.normal, + matrix = new THREE.Matrix4(); + + // Hughes & Moeller '99 + // "Building an Orthonormal Basis from a Unit Vector." + + if (Math.abs(zAxis.x) > Math.abs(zAxis.z)) { + yAxis.set(-zAxis.y, zAxis.x, 0); + } else { + yAxis.set(0, -zAxis.z, zAxis.y); + } + + xAxis.crossVectors(yAxis.normalize(), zAxis); + + plane.coplanarPoint(trans); + return matrix.set( + xAxis.x, + yAxis.x, + zAxis.x, + trans.x, + xAxis.y, + yAxis.y, + zAxis.y, + trans.y, + xAxis.z, + yAxis.z, + zAxis.z, + trans.z, + 0, + 0, + 0, + 1, + ); + }; +})(); + +// A regular tetrahedron for the clipping volume: + +const Vertices = [ + new THREE.Vector3(+1, 0, +Math.SQRT1_2), + new THREE.Vector3(-1, 0, +Math.SQRT1_2), + new THREE.Vector3(0, +1, -Math.SQRT1_2), + new THREE.Vector3(0, -1, -Math.SQRT1_2), + ], + Indices = [0, 1, 2, 0, 2, 3, 0, 3, 1, 1, 3, 2], + Planes = planesFromMesh(Vertices, Indices), + PlaneMatrices = Planes.map(planeToMatrix), + GlobalClippingPlanes = cylindricalPlanes(5, 2.5), + Empty = Object.freeze([]); + +let camera, scene, renderer, startTime, stats, object, clipMaterial, volumeVisualization, globalClippingPlanes; + +function init() { + camera = new THREE.PerspectiveCamera(36, window.innerWidth / window.innerHeight, 0.25, 16); + + camera.position.set(0, 1.5, 3); + + scene = new THREE.Scene(); + + // Lights + + scene.add(new THREE.AmbientLight(0xffffff)); + + const spotLight = new THREE.SpotLight(0xffffff, 60); + spotLight.angle = Math.PI / 5; + spotLight.penumbra = 0.2; + spotLight.position.set(2, 3, 3); + spotLight.castShadow = true; + spotLight.shadow.camera.near = 3; + spotLight.shadow.camera.far = 10; + spotLight.shadow.mapSize.width = 1024; + spotLight.shadow.mapSize.height = 1024; + scene.add(spotLight); + + const dirLight = new THREE.DirectionalLight(0xffffff, 1.5); + dirLight.position.set(0, 2, 0); + dirLight.castShadow = true; + dirLight.shadow.camera.near = 1; + dirLight.shadow.camera.far = 10; + + dirLight.shadow.camera.right = 1; + dirLight.shadow.camera.left = -1; + dirLight.shadow.camera.top = 1; + dirLight.shadow.camera.bottom = -1; + + dirLight.shadow.mapSize.width = 1024; + dirLight.shadow.mapSize.height = 1024; + scene.add(dirLight); + + // Geometry + + clipMaterial = new THREE.MeshPhongMaterial({ + color: 0xee0a10, + shininess: 100, + side: THREE.DoubleSide, + // Clipping setup: + clippingPlanes: createPlanes(Planes.length), + clipShadows: true, + }); + + object = new THREE.Group(); + + const geometry = new THREE.BoxGeometry(0.18, 0.18, 0.18); + + for (let z = -2; z <= 2; ++z) + for (let y = -2; y <= 2; ++y) + for (let x = -2; x <= 2; ++x) { + const mesh = new THREE.Mesh(geometry, clipMaterial); + mesh.position.set(x / 5, y / 5, z / 5); + mesh.castShadow = true; + object.add(mesh); + } + + scene.add(object); + + const planeGeometry = new THREE.PlaneGeometry(3, 3, 1, 1), + color = new THREE.Color(); + + volumeVisualization = new THREE.Group(); + volumeVisualization.visible = false; + + for (let i = 0, n = Planes.length; i !== n; ++i) { + const material = new THREE.MeshBasicMaterial({ + color: color.setHSL(i / n, 0.5, 0.5).getHex(), + side: THREE.DoubleSide, + + opacity: 0.2, + transparent: true, + + // clip to the others to show the volume (wildly + // intersecting transparent planes look bad) + clippingPlanes: clipMaterial.clippingPlanes.filter(function (_, j) { + return j !== i; + }), + + // no need to enable shadow clipping - the plane + // visualization does not cast shadows + }); + + const mesh = new THREE.Mesh(planeGeometry, material); + mesh.matrixAutoUpdate = false; + + volumeVisualization.add(mesh); + } + + scene.add(volumeVisualization); + + const ground = new THREE.Mesh( + planeGeometry, + new THREE.MeshPhongMaterial({ + color: 0xa0adaf, + shininess: 10, + }), + ); + ground.rotation.x = -Math.PI / 2; + ground.scale.multiplyScalar(3); + ground.receiveShadow = true; + scene.add(ground); + + // Renderer + + const container = document.body; + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + container.appendChild(renderer.domElement); + // Clipping setup: + globalClippingPlanes = createPlanes(GlobalClippingPlanes.length); + renderer.clippingPlanes = Empty; + renderer.localClippingEnabled = true; + + window.addEventListener('resize', onWindowResize); + + // Stats + + stats = new Stats(); + container.appendChild(stats.dom); + + // Controls + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 1; + controls.maxDistance = 8; + controls.target.set(0, 1, 0); + controls.update(); + + // GUI + + const gui = new GUI(), + folder = gui.addFolder('Local Clipping'), + props = { + get Enabled() { + return renderer.localClippingEnabled; + }, + set Enabled(v) { + renderer.localClippingEnabled = v; + if (!v) volumeVisualization.visible = false; + }, + + get Shadows() { + return clipMaterial.clipShadows; + }, + set Shadows(v) { + clipMaterial.clipShadows = v; + }, + + get Visualize() { + return volumeVisualization.visible; + }, + set Visualize(v) { + if (renderer.localClippingEnabled) volumeVisualization.visible = v; + }, + }; + + folder.add(props, 'Enabled'); + folder.add(props, 'Shadows'); + folder.add(props, 'Visualize').listen(); + + gui.addFolder('Global Clipping').add( + { + get Enabled() { + return renderer.clippingPlanes !== Empty; + }, + set Enabled(v) { + renderer.clippingPlanes = v ? globalClippingPlanes : Empty; + }, + }, + 'Enabled', + ); + + // Start + + startTime = Date.now(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function setObjectWorldMatrix(object, matrix) { + // set the orientation of an object based on a world matrix + + const parent = object.parent; + scene.updateMatrixWorld(); + object.matrix.copy(parent.matrixWorld).invert(); + object.applyMatrix4(matrix); +} + +const transform = new THREE.Matrix4(), + tmpMatrix = new THREE.Matrix4(); + +function animate() { + const currentTime = Date.now(), + time = (currentTime - startTime) / 1000; + + object.position.y = 1; + object.rotation.x = time * 0.5; + object.rotation.y = time * 0.2; + + object.updateMatrix(); + transform.copy(object.matrix); + + const bouncy = Math.cos(time * 0.5) * 0.5 + 0.7; + transform.multiply(tmpMatrix.makeScale(bouncy, bouncy, bouncy)); + + assignTransformedPlanes(clipMaterial.clippingPlanes, Planes, transform); + + const planeMeshes = volumeVisualization.children; + + for (let i = 0, n = planeMeshes.length; i !== n; ++i) { + tmpMatrix.multiplyMatrices(transform, PlaneMatrices[i]); + setObjectWorldMatrix(planeMeshes[i], tmpMatrix); + } + + transform.makeRotationY(time * 0.1); + + assignTransformedPlanes(globalClippingPlanes, GlobalClippingPlanes, transform); + + stats.begin(); + renderer.render(scene, camera); + stats.end(); +} + +init(); diff --git a/examples-testing/examples/webgl_clipping_intersection.ts b/examples-testing/examples/webgl_clipping_intersection.ts new file mode 100644 index 000000000..5f45e45df --- /dev/null +++ b/examples-testing/examples/webgl_clipping_intersection.ts @@ -0,0 +1,137 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer; + +const params = { + clipIntersection: true, + planeConstant: 0, + showHelpers: false, + alphaToCoverage: true, +}; + +const clipPlanes = [ + new THREE.Plane(new THREE.Vector3(1, 0, 0), 0), + new THREE.Plane(new THREE.Vector3(0, -1, 0), 0), + new THREE.Plane(new THREE.Vector3(0, 0, -1), 0), +]; + +init(); +render(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.localClippingEnabled = true; + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 200); + + camera.position.set(-1.5, 2.5, 3.0); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use only if there is no animation loop + controls.minDistance = 1; + controls.maxDistance = 10; + controls.enablePan = false; + + const light = new THREE.HemisphereLight(0xffffff, 0x080808, 4.5); + light.position.set(-1.25, 1, 1.25); + scene.add(light); + + // + + const group = new THREE.Group(); + + for (let i = 1; i <= 30; i += 2) { + const geometry = new THREE.SphereGeometry(i / 30, 48, 24); + + const material = new THREE.MeshPhongMaterial({ + color: new THREE.Color().setHSL(Math.random(), 0.5, 0.5, THREE.SRGBColorSpace), + side: THREE.DoubleSide, + clippingPlanes: clipPlanes, + clipIntersection: params.clipIntersection, + alphaToCoverage: true, + }); + + group.add(new THREE.Mesh(geometry, material)); + } + + scene.add(group); + + // helpers + + const helpers = new THREE.Group(); + helpers.add(new THREE.PlaneHelper(clipPlanes[0], 2, 0xff0000)); + helpers.add(new THREE.PlaneHelper(clipPlanes[1], 2, 0x00ff00)); + helpers.add(new THREE.PlaneHelper(clipPlanes[2], 2, 0x0000ff)); + helpers.visible = false; + scene.add(helpers); + + // gui + + const gui = new GUI(); + + gui.add(params, 'alphaToCoverage').onChange(function (value) { + group.children.forEach(c => { + c.material.alphaToCoverage = Boolean(value); + c.material.needsUpdate = true; + }); + + render(); + }); + + gui.add(params, 'clipIntersection') + .name('clip intersection') + .onChange(function (value) { + const children = group.children; + + for (let i = 0; i < children.length; i++) { + children[i].material.clipIntersection = value; + } + + render(); + }); + + gui.add(params, 'planeConstant', -1, 1) + .step(0.01) + .name('plane constant') + .onChange(function (value) { + for (let j = 0; j < clipPlanes.length; j++) { + clipPlanes[j].constant = value; + } + + render(); + }); + + gui.add(params, 'showHelpers') + .name('show helpers') + .onChange(function (value) { + helpers.visible = value; + + render(); + }); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_clipping_stencil.ts b/examples-testing/examples/webgl_clipping_stencil.ts new file mode 100644 index 000000000..ecb6b42b8 --- /dev/null +++ b/examples-testing/examples/webgl_clipping_stencil.ts @@ -0,0 +1,260 @@ +import * as THREE from 'three'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import Stats from 'three/addons/libs/stats.module.js'; + +let camera, scene, renderer, object, stats; +let planes, planeObjects, planeHelpers; +let clock; + +const params = { + animate: true, + planeX: { + constant: 0, + negated: false, + displayHelper: false, + }, + planeY: { + constant: 0, + negated: false, + displayHelper: false, + }, + planeZ: { + constant: 0, + negated: false, + displayHelper: false, + }, +}; + +init(); + +function createPlaneStencilGroup(geometry, plane, renderOrder) { + const group = new THREE.Group(); + const baseMat = new THREE.MeshBasicMaterial(); + baseMat.depthWrite = false; + baseMat.depthTest = false; + baseMat.colorWrite = false; + baseMat.stencilWrite = true; + baseMat.stencilFunc = THREE.AlwaysStencilFunc; + + // back faces + const mat0 = baseMat.clone(); + mat0.side = THREE.BackSide; + mat0.clippingPlanes = [plane]; + mat0.stencilFail = THREE.IncrementWrapStencilOp; + mat0.stencilZFail = THREE.IncrementWrapStencilOp; + mat0.stencilZPass = THREE.IncrementWrapStencilOp; + + const mesh0 = new THREE.Mesh(geometry, mat0); + mesh0.renderOrder = renderOrder; + group.add(mesh0); + + // front faces + const mat1 = baseMat.clone(); + mat1.side = THREE.FrontSide; + mat1.clippingPlanes = [plane]; + mat1.stencilFail = THREE.DecrementWrapStencilOp; + mat1.stencilZFail = THREE.DecrementWrapStencilOp; + mat1.stencilZPass = THREE.DecrementWrapStencilOp; + + const mesh1 = new THREE.Mesh(geometry, mat1); + mesh1.renderOrder = renderOrder; + + group.add(mesh1); + + return group; +} + +function init() { + clock = new THREE.Clock(); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(36, window.innerWidth / window.innerHeight, 1, 100); + camera.position.set(2, 2, 2); + + scene.add(new THREE.AmbientLight(0xffffff, 1.5)); + + const dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.position.set(5, 10, 7.5); + dirLight.castShadow = true; + dirLight.shadow.camera.right = 2; + dirLight.shadow.camera.left = -2; + dirLight.shadow.camera.top = 2; + dirLight.shadow.camera.bottom = -2; + + dirLight.shadow.mapSize.width = 1024; + dirLight.shadow.mapSize.height = 1024; + scene.add(dirLight); + + planes = [ + new THREE.Plane(new THREE.Vector3(-1, 0, 0), 0), + new THREE.Plane(new THREE.Vector3(0, -1, 0), 0), + new THREE.Plane(new THREE.Vector3(0, 0, -1), 0), + ]; + + planeHelpers = planes.map(p => new THREE.PlaneHelper(p, 2, 0xffffff)); + planeHelpers.forEach(ph => { + ph.visible = false; + scene.add(ph); + }); + + const geometry = new THREE.TorusKnotGeometry(0.4, 0.15, 220, 60); + object = new THREE.Group(); + scene.add(object); + + // Set up clip plane rendering + planeObjects = []; + const planeGeom = new THREE.PlaneGeometry(4, 4); + + for (let i = 0; i < 3; i++) { + const poGroup = new THREE.Group(); + const plane = planes[i]; + const stencilGroup = createPlaneStencilGroup(geometry, plane, i + 1); + + // plane is clipped by the other clipping planes + const planeMat = new THREE.MeshStandardMaterial({ + color: 0xe91e63, + metalness: 0.1, + roughness: 0.75, + clippingPlanes: planes.filter(p => p !== plane), + + stencilWrite: true, + stencilRef: 0, + stencilFunc: THREE.NotEqualStencilFunc, + stencilFail: THREE.ReplaceStencilOp, + stencilZFail: THREE.ReplaceStencilOp, + stencilZPass: THREE.ReplaceStencilOp, + }); + const po = new THREE.Mesh(planeGeom, planeMat); + po.onAfterRender = function (renderer) { + renderer.clearStencil(); + }; + + po.renderOrder = i + 1.1; + + object.add(stencilGroup); + poGroup.add(po); + planeObjects.push(po); + scene.add(poGroup); + } + + const material = new THREE.MeshStandardMaterial({ + color: 0xffc107, + metalness: 0.1, + roughness: 0.75, + clippingPlanes: planes, + clipShadows: true, + shadowSide: THREE.DoubleSide, + }); + + // add the color + const clippedColorFront = new THREE.Mesh(geometry, material); + clippedColorFront.castShadow = true; + clippedColorFront.renderOrder = 6; + object.add(clippedColorFront); + + const ground = new THREE.Mesh( + new THREE.PlaneGeometry(9, 9, 1, 1), + new THREE.ShadowMaterial({ color: 0x000000, opacity: 0.25, side: THREE.DoubleSide }), + ); + + ground.rotation.x = -Math.PI / 2; // rotates X/Y to X/Z + ground.position.y = -1; + ground.receiveShadow = true; + scene.add(ground); + + // Renderer + renderer = new THREE.WebGLRenderer({ antialias: true, stencil: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setClearColor(0x263238); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + renderer.localClippingEnabled = true; + document.body.appendChild(renderer.domElement); + + // Stats + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); + + // Controls + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 2; + controls.maxDistance = 20; + controls.update(); + + // GUI + const gui = new GUI(); + gui.add(params, 'animate'); + + const planeX = gui.addFolder('planeX'); + planeX.add(params.planeX, 'displayHelper').onChange(v => (planeHelpers[0].visible = v)); + planeX + .add(params.planeX, 'constant') + .min(-1) + .max(1) + .onChange(d => (planes[0].constant = d)); + planeX.add(params.planeX, 'negated').onChange(() => { + planes[0].negate(); + params.planeX.constant = planes[0].constant; + }); + planeX.open(); + + const planeY = gui.addFolder('planeY'); + planeY.add(params.planeY, 'displayHelper').onChange(v => (planeHelpers[1].visible = v)); + planeY + .add(params.planeY, 'constant') + .min(-1) + .max(1) + .onChange(d => (planes[1].constant = d)); + planeY.add(params.planeY, 'negated').onChange(() => { + planes[1].negate(); + params.planeY.constant = planes[1].constant; + }); + planeY.open(); + + const planeZ = gui.addFolder('planeZ'); + planeZ.add(params.planeZ, 'displayHelper').onChange(v => (planeHelpers[2].visible = v)); + planeZ + .add(params.planeZ, 'constant') + .min(-1) + .max(1) + .onChange(d => (planes[2].constant = d)); + planeZ.add(params.planeZ, 'negated').onChange(() => { + planes[2].negate(); + params.planeZ.constant = planes[2].constant; + }); + planeZ.open(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const delta = clock.getDelta(); + + if (params.animate) { + object.rotation.x += delta * 0.5; + object.rotation.y += delta * 0.2; + } + + for (let i = 0; i < planeObjects.length; i++) { + const plane = planes[i]; + const po = planeObjects[i]; + plane.coplanarPoint(po.position); + po.lookAt(po.position.x - plane.normal.x, po.position.y - plane.normal.y, po.position.z - plane.normal.z); + } + + stats.begin(); + renderer.render(scene, camera); + stats.end(); +} diff --git a/examples-testing/examples/webgl_custom_attributes.ts b/examples-testing/examples/webgl_custom_attributes.ts new file mode 100644 index 000000000..0dc897748 --- /dev/null +++ b/examples-testing/examples/webgl_custom_attributes.ts @@ -0,0 +1,100 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let renderer, scene, camera, stats; + +let sphere, uniforms; + +let displacement, noise; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 300; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x050505); + + uniforms = { + amplitude: { value: 1.0 }, + color: { value: new THREE.Color(0xff2200) }, + colorTexture: { value: new THREE.TextureLoader().load('textures/water.jpg') }, + }; + + uniforms['colorTexture'].value.wrapS = uniforms['colorTexture'].value.wrapT = THREE.RepeatWrapping; + + const shaderMaterial = new THREE.ShaderMaterial({ + uniforms: uniforms, + vertexShader: document.getElementById('vertexshader').textContent, + fragmentShader: document.getElementById('fragmentshader').textContent, + }); + + const radius = 50, + segments = 128, + rings = 64; + + const geometry = new THREE.SphereGeometry(radius, segments, rings); + + displacement = new Float32Array(geometry.attributes.position.count); + noise = new Float32Array(geometry.attributes.position.count); + + for (let i = 0; i < displacement.length; i++) { + noise[i] = Math.random() * 5; + } + + geometry.setAttribute('displacement', new THREE.BufferAttribute(displacement, 1)); + + sphere = new THREE.Mesh(geometry, shaderMaterial); + scene.add(sphere); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + + const container = document.getElementById('container'); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + const time = Date.now() * 0.01; + + sphere.rotation.y = sphere.rotation.z = 0.01 * time; + + uniforms['amplitude'].value = 2.5 * Math.sin(sphere.rotation.y * 0.125); + uniforms['color'].value.offsetHSL(0.0005, 0, 0); + + for (let i = 0; i < displacement.length; i++) { + displacement[i] = Math.sin(0.1 * i + time); + + noise[i] += 0.5 * (0.5 - Math.random()); + noise[i] = THREE.MathUtils.clamp(noise[i], -5, 5); + + displacement[i] += noise[i]; + } + + sphere.geometry.attributes.displacement.needsUpdate = true; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_custom_attributes_lines.ts b/examples-testing/examples/webgl_custom_attributes_lines.ts new file mode 100644 index 000000000..3e2454e92 --- /dev/null +++ b/examples-testing/examples/webgl_custom_attributes_lines.ts @@ -0,0 +1,121 @@ +import * as THREE from 'three'; + +import { FontLoader } from 'three/addons/loaders/FontLoader.js'; +import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let renderer, scene, camera, stats; + +let line, uniforms; + +const loader = new FontLoader(); +loader.load('fonts/helvetiker_bold.typeface.json', function (font) { + init(font); +}); + +function init(font) { + camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 400; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x050505); + + uniforms = { + amplitude: { value: 5.0 }, + opacity: { value: 0.3 }, + color: { value: new THREE.Color(0xffffff) }, + }; + + const shaderMaterial = new THREE.ShaderMaterial({ + uniforms: uniforms, + vertexShader: document.getElementById('vertexshader').textContent, + fragmentShader: document.getElementById('fragmentshader').textContent, + blending: THREE.AdditiveBlending, + depthTest: false, + transparent: true, + }); + + const geometry = new TextGeometry('three.js', { + font: font, + + size: 50, + depth: 15, + curveSegments: 10, + + bevelThickness: 5, + bevelSize: 1.5, + bevelEnabled: true, + bevelSegments: 10, + }); + + geometry.center(); + + const count = geometry.attributes.position.count; + + const displacement = new THREE.Float32BufferAttribute(count * 3, 3); + geometry.setAttribute('displacement', displacement); + + const customColor = new THREE.Float32BufferAttribute(count * 3, 3); + geometry.setAttribute('customColor', customColor); + + const color = new THREE.Color(0xffffff); + + for (let i = 0, l = customColor.count; i < l; i++) { + color.setHSL(i / l, 0.5, 0.5); + color.toArray(customColor.array, i * customColor.itemSize); + } + + line = new THREE.Line(geometry, shaderMaterial); + line.rotation.x = 0.2; + scene.add(line); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + + const container = document.getElementById('container'); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + const time = Date.now() * 0.001; + + line.rotation.y = 0.25 * time; + + uniforms.amplitude.value = Math.sin(0.5 * time); + uniforms.color.value.offsetHSL(0.0005, 0, 0); + + const attributes = line.geometry.attributes; + const array = attributes.displacement.array; + + for (let i = 0, l = array.length; i < l; i += 3) { + array[i] += 0.3 * (0.5 - Math.random()); + array[i + 1] += 0.3 * (0.5 - Math.random()); + array[i + 2] += 0.3 * (0.5 - Math.random()); + } + + attributes.displacement.needsUpdate = true; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_custom_attributes_points.ts b/examples-testing/examples/webgl_custom_attributes_points.ts new file mode 100644 index 000000000..ae112980a --- /dev/null +++ b/examples-testing/examples/webgl_custom_attributes_points.ts @@ -0,0 +1,117 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let renderer, scene, camera, stats; + +let sphere; + +const WIDTH = window.innerWidth; +const HEIGHT = window.innerHeight; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(40, WIDTH / HEIGHT, 1, 10000); + camera.position.z = 300; + + scene = new THREE.Scene(); + + const amount = 100000; + const radius = 200; + + const positions = new Float32Array(amount * 3); + const colors = new Float32Array(amount * 3); + const sizes = new Float32Array(amount); + + const vertex = new THREE.Vector3(); + const color = new THREE.Color(0xffffff); + + for (let i = 0; i < amount; i++) { + vertex.x = (Math.random() * 2 - 1) * radius; + vertex.y = (Math.random() * 2 - 1) * radius; + vertex.z = (Math.random() * 2 - 1) * radius; + vertex.toArray(positions, i * 3); + + if (vertex.x < 0) { + color.setHSL(0.5 + 0.1 * (i / amount), 0.7, 0.5); + } else { + color.setHSL(0.0 + 0.1 * (i / amount), 0.9, 0.5); + } + + color.toArray(colors, i * 3); + + sizes[i] = 10; + } + + const geometry = new THREE.BufferGeometry(); + geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); + geometry.setAttribute('customColor', new THREE.BufferAttribute(colors, 3)); + geometry.setAttribute('size', new THREE.BufferAttribute(sizes, 1)); + + // + + const material = new THREE.ShaderMaterial({ + uniforms: { + color: { value: new THREE.Color(0xffffff) }, + pointTexture: { value: new THREE.TextureLoader().load('textures/sprites/spark1.png') }, + }, + vertexShader: document.getElementById('vertexshader').textContent, + fragmentShader: document.getElementById('fragmentshader').textContent, + + blending: THREE.AdditiveBlending, + depthTest: false, + transparent: true, + }); + + // + + sphere = new THREE.Points(geometry, material); + scene.add(sphere); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(WIDTH, HEIGHT); + renderer.setAnimationLoop(animate); + + const container = document.getElementById('container'); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + const time = Date.now() * 0.005; + + sphere.rotation.z = 0.01 * time; + + const geometry = sphere.geometry; + const attributes = geometry.attributes; + + for (let i = 0; i < attributes.size.array.length; i++) { + attributes.size.array[i] = 14 + 13 * Math.sin(0.1 * i + time); + } + + attributes.size.needsUpdate = true; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_custom_attributes_points2.ts b/examples-testing/examples/webgl_custom_attributes_points2.ts new file mode 100644 index 000000000..edd158fa1 --- /dev/null +++ b/examples-testing/examples/webgl_custom_attributes_points2.ts @@ -0,0 +1,193 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; + +let renderer, scene, camera, stats; +let sphere, length1; + +const WIDTH = window.innerWidth; +const HEIGHT = window.innerHeight; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(45, WIDTH / HEIGHT, 1, 10000); + camera.position.z = 300; + + scene = new THREE.Scene(); + + const radius = 100, + segments = 68, + rings = 38; + + let sphereGeometry = new THREE.SphereGeometry(radius, segments, rings); + let boxGeometry = new THREE.BoxGeometry(0.8 * radius, 0.8 * radius, 0.8 * radius, 10, 10, 10); + + // if normal and uv attributes are not removed, mergeVertices() can't consolidate identical vertices with different normal/uv data + + sphereGeometry.deleteAttribute('normal'); + sphereGeometry.deleteAttribute('uv'); + + boxGeometry.deleteAttribute('normal'); + boxGeometry.deleteAttribute('uv'); + + sphereGeometry = BufferGeometryUtils.mergeVertices(sphereGeometry); + boxGeometry = BufferGeometryUtils.mergeVertices(boxGeometry); + + const combinedGeometry = BufferGeometryUtils.mergeGeometries([sphereGeometry, boxGeometry]); + const positionAttribute = combinedGeometry.getAttribute('position'); + + const colors = []; + const sizes = []; + + const color = new THREE.Color(); + const vertex = new THREE.Vector3(); + + length1 = sphereGeometry.getAttribute('position').count; + + for (let i = 0, l = positionAttribute.count; i < l; i++) { + vertex.fromBufferAttribute(positionAttribute, i); + + if (i < length1) { + color.setHSL(0.01 + 0.1 * (i / length1), 0.99, (vertex.y + radius) / (4 * radius)); + } else { + color.setHSL(0.6, 0.75, 0.25 + vertex.y / (2 * radius)); + } + + color.toArray(colors, i * 3); + + sizes[i] = i < length1 ? 10 : 40; + } + + const geometry = new THREE.BufferGeometry(); + geometry.setAttribute('position', positionAttribute); + geometry.setAttribute('size', new THREE.Float32BufferAttribute(sizes, 1)); + geometry.setAttribute('ca', new THREE.Float32BufferAttribute(colors, 3)); + + // + + const texture = new THREE.TextureLoader().load('textures/sprites/disc.png'); + texture.wrapS = THREE.RepeatWrapping; + texture.wrapT = THREE.RepeatWrapping; + + const material = new THREE.ShaderMaterial({ + uniforms: { + color: { value: new THREE.Color(0xffffff) }, + pointTexture: { value: texture }, + }, + vertexShader: document.getElementById('vertexshader').textContent, + fragmentShader: document.getElementById('fragmentshader').textContent, + transparent: true, + }); + + // + + sphere = new THREE.Points(geometry, material); + scene.add(sphere); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(WIDTH, HEIGHT); + renderer.setAnimationLoop(animate); + + const container = document.getElementById('container'); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function sortPoints() { + const vector = new THREE.Vector3(); + + // Model View Projection matrix + + const matrix = new THREE.Matrix4(); + matrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse); + matrix.multiply(sphere.matrixWorld); + + // + + const geometry = sphere.geometry; + + let index = geometry.getIndex(); + const positions = geometry.getAttribute('position').array; + const length = positions.length / 3; + + if (index === null) { + const array = new Uint16Array(length); + + for (let i = 0; i < length; i++) { + array[i] = i; + } + + index = new THREE.BufferAttribute(array, 1); + + geometry.setIndex(index); + } + + const sortArray = []; + + for (let i = 0; i < length; i++) { + vector.fromArray(positions, i * 3); + vector.applyMatrix4(matrix); + + sortArray.push([vector.z, i]); + } + + function numericalSort(a, b) { + return b[0] - a[0]; + } + + sortArray.sort(numericalSort); + + const indices = index.array; + + for (let i = 0; i < length; i++) { + indices[i] = sortArray[i][1]; + } + + geometry.index.needsUpdate = true; +} + +function animate() { + render(); + stats.update(); +} + +function render() { + const time = Date.now() * 0.005; + + sphere.rotation.y = 0.02 * time; + sphere.rotation.z = 0.02 * time; + + const geometry = sphere.geometry; + const attributes = geometry.attributes; + + for (let i = 0; i < attributes.size.array.length; i++) { + if (i < length1) { + attributes.size.array[i] = 16 + 12 * Math.sin(0.1 * i + time); + } + } + + attributes.size.needsUpdate = true; + + sortPoints(); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_custom_attributes_points3.ts b/examples-testing/examples/webgl_custom_attributes_points3.ts new file mode 100644 index 000000000..1b46a805e --- /dev/null +++ b/examples-testing/examples/webgl_custom_attributes_points3.ts @@ -0,0 +1,200 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; + +let renderer, scene, camera, stats; + +let object; + +let vertices1; + +const WIDTH = window.innerWidth; +const HEIGHT = window.innerHeight; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(40, WIDTH / HEIGHT, 1, 1000); + camera.position.z = 500; + + scene = new THREE.Scene(); + + let radius = 100; + const inner = 0.6 * radius; + const vertex = new THREE.Vector3(); + const vertices = []; + + for (let i = 0; i < 100000; i++) { + vertex.x = Math.random() * 2 - 1; + vertex.y = Math.random() * 2 - 1; + vertex.z = Math.random() * 2 - 1; + vertex.multiplyScalar(radius); + + if ( + vertex.x > inner || + vertex.x < -inner || + vertex.y > inner || + vertex.y < -inner || + vertex.z > inner || + vertex.z < -inner + ) + vertices.push(vertex.x, vertex.y, vertex.z); + } + + vertices1 = vertices.length / 3; + + radius = 200; + + let boxGeometry1 = new THREE.BoxGeometry(radius, 0.1 * radius, 0.1 * radius, 50, 5, 5); + + // if normal and uv attributes are not removed, mergeVertices() can't consolidate indentical vertices with different normal/uv data + + boxGeometry1.deleteAttribute('normal'); + boxGeometry1.deleteAttribute('uv'); + + boxGeometry1 = BufferGeometryUtils.mergeVertices(boxGeometry1); + + const matrix = new THREE.Matrix4(); + const position = new THREE.Vector3(); + const rotation = new THREE.Euler(); + const quaternion = new THREE.Quaternion(); + const scale = new THREE.Vector3(1, 1, 1); + + function addGeo(geo, x, y, z, ry) { + position.set(x, y, z); + rotation.set(0, ry, 0); + + matrix.compose(position, quaternion.setFromEuler(rotation), scale); + + const positionAttribute = geo.getAttribute('position'); + + for (let i = 0, l = positionAttribute.count; i < l; i++) { + vertex.fromBufferAttribute(positionAttribute, i); + vertex.applyMatrix4(matrix); + vertices.push(vertex.x, vertex.y, vertex.z); + } + } + + // side 1 + + addGeo(boxGeometry1, 0, 110, 110, 0); + addGeo(boxGeometry1, 0, 110, -110, 0); + addGeo(boxGeometry1, 0, -110, 110, 0); + addGeo(boxGeometry1, 0, -110, -110, 0); + + // side 2 + + addGeo(boxGeometry1, 110, 110, 0, Math.PI / 2); + addGeo(boxGeometry1, 110, -110, 0, Math.PI / 2); + addGeo(boxGeometry1, -110, 110, 0, Math.PI / 2); + addGeo(boxGeometry1, -110, -110, 0, Math.PI / 2); + + // corner edges + + let boxGeometry2 = new THREE.BoxGeometry(0.1 * radius, radius * 1.2, 0.1 * radius, 5, 60, 5); + + boxGeometry2.deleteAttribute('normal'); + boxGeometry2.deleteAttribute('uv'); + + boxGeometry2 = BufferGeometryUtils.mergeVertices(boxGeometry2); + + addGeo(boxGeometry2, 110, 0, 110, 0); + addGeo(boxGeometry2, 110, 0, -110, 0); + addGeo(boxGeometry2, -110, 0, 110, 0); + addGeo(boxGeometry2, -110, 0, -110, 0); + + const positionAttribute = new THREE.Float32BufferAttribute(vertices, 3); + + const colors = []; + const sizes = []; + + const color = new THREE.Color(); + + for (let i = 0; i < positionAttribute.count; i++) { + if (i < vertices1) { + color.setHSL(0.5 + 0.2 * (i / vertices1), 1, 0.5); + } else { + color.setHSL(0.1, 1, 0.5); + } + + color.toArray(colors, i * 3); + + sizes[i] = i < vertices1 ? 10 : 40; + } + + const geometry = new THREE.BufferGeometry(); + geometry.setAttribute('position', positionAttribute); + geometry.setAttribute('ca', new THREE.Float32BufferAttribute(colors, 3)); + geometry.setAttribute('size', new THREE.Float32BufferAttribute(sizes, 1)); + + // + + const texture = new THREE.TextureLoader().load('textures/sprites/ball.png'); + texture.wrapS = THREE.RepeatWrapping; + texture.wrapT = THREE.RepeatWrapping; + + const material = new THREE.ShaderMaterial({ + uniforms: { + amplitude: { value: 1.0 }, + color: { value: new THREE.Color(0xffffff) }, + pointTexture: { value: texture }, + }, + vertexShader: document.getElementById('vertexshader').textContent, + fragmentShader: document.getElementById('fragmentshader').textContent, + }); + + // + + object = new THREE.Points(geometry, material); + scene.add(object); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(WIDTH, HEIGHT); + renderer.setAnimationLoop(animate); + + const container = document.getElementById('container'); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + const time = Date.now() * 0.01; + + object.rotation.y = object.rotation.z = 0.02 * time; + + const geometry = object.geometry; + const attributes = geometry.attributes; + + for (let i = 0; i < attributes.size.array.length; i++) { + if (i < vertices1) { + attributes.size.array[i] = Math.max(0, 26 + 32 * Math.sin(0.1 * i + 0.6 * time)); + } + } + + attributes.size.needsUpdate = true; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_decals.ts b/examples-testing/examples/webgl_decals.ts new file mode 100644 index 000000000..23cdb4da8 --- /dev/null +++ b/examples-testing/examples/webgl_decals.ts @@ -0,0 +1,237 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { DecalGeometry } from 'three/addons/geometries/DecalGeometry.js'; + +const container = document.getElementById('container'); + +let renderer, scene, camera, stats; +let mesh; +let raycaster; +let line; + +const intersection = { + intersects: false, + point: new THREE.Vector3(), + normal: new THREE.Vector3(), +}; +const mouse = new THREE.Vector2(); +const intersects = []; + +const textureLoader = new THREE.TextureLoader(); +const decalDiffuse = textureLoader.load('textures/decal/decal-diffuse.png'); +decalDiffuse.colorSpace = THREE.SRGBColorSpace; +const decalNormal = textureLoader.load('textures/decal/decal-normal.jpg'); + +const decalMaterial = new THREE.MeshPhongMaterial({ + specular: 0x444444, + map: decalDiffuse, + normalMap: decalNormal, + normalScale: new THREE.Vector2(1, 1), + shininess: 30, + transparent: true, + depthTest: true, + depthWrite: false, + polygonOffset: true, + polygonOffsetFactor: -4, + wireframe: false, +}); + +const decals = []; +let mouseHelper; +const position = new THREE.Vector3(); +const orientation = new THREE.Euler(); +const size = new THREE.Vector3(10, 10, 10); + +const params = { + minScale: 10, + maxScale: 20, + rotate: true, + clear: function () { + removeDecals(); + }, +}; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 120; + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 50; + controls.maxDistance = 200; + + scene.add(new THREE.AmbientLight(0x666666)); + + const dirLight1 = new THREE.DirectionalLight(0xffddcc, 3); + dirLight1.position.set(1, 0.75, 0.5); + scene.add(dirLight1); + + const dirLight2 = new THREE.DirectionalLight(0xccccff, 3); + dirLight2.position.set(-1, 0.75, -0.5); + scene.add(dirLight2); + + const geometry = new THREE.BufferGeometry(); + geometry.setFromPoints([new THREE.Vector3(), new THREE.Vector3()]); + + line = new THREE.Line(geometry, new THREE.LineBasicMaterial()); + scene.add(line); + + loadLeePerrySmith(); + + raycaster = new THREE.Raycaster(); + + mouseHelper = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 10), new THREE.MeshNormalMaterial()); + mouseHelper.visible = false; + scene.add(mouseHelper); + + window.addEventListener('resize', onWindowResize); + + let moved = false; + + controls.addEventListener('change', function () { + moved = true; + }); + + window.addEventListener('pointerdown', function () { + moved = false; + }); + + window.addEventListener('pointerup', function (event) { + if (moved === false) { + checkIntersection(event.clientX, event.clientY); + + if (intersection.intersects) shoot(); + } + }); + + window.addEventListener('pointermove', onPointerMove); + + function onPointerMove(event) { + if (event.isPrimary) { + checkIntersection(event.clientX, event.clientY); + } + } + + function checkIntersection(x, y) { + if (mesh === undefined) return; + + mouse.x = (x / window.innerWidth) * 2 - 1; + mouse.y = -(y / window.innerHeight) * 2 + 1; + + raycaster.setFromCamera(mouse, camera); + raycaster.intersectObject(mesh, false, intersects); + + if (intersects.length > 0) { + const p = intersects[0].point; + mouseHelper.position.copy(p); + intersection.point.copy(p); + + const n = intersects[0].face.normal.clone(); + n.transformDirection(mesh.matrixWorld); + n.multiplyScalar(10); + n.add(intersects[0].point); + + intersection.normal.copy(intersects[0].face.normal); + mouseHelper.lookAt(n); + + const positions = line.geometry.attributes.position; + positions.setXYZ(0, p.x, p.y, p.z); + positions.setXYZ(1, n.x, n.y, n.z); + positions.needsUpdate = true; + + intersection.intersects = true; + + intersects.length = 0; + } else { + intersection.intersects = false; + } + } + + const gui = new GUI(); + + gui.add(params, 'minScale', 1, 30); + gui.add(params, 'maxScale', 1, 30); + gui.add(params, 'rotate'); + gui.add(params, 'clear'); + gui.open(); +} + +function loadLeePerrySmith() { + const map = textureLoader.load('models/gltf/LeePerrySmith/Map-COL.jpg'); + map.colorSpace = THREE.SRGBColorSpace; + const specularMap = textureLoader.load('models/gltf/LeePerrySmith/Map-SPEC.jpg'); + const normalMap = textureLoader.load('models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg'); + + const loader = new GLTFLoader(); + + loader.load('models/gltf/LeePerrySmith/LeePerrySmith.glb', function (gltf) { + mesh = gltf.scene.children[0]; + mesh.material = new THREE.MeshPhongMaterial({ + specular: 0x111111, + map: map, + specularMap: specularMap, + normalMap: normalMap, + shininess: 25, + }); + + scene.add(mesh); + mesh.scale.set(10, 10, 10); + }); +} + +function shoot() { + position.copy(intersection.point); + orientation.copy(mouseHelper.rotation); + + if (params.rotate) orientation.z = Math.random() * 2 * Math.PI; + + const scale = params.minScale + Math.random() * (params.maxScale - params.minScale); + size.set(scale, scale, scale); + + const material = decalMaterial.clone(); + material.color.setHex(Math.random() * 0xffffff); + + const m = new THREE.Mesh(new DecalGeometry(mesh, position, orientation, size), material); + m.renderOrder = decals.length; // give decals a fixed render order + + decals.push(m); + scene.add(m); +} + +function removeDecals() { + decals.forEach(function (d) { + scene.remove(d); + }); + + decals.length = 0; +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_effects_anaglyph.ts b/examples-testing/examples/webgl_effects_anaglyph.ts new file mode 100644 index 000000000..8415973df --- /dev/null +++ b/examples-testing/examples/webgl_effects_anaglyph.ts @@ -0,0 +1,114 @@ +import * as THREE from 'three'; + +import { AnaglyphEffect } from 'three/addons/effects/AnaglyphEffect.js'; + +let container, camera, scene, renderer, effect; + +const spheres = []; + +let mouseX = 0; +let mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +document.addEventListener('mousemove', onDocumentMouseMove); + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 100); + camera.position.z = 3; + + const path = 'textures/cube/pisa/'; + const format = '.png'; + const urls = [ + path + 'px' + format, + path + 'nx' + format, + path + 'py' + format, + path + 'ny' + format, + path + 'pz' + format, + path + 'nz' + format, + ]; + + const textureCube = new THREE.CubeTextureLoader().load(urls); + + scene = new THREE.Scene(); + scene.background = textureCube; + + const geometry = new THREE.SphereGeometry(0.1, 32, 16); + const material = new THREE.MeshBasicMaterial({ color: 0xffffff, envMap: textureCube }); + + for (let i = 0; i < 500; i++) { + const mesh = new THREE.Mesh(geometry, material); + + mesh.position.x = Math.random() * 10 - 5; + mesh.position.y = Math.random() * 10 - 5; + mesh.position.z = Math.random() * 10 - 5; + + mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1; + + scene.add(mesh); + + spheres.push(mesh); + } + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + const width = window.innerWidth || 2; + const height = window.innerHeight || 2; + + effect = new AnaglyphEffect(renderer); + effect.setSize(width, height); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + effect.setSize(window.innerWidth, window.innerHeight); +} + +function onDocumentMouseMove(event) { + mouseX = (event.clientX - windowHalfX) / 100; + mouseY = (event.clientY - windowHalfY) / 100; +} + +// + +function animate() { + render(); +} + +function render() { + const timer = 0.0001 * Date.now(); + + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-mouseY - camera.position.y) * 0.05; + + camera.lookAt(scene.position); + + for (let i = 0, il = spheres.length; i < il; i++) { + const sphere = spheres[i]; + + sphere.position.x = 5 * Math.cos(timer + i); + sphere.position.y = 5 * Math.sin(timer + i * 1.1); + } + + effect.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_effects_ascii.ts b/examples-testing/examples/webgl_effects_ascii.ts new file mode 100644 index 000000000..a412bb79e --- /dev/null +++ b/examples-testing/examples/webgl_effects_ascii.ts @@ -0,0 +1,81 @@ +import * as THREE from 'three'; + +import { AsciiEffect } from 'three/addons/effects/AsciiEffect.js'; +import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; + +let camera, controls, scene, renderer, effect; + +let sphere, plane; + +const start = Date.now(); + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.y = 150; + camera.position.z = 500; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0, 0, 0); + + const pointLight1 = new THREE.PointLight(0xffffff, 3, 0, 0); + pointLight1.position.set(500, 500, 500); + scene.add(pointLight1); + + const pointLight2 = new THREE.PointLight(0xffffff, 1, 0, 0); + pointLight2.position.set(-500, -500, -500); + scene.add(pointLight2); + + sphere = new THREE.Mesh(new THREE.SphereGeometry(200, 20, 10), new THREE.MeshPhongMaterial({ flatShading: true })); + scene.add(sphere); + + // Plane + + plane = new THREE.Mesh(new THREE.PlaneGeometry(400, 400), new THREE.MeshBasicMaterial({ color: 0xe0e0e0 })); + plane.position.y = -200; + plane.rotation.x = -Math.PI / 2; + scene.add(plane); + + renderer = new THREE.WebGLRenderer(); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + + effect = new AsciiEffect(renderer, ' .:-+*=%@#', { invert: true }); + effect.setSize(window.innerWidth, window.innerHeight); + effect.domElement.style.color = 'white'; + effect.domElement.style.backgroundColor = 'black'; + + // Special case: append effect.domElement, instead of renderer.domElement. + // AsciiEffect creates a custom domElement (a div container) where the ASCII elements are placed. + + document.body.appendChild(effect.domElement); + + controls = new TrackballControls(camera, effect.domElement); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + effect.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const timer = Date.now() - start; + + sphere.position.y = Math.abs(Math.sin(timer * 0.002)) * 150; + sphere.rotation.x = timer * 0.0003; + sphere.rotation.z = timer * 0.0002; + + controls.update(); + + effect.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_effects_parallaxbarrier.ts b/examples-testing/examples/webgl_effects_parallaxbarrier.ts new file mode 100644 index 000000000..90c867973 --- /dev/null +++ b/examples-testing/examples/webgl_effects_parallaxbarrier.ts @@ -0,0 +1,110 @@ +import * as THREE from 'three'; + +import { ParallaxBarrierEffect } from 'three/addons/effects/ParallaxBarrierEffect.js'; + +let container, camera, scene, renderer, effect; + +const spheres = []; + +let mouseX = 0; +let mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +document.addEventListener('mousemove', onDocumentMouseMove); + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 100); + camera.position.z = 3; + + const path = 'textures/cube/pisa/'; + const format = '.png'; + const urls = [ + path + 'px' + format, + path + 'nx' + format, + path + 'py' + format, + path + 'ny' + format, + path + 'pz' + format, + path + 'nz' + format, + ]; + + const textureCube = new THREE.CubeTextureLoader().load(urls); + + scene = new THREE.Scene(); + scene.background = textureCube; + + const geometry = new THREE.SphereGeometry(0.1, 32, 16); + const material = new THREE.MeshBasicMaterial({ color: 0xffffff, envMap: textureCube }); + + for (let i = 0; i < 500; i++) { + const mesh = new THREE.Mesh(geometry, material); + + mesh.position.x = Math.random() * 10 - 5; + mesh.position.y = Math.random() * 10 - 5; + mesh.position.z = Math.random() * 10 - 5; + + mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1; + + scene.add(mesh); + + spheres.push(mesh); + } + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + const width = window.innerWidth || 2; + const height = window.innerHeight || 2; + + effect = new ParallaxBarrierEffect(renderer); + effect.setSize(width, height); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + effect.setSize(window.innerWidth, window.innerHeight); +} + +function onDocumentMouseMove(event) { + mouseX = (event.clientX - windowHalfX) / 100; + mouseY = (event.clientY - windowHalfY) / 100; +} + +// + +function animate() { + const timer = 0.0001 * Date.now(); + + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-mouseY - camera.position.y) * 0.05; + + camera.lookAt(scene.position); + + for (let i = 0, il = spheres.length; i < il; i++) { + const sphere = spheres[i]; + + sphere.position.x = 5 * Math.cos(timer + i); + sphere.position.y = 5 * Math.sin(timer + i * 1.1); + } + + effect.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_effects_peppersghost.ts b/examples-testing/examples/webgl_effects_peppersghost.ts new file mode 100644 index 000000000..41dfb4b65 --- /dev/null +++ b/examples-testing/examples/webgl_effects_peppersghost.ts @@ -0,0 +1,85 @@ +import * as THREE from 'three'; + +import { PeppersGhostEffect } from 'three/addons/effects/PeppersGhostEffect.js'; + +let container; + +let camera, scene, renderer, effect; +let group; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 100000); + + scene = new THREE.Scene(); + + group = new THREE.Group(); + scene.add(group); + + // Cube + + const geometry = new THREE.BoxGeometry().toNonIndexed(); // ensure unique vertices for each triangle + + const position = geometry.attributes.position; + const colors = []; + const color = new THREE.Color(); + + // generate for each side of the cube a different color + + for (let i = 0; i < position.count; i += 6) { + color.setHex(Math.random() * 0xffffff); + + // first face + + colors.push(color.r, color.g, color.b); + colors.push(color.r, color.g, color.b); + colors.push(color.r, color.g, color.b); + + // second face + + colors.push(color.r, color.g, color.b); + colors.push(color.r, color.g, color.b); + colors.push(color.r, color.g, color.b); + } + + geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); + + const material = new THREE.MeshBasicMaterial({ vertexColors: true }); + + for (let i = 0; i < 10; i++) { + const cube = new THREE.Mesh(geometry, material); + cube.position.x = Math.random() * 2 - 1; + cube.position.y = Math.random() * 2 - 1; + cube.position.z = Math.random() * 2 - 1; + cube.scale.multiplyScalar(Math.random() + 0.5); + group.add(cube); + } + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + effect = new PeppersGhostEffect(renderer); + effect.setSize(window.innerWidth, window.innerHeight); + effect.cameraDistance = 5; + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + effect.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + group.rotation.y += 0.01; + + effect.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_effects_stereo.ts b/examples-testing/examples/webgl_effects_stereo.ts new file mode 100644 index 000000000..dd2f61f91 --- /dev/null +++ b/examples-testing/examples/webgl_effects_stereo.ts @@ -0,0 +1,98 @@ +import * as THREE from 'three'; + +import { StereoEffect } from 'three/addons/effects/StereoEffect.js'; + +let container, camera, scene, renderer, effect; + +const spheres = []; + +let mouseX = 0, + mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +document.addEventListener('mousemove', onDocumentMouseMove); + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 100000); + camera.position.z = 3200; + + scene = new THREE.Scene(); + scene.background = new THREE.CubeTextureLoader() + .setPath('textures/cube/Park3Med/') + .load(['px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg']); + + const geometry = new THREE.SphereGeometry(100, 32, 16); + + const textureCube = new THREE.CubeTextureLoader() + .setPath('textures/cube/Park3Med/') + .load(['px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg']); + textureCube.mapping = THREE.CubeRefractionMapping; + + const material = new THREE.MeshBasicMaterial({ color: 0xffffff, envMap: textureCube, refractionRatio: 0.95 }); + + for (let i = 0; i < 500; i++) { + const mesh = new THREE.Mesh(geometry, material); + mesh.position.x = Math.random() * 10000 - 5000; + mesh.position.y = Math.random() * 10000 - 5000; + mesh.position.z = Math.random() * 10000 - 5000; + mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1; + scene.add(mesh); + + spheres.push(mesh); + } + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + effect = new StereoEffect(renderer); + effect.setSize(window.innerWidth, window.innerHeight); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + effect.setSize(window.innerWidth, window.innerHeight); +} + +function onDocumentMouseMove(event) { + mouseX = (event.clientX - windowHalfX) * 10; + mouseY = (event.clientY - windowHalfY) * 10; +} + +// + +function animate() { + const timer = 0.0001 * Date.now(); + + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-mouseY - camera.position.y) * 0.05; + camera.lookAt(scene.position); + + for (let i = 0, il = spheres.length; i < il; i++) { + const sphere = spheres[i]; + + sphere.position.x = 5000 * Math.cos(timer + i); + sphere.position.y = 5000 * Math.sin(timer + i * 1.1); + } + + effect.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_framebuffer_texture.ts b/examples-testing/examples/webgl_framebuffer_texture.ts new file mode 100644 index 000000000..df4acc9d6 --- /dev/null +++ b/examples-testing/examples/webgl_framebuffer_texture.ts @@ -0,0 +1,151 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js'; + +let camera, scene, renderer; +let line, sprite, texture; + +let cameraOrtho, sceneOrtho; + +let offset = 0; + +const dpr = window.devicePixelRatio; + +const textureSize = 128 * dpr; +const vector = new THREE.Vector2(); +const color = new THREE.Color(); + +init(); + +function init() { + // + + const width = window.innerWidth; + const height = window.innerHeight; + + camera = new THREE.PerspectiveCamera(70, width / height, 1, 1000); + camera.position.z = 20; + + cameraOrtho = new THREE.OrthographicCamera(-width / 2, width / 2, height / 2, -height / 2, 1, 10); + cameraOrtho.position.z = 10; + + scene = new THREE.Scene(); + sceneOrtho = new THREE.Scene(); + + // + + const points = GeometryUtils.gosper(8); + + const geometry = new THREE.BufferGeometry(); + const positionAttribute = new THREE.Float32BufferAttribute(points, 3); + geometry.setAttribute('position', positionAttribute); + geometry.center(); + + const colorAttribute = new THREE.BufferAttribute(new Float32Array(positionAttribute.array.length), 3); + colorAttribute.setUsage(THREE.DynamicDrawUsage); + geometry.setAttribute('color', colorAttribute); + + const material = new THREE.LineBasicMaterial({ vertexColors: true }); + + line = new THREE.Line(geometry, material); + line.scale.setScalar(0.05); + scene.add(line); + + // + + texture = new THREE.FramebufferTexture(textureSize, textureSize); + + // + + const spriteMaterial = new THREE.SpriteMaterial({ map: texture }); + sprite = new THREE.Sprite(spriteMaterial); + sprite.scale.set(textureSize, textureSize, 1); + sceneOrtho.add(sprite); + + updateSpritePosition(); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.autoClear = false; + document.body.appendChild(renderer.domElement); + + // + + const selection = document.getElementById('selection'); + const controls = new OrbitControls(camera, selection); + controls.enablePan = false; + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + cameraOrtho.left = -width / 2; + cameraOrtho.right = width / 2; + cameraOrtho.top = height / 2; + cameraOrtho.bottom = -height / 2; + cameraOrtho.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + updateSpritePosition(); +} + +function updateSpritePosition() { + const halfWidth = window.innerWidth / 2; + const halfHeight = window.innerHeight / 2; + + const halfImageWidth = textureSize / 2; + const halfImageHeight = textureSize / 2; + + sprite.position.set(-halfWidth + halfImageWidth, halfHeight - halfImageHeight, 1); +} + +function animate() { + const colorAttribute = line.geometry.getAttribute('color'); + updateColors(colorAttribute); + + // scene rendering + + renderer.clear(); + renderer.render(scene, camera); + + // calculate start position for copying data + + vector.x = (window.innerWidth * dpr) / 2 - textureSize / 2; + vector.y = (window.innerHeight * dpr) / 2 - textureSize / 2; + + renderer.copyFramebufferToTexture(texture, vector); + + renderer.clearDepth(); + renderer.render(sceneOrtho, cameraOrtho); +} + +function updateColors(colorAttribute) { + const l = colorAttribute.count; + + for (let i = 0; i < l; i++) { + const h = ((offset + i) % l) / l; + + color.setHSL(h, 1, 0.5); + colorAttribute.setX(i, color.r); + colorAttribute.setY(i, color.g); + colorAttribute.setZ(i, color.b); + } + + colorAttribute.needsUpdate = true; + + offset -= 25; +} diff --git a/examples-testing/examples/webgl_furnace_test.ts b/examples-testing/examples/webgl_furnace_test.ts new file mode 100644 index 000000000..a81954176 --- /dev/null +++ b/examples-testing/examples/webgl_furnace_test.ts @@ -0,0 +1,96 @@ +import * as THREE from 'three'; + +let scene, camera, renderer, radianceMap; + +const COLOR = 0xcccccc; + +function init() { + const width = window.innerWidth; + const height = window.innerHeight; + const aspect = width / height; + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setSize(width, height); + renderer.setPixelRatio(window.devicePixelRatio); + document.body.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); + + document.body.addEventListener('mouseover', function () { + scene.traverse(function (child) { + if (child.isMesh) child.material.color.setHex(0xffffff); + }); + + render(); + }); + + document.body.addEventListener('mouseout', function () { + scene.traverse(function (child) { + if (child.isMesh) child.material.color.setHex(0xccccff); // tinted for visibility + }); + + render(); + }); + + // scene + + scene = new THREE.Scene(); + + // camera + camera = new THREE.PerspectiveCamera(40, aspect, 1, 30); + camera.position.set(0, 0, 18); +} + +function createObjects() { + const geometry = new THREE.SphereGeometry(0.4, 32, 16); + + for (let x = 0; x <= 10; x++) { + for (let y = 0; y <= 10; y++) { + const material = new THREE.MeshPhysicalMaterial({ + roughness: x / 10, + metalness: y / 10, + color: 0xffffff, + envMap: radianceMap, + envMapIntensity: 1, + transmission: 0, + ior: 1.5, + }); + + const mesh = new THREE.Mesh(geometry, material); + mesh.position.x = x - 5; + mesh.position.y = 5 - y; + scene.add(mesh); + } + } +} + +function createEnvironment() { + const envScene = new THREE.Scene(); + envScene.background = new THREE.Color(COLOR); + + const pmremGenerator = new THREE.PMREMGenerator(renderer); + radianceMap = pmremGenerator.fromScene(envScene).texture; + pmremGenerator.dispose(); + + scene.background = envScene.background; +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); + + render(); +} + +function render() { + renderer.render(scene, camera); +} + +Promise.resolve().then(init).then(createEnvironment).then(createObjects).then(render); diff --git a/examples-testing/examples/webgl_geometries.ts b/examples-testing/examples/webgl_geometries.ts new file mode 100644 index 000000000..2b2d02613 --- /dev/null +++ b/examples-testing/examples/webgl_geometries.ts @@ -0,0 +1,137 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let camera, scene, renderer, stats; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000); + camera.position.y = 400; + + scene = new THREE.Scene(); + + let object; + + const ambientLight = new THREE.AmbientLight(0xcccccc, 1.5); + scene.add(ambientLight); + + const pointLight = new THREE.PointLight(0xffffff, 2.5, 0, 0); + camera.add(pointLight); + scene.add(camera); + + const map = new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg'); + map.wrapS = map.wrapT = THREE.RepeatWrapping; + map.anisotropy = 16; + map.colorSpace = THREE.SRGBColorSpace; + + const material = new THREE.MeshPhongMaterial({ map: map, side: THREE.DoubleSide }); + + // + + object = new THREE.Mesh(new THREE.SphereGeometry(75, 20, 10), material); + object.position.set(-300, 0, 200); + scene.add(object); + + object = new THREE.Mesh(new THREE.IcosahedronGeometry(75, 1), material); + object.position.set(-100, 0, 200); + scene.add(object); + + object = new THREE.Mesh(new THREE.OctahedronGeometry(75, 2), material); + object.position.set(100, 0, 200); + scene.add(object); + + object = new THREE.Mesh(new THREE.TetrahedronGeometry(75, 0), material); + object.position.set(300, 0, 200); + scene.add(object); + + // + + object = new THREE.Mesh(new THREE.PlaneGeometry(100, 100, 4, 4), material); + object.position.set(-300, 0, 0); + scene.add(object); + + object = new THREE.Mesh(new THREE.BoxGeometry(100, 100, 100, 4, 4, 4), material); + object.position.set(-100, 0, 0); + scene.add(object); + + object = new THREE.Mesh(new THREE.CircleGeometry(50, 20, 0, Math.PI * 2), material); + object.position.set(100, 0, 0); + scene.add(object); + + object = new THREE.Mesh(new THREE.RingGeometry(10, 50, 20, 5, 0, Math.PI * 2), material); + object.position.set(300, 0, 0); + scene.add(object); + + // + + object = new THREE.Mesh(new THREE.CylinderGeometry(25, 75, 100, 40, 5), material); + object.position.set(-300, 0, -200); + scene.add(object); + + const points = []; + + for (let i = 0; i < 50; i++) { + points.push(new THREE.Vector2(Math.sin(i * 0.2) * Math.sin(i * 0.1) * 15 + 50, (i - 5) * 2)); + } + + object = new THREE.Mesh(new THREE.LatheGeometry(points, 20), material); + object.position.set(-100, 0, -200); + scene.add(object); + + object = new THREE.Mesh(new THREE.TorusGeometry(50, 20, 20, 20), material); + object.position.set(100, 0, -200); + scene.add(object); + + object = new THREE.Mesh(new THREE.TorusKnotGeometry(50, 10, 50, 20), material); + object.position.set(300, 0, -200); + scene.add(object); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + const timer = Date.now() * 0.0001; + + camera.position.x = Math.cos(timer) * 800; + camera.position.z = Math.sin(timer) * 800; + + camera.lookAt(scene.position); + + scene.traverse(function (object) { + if (object.isMesh === true) { + object.rotation.x = timer * 5; + object.rotation.y = timer * 2.5; + } + }); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_geometries_parametric.ts b/examples-testing/examples/webgl_geometries_parametric.ts new file mode 100644 index 000000000..29bf7ae26 --- /dev/null +++ b/examples-testing/examples/webgl_geometries_parametric.ts @@ -0,0 +1,124 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import * as Curves from 'three/addons/curves/CurveExtras.js'; +import { ParametricGeometry } from 'three/addons/geometries/ParametricGeometry.js'; +import { ParametricGeometries } from 'three/addons/geometries/ParametricGeometries.js'; + +let camera, scene, renderer, stats; + +init(); + +function init() { + const container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000); + camera.position.y = 400; + + scene = new THREE.Scene(); + + // + + const ambientLight = new THREE.AmbientLight(0xcccccc, 1.5); + scene.add(ambientLight); + + const pointLight = new THREE.PointLight(0xffffff, 2.5, 0, 0); + camera.add(pointLight); + scene.add(camera); + + // + + const map = new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg'); + map.wrapS = map.wrapT = THREE.RepeatWrapping; + map.anisotropy = 16; + map.colorSpace = THREE.SRGBColorSpace; + + const material = new THREE.MeshPhongMaterial({ map: map, side: THREE.DoubleSide }); + + // + + let geometry, object; + + geometry = new ParametricGeometry(ParametricGeometries.plane(100, 100), 10, 10); + geometry.center(); + object = new THREE.Mesh(geometry, material); + object.position.set(-200, 0, 200); + scene.add(object); + + geometry = new ParametricGeometry(ParametricGeometries.klein, 20, 20); + object = new THREE.Mesh(geometry, material); + object.position.set(0, 0, 200); + object.scale.multiplyScalar(5); + scene.add(object); + + geometry = new ParametricGeometry(ParametricGeometries.mobius, 20, 20); + object = new THREE.Mesh(geometry, material); + object.position.set(200, 0, 200); + object.scale.multiplyScalar(30); + scene.add(object); + + // + + const GrannyKnot = new Curves.GrannyKnot(); + + const torus = new ParametricGeometries.TorusKnotGeometry(50, 10, 50, 20, 2, 3); + const sphere = new ParametricGeometries.SphereGeometry(50, 20, 10); + const tube = new ParametricGeometries.TubeGeometry(GrannyKnot, 100, 3, 8, true); + + object = new THREE.Mesh(torus, material); + object.position.set(-200, 0, -200); + scene.add(object); + + object = new THREE.Mesh(sphere, material); + object.position.set(0, 0, -200); + scene.add(object); + + object = new THREE.Mesh(tube, material); + object.position.set(200, 0, -200); + object.scale.multiplyScalar(2); + scene.add(object); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + const timer = Date.now() * 0.0001; + + camera.position.x = Math.cos(timer) * 800; + camera.position.z = Math.sin(timer) * 800; + + camera.lookAt(scene.position); + + scene.traverse(function (object) { + if (object.isMesh === true) { + object.rotation.x = timer * 5; + object.rotation.y = timer * 2.5; + } + }); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_geometry_colors.ts b/examples-testing/examples/webgl_geometry_colors.ts new file mode 100644 index 000000000..bc0bf5174 --- /dev/null +++ b/examples-testing/examples/webgl_geometry_colors.ts @@ -0,0 +1,176 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; + +let camera, scene, renderer; + +let mouseX = 0, + mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +init(); + +function init() { + container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(20, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 1800; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0, 0, 1); + scene.add(light); + + // shadow + + const canvas = document.createElement('canvas'); + canvas.width = 128; + canvas.height = 128; + + const context = canvas.getContext('2d'); + const gradient = context.createRadialGradient( + canvas.width / 2, + canvas.height / 2, + 0, + canvas.width / 2, + canvas.height / 2, + canvas.width / 2, + ); + gradient.addColorStop(0.1, 'rgba(210,210,210,1)'); + gradient.addColorStop(1, 'rgba(255,255,255,1)'); + + context.fillStyle = gradient; + context.fillRect(0, 0, canvas.width, canvas.height); + + const shadowTexture = new THREE.CanvasTexture(canvas); + + const shadowMaterial = new THREE.MeshBasicMaterial({ map: shadowTexture }); + const shadowGeo = new THREE.PlaneGeometry(300, 300, 1, 1); + + let shadowMesh; + + shadowMesh = new THREE.Mesh(shadowGeo, shadowMaterial); + shadowMesh.position.y = -250; + shadowMesh.rotation.x = -Math.PI / 2; + scene.add(shadowMesh); + + shadowMesh = new THREE.Mesh(shadowGeo, shadowMaterial); + shadowMesh.position.y = -250; + shadowMesh.position.x = -400; + shadowMesh.rotation.x = -Math.PI / 2; + scene.add(shadowMesh); + + shadowMesh = new THREE.Mesh(shadowGeo, shadowMaterial); + shadowMesh.position.y = -250; + shadowMesh.position.x = 400; + shadowMesh.rotation.x = -Math.PI / 2; + scene.add(shadowMesh); + + const radius = 200; + + const geometry1 = new THREE.IcosahedronGeometry(radius, 1); + + const count = geometry1.attributes.position.count; + geometry1.setAttribute('color', new THREE.BufferAttribute(new Float32Array(count * 3), 3)); + + const geometry2 = geometry1.clone(); + const geometry3 = geometry1.clone(); + + const color = new THREE.Color(); + const positions1 = geometry1.attributes.position; + const positions2 = geometry2.attributes.position; + const positions3 = geometry3.attributes.position; + const colors1 = geometry1.attributes.color; + const colors2 = geometry2.attributes.color; + const colors3 = geometry3.attributes.color; + + for (let i = 0; i < count; i++) { + color.setHSL((positions1.getY(i) / radius + 1) / 2, 1.0, 0.5, THREE.SRGBColorSpace); + colors1.setXYZ(i, color.r, color.g, color.b); + + color.setHSL(0, (positions2.getY(i) / radius + 1) / 2, 0.5, THREE.SRGBColorSpace); + colors2.setXYZ(i, color.r, color.g, color.b); + + color.setRGB(1, 0.8 - (positions3.getY(i) / radius + 1) / 2, 0, THREE.SRGBColorSpace); + colors3.setXYZ(i, color.r, color.g, color.b); + } + + const material = new THREE.MeshPhongMaterial({ + color: 0xffffff, + flatShading: true, + vertexColors: true, + shininess: 0, + }); + + const wireframeMaterial = new THREE.MeshBasicMaterial({ color: 0x000000, wireframe: true, transparent: true }); + + let mesh = new THREE.Mesh(geometry1, material); + let wireframe = new THREE.Mesh(geometry1, wireframeMaterial); + mesh.add(wireframe); + mesh.position.x = -400; + mesh.rotation.x = -1.87; + scene.add(mesh); + + mesh = new THREE.Mesh(geometry2, material); + wireframe = new THREE.Mesh(geometry2, wireframeMaterial); + mesh.add(wireframe); + mesh.position.x = 400; + scene.add(mesh); + + mesh = new THREE.Mesh(geometry3, material); + wireframe = new THREE.Mesh(geometry3, wireframeMaterial); + mesh.add(wireframe); + scene.add(mesh); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + document.addEventListener('mousemove', onDocumentMouseMove); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onDocumentMouseMove(event) { + mouseX = event.clientX - windowHalfX; + mouseY = event.clientY - windowHalfY; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-mouseY - camera.position.y) * 0.05; + + camera.lookAt(scene.position); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_geometry_colors_lookuptable.ts b/examples-testing/examples/webgl_geometry_colors_lookuptable.ts new file mode 100644 index 000000000..6b0138529 --- /dev/null +++ b/examples-testing/examples/webgl_geometry_colors_lookuptable.ts @@ -0,0 +1,148 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { Lut } from 'three/addons/math/Lut.js'; + +let container; + +let perpCamera, orthoCamera, renderer, lut; + +let mesh, sprite; +let scene, uiScene; + +let params; + +init(); + +function init() { + container = document.getElementById('container'); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + + uiScene = new THREE.Scene(); + + lut = new Lut(); + + const width = window.innerWidth; + const height = window.innerHeight; + + perpCamera = new THREE.PerspectiveCamera(60, width / height, 1, 100); + perpCamera.position.set(0, 0, 10); + scene.add(perpCamera); + + orthoCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 1, 2); + orthoCamera.position.set(0.5, 0, 1); + + sprite = new THREE.Sprite( + new THREE.SpriteMaterial({ + map: new THREE.CanvasTexture(lut.createCanvas()), + }), + ); + sprite.material.map.colorSpace = THREE.SRGBColorSpace; + sprite.scale.x = 0.125; + uiScene.add(sprite); + + mesh = new THREE.Mesh( + undefined, + new THREE.MeshLambertMaterial({ + side: THREE.DoubleSide, + color: 0xf5f5f5, + vertexColors: true, + }), + ); + scene.add(mesh); + + params = { + colorMap: 'rainbow', + }; + loadModel(); + + const pointLight = new THREE.PointLight(0xffffff, 3, 0, 0); + perpCamera.add(pointLight); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.autoClear = false; + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(width, height); + container.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); + + const controls = new OrbitControls(perpCamera, renderer.domElement); + controls.addEventListener('change', render); + + const gui = new GUI(); + + gui.add(params, 'colorMap', ['rainbow', 'cooltowarm', 'blackbody', 'grayscale']).onChange(function () { + updateColors(); + render(); + }); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + perpCamera.aspect = width / height; + perpCamera.updateProjectionMatrix(); + + renderer.setSize(width, height); + render(); +} + +function render() { + renderer.clear(); + renderer.render(scene, perpCamera); + renderer.render(uiScene, orthoCamera); +} + +function loadModel() { + const loader = new THREE.BufferGeometryLoader(); + loader.load('models/json/pressure.json', function (geometry) { + geometry.center(); + geometry.computeVertexNormals(); + + // default color attribute + const colors = []; + + for (let i = 0, n = geometry.attributes.position.count; i < n; ++i) { + colors.push(1, 1, 1); + } + + geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); + + mesh.geometry = geometry; + updateColors(); + + render(); + }); +} + +function updateColors() { + lut.setColorMap(params.colorMap); + + lut.setMax(2000); + lut.setMin(0); + + const geometry = mesh.geometry; + const pressures = geometry.attributes.pressure; + const colors = geometry.attributes.color; + const color = new THREE.Color(); + + for (let i = 0; i < pressures.array.length; i++) { + const colorValue = pressures.array[i]; + + color.copy(lut.getColor(colorValue)).convertSRGBToLinear(); + + colors.setXYZ(i, color.r, color.g, color.b); + } + + colors.needsUpdate = true; + + const map = sprite.material.map; + lut.updateCanvas(map.image); + map.needsUpdate = true; +} diff --git a/examples-testing/examples/webgl_geometry_convex.ts b/examples-testing/examples/webgl_geometry_convex.ts new file mode 100644 index 000000000..ade9cb801 --- /dev/null +++ b/examples-testing/examples/webgl_geometry_convex.ts @@ -0,0 +1,117 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { ConvexGeometry } from 'three/addons/geometries/ConvexGeometry.js'; +import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; + +let group, camera, scene, renderer; + +init(); + +function init() { + scene = new THREE.Scene(); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // camera + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(15, 20, 30); + scene.add(camera); + + // controls + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 20; + controls.maxDistance = 50; + controls.maxPolarAngle = Math.PI / 2; + + // ambient light + + scene.add(new THREE.AmbientLight(0x666666)); + + // point light + + const light = new THREE.PointLight(0xffffff, 3, 0, 0); + camera.add(light); + + // helper + + scene.add(new THREE.AxesHelper(20)); + + // textures + + const loader = new THREE.TextureLoader(); + const texture = loader.load('textures/sprites/disc.png'); + texture.colorSpace = THREE.SRGBColorSpace; + + group = new THREE.Group(); + scene.add(group); + + // points + + let dodecahedronGeometry = new THREE.DodecahedronGeometry(10); + + // if normal and uv attributes are not removed, mergeVertices() can't consolidate indentical vertices with different normal/uv data + + dodecahedronGeometry.deleteAttribute('normal'); + dodecahedronGeometry.deleteAttribute('uv'); + + dodecahedronGeometry = BufferGeometryUtils.mergeVertices(dodecahedronGeometry); + + const vertices = []; + const positionAttribute = dodecahedronGeometry.getAttribute('position'); + + for (let i = 0; i < positionAttribute.count; i++) { + const vertex = new THREE.Vector3(); + vertex.fromBufferAttribute(positionAttribute, i); + vertices.push(vertex); + } + + const pointsMaterial = new THREE.PointsMaterial({ + color: 0x0080ff, + map: texture, + size: 1, + alphaTest: 0.5, + }); + + const pointsGeometry = new THREE.BufferGeometry().setFromPoints(vertices); + + const points = new THREE.Points(pointsGeometry, pointsMaterial); + group.add(points); + + // convex hull + + const meshMaterial = new THREE.MeshLambertMaterial({ + color: 0xffffff, + opacity: 0.5, + side: THREE.DoubleSide, + transparent: true, + }); + + const meshGeometry = new ConvexGeometry(vertices); + + const mesh = new THREE.Mesh(meshGeometry, meshMaterial); + group.add(mesh); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + group.rotation.y += 0.005; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_geometry_cube.ts b/examples-testing/examples/webgl_geometry_cube.ts new file mode 100644 index 000000000..572601acb --- /dev/null +++ b/examples-testing/examples/webgl_geometry_cube.ts @@ -0,0 +1,46 @@ +import * as THREE from 'three'; + +let camera, scene, renderer; +let mesh; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.z = 2; + + scene = new THREE.Scene(); + + const texture = new THREE.TextureLoader().load('textures/crate.gif'); + texture.colorSpace = THREE.SRGBColorSpace; + + const geometry = new THREE.BoxGeometry(); + const material = new THREE.MeshBasicMaterial({ map: texture }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + mesh.rotation.x += 0.005; + mesh.rotation.y += 0.01; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_geometry_dynamic.ts b/examples-testing/examples/webgl_geometry_dynamic.ts new file mode 100644 index 000000000..06e858f54 --- /dev/null +++ b/examples-testing/examples/webgl_geometry_dynamic.ts @@ -0,0 +1,97 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js'; + +let camera, controls, scene, renderer, stats; + +let mesh, geometry, material, clock; + +const worldWidth = 128, + worldDepth = 128; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 20000); + camera.position.y = 200; + + clock = new THREE.Clock(); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xaaccff); + scene.fog = new THREE.FogExp2(0xaaccff, 0.0007); + + geometry = new THREE.PlaneGeometry(20000, 20000, worldWidth - 1, worldDepth - 1); + geometry.rotateX(-Math.PI / 2); + + const position = geometry.attributes.position; + position.usage = THREE.DynamicDrawUsage; + + for (let i = 0; i < position.count; i++) { + const y = 35 * Math.sin(i / 2); + position.setY(i, y); + } + + const texture = new THREE.TextureLoader().load('textures/water.jpg'); + texture.wrapS = texture.wrapT = THREE.RepeatWrapping; + texture.repeat.set(5, 5); + texture.colorSpace = THREE.SRGBColorSpace; + + material = new THREE.MeshBasicMaterial({ color: 0x0044ff, map: texture }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + controls = new FirstPersonControls(camera, renderer.domElement); + + controls.movementSpeed = 500; + controls.lookSpeed = 0.1; + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + controls.handleResize(); +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + const delta = clock.getDelta(); + const time = clock.getElapsedTime() * 10; + + const position = geometry.attributes.position; + + for (let i = 0; i < position.count; i++) { + const y = 35 * Math.sin(i / 5 + (time + i) / 7); + position.setY(i, y); + } + + position.needsUpdate = true; + + controls.update(delta); + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_geometry_extrude_shapes.ts b/examples-testing/examples/webgl_geometry_extrude_shapes.ts new file mode 100644 index 000000000..7428aee31 --- /dev/null +++ b/examples-testing/examples/webgl_geometry_extrude_shapes.ts @@ -0,0 +1,149 @@ +import * as THREE from 'three'; + +import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; + +let camera, scene, renderer, controls; + +init(); + +function init() { + const info = document.createElement('div'); + info.style.position = 'absolute'; + info.style.top = '10px'; + info.style.width = '100%'; + info.style.textAlign = 'center'; + info.style.color = '#fff'; + info.style.link = '#f80'; + info.innerHTML = + 'three.js webgl - geometry extrude shapes'; + document.body.appendChild(info); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x222222); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 0, 500); + + controls = new TrackballControls(camera, renderer.domElement); + controls.minDistance = 200; + controls.maxDistance = 500; + + scene.add(new THREE.AmbientLight(0x666666)); + + const light = new THREE.PointLight(0xffffff, 3, 0, 0); + light.position.copy(camera.position); + scene.add(light); + + // + + const closedSpline = new THREE.CatmullRomCurve3([ + new THREE.Vector3(-60, -100, 60), + new THREE.Vector3(-60, 20, 60), + new THREE.Vector3(-60, 120, 60), + new THREE.Vector3(60, 20, -60), + new THREE.Vector3(60, -100, -60), + ]); + + closedSpline.curveType = 'catmullrom'; + closedSpline.closed = true; + + const extrudeSettings1 = { + steps: 100, + bevelEnabled: false, + extrudePath: closedSpline, + }; + + const pts1 = [], + count = 3; + + for (let i = 0; i < count; i++) { + const l = 20; + + const a = ((2 * i) / count) * Math.PI; + + pts1.push(new THREE.Vector2(Math.cos(a) * l, Math.sin(a) * l)); + } + + const shape1 = new THREE.Shape(pts1); + + const geometry1 = new THREE.ExtrudeGeometry(shape1, extrudeSettings1); + + const material1 = new THREE.MeshLambertMaterial({ color: 0xb00000, wireframe: false }); + + const mesh1 = new THREE.Mesh(geometry1, material1); + + scene.add(mesh1); + + // + + const randomPoints = []; + + for (let i = 0; i < 10; i++) { + randomPoints.push( + new THREE.Vector3((i - 4.5) * 50, THREE.MathUtils.randFloat(-50, 50), THREE.MathUtils.randFloat(-50, 50)), + ); + } + + const randomSpline = new THREE.CatmullRomCurve3(randomPoints); + + // + + const extrudeSettings2 = { + steps: 200, + bevelEnabled: false, + extrudePath: randomSpline, + }; + + const pts2 = [], + numPts = 5; + + for (let i = 0; i < numPts * 2; i++) { + const l = i % 2 == 1 ? 10 : 20; + + const a = (i / numPts) * Math.PI; + + pts2.push(new THREE.Vector2(Math.cos(a) * l, Math.sin(a) * l)); + } + + const shape2 = new THREE.Shape(pts2); + + const geometry2 = new THREE.ExtrudeGeometry(shape2, extrudeSettings2); + + const material2 = new THREE.MeshLambertMaterial({ color: 0xff8000, wireframe: false }); + + const mesh2 = new THREE.Mesh(geometry2, material2); + + scene.add(mesh2); + + // + + const materials = [material1, material2]; + + const extrudeSettings3 = { + depth: 20, + steps: 1, + bevelEnabled: true, + bevelThickness: 2, + bevelSize: 4, + bevelSegments: 1, + }; + + const geometry3 = new THREE.ExtrudeGeometry(shape2, extrudeSettings3); + + const mesh3 = new THREE.Mesh(geometry3, materials); + + mesh3.position.set(50, 100, 50); + + scene.add(mesh3); +} + +function animate() { + controls.update(); + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_geometry_extrude_splines.ts b/examples-testing/examples/webgl_geometry_extrude_splines.ts new file mode 100644 index 000000000..0741083da --- /dev/null +++ b/examples-testing/examples/webgl_geometry_extrude_splines.ts @@ -0,0 +1,310 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import * as Curves from 'three/addons/curves/CurveExtras.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let container, stats; + +let camera, scene, renderer, splineCamera, cameraHelper, cameraEye; + +const direction = new THREE.Vector3(); +const binormal = new THREE.Vector3(); +const normal = new THREE.Vector3(); +const position = new THREE.Vector3(); +const lookAt = new THREE.Vector3(); + +const pipeSpline = new THREE.CatmullRomCurve3([ + new THREE.Vector3(0, 10, -10), + new THREE.Vector3(10, 0, -10), + new THREE.Vector3(20, 0, 0), + new THREE.Vector3(30, 0, 10), + new THREE.Vector3(30, 0, 20), + new THREE.Vector3(20, 0, 30), + new THREE.Vector3(10, 0, 30), + new THREE.Vector3(0, 0, 30), + new THREE.Vector3(-10, 10, 30), + new THREE.Vector3(-10, 20, 30), + new THREE.Vector3(0, 30, 30), + new THREE.Vector3(10, 30, 30), + new THREE.Vector3(20, 30, 15), + new THREE.Vector3(10, 30, 10), + new THREE.Vector3(0, 30, 10), + new THREE.Vector3(-10, 20, 10), + new THREE.Vector3(-10, 10, 10), + new THREE.Vector3(0, 0, 10), + new THREE.Vector3(10, -10, 10), + new THREE.Vector3(20, -15, 10), + new THREE.Vector3(30, -15, 10), + new THREE.Vector3(40, -15, 10), + new THREE.Vector3(50, -15, 10), + new THREE.Vector3(60, 0, 10), + new THREE.Vector3(70, 0, 0), + new THREE.Vector3(80, 0, 0), + new THREE.Vector3(90, 0, 0), + new THREE.Vector3(100, 0, 0), +]); + +const sampleClosedSpline = new THREE.CatmullRomCurve3([ + new THREE.Vector3(0, -40, -40), + new THREE.Vector3(0, 40, -40), + new THREE.Vector3(0, 140, -40), + new THREE.Vector3(0, 40, 40), + new THREE.Vector3(0, -40, 40), +]); + +sampleClosedSpline.curveType = 'catmullrom'; +sampleClosedSpline.closed = true; + +// Keep a dictionary of Curve instances +const splines = { + GrannyKnot: new Curves.GrannyKnot(), + HeartCurve: new Curves.HeartCurve(3.5), + VivianiCurve: new Curves.VivianiCurve(70), + KnotCurve: new Curves.KnotCurve(), + HelixCurve: new Curves.HelixCurve(), + TrefoilKnot: new Curves.TrefoilKnot(), + TorusKnot: new Curves.TorusKnot(20), + CinquefoilKnot: new Curves.CinquefoilKnot(20), + TrefoilPolynomialKnot: new Curves.TrefoilPolynomialKnot(14), + FigureEightPolynomialKnot: new Curves.FigureEightPolynomialKnot(), + DecoratedTorusKnot4a: new Curves.DecoratedTorusKnot4a(), + DecoratedTorusKnot4b: new Curves.DecoratedTorusKnot4b(), + DecoratedTorusKnot5a: new Curves.DecoratedTorusKnot5a(), + DecoratedTorusKnot5c: new Curves.DecoratedTorusKnot5c(), + PipeSpline: pipeSpline, + SampleClosedSpline: sampleClosedSpline, +}; + +let parent, tubeGeometry, mesh; + +const params = { + spline: 'GrannyKnot', + scale: 4, + extrusionSegments: 100, + radiusSegments: 3, + closed: true, + animationView: false, + lookAhead: false, + cameraHelper: false, +}; + +const material = new THREE.MeshLambertMaterial({ color: 0xff00ff }); + +const wireframeMaterial = new THREE.MeshBasicMaterial({ + color: 0x000000, + opacity: 0.3, + wireframe: true, + transparent: true, +}); + +function addTube() { + if (mesh !== undefined) { + parent.remove(mesh); + mesh.geometry.dispose(); + } + + const extrudePath = splines[params.spline]; + + tubeGeometry = new THREE.TubeGeometry( + extrudePath, + params.extrusionSegments, + 2, + params.radiusSegments, + params.closed, + ); + + addGeometry(tubeGeometry); + + setScale(); +} + +function setScale() { + mesh.scale.set(params.scale, params.scale, params.scale); +} + +function addGeometry(geometry) { + // 3D shape + + mesh = new THREE.Mesh(geometry, material); + const wireframe = new THREE.Mesh(geometry, wireframeMaterial); + mesh.add(wireframe); + + parent.add(mesh); +} + +function animateCamera() { + cameraHelper.visible = params.cameraHelper; + cameraEye.visible = params.cameraHelper; +} + +init(); + +function init() { + container = document.getElementById('container'); + + // camera + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.01, 10000); + camera.position.set(0, 50, 500); + + // scene + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + // light + + scene.add(new THREE.AmbientLight(0xffffff)); + + const light = new THREE.DirectionalLight(0xffffff, 1.5); + light.position.set(0, 0, 1); + scene.add(light); + + // tube + + parent = new THREE.Object3D(); + scene.add(parent); + + splineCamera = new THREE.PerspectiveCamera(84, window.innerWidth / window.innerHeight, 0.01, 1000); + parent.add(splineCamera); + + cameraHelper = new THREE.CameraHelper(splineCamera); + scene.add(cameraHelper); + + addTube(); + + // debug camera + + cameraEye = new THREE.Mesh(new THREE.SphereGeometry(5), new THREE.MeshBasicMaterial({ color: 0xdddddd })); + parent.add(cameraEye); + + cameraHelper.visible = params.cameraHelper; + cameraEye.visible = params.cameraHelper; + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // stats + + stats = new Stats(); + container.appendChild(stats.dom); + + // dat.GUI + + const gui = new GUI({ width: 285 }); + + const folderGeometry = gui.addFolder('Geometry'); + folderGeometry.add(params, 'spline', Object.keys(splines)).onChange(function () { + addTube(); + }); + folderGeometry + .add(params, 'scale', 2, 10) + .step(2) + .onChange(function () { + setScale(); + }); + folderGeometry + .add(params, 'extrusionSegments', 50, 500) + .step(50) + .onChange(function () { + addTube(); + }); + folderGeometry + .add(params, 'radiusSegments', 2, 12) + .step(1) + .onChange(function () { + addTube(); + }); + folderGeometry.add(params, 'closed').onChange(function () { + addTube(); + }); + folderGeometry.open(); + + const folderCamera = gui.addFolder('Camera'); + folderCamera.add(params, 'animationView').onChange(function () { + animateCamera(); + }); + folderCamera.add(params, 'lookAhead').onChange(function () { + animateCamera(); + }); + folderCamera.add(params, 'cameraHelper').onChange(function () { + animateCamera(); + }); + folderCamera.open(); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 100; + controls.maxDistance = 2000; + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + // animate camera along spline + + const time = Date.now(); + const looptime = 20 * 1000; + const t = (time % looptime) / looptime; + + tubeGeometry.parameters.path.getPointAt(t, position); + position.multiplyScalar(params.scale); + + // interpolation + + const segments = tubeGeometry.tangents.length; + const pickt = t * segments; + const pick = Math.floor(pickt); + const pickNext = (pick + 1) % segments; + + binormal.subVectors(tubeGeometry.binormals[pickNext], tubeGeometry.binormals[pick]); + binormal.multiplyScalar(pickt - pick).add(tubeGeometry.binormals[pick]); + + tubeGeometry.parameters.path.getTangentAt(t, direction); + const offset = 15; + + normal.copy(binormal).cross(direction); + + // we move on a offset on its binormal + + position.add(normal.clone().multiplyScalar(offset)); + + splineCamera.position.copy(position); + cameraEye.position.copy(position); + + // using arclength for stablization in look ahead + + tubeGeometry.parameters.path.getPointAt((t + 30 / tubeGeometry.parameters.path.getLength()) % 1, lookAt); + lookAt.multiplyScalar(params.scale); + + // camera orientation 2 - up orientation via normal + + if (!params.lookAhead) lookAt.copy(position).add(direction); + splineCamera.matrix.lookAt(splineCamera.position, lookAt, normal); + splineCamera.quaternion.setFromRotationMatrix(splineCamera.matrix); + + cameraHelper.update(); + + renderer.render(scene, params.animationView === true ? splineCamera : camera); +} diff --git a/examples-testing/examples/webgl_geometry_minecraft.ts b/examples-testing/examples/webgl_geometry_minecraft.ts new file mode 100644 index 000000000..765aa1e49 --- /dev/null +++ b/examples-testing/examples/webgl_geometry_minecraft.ts @@ -0,0 +1,183 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js'; +import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js'; +import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; + +let container, stats; + +let camera, controls, scene, renderer; + +const worldWidth = 128, + worldDepth = 128; +const worldHalfWidth = worldWidth / 2; +const worldHalfDepth = worldDepth / 2; +const data = generateHeight(worldWidth, worldDepth); + +const clock = new THREE.Clock(); + +init(); + +function init() { + container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 20000); + camera.position.y = getY(worldHalfWidth, worldHalfDepth) * 100 + 100; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xbfd1e5); + + // sides + + const matrix = new THREE.Matrix4(); + + const pxGeometry = new THREE.PlaneGeometry(100, 100); + pxGeometry.attributes.uv.array[1] = 0.5; + pxGeometry.attributes.uv.array[3] = 0.5; + pxGeometry.rotateY(Math.PI / 2); + pxGeometry.translate(50, 0, 0); + + const nxGeometry = new THREE.PlaneGeometry(100, 100); + nxGeometry.attributes.uv.array[1] = 0.5; + nxGeometry.attributes.uv.array[3] = 0.5; + nxGeometry.rotateY(-Math.PI / 2); + nxGeometry.translate(-50, 0, 0); + + const pyGeometry = new THREE.PlaneGeometry(100, 100); + pyGeometry.attributes.uv.array[5] = 0.5; + pyGeometry.attributes.uv.array[7] = 0.5; + pyGeometry.rotateX(-Math.PI / 2); + pyGeometry.translate(0, 50, 0); + + const pzGeometry = new THREE.PlaneGeometry(100, 100); + pzGeometry.attributes.uv.array[1] = 0.5; + pzGeometry.attributes.uv.array[3] = 0.5; + pzGeometry.translate(0, 0, 50); + + const nzGeometry = new THREE.PlaneGeometry(100, 100); + nzGeometry.attributes.uv.array[1] = 0.5; + nzGeometry.attributes.uv.array[3] = 0.5; + nzGeometry.rotateY(Math.PI); + nzGeometry.translate(0, 0, -50); + + // + + const geometries = []; + + for (let z = 0; z < worldDepth; z++) { + for (let x = 0; x < worldWidth; x++) { + const h = getY(x, z); + + matrix.makeTranslation(x * 100 - worldHalfWidth * 100, h * 100, z * 100 - worldHalfDepth * 100); + + const px = getY(x + 1, z); + const nx = getY(x - 1, z); + const pz = getY(x, z + 1); + const nz = getY(x, z - 1); + + geometries.push(pyGeometry.clone().applyMatrix4(matrix)); + + if ((px !== h && px !== h + 1) || x === 0) { + geometries.push(pxGeometry.clone().applyMatrix4(matrix)); + } + + if ((nx !== h && nx !== h + 1) || x === worldWidth - 1) { + geometries.push(nxGeometry.clone().applyMatrix4(matrix)); + } + + if ((pz !== h && pz !== h + 1) || z === worldDepth - 1) { + geometries.push(pzGeometry.clone().applyMatrix4(matrix)); + } + + if ((nz !== h && nz !== h + 1) || z === 0) { + geometries.push(nzGeometry.clone().applyMatrix4(matrix)); + } + } + } + + const geometry = BufferGeometryUtils.mergeGeometries(geometries); + geometry.computeBoundingSphere(); + + const texture = new THREE.TextureLoader().load('textures/minecraft/atlas.png'); + texture.colorSpace = THREE.SRGBColorSpace; + texture.magFilter = THREE.NearestFilter; + + const mesh = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ map: texture, side: THREE.DoubleSide })); + scene.add(mesh); + + const ambientLight = new THREE.AmbientLight(0xeeeeee, 3); + scene.add(ambientLight); + + const directionalLight = new THREE.DirectionalLight(0xffffff, 12); + directionalLight.position.set(1, 1, 0.5).normalize(); + scene.add(directionalLight); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + controls = new FirstPersonControls(camera, renderer.domElement); + + controls.movementSpeed = 1000; + controls.lookSpeed = 0.125; + controls.lookVertical = true; + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + controls.handleResize(); +} + +function generateHeight(width, height) { + const data = [], + perlin = new ImprovedNoise(), + size = width * height, + z = Math.random() * 100; + + let quality = 2; + + for (let j = 0; j < 4; j++) { + if (j === 0) for (let i = 0; i < size; i++) data[i] = 0; + + for (let i = 0; i < size; i++) { + const x = i % width, + y = (i / width) | 0; + data[i] += perlin.noise(x / quality, y / quality, z) * quality; + } + + quality *= 4; + } + + return data; +} + +function getY(x, z) { + return (data[x + z * worldWidth] * 0.15) | 0; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + controls.update(clock.getDelta()); + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_geometry_nurbs.ts b/examples-testing/examples/webgl_geometry_nurbs.ts new file mode 100644 index 000000000..a603710bd --- /dev/null +++ b/examples-testing/examples/webgl_geometry_nurbs.ts @@ -0,0 +1,298 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { NURBSCurve } from 'three/addons/curves/NURBSCurve.js'; +import { NURBSSurface } from 'three/addons/curves/NURBSSurface.js'; +import { NURBSVolume } from 'three/addons/curves/NURBSVolume.js'; +import { ParametricGeometry } from 'three/addons/geometries/ParametricGeometry.js'; + +let container, stats; + +let camera, scene, renderer; +let group; + +let targetRotation = 0; +let targetRotationOnPointerDown = 0; + +let pointerX = 0; +let pointerXOnPointerDown = 0; + +let windowHalfX = window.innerWidth / 2; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 2000); + camera.position.set(0, 150, 750); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + scene.add(new THREE.AmbientLight(0xffffff)); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(1, 1, 1); + scene.add(light); + + group = new THREE.Group(); + group.position.y = 50; + scene.add(group); + + // NURBS curve + + const nurbsControlPoints = []; + const nurbsKnots = []; + const nurbsDegree = 3; + + for (let i = 0; i <= nurbsDegree; i++) { + nurbsKnots.push(0); + } + + for (let i = 0, j = 20; i < j; i++) { + nurbsControlPoints.push( + new THREE.Vector4( + Math.random() * 400 - 200, + Math.random() * 400, + Math.random() * 400 - 200, + 1, // weight of control point: higher means stronger attraction + ), + ); + + const knot = (i + 1) / (j - nurbsDegree); + nurbsKnots.push(THREE.MathUtils.clamp(knot, 0, 1)); + } + + const nurbsCurve = new NURBSCurve(nurbsDegree, nurbsKnots, nurbsControlPoints); + + const nurbsGeometry = new THREE.BufferGeometry(); + nurbsGeometry.setFromPoints(nurbsCurve.getPoints(200)); + + const nurbsMaterial = new THREE.LineBasicMaterial({ color: 0x333333 }); + + const nurbsLine = new THREE.Line(nurbsGeometry, nurbsMaterial); + nurbsLine.position.set(0, -100, 0); + group.add(nurbsLine); + + const nurbsControlPointsGeometry = new THREE.BufferGeometry(); + nurbsControlPointsGeometry.setFromPoints(nurbsCurve.controlPoints); + + const nurbsControlPointsMaterial = new THREE.LineBasicMaterial({ + color: 0x333333, + opacity: 0.25, + transparent: true, + }); + + const nurbsControlPointsLine = new THREE.Line(nurbsControlPointsGeometry, nurbsControlPointsMaterial); + nurbsControlPointsLine.position.copy(nurbsLine.position); + group.add(nurbsControlPointsLine); + + // NURBS surface + { + const nsControlPoints = [ + [ + new THREE.Vector4(-200, -200, 100, 1), + new THREE.Vector4(-200, -100, -200, 1), + new THREE.Vector4(-200, 100, 250, 1), + new THREE.Vector4(-200, 200, -100, 1), + ], + [ + new THREE.Vector4(0, -200, 0, 1), + new THREE.Vector4(0, -100, -100, 5), + new THREE.Vector4(0, 100, 150, 5), + new THREE.Vector4(0, 200, 0, 1), + ], + [ + new THREE.Vector4(200, -200, -100, 1), + new THREE.Vector4(200, -100, 200, 1), + new THREE.Vector4(200, 100, -250, 1), + new THREE.Vector4(200, 200, 100, 1), + ], + ]; + const degree1 = 2; + const degree2 = 3; + const knots1 = [0, 0, 0, 1, 1, 1]; + const knots2 = [0, 0, 0, 0, 1, 1, 1, 1]; + const nurbsSurface = new NURBSSurface(degree1, degree2, knots1, knots2, nsControlPoints); + + const map = new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg'); + map.wrapS = map.wrapT = THREE.RepeatWrapping; + map.anisotropy = 16; + map.colorSpace = THREE.SRGBColorSpace; + + function getSurfacePoint(u, v, target) { + return nurbsSurface.getPoint(u, v, target); + } + + const geometry = new ParametricGeometry(getSurfacePoint, 20, 20); + const material = new THREE.MeshLambertMaterial({ map: map, side: THREE.DoubleSide }); + const object = new THREE.Mesh(geometry, material); + object.position.set(-400, 100, 0); + object.scale.multiplyScalar(1); + group.add(object); + } + + // NURBS volume + { + const nsControlPoints = [ + [ + [new THREE.Vector4(-200, -200, -200, 1), new THREE.Vector4(-200, -200, 200, 1)], + [new THREE.Vector4(-200, -100, -200, 1), new THREE.Vector4(-200, -100, 200, 1)], + [new THREE.Vector4(-200, 100, -200, 1), new THREE.Vector4(-200, 100, 200, 1)], + [new THREE.Vector4(-200, 200, -200, 1), new THREE.Vector4(-200, 200, 200, 1)], + ], + [ + [new THREE.Vector4(0, -200, -200, 1), new THREE.Vector4(0, -200, 200, 1)], + [new THREE.Vector4(0, -100, -200, 1), new THREE.Vector4(0, -100, 200, 1)], + [new THREE.Vector4(0, 100, -200, 1), new THREE.Vector4(0, 100, 200, 1)], + [new THREE.Vector4(0, 200, -200, 1), new THREE.Vector4(0, 200, 200, 1)], + ], + [ + [new THREE.Vector4(200, -200, -200, 1), new THREE.Vector4(200, -200, 200, 1)], + [new THREE.Vector4(200, -100, 0, 1), new THREE.Vector4(200, -100, 100, 1)], + [new THREE.Vector4(200, 100, 0, 1), new THREE.Vector4(200, 100, 100, 1)], + [new THREE.Vector4(200, 200, 0, 1), new THREE.Vector4(200, 200, 100, 1)], + ], + ]; + const degree1 = 2; + const degree2 = 3; + const degree3 = 1; + const knots1 = [0, 0, 0, 1, 1, 1]; + const knots2 = [0, 0, 0, 0, 1, 1, 1, 1]; + const knots3 = [0, 0, 1, 1]; + const nurbsVolume = new NURBSVolume(degree1, degree2, degree3, knots1, knots2, knots3, nsControlPoints); + + const map = new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg'); + map.wrapS = map.wrapT = THREE.RepeatWrapping; + map.anisotropy = 16; + map.colorSpace = THREE.SRGBColorSpace; + + // Since ParametricGeometry() only support bi-variate parametric geometries + // we create evaluation functions for different surfaces with one of the three + // parameter values (u, v, w) kept constant and create multiple THREE.Mesh + // objects one for each surface + function getSurfacePointFront(u, v, target) { + return nurbsVolume.getPoint(u, v, 0, target); + } + + function getSurfacePointMiddle(u, v, target) { + return nurbsVolume.getPoint(u, v, 0.5, target); + } + + function getSurfacePointBack(u, v, target) { + return nurbsVolume.getPoint(u, v, 1, target); + } + + function getSurfacePointTop(u, w, target) { + return nurbsVolume.getPoint(u, 1, w, target); + } + + function getSurfacePointSide(v, w, target) { + return nurbsVolume.getPoint(0, v, w, target); + } + + const geometryFront = new ParametricGeometry(getSurfacePointFront, 20, 20); + const materialFront = new THREE.MeshLambertMaterial({ map: map, side: THREE.DoubleSide }); + const objectFront = new THREE.Mesh(geometryFront, materialFront); + objectFront.position.set(400, 100, 0); + objectFront.scale.multiplyScalar(0.5); + group.add(objectFront); + + const geometryMiddle = new ParametricGeometry(getSurfacePointMiddle, 20, 20); + const materialMiddle = new THREE.MeshLambertMaterial({ map: map, side: THREE.DoubleSide }); + const objectMiddle = new THREE.Mesh(geometryMiddle, materialMiddle); + objectMiddle.position.set(400, 100, 0); + objectMiddle.scale.multiplyScalar(0.5); + group.add(objectMiddle); + + const geometryBack = new ParametricGeometry(getSurfacePointBack, 20, 20); + const materialBack = new THREE.MeshLambertMaterial({ map: map, side: THREE.DoubleSide }); + const objectBack = new THREE.Mesh(geometryBack, materialBack); + objectBack.position.set(400, 100, 0); + objectBack.scale.multiplyScalar(0.5); + group.add(objectBack); + + const geometryTop = new ParametricGeometry(getSurfacePointTop, 20, 20); + const materialTop = new THREE.MeshLambertMaterial({ map: map, side: THREE.DoubleSide }); + const objectTop = new THREE.Mesh(geometryTop, materialTop); + objectTop.position.set(400, 100, 0); + objectTop.scale.multiplyScalar(0.5); + group.add(objectTop); + + const geometrySide = new ParametricGeometry(getSurfacePointSide, 20, 20); + const materialSide = new THREE.MeshLambertMaterial({ map: map, side: THREE.DoubleSide }); + const objectSide = new THREE.Mesh(geometrySide, materialSide); + objectSide.position.set(400, 100, 0); + objectSide.scale.multiplyScalar(0.5); + group.add(objectSide); + } + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + container.style.touchAction = 'none'; + container.addEventListener('pointerdown', onPointerDown); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function onPointerDown(event) { + if (event.isPrimary === false) return; + + pointerXOnPointerDown = event.clientX - windowHalfX; + targetRotationOnPointerDown = targetRotation; + + document.addEventListener('pointermove', onPointerMove); + document.addEventListener('pointerup', onPointerUp); +} + +function onPointerMove(event) { + if (event.isPrimary === false) return; + + pointerX = event.clientX - windowHalfX; + + targetRotation = targetRotationOnPointerDown + (pointerX - pointerXOnPointerDown) * 0.02; +} + +function onPointerUp() { + if (event.isPrimary === false) return; + + document.removeEventListener('pointermove', onPointerMove); + document.removeEventListener('pointerup', onPointerUp); +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + group.rotation.y += (targetRotation - group.rotation.y) * 0.05; + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_geometry_sdf.ts b/examples-testing/examples/webgl_geometry_sdf.ts new file mode 100644 index 000000000..fa5dba102 --- /dev/null +++ b/examples-testing/examples/webgl_geometry_sdf.ts @@ -0,0 +1,164 @@ +import * as THREE from 'three'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { SDFGeometryGenerator } from 'three/addons/geometries/SDFGeometryGenerator.js'; +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let renderer, stats, meshFromSDF, scene, camera, clock, controls; + +const settings = { + res: 4, + bounds: 1, + autoRotate: true, + wireframe: true, + material: 'depth', + vertexCount: '0', +}; + +// Example SDF from https://www.shadertoy.com/view/MdXSWn --> + +const shader = /* glsl */ ` + float dist(vec3 p) { + p.xyz = p.xzy; + p *= 1.2; + vec3 z = p; + vec3 dz=vec3(0.0); + float power = 8.0; + float r, theta, phi; + float dr = 1.0; + + float t0 = 1.0; + for(int i = 0; i < 7; ++i) { + r = length(z); + if(r > 2.0) continue; + theta = atan(z.y / z.x); + #ifdef phase_shift_on + phi = asin(z.z / r) ; + #else + phi = asin(z.z / r); + #endif + + dr = pow(r, power - 1.0) * dr * power + 1.0; + + r = pow(r, power); + theta = theta * power; + phi = phi * power; + + z = r * vec3(cos(theta)*cos(phi), sin(theta)*cos(phi), sin(phi)) + p; + + t0 = min(t0, r); + } + + return 0.5 * log(r) * r / dr; + } + `; + +init(); + +function init() { + const w = window.innerWidth; + const h = window.innerHeight; + + camera = new THREE.OrthographicCamera(w / -2, w / 2, h / 2, h / -2, 0.01, 1600); + camera.position.z = 1100; + + scene = new THREE.Scene(); + + clock = new THREE.Clock(); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + controls = new OrbitControls(camera, renderer.domElement); + controls.enableDamping = true; + + window.addEventListener('resize', onWindowResize); + + // + + const panel = new GUI(); + + panel.add(settings, 'res', 1, 6, 1).name('Res').onFinishChange(compile); + panel.add(settings, 'bounds', 1, 10, 1).name('Bounds').onFinishChange(compile); + panel.add(settings, 'material', ['depth', 'normal']).name('Material').onChange(setMaterial); + panel.add(settings, 'wireframe').name('Wireframe').onChange(setMaterial); + panel.add(settings, 'autoRotate').name('Auto Rotate'); + panel.add(settings, 'vertexCount').name('Vertex count').listen().disable(); + + // + + compile(); +} + +function compile() { + const generator = new SDFGeometryGenerator(renderer); + const geometry = generator.generate(Math.pow(2, settings.res + 2), shader, settings.bounds); + geometry.computeVertexNormals(); + + if (meshFromSDF) { + // updates mesh + + meshFromSDF.geometry.dispose(); + meshFromSDF.geometry = geometry; + } else { + // inits meshFromSDF : THREE.Mesh + + meshFromSDF = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial()); + scene.add(meshFromSDF); + + const scale = (Math.min(window.innerWidth, window.innerHeight) / 2) * 0.66; + meshFromSDF.scale.set(scale, scale, scale); + + setMaterial(); + } + + settings.vertexCount = geometry.attributes.position.count; +} + +function setMaterial() { + meshFromSDF.material.dispose(); + + if (settings.material == 'depth') { + meshFromSDF.material = new THREE.MeshDepthMaterial(); + } else if (settings.material == 'normal') { + meshFromSDF.material = new THREE.MeshNormalMaterial(); + } + + meshFromSDF.material.wireframe = settings.wireframe; +} + +function onWindowResize() { + const w = window.innerWidth; + const h = window.innerHeight; + + renderer.setSize(w, h); + + camera.left = w / -2; + camera.right = w / 2; + camera.top = h / 2; + camera.bottom = h / -2; + + camera.updateProjectionMatrix(); +} + +function render() { + renderer.render(scene, camera); +} + +function animate() { + controls.update(); + + if (settings.autoRotate) { + meshFromSDF.rotation.y += Math.PI * 0.05 * clock.getDelta(); + } + + render(); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_geometry_shapes.ts b/examples-testing/examples/webgl_geometry_shapes.ts new file mode 100644 index 000000000..f1d00f011 --- /dev/null +++ b/examples-testing/examples/webgl_geometry_shapes.ts @@ -0,0 +1,363 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; + +let camera, scene, renderer; + +let group; + +let targetRotation = 0; +let targetRotationOnPointerDown = 0; + +let pointerX = 0; +let pointerXOnPointerDown = 0; + +let windowHalfX = window.innerWidth / 2; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 150, 500); + scene.add(camera); + + const light = new THREE.PointLight(0xffffff, 2.5, 0, 0); + camera.add(light); + + group = new THREE.Group(); + group.position.y = 50; + scene.add(group); + + const loader = new THREE.TextureLoader(); + const texture = loader.load('textures/uv_grid_opengl.jpg'); + texture.colorSpace = THREE.SRGBColorSpace; + + // it's necessary to apply these settings in order to correctly display the texture on a shape geometry + + texture.wrapS = texture.wrapT = THREE.RepeatWrapping; + texture.repeat.set(0.008, 0.008); + + function addShape(shape, extrudeSettings, color, x, y, z, rx, ry, rz, s) { + // flat shape with texture + // note: default UVs generated by THREE.ShapeGeometry are simply the x- and y-coordinates of the vertices + + let geometry = new THREE.ShapeGeometry(shape); + + let mesh = new THREE.Mesh(geometry, new THREE.MeshPhongMaterial({ side: THREE.DoubleSide, map: texture })); + mesh.position.set(x, y, z - 175); + mesh.rotation.set(rx, ry, rz); + mesh.scale.set(s, s, s); + group.add(mesh); + + // flat shape + + geometry = new THREE.ShapeGeometry(shape); + + mesh = new THREE.Mesh(geometry, new THREE.MeshPhongMaterial({ color: color, side: THREE.DoubleSide })); + mesh.position.set(x, y, z - 125); + mesh.rotation.set(rx, ry, rz); + mesh.scale.set(s, s, s); + group.add(mesh); + + // extruded shape + + geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings); + + mesh = new THREE.Mesh(geometry, new THREE.MeshPhongMaterial({ color: color })); + mesh.position.set(x, y, z - 75); + mesh.rotation.set(rx, ry, rz); + mesh.scale.set(s, s, s); + group.add(mesh); + + addLineShape(shape, color, x, y, z, rx, ry, rz, s); + } + + function addLineShape(shape, color, x, y, z, rx, ry, rz, s) { + // lines + + shape.autoClose = true; + + const points = shape.getPoints(); + const spacedPoints = shape.getSpacedPoints(50); + + const geometryPoints = new THREE.BufferGeometry().setFromPoints(points); + const geometrySpacedPoints = new THREE.BufferGeometry().setFromPoints(spacedPoints); + + // solid line + + let line = new THREE.Line(geometryPoints, new THREE.LineBasicMaterial({ color: color })); + line.position.set(x, y, z - 25); + line.rotation.set(rx, ry, rz); + line.scale.set(s, s, s); + group.add(line); + + // line from equidistance sampled points + + line = new THREE.Line(geometrySpacedPoints, new THREE.LineBasicMaterial({ color: color })); + line.position.set(x, y, z + 25); + line.rotation.set(rx, ry, rz); + line.scale.set(s, s, s); + group.add(line); + + // vertices from real points + + let particles = new THREE.Points(geometryPoints, new THREE.PointsMaterial({ color: color, size: 4 })); + particles.position.set(x, y, z + 75); + particles.rotation.set(rx, ry, rz); + particles.scale.set(s, s, s); + group.add(particles); + + // equidistance sampled points + + particles = new THREE.Points(geometrySpacedPoints, new THREE.PointsMaterial({ color: color, size: 4 })); + particles.position.set(x, y, z + 125); + particles.rotation.set(rx, ry, rz); + particles.scale.set(s, s, s); + group.add(particles); + } + + // California + + const californiaPts = []; + + californiaPts.push(new THREE.Vector2(610, 320)); + californiaPts.push(new THREE.Vector2(450, 300)); + californiaPts.push(new THREE.Vector2(392, 392)); + californiaPts.push(new THREE.Vector2(266, 438)); + californiaPts.push(new THREE.Vector2(190, 570)); + californiaPts.push(new THREE.Vector2(190, 600)); + californiaPts.push(new THREE.Vector2(160, 620)); + californiaPts.push(new THREE.Vector2(160, 650)); + californiaPts.push(new THREE.Vector2(180, 640)); + californiaPts.push(new THREE.Vector2(165, 680)); + californiaPts.push(new THREE.Vector2(150, 670)); + californiaPts.push(new THREE.Vector2(90, 737)); + californiaPts.push(new THREE.Vector2(80, 795)); + californiaPts.push(new THREE.Vector2(50, 835)); + californiaPts.push(new THREE.Vector2(64, 870)); + californiaPts.push(new THREE.Vector2(60, 945)); + californiaPts.push(new THREE.Vector2(300, 945)); + californiaPts.push(new THREE.Vector2(300, 743)); + californiaPts.push(new THREE.Vector2(600, 473)); + californiaPts.push(new THREE.Vector2(626, 425)); + californiaPts.push(new THREE.Vector2(600, 370)); + californiaPts.push(new THREE.Vector2(610, 320)); + + for (let i = 0; i < californiaPts.length; i++) californiaPts[i].multiplyScalar(0.25); + + const californiaShape = new THREE.Shape(californiaPts); + + // Triangle + + const triangleShape = new THREE.Shape().moveTo(80, 20).lineTo(40, 80).lineTo(120, 80).lineTo(80, 20); // close path + + // Heart + + const x = 0, + y = 0; + + const heartShape = new THREE.Shape() + .moveTo(x + 25, y + 25) + .bezierCurveTo(x + 25, y + 25, x + 20, y, x, y) + .bezierCurveTo(x - 30, y, x - 30, y + 35, x - 30, y + 35) + .bezierCurveTo(x - 30, y + 55, x - 10, y + 77, x + 25, y + 95) + .bezierCurveTo(x + 60, y + 77, x + 80, y + 55, x + 80, y + 35) + .bezierCurveTo(x + 80, y + 35, x + 80, y, x + 50, y) + .bezierCurveTo(x + 35, y, x + 25, y + 25, x + 25, y + 25); + + // Square + + const sqLength = 80; + + const squareShape = new THREE.Shape() + .moveTo(0, 0) + .lineTo(0, sqLength) + .lineTo(sqLength, sqLength) + .lineTo(sqLength, 0) + .lineTo(0, 0); + + // Rounded rectangle + + const roundedRectShape = new THREE.Shape(); + + (function roundedRect(ctx, x, y, width, height, radius) { + ctx.moveTo(x, y + radius); + ctx.lineTo(x, y + height - radius); + ctx.quadraticCurveTo(x, y + height, x + radius, y + height); + ctx.lineTo(x + width - radius, y + height); + ctx.quadraticCurveTo(x + width, y + height, x + width, y + height - radius); + ctx.lineTo(x + width, y + radius); + ctx.quadraticCurveTo(x + width, y, x + width - radius, y); + ctx.lineTo(x + radius, y); + ctx.quadraticCurveTo(x, y, x, y + radius); + })(roundedRectShape, 0, 0, 50, 50, 20); + + // Track + + const trackShape = new THREE.Shape() + .moveTo(40, 40) + .lineTo(40, 160) + .absarc(60, 160, 20, Math.PI, 0, true) + .lineTo(80, 40) + .absarc(60, 40, 20, 2 * Math.PI, Math.PI, true); + + // Circle + + const circleRadius = 40; + const circleShape = new THREE.Shape() + .moveTo(0, circleRadius) + .quadraticCurveTo(circleRadius, circleRadius, circleRadius, 0) + .quadraticCurveTo(circleRadius, -circleRadius, 0, -circleRadius) + .quadraticCurveTo(-circleRadius, -circleRadius, -circleRadius, 0) + .quadraticCurveTo(-circleRadius, circleRadius, 0, circleRadius); + + // Fish + + const fishShape = new THREE.Shape() + .moveTo(x, y) + .quadraticCurveTo(x + 50, y - 80, x + 90, y - 10) + .quadraticCurveTo(x + 100, y - 10, x + 115, y - 40) + .quadraticCurveTo(x + 115, y, x + 115, y + 40) + .quadraticCurveTo(x + 100, y + 10, x + 90, y + 10) + .quadraticCurveTo(x + 50, y + 80, x, y); + + // Arc circle + + const arcShape = new THREE.Shape().moveTo(50, 10).absarc(10, 10, 40, 0, Math.PI * 2, false); + + const holePath = new THREE.Path().moveTo(20, 10).absarc(10, 10, 10, 0, Math.PI * 2, true); + + arcShape.holes.push(holePath); + + // Smiley + + const smileyShape = new THREE.Shape().moveTo(80, 40).absarc(40, 40, 40, 0, Math.PI * 2, false); + + const smileyEye1Path = new THREE.Path().moveTo(35, 20).absellipse(25, 20, 10, 10, 0, Math.PI * 2, true); + + const smileyEye2Path = new THREE.Path().moveTo(65, 20).absarc(55, 20, 10, 0, Math.PI * 2, true); + + const smileyMouthPath = new THREE.Path() + .moveTo(20, 40) + .quadraticCurveTo(40, 60, 60, 40) + .bezierCurveTo(70, 45, 70, 50, 60, 60) + .quadraticCurveTo(40, 80, 20, 60) + .quadraticCurveTo(5, 50, 20, 40); + + smileyShape.holes.push(smileyEye1Path); + smileyShape.holes.push(smileyEye2Path); + smileyShape.holes.push(smileyMouthPath); + + // Spline shape + + const splinepts = []; + splinepts.push(new THREE.Vector2(70, 20)); + splinepts.push(new THREE.Vector2(80, 90)); + splinepts.push(new THREE.Vector2(-30, 70)); + splinepts.push(new THREE.Vector2(0, 0)); + + const splineShape = new THREE.Shape().moveTo(0, 0).splineThru(splinepts); + + const extrudeSettings = { + depth: 8, + bevelEnabled: true, + bevelSegments: 2, + steps: 2, + bevelSize: 1, + bevelThickness: 1, + }; + + // addShape( shape, color, x, y, z, rx, ry,rz, s ); + + addShape(californiaShape, extrudeSettings, 0xf08000, -300, -100, 0, 0, 0, 0, 1); + addShape(triangleShape, extrudeSettings, 0x8080f0, -180, 0, 0, 0, 0, 0, 1); + addShape(roundedRectShape, extrudeSettings, 0x008000, -150, 150, 0, 0, 0, 0, 1); + addShape(trackShape, extrudeSettings, 0x008080, 200, -100, 0, 0, 0, 0, 1); + addShape(squareShape, extrudeSettings, 0x0040f0, 150, 100, 0, 0, 0, 0, 1); + addShape(heartShape, extrudeSettings, 0xf00000, 60, 100, 0, 0, 0, Math.PI, 1); + addShape(circleShape, extrudeSettings, 0x00f000, 120, 250, 0, 0, 0, 0, 1); + addShape(fishShape, extrudeSettings, 0x404040, -60, 200, 0, 0, 0, 0, 1); + addShape(smileyShape, extrudeSettings, 0xf000f0, -200, 250, 0, 0, 0, Math.PI, 1); + addShape(arcShape, extrudeSettings, 0x804000, 150, 0, 0, 0, 0, 0, 1); + addShape(splineShape, extrudeSettings, 0x808080, -50, -100, 0, 0, 0, 0, 1); + + addLineShape(arcShape.holes[0], 0x804000, 150, 0, 0, 0, 0, 0, 1); + + for (let i = 0; i < smileyShape.holes.length; i += 1) { + addLineShape(smileyShape.holes[i], 0xf000f0, -200, 250, 0, 0, 0, Math.PI, 1); + } + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + container.style.touchAction = 'none'; + container.addEventListener('pointerdown', onPointerDown); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function onPointerDown(event) { + if (event.isPrimary === false) return; + + pointerXOnPointerDown = event.clientX - windowHalfX; + targetRotationOnPointerDown = targetRotation; + + document.addEventListener('pointermove', onPointerMove); + document.addEventListener('pointerup', onPointerUp); +} + +function onPointerMove(event) { + if (event.isPrimary === false) return; + + pointerX = event.clientX - windowHalfX; + + targetRotation = targetRotationOnPointerDown + (pointerX - pointerXOnPointerDown) * 0.02; +} + +function onPointerUp() { + if (event.isPrimary === false) return; + + document.removeEventListener('pointermove', onPointerMove); + document.removeEventListener('pointerup', onPointerUp); +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + group.rotation.y += (targetRotation - group.rotation.y) * 0.05; + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_geometry_teapot.ts b/examples-testing/examples/webgl_geometry_teapot.ts new file mode 100644 index 000000000..4c884a559 --- /dev/null +++ b/examples-testing/examples/webgl_geometry_teapot.ts @@ -0,0 +1,180 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js'; + +let camera, scene, renderer; +let cameraControls; +let effectController; +const teapotSize = 300; +let ambientLight, light; + +let tess = -1; // force initialization +let bBottom; +let bLid; +let bBody; +let bFitLid; +let bNonBlinn; +let shading; + +let teapot, textureCube; +const materials = {}; + +init(); +render(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + const canvasWidth = window.innerWidth; + const canvasHeight = window.innerHeight; + + // CAMERA + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 80000); + camera.position.set(-600, 550, 1300); + + // LIGHTS + ambientLight = new THREE.AmbientLight(0x7c7c7c, 3.0); + + light = new THREE.DirectionalLight(0xffffff, 3.0); + light.position.set(0.32, 0.39, 0.7); + + // RENDERER + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(canvasWidth, canvasHeight); + container.appendChild(renderer.domElement); + + // EVENTS + window.addEventListener('resize', onWindowResize); + + // CONTROLS + cameraControls = new OrbitControls(camera, renderer.domElement); + cameraControls.addEventListener('change', render); + + // TEXTURE MAP + const textureMap = new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg'); + textureMap.wrapS = textureMap.wrapT = THREE.RepeatWrapping; + textureMap.anisotropy = 16; + textureMap.colorSpace = THREE.SRGBColorSpace; + + // REFLECTION MAP + const path = 'textures/cube/pisa/'; + const urls = ['px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png']; + + textureCube = new THREE.CubeTextureLoader().setPath(path).load(urls); + + materials['wireframe'] = new THREE.MeshBasicMaterial({ wireframe: true }); + materials['flat'] = new THREE.MeshPhongMaterial({ specular: 0x000000, flatShading: true, side: THREE.DoubleSide }); + materials['smooth'] = new THREE.MeshLambertMaterial({ side: THREE.DoubleSide }); + materials['glossy'] = new THREE.MeshPhongMaterial({ side: THREE.DoubleSide }); + materials['textured'] = new THREE.MeshPhongMaterial({ map: textureMap, side: THREE.DoubleSide }); + materials['reflective'] = new THREE.MeshPhongMaterial({ envMap: textureCube, side: THREE.DoubleSide }); + + // scene itself + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xaaaaaa); + + scene.add(ambientLight); + scene.add(light); + + // GUI + setupGui(); +} + +// EVENT HANDLERS + +function onWindowResize() { + const canvasWidth = window.innerWidth; + const canvasHeight = window.innerHeight; + + renderer.setSize(canvasWidth, canvasHeight); + + camera.aspect = canvasWidth / canvasHeight; + camera.updateProjectionMatrix(); + + render(); +} + +function setupGui() { + effectController = { + newTess: 15, + bottom: true, + lid: true, + body: true, + fitLid: false, + nonblinn: false, + newShading: 'glossy', + }; + + const gui = new GUI(); + gui.add(effectController, 'newTess', [2, 3, 4, 5, 6, 8, 10, 15, 20, 30, 40, 50]) + .name('Tessellation Level') + .onChange(render); + gui.add(effectController, 'lid').name('display lid').onChange(render); + gui.add(effectController, 'body').name('display body').onChange(render); + gui.add(effectController, 'bottom').name('display bottom').onChange(render); + gui.add(effectController, 'fitLid').name('snug lid').onChange(render); + gui.add(effectController, 'nonblinn').name('original scale').onChange(render); + gui.add(effectController, 'newShading', ['wireframe', 'flat', 'smooth', 'glossy', 'textured', 'reflective']) + .name('Shading') + .onChange(render); +} + +// + +function render() { + if ( + effectController.newTess !== tess || + effectController.bottom !== bBottom || + effectController.lid !== bLid || + effectController.body !== bBody || + effectController.fitLid !== bFitLid || + effectController.nonblinn !== bNonBlinn || + effectController.newShading !== shading + ) { + tess = effectController.newTess; + bBottom = effectController.bottom; + bLid = effectController.lid; + bBody = effectController.body; + bFitLid = effectController.fitLid; + bNonBlinn = effectController.nonblinn; + shading = effectController.newShading; + + createNewTeapot(); + } + + // skybox is rendered separately, so that it is always behind the teapot. + if (shading === 'reflective') { + scene.background = textureCube; + } else { + scene.background = null; + } + + renderer.render(scene, camera); +} + +// Whenever the teapot changes, the scene is rebuilt from scratch (not much to it). +function createNewTeapot() { + if (teapot !== undefined) { + teapot.geometry.dispose(); + scene.remove(teapot); + } + + const geometry = new TeapotGeometry( + teapotSize, + tess, + effectController.bottom, + effectController.lid, + effectController.body, + effectController.fitLid, + !effectController.nonblinn, + ); + + teapot = new THREE.Mesh(geometry, materials[shading]); + + scene.add(teapot); +} diff --git a/examples-testing/examples/webgl_geometry_terrain.ts b/examples-testing/examples/webgl_geometry_terrain.ts new file mode 100644 index 000000000..8b6ed84ea --- /dev/null +++ b/examples-testing/examples/webgl_geometry_terrain.ts @@ -0,0 +1,173 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js'; +import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js'; + +let container, stats; +let camera, controls, scene, renderer; +let mesh, texture; + +const worldWidth = 256, + worldDepth = 256; +const clock = new THREE.Clock(); + +init(); + +function init() { + container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 10000); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xefd1b5); + scene.fog = new THREE.FogExp2(0xefd1b5, 0.0025); + + const data = generateHeight(worldWidth, worldDepth); + + camera.position.set(100, 800, -800); + camera.lookAt(-100, 810, -800); + + const geometry = new THREE.PlaneGeometry(7500, 7500, worldWidth - 1, worldDepth - 1); + geometry.rotateX(-Math.PI / 2); + + const vertices = geometry.attributes.position.array; + + for (let i = 0, j = 0, l = vertices.length; i < l; i++, j += 3) { + vertices[j + 1] = data[i] * 10; + } + + texture = new THREE.CanvasTexture(generateTexture(data, worldWidth, worldDepth)); + texture.wrapS = THREE.ClampToEdgeWrapping; + texture.wrapT = THREE.ClampToEdgeWrapping; + texture.colorSpace = THREE.SRGBColorSpace; + + mesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ map: texture })); + scene.add(mesh); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + controls = new FirstPersonControls(camera, renderer.domElement); + controls.movementSpeed = 150; + controls.lookSpeed = 0.1; + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + controls.handleResize(); +} + +function generateHeight(width, height) { + let seed = Math.PI / 4; + window.Math.random = function () { + const x = Math.sin(seed++) * 10000; + return x - Math.floor(x); + }; + + const size = width * height, + data = new Uint8Array(size); + const perlin = new ImprovedNoise(), + z = Math.random() * 100; + + let quality = 1; + + for (let j = 0; j < 4; j++) { + for (let i = 0; i < size; i++) { + const x = i % width, + y = ~~(i / width); + data[i] += Math.abs(perlin.noise(x / quality, y / quality, z) * quality * 1.75); + } + + quality *= 5; + } + + return data; +} + +function generateTexture(data, width, height) { + let context, image, imageData, shade; + + const vector3 = new THREE.Vector3(0, 0, 0); + + const sun = new THREE.Vector3(1, 1, 1); + sun.normalize(); + + const canvas = document.createElement('canvas'); + canvas.width = width; + canvas.height = height; + + context = canvas.getContext('2d'); + context.fillStyle = '#000'; + context.fillRect(0, 0, width, height); + + image = context.getImageData(0, 0, canvas.width, canvas.height); + imageData = image.data; + + for (let i = 0, j = 0, l = imageData.length; i < l; i += 4, j++) { + vector3.x = data[j - 2] - data[j + 2]; + vector3.y = 2; + vector3.z = data[j - width * 2] - data[j + width * 2]; + vector3.normalize(); + + shade = vector3.dot(sun); + + imageData[i] = (96 + shade * 128) * (0.5 + data[j] * 0.007); + imageData[i + 1] = (32 + shade * 96) * (0.5 + data[j] * 0.007); + imageData[i + 2] = shade * 96 * (0.5 + data[j] * 0.007); + } + + context.putImageData(image, 0, 0); + + // Scaled 4x + + const canvasScaled = document.createElement('canvas'); + canvasScaled.width = width * 4; + canvasScaled.height = height * 4; + + context = canvasScaled.getContext('2d'); + context.scale(4, 4); + context.drawImage(canvas, 0, 0); + + image = context.getImageData(0, 0, canvasScaled.width, canvasScaled.height); + imageData = image.data; + + for (let i = 0, l = imageData.length; i < l; i += 4) { + const v = ~~(Math.random() * 5); + + imageData[i] += v; + imageData[i + 1] += v; + imageData[i + 2] += v; + } + + context.putImageData(image, 0, 0); + + return canvasScaled; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + controls.update(clock.getDelta()); + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_geometry_terrain_raycast.ts b/examples-testing/examples/webgl_geometry_terrain_raycast.ts new file mode 100644 index 000000000..f1383c138 --- /dev/null +++ b/examples-testing/examples/webgl_geometry_terrain_raycast.ts @@ -0,0 +1,206 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js'; + +let container, stats; + +let camera, controls, scene, renderer; + +let mesh, texture; + +const worldWidth = 256, + worldDepth = 256, + worldHalfWidth = worldWidth / 2, + worldHalfDepth = worldDepth / 2; + +let helper; + +const raycaster = new THREE.Raycaster(); +const pointer = new THREE.Vector2(); + +init(); + +function init() { + container = document.getElementById('container'); + container.innerHTML = ''; + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xbfd1e5); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 10, 20000); + + controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 1000; + controls.maxDistance = 10000; + controls.maxPolarAngle = Math.PI / 2; + + // + + const data = generateHeight(worldWidth, worldDepth); + + controls.target.y = data[worldHalfWidth + worldHalfDepth * worldWidth] + 500; + camera.position.y = controls.target.y + 2000; + camera.position.x = 2000; + controls.update(); + + const geometry = new THREE.PlaneGeometry(7500, 7500, worldWidth - 1, worldDepth - 1); + geometry.rotateX(-Math.PI / 2); + + const vertices = geometry.attributes.position.array; + + for (let i = 0, j = 0, l = vertices.length; i < l; i++, j += 3) { + vertices[j + 1] = data[i] * 10; + } + + // + + texture = new THREE.CanvasTexture(generateTexture(data, worldWidth, worldDepth)); + texture.wrapS = THREE.ClampToEdgeWrapping; + texture.wrapT = THREE.ClampToEdgeWrapping; + texture.colorSpace = THREE.SRGBColorSpace; + + mesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ map: texture })); + scene.add(mesh); + + const geometryHelper = new THREE.ConeGeometry(20, 100, 3); + geometryHelper.translate(0, 50, 0); + geometryHelper.rotateX(Math.PI / 2); + helper = new THREE.Mesh(geometryHelper, new THREE.MeshNormalMaterial()); + scene.add(helper); + + container.addEventListener('pointermove', onPointerMove); + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function generateHeight(width, height) { + const size = width * height, + data = new Uint8Array(size), + perlin = new ImprovedNoise(), + z = Math.random() * 100; + + let quality = 1; + + for (let j = 0; j < 4; j++) { + for (let i = 0; i < size; i++) { + const x = i % width, + y = ~~(i / width); + data[i] += Math.abs(perlin.noise(x / quality, y / quality, z) * quality * 1.75); + } + + quality *= 5; + } + + return data; +} + +function generateTexture(data, width, height) { + // bake lighting into texture + + let context, image, imageData, shade; + + const vector3 = new THREE.Vector3(0, 0, 0); + + const sun = new THREE.Vector3(1, 1, 1); + sun.normalize(); + + const canvas = document.createElement('canvas'); + canvas.width = width; + canvas.height = height; + + context = canvas.getContext('2d'); + context.fillStyle = '#000'; + context.fillRect(0, 0, width, height); + + image = context.getImageData(0, 0, canvas.width, canvas.height); + imageData = image.data; + + for (let i = 0, j = 0, l = imageData.length; i < l; i += 4, j++) { + vector3.x = data[j - 2] - data[j + 2]; + vector3.y = 2; + vector3.z = data[j - width * 2] - data[j + width * 2]; + vector3.normalize(); + + shade = vector3.dot(sun); + + imageData[i] = (96 + shade * 128) * (0.5 + data[j] * 0.007); + imageData[i + 1] = (32 + shade * 96) * (0.5 + data[j] * 0.007); + imageData[i + 2] = shade * 96 * (0.5 + data[j] * 0.007); + } + + context.putImageData(image, 0, 0); + + // Scaled 4x + + const canvasScaled = document.createElement('canvas'); + canvasScaled.width = width * 4; + canvasScaled.height = height * 4; + + context = canvasScaled.getContext('2d'); + context.scale(4, 4); + context.drawImage(canvas, 0, 0); + + image = context.getImageData(0, 0, canvasScaled.width, canvasScaled.height); + imageData = image.data; + + for (let i = 0, l = imageData.length; i < l; i += 4) { + const v = ~~(Math.random() * 5); + + imageData[i] += v; + imageData[i + 1] += v; + imageData[i + 2] += v; + } + + context.putImageData(image, 0, 0); + + return canvasScaled; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + renderer.render(scene, camera); +} + +function onPointerMove(event) { + pointer.x = (event.clientX / renderer.domElement.clientWidth) * 2 - 1; + pointer.y = -(event.clientY / renderer.domElement.clientHeight) * 2 + 1; + raycaster.setFromCamera(pointer, camera); + + // See if the ray from the camera into the world hits one of our meshes + const intersects = raycaster.intersectObject(mesh); + + // Toggle rotation bool for meshes that we clicked + if (intersects.length > 0) { + helper.position.set(0, 0, 0); + helper.lookAt(intersects[0].face.normal); + + helper.position.copy(intersects[0].point); + } +} diff --git a/examples-testing/examples/webgl_geometry_text.ts b/examples-testing/examples/webgl_geometry_text.ts new file mode 100644 index 000000000..831ebcd6b --- /dev/null +++ b/examples-testing/examples/webgl_geometry_text.ts @@ -0,0 +1,312 @@ +import * as THREE from 'three'; + +import { FontLoader } from 'three/addons/loaders/FontLoader.js'; +import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +THREE.Cache.enabled = true; + +let container; + +let camera, cameraTarget, scene, renderer; + +let group, textMesh1, textMesh2, textGeo, materials; + +let firstLetter = true; + +let text = 'three.js', + bevelEnabled = true, + font = undefined, + fontName = 'optimer', // helvetiker, optimer, gentilis, droid sans, droid serif + fontWeight = 'bold'; // normal bold + +const depth = 20, + size = 70, + hover = 30, + curveSegments = 4, + bevelThickness = 2, + bevelSize = 1.5; + +const mirror = true; + +const fontMap = { + helvetiker: 0, + optimer: 1, + gentilis: 2, + 'droid/droid_sans': 3, + 'droid/droid_serif': 4, +}; + +const weightMap = { + regular: 0, + bold: 1, +}; + +const reverseFontMap = []; +const reverseWeightMap = []; + +for (const i in fontMap) reverseFontMap[fontMap[i]] = i; +for (const i in weightMap) reverseWeightMap[weightMap[i]] = i; + +let targetRotation = 0; +let targetRotationOnPointerDown = 0; + +let pointerX = 0; +let pointerXOnPointerDown = 0; + +let windowHalfX = window.innerWidth / 2; + +let fontIndex = 1; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + // CAMERA + + camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 1500); + camera.position.set(0, 400, 700); + + cameraTarget = new THREE.Vector3(0, 150, 0); + + // SCENE + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x000000); + scene.fog = new THREE.Fog(0x000000, 250, 1400); + + // LIGHTS + + const dirLight = new THREE.DirectionalLight(0xffffff, 0.4); + dirLight.position.set(0, 0, 1).normalize(); + scene.add(dirLight); + + const pointLight = new THREE.PointLight(0xffffff, 4.5, 0, 0); + pointLight.color.setHSL(Math.random(), 1, 0.5); + pointLight.position.set(0, 100, 90); + scene.add(pointLight); + + materials = [ + new THREE.MeshPhongMaterial({ color: 0xffffff, flatShading: true }), // front + new THREE.MeshPhongMaterial({ color: 0xffffff }), // side + ]; + + group = new THREE.Group(); + group.position.y = 100; + + scene.add(group); + + loadFont(); + + const plane = new THREE.Mesh( + new THREE.PlaneGeometry(10000, 10000), + new THREE.MeshBasicMaterial({ color: 0xffffff, opacity: 0.5, transparent: true }), + ); + plane.position.y = 100; + plane.rotation.x = -Math.PI / 2; + scene.add(plane); + + // RENDERER + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // EVENTS + + container.style.touchAction = 'none'; + container.addEventListener('pointerdown', onPointerDown); + + document.addEventListener('keypress', onDocumentKeyPress); + document.addEventListener('keydown', onDocumentKeyDown); + + // + + const params = { + changeColor: function () { + pointLight.color.setHSL(Math.random(), 1, 0.5); + }, + changeFont: function () { + fontIndex++; + + fontName = reverseFontMap[fontIndex % reverseFontMap.length]; + + loadFont(); + }, + changeWeight: function () { + if (fontWeight === 'bold') { + fontWeight = 'regular'; + } else { + fontWeight = 'bold'; + } + + loadFont(); + }, + changeBevel: function () { + bevelEnabled = !bevelEnabled; + + refreshText(); + }, + }; + + // + + const gui = new GUI(); + + gui.add(params, 'changeColor').name('change color'); + gui.add(params, 'changeFont').name('change font'); + gui.add(params, 'changeWeight').name('change weight'); + gui.add(params, 'changeBevel').name('change bevel'); + gui.open(); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function onDocumentKeyDown(event) { + if (firstLetter) { + firstLetter = false; + text = ''; + } + + const keyCode = event.keyCode; + + // backspace + + if (keyCode == 8) { + event.preventDefault(); + + text = text.substring(0, text.length - 1); + refreshText(); + + return false; + } +} + +function onDocumentKeyPress(event) { + const keyCode = event.which; + + // backspace + + if (keyCode == 8) { + event.preventDefault(); + } else { + const ch = String.fromCharCode(keyCode); + text += ch; + + refreshText(); + } +} + +function loadFont() { + const loader = new FontLoader(); + loader.load('fonts/' + fontName + '_' + fontWeight + '.typeface.json', function (response) { + font = response; + + refreshText(); + }); +} + +function createText() { + textGeo = new TextGeometry(text, { + font: font, + + size: size, + depth: depth, + curveSegments: curveSegments, + + bevelThickness: bevelThickness, + bevelSize: bevelSize, + bevelEnabled: bevelEnabled, + }); + + textGeo.computeBoundingBox(); + + const centerOffset = -0.5 * (textGeo.boundingBox.max.x - textGeo.boundingBox.min.x); + + textMesh1 = new THREE.Mesh(textGeo, materials); + + textMesh1.position.x = centerOffset; + textMesh1.position.y = hover; + textMesh1.position.z = 0; + + textMesh1.rotation.x = 0; + textMesh1.rotation.y = Math.PI * 2; + + group.add(textMesh1); + + if (mirror) { + textMesh2 = new THREE.Mesh(textGeo, materials); + + textMesh2.position.x = centerOffset; + textMesh2.position.y = -hover; + textMesh2.position.z = depth; + + textMesh2.rotation.x = Math.PI; + textMesh2.rotation.y = Math.PI * 2; + + group.add(textMesh2); + } +} + +function refreshText() { + group.remove(textMesh1); + if (mirror) group.remove(textMesh2); + + if (!text) return; + + createText(); +} + +function onPointerDown(event) { + if (event.isPrimary === false) return; + + pointerXOnPointerDown = event.clientX - windowHalfX; + targetRotationOnPointerDown = targetRotation; + + document.addEventListener('pointermove', onPointerMove); + document.addEventListener('pointerup', onPointerUp); +} + +function onPointerMove(event) { + if (event.isPrimary === false) return; + + pointerX = event.clientX - windowHalfX; + + targetRotation = targetRotationOnPointerDown + (pointerX - pointerXOnPointerDown) * 0.02; +} + +function onPointerUp() { + if (event.isPrimary === false) return; + + document.removeEventListener('pointermove', onPointerMove); + document.removeEventListener('pointerup', onPointerUp); +} + +// + +function animate() { + group.rotation.y += (targetRotation - group.rotation.y) * 0.05; + + camera.lookAt(cameraTarget); + + renderer.clear(); + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_geometry_text_shapes.ts b/examples-testing/examples/webgl_geometry_text_shapes.ts new file mode 100644 index 000000000..adfb6008d --- /dev/null +++ b/examples-testing/examples/webgl_geometry_text_shapes.ts @@ -0,0 +1,112 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { FontLoader } from 'three/addons/loaders/FontLoader.js'; + +let camera, scene, renderer; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.set(0, -400, 600); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + const loader = new FontLoader(); + loader.load('fonts/helvetiker_regular.typeface.json', function (font) { + const color = 0x006699; + + const matDark = new THREE.LineBasicMaterial({ + color: color, + side: THREE.DoubleSide, + }); + + const matLite = new THREE.MeshBasicMaterial({ + color: color, + transparent: true, + opacity: 0.4, + side: THREE.DoubleSide, + }); + + const message = ' Three.js\nSimple text.'; + + const shapes = font.generateShapes(message, 100); + + const geometry = new THREE.ShapeGeometry(shapes); + + geometry.computeBoundingBox(); + + const xMid = -0.5 * (geometry.boundingBox.max.x - geometry.boundingBox.min.x); + + geometry.translate(xMid, 0, 0); + + // make shape ( N.B. edge view not visible ) + + const text = new THREE.Mesh(geometry, matLite); + text.position.z = -150; + scene.add(text); + + // make line shape ( N.B. edge view remains visible ) + + const holeShapes = []; + + for (let i = 0; i < shapes.length; i++) { + const shape = shapes[i]; + + if (shape.holes && shape.holes.length > 0) { + for (let j = 0; j < shape.holes.length; j++) { + const hole = shape.holes[j]; + holeShapes.push(hole); + } + } + } + + shapes.push.apply(shapes, holeShapes); + + const lineText = new THREE.Object3D(); + + for (let i = 0; i < shapes.length; i++) { + const shape = shapes[i]; + + const points = shape.getPoints(); + const geometry = new THREE.BufferGeometry().setFromPoints(points); + + geometry.translate(xMid, 0, 0); + + const lineMesh = new THREE.Line(geometry, matDark); + lineText.add(lineMesh); + } + + scene.add(lineText); + + render(); + }); //end load function + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 0, 0); + controls.update(); + + controls.addEventListener('change', render); + + window.addEventListener('resize', onWindowResize); +} // end init + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_geometry_text_stroke.ts b/examples-testing/examples/webgl_geometry_text_stroke.ts new file mode 100644 index 000000000..9a1983253 --- /dev/null +++ b/examples-testing/examples/webgl_geometry_text_stroke.ts @@ -0,0 +1,116 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { SVGLoader } from 'three/addons/loaders/SVGLoader.js'; +import { FontLoader } from 'three/addons/loaders/FontLoader.js'; + +let camera, scene, renderer; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.set(0, -400, 600); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + const loader = new FontLoader(); + loader.load('fonts/helvetiker_regular.typeface.json', function (font) { + const color = new THREE.Color(0x006699); + + const matDark = new THREE.MeshBasicMaterial({ + color: color, + side: THREE.DoubleSide, + }); + + const matLite = new THREE.MeshBasicMaterial({ + color: color, + transparent: true, + opacity: 0.4, + side: THREE.DoubleSide, + }); + + const message = ' Three.js\nStroke text.'; + + const shapes = font.generateShapes(message, 100); + + const geometry = new THREE.ShapeGeometry(shapes); + + geometry.computeBoundingBox(); + + const xMid = -0.5 * (geometry.boundingBox.max.x - geometry.boundingBox.min.x); + + geometry.translate(xMid, 0, 0); + + // make shape ( N.B. edge view not visible ) + + const text = new THREE.Mesh(geometry, matLite); + text.position.z = -150; + scene.add(text); + + // make line shape ( N.B. edge view remains visible ) + + const holeShapes = []; + + for (let i = 0; i < shapes.length; i++) { + const shape = shapes[i]; + + if (shape.holes && shape.holes.length > 0) { + for (let j = 0; j < shape.holes.length; j++) { + const hole = shape.holes[j]; + holeShapes.push(hole); + } + } + } + + shapes.push.apply(shapes, holeShapes); + + const style = SVGLoader.getStrokeStyle(5, color.getStyle()); + + const strokeText = new THREE.Group(); + + for (let i = 0; i < shapes.length; i++) { + const shape = shapes[i]; + + const points = shape.getPoints(); + + const geometry = SVGLoader.pointsToStroke(points, style); + + geometry.translate(xMid, 0, 0); + + const strokeMesh = new THREE.Mesh(geometry, matDark); + strokeText.add(strokeMesh); + } + + scene.add(strokeText); + + render(); + }); //end load function + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 0, 0); + controls.update(); + + controls.addEventListener('change', render); + + window.addEventListener('resize', onWindowResize); +} // end init + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_gpgpu_birds.ts b/examples-testing/examples/webgl_gpgpu_birds.ts new file mode 100644 index 000000000..20a5e0d97 --- /dev/null +++ b/examples-testing/examples/webgl_gpgpu_birds.ts @@ -0,0 +1,313 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { GPUComputationRenderer } from 'three/addons/misc/GPUComputationRenderer.js'; + +/* TEXTURE WIDTH FOR SIMULATION */ +const WIDTH = 32; + +const BIRDS = WIDTH * WIDTH; + +// Custom Geometry - using 3 triangles each. No UVs, no normals currently. +class BirdGeometry extends THREE.BufferGeometry { + constructor() { + super(); + + const trianglesPerBird = 3; + const triangles = BIRDS * trianglesPerBird; + const points = triangles * 3; + + const vertices = new THREE.BufferAttribute(new Float32Array(points * 3), 3); + const birdColors = new THREE.BufferAttribute(new Float32Array(points * 3), 3); + const references = new THREE.BufferAttribute(new Float32Array(points * 2), 2); + const birdVertex = new THREE.BufferAttribute(new Float32Array(points), 1); + + this.setAttribute('position', vertices); + this.setAttribute('birdColor', birdColors); + this.setAttribute('reference', references); + this.setAttribute('birdVertex', birdVertex); + + // this.setAttribute( 'normal', new Float32Array( points * 3 ), 3 ); + + let v = 0; + + function verts_push() { + for (let i = 0; i < arguments.length; i++) { + vertices.array[v++] = arguments[i]; + } + } + + const wingsSpan = 20; + + for (let f = 0; f < BIRDS; f++) { + // Body + + verts_push(0, -0, -20, 0, 4, -20, 0, 0, 30); + + // Wings + + verts_push(0, 0, -15, -wingsSpan, 0, 0, 0, 0, 15); + + verts_push(0, 0, 15, wingsSpan, 0, 0, 0, 0, -15); + } + + for (let v = 0; v < triangles * 3; v++) { + const triangleIndex = ~~(v / 3); + const birdIndex = ~~(triangleIndex / trianglesPerBird); + const x = (birdIndex % WIDTH) / WIDTH; + const y = ~~(birdIndex / WIDTH) / WIDTH; + + const c = new THREE.Color(0x666666 + (~~(v / 9) / BIRDS) * 0x666666); + + birdColors.array[v * 3 + 0] = c.r; + birdColors.array[v * 3 + 1] = c.g; + birdColors.array[v * 3 + 2] = c.b; + + references.array[v * 2] = x; + references.array[v * 2 + 1] = y; + + birdVertex.array[v] = v % 9; + } + + this.scale(0.2, 0.2, 0.2); + } +} + +// + +let container, stats; +let camera, scene, renderer; +let mouseX = 0, + mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +const BOUNDS = 800, + BOUNDS_HALF = BOUNDS / 2; + +let last = performance.now(); + +let gpuCompute; +let velocityVariable; +let positionVariable; +let positionUniforms; +let velocityUniforms; +let birdUniforms; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 3000); + camera.position.z = 350; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + scene.fog = new THREE.Fog(0xffffff, 100, 1000); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + initComputeRenderer(); + + stats = new Stats(); + container.appendChild(stats.dom); + + container.style.touchAction = 'none'; + container.addEventListener('pointermove', onPointerMove); + + // + + window.addEventListener('resize', onWindowResize); + + const gui = new GUI(); + + const effectController = { + separation: 20.0, + alignment: 20.0, + cohesion: 20.0, + freedom: 0.75, + }; + + const valuesChanger = function () { + velocityUniforms['separationDistance'].value = effectController.separation; + velocityUniforms['alignmentDistance'].value = effectController.alignment; + velocityUniforms['cohesionDistance'].value = effectController.cohesion; + velocityUniforms['freedomFactor'].value = effectController.freedom; + }; + + valuesChanger(); + + gui.add(effectController, 'separation', 0.0, 100.0, 1.0).onChange(valuesChanger); + gui.add(effectController, 'alignment', 0.0, 100, 0.001).onChange(valuesChanger); + gui.add(effectController, 'cohesion', 0.0, 100, 0.025).onChange(valuesChanger); + gui.close(); + + initBirds(); +} + +function initComputeRenderer() { + gpuCompute = new GPUComputationRenderer(WIDTH, WIDTH, renderer); + + const dtPosition = gpuCompute.createTexture(); + const dtVelocity = gpuCompute.createTexture(); + fillPositionTexture(dtPosition); + fillVelocityTexture(dtVelocity); + + velocityVariable = gpuCompute.addVariable( + 'textureVelocity', + document.getElementById('fragmentShaderVelocity').textContent, + dtVelocity, + ); + positionVariable = gpuCompute.addVariable( + 'texturePosition', + document.getElementById('fragmentShaderPosition').textContent, + dtPosition, + ); + + gpuCompute.setVariableDependencies(velocityVariable, [positionVariable, velocityVariable]); + gpuCompute.setVariableDependencies(positionVariable, [positionVariable, velocityVariable]); + + positionUniforms = positionVariable.material.uniforms; + velocityUniforms = velocityVariable.material.uniforms; + + positionUniforms['time'] = { value: 0.0 }; + positionUniforms['delta'] = { value: 0.0 }; + velocityUniforms['time'] = { value: 1.0 }; + velocityUniforms['delta'] = { value: 0.0 }; + velocityUniforms['testing'] = { value: 1.0 }; + velocityUniforms['separationDistance'] = { value: 1.0 }; + velocityUniforms['alignmentDistance'] = { value: 1.0 }; + velocityUniforms['cohesionDistance'] = { value: 1.0 }; + velocityUniforms['freedomFactor'] = { value: 1.0 }; + velocityUniforms['predator'] = { value: new THREE.Vector3() }; + velocityVariable.material.defines.BOUNDS = BOUNDS.toFixed(2); + + velocityVariable.wrapS = THREE.RepeatWrapping; + velocityVariable.wrapT = THREE.RepeatWrapping; + positionVariable.wrapS = THREE.RepeatWrapping; + positionVariable.wrapT = THREE.RepeatWrapping; + + const error = gpuCompute.init(); + + if (error !== null) { + console.error(error); + } +} + +function initBirds() { + const geometry = new BirdGeometry(); + + // For Vertex and Fragment + birdUniforms = { + color: { value: new THREE.Color(0xff2200) }, + texturePosition: { value: null }, + textureVelocity: { value: null }, + time: { value: 1.0 }, + delta: { value: 0.0 }, + }; + + // THREE.ShaderMaterial + const material = new THREE.ShaderMaterial({ + uniforms: birdUniforms, + vertexShader: document.getElementById('birdVS').textContent, + fragmentShader: document.getElementById('birdFS').textContent, + side: THREE.DoubleSide, + }); + + const birdMesh = new THREE.Mesh(geometry, material); + birdMesh.rotation.y = Math.PI / 2; + birdMesh.matrixAutoUpdate = false; + birdMesh.updateMatrix(); + + scene.add(birdMesh); +} + +function fillPositionTexture(texture) { + const theArray = texture.image.data; + + for (let k = 0, kl = theArray.length; k < kl; k += 4) { + const x = Math.random() * BOUNDS - BOUNDS_HALF; + const y = Math.random() * BOUNDS - BOUNDS_HALF; + const z = Math.random() * BOUNDS - BOUNDS_HALF; + + theArray[k + 0] = x; + theArray[k + 1] = y; + theArray[k + 2] = z; + theArray[k + 3] = 1; + } +} + +function fillVelocityTexture(texture) { + const theArray = texture.image.data; + + for (let k = 0, kl = theArray.length; k < kl; k += 4) { + const x = Math.random() - 0.5; + const y = Math.random() - 0.5; + const z = Math.random() - 0.5; + + theArray[k + 0] = x * 10; + theArray[k + 1] = y * 10; + theArray[k + 2] = z * 10; + theArray[k + 3] = 1; + } +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onPointerMove(event) { + if (event.isPrimary === false) return; + + mouseX = event.clientX - windowHalfX; + mouseY = event.clientY - windowHalfY; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + const now = performance.now(); + let delta = (now - last) / 1000; + + if (delta > 1) delta = 1; // safety cap on large deltas + last = now; + + positionUniforms['time'].value = now; + positionUniforms['delta'].value = delta; + velocityUniforms['time'].value = now; + velocityUniforms['delta'].value = delta; + birdUniforms['time'].value = now; + birdUniforms['delta'].value = delta; + + velocityUniforms['predator'].value.set((0.5 * mouseX) / windowHalfX, (-0.5 * mouseY) / windowHalfY, 0); + + mouseX = 10000; + mouseY = 10000; + + gpuCompute.compute(); + + birdUniforms['texturePosition'].value = gpuCompute.getCurrentRenderTarget(positionVariable).texture; + birdUniforms['textureVelocity'].value = gpuCompute.getCurrentRenderTarget(velocityVariable).texture; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_gpgpu_birds_gltf.ts b/examples-testing/examples/webgl_gpgpu_birds_gltf.ts new file mode 100644 index 000000000..3176b95a9 --- /dev/null +++ b/examples-testing/examples/webgl_gpgpu_birds_gltf.ts @@ -0,0 +1,415 @@ +import * as THREE from 'three'; +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { GPUComputationRenderer } from 'three/addons/misc/GPUComputationRenderer.js'; + +/* TEXTURE WIDTH FOR SIMULATION */ +const WIDTH = 64; +const BIRDS = WIDTH * WIDTH; + +/* BAKE ANIMATION INTO TEXTURE and CREATE GEOMETRY FROM BASE MODEL */ +const BirdGeometry = new THREE.BufferGeometry(); +let textureAnimation, durationAnimation, birdMesh, materialShader, indicesPerBird; + +function nextPowerOf2(n) { + return Math.pow(2, Math.ceil(Math.log(n) / Math.log(2))); +} + +Math.lerp = function (value1, value2, amount) { + amount = Math.max(Math.min(amount, 1), 0); + return value1 + (value2 - value1) * amount; +}; + +const gltfs = ['models/gltf/Parrot.glb', 'models/gltf/Flamingo.glb']; +const colors = [0xccffff, 0xffdeff]; +const sizes = [0.2, 0.1]; +const selectModel = Math.floor(Math.random() * gltfs.length); +new GLTFLoader().load(gltfs[selectModel], function (gltf) { + const animations = gltf.animations; + durationAnimation = Math.round(animations[0].duration * 60); + const birdGeo = gltf.scene.children[0].geometry; + const morphAttributes = birdGeo.morphAttributes.position; + const tHeight = nextPowerOf2(durationAnimation); + const tWidth = nextPowerOf2(birdGeo.getAttribute('position').count); + indicesPerBird = birdGeo.index.count; + const tData = new Float32Array(4 * tWidth * tHeight); + + for (let i = 0; i < tWidth; i++) { + for (let j = 0; j < tHeight; j++) { + const offset = j * tWidth * 4; + + const curMorph = Math.floor((j / durationAnimation) * morphAttributes.length); + const nextMorph = + (Math.floor((j / durationAnimation) * morphAttributes.length) + 1) % morphAttributes.length; + const lerpAmount = ((j / durationAnimation) * morphAttributes.length) % 1; + + if (j < durationAnimation) { + let d0, d1; + + d0 = morphAttributes[curMorph].array[i * 3]; + d1 = morphAttributes[nextMorph].array[i * 3]; + + if (d0 !== undefined && d1 !== undefined) tData[offset + i * 4] = Math.lerp(d0, d1, lerpAmount); + + d0 = morphAttributes[curMorph].array[i * 3 + 1]; + d1 = morphAttributes[nextMorph].array[i * 3 + 1]; + + if (d0 !== undefined && d1 !== undefined) tData[offset + i * 4 + 1] = Math.lerp(d0, d1, lerpAmount); + + d0 = morphAttributes[curMorph].array[i * 3 + 2]; + d1 = morphAttributes[nextMorph].array[i * 3 + 2]; + + if (d0 !== undefined && d1 !== undefined) tData[offset + i * 4 + 2] = Math.lerp(d0, d1, lerpAmount); + + tData[offset + i * 4 + 3] = 1; + } + } + } + + textureAnimation = new THREE.DataTexture(tData, tWidth, tHeight, THREE.RGBAFormat, THREE.FloatType); + textureAnimation.needsUpdate = true; + + const vertices = [], + color = [], + reference = [], + seeds = [], + indices = []; + const totalVertices = birdGeo.getAttribute('position').count * 3 * BIRDS; + for (let i = 0; i < totalVertices; i++) { + const bIndex = i % (birdGeo.getAttribute('position').count * 3); + vertices.push(birdGeo.getAttribute('position').array[bIndex]); + color.push(birdGeo.getAttribute('color').array[bIndex]); + } + + let r = Math.random(); + for (let i = 0; i < birdGeo.getAttribute('position').count * BIRDS; i++) { + const bIndex = i % birdGeo.getAttribute('position').count; + const bird = Math.floor(i / birdGeo.getAttribute('position').count); + if (bIndex == 0) r = Math.random(); + const j = ~~bird; + const x = (j % WIDTH) / WIDTH; + const y = ~~(j / WIDTH) / WIDTH; + reference.push(x, y, bIndex / tWidth, durationAnimation / tHeight); + seeds.push(bird, r, Math.random(), Math.random()); + } + + for (let i = 0; i < birdGeo.index.array.length * BIRDS; i++) { + const offset = Math.floor(i / birdGeo.index.array.length) * birdGeo.getAttribute('position').count; + indices.push(birdGeo.index.array[i % birdGeo.index.array.length] + offset); + } + + BirdGeometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(vertices), 3)); + BirdGeometry.setAttribute('birdColor', new THREE.BufferAttribute(new Float32Array(color), 3)); + BirdGeometry.setAttribute('color', new THREE.BufferAttribute(new Float32Array(color), 3)); + BirdGeometry.setAttribute('reference', new THREE.BufferAttribute(new Float32Array(reference), 4)); + BirdGeometry.setAttribute('seeds', new THREE.BufferAttribute(new Float32Array(seeds), 4)); + + BirdGeometry.setIndex(indices); + + init(); +}); + +let container, stats; +let camera, scene, renderer; +let mouseX = 0, + mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +const BOUNDS = 800, + BOUNDS_HALF = BOUNDS / 2; + +let last = performance.now(); + +let gpuCompute; +let velocityVariable; +let positionVariable; +let positionUniforms; +let velocityUniforms; + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 3000); + camera.position.z = 350; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(colors[selectModel]); + scene.fog = new THREE.Fog(colors[selectModel], 100, 1000); + + // LIGHTS + + const hemiLight = new THREE.HemisphereLight(colors[selectModel], 0xffffff, 4.5); + hemiLight.color.setHSL(0.6, 1, 0.6, THREE.SRGBColorSpace); + hemiLight.groundColor.setHSL(0.095, 1, 0.75, THREE.SRGBColorSpace); + hemiLight.position.set(0, 50, 0); + scene.add(hemiLight); + + const dirLight = new THREE.DirectionalLight(0x00ced1, 2.0); + dirLight.color.setHSL(0.1, 1, 0.95, THREE.SRGBColorSpace); + dirLight.position.set(-1, 1.75, 1); + dirLight.position.multiplyScalar(30); + scene.add(dirLight); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + initComputeRenderer(); + + stats = new Stats(); + container.appendChild(stats.dom); + + container.style.touchAction = 'none'; + container.addEventListener('pointermove', onPointerMove); + + window.addEventListener('resize', onWindowResize); + + const gui = new GUI(); + + const effectController = { + separation: 20.0, + alignment: 20.0, + cohesion: 20.0, + freedom: 0.75, + size: sizes[selectModel], + count: Math.floor(BIRDS / 4), + }; + + const valuesChanger = function () { + velocityUniforms['separationDistance'].value = effectController.separation; + velocityUniforms['alignmentDistance'].value = effectController.alignment; + velocityUniforms['cohesionDistance'].value = effectController.cohesion; + velocityUniforms['freedomFactor'].value = effectController.freedom; + if (materialShader) materialShader.uniforms['size'].value = effectController.size; + BirdGeometry.setDrawRange(0, indicesPerBird * effectController.count); + }; + + valuesChanger(); + + gui.add(effectController, 'separation', 0.0, 100.0, 1.0).onChange(valuesChanger); + gui.add(effectController, 'alignment', 0.0, 100, 0.001).onChange(valuesChanger); + gui.add(effectController, 'cohesion', 0.0, 100, 0.025).onChange(valuesChanger); + gui.add(effectController, 'size', 0, 1, 0.01).onChange(valuesChanger); + gui.add(effectController, 'count', 0, BIRDS, 1).onChange(valuesChanger); + gui.close(); + + initBirds(effectController); +} + +function initComputeRenderer() { + gpuCompute = new GPUComputationRenderer(WIDTH, WIDTH, renderer); + + const dtPosition = gpuCompute.createTexture(); + const dtVelocity = gpuCompute.createTexture(); + fillPositionTexture(dtPosition); + fillVelocityTexture(dtVelocity); + + velocityVariable = gpuCompute.addVariable( + 'textureVelocity', + document.getElementById('fragmentShaderVelocity').textContent, + dtVelocity, + ); + positionVariable = gpuCompute.addVariable( + 'texturePosition', + document.getElementById('fragmentShaderPosition').textContent, + dtPosition, + ); + + gpuCompute.setVariableDependencies(velocityVariable, [positionVariable, velocityVariable]); + gpuCompute.setVariableDependencies(positionVariable, [positionVariable, velocityVariable]); + + positionUniforms = positionVariable.material.uniforms; + velocityUniforms = velocityVariable.material.uniforms; + + positionUniforms['time'] = { value: 0.0 }; + positionUniforms['delta'] = { value: 0.0 }; + velocityUniforms['time'] = { value: 1.0 }; + velocityUniforms['delta'] = { value: 0.0 }; + velocityUniforms['testing'] = { value: 1.0 }; + velocityUniforms['separationDistance'] = { value: 1.0 }; + velocityUniforms['alignmentDistance'] = { value: 1.0 }; + velocityUniforms['cohesionDistance'] = { value: 1.0 }; + velocityUniforms['freedomFactor'] = { value: 1.0 }; + velocityUniforms['predator'] = { value: new THREE.Vector3() }; + velocityVariable.material.defines.BOUNDS = BOUNDS.toFixed(2); + + velocityVariable.wrapS = THREE.RepeatWrapping; + velocityVariable.wrapT = THREE.RepeatWrapping; + positionVariable.wrapS = THREE.RepeatWrapping; + positionVariable.wrapT = THREE.RepeatWrapping; + + const error = gpuCompute.init(); + + if (error !== null) { + console.error(error); + } +} + +function initBirds(effectController) { + const geometry = BirdGeometry; + + const m = new THREE.MeshStandardMaterial({ + vertexColors: true, + flatShading: true, + roughness: 1, + metalness: 0, + }); + + m.onBeforeCompile = shader => { + shader.uniforms.texturePosition = { value: null }; + shader.uniforms.textureVelocity = { value: null }; + shader.uniforms.textureAnimation = { value: textureAnimation }; + shader.uniforms.time = { value: 1.0 }; + shader.uniforms.size = { value: effectController.size }; + shader.uniforms.delta = { value: 0.0 }; + + let token = '#define STANDARD'; + + let insert = /* glsl */ ` + attribute vec4 reference; + attribute vec4 seeds; + attribute vec3 birdColor; + uniform sampler2D texturePosition; + uniform sampler2D textureVelocity; + uniform sampler2D textureAnimation; + uniform float size; + uniform float time; + `; + + shader.vertexShader = shader.vertexShader.replace(token, token + insert); + + token = '#include '; + + insert = /* glsl */ ` + vec4 tmpPos = texture2D( texturePosition, reference.xy ); + + vec3 pos = tmpPos.xyz; + vec3 velocity = normalize(texture2D( textureVelocity, reference.xy ).xyz); + vec3 aniPos = texture2D( textureAnimation, vec2( reference.z, mod( time + ( seeds.x ) * ( ( 0.0004 + seeds.y / 10000.0) + normalize( velocity ) / 20000.0 ), reference.w ) ) ).xyz; + vec3 newPosition = position; + + newPosition = mat3( modelMatrix ) * ( newPosition + aniPos ); + newPosition *= size + seeds.y * size * 0.2; + + velocity.z *= -1.; + float xz = length( velocity.xz ); + float xyz = 1.; + float x = sqrt( 1. - velocity.y * velocity.y ); + + float cosry = velocity.x / xz; + float sinry = velocity.z / xz; + + float cosrz = x / xyz; + float sinrz = velocity.y / xyz; + + mat3 maty = mat3( cosry, 0, -sinry, 0 , 1, 0 , sinry, 0, cosry ); + mat3 matz = mat3( cosrz , sinrz, 0, -sinrz, cosrz, 0, 0 , 0 , 1 ); + + newPosition = maty * matz * newPosition; + newPosition += pos; + + vec3 transformed = vec3( newPosition ); + `; + + shader.vertexShader = shader.vertexShader.replace(token, insert); + + materialShader = shader; + }; + + birdMesh = new THREE.Mesh(geometry, m); + birdMesh.rotation.y = Math.PI / 2; + + birdMesh.castShadow = true; + birdMesh.receiveShadow = true; + + scene.add(birdMesh); +} + +function fillPositionTexture(texture) { + const theArray = texture.image.data; + + for (let k = 0, kl = theArray.length; k < kl; k += 4) { + const x = Math.random() * BOUNDS - BOUNDS_HALF; + const y = Math.random() * BOUNDS - BOUNDS_HALF; + const z = Math.random() * BOUNDS - BOUNDS_HALF; + + theArray[k + 0] = x; + theArray[k + 1] = y; + theArray[k + 2] = z; + theArray[k + 3] = 1; + } +} + +function fillVelocityTexture(texture) { + const theArray = texture.image.data; + + for (let k = 0, kl = theArray.length; k < kl; k += 4) { + const x = Math.random() - 0.5; + const y = Math.random() - 0.5; + const z = Math.random() - 0.5; + + theArray[k + 0] = x * 10; + theArray[k + 1] = y * 10; + theArray[k + 2] = z * 10; + theArray[k + 3] = 1; + } +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onPointerMove(event) { + if (event.isPrimary === false) return; + + mouseX = event.clientX - windowHalfX; + mouseY = event.clientY - windowHalfY; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + const now = performance.now(); + let delta = (now - last) / 1000; + + if (delta > 1) delta = 1; // safety cap on large deltas + last = now; + + positionUniforms['time'].value = now; + positionUniforms['delta'].value = delta; + velocityUniforms['time'].value = now; + velocityUniforms['delta'].value = delta; + if (materialShader) materialShader.uniforms['time'].value = now / 1000; + if (materialShader) materialShader.uniforms['delta'].value = delta; + + velocityUniforms['predator'].value.set((0.5 * mouseX) / windowHalfX, (-0.5 * mouseY) / windowHalfY, 0); + + mouseX = 10000; + mouseY = 10000; + + gpuCompute.compute(); + + if (materialShader) + materialShader.uniforms['texturePosition'].value = gpuCompute.getCurrentRenderTarget(positionVariable).texture; + if (materialShader) + materialShader.uniforms['textureVelocity'].value = gpuCompute.getCurrentRenderTarget(velocityVariable).texture; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_gpgpu_protoplanet.ts b/examples-testing/examples/webgl_gpgpu_protoplanet.ts new file mode 100644 index 000000000..30444ddba --- /dev/null +++ b/examples-testing/examples/webgl_gpgpu_protoplanet.ts @@ -0,0 +1,280 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GPUComputationRenderer } from 'three/addons/misc/GPUComputationRenderer.js'; + +// Texture width for simulation (each texel is a debris particle) +const WIDTH = 64; + +let container, stats; +let camera, scene, renderer, geometry; + +const PARTICLES = WIDTH * WIDTH; + +let gpuCompute; +let velocityVariable; +let positionVariable; +let velocityUniforms; +let particleUniforms; +let effectController; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 5, 15000); + camera.position.y = 120; + camera.position.z = 400; + + scene = new THREE.Scene(); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 100; + controls.maxDistance = 1000; + + effectController = { + // Can be changed dynamically + gravityConstant: 100.0, + density: 0.45, + + // Must restart simulation + radius: 300, + height: 8, + exponent: 0.4, + maxMass: 15.0, + velocity: 70, + velocityExponent: 0.2, + randVelocity: 0.001, + }; + + initComputeRenderer(); + + stats = new Stats(); + container.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); + + initGUI(); + + initProtoplanets(); + + dynamicValuesChanger(); +} + +function initComputeRenderer() { + gpuCompute = new GPUComputationRenderer(WIDTH, WIDTH, renderer); + + const dtPosition = gpuCompute.createTexture(); + const dtVelocity = gpuCompute.createTexture(); + + fillTextures(dtPosition, dtVelocity); + + velocityVariable = gpuCompute.addVariable( + 'textureVelocity', + document.getElementById('computeShaderVelocity').textContent, + dtVelocity, + ); + positionVariable = gpuCompute.addVariable( + 'texturePosition', + document.getElementById('computeShaderPosition').textContent, + dtPosition, + ); + + gpuCompute.setVariableDependencies(velocityVariable, [positionVariable, velocityVariable]); + gpuCompute.setVariableDependencies(positionVariable, [positionVariable, velocityVariable]); + + velocityUniforms = velocityVariable.material.uniforms; + + velocityUniforms['gravityConstant'] = { value: 0.0 }; + velocityUniforms['density'] = { value: 0.0 }; + + const error = gpuCompute.init(); + + if (error !== null) { + console.error(error); + } +} + +function restartSimulation() { + const dtPosition = gpuCompute.createTexture(); + const dtVelocity = gpuCompute.createTexture(); + + fillTextures(dtPosition, dtVelocity); + + gpuCompute.renderTexture(dtPosition, positionVariable.renderTargets[0]); + gpuCompute.renderTexture(dtPosition, positionVariable.renderTargets[1]); + gpuCompute.renderTexture(dtVelocity, velocityVariable.renderTargets[0]); + gpuCompute.renderTexture(dtVelocity, velocityVariable.renderTargets[1]); +} + +function initProtoplanets() { + geometry = new THREE.BufferGeometry(); + + const positions = new Float32Array(PARTICLES * 3); + let p = 0; + + for (let i = 0; i < PARTICLES; i++) { + positions[p++] = (Math.random() * 2 - 1) * effectController.radius; + positions[p++] = 0; //( Math.random() * 2 - 1 ) * effectController.radius; + positions[p++] = (Math.random() * 2 - 1) * effectController.radius; + } + + const uvs = new Float32Array(PARTICLES * 2); + p = 0; + + for (let j = 0; j < WIDTH; j++) { + for (let i = 0; i < WIDTH; i++) { + uvs[p++] = i / (WIDTH - 1); + uvs[p++] = j / (WIDTH - 1); + } + } + + geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); + geometry.setAttribute('uv', new THREE.BufferAttribute(uvs, 2)); + + particleUniforms = { + texturePosition: { value: null }, + textureVelocity: { value: null }, + cameraConstant: { value: getCameraConstant(camera) }, + density: { value: 0.0 }, + }; + + // THREE.ShaderMaterial + const material = new THREE.ShaderMaterial({ + uniforms: particleUniforms, + vertexShader: document.getElementById('particleVertexShader').textContent, + fragmentShader: document.getElementById('particleFragmentShader').textContent, + }); + + const particles = new THREE.Points(geometry, material); + particles.matrixAutoUpdate = false; + particles.updateMatrix(); + + scene.add(particles); +} + +function fillTextures(texturePosition, textureVelocity) { + const posArray = texturePosition.image.data; + const velArray = textureVelocity.image.data; + + const radius = effectController.radius; + const height = effectController.height; + const exponent = effectController.exponent; + const maxMass = (effectController.maxMass * 1024) / PARTICLES; + const maxVel = effectController.velocity; + const velExponent = effectController.velocityExponent; + const randVel = effectController.randVelocity; + + for (let k = 0, kl = posArray.length; k < kl; k += 4) { + // Position + let x, z, rr; + + do { + x = Math.random() * 2 - 1; + z = Math.random() * 2 - 1; + rr = x * x + z * z; + } while (rr > 1); + + rr = Math.sqrt(rr); + + const rExp = radius * Math.pow(rr, exponent); + + // Velocity + const vel = maxVel * Math.pow(rr, velExponent); + + const vx = vel * z + (Math.random() * 2 - 1) * randVel; + const vy = (Math.random() * 2 - 1) * randVel * 0.05; + const vz = -vel * x + (Math.random() * 2 - 1) * randVel; + + x *= rExp; + z *= rExp; + const y = (Math.random() * 2 - 1) * height; + + const mass = Math.random() * maxMass + 1; + + // Fill in texture values + posArray[k + 0] = x; + posArray[k + 1] = y; + posArray[k + 2] = z; + posArray[k + 3] = 1; + + velArray[k + 0] = vx; + velArray[k + 1] = vy; + velArray[k + 2] = vz; + velArray[k + 3] = mass; + } +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + particleUniforms['cameraConstant'].value = getCameraConstant(camera); +} + +function dynamicValuesChanger() { + velocityUniforms['gravityConstant'].value = effectController.gravityConstant; + velocityUniforms['density'].value = effectController.density; + particleUniforms['density'].value = effectController.density; +} + +function initGUI() { + const gui = new GUI({ width: 280 }); + + const folder1 = gui.addFolder('Dynamic parameters'); + + folder1.add(effectController, 'gravityConstant', 0.0, 1000.0, 0.05).onChange(dynamicValuesChanger); + folder1.add(effectController, 'density', 0.0, 10.0, 0.001).onChange(dynamicValuesChanger); + + const folder2 = gui.addFolder('Static parameters'); + + folder2.add(effectController, 'radius', 10.0, 1000.0, 1.0); + folder2.add(effectController, 'height', 0.0, 50.0, 0.01); + folder2.add(effectController, 'exponent', 0.0, 2.0, 0.001); + folder2.add(effectController, 'maxMass', 1.0, 50.0, 0.1); + folder2.add(effectController, 'velocity', 0.0, 150.0, 0.1); + folder2.add(effectController, 'velocityExponent', 0.0, 1.0, 0.01); + folder2.add(effectController, 'randVelocity', 0.0, 50.0, 0.1); + + const buttonRestart = { + restartSimulation: function () { + restartSimulation(); + }, + }; + + folder2.add(buttonRestart, 'restartSimulation'); + + folder1.open(); + folder2.open(); +} + +function getCameraConstant(camera) { + return window.innerHeight / (Math.tan(THREE.MathUtils.DEG2RAD * 0.5 * camera.fov) / camera.zoom); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + gpuCompute.compute(); + + particleUniforms['texturePosition'].value = gpuCompute.getCurrentRenderTarget(positionVariable).texture; + particleUniforms['textureVelocity'].value = gpuCompute.getCurrentRenderTarget(velocityVariable).texture; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_gpgpu_water.ts b/examples-testing/examples/webgl_gpgpu_water.ts new file mode 100644 index 000000000..00c32f229 --- /dev/null +++ b/examples-testing/examples/webgl_gpgpu_water.ts @@ -0,0 +1,397 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { GPUComputationRenderer } from 'three/addons/misc/GPUComputationRenderer.js'; +import { SimplexNoise } from 'three/addons/math/SimplexNoise.js'; + +// Texture width for simulation +const WIDTH = 128; + +// Water size in system units +const BOUNDS = 512; +const BOUNDS_HALF = BOUNDS * 0.5; + +let container, stats; +let camera, scene, renderer; +let mouseMoved = false; +const mouseCoords = new THREE.Vector2(); +const raycaster = new THREE.Raycaster(); + +let waterMesh; +let meshRay; +let gpuCompute; +let heightmapVariable; +let waterUniforms; +let smoothShader; +let readWaterLevelShader; +let readWaterLevelRenderTarget; +let readWaterLevelImage; +const waterNormal = new THREE.Vector3(); + +const NUM_SPHERES = 5; +const spheres = []; +let spheresEnabled = true; + +const simplex = new SimplexNoise(); + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 3000); + camera.position.set(0, 200, 350); + camera.lookAt(0, 0, 0); + + scene = new THREE.Scene(); + + const sun = new THREE.DirectionalLight(0xffffff, 3.0); + sun.position.set(300, 400, 175); + scene.add(sun); + + const sun2 = new THREE.DirectionalLight(0x40a040, 2.0); + sun2.position.set(-100, 350, -200); + scene.add(sun2); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + container.style.touchAction = 'none'; + container.addEventListener('pointermove', onPointerMove); + + document.addEventListener('keydown', function (event) { + // W Pressed: Toggle wireframe + if (event.keyCode === 87) { + waterMesh.material.wireframe = !waterMesh.material.wireframe; + waterMesh.material.needsUpdate = true; + } + }); + + window.addEventListener('resize', onWindowResize); + + const gui = new GUI(); + + const effectController = { + mouseSize: 20.0, + viscosity: 0.98, + spheresEnabled: spheresEnabled, + }; + + const valuesChanger = function () { + heightmapVariable.material.uniforms['mouseSize'].value = effectController.mouseSize; + heightmapVariable.material.uniforms['viscosityConstant'].value = effectController.viscosity; + spheresEnabled = effectController.spheresEnabled; + for (let i = 0; i < NUM_SPHERES; i++) { + if (spheres[i]) { + spheres[i].visible = spheresEnabled; + } + } + }; + + gui.add(effectController, 'mouseSize', 1.0, 100.0, 1.0).onChange(valuesChanger); + gui.add(effectController, 'viscosity', 0.9, 0.999, 0.001).onChange(valuesChanger); + gui.add(effectController, 'spheresEnabled').onChange(valuesChanger); + const buttonSmooth = { + smoothWater: function () { + smoothWater(); + }, + }; + gui.add(buttonSmooth, 'smoothWater'); + + initWater(); + + createSpheres(); + + valuesChanger(); +} + +function initWater() { + const materialColor = 0x0040c0; + + const geometry = new THREE.PlaneGeometry(BOUNDS, BOUNDS, WIDTH - 1, WIDTH - 1); + + // material: make a THREE.ShaderMaterial clone of THREE.MeshPhongMaterial, with customized vertex shader + const material = new THREE.ShaderMaterial({ + uniforms: THREE.UniformsUtils.merge([ + THREE.ShaderLib['phong'].uniforms, + { + heightmap: { value: null }, + }, + ]), + vertexShader: document.getElementById('waterVertexShader').textContent, + fragmentShader: THREE.ShaderChunk['meshphong_frag'], + }); + + material.lights = true; + + // Material attributes from THREE.MeshPhongMaterial + // Sets the uniforms with the material values + material.uniforms['diffuse'].value = new THREE.Color(materialColor); + material.uniforms['specular'].value = new THREE.Color(0x111111); + material.uniforms['shininess'].value = Math.max(50, 1e-4); + material.uniforms['opacity'].value = material.opacity; + + // Defines + material.defines.WIDTH = WIDTH.toFixed(1); + material.defines.BOUNDS = BOUNDS.toFixed(1); + + waterUniforms = material.uniforms; + + waterMesh = new THREE.Mesh(geometry, material); + waterMesh.rotation.x = -Math.PI / 2; + waterMesh.matrixAutoUpdate = false; + waterMesh.updateMatrix(); + + scene.add(waterMesh); + + // THREE.Mesh just for mouse raycasting + const geometryRay = new THREE.PlaneGeometry(BOUNDS, BOUNDS, 1, 1); + meshRay = new THREE.Mesh(geometryRay, new THREE.MeshBasicMaterial({ color: 0xffffff, visible: false })); + meshRay.rotation.x = -Math.PI / 2; + meshRay.matrixAutoUpdate = false; + meshRay.updateMatrix(); + scene.add(meshRay); + + // Creates the gpu computation class and sets it up + + gpuCompute = new GPUComputationRenderer(WIDTH, WIDTH, renderer); + + const heightmap0 = gpuCompute.createTexture(); + + fillTexture(heightmap0); + + heightmapVariable = gpuCompute.addVariable( + 'heightmap', + document.getElementById('heightmapFragmentShader').textContent, + heightmap0, + ); + + gpuCompute.setVariableDependencies(heightmapVariable, [heightmapVariable]); + + heightmapVariable.material.uniforms['mousePos'] = { value: new THREE.Vector2(10000, 10000) }; + heightmapVariable.material.uniforms['mouseSize'] = { value: 20.0 }; + heightmapVariable.material.uniforms['viscosityConstant'] = { value: 0.98 }; + heightmapVariable.material.uniforms['heightCompensation'] = { value: 0 }; + heightmapVariable.material.defines.BOUNDS = BOUNDS.toFixed(1); + + const error = gpuCompute.init(); + if (error !== null) { + console.error(error); + } + + // Create compute shader to smooth the water surface and velocity + smoothShader = gpuCompute.createShaderMaterial(document.getElementById('smoothFragmentShader').textContent, { + smoothTexture: { value: null }, + }); + + // Create compute shader to read water level + readWaterLevelShader = gpuCompute.createShaderMaterial( + document.getElementById('readWaterLevelFragmentShader').textContent, + { + point1: { value: new THREE.Vector2() }, + levelTexture: { value: null }, + }, + ); + readWaterLevelShader.defines.WIDTH = WIDTH.toFixed(1); + readWaterLevelShader.defines.BOUNDS = BOUNDS.toFixed(1); + + // Create a 4x1 pixel image and a render target (Uint8, 4 channels, 1 byte per channel) to read water height and orientation + readWaterLevelImage = new Uint8Array(4 * 1 * 4); + + readWaterLevelRenderTarget = new THREE.WebGLRenderTarget(4, 1, { + wrapS: THREE.ClampToEdgeWrapping, + wrapT: THREE.ClampToEdgeWrapping, + minFilter: THREE.NearestFilter, + magFilter: THREE.NearestFilter, + format: THREE.RGBAFormat, + type: THREE.UnsignedByteType, + depthBuffer: false, + }); +} + +function fillTexture(texture) { + const waterMaxHeight = 10; + + function noise(x, y) { + let multR = waterMaxHeight; + let mult = 0.025; + let r = 0; + for (let i = 0; i < 15; i++) { + r += multR * simplex.noise(x * mult, y * mult); + multR *= 0.53 + 0.025 * i; + mult *= 1.25; + } + + return r; + } + + const pixels = texture.image.data; + + let p = 0; + for (let j = 0; j < WIDTH; j++) { + for (let i = 0; i < WIDTH; i++) { + const x = (i * 128) / WIDTH; + const y = (j * 128) / WIDTH; + + pixels[p + 0] = noise(x, y); + pixels[p + 1] = pixels[p + 0]; + pixels[p + 2] = 0; + pixels[p + 3] = 1; + + p += 4; + } + } +} + +function smoothWater() { + const currentRenderTarget = gpuCompute.getCurrentRenderTarget(heightmapVariable); + const alternateRenderTarget = gpuCompute.getAlternateRenderTarget(heightmapVariable); + + for (let i = 0; i < 10; i++) { + smoothShader.uniforms['smoothTexture'].value = currentRenderTarget.texture; + gpuCompute.doRenderTarget(smoothShader, alternateRenderTarget); + + smoothShader.uniforms['smoothTexture'].value = alternateRenderTarget.texture; + gpuCompute.doRenderTarget(smoothShader, currentRenderTarget); + } +} + +function createSpheres() { + const sphereTemplate = new THREE.Mesh( + new THREE.SphereGeometry(4, 24, 12), + new THREE.MeshPhongMaterial({ color: 0xffff00 }), + ); + + for (let i = 0; i < NUM_SPHERES; i++) { + let sphere = sphereTemplate; + if (i < NUM_SPHERES - 1) { + sphere = sphereTemplate.clone(); + } + + sphere.position.x = (Math.random() - 0.5) * BOUNDS * 0.7; + sphere.position.z = (Math.random() - 0.5) * BOUNDS * 0.7; + + sphere.userData.velocity = new THREE.Vector3(); + + scene.add(sphere); + + spheres[i] = sphere; + } +} + +function sphereDynamics() { + const currentRenderTarget = gpuCompute.getCurrentRenderTarget(heightmapVariable); + + readWaterLevelShader.uniforms['levelTexture'].value = currentRenderTarget.texture; + + for (let i = 0; i < NUM_SPHERES; i++) { + const sphere = spheres[i]; + + if (sphere) { + // Read water level and orientation + const u = (0.5 * sphere.position.x) / BOUNDS_HALF + 0.5; + const v = 1 - ((0.5 * sphere.position.z) / BOUNDS_HALF + 0.5); + readWaterLevelShader.uniforms['point1'].value.set(u, v); + gpuCompute.doRenderTarget(readWaterLevelShader, readWaterLevelRenderTarget); + + renderer.readRenderTargetPixels(readWaterLevelRenderTarget, 0, 0, 4, 1, readWaterLevelImage); + const pixels = new Float32Array(readWaterLevelImage.buffer); + + // Get orientation + waterNormal.set(pixels[1], 0, -pixels[2]); + + const pos = sphere.position; + + // Set height + pos.y = pixels[0]; + + // Move sphere + waterNormal.multiplyScalar(0.1); + sphere.userData.velocity.add(waterNormal); + sphere.userData.velocity.multiplyScalar(0.998); + pos.add(sphere.userData.velocity); + + if (pos.x < -BOUNDS_HALF) { + pos.x = -BOUNDS_HALF + 0.001; + sphere.userData.velocity.x *= -0.3; + } else if (pos.x > BOUNDS_HALF) { + pos.x = BOUNDS_HALF - 0.001; + sphere.userData.velocity.x *= -0.3; + } + + if (pos.z < -BOUNDS_HALF) { + pos.z = -BOUNDS_HALF + 0.001; + sphere.userData.velocity.z *= -0.3; + } else if (pos.z > BOUNDS_HALF) { + pos.z = BOUNDS_HALF - 0.001; + sphere.userData.velocity.z *= -0.3; + } + } + } +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function setMouseCoords(x, y) { + mouseCoords.set((x / renderer.domElement.clientWidth) * 2 - 1, -(y / renderer.domElement.clientHeight) * 2 + 1); + mouseMoved = true; +} + +function onPointerMove(event) { + if (event.isPrimary === false) return; + + setMouseCoords(event.clientX, event.clientY); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + // Set uniforms: mouse interaction + const uniforms = heightmapVariable.material.uniforms; + if (mouseMoved) { + raycaster.setFromCamera(mouseCoords, camera); + + const intersects = raycaster.intersectObject(meshRay); + + if (intersects.length > 0) { + const point = intersects[0].point; + uniforms['mousePos'].value.set(point.x, point.z); + } else { + uniforms['mousePos'].value.set(10000, 10000); + } + + mouseMoved = false; + } else { + uniforms['mousePos'].value.set(10000, 10000); + } + + // Do the gpu computation + gpuCompute.compute(); + + if (spheresEnabled) { + sphereDynamics(); + } + + // Get compute output in custom uniform + waterUniforms['heightmap'].value = gpuCompute.getCurrentRenderTarget(heightmapVariable).texture; + + // Render + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_helpers.ts b/examples-testing/examples/webgl_helpers.ts new file mode 100644 index 000000000..a8c3b9773 --- /dev/null +++ b/examples-testing/examples/webgl_helpers.ts @@ -0,0 +1,117 @@ +import * as THREE from 'three'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +import { VertexNormalsHelper } from 'three/addons/helpers/VertexNormalsHelper.js'; +import { VertexTangentsHelper } from 'three/addons/helpers/VertexTangentsHelper.js'; + +let scene, renderer; +let camera, light; +let vnh; +let vth; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 400; + + scene = new THREE.Scene(); + + light = new THREE.PointLight(); + light.position.set(200, 100, 150); + scene.add(light); + + scene.add(new THREE.PointLightHelper(light, 15)); + + const gridHelper = new THREE.GridHelper(400, 40, 0x0000ff, 0x808080); + gridHelper.position.y = -150; + gridHelper.position.x = -150; + scene.add(gridHelper); + + const polarGridHelper = new THREE.PolarGridHelper(200, 16, 8, 64, 0x0000ff, 0x808080); + polarGridHelper.position.y = -150; + polarGridHelper.position.x = 200; + scene.add(polarGridHelper); + + const loader = new GLTFLoader(); + loader.load('models/gltf/LeePerrySmith/LeePerrySmith.glb', function (gltf) { + const mesh = gltf.scene.children[0]; + + mesh.geometry.computeTangents(); // generates bad data due to degenerate UVs + + const group = new THREE.Group(); + group.scale.multiplyScalar(50); + scene.add(group); + + // To make sure that the matrixWorld is up to date for the boxhelpers + group.updateMatrixWorld(true); + + group.add(mesh); + + vnh = new VertexNormalsHelper(mesh, 5); + scene.add(vnh); + + vth = new VertexTangentsHelper(mesh, 5); + scene.add(vth); + + scene.add(new THREE.BoxHelper(mesh)); + + const wireframe = new THREE.WireframeGeometry(mesh.geometry); + let line = new THREE.LineSegments(wireframe); + line.material.depthTest = false; + line.material.opacity = 0.25; + line.material.transparent = true; + line.position.x = 4; + group.add(line); + scene.add(new THREE.BoxHelper(line)); + + const edges = new THREE.EdgesGeometry(mesh.geometry); + line = new THREE.LineSegments(edges); + line.material.depthTest = false; + line.material.opacity = 0.25; + line.material.transparent = true; + line.position.x = -4; + group.add(line); + scene.add(new THREE.BoxHelper(line)); + + scene.add(new THREE.BoxHelper(group)); + scene.add(new THREE.BoxHelper(scene)); + }); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const time = -performance.now() * 0.0003; + + camera.position.x = 400 * Math.cos(time); + camera.position.z = 400 * Math.sin(time); + camera.lookAt(scene.position); + + light.position.x = Math.sin(time * 1.7) * 300; + light.position.y = Math.cos(time * 1.5) * 400; + light.position.z = Math.cos(time * 1.3) * 300; + + if (vnh) vnh.update(); + if (vth) vth.update(); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_instancing_dynamic.ts b/examples-testing/examples/webgl_instancing_dynamic.ts new file mode 100644 index 000000000..88562fc5a --- /dev/null +++ b/examples-testing/examples/webgl_instancing_dynamic.ts @@ -0,0 +1,103 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, stats; + +let mesh; +const amount = parseInt(window.location.search.slice(1)) || 10; +const count = Math.pow(amount, 3); +const dummy = new THREE.Object3D(); + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(amount * 0.9, amount * 0.9, amount * 0.9); + camera.lookAt(0, 0, 0); + + scene = new THREE.Scene(); + + const loader = new THREE.BufferGeometryLoader(); + loader.load('models/json/suzanne_buffergeometry.json', function (geometry) { + geometry.computeVertexNormals(); + geometry.scale(0.5, 0.5, 0.5); + + const material = new THREE.MeshNormalMaterial(); + // check overdraw + // let material = new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.1, transparent: true } ); + + mesh = new THREE.InstancedMesh(geometry, material, count); + mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage); // will be updated every frame + scene.add(mesh); + + // + + const gui = new GUI(); + gui.add(mesh, 'count', 0, count); + }); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + render(); + + stats.update(); +} + +function render() { + if (mesh) { + const time = Date.now() * 0.001; + + mesh.rotation.x = Math.sin(time / 4); + mesh.rotation.y = Math.sin(time / 2); + + let i = 0; + const offset = (amount - 1) / 2; + + for (let x = 0; x < amount; x++) { + for (let y = 0; y < amount; y++) { + for (let z = 0; z < amount; z++) { + dummy.position.set(offset - x, offset - y, offset - z); + dummy.rotation.y = Math.sin(x / 4 + time) + Math.sin(y / 4 + time) + Math.sin(z / 4 + time); + dummy.rotation.z = dummy.rotation.y * 2; + + dummy.updateMatrix(); + + mesh.setMatrixAt(i++, dummy.matrix); + } + } + } + + mesh.instanceMatrix.needsUpdate = true; + mesh.computeBoundingSphere(); + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_instancing_morph.ts b/examples-testing/examples/webgl_instancing_morph.ts new file mode 100644 index 000000000..8686a75b9 --- /dev/null +++ b/examples-testing/examples/webgl_instancing_morph.ts @@ -0,0 +1,147 @@ +import * as THREE from 'three'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let camera, scene, renderer, stats, mesh, mixer, dummy; + +const offset = 5000; + +const timeOffsets = new Float32Array(1024); + +for (let i = 0; i < 1024; i++) { + timeOffsets[i] = Math.random() * 3; +} + +const clock = new THREE.Clock(true); + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 100, 10000); + + scene = new THREE.Scene(); + + scene.background = new THREE.Color(0x99ddff); + + scene.fog = new THREE.Fog(0x99ddff, 5000, 10000); + + const light = new THREE.DirectionalLight(0xffffff, 1); + + light.position.set(200, 1000, 50); + + light.castShadow = true; + + light.shadow.camera.left = -5000; + light.shadow.camera.right = 5000; + light.shadow.camera.top = 5000; + light.shadow.camera.bottom = -5000; + light.shadow.camera.far = 2000; + + light.shadow.bias = -0.01; + + light.shadow.camera.updateProjectionMatrix(); + + scene.add(light); + + const hemi = new THREE.HemisphereLight(0x99ddff, 0x669933, 1 / 3); + + scene.add(hemi); + + const ground = new THREE.Mesh( + new THREE.PlaneGeometry(1000000, 1000000), + new THREE.MeshStandardMaterial({ color: 0x669933, depthWrite: true }), + ); + + ground.rotation.x = -Math.PI / 2; + + ground.receiveShadow = true; + + scene.add(ground); + + const loader = new GLTFLoader(); + + loader.load('models/gltf/Horse.glb', function (glb) { + dummy = glb.scene.children[0]; + + mesh = new THREE.InstancedMesh(dummy.geometry, dummy.material, 1024); + + mesh.castShadow = true; + + for (let x = 0, i = 0; x < 32; x++) { + for (let y = 0; y < 32; y++) { + dummy.position.set(offset - 300 * x + 200 * Math.random(), 0, offset - 300 * y); + + dummy.updateMatrix(); + + mesh.setMatrixAt(i, dummy.matrix); + + mesh.setColorAt(i, new THREE.Color(`hsl(${Math.random() * 360}, 50%, 66%)`)); + + i++; + } + } + + scene.add(mesh); + + mixer = new THREE.AnimationMixer(glb.scene); + + const action = mixer.clipAction(glb.animations[0]); + + action.play(); + }); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + renderer.shadowMap.enabled = true; + renderer.shadowMap.type = THREE.VSMShadowMap; + // + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + render(); + + stats.update(); +} + +function render() { + const time = clock.getElapsedTime(); + + const r = 3000; + camera.position.set(Math.sin(time / 10) * r, 1500 + 1000 * Math.cos(time / 5), Math.cos(time / 10) * r); + camera.lookAt(0, 0, 0); + + if (mesh) { + for (let i = 0; i < 1024; i++) { + mixer.setTime(time + timeOffsets[i]); + + mesh.setMorphAt(i, dummy); + } + + mesh.morphTexture.needsUpdate = true; + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_instancing_performance.ts b/examples-testing/examples/webgl_instancing_performance.ts new file mode 100644 index 000000000..bf1deabad --- /dev/null +++ b/examples-testing/examples/webgl_instancing_performance.ts @@ -0,0 +1,262 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; + +let container, stats, gui, guiStatsEl; +let camera, controls, scene, renderer, material; + +// gui + +const Method = { + INSTANCED: 'INSTANCED', + MERGED: 'MERGED', + NAIVE: 'NAIVE', +}; + +const api = { + method: Method.INSTANCED, + count: 1000, +}; + +// + +init(); +initMesh(); + +// + +function clean() { + const meshes = []; + + scene.traverse(function (object) { + if (object.isMesh) meshes.push(object); + }); + + for (let i = 0; i < meshes.length; i++) { + const mesh = meshes[i]; + mesh.material.dispose(); + mesh.geometry.dispose(); + + scene.remove(mesh); + } +} + +const randomizeMatrix = (function () { + const position = new THREE.Vector3(); + const quaternion = new THREE.Quaternion(); + const scale = new THREE.Vector3(); + + return function (matrix) { + position.x = Math.random() * 40 - 20; + position.y = Math.random() * 40 - 20; + position.z = Math.random() * 40 - 20; + + quaternion.random(); + + scale.x = scale.y = scale.z = Math.random() * 1; + + matrix.compose(position, quaternion, scale); + }; +})(); + +function initMesh() { + clean(); + + // make instances + new THREE.BufferGeometryLoader().setPath('models/json/').load('suzanne_buffergeometry.json', function (geometry) { + material = new THREE.MeshNormalMaterial(); + + geometry.computeVertexNormals(); + + console.time(api.method + ' (build)'); + + switch (api.method) { + case Method.INSTANCED: + makeInstanced(geometry); + break; + + case Method.MERGED: + makeMerged(geometry); + break; + + case Method.NAIVE: + makeNaive(geometry); + break; + } + + console.timeEnd(api.method + ' (build)'); + }); +} + +function makeInstanced(geometry) { + const matrix = new THREE.Matrix4(); + const mesh = new THREE.InstancedMesh(geometry, material, api.count); + + for (let i = 0; i < api.count; i++) { + randomizeMatrix(matrix); + mesh.setMatrixAt(i, matrix); + } + + scene.add(mesh); + + // + + const geometryByteLength = getGeometryByteLength(geometry); + + guiStatsEl.innerHTML = [ + 'GPU draw calls: 1', + 'GPU memory: ' + formatBytes(api.count * 16 + geometryByteLength, 2), + ].join('
'); +} + +function makeMerged(geometry) { + const geometries = []; + const matrix = new THREE.Matrix4(); + + for (let i = 0; i < api.count; i++) { + randomizeMatrix(matrix); + + const instanceGeometry = geometry.clone(); + instanceGeometry.applyMatrix4(matrix); + + geometries.push(instanceGeometry); + } + + const mergedGeometry = BufferGeometryUtils.mergeGeometries(geometries); + + scene.add(new THREE.Mesh(mergedGeometry, material)); + + // + + guiStatsEl.innerHTML = [ + 'GPU draw calls: 1', + 'GPU memory: ' + formatBytes(getGeometryByteLength(mergedGeometry), 2), + ].join('
'); +} + +function makeNaive(geometry) { + const matrix = new THREE.Matrix4(); + + for (let i = 0; i < api.count; i++) { + randomizeMatrix(matrix); + + const mesh = new THREE.Mesh(geometry, material); + mesh.applyMatrix4(matrix); + + scene.add(mesh); + } + + // + + const geometryByteLength = getGeometryByteLength(geometry); + + guiStatsEl.innerHTML = [ + 'GPU draw calls: ' + api.count, + 'GPU memory: ' + formatBytes(api.count * 16 + geometryByteLength, 2), + ].join('
'); +} + +function init() { + const width = window.innerWidth; + const height = window.innerHeight; + + // camera + + camera = new THREE.PerspectiveCamera(70, width / height, 1, 100); + camera.position.z = 30; + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(width, height); + renderer.setAnimationLoop(animate); + container = document.getElementById('container'); + container.appendChild(renderer.domElement); + + // scene + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + + // controls + + controls = new OrbitControls(camera, renderer.domElement); + controls.autoRotate = true; + + // stats + + stats = new Stats(); + container.appendChild(stats.dom); + + // gui + + gui = new GUI(); + gui.add(api, 'method', Method).onChange(initMesh); + gui.add(api, 'count', 1, 10000).step(1).onChange(initMesh); + + const perfFolder = gui.addFolder('Performance'); + + guiStatsEl = document.createElement('div'); + guiStatsEl.classList.add('gui-stats'); + + perfFolder.$children.appendChild(guiStatsEl); + perfFolder.open(); + + // listeners + + window.addEventListener('resize', onWindowResize); + + Object.assign(window, { scene }); +} + +// + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); +} + +function animate() { + controls.update(); + + renderer.render(scene, camera); + + stats.update(); +} + +// + +function getGeometryByteLength(geometry) { + let total = 0; + + if (geometry.index) total += geometry.index.array.byteLength; + + for (const name in geometry.attributes) { + total += geometry.attributes[name].array.byteLength; + } + + return total; +} + +// Source: https://stackoverflow.com/a/18650828/1314762 +function formatBytes(bytes, decimals) { + if (bytes === 0) return '0 bytes'; + + const k = 1024; + const dm = decimals < 0 ? 0 : decimals; + const sizes = ['bytes', 'KB', 'MB']; + + const i = Math.floor(Math.log(bytes) / Math.log(k)); + + return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; +} diff --git a/examples-testing/examples/webgl_instancing_raycast.ts b/examples-testing/examples/webgl_instancing_raycast.ts new file mode 100644 index 000000000..371ea070b --- /dev/null +++ b/examples-testing/examples/webgl_instancing_raycast.ts @@ -0,0 +1,116 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer, controls, stats; + +let mesh; +const amount = parseInt(window.location.search.slice(1)) || 10; +const count = Math.pow(amount, 3); + +const raycaster = new THREE.Raycaster(); +const mouse = new THREE.Vector2(1, 1); + +const color = new THREE.Color(); +const white = new THREE.Color().setHex(0xffffff); + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(amount, amount, amount); + camera.lookAt(0, 0, 0); + + scene = new THREE.Scene(); + + const light = new THREE.HemisphereLight(0xffffff, 0x888888, 3); + light.position.set(0, 1, 0); + scene.add(light); + + const geometry = new THREE.IcosahedronGeometry(0.5, 3); + const material = new THREE.MeshPhongMaterial({ color: 0xffffff }); + + mesh = new THREE.InstancedMesh(geometry, material, count); + + let i = 0; + const offset = (amount - 1) / 2; + + const matrix = new THREE.Matrix4(); + + for (let x = 0; x < amount; x++) { + for (let y = 0; y < amount; y++) { + for (let z = 0; z < amount; z++) { + matrix.setPosition(offset - x, offset - y, offset - z); + + mesh.setMatrixAt(i, matrix); + mesh.setColorAt(i, color); + + i++; + } + } + } + + scene.add(mesh); + + // + + const gui = new GUI(); + gui.add(mesh, 'count', 0, count); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + controls = new OrbitControls(camera, renderer.domElement); + controls.enableDamping = true; + controls.enableZoom = false; + controls.enablePan = false; + + stats = new Stats(); + document.body.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); + document.addEventListener('mousemove', onMouseMove); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onMouseMove(event) { + event.preventDefault(); + + mouse.x = (event.clientX / window.innerWidth) * 2 - 1; + mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; +} + +function animate() { + controls.update(); + + raycaster.setFromCamera(mouse, camera); + + const intersection = raycaster.intersectObject(mesh); + + if (intersection.length > 0) { + const instanceId = intersection[0].instanceId; + + mesh.getColorAt(instanceId, color); + + if (color.equals(white)) { + mesh.setColorAt(instanceId, color.setHex(Math.random() * 0xffffff)); + + mesh.instanceColor.needsUpdate = true; + } + } + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_instancing_scatter.ts b/examples-testing/examples/webgl_instancing_scatter.ts new file mode 100644 index 000000000..fc3b9cc9f --- /dev/null +++ b/examples-testing/examples/webgl_instancing_scatter.ts @@ -0,0 +1,257 @@ +import * as THREE from 'three'; + +import { MeshSurfaceSampler } from 'three/addons/math/MeshSurfaceSampler.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, stats; + +const api = { + count: 2000, + distribution: 'random', + resample: resample, + surfaceColor: 0xfff784, + backgroundColor: 0xe39469, +}; + +let stemMesh, blossomMesh; +let stemGeometry, blossomGeometry; +let stemMaterial, blossomMaterial; + +let sampler; +const count = api.count; +const ages = new Float32Array(count); +const scales = new Float32Array(count); +const dummy = new THREE.Object3D(); + +const _position = new THREE.Vector3(); +const _normal = new THREE.Vector3(); +const _scale = new THREE.Vector3(); + +// let surfaceGeometry = new THREE.BoxGeometry( 10, 10, 10 ).toNonIndexed(); +const surfaceGeometry = new THREE.TorusKnotGeometry(10, 3, 100, 16).toNonIndexed(); +const surfaceMaterial = new THREE.MeshLambertMaterial({ color: api.surfaceColor, wireframe: false }); +const surface = new THREE.Mesh(surfaceGeometry, surfaceMaterial); + +// Source: https://gist.github.com/gre/1650294 +const easeOutCubic = function (t) { + return --t * t * t + 1; +}; + +// Scaling curve causes particles to grow quickly, ease gradually into full scale, then +// disappear quickly. More of the particle's lifetime is spent around full scale. +const scaleCurve = function (t) { + return Math.abs(easeOutCubic((t > 0.5 ? 1 - t : t) * 2)); +}; + +const loader = new GLTFLoader(); + +loader.load('./models/gltf/Flower/Flower.glb', function (gltf) { + const _stemMesh = gltf.scene.getObjectByName('Stem'); + const _blossomMesh = gltf.scene.getObjectByName('Blossom'); + + stemGeometry = _stemMesh.geometry.clone(); + blossomGeometry = _blossomMesh.geometry.clone(); + + const defaultTransform = new THREE.Matrix4() + .makeRotationX(Math.PI) + .multiply(new THREE.Matrix4().makeScale(7, 7, 7)); + + stemGeometry.applyMatrix4(defaultTransform); + blossomGeometry.applyMatrix4(defaultTransform); + + stemMaterial = _stemMesh.material; + blossomMaterial = _blossomMesh.material; + + stemMesh = new THREE.InstancedMesh(stemGeometry, stemMaterial, count); + blossomMesh = new THREE.InstancedMesh(blossomGeometry, blossomMaterial, count); + + // Assign random colors to the blossoms. + const color = new THREE.Color(); + const blossomPalette = [0xf20587, 0xf2d479, 0xf2c879, 0xf2b077, 0xf24405]; + + for (let i = 0; i < count; i++) { + color.setHex(blossomPalette[Math.floor(Math.random() * blossomPalette.length)]); + blossomMesh.setColorAt(i, color); + } + + // Instance matrices will be updated every frame. + stemMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage); + blossomMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage); + + resample(); + + init(); +}); + +function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(25, 25, 25); + camera.lookAt(0, 0, 0); + + // + + scene = new THREE.Scene(); + scene.background = new THREE.Color(api.backgroundColor); + + const pointLight = new THREE.PointLight(0xaa8899, 2.5, 0, 0); + pointLight.position.set(50, -25, 75); + scene.add(pointLight); + + scene.add(new THREE.AmbientLight(0xffffff, 3)); + + // + + scene.add(stemMesh); + scene.add(blossomMesh); + + scene.add(surface); + + // + + const gui = new GUI(); + gui.add(api, 'count', 0, count).onChange(function () { + stemMesh.count = api.count; + blossomMesh.count = api.count; + }); + + // gui.addColor( api, 'backgroundColor' ).onChange( function () { + + // scene.background.setHex( api.backgroundColor ); + + // } ); + + // gui.addColor( api, 'surfaceColor' ).onChange( function () { + + // surfaceMaterial.color.setHex( api.surfaceColor ); + + // } ); + + gui.add(api, 'distribution').options(['random', 'weighted']).onChange(resample); + gui.add(api, 'resample'); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function resample() { + const vertexCount = surface.geometry.getAttribute('position').count; + + console.info('Sampling ' + count + ' points from a surface with ' + vertexCount + ' vertices...'); + + // + + console.time('.build()'); + + sampler = new MeshSurfaceSampler(surface).setWeightAttribute(api.distribution === 'weighted' ? 'uv' : null).build(); + + console.timeEnd('.build()'); + + // + + console.time('.sample()'); + + for (let i = 0; i < count; i++) { + ages[i] = Math.random(); + scales[i] = scaleCurve(ages[i]); + + resampleParticle(i); + } + + console.timeEnd('.sample()'); + + stemMesh.instanceMatrix.needsUpdate = true; + blossomMesh.instanceMatrix.needsUpdate = true; +} + +function resampleParticle(i) { + sampler.sample(_position, _normal); + _normal.add(_position); + + dummy.position.copy(_position); + dummy.scale.set(scales[i], scales[i], scales[i]); + dummy.lookAt(_normal); + dummy.updateMatrix(); + + stemMesh.setMatrixAt(i, dummy.matrix); + blossomMesh.setMatrixAt(i, dummy.matrix); +} + +function updateParticle(i) { + // Update lifecycle. + + ages[i] += 0.005; + + if (ages[i] >= 1) { + ages[i] = 0.001; + scales[i] = scaleCurve(ages[i]); + + resampleParticle(i); + + return; + } + + // Update scale. + + const prevScale = scales[i]; + scales[i] = scaleCurve(ages[i]); + _scale.set(scales[i] / prevScale, scales[i] / prevScale, scales[i] / prevScale); + + // Update transform. + + stemMesh.getMatrixAt(i, dummy.matrix); + dummy.matrix.scale(_scale); + stemMesh.setMatrixAt(i, dummy.matrix); + blossomMesh.setMatrixAt(i, dummy.matrix); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + render(); + + stats.update(); +} + +function render() { + if (stemMesh && blossomMesh) { + const time = Date.now() * 0.001; + + scene.rotation.x = Math.sin(time / 4); + scene.rotation.y = Math.sin(time / 2); + + for (let i = 0; i < api.count; i++) { + updateParticle(i); + } + + stemMesh.instanceMatrix.needsUpdate = true; + blossomMesh.instanceMatrix.needsUpdate = true; + + stemMesh.computeBoundingSphere(); + blossomMesh.computeBoundingSphere(); + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_interactive_buffergeometry.ts b/examples-testing/examples/webgl_interactive_buffergeometry.ts new file mode 100644 index 000000000..1d6608b13 --- /dev/null +++ b/examples-testing/examples/webgl_interactive_buffergeometry.ts @@ -0,0 +1,244 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; + +let camera, scene, renderer; + +let raycaster, pointer; + +let mesh, line; + +init(); + +function init() { + container = document.getElementById('container'); + + // + + camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 3500); + camera.position.z = 2750; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x050505); + scene.fog = new THREE.Fog(0x050505, 2000, 3500); + + // + + scene.add(new THREE.AmbientLight(0x444444, 3)); + + const light1 = new THREE.DirectionalLight(0xffffff, 1.5); + light1.position.set(1, 1, 1); + scene.add(light1); + + const light2 = new THREE.DirectionalLight(0xffffff, 4.5); + light2.position.set(0, -1, 0); + scene.add(light2); + + // + + const triangles = 5000; + + let geometry = new THREE.BufferGeometry(); + + const positions = new Float32Array(triangles * 3 * 3); + const normals = new Float32Array(triangles * 3 * 3); + const colors = new Float32Array(triangles * 3 * 3); + + const color = new THREE.Color(); + + const n = 800, + n2 = n / 2; // triangles spread in the cube + const d = 120, + d2 = d / 2; // individual triangle size + + const pA = new THREE.Vector3(); + const pB = new THREE.Vector3(); + const pC = new THREE.Vector3(); + + const cb = new THREE.Vector3(); + const ab = new THREE.Vector3(); + + for (let i = 0; i < positions.length; i += 9) { + // positions + + const x = Math.random() * n - n2; + const y = Math.random() * n - n2; + const z = Math.random() * n - n2; + + const ax = x + Math.random() * d - d2; + const ay = y + Math.random() * d - d2; + const az = z + Math.random() * d - d2; + + const bx = x + Math.random() * d - d2; + const by = y + Math.random() * d - d2; + const bz = z + Math.random() * d - d2; + + const cx = x + Math.random() * d - d2; + const cy = y + Math.random() * d - d2; + const cz = z + Math.random() * d - d2; + + positions[i] = ax; + positions[i + 1] = ay; + positions[i + 2] = az; + + positions[i + 3] = bx; + positions[i + 4] = by; + positions[i + 5] = bz; + + positions[i + 6] = cx; + positions[i + 7] = cy; + positions[i + 8] = cz; + + // flat face normals + + pA.set(ax, ay, az); + pB.set(bx, by, bz); + pC.set(cx, cy, cz); + + cb.subVectors(pC, pB); + ab.subVectors(pA, pB); + cb.cross(ab); + + cb.normalize(); + + const nx = cb.x; + const ny = cb.y; + const nz = cb.z; + + normals[i] = nx; + normals[i + 1] = ny; + normals[i + 2] = nz; + + normals[i + 3] = nx; + normals[i + 4] = ny; + normals[i + 5] = nz; + + normals[i + 6] = nx; + normals[i + 7] = ny; + normals[i + 8] = nz; + + // colors + + const vx = x / n + 0.5; + const vy = y / n + 0.5; + const vz = z / n + 0.5; + + color.setRGB(vx, vy, vz); + + colors[i] = color.r; + colors[i + 1] = color.g; + colors[i + 2] = color.b; + + colors[i + 3] = color.r; + colors[i + 4] = color.g; + colors[i + 5] = color.b; + + colors[i + 6] = color.r; + colors[i + 7] = color.g; + colors[i + 8] = color.b; + } + + geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); + geometry.setAttribute('normal', new THREE.BufferAttribute(normals, 3)); + geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3)); + + geometry.computeBoundingSphere(); + + let material = new THREE.MeshPhongMaterial({ + color: 0xaaaaaa, + specular: 0xffffff, + shininess: 250, + side: THREE.DoubleSide, + vertexColors: true, + }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + raycaster = new THREE.Raycaster(); + + pointer = new THREE.Vector2(); + + geometry = new THREE.BufferGeometry(); + geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(4 * 3), 3)); + + material = new THREE.LineBasicMaterial({ color: 0xffffff, transparent: true }); + + line = new THREE.Line(geometry, material); + scene.add(line); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); + document.addEventListener('pointermove', onPointerMove); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onPointerMove(event) { + pointer.x = (event.clientX / window.innerWidth) * 2 - 1; + pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + const time = Date.now() * 0.001; + + mesh.rotation.x = time * 0.15; + mesh.rotation.y = time * 0.25; + + raycaster.setFromCamera(pointer, camera); + + const intersects = raycaster.intersectObject(mesh); + + if (intersects.length > 0) { + const intersect = intersects[0]; + const face = intersect.face; + + const linePosition = line.geometry.attributes.position; + const meshPosition = mesh.geometry.attributes.position; + + linePosition.copyAt(0, meshPosition, face.a); + linePosition.copyAt(1, meshPosition, face.b); + linePosition.copyAt(2, meshPosition, face.c); + linePosition.copyAt(3, meshPosition, face.a); + + mesh.updateMatrix(); + + line.geometry.applyMatrix4(mesh.matrix); + + line.visible = true; + } else { + line.visible = false; + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_interactive_cubes.ts b/examples-testing/examples/webgl_interactive_cubes.ts new file mode 100644 index 000000000..adfcfddf8 --- /dev/null +++ b/examples-testing/examples/webgl_interactive_cubes.ts @@ -0,0 +1,114 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let stats; +let camera, scene, raycaster, renderer; + +let INTERSECTED; +let theta = 0; + +const pointer = new THREE.Vector2(); +const radius = 5; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(1, 1, 1).normalize(); + scene.add(light); + + const geometry = new THREE.BoxGeometry(); + + for (let i = 0; i < 2000; i++) { + const object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff })); + + object.position.x = Math.random() * 40 - 20; + object.position.y = Math.random() * 40 - 20; + object.position.z = Math.random() * 40 - 20; + + object.rotation.x = Math.random() * 2 * Math.PI; + object.rotation.y = Math.random() * 2 * Math.PI; + object.rotation.z = Math.random() * 2 * Math.PI; + + object.scale.x = Math.random() + 0.5; + object.scale.y = Math.random() + 0.5; + object.scale.z = Math.random() + 0.5; + + scene.add(object); + } + + raycaster = new THREE.Raycaster(); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + document.addEventListener('mousemove', onPointerMove); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onPointerMove(event) { + pointer.x = (event.clientX / window.innerWidth) * 2 - 1; + pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + theta += 0.1; + + camera.position.x = radius * Math.sin(THREE.MathUtils.degToRad(theta)); + camera.position.y = radius * Math.sin(THREE.MathUtils.degToRad(theta)); + camera.position.z = radius * Math.cos(THREE.MathUtils.degToRad(theta)); + camera.lookAt(scene.position); + + camera.updateMatrixWorld(); + + // find intersections + + raycaster.setFromCamera(pointer, camera); + + const intersects = raycaster.intersectObjects(scene.children, false); + + if (intersects.length > 0) { + if (INTERSECTED != intersects[0].object) { + if (INTERSECTED) INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex); + + INTERSECTED = intersects[0].object; + INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex(); + INTERSECTED.material.emissive.setHex(0xff0000); + } + } else { + if (INTERSECTED) INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex); + + INTERSECTED = null; + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_interactive_cubes_gpu.ts b/examples-testing/examples/webgl_interactive_cubes_gpu.ts new file mode 100644 index 000000000..2644469c3 --- /dev/null +++ b/examples-testing/examples/webgl_interactive_cubes_gpu.ts @@ -0,0 +1,229 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; +import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; + +let container, stats; +let camera, controls, scene, renderer; +let pickingTexture, pickingScene; +let highlightBox; + +const pickingData = []; + +const pointer = new THREE.Vector2(); +const offset = new THREE.Vector3(10, 10, 10); +const clearColor = new THREE.Color(); + +init(); + +function init() { + container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 1000; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + + scene.add(new THREE.AmbientLight(0xcccccc)); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0, 500, 2000); + scene.add(light); + + const defaultMaterial = new THREE.MeshPhongMaterial({ + color: 0xffffff, + flatShading: true, + vertexColors: true, + shininess: 0, + }); + + // set up the picking texture to use a 32 bit integer so we can write and read integer ids from it + pickingScene = new THREE.Scene(); + pickingTexture = new THREE.WebGLRenderTarget(1, 1, { + type: THREE.IntType, + format: THREE.RGBAIntegerFormat, + internalFormat: 'RGBA32I', + }); + const pickingMaterial = new THREE.ShaderMaterial({ + glslVersion: THREE.GLSL3, + + vertexShader: /* glsl */ ` + attribute int id; + flat varying int vid; + void main() { + + vid = id; + gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); + + } + `, + + fragmentShader: /* glsl */ ` + layout(location = 0) out int out_id; + flat varying int vid; + + void main() { + + out_id = vid; + + } + `, + }); + + function applyId(geometry, id) { + const position = geometry.attributes.position; + const array = new Int16Array(position.count); + array.fill(id); + + const bufferAttribute = new THREE.Int16BufferAttribute(array, 1, false); + bufferAttribute.gpuType = THREE.IntType; + geometry.setAttribute('id', bufferAttribute); + } + + function applyVertexColors(geometry, color) { + const position = geometry.attributes.position; + const colors = []; + + for (let i = 0; i < position.count; i++) { + colors.push(color.r, color.g, color.b); + } + + geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); + } + + const geometries = []; + const matrix = new THREE.Matrix4(); + const quaternion = new THREE.Quaternion(); + const color = new THREE.Color(); + + for (let i = 0; i < 5000; i++) { + const geometry = new THREE.BoxGeometry(); + + const position = new THREE.Vector3(); + position.x = Math.random() * 10000 - 5000; + position.y = Math.random() * 6000 - 3000; + position.z = Math.random() * 8000 - 4000; + + const rotation = new THREE.Euler(); + rotation.x = Math.random() * 2 * Math.PI; + rotation.y = Math.random() * 2 * Math.PI; + rotation.z = Math.random() * 2 * Math.PI; + + const scale = new THREE.Vector3(); + scale.x = Math.random() * 200 + 100; + scale.y = Math.random() * 200 + 100; + scale.z = Math.random() * 200 + 100; + + quaternion.setFromEuler(rotation); + matrix.compose(position, quaternion, scale); + + geometry.applyMatrix4(matrix); + + // give the geometry's vertices a random color to be displayed and an integer + // identifier as a vertex attribute so boxes can be identified after being merged. + applyVertexColors(geometry, color.setHex(Math.random() * 0xffffff)); + applyId(geometry, i); + + geometries.push(geometry); + + pickingData[i] = { + position: position, + rotation: rotation, + scale: scale, + }; + } + + const mergedGeometry = BufferGeometryUtils.mergeGeometries(geometries); + scene.add(new THREE.Mesh(mergedGeometry, defaultMaterial)); + pickingScene.add(new THREE.Mesh(mergedGeometry, pickingMaterial)); + + highlightBox = new THREE.Mesh(new THREE.BoxGeometry(), new THREE.MeshLambertMaterial({ color: 0xffff00 })); + scene.add(highlightBox); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + controls = new TrackballControls(camera, renderer.domElement); + controls.rotateSpeed = 1.0; + controls.zoomSpeed = 1.2; + controls.panSpeed = 0.8; + controls.noZoom = false; + controls.noPan = false; + controls.staticMoving = true; + controls.dynamicDampingFactor = 0.3; + + stats = new Stats(); + container.appendChild(stats.dom); + + renderer.domElement.addEventListener('pointermove', onPointerMove); +} + +// + +function onPointerMove(e) { + pointer.x = e.clientX; + pointer.y = e.clientY; +} + +function animate() { + render(); + stats.update(); +} + +function pick() { + // render the picking scene off-screen + // set the view offset to represent just a single pixel under the mouse + const dpr = window.devicePixelRatio; + camera.setViewOffset( + renderer.domElement.width, + renderer.domElement.height, + Math.floor(pointer.x * dpr), + Math.floor(pointer.y * dpr), + 1, + 1, + ); + + // render the scene + renderer.setRenderTarget(pickingTexture); + + // clear the background to - 1 meaning no item was hit + clearColor.setRGB(-1, -1, -1); + renderer.setClearColor(clearColor); + renderer.render(pickingScene, camera); + + // clear the view offset so rendering returns to normal + camera.clearViewOffset(); + + // create buffer for reading single pixel + const pixelBuffer = new Int32Array(4); + + // read the pixel + renderer.readRenderTargetPixelsAsync(pickingTexture, 0, 0, 1, 1, pixelBuffer).then(() => { + const id = pixelBuffer[0]; + if (id !== -1) { + // move our highlightBox so that it surrounds the picked object + const data = pickingData[id]; + highlightBox.position.copy(data.position); + highlightBox.rotation.copy(data.rotation); + highlightBox.scale.copy(data.scale).add(offset); + highlightBox.visible = true; + } else { + highlightBox.visible = false; + } + }); +} + +function render() { + controls.update(); + + pick(); + + renderer.setRenderTarget(null); + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_interactive_cubes_ortho.ts b/examples-testing/examples/webgl_interactive_cubes_ortho.ts new file mode 100644 index 000000000..520674b5f --- /dev/null +++ b/examples-testing/examples/webgl_interactive_cubes_ortho.ts @@ -0,0 +1,129 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let stats; +let camera, scene, raycaster, renderer; + +let theta = 0; +let INTERSECTED; + +const pointer = new THREE.Vector2(); +const radius = 25; +const frustumSize = 50; + +init(); + +function init() { + const aspect = window.innerWidth / window.innerHeight; + camera = new THREE.OrthographicCamera( + (frustumSize * aspect) / -2, + (frustumSize * aspect) / 2, + frustumSize / 2, + frustumSize / -2, + 0.1, + 100, + ); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(1, 1, 1).normalize(); + scene.add(light); + + const geometry = new THREE.BoxGeometry(); + + for (let i = 0; i < 2000; i++) { + const object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff })); + + object.position.x = Math.random() * 40 - 20; + object.position.y = Math.random() * 40 - 20; + object.position.z = Math.random() * 40 - 20; + + object.rotation.x = Math.random() * 2 * Math.PI; + object.rotation.y = Math.random() * 2 * Math.PI; + object.rotation.z = Math.random() * 2 * Math.PI; + + object.scale.x = Math.random() + 0.5; + object.scale.y = Math.random() + 0.5; + object.scale.z = Math.random() + 0.5; + + scene.add(object); + } + + raycaster = new THREE.Raycaster(); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + document.addEventListener('pointermove', onPointerMove); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const aspect = window.innerWidth / window.innerHeight; + + camera.left = (-frustumSize * aspect) / 2; + camera.right = (frustumSize * aspect) / 2; + camera.top = frustumSize / 2; + camera.bottom = -frustumSize / 2; + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onPointerMove(event) { + pointer.x = (event.clientX / window.innerWidth) * 2 - 1; + pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + theta += 0.1; + + camera.position.x = radius * Math.sin(THREE.MathUtils.degToRad(theta)); + camera.position.y = radius * Math.sin(THREE.MathUtils.degToRad(theta)); + camera.position.z = radius * Math.cos(THREE.MathUtils.degToRad(theta)); + camera.lookAt(scene.position); + + camera.updateMatrixWorld(); + + // find intersections + + raycaster.setFromCamera(pointer, camera); + + const intersects = raycaster.intersectObjects(scene.children, false); + + if (intersects.length > 0) { + if (INTERSECTED != intersects[0].object) { + if (INTERSECTED) INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex); + + INTERSECTED = intersects[0].object; + INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex(); + INTERSECTED.material.emissive.setHex(0xff0000); + } + } else { + if (INTERSECTED) INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex); + + INTERSECTED = null; + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_interactive_lines.ts b/examples-testing/examples/webgl_interactive_lines.ts new file mode 100644 index 000000000..b137c5501 --- /dev/null +++ b/examples-testing/examples/webgl_interactive_lines.ts @@ -0,0 +1,160 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; +let camera, scene, raycaster, renderer, parentTransform, sphereInter; + +const pointer = new THREE.Vector2(); +const radius = 100; +let theta = 0; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + const info = document.createElement('div'); + info.style.position = 'absolute'; + info.style.top = '10px'; + info.style.width = '100%'; + info.style.textAlign = 'center'; + info.innerHTML = + 'three.js webgl - interactive lines'; + container.appendChild(info); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 10000); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + const geometry = new THREE.SphereGeometry(5); + const material = new THREE.MeshBasicMaterial({ color: 0xff0000 }); + + sphereInter = new THREE.Mesh(geometry, material); + sphereInter.visible = false; + scene.add(sphereInter); + + const lineGeometry = new THREE.BufferGeometry(); + const points = []; + + const point = new THREE.Vector3(); + const direction = new THREE.Vector3(); + + for (let i = 0; i < 50; i++) { + direction.x += Math.random() - 0.5; + direction.y += Math.random() - 0.5; + direction.z += Math.random() - 0.5; + direction.normalize().multiplyScalar(10); + + point.add(direction); + points.push(point.x, point.y, point.z); + } + + lineGeometry.setAttribute('position', new THREE.Float32BufferAttribute(points, 3)); + + parentTransform = new THREE.Object3D(); + parentTransform.position.x = Math.random() * 40 - 20; + parentTransform.position.y = Math.random() * 40 - 20; + parentTransform.position.z = Math.random() * 40 - 20; + + parentTransform.rotation.x = Math.random() * 2 * Math.PI; + parentTransform.rotation.y = Math.random() * 2 * Math.PI; + parentTransform.rotation.z = Math.random() * 2 * Math.PI; + + parentTransform.scale.x = Math.random() + 0.5; + parentTransform.scale.y = Math.random() + 0.5; + parentTransform.scale.z = Math.random() + 0.5; + + for (let i = 0; i < 50; i++) { + let object; + + const lineMaterial = new THREE.LineBasicMaterial({ color: Math.random() * 0xffffff }); + + if (Math.random() > 0.5) { + object = new THREE.Line(lineGeometry, lineMaterial); + } else { + object = new THREE.LineSegments(lineGeometry, lineMaterial); + } + + object.position.x = Math.random() * 400 - 200; + object.position.y = Math.random() * 400 - 200; + object.position.z = Math.random() * 400 - 200; + + object.rotation.x = Math.random() * 2 * Math.PI; + object.rotation.y = Math.random() * 2 * Math.PI; + object.rotation.z = Math.random() * 2 * Math.PI; + + object.scale.x = Math.random() + 0.5; + object.scale.y = Math.random() + 0.5; + object.scale.z = Math.random() + 0.5; + + parentTransform.add(object); + } + + scene.add(parentTransform); + + raycaster = new THREE.Raycaster(); + raycaster.params.Line.threshold = 3; + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + document.addEventListener('pointermove', onPointerMove); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onPointerMove(event) { + pointer.x = (event.clientX / window.innerWidth) * 2 - 1; + pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + theta += 0.1; + + camera.position.x = radius * Math.sin(THREE.MathUtils.degToRad(theta)); + camera.position.y = radius * Math.sin(THREE.MathUtils.degToRad(theta)); + camera.position.z = radius * Math.cos(THREE.MathUtils.degToRad(theta)); + camera.lookAt(scene.position); + + camera.updateMatrixWorld(); + + // find intersections + + raycaster.setFromCamera(pointer, camera); + + const intersects = raycaster.intersectObjects(parentTransform.children, true); + + if (intersects.length > 0) { + sphereInter.visible = true; + sphereInter.position.copy(intersects[0].point); + } else { + sphereInter.visible = false; + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_interactive_points.ts b/examples-testing/examples/webgl_interactive_points.ts new file mode 100644 index 000000000..93113b867 --- /dev/null +++ b/examples-testing/examples/webgl_interactive_points.ts @@ -0,0 +1,143 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; + +let renderer, scene, camera, stats; + +let particles; + +const PARTICLE_SIZE = 20; + +let raycaster, intersects; +let pointer, INTERSECTED; + +init(); + +function init() { + const container = document.getElementById('container'); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 250; + + // + + let boxGeometry = new THREE.BoxGeometry(200, 200, 200, 16, 16, 16); + + // if normal and uv attributes are not removed, mergeVertices() can't consolidate indentical vertices with different normal/uv data + + boxGeometry.deleteAttribute('normal'); + boxGeometry.deleteAttribute('uv'); + + boxGeometry = BufferGeometryUtils.mergeVertices(boxGeometry); + + // + + const positionAttribute = boxGeometry.getAttribute('position'); + + const colors = []; + const sizes = []; + + const color = new THREE.Color(); + + for (let i = 0, l = positionAttribute.count; i < l; i++) { + color.setHSL(0.01 + 0.1 * (i / l), 1.0, 0.5); + color.toArray(colors, i * 3); + + sizes[i] = PARTICLE_SIZE * 0.5; + } + + const geometry = new THREE.BufferGeometry(); + geometry.setAttribute('position', positionAttribute); + geometry.setAttribute('customColor', new THREE.Float32BufferAttribute(colors, 3)); + geometry.setAttribute('size', new THREE.Float32BufferAttribute(sizes, 1)); + + // + + const material = new THREE.ShaderMaterial({ + uniforms: { + color: { value: new THREE.Color(0xffffff) }, + pointTexture: { value: new THREE.TextureLoader().load('textures/sprites/disc.png') }, + alphaTest: { value: 0.9 }, + }, + vertexShader: document.getElementById('vertexshader').textContent, + fragmentShader: document.getElementById('fragmentshader').textContent, + }); + + // + + particles = new THREE.Points(geometry, material); + scene.add(particles); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + raycaster = new THREE.Raycaster(); + pointer = new THREE.Vector2(); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); + document.addEventListener('pointermove', onPointerMove); +} + +function onPointerMove(event) { + pointer.x = (event.clientX / window.innerWidth) * 2 - 1; + pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + particles.rotation.x += 0.0005; + particles.rotation.y += 0.001; + + const geometry = particles.geometry; + const attributes = geometry.attributes; + + raycaster.setFromCamera(pointer, camera); + + intersects = raycaster.intersectObject(particles); + + if (intersects.length > 0) { + if (INTERSECTED != intersects[0].index) { + attributes.size.array[INTERSECTED] = PARTICLE_SIZE; + + INTERSECTED = intersects[0].index; + + attributes.size.array[INTERSECTED] = PARTICLE_SIZE * 1.25; + attributes.size.needsUpdate = true; + } + } else if (INTERSECTED !== null) { + attributes.size.array[INTERSECTED] = PARTICLE_SIZE; + attributes.size.needsUpdate = true; + INTERSECTED = null; + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_interactive_raycasting_points.ts b/examples-testing/examples/webgl_interactive_raycasting_points.ts new file mode 100644 index 000000000..41c158a43 --- /dev/null +++ b/examples-testing/examples/webgl_interactive_raycasting_points.ts @@ -0,0 +1,220 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let renderer, scene, camera, stats; +let pointclouds; +let raycaster; +let intersection = null; +let spheresIndex = 0; +let clock; +let toggle = 0; + +const pointer = new THREE.Vector2(); +const spheres = []; + +const threshold = 0.1; +const pointSize = 0.05; +const width = 80; +const length = 160; +const rotateY = new THREE.Matrix4().makeRotationY(0.005); + +init(); + +function generatePointCloudGeometry(color, width, length) { + const geometry = new THREE.BufferGeometry(); + const numPoints = width * length; + + const positions = new Float32Array(numPoints * 3); + const colors = new Float32Array(numPoints * 3); + + let k = 0; + + for (let i = 0; i < width; i++) { + for (let j = 0; j < length; j++) { + const u = i / width; + const v = j / length; + const x = u - 0.5; + const y = (Math.cos(u * Math.PI * 4) + Math.sin(v * Math.PI * 8)) / 20; + const z = v - 0.5; + + positions[3 * k] = x; + positions[3 * k + 1] = y; + positions[3 * k + 2] = z; + + const intensity = (y + 0.1) * 5; + colors[3 * k] = color.r * intensity; + colors[3 * k + 1] = color.g * intensity; + colors[3 * k + 2] = color.b * intensity; + + k++; + } + } + + geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); + geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3)); + geometry.computeBoundingBox(); + + return geometry; +} + +function generatePointcloud(color, width, length) { + const geometry = generatePointCloudGeometry(color, width, length); + const material = new THREE.PointsMaterial({ size: pointSize, vertexColors: true }); + + return new THREE.Points(geometry, material); +} + +function generateIndexedPointcloud(color, width, length) { + const geometry = generatePointCloudGeometry(color, width, length); + const numPoints = width * length; + const indices = new Uint16Array(numPoints); + + let k = 0; + + for (let i = 0; i < width; i++) { + for (let j = 0; j < length; j++) { + indices[k] = k; + k++; + } + } + + geometry.setIndex(new THREE.BufferAttribute(indices, 1)); + + const material = new THREE.PointsMaterial({ size: pointSize, vertexColors: true }); + + return new THREE.Points(geometry, material); +} + +function generateIndexedWithOffsetPointcloud(color, width, length) { + const geometry = generatePointCloudGeometry(color, width, length); + const numPoints = width * length; + const indices = new Uint16Array(numPoints); + + let k = 0; + + for (let i = 0; i < width; i++) { + for (let j = 0; j < length; j++) { + indices[k] = k; + k++; + } + } + + geometry.setIndex(new THREE.BufferAttribute(indices, 1)); + geometry.addGroup(0, indices.length); + + const material = new THREE.PointsMaterial({ size: pointSize, vertexColors: true }); + + return new THREE.Points(geometry, material); +} + +function init() { + const container = document.getElementById('container'); + + scene = new THREE.Scene(); + + clock = new THREE.Clock(); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.set(10, 10, 10); + camera.lookAt(scene.position); + camera.updateMatrix(); + + // + + const pcBuffer = generatePointcloud(new THREE.Color(1, 0, 0), width, length); + pcBuffer.scale.set(5, 10, 10); + pcBuffer.position.set(-5, 0, 0); + scene.add(pcBuffer); + + const pcIndexed = generateIndexedPointcloud(new THREE.Color(0, 1, 0), width, length); + pcIndexed.scale.set(5, 10, 10); + pcIndexed.position.set(0, 0, 0); + scene.add(pcIndexed); + + const pcIndexedOffset = generateIndexedWithOffsetPointcloud(new THREE.Color(0, 1, 1), width, length); + pcIndexedOffset.scale.set(5, 10, 10); + pcIndexedOffset.position.set(5, 0, 0); + scene.add(pcIndexedOffset); + + pointclouds = [pcBuffer, pcIndexed, pcIndexedOffset]; + + // + + const sphereGeometry = new THREE.SphereGeometry(0.1, 32, 32); + const sphereMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 }); + + for (let i = 0; i < 40; i++) { + const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); + scene.add(sphere); + spheres.push(sphere); + } + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + raycaster = new THREE.Raycaster(); + raycaster.params.Points.threshold = threshold; + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); + document.addEventListener('pointermove', onPointerMove); +} + +function onPointerMove(event) { + pointer.x = (event.clientX / window.innerWidth) * 2 - 1; + pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + camera.applyMatrix4(rotateY); + camera.updateMatrixWorld(); + + raycaster.setFromCamera(pointer, camera); + + const intersections = raycaster.intersectObjects(pointclouds, false); + intersection = intersections.length > 0 ? intersections[0] : null; + + if (toggle > 0.02 && intersection !== null) { + spheres[spheresIndex].position.copy(intersection.point); + spheres[spheresIndex].scale.set(1, 1, 1); + spheresIndex = (spheresIndex + 1) % spheres.length; + + toggle = 0; + } + + for (let i = 0; i < spheres.length; i++) { + const sphere = spheres[i]; + sphere.scale.multiplyScalar(0.98); + sphere.scale.clampScalar(0.01, 1); + } + + toggle += clock.getDelta(); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_interactive_voxelpainter.ts b/examples-testing/examples/webgl_interactive_voxelpainter.ts new file mode 100644 index 000000000..48b16f3b7 --- /dev/null +++ b/examples-testing/examples/webgl_interactive_voxelpainter.ts @@ -0,0 +1,158 @@ +import * as THREE from 'three'; + +let camera, scene, renderer; +let plane; +let pointer, + raycaster, + isShiftDown = false; + +let rollOverMesh, rollOverMaterial; +let cubeGeo, cubeMaterial; + +const objects = []; + +init(); +render(); + +function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.set(500, 800, 1300); + camera.lookAt(0, 0, 0); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + // roll-over helpers + + const rollOverGeo = new THREE.BoxGeometry(50, 50, 50); + rollOverMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000, opacity: 0.5, transparent: true }); + rollOverMesh = new THREE.Mesh(rollOverGeo, rollOverMaterial); + scene.add(rollOverMesh); + + // cubes + + const map = new THREE.TextureLoader().load('textures/square-outline-textured.png'); + map.colorSpace = THREE.SRGBColorSpace; + cubeGeo = new THREE.BoxGeometry(50, 50, 50); + cubeMaterial = new THREE.MeshLambertMaterial({ color: 0xfeb74c, map: map }); + + // grid + + const gridHelper = new THREE.GridHelper(1000, 20); + scene.add(gridHelper); + + // + + raycaster = new THREE.Raycaster(); + pointer = new THREE.Vector2(); + + const geometry = new THREE.PlaneGeometry(1000, 1000); + geometry.rotateX(-Math.PI / 2); + + plane = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ visible: false })); + scene.add(plane); + + objects.push(plane); + + // lights + + const ambientLight = new THREE.AmbientLight(0x606060, 3); + scene.add(ambientLight); + + const directionalLight = new THREE.DirectionalLight(0xffffff, 3); + directionalLight.position.set(1, 0.75, 0.5).normalize(); + scene.add(directionalLight); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + document.addEventListener('pointermove', onPointerMove); + document.addEventListener('pointerdown', onPointerDown); + document.addEventListener('keydown', onDocumentKeyDown); + document.addEventListener('keyup', onDocumentKeyUp); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function onPointerMove(event) { + pointer.set((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1); + + raycaster.setFromCamera(pointer, camera); + + const intersects = raycaster.intersectObjects(objects, false); + + if (intersects.length > 0) { + const intersect = intersects[0]; + + rollOverMesh.position.copy(intersect.point).add(intersect.face.normal); + rollOverMesh.position.divideScalar(50).floor().multiplyScalar(50).addScalar(25); + + render(); + } +} + +function onPointerDown(event) { + pointer.set((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1); + + raycaster.setFromCamera(pointer, camera); + + const intersects = raycaster.intersectObjects(objects, false); + + if (intersects.length > 0) { + const intersect = intersects[0]; + + // delete cube + + if (isShiftDown) { + if (intersect.object !== plane) { + scene.remove(intersect.object); + + objects.splice(objects.indexOf(intersect.object), 1); + } + + // create cube + } else { + const voxel = new THREE.Mesh(cubeGeo, cubeMaterial); + voxel.position.copy(intersect.point).add(intersect.face.normal); + voxel.position.divideScalar(50).floor().multiplyScalar(50).addScalar(25); + scene.add(voxel); + + objects.push(voxel); + } + + render(); + } +} + +function onDocumentKeyDown(event) { + switch (event.keyCode) { + case 16: + isShiftDown = true; + break; + } +} + +function onDocumentKeyUp(event) { + switch (event.keyCode) { + case 16: + isShiftDown = false; + break; + } +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_layers.ts b/examples-testing/examples/webgl_layers.ts new file mode 100644 index 000000000..8bdcda7f9 --- /dev/null +++ b/examples-testing/examples/webgl_layers.ts @@ -0,0 +1,125 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let container, stats; +let camera, scene, renderer; + +let theta = 0; +const radius = 5; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100); + camera.layers.enable(0); // enabled by default + camera.layers.enable(1); + camera.layers.enable(2); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + const light = new THREE.PointLight(0xffffff, 3, 0, 0); + light.layers.enable(0); + light.layers.enable(1); + light.layers.enable(2); + + scene.add(camera); + camera.add(light); + + const colors = [0xff0000, 0x00ff00, 0x0000ff]; + const geometry = new THREE.BoxGeometry(); + + for (let i = 0; i < 300; i++) { + const layer = i % 3; + + const object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: colors[layer] })); + + object.position.x = Math.random() * 40 - 20; + object.position.y = Math.random() * 40 - 20; + object.position.z = Math.random() * 40 - 20; + + object.rotation.x = Math.random() * 2 * Math.PI; + object.rotation.y = Math.random() * 2 * Math.PI; + object.rotation.z = Math.random() * 2 * Math.PI; + + object.scale.x = Math.random() + 0.5; + object.scale.y = Math.random() + 0.5; + object.scale.z = Math.random() + 0.5; + + object.layers.set(layer); + + scene.add(object); + } + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + const layers = { + 'toggle red': function () { + camera.layers.toggle(0); + }, + + 'toggle green': function () { + camera.layers.toggle(1); + }, + + 'toggle blue': function () { + camera.layers.toggle(2); + }, + + 'enable all': function () { + camera.layers.enableAll(); + }, + + 'disable all': function () { + camera.layers.disableAll(); + }, + }; + + // + // Init gui + const gui = new GUI(); + gui.add(layers, 'toggle red'); + gui.add(layers, 'toggle green'); + gui.add(layers, 'toggle blue'); + gui.add(layers, 'enable all'); + gui.add(layers, 'disable all'); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + theta += 0.1; + + camera.position.x = radius * Math.sin(THREE.MathUtils.degToRad(theta)); + camera.position.y = radius * Math.sin(THREE.MathUtils.degToRad(theta)); + camera.position.z = radius * Math.cos(THREE.MathUtils.degToRad(theta)); + camera.lookAt(scene.position); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_lensflares.ts b/examples-testing/examples/webgl_lensflares.ts new file mode 100644 index 000000000..230cebfa0 --- /dev/null +++ b/examples-testing/examples/webgl_lensflares.ts @@ -0,0 +1,137 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { FlyControls } from 'three/addons/controls/FlyControls.js'; +import { Lensflare, LensflareElement } from 'three/addons/objects/Lensflare.js'; + +let container, stats; + +let camera, scene, renderer; +let controls; + +const clock = new THREE.Clock(); + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + // camera + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 15000); + camera.position.z = 250; + + // scene + + scene = new THREE.Scene(); + scene.background = new THREE.Color().setHSL(0.51, 0.4, 0.01, THREE.SRGBColorSpace); + scene.fog = new THREE.Fog(scene.background, 3500, 15000); + + // world + + const s = 250; + + const geometry = new THREE.BoxGeometry(s, s, s); + const material = new THREE.MeshPhongMaterial({ color: 0xffffff, specular: 0xffffff, shininess: 50 }); + + for (let i = 0; i < 3000; i++) { + const mesh = new THREE.Mesh(geometry, material); + + mesh.position.x = 8000 * (2.0 * Math.random() - 1.0); + mesh.position.y = 8000 * (2.0 * Math.random() - 1.0); + mesh.position.z = 8000 * (2.0 * Math.random() - 1.0); + + mesh.rotation.x = Math.random() * Math.PI; + mesh.rotation.y = Math.random() * Math.PI; + mesh.rotation.z = Math.random() * Math.PI; + + mesh.matrixAutoUpdate = false; + mesh.updateMatrix(); + + scene.add(mesh); + } + + // lights + + const dirLight = new THREE.DirectionalLight(0xffffff, 0.15); + dirLight.position.set(0, -1, 0).normalize(); + dirLight.color.setHSL(0.1, 0.7, 0.5); + scene.add(dirLight); + + // lensflares + const textureLoader = new THREE.TextureLoader(); + + const textureFlare0 = textureLoader.load('textures/lensflare/lensflare0.png'); + const textureFlare3 = textureLoader.load('textures/lensflare/lensflare3.png'); + + addLight(0.55, 0.9, 0.5, 5000, 0, -1000); + addLight(0.08, 0.8, 0.5, 0, 0, -1000); + addLight(0.995, 0.5, 0.9, 5000, 5000, -1000); + + function addLight(h, s, l, x, y, z) { + const light = new THREE.PointLight(0xffffff, 1.5, 2000, 0); + light.color.setHSL(h, s, l); + light.position.set(x, y, z); + scene.add(light); + + const lensflare = new Lensflare(); + lensflare.addElement(new LensflareElement(textureFlare0, 700, 0, light.color)); + lensflare.addElement(new LensflareElement(textureFlare3, 60, 0.6)); + lensflare.addElement(new LensflareElement(textureFlare3, 70, 0.7)); + lensflare.addElement(new LensflareElement(textureFlare3, 120, 0.9)); + lensflare.addElement(new LensflareElement(textureFlare3, 70, 1)); + light.add(lensflare); + } + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + controls = new FlyControls(camera, renderer.domElement); + + controls.movementSpeed = 2500; + controls.domElement = container; + controls.rollSpeed = Math.PI / 6; + controls.autoForward = false; + controls.dragToLook = false; + + // stats + + stats = new Stats(); + container.appendChild(stats.dom); + + // events + + window.addEventListener('resize', onWindowResize); +} + +// + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + const delta = clock.getDelta(); + + controls.update(delta); + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_lightprobe.ts b/examples-testing/examples/webgl_lightprobe.ts new file mode 100644 index 000000000..2efcad52a --- /dev/null +++ b/examples-testing/examples/webgl_lightprobe.ts @@ -0,0 +1,133 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { LightProbeGenerator } from 'three/addons/lights/LightProbeGenerator.js'; + +let mesh, renderer, scene, camera; + +let gui; + +let lightProbe; +let directionalLight; + +// linear color space +const API = { + lightProbeIntensity: 1.0, + directionalLightIntensity: 0.6, + envMapIntensity: 1, +}; + +init(); + +function init() { + // renderer + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + // tone mapping + renderer.toneMapping = THREE.NoToneMapping; + + // scene + scene = new THREE.Scene(); + + // camera + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 0, 30); + + // controls + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); + controls.minDistance = 10; + controls.maxDistance = 50; + controls.enablePan = false; + + // probe + lightProbe = new THREE.LightProbe(); + scene.add(lightProbe); + + // light + directionalLight = new THREE.DirectionalLight(0xffffff, API.directionalLightIntensity); + directionalLight.position.set(10, 10, 10); + scene.add(directionalLight); + + // envmap + const genCubeUrls = function (prefix, postfix) { + return [ + prefix + 'px' + postfix, + prefix + 'nx' + postfix, + prefix + 'py' + postfix, + prefix + 'ny' + postfix, + prefix + 'pz' + postfix, + prefix + 'nz' + postfix, + ]; + }; + + const urls = genCubeUrls('textures/cube/pisa/', '.png'); + + new THREE.CubeTextureLoader().load(urls, function (cubeTexture) { + scene.background = cubeTexture; + + lightProbe.copy(LightProbeGenerator.fromCubeTexture(cubeTexture)); + + const geometry = new THREE.SphereGeometry(5, 64, 32); + //const geometry = new THREE.TorusKnotGeometry( 4, 1.5, 256, 32, 2, 3 ); + + const material = new THREE.MeshStandardMaterial({ + color: 0xffffff, + metalness: 0, + roughness: 0, + envMap: cubeTexture, + envMapIntensity: API.envMapIntensity, + }); + + // mesh + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + render(); + }); + + // gui + gui = new GUI({ title: 'Intensity' }); + + gui.add(API, 'lightProbeIntensity', 0, 1, 0.02) + .name('light probe') + .onChange(function () { + lightProbe.intensity = API.lightProbeIntensity; + render(); + }); + + gui.add(API, 'directionalLightIntensity', 0, 1, 0.02) + .name('directional light') + .onChange(function () { + directionalLight.intensity = API.directionalLightIntensity; + render(); + }); + + gui.add(API, 'envMapIntensity', 0, 1, 0.02) + .name('envMap') + .onChange(function () { + mesh.material.envMapIntensity = API.envMapIntensity; + render(); + }); + + // listener + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_lightprobe_cubecamera.ts b/examples-testing/examples/webgl_lightprobe_cubecamera.ts new file mode 100644 index 000000000..c714d2978 --- /dev/null +++ b/examples-testing/examples/webgl_lightprobe_cubecamera.ts @@ -0,0 +1,83 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { LightProbeHelper } from 'three/addons/helpers/LightProbeHelper.js'; +import { LightProbeGenerator } from 'three/addons/lights/LightProbeGenerator.js'; + +let renderer, scene, camera, cubeCamera; + +let lightProbe; + +init(); + +function init() { + // renderer + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + // scene + scene = new THREE.Scene(); + + // camera + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 0, 30); + + const cubeRenderTarget = new THREE.WebGLCubeRenderTarget(256); + + cubeCamera = new THREE.CubeCamera(1, 1000, cubeRenderTarget); + + // controls + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); + controls.minDistance = 10; + controls.maxDistance = 50; + controls.enablePan = false; + + // probe + lightProbe = new THREE.LightProbe(); + scene.add(lightProbe); + + // envmap + const genCubeUrls = function (prefix, postfix) { + return [ + prefix + 'px' + postfix, + prefix + 'nx' + postfix, + prefix + 'py' + postfix, + prefix + 'ny' + postfix, + prefix + 'pz' + postfix, + prefix + 'nz' + postfix, + ]; + }; + + const urls = genCubeUrls('textures/cube/pisa/', '.png'); + + new THREE.CubeTextureLoader().load(urls, function (cubeTexture) { + scene.background = cubeTexture; + + cubeCamera.update(renderer, scene); + + lightProbe.copy(LightProbeGenerator.fromCubeRenderTarget(renderer, cubeRenderTarget)); + + scene.add(new LightProbeHelper(lightProbe, 5)); + + render(); + }); + + // listener + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_lights_hemisphere.ts b/examples-testing/examples/webgl_lights_hemisphere.ts new file mode 100644 index 000000000..15bc76099 --- /dev/null +++ b/examples-testing/examples/webgl_lights_hemisphere.ts @@ -0,0 +1,188 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +let camera, scene, renderer; +const mixers = []; +let stats; + +const clock = new THREE.Clock(); + +init(); + +function init() { + const container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 5000); + camera.position.set(0, 0, 250); + + scene = new THREE.Scene(); + scene.background = new THREE.Color().setHSL(0.6, 0, 1); + scene.fog = new THREE.Fog(scene.background, 1, 5000); + + // LIGHTS + + const hemiLight = new THREE.HemisphereLight(0xffffff, 0xffffff, 2); + hemiLight.color.setHSL(0.6, 1, 0.6); + hemiLight.groundColor.setHSL(0.095, 1, 0.75); + hemiLight.position.set(0, 50, 0); + scene.add(hemiLight); + + const hemiLightHelper = new THREE.HemisphereLightHelper(hemiLight, 10); + scene.add(hemiLightHelper); + + // + + const dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.color.setHSL(0.1, 1, 0.95); + dirLight.position.set(-1, 1.75, 1); + dirLight.position.multiplyScalar(30); + scene.add(dirLight); + + dirLight.castShadow = true; + + dirLight.shadow.mapSize.width = 2048; + dirLight.shadow.mapSize.height = 2048; + + const d = 50; + + dirLight.shadow.camera.left = -d; + dirLight.shadow.camera.right = d; + dirLight.shadow.camera.top = d; + dirLight.shadow.camera.bottom = -d; + + dirLight.shadow.camera.far = 3500; + dirLight.shadow.bias = -0.0001; + + const dirLightHelper = new THREE.DirectionalLightHelper(dirLight, 10); + scene.add(dirLightHelper); + + // GROUND + + const groundGeo = new THREE.PlaneGeometry(10000, 10000); + const groundMat = new THREE.MeshLambertMaterial({ color: 0xffffff }); + groundMat.color.setHSL(0.095, 1, 0.75); + + const ground = new THREE.Mesh(groundGeo, groundMat); + ground.position.y = -33; + ground.rotation.x = -Math.PI / 2; + ground.receiveShadow = true; + scene.add(ground); + + // SKYDOME + + const vertexShader = document.getElementById('vertexShader').textContent; + const fragmentShader = document.getElementById('fragmentShader').textContent; + const uniforms = { + topColor: { value: new THREE.Color(0x0077ff) }, + bottomColor: { value: new THREE.Color(0xffffff) }, + offset: { value: 33 }, + exponent: { value: 0.6 }, + }; + uniforms['topColor'].value.copy(hemiLight.color); + + scene.fog.color.copy(uniforms['bottomColor'].value); + + const skyGeo = new THREE.SphereGeometry(4000, 32, 15); + const skyMat = new THREE.ShaderMaterial({ + uniforms: uniforms, + vertexShader: vertexShader, + fragmentShader: fragmentShader, + side: THREE.BackSide, + }); + + const sky = new THREE.Mesh(skyGeo, skyMat); + scene.add(sky); + + // MODEL + + const loader = new GLTFLoader(); + + loader.load('models/gltf/Flamingo.glb', function (gltf) { + const mesh = gltf.scene.children[0]; + + const s = 0.35; + mesh.scale.set(s, s, s); + mesh.position.y = 15; + mesh.rotation.y = -1; + + mesh.castShadow = true; + mesh.receiveShadow = true; + + scene.add(mesh); + + const mixer = new THREE.AnimationMixer(mesh); + mixer.clipAction(gltf.animations[0]).setDuration(1).play(); + mixers.push(mixer); + }); + + // RENDERER + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + renderer.shadowMap.enabled = true; + + // STATS + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + const params = { + toggleHemisphereLight: function () { + hemiLight.visible = !hemiLight.visible; + hemiLightHelper.visible = !hemiLightHelper.visible; + }, + toggleDirectionalLight: function () { + dirLight.visible = !dirLight.visible; + dirLightHelper.visible = !dirLightHelper.visible; + }, + shadowIntensity: 1, + }; + + const gui = new GUI(); + + gui.add(params, 'toggleHemisphereLight').name('toggle hemisphere light'); + gui.add(params, 'toggleDirectionalLight').name('toggle directional light'); + gui.add(params, 'shadowIntensity', 0, 1) + .name('shadow intensity') + .onChange(value => { + dirLight.shadow.intensity = value; + }); + gui.open(); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + const delta = clock.getDelta(); + + for (let i = 0; i < mixers.length; i++) { + mixers[i].update(delta); + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_lights_physical.ts b/examples-testing/examples/webgl_lights_physical.ts new file mode 100644 index 000000000..707ef200e --- /dev/null +++ b/examples-testing/examples/webgl_lights_physical.ts @@ -0,0 +1,237 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer, bulbLight, bulbMat, hemiLight, stats; +let ballMat, cubeMat, floorMat; + +let previousShadowMap = false; + +// ref for lumens: http://www.power-sure.com/lumens.htm +const bulbLuminousPowers = { + '110000 lm (1000W)': 110000, + '3500 lm (300W)': 3500, + '1700 lm (100W)': 1700, + '800 lm (60W)': 800, + '400 lm (40W)': 400, + '180 lm (25W)': 180, + '20 lm (4W)': 20, + Off: 0, +}; + +// ref for solar irradiances: https://en.wikipedia.org/wiki/Lux +const hemiLuminousIrradiances = { + '0.0001 lx (Moonless Night)': 0.0001, + '0.002 lx (Night Airglow)': 0.002, + '0.5 lx (Full Moon)': 0.5, + '3.4 lx (City Twilight)': 3.4, + '50 lx (Living Room)': 50, + '100 lx (Very Overcast)': 100, + '350 lx (Office Room)': 350, + '400 lx (Sunrise/Sunset)': 400, + '1000 lx (Overcast)': 1000, + '18000 lx (Daylight)': 18000, + '50000 lx (Direct Sun)': 50000, +}; + +const params = { + shadows: true, + exposure: 0.68, + bulbPower: Object.keys(bulbLuminousPowers)[4], + hemiIrradiance: Object.keys(hemiLuminousIrradiances)[0], +}; + +init(); + +function init() { + const container = document.getElementById('container'); + + stats = new Stats(); + container.appendChild(stats.dom); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.x = -4; + camera.position.z = 4; + camera.position.y = 2; + + scene = new THREE.Scene(); + + const bulbGeometry = new THREE.SphereGeometry(0.02, 16, 8); + bulbLight = new THREE.PointLight(0xffee88, 1, 100, 2); + + bulbMat = new THREE.MeshStandardMaterial({ + emissive: 0xffffee, + emissiveIntensity: 1, + color: 0x000000, + }); + bulbLight.add(new THREE.Mesh(bulbGeometry, bulbMat)); + bulbLight.position.set(0, 2, 0); + bulbLight.castShadow = true; + scene.add(bulbLight); + + hemiLight = new THREE.HemisphereLight(0xddeeff, 0x0f0e0d, 0.02); + scene.add(hemiLight); + + floorMat = new THREE.MeshStandardMaterial({ + roughness: 0.8, + color: 0xffffff, + metalness: 0.2, + bumpScale: 1, + }); + const textureLoader = new THREE.TextureLoader(); + textureLoader.load('textures/hardwood2_diffuse.jpg', function (map) { + map.wrapS = THREE.RepeatWrapping; + map.wrapT = THREE.RepeatWrapping; + map.anisotropy = 4; + map.repeat.set(10, 24); + map.colorSpace = THREE.SRGBColorSpace; + floorMat.map = map; + floorMat.needsUpdate = true; + }); + textureLoader.load('textures/hardwood2_bump.jpg', function (map) { + map.wrapS = THREE.RepeatWrapping; + map.wrapT = THREE.RepeatWrapping; + map.anisotropy = 4; + map.repeat.set(10, 24); + floorMat.bumpMap = map; + floorMat.needsUpdate = true; + }); + textureLoader.load('textures/hardwood2_roughness.jpg', function (map) { + map.wrapS = THREE.RepeatWrapping; + map.wrapT = THREE.RepeatWrapping; + map.anisotropy = 4; + map.repeat.set(10, 24); + floorMat.roughnessMap = map; + floorMat.needsUpdate = true; + }); + + cubeMat = new THREE.MeshStandardMaterial({ + roughness: 0.7, + color: 0xffffff, + bumpScale: 1, + metalness: 0.2, + }); + textureLoader.load('textures/brick_diffuse.jpg', function (map) { + map.wrapS = THREE.RepeatWrapping; + map.wrapT = THREE.RepeatWrapping; + map.anisotropy = 4; + map.repeat.set(1, 1); + map.colorSpace = THREE.SRGBColorSpace; + cubeMat.map = map; + cubeMat.needsUpdate = true; + }); + textureLoader.load('textures/brick_bump.jpg', function (map) { + map.wrapS = THREE.RepeatWrapping; + map.wrapT = THREE.RepeatWrapping; + map.anisotropy = 4; + map.repeat.set(1, 1); + cubeMat.bumpMap = map; + cubeMat.needsUpdate = true; + }); + + ballMat = new THREE.MeshStandardMaterial({ + color: 0xffffff, + roughness: 0.5, + metalness: 1.0, + }); + textureLoader.load('textures/planets/earth_atmos_2048.jpg', function (map) { + map.anisotropy = 4; + map.colorSpace = THREE.SRGBColorSpace; + ballMat.map = map; + ballMat.needsUpdate = true; + }); + textureLoader.load('textures/planets/earth_specular_2048.jpg', function (map) { + map.anisotropy = 4; + map.colorSpace = THREE.SRGBColorSpace; + ballMat.metalnessMap = map; + ballMat.needsUpdate = true; + }); + + const floorGeometry = new THREE.PlaneGeometry(20, 20); + const floorMesh = new THREE.Mesh(floorGeometry, floorMat); + floorMesh.receiveShadow = true; + floorMesh.rotation.x = -Math.PI / 2.0; + scene.add(floorMesh); + + const ballGeometry = new THREE.SphereGeometry(0.25, 32, 32); + const ballMesh = new THREE.Mesh(ballGeometry, ballMat); + ballMesh.position.set(1, 0.25, 1); + ballMesh.rotation.y = Math.PI; + ballMesh.castShadow = true; + scene.add(ballMesh); + + const boxGeometry = new THREE.BoxGeometry(0.5, 0.5, 0.5); + const boxMesh = new THREE.Mesh(boxGeometry, cubeMat); + boxMesh.position.set(-0.5, 0.25, -1); + boxMesh.castShadow = true; + scene.add(boxMesh); + + const boxMesh2 = new THREE.Mesh(boxGeometry, cubeMat); + boxMesh2.position.set(0, 0.25, -5); + boxMesh2.castShadow = true; + scene.add(boxMesh2); + + const boxMesh3 = new THREE.Mesh(boxGeometry, cubeMat); + boxMesh3.position.set(7, 0.25, 0); + boxMesh3.castShadow = true; + scene.add(boxMesh3); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + renderer.toneMapping = THREE.ReinhardToneMapping; + container.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 1; + controls.maxDistance = 20; + + window.addEventListener('resize', onWindowResize); + + const gui = new GUI(); + + gui.add(params, 'hemiIrradiance', Object.keys(hemiLuminousIrradiances)); + gui.add(params, 'bulbPower', Object.keys(bulbLuminousPowers)); + gui.add(params, 'exposure', 0, 1); + gui.add(params, 'shadows'); + gui.open(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + renderer.toneMappingExposure = Math.pow(params.exposure, 5.0); // to allow for very bright scenes. + renderer.shadowMap.enabled = params.shadows; + bulbLight.castShadow = params.shadows; + + if (params.shadows !== previousShadowMap) { + ballMat.needsUpdate = true; + cubeMat.needsUpdate = true; + floorMat.needsUpdate = true; + previousShadowMap = params.shadows; + } + + bulbLight.power = bulbLuminousPowers[params.bulbPower]; + bulbMat.emissiveIntensity = bulbLight.intensity / Math.pow(0.02, 2.0); // convert from intensity to irradiance at bulb surface + + hemiLight.intensity = hemiLuminousIrradiances[params.hemiIrradiance]; + const time = Date.now() * 0.0005; + + bulbLight.position.y = Math.cos(time) * 0.75 + 1.25; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_lights_pointlights.ts b/examples-testing/examples/webgl_lights_pointlights.ts new file mode 100644 index 000000000..ea95070ce --- /dev/null +++ b/examples-testing/examples/webgl_lights_pointlights.ts @@ -0,0 +1,100 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; + +let camera, scene, renderer, light1, light2, light3, light4, object, stats; + +const clock = new THREE.Clock(); + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 100; + + scene = new THREE.Scene(); + + //model + + const loader = new OBJLoader(); + loader.load('models/obj/walt/WaltHead.obj', function (obj) { + object = obj; + object.scale.multiplyScalar(0.8); + object.position.y = -30; + scene.add(object); + }); + + const sphere = new THREE.SphereGeometry(0.5, 16, 8); + + //lights + + light1 = new THREE.PointLight(0xff0040, 400); + light1.add(new THREE.Mesh(sphere, new THREE.MeshBasicMaterial({ color: 0xff0040 }))); + scene.add(light1); + + light2 = new THREE.PointLight(0x0040ff, 400); + light2.add(new THREE.Mesh(sphere, new THREE.MeshBasicMaterial({ color: 0x0040ff }))); + scene.add(light2); + + light3 = new THREE.PointLight(0x80ff80, 400); + light3.add(new THREE.Mesh(sphere, new THREE.MeshBasicMaterial({ color: 0x80ff80 }))); + scene.add(light3); + + light4 = new THREE.PointLight(0xffaa00, 400); + light4.add(new THREE.Mesh(sphere, new THREE.MeshBasicMaterial({ color: 0xffaa00 }))); + scene.add(light4); + + //renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + //stats + + stats = new Stats(); + document.body.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + const time = Date.now() * 0.0005; + const delta = clock.getDelta(); + + if (object) object.rotation.y -= 0.5 * delta; + + light1.position.x = Math.sin(time * 0.7) * 30; + light1.position.y = Math.cos(time * 0.5) * 40; + light1.position.z = Math.cos(time * 0.3) * 30; + + light2.position.x = Math.cos(time * 0.3) * 30; + light2.position.y = Math.sin(time * 0.5) * 40; + light2.position.z = Math.sin(time * 0.7) * 30; + + light3.position.x = Math.sin(time * 0.7) * 30; + light3.position.y = Math.cos(time * 0.3) * 40; + light3.position.z = Math.sin(time * 0.5) * 30; + + light4.position.x = Math.sin(time * 0.3) * 30; + light4.position.y = Math.cos(time * 0.7) * 40; + light4.position.z = Math.sin(time * 0.5) * 30; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_lights_rectarealight.ts b/examples-testing/examples/webgl_lights_rectarealight.ts new file mode 100644 index 000000000..b841fa6b5 --- /dev/null +++ b/examples-testing/examples/webgl_lights_rectarealight.ts @@ -0,0 +1,79 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { RectAreaLightHelper } from 'three/addons/helpers/RectAreaLightHelper.js'; +import { RectAreaLightUniformsLib } from 'three/addons/lights/RectAreaLightUniformsLib.js'; + +let renderer, scene, camera; +let stats, meshKnot; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animation); + document.body.appendChild(renderer.domElement); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 5, -15); + + scene = new THREE.Scene(); + + RectAreaLightUniformsLib.init(); + + const rectLight1 = new THREE.RectAreaLight(0xff0000, 5, 4, 10); + rectLight1.position.set(-5, 5, 5); + scene.add(rectLight1); + + const rectLight2 = new THREE.RectAreaLight(0x00ff00, 5, 4, 10); + rectLight2.position.set(0, 5, 5); + scene.add(rectLight2); + + const rectLight3 = new THREE.RectAreaLight(0x0000ff, 5, 4, 10); + rectLight3.position.set(5, 5, 5); + scene.add(rectLight3); + + scene.add(new RectAreaLightHelper(rectLight1)); + scene.add(new RectAreaLightHelper(rectLight2)); + scene.add(new RectAreaLightHelper(rectLight3)); + + const geoFloor = new THREE.BoxGeometry(2000, 0.1, 2000); + const matStdFloor = new THREE.MeshStandardMaterial({ color: 0xbcbcbc, roughness: 0.1, metalness: 0 }); + const mshStdFloor = new THREE.Mesh(geoFloor, matStdFloor); + scene.add(mshStdFloor); + + const geoKnot = new THREE.TorusKnotGeometry(1.5, 0.5, 200, 16); + const matKnot = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0, metalness: 0 }); + meshKnot = new THREE.Mesh(geoKnot, matKnot); + meshKnot.position.set(0, 5, 0); + scene.add(meshKnot); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.copy(meshKnot.position); + controls.update(); + + // + + window.addEventListener('resize', onWindowResize); + + stats = new Stats(); + document.body.appendChild(stats.dom); +} + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); +} + +function animation(time) { + meshKnot.rotation.y = time / 1000; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_lights_spotlight.ts b/examples-testing/examples/webgl_lights_spotlight.ts new file mode 100644 index 000000000..894abaf6f --- /dev/null +++ b/examples-testing/examples/webgl_lights_spotlight.ts @@ -0,0 +1,183 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { PLYLoader } from 'three/addons/loaders/PLYLoader.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let renderer, scene, camera; + +let spotLight, lightHelper; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + renderer.shadowMap.enabled = true; + renderer.shadowMap.type = THREE.PCFSoftShadowMap; + + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1; + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(7, 4, 1); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 2; + controls.maxDistance = 10; + controls.maxPolarAngle = Math.PI / 2; + controls.target.set(0, 1, 0); + controls.update(); + + const ambient = new THREE.HemisphereLight(0xffffff, 0x8d8d8d, 0.15); + scene.add(ambient); + + const loader = new THREE.TextureLoader().setPath('textures/'); + const filenames = ['disturb.jpg', 'colors.png', 'uv_grid_opengl.jpg']; + + const textures = { none: null }; + + for (let i = 0; i < filenames.length; i++) { + const filename = filenames[i]; + + const texture = loader.load(filename); + texture.minFilter = THREE.LinearFilter; + texture.magFilter = THREE.LinearFilter; + texture.colorSpace = THREE.SRGBColorSpace; + + textures[filename] = texture; + } + + spotLight = new THREE.SpotLight(0xffffff, 100); + spotLight.position.set(2.5, 5, 2.5); + spotLight.angle = Math.PI / 6; + spotLight.penumbra = 1; + spotLight.decay = 2; + spotLight.distance = 0; + spotLight.map = textures['disturb.jpg']; + + spotLight.castShadow = true; + spotLight.shadow.mapSize.width = 1024; + spotLight.shadow.mapSize.height = 1024; + spotLight.shadow.camera.near = 1; + spotLight.shadow.camera.far = 10; + spotLight.shadow.focus = 1; + scene.add(spotLight); + + lightHelper = new THREE.SpotLightHelper(spotLight); + scene.add(lightHelper); + + // + + const geometry = new THREE.PlaneGeometry(200, 200); + const material = new THREE.MeshLambertMaterial({ color: 0xbcbcbc }); + + const mesh = new THREE.Mesh(geometry, material); + mesh.position.set(0, -1, 0); + mesh.rotation.x = -Math.PI / 2; + mesh.receiveShadow = true; + scene.add(mesh); + + // + + new PLYLoader().load('models/ply/binary/Lucy100k.ply', function (geometry) { + geometry.scale(0.0024, 0.0024, 0.0024); + geometry.computeVertexNormals(); + + const material = new THREE.MeshLambertMaterial(); + + const mesh = new THREE.Mesh(geometry, material); + mesh.rotation.y = -Math.PI / 2; + mesh.position.y = 0.8; + mesh.castShadow = true; + mesh.receiveShadow = true; + scene.add(mesh); + }); + + window.addEventListener('resize', onWindowResize); + + // GUI + + const gui = new GUI(); + + const params = { + map: textures['disturb.jpg'], + color: spotLight.color.getHex(), + intensity: spotLight.intensity, + distance: spotLight.distance, + angle: spotLight.angle, + penumbra: spotLight.penumbra, + decay: spotLight.decay, + focus: spotLight.shadow.focus, + shadows: true, + }; + + gui.add(params, 'map', textures).onChange(function (val) { + spotLight.map = val; + }); + + gui.addColor(params, 'color').onChange(function (val) { + spotLight.color.setHex(val); + }); + + gui.add(params, 'intensity', 0, 500).onChange(function (val) { + spotLight.intensity = val; + }); + + gui.add(params, 'distance', 0, 20).onChange(function (val) { + spotLight.distance = val; + }); + + gui.add(params, 'angle', 0, Math.PI / 3).onChange(function (val) { + spotLight.angle = val; + }); + + gui.add(params, 'penumbra', 0, 1).onChange(function (val) { + spotLight.penumbra = val; + }); + + gui.add(params, 'decay', 1, 2).onChange(function (val) { + spotLight.decay = val; + }); + + gui.add(params, 'focus', 0, 1).onChange(function (val) { + spotLight.shadow.focus = val; + }); + + gui.add(params, 'shadows').onChange(function (val) { + renderer.shadowMap.enabled = val; + + scene.traverse(function (child) { + if (child.material) { + child.material.needsUpdate = true; + } + }); + }); + + gui.open(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const time = performance.now() / 3000; + + spotLight.position.x = Math.cos(time) * 2.5; + spotLight.position.z = Math.sin(time) * 2.5; + + lightHelper.update(); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_lights_spotlights.ts b/examples-testing/examples/webgl_lights_spotlights.ts new file mode 100644 index 000000000..70caf5a58 --- /dev/null +++ b/examples-testing/examples/webgl_lights_spotlights.ts @@ -0,0 +1,133 @@ +import * as THREE from 'three'; +import TWEEN from 'three/addons/libs/tween.module.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +const renderer = new THREE.WebGLRenderer({ antialias: true }); +renderer.setPixelRatio(window.devicePixelRatio); +renderer.setSize(window.innerWidth, window.innerHeight); +renderer.setAnimationLoop(animate); + +const camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 100); + +const controls = new OrbitControls(camera, renderer.domElement); + +const scene = new THREE.Scene(); + +const matFloor = new THREE.MeshPhongMaterial({ color: 0x808080 }); +const matBox = new THREE.MeshPhongMaterial({ color: 0xaaaaaa }); + +const geoFloor = new THREE.PlaneGeometry(100, 100); +const geoBox = new THREE.BoxGeometry(0.3, 0.1, 0.2); + +const mshFloor = new THREE.Mesh(geoFloor, matFloor); +mshFloor.rotation.x = -Math.PI * 0.5; +const mshBox = new THREE.Mesh(geoBox, matBox); + +const ambient = new THREE.AmbientLight(0x444444); + +const spotLight1 = createSpotlight(0xff7f00); +const spotLight2 = createSpotlight(0x00ff7f); +const spotLight3 = createSpotlight(0x7f00ff); + +let lightHelper1, lightHelper2, lightHelper3; + +function init() { + renderer.shadowMap.enabled = true; + renderer.shadowMap.type = THREE.PCFSoftShadowMap; + + camera.position.set(4.6, 2.2, -2.1); + + spotLight1.position.set(1.5, 4, 4.5); + spotLight2.position.set(0, 4, 3.5); + spotLight3.position.set(-1.5, 4, 4.5); + + lightHelper1 = new THREE.SpotLightHelper(spotLight1); + lightHelper2 = new THREE.SpotLightHelper(spotLight2); + lightHelper3 = new THREE.SpotLightHelper(spotLight3); + + mshFloor.receiveShadow = true; + mshFloor.position.set(0, -0.05, 0); + + mshBox.castShadow = true; + mshBox.receiveShadow = true; + mshBox.position.set(0, 0.5, 0); + + scene.add(mshFloor); + scene.add(mshBox); + scene.add(ambient); + scene.add(spotLight1, spotLight2, spotLight3); + scene.add(lightHelper1, lightHelper2, lightHelper3); + + document.body.appendChild(renderer.domElement); + window.addEventListener('resize', onWindowResize); + + controls.target.set(0, 0.5, 0); + controls.maxPolarAngle = Math.PI / 2; + controls.minDistance = 1; + controls.maxDistance = 10; + controls.update(); +} + +function createSpotlight(color) { + const newObj = new THREE.SpotLight(color, 10); + + newObj.castShadow = true; + newObj.angle = 0.3; + newObj.penumbra = 0.2; + newObj.decay = 2; + newObj.distance = 50; + + return newObj; +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function tween(light) { + new TWEEN.Tween(light) + .to( + { + angle: Math.random() * 0.7 + 0.1, + penumbra: Math.random() + 1, + }, + Math.random() * 3000 + 2000, + ) + .easing(TWEEN.Easing.Quadratic.Out) + .start(); + + new TWEEN.Tween(light.position) + .to( + { + x: Math.random() * 3 - 1.5, + y: Math.random() * 1 + 1.5, + z: Math.random() * 3 - 1.5, + }, + Math.random() * 3000 + 2000, + ) + .easing(TWEEN.Easing.Quadratic.Out) + .start(); +} + +function updateTweens() { + tween(spotLight1); + tween(spotLight2); + tween(spotLight3); + + setTimeout(updateTweens, 5000); +} + +function animate() { + TWEEN.update(); + + if (lightHelper1) lightHelper1.update(); + if (lightHelper2) lightHelper2.update(); + if (lightHelper3) lightHelper3.update(); + + renderer.render(scene, camera); +} + +init(); +updateTweens(); diff --git a/examples-testing/examples/webgl_lines_colors.ts b/examples-testing/examples/webgl_lines_colors.ts new file mode 100644 index 000000000..9da19ee2e --- /dev/null +++ b/examples-testing/examples/webgl_lines_colors.ts @@ -0,0 +1,181 @@ +import * as THREE from 'three'; + +import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js'; + +let mouseX = 0, + mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +let camera, scene, renderer; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(33, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 1000; + + scene = new THREE.Scene(); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + const hilbertPoints = GeometryUtils.hilbert3D(new THREE.Vector3(0, 0, 0), 200.0, 1, 0, 1, 2, 3, 4, 5, 6, 7); + + const geometry1 = new THREE.BufferGeometry(); + const geometry2 = new THREE.BufferGeometry(); + const geometry3 = new THREE.BufferGeometry(); + + const subdivisions = 6; + + let vertices = []; + let colors1 = []; + let colors2 = []; + let colors3 = []; + + const point = new THREE.Vector3(); + const color = new THREE.Color(); + + const spline = new THREE.CatmullRomCurve3(hilbertPoints); + + for (let i = 0; i < hilbertPoints.length * subdivisions; i++) { + const t = i / (hilbertPoints.length * subdivisions); + spline.getPoint(t, point); + + vertices.push(point.x, point.y, point.z); + + color.setHSL(0.6, 1.0, Math.max(0, -point.x / 200) + 0.5, THREE.SRGBColorSpace); + colors1.push(color.r, color.g, color.b); + + color.setHSL(0.9, 1.0, Math.max(0, -point.y / 200) + 0.5, THREE.SRGBColorSpace); + colors2.push(color.r, color.g, color.b); + + color.setHSL(i / (hilbertPoints.length * subdivisions), 1.0, 0.5, THREE.SRGBColorSpace); + colors3.push(color.r, color.g, color.b); + } + + geometry1.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); + geometry2.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); + geometry3.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); + + geometry1.setAttribute('color', new THREE.Float32BufferAttribute(colors1, 3)); + geometry2.setAttribute('color', new THREE.Float32BufferAttribute(colors2, 3)); + geometry3.setAttribute('color', new THREE.Float32BufferAttribute(colors3, 3)); + + // + + const geometry4 = new THREE.BufferGeometry(); + const geometry5 = new THREE.BufferGeometry(); + const geometry6 = new THREE.BufferGeometry(); + + vertices = []; + colors1 = []; + colors2 = []; + colors3 = []; + + for (let i = 0; i < hilbertPoints.length; i++) { + const point = hilbertPoints[i]; + + vertices.push(point.x, point.y, point.z); + + color.setHSL(0.6, 1.0, Math.max(0, (200 - hilbertPoints[i].x) / 400) * 0.5 + 0.5, THREE.SRGBColorSpace); + colors1.push(color.r, color.g, color.b); + + color.setHSL(0.3, 1.0, Math.max(0, (200 + hilbertPoints[i].x) / 400) * 0.5, THREE.SRGBColorSpace); + colors2.push(color.r, color.g, color.b); + + color.setHSL(i / hilbertPoints.length, 1.0, 0.5, THREE.SRGBColorSpace); + colors3.push(color.r, color.g, color.b); + } + + geometry4.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); + geometry5.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); + geometry6.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); + + geometry4.setAttribute('color', new THREE.Float32BufferAttribute(colors1, 3)); + geometry5.setAttribute('color', new THREE.Float32BufferAttribute(colors2, 3)); + geometry6.setAttribute('color', new THREE.Float32BufferAttribute(colors3, 3)); + + // Create lines and add to scene + + const material = new THREE.LineBasicMaterial({ color: 0xffffff, vertexColors: true }); + + let line, p; + const scale = 0.3, + d = 225; + + const parameters = [ + [material, scale * 1.5, [-d, -d / 2, 0], geometry1], + [material, scale * 1.5, [0, -d / 2, 0], geometry2], + [material, scale * 1.5, [d, -d / 2, 0], geometry3], + + [material, scale * 1.5, [-d, d / 2, 0], geometry4], + [material, scale * 1.5, [0, d / 2, 0], geometry5], + [material, scale * 1.5, [d, d / 2, 0], geometry6], + ]; + + for (let i = 0; i < parameters.length; i++) { + p = parameters[i]; + line = new THREE.Line(p[3], p[0]); + line.scale.x = line.scale.y = line.scale.z = p[1]; + line.position.x = p[2][0]; + line.position.y = p[2][1]; + line.position.z = p[2][2]; + scene.add(line); + } + + // + + document.body.style.touchAction = 'none'; + document.body.addEventListener('pointermove', onPointerMove); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function onPointerMove(event) { + if (event.isPrimary === false) return; + + mouseX = event.clientX - windowHalfX; + mouseY = event.clientY - windowHalfY; +} + +// + +function animate() { + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-mouseY + 200 - camera.position.y) * 0.05; + + camera.lookAt(scene.position); + + const time = Date.now() * 0.0005; + + for (let i = 0; i < scene.children.length; i++) { + const object = scene.children[i]; + + if (object.isLine) { + object.rotation.y = time * (i % 2 ? 1 : -1); + } + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_lines_dashed.ts b/examples-testing/examples/webgl_lines_dashed.ts new file mode 100644 index 000000000..4849e7c3a --- /dev/null +++ b/examples-testing/examples/webgl_lines_dashed.ts @@ -0,0 +1,186 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js'; + +let renderer, scene, camera, stats; +const objects = []; + +const WIDTH = window.innerWidth, + HEIGHT = window.innerHeight; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(60, WIDTH / HEIGHT, 1, 200); + camera.position.z = 150; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x111111); + scene.fog = new THREE.Fog(0x111111, 150, 200); + + const subdivisions = 6; + const recursion = 1; + + const points = GeometryUtils.hilbert3D(new THREE.Vector3(0, 0, 0), 25.0, recursion, 0, 1, 2, 3, 4, 5, 6, 7); + const spline = new THREE.CatmullRomCurve3(points); + + const samples = spline.getPoints(points.length * subdivisions); + const geometrySpline = new THREE.BufferGeometry().setFromPoints(samples); + + const line = new THREE.Line( + geometrySpline, + new THREE.LineDashedMaterial({ color: 0xffffff, dashSize: 1, gapSize: 0.5 }), + ); + line.computeLineDistances(); + + objects.push(line); + scene.add(line); + + const geometryBox = box(50, 50, 50); + + const lineSegments = new THREE.LineSegments( + geometryBox, + new THREE.LineDashedMaterial({ color: 0xffaa00, dashSize: 3, gapSize: 1 }), + ); + lineSegments.computeLineDistances(); + + objects.push(lineSegments); + scene.add(lineSegments); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(WIDTH, HEIGHT); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function box(width, height, depth) { + (width = width * 0.5), (height = height * 0.5), (depth = depth * 0.5); + + const geometry = new THREE.BufferGeometry(); + const position = []; + + position.push( + -width, + -height, + -depth, + -width, + height, + -depth, + + -width, + height, + -depth, + width, + height, + -depth, + + width, + height, + -depth, + width, + -height, + -depth, + + width, + -height, + -depth, + -width, + -height, + -depth, + + -width, + -height, + depth, + -width, + height, + depth, + + -width, + height, + depth, + width, + height, + depth, + + width, + height, + depth, + width, + -height, + depth, + + width, + -height, + depth, + -width, + -height, + depth, + + -width, + -height, + -depth, + -width, + -height, + depth, + + -width, + height, + -depth, + -width, + height, + depth, + + width, + height, + -depth, + width, + height, + depth, + + width, + -height, + -depth, + width, + -height, + depth, + ); + + geometry.setAttribute('position', new THREE.Float32BufferAttribute(position, 3)); + + return geometry; +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + const time = Date.now() * 0.001; + + scene.traverse(function (object) { + if (object.isLine) { + object.rotation.x = 0.25 * time; + object.rotation.y = 0.25 * time; + } + }); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_lines_fat.ts b/examples-testing/examples/webgl_lines_fat.ts new file mode 100644 index 000000000..34c2e2d5e --- /dev/null +++ b/examples-testing/examples/webgl_lines_fat.ts @@ -0,0 +1,251 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GPUStatsPanel } from 'three/addons/utils/GPUStatsPanel.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { Line2 } from 'three/addons/lines/Line2.js'; +import { LineMaterial } from 'three/addons/lines/LineMaterial.js'; +import { LineGeometry } from 'three/addons/lines/LineGeometry.js'; +import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js'; + +let line, renderer, scene, camera, camera2, controls; +let line1; +let matLine, matLineBasic, matLineDashed; +let stats, gpuPanel; +let gui; + +// viewport +let insetWidth; +let insetHeight; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setClearColor(0x000000, 0.0); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(-40, 0, 60); + + camera2 = new THREE.PerspectiveCamera(40, 1, 1, 1000); + camera2.position.copy(camera.position); + + controls = new OrbitControls(camera, renderer.domElement); + controls.enableDamping = true; + controls.minDistance = 10; + controls.maxDistance = 500; + + // Position and THREE.Color Data + + const positions = []; + const colors = []; + + const points = GeometryUtils.hilbert3D(new THREE.Vector3(0, 0, 0), 20.0, 1, 0, 1, 2, 3, 4, 5, 6, 7); + + const spline = new THREE.CatmullRomCurve3(points); + const divisions = Math.round(12 * points.length); + const point = new THREE.Vector3(); + const color = new THREE.Color(); + + for (let i = 0, l = divisions; i < l; i++) { + const t = i / l; + + spline.getPoint(t, point); + positions.push(point.x, point.y, point.z); + + color.setHSL(t, 1.0, 0.5, THREE.SRGBColorSpace); + colors.push(color.r, color.g, color.b); + } + + // Line2 ( LineGeometry, LineMaterial ) + + const geometry = new LineGeometry(); + geometry.setPositions(positions); + geometry.setColors(colors); + + matLine = new LineMaterial({ + color: 0xffffff, + linewidth: 5, // in world units with size attenuation, pixels otherwise + vertexColors: true, + + dashed: false, + alphaToCoverage: true, + }); + + line = new Line2(geometry, matLine); + line.computeLineDistances(); + line.scale.set(1, 1, 1); + scene.add(line); + + // THREE.Line ( THREE.BufferGeometry, THREE.LineBasicMaterial ) - rendered with gl.LINE_STRIP + + const geo = new THREE.BufferGeometry(); + geo.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); + geo.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); + + matLineBasic = new THREE.LineBasicMaterial({ vertexColors: true }); + matLineDashed = new THREE.LineDashedMaterial({ vertexColors: true, scale: 2, dashSize: 1, gapSize: 1 }); + + line1 = new THREE.Line(geo, matLineBasic); + line1.computeLineDistances(); + line1.visible = false; + scene.add(line1); + + // + + window.addEventListener('resize', onWindowResize); + onWindowResize(); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + gpuPanel = new GPUStatsPanel(renderer.getContext()); + stats.addPanel(gpuPanel); + stats.showPanel(0); + + initGui(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + insetWidth = window.innerHeight / 4; // square + insetHeight = window.innerHeight / 4; + + camera2.aspect = insetWidth / insetHeight; + camera2.updateProjectionMatrix(); +} + +function animate() { + // main scene + + renderer.setClearColor(0x000000, 0); + + renderer.setViewport(0, 0, window.innerWidth, window.innerHeight); + + controls.update(); + + gpuPanel.startQuery(); + renderer.render(scene, camera); + gpuPanel.endQuery(); + + // inset scene + + renderer.setClearColor(0x222222, 1); + + renderer.clearDepth(); // important! + + renderer.setScissorTest(true); + + renderer.setScissor(20, 20, insetWidth, insetHeight); + + renderer.setViewport(20, 20, insetWidth, insetHeight); + + camera2.position.copy(camera.position); + camera2.quaternion.copy(camera.quaternion); + + renderer.render(scene, camera2); + + renderer.setScissorTest(false); + + stats.update(); +} + +// + +function initGui() { + gui = new GUI(); + + const param = { + 'line type': 0, + 'world units': false, + width: 5, + alphaToCoverage: true, + dashed: false, + 'dash scale': 1, + 'dash / gap': 1, + }; + + gui.add(param, 'line type', { LineGeometry: 0, 'gl.LINE': 1 }).onChange(function (val) { + switch (val) { + case 0: + line.visible = true; + + line1.visible = false; + + break; + + case 1: + line.visible = false; + + line1.visible = true; + + break; + } + }); + + gui.add(param, 'world units').onChange(function (val) { + matLine.worldUnits = val; + matLine.needsUpdate = true; + }); + + gui.add(param, 'width', 1, 10).onChange(function (val) { + matLine.linewidth = val; + }); + + gui.add(param, 'alphaToCoverage').onChange(function (val) { + matLine.alphaToCoverage = val; + }); + + gui.add(param, 'dashed').onChange(function (val) { + matLine.dashed = val; + line1.material = val ? matLineDashed : matLineBasic; + }); + + gui.add(param, 'dash scale', 0.5, 2, 0.1).onChange(function (val) { + matLine.dashScale = val; + matLineDashed.scale = val; + }); + + gui.add(param, 'dash / gap', { '2 : 1': 0, '1 : 1': 1, '1 : 2': 2 }).onChange(function (val) { + switch (val) { + case 0: + matLine.dashSize = 2; + matLine.gapSize = 1; + + matLineDashed.dashSize = 2; + matLineDashed.gapSize = 1; + + break; + + case 1: + matLine.dashSize = 1; + matLine.gapSize = 1; + + matLineDashed.dashSize = 1; + matLineDashed.gapSize = 1; + + break; + + case 2: + matLine.dashSize = 1; + matLine.gapSize = 2; + + matLineDashed.dashSize = 1; + matLineDashed.gapSize = 2; + + break; + } + }); +} diff --git a/examples-testing/examples/webgl_lines_fat_raycasting.ts b/examples-testing/examples/webgl_lines_fat_raycasting.ts new file mode 100644 index 000000000..75cef6204 --- /dev/null +++ b/examples-testing/examples/webgl_lines_fat_raycasting.ts @@ -0,0 +1,294 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GPUStatsPanel } from 'three/addons/utils/GPUStatsPanel.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { LineMaterial } from 'three/addons/lines/LineMaterial.js'; +import { LineSegments2 } from 'three/addons/lines/LineSegments2.js'; +import { LineSegmentsGeometry } from 'three/addons/lines/LineSegmentsGeometry.js'; +import { Line2 } from 'three/addons/lines/Line2.js'; +import { LineGeometry } from 'three/addons/lines/LineGeometry.js'; + +let line, thresholdLine, segments, thresholdSegments; +let renderer, scene, camera, controls; +let sphereInter, sphereOnLine; +let stats, gpuPanel; +let gui; +let clock; + +const color = new THREE.Color(); + +const pointer = new THREE.Vector2(Infinity, Infinity); + +const raycaster = new THREE.Raycaster(); + +raycaster.params.Line2 = {}; +raycaster.params.Line2.threshold = 0; + +const matLine = new LineMaterial({ + color: 0xffffff, + linewidth: 1, // in world units with size attenuation, pixels otherwise + worldUnits: true, + vertexColors: true, + + alphaToCoverage: true, +}); + +const matThresholdLine = new LineMaterial({ + color: 0xffffff, + linewidth: matLine.linewidth, // in world units with size attenuation, pixels otherwise + worldUnits: true, + // vertexColors: true, + transparent: true, + opacity: 0.2, + depthTest: false, + visible: false, +}); + +const params = { + 'line type': 0, + 'world units': matLine.worldUnits, + 'visualize threshold': matThresholdLine.visible, + width: matLine.linewidth, + alphaToCoverage: matLine.alphaToCoverage, + threshold: raycaster.params.Line2.threshold, + translation: raycaster.params.Line2.threshold, + animate: true, +}; + +init(); + +function init() { + clock = new THREE.Clock(); + + renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setClearColor(0x000000, 0.0); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(-40, 0, 60); + + controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 10; + controls.maxDistance = 500; + + const sphereGeometry = new THREE.SphereGeometry(0.25, 8, 4); + const sphereInterMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000, depthTest: false }); + const sphereOnLineMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00, depthTest: false }); + + sphereInter = new THREE.Mesh(sphereGeometry, sphereInterMaterial); + sphereOnLine = new THREE.Mesh(sphereGeometry, sphereOnLineMaterial); + sphereInter.visible = false; + sphereOnLine.visible = false; + sphereInter.renderOrder = 10; + sphereOnLine.renderOrder = 10; + scene.add(sphereInter); + scene.add(sphereOnLine); + + // Position and THREE.Color Data + + const positions = []; + const colors = []; + const points = []; + for (let i = -50; i < 50; i++) { + const t = i / 3; + points.push(new THREE.Vector3(t * Math.sin(2 * t), t, t * Math.cos(2 * t))); + } + + const spline = new THREE.CatmullRomCurve3(points); + const divisions = Math.round(3 * points.length); + const point = new THREE.Vector3(); + const color = new THREE.Color(); + + for (let i = 0, l = divisions; i < l; i++) { + const t = i / l; + + spline.getPoint(t, point); + positions.push(point.x, point.y, point.z); + + color.setHSL(t, 1.0, 0.5, THREE.SRGBColorSpace); + colors.push(color.r, color.g, color.b); + } + + const lineGeometry = new LineGeometry(); + lineGeometry.setPositions(positions); + lineGeometry.setColors(colors); + + const segmentsGeometry = new LineSegmentsGeometry(); + segmentsGeometry.setPositions(positions); + segmentsGeometry.setColors(colors); + + segments = new LineSegments2(segmentsGeometry, matLine); + segments.computeLineDistances(); + segments.scale.set(1, 1, 1); + scene.add(segments); + segments.visible = false; + + thresholdSegments = new LineSegments2(segmentsGeometry, matThresholdLine); + thresholdSegments.computeLineDistances(); + thresholdSegments.scale.set(1, 1, 1); + scene.add(thresholdSegments); + thresholdSegments.visible = false; + + line = new Line2(lineGeometry, matLine); + line.computeLineDistances(); + line.scale.set(1, 1, 1); + scene.add(line); + + thresholdLine = new Line2(lineGeometry, matThresholdLine); + thresholdLine.computeLineDistances(); + thresholdLine.scale.set(1, 1, 1); + scene.add(thresholdLine); + + const geo = new THREE.BufferGeometry(); + geo.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); + geo.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); + + // + + document.addEventListener('pointermove', onPointerMove); + window.addEventListener('resize', onWindowResize); + onWindowResize(); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + gpuPanel = new GPUStatsPanel(renderer.getContext()); + stats.addPanel(gpuPanel); + stats.showPanel(0); + initGui(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onPointerMove(event) { + pointer.x = (event.clientX / window.innerWidth) * 2 - 1; + pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; +} + +function animate() { + const delta = clock.getDelta(); + + const obj = line.visible ? line : segments; + thresholdLine.position.copy(line.position); + thresholdLine.quaternion.copy(line.quaternion); + thresholdSegments.position.copy(segments.position); + thresholdSegments.quaternion.copy(segments.quaternion); + + if (params.animate) { + line.rotation.y += delta * 0.1; + + segments.rotation.y = line.rotation.y; + } + + raycaster.setFromCamera(pointer, camera); + + const intersects = raycaster.intersectObject(obj); + + if (intersects.length > 0) { + sphereInter.visible = true; + sphereOnLine.visible = true; + + sphereInter.position.copy(intersects[0].point); + sphereOnLine.position.copy(intersects[0].pointOnLine); + + const index = intersects[0].faceIndex; + const colors = obj.geometry.getAttribute('instanceColorStart'); + + color.fromBufferAttribute(colors, index); + + sphereInter.material.color.copy(color).offsetHSL(0.3, 0, 0); + sphereOnLine.material.color.copy(color).offsetHSL(0.7, 0, 0); + + renderer.domElement.style.cursor = 'crosshair'; + } else { + sphereInter.visible = false; + sphereOnLine.visible = false; + renderer.domElement.style.cursor = ''; + } + + gpuPanel.startQuery(); + renderer.render(scene, camera); + gpuPanel.endQuery(); + + stats.update(); +} + +// + +function switchLine(val) { + switch (val) { + case 0: + line.visible = true; + thresholdLine.visible = true; + + segments.visible = false; + thresholdSegments.visible = false; + + break; + + case 1: + line.visible = false; + thresholdLine.visible = false; + + segments.visible = true; + thresholdSegments.visible = true; + + break; + } +} + +function initGui() { + gui = new GUI(); + + gui.add(params, 'line type', { LineGeometry: 0, LineSegmentsGeometry: 1 }) + .onChange(function (val) { + switchLine(val); + }) + .setValue(1); + + gui.add(params, 'world units').onChange(function (val) { + matLine.worldUnits = val; + matLine.needsUpdate = true; + + matThresholdLine.worldUnits = val; + matThresholdLine.needsUpdate = true; + }); + + gui.add(params, 'visualize threshold').onChange(function (val) { + matThresholdLine.visible = val; + }); + + gui.add(params, 'width', 1, 10).onChange(function (val) { + matLine.linewidth = val; + matThresholdLine.linewidth = matLine.linewidth + raycaster.params.Line2.threshold; + }); + + gui.add(params, 'alphaToCoverage').onChange(function (val) { + matLine.alphaToCoverage = val; + }); + + gui.add(params, 'threshold', 0, 10).onChange(function (val) { + raycaster.params.Line2.threshold = val; + matThresholdLine.linewidth = matLine.linewidth + raycaster.params.Line2.threshold; + }); + + gui.add(params, 'translation', 0, 10).onChange(function (val) { + line.position.x = val; + segments.position.x = val; + }); + + gui.add(params, 'animate'); +} diff --git a/examples-testing/examples/webgl_lines_fat_wireframe.ts b/examples-testing/examples/webgl_lines_fat_wireframe.ts new file mode 100644 index 000000000..59660ad7e --- /dev/null +++ b/examples-testing/examples/webgl_lines_fat_wireframe.ts @@ -0,0 +1,210 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { LineMaterial } from 'three/addons/lines/LineMaterial.js'; +import { Wireframe } from 'three/addons/lines/Wireframe.js'; +import { WireframeGeometry2 } from 'three/addons/lines/WireframeGeometry2.js'; + +let wireframe, renderer, scene, camera, camera2, controls; +let wireframe1; +let matLine, matLineBasic, matLineDashed; +let stats; +let gui; + +// viewport +let insetWidth; +let insetHeight; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setClearColor(0x000000, 0.0); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(-50, 0, 50); + + camera2 = new THREE.PerspectiveCamera(40, 1, 1, 1000); + camera2.position.copy(camera.position); + + controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 10; + controls.maxDistance = 500; + + // Wireframe ( WireframeGeometry2, LineMaterial ) + + let geo = new THREE.IcosahedronGeometry(20, 1); + + const geometry = new WireframeGeometry2(geo); + + matLine = new LineMaterial({ + color: 0x4080ff, + linewidth: 5, // in pixels + dashed: false, + }); + + wireframe = new Wireframe(geometry, matLine); + wireframe.computeLineDistances(); + wireframe.scale.set(1, 1, 1); + scene.add(wireframe); + + // Line ( THREE.WireframeGeometry, THREE.LineBasicMaterial ) - rendered with gl.LINE + + geo = new THREE.WireframeGeometry(geo); + + matLineBasic = new THREE.LineBasicMaterial({ color: 0x4080ff }); + matLineDashed = new THREE.LineDashedMaterial({ scale: 2, dashSize: 1, gapSize: 1 }); + + wireframe1 = new THREE.LineSegments(geo, matLineBasic); + wireframe1.computeLineDistances(); + wireframe1.visible = false; + scene.add(wireframe1); + + // + + window.addEventListener('resize', onWindowResize); + onWindowResize(); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + initGui(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + insetWidth = window.innerHeight / 4; // square + insetHeight = window.innerHeight / 4; + + camera2.aspect = insetWidth / insetHeight; + camera2.updateProjectionMatrix(); +} + +function animate() { + // main scene + + renderer.setClearColor(0x000000, 0); + + renderer.setViewport(0, 0, window.innerWidth, window.innerHeight); + + renderer.render(scene, camera); + + // inset scene + + renderer.setClearColor(0x222222, 1); + + renderer.clearDepth(); // important! + + renderer.setScissorTest(true); + + renderer.setScissor(20, 20, insetWidth, insetHeight); + + renderer.setViewport(20, 20, insetWidth, insetHeight); + + camera2.position.copy(camera.position); + camera2.quaternion.copy(camera.quaternion); + + renderer.render(scene, camera2); + + renderer.setScissorTest(false); + + stats.update(); +} + +// + +function initGui() { + gui = new GUI(); + + const param = { + 'line type': 0, + 'width (px)': 5, + dashed: false, + 'dash scale': 1, + 'dash / gap': 1, + }; + + gui.add(param, 'line type', { LineGeometry: 0, 'gl.LINE': 1 }).onChange(function (val) { + switch (val) { + case 0: + wireframe.visible = true; + + wireframe1.visible = false; + + break; + + case 1: + wireframe.visible = false; + + wireframe1.visible = true; + + break; + } + }); + + gui.add(param, 'width (px)', 1, 10).onChange(function (val) { + matLine.linewidth = val; + }); + + gui.add(param, 'dashed').onChange(function (val) { + matLine.dashed = val; + + // dashed is implemented as a defines -- not as a uniform. this could be changed. + // ... or THREE.LineDashedMaterial could be implemented as a separate material + // temporary hack - renderer should do this eventually + if (val) matLine.defines.USE_DASH = ''; + else delete matLine.defines.USE_DASH; + matLine.needsUpdate = true; + + wireframe1.material = val ? matLineDashed : matLineBasic; + }); + + gui.add(param, 'dash scale', 0.5, 1, 0.1).onChange(function (val) { + matLine.dashScale = val; + matLineDashed.scale = val; + }); + + gui.add(param, 'dash / gap', { '2 : 1': 0, '1 : 1': 1, '1 : 2': 2 }).onChange(function (val) { + switch (val) { + case 0: + matLine.dashSize = 2; + matLine.gapSize = 1; + + matLineDashed.dashSize = 2; + matLineDashed.gapSize = 1; + + break; + + case 1: + matLine.dashSize = 1; + matLine.gapSize = 1; + + matLineDashed.dashSize = 1; + matLineDashed.gapSize = 1; + + break; + + case 2: + matLine.dashSize = 1; + matLine.gapSize = 2; + + matLineDashed.dashSize = 1; + matLineDashed.gapSize = 2; + + break; + } + }); +} diff --git a/examples-testing/examples/webgl_loader_3dm.ts b/examples-testing/examples/webgl_loader_3dm.ts new file mode 100644 index 000000000..7570306fd --- /dev/null +++ b/examples-testing/examples/webgl_loader_3dm.ts @@ -0,0 +1,95 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { Rhino3dmLoader } from 'three/addons/loaders/3DMLoader.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer; +let controls, gui; + +init(); + +function init() { + THREE.Object3D.DEFAULT_UP.set(0, 0, 1); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(26, -40, 5); + + scene = new THREE.Scene(); + + const directionalLight = new THREE.DirectionalLight(0xffffff, 6); + directionalLight.position.set(0, 0, 2); + scene.add(directionalLight); + + const loader = new Rhino3dmLoader(); + //generally, use this for the Library Path: https://cdn.jsdelivr.net/npm/rhino3dm@8.0.1 + loader.setLibraryPath('jsm/libs/rhino3dm/'); + loader.load( + 'models/3dm/Rhino_Logo.3dm', + function (object) { + scene.add(object); + initGUI(object.userData.layers); + + // hide spinner + document.getElementById('loader').style.display = 'none'; + }, + function (progress) { + console.log((progress.loaded / progress.total) * 100 + '%'); + }, + function (error) { + console.log(error); + }, + ); + + controls = new OrbitControls(camera, renderer.domElement); + + window.addEventListener('resize', resize); +} + +function resize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); +} + +function animate() { + controls.update(); + renderer.render(scene, camera); +} + +function initGUI(layers) { + gui = new GUI({ title: 'layers' }); + + for (let i = 0; i < layers.length; i++) { + const layer = layers[i]; + gui.add(layer, 'visible') + .name(layer.name) + .onChange(function (val) { + const name = this.object.name; + + scene.traverse(function (child) { + if (child.userData.hasOwnProperty('attributes')) { + if ('layerIndex' in child.userData.attributes) { + const layerName = layers[child.userData.attributes.layerIndex].name; + + if (layerName === name) { + child.visible = val; + layer.visible = val; + } + } + } + }); + }); + } +} diff --git a/examples-testing/examples/webgl_loader_3ds.ts b/examples-testing/examples/webgl_loader_3ds.ts new file mode 100644 index 000000000..10ce34076 --- /dev/null +++ b/examples-testing/examples/webgl_loader_3ds.ts @@ -0,0 +1,62 @@ +import * as THREE from 'three'; + +import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; +import { TDSLoader } from 'three/addons/loaders/TDSLoader.js'; + +let container, controls; +let camera, scene, renderer; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 10); + camera.position.z = 2; + + scene = new THREE.Scene(); + scene.add(new THREE.AmbientLight(0xffffff, 3)); + + const directionalLight = new THREE.DirectionalLight(0xffeedd, 3); + directionalLight.position.set(0, 0, 2); + scene.add(directionalLight); + + //3ds files dont store normal maps + const normal = new THREE.TextureLoader().load('models/3ds/portalgun/textures/normal.jpg'); + + const loader = new TDSLoader(); + loader.setResourcePath('models/3ds/portalgun/textures/'); + loader.load('models/3ds/portalgun/portalgun.3ds', function (object) { + object.traverse(function (child) { + if (child.isMesh) { + child.material.specular.setScalar(0.1); + child.material.normalMap = normal; + } + }); + + scene.add(object); + }); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + controls = new TrackballControls(camera, renderer.domElement); + + window.addEventListener('resize', resize); +} + +function resize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + controls.update(); + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_3mf.ts b/examples-testing/examples/webgl_loader_3mf.ts new file mode 100644 index 000000000..c31e32196 --- /dev/null +++ b/examples-testing/examples/webgl_loader_3mf.ts @@ -0,0 +1,105 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { ThreeMFLoader } from 'three/addons/loaders/3MFLoader.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, object, loader, controls; + +const params = { + asset: 'cube_gears', +}; + +const assets = ['cube_gears', 'facecolors', 'multipletextures', 'vertexcolors']; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x333333); + + camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 500); + + // Z is up for objects intended to be 3D printed. + + camera.up.set(0, 0, 1); + camera.position.set(-100, -250, 100); + scene.add(camera); + + controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); + controls.minDistance = 50; + controls.maxDistance = 400; + controls.enablePan = false; + controls.update(); + + scene.add(new THREE.AmbientLight(0xffffff, 0.6)); + + const light = new THREE.DirectionalLight(0xffffff, 2); + light.position.set(-1, -2.5, 1); + scene.add(light); + + const manager = new THREE.LoadingManager(); + + manager.onLoad = function () { + const aabb = new THREE.Box3().setFromObject(object); + const center = aabb.getCenter(new THREE.Vector3()); + + object.position.x += object.position.x - center.x; + object.position.y += object.position.y - center.y; + object.position.z += object.position.z - center.z; + + controls.reset(); + + scene.add(object); + render(); + }; + + loader = new ThreeMFLoader(manager); + loadAsset(params.asset); + + window.addEventListener('resize', onWindowResize); + + // + + const gui = new GUI(); + gui.add(params, 'asset', assets).onChange(function (value) { + loadAsset(value); + }); +} + +function loadAsset(asset) { + loader.load('models/3mf/' + asset + '.3mf', function (group) { + if (object) { + object.traverse(function (child) { + if (child.material) child.material.dispose(); + if (child.material && child.material.map) child.material.map.dispose(); + if (child.geometry) child.geometry.dispose(); + }); + + scene.remove(object); + } + + // + + object = group; + }); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_3mf_materials.ts b/examples-testing/examples/webgl_loader_3mf_materials.ts new file mode 100644 index 000000000..fcdd7308e --- /dev/null +++ b/examples-testing/examples/webgl_loader_3mf_materials.ts @@ -0,0 +1,106 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { ThreeMFLoader } from 'three/addons/loaders/3MFLoader.js'; + +let camera, scene, renderer; + +init(); + +function init() { + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xa0a0a0); + scene.fog = new THREE.Fog(0xa0a0a0, 10, 500); + + camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 500); + camera.position.set(-50, 40, 50); + scene.add(camera); + + // + + const hemiLight = new THREE.HemisphereLight(0xffffff, 0x8d8d8d, 3); + hemiLight.position.set(0, 100, 0); + scene.add(hemiLight); + + const dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.position.set(-0, 40, 50); + dirLight.castShadow = true; + dirLight.shadow.camera.top = 50; + dirLight.shadow.camera.bottom = -25; + dirLight.shadow.camera.left = -25; + dirLight.shadow.camera.right = 25; + dirLight.shadow.camera.near = 0.1; + dirLight.shadow.camera.far = 200; + dirLight.shadow.mapSize.set(1024, 1024); + scene.add(dirLight); + + // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) ); + + // + + const manager = new THREE.LoadingManager(); + + const loader = new ThreeMFLoader(manager); + loader.load('./models/3mf/truck.3mf', function (object) { + object.rotation.set(-Math.PI / 2, 0, 0); // z-up conversion + + object.traverse(function (child) { + child.castShadow = true; + }); + + scene.add(object); + }); + + // + + manager.onLoad = function () { + render(); + }; + + // + + const ground = new THREE.Mesh( + new THREE.PlaneGeometry(1000, 1000), + new THREE.MeshPhongMaterial({ color: 0xcbcbcb, depthWrite: false }), + ); + ground.rotation.x = -Math.PI / 2; + ground.position.y = 11; + ground.receiveShadow = true; + scene.add(ground); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.shadowMap.enabled = true; + renderer.shadowMap.type = THREE.PCFSoftShadowMap; + document.body.appendChild(renderer.domElement); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); + controls.minDistance = 50; + controls.maxDistance = 200; + controls.enablePan = false; + controls.target.set(0, 20, 0); + controls.update(); + + window.addEventListener('resize', onWindowResize); + + render(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_amf.ts b/examples-testing/examples/webgl_loader_amf.ts new file mode 100644 index 000000000..ee576e04f --- /dev/null +++ b/examples-testing/examples/webgl_loader_amf.ts @@ -0,0 +1,62 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { AMFLoader } from 'three/addons/loaders/AMFLoader.js'; + +let camera, scene, renderer; + +init(); + +function init() { + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x999999); + + scene.add(new THREE.AmbientLight(0x999999)); + + camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 500); + + // Z is up for objects intended to be 3D printed. + + camera.up.set(0, 0, 1); + camera.position.set(0, -9, 6); + + camera.add(new THREE.PointLight(0xffffff, 250)); + + scene.add(camera); + + const grid = new THREE.GridHelper(50, 50, 0xffffff, 0x555555); + grid.rotateOnAxis(new THREE.Vector3(1, 0, 0), 90 * (Math.PI / 180)); + scene.add(grid); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + const loader = new AMFLoader(); + loader.load('./models/amf/rook.amf', function (amfobject) { + scene.add(amfobject); + render(); + }); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); + controls.target.set(0, 0, 2); + controls.enableZoom = false; + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_bvh.ts b/examples-testing/examples/webgl_loader_bvh.ts new file mode 100644 index 000000000..0be3add4d --- /dev/null +++ b/examples-testing/examples/webgl_loader_bvh.ts @@ -0,0 +1,61 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { BVHLoader } from 'three/addons/loaders/BVHLoader.js'; + +const clock = new THREE.Clock(); + +let camera, controls, scene, renderer; +let mixer; + +init(); + +const loader = new BVHLoader(); +loader.load('models/bvh/pirouette.bvh', function (result) { + const skeletonHelper = new THREE.SkeletonHelper(result.skeleton.bones[0]); + + scene.add(result.skeleton.bones[0]); + scene.add(skeletonHelper); + + // play animation + mixer = new THREE.AnimationMixer(result.skeleton.bones[0]); + mixer.clipAction(result.clip).play(); +}); + +function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 200, 300); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xeeeeee); + + scene.add(new THREE.GridHelper(400, 10)); + + // renderer + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 300; + controls.maxDistance = 700; + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const delta = clock.getDelta(); + + if (mixer) mixer.update(delta); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_collada.ts b/examples-testing/examples/webgl_loader_collada.ts new file mode 100644 index 000000000..62588b698 --- /dev/null +++ b/examples-testing/examples/webgl_loader_collada.ts @@ -0,0 +1,83 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { ColladaLoader } from 'three/addons/loaders/ColladaLoader.js'; + +let container, stats, clock; +let camera, scene, renderer, elf; + +init(); + +function init() { + container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 2000); + camera.position.set(8, 10, 8); + camera.lookAt(0, 3, 0); + + scene = new THREE.Scene(); + + clock = new THREE.Clock(); + + // loading manager + + const loadingManager = new THREE.LoadingManager(function () { + scene.add(elf); + }); + + // collada + + const loader = new ColladaLoader(loadingManager); + loader.load('./models/collada/elf/elf.dae', function (collada) { + elf = collada.scene; + }); + + // + + const ambientLight = new THREE.AmbientLight(0xffffff); + scene.add(ambientLight); + + const directionalLight = new THREE.DirectionalLight(0xffffff, 2.5); + directionalLight.position.set(1, 1, 0).normalize(); + scene.add(directionalLight); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + const delta = clock.getDelta(); + + if (elf !== undefined) { + elf.rotation.z += delta * 0.5; + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_collada_skinning.ts b/examples-testing/examples/webgl_loader_collada_skinning.ts new file mode 100644 index 000000000..5cb808b17 --- /dev/null +++ b/examples-testing/examples/webgl_loader_collada_skinning.ts @@ -0,0 +1,97 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { ColladaLoader } from 'three/addons/loaders/ColladaLoader.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let container, stats, clock, controls; +let camera, scene, renderer, mixer; + +init(); + +function init() { + container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(25, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(15, 10, -15); + + scene = new THREE.Scene(); + + clock = new THREE.Clock(); + + // collada + + const loader = new ColladaLoader(); + loader.load('./models/collada/stormtrooper/stormtrooper.dae', function (collada) { + const avatar = collada.scene; + const animations = avatar.animations; + + mixer = new THREE.AnimationMixer(avatar); + mixer.clipAction(animations[0]).play(); + + scene.add(avatar); + }); + + // + + const gridHelper = new THREE.GridHelper(10, 20, 0xc1c1c1, 0x8d8d8d); + scene.add(gridHelper); + + // + + const ambientLight = new THREE.AmbientLight(0xffffff, 0.6); + scene.add(ambientLight); + + const directionalLight = new THREE.DirectionalLight(0xffffff, 3); + directionalLight.position.set(1.5, 1, -1.5); + scene.add(directionalLight); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + controls = new OrbitControls(camera, renderer.domElement); + controls.screenSpacePanning = true; + controls.minDistance = 5; + controls.maxDistance = 40; + controls.target.set(0, 2, 0); + controls.update(); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + const delta = clock.getDelta(); + + if (mixer !== undefined) { + mixer.update(delta); + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_draco.ts b/examples-testing/examples/webgl_loader_draco.ts new file mode 100644 index 000000000..c9947c693 --- /dev/null +++ b/examples-testing/examples/webgl_loader_draco.ts @@ -0,0 +1,85 @@ +import * as THREE from 'three'; + +import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; + +let camera, scene, renderer; + +const container = document.querySelector('#container'); + +// Configure and create Draco decoder. +const dracoLoader = new DRACOLoader(); +dracoLoader.setDecoderPath('jsm/libs/draco/'); +dracoLoader.setDecoderConfig({ type: 'js' }); + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 15); + camera.position.set(3, 0.25, 3); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x443333); + scene.fog = new THREE.Fog(0x443333, 1, 4); + + // Ground + const plane = new THREE.Mesh( + new THREE.PlaneGeometry(8, 8), + new THREE.MeshPhongMaterial({ color: 0xcbcbcb, specular: 0x101010 }), + ); + plane.rotation.x = -Math.PI / 2; + plane.position.y = 0.03; + plane.receiveShadow = true; + scene.add(plane); + + // Lights + const hemiLight = new THREE.HemisphereLight(0x8d7c7c, 0x494966, 3); + scene.add(hemiLight); + + const spotLight = new THREE.SpotLight(); + spotLight.intensity = 7; + spotLight.angle = Math.PI / 16; + spotLight.penumbra = 0.5; + spotLight.castShadow = true; + spotLight.position.set(-1, 1, 1); + scene.add(spotLight); + + dracoLoader.load('models/draco/bunny.drc', function (geometry) { + geometry.computeVertexNormals(); + + const material = new THREE.MeshStandardMaterial({ color: 0xa5a5a5 }); + const mesh = new THREE.Mesh(geometry, material); + mesh.castShadow = true; + mesh.receiveShadow = true; + scene.add(mesh); + + // Release decoder resources. + dracoLoader.dispose(); + }); + + // renderer + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + container.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const timer = Date.now() * 0.0003; + + camera.position.x = Math.sin(timer) * 0.5; + camera.position.z = Math.cos(timer) * 0.5; + camera.lookAt(0, 0.1, 0); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_fbx.ts b/examples-testing/examples/webgl_loader_fbx.ts new file mode 100644 index 000000000..3b157a222 --- /dev/null +++ b/examples-testing/examples/webgl_loader_fbx.ts @@ -0,0 +1,162 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { FBXLoader } from 'three/addons/loaders/FBXLoader.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +const manager = new THREE.LoadingManager(); + +let camera, scene, renderer, stats, object, loader, guiMorphsFolder; +let mixer; + +const clock = new THREE.Clock(); + +const params = { + asset: 'Samba Dancing', +}; + +const assets = ['Samba Dancing', 'morph_test']; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000); + camera.position.set(100, 200, 300); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xa0a0a0); + scene.fog = new THREE.Fog(0xa0a0a0, 200, 1000); + + const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 5); + hemiLight.position.set(0, 200, 0); + scene.add(hemiLight); + + const dirLight = new THREE.DirectionalLight(0xffffff, 5); + dirLight.position.set(0, 200, 100); + dirLight.castShadow = true; + dirLight.shadow.camera.top = 180; + dirLight.shadow.camera.bottom = -100; + dirLight.shadow.camera.left = -120; + dirLight.shadow.camera.right = 120; + scene.add(dirLight); + + // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) ); + + // ground + const mesh = new THREE.Mesh( + new THREE.PlaneGeometry(2000, 2000), + new THREE.MeshPhongMaterial({ color: 0x999999, depthWrite: false }), + ); + mesh.rotation.x = -Math.PI / 2; + mesh.receiveShadow = true; + scene.add(mesh); + + const grid = new THREE.GridHelper(2000, 20, 0x000000, 0x000000); + grid.material.opacity = 0.2; + grid.material.transparent = true; + scene.add(grid); + + loader = new FBXLoader(manager); + loadAsset(params.asset); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + container.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 100, 0); + controls.update(); + + window.addEventListener('resize', onWindowResize); + + // stats + stats = new Stats(); + container.appendChild(stats.dom); + + const gui = new GUI(); + gui.add(params, 'asset', assets).onChange(function (value) { + loadAsset(value); + }); + + guiMorphsFolder = gui.addFolder('Morphs').hide(); +} + +function loadAsset(asset) { + loader.load('models/fbx/' + asset + '.fbx', function (group) { + if (object) { + object.traverse(function (child) { + if (child.material) { + const materials = Array.isArray(child.material) ? child.material : [child.material]; + materials.forEach(material => { + if (material.map) material.map.dispose(); + material.dispose(); + }); + } + + if (child.geometry) child.geometry.dispose(); + }); + + scene.remove(object); + } + + // + + object = group; + + if (object.animations && object.animations.length) { + mixer = new THREE.AnimationMixer(object); + + const action = mixer.clipAction(object.animations[0]); + action.play(); + } else { + mixer = null; + } + + guiMorphsFolder.children.forEach(child => child.destroy()); + guiMorphsFolder.hide(); + + object.traverse(function (child) { + if (child.isMesh) { + child.castShadow = true; + child.receiveShadow = true; + + if (child.morphTargetDictionary) { + guiMorphsFolder.show(); + const meshFolder = guiMorphsFolder.addFolder(child.name || child.uuid); + Object.keys(child.morphTargetDictionary).forEach(key => { + meshFolder.add(child.morphTargetInfluences, child.morphTargetDictionary[key], 0, 1, 0.01); + }); + } + } + }); + + scene.add(object); + }); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const delta = clock.getDelta(); + + if (mixer) mixer.update(delta); + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_loader_fbx_nurbs.ts b/examples-testing/examples/webgl_loader_fbx_nurbs.ts new file mode 100644 index 000000000..f2e45bcb5 --- /dev/null +++ b/examples-testing/examples/webgl_loader_fbx_nurbs.ts @@ -0,0 +1,61 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { FBXLoader } from 'three/addons/loaders/FBXLoader.js'; + +let camera, scene, renderer, stats; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000); + camera.position.set(2, 18, 28); + + scene = new THREE.Scene(); + + // grid + const gridHelper = new THREE.GridHelper(28, 28, 0x303030, 0x303030); + scene.add(gridHelper); + + // stats + stats = new Stats(); + container.appendChild(stats.dom); + + // model + const loader = new FBXLoader(); + loader.load('models/fbx/nurbs.fbx', function (object) { + scene.add(object); + }); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 12, 0); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_loader_gcode.ts b/examples-testing/examples/webgl_loader_gcode.ts new file mode 100644 index 000000000..6fd3e149d --- /dev/null +++ b/examples-testing/examples/webgl_loader_gcode.ts @@ -0,0 +1,49 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GCodeLoader } from 'three/addons/loaders/GCodeLoader.js'; + +let camera, scene, renderer; + +init(); +render(); + +function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 0, 70); + + scene = new THREE.Scene(); + + const loader = new GCodeLoader(); + loader.load('models/gcode/benchy.gcode', function (object) { + object.position.set(-100, -20, 100); + scene.add(object); + + render(); + }); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.minDistance = 10; + controls.maxDistance = 100; + + window.addEventListener('resize', resize); +} + +function resize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_gltf.ts b/examples-testing/examples/webgl_loader_gltf.ts new file mode 100644 index 000000000..e1b0adc51 --- /dev/null +++ b/examples-testing/examples/webgl_loader_gltf.ts @@ -0,0 +1,74 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +let camera, scene, renderer; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); + camera.position.set(-1.8, 0.6, 2.7); + + scene = new THREE.Scene(); + + new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + scene.environment = texture; + + render(); + + // model + + const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); + loader.load('DamagedHelmet.gltf', async function (gltf) { + const model = gltf.scene; + + // wait until the model can be added to the scene without blocking due to shader compilation + + await renderer.compileAsync(model, camera, scene); + + scene.add(model); + + render(); + }); + }); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1; + container.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.minDistance = 2; + controls.maxDistance = 10; + controls.target.set(0, 0, -0.2); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +// + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_gltf_anisotropy.ts b/examples-testing/examples/webgl_loader_gltf_anisotropy.ts new file mode 100644 index 000000000..6e240a272 --- /dev/null +++ b/examples-testing/examples/webgl_loader_gltf_anisotropy.ts @@ -0,0 +1,68 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +let renderer, scene, camera, controls; + +init(); + +async function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1.35; + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.01, 10); + camera.position.set(-0.35, -0.2, 0.35); + + controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, -0.08, 0.11); + controls.minDistance = 0.1; + controls.maxDistance = 2; + controls.addEventListener('change', render); + controls.update(); + + const rgbeLoader = new RGBELoader().setPath('textures/equirectangular/'); + const gltfLoader = new GLTFLoader().setPath('models/gltf/'); + + const [texture, gltf] = await Promise.all([ + rgbeLoader.loadAsync('royal_esplanade_1k.hdr'), + gltfLoader.loadAsync('AnisotropyBarnLamp.glb'), + ]); + + // environment + + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + scene.backgroundBlurriness = 0.5; + scene.environment = texture; + + // model + + scene.add(gltf.scene); + + render(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_gltf_avif.ts b/examples-testing/examples/webgl_loader_gltf_avif.ts new file mode 100644 index 000000000..37d63859e --- /dev/null +++ b/examples-testing/examples/webgl_loader_gltf_avif.ts @@ -0,0 +1,61 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; + +let camera, scene, renderer; + +init(); +render(); + +function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(1.5, 4, 9); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf6eedc); + + // + + const dracoLoader = new DRACOLoader(); + dracoLoader.setDecoderPath('jsm/libs/draco/gltf/'); + + const loader = new GLTFLoader(); + loader.setDRACOLoader(dracoLoader); + loader.setPath('models/gltf/AVIFTest/'); + loader.load('forest_house.glb', function (gltf) { + scene.add(gltf.scene); + + render(); + }); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); + controls.target.set(0, 2, 0); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +// + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_gltf_compressed.ts b/examples-testing/examples/webgl_loader_gltf_compressed.ts new file mode 100644 index 000000000..3bdcea8ec --- /dev/null +++ b/examples-testing/examples/webgl_loader_gltf_compressed.ts @@ -0,0 +1,83 @@ +import * as THREE from 'three'; + +import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js'; +import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js'; + +let camera, scene, renderer; + +init(); +render(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1; + container.appendChild(renderer.domElement); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000); + camera.position.set(0, 100, 0); + + const environment = new RoomEnvironment(); + const pmremGenerator = new THREE.PMREMGenerator(renderer); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xbbbbbb); + scene.environment = pmremGenerator.fromScene(environment).texture; + environment.dispose(); + + const grid = new THREE.GridHelper(500, 10, 0xffffff, 0xffffff); + grid.material.opacity = 0.5; + grid.material.depthWrite = false; + grid.material.transparent = true; + scene.add(grid); + + const ktx2Loader = new KTX2Loader().setTranscoderPath('jsm/libs/basis/').detectSupport(renderer); + + const loader = new GLTFLoader().setPath('models/gltf/'); + loader.setKTX2Loader(ktx2Loader); + loader.setMeshoptDecoder(MeshoptDecoder); + loader.load('coffeemat.glb', function (gltf) { + // coffeemat.glb was produced from the source scene using gltfpack: + // gltfpack -i coffeemat/scene.gltf -o coffeemat.glb -cc -tc + // The resulting model uses EXT_meshopt_compression (for geometry) and KHR_texture_basisu (for texture compression using ETC1S/BasisLZ) + + gltf.scene.position.y = 8; + + scene.add(gltf.scene); + + render(); + }); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.minDistance = 400; + controls.maxDistance = 1000; + controls.target.set(10, 90, -16); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +// + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_gltf_dispersion.ts b/examples-testing/examples/webgl_loader_gltf_dispersion.ts new file mode 100644 index 000000000..100badcab --- /dev/null +++ b/examples-testing/examples/webgl_loader_gltf_dispersion.ts @@ -0,0 +1,66 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; + +let camera, scene, renderer; + +init().then(render); + +async function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 5); + camera.position.set(0.1, 0.05, 0.15); + + scene = new THREE.Scene(); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.toneMapping = THREE.NeutralToneMapping; + renderer.toneMappingExposure = 1; + container.appendChild(renderer.domElement); + + const environment = new RoomEnvironment(); + const pmremGenerator = new THREE.PMREMGenerator(renderer); + + scene = new THREE.Scene(); + scene.backgroundBlurriness = 0.5; + + const env = pmremGenerator.fromScene(environment).texture; + scene.background = env; + scene.environment = env; + environment.dispose(); + + const loader = new GLTFLoader(); + const gltf = await loader.loadAsync('models/gltf/DispersionTest.glb'); + + scene.add(gltf.scene); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.minDistance = 0.1; + controls.maxDistance = 10; + controls.target.set(0, 0, 0); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +// + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_gltf_instancing.ts b/examples-testing/examples/webgl_loader_gltf_instancing.ts new file mode 100644 index 000000000..5d23e7750 --- /dev/null +++ b/examples-testing/examples/webgl_loader_gltf_instancing.ts @@ -0,0 +1,69 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +let camera, scene, renderer; + +init(); +render(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); + camera.position.set(-0.9, 0.41, -0.89); + + scene = new THREE.Scene(); + + new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + scene.environment = texture; + + render(); + + // model + + const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF-instancing/'); + loader.load('DamagedHelmetGpuInstancing.gltf', function (gltf) { + scene.add(gltf.scene); + + render(); + }); + }); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1; + container.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.minDistance = 0.2; + controls.maxDistance = 10; + controls.target.set(0, 0.25, 0); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +// + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_gltf_iridescence.ts b/examples-testing/examples/webgl_loader_gltf_iridescence.ts new file mode 100644 index 000000000..eb0f8d914 --- /dev/null +++ b/examples-testing/examples/webgl_loader_gltf_iridescence.ts @@ -0,0 +1,66 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +let renderer, scene, camera, controls; + +init().catch(function (err) { + console.error(err); +}); + +async function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setAnimationLoop(animate); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.05, 20); + camera.position.set(0.35, 0.05, 0.35); + + controls = new OrbitControls(camera, renderer.domElement); + controls.autoRotate = true; + controls.autoRotateSpeed = -0.5; + controls.target.set(0, 0.2, 0); + controls.update(); + + const rgbeLoader = new RGBELoader().setPath('textures/equirectangular/'); + + const gltfLoader = new GLTFLoader().setPath('models/gltf/'); + + const [texture, gltf] = await Promise.all([ + rgbeLoader.loadAsync('venice_sunset_1k.hdr'), + gltfLoader.loadAsync('IridescenceLamp.glb'), + ]); + + // environment + + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + scene.environment = texture; + + // model + + scene.add(gltf.scene); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + controls.update(); + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_gltf_lights.ts b/examples-testing/examples/webgl_loader_gltf_lights.ts new file mode 100644 index 000000000..f2bd5b104 --- /dev/null +++ b/examples-testing/examples/webgl_loader_gltf_lights.ts @@ -0,0 +1,83 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer; + +const params = { + punctualLightsEnabled: true, +}; + +init().then(render); + +async function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); + camera.position.set(-2, 1.5, 3); + + scene = new THREE.Scene(); + + const rgbeLoader = new RGBELoader(); + const envMap = await rgbeLoader.loadAsync('textures/equirectangular/moonless_golf_1k.hdr '); + envMap.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = envMap; + scene.environment = envMap; + + const loader = new GLTFLoader(); + const gltf = await loader.loadAsync('models/gltf/LightsPunctualLamp.glb'); + + scene.add(gltf.scene); + + const gui = new GUI(); + + gui.add(params, 'punctualLightsEnabled').onChange(toggleLights); + gui.open(); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1; + container.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.minDistance = 2; + controls.maxDistance = 10; + controls.target.set(0, 1, 0); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function toggleLights(visible) { + scene.traverse(function (object) { + if (object.isLight) { + object.visible = visible; + } + }); + + render(); +} + +// + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_gltf_sheen.ts b/examples-testing/examples/webgl_loader_gltf_sheen.ts new file mode 100644 index 000000000..bd258d5c1 --- /dev/null +++ b/examples-testing/examples/webgl_loader_gltf_sheen.ts @@ -0,0 +1,72 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, controls; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 20); + camera.position.set(-0.75, 0.7, 1.25); + + scene = new THREE.Scene(); + + // model + + new GLTFLoader().setPath('models/gltf/').load('SheenChair.glb', function (gltf) { + scene.add(gltf.scene); + + const object = gltf.scene.getObjectByName('SheenChair_fabric'); + + const gui = new GUI(); + + gui.add(object.material, 'sheen', 0, 1); + gui.open(); + }); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1; + container.appendChild(renderer.domElement); + + const environment = new RoomEnvironment(); + const pmremGenerator = new THREE.PMREMGenerator(renderer); + + scene.background = new THREE.Color(0xbbbbbb); + scene.environment = pmremGenerator.fromScene(environment).texture; + + controls = new OrbitControls(camera, renderer.domElement); + controls.enableDamping = true; + controls.minDistance = 1; + controls.maxDistance = 10; + controls.target.set(0, 0.35, 0); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + controls.update(); // required if damping enabled + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_gltf_transmission.ts b/examples-testing/examples/webgl_loader_gltf_transmission.ts new file mode 100644 index 000000000..87a47d2c0 --- /dev/null +++ b/examples-testing/examples/webgl_loader_gltf_transmission.ts @@ -0,0 +1,80 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; + +let camera, scene, renderer, controls, clock, mixer; + +init(); + +function init() { + clock = new THREE.Clock(); + + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); + camera.position.set(0, 0.4, 0.7); + + scene = new THREE.Scene(); + + new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + scene.backgroundBlurriness = 0.35; + + scene.environment = texture; + + // model + + new GLTFLoader() + .setPath('models/gltf/') + .setDRACOLoader(new DRACOLoader().setDecoderPath('jsm/libs/draco/gltf/')) + .load('IridescentDishWithOlives.glb', function (gltf) { + mixer = new THREE.AnimationMixer(gltf.scene); + mixer.clipAction(gltf.animations[0]).play(); + + scene.add(gltf.scene); + }); + }); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1; + container.appendChild(renderer.domElement); + + controls = new OrbitControls(camera, renderer.domElement); + controls.autoRotate = true; + controls.autoRotateSpeed = -0.75; + controls.enableDamping = true; + controls.minDistance = 0.5; + controls.maxDistance = 1; + controls.target.set(0, 0.1, 0); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + if (mixer) mixer.update(clock.getDelta()); + + controls.update(); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_imagebitmap.ts b/examples-testing/examples/webgl_loader_imagebitmap.ts new file mode 100644 index 000000000..1049e9857 --- /dev/null +++ b/examples-testing/examples/webgl_loader_imagebitmap.ts @@ -0,0 +1,109 @@ +import * as THREE from 'three'; + +let camera, scene, renderer; +let group, cubes; + +init(); + +function addImageBitmap() { + new THREE.ImageBitmapLoader().load( + 'textures/planets/earth_atmos_2048.jpg?' + performance.now(), + function (imageBitmap) { + const texture = new THREE.CanvasTexture(imageBitmap); + texture.colorSpace = THREE.SRGBColorSpace; + const material = new THREE.MeshBasicMaterial({ map: texture }); + + /* ImageBitmap should be disposed when done with it + Can't be done until it's actually uploaded to WebGLTexture */ + + // imageBitmap.close(); + + addCube(material); + }, + function (p) { + console.log(p); + }, + function (e) { + console.log(e); + }, + ); +} + +function addImage() { + new THREE.ImageLoader() + .setCrossOrigin('*') + .load('textures/planets/earth_atmos_2048.jpg?' + performance.now(), function (image) { + const texture = new THREE.CanvasTexture(image); + texture.colorSpace = THREE.SRGBColorSpace; + const material = new THREE.MeshBasicMaterial({ color: 0xff8888, map: texture }); + addCube(material); + }); +} + +const geometry = new THREE.BoxGeometry(); + +function addCube(material) { + const cube = new THREE.Mesh(geometry, material); + cube.position.set(Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1); + cube.rotation.set(Math.random() * 2 * Math.PI, Math.random() * 2 * Math.PI, Math.random() * 2 * Math.PI); + cubes.add(cube); +} + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + // CAMERA + + camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 1500); + camera.position.set(0, 4, 7); + camera.lookAt(0, 0, 0); + + // SCENE + + scene = new THREE.Scene(); + + // + + group = new THREE.Group(); + scene.add(group); + + group.add(new THREE.GridHelper(4, 12, 0x888888, 0x444444)); + + cubes = new THREE.Group(); + group.add(cubes); + + // RENDERER + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // TESTS + + setTimeout(addImage, 300); + setTimeout(addImage, 600); + setTimeout(addImage, 900); + setTimeout(addImageBitmap, 1300); + setTimeout(addImageBitmap, 1600); + setTimeout(addImageBitmap, 1900); + + // EVENTS + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + group.rotation.y = performance.now() / 3000; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_kmz.ts b/examples-testing/examples/webgl_loader_kmz.ts new file mode 100644 index 000000000..f93555e41 --- /dev/null +++ b/examples-testing/examples/webgl_loader_kmz.ts @@ -0,0 +1,59 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { KMZLoader } from 'three/addons/loaders/KMZLoader.js'; + +let camera, scene, renderer; + +init(); + +function init() { + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x999999); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0.5, 1.0, 0.5).normalize(); + + scene.add(light); + + camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 500); + + camera.position.y = 5; + camera.position.z = 10; + + scene.add(camera); + + const grid = new THREE.GridHelper(50, 50, 0xffffff, 0x7b7b7b); + scene.add(grid); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + const loader = new KMZLoader(); + loader.load('./models/kmz/Box.kmz', function (kmz) { + kmz.scene.position.y = 0.5; + scene.add(kmz.scene); + render(); + }); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_lwo.ts b/examples-testing/examples/webgl_loader_lwo.ts new file mode 100644 index 000000000..fb10c8340 --- /dev/null +++ b/examples-testing/examples/webgl_loader_lwo.ts @@ -0,0 +1,69 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { LWOLoader } from 'three/addons/loaders/LWOLoader.js'; + +let camera, scene, renderer; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 200); + camera.position.set(0.7, 14.6, -43.2); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xa0a0a0); + + const ambientLight = new THREE.AmbientLight(0xbbbbbb); + scene.add(ambientLight); + + const light1 = new THREE.DirectionalLight(0xc1c1c1, 3); + light1.position.set(0, 200, -100); + scene.add(light1); + + const grid = new THREE.GridHelper(200, 20, 0x000000, 0x000000); + grid.material.opacity = 0.3; + grid.material.transparent = true; + scene.add(grid); + + const loader = new LWOLoader(); + loader.load('models/lwo/Objects/LWO3/Demo.lwo', function (object) { + const phong = object.meshes[0]; + phong.position.set(2, 12, 0); + + const standard = object.meshes[1]; + standard.position.set(-2, 12, 0); + + const rocket = object.meshes[2]; + rocket.position.set(0, 10.5, 1); + + scene.add(phong, standard, rocket); + }); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + container.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(-1.33, 10, 6.7); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_md2_control.ts b/examples-testing/examples/webgl_loader_md2_control.ts new file mode 100644 index 000000000..683e4c2ad --- /dev/null +++ b/examples-testing/examples/webgl_loader_md2_control.ts @@ -0,0 +1,289 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { MD2CharacterComplex } from 'three/addons/misc/MD2CharacterComplex.js'; +import { Gyroscope } from 'three/addons/misc/Gyroscope.js'; + +let SCREEN_WIDTH = window.innerWidth; +let SCREEN_HEIGHT = window.innerHeight; + +let container, stats; +let camera, scene, renderer; + +const characters = []; +let nCharacters = 0; + +let cameraControls; + +const controls = { + moveForward: false, + moveBackward: false, + moveLeft: false, + moveRight: false, +}; + +const clock = new THREE.Clock(); + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + // CAMERA + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 4000); + camera.position.set(0, 150, 1300); + + // SCENE + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + scene.fog = new THREE.Fog(0xffffff, 1000, 4000); + + scene.add(camera); + + // LIGHTS + + scene.add(new THREE.AmbientLight(0x666666, 3)); + + const light = new THREE.DirectionalLight(0xffffff, 7); + light.position.set(200, 450, 500); + + light.castShadow = true; + + light.shadow.mapSize.width = 1024; + light.shadow.mapSize.height = 512; + + light.shadow.camera.near = 100; + light.shadow.camera.far = 1200; + + light.shadow.camera.left = -1000; + light.shadow.camera.right = 1000; + light.shadow.camera.top = 350; + light.shadow.camera.bottom = -350; + + scene.add(light); + // scene.add( new THREE.CameraHelper( light.shadow.camera ) ); + + // GROUND + + const gt = new THREE.TextureLoader().load('textures/terrain/grasslight-big.jpg'); + const gg = new THREE.PlaneGeometry(16000, 16000); + const gm = new THREE.MeshPhongMaterial({ color: 0xffffff, map: gt }); + + const ground = new THREE.Mesh(gg, gm); + ground.rotation.x = -Math.PI / 2; + ground.material.map.repeat.set(64, 64); + ground.material.map.wrapS = THREE.RepeatWrapping; + ground.material.map.wrapT = THREE.RepeatWrapping; + ground.material.map.colorSpace = THREE.SRGBColorSpace; + // note that because the ground does not cast a shadow, .castShadow is left false + ground.receiveShadow = true; + + scene.add(ground); + + // RENDERER + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + renderer.shadowMap.enabled = true; + renderer.shadowMap.type = THREE.PCFSoftShadowMap; + + // STATS + + stats = new Stats(); + container.appendChild(stats.dom); + + // EVENTS + + window.addEventListener('resize', onWindowResize); + document.addEventListener('keydown', onKeyDown); + document.addEventListener('keyup', onKeyUp); + + // CONTROLS + + cameraControls = new OrbitControls(camera, renderer.domElement); + cameraControls.target.set(0, 50, 0); + cameraControls.update(); + + // CHARACTER + + const configOgro = { + baseUrl: 'models/md2/ogro/', + + body: 'ogro.md2', + skins: [ + 'grok.jpg', + 'ogrobase.png', + 'arboshak.png', + 'ctf_r.png', + 'ctf_b.png', + 'darkam.png', + 'freedom.png', + 'gib.png', + 'gordogh.png', + 'igdosh.png', + 'khorne.png', + 'nabogro.png', + 'sharokh.png', + ], + weapons: [['weapon.md2', 'weapon.jpg']], + animations: { + move: 'run', + idle: 'stand', + jump: 'jump', + attack: 'attack', + crouchMove: 'cwalk', + crouchIdle: 'cstand', + crouchAttach: 'crattack', + }, + + walkSpeed: 350, + crouchSpeed: 175, + }; + + const nRows = 1; + const nSkins = configOgro.skins.length; + + nCharacters = nSkins * nRows; + + for (let i = 0; i < nCharacters; i++) { + const character = new MD2CharacterComplex(); + character.scale = 3; + character.controls = controls; + characters.push(character); + } + + const baseCharacter = new MD2CharacterComplex(); + baseCharacter.scale = 3; + + baseCharacter.onLoadComplete = function () { + let k = 0; + + for (let j = 0; j < nRows; j++) { + for (let i = 0; i < nSkins; i++) { + const cloneCharacter = characters[k]; + + cloneCharacter.shareParts(baseCharacter); + + // cast and receive shadows + cloneCharacter.enableShadows(true); + + cloneCharacter.setWeapon(0); + cloneCharacter.setSkin(i); + + cloneCharacter.root.position.x = (i - nSkins / 2) * 150; + cloneCharacter.root.position.z = j * 250; + + scene.add(cloneCharacter.root); + + k++; + } + } + + const gyro = new Gyroscope(); + gyro.add(camera); + gyro.add(light, light.target); + + characters[Math.floor(nSkins / 2)].root.add(gyro); + }; + + baseCharacter.loadParts(configOgro); +} + +// EVENT HANDLERS + +function onWindowResize() { + SCREEN_WIDTH = window.innerWidth; + SCREEN_HEIGHT = window.innerHeight; + + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + + camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT; + camera.updateProjectionMatrix(); +} + +function onKeyDown(event) { + switch (event.code) { + case 'ArrowUp': + case 'KeyW': + controls.moveForward = true; + break; + + case 'ArrowDown': + case 'KeyS': + controls.moveBackward = true; + break; + + case 'ArrowLeft': + case 'KeyA': + controls.moveLeft = true; + break; + + case 'ArrowRight': + case 'KeyD': + controls.moveRight = true; + break; + + // case 'KeyC': controls.crouch = true; break; + // case 'Space': controls.jump = true; break; + // case 'ControlLeft': + // case 'ControlRight': controls.attack = true; break; + } +} + +function onKeyUp(event) { + switch (event.code) { + case 'ArrowUp': + case 'KeyW': + controls.moveForward = false; + break; + + case 'ArrowDown': + case 'KeyS': + controls.moveBackward = false; + break; + + case 'ArrowLeft': + case 'KeyA': + controls.moveLeft = false; + break; + + case 'ArrowRight': + case 'KeyD': + controls.moveRight = false; + break; + + // case 'KeyC': controls.crouch = false; break; + // case 'Space': controls.jump = false; break; + // case 'ControlLeft': + // case 'ControlRight': controls.attack = false; break; + } +} + +// + +function animate() { + render(); + + stats.update(); +} + +function render() { + const delta = clock.getDelta(); + + for (let i = 0; i < nCharacters; i++) { + characters[i].update(delta); + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_mdd.ts b/examples-testing/examples/webgl_loader_mdd.ts new file mode 100644 index 000000000..5b13e8f4b --- /dev/null +++ b/examples-testing/examples/webgl_loader_mdd.ts @@ -0,0 +1,62 @@ +import * as THREE from 'three'; + +import { MDDLoader } from 'three/addons/loaders/MDDLoader.js'; + +let camera, scene, renderer, mixer, clock; + +init(); + +function init() { + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(8, 8, 8); + camera.lookAt(scene.position); + + clock = new THREE.Clock(); + + // + + const loader = new MDDLoader(); + loader.load('models/mdd/cube.mdd', function (result) { + const morphTargets = result.morphTargets; + const clip = result.clip; + // clip.optimize(); // optional + + const geometry = new THREE.BoxGeometry(); + geometry.morphAttributes.position = morphTargets; // apply morph targets + + const material = new THREE.MeshNormalMaterial(); + + const mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + mixer = new THREE.AnimationMixer(mesh); + mixer.clipAction(clip).play(); // use clip + }); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const delta = clock.getDelta(); + + if (mixer) mixer.update(delta); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_obj.ts b/examples-testing/examples/webgl_loader_obj.ts new file mode 100644 index 000000000..f61eeb758 --- /dev/null +++ b/examples-testing/examples/webgl_loader_obj.ts @@ -0,0 +1,98 @@ +import * as THREE from 'three'; + +import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer; + +let object; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 20); + camera.position.z = 2.5; + + // scene + + scene = new THREE.Scene(); + + const ambientLight = new THREE.AmbientLight(0xffffff); + scene.add(ambientLight); + + const pointLight = new THREE.PointLight(0xffffff, 15); + camera.add(pointLight); + scene.add(camera); + + // manager + + function loadModel() { + object.traverse(function (child) { + if (child.isMesh) child.material.map = texture; + }); + + object.position.y = -0.95; + object.scale.setScalar(0.01); + scene.add(object); + + render(); + } + + const manager = new THREE.LoadingManager(loadModel); + + // texture + + const textureLoader = new THREE.TextureLoader(manager); + const texture = textureLoader.load('textures/uv_grid_opengl.jpg', render); + texture.colorSpace = THREE.SRGBColorSpace; + + // model + + function onProgress(xhr) { + if (xhr.lengthComputable) { + const percentComplete = (xhr.loaded / xhr.total) * 100; + console.log('model ' + percentComplete.toFixed(2) + '% downloaded'); + } + } + + function onError() {} + + const loader = new OBJLoader(manager); + loader.load( + 'models/obj/male02/male02.obj', + function (obj) { + object = obj; + }, + onProgress, + onError, + ); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 2; + controls.maxDistance = 5; + controls.addEventListener('change', render); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_obj_mtl.ts b/examples-testing/examples/webgl_loader_obj_mtl.ts new file mode 100644 index 000000000..4308aee7b --- /dev/null +++ b/examples-testing/examples/webgl_loader_obj_mtl.ts @@ -0,0 +1,82 @@ +import * as THREE from 'three'; + +import { MTLLoader } from 'three/addons/loaders/MTLLoader.js'; +import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 20); + camera.position.z = 2.5; + + // scene + + scene = new THREE.Scene(); + + const ambientLight = new THREE.AmbientLight(0xffffff); + scene.add(ambientLight); + + const pointLight = new THREE.PointLight(0xffffff, 15); + camera.add(pointLight); + scene.add(camera); + + // model + + const onProgress = function (xhr) { + if (xhr.lengthComputable) { + const percentComplete = (xhr.loaded / xhr.total) * 100; + console.log(percentComplete.toFixed(2) + '% downloaded'); + } + }; + + new MTLLoader().setPath('models/obj/male02/').load('male02.mtl', function (materials) { + materials.preload(); + + new OBJLoader() + .setMaterials(materials) + .setPath('models/obj/male02/') + .load( + 'male02.obj', + function (object) { + object.position.y = -0.95; + object.scale.setScalar(0.01); + scene.add(object); + }, + onProgress, + ); + }); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 2; + controls.maxDistance = 5; + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_pcd.ts b/examples-testing/examples/webgl_loader_pcd.ts new file mode 100644 index 000000000..d69e3fa2a --- /dev/null +++ b/examples-testing/examples/webgl_loader_pcd.ts @@ -0,0 +1,65 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { PCDLoader } from 'three/addons/loaders/PCDLoader.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer; + +init(); +render(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 0.01, 40); + camera.position.set(0, 0, 1); + scene.add(camera); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.minDistance = 0.5; + controls.maxDistance = 10; + + //scene.add( new THREE.AxesHelper( 1 ) ); + + const loader = new PCDLoader(); + loader.load('./models/pcd/binary/Zaghetto.pcd', function (points) { + points.geometry.center(); + points.geometry.rotateX(Math.PI); + points.name = 'Zaghetto.pcd'; + scene.add(points); + + // + + const gui = new GUI(); + + gui.add(points.material, 'size', 0.001, 0.01).onChange(render); + gui.addColor(points.material, 'color').onChange(render); + gui.open(); + + // + + render(); + }); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_pdb.ts b/examples-testing/examples/webgl_loader_pdb.ts new file mode 100644 index 000000000..b560efa73 --- /dev/null +++ b/examples-testing/examples/webgl_loader_pdb.ts @@ -0,0 +1,208 @@ +import * as THREE from 'three'; + +import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; +import { PDBLoader } from 'three/addons/loaders/PDBLoader.js'; +import { CSS2DRenderer, CSS2DObject } from 'three/addons/renderers/CSS2DRenderer.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, labelRenderer; +let controls; + +let root; + +const MOLECULES = { + Ethanol: 'ethanol.pdb', + Aspirin: 'aspirin.pdb', + Caffeine: 'caffeine.pdb', + Nicotine: 'nicotine.pdb', + LSD: 'lsd.pdb', + Cocaine: 'cocaine.pdb', + Cholesterol: 'cholesterol.pdb', + Lycopene: 'lycopene.pdb', + Glucose: 'glucose.pdb', + 'Aluminium oxide': 'Al2O3.pdb', + Cubane: 'cubane.pdb', + Copper: 'cu.pdb', + Fluorite: 'caf2.pdb', + Salt: 'nacl.pdb', + 'YBCO superconductor': 'ybco.pdb', + Buckyball: 'buckyball.pdb', + Graphite: 'graphite.pdb', +}; + +const params = { + molecule: 'caffeine.pdb', +}; + +const loader = new PDBLoader(); +const offset = new THREE.Vector3(); + +init(); + +function init() { + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x050505); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 5000); + camera.position.z = 1000; + scene.add(camera); + + const light1 = new THREE.DirectionalLight(0xffffff, 2.5); + light1.position.set(1, 1, 1); + scene.add(light1); + + const light2 = new THREE.DirectionalLight(0xffffff, 1.5); + light2.position.set(-1, -1, 1); + scene.add(light2); + + root = new THREE.Group(); + scene.add(root); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.getElementById('container').appendChild(renderer.domElement); + + labelRenderer = new CSS2DRenderer(); + labelRenderer.setSize(window.innerWidth, window.innerHeight); + labelRenderer.domElement.style.position = 'absolute'; + labelRenderer.domElement.style.top = '0px'; + labelRenderer.domElement.style.pointerEvents = 'none'; + document.getElementById('container').appendChild(labelRenderer.domElement); + + // + + controls = new TrackballControls(camera, renderer.domElement); + controls.minDistance = 500; + controls.maxDistance = 2000; + + // + + loadMolecule(params.molecule); + + // + + window.addEventListener('resize', onWindowResize); + + // + + const gui = new GUI(); + + gui.add(params, 'molecule', MOLECULES).onChange(loadMolecule); + gui.open(); +} + +// + +function loadMolecule(model) { + const url = 'models/pdb/' + model; + + while (root.children.length > 0) { + const object = root.children[0]; + object.parent.remove(object); + } + + loader.load(url, function (pdb) { + const geometryAtoms = pdb.geometryAtoms; + const geometryBonds = pdb.geometryBonds; + const json = pdb.json; + + const boxGeometry = new THREE.BoxGeometry(1, 1, 1); + const sphereGeometry = new THREE.IcosahedronGeometry(1, 3); + + geometryAtoms.computeBoundingBox(); + geometryAtoms.boundingBox.getCenter(offset).negate(); + + geometryAtoms.translate(offset.x, offset.y, offset.z); + geometryBonds.translate(offset.x, offset.y, offset.z); + + let positions = geometryAtoms.getAttribute('position'); + const colors = geometryAtoms.getAttribute('color'); + + const position = new THREE.Vector3(); + const color = new THREE.Color(); + + for (let i = 0; i < positions.count; i++) { + position.x = positions.getX(i); + position.y = positions.getY(i); + position.z = positions.getZ(i); + + color.r = colors.getX(i); + color.g = colors.getY(i); + color.b = colors.getZ(i); + + const material = new THREE.MeshPhongMaterial({ color: color }); + + const object = new THREE.Mesh(sphereGeometry, material); + object.position.copy(position); + object.position.multiplyScalar(75); + object.scale.multiplyScalar(25); + root.add(object); + + const atom = json.atoms[i]; + + const text = document.createElement('div'); + text.className = 'label'; + text.style.color = 'rgb(' + atom[3][0] + ',' + atom[3][1] + ',' + atom[3][2] + ')'; + text.textContent = atom[4]; + + const label = new CSS2DObject(text); + label.position.copy(object.position); + root.add(label); + } + + positions = geometryBonds.getAttribute('position'); + + const start = new THREE.Vector3(); + const end = new THREE.Vector3(); + + for (let i = 0; i < positions.count; i += 2) { + start.x = positions.getX(i); + start.y = positions.getY(i); + start.z = positions.getZ(i); + + end.x = positions.getX(i + 1); + end.y = positions.getY(i + 1); + end.z = positions.getZ(i + 1); + + start.multiplyScalar(75); + end.multiplyScalar(75); + + const object = new THREE.Mesh(boxGeometry, new THREE.MeshPhongMaterial({ color: 0xffffff })); + object.position.copy(start); + object.position.lerp(end, 0.5); + object.scale.set(5, 5, start.distanceTo(end)); + object.lookAt(end); + root.add(object); + } + }); +} + +// + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + labelRenderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + controls.update(); + + const time = Date.now() * 0.0004; + + root.rotation.x = time; + root.rotation.y = time * 0.7; + + render(); +} + +function render() { + renderer.render(scene, camera); + labelRenderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_ply.ts b/examples-testing/examples/webgl_loader_ply.ts new file mode 100644 index 000000000..0f4042b7d --- /dev/null +++ b/examples-testing/examples/webgl_loader_ply.ts @@ -0,0 +1,146 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { PLYLoader } from 'three/addons/loaders/PLYLoader.js'; + +let container, stats; + +let camera, cameraTarget, scene, renderer; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 15); + camera.position.set(3, 0.15, 3); + + cameraTarget = new THREE.Vector3(0, -0.1, 0); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x72645b); + scene.fog = new THREE.Fog(0x72645b, 2, 15); + + // Ground + + const plane = new THREE.Mesh( + new THREE.PlaneGeometry(40, 40), + new THREE.MeshPhongMaterial({ color: 0xcbcbcb, specular: 0x474747 }), + ); + plane.rotation.x = -Math.PI / 2; + plane.position.y = -0.5; + scene.add(plane); + + plane.receiveShadow = true; + + // PLY file + + const loader = new PLYLoader(); + loader.load('./models/ply/ascii/dolphins.ply', function (geometry) { + geometry.computeVertexNormals(); + + const material = new THREE.MeshStandardMaterial({ color: 0x009cff, flatShading: true }); + const mesh = new THREE.Mesh(geometry, material); + + mesh.position.y = -0.2; + mesh.position.z = 0.3; + mesh.rotation.x = -Math.PI / 2; + mesh.scale.multiplyScalar(0.001); + + mesh.castShadow = true; + mesh.receiveShadow = true; + + scene.add(mesh); + }); + + loader.load('./models/ply/binary/Lucy100k.ply', function (geometry) { + geometry.computeVertexNormals(); + + const material = new THREE.MeshStandardMaterial({ color: 0x009cff, flatShading: true }); + const mesh = new THREE.Mesh(geometry, material); + + mesh.position.x = -0.2; + mesh.position.y = -0.02; + mesh.position.z = -0.2; + mesh.scale.multiplyScalar(0.0006); + + mesh.castShadow = true; + mesh.receiveShadow = true; + + scene.add(mesh); + }); + + // Lights + + scene.add(new THREE.HemisphereLight(0x8d7c7c, 0x494966, 3)); + + addShadowedLight(1, 1, 1, 0xffffff, 3.5); + addShadowedLight(0.5, 1, -1, 0xffd500, 3); + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + + renderer.shadowMap.enabled = true; + + container.appendChild(renderer.domElement); + + // stats + + stats = new Stats(); + container.appendChild(stats.dom); + + // resize + + window.addEventListener('resize', onWindowResize); +} + +function addShadowedLight(x, y, z, color, intensity) { + const directionalLight = new THREE.DirectionalLight(color, intensity); + directionalLight.position.set(x, y, z); + scene.add(directionalLight); + + directionalLight.castShadow = true; + + const d = 1; + directionalLight.shadow.camera.left = -d; + directionalLight.shadow.camera.right = d; + directionalLight.shadow.camera.top = d; + directionalLight.shadow.camera.bottom = -d; + + directionalLight.shadow.camera.near = 1; + directionalLight.shadow.camera.far = 4; + + directionalLight.shadow.mapSize.width = 1024; + directionalLight.shadow.mapSize.height = 1024; + + directionalLight.shadow.bias = -0.001; +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + const timer = Date.now() * 0.0005; + + camera.position.x = Math.sin(timer) * 2.5; + camera.position.z = Math.cos(timer) * 2.5; + + camera.lookAt(cameraTarget); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_svg.ts b/examples-testing/examples/webgl_loader_svg.ts new file mode 100644 index 000000000..45361b92f --- /dev/null +++ b/examples-testing/examples/webgl_loader_svg.ts @@ -0,0 +1,193 @@ +import * as THREE from 'three'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { SVGLoader } from 'three/addons/loaders/SVGLoader.js'; + +let renderer, scene, camera, gui, guiData; + +init(); + +// + +function init() { + const container = document.getElementById('container'); + + // + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 0, 200); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + container.appendChild(renderer.domElement); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); + controls.screenSpacePanning = true; + + // + + window.addEventListener('resize', onWindowResize); + + guiData = { + currentURL: 'models/svg/tiger.svg', + drawFillShapes: true, + drawStrokes: true, + fillShapesWireframe: false, + strokesWireframe: false, + }; + + loadSVG(guiData.currentURL); + + createGUI(); +} + +function createGUI() { + if (gui) gui.destroy(); + + gui = new GUI(); + + gui.add(guiData, 'currentURL', { + Tiger: 'models/svg/tiger.svg', + 'Joins and caps': 'models/svg/lineJoinsAndCaps.svg', + Hexagon: 'models/svg/hexagon.svg', + Energy: 'models/svg/energy.svg', + 'Test 1': 'models/svg/tests/1.svg', + 'Test 2': 'models/svg/tests/2.svg', + 'Test 3': 'models/svg/tests/3.svg', + 'Test 4': 'models/svg/tests/4.svg', + 'Test 5': 'models/svg/tests/5.svg', + 'Test 6': 'models/svg/tests/6.svg', + 'Test 7': 'models/svg/tests/7.svg', + 'Test 8': 'models/svg/tests/8.svg', + 'Test 9': 'models/svg/tests/9.svg', + Units: 'models/svg/tests/units.svg', + Ordering: 'models/svg/tests/ordering.svg', + Defs: 'models/svg/tests/testDefs/Svg-defs.svg', + Defs2: 'models/svg/tests/testDefs/Svg-defs2.svg', + Defs3: 'models/svg/tests/testDefs/Wave-defs.svg', + Defs4: 'models/svg/tests/testDefs/defs4.svg', + Defs5: 'models/svg/tests/testDefs/defs5.svg', + 'Style CSS inside defs': 'models/svg/style-css-inside-defs.svg', + 'Multiple CSS classes': 'models/svg/multiple-css-classes.svg', + 'Zero Radius': 'models/svg/zero-radius.svg', + 'Styles in svg tag': 'models/svg/tests/styles.svg', + 'Round join': 'models/svg/tests/roundJoinPrecisionIssue.svg', + 'Ellipse Transformations': 'models/svg/tests/ellipseTransform.svg', + singlePointTest: 'models/svg/singlePointTest.svg', + singlePointTest2: 'models/svg/singlePointTest2.svg', + singlePointTest3: 'models/svg/singlePointTest3.svg', + emptyPath: 'models/svg/emptyPath.svg', + }) + .name('SVG File') + .onChange(update); + + gui.add(guiData, 'drawStrokes').name('Draw strokes').onChange(update); + + gui.add(guiData, 'drawFillShapes').name('Draw fill shapes').onChange(update); + + gui.add(guiData, 'strokesWireframe').name('Wireframe strokes').onChange(update); + + gui.add(guiData, 'fillShapesWireframe').name('Wireframe fill shapes').onChange(update); + + function update() { + loadSVG(guiData.currentURL); + } +} + +function loadSVG(url) { + // + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xb0b0b0); + + // + + const helper = new THREE.GridHelper(160, 10, 0x8d8d8d, 0xc1c1c1); + helper.rotation.x = Math.PI / 2; + scene.add(helper); + + // + + const loader = new SVGLoader(); + + loader.load(url, function (data) { + const group = new THREE.Group(); + group.scale.multiplyScalar(0.25); + group.position.x = -70; + group.position.y = 70; + group.scale.y *= -1; + + let renderOrder = 0; + + for (const path of data.paths) { + const fillColor = path.userData.style.fill; + + if (guiData.drawFillShapes && fillColor !== undefined && fillColor !== 'none') { + const material = new THREE.MeshBasicMaterial({ + color: new THREE.Color().setStyle(fillColor), + opacity: path.userData.style.fillOpacity, + transparent: true, + side: THREE.DoubleSide, + depthWrite: false, + wireframe: guiData.fillShapesWireframe, + }); + + const shapes = SVGLoader.createShapes(path); + + for (const shape of shapes) { + const geometry = new THREE.ShapeGeometry(shape); + const mesh = new THREE.Mesh(geometry, material); + mesh.renderOrder = renderOrder++; + + group.add(mesh); + } + } + + const strokeColor = path.userData.style.stroke; + + if (guiData.drawStrokes && strokeColor !== undefined && strokeColor !== 'none') { + const material = new THREE.MeshBasicMaterial({ + color: new THREE.Color().setStyle(strokeColor), + opacity: path.userData.style.strokeOpacity, + transparent: true, + side: THREE.DoubleSide, + depthWrite: false, + wireframe: guiData.strokesWireframe, + }); + + for (const subPath of path.subPaths) { + const geometry = SVGLoader.pointsToStroke(subPath.getPoints(), path.userData.style); + + if (geometry) { + const mesh = new THREE.Mesh(geometry, material); + mesh.renderOrder = renderOrder++; + + group.add(mesh); + } + } + } + } + + scene.add(group); + + render(); + }); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_texture_dds.ts b/examples-testing/examples/webgl_loader_texture_dds.ts new file mode 100644 index 000000000..bc4bd0572 --- /dev/null +++ b/examples-testing/examples/webgl_loader_texture_dds.ts @@ -0,0 +1,207 @@ +import * as THREE from 'three'; + +import { DDSLoader } from 'three/addons/loaders/DDSLoader.js'; + +let camera, scene, renderer; +const meshes = []; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.z = 15; + + scene = new THREE.Scene(); + + const geometry = new THREE.BoxGeometry(2, 2, 2); + + /* + This is how compressed textures are supposed to be used: + + DXT1 - RGB - opaque textures + DXT3 - RGBA - transparent textures with sharp alpha transitions + DXT5 - RGBA - transparent textures with full alpha range + */ + + const loader = new DDSLoader(); + + const map1 = loader.load('textures/compressed/disturb_dxt1_nomip.dds'); + map1.minFilter = map1.magFilter = THREE.LinearFilter; + map1.anisotropy = 4; + map1.colorSpace = THREE.SRGBColorSpace; + + const map2 = loader.load('textures/compressed/disturb_dxt1_mip.dds'); + map2.anisotropy = 4; + map2.colorSpace = THREE.SRGBColorSpace; + + const map3 = loader.load('textures/compressed/hepatica_dxt3_mip.dds'); + map3.anisotropy = 4; + map3.colorSpace = THREE.SRGBColorSpace; + + const map4 = loader.load('textures/compressed/explosion_dxt5_mip.dds'); + map4.anisotropy = 4; + map4.colorSpace = THREE.SRGBColorSpace; + + const map5 = loader.load('textures/compressed/disturb_argb_nomip.dds'); + map5.minFilter = map5.magFilter = THREE.LinearFilter; + map5.anisotropy = 4; + map5.colorSpace = THREE.SRGBColorSpace; + + const map6 = loader.load('textures/compressed/disturb_argb_mip.dds'); + map6.anisotropy = 4; + map6.colorSpace = THREE.SRGBColorSpace; + + const map7 = loader.load('textures/compressed/disturb_dx10_bc6h_signed_nomip.dds'); + map7.anisotropy = 4; + + const map8 = loader.load('textures/compressed/disturb_dx10_bc6h_signed_mip.dds'); + map8.anisotropy = 4; + + const map9 = loader.load('textures/compressed/disturb_dx10_bc6h_unsigned_nomip.dds'); + map9.anisotropy = 4; + + const map10 = loader.load('textures/compressed/disturb_dx10_bc6h_unsigned_mip.dds'); + map10.anisotropy = 4; + + const cubemap1 = loader.load('textures/compressed/Mountains.dds', function (texture) { + texture.magFilter = THREE.LinearFilter; + texture.minFilter = THREE.LinearFilter; + texture.mapping = THREE.CubeReflectionMapping; + texture.colorSpace = THREE.SRGBColorSpace; + material1.needsUpdate = true; + }); + + const cubemap2 = loader.load('textures/compressed/Mountains_argb_mip.dds', function (texture) { + texture.magFilter = THREE.LinearFilter; + texture.minFilter = THREE.LinearFilter; + texture.mapping = THREE.CubeReflectionMapping; + texture.colorSpace = THREE.SRGBColorSpace; + material5.needsUpdate = true; + }); + + const cubemap3 = loader.load('textures/compressed/Mountains_argb_nomip.dds', function (texture) { + texture.magFilter = THREE.LinearFilter; + texture.minFilter = THREE.LinearFilter; + texture.mapping = THREE.CubeReflectionMapping; + texture.colorSpace = THREE.SRGBColorSpace; + material6.needsUpdate = true; + }); + + const material1 = new THREE.MeshBasicMaterial({ map: map1, envMap: cubemap1 }); + const material2 = new THREE.MeshBasicMaterial({ map: map2 }); + const material3 = new THREE.MeshBasicMaterial({ map: map3, alphaTest: 0.5, side: THREE.DoubleSide }); + const material4 = new THREE.MeshBasicMaterial({ + map: map4, + side: THREE.DoubleSide, + blending: THREE.AdditiveBlending, + depthTest: false, + transparent: true, + }); + const material5 = new THREE.MeshBasicMaterial({ envMap: cubemap2 }); + const material6 = new THREE.MeshBasicMaterial({ envMap: cubemap3 }); + const material7 = new THREE.MeshBasicMaterial({ map: map5 }); + const material8 = new THREE.MeshBasicMaterial({ map: map6 }); + const material9 = new THREE.MeshBasicMaterial({ map: map7 }); + const material10 = new THREE.MeshBasicMaterial({ map: map8 }); + const material11 = new THREE.MeshBasicMaterial({ map: map9 }); + const material12 = new THREE.MeshBasicMaterial({ map: map10 }); + + let mesh = new THREE.Mesh(new THREE.TorusGeometry(), material1); + mesh.position.x = -10; + mesh.position.y = -2; + scene.add(mesh); + meshes.push(mesh); + + mesh = new THREE.Mesh(geometry, material2); + mesh.position.x = -6; + mesh.position.y = -2; + scene.add(mesh); + meshes.push(mesh); + + mesh = new THREE.Mesh(geometry, material3); + mesh.position.x = -6; + mesh.position.y = 2; + scene.add(mesh); + meshes.push(mesh); + + mesh = new THREE.Mesh(geometry, material4); + mesh.position.x = -10; + mesh.position.y = 2; + scene.add(mesh); + meshes.push(mesh); + + mesh = new THREE.Mesh(geometry, material5); + mesh.position.x = -2; + mesh.position.y = 2; + scene.add(mesh); + meshes.push(mesh); + + mesh = new THREE.Mesh(geometry, material6); + mesh.position.x = -2; + mesh.position.y = -2; + scene.add(mesh); + meshes.push(mesh); + + mesh = new THREE.Mesh(geometry, material7); + mesh.position.x = 2; + mesh.position.y = -2; + scene.add(mesh); + meshes.push(mesh); + + mesh = new THREE.Mesh(geometry, material8); + mesh.position.x = 2; + mesh.position.y = 2; + scene.add(mesh); + meshes.push(mesh); + + mesh = new THREE.Mesh(geometry, material9); + mesh.position.x = 6; + mesh.position.y = -2; + scene.add(mesh); + meshes.push(mesh); + + mesh = new THREE.Mesh(geometry, material10); + mesh.position.x = 6; + mesh.position.y = 2; + scene.add(mesh); + meshes.push(mesh); + + mesh = new THREE.Mesh(geometry, material11); + mesh.position.x = 10; + mesh.position.y = -2; + scene.add(mesh); + meshes.push(mesh); + + mesh = new THREE.Mesh(geometry, material12); + mesh.position.x = 10; + mesh.position.y = 2; + scene.add(mesh); + meshes.push(mesh); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const time = Date.now() * 0.001; + + for (let i = 0; i < meshes.length; i++) { + const mesh = meshes[i]; + mesh.rotation.x = time; + mesh.rotation.y = time; + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_texture_ktx.ts b/examples-testing/examples/webgl_loader_texture_ktx.ts new file mode 100644 index 000000000..af66eb810 --- /dev/null +++ b/examples-testing/examples/webgl_loader_texture_ktx.ts @@ -0,0 +1,137 @@ +import * as THREE from 'three'; + +import { KTXLoader } from 'three/addons/loaders/KTXLoader.js'; + +/* + This is how compressed textures are supposed to be used: + + best for desktop: + BC1(DXT1) - opaque textures + BC3(DXT5) - transparent textures with full alpha range + + best for iOS: + PVR2, PVR4 - opaque textures or alpha + + best for Android: + ETC1 - opaque textures + ASTC_4x4, ASTC8x8 - transparent textures with full alpha range + */ + +let camera, scene, renderer; +const meshes = []; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + const formats = { + astc: renderer.extensions.has('WEBGL_compressed_texture_astc'), + etc1: renderer.extensions.has('WEBGL_compressed_texture_etc1'), + s3tc: renderer.extensions.has('WEBGL_compressed_texture_s3tc'), + pvrtc: renderer.extensions.has('WEBGL_compressed_texture_pvrtc'), + }; + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 2000); + camera.position.z = 1000; + + scene = new THREE.Scene(); + + const geometry = new THREE.BoxGeometry(200, 200, 200); + let material1, material2; + + // TODO: add cubemap support + const loader = new KTXLoader(); + + if (formats.pvrtc) { + material1 = new THREE.MeshBasicMaterial({ + map: loader.load('textures/compressed/disturb_PVR2bpp.ktx'), + }); + material1.map.colorSpace = THREE.SRGBColorSpace; + material2 = new THREE.MeshBasicMaterial({ + map: loader.load('textures/compressed/lensflare_PVR4bpp.ktx'), + depthTest: false, + transparent: true, + side: THREE.DoubleSide, + }); + material2.map.colorSpace = THREE.SRGBColorSpace; + + meshes.push(new THREE.Mesh(geometry, material1)); + meshes.push(new THREE.Mesh(geometry, material2)); + } + + if (formats.s3tc) { + material1 = new THREE.MeshBasicMaterial({ + map: loader.load('textures/compressed/disturb_BC1.ktx'), + }); + material1.map.colorSpace = THREE.SRGBColorSpace; + material2 = new THREE.MeshBasicMaterial({ + map: loader.load('textures/compressed/lensflare_BC3.ktx'), + depthTest: false, + transparent: true, + side: THREE.DoubleSide, + }); + material2.map.colorSpace = THREE.SRGBColorSpace; + + meshes.push(new THREE.Mesh(geometry, material1)); + meshes.push(new THREE.Mesh(geometry, material2)); + } + + if (formats.etc1) { + material1 = new THREE.MeshBasicMaterial({ + map: loader.load('textures/compressed/disturb_ETC1.ktx'), + }); + + meshes.push(new THREE.Mesh(geometry, material1)); + } + + if (formats.astc) { + material1 = new THREE.MeshBasicMaterial({ + map: loader.load('textures/compressed/disturb_ASTC4x4.ktx'), + }); + material1.map.colorSpace = THREE.SRGBColorSpace; + material2 = new THREE.MeshBasicMaterial({ + map: loader.load('textures/compressed/lensflare_ASTC8x8.ktx'), + depthTest: false, + transparent: true, + side: THREE.DoubleSide, + }); + material2.map.colorSpace = THREE.SRGBColorSpace; + + meshes.push(new THREE.Mesh(geometry, material1)); + meshes.push(new THREE.Mesh(geometry, material2)); + } + + let x = (-meshes.length / 2) * 150; + for (let i = 0; i < meshes.length; ++i, x += 300) { + const mesh = meshes[i]; + mesh.position.x = x; + mesh.position.y = 0; + scene.add(mesh); + } + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const time = Date.now() * 0.001; + + for (let i = 0; i < meshes.length; i++) { + const mesh = meshes[i]; + mesh.rotation.x = time; + mesh.rotation.y = time; + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_texture_logluv.ts b/examples-testing/examples/webgl_loader_texture_logluv.ts new file mode 100644 index 000000000..7f3fbd4a0 --- /dev/null +++ b/examples-testing/examples/webgl_loader_texture_logluv.ts @@ -0,0 +1,75 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { LogLuvLoader } from 'three/addons/loaders/LogLuvLoader.js'; + +const params = { + exposure: 2.0, +}; + +let renderer, scene, camera; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + renderer.toneMapping = THREE.ReinhardToneMapping; + renderer.toneMappingExposure = params.exposure; + + scene = new THREE.Scene(); + + const aspect = window.innerWidth / window.innerHeight; + + camera = new THREE.OrthographicCamera(-aspect, aspect, 1, -1, 0, 1); + + new LogLuvLoader().load('textures/memorial.tif', function (texture) { + const material = new THREE.MeshBasicMaterial({ map: texture }); + + const quad = new THREE.PlaneGeometry(1, 1.5); + + const mesh = new THREE.Mesh(quad, material); + + scene.add(mesh); + + render(); + }); + + // + + const gui = new GUI(); + + gui.add(params, 'exposure', 0, 4, 0.01).onChange(render); + gui.open(); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const aspect = window.innerWidth / window.innerHeight; + + const frustumHeight = camera.top - camera.bottom; + + camera.left = (-frustumHeight * aspect) / 2; + camera.right = (frustumHeight * aspect) / 2; + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +// + +function render() { + renderer.toneMappingExposure = params.exposure; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_texture_rgbm.ts b/examples-testing/examples/webgl_loader_texture_rgbm.ts new file mode 100644 index 000000000..a882cdbc5 --- /dev/null +++ b/examples-testing/examples/webgl_loader_texture_rgbm.ts @@ -0,0 +1,75 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { RGBMLoader } from 'three/addons/loaders/RGBMLoader.js'; + +const params = { + exposure: 2.0, +}; + +let renderer, scene, camera; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + renderer.toneMapping = THREE.ReinhardToneMapping; + renderer.toneMappingExposure = params.exposure; + + scene = new THREE.Scene(); + + const aspect = window.innerWidth / window.innerHeight; + + camera = new THREE.OrthographicCamera(-aspect, aspect, 1, -1, 0, 1); + + new RGBMLoader().setMaxRange(16).load('textures/memorial.png', function (texture) { + const material = new THREE.MeshBasicMaterial({ map: texture }); + + const quad = new THREE.PlaneGeometry(1, 1.5); + + const mesh = new THREE.Mesh(quad, material); + + scene.add(mesh); + + render(); + }); + + // + + const gui = new GUI(); + + gui.add(params, 'exposure', 0, 4, 0.01).onChange(render); + gui.open(); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const aspect = window.innerWidth / window.innerHeight; + + const frustumHeight = camera.top - camera.bottom; + + camera.left = (-frustumHeight * aspect) / 2; + camera.right = (frustumHeight * aspect) / 2; + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +// + +function render() { + renderer.toneMappingExposure = params.exposure; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_texture_tga.ts b/examples-testing/examples/webgl_loader_texture_tga.ts new file mode 100644 index 000000000..c4f65b79a --- /dev/null +++ b/examples-testing/examples/webgl_loader_texture_tga.ts @@ -0,0 +1,90 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { TGALoader } from 'three/addons/loaders/TGALoader.js'; + +let camera, scene, renderer, stats; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(0, 1, 5); + + scene = new THREE.Scene(); + + // + + const loader = new TGALoader(); + const geometry = new THREE.BoxGeometry(); + + // add box 1 - grey8 texture + + const texture1 = loader.load('textures/crate_grey8.tga'); + texture1.colorSpace = THREE.SRGBColorSpace; + const material1 = new THREE.MeshPhongMaterial({ color: 0xffffff, map: texture1 }); + + const mesh1 = new THREE.Mesh(geometry, material1); + mesh1.position.x = -1; + + scene.add(mesh1); + + // add box 2 - tga texture + + const texture2 = loader.load('textures/crate_color8.tga'); + texture2.colorSpace = THREE.SRGBColorSpace; + const material2 = new THREE.MeshPhongMaterial({ color: 0xffffff, map: texture2 }); + + const mesh2 = new THREE.Mesh(geometry, material2); + mesh2.position.x = 1; + + scene.add(mesh2); + + // + + const ambientLight = new THREE.AmbientLight(0xffffff, 1.5); + scene.add(ambientLight); + + const light = new THREE.DirectionalLight(0xffffff, 2.5); + light.position.set(1, 1, 1); + scene.add(light); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.enableZoom = false; + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); + stats.update(); +} diff --git a/examples-testing/examples/webgl_loader_texture_tiff.ts b/examples-testing/examples/webgl_loader_texture_tiff.ts new file mode 100644 index 000000000..f097774aa --- /dev/null +++ b/examples-testing/examples/webgl_loader_texture_tiff.ts @@ -0,0 +1,87 @@ +import * as THREE from 'three'; + +import { TIFFLoader } from 'three/addons/loaders/TIFFLoader.js'; + +let renderer, scene, camera; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 10); + camera.position.set(0, 0, 4); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + const loader = new TIFFLoader(); + + const geometry = new THREE.PlaneGeometry(); + + // uncompressed + + loader.load('textures/tiff/crate_uncompressed.tif', function (texture) { + texture.colorSpace = THREE.SRGBColorSpace; + + const material = new THREE.MeshBasicMaterial({ map: texture }); + + const mesh = new THREE.Mesh(geometry, material); + mesh.position.set(-1.5, 0, 0); + + scene.add(mesh); + + render(); + }); + + // LZW + + loader.load('textures/tiff/crate_lzw.tif', function (texture) { + texture.colorSpace = THREE.SRGBColorSpace; + + const material = new THREE.MeshBasicMaterial({ map: texture }); + + const mesh = new THREE.Mesh(geometry, material); + mesh.position.set(0, 0, 0); + + scene.add(mesh); + + render(); + }); + + // JPEG + + loader.load('textures/tiff/crate_jpeg.tif', function (texture) { + texture.colorSpace = THREE.SRGBColorSpace; + + const material = new THREE.MeshBasicMaterial({ map: texture }); + + const mesh = new THREE.Mesh(geometry, material); + mesh.position.set(1.5, 0, 0); + + scene.add(mesh); + + render(); + }); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +// + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_texture_ultrahdr.ts b/examples-testing/examples/webgl_loader_texture_ultrahdr.ts new file mode 100644 index 000000000..c8bce4bf9 --- /dev/null +++ b/examples-testing/examples/webgl_loader_texture_ultrahdr.ts @@ -0,0 +1,101 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +const params = { + autoRotate: true, + metalness: 1.0, + roughness: 0.0, + exposure: 1.0, + resolution: '2k', + type: 'HalfFloatType', +}; + +let renderer, scene, camera, controls, torusMesh, loader; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = params.exposure; + + renderer.setAnimationLoop(render); + + scene = new THREE.Scene(); + + torusMesh = new THREE.Mesh( + new THREE.TorusKnotGeometry(1, 0.4, 128, 128, 1, 3), + new THREE.MeshStandardMaterial({ roughness: params.roughness, metalness: params.metalness }), + ); + scene.add(torusMesh); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 500); + camera.position.set(0.0, 0.0, -6.0); + + controls = new OrbitControls(camera, renderer.domElement); + + loader = new UltraHDRLoader(); + loader.setDataType(THREE.FloatType); + + const loadEnvironment = function (resolution = '2k', type = 'HalfFloatType') { + loader.setDataType(THREE[type]); + + loader.load(`textures/equirectangular/spruit_sunrise_${resolution}.hdr.jpg`, function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + texture.needsUpdate = true; + + scene.background = texture; + scene.environment = texture; + }); + }; + + loadEnvironment(params.resolution, params.type); + + const gui = new GUI(); + + gui.add(params, 'autoRotate'); + gui.add(params, 'metalness', 0, 1, 0.01); + gui.add(params, 'roughness', 0, 1, 0.01); + gui.add(params, 'exposure', 0, 4, 0.01); + gui.add(params, 'resolution', ['2k', '4k']).onChange(value => { + loadEnvironment(value, params.type); + }); + gui.add(params, 'type', ['HalfFloatType', 'FloatType']).onChange(value => { + loadEnvironment(params.resolution, value); + }); + + gui.open(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function render() { + torusMesh.material.roughness = params.roughness; + torusMesh.material.metalness = params.metalness; + + if (params.autoRotate) { + torusMesh.rotation.y += 0.005; + } + + renderer.toneMappingExposure = params.exposure; + + controls.update(); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_tilt.ts b/examples-testing/examples/webgl_loader_tilt.ts new file mode 100644 index 000000000..2a583c2b0 --- /dev/null +++ b/examples-testing/examples/webgl_loader_tilt.ts @@ -0,0 +1,54 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { TiltLoader } from 'three/addons/loaders/TiltLoader.js'; + +let camera, scene, renderer; + +init(); + +function init() { + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 500); + + camera.position.y = 43; + camera.position.z = 100; + + scene.add(camera); + + const grid = new THREE.GridHelper(50, 50, 0xffffff, 0x555555); + scene.add(grid); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + const loader = new TiltLoader(); + loader.load('./models/tilt/BRUSH_DOME.tilt', function (object) { + // console.log( object.children.length ); + scene.add(object); + render(); + }); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); + controls.target.y = camera.position.y; + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_ttf.ts b/examples-testing/examples/webgl_loader_ttf.ts new file mode 100644 index 000000000..168371a14 --- /dev/null +++ b/examples-testing/examples/webgl_loader_ttf.ts @@ -0,0 +1,231 @@ +import * as THREE from 'three'; + +import { TTFLoader } from 'three/addons/loaders/TTFLoader.js'; +import { Font } from 'three/addons/loaders/FontLoader.js'; +import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; + +let container; +let camera, cameraTarget, scene, renderer; +let group, textMesh1, textMesh2, textGeo, material; +let firstLetter = true; + +let text = 'three.js'; +const depth = 20, + size = 70, + hover = 30, + curveSegments = 4, + bevelThickness = 2, + bevelSize = 1.5; + +let font = null; +const mirror = true; + +let targetRotation = 0; +let targetRotationOnPointerDown = 0; + +let pointerX = 0; +let pointerXOnPointerDown = 0; + +let windowHalfX = window.innerWidth / 2; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + // CAMERA + + camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 1500); + camera.position.set(0, 400, 700); + + cameraTarget = new THREE.Vector3(0, 150, 0); + + // SCENE + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x000000); + scene.fog = new THREE.Fog(0x000000, 250, 1400); + + // LIGHTS + + const dirLight1 = new THREE.DirectionalLight(0xffffff, 0.4); + dirLight1.position.set(0, 0, 1).normalize(); + scene.add(dirLight1); + + const dirLight2 = new THREE.DirectionalLight(0xffffff, 2); + dirLight2.position.set(0, hover, 10).normalize(); + dirLight2.color.setHSL(Math.random(), 1, 0.5, THREE.SRGBColorSpace); + scene.add(dirLight2); + + material = new THREE.MeshPhongMaterial({ color: 0xffffff, flatShading: true }); + + group = new THREE.Group(); + group.position.y = 100; + + scene.add(group); + + const loader = new TTFLoader(); + + loader.load('fonts/ttf/kenpixel.ttf', function (json) { + font = new Font(json); + createText(); + }); + + const plane = new THREE.Mesh( + new THREE.PlaneGeometry(10000, 10000), + new THREE.MeshBasicMaterial({ color: 0xffffff, opacity: 0.5, transparent: true }), + ); + plane.position.y = 100; + plane.rotation.x = -Math.PI / 2; + scene.add(plane); + + // RENDERER + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // EVENTS + + container.style.touchAction = 'none'; + container.addEventListener('pointerdown', onPointerDown); + + document.addEventListener('keypress', onDocumentKeyPress); + document.addEventListener('keydown', onDocumentKeyDown); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onDocumentKeyDown(event) { + if (firstLetter) { + firstLetter = false; + text = ''; + } + + const keyCode = event.keyCode; + + // backspace + + if (keyCode === 8) { + event.preventDefault(); + + text = text.substring(0, text.length - 1); + refreshText(); + + return false; + } +} + +function onDocumentKeyPress(event) { + const keyCode = event.which; + + // backspace + + if (keyCode === 8) { + event.preventDefault(); + } else { + const ch = String.fromCharCode(keyCode); + text += ch; + + refreshText(); + } +} + +function createText() { + textGeo = new TextGeometry(text, { + font: font, + + size: size, + depth: depth, + curveSegments: curveSegments, + + bevelThickness: bevelThickness, + bevelSize: bevelSize, + bevelEnabled: true, + }); + + textGeo.computeBoundingBox(); + textGeo.computeVertexNormals(); + + const centerOffset = -0.5 * (textGeo.boundingBox.max.x - textGeo.boundingBox.min.x); + + textMesh1 = new THREE.Mesh(textGeo, material); + + textMesh1.position.x = centerOffset; + textMesh1.position.y = hover; + textMesh1.position.z = 0; + + textMesh1.rotation.x = 0; + textMesh1.rotation.y = Math.PI * 2; + + group.add(textMesh1); + + if (mirror) { + textMesh2 = new THREE.Mesh(textGeo, material); + + textMesh2.position.x = centerOffset; + textMesh2.position.y = -hover; + textMesh2.position.z = depth; + + textMesh2.rotation.x = Math.PI; + textMesh2.rotation.y = Math.PI * 2; + + group.add(textMesh2); + } +} + +function refreshText() { + group.remove(textMesh1); + if (mirror) group.remove(textMesh2); + + if (!text) return; + + createText(); +} + +function onPointerDown(event) { + if (event.isPrimary === false) return; + + pointerXOnPointerDown = event.clientX - windowHalfX; + targetRotationOnPointerDown = targetRotation; + + document.addEventListener('pointermove', onPointerMove); + document.addEventListener('pointerup', onPointerUp); +} + +function onPointerMove(event) { + if (event.isPrimary === false) return; + + pointerX = event.clientX - windowHalfX; + + targetRotation = targetRotationOnPointerDown + (pointerX - pointerXOnPointerDown) * 0.02; +} + +function onPointerUp() { + if (event.isPrimary === false) return; + + document.removeEventListener('pointermove', onPointerMove); + document.removeEventListener('pointerup', onPointerUp); +} + +// + +function animate() { + group.rotation.y += (targetRotation - group.rotation.y) * 0.05; + + camera.lookAt(cameraTarget); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_usdz.ts b/examples-testing/examples/webgl_loader_usdz.ts new file mode 100644 index 000000000..d75823d88 --- /dev/null +++ b/examples-testing/examples/webgl_loader_usdz.ts @@ -0,0 +1,68 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; +import { USDZLoader } from 'three/addons/loaders/USDZLoader.js'; + +let camera, scene, renderer; + +init(); + +async function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(0, 0.75, -1.5); + + scene = new THREE.Scene(); + + const rgbeLoader = new RGBELoader().setPath('textures/equirectangular/'); + + const usdzLoader = new USDZLoader().setPath('models/usdz/'); + + const [texture, model] = await Promise.all([ + rgbeLoader.loadAsync('venice_sunset_1k.hdr'), + usdzLoader.loadAsync('saeukkang.usdz'), + ]); + + // environment + + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + scene.backgroundBlurriness = 0.5; + scene.environment = texture; + + // model + + model.position.y = 0.25; + model.position.z = -0.25; + scene.add(model); + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 2.0; + document.body.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 1; + controls.maxDistance = 8; + // controls.target.y = 15; + // controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_vox.ts b/examples-testing/examples/webgl_loader_vox.ts new file mode 100644 index 000000000..061848012 --- /dev/null +++ b/examples-testing/examples/webgl_loader_vox.ts @@ -0,0 +1,104 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { VOXLoader, VOXMesh } from 'three/addons/loaders/VOXLoader.js'; + +let camera, controls, scene, renderer; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.01, 10); + camera.position.set(0.175, 0.075, 0.175); + + scene = new THREE.Scene(); + scene.add(camera); + + // light + + const hemiLight = new THREE.HemisphereLight(0xcccccc, 0x444444, 3); + scene.add(hemiLight); + + const dirLight = new THREE.DirectionalLight(0xffffff, 2.5); + dirLight.position.set(1.5, 3, 2.5); + scene.add(dirLight); + + const dirLight2 = new THREE.DirectionalLight(0xffffff, 1.5); + dirLight2.position.set(-1.5, -3, -2.5); + scene.add(dirLight2); + + const loader = new VOXLoader(); + loader.load('models/vox/monu10.vox', function (chunks) { + for (let i = 0; i < chunks.length; i++) { + const chunk = chunks[i]; + + // displayPalette( chunk.palette ); + + const mesh = new VOXMesh(chunk); + mesh.scale.setScalar(0.0015); + scene.add(mesh); + } + }); + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // controls + + controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 0.1; + controls.maxDistance = 0.5; + + // + + window.addEventListener('resize', onWindowResize); +} + +/* + function displayPalette( palette ) { + + const canvas = document.createElement( 'canvas' ); + canvas.width = 8; + canvas.height = 32; + canvas.style.position = 'absolute'; + canvas.style.top = '0'; + canvas.style.width = '100px'; + canvas.style.imageRendering = 'pixelated'; + document.body.appendChild( canvas ); + + const context = canvas.getContext( '2d' ); + + for ( let c = 0; c < 256; c ++ ) { + + const x = c % 8; + const y = Math.floor( c / 8 ); + + const hex = palette[ c + 1 ]; + const r = hex >> 0 & 0xff; + const g = hex >> 8 & 0xff; + const b = hex >> 16 & 0xff; + context.fillStyle = `rgba(${r},${g},${b},1)`; + context.fillRect( x, 31 - y, 1, 1 ); + + } + + } + */ + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + controls.update(); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_loader_vrml.ts b/examples-testing/examples/webgl_loader_vrml.ts new file mode 100644 index 000000000..fecf4bb45 --- /dev/null +++ b/examples-testing/examples/webgl_loader_vrml.ts @@ -0,0 +1,118 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { VRMLLoader } from 'three/addons/loaders/VRMLLoader.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, stats, controls, loader; + +const params = { + asset: 'house', +}; + +const assets = [ + 'creaseAngle', + 'crystal', + 'house', + 'elevationGrid1', + 'elevationGrid2', + 'extrusion1', + 'extrusion2', + 'extrusion3', + 'lines', + 'linesTransparent', + 'meshWithLines', + 'meshWithTexture', + 'pixelTexture', + 'points', +]; + +let vrmlScene; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1e10); + camera.position.set(-10, 5, 10); + + scene = new THREE.Scene(); + scene.add(camera); + + // light + + const ambientLight = new THREE.AmbientLight(0xffffff, 1.2); + scene.add(ambientLight); + + const dirLight = new THREE.DirectionalLight(0xffffff, 2.0); + dirLight.position.set(200, 200, 200); + scene.add(dirLight); + + loader = new VRMLLoader(); + loadAsset(params.asset); + + // renderer + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // controls + + controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 1; + controls.maxDistance = 200; + controls.enableDamping = true; + + // + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); + + // + + const gui = new GUI(); + gui.add(params, 'asset', assets).onChange(function (value) { + if (vrmlScene) { + vrmlScene.traverse(function (object) { + if (object.material) object.material.dispose(); + if (object.material && object.material.map) object.material.map.dispose(); + if (object.geometry) object.geometry.dispose(); + }); + + scene.remove(vrmlScene); + } + + loadAsset(value); + }); +} + +function loadAsset(asset) { + loader.load('models/vrml/' + asset + '.wrl', function (object) { + vrmlScene = object; + scene.add(object); + controls.reset(); + }); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + controls.update(); // to support damping + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_loader_vtk.ts b/examples-testing/examples/webgl_loader_vtk.ts new file mode 100644 index 000000000..dfc798657 --- /dev/null +++ b/examples-testing/examples/webgl_loader_vtk.ts @@ -0,0 +1,123 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; +import { VTKLoader } from 'three/addons/loaders/VTKLoader.js'; + +let stats; + +let camera, controls, scene, renderer; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 100); + camera.position.z = 0.2; + + scene = new THREE.Scene(); + + scene.add(camera); + + // light + + const hemiLight = new THREE.HemisphereLight(0xffffff, 0x000000, 3); + scene.add(hemiLight); + + const dirLight = new THREE.DirectionalLight(0xffffff, 1.5); + dirLight.position.set(2, 2, 2); + scene.add(dirLight); + + const loader = new VTKLoader(); + loader.load('models/vtk/bunny.vtk', function (geometry) { + geometry.center(); + geometry.computeVertexNormals(); + + const material = new THREE.MeshLambertMaterial({ color: 0xffffff }); + const mesh = new THREE.Mesh(geometry, material); + mesh.position.set(-0.075, 0.005, 0); + mesh.scale.multiplyScalar(0.2); + scene.add(mesh); + }); + + const loader1 = new VTKLoader(); + loader1.load('models/vtk/cube_ascii.vtp', function (geometry) { + geometry.computeVertexNormals(); + geometry.center(); + + const material = new THREE.MeshLambertMaterial({ color: 0x00ff00 }); + const mesh = new THREE.Mesh(geometry, material); + + mesh.position.set(-0.025, 0, 0); + mesh.scale.multiplyScalar(0.01); + + scene.add(mesh); + }); + + const loader2 = new VTKLoader(); + loader2.load('models/vtk/cube_binary.vtp', function (geometry) { + geometry.computeVertexNormals(); + geometry.center(); + + const material = new THREE.MeshLambertMaterial({ color: 0x0000ff }); + const mesh = new THREE.Mesh(geometry, material); + + mesh.position.set(0.025, 0, 0); + mesh.scale.multiplyScalar(0.01); + + scene.add(mesh); + }); + + const loader3 = new VTKLoader(); + loader3.load('models/vtk/cube_no_compression.vtp', function (geometry) { + geometry.computeVertexNormals(); + geometry.center(); + + const material = new THREE.MeshLambertMaterial({ color: 0xff0000 }); + const mesh = new THREE.Mesh(geometry, material); + + mesh.position.set(0.075, 0, 0); + mesh.scale.multiplyScalar(0.01); + + scene.add(mesh); + }); + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // controls + + controls = new TrackballControls(camera, renderer.domElement); + controls.minDistance = 0.1; + controls.maxDistance = 0.5; + controls.rotateSpeed = 5.0; + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + controls.handleResize(); +} + +function animate() { + controls.update(); + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_loader_xyz.ts b/examples-testing/examples/webgl_loader_xyz.ts new file mode 100644 index 000000000..90e009840 --- /dev/null +++ b/examples-testing/examples/webgl_loader_xyz.ts @@ -0,0 +1,62 @@ +import * as THREE from 'three'; + +import { XYZLoader } from 'three/addons/loaders/XYZLoader.js'; + +let camera, scene, renderer, clock; + +let points; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(10, 7, 10); + + scene = new THREE.Scene(); + scene.add(camera); + camera.lookAt(scene.position); + + clock = new THREE.Clock(); + + const loader = new XYZLoader(); + loader.load('models/xyz/helix_201.xyz', function (geometry) { + geometry.center(); + + const vertexColors = geometry.hasAttribute('color') === true; + + const material = new THREE.PointsMaterial({ size: 0.1, vertexColors: vertexColors }); + + points = new THREE.Points(geometry, material); + scene.add(points); + }); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const delta = clock.getDelta(); + + if (points) { + points.rotation.x += delta * 0.2; + points.rotation.y += delta * 0.5; + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_lod.ts b/examples-testing/examples/webgl_lod.ts new file mode 100644 index 000000000..0bb9e7be0 --- /dev/null +++ b/examples-testing/examples/webgl_lod.ts @@ -0,0 +1,88 @@ +import * as THREE from 'three'; + +import { FlyControls } from 'three/addons/controls/FlyControls.js'; + +let container; + +let camera, scene, renderer, controls; + +const clock = new THREE.Clock(); + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 15000); + camera.position.z = 1000; + + scene = new THREE.Scene(); + scene.fog = new THREE.Fog(0x000000, 1, 15000); + + const pointLight = new THREE.PointLight(0xff2200, 3, 0, 0); + pointLight.position.set(0, 0, 0); + scene.add(pointLight); + + const dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.position.set(0, 0, 1).normalize(); + scene.add(dirLight); + + const geometry = [ + [new THREE.IcosahedronGeometry(100, 16), 50], + [new THREE.IcosahedronGeometry(100, 8), 300], + [new THREE.IcosahedronGeometry(100, 4), 1000], + [new THREE.IcosahedronGeometry(100, 2), 2000], + [new THREE.IcosahedronGeometry(100, 1), 8000], + ]; + + const material = new THREE.MeshLambertMaterial({ color: 0xffffff, wireframe: true }); + + for (let j = 0; j < 1000; j++) { + const lod = new THREE.LOD(); + + for (let i = 0; i < geometry.length; i++) { + const mesh = new THREE.Mesh(geometry[i][0], material); + mesh.scale.set(1.5, 1.5, 1.5); + mesh.updateMatrix(); + mesh.matrixAutoUpdate = false; + lod.addLevel(mesh, geometry[i][1]); + } + + lod.position.x = 10000 * (0.5 - Math.random()); + lod.position.y = 7500 * (0.5 - Math.random()); + lod.position.z = 10000 * (0.5 - Math.random()); + lod.updateMatrix(); + lod.matrixAutoUpdate = false; + scene.add(lod); + } + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + controls = new FlyControls(camera, renderer.domElement); + controls.movementSpeed = 1000; + controls.rollSpeed = Math.PI / 10; + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + controls.update(clock.getDelta()); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_marchingcubes.ts b/examples-testing/examples/webgl_marchingcubes.ts new file mode 100644 index 000000000..d11df56a4 --- /dev/null +++ b/examples-testing/examples/webgl_marchingcubes.ts @@ -0,0 +1,311 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { MarchingCubes } from 'three/addons/objects/MarchingCubes.js'; +import { ToonShader1, ToonShader2, ToonShaderHatching, ToonShaderDotted } from 'three/addons/shaders/ToonShader.js'; + +let container, stats; + +let camera, scene, renderer; + +let materials, current_material; + +let light, pointLight, ambientLight; + +let effect, resolution; + +let effectController; + +let time = 0; + +const clock = new THREE.Clock(); + +init(); + +function init() { + container = document.getElementById('container'); + + // CAMERA + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.set(-500, 500, 1500); + + // SCENE + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x050505); + + // LIGHTS + + light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0.5, 0.5, 1); + scene.add(light); + + pointLight = new THREE.PointLight(0xff7c00, 3, 0, 0); + pointLight.position.set(0, 0, 100); + scene.add(pointLight); + + ambientLight = new THREE.AmbientLight(0x323232, 3); + scene.add(ambientLight); + + // MATERIALS + + materials = generateMaterials(); + current_material = 'shiny'; + + // MARCHING CUBES + + resolution = 28; + + effect = new MarchingCubes(resolution, materials[current_material], true, true, 100000); + effect.position.set(0, 0, 0); + effect.scale.set(700, 700, 700); + + effect.enableUvs = false; + effect.enableColors = false; + + scene.add(effect); + + // RENDERER + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // CONTROLS + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 500; + controls.maxDistance = 5000; + + // STATS + + stats = new Stats(); + container.appendChild(stats.dom); + + // GUI + + setupGui(); + + // EVENTS + + window.addEventListener('resize', onWindowResize); +} + +// + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function generateMaterials() { + // environment map + + const path = 'textures/cube/SwedishRoyalCastle/'; + const format = '.jpg'; + const urls = [ + path + 'px' + format, + path + 'nx' + format, + path + 'py' + format, + path + 'ny' + format, + path + 'pz' + format, + path + 'nz' + format, + ]; + + const cubeTextureLoader = new THREE.CubeTextureLoader(); + + const reflectionCube = cubeTextureLoader.load(urls); + const refractionCube = cubeTextureLoader.load(urls); + refractionCube.mapping = THREE.CubeRefractionMapping; + + // toons + + const toonMaterial1 = createShaderMaterial(ToonShader1, light, ambientLight); + const toonMaterial2 = createShaderMaterial(ToonShader2, light, ambientLight); + const hatchingMaterial = createShaderMaterial(ToonShaderHatching, light, ambientLight); + const dottedMaterial = createShaderMaterial(ToonShaderDotted, light, ambientLight); + + const texture = new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg'); + texture.wrapS = THREE.RepeatWrapping; + texture.wrapT = THREE.RepeatWrapping; + texture.colorSpace = THREE.SRGBColorSpace; + + const materials = { + shiny: new THREE.MeshStandardMaterial({ + color: 0x9c0000, + envMap: reflectionCube, + roughness: 0.1, + metalness: 1.0, + }), + chrome: new THREE.MeshLambertMaterial({ color: 0xffffff, envMap: reflectionCube }), + liquid: new THREE.MeshLambertMaterial({ color: 0xffffff, envMap: refractionCube, refractionRatio: 0.85 }), + matte: new THREE.MeshPhongMaterial({ specular: 0x494949, shininess: 1 }), + flat: new THREE.MeshLambertMaterial({ + /*TODO flatShading: true */ + }), + textured: new THREE.MeshPhongMaterial({ color: 0xffffff, specular: 0x111111, shininess: 1, map: texture }), + colors: new THREE.MeshPhongMaterial({ color: 0xffffff, specular: 0xffffff, shininess: 2, vertexColors: true }), + multiColors: new THREE.MeshPhongMaterial({ shininess: 2, vertexColors: true }), + plastic: new THREE.MeshPhongMaterial({ specular: 0xc1c1c1, shininess: 250 }), + toon1: toonMaterial1, + toon2: toonMaterial2, + hatching: hatchingMaterial, + dotted: dottedMaterial, + }; + + return materials; +} + +function createShaderMaterial(shader, light, ambientLight) { + const u = THREE.UniformsUtils.clone(shader.uniforms); + + const vs = shader.vertexShader; + const fs = shader.fragmentShader; + + const material = new THREE.ShaderMaterial({ uniforms: u, vertexShader: vs, fragmentShader: fs }); + + material.uniforms['uDirLightPos'].value = light.position; + material.uniforms['uDirLightColor'].value = light.color; + + material.uniforms['uAmbientLightColor'].value = ambientLight.color; + + return material; +} + +// + +function setupGui() { + const createHandler = function (id) { + return function () { + current_material = id; + + effect.material = materials[id]; + effect.enableUvs = current_material === 'textured' ? true : false; + effect.enableColors = current_material === 'colors' || current_material === 'multiColors' ? true : false; + }; + }; + + effectController = { + material: 'shiny', + + speed: 1.0, + numBlobs: 10, + resolution: 28, + isolation: 80, + + floor: true, + wallx: false, + wallz: false, + + dummy: function () {}, + }; + + let h; + + const gui = new GUI(); + + // material (type) + + h = gui.addFolder('Materials'); + + for (const m in materials) { + effectController[m] = createHandler(m); + h.add(effectController, m).name(m); + } + + // simulation + + h = gui.addFolder('Simulation'); + + h.add(effectController, 'speed', 0.1, 8.0, 0.05); + h.add(effectController, 'numBlobs', 1, 50, 1); + h.add(effectController, 'resolution', 14, 100, 1); + h.add(effectController, 'isolation', 10, 300, 1); + + h.add(effectController, 'floor'); + h.add(effectController, 'wallx'); + h.add(effectController, 'wallz'); +} + +// this controls content of marching cubes voxel field + +function updateCubes(object, time, numblobs, floor, wallx, wallz) { + object.reset(); + + // fill the field with some metaballs + + const rainbow = [ + new THREE.Color(0xff0000), + new THREE.Color(0xffbb00), + new THREE.Color(0xffff00), + new THREE.Color(0x00ff00), + new THREE.Color(0x0000ff), + new THREE.Color(0x9400bd), + new THREE.Color(0xc800eb), + ]; + const subtract = 12; + const strength = 1.2 / ((Math.sqrt(numblobs) - 1) / 4 + 1); + + for (let i = 0; i < numblobs; i++) { + const ballx = Math.sin(i + 1.26 * time * (1.03 + 0.5 * Math.cos(0.21 * i))) * 0.27 + 0.5; + const bally = Math.abs(Math.cos(i + 1.12 * time * Math.cos(1.22 + 0.1424 * i))) * 0.77; // dip into the floor + const ballz = Math.cos(i + 1.32 * time * 0.1 * Math.sin(0.92 + 0.53 * i)) * 0.27 + 0.5; + + if (current_material === 'multiColors') { + object.addBall(ballx, bally, ballz, strength, subtract, rainbow[i % 7]); + } else { + object.addBall(ballx, bally, ballz, strength, subtract); + } + } + + if (floor) object.addPlaneY(2, 12); + if (wallz) object.addPlaneZ(2, 12); + if (wallx) object.addPlaneX(2, 12); + + object.update(); +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + const delta = clock.getDelta(); + + time += delta * effectController.speed * 0.5; + + // marching cubes + + if (effectController.resolution !== resolution) { + resolution = effectController.resolution; + effect.init(Math.floor(resolution)); + } + + if (effectController.isolation !== effect.isolation) { + effect.isolation = effectController.isolation; + } + + updateCubes( + effect, + time, + effectController.numBlobs, + effectController.floor, + effectController.wallx, + effectController.wallz, + ); + + // render + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_alphahash.ts b/examples-testing/examples/webgl_materials_alphahash.ts new file mode 100644 index 000000000..1ecf95f26 --- /dev/null +++ b/examples-testing/examples/webgl_materials_alphahash.ts @@ -0,0 +1,178 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { TAARenderPass } from 'three/addons/postprocessing/TAARenderPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +let camera, scene, renderer, controls, stats, mesh, material; + +let composer, renderPass, taaRenderPass, outputPass; + +let needsUpdate = false; + +const amount = parseInt(window.location.search.slice(1)) || 3; +const count = Math.pow(amount, 3); + +const color = new THREE.Color(); + +const params = { + alpha: 0.5, + alphaHash: true, + taa: true, + sampleLevel: 2, +}; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(amount, amount, amount); + camera.lookAt(0, 0, 0); + + scene = new THREE.Scene(); + + const geometry = new THREE.IcosahedronGeometry(0.5, 3); + + material = new THREE.MeshStandardMaterial({ + color: 0xffffff, + alphaHash: params.alphaHash, + opacity: params.alpha, + }); + + mesh = new THREE.InstancedMesh(geometry, material, count); + + let i = 0; + const offset = (amount - 1) / 2; + + const matrix = new THREE.Matrix4(); + + for (let x = 0; x < amount; x++) { + for (let y = 0; y < amount; y++) { + for (let z = 0; z < amount; z++) { + matrix.setPosition(offset - x, offset - y, offset - z); + + mesh.setMatrixAt(i, matrix); + mesh.setColorAt(i, color.setHex(Math.random() * 0xffffff)); + + i++; + } + } + } + + scene.add(mesh); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + const environment = new RoomEnvironment(); + const pmremGenerator = new THREE.PMREMGenerator(renderer); + + scene.environment = pmremGenerator.fromScene(environment).texture; + environment.dispose(); + + // + + composer = new EffectComposer(renderer); + + renderPass = new RenderPass(scene, camera); + renderPass.enabled = false; + + taaRenderPass = new TAARenderPass(scene, camera); + + outputPass = new OutputPass(); + + composer.addPass(renderPass); + composer.addPass(taaRenderPass); + composer.addPass(outputPass); + + // + + controls = new OrbitControls(camera, renderer.domElement); + controls.enableZoom = false; + controls.enablePan = false; + + controls.addEventListener('change', () => (needsUpdate = true)); + + // + + const gui = new GUI(); + + gui.add(params, 'alpha', 0, 1).onChange(onMaterialUpdate); + gui.add(params, 'alphaHash').onChange(onMaterialUpdate); + + const taaFolder = gui.addFolder('Temporal Anti-Aliasing'); + + taaFolder + .add(params, 'taa') + .name('enabled') + .onChange(() => { + renderPass.enabled = !params.taa; + taaRenderPass.enabled = params.taa; + + sampleLevelCtrl.enable(params.taa); + + needsUpdate = true; + }); + + const sampleLevelCtrl = taaFolder.add(params, 'sampleLevel', 0, 6, 1).onChange(() => (needsUpdate = true)); + + // + + stats = new Stats(); + document.body.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + composer.setSize(window.innerWidth, window.innerHeight); + + needsUpdate = true; +} + +function onMaterialUpdate() { + material.opacity = params.alpha; + material.alphaHash = params.alphaHash; + material.transparent = !params.alphaHash; + material.depthWrite = params.alphaHash; + + material.needsUpdate = true; + needsUpdate = true; +} + +function animate() { + render(); + + stats.update(); +} + +function render() { + if (needsUpdate) { + taaRenderPass.accumulate = false; + taaRenderPass.sampleLevel = 0; + + needsUpdate = false; + } else { + taaRenderPass.accumulate = true; + taaRenderPass.sampleLevel = params.sampleLevel; + } + + composer.render(); +} diff --git a/examples-testing/examples/webgl_materials_blending.ts b/examples-testing/examples/webgl_materials_blending.ts new file mode 100644 index 000000000..11cc009bc --- /dev/null +++ b/examples-testing/examples/webgl_materials_blending.ts @@ -0,0 +1,147 @@ +import * as THREE from 'three'; + +let camera, scene, renderer; +let mapBg; + +const textureLoader = new THREE.TextureLoader(); + +init(); + +function init() { + // CAMERA + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 600; + + // SCENE + + scene = new THREE.Scene(); + + // BACKGROUND + + const canvas = document.createElement('canvas'); + const ctx = canvas.getContext('2d'); + canvas.width = canvas.height = 128; + ctx.fillStyle = '#ddd'; + ctx.fillRect(0, 0, 128, 128); + ctx.fillStyle = '#555'; + ctx.fillRect(0, 0, 64, 64); + ctx.fillStyle = '#999'; + ctx.fillRect(32, 32, 32, 32); + ctx.fillStyle = '#555'; + ctx.fillRect(64, 64, 64, 64); + ctx.fillStyle = '#777'; + ctx.fillRect(96, 96, 32, 32); + + mapBg = new THREE.CanvasTexture(canvas); + mapBg.colorSpace = THREE.SRGBColorSpace; + mapBg.wrapS = mapBg.wrapT = THREE.RepeatWrapping; + mapBg.repeat.set(64, 32); + + scene.background = mapBg; + + // OBJECTS + + const blendings = [ + { name: 'No', constant: THREE.NoBlending }, + { name: 'Normal', constant: THREE.NormalBlending }, + { name: 'Additive', constant: THREE.AdditiveBlending }, + { name: 'Subtractive', constant: THREE.SubtractiveBlending }, + { name: 'Multiply', constant: THREE.MultiplyBlending }, + ]; + + const assignSRGB = texture => { + texture.colorSpace = THREE.SRGBColorSpace; + }; + + const map0 = textureLoader.load('textures/uv_grid_opengl.jpg', assignSRGB); + const map1 = textureLoader.load('textures/sprite0.jpg', assignSRGB); + const map2 = textureLoader.load('textures/sprite0.png', assignSRGB); + const map3 = textureLoader.load('textures/lensflare/lensflare0.png', assignSRGB); + const map4 = textureLoader.load('textures/lensflare/lensflare0_alpha.png', assignSRGB); + + const geo1 = new THREE.PlaneGeometry(100, 100); + const geo2 = new THREE.PlaneGeometry(100, 25); + + addImageRow(map0, 300); + addImageRow(map1, 150); + addImageRow(map2, 0); + addImageRow(map3, -150); + addImageRow(map4, -300); + + function addImageRow(map, y) { + for (let i = 0; i < blendings.length; i++) { + const blending = blendings[i]; + + const material = new THREE.MeshBasicMaterial({ map: map }); + material.transparent = true; + material.blending = blending.constant; + + const x = (i - blendings.length / 2) * 110; + const z = 0; + + let mesh = new THREE.Mesh(geo1, material); + mesh.position.set(x, y, z); + scene.add(mesh); + + mesh = new THREE.Mesh(geo2, generateLabelMaterial(blending.name)); + mesh.position.set(x, y - 75, z); + scene.add(mesh); + } + } + + // RENDERER + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // EVENTS + + window.addEventListener('resize', onWindowResize); +} + +// + +function onWindowResize() { + const SCREEN_WIDTH = window.innerWidth; + const SCREEN_HEIGHT = window.innerHeight; + + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + + camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT; + camera.updateProjectionMatrix(); +} + +function generateLabelMaterial(text) { + const canvas = document.createElement('canvas'); + const ctx = canvas.getContext('2d'); + canvas.width = 128; + canvas.height = 32; + + ctx.fillStyle = 'rgba( 0, 0, 0, 0.95 )'; + ctx.fillRect(0, 0, 128, 32); + + ctx.fillStyle = 'white'; + ctx.font = 'bold 12pt arial'; + ctx.fillText(text, 10, 22); + + const map = new THREE.CanvasTexture(canvas); + map.colorSpace = THREE.SRGBColorSpace; + + const material = new THREE.MeshBasicMaterial({ map: map, transparent: true }); + + return material; +} + +function animate() { + const time = Date.now() * 0.00025; + const ox = (time * -0.01 * mapBg.repeat.x) % 1; + const oy = (time * -0.01 * mapBg.repeat.y) % 1; + + mapBg.offset.set(ox, oy); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_blending_custom.ts b/examples-testing/examples/webgl_materials_blending_custom.ts new file mode 100644 index 000000000..072447426 --- /dev/null +++ b/examples-testing/examples/webgl_materials_blending_custom.ts @@ -0,0 +1,214 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer; + +let mapBg; +const materials = []; + +const params = { + blendEquation: THREE.AddEquation, +}; + +const equations = { + Add: THREE.AddEquation, + Subtract: THREE.SubtractEquation, + ReverseSubtract: THREE.ReverseSubtractEquation, + Min: THREE.MinEquation, + Max: THREE.MaxEquation, +}; + +init(); + +function init() { + // CAMERA + + camera = new THREE.PerspectiveCamera(80, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 700; + + // SCENE + + scene = new THREE.Scene(); + + // BACKGROUND + + const canvas = document.createElement('canvas'); + const ctx = canvas.getContext('2d'); + canvas.width = canvas.height = 128; + ctx.fillStyle = '#ddd'; + ctx.fillRect(0, 0, 128, 128); + ctx.fillStyle = '#555'; + ctx.fillRect(0, 0, 64, 64); + ctx.fillStyle = '#999'; + ctx.fillRect(32, 32, 32, 32); + ctx.fillStyle = '#555'; + ctx.fillRect(64, 64, 64, 64); + ctx.fillStyle = '#777'; + ctx.fillRect(96, 96, 32, 32); + + mapBg = new THREE.CanvasTexture(canvas); + mapBg.colorSpace = THREE.SRGBColorSpace; + mapBg.wrapS = mapBg.wrapT = THREE.RepeatWrapping; + mapBg.repeat.set(64, 32); + + scene.background = mapBg; + + // FOREGROUND OBJECTS + + const src = [ + { name: 'Zero', constant: THREE.ZeroFactor }, + { name: 'One', constant: THREE.OneFactor }, + { name: 'SrcColor', constant: THREE.SrcColorFactor }, + { name: 'OneMinusSrcColor', constant: THREE.OneMinusSrcColorFactor }, + { name: 'SrcAlpha', constant: THREE.SrcAlphaFactor }, + { name: 'OneMinusSrcAlpha', constant: THREE.OneMinusSrcAlphaFactor }, + { name: 'DstAlpha', constant: THREE.DstAlphaFactor }, + { name: 'OneMinusDstAlpha', constant: THREE.OneMinusDstAlphaFactor }, + { name: 'DstColor', constant: THREE.DstColorFactor }, + { name: 'OneMinusDstColor', constant: THREE.OneMinusDstColorFactor }, + { name: 'SrcAlphaSaturate', constant: THREE.SrcAlphaSaturateFactor }, + ]; + + const dst = [ + { name: 'Zero', constant: THREE.ZeroFactor }, + { name: 'One', constant: THREE.OneFactor }, + { name: 'SrcColor', constant: THREE.SrcColorFactor }, + { name: 'OneMinusSrcColor', constant: THREE.OneMinusSrcColorFactor }, + { name: 'SrcAlpha', constant: THREE.SrcAlphaFactor }, + { name: 'OneMinusSrcAlpha', constant: THREE.OneMinusSrcAlphaFactor }, + { name: 'DstAlpha', constant: THREE.DstAlphaFactor }, + { name: 'OneMinusDstAlpha', constant: THREE.OneMinusDstAlphaFactor }, + { name: 'DstColor', constant: THREE.DstColorFactor }, + { name: 'OneMinusDstColor', constant: THREE.OneMinusDstColorFactor }, + ]; + + const geo1 = new THREE.PlaneGeometry(100, 100); + const geo2 = new THREE.PlaneGeometry(100, 25); + + const texture = new THREE.TextureLoader().load('textures/lensflare/lensflare0_alpha.png'); + texture.colorSpace = THREE.SRGBColorSpace; + + for (let i = 0; i < dst.length; i++) { + const blendDst = dst[i]; + + for (let j = 0; j < src.length; j++) { + const blendSrc = src[j]; + + const material = new THREE.MeshBasicMaterial({ map: texture }); + material.transparent = true; + + material.blending = THREE.CustomBlending; + material.blendSrc = blendSrc.constant; + material.blendDst = blendDst.constant; + material.blendEquation = THREE.AddEquation; + + const x = (j - src.length / 2) * 110; + const z = 0; + const y = (i - dst.length / 2) * 110 + 50; + + const mesh = new THREE.Mesh(geo1, material); + mesh.position.set(x, -y, z); + mesh.matrixAutoUpdate = false; + mesh.updateMatrix(); + scene.add(mesh); + + materials.push(material); + } + } + + for (let j = 0; j < src.length; j++) { + const blendSrc = src[j]; + + const x = (j - src.length / 2) * 110; + const z = 0; + const y = (0 - dst.length / 2) * 110 + 50; + + const mesh = new THREE.Mesh(geo2, generateLabelMaterial(blendSrc.name, 'rgba( 0, 150, 0, 1 )')); + mesh.position.set(x, -(y - 70), z); + mesh.matrixAutoUpdate = false; + mesh.updateMatrix(); + scene.add(mesh); + } + + for (let i = 0; i < dst.length; i++) { + const blendDst = dst[i]; + + const x = (0 - src.length / 2) * 110 - 125; + const z = 0; + const y = (i - dst.length / 2) * 110 + 165; + + const mesh = new THREE.Mesh(geo2, generateLabelMaterial(blendDst.name, 'rgba( 150, 0, 0, 1 )')); + mesh.position.set(x, -(y - 120), z); + mesh.matrixAutoUpdate = false; + mesh.updateMatrix(); + scene.add(mesh); + } + + // RENDERER + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // EVENTS + + window.addEventListener('resize', onWindowResize); + + // GUI + + // + const gui = new GUI({ width: 300 }); + + gui.add(params, 'blendEquation', equations).onChange(updateBlendEquation); + gui.open(); +} + +// + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); +} + +// + +function generateLabelMaterial(text, bg) { + const canvas = document.createElement('canvas'); + const ctx = canvas.getContext('2d'); + canvas.width = 128; + canvas.height = 32; + + ctx.fillStyle = bg; + ctx.fillRect(0, 0, 128, 32); + + ctx.fillStyle = 'white'; + ctx.font = 'bold 11pt arial'; + ctx.fillText(text, 8, 22); + + const map = new THREE.CanvasTexture(canvas); + map.colorSpace = THREE.SRGBColorSpace; + + const material = new THREE.MeshBasicMaterial({ map: map, transparent: true }); + return material; +} + +function updateBlendEquation(value) { + for (const material of materials) { + material.blendEquation = value; + } +} + +function animate() { + const time = Date.now() * 0.00025; + const ox = (time * -0.01 * mapBg.repeat.x) % 1; + const oy = (time * -0.01 * mapBg.repeat.y) % 1; + + mapBg.offset.set(ox, oy); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_bumpmap.ts b/examples-testing/examples/webgl_materials_bumpmap.ts new file mode 100644 index 000000000..d954fab7e --- /dev/null +++ b/examples-testing/examples/webgl_materials_bumpmap.ts @@ -0,0 +1,140 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +let container, stats, loader; + +let camera, scene, renderer; + +let mesh; + +let spotLight; + +let mouseX = 0; +let mouseY = 0; + +let targetX = 0; +let targetY = 0; + +const windowHalfX = window.innerWidth / 2; +const windowHalfY = window.innerHeight / 2; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + // + + camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.z = 12; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x060708); + + // LIGHTS + + scene.add(new THREE.HemisphereLight(0x8d7c7c, 0x494966, 3)); + + spotLight = new THREE.SpotLight(0xffffde, 200); + spotLight.position.set(3.5, 0, 7); + scene.add(spotLight); + + spotLight.castShadow = true; + + spotLight.shadow.mapSize.width = 2048; + spotLight.shadow.mapSize.height = 2048; + + spotLight.shadow.camera.near = 2; + spotLight.shadow.camera.far = 15; + + spotLight.shadow.camera.fov = 40; + + spotLight.shadow.bias = -0.005; + + // + + const mapHeight = new THREE.TextureLoader().load( + 'models/gltf/LeePerrySmith/Infinite-Level_02_Disp_NoSmoothUV-4096.jpg', + ); + + const material = new THREE.MeshPhongMaterial({ + color: 0x9c6e49, + specular: 0x666666, + shininess: 25, + bumpMap: mapHeight, + bumpScale: 10, + }); + + loader = new GLTFLoader(); + loader.load('models/gltf/LeePerrySmith/LeePerrySmith.glb', function (gltf) { + createScene(gltf.scene.children[0].geometry, 1, material); + }); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + renderer.shadowMap.enabled = true; + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // EVENTS + + document.addEventListener('mousemove', onDocumentMouseMove); + window.addEventListener('resize', onWindowResize); +} + +function createScene(geometry, scale, material) { + mesh = new THREE.Mesh(geometry, material); + + mesh.position.y = -0.5; + mesh.scale.set(scale, scale, scale); + + mesh.castShadow = true; + mesh.receiveShadow = true; + + scene.add(mesh); +} + +// + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); +} + +function onDocumentMouseMove(event) { + mouseX = event.clientX - windowHalfX; + mouseY = event.clientY - windowHalfY; +} + +// + +function animate() { + render(); + + stats.update(); +} + +function render() { + targetX = mouseX * 0.001; + targetY = mouseY * 0.001; + + if (mesh) { + mesh.rotation.y += 0.05 * (targetX - mesh.rotation.y); + mesh.rotation.x += 0.05 * (targetY - mesh.rotation.x); + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_car.ts b/examples-testing/examples/webgl_materials_car.ts new file mode 100644 index 000000000..e810f7b7d --- /dev/null +++ b/examples-testing/examples/webgl_materials_car.ts @@ -0,0 +1,167 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +let camera, scene, renderer; +let stats; + +let grid; +let controls; + +const wheels = []; + +function init() { + const container = document.getElementById('container'); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 0.85; + container.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(4.25, 1.4, -4.5); + + controls = new OrbitControls(camera, container); + controls.maxDistance = 9; + controls.maxPolarAngle = THREE.MathUtils.degToRad(90); + controls.target.set(0, 0.5, 0); + controls.update(); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x333333); + scene.environment = new RGBELoader().load('textures/equirectangular/venice_sunset_1k.hdr'); + scene.environment.mapping = THREE.EquirectangularReflectionMapping; + scene.fog = new THREE.Fog(0x333333, 10, 15); + + grid = new THREE.GridHelper(20, 40, 0xffffff, 0xffffff); + grid.material.opacity = 0.2; + grid.material.depthWrite = false; + grid.material.transparent = true; + scene.add(grid); + + // materials + + const bodyMaterial = new THREE.MeshPhysicalMaterial({ + color: 0xff0000, + metalness: 1.0, + roughness: 0.5, + clearcoat: 1.0, + clearcoatRoughness: 0.03, + }); + + const detailsMaterial = new THREE.MeshStandardMaterial({ + color: 0xffffff, + metalness: 1.0, + roughness: 0.5, + }); + + const glassMaterial = new THREE.MeshPhysicalMaterial({ + color: 0xffffff, + metalness: 0.25, + roughness: 0, + transmission: 1.0, + }); + + const bodyColorInput = document.getElementById('body-color'); + bodyColorInput.addEventListener('input', function () { + bodyMaterial.color.set(this.value); + }); + + const detailsColorInput = document.getElementById('details-color'); + detailsColorInput.addEventListener('input', function () { + detailsMaterial.color.set(this.value); + }); + + const glassColorInput = document.getElementById('glass-color'); + glassColorInput.addEventListener('input', function () { + glassMaterial.color.set(this.value); + }); + + // Car + + const shadow = new THREE.TextureLoader().load('models/gltf/ferrari_ao.png'); + + const dracoLoader = new DRACOLoader(); + dracoLoader.setDecoderPath('jsm/libs/draco/gltf/'); + + const loader = new GLTFLoader(); + loader.setDRACOLoader(dracoLoader); + + loader.load('models/gltf/ferrari.glb', function (gltf) { + const carModel = gltf.scene.children[0]; + + carModel.getObjectByName('body').material = bodyMaterial; + + carModel.getObjectByName('rim_fl').material = detailsMaterial; + carModel.getObjectByName('rim_fr').material = detailsMaterial; + carModel.getObjectByName('rim_rr').material = detailsMaterial; + carModel.getObjectByName('rim_rl').material = detailsMaterial; + carModel.getObjectByName('trim').material = detailsMaterial; + + carModel.getObjectByName('glass').material = glassMaterial; + + wheels.push( + carModel.getObjectByName('wheel_fl'), + carModel.getObjectByName('wheel_fr'), + carModel.getObjectByName('wheel_rl'), + carModel.getObjectByName('wheel_rr'), + ); + + // shadow + const mesh = new THREE.Mesh( + new THREE.PlaneGeometry(0.655 * 4, 1.3 * 4), + new THREE.MeshBasicMaterial({ + map: shadow, + blending: THREE.MultiplyBlending, + toneMapped: false, + transparent: true, + }), + ); + mesh.rotation.x = -Math.PI / 2; + mesh.renderOrder = 2; + carModel.add(mesh); + + scene.add(carModel); + }); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + controls.update(); + + const time = -performance.now() / 1000; + + for (let i = 0; i < wheels.length; i++) { + wheels[i].rotation.x = time * Math.PI * 2; + } + + grid.position.z = -time % 1; + + renderer.render(scene, camera); + + stats.update(); +} + +init(); diff --git a/examples-testing/examples/webgl_materials_cubemap.ts b/examples-testing/examples/webgl_materials_cubemap.ts new file mode 100644 index 000000000..5f2692751 --- /dev/null +++ b/examples-testing/examples/webgl_materials_cubemap.ts @@ -0,0 +1,115 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; + +let container, stats; + +let camera, scene, renderer; + +let pointLight; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.z = 13; + + //cubemap + const path = 'textures/cube/SwedishRoyalCastle/'; + const format = '.jpg'; + const urls = [ + path + 'px' + format, + path + 'nx' + format, + path + 'py' + format, + path + 'ny' + format, + path + 'pz' + format, + path + 'nz' + format, + ]; + + const reflectionCube = new THREE.CubeTextureLoader().load(urls); + const refractionCube = new THREE.CubeTextureLoader().load(urls); + refractionCube.mapping = THREE.CubeRefractionMapping; + + scene = new THREE.Scene(); + scene.background = reflectionCube; + + //lights + const ambient = new THREE.AmbientLight(0xffffff, 3); + scene.add(ambient); + + pointLight = new THREE.PointLight(0xffffff, 200); + scene.add(pointLight); + + //materials + const cubeMaterial3 = new THREE.MeshLambertMaterial({ + color: 0xffaa00, + envMap: reflectionCube, + combine: THREE.MixOperation, + reflectivity: 0.3, + }); + const cubeMaterial2 = new THREE.MeshLambertMaterial({ + color: 0xfff700, + envMap: refractionCube, + refractionRatio: 0.95, + }); + const cubeMaterial1 = new THREE.MeshLambertMaterial({ color: 0xffffff, envMap: reflectionCube }); + + //models + const objLoader = new OBJLoader(); + + objLoader.setPath('models/obj/walt/'); + objLoader.load('WaltHead.obj', function (object) { + const head = object.children[0]; + head.scale.setScalar(0.1); + head.position.y = -3; + head.material = cubeMaterial1; + + const head2 = head.clone(); + head2.position.x = -6; + head2.material = cubeMaterial2; + + const head3 = head.clone(); + head3.position.x = 6; + head3.material = cubeMaterial3; + + scene.add(head, head2, head3); + }); + + //renderer + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + //controls + const controls = new OrbitControls(camera, renderer.domElement); + controls.enableZoom = false; + controls.enablePan = false; + controls.minPolarAngle = Math.PI / 4; + controls.maxPolarAngle = Math.PI / 1.5; + + //stats + stats = new Stats(); + container.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); + stats.update(); +} diff --git a/examples-testing/examples/webgl_materials_cubemap_dynamic.ts b/examples-testing/examples/webgl_materials_cubemap_dynamic.ts new file mode 100644 index 000000000..13a268901 --- /dev/null +++ b/examples-testing/examples/webgl_materials_cubemap_dynamic.ts @@ -0,0 +1,115 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import Stats from 'three/addons/libs/stats.module.js'; + +let camera, scene, renderer, stats; +let cube, sphere, torus, material; + +let cubeCamera, cubeRenderTarget; + +let controls; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + document.body.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResized); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 75; + + scene = new THREE.Scene(); + scene.rotation.y = 0.5; // avoid flying objects occluding the sun + + new RGBELoader().setPath('textures/equirectangular/').load('quarry_01_1k.hdr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + scene.environment = texture; + }); + + // + + cubeRenderTarget = new THREE.WebGLCubeRenderTarget(256); + cubeRenderTarget.texture.type = THREE.HalfFloatType; + + cubeCamera = new THREE.CubeCamera(1, 1000, cubeRenderTarget); + + // + + material = new THREE.MeshStandardMaterial({ + envMap: cubeRenderTarget.texture, + roughness: 0.05, + metalness: 1, + }); + + const gui = new GUI(); + gui.add(material, 'roughness', 0, 1); + gui.add(material, 'metalness', 0, 1); + gui.add(renderer, 'toneMappingExposure', 0, 2).name('exposure'); + + sphere = new THREE.Mesh(new THREE.IcosahedronGeometry(15, 8), material); + scene.add(sphere); + + const material2 = new THREE.MeshStandardMaterial({ + roughness: 0.1, + metalness: 0, + }); + + cube = new THREE.Mesh(new THREE.BoxGeometry(15, 15, 15), material2); + scene.add(cube); + + torus = new THREE.Mesh(new THREE.TorusKnotGeometry(8, 3, 128, 16), material2); + scene.add(torus); + + // + + controls = new OrbitControls(camera, renderer.domElement); + controls.autoRotate = true; +} + +function onWindowResized() { + renderer.setSize(window.innerWidth, window.innerHeight); + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); +} + +function animate(msTime) { + const time = msTime / 1000; + + cube.position.x = Math.cos(time) * 30; + cube.position.y = Math.sin(time) * 30; + cube.position.z = Math.sin(time) * 30; + + cube.rotation.x += 0.02; + cube.rotation.y += 0.03; + + torus.position.x = Math.cos(time + 10) * 30; + torus.position.y = Math.sin(time + 10) * 30; + torus.position.z = Math.sin(time + 10) * 30; + + torus.rotation.x += 0.02; + torus.rotation.y += 0.03; + + cubeCamera.update(renderer, scene); + + controls.update(); + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_materials_cubemap_mipmaps.ts b/examples-testing/examples/webgl_materials_cubemap_mipmaps.ts new file mode 100644 index 000000000..944f4c18e --- /dev/null +++ b/examples-testing/examples/webgl_materials_cubemap_mipmaps.ts @@ -0,0 +1,119 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let container; + +let camera, scene, renderer; + +init(); + +//load customized cube texture +async function loadCubeTextureWithMipmaps() { + const path = 'textures/cube/angus/'; + const format = '.jpg'; + const mipmaps = []; + const maxLevel = 8; + + async function loadCubeTexture(urls) { + return new Promise(function (resolve) { + new THREE.CubeTextureLoader().load(urls, function (cubeTexture) { + resolve(cubeTexture); + }); + }); + } + + // load mipmaps + const pendings = []; + + for (let level = 0; level <= maxLevel; ++level) { + const urls = []; + + for (let face = 0; face < 6; ++face) { + urls.push(path + 'cube_m0' + level + '_c0' + face + format); + } + + const mipmapLevel = level; + + pendings.push( + loadCubeTexture(urls).then(function (cubeTexture) { + mipmaps[mipmapLevel] = cubeTexture; + }), + ); + } + + await Promise.all(pendings); + + const customizedCubeTexture = mipmaps.shift(); + customizedCubeTexture.mipmaps = mipmaps; + customizedCubeTexture.colorSpace = THREE.SRGBColorSpace; + customizedCubeTexture.minFilter = THREE.LinearMipMapLinearFilter; + customizedCubeTexture.magFilter = THREE.LinearFilter; + customizedCubeTexture.generateMipmaps = false; + customizedCubeTexture.needsUpdate = true; + + return customizedCubeTexture; +} + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 500; + + scene = new THREE.Scene(); + + loadCubeTextureWithMipmaps().then(function (cubeTexture) { + //model + const sphere = new THREE.SphereGeometry(100, 128, 128); + + //manual mipmaps + let material = new THREE.MeshBasicMaterial({ color: 0xffffff, envMap: cubeTexture }); + material.name = 'manual mipmaps'; + + let mesh = new THREE.Mesh(sphere, material); + mesh.position.set(100, 0, 0); + scene.add(mesh); + + //webgl mipmaps + material = material.clone(); + material.name = 'auto mipmaps'; + + const autoCubeTexture = cubeTexture.clone(); + autoCubeTexture.mipmaps = []; + autoCubeTexture.generateMipmaps = true; + autoCubeTexture.needsUpdate = true; + + material.envMap = autoCubeTexture; + + mesh = new THREE.Mesh(sphere, material); + mesh.position.set(-100, 0, 0); + scene.add(mesh); + }); + + //renderer + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + //controls + const controls = new OrbitControls(camera, renderer.domElement); + controls.minPolarAngle = Math.PI / 4; + controls.maxPolarAngle = Math.PI / 1.5; + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_cubemap_refraction.ts b/examples-testing/examples/webgl_materials_cubemap_refraction.ts new file mode 100644 index 000000000..8c025071f --- /dev/null +++ b/examples-testing/examples/webgl_materials_cubemap_refraction.ts @@ -0,0 +1,126 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { PLYLoader } from 'three/addons/loaders/PLYLoader.js'; + +let container, stats; + +let camera, scene, renderer; + +let mouseX = 0, + mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 100000); + camera.position.z = -4000; + + // + + const r = 'textures/cube/Park3Med/'; + + const urls = [r + 'px.jpg', r + 'nx.jpg', r + 'py.jpg', r + 'ny.jpg', r + 'pz.jpg', r + 'nz.jpg']; + + const textureCube = new THREE.CubeTextureLoader().load(urls); + textureCube.mapping = THREE.CubeRefractionMapping; + + scene = new THREE.Scene(); + scene.background = textureCube; + + // LIGHTS + + const ambient = new THREE.AmbientLight(0xffffff, 3.5); + scene.add(ambient); + + // material samples + + const cubeMaterial3 = new THREE.MeshPhongMaterial({ + color: 0xccddff, + envMap: textureCube, + refractionRatio: 0.98, + reflectivity: 0.9, + }); + const cubeMaterial2 = new THREE.MeshPhongMaterial({ color: 0xccfffd, envMap: textureCube, refractionRatio: 0.985 }); + const cubeMaterial1 = new THREE.MeshPhongMaterial({ color: 0xffffff, envMap: textureCube, refractionRatio: 0.98 }); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + const loader = new PLYLoader(); + loader.load('models/ply/binary/Lucy100k.ply', function (geometry) { + createScene(geometry, cubeMaterial1, cubeMaterial2, cubeMaterial3); + }); + + document.addEventListener('mousemove', onDocumentMouseMove); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function createScene(geometry, m1, m2, m3) { + geometry.computeVertexNormals(); + + const s = 1.5; + + let mesh = new THREE.Mesh(geometry, m1); + mesh.scale.x = mesh.scale.y = mesh.scale.z = s; + scene.add(mesh); + + mesh = new THREE.Mesh(geometry, m2); + mesh.position.x = -1500; + mesh.scale.x = mesh.scale.y = mesh.scale.z = s; + scene.add(mesh); + + mesh = new THREE.Mesh(geometry, m3); + mesh.position.x = 1500; + mesh.scale.x = mesh.scale.y = mesh.scale.z = s; + scene.add(mesh); +} + +function onDocumentMouseMove(event) { + mouseX = (event.clientX - windowHalfX) * 4; + mouseY = (event.clientY - windowHalfY) * 4; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-mouseY - camera.position.y) * 0.05; + + camera.lookAt(scene.position); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_cubemap_render_to_mipmaps.ts b/examples-testing/examples/webgl_materials_cubemap_render_to_mipmaps.ts new file mode 100644 index 000000000..599a1369b --- /dev/null +++ b/examples-testing/examples/webgl_materials_cubemap_render_to_mipmaps.ts @@ -0,0 +1,183 @@ +import * as THREE from 'three'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let container; +let camera, scene, renderer; + +const CubemapFilterShader = { + name: 'CubemapFilterShader', + + uniforms: { + cubeTexture: { value: null }, + mipIndex: { value: 0 }, + }, + + vertexShader: /* glsl */ ` + + varying vec3 vWorldDirection; + + #include + + void main() { + vWorldDirection = transformDirection(position, modelMatrix); + #include + #include + gl_Position.z = gl_Position.w; // set z to camera.far + } + + `, + + fragmentShader: /* glsl */ ` + + uniform samplerCube cubeTexture; + varying vec3 vWorldDirection; + + uniform float mipIndex; + + #include + + void main() { + vec3 cubeCoordinates = normalize(vWorldDirection); + + // Colorize mip levels + vec4 color = vec4(1.0, 0.0, 0.0, 1.0); + if (mipIndex == 0.0) color.rgb = vec3(1.0, 1.0, 1.0); + else if (mipIndex == 1.0) color.rgb = vec3(0.0, 0.0, 1.0); + else if (mipIndex == 2.0) color.rgb = vec3(0.0, 1.0, 1.0); + else if (mipIndex == 3.0) color.rgb = vec3(0.0, 1.0, 0.0); + else if (mipIndex == 4.0) color.rgb = vec3(1.0, 1.0, 0.0); + + gl_FragColor = textureCube(cubeTexture, cubeCoordinates, 0.0) * color; + } + + `, +}; + +init(); + +async function loadCubeTexture(urls) { + return new Promise(function (resolve) { + new THREE.CubeTextureLoader().load(urls, function (cubeTexture) { + resolve(cubeTexture); + }); + }); +} + +function allocateCubemapRenderTarget(cubeMapSize) { + const params = { + magFilter: THREE.LinearFilter, + minFilter: THREE.LinearMipMapLinearFilter, + generateMipmaps: false, + type: THREE.HalfFloatType, + format: THREE.RGBAFormat, + colorSpace: THREE.LinearSRGBColorSpace, + depthBuffer: false, + }; + + const rt = new THREE.WebGLCubeRenderTarget(cubeMapSize, params); + + const mipLevels = Math.log(cubeMapSize) * Math.LOG2E + 1.0; + for (let i = 0; i < mipLevels; i++) rt.texture.mipmaps.push({}); + + rt.texture.mapping = THREE.CubeReflectionMapping; + return rt; +} + +function renderToCubeTexture(cubeMapRenderTarget, sourceCubeTexture) { + const geometry = new THREE.BoxGeometry(5, 5, 5); + + const material = new THREE.ShaderMaterial({ + name: CubemapFilterShader.name, + uniforms: THREE.UniformsUtils.clone(CubemapFilterShader.uniforms), + vertexShader: CubemapFilterShader.vertexShader, + fragmentShader: CubemapFilterShader.fragmentShader, + side: THREE.BackSide, + blending: THREE.NoBlending, + }); + + material.uniforms.cubeTexture.value = sourceCubeTexture; + + const mesh = new THREE.Mesh(geometry, material); + const cubeCamera = new THREE.CubeCamera(1, 10, cubeMapRenderTarget); + const mipmapCount = Math.floor(Math.log2(Math.max(cubeMapRenderTarget.width, cubeMapRenderTarget.height))); + + for (let mipmap = 0; mipmap < mipmapCount; mipmap++) { + material.uniforms.mipIndex.value = mipmap; + material.needsUpdate = true; + + cubeMapRenderTarget.viewport.set( + 0, + 0, + cubeMapRenderTarget.width >> mipmap, + cubeMapRenderTarget.height >> mipmap, + ); + + cubeCamera.activeMipmapLevel = mipmap; + cubeCamera.update(renderer, mesh); + } + + mesh.geometry.dispose(); + mesh.material.dispose(); +} + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + // Create renderer + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 500; + + // Create controls + const controls = new OrbitControls(camera, renderer.domElement); + controls.minPolarAngle = Math.PI / 4; + controls.maxPolarAngle = Math.PI / 1.5; + + window.addEventListener('resize', onWindowResize); + + // Load a cube texture + const r = 'textures/cube/Park3Med/'; + const urls = [r + 'px.jpg', r + 'nx.jpg', r + 'py.jpg', r + 'ny.jpg', r + 'pz.jpg', r + 'nz.jpg']; + + loadCubeTexture(urls).then(cubeTexture => { + // Allocate a cube map render target + const cubeMapRenderTarget = allocateCubemapRenderTarget(512); + + // Render to all the mip levels of cubeMapRenderTarget + renderToCubeTexture(cubeMapRenderTarget, cubeTexture); + + // Create geometry + const sphere = new THREE.SphereGeometry(100, 128, 128); + let material = new THREE.MeshBasicMaterial({ color: 0xffffff, envMap: cubeTexture }); + + let mesh = new THREE.Mesh(sphere, material); + mesh.position.set(-100, 0, 0); + scene.add(mesh); + + material = material.clone(); + material.envMap = cubeMapRenderTarget.texture; + + mesh = new THREE.Mesh(sphere, material); + mesh.position.set(100, 0, 0); + scene.add(mesh); + }); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_curvature.ts b/examples-testing/examples/webgl_materials_curvature.ts new file mode 100644 index 000000000..60992dbc6 --- /dev/null +++ b/examples-testing/examples/webgl_materials_curvature.ts @@ -0,0 +1,252 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; + +let camera, scene, renderer; + +let ninjaMeshRaw, curvatureAttribute, bufferGeo; + +init(); + +//returns average of elements in a dictionary +function average(dict) { + let sum = 0; + let length = 0; + + Object.keys(dict).forEach(function (key) { + sum += dict[key]; + length++; + }); + + return sum / length; +} + +//clamp a number between min and max +function clamp(number, min, max) { + return Math.max(min, Math.min(number, max)); +} + +//filter the curvature array to only show concave values +function filterConcave(curvature) { + for (let i = 0; i < curvature.length; i++) { + curvature[i] = Math.abs(clamp(curvature[i], -1, 0)); + } +} + +//filter the curvature array to only show convex values +function filterConvex(curvature) { + for (let i = 0; i < curvature.length; i++) { + curvature[i] = clamp(curvature[i], 0, 1); + } +} + +//filter the curvature array to show both the concave and convex values +function filterBoth(curvature) { + for (let i = 0; i < curvature.length; i++) { + curvature[i] = Math.abs(curvature[i]); + } +} + +//initialize the scene +function init() { + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); + + camera.position.x = -23; + camera.position.y = 2; + camera.position.z = 24; + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.autoClear = false; + document.body.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 20; + controls.maxDistance = 100; + + const loader = new OBJLoader(); + //load the obj + loader.load('models/obj/ninja/ninjaHead_Low.obj', function (object) { + object.traverse(function (child) { + if (child.isMesh) { + bufferGeo = child.geometry; + bufferGeo.center(); + const dict = {}; + + for (let i = 0; i < bufferGeo.attributes.position.count; i += 3) { + //create a dictionary of every position, and its neighboring positions + const array = bufferGeo.attributes.position.array; + const normArray = bufferGeo.attributes.normal.array; + + const posA = new THREE.Vector3(array[3 * i], array[3 * i + 1], array[3 * i + 2]); + const posB = new THREE.Vector3(array[3 * (i + 1)], array[3 * (i + 1) + 1], array[3 * (i + 1) + 2]); + const posC = new THREE.Vector3(array[3 * (i + 2)], array[3 * (i + 2) + 1], array[3 * (i + 2) + 2]); + + const normA = new THREE.Vector3( + normArray[3 * i], + normArray[3 * i + 1], + normArray[3 * i + 2], + ).normalize(); + const normB = new THREE.Vector3( + normArray[3 * (i + 1)], + normArray[3 * (i + 1) + 1], + normArray[3 * (i + 1) + 2], + ).normalize(); + const normC = new THREE.Vector3( + normArray[3 * (i + 2)], + normArray[3 * (i + 2) + 1], + normArray[3 * (i + 2) + 2], + ).normalize(); + + const strA = posA.toArray().toString(); + const strB = posB.toArray().toString(); + const strC = posC.toArray().toString(); + + const posB_A = new THREE.Vector3().subVectors(posB, posA); + const posB_C = new THREE.Vector3().subVectors(posB, posC); + const posC_A = new THREE.Vector3().subVectors(posC, posA); + + const b2a = normB.dot(posB_A.normalize()); + const b2c = normB.dot(posB_C.normalize()); + const c2a = normC.dot(posC_A.normalize()); + + const a2b = -normA.dot(posB_A.normalize()); + const c2b = -normC.dot(posB_C.normalize()); + const a2c = -normA.dot(posC_A.normalize()); + + if (dict[strA] === undefined) { + dict[strA] = {}; + } + + if (dict[strB] === undefined) { + dict[strB] = {}; + } + + if (dict[strC] === undefined) { + dict[strC] = {}; + } + + dict[strA][strB] = a2b; + dict[strA][strC] = a2c; + dict[strB][strA] = b2a; + dict[strB][strC] = b2c; + dict[strC][strA] = c2a; + dict[strC][strB] = c2b; + } + + let curvatureDict = {}; + let min = 10, + max = 0; + + Object.keys(dict).forEach(function (key) { + curvatureDict[key] = average(dict[key]); + }); + + //smoothing + const smoothCurvatureDict = Object.create(curvatureDict); + + Object.keys(dict).forEach(function (key) { + let count = 0; + let sum = 0; + Object.keys(dict[key]).forEach(function (key2) { + sum += smoothCurvatureDict[key2]; + count++; + }); + smoothCurvatureDict[key] = sum / count; + }); + + curvatureDict = smoothCurvatureDict; + + // fit values to 0 and 1 + Object.keys(curvatureDict).forEach(function (key) { + const val = Math.abs(curvatureDict[key]); + if (val < min) min = val; + if (val > max) max = val; + }); + + const range = max - min; + + Object.keys(curvatureDict).forEach(function (key) { + const val = Math.abs(curvatureDict[key]); + if (curvatureDict[key] < 0) { + curvatureDict[key] = (min - val) / range; + } else { + curvatureDict[key] = (val - min) / range; + } + }); + + curvatureAttribute = new Float32Array(bufferGeo.attributes.position.count); + + for (let i = 0; i < bufferGeo.attributes.position.count; i++) { + const array = bufferGeo.attributes.position.array; + const pos = new THREE.Vector3(array[3 * i], array[3 * i + 1], array[3 * i + 2]); + const str = pos.toArray().toString(); + curvatureAttribute[i] = curvatureDict[str]; + } + + bufferGeo.setAttribute('curvature', new THREE.BufferAttribute(curvatureAttribute, 1)); + + //starting filter is to show both concave and convex + const curvatureFiltered = new Float32Array(curvatureAttribute); + filterBoth(curvatureFiltered); + + const materialRaw = new THREE.ShaderMaterial({ + vertexShader: document.getElementById('vertexShaderRaw').textContent, + fragmentShader: document.getElementById('fragmentShaderRaw').textContent, + }); + + ninjaMeshRaw = new THREE.Mesh(bufferGeo, materialRaw); + } + }); + + scene.add(ninjaMeshRaw); + }); + + //init GUI + const params = { + filterConvex: function () { + const curvatureFiltered = new Float32Array(curvatureAttribute); + filterConvex(curvatureFiltered); + bufferGeo.attributes.curvature.array = curvatureFiltered; + bufferGeo.attributes.curvature.needsUpdate = true; + }, + filterConcave: function () { + const curvatureFiltered = new Float32Array(curvatureAttribute); + filterConcave(curvatureFiltered); + bufferGeo.attributes.curvature.array = curvatureFiltered; + bufferGeo.attributes.curvature.needsUpdate = true; + }, + filterBoth: function () { + const curvatureFiltered = new Float32Array(curvatureAttribute); + filterBoth(curvatureFiltered); + bufferGeo.attributes.curvature.array = curvatureFiltered; + bufferGeo.attributes.curvature.needsUpdate = true; + }, + }; + + const gui = new GUI({ title: 'Topology' }); + + gui.add(params, 'filterConvex'); + gui.add(params, 'filterConcave'); + gui.add(params, 'filterBoth'); + + onWindowResize(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); +} + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_displacementmap.ts b/examples-testing/examples/webgl_materials_displacementmap.ts new file mode 100644 index 000000000..fd0be9a5e --- /dev/null +++ b/examples-testing/examples/webgl_materials_displacementmap.ts @@ -0,0 +1,224 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; + +let stats; +let camera, scene, renderer, controls; + +const settings = { + metalness: 1.0, + roughness: 0.4, + ambientIntensity: 0.2, + aoMapIntensity: 1.0, + envMapIntensity: 1.0, + displacementScale: 2.436143, // from original model + normalScale: 1.0, +}; + +let mesh, material; + +let pointLight, ambientLight; + +const height = 500; // of camera frustum + +let r = 0.0; + +init(); +initGui(); + +// Init gui +function initGui() { + const gui = new GUI(); + //let gui = gui.addFolder( "Material" ); + gui.add(settings, 'metalness') + .min(0) + .max(1) + .onChange(function (value) { + material.metalness = value; + }); + + gui.add(settings, 'roughness') + .min(0) + .max(1) + .onChange(function (value) { + material.roughness = value; + }); + + gui.add(settings, 'aoMapIntensity') + .min(0) + .max(1) + .onChange(function (value) { + material.aoMapIntensity = value; + }); + + gui.add(settings, 'ambientIntensity') + .min(0) + .max(1) + .onChange(function (value) { + ambientLight.intensity = value; + }); + + gui.add(settings, 'envMapIntensity') + .min(0) + .max(3) + .onChange(function (value) { + material.envMapIntensity = value; + }); + + gui.add(settings, 'displacementScale') + .min(0) + .max(3.0) + .onChange(function (value) { + material.displacementScale = value; + }); + + gui.add(settings, 'normalScale') + .min(-1) + .max(1) + .onChange(function (value) { + material.normalScale.set(1, -1).multiplyScalar(value); + }); +} + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + scene = new THREE.Scene(); + + const aspect = window.innerWidth / window.innerHeight; + camera = new THREE.OrthographicCamera(-height * aspect, height * aspect, height, -height, 1, 10000); + camera.position.z = 1500; + scene.add(camera); + + controls = new OrbitControls(camera, renderer.domElement); + controls.enableZoom = false; + controls.enableDamping = true; + + // lights + + ambientLight = new THREE.AmbientLight(0xffffff, settings.ambientIntensity); + scene.add(ambientLight); + + pointLight = new THREE.PointLight(0xff0000, 1.5, 0, 0); + pointLight.position.z = 2500; + scene.add(pointLight); + + const pointLight2 = new THREE.PointLight(0xff6666, 3, 0, 0); + camera.add(pointLight2); + + const pointLight3 = new THREE.PointLight(0x0000ff, 1.5, 0, 0); + pointLight3.position.x = -1000; + pointLight3.position.z = 1000; + scene.add(pointLight3); + + // env map + + const path = 'textures/cube/SwedishRoyalCastle/'; + const format = '.jpg'; + const urls = [ + path + 'px' + format, + path + 'nx' + format, + path + 'py' + format, + path + 'ny' + format, + path + 'pz' + format, + path + 'nz' + format, + ]; + + const reflectionCube = new THREE.CubeTextureLoader().load(urls); + + // textures + + const textureLoader = new THREE.TextureLoader(); + const normalMap = textureLoader.load('models/obj/ninja/normal.png'); + const aoMap = textureLoader.load('models/obj/ninja/ao.jpg'); + const displacementMap = textureLoader.load('models/obj/ninja/displacement.jpg'); + + // material + + material = new THREE.MeshStandardMaterial({ + color: 0xc1c1c1, + roughness: settings.roughness, + metalness: settings.metalness, + + normalMap: normalMap, + normalScale: new THREE.Vector2(1, -1), // why does the normal map require negation in this case? + + aoMap: aoMap, + aoMapIntensity: 1, + + displacementMap: displacementMap, + displacementScale: settings.displacementScale, + displacementBias: -0.428408, // from original model + + envMap: reflectionCube, + envMapIntensity: settings.envMapIntensity, + + side: THREE.DoubleSide, + }); + + // + + const loader = new OBJLoader(); + loader.load('models/obj/ninja/ninjaHead_Low.obj', function (group) { + const geometry = group.children[0].geometry; + geometry.center(); + + mesh = new THREE.Mesh(geometry, material); + mesh.scale.multiplyScalar(25); + scene.add(mesh); + }); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const aspect = window.innerWidth / window.innerHeight; + + camera.left = -height * aspect; + camera.right = height * aspect; + camera.top = height; + camera.bottom = -height; + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + controls.update(); + + stats.begin(); + render(); + stats.end(); +} + +function render() { + pointLight.position.x = 2500 * Math.cos(r); + pointLight.position.z = 2500 * Math.sin(r); + + r += 0.01; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_envmaps.ts b/examples-testing/examples/webgl_materials_envmaps.ts new file mode 100644 index 000000000..18a5542ed --- /dev/null +++ b/examples-testing/examples/webgl_materials_envmaps.ts @@ -0,0 +1,131 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let controls, camera, scene, renderer; +let textureEquirec, textureCube; +let sphereMesh, sphereMaterial, params; + +init(); + +function init() { + // CAMERAS + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(0, 0, 2.5); + + // SCENE + + scene = new THREE.Scene(); + + // Textures + + const loader = new THREE.CubeTextureLoader(); + loader.setPath('textures/cube/Bridge2/'); + + textureCube = loader.load(['posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg']); + + const textureLoader = new THREE.TextureLoader(); + + textureEquirec = textureLoader.load('textures/2294472375_24a3b8ef46_o.jpg'); + textureEquirec.mapping = THREE.EquirectangularReflectionMapping; + textureEquirec.colorSpace = THREE.SRGBColorSpace; + + scene.background = textureCube; + + // + + const geometry = new THREE.IcosahedronGeometry(1, 15); + sphereMaterial = new THREE.MeshBasicMaterial({ envMap: textureCube }); + sphereMesh = new THREE.Mesh(geometry, sphereMaterial); + scene.add(sphereMesh); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 1.5; + controls.maxDistance = 6; + + // + + params = { + Cube: function () { + scene.background = textureCube; + + sphereMaterial.envMap = textureCube; + sphereMaterial.needsUpdate = true; + }, + Equirectangular: function () { + scene.background = textureEquirec; + + sphereMaterial.envMap = textureEquirec; + sphereMaterial.needsUpdate = true; + }, + Refraction: false, + backgroundRotationX: false, + backgroundRotationY: false, + backgroundRotationZ: false, + syncMaterial: false, + }; + + const gui = new GUI({ width: 300 }); + gui.add(params, 'Cube'); + gui.add(params, 'Equirectangular'); + gui.add(params, 'Refraction').onChange(function (value) { + if (value) { + textureEquirec.mapping = THREE.EquirectangularRefractionMapping; + textureCube.mapping = THREE.CubeRefractionMapping; + } else { + textureEquirec.mapping = THREE.EquirectangularReflectionMapping; + textureCube.mapping = THREE.CubeReflectionMapping; + } + + sphereMaterial.needsUpdate = true; + }); + gui.add(params, 'backgroundRotationX'); + gui.add(params, 'backgroundRotationY'); + gui.add(params, 'backgroundRotationZ'); + gui.add(params, 'syncMaterial'); + gui.open(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + if (params.backgroundRotationX) { + scene.backgroundRotation.x += 0.001; + } + + if (params.backgroundRotationY) { + scene.backgroundRotation.y += 0.001; + } + + if (params.backgroundRotationZ) { + scene.backgroundRotation.z += 0.001; + } + + if (params.syncMaterial) { + sphereMesh.material.envMapRotation.copy(scene.backgroundRotation); + } + + camera.lookAt(scene.position); + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_envmaps_exr.ts b/examples-testing/examples/webgl_materials_envmaps_exr.ts new file mode 100644 index 000000000..c3f3f4f7d --- /dev/null +++ b/examples-testing/examples/webgl_materials_envmaps_exr.ts @@ -0,0 +1,153 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { EXRLoader } from 'three/addons/loaders/EXRLoader.js'; + +const params = { + envMap: 'EXR', + roughness: 0.0, + metalness: 0.0, + exposure: 1.0, + debug: false, +}; + +let container, stats; +let camera, scene, renderer, controls; +let torusMesh, planeMesh; +let pngCubeRenderTarget, exrCubeRenderTarget; +let pngBackground, exrBackground; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 0, 120); + + scene = new THREE.Scene(); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + + container.appendChild(renderer.domElement); + + renderer.toneMapping = THREE.ACESFilmicToneMapping; + + // + + let geometry = new THREE.TorusKnotGeometry(18, 8, 150, 20); + let material = new THREE.MeshStandardMaterial({ + metalness: params.metalness, + roughness: params.roughness, + envMapIntensity: 1.0, + }); + + torusMesh = new THREE.Mesh(geometry, material); + scene.add(torusMesh); + + geometry = new THREE.PlaneGeometry(200, 200); + material = new THREE.MeshBasicMaterial(); + + planeMesh = new THREE.Mesh(geometry, material); + planeMesh.position.y = -50; + planeMesh.rotation.x = -Math.PI * 0.5; + scene.add(planeMesh); + + THREE.DefaultLoadingManager.onLoad = function () { + pmremGenerator.dispose(); + }; + + new EXRLoader().load('textures/piz_compressed.exr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + + exrCubeRenderTarget = pmremGenerator.fromEquirectangular(texture); + exrBackground = texture; + }); + + new THREE.TextureLoader().load('textures/equirectangular.png', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + texture.colorSpace = THREE.SRGBColorSpace; + + pngCubeRenderTarget = pmremGenerator.fromEquirectangular(texture); + pngBackground = texture; + }); + + const pmremGenerator = new THREE.PMREMGenerator(renderer); + pmremGenerator.compileEquirectangularShader(); + + stats = new Stats(); + container.appendChild(stats.dom); + + controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 50; + controls.maxDistance = 300; + + window.addEventListener('resize', onWindowResize); + + const gui = new GUI(); + + gui.add(params, 'envMap', ['EXR', 'PNG']); + gui.add(params, 'roughness', 0, 1, 0.01); + gui.add(params, 'metalness', 0, 1, 0.01); + gui.add(params, 'exposure', 0, 2, 0.01); + gui.add(params, 'debug'); + gui.open(); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); +} + +function animate() { + stats.begin(); + render(); + stats.end(); +} + +function render() { + torusMesh.material.roughness = params.roughness; + torusMesh.material.metalness = params.metalness; + + let newEnvMap = torusMesh.material.envMap; + let background = scene.background; + + switch (params.envMap) { + case 'EXR': + newEnvMap = exrCubeRenderTarget ? exrCubeRenderTarget.texture : null; + background = exrBackground; + break; + case 'PNG': + newEnvMap = pngCubeRenderTarget ? pngCubeRenderTarget.texture : null; + background = pngBackground; + break; + } + + if (newEnvMap !== torusMesh.material.envMap) { + torusMesh.material.envMap = newEnvMap; + torusMesh.material.needsUpdate = true; + + planeMesh.material.map = newEnvMap; + planeMesh.material.needsUpdate = true; + } + + torusMesh.rotation.y += 0.005; + planeMesh.visible = params.debug; + + scene.background = background; + renderer.toneMappingExposure = params.exposure; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_envmaps_groundprojected.ts b/examples-testing/examples/webgl_materials_envmaps_groundprojected.ts new file mode 100644 index 000000000..48e0077f4 --- /dev/null +++ b/examples-testing/examples/webgl_materials_envmaps_groundprojected.ts @@ -0,0 +1,150 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GroundedSkybox } from 'three/addons/objects/GroundedSkybox.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +const params = { + height: 15, + radius: 100, + enabled: true, +}; + +let camera, scene, renderer, skybox; + +init().then(render); + +async function init() { + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(-20, 7, 20); + camera.lookAt(0, 4, 0); + + scene = new THREE.Scene(); + + const hdrLoader = new RGBELoader(); + const envMap = await hdrLoader.loadAsync('textures/equirectangular/blouberg_sunrise_2_1k.hdr'); + envMap.mapping = THREE.EquirectangularReflectionMapping; + + skybox = new GroundedSkybox(envMap, params.height, params.radius); + skybox.position.y = params.height - 0.01; + scene.add(skybox); + + scene.environment = envMap; + + const dracoLoader = new DRACOLoader(); + dracoLoader.setDecoderPath('jsm/libs/draco/gltf/'); + + const loader = new GLTFLoader(); + loader.setDRACOLoader(dracoLoader); + + const shadow = new THREE.TextureLoader().load('models/gltf/ferrari_ao.png'); + + loader.load('models/gltf/ferrari.glb', function (gltf) { + const bodyMaterial = new THREE.MeshPhysicalMaterial({ + color: 0x000000, + metalness: 1.0, + roughness: 0.8, + clearcoat: 1.0, + clearcoatRoughness: 0.2, + }); + + const detailsMaterial = new THREE.MeshStandardMaterial({ + color: 0xffffff, + metalness: 1.0, + roughness: 0.5, + }); + + const glassMaterial = new THREE.MeshPhysicalMaterial({ + color: 0xffffff, + metalness: 0.25, + roughness: 0, + transmission: 1.0, + }); + + const carModel = gltf.scene.children[0]; + carModel.scale.multiplyScalar(4); + carModel.rotation.y = Math.PI; + + carModel.getObjectByName('body').material = bodyMaterial; + + carModel.getObjectByName('rim_fl').material = detailsMaterial; + carModel.getObjectByName('rim_fr').material = detailsMaterial; + carModel.getObjectByName('rim_rr').material = detailsMaterial; + carModel.getObjectByName('rim_rl').material = detailsMaterial; + carModel.getObjectByName('trim').material = detailsMaterial; + + carModel.getObjectByName('glass').material = glassMaterial; + + // shadow + const mesh = new THREE.Mesh( + new THREE.PlaneGeometry(0.655 * 4, 1.3 * 4), + new THREE.MeshBasicMaterial({ + map: shadow, + blending: THREE.MultiplyBlending, + toneMapped: false, + transparent: true, + }), + ); + mesh.rotation.x = -Math.PI / 2; + carModel.add(mesh); + + scene.add(carModel); + + render(); + }); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); + controls.target.set(0, 2, 0); + controls.maxPolarAngle = THREE.MathUtils.degToRad(90); + controls.maxDistance = 80; + controls.minDistance = 20; + controls.enablePan = false; + controls.update(); + + document.body.appendChild(renderer.domElement); + window.addEventListener('resize', onWindowResize); + + const gui = new GUI(); + + gui.add(params, 'enabled') + .name('Grounded') + .onChange(function (value) { + if (value) { + scene.add(skybox); + scene.background = null; + } else { + scene.remove(skybox); + scene.background = scene.environment; + } + + render(); + }); + + gui.open(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_envmaps_hdr.ts b/examples-testing/examples/webgl_materials_envmaps_hdr.ts new file mode 100644 index 000000000..b4c6f64ef --- /dev/null +++ b/examples-testing/examples/webgl_materials_envmaps_hdr.ts @@ -0,0 +1,176 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { HDRCubeTextureLoader } from 'three/addons/loaders/HDRCubeTextureLoader.js'; +import { RGBMLoader } from 'three/addons/loaders/RGBMLoader.js'; +import { DebugEnvironment } from 'three/addons/environments/DebugEnvironment.js'; + +const params = { + envMap: 'HDR', + roughness: 0.0, + metalness: 0.0, + exposure: 1.0, + debug: false, +}; + +let container, stats; +let camera, scene, renderer, controls; +let torusMesh, planeMesh; +let generatedCubeRenderTarget, ldrCubeRenderTarget, hdrCubeRenderTarget, rgbmCubeRenderTarget; +let ldrCubeMap, hdrCubeMap, rgbmCubeMap; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 0, 120); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x000000); + + renderer = new THREE.WebGLRenderer(); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + + // + + let geometry = new THREE.TorusKnotGeometry(18, 8, 150, 20); + // let geometry = new THREE.SphereGeometry( 26, 64, 32 ); + let material = new THREE.MeshStandardMaterial({ + color: 0xffffff, + metalness: params.metalness, + roughness: params.roughness, + }); + + torusMesh = new THREE.Mesh(geometry, material); + scene.add(torusMesh); + + geometry = new THREE.PlaneGeometry(200, 200); + material = new THREE.MeshBasicMaterial(); + + planeMesh = new THREE.Mesh(geometry, material); + planeMesh.position.y = -50; + planeMesh.rotation.x = -Math.PI * 0.5; + scene.add(planeMesh); + + THREE.DefaultLoadingManager.onLoad = function () { + pmremGenerator.dispose(); + }; + + const hdrUrls = ['px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr']; + hdrCubeMap = new HDRCubeTextureLoader().setPath('./textures/cube/pisaHDR/').load(hdrUrls, function () { + hdrCubeRenderTarget = pmremGenerator.fromCubemap(hdrCubeMap); + + hdrCubeMap.magFilter = THREE.LinearFilter; + hdrCubeMap.needsUpdate = true; + }); + + const ldrUrls = ['px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png']; + ldrCubeMap = new THREE.CubeTextureLoader().setPath('./textures/cube/pisa/').load(ldrUrls, function () { + ldrCubeRenderTarget = pmremGenerator.fromCubemap(ldrCubeMap); + }); + + const rgbmUrls = ['px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png']; + rgbmCubeMap = new RGBMLoader() + .setMaxRange(16) + .setPath('./textures/cube/pisaRGBM16/') + .loadCubemap(rgbmUrls, function () { + rgbmCubeRenderTarget = pmremGenerator.fromCubemap(rgbmCubeMap); + }); + + const pmremGenerator = new THREE.PMREMGenerator(renderer); + pmremGenerator.compileCubemapShader(); + + const envScene = new DebugEnvironment(); + generatedCubeRenderTarget = pmremGenerator.fromScene(envScene); + + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + //renderer.toneMapping = ReinhardToneMapping; + + stats = new Stats(); + container.appendChild(stats.dom); + + controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 50; + controls.maxDistance = 300; + + window.addEventListener('resize', onWindowResize); + + const gui = new GUI(); + + gui.add(params, 'envMap', ['Generated', 'LDR', 'HDR', 'RGBM16']); + gui.add(params, 'roughness', 0, 1, 0.01); + gui.add(params, 'metalness', 0, 1, 0.01); + gui.add(params, 'exposure', 0, 2, 0.01); + gui.add(params, 'debug'); + gui.open(); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); +} + +function animate() { + stats.begin(); + render(); + stats.end(); +} + +function render() { + torusMesh.material.roughness = params.roughness; + torusMesh.material.metalness = params.metalness; + + let renderTarget, cubeMap; + + switch (params.envMap) { + case 'Generated': + renderTarget = generatedCubeRenderTarget; + cubeMap = generatedCubeRenderTarget.texture; + break; + case 'LDR': + renderTarget = ldrCubeRenderTarget; + cubeMap = ldrCubeMap; + break; + case 'HDR': + renderTarget = hdrCubeRenderTarget; + cubeMap = hdrCubeMap; + break; + case 'RGBM16': + renderTarget = rgbmCubeRenderTarget; + cubeMap = rgbmCubeMap; + break; + } + + const newEnvMap = renderTarget ? renderTarget.texture : null; + + if (newEnvMap && newEnvMap !== torusMesh.material.envMap) { + torusMesh.material.envMap = newEnvMap; + torusMesh.material.needsUpdate = true; + + planeMesh.material.map = newEnvMap; + planeMesh.material.needsUpdate = true; + } + + torusMesh.rotation.y += 0.005; + planeMesh.visible = params.debug; + + scene.background = cubeMap; + renderer.toneMappingExposure = params.exposure; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_modified.ts b/examples-testing/examples/webgl_materials_modified.ts new file mode 100644 index 000000000..c6ee5af3c --- /dev/null +++ b/examples-testing/examples/webgl_materials_modified.ts @@ -0,0 +1,115 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +let camera, scene, renderer, stats; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.z = 20; + + scene = new THREE.Scene(); + + const loader = new GLTFLoader(); + loader.load('models/gltf/LeePerrySmith/LeePerrySmith.glb', function (gltf) { + const geometry = gltf.scene.children[0].geometry; + + let mesh = new THREE.Mesh(geometry, buildTwistMaterial(2.0)); + mesh.position.x = -3.5; + mesh.position.y = -0.5; + scene.add(mesh); + + mesh = new THREE.Mesh(geometry, buildTwistMaterial(-2.0)); + mesh.position.x = 3.5; + mesh.position.y = -0.5; + scene.add(mesh); + }); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 10; + controls.maxDistance = 50; + + // + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // EVENTS + + window.addEventListener('resize', onWindowResize); +} + +function buildTwistMaterial(amount) { + const material = new THREE.MeshNormalMaterial(); + material.onBeforeCompile = function (shader) { + shader.uniforms.time = { value: 0 }; + + shader.vertexShader = 'uniform float time;\n' + shader.vertexShader; + shader.vertexShader = shader.vertexShader.replace( + '#include ', + [ + `float theta = sin( time + position.y ) / ${amount.toFixed(1)};`, + 'float c = cos( theta );', + 'float s = sin( theta );', + 'mat3 m = mat3( c, 0, s, 0, 1, 0, -s, 0, c );', + 'vec3 transformed = vec3( position ) * m;', + 'vNormal = vNormal * m;', + ].join('\n'), + ); + + material.userData.shader = shader; + }; + + // Make sure WebGLRenderer doesnt reuse a single program + + material.customProgramCacheKey = function () { + return amount.toFixed(1); + }; + + return material; +} + +// + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); +} + +// + +function animate() { + render(); + + stats.update(); +} + +function render() { + scene.traverse(function (child) { + if (child.isMesh) { + const shader = child.material.userData.shader; + + if (shader) { + shader.uniforms.time.value = performance.now() / 1000; + } + } + }); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_normalmap_object_space.ts b/examples-testing/examples/webgl_materials_normalmap_object_space.ts new file mode 100644 index 000000000..1fc6f8066 --- /dev/null +++ b/examples-testing/examples/webgl_materials_normalmap_object_space.ts @@ -0,0 +1,82 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +let renderer, scene, camera; + +init(); + +function init() { + // renderer + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + // scene + scene = new THREE.Scene(); + + // camera + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(-10, 0, 23); + scene.add(camera); + + // controls + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); + controls.minDistance = 10; + controls.maxDistance = 50; + controls.enablePan = false; + + // ambient + scene.add(new THREE.AmbientLight(0xffffff, 0.6)); + + // light + const light = new THREE.PointLight(0xffffff, 4.5, 0, 0); + camera.add(light); + + // model + new GLTFLoader().load('models/gltf/Nefertiti/Nefertiti.glb', function (gltf) { + gltf.scene.traverse(function (child) { + if (child.isMesh) { + // glTF currently supports only tangent-space normal maps. + // this model has been modified to demonstrate the use of an object-space normal map. + + child.material.normalMapType = THREE.ObjectSpaceNormalMap; + + // attribute normals are not required with an object-space normal map. remove them. + + child.geometry.deleteAttribute('normal'); + + // + + child.material.side = THREE.DoubleSide; + + child.scale.multiplyScalar(0.5); + + // recenter + + new THREE.Box3().setFromObject(child).getCenter(child.position).multiplyScalar(-1); + + scene.add(child); + } + }); + + render(); + }); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_physical_clearcoat.ts b/examples-testing/examples/webgl_materials_physical_clearcoat.ts new file mode 100644 index 000000000..408fd9921 --- /dev/null +++ b/examples-testing/examples/webgl_materials_physical_clearcoat.ts @@ -0,0 +1,208 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { HDRCubeTextureLoader } from 'three/addons/loaders/HDRCubeTextureLoader.js'; + +import { FlakesTexture } from 'three/addons/textures/FlakesTexture.js'; + +let container, stats; + +let camera, scene, renderer; + +let particleLight; +let group; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 0.25, 50); + camera.position.z = 10; + + scene = new THREE.Scene(); + + group = new THREE.Group(); + scene.add(group); + + new HDRCubeTextureLoader() + .setPath('textures/cube/pisaHDR/') + .load(['px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr'], function (texture) { + const geometry = new THREE.SphereGeometry(0.8, 64, 32); + + const textureLoader = new THREE.TextureLoader(); + + const diffuse = textureLoader.load('textures/carbon/Carbon.png'); + diffuse.colorSpace = THREE.SRGBColorSpace; + diffuse.wrapS = THREE.RepeatWrapping; + diffuse.wrapT = THREE.RepeatWrapping; + diffuse.repeat.x = 10; + diffuse.repeat.y = 10; + + const normalMap = textureLoader.load('textures/carbon/Carbon_Normal.png'); + normalMap.wrapS = THREE.RepeatWrapping; + normalMap.wrapT = THREE.RepeatWrapping; + normalMap.repeat.x = 10; + normalMap.repeat.y = 10; + + const normalMap2 = textureLoader.load('textures/water/Water_1_M_Normal.jpg'); + + const normalMap3 = new THREE.CanvasTexture(new FlakesTexture()); + normalMap3.wrapS = THREE.RepeatWrapping; + normalMap3.wrapT = THREE.RepeatWrapping; + normalMap3.repeat.x = 10; + normalMap3.repeat.y = 6; + normalMap3.anisotropy = 16; + + const normalMap4 = textureLoader.load('textures/golfball.jpg'); + + const clearcoatNormalMap = textureLoader.load( + 'textures/pbr/Scratched_gold/Scratched_gold_01_1K_Normal.png', + ); + + // car paint + + let material = new THREE.MeshPhysicalMaterial({ + clearcoat: 1.0, + clearcoatRoughness: 0.1, + metalness: 0.9, + roughness: 0.5, + color: 0x0000ff, + normalMap: normalMap3, + normalScale: new THREE.Vector2(0.15, 0.15), + }); + + let mesh = new THREE.Mesh(geometry, material); + mesh.position.x = -1; + mesh.position.y = 1; + group.add(mesh); + + // fibers + + material = new THREE.MeshPhysicalMaterial({ + roughness: 0.5, + clearcoat: 1.0, + clearcoatRoughness: 0.1, + map: diffuse, + normalMap: normalMap, + }); + mesh = new THREE.Mesh(geometry, material); + mesh.position.x = 1; + mesh.position.y = 1; + group.add(mesh); + + // golf + + material = new THREE.MeshPhysicalMaterial({ + metalness: 0.0, + roughness: 0.1, + clearcoat: 1.0, + normalMap: normalMap4, + clearcoatNormalMap: clearcoatNormalMap, + + // y scale is negated to compensate for normal map handedness. + clearcoatNormalScale: new THREE.Vector2(2.0, -2.0), + }); + mesh = new THREE.Mesh(geometry, material); + mesh.position.x = -1; + mesh.position.y = -1; + group.add(mesh); + + // clearcoat + normalmap + + material = new THREE.MeshPhysicalMaterial({ + clearcoat: 1.0, + metalness: 1.0, + color: 0xff0000, + normalMap: normalMap2, + normalScale: new THREE.Vector2(0.15, 0.15), + clearcoatNormalMap: clearcoatNormalMap, + + // y scale is negated to compensate for normal map handedness. + clearcoatNormalScale: new THREE.Vector2(2.0, -2.0), + }); + mesh = new THREE.Mesh(geometry, material); + mesh.position.x = 1; + mesh.position.y = -1; + group.add(mesh); + + // + + scene.background = texture; + scene.environment = texture; + }); + + // LIGHTS + + particleLight = new THREE.Mesh( + new THREE.SphereGeometry(0.05, 8, 8), + new THREE.MeshBasicMaterial({ color: 0xffffff }), + ); + scene.add(particleLight); + + particleLight.add(new THREE.PointLight(0xffffff, 30)); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1.25; + + // + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // EVENTS + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 3; + controls.maxDistance = 30; + + window.addEventListener('resize', onWindowResize); +} + +// + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); +} + +// + +function animate() { + render(); + + stats.update(); +} + +function render() { + const timer = Date.now() * 0.00025; + + particleLight.position.x = Math.sin(timer * 7) * 3; + particleLight.position.y = Math.cos(timer * 5) * 4; + particleLight.position.z = Math.cos(timer * 3) * 3; + + for (let i = 0; i < group.children.length; i++) { + const child = group.children[i]; + child.rotation.y += 0.005; + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_physical_transmission.ts b/examples-testing/examples/webgl_materials_physical_transmission.ts new file mode 100644 index 000000000..d45967971 --- /dev/null +++ b/examples-testing/examples/webgl_materials_physical_transmission.ts @@ -0,0 +1,182 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +const params = { + color: 0xffffff, + transmission: 1, + opacity: 1, + metalness: 0, + roughness: 0, + ior: 1.5, + thickness: 0.01, + specularIntensity: 1, + specularColor: 0xffffff, + envMapIntensity: 1, + lightIntensity: 1, + exposure: 1, +}; + +let camera, scene, renderer; + +let mesh; + +const hdrEquirect = new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function () { + hdrEquirect.mapping = THREE.EquirectangularReflectionMapping; + + init(); + render(); +}); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.shadowMap.enabled = true; + document.body.appendChild(renderer.domElement); + + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = params.exposure; + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 2000); + camera.position.set(0, 0, 120); + + // + + scene.background = hdrEquirect; + + // + + const geometry = new THREE.SphereGeometry(20, 64, 32); + + const texture = new THREE.CanvasTexture(generateTexture()); + texture.magFilter = THREE.NearestFilter; + texture.wrapT = THREE.RepeatWrapping; + texture.wrapS = THREE.RepeatWrapping; + texture.repeat.set(1, 3.5); + + const material = new THREE.MeshPhysicalMaterial({ + color: params.color, + metalness: params.metalness, + roughness: params.roughness, + ior: params.ior, + alphaMap: texture, + envMap: hdrEquirect, + envMapIntensity: params.envMapIntensity, + transmission: params.transmission, // use material.transmission for glass materials + specularIntensity: params.specularIntensity, + specularColor: params.specularColor, + opacity: params.opacity, + side: THREE.DoubleSide, + transparent: true, + }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.minDistance = 10; + controls.maxDistance = 150; + + window.addEventListener('resize', onWindowResize); + + // + + const gui = new GUI(); + + gui.addColor(params, 'color').onChange(function () { + material.color.set(params.color); + render(); + }); + + gui.add(params, 'transmission', 0, 1, 0.01).onChange(function () { + material.transmission = params.transmission; + render(); + }); + + gui.add(params, 'opacity', 0, 1, 0.01).onChange(function () { + material.opacity = params.opacity; + render(); + }); + + gui.add(params, 'metalness', 0, 1, 0.01).onChange(function () { + material.metalness = params.metalness; + render(); + }); + + gui.add(params, 'roughness', 0, 1, 0.01).onChange(function () { + material.roughness = params.roughness; + render(); + }); + + gui.add(params, 'ior', 1, 2, 0.01).onChange(function () { + material.ior = params.ior; + render(); + }); + + gui.add(params, 'thickness', 0, 5, 0.01).onChange(function () { + material.thickness = params.thickness; + render(); + }); + + gui.add(params, 'specularIntensity', 0, 1, 0.01).onChange(function () { + material.specularIntensity = params.specularIntensity; + render(); + }); + + gui.addColor(params, 'specularColor').onChange(function () { + material.specularColor.set(params.specularColor); + render(); + }); + + gui.add(params, 'envMapIntensity', 0, 1, 0.01) + .name('envMap intensity') + .onChange(function () { + material.envMapIntensity = params.envMapIntensity; + render(); + }); + + gui.add(params, 'exposure', 0, 1, 0.01).onChange(function () { + renderer.toneMappingExposure = params.exposure; + render(); + }); + + gui.open(); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); + + render(); +} + +// + +function generateTexture() { + const canvas = document.createElement('canvas'); + canvas.width = 2; + canvas.height = 2; + + const context = canvas.getContext('2d'); + context.fillStyle = 'white'; + context.fillRect(0, 1, 2, 1); + + return canvas; +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_physical_transmission_alpha.ts b/examples-testing/examples/webgl_materials_physical_transmission_alpha.ts new file mode 100644 index 000000000..d81f59c37 --- /dev/null +++ b/examples-testing/examples/webgl_materials_physical_transmission_alpha.ts @@ -0,0 +1,192 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +const params = { + color: 0xffffff, + transmission: 1, + opacity: 1, + metalness: 0, + roughness: 0, + ior: 1.5, + thickness: 0.01, + attenuationColor: 0xffffff, + attenuationDistance: 1, + specularIntensity: 1, + specularColor: 0xffffff, + envMapIntensity: 1, + lightIntensity: 1, + exposure: 1, +}; + +let camera, scene, renderer; + +let mesh, material; + +const hdrEquirect = new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function () { + hdrEquirect.mapping = THREE.EquirectangularReflectionMapping; + + new GLTFLoader().setPath('models/gltf/').load('DragonAttenuation.glb', function (gltf) { + gltf.scene.traverse(function (child) { + if (child.isMesh && child.material.isMeshPhysicalMaterial) { + mesh = child; + material = mesh.material; + + const color = new THREE.Color(); + + params.color = color.copy(mesh.material.color).getHex(); + params.roughness = mesh.material.roughness; + params.metalness = mesh.material.metalness; + + params.ior = mesh.material.ior; + params.specularIntensity = mesh.material.specularIntensity; + + params.transmission = mesh.material.transmission; + params.thickness = mesh.material.thickness; + params.attenuationColor = color.copy(mesh.material.attenuationColor).getHex(); + params.attenuationDistance = mesh.material.attenuationDistance; + } + }); + + init(); + + scene.add(gltf.scene); + + scene.environment = hdrEquirect; + //scene.background = hdrEquirect; + + render(); + }); +}); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.shadowMap.enabled = true; + document.body.appendChild(renderer.domElement); + + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = params.exposure; + + // accommodate CSS table + renderer.domElement.style.position = 'absolute'; + renderer.domElement.style.top = 0; + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 2000); + camera.position.set(-5, 0.5, 0); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.minDistance = 5; + controls.maxDistance = 20; + controls.target.y = 0.5; + controls.update(); + + window.addEventListener('resize', onWindowResize); + + // + + const gui = new GUI(); + + gui.addColor(params, 'color').onChange(function () { + material.color.set(params.color); + render(); + }); + + gui.add(params, 'transmission', 0, 1, 0.01).onChange(function () { + material.transmission = params.transmission; + render(); + }); + + gui.add(params, 'opacity', 0, 1, 0.01).onChange(function () { + material.opacity = params.opacity; + const transparent = params.opacity < 1; + + if (transparent !== material.transparent) { + material.transparent = transparent; + material.needsUpdate = true; + } + + render(); + }); + + gui.add(params, 'metalness', 0, 1, 0.01).onChange(function () { + material.metalness = params.metalness; + render(); + }); + + gui.add(params, 'roughness', 0, 1, 0.01).onChange(function () { + material.roughness = params.roughness; + render(); + }); + + gui.add(params, 'ior', 1, 2, 0.01).onChange(function () { + material.ior = params.ior; + render(); + }); + + gui.add(params, 'thickness', 0, 5, 0.01).onChange(function () { + material.thickness = params.thickness; + render(); + }); + + gui.addColor(params, 'attenuationColor') + .name('attenuation color') + .onChange(function () { + material.attenuationColor.set(params.attenuationColor); + render(); + }); + + gui.add(params, 'attenuationDistance', 0, 1, 0.01).onChange(function () { + material.attenuationDistance = params.attenuationDistance; + render(); + }); + + gui.add(params, 'specularIntensity', 0, 1, 0.01).onChange(function () { + material.specularIntensity = params.specularIntensity; + render(); + }); + + gui.addColor(params, 'specularColor').onChange(function () { + material.specularColor.set(params.specularColor); + render(); + }); + + gui.add(params, 'envMapIntensity', 0, 1, 0.01) + .name('envMap intensity') + .onChange(function () { + material.envMapIntensity = params.envMapIntensity; + render(); + }); + + gui.add(params, 'exposure', 0, 1, 0.01).onChange(function () { + renderer.toneMappingExposure = params.exposure; + render(); + }); + + gui.open(); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); + + render(); +} + +// + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_texture_anisotropy.ts b/examples-testing/examples/webgl_materials_texture_anisotropy.ts new file mode 100644 index 000000000..1e030d64d --- /dev/null +++ b/examples-testing/examples/webgl_materials_texture_anisotropy.ts @@ -0,0 +1,143 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +const SCREEN_WIDTH = window.innerWidth; +const SCREEN_HEIGHT = window.innerHeight; + +let container, stats; + +let camera, scene1, scene2, renderer; + +let mouseX = 0, + mouseY = 0; + +const windowHalfX = window.innerWidth / 2; +const windowHalfY = window.innerHeight / 2; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + + // + + camera = new THREE.PerspectiveCamera(35, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 25000); + camera.position.z = 1500; + + scene1 = new THREE.Scene(); + scene1.background = new THREE.Color(0xf2f7ff); + scene1.fog = new THREE.Fog(0xf2f7ff, 1, 25000); + + scene2 = new THREE.Scene(); + scene2.background = new THREE.Color(0xf2f7ff); + scene2.fog = new THREE.Fog(0xf2f7ff, 1, 25000); + + scene1.add(new THREE.AmbientLight(0xeef0ff, 3)); + scene2.add(new THREE.AmbientLight(0xeef0ff, 3)); + + const light1 = new THREE.DirectionalLight(0xffffff, 6); + light1.position.set(1, 1, 1); + scene1.add(light1); + + const light2 = new THREE.DirectionalLight(0xffffff, 6); + light2.position.set(1, 1, 1); + scene2.add(light2); + + // GROUND + + const textureLoader = new THREE.TextureLoader(); + + const maxAnisotropy = renderer.capabilities.getMaxAnisotropy(); + + const texture1 = textureLoader.load('textures/crate.gif'); + const material1 = new THREE.MeshPhongMaterial({ color: 0xffffff, map: texture1 }); + + texture1.colorSpace = THREE.SRGBColorSpace; + texture1.anisotropy = maxAnisotropy; + texture1.wrapS = texture1.wrapT = THREE.RepeatWrapping; + texture1.repeat.set(512, 512); + + const texture2 = textureLoader.load('textures/crate.gif'); + const material2 = new THREE.MeshPhongMaterial({ color: 0xffffff, map: texture2 }); + + texture2.colorSpace = THREE.SRGBColorSpace; + texture2.anisotropy = 1; + texture2.wrapS = texture2.wrapT = THREE.RepeatWrapping; + texture2.repeat.set(512, 512); + + if (maxAnisotropy > 0) { + document.getElementById('val_left').innerHTML = texture1.anisotropy; + document.getElementById('val_right').innerHTML = texture2.anisotropy; + } else { + document.getElementById('val_left').innerHTML = 'not supported'; + document.getElementById('val_right').innerHTML = 'not supported'; + } + + // + + const geometry = new THREE.PlaneGeometry(100, 100); + + const mesh1 = new THREE.Mesh(geometry, material1); + mesh1.rotation.x = -Math.PI / 2; + mesh1.scale.set(1000, 1000, 1000); + + const mesh2 = new THREE.Mesh(geometry, material2); + mesh2.rotation.x = -Math.PI / 2; + mesh2.scale.set(1000, 1000, 1000); + + scene1.add(mesh1); + scene2.add(mesh2); + + // RENDERER + + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + renderer.setAnimationLoop(animate); + renderer.autoClear = false; + + renderer.domElement.style.position = 'relative'; + container.appendChild(renderer.domElement); + + // STATS1 + + stats = new Stats(); + container.appendChild(stats.dom); + + document.addEventListener('mousemove', onDocumentMouseMove); +} + +function onDocumentMouseMove(event) { + mouseX = event.clientX - windowHalfX; + mouseY = event.clientY - windowHalfY; +} + +function animate() { + render(); + stats.update(); +} + +function render() { + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y = THREE.MathUtils.clamp( + camera.position.y + (-(mouseY - 200) - camera.position.y) * 0.05, + 50, + 1000, + ); + + camera.lookAt(scene1.position); + + renderer.clear(); + renderer.setScissorTest(true); + + renderer.setScissor(0, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); + renderer.render(scene1, camera); + + renderer.setScissor(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); + renderer.render(scene2, camera); + + renderer.setScissorTest(false); +} diff --git a/examples-testing/examples/webgl_materials_texture_canvas.ts b/examples-testing/examples/webgl_materials_texture_canvas.ts new file mode 100644 index 000000000..d23c68436 --- /dev/null +++ b/examples-testing/examples/webgl_materials_texture_canvas.ts @@ -0,0 +1,92 @@ +import * as THREE from 'three'; + +let camera, scene, renderer, mesh, material; +const drawStartPos = new THREE.Vector2(); + +init(); +setupCanvasDrawing(); + +function init() { + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 2000); + camera.position.z = 500; + + scene = new THREE.Scene(); + + material = new THREE.MeshBasicMaterial(); + + mesh = new THREE.Mesh(new THREE.BoxGeometry(200, 200, 200), material); + scene.add(mesh); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); +} + +// Sets up the drawing canvas and adds it as the material map + +function setupCanvasDrawing() { + // get canvas and context + + const drawingCanvas = document.getElementById('drawing-canvas'); + const drawingContext = drawingCanvas.getContext('2d'); + + // draw white background + + drawingContext.fillStyle = '#FFFFFF'; + drawingContext.fillRect(0, 0, 128, 128); + + // set canvas as material.map (this could be done to any map, bump, displacement etc.) + + material.map = new THREE.CanvasTexture(drawingCanvas); + + // set the variable to keep track of when to draw + + let paint = false; + + // add canvas event listeners + drawingCanvas.addEventListener('pointerdown', function (e) { + paint = true; + drawStartPos.set(e.offsetX, e.offsetY); + }); + + drawingCanvas.addEventListener('pointermove', function (e) { + if (paint) draw(drawingContext, e.offsetX, e.offsetY); + }); + + drawingCanvas.addEventListener('pointerup', function () { + paint = false; + }); + + drawingCanvas.addEventListener('pointerleave', function () { + paint = false; + }); +} + +function draw(drawContext, x, y) { + drawContext.moveTo(drawStartPos.x, drawStartPos.y); + drawContext.strokeStyle = '#000000'; + drawContext.lineTo(x, y); + drawContext.stroke(); + // reset drawing start position to current position. + drawStartPos.set(x, y); + // need to flag the map as needing updating. + material.map.needsUpdate = true; +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + mesh.rotation.x += 0.01; + mesh.rotation.y += 0.01; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_texture_filters.ts b/examples-testing/examples/webgl_materials_texture_filters.ts new file mode 100644 index 000000000..178c2ce49 --- /dev/null +++ b/examples-testing/examples/webgl_materials_texture_filters.ts @@ -0,0 +1,164 @@ +import * as THREE from 'three'; + +const SCREEN_WIDTH = window.innerWidth; +const SCREEN_HEIGHT = window.innerHeight; + +let container; + +let camera, scene, scene2, renderer; + +let mouseX = 0, + mouseY = 0; + +const windowHalfX = window.innerWidth / 2; +const windowHalfY = window.innerHeight / 2; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(35, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 5000); + camera.position.z = 1500; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x000000); + scene.fog = new THREE.Fog(0x000000, 1500, 4000); + + scene2 = new THREE.Scene(); + scene2.background = new THREE.Color(0x000000); + scene2.fog = new THREE.Fog(0x000000, 1500, 4000); + + // GROUND + + const imageCanvas = document.createElement('canvas'); + const context = imageCanvas.getContext('2d'); + + imageCanvas.width = imageCanvas.height = 128; + + context.fillStyle = '#444'; + context.fillRect(0, 0, 128, 128); + + context.fillStyle = '#fff'; + context.fillRect(0, 0, 64, 64); + context.fillRect(64, 64, 64, 64); + + const textureCanvas = new THREE.CanvasTexture(imageCanvas); + textureCanvas.colorSpace = THREE.SRGBColorSpace; + textureCanvas.repeat.set(1000, 1000); + textureCanvas.wrapS = THREE.RepeatWrapping; + textureCanvas.wrapT = THREE.RepeatWrapping; + + const textureCanvas2 = textureCanvas.clone(); + textureCanvas2.magFilter = THREE.NearestFilter; + textureCanvas2.minFilter = THREE.NearestFilter; + + const materialCanvas = new THREE.MeshBasicMaterial({ map: textureCanvas }); + const materialCanvas2 = new THREE.MeshBasicMaterial({ color: 0xffccaa, map: textureCanvas2 }); + + const geometry = new THREE.PlaneGeometry(100, 100); + + const meshCanvas = new THREE.Mesh(geometry, materialCanvas); + meshCanvas.rotation.x = -Math.PI / 2; + meshCanvas.scale.set(1000, 1000, 1000); + + const meshCanvas2 = new THREE.Mesh(geometry, materialCanvas2); + meshCanvas2.rotation.x = -Math.PI / 2; + meshCanvas2.scale.set(1000, 1000, 1000); + + // PAINTING + + const callbackPainting = function () { + const image = texturePainting.image; + + texturePainting2.image = image; + texturePainting2.needsUpdate = true; + + scene.add(meshCanvas); + scene2.add(meshCanvas2); + + const geometry = new THREE.PlaneGeometry(100, 100); + const mesh = new THREE.Mesh(geometry, materialPainting); + const mesh2 = new THREE.Mesh(geometry, materialPainting2); + + addPainting(scene, mesh); + addPainting(scene2, mesh2); + + function addPainting(zscene, zmesh) { + zmesh.scale.x = image.width / 100; + zmesh.scale.y = image.height / 100; + + zscene.add(zmesh); + + const meshFrame = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ color: 0x000000 })); + meshFrame.position.z = -10.0; + meshFrame.scale.x = (1.1 * image.width) / 100; + meshFrame.scale.y = (1.1 * image.height) / 100; + zscene.add(meshFrame); + + const meshShadow = new THREE.Mesh( + geometry, + new THREE.MeshBasicMaterial({ color: 0x000000, opacity: 0.75, transparent: true }), + ); + meshShadow.position.y = (-1.1 * image.height) / 2; + meshShadow.position.z = (-1.1 * image.height) / 2; + meshShadow.rotation.x = -Math.PI / 2; + meshShadow.scale.x = (1.1 * image.width) / 100; + meshShadow.scale.y = (1.1 * image.height) / 100; + zscene.add(meshShadow); + + const floorHeight = (-1.117 * image.height) / 2; + meshCanvas.position.y = meshCanvas2.position.y = floorHeight; + } + }; + + const texturePainting = new THREE.TextureLoader().load( + 'textures/758px-Canestra_di_frutta_(Caravaggio).jpg', + callbackPainting, + ); + const texturePainting2 = new THREE.Texture(); + + const materialPainting = new THREE.MeshBasicMaterial({ color: 0xffffff, map: texturePainting }); + const materialPainting2 = new THREE.MeshBasicMaterial({ color: 0xffccaa, map: texturePainting2 }); + + texturePainting.colorSpace = THREE.SRGBColorSpace; + texturePainting2.colorSpace = THREE.SRGBColorSpace; + texturePainting2.minFilter = texturePainting2.magFilter = THREE.NearestFilter; + texturePainting.minFilter = texturePainting.magFilter = THREE.LinearFilter; + texturePainting.mapping = THREE.UVMapping; + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + renderer.setAnimationLoop(animate); + renderer.autoClear = false; + + renderer.domElement.style.position = 'relative'; + container.appendChild(renderer.domElement); + + document.addEventListener('mousemove', onDocumentMouseMove); +} + +function onDocumentMouseMove(event) { + mouseX = event.clientX - windowHalfX; + mouseY = event.clientY - windowHalfY; +} + +function animate() { + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-(mouseY - 200) - camera.position.y) * 0.05; + + camera.lookAt(scene.position); + + renderer.clear(); + renderer.setScissorTest(true); + + renderer.setScissor(0, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); + renderer.render(scene, camera); + + renderer.setScissor(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); + renderer.render(scene2, camera); + + renderer.setScissorTest(false); +} diff --git a/examples-testing/examples/webgl_materials_texture_manualmipmap.ts b/examples-testing/examples/webgl_materials_texture_manualmipmap.ts new file mode 100644 index 000000000..24bd4eb9f --- /dev/null +++ b/examples-testing/examples/webgl_materials_texture_manualmipmap.ts @@ -0,0 +1,175 @@ +import * as THREE from 'three'; + +const SCREEN_WIDTH = window.innerWidth; +const SCREEN_HEIGHT = window.innerHeight; + +let container; + +let camera, scene1, scene2, renderer; + +let mouseX = 0, + mouseY = 0; + +const windowHalfX = window.innerWidth / 2; +const windowHalfY = window.innerHeight / 2; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(35, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 5000); + camera.position.z = 1500; + + scene1 = new THREE.Scene(); + scene1.background = new THREE.Color(0x000000); + scene1.fog = new THREE.Fog(0x000000, 1500, 4000); + + scene2 = new THREE.Scene(); + scene2.background = new THREE.Color(0x000000); + scene2.fog = new THREE.Fog(0x000000, 1500, 4000); + + // GROUND + + function mipmap(size, color) { + const imageCanvas = document.createElement('canvas'); + const context = imageCanvas.getContext('2d'); + + imageCanvas.width = imageCanvas.height = size; + + context.fillStyle = '#444'; + context.fillRect(0, 0, size, size); + + context.fillStyle = color; + context.fillRect(0, 0, size / 2, size / 2); + context.fillRect(size / 2, size / 2, size / 2, size / 2); + return imageCanvas; + } + + const canvas = mipmap(128, '#f00'); + const textureCanvas1 = new THREE.CanvasTexture(canvas); + textureCanvas1.mipmaps[0] = canvas; + textureCanvas1.mipmaps[1] = mipmap(64, '#0f0'); + textureCanvas1.mipmaps[2] = mipmap(32, '#00f'); + textureCanvas1.mipmaps[3] = mipmap(16, '#400'); + textureCanvas1.mipmaps[4] = mipmap(8, '#040'); + textureCanvas1.mipmaps[5] = mipmap(4, '#004'); + textureCanvas1.mipmaps[6] = mipmap(2, '#044'); + textureCanvas1.mipmaps[7] = mipmap(1, '#404'); + textureCanvas1.colorSpace = THREE.SRGBColorSpace; + textureCanvas1.repeat.set(1000, 1000); + textureCanvas1.wrapS = THREE.RepeatWrapping; + textureCanvas1.wrapT = THREE.RepeatWrapping; + + const textureCanvas2 = textureCanvas1.clone(); + textureCanvas2.magFilter = THREE.NearestFilter; + textureCanvas2.minFilter = THREE.NearestMipmapNearestFilter; + + const materialCanvas1 = new THREE.MeshBasicMaterial({ map: textureCanvas1 }); + const materialCanvas2 = new THREE.MeshBasicMaterial({ color: 0xffccaa, map: textureCanvas2 }); + + const geometry = new THREE.PlaneGeometry(100, 100); + + const meshCanvas1 = new THREE.Mesh(geometry, materialCanvas1); + meshCanvas1.rotation.x = -Math.PI / 2; + meshCanvas1.scale.set(1000, 1000, 1000); + + const meshCanvas2 = new THREE.Mesh(geometry, materialCanvas2); + meshCanvas2.rotation.x = -Math.PI / 2; + meshCanvas2.scale.set(1000, 1000, 1000); + + // PAINTING + + const callbackPainting = function () { + const image = texturePainting1.image; + + texturePainting2.image = image; + texturePainting2.needsUpdate = true; + + scene1.add(meshCanvas1); + scene2.add(meshCanvas2); + + const geometry = new THREE.PlaneGeometry(100, 100); + const mesh1 = new THREE.Mesh(geometry, materialPainting1); + const mesh2 = new THREE.Mesh(geometry, materialPainting2); + + addPainting(scene1, mesh1); + addPainting(scene2, mesh2); + + function addPainting(zscene, zmesh) { + zmesh.scale.x = image.width / 100; + zmesh.scale.y = image.height / 100; + + zscene.add(zmesh); + + const meshFrame = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ color: 0x000000 })); + meshFrame.position.z = -10.0; + meshFrame.scale.x = (1.1 * image.width) / 100; + meshFrame.scale.y = (1.1 * image.height) / 100; + zscene.add(meshFrame); + + const meshShadow = new THREE.Mesh( + geometry, + new THREE.MeshBasicMaterial({ color: 0x000000, opacity: 0.75, transparent: true }), + ); + meshShadow.position.y = (-1.1 * image.height) / 2; + meshShadow.position.z = (-1.1 * image.height) / 2; + meshShadow.rotation.x = -Math.PI / 2; + meshShadow.scale.x = (1.1 * image.width) / 100; + meshShadow.scale.y = (1.1 * image.height) / 100; + zscene.add(meshShadow); + + const floorHeight = (-1.117 * image.height) / 2; + meshCanvas1.position.y = meshCanvas2.position.y = floorHeight; + } + }; + + const texturePainting1 = new THREE.TextureLoader().load( + 'textures/758px-Canestra_di_frutta_(Caravaggio).jpg', + callbackPainting, + ); + const texturePainting2 = new THREE.Texture(); + const materialPainting1 = new THREE.MeshBasicMaterial({ color: 0xffffff, map: texturePainting1 }); + const materialPainting2 = new THREE.MeshBasicMaterial({ color: 0xffccaa, map: texturePainting2 }); + + texturePainting1.colorSpace = THREE.SRGBColorSpace; + texturePainting2.colorSpace = THREE.SRGBColorSpace; + texturePainting2.minFilter = texturePainting2.magFilter = THREE.NearestFilter; + texturePainting1.minFilter = texturePainting1.magFilter = THREE.LinearFilter; + texturePainting1.mapping = THREE.UVMapping; + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + renderer.setAnimationLoop(animate); + renderer.autoClear = false; + + renderer.domElement.style.position = 'relative'; + container.appendChild(renderer.domElement); + + document.addEventListener('mousemove', onDocumentMouseMove); +} + +function onDocumentMouseMove(event) { + mouseX = event.clientX - windowHalfX; + mouseY = event.clientY - windowHalfY; +} + +function animate() { + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-(mouseY - 200) - camera.position.y) * 0.05; + + camera.lookAt(scene1.position); + + renderer.clear(); + renderer.setScissorTest(true); + + renderer.setScissor(0, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); + renderer.render(scene1, camera); + + renderer.setScissor(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); + renderer.render(scene2, camera); + + renderer.setScissorTest(false); +} diff --git a/examples-testing/examples/webgl_materials_texture_partialupdate.ts b/examples-testing/examples/webgl_materials_texture_partialupdate.ts new file mode 100644 index 000000000..5adfc8e69 --- /dev/null +++ b/examples-testing/examples/webgl_materials_texture_partialupdate.ts @@ -0,0 +1,100 @@ +import * as THREE from 'three'; + +let camera, scene, renderer, clock, dataTexture, diffuseMap; + +let last = 0; +const position = new THREE.Vector2(); +const color = new THREE.Color(); + +init(); + +async function init() { + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10); + camera.position.z = 2; + + scene = new THREE.Scene(); + + clock = new THREE.Clock(); + + const loader = new THREE.TextureLoader(); + diffuseMap = await loader.loadAsync('textures/floors/FloorsCheckerboard_S_Diffuse.jpg'); + diffuseMap.colorSpace = THREE.SRGBColorSpace; + diffuseMap.minFilter = THREE.LinearFilter; + diffuseMap.generateMipmaps = false; + + const geometry = new THREE.PlaneGeometry(2, 2); + const material = new THREE.MeshBasicMaterial({ map: diffuseMap }); + + const mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + const width = 32; + const height = 32; + + const data = new Uint8Array(width * height * 4); + dataTexture = new THREE.DataTexture(data, width, height); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const elapsedTime = clock.getElapsedTime(); + + if (elapsedTime - last > 0.1) { + last = elapsedTime; + + position.x = 32 * THREE.MathUtils.randInt(1, 16) - 32; + position.y = 32 * THREE.MathUtils.randInt(1, 16) - 32; + + // generate new color data + + updateDataTexture(dataTexture); + + // perform copy from src to dest texture to a random position + + renderer.copyTextureToTexture(dataTexture, diffuseMap, null, position); + } + + renderer.render(scene, camera); +} + +function updateDataTexture(texture) { + const size = texture.image.width * texture.image.height; + const data = texture.image.data; + + // generate a random color and update texture data + + color.setHex(Math.random() * 0xffffff); + + const r = Math.floor(color.r * 255); + const g = Math.floor(color.g * 255); + const b = Math.floor(color.b * 255); + + for (let i = 0; i < size; i++) { + const stride = i * 4; + + data[stride] = r; + data[stride + 1] = g; + data[stride + 2] = b; + data[stride + 3] = 1; + } +} diff --git a/examples-testing/examples/webgl_materials_texture_rotation.ts b/examples-testing/examples/webgl_materials_texture_rotation.ts new file mode 100644 index 000000000..2666d09d6 --- /dev/null +++ b/examples-testing/examples/webgl_materials_texture_rotation.ts @@ -0,0 +1,113 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let mesh, renderer, scene, camera; + +let gui; + +const API = { + offsetX: 0, + offsetY: 0, + repeatX: 0.25, + repeatY: 0.25, + rotation: Math.PI / 4, // positive is counterclockwise + centerX: 0.5, + centerY: 0.5, +}; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(10, 15, 25); + scene.add(camera); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); + controls.minDistance = 20; + controls.maxDistance = 50; + controls.maxPolarAngle = Math.PI / 2; + + const geometry = new THREE.BoxGeometry(10, 10, 10); + + new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg', function (texture) { + texture.wrapS = texture.wrapT = THREE.RepeatWrapping; + texture.anisotropy = renderer.capabilities.getMaxAnisotropy(); + texture.colorSpace = THREE.SRGBColorSpace; + + //texture.matrixAutoUpdate = false; // default true; set to false to update texture.matrix manually + + const material = new THREE.MeshBasicMaterial({ map: texture }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + updateUvTransform(); + + initGui(); + + render(); + }); + + window.addEventListener('resize', onWindowResize); +} + +function render() { + renderer.render(scene, camera); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function updateUvTransform() { + const texture = mesh.material.map; + + if (texture.matrixAutoUpdate === true) { + texture.offset.set(API.offsetX, API.offsetY); + texture.repeat.set(API.repeatX, API.repeatY); + texture.center.set(API.centerX, API.centerY); + texture.rotation = API.rotation; // rotation is around center + } else { + // setting the matrix uv transform directly + //texture.matrix.setUvTransform( API.offsetX, API.offsetY, API.repeatX, API.repeatY, API.rotation, API.centerX, API.centerY ); + + // another way... + texture.matrix + .identity() + .translate(-API.centerX, -API.centerY) + .rotate(API.rotation) // I don't understand how rotation can preceed scale, but it seems to be required... + .scale(API.repeatX, API.repeatY) + .translate(API.centerX, API.centerY) + .translate(API.offsetX, API.offsetY); + } + + render(); +} + +function initGui() { + gui = new GUI(); + + gui.add(API, 'offsetX', 0.0, 1.0).name('offset.x').onChange(updateUvTransform); + gui.add(API, 'offsetY', 0.0, 1.0).name('offset.y').onChange(updateUvTransform); + gui.add(API, 'repeatX', 0.25, 2.0).name('repeat.x').onChange(updateUvTransform); + gui.add(API, 'repeatY', 0.25, 2.0).name('repeat.y').onChange(updateUvTransform); + gui.add(API, 'rotation', -2.0, 2.0).name('rotation').onChange(updateUvTransform); + gui.add(API, 'centerX', 0.0, 1.0).name('center.x').onChange(updateUvTransform); + gui.add(API, 'centerY', 0.0, 1.0).name('center.y').onChange(updateUvTransform); +} diff --git a/examples-testing/examples/webgl_materials_toon.ts b/examples-testing/examples/webgl_materials_toon.ts new file mode 100644 index 000000000..03db286ad --- /dev/null +++ b/examples-testing/examples/webgl_materials_toon.ts @@ -0,0 +1,152 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { OutlineEffect } from 'three/addons/effects/OutlineEffect.js'; +import { FontLoader } from 'three/addons/loaders/FontLoader.js'; +import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; + +let container, stats; + +let camera, scene, renderer, effect; +let particleLight; + +const loader = new FontLoader(); +loader.load('fonts/gentilis_regular.typeface.json', function (font) { + init(font); +}); + +function init(font) { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 2500); + camera.position.set(0.0, 400, 400 * 3.5); + + // + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x444488); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // Materials + + const cubeWidth = 400; + const numberOfSphersPerSide = 5; + const sphereRadius = (cubeWidth / numberOfSphersPerSide) * 0.8 * 0.5; + const stepSize = 1.0 / numberOfSphersPerSide; + + const geometry = new THREE.SphereGeometry(sphereRadius, 32, 16); + + for (let alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex++) { + const colors = new Uint8Array(alphaIndex + 2); + + for (let c = 0; c <= colors.length; c++) { + colors[c] = (c / colors.length) * 256; + } + + const gradientMap = new THREE.DataTexture(colors, colors.length, 1, THREE.RedFormat); + gradientMap.needsUpdate = true; + + for (let beta = 0; beta <= 1.0; beta += stepSize) { + for (let gamma = 0; gamma <= 1.0; gamma += stepSize) { + // basic monochromatic energy preservation + const diffuseColor = new THREE.Color() + .setHSL(alpha, 0.5, gamma * 0.5 + 0.1) + .multiplyScalar(1 - beta * 0.2); + + const material = new THREE.MeshToonMaterial({ + color: diffuseColor, + gradientMap: gradientMap, + }); + + const mesh = new THREE.Mesh(geometry, material); + + mesh.position.x = alpha * 400 - 200; + mesh.position.y = beta * 400 - 200; + mesh.position.z = gamma * 400 - 200; + + scene.add(mesh); + } + } + } + + function addLabel(name, location) { + const textGeo = new TextGeometry(name, { + font: font, + + size: 20, + depth: 1, + curveSegments: 1, + }); + + const textMaterial = new THREE.MeshBasicMaterial(); + const textMesh = new THREE.Mesh(textGeo, textMaterial); + textMesh.position.copy(location); + scene.add(textMesh); + } + + addLabel('-gradientMap', new THREE.Vector3(-350, 0, 0)); + addLabel('+gradientMap', new THREE.Vector3(350, 0, 0)); + + addLabel('-diffuse', new THREE.Vector3(0, 0, -300)); + addLabel('+diffuse', new THREE.Vector3(0, 0, 300)); + + particleLight = new THREE.Mesh(new THREE.SphereGeometry(4, 8, 8), new THREE.MeshBasicMaterial({ color: 0xffffff })); + scene.add(particleLight); + + // Lights + + scene.add(new THREE.AmbientLight(0xc1c1c1, 3)); + + const pointLight = new THREE.PointLight(0xffffff, 2, 800, 0); + particleLight.add(pointLight); + + // + + effect = new OutlineEffect(renderer); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 200; + controls.maxDistance = 2000; + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + stats.begin(); + render(); + stats.end(); +} + +function render() { + const timer = Date.now() * 0.00025; + + particleLight.position.x = Math.sin(timer * 7) * 300; + particleLight.position.y = Math.cos(timer * 5) * 400; + particleLight.position.z = Math.cos(timer * 3) * 300; + + effect.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_video.ts b/examples-testing/examples/webgl_materials_video.ts new file mode 100644 index 000000000..4f0d26a18 --- /dev/null +++ b/examples-testing/examples/webgl_materials_video.ts @@ -0,0 +1,208 @@ +import * as THREE from 'three'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { BloomPass } from 'three/addons/postprocessing/BloomPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +let container; + +let camera, scene, renderer; + +let video, texture, material, mesh; + +let composer; + +let mouseX = 0; +let mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +let cube_count; + +const meshes = [], + materials = [], + xgrid = 20, + ygrid = 10; + +const startButton = document.getElementById('startButton'); +startButton.addEventListener('click', function () { + init(); +}); + +function init() { + const overlay = document.getElementById('overlay'); + overlay.remove(); + + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 500; + + scene = new THREE.Scene(); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0.5, 1, 1).normalize(); + scene.add(light); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + video = document.getElementById('video'); + video.play(); + video.addEventListener('play', function () { + this.currentTime = 3; + }); + + texture = new THREE.VideoTexture(video); + texture.colorSpace = THREE.SRGBColorSpace; + + // + + let i, j, ox, oy, geometry; + + const ux = 1 / xgrid; + const uy = 1 / ygrid; + + const xsize = 480 / xgrid; + const ysize = 204 / ygrid; + + const parameters = { color: 0xffffff, map: texture }; + + cube_count = 0; + + for (i = 0; i < xgrid; i++) { + for (j = 0; j < ygrid; j++) { + ox = i; + oy = j; + + geometry = new THREE.BoxGeometry(xsize, ysize, xsize); + + change_uvs(geometry, ux, uy, ox, oy); + + materials[cube_count] = new THREE.MeshLambertMaterial(parameters); + + material = materials[cube_count]; + + material.hue = i / xgrid; + material.saturation = 1 - j / ygrid; + + material.color.setHSL(material.hue, material.saturation, 0.5); + + mesh = new THREE.Mesh(geometry, material); + + mesh.position.x = (i - xgrid / 2) * xsize; + mesh.position.y = (j - ygrid / 2) * ysize; + mesh.position.z = 0; + + mesh.scale.x = mesh.scale.y = mesh.scale.z = 1; + + scene.add(mesh); + + mesh.dx = 0.001 * (0.5 - Math.random()); + mesh.dy = 0.001 * (0.5 - Math.random()); + + meshes[cube_count] = mesh; + + cube_count += 1; + } + } + + renderer.autoClear = false; + + document.addEventListener('mousemove', onDocumentMouseMove); + + // postprocessing + + const renderPass = new RenderPass(scene, camera); + const bloomPass = new BloomPass(1.3); + const outputPass = new OutputPass(); + + composer = new EffectComposer(renderer); + + composer.addPass(renderPass); + composer.addPass(bloomPass); + composer.addPass(outputPass); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + composer.setSize(window.innerWidth, window.innerHeight); +} + +function change_uvs(geometry, unitx, unity, offsetx, offsety) { + const uvs = geometry.attributes.uv.array; + + for (let i = 0; i < uvs.length; i += 2) { + uvs[i] = (uvs[i] + offsetx) * unitx; + uvs[i + 1] = (uvs[i + 1] + offsety) * unity; + } +} + +function onDocumentMouseMove(event) { + mouseX = event.clientX - windowHalfX; + mouseY = (event.clientY - windowHalfY) * 0.3; +} + +// + +let h, + counter = 1; + +function animate() { + const time = Date.now() * 0.00005; + + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-mouseY - camera.position.y) * 0.05; + + camera.lookAt(scene.position); + + for (let i = 0; i < cube_count; i++) { + material = materials[i]; + + h = ((360 * (material.hue + time)) % 360) / 360; + material.color.setHSL(h, material.saturation, 0.5); + } + + if (counter % 1000 > 200) { + for (let i = 0; i < cube_count; i++) { + mesh = meshes[i]; + + mesh.rotation.x += 10 * mesh.dx; + mesh.rotation.y += 10 * mesh.dy; + + mesh.position.x -= 150 * mesh.dx; + mesh.position.y += 150 * mesh.dy; + mesh.position.z += 300 * mesh.dx; + } + } + + if (counter % 1000 === 0) { + for (let i = 0; i < cube_count; i++) { + mesh = meshes[i]; + + mesh.dx *= -1; + mesh.dy *= -1; + } + } + + counter++; + + renderer.clear(); + composer.render(); +} diff --git a/examples-testing/examples/webgl_materials_video_webcam.ts b/examples-testing/examples/webgl_materials_video_webcam.ts new file mode 100644 index 000000000..cf6f8d50c --- /dev/null +++ b/examples-testing/examples/webgl_materials_video_webcam.ts @@ -0,0 +1,79 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer, video; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.z = 0.01; + + scene = new THREE.Scene(); + + video = document.getElementById('video'); + + const texture = new THREE.VideoTexture(video); + texture.colorSpace = THREE.SRGBColorSpace; + + const geometry = new THREE.PlaneGeometry(16, 9); + geometry.scale(0.5, 0.5, 0.5); + const material = new THREE.MeshBasicMaterial({ map: texture }); + + const count = 128; + const radius = 32; + + for (let i = 1, l = count; i <= l; i++) { + const phi = Math.acos(-1 + (2 * i) / l); + const theta = Math.sqrt(l * Math.PI) * phi; + + const mesh = new THREE.Mesh(geometry, material); + mesh.position.setFromSphericalCoords(radius, phi, theta); + mesh.lookAt(camera.position); + scene.add(mesh); + } + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.enableZoom = false; + controls.enablePan = false; + + window.addEventListener('resize', onWindowResize); + + // + + if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { + const constraints = { video: { width: 1280, height: 720, facingMode: 'user' } }; + + navigator.mediaDevices + .getUserMedia(constraints) + .then(function (stream) { + // apply the stream to the video element used in the texture + + video.srcObject = stream; + video.play(); + }) + .catch(function (error) { + console.error('Unable to access the camera/webcam.', error); + }); + } else { + console.error('MediaDevices interface not available.'); + } +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_materials_wireframe.ts b/examples-testing/examples/webgl_materials_wireframe.ts new file mode 100644 index 000000000..8adbd71d6 --- /dev/null +++ b/examples-testing/examples/webgl_materials_wireframe.ts @@ -0,0 +1,107 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +const API = { + thickness: 1, +}; + +let renderer, scene, camera, mesh2; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 500); + camera.position.z = 200; + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.enablePan = false; + controls.enableZoom = false; + + new THREE.BufferGeometryLoader().load('models/json/WaltHeadLo_buffergeometry.json', function (geometry) { + geometry.deleteAttribute('normal'); + geometry.deleteAttribute('uv'); + + setupAttributes(geometry); + + // left + + const material1 = new THREE.MeshBasicMaterial({ + color: 0xe0e0ff, + wireframe: true, + }); + + const mesh1 = new THREE.Mesh(geometry, material1); + mesh1.position.set(-40, 0, 0); + + scene.add(mesh1); + + // right + + const material2 = new THREE.ShaderMaterial({ + uniforms: { thickness: { value: API.thickness } }, + vertexShader: document.getElementById('vertexShader').textContent, + fragmentShader: document.getElementById('fragmentShader').textContent, + side: THREE.DoubleSide, + alphaToCoverage: true, // only works when WebGLRenderer's "antialias" is set to "true" + }); + + mesh2 = new THREE.Mesh(geometry, material2); + mesh2.position.set(40, 0, 0); + + scene.add(mesh2); + + // + + render(); + }); + + // + + const gui = new GUI(); + + gui.add(API, 'thickness', 0, 4).onChange(function () { + mesh2.material.uniforms.thickness.value = API.thickness; + render(); + }); + + gui.open(); + + // + + window.addEventListener('resize', onWindowResize); +} + +function setupAttributes(geometry) { + const vectors = [new THREE.Vector3(1, 0, 0), new THREE.Vector3(0, 1, 0), new THREE.Vector3(0, 0, 1)]; + + const position = geometry.attributes.position; + const centers = new Float32Array(position.count * 3); + + for (let i = 0, l = position.count; i < l; i++) { + vectors[i % 3].toArray(centers, i * 3); + } + + geometry.setAttribute('center', new THREE.BufferAttribute(centers, 3)); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_math_obb.ts b/examples-testing/examples/webgl_math_obb.ts new file mode 100644 index 000000000..48480d10b --- /dev/null +++ b/examples-testing/examples/webgl_math_obb.ts @@ -0,0 +1,189 @@ +import * as THREE from 'three'; + +import { OBB } from 'three/addons/math/OBB.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let camera, scene, renderer, clock, controls, stats, raycaster, hitbox; + +const objects = [], + mouse = new THREE.Vector2(); + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 0, 75); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + + clock = new THREE.Clock(); + + raycaster = new THREE.Raycaster(); + + const hemiLight = new THREE.HemisphereLight(0xffffff, 0x222222, 4); + hemiLight.position.set(1, 1, 1); + scene.add(hemiLight); + + const size = new THREE.Vector3(10, 5, 6); + const geometry = new THREE.BoxGeometry(size.x, size.y, size.z); + + // setup OBB on geometry level (doing this manually for now) + + geometry.userData.obb = new OBB(); + geometry.userData.obb.halfSize.copy(size).multiplyScalar(0.5); + + for (let i = 0; i < 100; i++) { + const object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: 0x00ff00 })); + object.matrixAutoUpdate = false; + + object.position.x = Math.random() * 80 - 40; + object.position.y = Math.random() * 80 - 40; + object.position.z = Math.random() * 80 - 40; + + object.rotation.x = Math.random() * 2 * Math.PI; + object.rotation.y = Math.random() * 2 * Math.PI; + object.rotation.z = Math.random() * 2 * Math.PI; + + object.scale.x = Math.random() + 0.5; + object.scale.y = Math.random() + 0.5; + object.scale.z = Math.random() + 0.5; + + scene.add(object); + + // bounding volume on object level (this will reflect the current world transform) + + object.userData.obb = new OBB(); + + objects.push(object); + } + + // + + hitbox = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ color: 0x000000, wireframe: true })); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + controls = new OrbitControls(camera, renderer.domElement); + controls.enableDamping = true; + + // + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); + + document.addEventListener('click', onClick); +} + +function onClick(event) { + event.preventDefault(); + + mouse.x = (event.clientX / window.innerWidth) * 2 - 1; + mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; + + raycaster.setFromCamera(mouse, camera); + + const intersectionPoint = new THREE.Vector3(); + const intersections = []; + + for (let i = 0, il = objects.length; i < il; i++) { + const object = objects[i]; + const obb = object.userData.obb; + + const ray = raycaster.ray; + + if (obb.intersectRay(ray, intersectionPoint) !== null) { + const distance = ray.origin.distanceTo(intersectionPoint); + intersections.push({ distance: distance, object: object }); + } + } + + if (intersections.length > 0) { + // determine closest intersection and highlight the respective 3D object + + intersections.sort(sortIntersections); + + intersections[0].object.add(hitbox); + } else { + const parent = hitbox.parent; + + if (parent) parent.remove(hitbox); + } +} + +function sortIntersections(a, b) { + return a.distance - b.distance; +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + controls.update(); + + // transform cubes + + const delta = clock.getDelta(); + + for (let i = 0, il = objects.length; i < il; i++) { + const object = objects[i]; + + object.rotation.x += delta * Math.PI * 0.2; + object.rotation.y += delta * Math.PI * 0.1; + + object.updateMatrix(); + object.updateMatrixWorld(); + + // update OBB + + object.userData.obb.copy(object.geometry.userData.obb); + object.userData.obb.applyMatrix4(object.matrixWorld); + + // reset + + object.material.color.setHex(0x00ff00); + } + + // collision detection + + for (let i = 0, il = objects.length; i < il; i++) { + const object = objects[i]; + const obb = object.userData.obb; + + for (let j = i + 1, jl = objects.length; j < jl; j++) { + const objectToTest = objects[j]; + const obbToTest = objectToTest.userData.obb; + + // now perform intersection test + + if (obb.intersectsOBB(obbToTest) === true) { + object.material.color.setHex(0xff0000); + objectToTest.material.color.setHex(0xff0000); + } + } + } + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_math_orientation_transform.ts b/examples-testing/examples/webgl_math_orientation_transform.ts new file mode 100644 index 000000000..99be247d8 --- /dev/null +++ b/examples-testing/examples/webgl_math_orientation_transform.ts @@ -0,0 +1,95 @@ +import * as THREE from 'three'; + +let camera, scene, renderer, mesh, target; + +const spherical = new THREE.Spherical(); +const rotationMatrix = new THREE.Matrix4(); +const targetQuaternion = new THREE.Quaternion(); +const clock = new THREE.Clock(); +const speed = 2; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10); + camera.position.z = 5; + + scene = new THREE.Scene(); + + const geometry = new THREE.ConeGeometry(0.1, 0.5, 8); + geometry.rotateX(Math.PI * 0.5); + const material = new THREE.MeshNormalMaterial(); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + const targetGeometry = new THREE.SphereGeometry(0.05); + const targetMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 }); + target = new THREE.Mesh(targetGeometry, targetMaterial); + scene.add(target); + + // + + const sphereGeometry = new THREE.SphereGeometry(2, 32, 32); + const sphereMaterial = new THREE.MeshBasicMaterial({ + color: 0xcccccc, + wireframe: true, + transparent: true, + opacity: 0.3, + }); + const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); + scene.add(sphere); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + window.addEventListener('resize', onWindowResize); + + // + + generateTarget(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const delta = clock.getDelta(); + + if (!mesh.quaternion.equals(targetQuaternion)) { + const step = speed * delta; + mesh.quaternion.rotateTowards(targetQuaternion, step); + } + + renderer.render(scene, camera); +} + +function generateTarget() { + // generate a random point on a sphere + + spherical.theta = Math.random() * Math.PI * 2; + spherical.phi = Math.acos(2 * Math.random() - 1); + spherical.radius = 2; + + target.position.setFromSpherical(spherical); + + // compute target rotation + + rotationMatrix.lookAt(target.position, mesh.position, mesh.up); + targetQuaternion.setFromRotationMatrix(rotationMatrix); + + setTimeout(generateTarget, 2000); +} diff --git a/examples-testing/examples/webgl_mesh_batch.ts b/examples-testing/examples/webgl_mesh_batch.ts new file mode 100644 index 000000000..f93e5fb85 --- /dev/null +++ b/examples-testing/examples/webgl_mesh_batch.ts @@ -0,0 +1,305 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { radixSort } from 'three/addons/utils/SortUtils.js'; + +let stats, gui, guiStatsEl; +let camera, controls, scene, renderer; +let geometries, mesh, material; +const ids = []; +const matrix = new THREE.Matrix4(); + +// + +const position = new THREE.Vector3(); +const rotation = new THREE.Euler(); +const quaternion = new THREE.Quaternion(); +const scale = new THREE.Vector3(); + +// + +const MAX_GEOMETRY_COUNT = 20000; + +const Method = { + BATCHED: 'BATCHED', + NAIVE: 'NAIVE', +}; + +const api = { + method: Method.BATCHED, + count: 256, + dynamic: 16, + + sortObjects: true, + perObjectFrustumCulled: true, + opacity: 1, + useCustomSort: true, +}; + +init(); +initGeometries(); +initMesh(); + +// + +function randomizeMatrix(matrix) { + position.x = Math.random() * 40 - 20; + position.y = Math.random() * 40 - 20; + position.z = Math.random() * 40 - 20; + + rotation.x = Math.random() * 2 * Math.PI; + rotation.y = Math.random() * 2 * Math.PI; + rotation.z = Math.random() * 2 * Math.PI; + + quaternion.setFromEuler(rotation); + + scale.x = scale.y = scale.z = 0.5 + Math.random() * 0.5; + + return matrix.compose(position, quaternion, scale); +} + +function randomizeRotationSpeed(rotation) { + rotation.x = Math.random() * 0.01; + rotation.y = Math.random() * 0.01; + rotation.z = Math.random() * 0.01; + return rotation; +} + +function initGeometries() { + geometries = [ + new THREE.ConeGeometry(1.0, 2.0), + new THREE.BoxGeometry(2.0, 2.0, 2.0), + new THREE.SphereGeometry(1.0, 16, 8), + ]; +} + +function createMaterial() { + if (!material) { + material = new THREE.MeshNormalMaterial(); + } + + return material; +} + +function cleanup() { + if (mesh) { + mesh.parent.remove(mesh); + + if (mesh.dispose) { + mesh.dispose(); + } + } +} + +function initMesh() { + cleanup(); + + if (api.method === Method.BATCHED) { + initBatchedMesh(); + } else { + initRegularMesh(); + } +} + +function initRegularMesh() { + mesh = new THREE.Group(); + const material = createMaterial(); + + for (let i = 0; i < api.count; i++) { + const child = new THREE.Mesh(geometries[i % geometries.length], material); + randomizeMatrix(child.matrix); + child.matrix.decompose(child.position, child.quaternion, child.scale); + child.userData.rotationSpeed = randomizeRotationSpeed(new THREE.Euler()); + mesh.add(child); + } + + scene.add(mesh); +} + +function initBatchedMesh() { + const geometryCount = api.count; + const vertexCount = geometries.length * 512; + const indexCount = geometries.length * 1024; + + const euler = new THREE.Euler(); + const matrix = new THREE.Matrix4(); + mesh = new THREE.BatchedMesh(geometryCount, vertexCount, indexCount, createMaterial()); + mesh.userData.rotationSpeeds = []; + + // disable full-object frustum culling since all of the objects can be dynamic. + mesh.frustumCulled = false; + + ids.length = 0; + + const geometryIds = [ + mesh.addGeometry(geometries[0]), + mesh.addGeometry(geometries[1]), + mesh.addGeometry(geometries[2]), + ]; + + for (let i = 0; i < api.count; i++) { + const id = mesh.addInstance(geometryIds[i % geometryIds.length]); + mesh.setMatrixAt(id, randomizeMatrix(matrix)); + + const rotationMatrix = new THREE.Matrix4(); + rotationMatrix.makeRotationFromEuler(randomizeRotationSpeed(euler)); + mesh.userData.rotationSpeeds.push(rotationMatrix); + + ids.push(id); + } + + scene.add(mesh); +} + +function init() { + const width = window.innerWidth; + const height = window.innerHeight; + + // camera + + camera = new THREE.PerspectiveCamera(70, width / height, 1, 100); + camera.position.z = 30; + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(width, height); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // scene + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + + // controls + + controls = new OrbitControls(camera, renderer.domElement); + controls.autoRotate = true; + controls.autoRotateSpeed = 1.0; + + // stats + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // gui + + gui = new GUI(); + gui.add(api, 'count', 1, MAX_GEOMETRY_COUNT).step(1).onChange(initMesh); + gui.add(api, 'dynamic', 0, MAX_GEOMETRY_COUNT).step(1); + gui.add(api, 'method', Method).onChange(initMesh); + gui.add(api, 'opacity', 0, 1).onChange(v => { + if (v < 1) { + material.transparent = true; + material.depthWrite = false; + } else { + material.transparent = false; + material.depthWrite = true; + } + + material.opacity = v; + material.needsUpdate = true; + }); + gui.add(api, 'sortObjects'); + gui.add(api, 'perObjectFrustumCulled'); + gui.add(api, 'useCustomSort'); + + guiStatsEl = document.createElement('li'); + guiStatsEl.classList.add('gui-stats'); + + // listeners + + window.addEventListener('resize', onWindowResize); +} + +// + +function sortFunction(list) { + // initialize options + this._options = this._options || { + get: el => el.z, + aux: new Array(this.maxInstanceCount), + }; + + const options = this._options; + options.reversed = this.material.transparent; + + let minZ = Infinity; + let maxZ = -Infinity; + for (let i = 0, l = list.length; i < l; i++) { + const z = list[i].z; + if (z > maxZ) maxZ = z; + if (z < minZ) minZ = z; + } + + // convert depth to unsigned 32 bit range + const depthDelta = maxZ - minZ; + const factor = (2 ** 32 - 1) / depthDelta; // UINT32_MAX / z range + for (let i = 0, l = list.length; i < l; i++) { + list[i].z -= minZ; + list[i].z *= factor; + } + + // perform a fast-sort using the hybrid radix sort function + radixSort(list, options); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); +} + +function animate() { + animateMeshes(); + + controls.update(); + stats.update(); + + render(); +} + +function animateMeshes() { + const loopNum = Math.min(api.count, api.dynamic); + + if (api.method === Method.BATCHED) { + for (let i = 0; i < loopNum; i++) { + const rotationMatrix = mesh.userData.rotationSpeeds[i]; + const id = ids[i]; + + mesh.getMatrixAt(id, matrix); + matrix.multiply(rotationMatrix); + mesh.setMatrixAt(id, matrix); + } + } else { + for (let i = 0; i < loopNum; i++) { + const child = mesh.children[i]; + const rotationSpeed = child.userData.rotationSpeed; + + child.rotation.set( + child.rotation.x + rotationSpeed.x, + child.rotation.y + rotationSpeed.y, + child.rotation.z + rotationSpeed.z, + ); + } + } +} + +function render() { + if (mesh.isBatchedMesh) { + mesh.sortObjects = api.sortObjects; + mesh.perObjectFrustumCulled = api.perObjectFrustumCulled; + mesh.setCustomSort(api.useCustomSort ? sortFunction : null); + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_mirror.ts b/examples-testing/examples/webgl_mirror.ts new file mode 100644 index 000000000..8b27363a8 --- /dev/null +++ b/examples-testing/examples/webgl_mirror.ts @@ -0,0 +1,168 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { Reflector } from 'three/addons/objects/Reflector.js'; + +let camera, scene, renderer; + +let cameraControls; + +let sphereGroup, smallSphere; + +let groundMirror, verticalMirror; + +init(); + +function init() { + const container = document.getElementById('container'); + + // renderer + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // scene + scene = new THREE.Scene(); + + // camera + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 500); + camera.position.set(0, 75, 160); + + cameraControls = new OrbitControls(camera, renderer.domElement); + cameraControls.target.set(0, 40, 0); + cameraControls.maxDistance = 400; + cameraControls.minDistance = 10; + cameraControls.update(); + + // + + const planeGeo = new THREE.PlaneGeometry(100.1, 100.1); + + // reflectors/mirrors + + let geometry, material; + + geometry = new THREE.CircleGeometry(40, 64); + groundMirror = new Reflector(geometry, { + clipBias: 0.003, + textureWidth: window.innerWidth * window.devicePixelRatio, + textureHeight: window.innerHeight * window.devicePixelRatio, + color: 0xb5b5b5, + }); + groundMirror.position.y = 0.5; + groundMirror.rotateX(-Math.PI / 2); + scene.add(groundMirror); + + geometry = new THREE.PlaneGeometry(100, 100); + verticalMirror = new Reflector(geometry, { + clipBias: 0.003, + textureWidth: window.innerWidth * window.devicePixelRatio, + textureHeight: window.innerHeight * window.devicePixelRatio, + color: 0xc1cbcb, + }); + verticalMirror.position.y = 50; + verticalMirror.position.z = -50; + scene.add(verticalMirror); + + sphereGroup = new THREE.Object3D(); + scene.add(sphereGroup); + + geometry = new THREE.CylinderGeometry(0.1, 15 * Math.cos((Math.PI / 180) * 30), 0.1, 24, 1); + material = new THREE.MeshPhongMaterial({ color: 0xffffff, emissive: 0x8d8d8d }); + const sphereCap = new THREE.Mesh(geometry, material); + sphereCap.position.y = -15 * Math.sin((Math.PI / 180) * 30) - 0.05; + sphereCap.rotateX(-Math.PI); + + geometry = new THREE.SphereGeometry(15, 24, 24, Math.PI / 2, Math.PI * 2, 0, (Math.PI / 180) * 120); + const halfSphere = new THREE.Mesh(geometry, material); + halfSphere.add(sphereCap); + halfSphere.rotateX((-Math.PI / 180) * 135); + halfSphere.rotateZ((-Math.PI / 180) * 20); + halfSphere.position.y = 7.5 + 15 * Math.sin((Math.PI / 180) * 30); + + sphereGroup.add(halfSphere); + + geometry = new THREE.IcosahedronGeometry(5, 0); + material = new THREE.MeshPhongMaterial({ color: 0xffffff, emissive: 0x7b7b7b, flatShading: true }); + smallSphere = new THREE.Mesh(geometry, material); + scene.add(smallSphere); + + // walls + const planeTop = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); + planeTop.position.y = 100; + planeTop.rotateX(Math.PI / 2); + scene.add(planeTop); + + const planeBottom = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); + planeBottom.rotateX(-Math.PI / 2); + scene.add(planeBottom); + + const planeFront = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x7f7fff })); + planeFront.position.z = 50; + planeFront.position.y = 50; + planeFront.rotateY(Math.PI); + scene.add(planeFront); + + const planeRight = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x00ff00 })); + planeRight.position.x = 50; + planeRight.position.y = 50; + planeRight.rotateY(-Math.PI / 2); + scene.add(planeRight); + + const planeLeft = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xff0000 })); + planeLeft.position.x = -50; + planeLeft.position.y = 50; + planeLeft.rotateY(Math.PI / 2); + scene.add(planeLeft); + + // lights + const mainLight = new THREE.PointLight(0xe7e7e7, 2.5, 250, 0); + mainLight.position.y = 60; + scene.add(mainLight); + + const greenLight = new THREE.PointLight(0x00ff00, 0.5, 1000, 0); + greenLight.position.set(550, 50, 0); + scene.add(greenLight); + + const redLight = new THREE.PointLight(0xff0000, 0.5, 1000, 0); + redLight.position.set(-550, 50, 0); + scene.add(redLight); + + const blueLight = new THREE.PointLight(0xbbbbfe, 0.5, 1000, 0); + blueLight.position.set(0, 50, 550); + scene.add(blueLight); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + groundMirror + .getRenderTarget() + .setSize(window.innerWidth * window.devicePixelRatio, window.innerHeight * window.devicePixelRatio); + verticalMirror + .getRenderTarget() + .setSize(window.innerWidth * window.devicePixelRatio, window.innerHeight * window.devicePixelRatio); +} + +function animate() { + const timer = Date.now() * 0.01; + + sphereGroup.rotation.y -= 0.002; + + smallSphere.position.set( + Math.cos(timer * 0.1) * 30, + Math.abs(Math.cos(timer * 0.2)) * 20 + 5, + Math.sin(timer * 0.1) * 30, + ); + smallSphere.rotation.y = Math.PI / 2 - timer * 0.1; + smallSphere.rotation.z = timer * 0.8; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_modifier_edgesplit.ts b/examples-testing/examples/webgl_modifier_edgesplit.ts new file mode 100644 index 000000000..4725eff62 --- /dev/null +++ b/examples-testing/examples/webgl_modifier_edgesplit.ts @@ -0,0 +1,136 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; +import { EdgeSplitModifier } from 'three/addons/modifiers/EdgeSplitModifier.js'; +import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let renderer, scene, camera; +let modifier, mesh, baseGeometry; +let map; + +const params = { + smoothShading: true, + edgeSplit: true, + cutOffAngle: 20, + showMap: false, + tryKeepNormals: true, +}; + +init(); + +function init() { + const info = document.createElement('div'); + info.style.position = 'absolute'; + info.style.top = '10px'; + info.style.width = '100%'; + info.style.textAlign = 'center'; + info.innerHTML = 'three.js - Edge Split modifier'; + document.body.appendChild(info); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.enableDamping = true; + controls.dampingFactor = 0.25; + controls.rotateSpeed = 0.35; + controls.minZoom = 1; + camera.position.set(0, 0, 4); + + scene.add(new THREE.HemisphereLight(0xffffff, 0x444444, 3)); + + new OBJLoader().load('./models/obj/cerberus/Cerberus.obj', function (group) { + const cerberus = group.children[0]; + const modelGeometry = cerberus.geometry; + + modifier = new EdgeSplitModifier(); + baseGeometry = BufferGeometryUtils.mergeVertices(modelGeometry); + + mesh = new THREE.Mesh(getGeometry(), new THREE.MeshStandardMaterial()); + mesh.material.flatShading = !params.smoothShading; + mesh.rotateY(-Math.PI / 2); + mesh.scale.set(3.5, 3.5, 3.5); + mesh.translateZ(1.5); + scene.add(mesh); + + if (map !== undefined && params.showMap) { + mesh.material.map = map; + mesh.material.needsUpdate = true; + } + + render(); + }); + + window.addEventListener('resize', onWindowResize); + + new THREE.TextureLoader().load('./models/obj/cerberus/Cerberus_A.jpg', function (texture) { + map = texture; + map.colorSpace = THREE.SRGBColorSpace; + + if (mesh !== undefined && params.showMap) { + mesh.material.map = map; + mesh.material.needsUpdate = true; + } + }); + + const gui = new GUI({ title: 'Edge split modifier parameters' }); + + gui.add(params, 'showMap').onFinishChange(updateMesh); + gui.add(params, 'smoothShading').onFinishChange(updateMesh); + gui.add(params, 'edgeSplit').onFinishChange(updateMesh); + gui.add(params, 'cutOffAngle').min(0).max(180).onFinishChange(updateMesh); + gui.add(params, 'tryKeepNormals').onFinishChange(updateMesh); +} + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + render(); +} + +function getGeometry() { + let geometry; + + if (params.edgeSplit) { + geometry = modifier.modify(baseGeometry, (params.cutOffAngle * Math.PI) / 180, params.tryKeepNormals); + } else { + geometry = baseGeometry; + } + + return geometry; +} + +function updateMesh() { + if (mesh !== undefined) { + mesh.geometry = getGeometry(); + + let needsUpdate = mesh.material.flatShading === params.smoothShading; + mesh.material.flatShading = params.smoothShading === false; + + if (map !== undefined) { + needsUpdate = needsUpdate || mesh.material.map !== (params.showMap ? map : null); + mesh.material.map = params.showMap ? map : null; + } + + mesh.material.needsUpdate = needsUpdate; + + render(); + } +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_modifier_simplifier.ts b/examples-testing/examples/webgl_modifier_simplifier.ts new file mode 100644 index 000000000..e6ea453b3 --- /dev/null +++ b/examples-testing/examples/webgl_modifier_simplifier.ts @@ -0,0 +1,77 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { SimplifyModifier } from 'three/addons/modifiers/SimplifyModifier.js'; + +let renderer, scene, camera; + +init(); + +function init() { + const info = document.createElement('div'); + info.style.position = 'absolute'; + info.style.top = '10px'; + info.style.width = '100%'; + info.style.textAlign = 'center'; + info.innerHTML = + 'three.js - Vertex Reduction using SimplifyModifier'; + document.body.appendChild(info); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 15; + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.enablePan = false; + controls.enableZoom = false; + + scene.add(new THREE.AmbientLight(0xffffff, 0.6)); + + const light = new THREE.PointLight(0xffffff, 400); + camera.add(light); + scene.add(camera); + + new GLTFLoader().load('models/gltf/LeePerrySmith/LeePerrySmith.glb', function (gltf) { + const mesh = gltf.scene.children[0]; + mesh.position.x = -3; + mesh.rotation.y = Math.PI / 2; + scene.add(mesh); + + const modifier = new SimplifyModifier(); + + const simplified = mesh.clone(); + simplified.material = simplified.material.clone(); + simplified.material.flatShading = true; + const count = Math.floor(simplified.geometry.attributes.position.count * 0.875); // number of vertices to remove + simplified.geometry = modifier.modify(simplified.geometry, count); + + simplified.position.x = 3; + simplified.rotation.y = -Math.PI / 2; + scene.add(simplified); + + render(); + }); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_modifier_tessellation.ts b/examples-testing/examples/webgl_modifier_tessellation.ts new file mode 100644 index 000000000..4600fc6cb --- /dev/null +++ b/examples-testing/examples/webgl_modifier_tessellation.ts @@ -0,0 +1,142 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; +import { TessellateModifier } from 'three/addons/modifiers/TessellateModifier.js'; +import { FontLoader } from 'three/addons/loaders/FontLoader.js'; +import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; + +let renderer, scene, camera, stats; + +let controls; + +let mesh, uniforms; + +const WIDTH = window.innerWidth; +const HEIGHT = window.innerHeight; + +const loader = new FontLoader(); +loader.load('fonts/helvetiker_bold.typeface.json', function (font) { + init(font); +}); + +function init(font) { + camera = new THREE.PerspectiveCamera(40, WIDTH / HEIGHT, 1, 10000); + camera.position.set(-100, 100, 200); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x050505); + + // + + let geometry = new TextGeometry('THREE.JS', { + font: font, + + size: 40, + depth: 5, + curveSegments: 3, + + bevelThickness: 2, + bevelSize: 1, + bevelEnabled: true, + }); + + geometry.center(); + + const tessellateModifier = new TessellateModifier(8, 6); + + geometry = tessellateModifier.modify(geometry); + + // + + const numFaces = geometry.attributes.position.count / 3; + + const colors = new Float32Array(numFaces * 3 * 3); + const displacement = new Float32Array(numFaces * 3 * 3); + + const color = new THREE.Color(); + + for (let f = 0; f < numFaces; f++) { + const index = 9 * f; + + const h = 0.2 * Math.random(); + const s = 0.5 + 0.5 * Math.random(); + const l = 0.5 + 0.5 * Math.random(); + + color.setHSL(h, s, l); + + const d = 10 * (0.5 - Math.random()); + + for (let i = 0; i < 3; i++) { + colors[index + 3 * i] = color.r; + colors[index + 3 * i + 1] = color.g; + colors[index + 3 * i + 2] = color.b; + + displacement[index + 3 * i] = d; + displacement[index + 3 * i + 1] = d; + displacement[index + 3 * i + 2] = d; + } + } + + geometry.setAttribute('customColor', new THREE.BufferAttribute(colors, 3)); + geometry.setAttribute('displacement', new THREE.BufferAttribute(displacement, 3)); + + // + + uniforms = { + amplitude: { value: 0.0 }, + }; + + const shaderMaterial = new THREE.ShaderMaterial({ + uniforms: uniforms, + vertexShader: document.getElementById('vertexshader').textContent, + fragmentShader: document.getElementById('fragmentshader').textContent, + }); + + // + + mesh = new THREE.Mesh(geometry, shaderMaterial); + + scene.add(mesh); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(WIDTH, HEIGHT); + renderer.setAnimationLoop(animate); + + const container = document.getElementById('container'); + container.appendChild(renderer.domElement); + + controls = new TrackballControls(camera, renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + render(); + + stats.update(); +} + +function render() { + const time = Date.now() * 0.001; + + uniforms.amplitude.value = 1.0 + Math.sin(time * 0.5); + + controls.update(); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_morphtargets.ts b/examples-testing/examples/webgl_morphtargets.ts new file mode 100644 index 000000000..40d605f8d --- /dev/null +++ b/examples-testing/examples/webgl_morphtargets.ts @@ -0,0 +1,120 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let container, camera, scene, renderer, mesh; + +init(); + +function init() { + container = document.getElementById('container'); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x8fbcd4); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 20); + camera.position.z = 10; + scene.add(camera); + + scene.add(new THREE.AmbientLight(0x8fbcd4, 1.5)); + + const pointLight = new THREE.PointLight(0xffffff, 200); + camera.add(pointLight); + + const geometry = createGeometry(); + + const material = new THREE.MeshPhongMaterial({ + color: 0xff0000, + flatShading: true, + }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + initGUI(); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(function () { + renderer.render(scene, camera); + }); + container.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.enableZoom = false; + + window.addEventListener('resize', onWindowResize); +} + +function createGeometry() { + const geometry = new THREE.BoxGeometry(2, 2, 2, 32, 32, 32); + + // create an empty array to hold targets for the attribute we want to morph + // morphing positions and normals is supported + geometry.morphAttributes.position = []; + + // the original positions of the cube's vertices + const positionAttribute = geometry.attributes.position; + + // for the first morph target we'll move the cube's vertices onto the surface of a sphere + const spherePositions = []; + + // for the second morph target, we'll twist the cubes vertices + const twistPositions = []; + const direction = new THREE.Vector3(1, 0, 0); + const vertex = new THREE.Vector3(); + + for (let i = 0; i < positionAttribute.count; i++) { + const x = positionAttribute.getX(i); + const y = positionAttribute.getY(i); + const z = positionAttribute.getZ(i); + + spherePositions.push( + x * Math.sqrt(1 - (y * y) / 2 - (z * z) / 2 + (y * y * z * z) / 3), + y * Math.sqrt(1 - (z * z) / 2 - (x * x) / 2 + (z * z * x * x) / 3), + z * Math.sqrt(1 - (x * x) / 2 - (y * y) / 2 + (x * x * y * y) / 3), + ); + + // stretch along the x-axis so we can see the twist better + vertex.set(x * 2, y, z); + + vertex.applyAxisAngle(direction, (Math.PI * x) / 2).toArray(twistPositions, twistPositions.length); + } + + // add the spherical positions as the first morph target + geometry.morphAttributes.position[0] = new THREE.Float32BufferAttribute(spherePositions, 3); + + // add the twisted positions as the second morph target + geometry.morphAttributes.position[1] = new THREE.Float32BufferAttribute(twistPositions, 3); + + return geometry; +} + +function initGUI() { + // Set up dat.GUI to control targets + const params = { + Spherify: 0, + Twist: 0, + }; + const gui = new GUI({ title: 'Morph Targets' }); + + gui.add(params, 'Spherify', 0, 1) + .step(0.01) + .onChange(function (value) { + mesh.morphTargetInfluences[0] = value; + }); + gui.add(params, 'Twist', 0, 1) + .step(0.01) + .onChange(function (value) { + mesh.morphTargetInfluences[1] = value; + }); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} diff --git a/examples-testing/examples/webgl_morphtargets_face.ts b/examples-testing/examples/webgl_morphtargets_face.ts new file mode 100644 index 000000000..76179d902 --- /dev/null +++ b/examples-testing/examples/webgl_morphtargets_face.ts @@ -0,0 +1,105 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js'; +import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js'; + +import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, stats, mixer, clock, controls; + +init(); + +function init() { + clock = new THREE.Clock(); + + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 20); + camera.position.set(-1.8, 0.8, 3); + + scene = new THREE.Scene(); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + + container.appendChild(renderer.domElement); + + const ktx2Loader = new KTX2Loader().setTranscoderPath('jsm/libs/basis/').detectSupport(renderer); + + new GLTFLoader() + .setKTX2Loader(ktx2Loader) + .setMeshoptDecoder(MeshoptDecoder) + .load('models/gltf/facecap.glb', gltf => { + const mesh = gltf.scene.children[0]; + + scene.add(mesh); + + mixer = new THREE.AnimationMixer(mesh); + + mixer.clipAction(gltf.animations[0]).play(); + + // GUI + + const head = mesh.getObjectByName('mesh_2'); + const influences = head.morphTargetInfluences; + + const gui = new GUI(); + gui.close(); + + for (const [key, value] of Object.entries(head.morphTargetDictionary)) { + gui.add(influences, value, 0, 1, 0.01).name(key.replace('blendShape1.', '')).listen(); + } + }); + + const environment = new RoomEnvironment(); + const pmremGenerator = new THREE.PMREMGenerator(renderer); + + scene.background = new THREE.Color(0x666666); + scene.environment = pmremGenerator.fromScene(environment).texture; + + controls = new OrbitControls(camera, renderer.domElement); + controls.enableDamping = true; + controls.minDistance = 2.5; + controls.maxDistance = 5; + controls.minAzimuthAngle = -Math.PI / 2; + controls.maxAzimuthAngle = Math.PI / 2; + controls.maxPolarAngle = Math.PI / 1.8; + controls.target.set(0, 0.15, -0.2); + + stats = new Stats(); + container.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const delta = clock.getDelta(); + + if (mixer) { + mixer.update(delta); + } + + renderer.render(scene, camera); + + controls.update(); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_morphtargets_horse.ts b/examples-testing/examples/webgl_morphtargets_horse.ts new file mode 100644 index 000000000..2c29e9c0e --- /dev/null +++ b/examples-testing/examples/webgl_morphtargets_horse.ts @@ -0,0 +1,100 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +let container, stats; +let camera, scene, renderer; +let mesh, mixer; + +const radius = 600; +let theta = 0; +let prevTime = Date.now(); + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + // + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.y = 300; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xf0f0f0); + + // + + const light1 = new THREE.DirectionalLight(0xefefff, 5); + light1.position.set(1, 1, 1).normalize(); + scene.add(light1); + + const light2 = new THREE.DirectionalLight(0xffefef, 5); + light2.position.set(-1, -1, -1).normalize(); + scene.add(light2); + + const loader = new GLTFLoader(); + loader.load('models/gltf/Horse.glb', function (gltf) { + mesh = gltf.scene.children[0]; + mesh.scale.set(1.5, 1.5, 1.5); + scene.add(mesh); + + mixer = new THREE.AnimationMixer(mesh); + + mixer.clipAction(gltf.animations[0]).setDuration(1).play(); + }); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + + container.appendChild(renderer.domElement); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + theta += 0.1; + + camera.position.x = radius * Math.sin(THREE.MathUtils.degToRad(theta)); + camera.position.z = radius * Math.cos(THREE.MathUtils.degToRad(theta)); + + camera.lookAt(0, 150, 0); + + if (mixer) { + const time = Date.now(); + + mixer.update((time - prevTime) * 0.001); + + prevTime = time; + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_morphtargets_sphere.ts b/examples-testing/examples/webgl_morphtargets_sphere.ts new file mode 100644 index 000000000..2b8899111 --- /dev/null +++ b/examples-testing/examples/webgl_morphtargets_sphere.ts @@ -0,0 +1,105 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { Timer } from 'three/addons/misc/Timer.js'; + +let camera, scene, renderer, timer; + +let mesh; + +let sign = 1; +const speed = 0.5; + +init(); + +function init() { + const container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.2, 100); + camera.position.set(0, 5, 5); + + scene = new THREE.Scene(); + + timer = new Timer(); + + const light1 = new THREE.PointLight(0xff2200, 50000); + light1.position.set(100, 100, 100); + scene.add(light1); + + const light2 = new THREE.PointLight(0x22ff00, 10000); + light2.position.set(-100, -100, -100); + scene.add(light2); + + scene.add(new THREE.AmbientLight(0x111111)); + + const loader = new GLTFLoader(); + loader.load('models/gltf/AnimatedMorphSphere/glTF/AnimatedMorphSphere.gltf', function (gltf) { + mesh = gltf.scene.getObjectByName('AnimatedMorphSphere'); + mesh.rotation.z = Math.PI / 2; + scene.add(mesh); + + // + + const pointsMaterial = new THREE.PointsMaterial({ + size: 10, + sizeAttenuation: false, + map: new THREE.TextureLoader().load('textures/sprites/disc.png'), + alphaTest: 0.5, + }); + + const points = new THREE.Points(mesh.geometry, pointsMaterial); + points.morphTargetInfluences = mesh.morphTargetInfluences; + points.morphTargetDictionary = mesh.morphTargetDictionary; + mesh.add(points); + }); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + + container.appendChild(renderer.domElement); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 1; + controls.maxDistance = 20; + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + timer.update(); + render(); +} + +function render() { + const delta = timer.getDelta(); + + if (mesh !== undefined) { + const step = delta * speed; + + mesh.rotation.y += step; + + mesh.morphTargetInfluences[1] = mesh.morphTargetInfluences[1] + step * sign; + + if (mesh.morphTargetInfluences[1] <= 0 || mesh.morphTargetInfluences[1] >= 1) { + sign *= -1; + } + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_multiple_elements.ts b/examples-testing/examples/webgl_multiple_elements.ts new file mode 100644 index 000000000..64f8a9c5f --- /dev/null +++ b/examples-testing/examples/webgl_multiple_elements.ts @@ -0,0 +1,139 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let canvas, renderer; + +const scenes = []; + +init(); + +function init() { + canvas = document.getElementById('c'); + + const geometries = [ + new THREE.BoxGeometry(1, 1, 1), + new THREE.SphereGeometry(0.5, 12, 8), + new THREE.DodecahedronGeometry(0.5), + new THREE.CylinderGeometry(0.5, 0.5, 1, 12), + ]; + + const content = document.getElementById('content'); + + for (let i = 0; i < 40; i++) { + const scene = new THREE.Scene(); + + // make a list item + const element = document.createElement('div'); + element.className = 'list-item'; + + const sceneElement = document.createElement('div'); + element.appendChild(sceneElement); + + const descriptionElement = document.createElement('div'); + descriptionElement.innerText = 'Scene ' + (i + 1); + element.appendChild(descriptionElement); + + // the element that represents the area we want to render the scene + scene.userData.element = sceneElement; + content.appendChild(element); + + const camera = new THREE.PerspectiveCamera(50, 1, 1, 10); + camera.position.z = 2; + scene.userData.camera = camera; + + const controls = new OrbitControls(scene.userData.camera, scene.userData.element); + controls.minDistance = 2; + controls.maxDistance = 5; + controls.enablePan = false; + controls.enableZoom = false; + scene.userData.controls = controls; + + // add one random mesh to each scene + const geometry = geometries[(geometries.length * Math.random()) | 0]; + + const material = new THREE.MeshStandardMaterial({ + color: new THREE.Color().setHSL(Math.random(), 1, 0.75, THREE.SRGBColorSpace), + roughness: 0.5, + metalness: 0, + flatShading: true, + }); + + scene.add(new THREE.Mesh(geometry, material)); + + scene.add(new THREE.HemisphereLight(0xaaaaaa, 0x444444, 3)); + + const light = new THREE.DirectionalLight(0xffffff, 1.5); + light.position.set(1, 1, 1); + scene.add(light); + + scenes.push(scene); + } + + renderer = new THREE.WebGLRenderer({ canvas: canvas, antialias: true }); + renderer.setClearColor(0xffffff, 1); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setAnimationLoop(animate); +} + +function updateSize() { + const width = canvas.clientWidth; + const height = canvas.clientHeight; + + if (canvas.width !== width || canvas.height !== height) { + renderer.setSize(width, height, false); + } +} + +function animate() { + updateSize(); + + canvas.style.transform = `translateY(${window.scrollY}px)`; + + renderer.setClearColor(0xffffff); + renderer.setScissorTest(false); + renderer.clear(); + + renderer.setClearColor(0xe0e0e0); + renderer.setScissorTest(true); + + scenes.forEach(function (scene) { + // so something moves + scene.children[0].rotation.y = Date.now() * 0.001; + + // get the element that is a place holder for where we want to + // draw the scene + const element = scene.userData.element; + + // get its position relative to the page's viewport + const rect = element.getBoundingClientRect(); + + // check if it's offscreen. If so skip it + if ( + rect.bottom < 0 || + rect.top > renderer.domElement.clientHeight || + rect.right < 0 || + rect.left > renderer.domElement.clientWidth + ) { + return; // it's off screen + } + + // set the viewport + const width = rect.right - rect.left; + const height = rect.bottom - rect.top; + const left = rect.left; + const bottom = renderer.domElement.clientHeight - rect.bottom; + + renderer.setViewport(left, bottom, width, height); + renderer.setScissor(left, bottom, width, height); + + const camera = scene.userData.camera; + + //camera.aspect = width / height; // not changing in this example + //camera.updateProjectionMatrix(); + + //scene.userData.controls.update(); + + renderer.render(scene, camera); + }); +} diff --git a/examples-testing/examples/webgl_multiple_rendertargets.ts b/examples-testing/examples/webgl_multiple_rendertargets.ts new file mode 100644 index 000000000..86708082b --- /dev/null +++ b/examples-testing/examples/webgl_multiple_rendertargets.ts @@ -0,0 +1,133 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, controls; +let renderTarget; +let postScene, postCamera; + +const parameters = { + samples: 4, + wireframe: false, +}; + +const gui = new GUI(); +gui.add(parameters, 'samples', 0, 4).step(1); +gui.add(parameters, 'wireframe'); +gui.onChange(render); + +init(); + +function init() { + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + // Create a multi render target with Float buffers + + renderTarget = new THREE.WebGLRenderTarget( + window.innerWidth * window.devicePixelRatio, + window.innerHeight * window.devicePixelRatio, + { + count: 2, + minFilter: THREE.NearestFilter, + magFilter: THREE.NearestFilter, + }, + ); + + // Name our G-Buffer attachments for debugging + + renderTarget.textures[0].name = 'diffuse'; + renderTarget.textures[1].name = 'normal'; + + // Scene setup + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x222222); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 50); + camera.position.z = 4; + + const loader = new THREE.TextureLoader(); + + const diffuse = loader.load('textures/hardwood2_diffuse.jpg', render); + diffuse.wrapS = THREE.RepeatWrapping; + diffuse.wrapT = THREE.RepeatWrapping; + diffuse.colorSpace = THREE.SRGBColorSpace; + + scene.add( + new THREE.Mesh( + new THREE.TorusKnotGeometry(1, 0.3, 128, 32), + new THREE.RawShaderMaterial({ + name: 'G-Buffer Shader', + vertexShader: document.querySelector('#gbuffer-vert').textContent.trim(), + fragmentShader: document.querySelector('#gbuffer-frag').textContent.trim(), + uniforms: { + tDiffuse: { value: diffuse }, + repeat: { value: new THREE.Vector2(5, 0.5) }, + }, + glslVersion: THREE.GLSL3, + }), + ), + ); + + // PostProcessing setup + + postScene = new THREE.Scene(); + postCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1); + + postScene.add( + new THREE.Mesh( + new THREE.PlaneGeometry(2, 2), + new THREE.RawShaderMaterial({ + name: 'Post-FX Shader', + vertexShader: document.querySelector('#render-vert').textContent.trim(), + fragmentShader: document.querySelector('#render-frag').textContent.trim(), + uniforms: { + tDiffuse: { value: renderTarget.textures[0] }, + tNormal: { value: renderTarget.textures[1] }, + }, + glslVersion: THREE.GLSL3, + }), + ), + ); + + // Controls + + controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + const dpr = renderer.getPixelRatio(); + renderTarget.setSize(window.innerWidth * dpr, window.innerHeight * dpr); + + render(); +} + +function render() { + renderTarget.samples = parameters.samples; + + scene.traverse(function (child) { + if (child.material !== undefined) { + child.material.wireframe = parameters.wireframe; + } + }); + + // render scene into target + renderer.setRenderTarget(renderTarget); + renderer.render(scene, camera); + + // render post FX + renderer.setRenderTarget(null); + renderer.render(postScene, postCamera); +} diff --git a/examples-testing/examples/webgl_multiple_scenes_comparison.ts b/examples-testing/examples/webgl_multiple_scenes_comparison.ts new file mode 100644 index 000000000..41a5130d4 --- /dev/null +++ b/examples-testing/examples/webgl_multiple_scenes_comparison.ts @@ -0,0 +1,98 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let container, camera, renderer, controls; +let sceneL, sceneR; + +let sliderPos = window.innerWidth / 2; + +init(); + +function init() { + container = document.querySelector('.container'); + + sceneL = new THREE.Scene(); + sceneL.background = new THREE.Color(0xbcd48f); + + sceneR = new THREE.Scene(); + sceneR.background = new THREE.Color(0x8fbcd4); + + camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.z = 6; + + controls = new OrbitControls(camera, container); + + const light = new THREE.HemisphereLight(0xffffff, 0x444444, 3); + light.position.set(-2, 2, 2); + sceneL.add(light.clone()); + sceneR.add(light.clone()); + + initMeshes(); + initSlider(); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setScissorTest(true); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); +} + +function initMeshes() { + const geometry = new THREE.IcosahedronGeometry(1, 3); + + const meshL = new THREE.Mesh(geometry, new THREE.MeshStandardMaterial()); + sceneL.add(meshL); + + const meshR = new THREE.Mesh(geometry, new THREE.MeshStandardMaterial({ wireframe: true })); + sceneR.add(meshR); +} + +function initSlider() { + const slider = document.querySelector('.slider'); + + function onPointerDown() { + if (event.isPrimary === false) return; + + controls.enabled = false; + + window.addEventListener('pointermove', onPointerMove); + window.addEventListener('pointerup', onPointerUp); + } + + function onPointerUp() { + controls.enabled = true; + + window.removeEventListener('pointermove', onPointerMove); + window.removeEventListener('pointerup', onPointerUp); + } + + function onPointerMove(e) { + if (event.isPrimary === false) return; + + sliderPos = Math.max(0, Math.min(window.innerWidth, e.pageX)); + + slider.style.left = sliderPos - slider.offsetWidth / 2 + 'px'; + } + + slider.style.touchAction = 'none'; // disable touch scroll + slider.addEventListener('pointerdown', onPointerDown); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.setScissor(0, 0, sliderPos, window.innerHeight); + renderer.render(sceneL, camera); + + renderer.setScissor(sliderPos, 0, window.innerWidth, window.innerHeight); + renderer.render(sceneR, camera); +} diff --git a/examples-testing/examples/webgl_multiple_views.ts b/examples-testing/examples/webgl_multiple_views.ts new file mode 100644 index 000000000..29126b013 --- /dev/null +++ b/examples-testing/examples/webgl_multiple_views.ts @@ -0,0 +1,237 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let stats; + +let scene, renderer; + +let mouseX = 0, + mouseY = 0; + +let windowWidth, windowHeight; + +const views = [ + { + left: 0, + bottom: 0, + width: 0.5, + height: 1.0, + background: new THREE.Color().setRGB(0.5, 0.5, 0.7, THREE.SRGBColorSpace), + eye: [0, 300, 1800], + up: [0, 1, 0], + fov: 30, + updateCamera: function (camera, scene, mouseX) { + camera.position.x += mouseX * 0.05; + camera.position.x = Math.max(Math.min(camera.position.x, 2000), -2000); + camera.lookAt(scene.position); + }, + }, + { + left: 0.5, + bottom: 0, + width: 0.5, + height: 0.5, + background: new THREE.Color().setRGB(0.7, 0.5, 0.5, THREE.SRGBColorSpace), + eye: [0, 1800, 0], + up: [0, 0, 1], + fov: 45, + updateCamera: function (camera, scene, mouseX) { + camera.position.x -= mouseX * 0.05; + camera.position.x = Math.max(Math.min(camera.position.x, 2000), -2000); + camera.lookAt(camera.position.clone().setY(0)); + }, + }, + { + left: 0.5, + bottom: 0.5, + width: 0.5, + height: 0.5, + background: new THREE.Color().setRGB(0.5, 0.7, 0.7, THREE.SRGBColorSpace), + eye: [1400, 800, 1400], + up: [0, 1, 0], + fov: 60, + updateCamera: function (camera, scene, mouseX) { + camera.position.y -= mouseX * 0.05; + camera.position.y = Math.max(Math.min(camera.position.y, 1600), -1600); + camera.lookAt(scene.position); + }, + }, +]; + +init(); + +function init() { + const container = document.getElementById('container'); + + for (let ii = 0; ii < views.length; ++ii) { + const view = views[ii]; + const camera = new THREE.PerspectiveCamera(view.fov, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.fromArray(view.eye); + camera.up.fromArray(view.up); + view.camera = camera; + } + + scene = new THREE.Scene(); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0, 0, 1); + scene.add(light); + + // shadow + + const canvas = document.createElement('canvas'); + canvas.width = 128; + canvas.height = 128; + + const context = canvas.getContext('2d'); + const gradient = context.createRadialGradient( + canvas.width / 2, + canvas.height / 2, + 0, + canvas.width / 2, + canvas.height / 2, + canvas.width / 2, + ); + gradient.addColorStop(0.1, 'rgba(0,0,0,0.15)'); + gradient.addColorStop(1, 'rgba(0,0,0,0)'); + + context.fillStyle = gradient; + context.fillRect(0, 0, canvas.width, canvas.height); + + const shadowTexture = new THREE.CanvasTexture(canvas); + + const shadowMaterial = new THREE.MeshBasicMaterial({ map: shadowTexture, transparent: true }); + const shadowGeo = new THREE.PlaneGeometry(300, 300, 1, 1); + + let shadowMesh; + + shadowMesh = new THREE.Mesh(shadowGeo, shadowMaterial); + shadowMesh.position.y = -250; + shadowMesh.rotation.x = -Math.PI / 2; + scene.add(shadowMesh); + + shadowMesh = new THREE.Mesh(shadowGeo, shadowMaterial); + shadowMesh.position.x = -400; + shadowMesh.position.y = -250; + shadowMesh.rotation.x = -Math.PI / 2; + scene.add(shadowMesh); + + shadowMesh = new THREE.Mesh(shadowGeo, shadowMaterial); + shadowMesh.position.x = 400; + shadowMesh.position.y = -250; + shadowMesh.rotation.x = -Math.PI / 2; + scene.add(shadowMesh); + + const radius = 200; + + const geometry1 = new THREE.IcosahedronGeometry(radius, 1); + + const count = geometry1.attributes.position.count; + geometry1.setAttribute('color', new THREE.BufferAttribute(new Float32Array(count * 3), 3)); + + const geometry2 = geometry1.clone(); + const geometry3 = geometry1.clone(); + + const color = new THREE.Color(); + const positions1 = geometry1.attributes.position; + const positions2 = geometry2.attributes.position; + const positions3 = geometry3.attributes.position; + const colors1 = geometry1.attributes.color; + const colors2 = geometry2.attributes.color; + const colors3 = geometry3.attributes.color; + + for (let i = 0; i < count; i++) { + color.setHSL((positions1.getY(i) / radius + 1) / 2, 1.0, 0.5, THREE.SRGBColorSpace); + colors1.setXYZ(i, color.r, color.g, color.b); + + color.setHSL(0, (positions2.getY(i) / radius + 1) / 2, 0.5, THREE.SRGBColorSpace); + colors2.setXYZ(i, color.r, color.g, color.b); + + color.setRGB(1, 0.8 - (positions3.getY(i) / radius + 1) / 2, 0, THREE.SRGBColorSpace); + colors3.setXYZ(i, color.r, color.g, color.b); + } + + const material = new THREE.MeshPhongMaterial({ + color: 0xffffff, + flatShading: true, + vertexColors: true, + shininess: 0, + }); + + const wireframeMaterial = new THREE.MeshBasicMaterial({ color: 0x000000, wireframe: true, transparent: true }); + + let mesh = new THREE.Mesh(geometry1, material); + let wireframe = new THREE.Mesh(geometry1, wireframeMaterial); + mesh.add(wireframe); + mesh.position.x = -400; + mesh.rotation.x = -1.87; + scene.add(mesh); + + mesh = new THREE.Mesh(geometry2, material); + wireframe = new THREE.Mesh(geometry2, wireframeMaterial); + mesh.add(wireframe); + mesh.position.x = 400; + scene.add(mesh); + + mesh = new THREE.Mesh(geometry3, material); + wireframe = new THREE.Mesh(geometry3, wireframeMaterial); + mesh.add(wireframe); + scene.add(mesh); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + document.addEventListener('mousemove', onDocumentMouseMove); +} + +function onDocumentMouseMove(event) { + mouseX = event.clientX - windowWidth / 2; + mouseY = event.clientY - windowHeight / 2; +} + +function updateSize() { + if (windowWidth != window.innerWidth || windowHeight != window.innerHeight) { + windowWidth = window.innerWidth; + windowHeight = window.innerHeight; + + renderer.setSize(windowWidth, windowHeight); + } +} + +function animate() { + render(); + stats.update(); +} + +function render() { + updateSize(); + + for (let ii = 0; ii < views.length; ++ii) { + const view = views[ii]; + const camera = view.camera; + + view.updateCamera(camera, scene, mouseX, mouseY); + + const left = Math.floor(windowWidth * view.left); + const bottom = Math.floor(windowHeight * view.bottom); + const width = Math.floor(windowWidth * view.width); + const height = Math.floor(windowHeight * view.height); + + renderer.setViewport(left, bottom, width, height); + renderer.setScissor(left, bottom, width, height); + renderer.setScissorTest(true); + renderer.setClearColor(view.background); + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.render(scene, camera); + } +} diff --git a/examples-testing/examples/webgl_multisampled_renderbuffers.ts b/examples-testing/examples/webgl_multisampled_renderbuffers.ts new file mode 100644 index 000000000..df84fb144 --- /dev/null +++ b/examples-testing/examples/webgl_multisampled_renderbuffers.ts @@ -0,0 +1,133 @@ +import * as THREE from 'three'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, renderer, group, container; + +let composer1, composer2; + +const params = { + animate: true, +}; + +init(); + +function init() { + container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(45, container.offsetWidth / container.offsetHeight, 10, 2000); + camera.position.z = 500; + + const scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + scene.fog = new THREE.Fog(0xcccccc, 100, 1500); + + // + + const hemiLight = new THREE.HemisphereLight(0xffffff, 0x222222, 5); + hemiLight.position.set(1, 1, 1); + scene.add(hemiLight); + + // + + group = new THREE.Group(); + + const geometry = new THREE.SphereGeometry(10, 64, 40); + const material = new THREE.MeshLambertMaterial({ + color: 0xee0808, + polygonOffset: true, + polygonOffsetFactor: 1, // positive value pushes polygon further away + polygonOffsetUnits: 1, + }); + const material2 = new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: true }); + + for (let i = 0; i < 50; i++) { + const mesh = new THREE.Mesh(geometry, material); + mesh.position.x = Math.random() * 600 - 300; + mesh.position.y = Math.random() * 600 - 300; + mesh.position.z = Math.random() * 600 - 300; + mesh.rotation.x = Math.random(); + mesh.rotation.z = Math.random(); + mesh.scale.setScalar(Math.random() * 5 + 5); + group.add(mesh); + + const mesh2 = new THREE.Mesh(geometry, material2); + mesh2.position.copy(mesh.position); + mesh2.rotation.copy(mesh.rotation); + mesh2.scale.copy(mesh.scale); + group.add(mesh2); + } + + scene.add(group); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(container.offsetWidth, container.offsetHeight); + renderer.setAnimationLoop(animate); + renderer.autoClear = false; + container.appendChild(renderer.domElement); + + // + + const size = renderer.getDrawingBufferSize(new THREE.Vector2()); + const renderTarget = new THREE.WebGLRenderTarget(size.width, size.height, { + samples: 4, + type: THREE.HalfFloatType, + }); + + const renderPass = new RenderPass(scene, camera); + const outputPass = new OutputPass(); + + // + + composer1 = new EffectComposer(renderer); + composer1.addPass(renderPass); + composer1.addPass(outputPass); + + // + + composer2 = new EffectComposer(renderer, renderTarget); + composer2.addPass(renderPass); + composer2.addPass(outputPass); + + // + + const gui = new GUI(); + gui.add(params, 'animate'); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = container.offsetWidth / container.offsetHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(container.offsetWidth, container.offsetHeight); + composer1.setSize(container.offsetWidth, container.offsetHeight); + composer2.setSize(container.offsetWidth, container.offsetHeight); +} + +function animate() { + const halfWidth = container.offsetWidth / 2; + + if (params.animate) { + group.rotation.y += 0.002; + } + + renderer.setScissorTest(true); + + renderer.setScissor(0, 0, halfWidth - 1, container.offsetHeight); + composer1.render(); + + renderer.setScissor(halfWidth, 0, halfWidth, container.offsetHeight); + composer2.render(); + + renderer.setScissorTest(false); +} diff --git a/examples-testing/examples/webgl_panorama_cube.ts b/examples-testing/examples/webgl_panorama_cube.ts new file mode 100644 index 000000000..efd09cfc5 --- /dev/null +++ b/examples-testing/examples/webgl_panorama_cube.ts @@ -0,0 +1,83 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, controls; +let renderer; +let scene; + +init(); + +function init() { + const container = document.getElementById('container'); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(90, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.z = 0.01; + + controls = new OrbitControls(camera, renderer.domElement); + controls.enableZoom = false; + controls.enablePan = false; + controls.enableDamping = true; + controls.rotateSpeed = -0.25; + + const textures = getTexturesFromAtlasFile('textures/cube/sun_temple_stripe.jpg', 6); + + const materials = []; + + for (let i = 0; i < 6; i++) { + materials.push(new THREE.MeshBasicMaterial({ map: textures[i] })); + } + + const skyBox = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), materials); + skyBox.geometry.scale(1, 1, -1); + scene.add(skyBox); + + window.addEventListener('resize', onWindowResize); +} + +function getTexturesFromAtlasFile(atlasImgUrl, tilesNum) { + const textures = []; + + for (let i = 0; i < tilesNum; i++) { + textures[i] = new THREE.Texture(); + } + + new THREE.ImageLoader().load(atlasImgUrl, image => { + let canvas, context; + const tileWidth = image.height; + + for (let i = 0; i < textures.length; i++) { + canvas = document.createElement('canvas'); + context = canvas.getContext('2d'); + canvas.height = tileWidth; + canvas.width = tileWidth; + context.drawImage(image, tileWidth * i, 0, tileWidth, tileWidth, 0, 0, tileWidth, tileWidth); + textures[i].colorSpace = THREE.SRGBColorSpace; + textures[i].image = canvas; + textures[i].needsUpdate = true; + } + }); + + return textures; +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + controls.update(); // required when damping is enabled + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_panorama_equirectangular.ts b/examples-testing/examples/webgl_panorama_equirectangular.ts new file mode 100644 index 000000000..552745222 --- /dev/null +++ b/examples-testing/examples/webgl_panorama_equirectangular.ts @@ -0,0 +1,142 @@ +import * as THREE from 'three'; + +let camera, scene, renderer; + +let isUserInteracting = false, + onPointerDownMouseX = 0, + onPointerDownMouseY = 0, + lon = 0, + onPointerDownLon = 0, + lat = 0, + onPointerDownLat = 0, + phi = 0, + theta = 0; + +init(); + +function init() { + const container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1100); + + scene = new THREE.Scene(); + + const geometry = new THREE.SphereGeometry(500, 60, 40); + // invert the geometry on the x-axis so that all of the faces point inward + geometry.scale(-1, 1, 1); + + const texture = new THREE.TextureLoader().load('textures/2294472375_24a3b8ef46_o.jpg'); + texture.colorSpace = THREE.SRGBColorSpace; + const material = new THREE.MeshBasicMaterial({ map: texture }); + + const mesh = new THREE.Mesh(geometry, material); + + scene.add(mesh); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + container.style.touchAction = 'none'; + container.addEventListener('pointerdown', onPointerDown); + + document.addEventListener('wheel', onDocumentMouseWheel); + + // + + document.addEventListener('dragover', function (event) { + event.preventDefault(); + event.dataTransfer.dropEffect = 'copy'; + }); + + document.addEventListener('dragenter', function () { + document.body.style.opacity = 0.5; + }); + + document.addEventListener('dragleave', function () { + document.body.style.opacity = 1; + }); + + document.addEventListener('drop', function (event) { + event.preventDefault(); + + const reader = new FileReader(); + reader.addEventListener('load', function (event) { + material.map.image.src = event.target.result; + material.map.needsUpdate = true; + }); + reader.readAsDataURL(event.dataTransfer.files[0]); + + document.body.style.opacity = 1; + }); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onPointerDown(event) { + if (event.isPrimary === false) return; + + isUserInteracting = true; + + onPointerDownMouseX = event.clientX; + onPointerDownMouseY = event.clientY; + + onPointerDownLon = lon; + onPointerDownLat = lat; + + document.addEventListener('pointermove', onPointerMove); + document.addEventListener('pointerup', onPointerUp); +} + +function onPointerMove(event) { + if (event.isPrimary === false) return; + + lon = (onPointerDownMouseX - event.clientX) * 0.1 + onPointerDownLon; + lat = (event.clientY - onPointerDownMouseY) * 0.1 + onPointerDownLat; +} + +function onPointerUp() { + if (event.isPrimary === false) return; + + isUserInteracting = false; + + document.removeEventListener('pointermove', onPointerMove); + document.removeEventListener('pointerup', onPointerUp); +} + +function onDocumentMouseWheel(event) { + const fov = camera.fov + event.deltaY * 0.05; + + camera.fov = THREE.MathUtils.clamp(fov, 10, 75); + + camera.updateProjectionMatrix(); +} + +function animate() { + if (isUserInteracting === false) { + lon += 0.1; + } + + lat = Math.max(-85, Math.min(85, lat)); + phi = THREE.MathUtils.degToRad(90 - lat); + theta = THREE.MathUtils.degToRad(lon); + + const x = 500 * Math.sin(phi) * Math.cos(theta); + const y = 500 * Math.cos(phi); + const z = 500 * Math.sin(phi) * Math.sin(theta); + + camera.lookAt(x, y, z); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_pmrem_test.ts b/examples-testing/examples/webgl_pmrem_test.ts new file mode 100644 index 000000000..b33e4e2f1 --- /dev/null +++ b/examples-testing/examples/webgl_pmrem_test.ts @@ -0,0 +1,141 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let scene, camera, controls, renderer; + +function init() { + const width = window.innerWidth; + const height = window.innerHeight; + const aspect = width / height; + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(width, height); + + // tonemapping + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1; + + document.body.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); + + // scene + + scene = new THREE.Scene(); + + // camera + + camera = new THREE.PerspectiveCamera(40, aspect, 1, 30); + updateCamera(); + camera.position.set(0, 0, 16); + + // controls + + controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.minDistance = 4; + controls.maxDistance = 20; + + // light + + const directionalLight = new THREE.DirectionalLight(0xffffff, 0); // set intensity to 0 to start + const x = 597; + const y = 213; + const theta = ((x + 0.5) * Math.PI) / 512; + const phi = ((y + 0.5) * Math.PI) / 512; + + directionalLight.position.setFromSphericalCoords(100, -phi, Math.PI / 2 - theta); + + scene.add(directionalLight); + // scene.add( new THREE.DirectionalLightHelper( directionalLight ) ); + + // The spot1Lux HDR environment map is expressed in nits (lux / sr). The directional light has units of lux, + // so to match a 1 lux light, we set a single pixel with a value equal to 1 divided by the solid + // angle of the pixel in steradians. This image is 1024 x 512, + // so the value is 1 / ( sin( phi ) * ( pi / 512 ) ^ 2 ) = 27,490 nits. + + const gui = new GUI(); + gui.add({ enabled: true }, 'enabled') + .name('PMREM') + .onChange(value => { + directionalLight.intensity = value ? 0 : 1; + + scene.traverse(function (child) { + if (child.isMesh) { + child.material.envMapIntensity = 1 - directionalLight.intensity; + } + }); + + render(); + }); +} + +function createObjects() { + let radianceMap = null; + new RGBELoader() + // .setDataType( THREE.FloatType ) + .setPath('textures/equirectangular/') + .load('spot1Lux.hdr', function (texture) { + radianceMap = pmremGenerator.fromEquirectangular(texture).texture; + pmremGenerator.dispose(); + + scene.background = radianceMap; + + const geometry = new THREE.SphereGeometry(0.4, 32, 32); + + for (let x = 0; x <= 10; x++) { + for (let y = 0; y <= 2; y++) { + const material = new THREE.MeshPhysicalMaterial({ + roughness: x / 10, + metalness: y < 1 ? 1 : 0, + color: y < 2 ? 0xffffff : 0x000000, + envMap: radianceMap, + envMapIntensity: 1, + }); + + const mesh = new THREE.Mesh(geometry, material); + mesh.position.x = x - 5; + mesh.position.y = 1 - y; + scene.add(mesh); + } + } + + render(); + }); + + const pmremGenerator = new THREE.PMREMGenerator(renderer); + pmremGenerator.compileEquirectangularShader(); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + updateCamera(); + + renderer.setSize(width, height); + + render(); +} + +function updateCamera() { + const horizontalFoV = 40; + const verticalFoV = + (2 * Math.atan(Math.tan(((horizontalFoV / 2) * Math.PI) / 180) / camera.aspect) * 180) / Math.PI; + camera.fov = verticalFoV; + camera.updateProjectionMatrix(); +} + +function render() { + renderer.render(scene, camera); +} + +Promise.resolve().then(init).then(createObjects).then(render); diff --git a/examples-testing/examples/webgl_points_billboards.ts b/examples-testing/examples/webgl_points_billboards.ts new file mode 100644 index 000000000..24d4de1a9 --- /dev/null +++ b/examples-testing/examples/webgl_points_billboards.ts @@ -0,0 +1,120 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, stats, material; +let mouseX = 0, + mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(55, window.innerWidth / window.innerHeight, 2, 2000); + camera.position.z = 1000; + + scene = new THREE.Scene(); + scene.fog = new THREE.FogExp2(0x000000, 0.001); + + const geometry = new THREE.BufferGeometry(); + const vertices = []; + + const sprite = new THREE.TextureLoader().load('textures/sprites/disc.png'); + sprite.colorSpace = THREE.SRGBColorSpace; + + for (let i = 0; i < 10000; i++) { + const x = 2000 * Math.random() - 1000; + const y = 2000 * Math.random() - 1000; + const z = 2000 * Math.random() - 1000; + + vertices.push(x, y, z); + } + + geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); + + material = new THREE.PointsMaterial({ + size: 35, + sizeAttenuation: true, + map: sprite, + alphaTest: 0.5, + transparent: true, + }); + material.color.setHSL(1.0, 0.3, 0.7, THREE.SRGBColorSpace); + + const particles = new THREE.Points(geometry, material); + scene.add(particles); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + const gui = new GUI(); + + gui.add(material, 'sizeAttenuation').onChange(function () { + material.needsUpdate = true; + }); + + gui.open(); + + // + + document.body.style.touchAction = 'none'; + document.body.addEventListener('pointermove', onPointerMove); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onPointerMove(event) { + if (event.isPrimary === false) return; + + mouseX = event.clientX - windowHalfX; + mouseY = event.clientY - windowHalfY; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + const time = Date.now() * 0.00005; + + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-mouseY - camera.position.y) * 0.05; + + camera.lookAt(scene.position); + + const h = ((360 * (1.0 + time)) % 360) / 360; + material.color.setHSL(h, 0.5, 0.5); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_points_sprites.ts b/examples-testing/examples/webgl_points_sprites.ts new file mode 100644 index 000000000..31b9e2ce1 --- /dev/null +++ b/examples-testing/examples/webgl_points_sprites.ts @@ -0,0 +1,167 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, stats, parameters; +let mouseX = 0, + mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +const materials = []; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 2000); + camera.position.z = 1000; + + scene = new THREE.Scene(); + scene.fog = new THREE.FogExp2(0x000000, 0.0008); + + const geometry = new THREE.BufferGeometry(); + const vertices = []; + + const textureLoader = new THREE.TextureLoader(); + + const assignSRGB = texture => { + texture.colorSpace = THREE.SRGBColorSpace; + }; + + const sprite1 = textureLoader.load('textures/sprites/snowflake1.png', assignSRGB); + const sprite2 = textureLoader.load('textures/sprites/snowflake2.png', assignSRGB); + const sprite3 = textureLoader.load('textures/sprites/snowflake3.png', assignSRGB); + const sprite4 = textureLoader.load('textures/sprites/snowflake4.png', assignSRGB); + const sprite5 = textureLoader.load('textures/sprites/snowflake5.png', assignSRGB); + + for (let i = 0; i < 10000; i++) { + const x = Math.random() * 2000 - 1000; + const y = Math.random() * 2000 - 1000; + const z = Math.random() * 2000 - 1000; + + vertices.push(x, y, z); + } + + geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); + + parameters = [ + [[1.0, 0.2, 0.5], sprite2, 20], + [[0.95, 0.1, 0.5], sprite3, 15], + [[0.9, 0.05, 0.5], sprite1, 10], + [[0.85, 0, 0.5], sprite5, 8], + [[0.8, 0, 0.5], sprite4, 5], + ]; + + for (let i = 0; i < parameters.length; i++) { + const color = parameters[i][0]; + const sprite = parameters[i][1]; + const size = parameters[i][2]; + + materials[i] = new THREE.PointsMaterial({ + size: size, + map: sprite, + blending: THREE.AdditiveBlending, + depthTest: false, + transparent: true, + }); + materials[i].color.setHSL(color[0], color[1], color[2], THREE.SRGBColorSpace); + + const particles = new THREE.Points(geometry, materials[i]); + + particles.rotation.x = Math.random() * 6; + particles.rotation.y = Math.random() * 6; + particles.rotation.z = Math.random() * 6; + + scene.add(particles); + } + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + const gui = new GUI(); + + const params = { + texture: true, + }; + + gui.add(params, 'texture').onChange(function (value) { + for (let i = 0; i < materials.length; i++) { + materials[i].map = value === true ? parameters[i][1] : null; + materials[i].needsUpdate = true; + } + }); + + gui.open(); + + document.body.style.touchAction = 'none'; + document.body.addEventListener('pointermove', onPointerMove); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onPointerMove(event) { + if (event.isPrimary === false) return; + + mouseX = event.clientX - windowHalfX; + mouseY = event.clientY - windowHalfY; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + const time = Date.now() * 0.00005; + + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-mouseY - camera.position.y) * 0.05; + + camera.lookAt(scene.position); + + for (let i = 0; i < scene.children.length; i++) { + const object = scene.children[i]; + + if (object instanceof THREE.Points) { + object.rotation.y = time * (i < 4 ? i + 1 : -(i + 1)); + } + } + + for (let i = 0; i < materials.length; i++) { + const color = parameters[i][0]; + + const h = ((360 * (color[0] + time)) % 360) / 360; + materials[i].color.setHSL(h, color[1], color[2], THREE.SRGBColorSpace); + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_points_waves.ts b/examples-testing/examples/webgl_points_waves.ts new file mode 100644 index 000000000..91986e9e9 --- /dev/null +++ b/examples-testing/examples/webgl_points_waves.ts @@ -0,0 +1,145 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +const SEPARATION = 100, + AMOUNTX = 50, + AMOUNTY = 50; + +let container, stats; +let camera, scene, renderer; + +let particles, + count = 0; + +let mouseX = 0, + mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 1000; + + scene = new THREE.Scene(); + + // + + const numParticles = AMOUNTX * AMOUNTY; + + const positions = new Float32Array(numParticles * 3); + const scales = new Float32Array(numParticles); + + let i = 0, + j = 0; + + for (let ix = 0; ix < AMOUNTX; ix++) { + for (let iy = 0; iy < AMOUNTY; iy++) { + positions[i] = ix * SEPARATION - (AMOUNTX * SEPARATION) / 2; // x + positions[i + 1] = 0; // y + positions[i + 2] = iy * SEPARATION - (AMOUNTY * SEPARATION) / 2; // z + + scales[j] = 1; + + i += 3; + j++; + } + } + + const geometry = new THREE.BufferGeometry(); + geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); + geometry.setAttribute('scale', new THREE.BufferAttribute(scales, 1)); + + const material = new THREE.ShaderMaterial({ + uniforms: { + color: { value: new THREE.Color(0xffffff) }, + }, + vertexShader: document.getElementById('vertexshader').textContent, + fragmentShader: document.getElementById('fragmentshader').textContent, + }); + + // + + particles = new THREE.Points(geometry, material); + scene.add(particles); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + container.style.touchAction = 'none'; + container.addEventListener('pointermove', onPointerMove); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function onPointerMove(event) { + if (event.isPrimary === false) return; + + mouseX = event.clientX - windowHalfX; + mouseY = event.clientY - windowHalfY; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-mouseY - camera.position.y) * 0.05; + camera.lookAt(scene.position); + + const positions = particles.geometry.attributes.position.array; + const scales = particles.geometry.attributes.scale.array; + + let i = 0, + j = 0; + + for (let ix = 0; ix < AMOUNTX; ix++) { + for (let iy = 0; iy < AMOUNTY; iy++) { + positions[i + 1] = Math.sin((ix + count) * 0.3) * 50 + Math.sin((iy + count) * 0.5) * 50; + + scales[j] = (Math.sin((ix + count) * 0.3) + 1) * 20 + (Math.sin((iy + count) * 0.5) + 1) * 20; + + i += 3; + j++; + } + } + + particles.geometry.attributes.position.needsUpdate = true; + particles.geometry.attributes.scale.needsUpdate = true; + + renderer.render(scene, camera); + + count += 0.1; +} diff --git a/examples-testing/examples/webgl_portal.ts b/examples-testing/examples/webgl_portal.ts new file mode 100644 index 000000000..4bc59593f --- /dev/null +++ b/examples-testing/examples/webgl_portal.ts @@ -0,0 +1,218 @@ +import * as THREE from 'three'; + +import * as CameraUtils from 'three/addons/utils/CameraUtils.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer; + +let cameraControls; + +let smallSphereOne, smallSphereTwo; + +let portalCamera, + leftPortal, + rightPortal, + leftPortalTexture, + reflectedPosition, + rightPortalTexture, + bottomLeftCorner, + bottomRightCorner, + topLeftCorner; + +init(); + +function init() { + const container = document.getElementById('container'); + + // renderer + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.localClippingEnabled = true; + renderer.toneMapping = THREE.ACESFilmicToneMapping; + container.appendChild(renderer.domElement); + + // scene + scene = new THREE.Scene(); + + // camera + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 5000); + camera.position.set(0, 75, 160); + + cameraControls = new OrbitControls(camera, renderer.domElement); + cameraControls.target.set(0, 40, 0); + cameraControls.maxDistance = 400; + cameraControls.minDistance = 10; + cameraControls.update(); + + // + + const planeGeo = new THREE.PlaneGeometry(100.1, 100.1); + + // bouncing icosphere + const portalPlane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0.0); + const geometry = new THREE.IcosahedronGeometry(5, 0); + const material = new THREE.MeshPhongMaterial({ + color: 0xffffff, + emissive: 0x333333, + flatShading: true, + clippingPlanes: [portalPlane], + clipShadows: true, + }); + smallSphereOne = new THREE.Mesh(geometry, material); + scene.add(smallSphereOne); + smallSphereTwo = new THREE.Mesh(geometry, material); + scene.add(smallSphereTwo); + + // portals + portalCamera = new THREE.PerspectiveCamera(45, 1.0, 0.1, 500.0); + scene.add(portalCamera); + //frustumHelper = new THREE.CameraHelper( portalCamera ); + //scene.add( frustumHelper ); + bottomLeftCorner = new THREE.Vector3(); + bottomRightCorner = new THREE.Vector3(); + topLeftCorner = new THREE.Vector3(); + reflectedPosition = new THREE.Vector3(); + + leftPortalTexture = new THREE.WebGLRenderTarget(256, 256); + leftPortal = new THREE.Mesh(planeGeo, new THREE.MeshBasicMaterial({ map: leftPortalTexture.texture })); + leftPortal.position.x = -30; + leftPortal.position.y = 20; + leftPortal.scale.set(0.35, 0.35, 0.35); + scene.add(leftPortal); + + rightPortalTexture = new THREE.WebGLRenderTarget(256, 256); + rightPortal = new THREE.Mesh(planeGeo, new THREE.MeshBasicMaterial({ map: rightPortalTexture.texture })); + rightPortal.position.x = 30; + rightPortal.position.y = 20; + rightPortal.scale.set(0.35, 0.35, 0.35); + scene.add(rightPortal); + + // walls + const planeTop = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); + planeTop.position.y = 100; + planeTop.rotateX(Math.PI / 2); + scene.add(planeTop); + + const planeBottom = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); + planeBottom.rotateX(-Math.PI / 2); + scene.add(planeBottom); + + const planeFront = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x7f7fff })); + planeFront.position.z = 50; + planeFront.position.y = 50; + planeFront.rotateY(Math.PI); + scene.add(planeFront); + + const planeBack = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xff7fff })); + planeBack.position.z = -50; + planeBack.position.y = 50; + //planeBack.rotateY( Math.PI ); + scene.add(planeBack); + + const planeRight = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x00ff00 })); + planeRight.position.x = 50; + planeRight.position.y = 50; + planeRight.rotateY(-Math.PI / 2); + scene.add(planeRight); + + const planeLeft = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xff0000 })); + planeLeft.position.x = -50; + planeLeft.position.y = 50; + planeLeft.rotateY(Math.PI / 2); + scene.add(planeLeft); + + // lights + const mainLight = new THREE.PointLight(0xe7e7e7, 2.5, 250, 0); + mainLight.position.y = 60; + scene.add(mainLight); + + const greenLight = new THREE.PointLight(0x00ff00, 0.5, 1000, 0); + greenLight.position.set(550, 50, 0); + scene.add(greenLight); + + const redLight = new THREE.PointLight(0xff0000, 0.5, 1000, 0); + redLight.position.set(-550, 50, 0); + scene.add(redLight); + + const blueLight = new THREE.PointLight(0xbbbbfe, 0.5, 1000, 0); + blueLight.position.set(0, 50, 550); + scene.add(blueLight); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function renderPortal(thisPortalMesh, otherPortalMesh, thisPortalTexture) { + // set the portal camera position to be reflected about the portal plane + thisPortalMesh.worldToLocal(reflectedPosition.copy(camera.position)); + reflectedPosition.x *= -1.0; + reflectedPosition.z *= -1.0; + otherPortalMesh.localToWorld(reflectedPosition); + portalCamera.position.copy(reflectedPosition); + + // grab the corners of the other portal + // - note: the portal is viewed backwards; flip the left/right coordinates + otherPortalMesh.localToWorld(bottomLeftCorner.set(50.05, -50.05, 0.0)); + otherPortalMesh.localToWorld(bottomRightCorner.set(-50.05, -50.05, 0.0)); + otherPortalMesh.localToWorld(topLeftCorner.set(50.05, 50.05, 0.0)); + // set the projection matrix to encompass the portal's frame + CameraUtils.frameCorners(portalCamera, bottomLeftCorner, bottomRightCorner, topLeftCorner, false); + + // render the portal + thisPortalTexture.texture.colorSpace = renderer.outputColorSpace; + renderer.setRenderTarget(thisPortalTexture); + renderer.state.buffers.depth.setMask(true); // make sure the depth buffer is writable so it can be properly cleared, see #18897 + if (renderer.autoClear === false) renderer.clear(); + thisPortalMesh.visible = false; // hide this portal from its own rendering + renderer.render(scene, portalCamera); + thisPortalMesh.visible = true; // re-enable this portal's visibility for general rendering +} + +function animate() { + // move the bouncing sphere(s) + const timerOne = Date.now() * 0.01; + const timerTwo = timerOne + Math.PI * 10.0; + + smallSphereOne.position.set( + Math.cos(timerOne * 0.1) * 30, + Math.abs(Math.cos(timerOne * 0.2)) * 20 + 5, + Math.sin(timerOne * 0.1) * 30, + ); + smallSphereOne.rotation.y = Math.PI / 2 - timerOne * 0.1; + smallSphereOne.rotation.z = timerOne * 0.8; + + smallSphereTwo.position.set( + Math.cos(timerTwo * 0.1) * 30, + Math.abs(Math.cos(timerTwo * 0.2)) * 20 + 5, + Math.sin(timerTwo * 0.1) * 30, + ); + smallSphereTwo.rotation.y = Math.PI / 2 - timerTwo * 0.1; + smallSphereTwo.rotation.z = timerTwo * 0.8; + + // save the original camera properties + const currentRenderTarget = renderer.getRenderTarget(); + const currentXrEnabled = renderer.xr.enabled; + const currentShadowAutoUpdate = renderer.shadowMap.autoUpdate; + renderer.xr.enabled = false; // Avoid camera modification + renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows + + // render the portal effect + renderPortal(leftPortal, rightPortal, leftPortalTexture); + renderPortal(rightPortal, leftPortal, rightPortalTexture); + + // restore the original rendering properties + renderer.xr.enabled = currentXrEnabled; + renderer.shadowMap.autoUpdate = currentShadowAutoUpdate; + renderer.setRenderTarget(currentRenderTarget); + + // render the main scene + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_postprocessing.ts b/examples-testing/examples/webgl_postprocessing.ts new file mode 100644 index 000000000..ecc9b28ee --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing.ts @@ -0,0 +1,86 @@ +import * as THREE from 'three'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js'; + +import { RGBShiftShader } from 'three/addons/shaders/RGBShiftShader.js'; +import { DotScreenShader } from 'three/addons/shaders/DotScreenShader.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +let camera, renderer, composer; +let object; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 400; + + const scene = new THREE.Scene(); + scene.fog = new THREE.Fog(0x000000, 1, 1000); + + object = new THREE.Object3D(); + scene.add(object); + + const geometry = new THREE.SphereGeometry(1, 4, 4); + const material = new THREE.MeshPhongMaterial({ color: 0xffffff, flatShading: true }); + + for (let i = 0; i < 100; i++) { + const mesh = new THREE.Mesh(geometry, material); + mesh.position.set(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5).normalize(); + mesh.position.multiplyScalar(Math.random() * 400); + mesh.rotation.set(Math.random() * 2, Math.random() * 2, Math.random() * 2); + mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50; + object.add(mesh); + } + + scene.add(new THREE.AmbientLight(0xcccccc)); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(1, 1, 1); + scene.add(light); + + // postprocessing + + composer = new EffectComposer(renderer); + composer.addPass(new RenderPass(scene, camera)); + + const effect1 = new ShaderPass(DotScreenShader); + effect1.uniforms['scale'].value = 4; + composer.addPass(effect1); + + const effect2 = new ShaderPass(RGBShiftShader); + effect2.uniforms['amount'].value = 0.0015; + composer.addPass(effect2); + + const effect3 = new OutputPass(); + composer.addPass(effect3); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + composer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + object.rotation.x += 0.005; + object.rotation.y += 0.01; + + composer.render(); +} diff --git a/examples-testing/examples/webgl_postprocessing_advanced.ts b/examples-testing/examples/webgl_postprocessing_advanced.ts new file mode 100644 index 000000000..adaef6208 --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_advanced.ts @@ -0,0 +1,304 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js'; +import { BloomPass } from 'three/addons/postprocessing/BloomPass.js'; +import { FilmPass } from 'three/addons/postprocessing/FilmPass.js'; +import { DotScreenPass } from 'three/addons/postprocessing/DotScreenPass.js'; +import { MaskPass, ClearMaskPass } from 'three/addons/postprocessing/MaskPass.js'; +import { TexturePass } from 'three/addons/postprocessing/TexturePass.js'; + +import { BleachBypassShader } from 'three/addons/shaders/BleachBypassShader.js'; +import { ColorifyShader } from 'three/addons/shaders/ColorifyShader.js'; +import { HorizontalBlurShader } from 'three/addons/shaders/HorizontalBlurShader.js'; +import { VerticalBlurShader } from 'three/addons/shaders/VerticalBlurShader.js'; +import { SepiaShader } from 'three/addons/shaders/SepiaShader.js'; +import { VignetteShader } from 'three/addons/shaders/VignetteShader.js'; +import { GammaCorrectionShader } from 'three/addons/shaders/GammaCorrectionShader.js'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +let container, stats; + +let composerScene, composer1, composer2, composer3, composer4; + +let cameraOrtho, cameraPerspective, sceneModel, sceneBG, renderer, mesh, directionalLight; + +const width = window.innerWidth || 2; +const height = window.innerHeight || 2; + +let halfWidth = width / 2; +let halfHeight = height / 2; + +let quadBG, quadMask, renderScene; + +const delta = 0.01; + +init(); + +function init() { + container = document.getElementById('container'); + + // + + cameraOrtho = new THREE.OrthographicCamera(-halfWidth, halfWidth, halfHeight, -halfHeight, -10000, 10000); + cameraOrtho.position.z = 100; + + cameraPerspective = new THREE.PerspectiveCamera(50, width / height, 1, 10000); + cameraPerspective.position.z = 900; + + // + + sceneModel = new THREE.Scene(); + sceneBG = new THREE.Scene(); + + // + + directionalLight = new THREE.DirectionalLight(0xffffff, 3); + directionalLight.position.set(0, -0.1, 1).normalize(); + sceneModel.add(directionalLight); + + const loader = new GLTFLoader(); + loader.load('models/gltf/LeePerrySmith/LeePerrySmith.glb', function (gltf) { + createMesh(gltf.scene.children[0].geometry, sceneModel, 100); + }); + + // + + const diffuseMap = new THREE.TextureLoader().load('textures/cube/SwedishRoyalCastle/pz.jpg'); + diffuseMap.colorSpace = THREE.SRGBColorSpace; + + const materialColor = new THREE.MeshBasicMaterial({ + map: diffuseMap, + depthTest: false, + }); + + quadBG = new THREE.Mesh(new THREE.PlaneGeometry(1, 1), materialColor); + quadBG.position.z = -500; + quadBG.scale.set(width, height, 1); + sceneBG.add(quadBG); + + // + + const sceneMask = new THREE.Scene(); + + quadMask = new THREE.Mesh(new THREE.PlaneGeometry(1, 1), new THREE.MeshBasicMaterial({ color: 0xffaa00 })); + quadMask.position.z = -300; + quadMask.scale.set(width / 2, height / 2, 1); + sceneMask.add(quadMask); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(width, height); + renderer.setAnimationLoop(animate); + renderer.autoClear = false; + + // + + container.appendChild(renderer.domElement); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + const shaderBleach = BleachBypassShader; + const shaderSepia = SepiaShader; + const shaderVignette = VignetteShader; + + const effectBleach = new ShaderPass(shaderBleach); + const effectSepia = new ShaderPass(shaderSepia); + const effectVignette = new ShaderPass(shaderVignette); + const gammaCorrection = new ShaderPass(GammaCorrectionShader); + + effectBleach.uniforms['opacity'].value = 0.95; + + effectSepia.uniforms['amount'].value = 0.9; + + effectVignette.uniforms['offset'].value = 0.95; + effectVignette.uniforms['darkness'].value = 1.6; + + const effectBloom = new BloomPass(0.5); + const effectFilm = new FilmPass(0.35); + const effectFilmBW = new FilmPass(0.35, true); + const effectDotScreen = new DotScreenPass(new THREE.Vector2(0, 0), 0.5, 0.8); + + const effectHBlur = new ShaderPass(HorizontalBlurShader); + const effectVBlur = new ShaderPass(VerticalBlurShader); + effectHBlur.uniforms['h'].value = 2 / (width / 2); + effectVBlur.uniforms['v'].value = 2 / (height / 2); + + const effectColorify1 = new ShaderPass(ColorifyShader); + const effectColorify2 = new ShaderPass(ColorifyShader); + effectColorify1.uniforms['color'] = new THREE.Uniform(new THREE.Color(1, 0.8, 0.8)); + effectColorify2.uniforms['color'] = new THREE.Uniform(new THREE.Color(1, 0.75, 0.5)); + + const clearMask = new ClearMaskPass(); + const renderMask = new MaskPass(sceneModel, cameraPerspective); + const renderMaskInverse = new MaskPass(sceneModel, cameraPerspective); + + renderMaskInverse.inverse = true; + + // + + const rtParameters = { + stencilBuffer: true, + }; + + const rtWidth = width / 2; + const rtHeight = height / 2; + + // + + const renderBackground = new RenderPass(sceneBG, cameraOrtho); + const renderModel = new RenderPass(sceneModel, cameraPerspective); + + renderModel.clear = false; + + composerScene = new EffectComposer(renderer, new THREE.WebGLRenderTarget(rtWidth * 2, rtHeight * 2, rtParameters)); + + composerScene.addPass(renderBackground); + composerScene.addPass(renderModel); + composerScene.addPass(renderMaskInverse); + composerScene.addPass(effectHBlur); + composerScene.addPass(effectVBlur); + composerScene.addPass(clearMask); + + // + + renderScene = new TexturePass(composerScene.renderTarget2.texture); + + // + + composer1 = new EffectComposer(renderer, new THREE.WebGLRenderTarget(rtWidth, rtHeight, rtParameters)); + + composer1.addPass(renderScene); + composer1.addPass(gammaCorrection); + composer1.addPass(effectFilmBW); + composer1.addPass(effectVignette); + + // + + composer2 = new EffectComposer(renderer, new THREE.WebGLRenderTarget(rtWidth, rtHeight, rtParameters)); + + composer2.addPass(renderScene); + composer2.addPass(gammaCorrection); + composer2.addPass(effectDotScreen); + composer2.addPass(renderMask); + composer2.addPass(effectColorify1); + composer2.addPass(clearMask); + composer2.addPass(renderMaskInverse); + composer2.addPass(effectColorify2); + composer2.addPass(clearMask); + composer2.addPass(effectVignette); + + // + + composer3 = new EffectComposer(renderer, new THREE.WebGLRenderTarget(rtWidth, rtHeight, rtParameters)); + + composer3.addPass(renderScene); + composer3.addPass(gammaCorrection); + composer3.addPass(effectSepia); + composer3.addPass(effectFilm); + composer3.addPass(effectVignette); + + // + + composer4 = new EffectComposer(renderer, new THREE.WebGLRenderTarget(rtWidth, rtHeight, rtParameters)); + + composer4.addPass(renderScene); + composer4.addPass(gammaCorrection); + composer4.addPass(effectBloom); + composer4.addPass(effectFilm); + composer4.addPass(effectBleach); + composer4.addPass(effectVignette); + + renderScene.uniforms['tDiffuse'].value = composerScene.renderTarget2.texture; + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + halfWidth = window.innerWidth / 2; + halfHeight = window.innerHeight / 2; + + cameraPerspective.aspect = window.innerWidth / window.innerHeight; + cameraPerspective.updateProjectionMatrix(); + + cameraOrtho.left = -halfWidth; + cameraOrtho.right = halfWidth; + cameraOrtho.top = halfHeight; + cameraOrtho.bottom = -halfHeight; + + cameraOrtho.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + composerScene.setSize(halfWidth * 2, halfHeight * 2); + + composer1.setSize(halfWidth, halfHeight); + composer2.setSize(halfWidth, halfHeight); + composer3.setSize(halfWidth, halfHeight); + composer4.setSize(halfWidth, halfHeight); + + renderScene.uniforms['tDiffuse'].value = composerScene.renderTarget2.texture; + + quadBG.scale.set(window.innerWidth, window.innerHeight, 1); + quadMask.scale.set(window.innerWidth / 2, window.innerHeight / 2, 1); +} + +function createMesh(geometry, scene, scale) { + const diffuseMap = new THREE.TextureLoader().load('models/gltf/LeePerrySmith/Map-COL.jpg'); + diffuseMap.colorSpace = THREE.SRGBColorSpace; + + const mat2 = new THREE.MeshPhongMaterial({ + color: 0xcbcbcb, + specular: 0x080808, + shininess: 20, + map: diffuseMap, + normalMap: new THREE.TextureLoader().load('models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg'), + normalScale: new THREE.Vector2(0.75, 0.75), + }); + + mesh = new THREE.Mesh(geometry, mat2); + mesh.position.set(0, -50, 0); + mesh.scale.set(scale, scale, scale); + + scene.add(mesh); +} + +// + +function animate() { + stats.begin(); + render(); + stats.end(); +} + +function render() { + const time = Date.now() * 0.0004; + + if (mesh) mesh.rotation.y = -time; + + renderer.setViewport(0, 0, halfWidth, halfHeight); + composerScene.render(delta); + + renderer.setViewport(0, 0, halfWidth, halfHeight); + composer1.render(delta); + + renderer.setViewport(halfWidth, 0, halfWidth, halfHeight); + composer2.render(delta); + + renderer.setViewport(0, halfHeight, halfWidth, halfHeight); + composer3.render(delta); + + renderer.setViewport(halfWidth, halfHeight, halfWidth, halfHeight); + composer4.render(delta); +} diff --git a/examples-testing/examples/webgl_postprocessing_afterimage.ts b/examples-testing/examples/webgl_postprocessing_afterimage.ts new file mode 100644 index 000000000..508f90b89 --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_afterimage.ts @@ -0,0 +1,72 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { AfterimagePass } from 'three/addons/postprocessing/AfterimagePass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +let camera, scene, renderer, composer; +let mesh; + +let afterimagePass; + +const params = { + enable: true, +}; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 400; + + scene = new THREE.Scene(); + scene.fog = new THREE.Fog(0x000000, 1, 1000); + + const geometry = new THREE.BoxGeometry(150, 150, 150, 2, 2, 2); + const material = new THREE.MeshNormalMaterial(); + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // postprocessing + + composer = new EffectComposer(renderer); + composer.addPass(new RenderPass(scene, camera)); + + afterimagePass = new AfterimagePass(); + composer.addPass(afterimagePass); + + const outputPass = new OutputPass(); + composer.addPass(outputPass); + + window.addEventListener('resize', onWindowResize); + + const gui = new GUI({ title: 'Damp setting' }); + gui.add(afterimagePass.uniforms['damp'], 'value', 0, 1).step(0.001); + gui.add(params, 'enable'); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + composer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + mesh.rotation.x += 0.005; + mesh.rotation.y += 0.01; + + afterimagePass.enabled = params.enable; + + composer.render(); +} diff --git a/examples-testing/examples/webgl_postprocessing_backgrounds.ts b/examples-testing/examples/webgl_postprocessing_backgrounds.ts new file mode 100644 index 000000000..57a6a2dbd --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_backgrounds.ts @@ -0,0 +1,214 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { TexturePass } from 'three/addons/postprocessing/TexturePass.js'; +import { CubeTexturePass } from 'three/addons/postprocessing/CubeTexturePass.js'; +import { ClearPass } from 'three/addons/postprocessing/ClearPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let scene, renderer, composer; +let clearPass, texturePass, renderPass; +let cameraP, cubeTexturePassP; +let gui, stats; + +const params = { + clearPass: true, + clearColor: 'white', + clearAlpha: 1.0, + + texturePass: true, + texturePassOpacity: 1.0, + + cubeTexturePass: true, + cubeTexturePassOpacity: 1.0, + + renderPass: true, +}; + +init(); + +clearGui(); + +function clearGui() { + if (gui) gui.destroy(); + + gui = new GUI(); + + gui.add(params, 'clearPass'); + gui.add(params, 'clearColor', ['black', 'white', 'blue', 'green', 'red']); + gui.add(params, 'clearAlpha', 0, 1); + + gui.add(params, 'texturePass'); + gui.add(params, 'texturePassOpacity', 0, 1); + + gui.add(params, 'cubeTexturePass'); + gui.add(params, 'cubeTexturePassOpacity', 0, 1); + + gui.add(params, 'renderPass'); + + gui.open(); +} + +function init() { + const container = document.getElementById('container'); + + const width = window.innerWidth || 1; + const height = window.innerHeight || 1; + const aspect = width / height; + const devicePixelRatio = window.devicePixelRatio || 1; + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(devicePixelRatio); + renderer.setSize(width, height); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + cameraP = new THREE.PerspectiveCamera(65, aspect, 1, 10); + cameraP.position.z = 7; + + scene = new THREE.Scene(); + + const group = new THREE.Group(); + scene.add(group); + + const light = new THREE.PointLight(0xefffef, 500); + light.position.z = 10; + light.position.y = -10; + light.position.x = -10; + scene.add(light); + + const light2 = new THREE.PointLight(0xffefef, 500); + light2.position.z = 10; + light2.position.x = -10; + light2.position.y = 10; + scene.add(light2); + + const light3 = new THREE.PointLight(0xefefff, 500); + light3.position.z = 10; + light3.position.x = 10; + light3.position.y = -10; + scene.add(light3); + + const geometry = new THREE.SphereGeometry(1, 48, 24); + + const material = new THREE.MeshStandardMaterial(); + material.roughness = 0.5 * Math.random() + 0.25; + material.metalness = 0; + material.color.setHSL(Math.random(), 1.0, 0.3); + + const mesh = new THREE.Mesh(geometry, material); + group.add(mesh); + + // postprocessing + + const genCubeUrls = function (prefix, postfix) { + return [ + prefix + 'px' + postfix, + prefix + 'nx' + postfix, + prefix + 'py' + postfix, + prefix + 'ny' + postfix, + prefix + 'pz' + postfix, + prefix + 'nz' + postfix, + ]; + }; + + composer = new EffectComposer(renderer); + + clearPass = new ClearPass(params.clearColor, params.clearAlpha); + composer.addPass(clearPass); + + texturePass = new TexturePass(); + composer.addPass(texturePass); + + const textureLoader = new THREE.TextureLoader(); + textureLoader.load('textures/hardwood2_diffuse.jpg', function (map) { + map.colorSpace = THREE.SRGBColorSpace; + texturePass.map = map; + }); + + cubeTexturePassP = null; + + const ldrUrls = genCubeUrls('textures/cube/pisa/', '.png'); + new THREE.CubeTextureLoader().load(ldrUrls, function (ldrCubeMap) { + cubeTexturePassP = new CubeTexturePass(cameraP, ldrCubeMap); + composer.insertPass(cubeTexturePassP, 2); + }); + + renderPass = new RenderPass(scene, cameraP); + renderPass.clear = false; + composer.addPass(renderPass); + + const outputPass = new OutputPass(); + composer.addPass(outputPass); + + const controls = new OrbitControls(cameraP, renderer.domElement); + controls.enableZoom = false; + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + const aspect = width / height; + + cameraP.aspect = aspect; + cameraP.updateProjectionMatrix(); + + renderer.setSize(width, height); + composer.setSize(width, height); +} + +function animate() { + stats.begin(); + + cameraP.updateMatrixWorld(true); + + let newColor = clearPass.clearColor; + + switch (params.clearColor) { + case 'blue': + newColor = 0x0000ff; + break; + case 'red': + newColor = 0xff0000; + break; + case 'green': + newColor = 0x00ff00; + break; + case 'white': + newColor = 0xffffff; + break; + case 'black': + newColor = 0x000000; + break; + } + + clearPass.enabled = params.clearPass; + clearPass.clearColor = newColor; + clearPass.clearAlpha = params.clearAlpha; + + texturePass.enabled = params.texturePass; + texturePass.opacity = params.texturePassOpacity; + + if (cubeTexturePassP !== null) { + cubeTexturePassP.enabled = params.cubeTexturePass; + cubeTexturePassP.opacity = params.cubeTexturePassOpacity; + } + + renderPass.enabled = params.renderPass; + + composer.render(); + + stats.end(); +} diff --git a/examples-testing/examples/webgl_postprocessing_fxaa.ts b/examples-testing/examples/webgl_postprocessing_fxaa.ts new file mode 100644 index 000000000..55745f88e --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_fxaa.ts @@ -0,0 +1,132 @@ +import * as THREE from 'three'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; +import { FXAAShader } from 'three/addons/shaders/FXAAShader.js'; + +let camera, scene, renderer, clock, group, container; + +let composer1, composer2, fxaaPass; + +init(); + +function init() { + container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(45, container.offsetWidth / container.offsetHeight, 1, 2000); + camera.position.z = 500; + + scene = new THREE.Scene(); + + clock = new THREE.Clock(); + + // + + const hemiLight = new THREE.HemisphereLight(0xffffff, 0x8d8d8d); + hemiLight.position.set(0, 1000, 0); + scene.add(hemiLight); + + const dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.position.set(-3000, 1000, -1000); + scene.add(dirLight); + + // + + group = new THREE.Group(); + + const geometry = new THREE.TetrahedronGeometry(10); + const material = new THREE.MeshStandardMaterial({ color: 0xf73232, flatShading: true }); + + for (let i = 0; i < 100; i++) { + const mesh = new THREE.Mesh(geometry, material); + + mesh.position.x = Math.random() * 500 - 250; + mesh.position.y = Math.random() * 500 - 250; + mesh.position.z = Math.random() * 500 - 250; + + mesh.scale.setScalar(Math.random() * 2 + 1); + + mesh.rotation.x = Math.random() * Math.PI; + mesh.rotation.y = Math.random() * Math.PI; + mesh.rotation.z = Math.random() * Math.PI; + + group.add(mesh); + } + + scene.add(group); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(container.offsetWidth, container.offsetHeight); + renderer.setAnimationLoop(animate); + renderer.autoClear = false; + container.appendChild(renderer.domElement); + + // + + const renderPass = new RenderPass(scene, camera); + renderPass.clearAlpha = 0; + + // + + fxaaPass = new ShaderPass(FXAAShader); + + const outputPass = new OutputPass(); + + composer1 = new EffectComposer(renderer); + composer1.addPass(renderPass); + composer1.addPass(outputPass); + + // + + const pixelRatio = renderer.getPixelRatio(); + + fxaaPass.material.uniforms['resolution'].value.x = 1 / (container.offsetWidth * pixelRatio); + fxaaPass.material.uniforms['resolution'].value.y = 1 / (container.offsetHeight * pixelRatio); + + composer2 = new EffectComposer(renderer); + composer2.addPass(renderPass); + composer2.addPass(outputPass); + + // FXAA is engineered to be applied towards the end of engine post processing after conversion to low dynamic range and conversion to the sRGB color space for display. + + composer2.addPass(fxaaPass); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = container.offsetWidth / container.offsetHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(container.offsetWidth, container.offsetHeight); + composer1.setSize(container.offsetWidth, container.offsetHeight); + composer2.setSize(container.offsetWidth, container.offsetHeight); + + const pixelRatio = renderer.getPixelRatio(); + + fxaaPass.material.uniforms['resolution'].value.x = 1 / (container.offsetWidth * pixelRatio); + fxaaPass.material.uniforms['resolution'].value.y = 1 / (container.offsetHeight * pixelRatio); +} + +function animate() { + const halfWidth = container.offsetWidth / 2; + + group.rotation.y += clock.getDelta() * 0.1; + + renderer.setScissorTest(true); + + renderer.setScissor(0, 0, halfWidth - 1, container.offsetHeight); + composer1.render(); + + renderer.setScissor(halfWidth, 0, halfWidth, container.offsetHeight); + composer2.render(); + + renderer.setScissorTest(false); +} diff --git a/examples-testing/examples/webgl_postprocessing_glitch.ts b/examples-testing/examples/webgl_postprocessing_glitch.ts new file mode 100644 index 000000000..f846c0ce6 --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_glitch.ts @@ -0,0 +1,97 @@ +import * as THREE from 'three'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { GlitchPass } from 'three/addons/postprocessing/GlitchPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +let camera, scene, renderer, composer; +let object, light; + +let glitchPass; + +const button = document.querySelector('#startButton'); +button.addEventListener('click', function () { + const overlay = document.getElementById('overlay'); + overlay.remove(); + + init(); +}); + +function updateOptions() { + const wildGlitch = document.getElementById('wildGlitch'); + glitchPass.goWild = wildGlitch.checked; +} + +function init() { + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 400; + + scene = new THREE.Scene(); + scene.fog = new THREE.Fog(0x000000, 1, 1000); + + object = new THREE.Object3D(); + scene.add(object); + + const geometry = new THREE.SphereGeometry(1, 4, 4); + + for (let i = 0; i < 100; i++) { + const material = new THREE.MeshPhongMaterial({ color: 0xffffff * Math.random(), flatShading: true }); + + const mesh = new THREE.Mesh(geometry, material); + mesh.position.set(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5).normalize(); + mesh.position.multiplyScalar(Math.random() * 400); + mesh.rotation.set(Math.random() * 2, Math.random() * 2, Math.random() * 2); + mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50; + object.add(mesh); + } + + scene.add(new THREE.AmbientLight(0xcccccc)); + + light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(1, 1, 1); + scene.add(light); + + // postprocessing + + composer = new EffectComposer(renderer); + composer.addPass(new RenderPass(scene, camera)); + + glitchPass = new GlitchPass(); + composer.addPass(glitchPass); + + const outputPass = new OutputPass(); + composer.addPass(outputPass); + + // + + window.addEventListener('resize', onWindowResize); + + const wildGlitchOption = document.getElementById('wildGlitch'); + wildGlitchOption.addEventListener('change', updateOptions); + + updateOptions(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + composer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + object.rotation.x += 0.005; + object.rotation.y += 0.01; + + composer.render(); +} diff --git a/examples-testing/examples/webgl_postprocessing_godrays.ts b/examples-testing/examples/webgl_postprocessing_godrays.ts new file mode 100644 index 000000000..fb7604411 --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_godrays.ts @@ -0,0 +1,347 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { + GodRaysFakeSunShader, + GodRaysDepthMaskShader, + GodRaysCombineShader, + GodRaysGenerateShader, +} from 'three/addons/shaders/GodRaysShader.js'; + +let container, stats; +let camera, scene, renderer, materialDepth; + +let sphereMesh; + +const sunPosition = new THREE.Vector3(0, 1000, -1000); +const clipPosition = new THREE.Vector4(); +const screenSpacePosition = new THREE.Vector3(); + +const postprocessing = { enabled: true }; + +const orbitRadius = 200; + +const bgColor = 0x000511; +const sunColor = 0xffee00; + +// Use a smaller size for some of the god-ray render targets for better performance. +const godrayRenderTargetResolutionMultiplier = 1.0 / 4.0; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + // + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 3000); + camera.position.z = 200; + + scene = new THREE.Scene(); + + // + + materialDepth = new THREE.MeshDepthMaterial(); + + // tree + + const loader = new OBJLoader(); + loader.load('models/obj/tree.obj', function (object) { + object.position.set(0, -150, -150); + object.scale.multiplyScalar(400); + scene.add(object); + }); + + // sphere + + const geo = new THREE.SphereGeometry(1, 20, 10); + sphereMesh = new THREE.Mesh(geo, new THREE.MeshBasicMaterial({ color: 0x000000 })); + sphereMesh.scale.multiplyScalar(20); + scene.add(sphereMesh); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setClearColor(0xffffff); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + renderer.autoClear = false; + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 50; + controls.maxDistance = 500; + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); + + // + + initPostprocessing(window.innerWidth, window.innerHeight); +} + +// + +function onWindowResize() { + const renderTargetWidth = window.innerWidth; + const renderTargetHeight = window.innerHeight; + + camera.aspect = renderTargetWidth / renderTargetHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(renderTargetWidth, renderTargetHeight); + postprocessing.rtTextureColors.setSize(renderTargetWidth, renderTargetHeight); + postprocessing.rtTextureDepth.setSize(renderTargetWidth, renderTargetHeight); + postprocessing.rtTextureDepthMask.setSize(renderTargetWidth, renderTargetHeight); + + const adjustedWidth = renderTargetWidth * godrayRenderTargetResolutionMultiplier; + const adjustedHeight = renderTargetHeight * godrayRenderTargetResolutionMultiplier; + postprocessing.rtTextureGodRays1.setSize(adjustedWidth, adjustedHeight); + postprocessing.rtTextureGodRays2.setSize(adjustedWidth, adjustedHeight); +} + +function initPostprocessing(renderTargetWidth, renderTargetHeight) { + postprocessing.scene = new THREE.Scene(); + + postprocessing.camera = new THREE.OrthographicCamera(-0.5, 0.5, 0.5, -0.5, -10000, 10000); + postprocessing.camera.position.z = 100; + + postprocessing.scene.add(postprocessing.camera); + + postprocessing.rtTextureColors = new THREE.WebGLRenderTarget(renderTargetWidth, renderTargetHeight, { + type: THREE.HalfFloatType, + }); + + // Switching the depth formats to luminance from rgb doesn't seem to work. I didn't + // investigate further for now. + // pars.format = LuminanceFormat; + + // I would have this quarter size and use it as one of the ping-pong render + // targets but the aliasing causes some temporal flickering + + postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget(renderTargetWidth, renderTargetHeight, { + type: THREE.HalfFloatType, + }); + postprocessing.rtTextureDepthMask = new THREE.WebGLRenderTarget(renderTargetWidth, renderTargetHeight, { + type: THREE.HalfFloatType, + }); + + // The ping-pong render targets can use an adjusted resolution to minimize cost + + const adjustedWidth = renderTargetWidth * godrayRenderTargetResolutionMultiplier; + const adjustedHeight = renderTargetHeight * godrayRenderTargetResolutionMultiplier; + postprocessing.rtTextureGodRays1 = new THREE.WebGLRenderTarget(adjustedWidth, adjustedHeight, { + type: THREE.HalfFloatType, + }); + postprocessing.rtTextureGodRays2 = new THREE.WebGLRenderTarget(adjustedWidth, adjustedHeight, { + type: THREE.HalfFloatType, + }); + + // god-ray shaders + + const godraysMaskShader = GodRaysDepthMaskShader; + postprocessing.godrayMaskUniforms = THREE.UniformsUtils.clone(godraysMaskShader.uniforms); + postprocessing.materialGodraysDepthMask = new THREE.ShaderMaterial({ + uniforms: postprocessing.godrayMaskUniforms, + vertexShader: godraysMaskShader.vertexShader, + fragmentShader: godraysMaskShader.fragmentShader, + }); + + const godraysGenShader = GodRaysGenerateShader; + postprocessing.godrayGenUniforms = THREE.UniformsUtils.clone(godraysGenShader.uniforms); + postprocessing.materialGodraysGenerate = new THREE.ShaderMaterial({ + uniforms: postprocessing.godrayGenUniforms, + vertexShader: godraysGenShader.vertexShader, + fragmentShader: godraysGenShader.fragmentShader, + }); + + const godraysCombineShader = GodRaysCombineShader; + postprocessing.godrayCombineUniforms = THREE.UniformsUtils.clone(godraysCombineShader.uniforms); + postprocessing.materialGodraysCombine = new THREE.ShaderMaterial({ + uniforms: postprocessing.godrayCombineUniforms, + vertexShader: godraysCombineShader.vertexShader, + fragmentShader: godraysCombineShader.fragmentShader, + }); + + const godraysFakeSunShader = GodRaysFakeSunShader; + postprocessing.godraysFakeSunUniforms = THREE.UniformsUtils.clone(godraysFakeSunShader.uniforms); + postprocessing.materialGodraysFakeSun = new THREE.ShaderMaterial({ + uniforms: postprocessing.godraysFakeSunUniforms, + vertexShader: godraysFakeSunShader.vertexShader, + fragmentShader: godraysFakeSunShader.fragmentShader, + }); + + postprocessing.godraysFakeSunUniforms.bgColor.value.setHex(bgColor); + postprocessing.godraysFakeSunUniforms.sunColor.value.setHex(sunColor); + + postprocessing.godrayCombineUniforms.fGodRayIntensity.value = 0.75; + + postprocessing.quad = new THREE.Mesh(new THREE.PlaneGeometry(1.0, 1.0), postprocessing.materialGodraysGenerate); + postprocessing.quad.position.z = -9900; + postprocessing.scene.add(postprocessing.quad); +} + +function animate() { + stats.begin(); + render(); + stats.end(); +} + +function getStepSize(filterLen, tapsPerPass, pass) { + return filterLen * Math.pow(tapsPerPass, -pass); +} + +function filterGodRays(inputTex, renderTarget, stepSize) { + postprocessing.scene.overrideMaterial = postprocessing.materialGodraysGenerate; + + postprocessing.godrayGenUniforms['fStepSize'].value = stepSize; + postprocessing.godrayGenUniforms['tInput'].value = inputTex; + + renderer.setRenderTarget(renderTarget); + renderer.render(postprocessing.scene, postprocessing.camera); + postprocessing.scene.overrideMaterial = null; +} + +function render() { + const time = Date.now() / 4000; + + sphereMesh.position.x = orbitRadius * Math.cos(time); + sphereMesh.position.z = orbitRadius * Math.sin(time) - 100; + + if (postprocessing.enabled) { + clipPosition.x = sunPosition.x; + clipPosition.y = sunPosition.y; + clipPosition.z = sunPosition.z; + clipPosition.w = 1; + + clipPosition.applyMatrix4(camera.matrixWorldInverse).applyMatrix4(camera.projectionMatrix); + + // perspective divide (produce NDC space) + + clipPosition.x /= clipPosition.w; + clipPosition.y /= clipPosition.w; + + screenSpacePosition.x = (clipPosition.x + 1) / 2; // transform from [-1,1] to [0,1] + screenSpacePosition.y = (clipPosition.y + 1) / 2; // transform from [-1,1] to [0,1] + screenSpacePosition.z = clipPosition.z; // needs to stay in clip space for visibilty checks + + // Give it to the god-ray and sun shaders + + postprocessing.godrayGenUniforms['vSunPositionScreenSpace'].value.copy(screenSpacePosition); + postprocessing.godraysFakeSunUniforms['vSunPositionScreenSpace'].value.copy(screenSpacePosition); + + // -- Draw sky and sun -- + + // Clear colors and depths, will clear to sky color + + renderer.setRenderTarget(postprocessing.rtTextureColors); + renderer.clear(true, true, false); + + // Sun render. Runs a shader that gives a brightness based on the screen + // space distance to the sun. Not very efficient, so i make a scissor + // rectangle around the suns position to avoid rendering surrounding pixels. + + const sunsqH = 0.74 * window.innerHeight; // 0.74 depends on extent of sun from shader + const sunsqW = 0.74 * window.innerHeight; // both depend on height because sun is aspect-corrected + + screenSpacePosition.x *= window.innerWidth; + screenSpacePosition.y *= window.innerHeight; + + renderer.setScissor(screenSpacePosition.x - sunsqW / 2, screenSpacePosition.y - sunsqH / 2, sunsqW, sunsqH); + renderer.setScissorTest(true); + + postprocessing.godraysFakeSunUniforms['fAspect'].value = window.innerWidth / window.innerHeight; + + postprocessing.scene.overrideMaterial = postprocessing.materialGodraysFakeSun; + renderer.setRenderTarget(postprocessing.rtTextureColors); + renderer.render(postprocessing.scene, postprocessing.camera); + + renderer.setScissorTest(false); + + // -- Draw scene objects -- + + // Colors + + scene.overrideMaterial = null; + renderer.setRenderTarget(postprocessing.rtTextureColors); + renderer.render(scene, camera); + + // Depth + + scene.overrideMaterial = materialDepth; + renderer.setRenderTarget(postprocessing.rtTextureDepth); + renderer.clear(); + renderer.render(scene, camera); + + // + + postprocessing.godrayMaskUniforms['tInput'].value = postprocessing.rtTextureDepth.texture; + + postprocessing.scene.overrideMaterial = postprocessing.materialGodraysDepthMask; + renderer.setRenderTarget(postprocessing.rtTextureDepthMask); + renderer.render(postprocessing.scene, postprocessing.camera); + + // -- Render god-rays -- + + // Maximum length of god-rays (in texture space [0,1]X[0,1]) + + const filterLen = 1.0; + + // Samples taken by filter + + const TAPS_PER_PASS = 6.0; + + // Pass order could equivalently be 3,2,1 (instead of 1,2,3), which + // would start with a small filter support and grow to large. however + // the large-to-small order produces less objectionable aliasing artifacts that + // appear as a glimmer along the length of the beams + + // pass 1 - render into first ping-pong target + filterGodRays( + postprocessing.rtTextureDepthMask.texture, + postprocessing.rtTextureGodRays2, + getStepSize(filterLen, TAPS_PER_PASS, 1.0), + ); + + // pass 2 - render into second ping-pong target + filterGodRays( + postprocessing.rtTextureGodRays2.texture, + postprocessing.rtTextureGodRays1, + getStepSize(filterLen, TAPS_PER_PASS, 2.0), + ); + + // pass 3 - 1st RT + filterGodRays( + postprocessing.rtTextureGodRays1.texture, + postprocessing.rtTextureGodRays2, + getStepSize(filterLen, TAPS_PER_PASS, 3.0), + ); + + // final pass - composite god-rays onto colors + + postprocessing.godrayCombineUniforms['tColors'].value = postprocessing.rtTextureColors.texture; + postprocessing.godrayCombineUniforms['tGodRays'].value = postprocessing.rtTextureGodRays2.texture; + + postprocessing.scene.overrideMaterial = postprocessing.materialGodraysCombine; + + renderer.setRenderTarget(null); + renderer.render(postprocessing.scene, postprocessing.camera); + postprocessing.scene.overrideMaterial = null; + } else { + renderer.setRenderTarget(null); + renderer.clear(); + renderer.render(scene, camera); + } +} diff --git a/examples-testing/examples/webgl_postprocessing_gtao.ts b/examples-testing/examples/webgl_postprocessing_gtao.ts new file mode 100644 index 000000000..4f16d1554 --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_gtao.ts @@ -0,0 +1,215 @@ +import * as THREE from 'three'; +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { GTAOPass } from 'three/addons/postprocessing/GTAOPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +let camera, scene, renderer, composer, controls, clock, stats, mixer; + +init(); + +function init() { + const dracoLoader = new DRACOLoader(); + dracoLoader.setDecoderPath('jsm/libs/draco/'); + dracoLoader.setDecoderConfig({ type: 'js' }); + const loader = new GLTFLoader(); + loader.setDRACOLoader(dracoLoader); + loader.setPath('models/gltf/'); + + clock = new THREE.Clock(); + const container = document.createElement('div'); + document.body.appendChild(container); + + stats = new Stats(); + container.appendChild(stats.dom); + + renderer = new THREE.WebGLRenderer(); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + const pmremGenerator = new THREE.PMREMGenerator(renderer); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xbfe3dd); + scene.environment = pmremGenerator.fromScene(new RoomEnvironment(), 0.04).texture; + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 100); + camera.position.set(5, 2, 8); + + controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 0.5, 0); + controls.update(); + controls.enablePan = false; + controls.enableDamping = true; + + const width = window.innerWidth; + const height = window.innerHeight; + + composer = new EffectComposer(renderer); + + const renderPass = new RenderPass(scene, camera); + composer.addPass(renderPass); + + const gtaoPass = new GTAOPass(scene, camera, width, height); + gtaoPass.output = GTAOPass.OUTPUT.Denoise; + composer.addPass(gtaoPass); + + const outputPass = new OutputPass(); + composer.addPass(outputPass); + + // + + loader.load( + 'LittlestTokyo.glb', + gltf => { + const model = gltf.scene; + model.position.set(1, 1, 0); + model.scale.set(0.01, 0.01, 0.01); + scene.add(model); + + mixer = new THREE.AnimationMixer(model); + mixer.clipAction(gltf.animations[0]).play(); + + const box = new THREE.Box3().setFromObject(scene); + gtaoPass.setSceneClipBox(box); + }, + undefined, + e => console.error(e), + ); + + // Init gui + const gui = new GUI(); + + gui.add(gtaoPass, 'output', { + Default: GTAOPass.OUTPUT.Default, + Diffuse: GTAOPass.OUTPUT.Diffuse, + 'AO Only': GTAOPass.OUTPUT.AO, + 'AO Only + Denoise': GTAOPass.OUTPUT.Denoise, + Depth: GTAOPass.OUTPUT.Depth, + Normal: GTAOPass.OUTPUT.Normal, + }).onChange(function (value) { + gtaoPass.output = value; + }); + + const aoParameters = { + radius: 0.25, + distanceExponent: 1, + thickness: 1, + scale: 1, + samples: 16, + distanceFallOff: 1, + screenSpaceRadius: false, + }; + const pdParameters = { + lumaPhi: 10, + depthPhi: 2, + normalPhi: 3, + radius: 4, + radiusExponent: 1, + rings: 2, + samples: 16, + }; + gtaoPass.updateGtaoMaterial(aoParameters); + gtaoPass.updatePdMaterial(pdParameters); + gui.add(gtaoPass, 'blendIntensity').min(0).max(1).step(0.01); + gui.add(aoParameters, 'radius') + .min(0.01) + .max(1) + .step(0.01) + .onChange(() => gtaoPass.updateGtaoMaterial(aoParameters)); + gui.add(aoParameters, 'distanceExponent') + .min(1) + .max(4) + .step(0.01) + .onChange(() => gtaoPass.updateGtaoMaterial(aoParameters)); + gui.add(aoParameters, 'thickness') + .min(0.01) + .max(10) + .step(0.01) + .onChange(() => gtaoPass.updateGtaoMaterial(aoParameters)); + gui.add(aoParameters, 'distanceFallOff') + .min(0) + .max(1) + .step(0.01) + .onChange(() => gtaoPass.updateGtaoMaterial(aoParameters)); + gui.add(aoParameters, 'scale') + .min(0.01) + .max(2.0) + .step(0.01) + .onChange(() => gtaoPass.updateGtaoMaterial(aoParameters)); + gui.add(aoParameters, 'samples') + .min(2) + .max(32) + .step(1) + .onChange(() => gtaoPass.updateGtaoMaterial(aoParameters)); + gui.add(aoParameters, 'screenSpaceRadius').onChange(() => gtaoPass.updateGtaoMaterial(aoParameters)); + gui.add(pdParameters, 'lumaPhi') + .min(0) + .max(20) + .step(0.01) + .onChange(() => gtaoPass.updatePdMaterial(pdParameters)); + gui.add(pdParameters, 'depthPhi') + .min(0.01) + .max(20) + .step(0.01) + .onChange(() => gtaoPass.updatePdMaterial(pdParameters)); + gui.add(pdParameters, 'normalPhi') + .min(0.01) + .max(20) + .step(0.01) + .onChange(() => gtaoPass.updatePdMaterial(pdParameters)); + gui.add(pdParameters, 'radius') + .min(0) + .max(32) + .step(1) + .onChange(() => gtaoPass.updatePdMaterial(pdParameters)); + gui.add(pdParameters, 'radiusExponent') + .min(0.1) + .max(4) + .step(0.1) + .onChange(() => gtaoPass.updatePdMaterial(pdParameters)); + gui.add(pdParameters, 'rings') + .min(1) + .max(16) + .step(0.125) + .onChange(() => gtaoPass.updatePdMaterial(pdParameters)); + gui.add(pdParameters, 'samples') + .min(2) + .max(32) + .step(1) + .onChange(() => gtaoPass.updatePdMaterial(pdParameters)); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); + composer.setSize(width, height); +} + +function animate() { + const delta = clock.getDelta(); + + if (mixer) { + mixer.update(delta); + } + + controls.update(); + + stats.begin(); + composer.render(); + stats.end(); +} diff --git a/examples-testing/examples/webgl_postprocessing_masking.ts b/examples-testing/examples/webgl_postprocessing_masking.ts new file mode 100644 index 000000000..f6e7310bf --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_masking.ts @@ -0,0 +1,100 @@ +import * as THREE from 'three'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { TexturePass } from 'three/addons/postprocessing/TexturePass.js'; +import { ClearPass } from 'three/addons/postprocessing/ClearPass.js'; +import { MaskPass, ClearMaskPass } from 'three/addons/postprocessing/MaskPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +let camera, composer, renderer; +let box, torus; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 10; + + const scene1 = new THREE.Scene(); + const scene2 = new THREE.Scene(); + + box = new THREE.Mesh(new THREE.BoxGeometry(4, 4, 4)); + scene1.add(box); + + torus = new THREE.Mesh(new THREE.TorusGeometry(3, 1, 16, 32)); + scene2.add(torus); + + renderer = new THREE.WebGLRenderer(); + renderer.setClearColor(0xe0e0e0); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.autoClear = false; + document.body.appendChild(renderer.domElement); + + // + + const clearPass = new ClearPass(); + + const clearMaskPass = new ClearMaskPass(); + + const maskPass1 = new MaskPass(scene1, camera); + const maskPass2 = new MaskPass(scene2, camera); + + const texture1 = new THREE.TextureLoader().load('textures/758px-Canestra_di_frutta_(Caravaggio).jpg'); + texture1.colorSpace = THREE.SRGBColorSpace; + texture1.minFilter = THREE.LinearFilter; + const texture2 = new THREE.TextureLoader().load('textures/2294472375_24a3b8ef46_o.jpg'); + texture2.colorSpace = THREE.SRGBColorSpace; + + const texturePass1 = new TexturePass(texture1); + const texturePass2 = new TexturePass(texture2); + + const outputPass = new OutputPass(); + + const parameters = { + stencilBuffer: true, + }; + + const renderTarget = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight, parameters); + + composer = new EffectComposer(renderer, renderTarget); + composer.addPass(clearPass); + composer.addPass(maskPass1); + composer.addPass(texturePass1); + composer.addPass(clearMaskPass); + composer.addPass(maskPass2); + composer.addPass(texturePass2); + composer.addPass(clearMaskPass); + composer.addPass(outputPass); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); + composer.setSize(width, height); +} + +function animate() { + const time = performance.now() * 0.001 + 6000; + + box.position.x = Math.cos(time / 1.5) * 2; + box.position.y = Math.sin(time) * 2; + box.rotation.x = time; + box.rotation.y = time / 2; + + torus.position.x = Math.cos(time) * 2; + torus.position.y = Math.sin(time / 1.5) * 2; + torus.rotation.x = time; + torus.rotation.y = time / 2; + + renderer.clear(); + composer.render(time); +} diff --git a/examples-testing/examples/webgl_postprocessing_material_ao.ts b/examples-testing/examples/webgl_postprocessing_material_ao.ts new file mode 100644 index 000000000..2f17a5304 --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_material_ao.ts @@ -0,0 +1,277 @@ +import * as THREE from 'three'; +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; +import { PLYLoader } from 'three/addons/loaders/PLYLoader.js'; +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { GTAOPass } from 'three/addons/postprocessing/GTAOPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; +import { MeshPostProcessingMaterial } from 'three/addons/materials/MeshPostProcessingMaterial.js'; + +let renderer, camera, scene, composer, controls, stats; +const sceneParameters = { + output: 0, + envMapIntensity: 1.0, + ambientLightIntensity: 0.0, + lightIntensity: 50, + shadow: true, +}; +const aoParameters = { + radius: 0.5, + distanceExponent: 2, + thickness: 10, + scale: 1, + samples: 16, + distanceFallOff: 1, +}; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + stats = new Stats(); + container.appendChild(stats.dom); + + renderer = new THREE.WebGLRenderer(); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + renderer.shadowMap.enabled = sceneParameters.shadow; + + const plyLoader = new PLYLoader(); + const rgbeloader = new RGBELoader(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 50); + camera.position.set(0, 3, 5); + controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 1, 0); + controls.update(); + controls.enablePan = false; + controls.enableDamping = true; + + const width = window.innerWidth; + const height = window.innerHeight; + + scene = new THREE.Scene(); + composer = new EffectComposer(renderer); + + const gtaoPass = new GTAOPass(scene, camera, width, height); + gtaoPass.output = GTAOPass.OUTPUT.Off; + const renderPasse = new RenderPass(scene, camera); + const outputPass = new OutputPass(); + + composer.addPass(gtaoPass); + composer.addPass(renderPasse); + composer.addPass(outputPass); + + rgbeloader.load('textures/equirectangular/royal_esplanade_1k.hdr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + scene.environment = texture; + }); + + const groundMaterial = new MeshPostProcessingMaterial({ + color: 0x7f7f7f, + envMapIntensity: sceneParameters.envMapIntensity, + aoPassMap: gtaoPass.gtaoMap, + }); + const objectMaterial = new MeshPostProcessingMaterial({ + color: 0xffffff, + roughness: 0.5, + metalness: 0.5, + envMapIntensity: sceneParameters.envMapIntensity, + aoPassMap: gtaoPass.gtaoMap, + }); + const emissiveMaterial = new MeshPostProcessingMaterial({ + color: 0, + emissive: 0xffffff, + aoPassMap: gtaoPass.gtaoMap, + }); + plyLoader.load('models/ply/binary/Lucy100k.ply', geometry => { + geometry.computeVertexNormals(); + const lucy = new THREE.Mesh(geometry, objectMaterial); + lucy.receiveShadow = true; + lucy.castShadow = true; + lucy.scale.setScalar(0.001); + lucy.rotation.set(0, Math.PI, 0); + lucy.position.set(0.04, 1.8, 0.02); + scene.add(lucy); + }); + const ambientLight = new THREE.AmbientLight(0xffffff, sceneParameters.ambientLightIntensity); + const lightGroup = new THREE.Group(); + const planeGeometry = new THREE.PlaneGeometry(6, 6); + const cylinderGeometry = new THREE.CylinderGeometry(0.5, 0.5, 1, 64); + const sphereGeometry = new THREE.SphereGeometry(0.5, 32, 32); + const lightSphereGeometry = new THREE.SphereGeometry(0.1, 32, 32); + scene.background = new THREE.Color(0xbfe3dd); + scene.add(ambientLight); + scene.add(lightGroup); + const targetObject = new THREE.Object3D(); + targetObject.position.set(0, 1, 0); + scene.add(targetObject); + const lightColors = [0xff4040, 0x40ff40, 0x4040ff]; + for (let j = 0; j < 3; ++j) { + const light = new THREE.SpotLight(lightColors[j], sceneParameters.lightIntensity, 0, Math.PI / 9); + light.castShadow = true; + light.shadow.camera.far = 15; + light.position.set(5 * Math.cos((Math.PI * j * 2) / 3), 2.5, 5 * Math.sin((Math.PI * j * 2) / 3)); + light.target = targetObject; + lightGroup.add(light); + } + + const groundPlane = new THREE.Mesh(planeGeometry, groundMaterial); + groundPlane.rotation.x = -Math.PI / 2; + groundPlane.position.set(0, 0, 0); + groundPlane.receiveShadow = true; + scene.add(groundPlane); + const pedestal = new THREE.Mesh(cylinderGeometry, groundMaterial); + pedestal.position.set(0, 0.5, 0); + pedestal.receiveShadow = true; + pedestal.castShadow = true; + scene.add(pedestal); + const sphereMesh = new THREE.InstancedMesh(sphereGeometry, objectMaterial, 6); + sphereMesh.receiveShadow = true; + sphereMesh.castShadow = true; + scene.add(sphereMesh); + [...Array(6).keys()].forEach(i => + sphereMesh.setMatrixAt( + i, + new THREE.Matrix4().makeTranslation(Math.cos((Math.PI * i) / 3), 0.5, Math.sin((Math.PI * i) / 3)), + ), + ); + const lightSphereMesh = new THREE.InstancedMesh(lightSphereGeometry, emissiveMaterial, 4); + scene.add(lightSphereMesh); + [...Array(4).keys()].forEach(i => + lightSphereMesh.setMatrixAt( + i, + new THREE.Matrix4().makeTranslation( + 0.4 * Math.cos((Math.PI * (i + 0.5)) / 2), + 1.1, + 0.45 * Math.sin((Math.PI * (i + 0.5)) / 2), + ), + ), + ); + + const updateGtaoMaterial = () => gtaoPass.updateGtaoMaterial(aoParameters); + const updateOutput = () => { + composer.removePass(gtaoPass); + composer.insertPass(gtaoPass, sceneParameters.output == 1 ? 1 : 0); + + switch (sceneParameters.output) { + default: + case 0: + gtaoPass.output = GTAOPass.OUTPUT.Off; + gtaoPass.enabled = true; + renderPasse.enabled = true; + break; + case 1: + gtaoPass.output = GTAOPass.OUTPUT.Default; + gtaoPass.enabled = true; + renderPasse.enabled = true; + break; + case 2: + gtaoPass.output = GTAOPass.OUTPUT.Diffuse; + gtaoPass.enabled = false; + renderPasse.enabled = true; + break; + case 3: + gtaoPass.output = GTAOPass.OUTPUT.Denoise; + gtaoPass.enabled = true; + renderPasse.enabled = false; + break; + } + + groundMaterial.aoPassMap = sceneParameters.output === 0 ? gtaoPass.gtaoMap : null; + objectMaterial.aoPassMap = sceneParameters.output === 0 ? gtaoPass.gtaoMap : null; + }; + + updateOutput(); + updateGtaoMaterial(); + + const gui = new GUI(); + gui.add(sceneParameters, 'output', { + 'material AO': 0, + 'post blended AO': 1, + 'only diffuse': 2, + 'only AO': 3, + }).onChange(() => updateOutput()); + gui.add(sceneParameters, 'envMapIntensity') + .min(0) + .max(1) + .step(0.01) + .onChange(() => { + groundMaterial.envMapIntensity = sceneParameters.envMapIntensity; + objectMaterial.envMapIntensity = sceneParameters.envMapIntensity; + }); + gui.add(sceneParameters, 'ambientLightIntensity') + .min(0.0) + .max(1.0) + .step(0.01) + .onChange(() => { + ambientLight.intensity = sceneParameters.ambientLightIntensity; + }); + gui.add(sceneParameters, 'lightIntensity') + .min(0) + .max(100) + .step(1) + .onChange(() => { + lightGroup.children.forEach(light => (light.intensity = sceneParameters.lightIntensity)); + }); + gui.add(sceneParameters, 'shadow').onChange(value => { + renderer.shadowMap.enabled = value; + lightGroup.children.forEach(light => (light.castShadow = value)); + }); + gui.add(aoParameters, 'radius') + .min(0.01) + .max(2) + .step(0.01) + .onChange(() => updateGtaoMaterial()); + gui.add(aoParameters, 'distanceExponent') + .min(1) + .max(4) + .step(0.01) + .onChange(() => updateGtaoMaterial()); + gui.add(aoParameters, 'thickness') + .min(0.01) + .max(10) + .step(0.01) + .onChange(() => updateGtaoMaterial()); + gui.add(aoParameters, 'distanceFallOff') + .min(0) + .max(1) + .step(0.01) + .onChange(() => updateGtaoMaterial()); + gui.add(aoParameters, 'scale') + .min(0.01) + .max(2.0) + .step(0.01) + .onChange(() => updateGtaoMaterial()); + gui.add(aoParameters, 'samples') + .min(2) + .max(32) + .step(1) + .onChange(() => updateGtaoMaterial()); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); + composer.setSize(width, height); +} + +function animate() { + controls.update(); + stats.begin(); + composer.render(); + stats.end(); +} diff --git a/examples-testing/examples/webgl_postprocessing_outline.ts b/examples-testing/examples/webgl_postprocessing_outline.ts new file mode 100644 index 000000000..356575460 --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_outline.ts @@ -0,0 +1,282 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js'; +import { OutlinePass } from 'three/addons/postprocessing/OutlinePass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; +import { FXAAShader } from 'three/addons/shaders/FXAAShader.js'; + +let container, stats; +let camera, scene, renderer, controls; +let composer, effectFXAA, outlinePass; + +let selectedObjects = []; + +const raycaster = new THREE.Raycaster(); +const mouse = new THREE.Vector2(); + +const obj3d = new THREE.Object3D(); +const group = new THREE.Group(); + +const params = { + edgeStrength: 3.0, + edgeGlow: 0.0, + edgeThickness: 1.0, + pulsePeriod: 0, + rotate: false, + usePatternTexture: false, +}; + +// Init gui + +const gui = new GUI({ width: 280 }); + +gui.add(params, 'edgeStrength', 0.01, 10).onChange(function (value) { + outlinePass.edgeStrength = Number(value); +}); + +gui.add(params, 'edgeGlow', 0.0, 1).onChange(function (value) { + outlinePass.edgeGlow = Number(value); +}); + +gui.add(params, 'edgeThickness', 1, 4).onChange(function (value) { + outlinePass.edgeThickness = Number(value); +}); + +gui.add(params, 'pulsePeriod', 0.0, 5).onChange(function (value) { + outlinePass.pulsePeriod = Number(value); +}); + +gui.add(params, 'rotate'); + +gui.add(params, 'usePatternTexture').onChange(function (value) { + outlinePass.usePatternTexture = value; +}); + +function Configuration() { + this.visibleEdgeColor = '#ffffff'; + this.hiddenEdgeColor = '#190a05'; +} + +const conf = new Configuration(); + +gui.addColor(conf, 'visibleEdgeColor').onChange(function (value) { + outlinePass.visibleEdgeColor.set(value); +}); + +gui.addColor(conf, 'hiddenEdgeColor').onChange(function (value) { + outlinePass.hiddenEdgeColor.set(value); +}); + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + const width = window.innerWidth; + const height = window.innerHeight; + + renderer = new THREE.WebGLRenderer(); + renderer.shadowMap.enabled = true; + // todo - support pixelRatio in this demo + renderer.setSize(width, height); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100); + camera.position.set(0, 0, 8); + + controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 5; + controls.maxDistance = 20; + controls.enablePan = false; + controls.enableDamping = true; + controls.dampingFactor = 0.05; + + // + + scene.add(new THREE.AmbientLight(0xaaaaaa, 0.6)); + + const light = new THREE.DirectionalLight(0xddffdd, 2); + light.position.set(1, 1, 1); + light.castShadow = true; + light.shadow.mapSize.width = 1024; + light.shadow.mapSize.height = 1024; + + const d = 10; + + light.shadow.camera.left = -d; + light.shadow.camera.right = d; + light.shadow.camera.top = d; + light.shadow.camera.bottom = -d; + light.shadow.camera.far = 1000; + + scene.add(light); + + // model + + const loader = new OBJLoader(); + loader.load('models/obj/tree.obj', function (object) { + let scale = 1.0; + + object.traverse(function (child) { + if (child instanceof THREE.Mesh) { + child.geometry.center(); + child.geometry.computeBoundingSphere(); + scale = 0.2 * child.geometry.boundingSphere.radius; + + const phongMaterial = new THREE.MeshPhongMaterial({ + color: 0xffffff, + specular: 0x111111, + shininess: 5, + }); + child.material = phongMaterial; + child.receiveShadow = true; + child.castShadow = true; + } + }); + + object.position.y = 1; + object.scale.divideScalar(scale); + obj3d.add(object); + }); + + scene.add(group); + + group.add(obj3d); + + // + + const geometry = new THREE.SphereGeometry(3, 48, 24); + + for (let i = 0; i < 20; i++) { + const material = new THREE.MeshLambertMaterial(); + material.color.setHSL(Math.random(), 1.0, 0.3); + + const mesh = new THREE.Mesh(geometry, material); + mesh.position.x = Math.random() * 4 - 2; + mesh.position.y = Math.random() * 4 - 2; + mesh.position.z = Math.random() * 4 - 2; + mesh.receiveShadow = true; + mesh.castShadow = true; + mesh.scale.multiplyScalar(Math.random() * 0.3 + 0.1); + group.add(mesh); + } + + const floorMaterial = new THREE.MeshLambertMaterial({ side: THREE.DoubleSide }); + + const floorGeometry = new THREE.PlaneGeometry(12, 12); + const floorMesh = new THREE.Mesh(floorGeometry, floorMaterial); + floorMesh.rotation.x -= Math.PI * 0.5; + floorMesh.position.y -= 1.5; + group.add(floorMesh); + floorMesh.receiveShadow = true; + + const torusGeometry = new THREE.TorusGeometry(1, 0.3, 16, 100); + const torusMaterial = new THREE.MeshPhongMaterial({ color: 0xffaaff }); + const torus = new THREE.Mesh(torusGeometry, torusMaterial); + torus.position.z = -4; + group.add(torus); + torus.receiveShadow = true; + torus.castShadow = true; + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // postprocessing + + composer = new EffectComposer(renderer); + + const renderPass = new RenderPass(scene, camera); + composer.addPass(renderPass); + + outlinePass = new OutlinePass(new THREE.Vector2(window.innerWidth, window.innerHeight), scene, camera); + composer.addPass(outlinePass); + + const textureLoader = new THREE.TextureLoader(); + textureLoader.load('textures/tri_pattern.jpg', function (texture) { + outlinePass.patternTexture = texture; + texture.wrapS = THREE.RepeatWrapping; + texture.wrapT = THREE.RepeatWrapping; + }); + + const outputPass = new OutputPass(); + composer.addPass(outputPass); + + effectFXAA = new ShaderPass(FXAAShader); + effectFXAA.uniforms['resolution'].value.set(1 / window.innerWidth, 1 / window.innerHeight); + composer.addPass(effectFXAA); + + window.addEventListener('resize', onWindowResize); + + renderer.domElement.style.touchAction = 'none'; + renderer.domElement.addEventListener('pointermove', onPointerMove); + + function onPointerMove(event) { + if (event.isPrimary === false) return; + + mouse.x = (event.clientX / window.innerWidth) * 2 - 1; + mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; + + checkIntersection(); + } + + function addSelectedObject(object) { + selectedObjects = []; + selectedObjects.push(object); + } + + function checkIntersection() { + raycaster.setFromCamera(mouse, camera); + + const intersects = raycaster.intersectObject(scene, true); + + if (intersects.length > 0) { + const selectedObject = intersects[0].object; + addSelectedObject(selectedObject); + outlinePass.selectedObjects = selectedObjects; + } else { + // outlinePass.selectedObjects = []; + } + } +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); + composer.setSize(width, height); + + effectFXAA.uniforms['resolution'].value.set(1 / window.innerWidth, 1 / window.innerHeight); +} + +function animate() { + stats.begin(); + + const timer = performance.now(); + + if (params.rotate) { + group.rotation.y = timer * 0.0001; + } + + controls.update(); + + composer.render(); + + stats.end(); +} diff --git a/examples-testing/examples/webgl_postprocessing_pixel.ts b/examples-testing/examples/webgl_postprocessing_pixel.ts new file mode 100644 index 000000000..15b54d072 --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_pixel.ts @@ -0,0 +1,228 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPixelatedPass } from 'three/addons/postprocessing/RenderPixelatedPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, composer, crystalMesh, clock; +let gui, params; + +init(); + +function init() { + const aspectRatio = window.innerWidth / window.innerHeight; + + camera = new THREE.OrthographicCamera(-aspectRatio, aspectRatio, 1, -1, 0.1, 10); + camera.position.y = 2 * Math.tan(Math.PI / 6); + camera.position.z = 2; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x151729); + + clock = new THREE.Clock(); + + renderer = new THREE.WebGLRenderer(); + renderer.shadowMap.enabled = true; + //renderer.setPixelRatio( window.devicePixelRatio ); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + composer = new EffectComposer(renderer); + const renderPixelatedPass = new RenderPixelatedPass(6, scene, camera); + composer.addPass(renderPixelatedPass); + + const outputPass = new OutputPass(); + composer.addPass(outputPass); + + window.addEventListener('resize', onWindowResize); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.maxZoom = 2; + + // gui + + gui = new GUI(); + params = { pixelSize: 6, normalEdgeStrength: 0.3, depthEdgeStrength: 0.4, pixelAlignedPanning: true }; + gui.add(params, 'pixelSize') + .min(1) + .max(16) + .step(1) + .onChange(() => { + renderPixelatedPass.setPixelSize(params.pixelSize); + }); + gui.add(renderPixelatedPass, 'normalEdgeStrength').min(0).max(2).step(0.05); + gui.add(renderPixelatedPass, 'depthEdgeStrength').min(0).max(1).step(0.05); + gui.add(params, 'pixelAlignedPanning'); + + // textures + + const loader = new THREE.TextureLoader(); + const texChecker = pixelTexture(loader.load('textures/checker.png')); + const texChecker2 = pixelTexture(loader.load('textures/checker.png')); + texChecker.repeat.set(3, 3); + texChecker2.repeat.set(1.5, 1.5); + + // meshes + + const boxMaterial = new THREE.MeshPhongMaterial({ map: texChecker2 }); + + function addBox(boxSideLength, x, z, rotation) { + const mesh = new THREE.Mesh(new THREE.BoxGeometry(boxSideLength, boxSideLength, boxSideLength), boxMaterial); + mesh.castShadow = true; + mesh.receiveShadow = true; + mesh.rotation.y = rotation; + mesh.position.y = boxSideLength / 2; + mesh.position.set(x, boxSideLength / 2 + 0.0001, z); + scene.add(mesh); + return mesh; + } + + addBox(0.4, 0, 0, Math.PI / 4); + addBox(0.5, -0.5, -0.5, Math.PI / 4); + + const planeSideLength = 2; + const planeMesh = new THREE.Mesh( + new THREE.PlaneGeometry(planeSideLength, planeSideLength), + new THREE.MeshPhongMaterial({ map: texChecker }), + ); + planeMesh.receiveShadow = true; + planeMesh.rotation.x = -Math.PI / 2; + scene.add(planeMesh); + + const radius = 0.2; + const geometry = new THREE.IcosahedronGeometry(radius); + crystalMesh = new THREE.Mesh( + geometry, + new THREE.MeshPhongMaterial({ + color: 0x68b7e9, + emissive: 0x4f7e8b, + shininess: 10, + specular: 0xffffff, + }), + ); + crystalMesh.receiveShadow = true; + crystalMesh.castShadow = true; + scene.add(crystalMesh); + + // lights + + scene.add(new THREE.AmbientLight(0x757f8e, 3)); + + const directionalLight = new THREE.DirectionalLight(0xfffecd, 1.5); + directionalLight.position.set(100, 100, 100); + directionalLight.castShadow = true; + directionalLight.shadow.mapSize.set(2048, 2048); + scene.add(directionalLight); + + const spotLight = new THREE.SpotLight(0xffc100, 10, 10, Math.PI / 16, 0.02, 2); + spotLight.position.set(2, 2, 0); + const target = spotLight.target; + scene.add(target); + target.position.set(0, 0, 0); + spotLight.castShadow = true; + scene.add(spotLight); +} + +function onWindowResize() { + const aspectRatio = window.innerWidth / window.innerHeight; + camera.left = -aspectRatio; + camera.right = aspectRatio; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + composer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const t = clock.getElapsedTime(); + + crystalMesh.material.emissiveIntensity = Math.sin(t * 3) * 0.5 + 0.5; + crystalMesh.position.y = 0.7 + Math.sin(t * 2) * 0.05; + crystalMesh.rotation.y = stopGoEased(t, 2, 4) * 2 * Math.PI; + + const rendererSize = renderer.getSize(new THREE.Vector2()); + const aspectRatio = rendererSize.x / rendererSize.y; + if (params['pixelAlignedPanning']) { + pixelAlignFrustum( + camera, + aspectRatio, + Math.floor(rendererSize.x / params['pixelSize']), + Math.floor(rendererSize.y / params['pixelSize']), + ); + } else if (camera.left != -aspectRatio || camera.top != 1.0) { + // Reset the Camera Frustum if it has been modified + camera.left = -aspectRatio; + camera.right = aspectRatio; + camera.top = 1.0; + camera.bottom = -1.0; + camera.updateProjectionMatrix(); + } + + composer.render(); +} + +// Helper functions + +function pixelTexture(texture) { + texture.minFilter = THREE.NearestFilter; + texture.magFilter = THREE.NearestFilter; + texture.generateMipmaps = false; + texture.wrapS = THREE.RepeatWrapping; + texture.wrapT = THREE.RepeatWrapping; + texture.colorSpace = THREE.SRGBColorSpace; + return texture; +} + +function easeInOutCubic(x) { + return x ** 2 * 3 - x ** 3 * 2; +} + +function linearStep(x, edge0, edge1) { + const w = edge1 - edge0; + const m = 1 / w; + const y0 = -m * edge0; + return THREE.MathUtils.clamp(y0 + m * x, 0, 1); +} + +function stopGoEased(x, downtime, period) { + const cycle = (x / period) | 0; + const tween = x - cycle * period; + const linStep = easeInOutCubic(linearStep(tween, downtime, period)); + return cycle + linStep; +} + +function pixelAlignFrustum(camera, aspectRatio, pixelsPerScreenWidth, pixelsPerScreenHeight) { + // 0. Get Pixel Grid Units + const worldScreenWidth = (camera.right - camera.left) / camera.zoom; + const worldScreenHeight = (camera.top - camera.bottom) / camera.zoom; + const pixelWidth = worldScreenWidth / pixelsPerScreenWidth; + const pixelHeight = worldScreenHeight / pixelsPerScreenHeight; + + // 1. Project the current camera position along its local rotation bases + const camPos = new THREE.Vector3(); + camera.getWorldPosition(camPos); + const camRot = new THREE.Quaternion(); + camera.getWorldQuaternion(camRot); + const camRight = new THREE.Vector3(1.0, 0.0, 0.0).applyQuaternion(camRot); + const camUp = new THREE.Vector3(0.0, 1.0, 0.0).applyQuaternion(camRot); + const camPosRight = camPos.dot(camRight); + const camPosUp = camPos.dot(camUp); + + // 2. Find how far along its position is along these bases in pixel units + const camPosRightPx = camPosRight / pixelWidth; + const camPosUpPx = camPosUp / pixelHeight; + + // 3. Find the fractional pixel units and convert to world units + const fractX = camPosRightPx - Math.round(camPosRightPx); + const fractY = camPosUpPx - Math.round(camPosUpPx); + + // 4. Add fractional world units to the left/right top/bottom to align with the pixel grid + camera.left = -aspectRatio - fractX * pixelWidth; + camera.right = aspectRatio - fractX * pixelWidth; + camera.top = 1.0 - fractY * pixelHeight; + camera.bottom = -1.0 - fractY * pixelHeight; + camera.updateProjectionMatrix(); +} diff --git a/examples-testing/examples/webgl_postprocessing_procedural.ts b/examples-testing/examples/webgl_postprocessing_procedural.ts new file mode 100644 index 000000000..869824270 --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_procedural.ts @@ -0,0 +1,77 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let postCamera, postScene, renderer; +let postMaterial, noiseRandom1DMaterial, noiseRandom2DMaterial, noiseRandom3DMaterial, postQuad; +let stats; + +const params = { procedure: 'noiseRandom3D' }; + +init(); + +function init() { + const container = document.getElementById('container'); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + // Setup post processing stage + postCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1); + noiseRandom1DMaterial = new THREE.ShaderMaterial({ + vertexShader: document.querySelector('#procedural-vert').textContent.trim(), + fragmentShader: document.querySelector('#noiseRandom1D-frag').textContent.trim(), + }); + noiseRandom2DMaterial = new THREE.ShaderMaterial({ + vertexShader: document.querySelector('#procedural-vert').textContent.trim(), + fragmentShader: document.querySelector('#noiseRandom2D-frag').textContent.trim(), + }); + noiseRandom3DMaterial = new THREE.ShaderMaterial({ + vertexShader: document.querySelector('#procedural-vert').textContent.trim(), + fragmentShader: document.querySelector('#noiseRandom3D-frag').textContent.trim(), + }); + postMaterial = noiseRandom3DMaterial; + const postPlane = new THREE.PlaneGeometry(2, 2); + postQuad = new THREE.Mesh(postPlane, postMaterial); + postScene = new THREE.Scene(); + postScene.add(postQuad); + + window.addEventListener('resize', onWindowResize); + + // + + const gui = new GUI(); + gui.add(params, 'procedure', ['noiseRandom1D', 'noiseRandom2D', 'noiseRandom3D']); +} + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + switch (params.procedure) { + case 'noiseRandom1D': + postMaterial = noiseRandom1DMaterial; + break; + case 'noiseRandom2D': + postMaterial = noiseRandom2DMaterial; + break; + case 'noiseRandom3D': + postMaterial = noiseRandom3DMaterial; + break; + } + + postQuad.material = postMaterial; + + // render post FX + renderer.render(postScene, postCamera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_postprocessing_rgb_halftone.ts b/examples-testing/examples/webgl_postprocessing_rgb_halftone.ts new file mode 100644 index 000000000..fa46d4c8d --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_rgb_halftone.ts @@ -0,0 +1,167 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { HalftonePass } from 'three/addons/postprocessing/HalftonePass.js'; + +let renderer, clock, camera, stats; + +const rotationSpeed = Math.PI / 64; + +let composer, group; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + + clock = new THREE.Clock(); + + camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 12; + + stats = new Stats(); + + document.body.appendChild(renderer.domElement); + document.body.appendChild(stats.dom); + + // camera controls + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 0, 0); + controls.update(); + + // scene + + const scene = new THREE.Scene(); + scene.background = new THREE.Color(0x444444); + + group = new THREE.Group(); + const floor = new THREE.Mesh(new THREE.BoxGeometry(100, 1, 100), new THREE.MeshPhongMaterial({})); + floor.position.y = -10; + const light = new THREE.PointLight(0xffffff, 250); + light.position.y = 2; + group.add(floor, light); + scene.add(group); + + const mat = new THREE.ShaderMaterial({ + uniforms: {}, + + vertexShader: [ + 'varying vec2 vUV;', + 'varying vec3 vNormal;', + + 'void main() {', + + 'vUV = uv;', + 'vNormal = vec3( normal );', + 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );', + + '}', + ].join('\n'), + + fragmentShader: [ + 'varying vec2 vUV;', + 'varying vec3 vNormal;', + + 'void main() {', + + 'vec4 c = vec4( abs( vNormal ) + vec3( vUV, 0.0 ), 0.0 );', + 'gl_FragColor = c;', + + '}', + ].join('\n'), + }); + + for (let i = 0; i < 50; ++i) { + // fill scene with coloured cubes + const mesh = new THREE.Mesh(new THREE.BoxGeometry(2, 2, 2), mat); + mesh.position.set(Math.random() * 16 - 8, Math.random() * 16 - 8, Math.random() * 16 - 8); + mesh.rotation.set(Math.random() * Math.PI * 2, Math.random() * Math.PI * 2, Math.random() * Math.PI * 2); + group.add(mesh); + } + + // post-processing + + composer = new EffectComposer(renderer); + const renderPass = new RenderPass(scene, camera); + const params = { + shape: 1, + radius: 4, + rotateR: Math.PI / 12, + rotateB: (Math.PI / 12) * 2, + rotateG: (Math.PI / 12) * 3, + scatter: 0, + blending: 1, + blendingMode: 1, + greyscale: false, + disable: false, + }; + const halftonePass = new HalftonePass(window.innerWidth, window.innerHeight, params); + composer.addPass(renderPass); + composer.addPass(halftonePass); + + window.onresize = function () { + // resize composer + renderer.setSize(window.innerWidth, window.innerHeight); + composer.setSize(window.innerWidth, window.innerHeight); + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + }; + + // GUI + + const controller = { + radius: halftonePass.uniforms['radius'].value, + rotateR: halftonePass.uniforms['rotateR'].value / (Math.PI / 180), + rotateG: halftonePass.uniforms['rotateG'].value / (Math.PI / 180), + rotateB: halftonePass.uniforms['rotateB'].value / (Math.PI / 180), + scatter: halftonePass.uniforms['scatter'].value, + shape: halftonePass.uniforms['shape'].value, + greyscale: halftonePass.uniforms['greyscale'].value, + blending: halftonePass.uniforms['blending'].value, + blendingMode: halftonePass.uniforms['blendingMode'].value, + disable: halftonePass.uniforms['disable'].value, + }; + + function onGUIChange() { + // update uniforms + halftonePass.uniforms['radius'].value = controller.radius; + halftonePass.uniforms['rotateR'].value = controller.rotateR * (Math.PI / 180); + halftonePass.uniforms['rotateG'].value = controller.rotateG * (Math.PI / 180); + halftonePass.uniforms['rotateB'].value = controller.rotateB * (Math.PI / 180); + halftonePass.uniforms['scatter'].value = controller.scatter; + halftonePass.uniforms['shape'].value = controller.shape; + halftonePass.uniforms['greyscale'].value = controller.greyscale; + halftonePass.uniforms['blending'].value = controller.blending; + halftonePass.uniforms['blendingMode'].value = controller.blendingMode; + halftonePass.uniforms['disable'].value = controller.disable; + } + + const gui = new GUI(); + gui.add(controller, 'shape', { Dot: 1, Ellipse: 2, Line: 3, Square: 4 }).onChange(onGUIChange); + gui.add(controller, 'radius', 1, 25).onChange(onGUIChange); + gui.add(controller, 'rotateR', 0, 90).onChange(onGUIChange); + gui.add(controller, 'rotateG', 0, 90).onChange(onGUIChange); + gui.add(controller, 'rotateB', 0, 90).onChange(onGUIChange); + gui.add(controller, 'scatter', 0, 1, 0.01).onChange(onGUIChange); + gui.add(controller, 'greyscale').onChange(onGUIChange); + gui.add(controller, 'blending', 0, 1, 0.01).onChange(onGUIChange); + gui.add(controller, 'blendingMode', { Linear: 1, Multiply: 2, Add: 3, Lighter: 4, Darker: 5 }).onChange( + onGUIChange, + ); + gui.add(controller, 'disable').onChange(onGUIChange); +} + +function animate() { + const delta = clock.getDelta(); + stats.update(); + group.rotation.y += delta * rotationSpeed; + composer.render(delta); +} diff --git a/examples-testing/examples/webgl_postprocessing_sao.ts b/examples-testing/examples/webgl_postprocessing_sao.ts new file mode 100644 index 000000000..bf40d026b --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_sao.ts @@ -0,0 +1,137 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { SAOPass } from 'three/addons/postprocessing/SAOPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +let container, stats; +let camera, scene, renderer; +let composer, renderPass, saoPass; +let group; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + const width = window.innerWidth; + const height = window.innerHeight; + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(width, height); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + camera = new THREE.PerspectiveCamera(65, width / height, 3, 10); + camera.position.z = 7; + + scene = new THREE.Scene(); + + group = new THREE.Object3D(); + scene.add(group); + + const light = new THREE.PointLight(0xefffef, 500); + light.position.z = 10; + light.position.y = -10; + light.position.x = -10; + scene.add(light); + + const light2 = new THREE.PointLight(0xffefef, 500); + light2.position.z = 10; + light2.position.x = -10; + light2.position.y = 10; + scene.add(light2); + + const light3 = new THREE.PointLight(0xefefff, 500); + light3.position.z = 10; + light3.position.x = 10; + light3.position.y = -10; + scene.add(light3); + + const light4 = new THREE.AmbientLight(0xffffff, 0.2); + scene.add(light4); + + const geometry = new THREE.SphereGeometry(3, 48, 24); + + for (let i = 0; i < 120; i++) { + const material = new THREE.MeshStandardMaterial(); + material.roughness = 0.5 * Math.random() + 0.25; + material.metalness = 0; + material.color.setHSL(Math.random(), 1.0, 0.3); + + const mesh = new THREE.Mesh(geometry, material); + mesh.position.x = Math.random() * 4 - 2; + mesh.position.y = Math.random() * 4 - 2; + mesh.position.z = Math.random() * 4 - 2; + mesh.rotation.x = Math.random(); + mesh.rotation.y = Math.random(); + mesh.rotation.z = Math.random(); + + mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 0.2 + 0.05; + group.add(mesh); + } + + stats = new Stats(); + container.appendChild(stats.dom); + + composer = new EffectComposer(renderer); + renderPass = new RenderPass(scene, camera); + composer.addPass(renderPass); + saoPass = new SAOPass(scene, camera); + composer.addPass(saoPass); + const outputPass = new OutputPass(); + composer.addPass(outputPass); + + // Init gui + const gui = new GUI(); + gui.add(saoPass.params, 'output', { + Default: SAOPass.OUTPUT.Default, + 'SAO Only': SAOPass.OUTPUT.SAO, + Normal: SAOPass.OUTPUT.Normal, + }).onChange(function (value) { + saoPass.params.output = value; + }); + gui.add(saoPass.params, 'saoBias', -1, 1); + gui.add(saoPass.params, 'saoIntensity', 0, 1); + gui.add(saoPass.params, 'saoScale', 0, 10); + gui.add(saoPass.params, 'saoKernelRadius', 1, 100); + gui.add(saoPass.params, 'saoMinResolution', 0, 1); + gui.add(saoPass.params, 'saoBlur'); + gui.add(saoPass.params, 'saoBlurRadius', 0, 200); + gui.add(saoPass.params, 'saoBlurStdDev', 0.5, 150); + gui.add(saoPass.params, 'saoBlurDepthCutoff', 0.0, 0.1); + gui.add(saoPass, 'enabled'); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const width = window.innerWidth || 1; + const height = window.innerHeight || 1; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + renderer.setSize(width, height); + + composer.setSize(width, height); +} + +function animate() { + stats.begin(); + render(); + stats.end(); +} + +function render() { + const timer = performance.now(); + group.rotation.x = timer * 0.0002; + group.rotation.y = timer * 0.0001; + + composer.render(); +} diff --git a/examples-testing/examples/webgl_postprocessing_smaa.ts b/examples-testing/examples/webgl_postprocessing_smaa.ts new file mode 100644 index 000000000..6f71f6478 --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_smaa.ts @@ -0,0 +1,109 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { SMAAPass } from 'three/addons/postprocessing/SMAAPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +let camera, scene, renderer, composer, stats, smaaPass; + +const params = { + enabled: true, + autoRotate: true, +}; + +init(); + +function init() { + const container = document.getElementById('container'); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 300; + + scene = new THREE.Scene(); + + const geometry = new THREE.BoxGeometry(120, 120, 120); + const material1 = new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: true }); + + const mesh1 = new THREE.Mesh(geometry, material1); + mesh1.position.x = -100; + scene.add(mesh1); + + const texture = new THREE.TextureLoader().load('textures/brick_diffuse.jpg'); + texture.anisotropy = renderer.capabilities.getMaxAnisotropy(); + texture.colorSpace = THREE.SRGBColorSpace; + + const material2 = new THREE.MeshBasicMaterial({ map: texture }); + + const mesh2 = new THREE.Mesh(geometry, material2); + mesh2.position.x = 100; + scene.add(mesh2); + + // postprocessing + + composer = new EffectComposer(renderer); + composer.addPass(new RenderPass(scene, camera)); + + smaaPass = new SMAAPass( + window.innerWidth * renderer.getPixelRatio(), + window.innerHeight * renderer.getPixelRatio(), + ); + composer.addPass(smaaPass); + + const outputPass = new OutputPass(); + composer.addPass(outputPass); + + window.addEventListener('resize', onWindowResize); + + const gui = new GUI(); + + const smaaFolder = gui.addFolder('SMAA'); + smaaFolder.add(params, 'enabled'); + + const sceneFolder = gui.addFolder('Scene'); + sceneFolder.add(params, 'autoRotate'); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); + composer.setSize(width, height); +} + +function animate() { + stats.begin(); + + if (params.autoRotate === true) { + for (let i = 0; i < scene.children.length; i++) { + const child = scene.children[i]; + + child.rotation.x += 0.005; + child.rotation.y += 0.01; + } + } + + smaaPass.enabled = params.enabled; + + composer.render(); + + stats.end(); +} diff --git a/examples-testing/examples/webgl_postprocessing_sobel.ts b/examples-testing/examples/webgl_postprocessing_sobel.ts new file mode 100644 index 000000000..55d88dc02 --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_sobel.ts @@ -0,0 +1,111 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js'; + +import { LuminosityShader } from 'three/addons/shaders/LuminosityShader.js'; +import { SobelOperatorShader } from 'three/addons/shaders/SobelOperatorShader.js'; + +let camera, scene, renderer, composer; + +let effectSobel; + +const params = { + enable: true, +}; + +init(); + +function init() { + // + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(0, 1, 3); + camera.lookAt(scene.position); + + // + + const geometry = new THREE.TorusKnotGeometry(1, 0.3, 256, 32); + const material = new THREE.MeshPhongMaterial({ color: 0xffff00 }); + + const mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + const ambientLight = new THREE.AmbientLight(0xe7e7e7); + scene.add(ambientLight); + + const pointLight = new THREE.PointLight(0xffffff, 20); + camera.add(pointLight); + scene.add(camera); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // postprocessing + + composer = new EffectComposer(renderer); + const renderPass = new RenderPass(scene, camera); + composer.addPass(renderPass); + + // color to grayscale conversion + + const effectGrayScale = new ShaderPass(LuminosityShader); + composer.addPass(effectGrayScale); + + // you might want to use a gaussian blur filter before + // the next pass to improve the result of the Sobel operator + + // Sobel operator + + effectSobel = new ShaderPass(SobelOperatorShader); + effectSobel.uniforms['resolution'].value.x = window.innerWidth * window.devicePixelRatio; + effectSobel.uniforms['resolution'].value.y = window.innerHeight * window.devicePixelRatio; + composer.addPass(effectSobel); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.enableZoom = false; + + // + + const gui = new GUI(); + + gui.add(params, 'enable'); + gui.open(); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + composer.setSize(window.innerWidth, window.innerHeight); + + effectSobel.uniforms['resolution'].value.x = window.innerWidth * window.devicePixelRatio; + effectSobel.uniforms['resolution'].value.y = window.innerHeight * window.devicePixelRatio; +} + +function animate() { + if (params.enable === true) { + composer.render(); + } else { + renderer.render(scene, camera); + } +} diff --git a/examples-testing/examples/webgl_postprocessing_ssaa.ts b/examples-testing/examples/webgl_postprocessing_ssaa.ts new file mode 100644 index 000000000..429e02dee --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_ssaa.ts @@ -0,0 +1,206 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { SSAARenderPass } from 'three/addons/postprocessing/SSAARenderPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +let scene, renderer, composer; +let cameraP, ssaaRenderPassP; +let cameraO, ssaaRenderPassO; +let gui, stats; + +const params = { + sampleLevel: 4, + unbiased: true, + camera: 'perspective', + clearColor: 'black', + clearAlpha: 1.0, + viewOffsetX: 0, + autoRotate: true, +}; + +init(); + +clearGui(); + +function clearGui() { + if (gui) gui.destroy(); + + gui = new GUI(); + + gui.add(params, 'unbiased'); + gui.add(params, 'sampleLevel', { + 'Level 0: 1 Sample': 0, + 'Level 1: 2 Samples': 1, + 'Level 2: 4 Samples': 2, + 'Level 3: 8 Samples': 3, + 'Level 4: 16 Samples': 4, + 'Level 5: 32 Samples': 5, + }); + gui.add(params, 'camera', ['perspective', 'orthographic']); + gui.add(params, 'clearColor', ['black', 'white', 'blue', 'green', 'red']); + gui.add(params, 'clearAlpha', 0, 1); + gui.add(params, 'viewOffsetX', -100, 100); + gui.add(params, 'autoRotate'); + + gui.open(); +} + +function init() { + const container = document.getElementById('container'); + + const width = window.innerWidth || 1; + const height = window.innerHeight || 1; + const aspect = width / height; + const devicePixelRatio = window.devicePixelRatio || 1; + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(devicePixelRatio); + renderer.setSize(width, height); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + cameraP = new THREE.PerspectiveCamera(65, aspect, 3, 10); + cameraP.position.z = 7; + cameraP.setViewOffset(width, height, params.viewOffsetX, 0, width, height); + + cameraO = new THREE.OrthographicCamera(width / -2, width / 2, height / 2, height / -2, 3, 10); + cameraO.position.z = 7; + + const fov = THREE.MathUtils.degToRad(cameraP.fov); + const hyperfocus = (cameraP.near + cameraP.far) / 2; + const _height = 2 * Math.tan(fov / 2) * hyperfocus; + cameraO.zoom = height / _height; + + scene = new THREE.Scene(); + + const group = new THREE.Group(); + scene.add(group); + + const light = new THREE.PointLight(0xefffef, 500); + light.position.z = 10; + light.position.y = -10; + light.position.x = -10; + scene.add(light); + + const light2 = new THREE.PointLight(0xffefef, 500); + light2.position.z = 10; + light2.position.x = -10; + light2.position.y = 10; + scene.add(light2); + + const light3 = new THREE.PointLight(0xefefff, 500); + light3.position.z = 10; + light3.position.x = 10; + light3.position.y = -10; + scene.add(light3); + + const light4 = new THREE.AmbientLight(0xffffff, 0.2); + scene.add(light4); + + const geometry = new THREE.SphereGeometry(3, 48, 24); + + for (let i = 0; i < 120; i++) { + const material = new THREE.MeshStandardMaterial(); + material.roughness = 0.5 * Math.random() + 0.25; + material.metalness = 0; + material.color.setHSL(Math.random(), 1.0, 0.3); + + const mesh = new THREE.Mesh(geometry, material); + mesh.position.x = Math.random() * 4 - 2; + mesh.position.y = Math.random() * 4 - 2; + mesh.position.z = Math.random() * 4 - 2; + mesh.rotation.x = Math.random(); + mesh.rotation.y = Math.random(); + mesh.rotation.z = Math.random(); + + mesh.scale.setScalar(Math.random() * 0.2 + 0.05); + group.add(mesh); + } + + // postprocessing + + composer = new EffectComposer(renderer); + composer.setPixelRatio(1); // ensure pixel ratio is always 1 for performance reasons + ssaaRenderPassP = new SSAARenderPass(scene, cameraP); + composer.addPass(ssaaRenderPassP); + ssaaRenderPassO = new SSAARenderPass(scene, cameraO); + composer.addPass(ssaaRenderPassO); + const outputPass = new OutputPass(); + composer.addPass(outputPass); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + const aspect = width / height; + + cameraP.aspect = aspect; + cameraP.setViewOffset(width, height, params.viewOffsetX, 0, width, height); + cameraO.updateProjectionMatrix(); + + cameraO.left = -height * aspect; + cameraO.right = height * aspect; + cameraO.top = height; + cameraO.bottom = -height; + cameraO.updateProjectionMatrix(); + + renderer.setSize(width, height); + composer.setSize(width, height); +} + +function animate() { + stats.begin(); + + if (params.autoRotate) { + for (let i = 0; i < scene.children.length; i++) { + const child = scene.children[i]; + + child.rotation.x += 0.005; + child.rotation.y += 0.01; + } + } + + let newColor = ssaaRenderPassP.clearColor; + + switch (params.clearColor) { + case 'blue': + newColor = 0x0000ff; + break; + case 'red': + newColor = 0xff0000; + break; + case 'green': + newColor = 0x00ff00; + break; + case 'white': + newColor = 0xffffff; + break; + case 'black': + newColor = 0x000000; + break; + } + + ssaaRenderPassP.clearColor = ssaaRenderPassO.clearColor = newColor; + ssaaRenderPassP.clearAlpha = ssaaRenderPassO.clearAlpha = params.clearAlpha; + + ssaaRenderPassP.sampleLevel = ssaaRenderPassO.sampleLevel = params.sampleLevel; + ssaaRenderPassP.unbiased = ssaaRenderPassO.unbiased = params.unbiased; + + ssaaRenderPassP.enabled = params.camera === 'perspective'; + ssaaRenderPassO.enabled = params.camera === 'orthographic'; + + cameraP.view.offsetX = params.viewOffsetX; + + composer.render(); + + stats.end(); +} diff --git a/examples-testing/examples/webgl_postprocessing_ssao.ts b/examples-testing/examples/webgl_postprocessing_ssao.ts new file mode 100644 index 000000000..e55ab0446 --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_ssao.ts @@ -0,0 +1,118 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { SSAOPass } from 'three/addons/postprocessing/SSAOPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +let container, stats; +let camera, scene, renderer; +let composer; +let group; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + renderer = new THREE.WebGLRenderer(); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + camera = new THREE.PerspectiveCamera(65, window.innerWidth / window.innerHeight, 100, 700); + camera.position.z = 500; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xaaaaaa); + + scene.add(new THREE.DirectionalLight(0xffffff, 4)); + scene.add(new THREE.AmbientLight(0xffffff)); + + group = new THREE.Group(); + scene.add(group); + + const geometry = new THREE.BoxGeometry(10, 10, 10); + + for (let i = 0; i < 100; i++) { + const material = new THREE.MeshLambertMaterial({ + color: Math.random() * 0xffffff, + }); + + const mesh = new THREE.Mesh(geometry, material); + mesh.position.x = Math.random() * 400 - 200; + mesh.position.y = Math.random() * 400 - 200; + mesh.position.z = Math.random() * 400 - 200; + mesh.rotation.x = Math.random(); + mesh.rotation.y = Math.random(); + mesh.rotation.z = Math.random(); + + mesh.scale.setScalar(Math.random() * 10 + 2); + group.add(mesh); + } + + stats = new Stats(); + container.appendChild(stats.dom); + + const width = window.innerWidth; + const height = window.innerHeight; + + composer = new EffectComposer(renderer); + + const renderPass = new RenderPass(scene, camera); + composer.addPass(renderPass); + + const ssaoPass = new SSAOPass(scene, camera, width, height); + composer.addPass(ssaoPass); + + const outputPass = new OutputPass(); + composer.addPass(outputPass); + + // Init gui + const gui = new GUI(); + + gui.add(ssaoPass, 'output', { + Default: SSAOPass.OUTPUT.Default, + 'SSAO Only': SSAOPass.OUTPUT.SSAO, + 'SSAO Only + Blur': SSAOPass.OUTPUT.Blur, + Depth: SSAOPass.OUTPUT.Depth, + Normal: SSAOPass.OUTPUT.Normal, + }).onChange(function (value) { + ssaoPass.output = value; + }); + gui.add(ssaoPass, 'kernelRadius').min(0).max(32); + gui.add(ssaoPass, 'minDistance').min(0.001).max(0.02); + gui.add(ssaoPass, 'maxDistance').min(0.01).max(0.3); + gui.add(ssaoPass, 'enabled'); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); + composer.setSize(width, height); +} + +function animate() { + stats.begin(); + render(); + stats.end(); +} + +function render() { + const timer = performance.now(); + group.rotation.x = timer * 0.0002; + group.rotation.y = timer * 0.0001; + + composer.render(); +} diff --git a/examples-testing/examples/webgl_postprocessing_ssr.ts b/examples-testing/examples/webgl_postprocessing_ssr.ts new file mode 100644 index 000000000..307cfd1de --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_ssr.ts @@ -0,0 +1,261 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { SSRPass } from 'three/addons/postprocessing/SSRPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; +import { ReflectorForSSRPass } from 'three/addons/objects/ReflectorForSSRPass.js'; + +import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; + +const params = { + enableSSR: true, + autoRotate: true, + otherMeshes: true, + groundReflector: true, +}; +let composer; +let ssrPass; +let gui; +let stats; +let controls; +let camera, scene, renderer; +const otherMeshes = []; +let groundReflector; +const selects = []; + +const container = document.querySelector('#container'); + +// Configure and create Draco decoder. +const dracoLoader = new DRACOLoader(); +dracoLoader.setDecoderPath('jsm/libs/draco/'); +dracoLoader.setDecoderConfig({ type: 'js' }); + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 15); + camera.position.set(0.13271600513224902, 0.3489546826045913, 0.43921296427927076); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x443333); + scene.fog = new THREE.Fog(0x443333, 1, 4); + + // Ground + const plane = new THREE.Mesh(new THREE.PlaneGeometry(8, 8), new THREE.MeshPhongMaterial({ color: 0xcbcbcb })); + plane.rotation.x = -Math.PI / 2; + plane.position.y = -0.0001; + // plane.receiveShadow = true; + scene.add(plane); + + // Lights + const hemiLight = new THREE.HemisphereLight(0x8d7c7c, 0x494966, 3); + scene.add(hemiLight); + + const spotLight = new THREE.SpotLight(); + spotLight.intensity = 8; + spotLight.angle = Math.PI / 16; + spotLight.penumbra = 0.5; + // spotLight.castShadow = true; + spotLight.position.set(-1, 1, 1); + scene.add(spotLight); + + dracoLoader.load('models/draco/bunny.drc', function (geometry) { + geometry.computeVertexNormals(); + + const material = new THREE.MeshStandardMaterial({ color: 0xa5a5a5 }); + const mesh = new THREE.Mesh(geometry, material); + mesh.position.y = -0.0365; + scene.add(mesh); + selects.push(mesh); + + // Release decoder resources. + dracoLoader.dispose(); + }); + + let geometry, material, mesh; + + geometry = new THREE.BoxGeometry(0.05, 0.05, 0.05); + material = new THREE.MeshStandardMaterial({ color: 'green' }); + mesh = new THREE.Mesh(geometry, material); + mesh.position.set(-0.12, 0.025, 0.015); + scene.add(mesh); + otherMeshes.push(mesh); + selects.push(mesh); + + geometry = new THREE.IcosahedronGeometry(0.025, 4); + material = new THREE.MeshStandardMaterial({ color: 'cyan' }); + mesh = new THREE.Mesh(geometry, material); + mesh.position.set(-0.05, 0.025, 0.08); + scene.add(mesh); + otherMeshes.push(mesh); + selects.push(mesh); + + geometry = new THREE.ConeGeometry(0.025, 0.05, 64); + material = new THREE.MeshStandardMaterial({ color: 'yellow' }); + mesh = new THREE.Mesh(geometry, material); + mesh.position.set(-0.05, 0.025, -0.055); + scene.add(mesh); + otherMeshes.push(mesh); + selects.push(mesh); + + geometry = new THREE.PlaneGeometry(1, 1); + groundReflector = new ReflectorForSSRPass(geometry, { + clipBias: 0.0003, + textureWidth: window.innerWidth, + textureHeight: window.innerHeight, + color: 0x888888, + useDepthTexture: true, + }); + groundReflector.material.depthWrite = false; + groundReflector.rotation.x = -Math.PI / 2; + groundReflector.visible = false; + scene.add(groundReflector); + + // renderer + renderer = new THREE.WebGLRenderer({ antialias: false }); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + controls = new OrbitControls(camera, renderer.domElement); + controls.enableDamping = true; + controls.target.set(0, 0.0635, 0); + controls.update(); + controls.enabled = !params.autoRotate; + + // STATS + + stats = new Stats(); + container.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); + + // composer + + composer = new EffectComposer(renderer); + ssrPass = new SSRPass({ + renderer, + scene, + camera, + width: innerWidth, + height: innerHeight, + groundReflector: params.groundReflector ? groundReflector : null, + selects: params.groundReflector ? selects : null, + }); + + composer.addPass(ssrPass); + composer.addPass(new OutputPass()); + + // GUI + + gui = new GUI({ width: 260 }); + gui.add(params, 'enableSSR').name('Enable SSR'); + gui.add(params, 'groundReflector').onChange(() => { + if (params.groundReflector) { + (ssrPass.groundReflector = groundReflector), (ssrPass.selects = selects); + } else { + (ssrPass.groundReflector = null), (ssrPass.selects = null); + } + }); + ssrPass.thickness = 0.018; + gui.add(ssrPass, 'thickness').min(0).max(0.1).step(0.0001); + ssrPass.infiniteThick = false; + gui.add(ssrPass, 'infiniteThick'); + gui.add(params, 'autoRotate').onChange(() => { + controls.enabled = !params.autoRotate; + }); + + const folder = gui.addFolder('more settings'); + folder.add(ssrPass, 'fresnel').onChange(() => { + groundReflector.fresnel = ssrPass.fresnel; + }); + folder.add(ssrPass, 'distanceAttenuation').onChange(() => { + groundReflector.distanceAttenuation = ssrPass.distanceAttenuation; + }); + ssrPass.maxDistance = 0.1; + groundReflector.maxDistance = ssrPass.maxDistance; + folder + .add(ssrPass, 'maxDistance') + .min(0) + .max(0.5) + .step(0.001) + .onChange(() => { + groundReflector.maxDistance = ssrPass.maxDistance; + }); + folder.add(params, 'otherMeshes').onChange(() => { + if (params.otherMeshes) { + otherMeshes.forEach(mesh => (mesh.visible = true)); + } else { + otherMeshes.forEach(mesh => (mesh.visible = false)); + } + }); + folder.add(ssrPass, 'bouncing'); + folder + .add(ssrPass, 'output', { + Default: SSRPass.OUTPUT.Default, + 'SSR Only': SSRPass.OUTPUT.SSR, + Beauty: SSRPass.OUTPUT.Beauty, + Depth: SSRPass.OUTPUT.Depth, + Normal: SSRPass.OUTPUT.Normal, + Metalness: SSRPass.OUTPUT.Metalness, + }) + .onChange(function (value) { + ssrPass.output = value; + }); + ssrPass.opacity = 1; + groundReflector.opacity = ssrPass.opacity; + folder + .add(ssrPass, 'opacity') + .min(0) + .max(1) + .onChange(() => { + groundReflector.opacity = ssrPass.opacity; + }); + folder.add(ssrPass, 'blur'); + // folder.open() + // gui.close() +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + composer.setSize(window.innerWidth, window.innerHeight); + groundReflector.getRenderTarget().setSize(window.innerWidth, window.innerHeight); + groundReflector.resolution.set(window.innerWidth, window.innerHeight); +} + +function animate() { + stats.begin(); + render(); + stats.end(); +} + +function render() { + if (params.autoRotate) { + const timer = Date.now() * 0.0003; + + camera.position.x = Math.sin(timer) * 0.5; + camera.position.y = 0.2135; + camera.position.z = Math.cos(timer) * 0.5; + camera.lookAt(0, 0.0635, 0); + } else { + controls.update(); + } + + if (params.enableSSR) { + // TODO: groundReflector has full ground info, need use it to solve reflection gaps problem on objects when camera near ground. + // TODO: the normal and depth info where groundReflector reflected need to be changed. + composer.render(); + } else { + renderer.render(scene, camera); + } +} diff --git a/examples-testing/examples/webgl_postprocessing_taa.ts b/examples-testing/examples/webgl_postprocessing_taa.ts new file mode 100644 index 000000000..11a986741 --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_taa.ts @@ -0,0 +1,139 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { TAARenderPass } from 'three/addons/postprocessing/TAARenderPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +let camera, scene, renderer, composer, taaRenderPass, renderPass; +let gui, stats; +let index = 0; + +const param = { TAAEnabled: '1', TAASampleLevel: 0 }; + +init(); + +clearGui(); + +function clearGui() { + if (gui) gui.destroy(); + + gui = new GUI(); + + gui.add(param, 'TAAEnabled', { + Disabled: '0', + Enabled: '1', + }).onFinishChange(function () { + if (taaRenderPass) { + taaRenderPass.enabled = param.TAAEnabled === '1'; + renderPass.enabled = param.TAAEnabled !== '1'; + } + }); + + gui.add(param, 'TAASampleLevel', { + 'Level 0: 1 Sample': 0, + 'Level 1: 2 Samples': 1, + 'Level 2: 4 Samples': 2, + 'Level 3: 8 Samples': 3, + 'Level 4: 16 Samples': 4, + 'Level 5: 32 Samples': 5, + }).onFinishChange(function () { + if (taaRenderPass) { + taaRenderPass.sampleLevel = param.TAASampleLevel; + } + }); + + gui.open(); +} + +function init() { + const container = document.getElementById('container'); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 300; + + scene = new THREE.Scene(); + + const geometry = new THREE.BoxGeometry(120, 120, 120); + const material1 = new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: true }); + + const mesh1 = new THREE.Mesh(geometry, material1); + mesh1.position.x = -100; + scene.add(mesh1); + + const texture = new THREE.TextureLoader().load('textures/brick_diffuse.jpg'); + texture.minFilter = THREE.NearestFilter; + texture.magFilter = THREE.NearestFilter; + texture.anisotropy = 1; + texture.generateMipmaps = false; + texture.colorSpace = THREE.SRGBColorSpace; + + const material2 = new THREE.MeshBasicMaterial({ map: texture }); + + const mesh2 = new THREE.Mesh(geometry, material2); + mesh2.position.x = 100; + scene.add(mesh2); + + // postprocessing + + composer = new EffectComposer(renderer); + + taaRenderPass = new TAARenderPass(scene, camera); + taaRenderPass.unbiased = false; + composer.addPass(taaRenderPass); + + renderPass = new RenderPass(scene, camera); + renderPass.enabled = false; + composer.addPass(renderPass); + + const outputPass = new OutputPass(); + composer.addPass(outputPass); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); + composer.setSize(width, height); +} + +function animate() { + index++; + + if (Math.round(index / 200) % 2 === 0) { + for (let i = 0; i < scene.children.length; i++) { + const child = scene.children[i]; + + child.rotation.x += 0.005; + child.rotation.y += 0.01; + } + + if (taaRenderPass) taaRenderPass.accumulate = false; + } else { + if (taaRenderPass) taaRenderPass.accumulate = true; + } + + composer.render(); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_postprocessing_transition.ts b/examples-testing/examples/webgl_postprocessing_transition.ts new file mode 100644 index 000000000..d05466131 --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_transition.ts @@ -0,0 +1,211 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import TWEEN from 'three/addons/libs/tween.module.js'; +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderTransitionPass } from 'three/addons/postprocessing/RenderTransitionPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +let stats; +let renderer, composer, renderTransitionPass; + +const textures = []; +const clock = new THREE.Clock(); + +const params = { + sceneAnimate: true, + transitionAnimate: true, + transition: 0, + useTexture: true, + texture: 5, + cycle: true, + threshold: 0.1, +}; + +const fxSceneA = new FXScene(new THREE.BoxGeometry(2, 2, 2), new THREE.Vector3(0, -0.4, 0), 0xffffff); +const fxSceneB = new FXScene(new THREE.IcosahedronGeometry(1, 1), new THREE.Vector3(0, 0.2, 0.1), 0x000000); + +init(); + +function init() { + initGUI(); + initTextures(); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + composer = new EffectComposer(renderer); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + renderTransitionPass = new RenderTransitionPass(fxSceneA.scene, fxSceneA.camera, fxSceneB.scene, fxSceneB.camera); + renderTransitionPass.setTexture(textures[0]); + composer.addPass(renderTransitionPass); + + const outputPass = new OutputPass(); + composer.addPass(outputPass); +} + +window.addEventListener('resize', onWindowResize); + +function onWindowResize() { + fxSceneA.resize(); + fxSceneB.resize(); + renderer.setSize(window.innerWidth, window.innerHeight); + composer.setSize(window.innerWidth, window.innerHeight); +} + +new TWEEN.Tween(params) + .to({ transition: 1 }, 1500) + .onUpdate(function () { + renderTransitionPass.setTransition(params.transition); + + // Change the current alpha texture after each transition + if (params.cycle) { + if (params.transition == 0 || params.transition == 1) { + params.texture = (params.texture + 1) % textures.length; + renderTransitionPass.setTexture(textures[params.texture]); + } + } + }) + .repeat(Infinity) + .delay(2000) + .yoyo(true) + .start(); + +function animate() { + // Transition animation + if (params.transitionAnimate) TWEEN.update(); + + const delta = clock.getDelta(); + fxSceneA.update(delta); + fxSceneB.update(delta); + + render(); + stats.update(); +} + +function initTextures() { + const loader = new THREE.TextureLoader(); + + for (let i = 0; i < 6; i++) { + textures[i] = loader.load('textures/transition/transition' + (i + 1) + '.png'); + } +} + +function initGUI() { + const gui = new GUI(); + + gui.add(params, 'sceneAnimate').name('Animate scene'); + gui.add(params, 'transitionAnimate').name('Animate transition'); + gui.add(params, 'transition', 0, 1, 0.01) + .onChange(function (value) { + renderTransitionPass.setTransition(value); + }) + .listen(); + + gui.add(params, 'useTexture').onChange(function (value) { + renderTransitionPass.useTexture(value); + }); + + gui.add(params, 'texture', { Perlin: 0, Squares: 1, Cells: 2, Distort: 3, Gradient: 4, Radial: 5 }) + .onChange(function (value) { + renderTransitionPass.setTexture(textures[value]); + }) + .listen(); + + gui.add(params, 'cycle'); + + gui.add(params, 'threshold', 0, 1, 0.01).onChange(function (value) { + renderTransitionPass.setTextureThreshold(value); + }); +} + +function render() { + // Prevent render both scenes when it's not necessary + if (params.transition === 0) { + renderer.render(fxSceneB.scene, fxSceneB.camera); + } else if (params.transition === 1) { + renderer.render(fxSceneA.scene, fxSceneA.camera); + } else { + // When 0 < transition < 1 render transition between two scenes + composer.render(); + } +} + +function FXScene(geometry, rotationSpeed, backgroundColor) { + const camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.z = 20; + + // Setup scene + const scene = new THREE.Scene(); + scene.background = new THREE.Color(backgroundColor); + scene.add(new THREE.AmbientLight(0xaaaaaa, 3)); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0, 1, 4); + scene.add(light); + + this.rotationSpeed = rotationSpeed; + + const color = geometry.type === 'BoxGeometry' ? 0x0000ff : 0xff0000; + const material = new THREE.MeshPhongMaterial({ color: color, flatShading: true }); + const mesh = generateInstancedMesh(geometry, material, 500); + scene.add(mesh); + + this.scene = scene; + this.camera = camera; + this.mesh = mesh; + + this.update = function (delta) { + if (params.sceneAnimate) { + mesh.rotation.x += this.rotationSpeed.x * delta; + mesh.rotation.y += this.rotationSpeed.y * delta; + mesh.rotation.z += this.rotationSpeed.z * delta; + } + }; + + this.resize = function () { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + }; +} + +function generateInstancedMesh(geometry, material, count) { + const mesh = new THREE.InstancedMesh(geometry, material, count); + + const dummy = new THREE.Object3D(); + const color = new THREE.Color(); + + for (let i = 0; i < count; i++) { + dummy.position.x = Math.random() * 100 - 50; + dummy.position.y = Math.random() * 60 - 30; + dummy.position.z = Math.random() * 80 - 40; + + dummy.rotation.x = Math.random() * 2 * Math.PI; + dummy.rotation.y = Math.random() * 2 * Math.PI; + dummy.rotation.z = Math.random() * 2 * Math.PI; + + dummy.scale.x = Math.random() * 2 + 1; + + if (geometry.type === 'BoxGeometry') { + dummy.scale.y = Math.random() * 2 + 1; + dummy.scale.z = Math.random() * 2 + 1; + } else { + dummy.scale.y = dummy.scale.x; + dummy.scale.z = dummy.scale.x; + } + + dummy.updateMatrix(); + + mesh.setMatrixAt(i, dummy.matrix); + mesh.setColorAt(i, color.setScalar(0.1 + 0.9 * Math.random())); + } + + return mesh; +} diff --git a/examples-testing/examples/webgl_postprocessing_unreal_bloom.ts b/examples-testing/examples/webgl_postprocessing_unreal_bloom.ts new file mode 100644 index 000000000..53ec2fe2f --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_unreal_bloom.ts @@ -0,0 +1,136 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +let camera, stats; +let composer, renderer, mixer, clock; + +const params = { + threshold: 0, + strength: 1, + radius: 0, + exposure: 1, +}; + +init(); + +async function init() { + const container = document.getElementById('container'); + + clock = new THREE.Clock(); + + const scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 100); + camera.position.set(-5, 2.5, -3.5); + scene.add(camera); + + scene.add(new THREE.AmbientLight(0xcccccc)); + + const pointLight = new THREE.PointLight(0xffffff, 100); + camera.add(pointLight); + + const loader = new GLTFLoader(); + const gltf = await loader.loadAsync('models/gltf/PrimaryIonDrive.glb'); + + const model = gltf.scene; + scene.add(model); + + mixer = new THREE.AnimationMixer(model); + const clip = gltf.animations[0]; + mixer.clipAction(clip.optimize()).play(); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ReinhardToneMapping; + container.appendChild(renderer.domElement); + + // + + const renderScene = new RenderPass(scene, camera); + + const bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight), 1.5, 0.4, 0.85); + bloomPass.threshold = params.threshold; + bloomPass.strength = params.strength; + bloomPass.radius = params.radius; + + const outputPass = new OutputPass(); + + composer = new EffectComposer(renderer); + composer.addPass(renderScene); + composer.addPass(bloomPass); + composer.addPass(outputPass); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.maxPolarAngle = Math.PI * 0.5; + controls.minDistance = 3; + controls.maxDistance = 8; + + // + + const gui = new GUI(); + + const bloomFolder = gui.addFolder('bloom'); + + bloomFolder.add(params, 'threshold', 0.0, 1.0).onChange(function (value) { + bloomPass.threshold = Number(value); + }); + + bloomFolder.add(params, 'strength', 0.0, 3.0).onChange(function (value) { + bloomPass.strength = Number(value); + }); + + gui.add(params, 'radius', 0.0, 1.0) + .step(0.01) + .onChange(function (value) { + bloomPass.radius = Number(value); + }); + + const toneMappingFolder = gui.addFolder('tone mapping'); + + toneMappingFolder.add(params, 'exposure', 0.1, 2).onChange(function (value) { + renderer.toneMappingExposure = Math.pow(value, 4.0); + }); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); + composer.setSize(width, height); +} + +function animate() { + const delta = clock.getDelta(); + + mixer.update(delta); + + stats.update(); + + composer.render(); +} diff --git a/examples-testing/examples/webgl_postprocessing_unreal_bloom_selective.ts b/examples-testing/examples/webgl_postprocessing_unreal_bloom_selective.ts new file mode 100644 index 000000000..d633806ee --- /dev/null +++ b/examples-testing/examples/webgl_postprocessing_unreal_bloom_selective.ts @@ -0,0 +1,195 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js'; +import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +const BLOOM_SCENE = 1; + +const bloomLayer = new THREE.Layers(); +bloomLayer.set(BLOOM_SCENE); + +const params = { + threshold: 0, + strength: 1, + radius: 0.5, + exposure: 1, +}; + +const darkMaterial = new THREE.MeshBasicMaterial({ color: 'black' }); +const materials = {}; + +const renderer = new THREE.WebGLRenderer({ antialias: true }); +renderer.setPixelRatio(window.devicePixelRatio); +renderer.setSize(window.innerWidth, window.innerHeight); +renderer.toneMapping = THREE.ReinhardToneMapping; +document.body.appendChild(renderer.domElement); + +const scene = new THREE.Scene(); + +const camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 200); +camera.position.set(0, 0, 20); +camera.lookAt(0, 0, 0); + +const controls = new OrbitControls(camera, renderer.domElement); +controls.maxPolarAngle = Math.PI * 0.5; +controls.minDistance = 1; +controls.maxDistance = 100; +controls.addEventListener('change', render); + +const renderScene = new RenderPass(scene, camera); + +const bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight), 1.5, 0.4, 0.85); +bloomPass.threshold = params.threshold; +bloomPass.strength = params.strength; +bloomPass.radius = params.radius; + +const bloomComposer = new EffectComposer(renderer); +bloomComposer.renderToScreen = false; +bloomComposer.addPass(renderScene); +bloomComposer.addPass(bloomPass); + +const mixPass = new ShaderPass( + new THREE.ShaderMaterial({ + uniforms: { + baseTexture: { value: null }, + bloomTexture: { value: bloomComposer.renderTarget2.texture }, + }, + vertexShader: document.getElementById('vertexshader').textContent, + fragmentShader: document.getElementById('fragmentshader').textContent, + defines: {}, + }), + 'baseTexture', +); +mixPass.needsSwap = true; + +const outputPass = new OutputPass(); + +const finalComposer = new EffectComposer(renderer); +finalComposer.addPass(renderScene); +finalComposer.addPass(mixPass); +finalComposer.addPass(outputPass); + +const raycaster = new THREE.Raycaster(); + +const mouse = new THREE.Vector2(); + +window.addEventListener('pointerdown', onPointerDown); + +const gui = new GUI(); + +const bloomFolder = gui.addFolder('bloom'); + +bloomFolder.add(params, 'threshold', 0.0, 1.0).onChange(function (value) { + bloomPass.threshold = Number(value); + render(); +}); + +bloomFolder.add(params, 'strength', 0.0, 3).onChange(function (value) { + bloomPass.strength = Number(value); + render(); +}); + +bloomFolder + .add(params, 'radius', 0.0, 1.0) + .step(0.01) + .onChange(function (value) { + bloomPass.radius = Number(value); + render(); + }); + +const toneMappingFolder = gui.addFolder('tone mapping'); + +toneMappingFolder.add(params, 'exposure', 0.1, 2).onChange(function (value) { + renderer.toneMappingExposure = Math.pow(value, 4.0); + render(); +}); + +setupScene(); + +function onPointerDown(event) { + mouse.x = (event.clientX / window.innerWidth) * 2 - 1; + mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; + + raycaster.setFromCamera(mouse, camera); + const intersects = raycaster.intersectObjects(scene.children, false); + if (intersects.length > 0) { + const object = intersects[0].object; + object.layers.toggle(BLOOM_SCENE); + render(); + } +} + +window.onresize = function () { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); + + bloomComposer.setSize(width, height); + finalComposer.setSize(width, height); + + render(); +}; + +function setupScene() { + scene.traverse(disposeMaterial); + scene.children.length = 0; + + const geometry = new THREE.IcosahedronGeometry(1, 15); + + for (let i = 0; i < 50; i++) { + const color = new THREE.Color(); + color.setHSL(Math.random(), 0.7, Math.random() * 0.2 + 0.05); + + const material = new THREE.MeshBasicMaterial({ color: color }); + const sphere = new THREE.Mesh(geometry, material); + sphere.position.x = Math.random() * 10 - 5; + sphere.position.y = Math.random() * 10 - 5; + sphere.position.z = Math.random() * 10 - 5; + sphere.position.normalize().multiplyScalar(Math.random() * 4.0 + 2.0); + sphere.scale.setScalar(Math.random() * Math.random() + 0.5); + scene.add(sphere); + + if (Math.random() < 0.25) sphere.layers.enable(BLOOM_SCENE); + } + + render(); +} + +function disposeMaterial(obj) { + if (obj.material) { + obj.material.dispose(); + } +} + +function render() { + scene.traverse(darkenNonBloomed); + bloomComposer.render(); + scene.traverse(restoreMaterial); + + // render the entire scene, then render bloom scene on top + finalComposer.render(); +} + +function darkenNonBloomed(obj) { + if (obj.isMesh && bloomLayer.test(obj.layers) === false) { + materials[obj.uuid] = obj.material; + obj.material = darkMaterial; + } +} + +function restoreMaterial(obj) { + if (materials[obj.uuid]) { + obj.material = materials[obj.uuid]; + delete materials[obj.uuid]; + } +} diff --git a/examples-testing/examples/webgl_raycaster_sprite.ts b/examples-testing/examples/webgl_raycaster_sprite.ts new file mode 100644 index 000000000..f35d5de17 --- /dev/null +++ b/examples-testing/examples/webgl_raycaster_sprite.ts @@ -0,0 +1,103 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let renderer, scene, camera; +let group; + +let selectedObject = null; +const raycaster = new THREE.Raycaster(); +const pointer = new THREE.Vector2(); + +init(); + +function init() { + // init renderer + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // init scene + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + + group = new THREE.Group(); + scene.add(group); + + // init camera + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(15, 15, 15); + camera.lookAt(scene.position); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 15; + controls.maxDistance = 250; + + // add sprites + + const sprite1 = new THREE.Sprite(new THREE.SpriteMaterial({ color: '#69f' })); + sprite1.position.set(6, 5, 5); + sprite1.scale.set(2, 5, 1); + group.add(sprite1); + + const sprite2 = new THREE.Sprite(new THREE.SpriteMaterial({ color: '#69f', sizeAttenuation: false })); + sprite2.material.rotation = (Math.PI / 3) * 4; + sprite2.position.set(8, -2, 2); + sprite2.center.set(0.5, 0); + sprite2.scale.set(0.1, 0.5, 0.1); + group.add(sprite2); + + const group2 = new THREE.Object3D(); + group2.scale.set(1, 2, 1); + group2.position.set(-5, 0, 0); + group2.rotation.set(Math.PI / 2, 0, 0); + group.add(group2); + + const sprite3 = new THREE.Sprite(new THREE.SpriteMaterial({ color: '#69f' })); + sprite3.position.set(0, 2, 5); + sprite3.scale.set(10, 2, 3); + sprite3.center.set(-0.1, 0); + sprite3.material.rotation = Math.PI / 3; + group2.add(sprite3); + + window.addEventListener('resize', onWindowResize); + document.addEventListener('pointermove', onPointerMove); +} + +function animate() { + renderer.render(scene, camera); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onPointerMove(event) { + if (selectedObject) { + selectedObject.material.color.set('#69f'); + selectedObject = null; + } + + pointer.x = (event.clientX / window.innerWidth) * 2 - 1; + pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; + + raycaster.setFromCamera(pointer, camera); + + const intersects = raycaster.intersectObject(group, true); + + if (intersects.length > 0) { + const res = intersects.filter(function (res) { + return res && res.object; + })[0]; + + if (res && res.object) { + selectedObject = res.object; + selectedObject.material.color.set('#f00'); + } + } +} diff --git a/examples-testing/examples/webgl_raycaster_texture.ts b/examples-testing/examples/webgl_raycaster_texture.ts new file mode 100644 index 000000000..72c7054dc --- /dev/null +++ b/examples-testing/examples/webgl_raycaster_texture.ts @@ -0,0 +1,286 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +const WRAPPING = { + RepeatWrapping: THREE.RepeatWrapping, + ClampToEdgeWrapping: THREE.ClampToEdgeWrapping, + MirroredRepeatWrapping: THREE.MirroredRepeatWrapping, +}; + +const params = { + wrapS: THREE.RepeatWrapping, + wrapT: THREE.RepeatWrapping, + offsetX: 0, + offsetY: 0, + repeatX: 1, + repeatY: 1, + rotation: 0, +}; + +function CanvasTexture(parentTexture) { + this._canvas = document.createElement('canvas'); + this._canvas.width = this._canvas.height = 1024; + this._context2D = this._canvas.getContext('2d'); + + if (parentTexture) { + this._parentTexture.push(parentTexture); + parentTexture.image = this._canvas; + } + + const that = this; + this._background = document.createElement('img'); + this._background.addEventListener('load', function () { + that._canvas.width = that._background.naturalWidth; + that._canvas.height = that._background.naturalHeight; + + that._crossRadius = Math.ceil(Math.min(that._canvas.width, that._canvas.height / 30)); + that._crossMax = Math.ceil(0.70710678 * that._crossRadius); + that._crossMin = Math.ceil(that._crossMax / 10); + that._crossThickness = Math.ceil(that._crossMax / 10); + + that._draw(); + }); + this._background.crossOrigin = ''; + this._background.src = 'textures/uv_grid_opengl.jpg'; + + this._draw(); +} + +CanvasTexture.prototype = { + constructor: CanvasTexture, + + _canvas: null, + _context2D: null, + _xCross: 0, + _yCross: 0, + + _crossRadius: 57, + _crossMax: 40, + _crossMin: 4, + _crossThickness: 4, + + _parentTexture: [], + + addParent: function (parentTexture) { + if (this._parentTexture.indexOf(parentTexture) === -1) { + this._parentTexture.push(parentTexture); + parentTexture.image = this._canvas; + } + }, + + setCrossPosition: function (x, y) { + this._xCross = x * this._canvas.width; + this._yCross = y * this._canvas.height; + + this._draw(); + }, + + _draw: function () { + if (!this._context2D) return; + + this._context2D.clearRect(0, 0, this._canvas.width, this._canvas.height); + + // Background. + this._context2D.drawImage(this._background, 0, 0); + + // Yellow cross. + this._context2D.lineWidth = this._crossThickness * 3; + this._context2D.strokeStyle = '#FFFF00'; + + this._context2D.beginPath(); + this._context2D.moveTo(this._xCross - this._crossMax - 2, this._yCross - this._crossMax - 2); + this._context2D.lineTo(this._xCross - this._crossMin, this._yCross - this._crossMin); + + this._context2D.moveTo(this._xCross + this._crossMin, this._yCross + this._crossMin); + this._context2D.lineTo(this._xCross + this._crossMax + 2, this._yCross + this._crossMax + 2); + + this._context2D.moveTo(this._xCross - this._crossMax - 2, this._yCross + this._crossMax + 2); + this._context2D.lineTo(this._xCross - this._crossMin, this._yCross + this._crossMin); + + this._context2D.moveTo(this._xCross + this._crossMin, this._yCross - this._crossMin); + this._context2D.lineTo(this._xCross + this._crossMax + 2, this._yCross - this._crossMax - 2); + + this._context2D.stroke(); + + for (let i = 0; i < this._parentTexture.length; i++) { + this._parentTexture[i].needsUpdate = true; + } + }, +}; + +const width = window.innerWidth; +const height = window.innerHeight; + +let canvas; +let planeTexture, cubeTexture, circleTexture; + +let container; + +let camera, scene, renderer; + +const raycaster = new THREE.Raycaster(); +const mouse = new THREE.Vector2(); +const onClickPosition = new THREE.Vector2(); + +init(); + +function init() { + container = document.getElementById('container'); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xeeeeee); + + camera = new THREE.PerspectiveCamera(45, width / height, 1, 1000); + camera.position.x = -30; + camera.position.y = 40; + camera.position.z = 50; + camera.lookAt(scene.position); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(width, height); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // A cube, in the middle. + cubeTexture = new THREE.Texture(undefined, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping); + cubeTexture.colorSpace = THREE.SRGBColorSpace; + canvas = new CanvasTexture(cubeTexture); + const cubeMaterial = new THREE.MeshBasicMaterial({ map: cubeTexture }); + const cubeGeometry = new THREE.BoxGeometry(20, 20, 20); + let uvs = cubeGeometry.attributes.uv.array; + // Set a specific texture mapping. + for (let i = 0; i < uvs.length; i++) { + uvs[i] *= 2; + } + + const cube = new THREE.Mesh(cubeGeometry, cubeMaterial); + cube.position.x = 4; + cube.position.y = -5; + cube.position.z = 0; + scene.add(cube); + + // A plane on the left + + planeTexture = new THREE.Texture( + undefined, + THREE.UVMapping, + THREE.MirroredRepeatWrapping, + THREE.MirroredRepeatWrapping, + ); + planeTexture.colorSpace = THREE.SRGBColorSpace; + canvas.addParent(planeTexture); + const planeMaterial = new THREE.MeshBasicMaterial({ map: planeTexture }); + const planeGeometry = new THREE.PlaneGeometry(25, 25, 1, 1); + uvs = planeGeometry.attributes.uv.array; + + // Set a specific texture mapping. + + for (let i = 0; i < uvs.length; i++) { + uvs[i] *= 2; + } + + const plane = new THREE.Mesh(planeGeometry, planeMaterial); + plane.position.x = -16; + plane.position.y = -5; + plane.position.z = 0; + scene.add(plane); + + // A circle on the right. + + circleTexture = new THREE.Texture(undefined, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping); + circleTexture.colorSpace = THREE.SRGBColorSpace; + canvas.addParent(circleTexture); + const circleMaterial = new THREE.MeshBasicMaterial({ map: circleTexture }); + const circleGeometry = new THREE.CircleGeometry(25, 40, 0, Math.PI * 2); + uvs = circleGeometry.attributes.uv.array; + + // Set a specific texture mapping. + + for (let i = 0; i < uvs.length; i++) { + uvs[i] = (uvs[i] - 0.25) * 2; + } + + const circle = new THREE.Mesh(circleGeometry, circleMaterial); + circle.position.x = 24; + circle.position.y = -5; + circle.position.z = 0; + scene.add(circle); + + window.addEventListener('resize', onWindowResize); + container.addEventListener('mousemove', onMouseMove); + + // + + const gui = new GUI(); + gui.title('Circle Texture Settings'); + + gui.add(params, 'wrapS', WRAPPING).onChange(setwrapS); + gui.add(params, 'wrapT', WRAPPING).onChange(setwrapT); + gui.add(params, 'offsetX', 0, 5); + gui.add(params, 'offsetY', 0, 5); + gui.add(params, 'repeatX', 0, 5); + gui.add(params, 'repeatY', 0, 5); + gui.add(params, 'rotation', 0, 2 * Math.PI); + gui.open(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onMouseMove(evt) { + evt.preventDefault(); + + const array = getMousePosition(container, evt.clientX, evt.clientY); + onClickPosition.fromArray(array); + + const intersects = getIntersects(onClickPosition, scene.children); + + if (intersects.length > 0 && intersects[0].uv) { + const uv = intersects[0].uv; + intersects[0].object.material.map.transformUv(uv); + canvas.setCrossPosition(uv.x, uv.y); + } +} + +function getMousePosition(dom, x, y) { + const rect = dom.getBoundingClientRect(); + return [(x - rect.left) / rect.width, (y - rect.top) / rect.height]; +} + +function getIntersects(point, objects) { + mouse.set(point.x * 2 - 1, -(point.y * 2) + 1); + + raycaster.setFromCamera(mouse, camera); + + return raycaster.intersectObjects(objects, false); +} + +function animate() { + // update texture parameters + + circleTexture.offset.x = params.offsetX; + circleTexture.offset.y = params.offsetY; + circleTexture.repeat.x = params.repeatX; + circleTexture.repeat.y = params.repeatY; + circleTexture.rotation = params.rotation; + + // + + renderer.render(scene, camera); +} + +function setwrapS(value) { + circleTexture.wrapS = value; + circleTexture.needsUpdate = true; +} + +function setwrapT(value) { + circleTexture.wrapT = value; + circleTexture.needsUpdate = true; +} diff --git a/examples-testing/examples/webgl_raymarching_reflect.ts b/examples-testing/examples/webgl_raymarching_reflect.ts new file mode 100644 index 000000000..e5448ebbd --- /dev/null +++ b/examples-testing/examples/webgl_raymarching_reflect.ts @@ -0,0 +1,95 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let dolly, camera, scene, renderer; +let geometry, material, mesh; +let stats, clock; + +const canvas = document.querySelector('#canvas'); + +const config = { + saveImage: function () { + renderer.render(scene, camera); + window.open(canvas.toDataURL()); + }, + resolution: '512', +}; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer({ canvas: canvas }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(parseInt(config.resolution), parseInt(config.resolution)); + renderer.setAnimationLoop(animate); + + window.addEventListener('resize', onWindowResize); + + // THREE.Scene + scene = new THREE.Scene(); + + dolly = new THREE.Group(); + scene.add(dolly); + + clock = new THREE.Clock(); + + camera = new THREE.PerspectiveCamera(60, canvas.width / canvas.height, 1, 2000); + camera.position.z = 4; + dolly.add(camera); + + geometry = new THREE.PlaneGeometry(2.0, 2.0); + material = new THREE.RawShaderMaterial({ + uniforms: { + resolution: { value: new THREE.Vector2(canvas.width, canvas.height) }, + cameraWorldMatrix: { value: camera.matrixWorld }, + cameraProjectionMatrixInverse: { value: camera.projectionMatrixInverse.clone() }, + }, + vertexShader: document.getElementById('vertex_shader').textContent, + fragmentShader: document.getElementById('fragment_shader').textContent, + }); + mesh = new THREE.Mesh(geometry, material); + mesh.frustumCulled = false; + scene.add(mesh); + + // Controls + const controls = new OrbitControls(camera, canvas); + controls.enableZoom = false; + + // GUI + const gui = new GUI(); + gui.add(config, 'saveImage').name('Save Image'); + gui.add(config, 'resolution', ['256', '512', '800', 'full']).name('Resolution').onChange(onWindowResize); + + stats = new Stats(); + document.body.appendChild(stats.dom); +} + +function onWindowResize() { + if (config.resolution === 'full') { + renderer.setSize(window.innerWidth, window.innerHeight); + } else { + renderer.setSize(parseInt(config.resolution), parseInt(config.resolution)); + } + + camera.aspect = canvas.width / canvas.height; + camera.updateProjectionMatrix(); + + material.uniforms.resolution.value.set(canvas.width, canvas.height); + material.uniforms.cameraProjectionMatrixInverse.value.copy(camera.projectionMatrixInverse); +} + +function animate() { + stats.begin(); + + const elapsedTime = clock.getElapsedTime(); + + dolly.position.z = -elapsedTime; + + renderer.render(scene, camera); + + stats.end(); +} diff --git a/examples-testing/examples/webgl_read_float_buffer.ts b/examples-testing/examples/webgl_read_float_buffer.ts new file mode 100644 index 000000000..68452a12a --- /dev/null +++ b/examples-testing/examples/webgl_read_float_buffer.ts @@ -0,0 +1,153 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; + +let cameraRTT, sceneRTT, sceneScreen, renderer, zmesh1, zmesh2; + +let mouseX = 0, + mouseY = 0; + +const windowHalfX = window.innerWidth / 2; +const windowHalfY = window.innerHeight / 2; + +let rtTexture, material, quad; + +let delta = 0.01; +let valueNode; + +init(); + +function init() { + container = document.getElementById('container'); + + cameraRTT = new THREE.OrthographicCamera( + window.innerWidth / -2, + window.innerWidth / 2, + window.innerHeight / 2, + window.innerHeight / -2, + -10000, + 10000, + ); + cameraRTT.position.z = 100; + + // + + sceneRTT = new THREE.Scene(); + sceneScreen = new THREE.Scene(); + + let light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0, 0, 1).normalize(); + sceneRTT.add(light); + + light = new THREE.DirectionalLight(0xffd5d5, 4.5); + light.position.set(0, 0, -1).normalize(); + sceneRTT.add(light); + + rtTexture = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight, { + minFilter: THREE.LinearFilter, + magFilter: THREE.NearestFilter, + format: THREE.RGBAFormat, + type: THREE.FloatType, + }); + + material = new THREE.ShaderMaterial({ + uniforms: { time: { value: 0.0 } }, + vertexShader: document.getElementById('vertexShader').textContent, + fragmentShader: document.getElementById('fragment_shader_pass_1').textContent, + }); + + const materialScreen = new THREE.ShaderMaterial({ + uniforms: { tDiffuse: { value: rtTexture.texture } }, + vertexShader: document.getElementById('vertexShader').textContent, + fragmentShader: document.getElementById('fragment_shader_screen').textContent, + + depthWrite: false, + }); + + const plane = new THREE.PlaneGeometry(window.innerWidth, window.innerHeight); + + quad = new THREE.Mesh(plane, material); + quad.position.z = -100; + sceneRTT.add(quad); + + const geometry = new THREE.TorusGeometry(100, 25, 15, 30); + + const mat1 = new THREE.MeshPhongMaterial({ color: 0x9c9c9c, specular: 0xffaa00, shininess: 5 }); + const mat2 = new THREE.MeshPhongMaterial({ color: 0x9c0000, specular: 0xff2200, shininess: 5 }); + + zmesh1 = new THREE.Mesh(geometry, mat1); + zmesh1.position.set(0, 0, 100); + zmesh1.scale.set(1.5, 1.5, 1.5); + sceneRTT.add(zmesh1); + + zmesh2 = new THREE.Mesh(geometry, mat2); + zmesh2.position.set(0, 150, 100); + zmesh2.scale.set(0.75, 0.75, 0.75); + sceneRTT.add(zmesh2); + + quad = new THREE.Mesh(plane, materialScreen); + quad.position.z = -100; + sceneScreen.add(quad); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.autoClear = false; + + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + valueNode = document.getElementById('values'); + + document.addEventListener('mousemove', onDocumentMouseMove); +} + +function onDocumentMouseMove(event) { + mouseX = event.clientX - windowHalfX; + mouseY = event.clientY - windowHalfY; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + const time = Date.now() * 0.0015; + + if (zmesh1 && zmesh2) { + zmesh1.rotation.y = -time; + zmesh2.rotation.y = -time + Math.PI / 2; + } + + if (material.uniforms['time'].value > 1 || material.uniforms['time'].value < 0) { + delta *= -1; + } + + material.uniforms['time'].value += delta; + + renderer.clear(); + + // Render first scene into texture + + renderer.setRenderTarget(rtTexture); + renderer.clear(); + renderer.render(sceneRTT, cameraRTT); + + // Render full screen quad with generated texture + + renderer.setRenderTarget(null); + renderer.render(sceneScreen, cameraRTT); + + const read = new Float32Array(4); + renderer.readRenderTargetPixels(rtTexture, windowHalfX + mouseX, windowHalfY - mouseY, 1, 1, read); + + valueNode.innerHTML = 'r:' + read[0] + '
g:' + read[1] + '
b:' + read[2]; +} diff --git a/examples-testing/examples/webgl_refraction.ts b/examples-testing/examples/webgl_refraction.ts new file mode 100644 index 000000000..572575afa --- /dev/null +++ b/examples-testing/examples/webgl_refraction.ts @@ -0,0 +1,135 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { Refractor } from 'three/addons/objects/Refractor.js'; +import { WaterRefractionShader } from 'three/addons/shaders/WaterRefractionShader.js'; + +let camera, scene, renderer, clock; + +let refractor, smallSphere; + +init(); + +async function init() { + const container = document.getElementById('container'); + + clock = new THREE.Clock(); + + // scene + scene = new THREE.Scene(); + + // camera + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 500); + camera.position.set(0, 75, 160); + + // refractor + + const refractorGeometry = new THREE.PlaneGeometry(90, 90); + + refractor = new Refractor(refractorGeometry, { + color: 0xcbcbcb, + textureWidth: 1024, + textureHeight: 1024, + shader: WaterRefractionShader, + }); + + refractor.position.set(0, 50, 0); + + scene.add(refractor); + + // load dudv map for distortion effect + + const loader = new THREE.TextureLoader(); + const dudvMap = await loader.loadAsync('textures/waterdudv.jpg'); + + dudvMap.wrapS = dudvMap.wrapT = THREE.RepeatWrapping; + refractor.material.uniforms.tDudv.value = dudvMap; + + // + + const geometry = new THREE.IcosahedronGeometry(5, 0); + const material = new THREE.MeshPhongMaterial({ color: 0xffffff, emissive: 0x333333, flatShading: true }); + smallSphere = new THREE.Mesh(geometry, material); + scene.add(smallSphere); + + // walls + const planeGeo = new THREE.PlaneGeometry(100.1, 100.1); + + const planeTop = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); + planeTop.position.y = 100; + planeTop.rotateX(Math.PI / 2); + scene.add(planeTop); + + const planeBottom = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); + planeBottom.rotateX(-Math.PI / 2); + scene.add(planeBottom); + + const planeBack = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x7f7fff })); + planeBack.position.z = -50; + planeBack.position.y = 50; + scene.add(planeBack); + + const planeRight = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x00ff00 })); + planeRight.position.x = 50; + planeRight.position.y = 50; + planeRight.rotateY(-Math.PI / 2); + scene.add(planeRight); + + const planeLeft = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xff0000 })); + planeLeft.position.x = -50; + planeLeft.position.y = 50; + planeLeft.rotateY(Math.PI / 2); + scene.add(planeLeft); + + // lights + const mainLight = new THREE.PointLight(0xe7e7e7, 2.5, 250, 0); + mainLight.position.y = 60; + scene.add(mainLight); + + const greenLight = new THREE.PointLight(0x00ff00, 0.5, 1000, 0); + greenLight.position.set(550, 50, 0); + scene.add(greenLight); + + const redLight = new THREE.PointLight(0xff0000, 0.5, 1000, 0); + redLight.position.set(-550, 50, 0); + scene.add(redLight); + + const blueLight = new THREE.PointLight(0xbbbbfe, 0.5, 1000, 0); + blueLight.position.set(0, 50, 550); + scene.add(blueLight); + + // renderer + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // controls + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 40, 0); + controls.maxDistance = 400; + controls.minDistance = 10; + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const time = clock.getElapsedTime(); + + refractor.material.uniforms.time.value = time; + + smallSphere.position.set(Math.cos(time) * 30, Math.abs(Math.cos(time * 2)) * 20 + 5, Math.sin(time) * 30); + smallSphere.rotation.y = Math.PI / 2 - time; + smallSphere.rotation.z = time * 8; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_rtt.ts b/examples-testing/examples/webgl_rtt.ts new file mode 100644 index 000000000..9f16fdab8 --- /dev/null +++ b/examples-testing/examples/webgl_rtt.ts @@ -0,0 +1,171 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; + +let cameraRTT, camera, sceneRTT, sceneScreen, scene, renderer, zmesh1, zmesh2; + +let mouseX = 0, + mouseY = 0; + +const windowHalfX = window.innerWidth / 2; +const windowHalfY = window.innerHeight / 2; + +let rtTexture, material, quad; + +let delta = 0.01; + +init(); + +function init() { + container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 100; + + cameraRTT = new THREE.OrthographicCamera( + window.innerWidth / -2, + window.innerWidth / 2, + window.innerHeight / 2, + window.innerHeight / -2, + -10000, + 10000, + ); + cameraRTT.position.z = 100; + + // + + scene = new THREE.Scene(); + sceneRTT = new THREE.Scene(); + sceneScreen = new THREE.Scene(); + + let light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0, 0, 1).normalize(); + sceneRTT.add(light); + + light = new THREE.DirectionalLight(0xffd5d5, 4.5); + light.position.set(0, 0, -1).normalize(); + sceneRTT.add(light); + + rtTexture = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight); + + material = new THREE.ShaderMaterial({ + uniforms: { time: { value: 0.0 } }, + vertexShader: document.getElementById('vertexShader').textContent, + fragmentShader: document.getElementById('fragment_shader_pass_1').textContent, + }); + + const materialScreen = new THREE.ShaderMaterial({ + uniforms: { tDiffuse: { value: rtTexture.texture } }, + vertexShader: document.getElementById('vertexShader').textContent, + fragmentShader: document.getElementById('fragment_shader_screen').textContent, + + depthWrite: false, + }); + + const plane = new THREE.PlaneGeometry(window.innerWidth, window.innerHeight); + + quad = new THREE.Mesh(plane, material); + quad.position.z = -100; + sceneRTT.add(quad); + + const torusGeometry = new THREE.TorusGeometry(100, 25, 15, 30); + + const mat1 = new THREE.MeshPhongMaterial({ color: 0x9c9c9c, specular: 0xffaa00, shininess: 5 }); + const mat2 = new THREE.MeshPhongMaterial({ color: 0x9c0000, specular: 0xff2200, shininess: 5 }); + + zmesh1 = new THREE.Mesh(torusGeometry, mat1); + zmesh1.position.set(0, 0, 100); + zmesh1.scale.set(1.5, 1.5, 1.5); + sceneRTT.add(zmesh1); + + zmesh2 = new THREE.Mesh(torusGeometry, mat2); + zmesh2.position.set(0, 150, 100); + zmesh2.scale.set(0.75, 0.75, 0.75); + sceneRTT.add(zmesh2); + + quad = new THREE.Mesh(plane, materialScreen); + quad.position.z = -100; + sceneScreen.add(quad); + + const n = 5, + geometry = new THREE.SphereGeometry(10, 64, 32), + material2 = new THREE.MeshBasicMaterial({ color: 0xffffff, map: rtTexture.texture }); + + for (let j = 0; j < n; j++) { + for (let i = 0; i < n; i++) { + const mesh = new THREE.Mesh(geometry, material2); + + mesh.position.x = (i - (n - 1) / 2) * 20; + mesh.position.y = (j - (n - 1) / 2) * 20; + mesh.position.z = 0; + + mesh.rotation.y = -Math.PI / 2; + + scene.add(mesh); + } + } + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.autoClear = false; + + container.appendChild(renderer.domElement); + + stats = new Stats(); + container.appendChild(stats.dom); + + document.addEventListener('mousemove', onDocumentMouseMove); +} + +function onDocumentMouseMove(event) { + mouseX = event.clientX - windowHalfX; + mouseY = event.clientY - windowHalfY; +} + +// + +function animate() { + render(); + stats.update(); +} + +function render() { + const time = Date.now() * 0.0015; + + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-mouseY - camera.position.y) * 0.05; + + camera.lookAt(scene.position); + + if (zmesh1 && zmesh2) { + zmesh1.rotation.y = -time; + zmesh2.rotation.y = -time + Math.PI / 2; + } + + if (material.uniforms['time'].value > 1 || material.uniforms['time'].value < 0) { + delta *= -1; + } + + material.uniforms['time'].value += delta; + + // Render first scene into texture + + renderer.setRenderTarget(rtTexture); + renderer.clear(); + renderer.render(sceneRTT, cameraRTT); + + // Render full screen quad with generated texture + + renderer.setRenderTarget(null); + renderer.clear(); + renderer.render(sceneScreen, cameraRTT); + + // Render second scene to screen + // (using first scene as regular texture) + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_shader.ts b/examples-testing/examples/webgl_shader.ts new file mode 100644 index 000000000..47a6c7ece --- /dev/null +++ b/examples-testing/examples/webgl_shader.ts @@ -0,0 +1,50 @@ +import * as THREE from 'three'; + +let camera, scene, renderer; + +let uniforms; + +init(); + +function init() { + const container = document.getElementById('container'); + + camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1); + + scene = new THREE.Scene(); + + const geometry = new THREE.PlaneGeometry(2, 2); + + uniforms = { + time: { value: 1.0 }, + }; + + const material = new THREE.ShaderMaterial({ + uniforms: uniforms, + vertexShader: document.getElementById('vertexShader').textContent, + fragmentShader: document.getElementById('fragmentShader').textContent, + }); + + const mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + uniforms['time'].value = performance.now() / 1000; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_shader_lava.ts b/examples-testing/examples/webgl_shader_lava.ts new file mode 100644 index 000000000..973a580eb --- /dev/null +++ b/examples-testing/examples/webgl_shader_lava.ts @@ -0,0 +1,101 @@ +import * as THREE from 'three'; + +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; +import { BloomPass } from 'three/addons/postprocessing/BloomPass.js'; +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; + +let camera, renderer, composer, clock; + +let uniforms, mesh; + +init(); + +function init() { + const container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 3000); + camera.position.z = 4; + + const scene = new THREE.Scene(); + + clock = new THREE.Clock(); + + const textureLoader = new THREE.TextureLoader(); + + const cloudTexture = textureLoader.load('textures/lava/cloud.png'); + const lavaTexture = textureLoader.load('textures/lava/lavatile.jpg'); + + lavaTexture.colorSpace = THREE.SRGBColorSpace; + + cloudTexture.wrapS = cloudTexture.wrapT = THREE.RepeatWrapping; + lavaTexture.wrapS = lavaTexture.wrapT = THREE.RepeatWrapping; + + uniforms = { + fogDensity: { value: 0.45 }, + fogColor: { value: new THREE.Vector3(0, 0, 0) }, + time: { value: 1.0 }, + uvScale: { value: new THREE.Vector2(3.0, 1.0) }, + texture1: { value: cloudTexture }, + texture2: { value: lavaTexture }, + }; + + const size = 0.65; + + const material = new THREE.ShaderMaterial({ + uniforms: uniforms, + vertexShader: document.getElementById('vertexShader').textContent, + fragmentShader: document.getElementById('fragmentShader').textContent, + }); + + mesh = new THREE.Mesh(new THREE.TorusGeometry(size, 0.3, 30, 30), material); + mesh.rotation.x = 0.3; + scene.add(mesh); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.autoClear = false; + container.appendChild(renderer.domElement); + + // + + const renderModel = new RenderPass(scene, camera); + const effectBloom = new BloomPass(1.25); + const outputPass = new OutputPass(); + + composer = new EffectComposer(renderer); + + composer.addPass(renderModel); + composer.addPass(effectBloom); + composer.addPass(outputPass); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + composer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const delta = 5 * clock.getDelta(); + + uniforms['time'].value += 0.2 * delta; + + mesh.rotation.y += 0.0125 * delta; + mesh.rotation.x += 0.05 * delta; + + renderer.clear(); + composer.render(0.01); +} diff --git a/examples-testing/examples/webgl_shaders_ocean.ts b/examples-testing/examples/webgl_shaders_ocean.ts new file mode 100644 index 000000000..8b0f9a738 --- /dev/null +++ b/examples-testing/examples/webgl_shaders_ocean.ts @@ -0,0 +1,169 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { Water } from 'three/addons/objects/Water.js'; +import { Sky } from 'three/addons/objects/Sky.js'; + +let container, stats; +let camera, scene, renderer; +let controls, water, sun, mesh; + +init(); + +function init() { + container = document.getElementById('container'); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 0.5; + container.appendChild(renderer.domElement); + + // + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(55, window.innerWidth / window.innerHeight, 1, 20000); + camera.position.set(30, 30, 100); + + // + + sun = new THREE.Vector3(); + + // Water + + const waterGeometry = new THREE.PlaneGeometry(10000, 10000); + + water = new Water(waterGeometry, { + textureWidth: 512, + textureHeight: 512, + waterNormals: new THREE.TextureLoader().load('textures/waternormals.jpg', function (texture) { + texture.wrapS = texture.wrapT = THREE.RepeatWrapping; + }), + sunDirection: new THREE.Vector3(), + sunColor: 0xffffff, + waterColor: 0x001e0f, + distortionScale: 3.7, + fog: scene.fog !== undefined, + }); + + water.rotation.x = -Math.PI / 2; + + scene.add(water); + + // Skybox + + const sky = new Sky(); + sky.scale.setScalar(10000); + scene.add(sky); + + const skyUniforms = sky.material.uniforms; + + skyUniforms['turbidity'].value = 10; + skyUniforms['rayleigh'].value = 2; + skyUniforms['mieCoefficient'].value = 0.005; + skyUniforms['mieDirectionalG'].value = 0.8; + + const parameters = { + elevation: 2, + azimuth: 180, + }; + + const pmremGenerator = new THREE.PMREMGenerator(renderer); + const sceneEnv = new THREE.Scene(); + + let renderTarget; + + function updateSun() { + const phi = THREE.MathUtils.degToRad(90 - parameters.elevation); + const theta = THREE.MathUtils.degToRad(parameters.azimuth); + + sun.setFromSphericalCoords(1, phi, theta); + + sky.material.uniforms['sunPosition'].value.copy(sun); + water.material.uniforms['sunDirection'].value.copy(sun).normalize(); + + if (renderTarget !== undefined) renderTarget.dispose(); + + sceneEnv.add(sky); + renderTarget = pmremGenerator.fromScene(sceneEnv); + scene.add(sky); + + scene.environment = renderTarget.texture; + } + + updateSun(); + + // + + const geometry = new THREE.BoxGeometry(30, 30, 30); + const material = new THREE.MeshStandardMaterial({ roughness: 0 }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + controls = new OrbitControls(camera, renderer.domElement); + controls.maxPolarAngle = Math.PI * 0.495; + controls.target.set(0, 10, 0); + controls.minDistance = 40.0; + controls.maxDistance = 200.0; + controls.update(); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // GUI + + const gui = new GUI(); + + const folderSky = gui.addFolder('Sky'); + folderSky.add(parameters, 'elevation', 0, 90, 0.1).onChange(updateSun); + folderSky.add(parameters, 'azimuth', -180, 180, 0.1).onChange(updateSun); + folderSky.open(); + + const waterUniforms = water.material.uniforms; + + const folderWater = gui.addFolder('Water'); + folderWater.add(waterUniforms.distortionScale, 'value', 0, 8, 0.1).name('distortionScale'); + folderWater.add(waterUniforms.size, 'value', 0.1, 10, 0.1).name('size'); + folderWater.open(); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + const time = performance.now() * 0.001; + + mesh.position.y = Math.sin(time) * 20 + 5; + mesh.rotation.x = time * 0.5; + mesh.rotation.z = time * 0.51; + + water.material.uniforms['time'].value += 1.0 / 60.0; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_shaders_sky.ts b/examples-testing/examples/webgl_shaders_sky.ts new file mode 100644 index 000000000..18020f78f --- /dev/null +++ b/examples-testing/examples/webgl_shaders_sky.ts @@ -0,0 +1,103 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { Sky } from 'three/addons/objects/Sky.js'; + +let camera, scene, renderer; + +let sky, sun; + +init(); +render(); + +function initSky() { + // Add Sky + sky = new Sky(); + sky.scale.setScalar(450000); + scene.add(sky); + + sun = new THREE.Vector3(); + + /// GUI + + const effectController = { + turbidity: 10, + rayleigh: 3, + mieCoefficient: 0.005, + mieDirectionalG: 0.7, + elevation: 2, + azimuth: 180, + exposure: renderer.toneMappingExposure, + }; + + function guiChanged() { + const uniforms = sky.material.uniforms; + uniforms['turbidity'].value = effectController.turbidity; + uniforms['rayleigh'].value = effectController.rayleigh; + uniforms['mieCoefficient'].value = effectController.mieCoefficient; + uniforms['mieDirectionalG'].value = effectController.mieDirectionalG; + + const phi = THREE.MathUtils.degToRad(90 - effectController.elevation); + const theta = THREE.MathUtils.degToRad(effectController.azimuth); + + sun.setFromSphericalCoords(1, phi, theta); + + uniforms['sunPosition'].value.copy(sun); + + renderer.toneMappingExposure = effectController.exposure; + renderer.render(scene, camera); + } + + const gui = new GUI(); + + gui.add(effectController, 'turbidity', 0.0, 20.0, 0.1).onChange(guiChanged); + gui.add(effectController, 'rayleigh', 0.0, 4, 0.001).onChange(guiChanged); + gui.add(effectController, 'mieCoefficient', 0.0, 0.1, 0.001).onChange(guiChanged); + gui.add(effectController, 'mieDirectionalG', 0.0, 1, 0.001).onChange(guiChanged); + gui.add(effectController, 'elevation', 0, 90, 0.1).onChange(guiChanged); + gui.add(effectController, 'azimuth', -180, 180, 0.1).onChange(guiChanged); + gui.add(effectController, 'exposure', 0, 1, 0.0001).onChange(guiChanged); + + guiChanged(); +} + +function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 100, 2000000); + camera.position.set(0, 100, 2000); + + scene = new THREE.Scene(); + + const helper = new THREE.GridHelper(10000, 2, 0xffffff, 0xffffff); + scene.add(helper); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 0.5; + document.body.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); + //controls.maxPolarAngle = Math.PI / 2; + controls.enableZoom = false; + controls.enablePan = false; + + initSky(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_shadow_contact.ts b/examples-testing/examples/webgl_shadow_contact.ts new file mode 100644 index 000000000..9eda35b83 --- /dev/null +++ b/examples-testing/examples/webgl_shadow_contact.ts @@ -0,0 +1,272 @@ +import * as THREE from 'three'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { HorizontalBlurShader } from 'three/addons/shaders/HorizontalBlurShader.js'; +import { VerticalBlurShader } from 'three/addons/shaders/VerticalBlurShader.js'; + +let camera, scene, renderer, stats, gui; + +const meshes = []; + +const PLANE_WIDTH = 2.5; +const PLANE_HEIGHT = 2.5; +const CAMERA_HEIGHT = 0.3; + +const state = { + shadow: { + blur: 3.5, + darkness: 1, + opacity: 1, + }, + plane: { + color: '#ffffff', + opacity: 1, + }, + showWireframe: false, +}; + +let shadowGroup, + renderTarget, + renderTargetBlur, + shadowCamera, + cameraHelper, + depthMaterial, + horizontalBlurMaterial, + verticalBlurMaterial; + +let plane, blurPlane, fillPlane; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(0.5, 1, 2); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); + + // add the example meshes + + const geometries = [ + new THREE.BoxGeometry(0.4, 0.4, 0.4), + new THREE.IcosahedronGeometry(0.3), + new THREE.TorusKnotGeometry(0.4, 0.05, 256, 24, 1, 3), + ]; + + const material = new THREE.MeshNormalMaterial(); + + for (let i = 0, l = geometries.length; i < l; i++) { + const angle = (i / l) * Math.PI * 2; + + const geometry = geometries[i]; + const mesh = new THREE.Mesh(geometry, material); + mesh.position.y = 0.1; + mesh.position.x = Math.cos(angle) / 2.0; + mesh.position.z = Math.sin(angle) / 2.0; + scene.add(mesh); + meshes.push(mesh); + } + + // the container, if you need to move the plane just move this + shadowGroup = new THREE.Group(); + shadowGroup.position.y = -0.3; + scene.add(shadowGroup); + + // the render target that will show the shadows in the plane texture + renderTarget = new THREE.WebGLRenderTarget(512, 512); + renderTarget.texture.generateMipmaps = false; + + // the render target that we will use to blur the first render target + renderTargetBlur = new THREE.WebGLRenderTarget(512, 512); + renderTargetBlur.texture.generateMipmaps = false; + + // make a plane and make it face up + const planeGeometry = new THREE.PlaneGeometry(PLANE_WIDTH, PLANE_HEIGHT).rotateX(Math.PI / 2); + const planeMaterial = new THREE.MeshBasicMaterial({ + map: renderTarget.texture, + opacity: state.shadow.opacity, + transparent: true, + depthWrite: false, + }); + plane = new THREE.Mesh(planeGeometry, planeMaterial); + // make sure it's rendered after the fillPlane + plane.renderOrder = 1; + shadowGroup.add(plane); + + // the y from the texture is flipped! + plane.scale.y = -1; + + // the plane onto which to blur the texture + blurPlane = new THREE.Mesh(planeGeometry); + blurPlane.visible = false; + shadowGroup.add(blurPlane); + + // the plane with the color of the ground + const fillPlaneMaterial = new THREE.MeshBasicMaterial({ + color: state.plane.color, + opacity: state.plane.opacity, + transparent: true, + depthWrite: false, + }); + fillPlane = new THREE.Mesh(planeGeometry, fillPlaneMaterial); + fillPlane.rotateX(Math.PI); + shadowGroup.add(fillPlane); + + // the camera to render the depth material from + shadowCamera = new THREE.OrthographicCamera( + -PLANE_WIDTH / 2, + PLANE_WIDTH / 2, + PLANE_HEIGHT / 2, + -PLANE_HEIGHT / 2, + 0, + CAMERA_HEIGHT, + ); + shadowCamera.rotation.x = Math.PI / 2; // get the camera to look up + shadowGroup.add(shadowCamera); + + cameraHelper = new THREE.CameraHelper(shadowCamera); + + // like MeshDepthMaterial, but goes from black to transparent + depthMaterial = new THREE.MeshDepthMaterial(); + depthMaterial.userData.darkness = { value: state.shadow.darkness }; + depthMaterial.onBeforeCompile = function (shader) { + shader.uniforms.darkness = depthMaterial.userData.darkness; + shader.fragmentShader = /* glsl */ ` + uniform float darkness; + ${shader.fragmentShader.replace( + 'gl_FragColor = vec4( vec3( 1.0 - fragCoordZ ), opacity );', + 'gl_FragColor = vec4( vec3( 0.0 ), ( 1.0 - fragCoordZ ) * darkness );', + )} + `; + }; + + depthMaterial.depthTest = false; + depthMaterial.depthWrite = false; + + horizontalBlurMaterial = new THREE.ShaderMaterial(HorizontalBlurShader); + horizontalBlurMaterial.depthTest = false; + + verticalBlurMaterial = new THREE.ShaderMaterial(VerticalBlurShader); + verticalBlurMaterial.depthTest = false; + + // + + gui = new GUI(); + const shadowFolder = gui.addFolder('shadow'); + shadowFolder.open(); + const planeFolder = gui.addFolder('plane'); + planeFolder.open(); + + shadowFolder.add(state.shadow, 'blur', 0, 15, 0.1); + shadowFolder.add(state.shadow, 'darkness', 1, 5, 0.1).onChange(function () { + depthMaterial.userData.darkness.value = state.shadow.darkness; + }); + shadowFolder.add(state.shadow, 'opacity', 0, 1, 0.01).onChange(function () { + plane.material.opacity = state.shadow.opacity; + }); + planeFolder.addColor(state.plane, 'color').onChange(function () { + fillPlane.material.color = new THREE.Color(state.plane.color); + }); + planeFolder.add(state.plane, 'opacity', 0, 1, 0.01).onChange(function () { + fillPlane.material.opacity = state.plane.opacity; + }); + + gui.add(state, 'showWireframe').onChange(function () { + if (state.showWireframe) { + scene.add(cameraHelper); + } else { + scene.remove(cameraHelper); + } + }); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + new OrbitControls(camera, renderer.domElement); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// renderTarget --> blurPlane (horizontalBlur) --> renderTargetBlur --> blurPlane (verticalBlur) --> renderTarget +function blurShadow(amount) { + blurPlane.visible = true; + + // blur horizontally and draw in the renderTargetBlur + blurPlane.material = horizontalBlurMaterial; + blurPlane.material.uniforms.tDiffuse.value = renderTarget.texture; + horizontalBlurMaterial.uniforms.h.value = (amount * 1) / 256; + + renderer.setRenderTarget(renderTargetBlur); + renderer.render(blurPlane, shadowCamera); + + // blur vertically and draw in the main renderTarget + blurPlane.material = verticalBlurMaterial; + blurPlane.material.uniforms.tDiffuse.value = renderTargetBlur.texture; + verticalBlurMaterial.uniforms.v.value = (amount * 1) / 256; + + renderer.setRenderTarget(renderTarget); + renderer.render(blurPlane, shadowCamera); + + blurPlane.visible = false; +} + +function animate() { + meshes.forEach(mesh => { + mesh.rotation.x += 0.01; + mesh.rotation.y += 0.02; + }); + + // + + // remove the background + const initialBackground = scene.background; + scene.background = null; + + // force the depthMaterial to everything + cameraHelper.visible = false; + scene.overrideMaterial = depthMaterial; + + // set renderer clear alpha + const initialClearAlpha = renderer.getClearAlpha(); + renderer.setClearAlpha(0); + + // render to the render target to get the depths + renderer.setRenderTarget(renderTarget); + renderer.render(scene, shadowCamera); + + // and reset the override material + scene.overrideMaterial = null; + cameraHelper.visible = true; + + blurShadow(state.shadow.blur); + + // a second pass to reduce the artifacts + // (0.4 is the minimum blur amout so that the artifacts are gone) + blurShadow(state.shadow.blur * 0.4); + + // reset and render the normal scene + renderer.setRenderTarget(null); + renderer.setClearAlpha(initialClearAlpha); + scene.background = initialBackground; + + renderer.render(scene, camera); + stats.update(); +} diff --git a/examples-testing/examples/webgl_shadowmap.ts b/examples-testing/examples/webgl_shadowmap.ts new file mode 100644 index 000000000..6d0ac3adb --- /dev/null +++ b/examples-testing/examples/webgl_shadowmap.ts @@ -0,0 +1,311 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { FontLoader } from 'three/addons/loaders/FontLoader.js'; +import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; +import { ShadowMapViewer } from 'three/addons/utils/ShadowMapViewer.js'; + +const SHADOW_MAP_WIDTH = 2048, + SHADOW_MAP_HEIGHT = 1024; + +let SCREEN_WIDTH = window.innerWidth; +let SCREEN_HEIGHT = window.innerHeight; +const FLOOR = -250; + +let camera, controls, scene, renderer; +let container, stats; + +const NEAR = 10, + FAR = 3000; + +let mixer; + +const morphs = []; + +let light; +let lightShadowMapViewer; + +const clock = new THREE.Clock(); + +let showHUD = false; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + // CAMERA + + camera = new THREE.PerspectiveCamera(23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR); + camera.position.set(700, 50, 1900); + + // SCENE + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x59472b); + scene.fog = new THREE.Fog(0x59472b, 1000, FAR); + + // LIGHTS + + const ambient = new THREE.AmbientLight(0xffffff); + scene.add(ambient); + + light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0, 1500, 1000); + light.castShadow = true; + light.shadow.camera.top = 2000; + light.shadow.camera.bottom = -2000; + light.shadow.camera.left = -2000; + light.shadow.camera.right = 2000; + light.shadow.camera.near = 1200; + light.shadow.camera.far = 2500; + light.shadow.bias = 0.0001; + + light.shadow.mapSize.width = SHADOW_MAP_WIDTH; + light.shadow.mapSize.height = SHADOW_MAP_HEIGHT; + + scene.add(light); + + createHUD(); + createScene(); + + // RENDERER + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + renderer.autoClear = false; + + // + + renderer.shadowMap.enabled = true; + renderer.shadowMap.type = THREE.PCFShadowMap; + + // CONTROLS + + controls = new FirstPersonControls(camera, renderer.domElement); + + controls.lookSpeed = 0.0125; + controls.movementSpeed = 500; + controls.lookVertical = true; + + controls.lookAt(scene.position); + + // STATS + + stats = new Stats(); + //container.appendChild( stats.dom ); + + // + + window.addEventListener('resize', onWindowResize); + window.addEventListener('keydown', onKeyDown); +} + +function onWindowResize() { + SCREEN_WIDTH = window.innerWidth; + SCREEN_HEIGHT = window.innerHeight; + + camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT; + camera.updateProjectionMatrix(); + + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + + controls.handleResize(); +} + +function onKeyDown(event) { + switch (event.keyCode) { + case 84 /*t*/: + showHUD = !showHUD; + break; + } +} + +function createHUD() { + lightShadowMapViewer = new ShadowMapViewer(light); + lightShadowMapViewer.position.x = 10; + lightShadowMapViewer.position.y = SCREEN_HEIGHT - SHADOW_MAP_HEIGHT / 4 - 10; + lightShadowMapViewer.size.width = SHADOW_MAP_WIDTH / 4; + lightShadowMapViewer.size.height = SHADOW_MAP_HEIGHT / 4; + lightShadowMapViewer.update(); +} + +function createScene() { + // GROUND + + const geometry = new THREE.PlaneGeometry(100, 100); + const planeMaterial = new THREE.MeshPhongMaterial({ color: 0xffdd99 }); + + const ground = new THREE.Mesh(geometry, planeMaterial); + + ground.position.set(0, FLOOR, 0); + ground.rotation.x = -Math.PI / 2; + ground.scale.set(100, 100, 100); + + ground.castShadow = false; + ground.receiveShadow = true; + + scene.add(ground); + + // TEXT + + const loader = new FontLoader(); + loader.load('fonts/helvetiker_bold.typeface.json', function (font) { + const textGeo = new TextGeometry('THREE.JS', { + font: font, + + size: 200, + depth: 50, + curveSegments: 12, + + bevelThickness: 2, + bevelSize: 5, + bevelEnabled: true, + }); + + textGeo.computeBoundingBox(); + const centerOffset = -0.5 * (textGeo.boundingBox.max.x - textGeo.boundingBox.min.x); + + const textMaterial = new THREE.MeshPhongMaterial({ color: 0xff0000, specular: 0xffffff }); + + const mesh = new THREE.Mesh(textGeo, textMaterial); + mesh.position.x = centerOffset; + mesh.position.y = FLOOR + 67; + + mesh.castShadow = true; + mesh.receiveShadow = true; + + scene.add(mesh); + }); + + // CUBES + + const cubes1 = new THREE.Mesh(new THREE.BoxGeometry(1500, 220, 150), planeMaterial); + + cubes1.position.y = FLOOR - 50; + cubes1.position.z = 20; + + cubes1.castShadow = true; + cubes1.receiveShadow = true; + + scene.add(cubes1); + + const cubes2 = new THREE.Mesh(new THREE.BoxGeometry(1600, 170, 250), planeMaterial); + + cubes2.position.y = FLOOR - 50; + cubes2.position.z = 20; + + cubes2.castShadow = true; + cubes2.receiveShadow = true; + + scene.add(cubes2); + + // MORPHS + + mixer = new THREE.AnimationMixer(scene); + + function addMorph(mesh, clip, speed, duration, x, y, z, fudgeColor) { + mesh = mesh.clone(); + mesh.material = mesh.material.clone(); + + if (fudgeColor) { + mesh.material.color.offsetHSL(0, Math.random() * 0.5 - 0.25, Math.random() * 0.5 - 0.25); + } + + mesh.speed = speed; + + mixer + .clipAction(clip, mesh) + .setDuration(duration) + // to shift the playback out of phase: + .startAt(-duration * Math.random()) + .play(); + + mesh.position.set(x, y, z); + mesh.rotation.y = Math.PI / 2; + + mesh.castShadow = true; + mesh.receiveShadow = true; + + scene.add(mesh); + + morphs.push(mesh); + } + + const gltfloader = new GLTFLoader(); + + gltfloader.load('models/gltf/Horse.glb', function (gltf) { + const mesh = gltf.scene.children[0]; + + const clip = gltf.animations[0]; + + addMorph(mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, 300, true); + addMorph(mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, 450, true); + addMorph(mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, 600, true); + + addMorph(mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, -300, true); + addMorph(mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, -450, true); + addMorph(mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, -600, true); + }); + + gltfloader.load('models/gltf/Flamingo.glb', function (gltf) { + const mesh = gltf.scene.children[0]; + const clip = gltf.animations[0]; + + addMorph(mesh, clip, 500, 1, 500 - Math.random() * 500, FLOOR + 350, 40); + }); + + gltfloader.load('models/gltf/Stork.glb', function (gltf) { + const mesh = gltf.scene.children[0]; + const clip = gltf.animations[0]; + + addMorph(mesh, clip, 350, 1, 500 - Math.random() * 500, FLOOR + 350, 340); + }); + + gltfloader.load('models/gltf/Parrot.glb', function (gltf) { + const mesh = gltf.scene.children[0]; + const clip = gltf.animations[0]; + + addMorph(mesh, clip, 450, 0.5, 500 - Math.random() * 500, FLOOR + 300, 700); + }); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + const delta = clock.getDelta(); + + mixer.update(delta); + + for (let i = 0; i < morphs.length; i++) { + const morph = morphs[i]; + + morph.position.x += morph.speed * delta; + + if (morph.position.x > 2000) { + morph.position.x = -1000 - Math.random() * 500; + } + } + + controls.update(delta); + + renderer.clear(); + renderer.render(scene, camera); + + // Render debug HUD with shadow map + + if (showHUD) { + lightShadowMapViewer.render(renderer); + } +} diff --git a/examples-testing/examples/webgl_shadowmap_csm.ts b/examples-testing/examples/webgl_shadowmap_csm.ts new file mode 100644 index 000000000..c89bc02df --- /dev/null +++ b/examples-testing/examples/webgl_shadowmap_csm.ts @@ -0,0 +1,253 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { CSM } from 'three/addons/csm/CSM.js'; +import { CSMHelper } from 'three/addons/csm/CSMHelper.js'; + +let renderer, scene, camera, orthoCamera, controls, csm, csmHelper; + +const params = { + orthographic: false, + fade: false, + shadows: true, + far: 1000, + mode: 'practical', + lightX: -1, + lightY: -1, + lightZ: -1, + margin: 100, + lightFar: 5000, + lightNear: 1, + autoUpdateHelper: true, + updateHelper: function () { + csmHelper.update(); + }, +}; + +init(); + +function updateOrthoCamera() { + const size = controls.target.distanceTo(camera.position); + const aspect = camera.aspect; + + orthoCamera.left = (size * aspect) / -2; + orthoCamera.right = (size * aspect) / 2; + + orthoCamera.top = size / 2; + orthoCamera.bottom = size / -2; + orthoCamera.position.copy(camera.position); + orthoCamera.rotation.copy(camera.rotation); + orthoCamera.updateProjectionMatrix(); +} + +function init() { + scene = new THREE.Scene(); + scene.background = new THREE.Color('#454e61'); + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 5000); + orthoCamera = new THREE.OrthographicCamera(); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + renderer.shadowMap.enabled = params.shadows; + renderer.shadowMap.type = THREE.PCFSoftShadowMap; + + controls = new OrbitControls(camera, renderer.domElement); + controls.maxPolarAngle = Math.PI / 2; + camera.position.set(60, 60, 0); + controls.target = new THREE.Vector3(-100, 10, 0); + controls.update(); + + const ambientLight = new THREE.AmbientLight(0xffffff, 1.5); + scene.add(ambientLight); + + const additionalDirectionalLight = new THREE.DirectionalLight(0x000020, 1.5); + additionalDirectionalLight.position + .set(params.lightX, params.lightY, params.lightZ) + .normalize() + .multiplyScalar(-200); + scene.add(additionalDirectionalLight); + + csm = new CSM({ + maxFar: params.far, + cascades: 4, + mode: params.mode, + parent: scene, + shadowMapSize: 1024, + lightDirection: new THREE.Vector3(params.lightX, params.lightY, params.lightZ).normalize(), + camera: camera, + }); + + csmHelper = new CSMHelper(csm); + csmHelper.visible = false; + scene.add(csmHelper); + + const floorMaterial = new THREE.MeshPhongMaterial({ color: '#252a34' }); + csm.setupMaterial(floorMaterial); + + const floor = new THREE.Mesh(new THREE.PlaneGeometry(10000, 10000, 8, 8), floorMaterial); + floor.rotation.x = -Math.PI / 2; + floor.castShadow = true; + floor.receiveShadow = true; + scene.add(floor); + + const material1 = new THREE.MeshPhongMaterial({ color: '#08d9d6' }); + csm.setupMaterial(material1); + + const material2 = new THREE.MeshPhongMaterial({ color: '#ff2e63' }); + csm.setupMaterial(material2); + + const geometry = new THREE.BoxGeometry(10, 10, 10); + + for (let i = 0; i < 40; i++) { + const cube1 = new THREE.Mesh(geometry, i % 2 === 0 ? material1 : material2); + cube1.castShadow = true; + cube1.receiveShadow = true; + scene.add(cube1); + cube1.position.set(-i * 25, 20, 30); + cube1.scale.y = Math.random() * 2 + 6; + + const cube2 = new THREE.Mesh(geometry, i % 2 === 0 ? material2 : material1); + cube2.castShadow = true; + cube2.receiveShadow = true; + scene.add(cube2); + cube2.position.set(-i * 25, 20, -30); + cube2.scale.y = Math.random() * 2 + 6; + } + + const gui = new GUI(); + + gui.add(params, 'orthographic').onChange(function (value) { + csm.camera = value ? orthoCamera : camera; + csm.updateFrustums(); + }); + + gui.add(params, 'fade').onChange(function (value) { + csm.fade = value; + csm.updateFrustums(); + }); + + gui.add(params, 'shadows').onChange(function (value) { + renderer.shadowMap.enabled = value; + + scene.traverse(function (child) { + if (child.material) { + child.material.needsUpdate = true; + } + }); + }); + + gui.add(params, 'far', 1, 5000) + .step(1) + .name('shadow far') + .onChange(function (value) { + csm.maxFar = value; + csm.updateFrustums(); + }); + + gui.add(params, 'mode', ['uniform', 'logarithmic', 'practical']) + .name('frustum split mode') + .onChange(function (value) { + csm.mode = value; + csm.updateFrustums(); + }); + + gui.add(params, 'lightX', -1, 1) + .name('light direction x') + .onChange(function (value) { + csm.lightDirection.x = value; + }); + + gui.add(params, 'lightY', -1, 1) + .name('light direction y') + .onChange(function (value) { + csm.lightDirection.y = value; + }); + + gui.add(params, 'lightZ', -1, 1) + .name('light direction z') + .onChange(function (value) { + csm.lightDirection.z = value; + }); + + gui.add(params, 'margin', 0, 200) + .name('light margin') + .onChange(function (value) { + csm.lightMargin = value; + }); + + gui.add(params, 'lightNear', 1, 10000) + .name('light near') + .onChange(function (value) { + for (let i = 0; i < csm.lights.length; i++) { + csm.lights[i].shadow.camera.near = value; + csm.lights[i].shadow.camera.updateProjectionMatrix(); + } + }); + + gui.add(params, 'lightFar', 1, 10000) + .name('light far') + .onChange(function (value) { + for (let i = 0; i < csm.lights.length; i++) { + csm.lights[i].shadow.camera.far = value; + csm.lights[i].shadow.camera.updateProjectionMatrix(); + } + }); + + const helperFolder = gui.addFolder('helper'); + + helperFolder.add(csmHelper, 'visible'); + + helperFolder.add(csmHelper, 'displayFrustum').onChange(function () { + csmHelper.updateVisibility(); + }); + + helperFolder.add(csmHelper, 'displayPlanes').onChange(function () { + csmHelper.updateVisibility(); + }); + + helperFolder.add(csmHelper, 'displayShadowBounds').onChange(function () { + csmHelper.updateVisibility(); + }); + + helperFolder.add(params, 'autoUpdateHelper').name('auto update'); + + helperFolder.add(params, 'updateHelper').name('update'); + + helperFolder.open(); + + window.addEventListener('resize', function () { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + updateOrthoCamera(); + csm.updateFrustums(); + + renderer.setSize(window.innerWidth, window.innerHeight); + }); +} + +function animate() { + camera.updateMatrixWorld(); + csm.update(); + controls.update(); + + if (params.orthographic) { + updateOrthoCamera(); + csm.updateFrustums(); + + if (params.autoUpdateHelper) { + csmHelper.update(); + } + + renderer.render(scene, orthoCamera); + } else { + if (params.autoUpdateHelper) { + csmHelper.update(); + } + + renderer.render(scene, camera); + } +} diff --git a/examples-testing/examples/webgl_shadowmap_pcss.ts b/examples-testing/examples/webgl_shadowmap_pcss.ts new file mode 100644 index 000000000..a47a011ff --- /dev/null +++ b/examples-testing/examples/webgl_shadowmap_pcss.ts @@ -0,0 +1,161 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let stats; +let camera, scene, renderer; + +let group; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + // scene + + scene = new THREE.Scene(); + scene.fog = new THREE.Fog(0xcce0ff, 5, 100); + + // camera + + camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 10000); + + // We use this particular camera position in order to expose a bug that can sometimes happen presumably + // due to lack of precision when interpolating values over really large triangles. + // It reproduced on at least NVIDIA GTX 1080 and GTX 1050 Ti GPUs when the ground plane was not + // subdivided into segments. + camera.position.x = 7; + camera.position.y = 13; + camera.position.z = 7; + + scene.add(camera); + + // lights + + scene.add(new THREE.AmbientLight(0xaaaaaa, 3)); + + const light = new THREE.DirectionalLight(0xf0f6ff, 4.5); + light.position.set(2, 8, 4); + + light.castShadow = true; + light.shadow.mapSize.width = 1024; + light.shadow.mapSize.height = 1024; + light.shadow.camera.far = 20; + + scene.add(light); + + // scene.add( new DirectionalLightHelper( light ) ); + scene.add(new THREE.CameraHelper(light.shadow.camera)); + + // group + + group = new THREE.Group(); + scene.add(group); + + const geometry = new THREE.SphereGeometry(0.3, 20, 20); + + for (let i = 0; i < 20; i++) { + const material = new THREE.MeshPhongMaterial({ color: Math.random() * 0xffffff }); + + const sphere = new THREE.Mesh(geometry, material); + sphere.position.x = Math.random() - 0.5; + sphere.position.z = Math.random() - 0.5; + sphere.position.normalize(); + sphere.position.multiplyScalar(Math.random() * 2 + 1); + sphere.castShadow = true; + sphere.receiveShadow = true; + sphere.userData.phase = Math.random() * Math.PI; + group.add(sphere); + } + + // ground + + const groundMaterial = new THREE.MeshPhongMaterial({ color: 0x898989 }); + + const ground = new THREE.Mesh(new THREE.PlaneGeometry(20000, 20000, 8, 8), groundMaterial); + ground.rotation.x = -Math.PI / 2; + ground.receiveShadow = true; + scene.add(ground); + + // column + + const column = new THREE.Mesh(new THREE.BoxGeometry(1, 4, 1), groundMaterial); + column.position.y = 2; + column.castShadow = true; + column.receiveShadow = true; + scene.add(column); + + // overwrite shadowmap code + + let shader = THREE.ShaderChunk.shadowmap_pars_fragment; + + shader = shader.replace( + '#ifdef USE_SHADOWMAP', + '#ifdef USE_SHADOWMAP' + document.getElementById('PCSS').textContent, + ); + + shader = shader.replace( + '#if defined( SHADOWMAP_TYPE_PCF )', + document.getElementById('PCSSGetShadow').textContent + '#if defined( SHADOWMAP_TYPE_PCF )', + ); + + THREE.ShaderChunk.shadowmap_pars_fragment = shader; + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.setClearColor(scene.fog.color); + + container.appendChild(renderer.domElement); + + renderer.shadowMap.enabled = true; + + // controls + const controls = new OrbitControls(camera, renderer.domElement); + controls.maxPolarAngle = Math.PI * 0.5; + controls.minDistance = 10; + controls.maxDistance = 75; + controls.target.set(0, 2.5, 0); + controls.update(); + + // performance monitor + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +// + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const time = performance.now() / 1000; + + group.traverse(function (child) { + if ('phase' in child.userData) { + child.position.y = Math.abs(Math.sin(time + child.userData.phase)) * 4 + 0.3; + } + }); + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_shadowmap_performance.ts b/examples-testing/examples/webgl_shadowmap_performance.ts new file mode 100644 index 000000000..0e45b63f9 --- /dev/null +++ b/examples-testing/examples/webgl_shadowmap_performance.ts @@ -0,0 +1,281 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { FontLoader } from 'three/addons/loaders/FontLoader.js'; +import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; + +const SHADOW_MAP_WIDTH = 2048, + SHADOW_MAP_HEIGHT = 1024; + +let SCREEN_WIDTH = window.innerWidth; +let SCREEN_HEIGHT = window.innerHeight; +const FLOOR = -250; + +const ANIMATION_GROUPS = 25; + +let camera, controls, scene, renderer; +let stats; + +const NEAR = 5, + FAR = 3000; + +let morph, mixer; + +const morphs = [], + animGroups = []; + +const clock = new THREE.Clock(); + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + // CAMERA + + camera = new THREE.PerspectiveCamera(23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR); + camera.position.set(700, 50, 1900); + + // SCENE + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x59472b); + scene.fog = new THREE.Fog(0x59472b, 1000, FAR); + + // LIGHTS + + const ambient = new THREE.AmbientLight(0xffffff); + scene.add(ambient); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0, 1500, 1000); + light.castShadow = true; + light.shadow.camera.top = 2000; + light.shadow.camera.bottom = -2000; + light.shadow.camera.left = -2000; + light.shadow.camera.right = 2000; + light.shadow.camera.near = 1200; + light.shadow.camera.far = 2500; + light.shadow.bias = 0.0001; + + light.shadow.mapSize.width = SHADOW_MAP_WIDTH; + light.shadow.mapSize.height = SHADOW_MAP_HEIGHT; + + scene.add(light); + + createScene(); + + // RENDERER + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + renderer.autoClear = false; + + // + + renderer.shadowMap.enabled = true; + renderer.shadowMap.type = THREE.PCFSoftShadowMap; + + // CONTROLS + + controls = new FirstPersonControls(camera, renderer.domElement); + + controls.lookSpeed = 0.0125; + controls.movementSpeed = 500; + controls.lookVertical = true; + + controls.lookAt(scene.position); + + // STATS + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + SCREEN_WIDTH = window.innerWidth; + SCREEN_HEIGHT = window.innerHeight; + + camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT; + camera.updateProjectionMatrix(); + + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + + controls.handleResize(); +} + +function createScene() { + // GROUND + + const geometry = new THREE.PlaneGeometry(100, 100); + const planeMaterial = new THREE.MeshPhongMaterial({ color: 0xffdd99 }); + + const ground = new THREE.Mesh(geometry, planeMaterial); + + ground.position.set(0, FLOOR, 0); + ground.rotation.x = -Math.PI / 2; + ground.scale.set(100, 100, 100); + + ground.castShadow = false; + ground.receiveShadow = true; + + scene.add(ground); + + // TEXT + + const loader = new FontLoader(); + loader.load('fonts/helvetiker_bold.typeface.json', function (font) { + const textGeo = new TextGeometry('THREE.JS', { + font: font, + + size: 200, + depth: 50, + curveSegments: 12, + + bevelThickness: 2, + bevelSize: 5, + bevelEnabled: true, + }); + + textGeo.computeBoundingBox(); + const centerOffset = -0.5 * (textGeo.boundingBox.max.x - textGeo.boundingBox.min.x); + + const textMaterial = new THREE.MeshPhongMaterial({ color: 0xff0000, specular: 0xffffff }); + + const mesh = new THREE.Mesh(textGeo, textMaterial); + mesh.position.x = centerOffset; + mesh.position.y = FLOOR + 67; + + mesh.castShadow = true; + mesh.receiveShadow = true; + + scene.add(mesh); + }); + + // CUBES + + const cubes1 = new THREE.Mesh(new THREE.BoxGeometry(1500, 220, 150), planeMaterial); + + cubes1.position.y = FLOOR - 50; + cubes1.position.z = 20; + + cubes1.castShadow = true; + cubes1.receiveShadow = true; + + scene.add(cubes1); + + const cubes2 = new THREE.Mesh(new THREE.BoxGeometry(1600, 170, 250), planeMaterial); + + cubes2.position.y = FLOOR - 50; + cubes2.position.z = 20; + + cubes2.castShadow = true; + cubes2.receiveShadow = true; + + scene.add(cubes2); + + mixer = new THREE.AnimationMixer(scene); + + for (let i = 0; i !== ANIMATION_GROUPS; ++i) { + const group = new THREE.AnimationObjectGroup(); + animGroups.push(group); + } + + // MORPHS + + function addMorph(mesh, clip, speed, duration, x, y, z, fudgeColor, massOptimization) { + mesh = mesh.clone(); + mesh.material = mesh.material.clone(); + + if (fudgeColor) { + mesh.material.color.offsetHSL(0, Math.random() * 0.5 - 0.25, Math.random() * 0.5 - 0.25); + } + + mesh.speed = speed; + + if (massOptimization) { + const index = Math.floor(Math.random() * ANIMATION_GROUPS), + animGroup = animGroups[index]; + + animGroup.add(mesh); + + if (!mixer.existingAction(clip, animGroup)) { + const randomness = 0.6 * Math.random() - 0.3; + const phase = (index + randomness) / ANIMATION_GROUPS; + + mixer + .clipAction(clip, animGroup) + .setDuration(duration) + .startAt(-duration * phase) + .play(); + } + } else { + mixer + .clipAction(clip, mesh) + .setDuration(duration) + .startAt(-duration * Math.random()) + .play(); + } + + mesh.position.set(x, y, z); + mesh.rotation.y = Math.PI / 2; + + mesh.castShadow = true; + mesh.receiveShadow = true; + + scene.add(mesh); + + morphs.push(mesh); + } + + const gltfLoader = new GLTFLoader(); + gltfLoader.load('models/gltf/Horse.glb', function (gltf) { + const mesh = gltf.scene.children[0]; + const clip = gltf.animations[0]; + + for (let i = -600; i < 601; i += 2) { + addMorph(mesh, clip, 550, 1, 100 - Math.random() * 3000, FLOOR, i, true, true); + } + }); +} + +// + +function animate() { + stats.begin(); + render(); + stats.end(); +} + +function render() { + const delta = clock.getDelta(); + + if (mixer) mixer.update(delta); + + for (let i = 0; i < morphs.length; i++) { + morph = morphs[i]; + + morph.position.x += morph.speed * delta; + + if (morph.position.x > 2000) { + morph.position.x = -1000 - Math.random() * 500; + } + } + + controls.update(delta); + + renderer.clear(); + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_shadowmap_pointlight.ts b/examples-testing/examples/webgl_shadowmap_pointlight.ts new file mode 100644 index 000000000..c68d69749 --- /dev/null +++ b/examples-testing/examples/webgl_shadowmap_pointlight.ts @@ -0,0 +1,139 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer, stats; +let pointLight, pointLight2; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 10, 40); + + scene = new THREE.Scene(); + scene.add(new THREE.AmbientLight(0x111122, 3)); + + // lights + + function createLight(color) { + const intensity = 200; + + const light = new THREE.PointLight(color, intensity, 20); + light.castShadow = true; + light.shadow.bias = -0.005; // reduces self-shadowing on double-sided objects + + let geometry = new THREE.SphereGeometry(0.3, 12, 6); + let material = new THREE.MeshBasicMaterial({ color: color }); + material.color.multiplyScalar(intensity); + let sphere = new THREE.Mesh(geometry, material); + light.add(sphere); + + const texture = new THREE.CanvasTexture(generateTexture()); + texture.magFilter = THREE.NearestFilter; + texture.wrapT = THREE.RepeatWrapping; + texture.wrapS = THREE.RepeatWrapping; + texture.repeat.set(1, 4.5); + + geometry = new THREE.SphereGeometry(2, 32, 8); + material = new THREE.MeshPhongMaterial({ + side: THREE.DoubleSide, + alphaMap: texture, + alphaTest: 0.5, + }); + + sphere = new THREE.Mesh(geometry, material); + sphere.castShadow = true; + sphere.receiveShadow = true; + light.add(sphere); + + return light; + } + + pointLight = createLight(0x0088ff); + scene.add(pointLight); + + pointLight2 = createLight(0xff8888); + scene.add(pointLight2); + // + + const geometry = new THREE.BoxGeometry(30, 30, 30); + + const material = new THREE.MeshPhongMaterial({ + color: 0xa0adaf, + shininess: 10, + specular: 0x111111, + side: THREE.BackSide, + }); + + const mesh = new THREE.Mesh(geometry, material); + mesh.position.y = 10; + mesh.receiveShadow = true; + scene.add(mesh); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + renderer.shadowMap.type = THREE.BasicShadowMap; + document.body.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 10, 0); + controls.update(); + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function generateTexture() { + const canvas = document.createElement('canvas'); + canvas.width = 2; + canvas.height = 2; + + const context = canvas.getContext('2d'); + context.fillStyle = 'white'; + context.fillRect(0, 1, 2, 1); + + return canvas; +} + +function animate() { + let time = performance.now() * 0.001; + + pointLight.position.x = Math.sin(time * 0.6) * 9; + pointLight.position.y = Math.sin(time * 0.7) * 9 + 6; + pointLight.position.z = Math.sin(time * 0.8) * 9; + + pointLight.rotation.x = time; + pointLight.rotation.z = time; + + time += 10000; + + pointLight2.position.x = Math.sin(time * 0.6) * 9; + pointLight2.position.y = Math.sin(time * 0.7) * 9 + 6; + pointLight2.position.z = Math.sin(time * 0.8) * 9; + + pointLight2.rotation.x = time; + pointLight2.rotation.z = time; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_shadowmap_progressive.ts b/examples-testing/examples/webgl_shadowmap_progressive.ts new file mode 100644 index 000000000..86ec68172 --- /dev/null +++ b/examples-testing/examples/webgl_shadowmap_progressive.ts @@ -0,0 +1,204 @@ +import * as THREE from 'three'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { TransformControls } from 'three/addons/controls/TransformControls.js'; +import { ProgressiveLightMap } from 'three/addons/misc/ProgressiveLightMap.js'; + +// ShadowMap + LightMap Res and Number of Directional Lights +const shadowMapRes = 512, + lightMapRes = 1024, + lightCount = 8; +let camera, + scene, + renderer, + controls, + control, + control2, + object = new THREE.Mesh(), + lightOrigin = null, + progressiveSurfacemap; +const dirLights = [], + lightmapObjects = []; +const params = { + Enable: true, + 'Blur Edges': true, + 'Blend Window': 200, + 'Light Radius': 50, + 'Ambient Weight': 0.5, + 'Debug Lightmap': false, +}; +init(); +createGUI(); + +function init() { + // renderer + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + document.body.appendChild(renderer.domElement); + + // camera + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 100, 200); + camera.name = 'Camera'; + + // scene + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x949494); + scene.fog = new THREE.Fog(0x949494, 1000, 3000); + + // progressive lightmap + progressiveSurfacemap = new ProgressiveLightMap(renderer, lightMapRes); + + // directional lighting "origin" + lightOrigin = new THREE.Group(); + lightOrigin.position.set(60, 150, 100); + scene.add(lightOrigin); + + // transform gizmo + control = new TransformControls(camera, renderer.domElement); + control.addEventListener('dragging-changed', event => { + controls.enabled = !event.value; + }); + control.attach(lightOrigin); + scene.add(control); + + // create 8 directional lights to speed up the convergence + for (let l = 0; l < lightCount; l++) { + const dirLight = new THREE.DirectionalLight(0xffffff, 1.0 / lightCount); + dirLight.name = 'Dir. Light ' + l; + dirLight.position.set(200, 200, 200); + dirLight.castShadow = true; + dirLight.shadow.camera.near = 100; + dirLight.shadow.camera.far = 5000; + dirLight.shadow.camera.right = 150; + dirLight.shadow.camera.left = -150; + dirLight.shadow.camera.top = 150; + dirLight.shadow.camera.bottom = -150; + dirLight.shadow.mapSize.width = shadowMapRes; + dirLight.shadow.mapSize.height = shadowMapRes; + lightmapObjects.push(dirLight); + dirLights.push(dirLight); + } + + // ground + const groundMesh = new THREE.Mesh( + new THREE.PlaneGeometry(600, 600), + new THREE.MeshPhongMaterial({ color: 0xffffff, depthWrite: true }), + ); + groundMesh.position.y = -0.1; + groundMesh.rotation.x = -Math.PI / 2; + groundMesh.name = 'Ground Mesh'; + lightmapObjects.push(groundMesh); + scene.add(groundMesh); + + // model + function loadModel() { + object.traverse(function (child) { + if (child.isMesh) { + child.name = 'Loaded Mesh'; + child.castShadow = true; + child.receiveShadow = true; + child.material = new THREE.MeshPhongMaterial(); + + // This adds the model to the lightmap + lightmapObjects.push(child); + progressiveSurfacemap.addObjectsToLightMap(lightmapObjects); + } else { + child.layers.disableAll(); // Disable Rendering for this + } + }); + scene.add(object); + object.scale.set(2, 2, 2); + object.position.set(0, -16, 0); + control2 = new TransformControls(camera, renderer.domElement); + control2.addEventListener('dragging-changed', event => { + controls.enabled = !event.value; + }); + control2.attach(object); + scene.add(control2); + const lightTarget = new THREE.Group(); + lightTarget.position.set(0, 20, 0); + for (let l = 0; l < dirLights.length; l++) { + dirLights[l].target = lightTarget; + } + + object.add(lightTarget); + } + + const manager = new THREE.LoadingManager(loadModel); + const loader = new GLTFLoader(manager); + loader.load('models/gltf/ShadowmappableMesh.glb', function (obj) { + object = obj.scene.children[0]; + }); + + // controls + controls = new OrbitControls(camera, renderer.domElement); + controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled + controls.dampingFactor = 0.05; + controls.screenSpacePanning = true; + controls.minDistance = 100; + controls.maxDistance = 500; + controls.maxPolarAngle = Math.PI / 1.5; + controls.target.set(0, 100, 0); + + window.addEventListener('resize', onWindowResize); +} + +function createGUI() { + const gui = new GUI({ title: 'Accumulation Settings' }); + gui.add(params, 'Enable'); + gui.add(params, 'Blur Edges'); + gui.add(params, 'Blend Window', 1, 500).step(1); + gui.add(params, 'Light Radius', 0, 200).step(10); + gui.add(params, 'Ambient Weight', 0, 1).step(0.1); + gui.add(params, 'Debug Lightmap'); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + // Update the inertia on the orbit controls + controls.update(); + + // Accumulate Surface Maps + if (params['Enable']) { + progressiveSurfacemap.update(camera, params['Blend Window'], params['Blur Edges']); + + if (!progressiveSurfacemap.firstUpdate) { + progressiveSurfacemap.showDebugLightmap(params['Debug Lightmap']); + } + } + + // Manually Update the Directional Lights + for (let l = 0; l < dirLights.length; l++) { + // Sometimes they will be sampled from the target direction + // Sometimes they will be uniformly sampled from the upper hemisphere + if (Math.random() > params['Ambient Weight']) { + dirLights[l].position.set( + lightOrigin.position.x + Math.random() * params['Light Radius'], + lightOrigin.position.y + Math.random() * params['Light Radius'], + lightOrigin.position.z + Math.random() * params['Light Radius'], + ); + } else { + // Uniform Hemispherical Surface Distribution for Ambient Occlusion + const lambda = Math.acos(2 * Math.random() - 1) - 3.14159 / 2.0; + const phi = 2 * 3.14159 * Math.random(); + dirLights[l].position.set( + Math.cos(lambda) * Math.cos(phi) * 300 + object.position.x, + Math.abs(Math.cos(lambda) * Math.sin(phi) * 300) + object.position.y + 20, + Math.sin(lambda) * 300 + object.position.z, + ); + } + } + + // Render Scene + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_shadowmap_viewer.ts b/examples-testing/examples/webgl_shadowmap_viewer.ts new file mode 100644 index 000000000..f974ef038 --- /dev/null +++ b/examples-testing/examples/webgl_shadowmap_viewer.ts @@ -0,0 +1,178 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { ShadowMapViewer } from 'three/addons/utils/ShadowMapViewer.js'; + +let camera, scene, renderer, clock, stats; +let dirLight, spotLight; +let torusKnot, cube; +let dirLightShadowMapViewer, spotLightShadowMapViewer; + +init(); + +function init() { + initScene(); + initShadowMapViewers(); + initMisc(); + + document.body.appendChild(renderer.domElement); + window.addEventListener('resize', onWindowResize); +} + +function initScene() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 15, 35); + + scene = new THREE.Scene(); + + // Lights + + scene.add(new THREE.AmbientLight(0x404040, 3)); + + spotLight = new THREE.SpotLight(0xffffff, 500); + spotLight.name = 'Spot Light'; + spotLight.angle = Math.PI / 5; + spotLight.penumbra = 0.3; + spotLight.position.set(10, 10, 5); + spotLight.castShadow = true; + spotLight.shadow.camera.near = 8; + spotLight.shadow.camera.far = 30; + spotLight.shadow.mapSize.width = 1024; + spotLight.shadow.mapSize.height = 1024; + scene.add(spotLight); + + scene.add(new THREE.CameraHelper(spotLight.shadow.camera)); + + dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.name = 'Dir. Light'; + dirLight.position.set(0, 10, 0); + dirLight.castShadow = true; + dirLight.shadow.camera.near = 1; + dirLight.shadow.camera.far = 10; + dirLight.shadow.camera.right = 15; + dirLight.shadow.camera.left = -15; + dirLight.shadow.camera.top = 15; + dirLight.shadow.camera.bottom = -15; + dirLight.shadow.mapSize.width = 1024; + dirLight.shadow.mapSize.height = 1024; + scene.add(dirLight); + + scene.add(new THREE.CameraHelper(dirLight.shadow.camera)); + + // Geometry + let geometry = new THREE.TorusKnotGeometry(25, 8, 75, 20); + let material = new THREE.MeshPhongMaterial({ + color: 0xff0000, + shininess: 150, + specular: 0x222222, + }); + + torusKnot = new THREE.Mesh(geometry, material); + torusKnot.scale.multiplyScalar(1 / 18); + torusKnot.position.y = 3; + torusKnot.castShadow = true; + torusKnot.receiveShadow = true; + scene.add(torusKnot); + + geometry = new THREE.BoxGeometry(3, 3, 3); + cube = new THREE.Mesh(geometry, material); + cube.position.set(8, 3, 8); + cube.castShadow = true; + cube.receiveShadow = true; + scene.add(cube); + + geometry = new THREE.BoxGeometry(10, 0.15, 10); + material = new THREE.MeshPhongMaterial({ + color: 0xa0adaf, + shininess: 150, + specular: 0x111111, + }); + + const ground = new THREE.Mesh(geometry, material); + ground.scale.multiplyScalar(3); + ground.castShadow = false; + ground.receiveShadow = true; + scene.add(ground); +} + +function initShadowMapViewers() { + dirLightShadowMapViewer = new ShadowMapViewer(dirLight); + spotLightShadowMapViewer = new ShadowMapViewer(spotLight); + resizeShadowMapViewers(); +} + +function initMisc() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + renderer.shadowMap.type = THREE.BasicShadowMap; + + // Mouse control + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 2, 0); + controls.update(); + + clock = new THREE.Clock(); + + stats = new Stats(); + document.body.appendChild(stats.dom); +} + +function resizeShadowMapViewers() { + const size = window.innerWidth * 0.15; + + dirLightShadowMapViewer.position.x = 10; + dirLightShadowMapViewer.position.y = 10; + dirLightShadowMapViewer.size.width = size; + dirLightShadowMapViewer.size.height = size; + dirLightShadowMapViewer.update(); //Required when setting position or size directly + + spotLightShadowMapViewer.size.set(size, size); + spotLightShadowMapViewer.position.set(size + 20, 10); + // spotLightShadowMapViewer.update(); //NOT required because .set updates automatically +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + resizeShadowMapViewers(); + dirLightShadowMapViewer.updateForWindowResize(); + spotLightShadowMapViewer.updateForWindowResize(); +} + +function animate() { + render(); + + stats.update(); +} + +function renderScene() { + renderer.render(scene, camera); +} + +function renderShadowMapViewers() { + dirLightShadowMapViewer.render(renderer); + spotLightShadowMapViewer.render(renderer); +} + +function render() { + const delta = clock.getDelta(); + + renderScene(); + renderShadowMapViewers(); + + torusKnot.rotation.x += 0.25 * delta; + torusKnot.rotation.y += 2 * delta; + torusKnot.rotation.z += 1 * delta; + + cube.rotation.x += 0.25 * delta; + cube.rotation.y += 2 * delta; + cube.rotation.z += 1 * delta; +} diff --git a/examples-testing/examples/webgl_shadowmap_vsm.ts b/examples-testing/examples/webgl_shadowmap_vsm.ts new file mode 100644 index 000000000..4867c7315 --- /dev/null +++ b/examples-testing/examples/webgl_shadowmap_vsm.ts @@ -0,0 +1,200 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer, clock, stats; +let dirLight, spotLight; +let torusKnot, dirGroup; + +init(); + +function init() { + initScene(); + initMisc(); + + // Init gui + const gui = new GUI(); + + const config = { + spotlightRadius: 4, + spotlightSamples: 8, + dirlightRadius: 4, + dirlightSamples: 8, + }; + + const spotlightFolder = gui.addFolder('Spotlight'); + spotlightFolder + .add(config, 'spotlightRadius') + .name('radius') + .min(0) + .max(25) + .onChange(function (value) { + spotLight.shadow.radius = value; + }); + + spotlightFolder + .add(config, 'spotlightSamples', 1, 25, 1) + .name('samples') + .onChange(function (value) { + spotLight.shadow.blurSamples = value; + }); + spotlightFolder.open(); + + const dirlightFolder = gui.addFolder('Directional Light'); + dirlightFolder + .add(config, 'dirlightRadius') + .name('radius') + .min(0) + .max(25) + .onChange(function (value) { + dirLight.shadow.radius = value; + }); + + dirlightFolder + .add(config, 'dirlightSamples', 1, 25, 1) + .name('samples') + .onChange(function (value) { + dirLight.shadow.blurSamples = value; + }); + dirlightFolder.open(); + + document.body.appendChild(renderer.domElement); + window.addEventListener('resize', onWindowResize); +} + +function initScene() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 10, 30); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x222244); + scene.fog = new THREE.Fog(0x222244, 50, 100); + + // Lights + + scene.add(new THREE.AmbientLight(0x444444)); + + spotLight = new THREE.SpotLight(0xff8888, 400); + spotLight.angle = Math.PI / 5; + spotLight.penumbra = 0.3; + spotLight.position.set(8, 10, 5); + spotLight.castShadow = true; + spotLight.shadow.camera.near = 8; + spotLight.shadow.camera.far = 200; + spotLight.shadow.mapSize.width = 256; + spotLight.shadow.mapSize.height = 256; + spotLight.shadow.bias = -0.002; + spotLight.shadow.radius = 4; + scene.add(spotLight); + + dirLight = new THREE.DirectionalLight(0x8888ff, 3); + dirLight.position.set(3, 12, 17); + dirLight.castShadow = true; + dirLight.shadow.camera.near = 0.1; + dirLight.shadow.camera.far = 500; + dirLight.shadow.camera.right = 17; + dirLight.shadow.camera.left = -17; + dirLight.shadow.camera.top = 17; + dirLight.shadow.camera.bottom = -17; + dirLight.shadow.mapSize.width = 512; + dirLight.shadow.mapSize.height = 512; + dirLight.shadow.radius = 4; + dirLight.shadow.bias = -0.0005; + + dirGroup = new THREE.Group(); + dirGroup.add(dirLight); + scene.add(dirGroup); + + // Geometry + + const geometry = new THREE.TorusKnotGeometry(25, 8, 75, 20); + const material = new THREE.MeshPhongMaterial({ + color: 0x999999, + shininess: 0, + specular: 0x222222, + }); + + torusKnot = new THREE.Mesh(geometry, material); + torusKnot.scale.multiplyScalar(1 / 18); + torusKnot.position.y = 3; + torusKnot.castShadow = true; + torusKnot.receiveShadow = true; + scene.add(torusKnot); + + const cylinderGeometry = new THREE.CylinderGeometry(0.75, 0.75, 7, 32); + + const pillar1 = new THREE.Mesh(cylinderGeometry, material); + pillar1.position.set(8, 3.5, 8); + pillar1.castShadow = true; + pillar1.receiveShadow = true; + + const pillar2 = pillar1.clone(); + pillar2.position.set(8, 3.5, -8); + const pillar3 = pillar1.clone(); + pillar3.position.set(-8, 3.5, 8); + const pillar4 = pillar1.clone(); + pillar4.position.set(-8, 3.5, -8); + + scene.add(pillar1); + scene.add(pillar2); + scene.add(pillar3); + scene.add(pillar4); + + const planeGeometry = new THREE.PlaneGeometry(200, 200); + const planeMaterial = new THREE.MeshPhongMaterial({ + color: 0x999999, + shininess: 0, + specular: 0x111111, + }); + + const ground = new THREE.Mesh(planeGeometry, planeMaterial); + ground.rotation.x = -Math.PI / 2; + ground.scale.multiplyScalar(3); + ground.castShadow = true; + ground.receiveShadow = true; + scene.add(ground); +} + +function initMisc() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + renderer.shadowMap.type = THREE.VSMShadowMap; + + // Mouse control + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 2, 0); + controls.update(); + + clock = new THREE.Clock(); + + stats = new Stats(); + document.body.appendChild(stats.dom); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate(time) { + const delta = clock.getDelta(); + + torusKnot.rotation.x += 0.25 * delta; + torusKnot.rotation.y += 0.5 * delta; + torusKnot.rotation.z += 1 * delta; + + dirGroup.rotation.y += 0.7 * delta; + dirLight.position.z = 17 + Math.sin(time * 0.001) * 5; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_shadowmesh.ts b/examples-testing/examples/webgl_shadowmesh.ts new file mode 100644 index 000000000..412fc0283 --- /dev/null +++ b/examples-testing/examples/webgl_shadowmesh.ts @@ -0,0 +1,250 @@ +import * as THREE from 'three'; + +import { ShadowMesh } from 'three/addons/objects/ShadowMesh.js'; + +let SCREEN_WIDTH = window.innerWidth; +let SCREEN_HEIGHT = window.innerHeight; + +const scene = new THREE.Scene(); +const camera = new THREE.PerspectiveCamera(55, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 3000); +const clock = new THREE.Clock(); +const renderer = new THREE.WebGLRenderer({ stencil: true }); + +const sunLight = new THREE.DirectionalLight('rgb(255,255,255)', 3); +let useDirectionalLight = true; +let arrowHelper1, arrowHelper2, arrowHelper3; +const arrowDirection = new THREE.Vector3(); +const arrowPosition1 = new THREE.Vector3(); +const arrowPosition2 = new THREE.Vector3(); +const arrowPosition3 = new THREE.Vector3(); +let groundMesh; +let lightSphere, lightHolder; +let pyramid, pyramidShadow; +let sphere, sphereShadow; +let cube, cubeShadow; +let cylinder, cylinderShadow; +let torus, torusShadow; +const normalVector = new THREE.Vector3(0, 1, 0); +const planeConstant = 0.01; // this value must be slightly higher than the groundMesh's y position of 0.0 +const groundPlane = new THREE.Plane(normalVector, planeConstant); +const lightPosition4D = new THREE.Vector4(); +let verticalAngle = 0; +let horizontalAngle = 0; +let frameTime = 0; +const TWO_PI = Math.PI * 2; + +init(); + +function init() { + scene.background = new THREE.Color(0x0096ff); + + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + renderer.setAnimationLoop(animate); + document.getElementById('container').appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); + + camera.position.set(0, 2.5, 10); + scene.add(camera); + onWindowResize(); + + sunLight.position.set(5, 7, -1); + sunLight.lookAt(scene.position); + scene.add(sunLight); + + lightPosition4D.x = sunLight.position.x; + lightPosition4D.y = sunLight.position.y; + lightPosition4D.z = sunLight.position.z; + // amount of light-ray divergence. Ranging from: + // 0.001 = sunlight(min divergence) to 1.0 = pointlight(max divergence) + lightPosition4D.w = 0.001; // must be slightly greater than 0, due to 0 causing matrixInverse errors + + // YELLOW ARROW HELPERS + arrowDirection.subVectors(scene.position, sunLight.position).normalize(); + + arrowPosition1.copy(sunLight.position); + arrowHelper1 = new THREE.ArrowHelper(arrowDirection, arrowPosition1, 0.9, 0xffff00, 0.25, 0.08); + scene.add(arrowHelper1); + + arrowPosition2.copy(sunLight.position).add(new THREE.Vector3(0, 0.2, 0)); + arrowHelper2 = new THREE.ArrowHelper(arrowDirection, arrowPosition2, 0.9, 0xffff00, 0.25, 0.08); + scene.add(arrowHelper2); + + arrowPosition3.copy(sunLight.position).add(new THREE.Vector3(0, -0.2, 0)); + arrowHelper3 = new THREE.ArrowHelper(arrowDirection, arrowPosition3, 0.9, 0xffff00, 0.25, 0.08); + scene.add(arrowHelper3); + + // LIGHTBULB + const lightSphereGeometry = new THREE.SphereGeometry(0.09); + const lightSphereMaterial = new THREE.MeshBasicMaterial({ color: 'rgb(255,255,255)' }); + lightSphere = new THREE.Mesh(lightSphereGeometry, lightSphereMaterial); + scene.add(lightSphere); + lightSphere.visible = false; + + const lightHolderGeometry = new THREE.CylinderGeometry(0.05, 0.05, 0.13); + const lightHolderMaterial = new THREE.MeshBasicMaterial({ color: 'rgb(75,75,75)' }); + lightHolder = new THREE.Mesh(lightHolderGeometry, lightHolderMaterial); + scene.add(lightHolder); + lightHolder.visible = false; + + // GROUND + const groundGeometry = new THREE.BoxGeometry(30, 0.01, 40); + const groundMaterial = new THREE.MeshLambertMaterial({ color: 'rgb(0,130,0)' }); + groundMesh = new THREE.Mesh(groundGeometry, groundMaterial); + groundMesh.position.y = 0.0; //this value must be slightly lower than the planeConstant (0.01) parameter above + scene.add(groundMesh); + + // RED CUBE and CUBE's SHADOW + const cubeGeometry = new THREE.BoxGeometry(1, 1, 1); + const cubeMaterial = new THREE.MeshLambertMaterial({ color: 'rgb(255,0,0)', emissive: 0x200000 }); + cube = new THREE.Mesh(cubeGeometry, cubeMaterial); + cube.position.z = -1; + scene.add(cube); + + cubeShadow = new ShadowMesh(cube); + scene.add(cubeShadow); + + // BLUE CYLINDER and CYLINDER's SHADOW + const cylinderGeometry = new THREE.CylinderGeometry(0.3, 0.3, 2); + const cylinderMaterial = new THREE.MeshPhongMaterial({ color: 'rgb(0,0,255)', emissive: 0x000020 }); + cylinder = new THREE.Mesh(cylinderGeometry, cylinderMaterial); + cylinder.position.z = -2.5; + scene.add(cylinder); + + cylinderShadow = new ShadowMesh(cylinder); + scene.add(cylinderShadow); + + // MAGENTA TORUS and TORUS' SHADOW + const torusGeometry = new THREE.TorusGeometry(1, 0.2, 10, 16, TWO_PI); + const torusMaterial = new THREE.MeshPhongMaterial({ color: 'rgb(255,0,255)', emissive: 0x200020 }); + torus = new THREE.Mesh(torusGeometry, torusMaterial); + torus.position.z = -6; + scene.add(torus); + + torusShadow = new ShadowMesh(torus); + scene.add(torusShadow); + + // WHITE SPHERE and SPHERE'S SHADOW + const sphereGeometry = new THREE.SphereGeometry(0.5, 20, 10); + const sphereMaterial = new THREE.MeshPhongMaterial({ color: 'rgb(255,255,255)', emissive: 0x222222 }); + sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); + sphere.position.set(4, 0.5, 2); + scene.add(sphere); + + sphereShadow = new ShadowMesh(sphere); + scene.add(sphereShadow); + + // YELLOW PYRAMID and PYRAMID'S SHADOW + const pyramidGeometry = new THREE.CylinderGeometry(0, 0.5, 2, 4); + const pyramidMaterial = new THREE.MeshPhongMaterial({ + color: 'rgb(255,255,0)', + emissive: 0x440000, + flatShading: true, + shininess: 0, + }); + pyramid = new THREE.Mesh(pyramidGeometry, pyramidMaterial); + pyramid.position.set(-4, 1, 2); + scene.add(pyramid); + + pyramidShadow = new ShadowMesh(pyramid); + scene.add(pyramidShadow); + + document.getElementById('lightButton').addEventListener('click', lightButtonHandler); +} + +function animate() { + frameTime = clock.getDelta(); + + cube.rotation.x += 1.0 * frameTime; + cube.rotation.y += 1.0 * frameTime; + + cylinder.rotation.y += 1.0 * frameTime; + cylinder.rotation.z -= 1.0 * frameTime; + + torus.rotation.x -= 1.0 * frameTime; + torus.rotation.y -= 1.0 * frameTime; + + pyramid.rotation.y += 0.5 * frameTime; + + horizontalAngle += 0.5 * frameTime; + if (horizontalAngle > TWO_PI) horizontalAngle -= TWO_PI; + cube.position.x = Math.sin(horizontalAngle) * 4; + cylinder.position.x = Math.sin(horizontalAngle) * -4; + torus.position.x = Math.cos(horizontalAngle) * 4; + + verticalAngle += 1.5 * frameTime; + if (verticalAngle > TWO_PI) verticalAngle -= TWO_PI; + cube.position.y = Math.sin(verticalAngle) * 2 + 2.9; + cylinder.position.y = Math.sin(verticalAngle) * 2 + 3.1; + torus.position.y = Math.cos(verticalAngle) * 2 + 3.3; + + // update the ShadowMeshes to follow their shadow-casting objects + cubeShadow.update(groundPlane, lightPosition4D); + cylinderShadow.update(groundPlane, lightPosition4D); + torusShadow.update(groundPlane, lightPosition4D); + sphereShadow.update(groundPlane, lightPosition4D); + pyramidShadow.update(groundPlane, lightPosition4D); + + renderer.render(scene, camera); +} + +function onWindowResize() { + SCREEN_WIDTH = window.innerWidth; + SCREEN_HEIGHT = window.innerHeight; + + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + + camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT; + camera.updateProjectionMatrix(); +} + +function lightButtonHandler() { + useDirectionalLight = !useDirectionalLight; + + if (useDirectionalLight) { + scene.background.setHex(0x0096ff); + + groundMesh.material.color.setHex(0x008200); + sunLight.position.set(5, 7, -1); + sunLight.lookAt(scene.position); + + lightPosition4D.x = sunLight.position.x; + lightPosition4D.y = sunLight.position.y; + lightPosition4D.z = sunLight.position.z; + lightPosition4D.w = 0.001; // more of a directional Light value + + arrowHelper1.visible = true; + arrowHelper2.visible = true; + arrowHelper3.visible = true; + + lightSphere.visible = false; + lightHolder.visible = false; + + document.getElementById('lightButton').value = 'Switch to PointLight'; + } else { + scene.background.setHex(0x000000); + + groundMesh.material.color.setHex(0x969696); + + sunLight.position.set(0, 6, -2); + sunLight.lookAt(scene.position); + lightSphere.position.copy(sunLight.position); + lightHolder.position.copy(lightSphere.position); + lightHolder.position.y += 0.12; + + lightPosition4D.x = sunLight.position.x; + lightPosition4D.y = sunLight.position.y; + lightPosition4D.z = sunLight.position.z; + lightPosition4D.w = 0.9; // more of a point Light value + + arrowHelper1.visible = false; + arrowHelper2.visible = false; + arrowHelper3.visible = false; + + lightSphere.visible = true; + lightHolder.visible = true; + + document.getElementById('lightButton').value = 'Switch to THREE.DirectionalLight'; + } +} diff --git a/examples-testing/examples/webgl_simple_gi.ts b/examples-testing/examples/webgl_simple_gi.ts new file mode 100644 index 000000000..4ab6dc895 --- /dev/null +++ b/examples-testing/examples/webgl_simple_gi.ts @@ -0,0 +1,172 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +class GIMesh extends THREE.Mesh { + copy(source) { + super.copy(source); + + this.geometry = source.geometry.clone(); + + return this; + } +} + +// + +const SimpleGI = function (renderer, scene) { + const SIZE = 32, + SIZE2 = SIZE * SIZE; + + const camera = new THREE.PerspectiveCamera(90, 1, 0.01, 100); + + scene.updateMatrixWorld(true); + + let clone = scene.clone(); + clone.matrixWorldAutoUpdate = false; + + const rt = new THREE.WebGLRenderTarget(SIZE, SIZE); + + const normalMatrix = new THREE.Matrix3(); + + const position = new THREE.Vector3(); + const normal = new THREE.Vector3(); + + let bounces = 0; + let currentVertex = 0; + + const color = new Float32Array(3); + const buffer = new Uint8Array(SIZE2 * 4); + + function compute() { + if (bounces === 3) return; + + const object = scene.children[0]; // torusKnot + const geometry = object.geometry; + + const attributes = geometry.attributes; + const positions = attributes.position.array; + const normals = attributes.normal.array; + + if (attributes.color === undefined) { + const colors = new Float32Array(positions.length); + geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3).setUsage(THREE.DynamicDrawUsage)); + } + + const colors = attributes.color.array; + + const startVertex = currentVertex; + const totalVertex = positions.length / 3; + + for (let i = 0; i < 32; i++) { + if (currentVertex >= totalVertex) break; + + position.fromArray(positions, currentVertex * 3); + position.applyMatrix4(object.matrixWorld); + + normal.fromArray(normals, currentVertex * 3); + normal.applyMatrix3(normalMatrix.getNormalMatrix(object.matrixWorld)).normalize(); + + camera.position.copy(position); + camera.lookAt(position.add(normal)); + + renderer.setRenderTarget(rt); + renderer.render(clone, camera); + + renderer.readRenderTargetPixels(rt, 0, 0, SIZE, SIZE, buffer); + + color[0] = 0; + color[1] = 0; + color[2] = 0; + + for (let k = 0, kl = buffer.length; k < kl; k += 4) { + color[0] += buffer[k + 0]; + color[1] += buffer[k + 1]; + color[2] += buffer[k + 2]; + } + + colors[currentVertex * 3 + 0] = color[0] / (SIZE2 * 255); + colors[currentVertex * 3 + 1] = color[1] / (SIZE2 * 255); + colors[currentVertex * 3 + 2] = color[2] / (SIZE2 * 255); + + currentVertex++; + } + + attributes.color.addUpdateRange(startVertex * 3, (currentVertex - startVertex) * 3); + attributes.color.needsUpdate = true; + + if (currentVertex >= totalVertex) { + clone = scene.clone(); + clone.matrixWorldAutoUpdate = false; + + bounces++; + currentVertex = 0; + } + + requestAnimationFrame(compute); + } + + requestAnimationFrame(compute); +}; + +// + +let camera, scene, renderer; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.z = 4; + + scene = new THREE.Scene(); + + // torus knot + + const torusGeometry = new THREE.TorusKnotGeometry(0.75, 0.3, 128, 32, 1); + const material = new THREE.MeshBasicMaterial({ vertexColors: true }); + + const torusKnot = new GIMesh(torusGeometry, material); + scene.add(torusKnot); + + // room + + const materials = []; + + for (let i = 0; i < 8; i++) { + materials.push(new THREE.MeshBasicMaterial({ color: Math.random() * 0xffffff, side: THREE.BackSide })); + } + + const boxGeometry = new THREE.BoxGeometry(3, 3, 3); + + const box = new THREE.Mesh(boxGeometry, materials); + scene.add(box); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + new SimpleGI(renderer, scene); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 1; + controls.maxDistance = 10; + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.setRenderTarget(null); + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_sprites.ts b/examples-testing/examples/webgl_sprites.ts new file mode 100644 index 000000000..2e4189347 --- /dev/null +++ b/examples-testing/examples/webgl_sprites.ts @@ -0,0 +1,187 @@ +import * as THREE from 'three'; + +let camera, scene, renderer; +let cameraOrtho, sceneOrtho; + +let spriteTL, spriteTR, spriteBL, spriteBR, spriteC; + +let mapC; + +let group; + +init(); + +function init() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera = new THREE.PerspectiveCamera(60, width / height, 1, 2100); + camera.position.z = 1500; + + cameraOrtho = new THREE.OrthographicCamera(-width / 2, width / 2, height / 2, -height / 2, 1, 10); + cameraOrtho.position.z = 10; + + scene = new THREE.Scene(); + scene.fog = new THREE.Fog(0x000000, 1500, 2100); + + sceneOrtho = new THREE.Scene(); + + // create sprites + + const amount = 200; + const radius = 500; + + const textureLoader = new THREE.TextureLoader(); + + textureLoader.load('textures/sprite0.png', createHUDSprites); + const mapB = textureLoader.load('textures/sprite1.png'); + mapC = textureLoader.load('textures/sprite2.png'); + + mapB.colorSpace = THREE.SRGBColorSpace; + mapC.colorSpace = THREE.SRGBColorSpace; + + group = new THREE.Group(); + + const materialC = new THREE.SpriteMaterial({ map: mapC, color: 0xffffff, fog: true }); + const materialB = new THREE.SpriteMaterial({ map: mapB, color: 0xffffff, fog: true }); + + for (let a = 0; a < amount; a++) { + const x = Math.random() - 0.5; + const y = Math.random() - 0.5; + const z = Math.random() - 0.5; + + let material; + + if (z < 0) { + material = materialB.clone(); + } else { + material = materialC.clone(); + material.color.setHSL(0.5 * Math.random(), 0.75, 0.5); + material.map.offset.set(-0.5, -0.5); + material.map.repeat.set(2, 2); + } + + const sprite = new THREE.Sprite(material); + + sprite.position.set(x, y, z); + sprite.position.normalize(); + sprite.position.multiplyScalar(radius); + + group.add(sprite); + } + + scene.add(group); + + // renderer + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.autoClear = false; // To allow render overlay on top of sprited sphere + + document.body.appendChild(renderer.domElement); + + // + + window.addEventListener('resize', onWindowResize); +} + +function createHUDSprites(texture) { + texture.colorSpace = THREE.SRGBColorSpace; + + const material = new THREE.SpriteMaterial({ map: texture }); + + const width = material.map.image.width; + const height = material.map.image.height; + + spriteTL = new THREE.Sprite(material); + spriteTL.center.set(0.0, 1.0); + spriteTL.scale.set(width, height, 1); + sceneOrtho.add(spriteTL); + + spriteTR = new THREE.Sprite(material); + spriteTR.center.set(1.0, 1.0); + spriteTR.scale.set(width, height, 1); + sceneOrtho.add(spriteTR); + + spriteBL = new THREE.Sprite(material); + spriteBL.center.set(0.0, 0.0); + spriteBL.scale.set(width, height, 1); + sceneOrtho.add(spriteBL); + + spriteBR = new THREE.Sprite(material); + spriteBR.center.set(1.0, 0.0); + spriteBR.scale.set(width, height, 1); + sceneOrtho.add(spriteBR); + + spriteC = new THREE.Sprite(material); + spriteC.center.set(0.5, 0.5); + spriteC.scale.set(width, height, 1); + sceneOrtho.add(spriteC); + + updateHUDSprites(); +} + +function updateHUDSprites() { + const width = window.innerWidth / 2; + const height = window.innerHeight / 2; + + spriteTL.position.set(-width, height, 1); // top left + spriteTR.position.set(width, height, 1); // top right + spriteBL.position.set(-width, -height, 1); // bottom left + spriteBR.position.set(width, -height, 1); // bottom right + spriteC.position.set(0, 0, 1); // center +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + cameraOrtho.left = -width / 2; + cameraOrtho.right = width / 2; + cameraOrtho.top = height / 2; + cameraOrtho.bottom = -height / 2; + cameraOrtho.updateProjectionMatrix(); + + updateHUDSprites(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const time = Date.now() / 1000; + + for (let i = 0, l = group.children.length; i < l; i++) { + const sprite = group.children[i]; + const material = sprite.material; + const scale = Math.sin(time + sprite.position.x * 0.01) * 0.3 + 1.0; + + let imageWidth = 1; + let imageHeight = 1; + + if (material.map && material.map.image && material.map.image.width) { + imageWidth = material.map.image.width; + imageHeight = material.map.image.height; + } + + sprite.material.rotation += 0.1 * (i / l); + sprite.scale.set(scale * imageWidth, scale * imageHeight, 1.0); + + if (material.map !== mapC) { + material.opacity = Math.sin(time + sprite.position.x * 0.01) * 0.4 + 0.6; + } + } + + group.rotation.x = time * 0.5; + group.rotation.y = time * 0.75; + group.rotation.z = time * 1.0; + + renderer.clear(); + renderer.render(scene, camera); + renderer.clearDepth(); + renderer.render(sceneOrtho, cameraOrtho); +} diff --git a/examples-testing/examples/webgl_test_memory.ts b/examples-testing/examples/webgl_test_memory.ts new file mode 100644 index 000000000..f5d0e112d --- /dev/null +++ b/examples-testing/examples/webgl_test_memory.ts @@ -0,0 +1,65 @@ +import * as THREE from 'three'; + +let camera, scene, renderer; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 200; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); +} + +function createImage() { + const canvas = document.createElement('canvas'); + canvas.width = 256; + canvas.height = 256; + + const context = canvas.getContext('2d'); + context.fillStyle = + 'rgb(' + + Math.floor(Math.random() * 256) + + ',' + + Math.floor(Math.random() * 256) + + ',' + + Math.floor(Math.random() * 256) + + ')'; + context.fillRect(0, 0, 256, 256); + + return canvas; +} + +// + +function animate() { + const geometry = new THREE.SphereGeometry(50, Math.random() * 64, Math.random() * 32); + + const texture = new THREE.CanvasTexture(createImage()); + + const material = new THREE.MeshBasicMaterial({ map: texture, wireframe: true }); + + const mesh = new THREE.Mesh(geometry, material); + + scene.add(mesh); + + renderer.render(scene, camera); + + scene.remove(mesh); + + // clean up + + geometry.dispose(); + material.dispose(); + texture.dispose(); +} diff --git a/examples-testing/examples/webgl_test_memory2.ts b/examples-testing/examples/webgl_test_memory2.ts new file mode 100644 index 000000000..366a27914 --- /dev/null +++ b/examples-testing/examples/webgl_test_memory2.ts @@ -0,0 +1,81 @@ +import * as THREE from 'three'; + +const N = 100; + +let container; + +let camera, scene, renderer; + +let geometry; + +const meshes = []; + +let fragmentShader, vertexShader; + +init(); +setInterval(render, 1000 / 60); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + vertexShader = document.getElementById('vertexShader').textContent; + fragmentShader = document.getElementById('fragmentShader').textContent; + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 2000; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + + geometry = new THREE.SphereGeometry(15, 64, 32); + + for (let i = 0; i < N; i++) { + const material = new THREE.ShaderMaterial({ + vertexShader: vertexShader, + fragmentShader: generateFragmentShader(), + }); + + const mesh = new THREE.Mesh(geometry, material); + + mesh.position.x = (0.5 - Math.random()) * 1000; + mesh.position.y = (0.5 - Math.random()) * 1000; + mesh.position.z = (0.5 - Math.random()) * 1000; + + scene.add(mesh); + + meshes.push(mesh); + } + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + container.appendChild(renderer.domElement); +} + +// + +function generateFragmentShader() { + return fragmentShader.replace('XXX', Math.random() + ',' + Math.random() + ',' + Math.random()); +} + +function render() { + for (let i = 0; i < N; i++) { + const mesh = meshes[i]; + mesh.material = new THREE.ShaderMaterial({ + vertexShader: vertexShader, + fragmentShader: generateFragmentShader(), + }); + } + + renderer.render(scene, camera); + + console.log('before', renderer.info.programs.length); + + for (let i = 0; i < N; i++) { + const mesh = meshes[i]; + mesh.material.dispose(); + } + + console.log('after', renderer.info.programs.length); +} diff --git a/examples-testing/examples/webgl_test_wide_gamut.ts b/examples-testing/examples/webgl_test_wide_gamut.ts new file mode 100644 index 000000000..693dd4593 --- /dev/null +++ b/examples-testing/examples/webgl_test_wide_gamut.ts @@ -0,0 +1,118 @@ +import * as THREE from 'three'; + +import WebGL from 'three/addons/capabilities/WebGL.js'; + +let container, camera, renderer, loader; +let sceneL, sceneR, textureL, textureR; + +let sliderPos = window.innerWidth / 2; + +const slider = document.querySelector('.slider'); + +const isP3Context = WebGL.isColorSpaceAvailable(THREE.DisplayP3ColorSpace); + +if (isP3Context) { + THREE.ColorManagement.workingColorSpace = THREE.LinearDisplayP3ColorSpace; +} + +init(); + +function init() { + container = document.querySelector('.container'); + + sceneL = new THREE.Scene(); + sceneR = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.z = 6; + + loader = new THREE.TextureLoader(); + + initTextures(); + initSlider(); + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.setScissorTest(true); + container.appendChild(renderer.domElement); + + if (isP3Context && window.matchMedia('( color-gamut: p3 )').matches) { + renderer.outputColorSpace = THREE.DisplayP3ColorSpace; + } + + window.addEventListener('resize', onWindowResize); + window.matchMedia('( color-gamut: p3 )').addEventListener('change', onGamutChange); +} + +async function initTextures() { + const path = 'textures/wide_gamut/logo_{colorSpace}.png'; + + textureL = await loader.loadAsync(path.replace('{colorSpace}', 'srgb')); + textureR = await loader.loadAsync(path.replace('{colorSpace}', 'p3')); + + textureL.colorSpace = THREE.SRGBColorSpace; + textureR.colorSpace = THREE.DisplayP3ColorSpace; + + sceneL.background = THREE.TextureUtils.contain(textureL, window.innerWidth / window.innerHeight); + sceneR.background = THREE.TextureUtils.contain(textureR, window.innerWidth / window.innerHeight); +} + +function initSlider() { + function onPointerDown() { + if (event.isPrimary === false) return; + + window.addEventListener('pointermove', onPointerMove); + window.addEventListener('pointerup', onPointerUp); + } + + function onPointerUp() { + window.removeEventListener('pointermove', onPointerMove); + window.removeEventListener('pointerup', onPointerUp); + } + + function onPointerMove(e) { + if (event.isPrimary === false) return; + + updateSlider(e.pageX); + } + + updateSlider(sliderPos); + + slider.style.touchAction = 'none'; // disable touch scroll + slider.addEventListener('pointerdown', onPointerDown); +} + +function updateSlider(offset) { + sliderPos = Math.max(10, Math.min(window.innerWidth - 10, offset)); + + slider.style.left = sliderPos - slider.offsetWidth / 2 + 'px'; +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + THREE.TextureUtils.contain(sceneL.background, window.innerWidth / window.innerHeight); + THREE.TextureUtils.contain(sceneR.background, window.innerWidth / window.innerHeight); + + updateSlider(sliderPos); +} + +function onGamutChange({ matches }) { + renderer.outputColorSpace = isP3Context && matches ? THREE.DisplayP3ColorSpace : THREE.SRGBColorSpace; + + textureL.needsUpdate = true; + textureR.needsUpdate = true; +} + +function animate() { + renderer.setScissor(0, 0, sliderPos, window.innerHeight); + renderer.render(sceneL, camera); + + renderer.setScissor(sliderPos, 0, window.innerWidth, window.innerHeight); + renderer.render(sceneR, camera); +} diff --git a/examples-testing/examples/webgl_texture2darray_compressed.ts b/examples-testing/examples/webgl_texture2darray_compressed.ts new file mode 100644 index 000000000..f263be706 --- /dev/null +++ b/examples-testing/examples/webgl_texture2darray_compressed.ts @@ -0,0 +1,88 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js'; + +let camera, scene, mesh, renderer, stats, clock; + +const planeWidth = 50; +const planeHeight = 25; + +let depthStep = 1; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 2000); + camera.position.z = 70; + + scene = new THREE.Scene(); + + // + clock = new THREE.Clock(); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + const ktx2Loader = new KTX2Loader(); + ktx2Loader.setTranscoderPath('jsm/libs/basis/'); + ktx2Loader.detectSupport(renderer); + + ktx2Loader.load('textures/spiritedaway.ktx2', function (texturearray) { + const material = new THREE.ShaderMaterial({ + uniforms: { + diffuse: { value: texturearray }, + depth: { value: 55 }, + size: { value: new THREE.Vector2(planeWidth, planeHeight) }, + }, + vertexShader: document.getElementById('vs').textContent.trim(), + fragmentShader: document.getElementById('fs').textContent.trim(), + glslVersion: THREE.GLSL3, + }); + + const geometry = new THREE.PlaneGeometry(planeWidth, planeHeight); + + mesh = new THREE.Mesh(geometry, material); + + scene.add(mesh); + }); + + stats = new Stats(); + container.appendChild(stats.dom); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + if (mesh) { + const delta = clock.getDelta() * 10; + + depthStep += delta; + + const value = depthStep % 5; + + mesh.material.uniforms['depth'].value = value; + } + + render(); + stats.update(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_texture2darray_layerupdate.ts b/examples-testing/examples/webgl_texture2darray_layerupdate.ts new file mode 100644 index 000000000..599fd8579 --- /dev/null +++ b/examples-testing/examples/webgl_texture2darray_layerupdate.ts @@ -0,0 +1,124 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js'; + +let camera, scene, mesh, renderer; + +const planeWidth = 20; +const planeHeight = 10; + +init(); + +async function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 2000); + camera.position.z = 70; + + scene = new THREE.Scene(); + + // Configure the renderer. + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + container.appendChild(renderer.domElement); + + // Configure the KTX2 loader. + + const ktx2Loader = new KTX2Loader(); + ktx2Loader.setTranscoderPath('jsm/libs/basis/'); + ktx2Loader.detectSupport(renderer); + + // Load several KTX2 textures which will later be used to modify + // specific texture array layers. + + const spiritedaway = await ktx2Loader.loadAsync('textures/spiritedaway.ktx2'); + + // Create a texture array for rendering. + + const layerByteLength = THREE.TextureUtils.getByteLength( + spiritedaway.image.width, + spiritedaway.image.height, + spiritedaway.format, + spiritedaway.type, + ); + + const textureArray = new THREE.CompressedArrayTexture( + [ + { + data: new Uint8Array(layerByteLength * 3), + width: spiritedaway.image.width, + height: spiritedaway.image.height, + }, + ], + spiritedaway.image.width, + spiritedaway.image.height, + 3, + spiritedaway.format, + spiritedaway.type, + ); + + // Setup the GUI + + const formData = { + srcLayer: 0, + destLayer: 0, + transfer() { + const layerElementLength = layerByteLength / spiritedaway.mipmaps[0].data.BYTES_PER_ELEMENT; + textureArray.mipmaps[0].data.set( + spiritedaway.mipmaps[0].data.subarray( + layerElementLength * (formData.srcLayer % spiritedaway.image.depth), + layerElementLength * ((formData.srcLayer % spiritedaway.image.depth) + 1), + ), + layerByteLength * formData.destLayer, + ); + textureArray.addLayerUpdate(formData.destLayer); + textureArray.needsUpdate = true; + + renderer.render(scene, camera); + }, + }; + + const gui = new GUI(); + gui.add(formData, 'srcLayer', 0, spiritedaway.image.depth - 1, 1); + gui.add(formData, 'destLayer', 0, textureArray.image.depth - 1, 1); + gui.add(formData, 'transfer'); + + /// Setup the scene. + + const material = new THREE.ShaderMaterial({ + uniforms: { + diffuse: { value: textureArray }, + size: { value: new THREE.Vector2(planeWidth, planeHeight) }, + }, + vertexShader: document.getElementById('vs').textContent.trim(), + fragmentShader: document.getElementById('fs').textContent.trim(), + glslVersion: THREE.GLSL3, + }); + + const geometry = new THREE.InstancedBufferGeometry(); + geometry.copy(new THREE.PlaneGeometry(planeWidth, planeHeight)); + geometry.instanceCount = 3; + + const instancedIndexAttribute = new THREE.InstancedBufferAttribute(new Uint16Array([0, 1, 2]), 1, false, 1); + instancedIndexAttribute.gpuType = THREE.IntType; + geometry.setAttribute('instancedIndex', instancedIndexAttribute); + + mesh = new THREE.InstancedMesh(geometry, material, 3); + + scene.add(mesh); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_texture3d.ts b/examples-testing/examples/webgl_texture3d.ts new file mode 100644 index 000000000..977dbadb7 --- /dev/null +++ b/examples-testing/examples/webgl_texture3d.ts @@ -0,0 +1,128 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { NRRDLoader } from 'three/addons/loaders/NRRDLoader.js'; +import { VolumeRenderShader1 } from 'three/addons/shaders/VolumeShader.js'; + +let renderer, scene, camera, controls, material, volconfig, cmtextures; + +init(); + +function init() { + scene = new THREE.Scene(); + + // Create renderer + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + // Create camera (The volume renderer does not work very well with perspective yet) + const h = 512; // frustum height + const aspect = window.innerWidth / window.innerHeight; + camera = new THREE.OrthographicCamera((-h * aspect) / 2, (h * aspect) / 2, h / 2, -h / 2, 1, 1000); + camera.position.set(-64, -64, 128); + camera.up.set(0, 0, 1); // In our data, z is up + + // Create controls + controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); + controls.target.set(64, 64, 128); + controls.minZoom = 0.5; + controls.maxZoom = 4; + controls.enablePan = false; + controls.update(); + + // scene.add( new AxesHelper( 128 ) ); + + // Lighting is baked into the shader a.t.m. + // let dirLight = new DirectionalLight( 0xffffff ); + + // The gui for interaction + volconfig = { clim1: 0, clim2: 1, renderstyle: 'iso', isothreshold: 0.15, colormap: 'viridis' }; + const gui = new GUI(); + gui.add(volconfig, 'clim1', 0, 1, 0.01).onChange(updateUniforms); + gui.add(volconfig, 'clim2', 0, 1, 0.01).onChange(updateUniforms); + gui.add(volconfig, 'colormap', { gray: 'gray', viridis: 'viridis' }).onChange(updateUniforms); + gui.add(volconfig, 'renderstyle', { mip: 'mip', iso: 'iso' }).onChange(updateUniforms); + gui.add(volconfig, 'isothreshold', 0, 1, 0.01).onChange(updateUniforms); + + // Load the data ... + new NRRDLoader().load('models/nrrd/stent.nrrd', function (volume) { + // Texture to hold the volume. We have scalars, so we put our data in the red channel. + // THREEJS will select R32F (33326) based on the THREE.RedFormat and THREE.FloatType. + // Also see https://www.khronos.org/registry/webgl/specs/latest/2.0/#TEXTURE_TYPES_FORMATS_FROM_DOM_ELEMENTS_TABLE + // TODO: look the dtype up in the volume metadata + const texture = new THREE.Data3DTexture(volume.data, volume.xLength, volume.yLength, volume.zLength); + texture.format = THREE.RedFormat; + texture.type = THREE.FloatType; + texture.minFilter = texture.magFilter = THREE.LinearFilter; + texture.unpackAlignment = 1; + texture.needsUpdate = true; + + // Colormap textures + cmtextures = { + viridis: new THREE.TextureLoader().load('textures/cm_viridis.png', render), + gray: new THREE.TextureLoader().load('textures/cm_gray.png', render), + }; + + // Material + const shader = VolumeRenderShader1; + + const uniforms = THREE.UniformsUtils.clone(shader.uniforms); + + uniforms['u_data'].value = texture; + uniforms['u_size'].value.set(volume.xLength, volume.yLength, volume.zLength); + uniforms['u_clim'].value.set(volconfig.clim1, volconfig.clim2); + uniforms['u_renderstyle'].value = volconfig.renderstyle == 'mip' ? 0 : 1; // 0: MIP, 1: ISO + uniforms['u_renderthreshold'].value = volconfig.isothreshold; // For ISO renderstyle + uniforms['u_cmdata'].value = cmtextures[volconfig.colormap]; + + material = new THREE.ShaderMaterial({ + uniforms: uniforms, + vertexShader: shader.vertexShader, + fragmentShader: shader.fragmentShader, + side: THREE.BackSide, // The volume shader uses the backface as its "reference point" + }); + + // THREE.Mesh + const geometry = new THREE.BoxGeometry(volume.xLength, volume.yLength, volume.zLength); + geometry.translate(volume.xLength / 2 - 0.5, volume.yLength / 2 - 0.5, volume.zLength / 2 - 0.5); + + const mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + render(); + }); + + window.addEventListener('resize', onWindowResize); +} + +function updateUniforms() { + material.uniforms['u_clim'].value.set(volconfig.clim1, volconfig.clim2); + material.uniforms['u_renderstyle'].value = volconfig.renderstyle == 'mip' ? 0 : 1; // 0: MIP, 1: ISO + material.uniforms['u_renderthreshold'].value = volconfig.isothreshold; // For ISO renderstyle + material.uniforms['u_cmdata'].value = cmtextures[volconfig.colormap]; + + render(); +} + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); + + const aspect = window.innerWidth / window.innerHeight; + + const frustumHeight = camera.top - camera.bottom; + + camera.left = (-frustumHeight * aspect) / 2; + camera.right = (frustumHeight * aspect) / 2; + + camera.updateProjectionMatrix(); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_texture3d_partialupdate.ts b/examples-testing/examples/webgl_texture3d_partialupdate.ts new file mode 100644 index 000000000..1ad6d2646 --- /dev/null +++ b/examples-testing/examples/webgl_texture3d_partialupdate.ts @@ -0,0 +1,326 @@ +import * as THREE from 'three'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +const INITIAL_CLOUD_SIZE = 128; + +let renderer, scene, camera; +let mesh; +let prevTime = performance.now(); +let cloudTexture = null; + +init(); + +function generateCloudTexture(size, scaleFactor = 1.0) { + const data = new Uint8Array(size * size * size); + const scale = (scaleFactor * 10.0) / size; + + let i = 0; + const perlin = new ImprovedNoise(); + const vector = new THREE.Vector3(); + + for (let z = 0; z < size; z++) { + for (let y = 0; y < size; y++) { + for (let x = 0; x < size; x++) { + const dist = vector + .set(x, y, z) + .subScalar(size / 2) + .divideScalar(size) + .length(); + const fadingFactor = (1.0 - dist) * (1.0 - dist); + data[i] = (128 + 128 * perlin.noise((x * scale) / 1.5, y * scale, (z * scale) / 1.5)) * fadingFactor; + + i++; + } + } + } + + return new THREE.Data3DTexture(data, size, size, size); +} + +function init() { + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(0, 0, 1.5); + + new OrbitControls(camera, renderer.domElement); + + // Sky + + const canvas = document.createElement('canvas'); + canvas.width = 1; + canvas.height = 32; + + const context = canvas.getContext('2d'); + const gradient = context.createLinearGradient(0, 0, 0, 32); + gradient.addColorStop(0.0, '#014a84'); + gradient.addColorStop(0.5, '#0561a0'); + gradient.addColorStop(1.0, '#437ab6'); + context.fillStyle = gradient; + context.fillRect(0, 0, 1, 32); + + const skyMap = new THREE.CanvasTexture(canvas); + skyMap.colorSpace = THREE.SRGBColorSpace; + + const sky = new THREE.Mesh( + new THREE.SphereGeometry(10), + new THREE.MeshBasicMaterial({ map: skyMap, side: THREE.BackSide }), + ); + scene.add(sky); + + // Texture + + const texture = new THREE.Data3DTexture( + new Uint8Array(INITIAL_CLOUD_SIZE * INITIAL_CLOUD_SIZE * INITIAL_CLOUD_SIZE).fill(0), + INITIAL_CLOUD_SIZE, + INITIAL_CLOUD_SIZE, + INITIAL_CLOUD_SIZE, + ); + texture.format = THREE.RedFormat; + texture.minFilter = THREE.LinearFilter; + texture.magFilter = THREE.LinearFilter; + texture.unpackAlignment = 1; + texture.needsUpdate = true; + + cloudTexture = texture; + + // Material + + const vertexShader = /* glsl */ ` + in vec3 position; + + uniform mat4 modelMatrix; + uniform mat4 modelViewMatrix; + uniform mat4 projectionMatrix; + uniform vec3 cameraPos; + + out vec3 vOrigin; + out vec3 vDirection; + + void main() { + vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); + + vOrigin = vec3( inverse( modelMatrix ) * vec4( cameraPos, 1.0 ) ).xyz; + vDirection = position - vOrigin; + + gl_Position = projectionMatrix * mvPosition; + } + `; + + const fragmentShader = /* glsl */ ` + precision highp float; + precision highp sampler3D; + + uniform mat4 modelViewMatrix; + uniform mat4 projectionMatrix; + + in vec3 vOrigin; + in vec3 vDirection; + + out vec4 color; + + uniform vec3 base; + uniform sampler3D map; + + uniform float threshold; + uniform float range; + uniform float opacity; + uniform float steps; + uniform float frame; + + uint wang_hash(uint seed) + { + seed = (seed ^ 61u) ^ (seed >> 16u); + seed *= 9u; + seed = seed ^ (seed >> 4u); + seed *= 0x27d4eb2du; + seed = seed ^ (seed >> 15u); + return seed; + } + + float randomFloat(inout uint seed) + { + return float(wang_hash(seed)) / 4294967296.; + } + + vec2 hitBox( vec3 orig, vec3 dir ) { + const vec3 box_min = vec3( - 0.5 ); + const vec3 box_max = vec3( 0.5 ); + vec3 inv_dir = 1.0 / dir; + vec3 tmin_tmp = ( box_min - orig ) * inv_dir; + vec3 tmax_tmp = ( box_max - orig ) * inv_dir; + vec3 tmin = min( tmin_tmp, tmax_tmp ); + vec3 tmax = max( tmin_tmp, tmax_tmp ); + float t0 = max( tmin.x, max( tmin.y, tmin.z ) ); + float t1 = min( tmax.x, min( tmax.y, tmax.z ) ); + return vec2( t0, t1 ); + } + + float sample1( vec3 p ) { + return texture( map, p ).r; + } + + float shading( vec3 coord ) { + float step = 0.01; + return sample1( coord + vec3( - step ) ) - sample1( coord + vec3( step ) ); + } + + vec4 linearToSRGB( in vec4 value ) { + return vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) * 1.055 - vec3( 0.055 ), value.rgb * 12.92, vec3( lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ) ), value.a ); + } + + void main(){ + vec3 rayDir = normalize( vDirection ); + vec2 bounds = hitBox( vOrigin, rayDir ); + + if ( bounds.x > bounds.y ) discard; + + bounds.x = max( bounds.x, 0.0 ); + + vec3 p = vOrigin + bounds.x * rayDir; + vec3 inc = 1.0 / abs( rayDir ); + float delta = min( inc.x, min( inc.y, inc.z ) ); + delta /= steps; + + // Jitter + + // Nice little seed from + // https://blog.demofox.org/2020/05/25/casual-shadertoy-path-tracing-1-basic-camera-diffuse-emissive/ + uint seed = uint( gl_FragCoord.x ) * uint( 1973 ) + uint( gl_FragCoord.y ) * uint( 9277 ) + uint( frame ) * uint( 26699 ); + vec3 size = vec3( textureSize( map, 0 ) ); + float randNum = randomFloat( seed ) * 2.0 - 1.0; + p += rayDir * randNum * ( 1.0 / size ); + + // + + vec4 ac = vec4( base, 0.0 ); + + for ( float t = bounds.x; t < bounds.y; t += delta ) { + + float d = sample1( p + 0.5 ); + + d = smoothstep( threshold - range, threshold + range, d ) * opacity; + + float col = shading( p + 0.5 ) * 3.0 + ( ( p.x + p.y ) * 0.25 ) + 0.2; + + ac.rgb += ( 1.0 - ac.a ) * d * col; + + ac.a += ( 1.0 - ac.a ) * d; + + if ( ac.a >= 0.95 ) break; + + p += rayDir * delta; + + } + + color = linearToSRGB( ac ); + + if ( color.a == 0.0 ) discard; + + } + `; + + const geometry = new THREE.BoxGeometry(1, 1, 1); + const material = new THREE.RawShaderMaterial({ + glslVersion: THREE.GLSL3, + uniforms: { + base: { value: new THREE.Color(0x798aa0) }, + map: { value: texture }, + cameraPos: { value: new THREE.Vector3() }, + threshold: { value: 0.25 }, + opacity: { value: 0.25 }, + range: { value: 0.1 }, + steps: { value: 100 }, + frame: { value: 0 }, + }, + vertexShader, + fragmentShader, + side: THREE.BackSide, + transparent: true, + }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + const parameters = { + threshold: 0.25, + opacity: 0.25, + range: 0.1, + steps: 100, + }; + + function update() { + material.uniforms.threshold.value = parameters.threshold; + material.uniforms.opacity.value = parameters.opacity; + material.uniforms.range.value = parameters.range; + material.uniforms.steps.value = parameters.steps; + } + + const gui = new GUI(); + gui.add(parameters, 'threshold', 0, 1, 0.01).onChange(update); + gui.add(parameters, 'opacity', 0, 1, 0.01).onChange(update); + gui.add(parameters, 'range', 0, 1, 0.01).onChange(update); + gui.add(parameters, 'steps', 0, 200, 1).onChange(update); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +let curr = 0; +const countPerRow = 4; +const countPerSlice = countPerRow * countPerRow; +const sliceCount = 4; +const totalCount = sliceCount * countPerSlice; +const margins = 8; + +const perElementPaddedSize = (INITIAL_CLOUD_SIZE - margins) / countPerRow; +const perElementSize = Math.floor((INITIAL_CLOUD_SIZE - 1) / countPerRow); + +function animate() { + const time = performance.now(); + if (time - prevTime > 1500.0 && curr < totalCount) { + const position = new THREE.Vector3( + Math.floor(curr % countPerRow) * perElementSize + margins * 0.5, + Math.floor((curr % countPerSlice) / countPerRow) * perElementSize + margins * 0.5, + Math.floor(curr / countPerSlice) * perElementSize + margins * 0.5, + ).floor(); + + const maxDimension = perElementPaddedSize - 1; + const box = new THREE.Box3( + new THREE.Vector3(0, 0, 0), + new THREE.Vector3(maxDimension, maxDimension, maxDimension), + ); + const scaleFactor = (Math.random() + 0.5) * 0.5; + const source = generateCloudTexture(perElementPaddedSize, scaleFactor); + + renderer.copyTextureToTexture3D(source, cloudTexture, box, position); + + prevTime = time; + + curr++; + } + + mesh.material.uniforms.cameraPos.value.copy(camera.position); + // mesh.rotation.y = - performance.now() / 7500; + + mesh.material.uniforms.frame.value++; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_tonemapping.ts b/examples-testing/examples/webgl_tonemapping.ts new file mode 100644 index 000000000..08115cf3e --- /dev/null +++ b/examples-testing/examples/webgl_tonemapping.ts @@ -0,0 +1,163 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +let mesh, renderer, scene, camera, controls; +let gui, + guiExposure = null; + +const params = { + exposure: 1.0, + toneMapping: 'AgX', + blurriness: 0.3, + intensity: 1.0, +}; + +const toneMappingOptions = { + None: THREE.NoToneMapping, + Linear: THREE.LinearToneMapping, + Reinhard: THREE.ReinhardToneMapping, + Cineon: THREE.CineonToneMapping, + ACESFilmic: THREE.ACESFilmicToneMapping, + AgX: THREE.AgXToneMapping, + Neutral: THREE.NeutralToneMapping, + Custom: THREE.CustomToneMapping, +}; + +init().catch(function (err) { + console.error(err); +}); + +async function init() { + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + renderer.toneMapping = toneMappingOptions[params.toneMapping]; + renderer.toneMappingExposure = params.exposure; + + // Set CustomToneMapping to Uncharted2 + // source: http://filmicworlds.com/blog/filmic-tonemapping-operators/ + + THREE.ShaderChunk.tonemapping_pars_fragment = THREE.ShaderChunk.tonemapping_pars_fragment.replace( + 'vec3 CustomToneMapping( vec3 color ) { return color; }', + + `#define Uncharted2Helper( x ) max( ( ( x * ( 0.15 * x + 0.10 * 0.50 ) + 0.20 * 0.02 ) / ( x * ( 0.15 * x + 0.50 ) + 0.20 * 0.30 ) ) - 0.02 / 0.30, vec3( 0.0 ) ) + + float toneMappingWhitePoint = 1.0; + + vec3 CustomToneMapping( vec3 color ) { + color *= toneMappingExposure; + return saturate( Uncharted2Helper( color ) / Uncharted2Helper( vec3( toneMappingWhitePoint ) ) ); + + }`, + ); + + scene = new THREE.Scene(); + scene.backgroundBlurriness = params.blurriness; + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); + camera.position.set(-1.8, 0.6, 2.7); + + controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.enableZoom = false; + controls.enablePan = false; + controls.target.set(0, 0, -0.2); + controls.update(); + + const rgbeLoader = new RGBELoader().setPath('textures/equirectangular/'); + + const gltfLoader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); + + const [texture, gltf] = await Promise.all([ + rgbeLoader.loadAsync('venice_sunset_1k.hdr'), + gltfLoader.loadAsync('DamagedHelmet.gltf'), + ]); + + // environment + + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + scene.environment = texture; + + // model + + mesh = gltf.scene.getObjectByName('node_damagedHelmet_-6514'); + scene.add(mesh); + + render(); + + window.addEventListener('resize', onWindowResize); + + gui = new GUI(); + const toneMappingFolder = gui.addFolder('tone mapping'); + + toneMappingFolder + .add(params, 'toneMapping', Object.keys(toneMappingOptions)) + + .onChange(function () { + updateGUI(toneMappingFolder); + + renderer.toneMapping = toneMappingOptions[params.toneMapping]; + render(); + }); + + const backgroundFolder = gui.addFolder('background'); + + backgroundFolder + .add(params, 'blurriness', 0, 1) + + .onChange(function (value) { + scene.backgroundBlurriness = value; + render(); + }); + + backgroundFolder + .add(params, 'intensity', 0, 1) + + .onChange(function (value) { + scene.backgroundIntensity = value; + render(); + }); + + updateGUI(toneMappingFolder); + + gui.open(); +} + +function updateGUI(folder) { + if (guiExposure !== null) { + guiExposure.destroy(); + guiExposure = null; + } + + if (params.toneMapping !== 'None') { + guiExposure = folder + .add(params, 'exposure', 0, 2) + + .onChange(function () { + renderer.toneMappingExposure = params.exposure; + render(); + }); + } +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_ubo.ts b/examples-testing/examples/webgl_ubo.ts new file mode 100644 index 000000000..01064f115 --- /dev/null +++ b/examples-testing/examples/webgl_ubo.ts @@ -0,0 +1,137 @@ +import * as THREE from 'three'; + +let camera, scene, renderer, clock; + +init(); + +function init() { + const container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(0, 0, 25); + + scene = new THREE.Scene(); + camera.lookAt(scene.position); + + clock = new THREE.Clock(); + + // geometry + + const geometry1 = new THREE.TetrahedronGeometry(); + const geometry2 = new THREE.BoxGeometry(); + + // texture + + const texture = new THREE.TextureLoader().load('textures/crate.gif'); + texture.colorSpace = THREE.SRGBColorSpace; + + // uniforms groups + + // Camera and lighting related data are perfect examples of using UBOs since you have to store these + // data just once. They can be shared across all shader programs. + + const cameraUniformsGroup = new THREE.UniformsGroup(); + cameraUniformsGroup.setName('ViewData'); + cameraUniformsGroup.add(new THREE.Uniform(camera.projectionMatrix)); // projection matrix + cameraUniformsGroup.add(new THREE.Uniform(camera.matrixWorldInverse)); // view matrix + + const lightingUniformsGroup = new THREE.UniformsGroup(); + lightingUniformsGroup.setName('LightingData'); + lightingUniformsGroup.add(new THREE.Uniform(new THREE.Vector3(0, 0, 10))); // light position + lightingUniformsGroup.add(new THREE.Uniform(new THREE.Color(0x7c7c7c))); // ambient color + lightingUniformsGroup.add(new THREE.Uniform(new THREE.Color(0xd5d5d5))); // diffuse color + lightingUniformsGroup.add(new THREE.Uniform(new THREE.Color(0xe7e7e7))); // specular color + lightingUniformsGroup.add(new THREE.Uniform(64)); // shininess + + // materials + + const material1 = new THREE.RawShaderMaterial({ + uniforms: { + modelMatrix: { value: null }, + normalMatrix: { value: null }, + color: { value: null }, + }, + vertexShader: document.getElementById('vertexShader1').textContent, + fragmentShader: document.getElementById('fragmentShader1').textContent, + glslVersion: THREE.GLSL3, + }); + + const material2 = new THREE.RawShaderMaterial({ + uniforms: { + modelMatrix: { value: null }, + diffuseMap: { value: null }, + }, + vertexShader: document.getElementById('vertexShader2').textContent, + fragmentShader: document.getElementById('fragmentShader2').textContent, + glslVersion: THREE.GLSL3, + }); + + // meshes + + for (let i = 0; i < 200; i++) { + let mesh; + + if (i % 2 === 0) { + mesh = new THREE.Mesh(geometry1, material1.clone()); + + mesh.material.uniformsGroups = [cameraUniformsGroup, lightingUniformsGroup]; + mesh.material.uniforms.modelMatrix.value = mesh.matrixWorld; + mesh.material.uniforms.normalMatrix.value = mesh.normalMatrix; + mesh.material.uniforms.color.value = new THREE.Color(0xffffff * Math.random()); + } else { + mesh = new THREE.Mesh(geometry2, material2.clone()); + + mesh.material.uniformsGroups = [cameraUniformsGroup, lightingUniformsGroup]; + mesh.material.uniforms.modelMatrix.value = mesh.matrixWorld; + mesh.material.uniforms.diffuseMap.value = texture; + } + + scene.add(mesh); + + const s = 1 + Math.random() * 0.5; + + mesh.scale.x = s; + mesh.scale.y = s; + mesh.scale.z = s; + + mesh.rotation.x = Math.random() * Math.PI; + mesh.rotation.y = Math.random() * Math.PI; + mesh.rotation.z = Math.random() * Math.PI; + + mesh.position.x = Math.random() * 40 - 20; + mesh.position.y = Math.random() * 40 - 20; + mesh.position.z = Math.random() * 20 - 10; + } + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize, false); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const delta = clock.getDelta(); + + scene.traverse(function (child) { + if (child.isMesh) { + child.rotation.x += delta * 0.5; + child.rotation.y += delta * 0.3; + } + }); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_ubo_arrays.ts b/examples-testing/examples/webgl_ubo_arrays.ts new file mode 100644 index 000000000..d846e1443 --- /dev/null +++ b/examples-testing/examples/webgl_ubo_arrays.ts @@ -0,0 +1,171 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, clock, stats; + +let lightingUniformsGroup, lightCenters; + +const container = document.getElementById('container'); + +const pointLightsMax = 300; + +const api = { + count: 200, +}; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(0, 50, 50); + + scene = new THREE.Scene(); + camera.lookAt(scene.position); + + clock = new THREE.Clock(); + + // geometry + + const geometry = new THREE.SphereGeometry(); + + // uniforms groups + + lightingUniformsGroup = new THREE.UniformsGroup(); + lightingUniformsGroup.setName('LightingData'); + + const data = []; + const dataColors = []; + lightCenters = []; + + for (let i = 0; i < pointLightsMax; i++) { + const col = new THREE.Color(0xffffff * Math.random()).toArray(); + const x = Math.random() * 50 - 25; + const z = Math.random() * 50 - 25; + + data.push(new THREE.Uniform(new THREE.Vector4(x, 1, z, 0))); // light position + dataColors.push(new THREE.Uniform(new THREE.Vector4(col[0], col[1], col[2], 0))); // light color + + // Store the center positions + lightCenters.push({ x, z }); + } + + lightingUniformsGroup.add(data); // light position + lightingUniformsGroup.add(dataColors); // light position + lightingUniformsGroup.add(new THREE.Uniform(pointLightsMax)); // light position + + const cameraUniformsGroup = new THREE.UniformsGroup(); + cameraUniformsGroup.setName('ViewData'); + cameraUniformsGroup.add(new THREE.Uniform(camera.projectionMatrix)); // projection matrix + cameraUniformsGroup.add(new THREE.Uniform(camera.matrixWorldInverse)); // view matrix + + const material = new THREE.RawShaderMaterial({ + uniforms: { + modelMatrix: { value: null }, + normalMatrix: { value: null }, + }, + // uniformsGroups: [ cameraUniformsGroup, lightingUniformsGroup ], + name: 'Box', + defines: { + POINTLIGHTS_MAX: pointLightsMax, + }, + vertexShader: document.getElementById('vertexShader').textContent, + fragmentShader: document.getElementById('fragmentShader').textContent, + glslVersion: THREE.GLSL3, + }); + + const plane = new THREE.Mesh(new THREE.PlaneGeometry(100, 100), material.clone()); + plane.material.uniformsGroups = [cameraUniformsGroup, lightingUniformsGroup]; + plane.material.uniforms.modelMatrix.value = plane.matrixWorld; + plane.material.uniforms.normalMatrix.value = plane.normalMatrix; + plane.rotation.x = -Math.PI / 2; + plane.position.y = -1; + scene.add(plane); + + // meshes + const gridSize = { x: 10, y: 1, z: 10 }; + const spacing = 6; + + for (let i = 0; i < gridSize.x; i++) { + for (let j = 0; j < gridSize.y; j++) { + for (let k = 0; k < gridSize.z; k++) { + const mesh = new THREE.Mesh(geometry, material.clone()); + mesh.name = 'Sphere'; + mesh.material.uniformsGroups = [cameraUniformsGroup, lightingUniformsGroup]; + mesh.material.uniforms.modelMatrix.value = mesh.matrixWorld; + mesh.material.uniforms.normalMatrix.value = mesh.normalMatrix; + scene.add(mesh); + + mesh.position.x = i * spacing - (gridSize.x * spacing) / 2; + mesh.position.y = 0; + mesh.position.z = k * spacing - (gridSize.z * spacing) / 2; + } + } + } + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize, false); + + // controls + + const controls = new OrbitControls(camera, renderer.domElement); + controls.enablePan = false; + + // stats + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // gui + const gui = new GUI(); + gui.add(api, 'count', 1, pointLightsMax) + .step(1) + .onChange(function () { + lightingUniformsGroup.uniforms[2].value = api.count; + }); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const elapsedTime = clock.getElapsedTime(); + + const lights = lightingUniformsGroup.uniforms[0]; + + // Parameters for circular movement + const radius = 5; // Smaller radius for individual circular movements + const speed = 0.5; // Speed of rotation + + // Update each light's position + for (let i = 0; i < lights.length; i++) { + const light = lights[i]; + const center = lightCenters[i]; + + // Calculate circular movement around the light's center + const angle = speed * elapsedTime + i * 0.5; // Phase difference for each light + const x = center.x + Math.sin(angle) * radius; + const z = center.z + Math.cos(angle) * radius; + + // Update the light's position + light.value.set(x, 1, z, 0); + } + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgl_video_kinect.ts b/examples-testing/examples/webgl_video_kinect.ts new file mode 100644 index 000000000..4f0e2f113 --- /dev/null +++ b/examples-testing/examples/webgl_video_kinect.ts @@ -0,0 +1,113 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let scene, camera, renderer; +let geometry, mesh, material; +let mouse, center; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + const info = document.createElement('div'); + info.id = 'info'; + info.innerHTML = 'three.js - kinect'; + document.body.appendChild(info); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.set(0, 0, 500); + + scene = new THREE.Scene(); + center = new THREE.Vector3(); + center.z = -1000; + + const video = document.getElementById('video'); + + const texture = new THREE.VideoTexture(video); + texture.minFilter = THREE.NearestFilter; + + const width = 640, + height = 480; + const nearClipping = 850, + farClipping = 4000; + + geometry = new THREE.BufferGeometry(); + + const vertices = new Float32Array(width * height * 3); + + for (let i = 0, j = 0, l = vertices.length; i < l; i += 3, j++) { + vertices[i] = j % width; + vertices[i + 1] = Math.floor(j / width); + } + + geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); + + material = new THREE.ShaderMaterial({ + uniforms: { + map: { value: texture }, + width: { value: width }, + height: { value: height }, + nearClipping: { value: nearClipping }, + farClipping: { value: farClipping }, + + pointSize: { value: 2 }, + zOffset: { value: 1000 }, + }, + vertexShader: document.getElementById('vs').textContent, + fragmentShader: document.getElementById('fs').textContent, + blending: THREE.AdditiveBlending, + depthTest: false, + depthWrite: false, + transparent: true, + }); + + mesh = new THREE.Points(geometry, material); + scene.add(mesh); + + const gui = new GUI(); + gui.add(material.uniforms.nearClipping, 'value', 1, 10000, 1.0).name('nearClipping'); + gui.add(material.uniforms.farClipping, 'value', 1, 10000, 1.0).name('farClipping'); + gui.add(material.uniforms.pointSize, 'value', 1, 10, 1.0).name('pointSize'); + gui.add(material.uniforms.zOffset, 'value', 0, 4000, 1.0).name('zOffset'); + + video.play(); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + mouse = new THREE.Vector3(0, 0, 1); + + document.addEventListener('mousemove', onDocumentMouseMove); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onDocumentMouseMove(event) { + mouse.x = (event.clientX - window.innerWidth / 2) * 8; + mouse.y = (event.clientY - window.innerHeight / 2) * 8; +} + +function animate() { + camera.position.x += (mouse.x - camera.position.x) * 0.05; + camera.position.y += (-mouse.y - camera.position.y) * 0.05; + camera.lookAt(center); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_video_panorama_equirectangular.ts b/examples-testing/examples/webgl_video_panorama_equirectangular.ts new file mode 100644 index 000000000..866eca16a --- /dev/null +++ b/examples-testing/examples/webgl_video_panorama_equirectangular.ts @@ -0,0 +1,95 @@ +import * as THREE from 'three'; + +let camera, scene, renderer; + +let isUserInteracting = false, + lon = 0, + lat = 0, + phi = 0, + theta = 0, + onPointerDownPointerX = 0, + onPointerDownPointerY = 0, + onPointerDownLon = 0, + onPointerDownLat = 0; + +const distance = 0.5; + +init(); + +function init() { + const container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.25, 10); + + scene = new THREE.Scene(); + + const geometry = new THREE.SphereGeometry(5, 60, 40); + // invert the geometry on the x-axis so that all of the faces point inward + geometry.scale(-1, 1, 1); + + const video = document.getElementById('video'); + video.play(); + + const texture = new THREE.VideoTexture(video); + texture.colorSpace = THREE.SRGBColorSpace; + const material = new THREE.MeshBasicMaterial({ map: texture }); + + const mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + document.addEventListener('pointerdown', onPointerDown); + document.addEventListener('pointermove', onPointerMove); + document.addEventListener('pointerup', onPointerUp); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onPointerDown(event) { + isUserInteracting = true; + + onPointerDownPointerX = event.clientX; + onPointerDownPointerY = event.clientY; + + onPointerDownLon = lon; + onPointerDownLat = lat; +} + +function onPointerMove(event) { + if (isUserInteracting === true) { + lon = (onPointerDownPointerX - event.clientX) * 0.1 + onPointerDownLon; + lat = (onPointerDownPointerY - event.clientY) * 0.1 + onPointerDownLat; + } +} + +function onPointerUp() { + isUserInteracting = false; +} + +function animate() { + lat = Math.max(-85, Math.min(85, lat)); + phi = THREE.MathUtils.degToRad(90 - lat); + theta = THREE.MathUtils.degToRad(lon); + + camera.position.x = distance * Math.sin(phi) * Math.cos(theta); + camera.position.y = distance * Math.cos(phi); + camera.position.z = distance * Math.sin(phi) * Math.sin(theta); + + camera.lookAt(0, 0, 0); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_volume_cloud.ts b/examples-testing/examples/webgl_volume_cloud.ts new file mode 100644 index 000000000..77dd8de43 --- /dev/null +++ b/examples-testing/examples/webgl_volume_cloud.ts @@ -0,0 +1,279 @@ +import * as THREE from 'three'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let renderer, scene, camera; +let mesh; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(0, 0, 1.5); + + new OrbitControls(camera, renderer.domElement); + + // Sky + + const canvas = document.createElement('canvas'); + canvas.width = 1; + canvas.height = 32; + + const context = canvas.getContext('2d'); + const gradient = context.createLinearGradient(0, 0, 0, 32); + gradient.addColorStop(0.0, '#014a84'); + gradient.addColorStop(0.5, '#0561a0'); + gradient.addColorStop(1.0, '#437ab6'); + context.fillStyle = gradient; + context.fillRect(0, 0, 1, 32); + + const skyMap = new THREE.CanvasTexture(canvas); + skyMap.colorSpace = THREE.SRGBColorSpace; + + const sky = new THREE.Mesh( + new THREE.SphereGeometry(10), + new THREE.MeshBasicMaterial({ map: skyMap, side: THREE.BackSide }), + ); + scene.add(sky); + + // Texture + + const size = 128; + const data = new Uint8Array(size * size * size); + + let i = 0; + const scale = 0.05; + const perlin = new ImprovedNoise(); + const vector = new THREE.Vector3(); + + for (let z = 0; z < size; z++) { + for (let y = 0; y < size; y++) { + for (let x = 0; x < size; x++) { + const d = + 1.0 - + vector + .set(x, y, z) + .subScalar(size / 2) + .divideScalar(size) + .length(); + data[i] = (128 + 128 * perlin.noise((x * scale) / 1.5, y * scale, (z * scale) / 1.5)) * d * d; + i++; + } + } + } + + const texture = new THREE.Data3DTexture(data, size, size, size); + texture.format = THREE.RedFormat; + texture.minFilter = THREE.LinearFilter; + texture.magFilter = THREE.LinearFilter; + texture.unpackAlignment = 1; + texture.needsUpdate = true; + + // Material + + const vertexShader = /* glsl */ ` + in vec3 position; + + uniform mat4 modelMatrix; + uniform mat4 modelViewMatrix; + uniform mat4 projectionMatrix; + uniform vec3 cameraPos; + + out vec3 vOrigin; + out vec3 vDirection; + + void main() { + vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); + + vOrigin = vec3( inverse( modelMatrix ) * vec4( cameraPos, 1.0 ) ).xyz; + vDirection = position - vOrigin; + + gl_Position = projectionMatrix * mvPosition; + } + `; + + const fragmentShader = /* glsl */ ` + precision highp float; + precision highp sampler3D; + + uniform mat4 modelViewMatrix; + uniform mat4 projectionMatrix; + + in vec3 vOrigin; + in vec3 vDirection; + + out vec4 color; + + uniform vec3 base; + uniform sampler3D map; + + uniform float threshold; + uniform float range; + uniform float opacity; + uniform float steps; + uniform float frame; + + uint wang_hash(uint seed) + { + seed = (seed ^ 61u) ^ (seed >> 16u); + seed *= 9u; + seed = seed ^ (seed >> 4u); + seed *= 0x27d4eb2du; + seed = seed ^ (seed >> 15u); + return seed; + } + + float randomFloat(inout uint seed) + { + return float(wang_hash(seed)) / 4294967296.; + } + + vec2 hitBox( vec3 orig, vec3 dir ) { + const vec3 box_min = vec3( - 0.5 ); + const vec3 box_max = vec3( 0.5 ); + vec3 inv_dir = 1.0 / dir; + vec3 tmin_tmp = ( box_min - orig ) * inv_dir; + vec3 tmax_tmp = ( box_max - orig ) * inv_dir; + vec3 tmin = min( tmin_tmp, tmax_tmp ); + vec3 tmax = max( tmin_tmp, tmax_tmp ); + float t0 = max( tmin.x, max( tmin.y, tmin.z ) ); + float t1 = min( tmax.x, min( tmax.y, tmax.z ) ); + return vec2( t0, t1 ); + } + + float sample1( vec3 p ) { + return texture( map, p ).r; + } + + float shading( vec3 coord ) { + float step = 0.01; + return sample1( coord + vec3( - step ) ) - sample1( coord + vec3( step ) ); + } + + vec4 linearToSRGB( in vec4 value ) { + return vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) * 1.055 - vec3( 0.055 ), value.rgb * 12.92, vec3( lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ) ), value.a ); + } + + void main(){ + vec3 rayDir = normalize( vDirection ); + vec2 bounds = hitBox( vOrigin, rayDir ); + + if ( bounds.x > bounds.y ) discard; + + bounds.x = max( bounds.x, 0.0 ); + + vec3 p = vOrigin + bounds.x * rayDir; + vec3 inc = 1.0 / abs( rayDir ); + float delta = min( inc.x, min( inc.y, inc.z ) ); + delta /= steps; + + // Jitter + + // Nice little seed from + // https://blog.demofox.org/2020/05/25/casual-shadertoy-path-tracing-1-basic-camera-diffuse-emissive/ + uint seed = uint( gl_FragCoord.x ) * uint( 1973 ) + uint( gl_FragCoord.y ) * uint( 9277 ) + uint( frame ) * uint( 26699 ); + vec3 size = vec3( textureSize( map, 0 ) ); + float randNum = randomFloat( seed ) * 2.0 - 1.0; + p += rayDir * randNum * ( 1.0 / size ); + + // + + vec4 ac = vec4( base, 0.0 ); + + for ( float t = bounds.x; t < bounds.y; t += delta ) { + + float d = sample1( p + 0.5 ); + + d = smoothstep( threshold - range, threshold + range, d ) * opacity; + + float col = shading( p + 0.5 ) * 3.0 + ( ( p.x + p.y ) * 0.25 ) + 0.2; + + ac.rgb += ( 1.0 - ac.a ) * d * col; + + ac.a += ( 1.0 - ac.a ) * d; + + if ( ac.a >= 0.95 ) break; + + p += rayDir * delta; + + } + + color = linearToSRGB( ac ); + + if ( color.a == 0.0 ) discard; + + } + `; + + const geometry = new THREE.BoxGeometry(1, 1, 1); + const material = new THREE.RawShaderMaterial({ + glslVersion: THREE.GLSL3, + uniforms: { + base: { value: new THREE.Color(0x798aa0) }, + map: { value: texture }, + cameraPos: { value: new THREE.Vector3() }, + threshold: { value: 0.25 }, + opacity: { value: 0.25 }, + range: { value: 0.1 }, + steps: { value: 100 }, + frame: { value: 0 }, + }, + vertexShader, + fragmentShader, + side: THREE.BackSide, + transparent: true, + }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + const parameters = { + threshold: 0.25, + opacity: 0.25, + range: 0.1, + steps: 100, + }; + + function update() { + material.uniforms.threshold.value = parameters.threshold; + material.uniforms.opacity.value = parameters.opacity; + material.uniforms.range.value = parameters.range; + material.uniforms.steps.value = parameters.steps; + } + + const gui = new GUI(); + gui.add(parameters, 'threshold', 0, 1, 0.01).onChange(update); + gui.add(parameters, 'opacity', 0, 1, 0.01).onChange(update); + gui.add(parameters, 'range', 0, 1, 0.01).onChange(update); + gui.add(parameters, 'steps', 0, 200, 1).onChange(update); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + mesh.material.uniforms.cameraPos.value.copy(camera.position); + mesh.rotation.y = -performance.now() / 7500; + + mesh.material.uniforms.frame.value++; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_volume_instancing.ts b/examples-testing/examples/webgl_volume_instancing.ts new file mode 100644 index 000000000..bf90eeea9 --- /dev/null +++ b/examples-testing/examples/webgl_volume_instancing.ts @@ -0,0 +1,192 @@ +import * as THREE from 'three'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { VOXLoader, VOXData3DTexture } from 'three/addons/loaders/VOXLoader.js'; + +let renderer, scene, camera, controls, clock; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000); + camera.position.set(0, 0, 4); + + controls = new OrbitControls(camera, renderer.domElement); + controls.autoRotate = true; + controls.autoRotateSpeed = -1.0; + controls.enableDamping = true; + + clock = new THREE.Clock(); + + // Material + + const vertexShader = /* glsl */ ` + in vec3 position; + in mat4 instanceMatrix; + + uniform mat4 modelMatrix; + uniform mat4 modelViewMatrix; + uniform mat4 projectionMatrix; + uniform vec3 cameraPos; + + out vec3 vOrigin; + out vec3 vDirection; + + void main() { + vec4 mvPosition = modelViewMatrix * instanceMatrix * vec4( position, 1.0 ); + + vOrigin = vec3( inverse( instanceMatrix * modelMatrix ) * vec4( cameraPos, 1.0 ) ).xyz; + vDirection = position - vOrigin; + + gl_Position = projectionMatrix * mvPosition; + } + `; + + const fragmentShader = /* glsl */ ` + precision highp float; + precision highp sampler3D; + + uniform mat4 modelViewMatrix; + uniform mat4 projectionMatrix; + + in vec3 vOrigin; + in vec3 vDirection; + + out vec4 color; + + uniform sampler3D map; + + uniform float threshold; + uniform float steps; + + vec2 hitBox( vec3 orig, vec3 dir ) { + const vec3 box_min = vec3( - 0.5 ); + const vec3 box_max = vec3( 0.5 ); + vec3 inv_dir = 1.0 / dir; + vec3 tmin_tmp = ( box_min - orig ) * inv_dir; + vec3 tmax_tmp = ( box_max - orig ) * inv_dir; + vec3 tmin = min( tmin_tmp, tmax_tmp ); + vec3 tmax = max( tmin_tmp, tmax_tmp ); + float t0 = max( tmin.x, max( tmin.y, tmin.z ) ); + float t1 = min( tmax.x, min( tmax.y, tmax.z ) ); + return vec2( t0, t1 ); + } + + float sample1( vec3 p ) { + return texture( map, p ).r; + } + + #define epsilon .0001 + + vec3 normal( vec3 coord ) { + if ( coord.x < epsilon ) return vec3( 1.0, 0.0, 0.0 ); + if ( coord.y < epsilon ) return vec3( 0.0, 1.0, 0.0 ); + if ( coord.z < epsilon ) return vec3( 0.0, 0.0, 1.0 ); + if ( coord.x > 1.0 - epsilon ) return vec3( - 1.0, 0.0, 0.0 ); + if ( coord.y > 1.0 - epsilon ) return vec3( 0.0, - 1.0, 0.0 ); + if ( coord.z > 1.0 - epsilon ) return vec3( 0.0, 0.0, - 1.0 ); + + float step = 0.01; + float x = sample1( coord + vec3( - step, 0.0, 0.0 ) ) - sample1( coord + vec3( step, 0.0, 0.0 ) ); + float y = sample1( coord + vec3( 0.0, - step, 0.0 ) ) - sample1( coord + vec3( 0.0, step, 0.0 ) ); + float z = sample1( coord + vec3( 0.0, 0.0, - step ) ) - sample1( coord + vec3( 0.0, 0.0, step ) ); + + return normalize( vec3( x, y, z ) ); + } + + void main(){ + + vec3 rayDir = normalize( vDirection ); + vec2 bounds = hitBox( vOrigin, rayDir ); + + if ( bounds.x > bounds.y ) discard; + + bounds.x = max( bounds.x, 0.0 ); + + vec3 p = vOrigin + bounds.x * rayDir; + vec3 inc = 1.0 / abs( rayDir ); + float delta = min( inc.x, min( inc.y, inc.z ) ); + delta /= 50.0; + + for ( float t = bounds.x; t < bounds.y; t += delta ) { + + float d = sample1( p + 0.5 ); + + if ( d > 0.5 ) { + + color.rgb = p * 2.0; // normal( p + 0.5 ); // * 0.5 + ( p * 1.5 + 0.25 ); + color.a = 1.; + break; + + } + + p += rayDir * delta; + + } + + if ( color.a == 0.0 ) discard; + + } + `; + + const loader = new VOXLoader(); + loader.load('models/vox/menger.vox', function (chunks) { + for (let i = 0; i < chunks.length; i++) { + const chunk = chunks[i]; + + const geometry = new THREE.BoxGeometry(1, 1, 1); + const material = new THREE.RawShaderMaterial({ + glslVersion: THREE.GLSL3, + uniforms: { + map: { value: new VOXData3DTexture(chunk) }, + cameraPos: { value: new THREE.Vector3() }, + }, + vertexShader, + fragmentShader, + side: THREE.BackSide, + }); + + const mesh = new THREE.InstancedMesh(geometry, material, 50000); + mesh.onBeforeRender = function () { + this.material.uniforms.cameraPos.value.copy(camera.position); + }; + + const transform = new THREE.Object3D(); + + for (let i = 0; i < mesh.count; i++) { + transform.position.random().subScalar(0.5).multiplyScalar(150); + transform.rotation.x = Math.random() * Math.PI; + transform.rotation.y = Math.random() * Math.PI; + transform.rotation.z = Math.random() * Math.PI; + transform.updateMatrix(); + + mesh.setMatrixAt(i, transform.matrix); + } + + scene.add(mesh); + } + }); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const delta = clock.getDelta(); + controls.update(delta); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_volume_perlin.ts b/examples-testing/examples/webgl_volume_perlin.ts new file mode 100644 index 000000000..a98f9a682 --- /dev/null +++ b/examples-testing/examples/webgl_volume_perlin.ts @@ -0,0 +1,208 @@ +import * as THREE from 'three'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let renderer, scene, camera; +let mesh; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(0, 0, 2); + + new OrbitControls(camera, renderer.domElement); + + // Texture + + const size = 128; + const data = new Uint8Array(size * size * size); + + let i = 0; + const perlin = new ImprovedNoise(); + const vector = new THREE.Vector3(); + + for (let z = 0; z < size; z++) { + for (let y = 0; y < size; y++) { + for (let x = 0; x < size; x++) { + vector.set(x, y, z).divideScalar(size); + + const d = perlin.noise(vector.x * 6.5, vector.y * 6.5, vector.z * 6.5); + + data[i++] = d * 128 + 128; + } + } + } + + const texture = new THREE.Data3DTexture(data, size, size, size); + texture.format = THREE.RedFormat; + texture.minFilter = THREE.LinearFilter; + texture.magFilter = THREE.LinearFilter; + texture.unpackAlignment = 1; + texture.needsUpdate = true; + + // Material + + const vertexShader = /* glsl */ ` + in vec3 position; + + uniform mat4 modelMatrix; + uniform mat4 modelViewMatrix; + uniform mat4 projectionMatrix; + uniform vec3 cameraPos; + + out vec3 vOrigin; + out vec3 vDirection; + + void main() { + vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); + + vOrigin = vec3( inverse( modelMatrix ) * vec4( cameraPos, 1.0 ) ).xyz; + vDirection = position - vOrigin; + + gl_Position = projectionMatrix * mvPosition; + } + `; + + const fragmentShader = /* glsl */ ` + precision highp float; + precision highp sampler3D; + + uniform mat4 modelViewMatrix; + uniform mat4 projectionMatrix; + + in vec3 vOrigin; + in vec3 vDirection; + + out vec4 color; + + uniform sampler3D map; + + uniform float threshold; + uniform float steps; + + vec2 hitBox( vec3 orig, vec3 dir ) { + const vec3 box_min = vec3( - 0.5 ); + const vec3 box_max = vec3( 0.5 ); + vec3 inv_dir = 1.0 / dir; + vec3 tmin_tmp = ( box_min - orig ) * inv_dir; + vec3 tmax_tmp = ( box_max - orig ) * inv_dir; + vec3 tmin = min( tmin_tmp, tmax_tmp ); + vec3 tmax = max( tmin_tmp, tmax_tmp ); + float t0 = max( tmin.x, max( tmin.y, tmin.z ) ); + float t1 = min( tmax.x, min( tmax.y, tmax.z ) ); + return vec2( t0, t1 ); + } + + float sample1( vec3 p ) { + return texture( map, p ).r; + } + + #define epsilon .0001 + + vec3 normal( vec3 coord ) { + if ( coord.x < epsilon ) return vec3( 1.0, 0.0, 0.0 ); + if ( coord.y < epsilon ) return vec3( 0.0, 1.0, 0.0 ); + if ( coord.z < epsilon ) return vec3( 0.0, 0.0, 1.0 ); + if ( coord.x > 1.0 - epsilon ) return vec3( - 1.0, 0.0, 0.0 ); + if ( coord.y > 1.0 - epsilon ) return vec3( 0.0, - 1.0, 0.0 ); + if ( coord.z > 1.0 - epsilon ) return vec3( 0.0, 0.0, - 1.0 ); + + float step = 0.01; + float x = sample1( coord + vec3( - step, 0.0, 0.0 ) ) - sample1( coord + vec3( step, 0.0, 0.0 ) ); + float y = sample1( coord + vec3( 0.0, - step, 0.0 ) ) - sample1( coord + vec3( 0.0, step, 0.0 ) ); + float z = sample1( coord + vec3( 0.0, 0.0, - step ) ) - sample1( coord + vec3( 0.0, 0.0, step ) ); + + return normalize( vec3( x, y, z ) ); + } + + void main(){ + + vec3 rayDir = normalize( vDirection ); + vec2 bounds = hitBox( vOrigin, rayDir ); + + if ( bounds.x > bounds.y ) discard; + + bounds.x = max( bounds.x, 0.0 ); + + vec3 p = vOrigin + bounds.x * rayDir; + vec3 inc = 1.0 / abs( rayDir ); + float delta = min( inc.x, min( inc.y, inc.z ) ); + delta /= steps; + + for ( float t = bounds.x; t < bounds.y; t += delta ) { + + float d = sample1( p + 0.5 ); + + if ( d > threshold ) { + + color.rgb = normal( p + 0.5 ) * 0.5 + ( p * 1.5 + 0.25 ); + color.a = 1.; + break; + + } + + p += rayDir * delta; + + } + + if ( color.a == 0.0 ) discard; + + } + `; + + const geometry = new THREE.BoxGeometry(1, 1, 1); + const material = new THREE.RawShaderMaterial({ + glslVersion: THREE.GLSL3, + uniforms: { + map: { value: texture }, + cameraPos: { value: new THREE.Vector3() }, + threshold: { value: 0.6 }, + steps: { value: 200 }, + }, + vertexShader, + fragmentShader, + side: THREE.BackSide, + }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + const parameters = { threshold: 0.6, steps: 200 }; + + function update() { + material.uniforms.threshold.value = parameters.threshold; + material.uniforms.steps.value = parameters.steps; + } + + const gui = new GUI(); + gui.add(parameters, 'threshold', 0, 1, 0.01).onChange(update); + gui.add(parameters, 'steps', 0, 300, 1).onChange(update); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + mesh.material.uniforms.cameraPos.value.copy(camera.position); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_water.ts b/examples-testing/examples/webgl_water.ts new file mode 100644 index 000000000..496a5f855 --- /dev/null +++ b/examples-testing/examples/webgl_water.ts @@ -0,0 +1,162 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { Water } from 'three/addons/objects/Water2.js'; + +let scene, camera, clock, renderer, water; + +let torusKnot; + +const params = { + color: '#ffffff', + scale: 4, + flowX: 1, + flowY: 1, +}; + +init(); + +function init() { + // scene + + scene = new THREE.Scene(); + + // camera + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200); + camera.position.set(-15, 7, 15); + camera.lookAt(scene.position); + + // clock + + clock = new THREE.Clock(); + + // mesh + + const torusKnotGeometry = new THREE.TorusKnotGeometry(3, 1, 256, 32); + const torusKnotMaterial = new THREE.MeshNormalMaterial(); + + torusKnot = new THREE.Mesh(torusKnotGeometry, torusKnotMaterial); + torusKnot.position.y = 4; + torusKnot.scale.set(0.5, 0.5, 0.5); + scene.add(torusKnot); + + // ground + + const groundGeometry = new THREE.PlaneGeometry(20, 20); + const groundMaterial = new THREE.MeshStandardMaterial({ roughness: 0.8, metalness: 0.4 }); + const ground = new THREE.Mesh(groundGeometry, groundMaterial); + ground.rotation.x = Math.PI * -0.5; + scene.add(ground); + + const textureLoader = new THREE.TextureLoader(); + textureLoader.load('textures/hardwood2_diffuse.jpg', function (map) { + map.wrapS = THREE.RepeatWrapping; + map.wrapT = THREE.RepeatWrapping; + map.anisotropy = 16; + map.repeat.set(4, 4); + map.colorSpace = THREE.SRGBColorSpace; + groundMaterial.map = map; + groundMaterial.needsUpdate = true; + }); + + // water + + const waterGeometry = new THREE.PlaneGeometry(20, 20); + + water = new Water(waterGeometry, { + color: params.color, + scale: params.scale, + flowDirection: new THREE.Vector2(params.flowX, params.flowY), + textureWidth: 1024, + textureHeight: 1024, + }); + + water.position.y = 1; + water.rotation.x = Math.PI * -0.5; + scene.add(water); + + // skybox + + const cubeTextureLoader = new THREE.CubeTextureLoader(); + cubeTextureLoader.setPath('textures/cube/Park2/'); + + const cubeTexture = cubeTextureLoader.load([ + 'posx.jpg', + 'negx.jpg', + 'posy.jpg', + 'negy.jpg', + 'posz.jpg', + 'negz.jpg', + ]); + + scene.background = cubeTexture; + + // light + + const ambientLight = new THREE.AmbientLight(0xe7e7e7, 1.2); + scene.add(ambientLight); + + const directionalLight = new THREE.DirectionalLight(0xffffff, 2); + directionalLight.position.set(-1, 1, 1); + scene.add(directionalLight); + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // gui + + const gui = new GUI(); + + gui.addColor(params, 'color').onChange(function (value) { + water.material.uniforms['color'].value.set(value); + }); + gui.add(params, 'scale', 1, 10).onChange(function (value) { + water.material.uniforms['config'].value.w = value; + }); + gui.add(params, 'flowX', -1, 1) + .step(0.01) + .onChange(function (value) { + water.material.uniforms['flowDirection'].value.x = value; + water.material.uniforms['flowDirection'].value.normalize(); + }); + gui.add(params, 'flowY', -1, 1) + .step(0.01) + .onChange(function (value) { + water.material.uniforms['flowDirection'].value.y = value; + water.material.uniforms['flowDirection'].value.normalize(); + }); + + gui.open(); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 5; + controls.maxDistance = 50; + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const delta = clock.getDelta(); + + torusKnot.rotation.x += delta; + torusKnot.rotation.y += delta * 0.5; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgl_water_flowmap.ts b/examples-testing/examples/webgl_water_flowmap.ts new file mode 100644 index 000000000..d0255e431 --- /dev/null +++ b/examples-testing/examples/webgl_water_flowmap.ts @@ -0,0 +1,100 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { Water } from 'three/addons/objects/Water2.js'; + +let scene, camera, renderer, water; + +init(); + +function init() { + // scene + + scene = new THREE.Scene(); + + // camera + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200); + camera.position.set(0, 25, 0); + camera.lookAt(scene.position); + + // ground + + const groundGeometry = new THREE.PlaneGeometry(20, 20, 10, 10); + const groundMaterial = new THREE.MeshBasicMaterial({ color: 0xe7e7e7 }); + const ground = new THREE.Mesh(groundGeometry, groundMaterial); + ground.rotation.x = Math.PI * -0.5; + scene.add(ground); + + const textureLoader = new THREE.TextureLoader(); + textureLoader.load('textures/floors/FloorsCheckerboard_S_Diffuse.jpg', function (map) { + map.wrapS = THREE.RepeatWrapping; + map.wrapT = THREE.RepeatWrapping; + map.anisotropy = 16; + map.repeat.set(4, 4); + map.colorSpace = THREE.SRGBColorSpace; + groundMaterial.map = map; + groundMaterial.needsUpdate = true; + }); + + // water + + const waterGeometry = new THREE.PlaneGeometry(20, 20); + const flowMap = textureLoader.load('textures/water/Water_1_M_Flow.jpg'); + + water = new Water(waterGeometry, { + scale: 2, + textureWidth: 1024, + textureHeight: 1024, + flowMap: flowMap, + }); + + water.position.y = 1; + water.rotation.x = Math.PI * -0.5; + scene.add(water); + + // flow map helper + + const helperGeometry = new THREE.PlaneGeometry(20, 20); + const helperMaterial = new THREE.MeshBasicMaterial({ map: flowMap }); + const helper = new THREE.Mesh(helperGeometry, helperMaterial); + helper.position.y = 1.01; + helper.rotation.x = Math.PI * -0.5; + helper.visible = false; + scene.add(helper); + + // renderer + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + const gui = new GUI(); + gui.add(helper, 'visible').name('Show Flow Map'); + gui.open(); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 5; + controls.maxDistance = 50; + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_backdrop_area.ts b/examples-testing/examples/webgpu_backdrop_area.ts new file mode 100644 index 000000000..208bb15ee --- /dev/null +++ b/examples-testing/examples/webgpu_backdrop_area.ts @@ -0,0 +1,162 @@ +import * as THREE from 'three'; +import { + color, + linearDepth, + viewportLinearDepth, + viewportSharedTexture, + viewportMipTexture, + viewportTopLeft, + checker, + uv, + modelScale, +} from 'three/tsl'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer; +let mixer, clock; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.25, 25); + camera.position.set(3, 2, 3); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x777777); + camera.lookAt(0, 1, 0); + + clock = new THREE.Clock(); + + const light = new THREE.PointLight(0xffffff, 50); + camera.add(light); + scene.add(camera); + + const ambient = new THREE.AmbientLight(0x4466ff, 1); + scene.add(ambient); + + // model + + const loader = new GLTFLoader(); + loader.load('models/gltf/Michelle.glb', function (gltf) { + const object = gltf.scene; + mixer = new THREE.AnimationMixer(object); + + const action = mixer.clipAction(gltf.animations[0]); + action.play(); + + scene.add(object); + }); + + // volume + + // compare depth from viewportLinearDepth with linearDepth() to create a distance field + // viewportLinearDepth return the linear depth of the scene + // linearDepth() returns the linear depth of the mesh + const depthDistance = viewportLinearDepth.distance(linearDepth()); + + const depthAlphaNode = depthDistance.oneMinus().smoothstep(0.9, 2).mul(10).saturate(); + const depthBlurred = viewportMipTexture().bicubic( + depthDistance + .smoothstep(0, 0.6) + .mul(40 * 5) + .clamp(0, 5), + ); + + const blurredBlur = new THREE.MeshBasicNodeMaterial(); + blurredBlur.backdropNode = depthBlurred.add(depthAlphaNode.mix(color(0x0066ff), 0)); + blurredBlur.transparent = true; + blurredBlur.side = THREE.DoubleSide; + + const volumeMaterial = new THREE.MeshBasicNodeMaterial(); + volumeMaterial.colorNode = color(0x0066ff); + volumeMaterial.backdropNode = viewportSharedTexture(); + volumeMaterial.backdropAlphaNode = depthAlphaNode; + volumeMaterial.transparent = true; + volumeMaterial.side = THREE.DoubleSide; + + const depthMaterial = new THREE.MeshBasicNodeMaterial(); + depthMaterial.backdropNode = depthAlphaNode; + depthMaterial.transparent = true; + depthMaterial.side = THREE.DoubleSide; + + const bicubicMaterial = new THREE.MeshBasicNodeMaterial(); + bicubicMaterial.backdropNode = viewportMipTexture().bicubic(5); // @TODO: Move to alpha value [ 0, 1 ] + bicubicMaterial.backdropAlphaNode = checker(uv().mul(3).mul(modelScale.xy)); + bicubicMaterial.opacityNode = bicubicMaterial.backdropAlphaNode; + bicubicMaterial.transparent = true; + bicubicMaterial.side = THREE.DoubleSide; + + const pixelMaterial = new THREE.MeshBasicNodeMaterial(); + pixelMaterial.backdropNode = viewportSharedTexture(viewportTopLeft.mul(100).floor().div(100)); + pixelMaterial.transparent = true; + + // box / floor + + const box = new THREE.Mesh(new THREE.BoxGeometry(2, 2, 2), volumeMaterial); + box.position.set(0, 1, 0); + scene.add(box); + + const floor = new THREE.Mesh( + new THREE.BoxGeometry(1.99, 0.01, 1.99), + new THREE.MeshBasicNodeMaterial({ color: 0x333333 }), + ); + floor.position.set(0, 0, 0); + scene.add(floor); + + // renderer + + renderer = new THREE.WebGPURenderer(/*{ antialias: true }*/); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.LinearToneMapping; + renderer.toneMappingExposure = 0.2; + document.body.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 1, 0); + controls.update(); + + window.addEventListener('resize', onWindowResize); + + // gui + + const materials = { + blurred: blurredBlur, + volume: volumeMaterial, + depth: depthMaterial, + bicubic: bicubicMaterial, + pixel: pixelMaterial, + }; + + const gui = new GUI(); + const options = { material: 'blurred' }; + + box.material = materials[options.material]; + + gui.add(box.scale, 'x', 0.1, 2, 0.01); + gui.add(box.scale, 'z', 0.1, 2, 0.01); + gui.add(options, 'material', Object.keys(materials)).onChange(name => { + box.material = materials[name]; + }); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const delta = clock.getDelta(); + + if (mixer) mixer.update(delta); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_camera_logarithmicdepthbuffer.ts b/examples-testing/examples/webgpu_camera_logarithmicdepthbuffer.ts new file mode 100644 index 000000000..155276322 --- /dev/null +++ b/examples-testing/examples/webgpu_camera_logarithmicdepthbuffer.ts @@ -0,0 +1,245 @@ +import * as THREE from 'three'; + +import { FontLoader } from 'three/addons/loaders/FontLoader.js'; +import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; + +import Stats from 'three/addons/libs/stats.module.js'; + +// 1 micrometer to 100 billion light years in one scene, with 1 unit = 1 meter? preposterous! and yet... +const NEAR = 1e-6, + FAR = 1e27; +let SCREEN_WIDTH = window.innerWidth; +let SCREEN_HEIGHT = window.innerHeight; +let screensplit = 0.25, + screensplit_right = 0; +const mouse = [0.5, 0.5]; +let zoompos = -100, + minzoomspeed = 0.015; +let zoomspeed = minzoomspeed; + +let container, border, stats; +const objects = {}; + +// Generate a number of text labels, from 1µm in size up to 100,000,000 light years +// Try to use some descriptive real-world examples of objects at each scale + +const labeldata = [ + { size: 0.01, scale: 0.0001, label: 'microscopic (1µm)' }, // FIXME - triangulating text fails at this size, so we scale instead + { size: 0.01, scale: 0.1, label: 'minuscule (1mm)' }, + { size: 0.01, scale: 1.0, label: 'tiny (1cm)' }, + { size: 1, scale: 1.0, label: 'child-sized (1m)' }, + { size: 10, scale: 1.0, label: 'tree-sized (10m)' }, + { size: 100, scale: 1.0, label: 'building-sized (100m)' }, + { size: 1000, scale: 1.0, label: 'medium (1km)' }, + { size: 10000, scale: 1.0, label: 'city-sized (10km)' }, + { size: 3400000, scale: 1.0, label: 'moon-sized (3,400 Km)' }, + { size: 12000000, scale: 1.0, label: 'planet-sized (12,000 km)' }, + { size: 1400000000, scale: 1.0, label: 'sun-sized (1,400,000 km)' }, + { size: 7.47e12, scale: 1.0, label: 'solar system-sized (50Au)' }, + { size: 9.4605284e15, scale: 1.0, label: 'gargantuan (1 light year)' }, + { size: 3.08567758e16, scale: 1.0, label: 'ludicrous (1 parsec)' }, + { size: 1e19, scale: 1.0, label: 'mind boggling (1000 light years)' }, +]; + +init().then(animate); + +async function init() { + container = document.getElementById('container'); + + const loader = new FontLoader(); + const font = await loader.loadAsync('fonts/helvetiker_regular.typeface.json'); + + const scene = initScene(font); + + // Initialize two copies of the same scene, one with normal z-buffer and one with logarithmic z-buffer + objects.normal = await initView(scene, 'normal', false); + objects.logzbuf = await initView(scene, 'logzbuf', true); + + stats = new Stats(); + container.appendChild(stats.dom); + + // Resize border allows the user to easily compare effects of logarithmic depth buffer over the whole scene + border = document.getElementById('renderer_border'); + border.addEventListener('pointerdown', onBorderPointerDown); + + window.addEventListener('mousemove', onMouseMove); + window.addEventListener('resize', onWindowResize); + window.addEventListener('wheel', onMouseWheel); +} + +async function initView(scene, name, logDepthBuf) { + const framecontainer = document.getElementById('container_' + name); + + const camera = new THREE.PerspectiveCamera(50, (screensplit * SCREEN_WIDTH) / SCREEN_HEIGHT, NEAR, FAR); + scene.add(camera); + + const renderer = new THREE.WebGPURenderer({ antialias: true, logarithmicDepthBuffer: logDepthBuf }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(SCREEN_WIDTH / 2, SCREEN_HEIGHT); + renderer.domElement.style.position = 'relative'; + renderer.domElement.id = 'renderer_' + name; + framecontainer.appendChild(renderer.domElement); + + await renderer.init(); + + return { container: framecontainer, renderer: renderer, scene: scene, camera: camera }; +} + +function initScene(font) { + const scene = new THREE.Scene(); + + scene.add(new THREE.AmbientLight(0x777777)); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(100, 100, 100); + scene.add(light); + + const materialargs = { + color: 0xffffff, + specular: 0x050505, + shininess: 50, + emissive: 0x000000, + }; + + const geometry = new THREE.SphereGeometry(0.5, 24, 12); + + for (let i = 0; i < labeldata.length; i++) { + const scale = labeldata[i].scale || 1; + + const labelgeo = new TextGeometry(labeldata[i].label, { + font: font, + size: labeldata[i].size, + depth: labeldata[i].size / 2, + }); + + labelgeo.computeBoundingSphere(); + + // center text + labelgeo.translate(-labelgeo.boundingSphere.radius, 0, 0); + + materialargs.color = new THREE.Color().setHSL(Math.random(), 0.5, 0.5); + + const material = new THREE.MeshPhongMaterial(materialargs); + + const group = new THREE.Group(); + group.position.z = -labeldata[i].size * scale; + scene.add(group); + + const textmesh = new THREE.Mesh(labelgeo, material); + textmesh.scale.set(scale, scale, scale); + textmesh.position.z = -labeldata[i].size * scale; + textmesh.position.y = (labeldata[i].size / 4) * scale; + group.add(textmesh); + + const dotmesh = new THREE.Mesh(geometry, material); + dotmesh.position.y = (-labeldata[i].size / 4) * scale; + dotmesh.scale.multiplyScalar(labeldata[i].size * scale); + group.add(dotmesh); + } + + return scene; +} + +function updateRendererSizes() { + // Recalculate size for both renderers when screen size or split location changes + + SCREEN_WIDTH = window.innerWidth; + SCREEN_HEIGHT = window.innerHeight; + + screensplit_right = 1 - screensplit; + + objects.normal.renderer.setSize(screensplit * SCREEN_WIDTH, SCREEN_HEIGHT); + objects.normal.camera.aspect = (screensplit * SCREEN_WIDTH) / SCREEN_HEIGHT; + objects.normal.camera.updateProjectionMatrix(); + objects.normal.camera.setViewOffset(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, SCREEN_WIDTH * screensplit, SCREEN_HEIGHT); + objects.normal.container.style.width = screensplit * 100 + '%'; + + objects.logzbuf.renderer.setSize(screensplit_right * SCREEN_WIDTH, SCREEN_HEIGHT); + objects.logzbuf.camera.aspect = (screensplit_right * SCREEN_WIDTH) / SCREEN_HEIGHT; + objects.logzbuf.camera.updateProjectionMatrix(); + objects.logzbuf.camera.setViewOffset( + SCREEN_WIDTH, + SCREEN_HEIGHT, + SCREEN_WIDTH * screensplit, + 0, + SCREEN_WIDTH * screensplit_right, + SCREEN_HEIGHT, + ); + objects.logzbuf.container.style.width = screensplit_right * 100 + '%'; + + border.style.left = screensplit * 100 + '%'; +} + +function animate() { + requestAnimationFrame(animate); + + // Put some limits on zooming + const minzoom = labeldata[0].size * labeldata[0].scale * 1; + const maxzoom = labeldata[labeldata.length - 1].size * labeldata[labeldata.length - 1].scale * 100; + let damping = Math.abs(zoomspeed) > minzoomspeed ? 0.95 : 1.0; + + // Zoom out faster the further out you go + const zoom = THREE.MathUtils.clamp(Math.pow(Math.E, zoompos), minzoom, maxzoom); + zoompos = Math.log(zoom); + + // Slow down quickly at the zoom limits + if ((zoom == minzoom && zoomspeed < 0) || (zoom == maxzoom && zoomspeed > 0)) { + damping = 0.85; + } + + zoompos += zoomspeed; + zoomspeed *= damping; + + objects.normal.camera.position.x = Math.sin(0.5 * Math.PI * (mouse[0] - 0.5)) * zoom; + objects.normal.camera.position.y = Math.sin(0.25 * Math.PI * (mouse[1] - 0.5)) * zoom; + objects.normal.camera.position.z = Math.cos(0.5 * Math.PI * (mouse[0] - 0.5)) * zoom; + objects.normal.camera.lookAt(objects.normal.scene.position); + + // Clone camera settings across both scenes + objects.logzbuf.camera.position.copy(objects.normal.camera.position); + objects.logzbuf.camera.quaternion.copy(objects.normal.camera.quaternion); + + // Update renderer sizes if the split has changed + if (screensplit_right != 1 - screensplit) { + updateRendererSizes(); + } + + objects.normal.renderer.render(objects.normal.scene, objects.normal.camera); + objects.logzbuf.renderer.render(objects.logzbuf.scene, objects.logzbuf.camera); + + stats.update(); +} + +function onWindowResize() { + updateRendererSizes(); +} + +function onBorderPointerDown() { + // activate draggable window resizing bar + window.addEventListener('pointermove', onBorderPointerMove); + window.addEventListener('pointerup', onBorderPointerUp); +} + +function onBorderPointerMove(ev) { + screensplit = Math.max(0, Math.min(1, ev.clientX / window.innerWidth)); +} + +function onBorderPointerUp() { + window.removeEventListener('pointermove', onBorderPointerMove); + window.removeEventListener('pointerup', onBorderPointerUp); +} + +function onMouseMove(ev) { + mouse[0] = ev.clientX / window.innerWidth; + mouse[1] = ev.clientY / window.innerHeight; +} + +function onMouseWheel(ev) { + const amount = ev.deltaY; + if (amount === 0) return; + const dir = amount / Math.abs(amount); + zoomspeed = dir / 10; + + // Slow down default zoom speed after user starts zooming, to give them more control + minzoomspeed = 0.001; +} diff --git a/examples-testing/examples/webgpu_clearcoat.ts b/examples-testing/examples/webgpu_clearcoat.ts new file mode 100644 index 000000000..0d5b70a2f --- /dev/null +++ b/examples-testing/examples/webgpu_clearcoat.ts @@ -0,0 +1,205 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { HDRCubeTextureLoader } from 'three/addons/loaders/HDRCubeTextureLoader.js'; + +import { FlakesTexture } from 'three/addons/textures/FlakesTexture.js'; + +let container, stats; + +let camera, scene, renderer; + +let particleLight; +let group; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 0.25, 50); + camera.position.z = 10; + + scene = new THREE.Scene(); + + group = new THREE.Group(); + scene.add(group); + + new HDRCubeTextureLoader() + .setPath('textures/cube/pisaHDR/') + .load(['px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr'], function (texture) { + const geometry = new THREE.SphereGeometry(0.8, 64, 32); + + const textureLoader = new THREE.TextureLoader(); + + const diffuse = textureLoader.load('textures/carbon/Carbon.png'); + diffuse.colorSpace = THREE.SRGBColorSpace; + diffuse.wrapS = THREE.RepeatWrapping; + diffuse.wrapT = THREE.RepeatWrapping; + diffuse.repeat.x = 10; + diffuse.repeat.y = 10; + + const normalMap = textureLoader.load('textures/carbon/Carbon_Normal.png'); + normalMap.wrapS = THREE.RepeatWrapping; + normalMap.wrapT = THREE.RepeatWrapping; + normalMap.repeat.x = 10; + normalMap.repeat.y = 10; + + const normalMap2 = textureLoader.load('textures/water/Water_1_M_Normal.jpg'); + + const normalMap3 = new THREE.CanvasTexture(new FlakesTexture()); + normalMap3.wrapS = THREE.RepeatWrapping; + normalMap3.wrapT = THREE.RepeatWrapping; + normalMap3.repeat.x = 10; + normalMap3.repeat.y = 6; + normalMap3.anisotropy = 16; + + const normalMap4 = textureLoader.load('textures/golfball.jpg'); + + const clearcoatNormalMap = textureLoader.load( + 'textures/pbr/Scratched_gold/Scratched_gold_01_1K_Normal.png', + ); + + // car paint + + let material = new THREE.MeshPhysicalMaterial({ + clearcoat: 1.0, + clearcoatRoughness: 0.1, + metalness: 0.9, + roughness: 0.5, + color: 0x0000ff, + normalMap: normalMap3, + normalScale: new THREE.Vector2(0.15, 0.15), + }); + let mesh = new THREE.Mesh(geometry, material); + mesh.position.x = -1; + mesh.position.y = 1; + group.add(mesh); + + // fibers + + material = new THREE.MeshPhysicalMaterial({ + roughness: 0.5, + clearcoat: 1.0, + clearcoatRoughness: 0.1, + map: diffuse, + normalMap: normalMap, + }); + mesh = new THREE.Mesh(geometry, material); + mesh.position.x = 1; + mesh.position.y = 1; + group.add(mesh); + + // golf + + material = new THREE.MeshPhysicalMaterial({ + metalness: 0.0, + roughness: 0.1, + clearcoat: 1.0, + normalMap: normalMap4, + clearcoatNormalMap: clearcoatNormalMap, + + // y scale is negated to compensate for normal map handedness. + clearcoatNormalScale: new THREE.Vector2(2.0, -2.0), + }); + mesh = new THREE.Mesh(geometry, material); + mesh.position.x = -1; + mesh.position.y = -1; + group.add(mesh); + + // clearcoat + normalmap + + material = new THREE.MeshPhysicalMaterial({ + clearcoat: 1.0, + metalness: 1.0, + color: 0xff0000, + normalMap: normalMap2, + normalScale: new THREE.Vector2(0.15, 0.15), + clearcoatNormalMap: clearcoatNormalMap, + + // y scale is negated to compensate for normal map handedness. + clearcoatNormalScale: new THREE.Vector2(2.0, -2.0), + }); + mesh = new THREE.Mesh(geometry, material); + mesh.position.x = 1; + mesh.position.y = -1; + group.add(mesh); + + // + + scene.background = texture; + scene.environment = texture; + }); + + // LIGHTS + + particleLight = new THREE.Mesh( + new THREE.SphereGeometry(0.05, 8, 8), + new THREE.MeshBasicMaterial({ color: 0xffffff }), + ); + scene.add(particleLight); + + particleLight.add(new THREE.PointLight(0xffffff, 30)); + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + // + + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1.25; + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // EVENTS + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 3; + controls.maxDistance = 30; + + window.addEventListener('resize', onWindowResize); +} + +// + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); +} + +// + +function animate() { + render(); + + stats.update(); +} + +function render() { + const timer = Date.now() * 0.00025; + + particleLight.position.x = Math.sin(timer * 7) * 3; + particleLight.position.y = Math.cos(timer * 5) * 4; + particleLight.position.z = Math.cos(timer * 3) * 3; + + for (let i = 0; i < group.children.length; i++) { + const child = group.children[i]; + child.rotation.y += 0.005; + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_clipping.ts b/examples-testing/examples/webgpu_clipping.ts new file mode 100644 index 000000000..e57a7e96c --- /dev/null +++ b/examples-testing/examples/webgpu_clipping.ts @@ -0,0 +1,207 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer, startTime, object, stats; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(36, window.innerWidth / window.innerHeight, 0.25, 16); + + camera.position.set(0, 1.3, 3); + + scene = new THREE.Scene(); + + // Lights + + scene.add(new THREE.AmbientLight(0xcccccc)); + + const spotLight = new THREE.SpotLight(0xffffff, 60); + spotLight.angle = Math.PI / 5; + spotLight.penumbra = 0.2; + spotLight.position.set(2, 3, 3); + spotLight.castShadow = true; + spotLight.shadow.camera.near = 3; + spotLight.shadow.camera.far = 10; + spotLight.shadow.mapSize.width = 2048; + spotLight.shadow.mapSize.height = 2048; + spotLight.shadow.bias = -0.002; + spotLight.shadow.radius = 4; + scene.add(spotLight); + + const dirLight = new THREE.DirectionalLight(0x55505a, 3); + dirLight.position.set(0, 3, 0); + dirLight.castShadow = true; + dirLight.shadow.camera.near = 1; + dirLight.shadow.camera.far = 10; + + dirLight.shadow.camera.right = 1; + dirLight.shadow.camera.left = -1; + dirLight.shadow.camera.top = 1; + dirLight.shadow.camera.bottom = -1; + + dirLight.shadow.mapSize.width = 1024; + dirLight.shadow.mapSize.height = 1024; + scene.add(dirLight); + + // ***** Clipping planes: ***** + + const localPlane = new THREE.Plane(new THREE.Vector3(0, -1, 0), 0.8); + const localPlane2 = new THREE.Plane(new THREE.Vector3(0, 0, -1), 0.1); + const globalPlane = new THREE.Plane(new THREE.Vector3(-1, 0, 0), 0.1); + + // Geometry + + const material = new THREE.MeshPhongNodeMaterial({ + color: 0x80ee10, + shininess: 0, + side: THREE.DoubleSide, + + // ***** Clipping setup (material): ***** + clippingPlanes: [localPlane, localPlane2], + clipShadows: true, + alphaToCoverage: true, + clipIntersection: true, + }); + + const geometry = new THREE.TorusKnotGeometry(0.4, 0.08, 95, 20); + + object = new THREE.Mesh(geometry, material); + object.castShadow = true; + scene.add(object); + + const ground = new THREE.Mesh( + new THREE.PlaneGeometry(9, 9, 1, 1), + new THREE.MeshPhongNodeMaterial({ color: 0xa0adaf, shininess: 150 }), + ); + + ground.rotation.x = -Math.PI / 2; // rotates X/Y to X/Z + ground.receiveShadow = true; + scene.add(ground); + + // Stats + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // Renderer + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.shadowMap.enabled = true; + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + window.addEventListener('resize', onWindowResize); + document.body.appendChild(renderer.domElement); + + // ***** Clipping setup (renderer): ***** + const globalPlanes = [globalPlane]; + const Empty = Object.freeze([]); + + renderer.clippingPlanes = Empty; // GUI sets it to globalPlanes + renderer.localClippingEnabled = true; + + // Controls + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 1, 0); + controls.update(); + + // GUI + + const gui = new GUI(), + props = { + alphaToCoverage: true, + }, + folderLocal = gui.addFolder('Local Clipping'), + propsLocal = { + get Enabled() { + return renderer.localClippingEnabled; + }, + set Enabled(v) { + renderer.localClippingEnabled = v; + }, + + get Shadows() { + return material.clipShadows; + }, + set Shadows(v) { + material.clipShadows = v; + }, + + get Intersection() { + return material.clipIntersection; + }, + + set Intersection(v) { + material.clipIntersection = v; + }, + + get Plane() { + return localPlane.constant; + }, + set Plane(v) { + localPlane.constant = v; + }, + }, + folderGlobal = gui.addFolder('Global Clipping'), + propsGlobal = { + get Enabled() { + return renderer.clippingPlanes !== Empty; + }, + set Enabled(v) { + renderer.clippingPlanes = v ? globalPlanes : Empty; + }, + + get Plane() { + return globalPlane.constant; + }, + set Plane(v) { + globalPlane.constant = v; + }, + }; + + gui.add(props, 'alphaToCoverage').onChange(function (value) { + ground.material.alphaToCoverage = value; + ground.material.needsUpdate = true; + + material.alphaToCoverage = value; + material.needsUpdate = true; + }); + + folderLocal.add(propsLocal, 'Enabled'); + folderLocal.add(propsLocal, 'Shadows'); + folderLocal.add(propsLocal, 'Intersection'); + folderLocal.add(propsLocal, 'Plane', 0.3, 1.25); + + folderGlobal.add(propsGlobal, 'Enabled'); + folderGlobal.add(propsGlobal, 'Plane', -0.4, 3); + + // Start + + startTime = Date.now(); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate(currentTime) { + const time = (currentTime - startTime) / 1000; + + object.position.y = 0.8; + object.rotation.x = time * 0.5; + object.rotation.y = time * 0.2; + object.scale.setScalar(Math.cos(time) * 0.125 + 0.875); + + stats.begin(); + renderer.render(scene, camera); + stats.end(); +} diff --git a/examples-testing/examples/webgpu_custom_fog_background.ts b/examples-testing/examples/webgpu_custom_fog_background.ts new file mode 100644 index 000000000..4a2e6c800 --- /dev/null +++ b/examples-testing/examples/webgpu_custom_fog_background.ts @@ -0,0 +1,93 @@ +import * as THREE from 'three'; +import { pass, color, rangeFog } from 'three/tsl'; + +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +let camera, scene, renderer; +let postProcessing; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); + camera.position.set(-1.8, 0.6, 2.7); + + scene = new THREE.Scene(); + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + //renderer.toneMapping = THREE.ACESFilmicToneMapping; // apply tone mapping in post processing + container.appendChild(renderer.domElement); + + // post processing + + // render scene pass ( the same of css ) + const scenePass = pass(scene, camera); + const scenePassViewZ = scenePass.getViewZNode(); + + // background color + const backgroundColor = color(0x0066ff); + + // get fog factor from scene pass context + // equivalent to: scene.fog = new THREE.Fog( 0x0066ff, 2.7, 4 ); + const fogFactor = rangeFog(null, 2.7, 4).context({ getViewZ: () => scenePassViewZ }); + + // tone mapping scene pass + const scenePassTM = scenePass.toneMapping(THREE.ACESFilmicToneMapping); + + // mix fog from fog factor and background color + const compose = fogFactor.mix(scenePassTM, backgroundColor); + + postProcessing = new THREE.PostProcessing(renderer); + postProcessing.outputNode = compose; + + // + + new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.environment = texture; + + // model + + const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); + loader.load('DamagedHelmet.gltf', function (gltf) { + scene.add(gltf.scene); + + render(); + }); + }); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 2; + controls.maxDistance = 5; + controls.target.set(0, -0.1, -0.2); + controls.update(); + controls.addEventListener('change', render); // use if there is no animation loop + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +// + +function render() { + postProcessing.renderAsync(); +} diff --git a/examples-testing/examples/webgpu_instancing_morph.ts b/examples-testing/examples/webgpu_instancing_morph.ts new file mode 100644 index 000000000..cfd721721 --- /dev/null +++ b/examples-testing/examples/webgpu_instancing_morph.ts @@ -0,0 +1,148 @@ +import * as THREE from 'three'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let camera, scene, renderer, stats, mesh, mixer, dummy; + +const offset = 5000; + +const timeOffsets = new Float32Array(1024); + +for (let i = 0; i < 1024; i++) { + timeOffsets[i] = Math.random() * 3; +} + +const clock = new THREE.Clock(true); + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 100, 10000); + + scene = new THREE.Scene(); + + scene.background = new THREE.Color(0x99ddff); + + scene.fog = new THREE.Fog(0x99ddff, 5000, 10000); + + // + + stats = new Stats(); + document.body.appendChild(stats.dom); + + const light = new THREE.DirectionalLight(0xffffff, 1); + + light.position.set(200, 1000, 50); + + light.shadow.mapSize.width = 2048; + light.shadow.mapSize.height = 2048; + light.castShadow = true; + + light.shadow.camera.left = -5000; + light.shadow.camera.right = 5000; + light.shadow.camera.top = 5000; + light.shadow.camera.bottom = -5000; + light.shadow.camera.far = 2000; + + light.shadow.bias = -0.01; + + light.shadow.camera.updateProjectionMatrix(); + + scene.add(light); + + const hemi = new THREE.HemisphereLight(0x99ddff, 0x669933, 1 / 3); + + scene.add(hemi); + + const ground = new THREE.Mesh( + new THREE.PlaneGeometry(1000000, 1000000), + new THREE.MeshStandardMaterial({ color: 0x669933 }), + ); + + ground.rotation.x = -Math.PI / 2; + + ground.receiveShadow = true; + + scene.add(ground); + + const loader = new GLTFLoader(); + + loader.load('models/gltf/Horse.glb', function (glb) { + dummy = glb.scene.children[0]; + + mesh = new THREE.InstancedMesh( + dummy.geometry, + new THREE.MeshStandardNodeMaterial({ + flatShading: true, + }), + 1024, + ); + + mesh.castShadow = true; + + for (let x = 0, i = 0; x < 32; x++) { + for (let y = 0; y < 32; y++) { + dummy.position.set(offset - 300 * x + 200 * Math.random(), 0, offset - 300 * y); + + dummy.updateMatrix(); + + mesh.setMatrixAt(i, dummy.matrix); + + mesh.setColorAt(i, new THREE.Color(`hsl(${Math.random() * 360}, 50%, 66%)`)); + + i++; + } + } + + scene.add(mesh); + + mixer = new THREE.AnimationMixer(glb.scene); + + const action = mixer.clipAction(glb.animations[0]); + + action.play(); + }); + + // renderer + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setAnimationLoop(animate); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + document.body.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const time = clock.getElapsedTime(); + + const r = 3000; + camera.position.set(Math.sin(time / 10) * r, 1500 + 1000 * Math.cos(time / 5), Math.cos(time / 10) * r); + camera.lookAt(0, 0, 0); + + if (mesh) { + for (let i = 0; i < 1024; i++) { + mixer.setTime(time + timeOffsets[i]); + + mesh.setMorphAt(i, dummy); + } + + mesh.morphTexture.needsUpdate = true; + } + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgpu_lights_ies_spotlight.ts b/examples-testing/examples/webgpu_lights_ies_spotlight.ts new file mode 100644 index 000000000..41b56de88 --- /dev/null +++ b/examples-testing/examples/webgpu_lights_ies_spotlight.ts @@ -0,0 +1,117 @@ +import * as THREE from 'three'; + +import { OrbitControls } from './jsm/controls/OrbitControls.js'; + +import { IESLoader } from 'three/addons/loaders/IESLoader.js'; + +let renderer, scene, camera; +let lights; + +async function init() { + const iesLoader = new IESLoader().setPath('./ies/'); + //iesLoader.type = THREE.UnsignedByteType; // LDR + + const [iesTexture1, iesTexture2, iesTexture3, iesTexture4] = await Promise.all([ + iesLoader.loadAsync('007cfb11e343e2f42e3b476be4ab684e.ies'), + iesLoader.loadAsync('06b4cfdc8805709e767b5e2e904be8ad.ies'), + iesLoader.loadAsync('02a7562c650498ebb301153dbbf59207.ies'), + iesLoader.loadAsync('1a936937a49c63374e6d4fbed9252b29.ies'), + ]); + + // + + scene = new THREE.Scene(); + + // + + const spotLight = new THREE.IESSpotLight(0xff0000, 500); + spotLight.position.set(6.5, 1.5, 6.5); + spotLight.angle = Math.PI / 8; + spotLight.penumbra = 0.7; + spotLight.distance = 20; + spotLight.iesMap = iesTexture1; + scene.add(spotLight); + + // + + const spotLight2 = new THREE.IESSpotLight(0x00ff00, 500); + spotLight2.position.set(6.5, 1.5, -6.5); + spotLight2.angle = Math.PI / 8; + spotLight2.penumbra = 0.7; + spotLight2.distance = 20; + spotLight2.iesMap = iesTexture2; + scene.add(spotLight2); + + // + + const spotLight3 = new THREE.IESSpotLight(0x0000ff, 500); + spotLight3.position.set(-6.5, 1.5, -6.5); + spotLight3.angle = Math.PI / 8; + spotLight3.penumbra = 0.7; + spotLight3.distance = 20; + spotLight3.iesMap = iesTexture3; + scene.add(spotLight3); + + // + + const spotLight4 = new THREE.IESSpotLight(0xffffff, 500); + spotLight4.position.set(-6.5, 1.5, 6.5); + spotLight4.angle = Math.PI / 8; + spotLight4.penumbra = 0.7; + spotLight4.distance = 20; + spotLight4.iesMap = iesTexture4; + scene.add(spotLight4); + + // + + lights = [spotLight, spotLight2, spotLight3, spotLight4]; + + // + + const material = new THREE.MeshPhongMaterial({ color: 0x808080 /*, dithering: true*/ }); + + const geometry = new THREE.PlaneGeometry(200, 200); + + const mesh = new THREE.Mesh(geometry, material); + mesh.rotation.x = -Math.PI * 0.5; + scene.add(mesh); + + // + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(render); + document.body.appendChild(renderer.domElement); + + camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(16, 4, 1); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 2; + controls.maxDistance = 50; + controls.enablePan = false; + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function render(time) { + time = (time / 1000) * 2.0; + + for (let i = 0; i < lights.length; i++) { + lights[i].position.y = Math.sin(time + i) + 0.97; + } + + renderer.render(scene, camera); +} + +init(); diff --git a/examples-testing/examples/webgpu_lights_rectarealight.ts b/examples-testing/examples/webgpu_lights_rectarealight.ts new file mode 100644 index 000000000..5638c9029 --- /dev/null +++ b/examples-testing/examples/webgpu_lights_rectarealight.ts @@ -0,0 +1,79 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { RectAreaLightHelper } from 'three/addons/helpers/RectAreaLightHelper.js'; +import { RectAreaLightTexturesLib } from 'three/addons/lights/RectAreaLightTexturesLib.js'; + +let renderer, scene, camera; +let stats, meshKnot; + +init(); + +function init() { + THREE.RectAreaLightNode.setLTC(RectAreaLightTexturesLib.init()); + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animation); + document.body.appendChild(renderer.domElement); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.set(0, 5, -15); + + scene = new THREE.Scene(); + + const rectLight1 = new THREE.RectAreaLight(0xff0000, 5, 4, 10); + rectLight1.position.set(-5, 5, 5); + scene.add(rectLight1); + + const rectLight2 = new THREE.RectAreaLight(0x00ff00, 5, 4, 10); + rectLight2.position.set(0, 5, 5); + scene.add(rectLight2); + + const rectLight3 = new THREE.RectAreaLight(0x0000ff, 5, 4, 10); + rectLight3.position.set(5, 5, 5); + scene.add(rectLight3); + + scene.add(new RectAreaLightHelper(rectLight1)); + scene.add(new RectAreaLightHelper(rectLight2)); + scene.add(new RectAreaLightHelper(rectLight3)); + + const geoFloor = new THREE.BoxGeometry(2000, 0.1, 2000); + const matStdFloor = new THREE.MeshStandardMaterial({ color: 0xbcbcbc, roughness: 0.1, metalness: 0 }); + const mshStdFloor = new THREE.Mesh(geoFloor, matStdFloor); + scene.add(mshStdFloor); + + const geoKnot = new THREE.TorusKnotGeometry(1.5, 0.5, 200, 16); + const matKnot = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0, metalness: 0 }); + meshKnot = new THREE.Mesh(geoKnot, matKnot); + meshKnot.position.set(0, 5, 0); + scene.add(meshKnot); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.target.copy(meshKnot.position); + controls.update(); + + // + + window.addEventListener('resize', onWindowResize); + + stats = new Stats(); + document.body.appendChild(stats.dom); +} + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); +} + +function animation(time) { + meshKnot.rotation.y = time / 1000; + + renderer.render(scene, camera); + + stats.update(); +} diff --git a/examples-testing/examples/webgpu_loader_gltf.ts b/examples-testing/examples/webgpu_loader_gltf.ts new file mode 100644 index 000000000..64d1fda4b --- /dev/null +++ b/examples-testing/examples/webgpu_loader_gltf.ts @@ -0,0 +1,71 @@ +import * as THREE from 'three'; + +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +let camera, scene, renderer; + +init(); +render(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); + camera.position.set(-1.8, 0.6, 2.7); + + scene = new THREE.Scene(); + + new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + //texture.minFilter = THREE.LinearMipmapLinearFilter; + //texture.generateMipmaps = true; + + scene.background = texture; + scene.environment = texture; + + render(); + + // model + + const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); + loader.load('DamagedHelmet.gltf', function (gltf) { + scene.add(gltf.scene); + + render(); + }); + }); + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + container.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.addEventListener('change', render); // use if there is no animation loop + controls.minDistance = 2; + controls.maxDistance = 10; + controls.target.set(0, 0, -0.2); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +// + +function render() { + renderer.renderAsync(scene, camera); +} diff --git a/examples-testing/examples/webgpu_loader_gltf_anisotropy.ts b/examples-testing/examples/webgpu_loader_gltf_anisotropy.ts new file mode 100644 index 000000000..d100e8c81 --- /dev/null +++ b/examples-testing/examples/webgpu_loader_gltf_anisotropy.ts @@ -0,0 +1,65 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +let renderer, scene, camera, controls; + +init(); + +async function init() { + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(render); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1.35; + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.01, 10); + camera.position.set(-0.35, -0.2, 0.35); + + controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, -0.08, 0.11); + controls.minDistance = 0.1; + controls.maxDistance = 2; + controls.addEventListener('change', render); + controls.update(); + + const rgbeLoader = new RGBELoader().setPath('textures/equirectangular/'); + const gltfLoader = new GLTFLoader().setPath('models/gltf/'); + + const [texture, gltf] = await Promise.all([ + rgbeLoader.loadAsync('royal_esplanade_1k.hdr'), + gltfLoader.loadAsync('AnisotropyBarnLamp.glb'), + ]); + + // environment + + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + scene.backgroundBlurriness = 0.5; + scene.environment = texture; + + // model + + scene.add(gltf.scene); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function render() { + renderer.renderAsync(scene, camera); +} diff --git a/examples-testing/examples/webgpu_loader_gltf_compressed.ts b/examples-testing/examples/webgpu_loader_gltf_compressed.ts new file mode 100644 index 000000000..9405b64ae --- /dev/null +++ b/examples-testing/examples/webgpu_loader_gltf_compressed.ts @@ -0,0 +1,67 @@ +import * as THREE from 'three'; + +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js'; +import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer; + +init(); + +async function init() { + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 20); + camera.position.set(2, 2, 2); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xeeeeee); + + //lights + + const light = new THREE.PointLight(0xffffff); + light.power = 1300; + camera.add(light); + scene.add(camera); + + //renderer + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ReinhardToneMapping; + renderer.toneMappingExposure = 1; + document.body.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 3; + controls.maxDistance = 6; + controls.update(); + + const ktx2Loader = await new KTX2Loader().setTranscoderPath('jsm/libs/basis/').detectSupportAsync(renderer); + + const loader = new GLTFLoader(); + loader.setKTX2Loader(ktx2Loader); + loader.setMeshoptDecoder(MeshoptDecoder); + loader.load('models/gltf/coffeemat.glb', function (gltf) { + const gltfScene = gltf.scene; + gltfScene.position.y = -0.8; + gltfScene.scale.setScalar(0.01); + + scene.add(gltfScene); + }); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_loader_gltf_dispersion.ts b/examples-testing/examples/webgpu_loader_gltf_dispersion.ts new file mode 100644 index 000000000..c1f1ecc8f --- /dev/null +++ b/examples-testing/examples/webgpu_loader_gltf_dispersion.ts @@ -0,0 +1,63 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +let camera, scene, renderer; + +init().then(render); + +async function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 5); + camera.position.set(0.1, 0.05, 0.15); + + scene = new THREE.Scene(); + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(render); + renderer.toneMapping = THREE.ReinhardToneMapping; // TODO: Add THREE.NeutralToneMapping; + renderer.toneMappingExposure = 1; + container.appendChild(renderer.domElement); + + const rgbeLoader = await new RGBELoader() + .setPath('textures/equirectangular/') + .loadAsync('pedestrian_overpass_1k.hdr'); + rgbeLoader.mapping = THREE.EquirectangularReflectionMapping; + + scene = new THREE.Scene(); + scene.backgroundBlurriness = 0.5; + scene.environment = rgbeLoader; + scene.background = rgbeLoader; + + const loader = new GLTFLoader(); + const gltf = await loader.loadAsync('models/gltf/DispersionTest.glb'); + + scene.add(gltf.scene); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 0.1; + controls.maxDistance = 10; + controls.target.set(0, 0, 0); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_loader_gltf_iridescence.ts b/examples-testing/examples/webgpu_loader_gltf_iridescence.ts new file mode 100644 index 000000000..f163ea770 --- /dev/null +++ b/examples-testing/examples/webgpu_loader_gltf_iridescence.ts @@ -0,0 +1,70 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +let renderer, scene, camera, controls; + +init().catch(function (err) { + console.error(err); +}); + +async function init() { + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setAnimationLoop(render); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + document.body.appendChild(renderer.domElement); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.05, 20); + camera.position.set(0.35, 0.05, 0.35); + + controls = new OrbitControls(camera, renderer.domElement); + controls.autoRotate = true; + controls.autoRotateSpeed = -0.5; + controls.target.set(0, 0.2, 0); + controls.update(); + + const rgbeLoader = new RGBELoader().setPath('textures/equirectangular/'); + + const gltfLoader = new GLTFLoader().setPath('models/gltf/'); + + const [texture, gltf] = await Promise.all([ + rgbeLoader.loadAsync('venice_sunset_1k.hdr'), + gltfLoader.loadAsync('IridescenceLamp.glb'), + ]); + + // environment + + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + scene.environment = texture; + + // model + + scene.add(gltf.scene); + + render(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + render(); +} + +function render() { + controls.update(); + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_loader_gltf_sheen.ts b/examples-testing/examples/webgpu_loader_gltf_sheen.ts new file mode 100644 index 000000000..788ef2a89 --- /dev/null +++ b/examples-testing/examples/webgpu_loader_gltf_sheen.ts @@ -0,0 +1,81 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, controls; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 20); + camera.position.set(-0.75, 0.7, 1.25); + + scene = new THREE.Scene(); + //scene.add( new THREE.DirectionalLight( 0xffffff, 2 ) ); + + // model + + new GLTFLoader().setPath('models/gltf/').load('SheenChair.glb', function (gltf) { + scene.add(gltf.scene); + + const object = gltf.scene.getObjectByName('SheenChair_fabric'); + + const gui = new GUI(); + + gui.add(object.material, 'sheen', 0, 1); + gui.open(); + }); + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1; + container.appendChild(renderer.domElement); + + scene.background = new THREE.Color(0xaaaaaa); + + new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + //scene.backgroundBlurriness = 1; // @TODO: Needs PMREM + scene.environment = texture; + }); + + controls = new OrbitControls(camera, renderer.domElement); + controls.enableDamping = true; + controls.minDistance = 1; + controls.maxDistance = 10; + controls.target.set(0, 0.35, 0); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + controls.update(); // required if damping enabled + + render(); +} + +function render() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_loader_gltf_transmission.ts b/examples-testing/examples/webgpu_loader_gltf_transmission.ts new file mode 100644 index 000000000..040233262 --- /dev/null +++ b/examples-testing/examples/webgpu_loader_gltf_transmission.ts @@ -0,0 +1,80 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; + +let camera, scene, renderer, controls, clock, mixer; + +init(); + +function init() { + clock = new THREE.Clock(); + + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); + camera.position.set(0, 0.4, 0.7); + + scene = new THREE.Scene(); + + new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + scene.backgroundBlurriness = 0.35; + + scene.environment = texture; + + // model + + new GLTFLoader() + .setPath('models/gltf/') + .setDRACOLoader(new DRACOLoader().setDecoderPath('jsm/libs/draco/gltf/')) + .load('IridescentDishWithOlives.glb', function (gltf) { + mixer = new THREE.AnimationMixer(gltf.scene); + mixer.clipAction(gltf.animations[0]).play(); + + scene.add(gltf.scene); + }); + }); + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setAnimationLoop(render); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1; + container.appendChild(renderer.domElement); + + controls = new OrbitControls(camera, renderer.domElement); + controls.autoRotate = true; + controls.autoRotateSpeed = -0.75; + controls.enableDamping = true; + controls.minDistance = 0.5; + controls.maxDistance = 1; + controls.target.set(0, 0.1, 0); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function render() { + if (mixer) mixer.update(clock.getDelta()); + + controls.update(); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_materials_basic.ts b/examples-testing/examples/webgpu_materials_basic.ts new file mode 100644 index 000000000..0161a9c7b --- /dev/null +++ b/examples-testing/examples/webgpu_materials_basic.ts @@ -0,0 +1,137 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer; + +const spheres = []; + +let mouseX = 0; +let mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +const params = { + color: '#ffffff', + mapping: THREE.CubeReflectionMapping, + refractionRatio: 0.98, + transparent: false, + opacity: 1, +}; + +const mappings = { ReflectionMapping: THREE.CubeReflectionMapping, RefractionMapping: THREE.CubeRefractionMapping }; + +document.addEventListener('mousemove', onDocumentMouseMove); + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 100); + camera.position.z = 3; + + const path = './textures/cube/pisa/'; + const format = '.png'; + const urls = [ + path + 'px' + format, + path + 'nx' + format, + path + 'py' + format, + path + 'ny' + format, + path + 'pz' + format, + path + 'nz' + format, + ]; + + const textureCube = new THREE.CubeTextureLoader().load(urls); + + scene = new THREE.Scene(); + scene.background = textureCube; + + const geometry = new THREE.SphereGeometry(0.1, 32, 16); + const material = new THREE.MeshBasicMaterial({ color: 0xffffff, envMap: textureCube }); + + for (let i = 0; i < 500; i++) { + const mesh = new THREE.Mesh(geometry, material); + + mesh.position.x = Math.random() * 10 - 5; + mesh.position.y = Math.random() * 10 - 5; + mesh.position.z = Math.random() * 10 - 5; + + mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1; + + scene.add(mesh); + + spheres.push(mesh); + } + + // + + renderer = new THREE.WebGPURenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + const gui = new GUI({ width: 300 }); + + gui.addColor(params, 'color').onChange(value => material.color.set(value)); + gui.add(params, 'mapping', mappings).onChange(value => { + textureCube.mapping = value; + material.needsUpdate = true; + }); + gui.add(params, 'refractionRatio') + .min(0.0) + .max(1.0) + .step(0.01) + .onChange(value => (material.refractionRatio = value)); + gui.add(params, 'transparent').onChange(value => { + material.transparent = value; + material.needsUpdate = true; + }); + gui.add(params, 'opacity') + .min(0.0) + .max(1.0) + .step(0.01) + .onChange(value => (material.opacity = value)); + gui.open(); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onDocumentMouseMove(event) { + mouseX = (event.clientX - windowHalfX) / 100; + mouseY = (event.clientY - windowHalfY) / 100; +} + +// + +function animate() { + const timer = 0.0001 * Date.now(); + + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-mouseY - camera.position.y) * 0.05; + + camera.lookAt(scene.position); + + for (let i = 0, il = spheres.length; i < il; i++) { + const sphere = spheres[i]; + + sphere.position.x = 5 * Math.cos(timer + i); + sphere.position.y = 5 * Math.sin(timer + i * 1.1); + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_materials_displacementmap.ts b/examples-testing/examples/webgpu_materials_displacementmap.ts new file mode 100644 index 000000000..54d26d65e --- /dev/null +++ b/examples-testing/examples/webgpu_materials_displacementmap.ts @@ -0,0 +1,224 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; + +let stats; +let camera, scene, renderer, controls; + +const settings = { + metalness: 1.0, + roughness: 0.4, + ambientIntensity: 0.2, + aoMapIntensity: 1.0, + envMapIntensity: 1.0, + displacementScale: 2.436143, // from original model + normalScale: 1.0, +}; + +let mesh, material; + +let pointLight, ambientLight; + +const height = 500; // of camera frustum + +let r = 0.0; + +init(); +initGui(); + +// Init gui +function initGui() { + const gui = new GUI(); + + gui.add(settings, 'metalness') + .min(0) + .max(1) + .onChange(function (value) { + material.metalness = value; + }); + + gui.add(settings, 'roughness') + .min(0) + .max(1) + .onChange(function (value) { + material.roughness = value; + }); + + gui.add(settings, 'aoMapIntensity') + .min(0) + .max(1) + .onChange(function (value) { + material.aoMapIntensity = value; + }); + + gui.add(settings, 'ambientIntensity') + .min(0) + .max(1) + .onChange(function (value) { + ambientLight.intensity = value; + }); + + gui.add(settings, 'envMapIntensity') + .min(0) + .max(3) + .onChange(function (value) { + material.envMapIntensity = value; + }); + + gui.add(settings, 'displacementScale') + .min(0) + .max(3.0) + .onChange(function (value) { + material.displacementScale = value; + }); + + gui.add(settings, 'normalScale') + .min(-1) + .max(1) + .onChange(function (value) { + material.normalScale.set(1, -1).multiplyScalar(value); + }); +} + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + renderer = new THREE.WebGPURenderer(); + renderer.setAnimationLoop(animate); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + container.appendChild(renderer.domElement); + + // + + scene = new THREE.Scene(); + + const aspect = window.innerWidth / window.innerHeight; + camera = new THREE.OrthographicCamera(-height * aspect, height * aspect, height, -height, 1, 10000); + camera.position.z = 1500; + scene.add(camera); + + controls = new OrbitControls(camera, renderer.domElement); + controls.enableZoom = false; + controls.enableDamping = true; + + // lights + + ambientLight = new THREE.AmbientLight(0xffffff, settings.ambientIntensity); + scene.add(ambientLight); + + pointLight = new THREE.PointLight(0xff0000, 1.5, 0, 0); + pointLight.position.z = 2500; + scene.add(pointLight); + + const pointLight2 = new THREE.PointLight(0xff6666, 3, 0, 0); + camera.add(pointLight2); + + const pointLight3 = new THREE.PointLight(0x0000ff, 1.5, 0, 0); + pointLight3.position.x = -1000; + pointLight3.position.z = 1000; + scene.add(pointLight3); + + // env map + + const path = 'textures/cube/SwedishRoyalCastle/'; + const format = '.jpg'; + const urls = [ + path + 'px' + format, + path + 'nx' + format, + path + 'py' + format, + path + 'ny' + format, + path + 'pz' + format, + path + 'nz' + format, + ]; + + const reflectionCube = new THREE.CubeTextureLoader().load(urls); + + // textures + + const textureLoader = new THREE.TextureLoader(); + const normalMap = textureLoader.load('models/obj/ninja/normal.png'); + const aoMap = textureLoader.load('models/obj/ninja/ao.jpg'); + const displacementMap = textureLoader.load('models/obj/ninja/displacement.jpg'); + + // material + + material = new THREE.MeshStandardNodeMaterial({ + color: 0xc1c1c1, + roughness: settings.roughness, + metalness: settings.metalness, + + normalMap: normalMap, + normalScale: new THREE.Vector2(1, -1), // why does the normal map require negation in this case? + + aoMap: aoMap, + aoMapIntensity: 1, + + displacementMap: displacementMap, + displacementScale: settings.displacementScale, + displacementBias: -0.428408, // from original model + + envMap: reflectionCube, + envMapIntensity: settings.envMapIntensity, + + side: THREE.DoubleSide, + }); + + // + + const loader = new OBJLoader(); + loader.load('models/obj/ninja/ninjaHead_Low.obj', function (group) { + const geometry = group.children[0].geometry; + geometry.center(); + + mesh = new THREE.Mesh(geometry, material); + mesh.scale.multiplyScalar(25); + scene.add(mesh); + }); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const aspect = window.innerWidth / window.innerHeight; + + camera.left = -height * aspect; + camera.right = height * aspect; + camera.top = height; + camera.bottom = -height; + + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + controls.update(); + + stats.begin(); + render(); + stats.end(); +} + +function render() { + pointLight.position.x = 2500 * Math.cos(r); + pointLight.position.z = 2500 * Math.sin(r); + + r += 0.01; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_materials_lightmap.ts b/examples-testing/examples/webgpu_materials_lightmap.ts new file mode 100644 index 000000000..616645aab --- /dev/null +++ b/examples-testing/examples/webgpu_materials_lightmap.ts @@ -0,0 +1,94 @@ +import * as THREE from 'three'; +import { vec4, color, positionLocal, mix } from 'three/tsl'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let container, stats; +let camera, scene, renderer; + +init(); + +async function init() { + const { innerWidth, innerHeight } = window; + + container = document.createElement('div'); + document.body.appendChild(container); + + // CAMERA + + camera = new THREE.PerspectiveCamera(40, innerWidth / innerHeight, 1, 10000); + camera.position.set(700, 200, -500); + + // SCENE + + scene = new THREE.Scene(); + + // LIGHTS + + const light = new THREE.DirectionalLight(0xd5deff); + light.position.x = 300; + light.position.y = 250; + light.position.z = -500; + scene.add(light); + + // SKYDOME + + const topColor = new THREE.Color().copy(light.color); + const bottomColor = new THREE.Color(0xffffff); + const offset = 400; + const exponent = 0.6; + + const h = positionLocal.add(offset).normalize().y; + + const skyMat = new THREE.MeshBasicNodeMaterial(); + skyMat.colorNode = vec4(mix(color(bottomColor), color(topColor), h.max(0.0).pow(exponent)), 1.0); + skyMat.side = THREE.BackSide; + + const sky = new THREE.Mesh(new THREE.SphereGeometry(4000, 32, 15), skyMat); + scene.add(sky); + + // MODEL + + const loader = new THREE.ObjectLoader(); + const object = await loader.loadAsync('models/json/lightmap/lightmap.json'); + scene.add(object); + + // RENDERER + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setAnimationLoop(animate); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(innerWidth, innerHeight); + container.appendChild(renderer.domElement); + + // CONTROLS + + const controls = new OrbitControls(camera, renderer.domElement); + controls.maxPolarAngle = (0.9 * Math.PI) / 2; + controls.enableZoom = false; + + // STATS + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + renderer.render(scene, camera); + stats.update(); +} diff --git a/examples-testing/examples/webgpu_materials_toon.ts b/examples-testing/examples/webgpu_materials_toon.ts new file mode 100644 index 000000000..217460596 --- /dev/null +++ b/examples-testing/examples/webgpu_materials_toon.ts @@ -0,0 +1,148 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { FontLoader } from 'three/addons/loaders/FontLoader.js'; +import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; + +let container, stats; + +let camera, scene, renderer; +let particleLight; + +const loader = new FontLoader(); +loader.load('fonts/gentilis_regular.typeface.json', function (font) { + init(font); +}); + +function init(font) { + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 2500); + camera.position.set(0.0, 400, 400 * 3.5); + + // + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x444488); + + // + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(render); + container.appendChild(renderer.domElement); + + // Materials + + const cubeWidth = 400; + const numberOfSphersPerSide = 5; + const sphereRadius = (cubeWidth / numberOfSphersPerSide) * 0.8 * 0.5; + const stepSize = 1.0 / numberOfSphersPerSide; + + const geometry = new THREE.SphereGeometry(sphereRadius, 32, 16); + + for (let alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex++) { + const colors = new Uint8Array(alphaIndex + 2); + + for (let c = 0; c <= colors.length; c++) { + colors[c] = (c / colors.length) * 256; + } + + const gradientMap = new THREE.DataTexture(colors, colors.length, 1, THREE.RedFormat); + gradientMap.needsUpdate = true; + + for (let beta = 0; beta <= 1.0; beta += stepSize) { + for (let gamma = 0; gamma <= 1.0; gamma += stepSize) { + // basic monochromatic energy preservation + const diffuseColor = new THREE.Color() + .setHSL(alpha, 0.5, gamma * 0.5 + 0.1) + .multiplyScalar(1 - beta * 0.2); + + const material = new THREE.MeshToonNodeMaterial({ + color: diffuseColor, + gradientMap: gradientMap, + }); + + const mesh = new THREE.Mesh(geometry, material); + + mesh.position.x = alpha * 400 - 200; + mesh.position.y = beta * 400 - 200; + mesh.position.z = gamma * 400 - 200; + + scene.add(mesh); + } + } + } + + function addLabel(name, location) { + const textGeo = new TextGeometry(name, { + font: font, + + size: 20, + depth: 1, + curveSegments: 1, + }); + + const textMaterial = new THREE.MeshBasicNodeMaterial(); + const textMesh = new THREE.Mesh(textGeo, textMaterial); + textMesh.position.copy(location); + scene.add(textMesh); + } + + addLabel('-gradientMap', new THREE.Vector3(-350, 0, 0)); + addLabel('+gradientMap', new THREE.Vector3(350, 0, 0)); + + addLabel('-diffuse', new THREE.Vector3(0, 0, -300)); + addLabel('+diffuse', new THREE.Vector3(0, 0, 300)); + + particleLight = new THREE.Mesh( + new THREE.SphereGeometry(4, 8, 8), + new THREE.MeshBasicNodeMaterial({ color: 0xffffff }), + ); + scene.add(particleLight); + + // Lights + + scene.add(new THREE.AmbientLight(0xc1c1c1, 3)); + + const pointLight = new THREE.PointLight(0xffffff, 2, 800, 0); + particleLight.add(pointLight); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 200; + controls.maxDistance = 2000; + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function render() { + const timer = Date.now() * 0.00025; + + particleLight.position.x = Math.sin(timer * 7) * 300; + particleLight.position.y = Math.cos(timer * 5) * 400; + particleLight.position.z = Math.cos(timer * 3) * 300; + + stats.begin(); + + renderer.render(scene, camera); + + stats.end(); +} diff --git a/examples-testing/examples/webgpu_materials_transmission.ts b/examples-testing/examples/webgpu_materials_transmission.ts new file mode 100644 index 000000000..0e04ddad9 --- /dev/null +++ b/examples-testing/examples/webgpu_materials_transmission.ts @@ -0,0 +1,168 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +const params = { + color: 0xffffff, + transmission: 1, + opacity: 1, + metalness: 0, + roughness: 0, + ior: 1.5, + thickness: 0.01, + specularIntensity: 1, + specularColor: 0xffffff, + envMapIntensity: 1, + lightIntensity: 1, + exposure: 1, +}; + +let camera, scene, renderer; + +let mesh; + +const hdrEquirect = new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function () { + hdrEquirect.mapping = THREE.EquirectangularReflectionMapping; + + init(); + render(); +}); + +function init() { + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(render); + document.body.appendChild(renderer.domElement); + + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = params.exposure; + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 2000); + camera.position.set(0, 0, 120); + + // + + scene.background = hdrEquirect; + + // + + const geometry = new THREE.SphereGeometry(20, 64, 32); + + const texture = new THREE.CanvasTexture(generateTexture()); + texture.magFilter = THREE.NearestFilter; + texture.wrapT = THREE.RepeatWrapping; + texture.wrapS = THREE.RepeatWrapping; + texture.repeat.set(1, 3.5); + + const material = new THREE.MeshPhysicalMaterial({ + color: params.color, + metalness: params.metalness, + roughness: params.roughness, + ior: params.ior, + alphaMap: texture, + envMap: hdrEquirect, + envMapIntensity: params.envMapIntensity, + transmission: params.transmission, // use material.transmission for glass materials + specularIntensity: params.specularIntensity, + specularColor: params.specularColor, + opacity: params.opacity, + side: THREE.DoubleSide, + transparent: true, + }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 10; + controls.maxDistance = 150; + + window.addEventListener('resize', onWindowResize); + + // + + const gui = new GUI(); + + gui.addColor(params, 'color').onChange(function () { + material.color.set(params.color); + }); + + gui.add(params, 'transmission', 0, 1, 0.01).onChange(function () { + material.transmission = params.transmission; + }); + + gui.add(params, 'opacity', 0, 1, 0.01).onChange(function () { + material.opacity = params.opacity; + }); + + gui.add(params, 'metalness', 0, 1, 0.01).onChange(function () { + material.metalness = params.metalness; + }); + + gui.add(params, 'roughness', 0, 1, 0.01).onChange(function () { + material.roughness = params.roughness; + }); + + gui.add(params, 'ior', 1, 2, 0.01).onChange(function () { + material.ior = params.ior; + }); + + gui.add(params, 'thickness', 0, 5, 0.01).onChange(function () { + material.thickness = params.thickness; + }); + + gui.add(params, 'specularIntensity', 0, 1, 0.01).onChange(function () { + material.specularIntensity = params.specularIntensity; + }); + + gui.addColor(params, 'specularColor').onChange(function () { + material.specularColor.set(params.specularColor); + }); + + gui.add(params, 'envMapIntensity', 0, 1, 0.01) + .name('envMap intensity') + .onChange(function () { + material.envMapIntensity = params.envMapIntensity; + }); + + gui.add(params, 'exposure', 0, 1, 0.01).onChange(function () { + renderer.toneMappingExposure = params.exposure; + }); + + gui.open(); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); +} + +// + +function generateTexture() { + const canvas = document.createElement('canvas'); + canvas.width = 2; + canvas.height = 2; + + const context = canvas.getContext('2d'); + context.fillStyle = 'white'; + context.fillRect(0, 1, 2, 1); + + return canvas; +} + +function render() { + renderer.renderAsync(scene, camera); +} diff --git a/examples-testing/examples/webgpu_materials_video.ts b/examples-testing/examples/webgpu_materials_video.ts new file mode 100644 index 000000000..bd84aba0f --- /dev/null +++ b/examples-testing/examples/webgpu_materials_video.ts @@ -0,0 +1,184 @@ +import * as THREE from 'three'; + +let container; + +let camera, scene, renderer; + +let video, texture, material, mesh; + +let mouseX = 0; +let mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +let cube_count; + +const meshes = [], + materials = [], + xgrid = 20, + ygrid = 10; + +const startButton = document.getElementById('startButton'); +startButton.addEventListener('click', function () { + init(); +}); + +function init() { + const overlay = document.getElementById('overlay'); + overlay.remove(); + + container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10000); + camera.position.z = 500; + + scene = new THREE.Scene(); + + const light = new THREE.DirectionalLight(0xffffff, 7); + light.position.set(0.5, 1, 1).normalize(); + scene.add(light); + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(render); + container.appendChild(renderer.domElement); + + video = document.getElementById('video'); + video.play(); + video.addEventListener('play', function () { + this.currentTime = 3; + }); + + texture = new THREE.VideoTexture(video); + + // + + let i, j, ox, oy, geometry; + + const ux = 1 / xgrid; + const uy = 1 / ygrid; + + const xsize = 480 / xgrid; + const ysize = 204 / ygrid; + + const parameters = { color: 0xffffff, map: texture }; + + cube_count = 0; + + for (i = 0; i < xgrid; i++) { + for (j = 0; j < ygrid; j++) { + ox = i; + oy = j; + + geometry = new THREE.BoxGeometry(xsize, ysize, xsize); + + change_uvs(geometry, ux, uy, ox, oy); + + materials[cube_count] = new THREE.MeshPhongMaterial(parameters); + + material = materials[cube_count]; + + material.hue = i / xgrid; + material.saturation = 1 - j / ygrid; + + material.color.setHSL(material.hue, material.saturation, 0.5); + + mesh = new THREE.Mesh(geometry, material); + + mesh.position.x = (i - xgrid / 2) * xsize; + mesh.position.y = (j - ygrid / 2) * ysize; + mesh.position.z = 0; + + mesh.scale.x = mesh.scale.y = mesh.scale.z = 1; + + scene.add(mesh); + + mesh.dx = 0.001 * (0.5 - Math.random()); + mesh.dy = 0.001 * (0.5 - Math.random()); + + meshes[cube_count] = mesh; + + cube_count += 1; + } + } + + document.addEventListener('mousemove', onDocumentMouseMove); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function change_uvs(geometry, unitx, unity, offsetx, offsety) { + const uvs = geometry.attributes.uv.array; + + for (let i = 0; i < uvs.length; i += 2) { + uvs[i] = (uvs[i] + offsetx) * unitx; + uvs[i + 1] = (uvs[i + 1] + offsety) * unity; + } +} + +function onDocumentMouseMove(event) { + mouseX = event.clientX - windowHalfX; + mouseY = (event.clientY - windowHalfY) * 0.3; +} + +// + +let h, + counter = 1; + +function render() { + const time = Date.now() * 0.00005; + + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y += (-mouseY - camera.position.y) * 0.05; + + camera.lookAt(scene.position); + + for (let i = 0; i < cube_count; i++) { + material = materials[i]; + + h = ((360 * (material.hue + time)) % 360) / 360; + material.color.setHSL(h, material.saturation, 0.5); + } + + if (counter % 1000 > 200) { + for (let i = 0; i < cube_count; i++) { + mesh = meshes[i]; + + mesh.rotation.x += 10 * mesh.dx; + mesh.rotation.y += 10 * mesh.dy; + + mesh.position.x -= 150 * mesh.dx; + mesh.position.y += 150 * mesh.dy; + mesh.position.z += 300 * mesh.dx; + } + } + + if (counter % 1000 === 0) { + for (let i = 0; i < cube_count; i++) { + mesh = meshes[i]; + + mesh.dx *= -1; + mesh.dy *= -1; + } + } + + counter++; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_mesh_batch.ts b/examples-testing/examples/webgpu_mesh_batch.ts new file mode 100644 index 000000000..a619f430b --- /dev/null +++ b/examples-testing/examples/webgpu_mesh_batch.ts @@ -0,0 +1,271 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { radixSort } from 'three/addons/utils/SortUtils.js'; + +let camera, scene, renderer; +let controls, stats; +let gui; +let geometries, mesh, material; +const ids = []; + +const matrix = new THREE.Matrix4(); + +// + +const position = new THREE.Vector3(); +const rotation = new THREE.Euler(); +const quaternion = new THREE.Quaternion(); +const scale = new THREE.Vector3(); + +// + +const MAX_GEOMETRY_COUNT = 20000; + +const api = { + webgpu: true, + count: 512, + dynamic: 16, + + sortObjects: true, + perObjectFrustumCulled: true, + opacity: 1, + useCustomSort: true, +}; + +init(); + +// + +function randomizeMatrix(matrix) { + position.x = Math.random() * 40 - 20; + position.y = Math.random() * 40 - 20; + position.z = Math.random() * 40 - 20; + + rotation.x = Math.random() * 2 * Math.PI; + rotation.y = Math.random() * 2 * Math.PI; + rotation.z = Math.random() * 2 * Math.PI; + + quaternion.setFromEuler(rotation); + + scale.x = scale.y = scale.z = 0.5 + Math.random() * 0.5; + + return matrix.compose(position, quaternion, scale); +} + +function randomizeRotationSpeed(rotation) { + rotation.x = Math.random() * 0.01; + rotation.y = Math.random() * 0.01; + rotation.z = Math.random() * 0.01; + return rotation; +} + +function initGeometries() { + geometries = [ + new THREE.ConeGeometry(1.0, 2.0), + new THREE.BoxGeometry(2.0, 2.0, 2.0), + new THREE.SphereGeometry(1.0, 16, 8), + ]; +} + +function createMaterial() { + if (!material) { + material = new THREE.MeshNormalNodeMaterial(); + } + + return material; +} + +function cleanup() { + if (mesh) { + mesh.parent.remove(mesh); + + if (mesh.dispose) { + mesh.dispose(); + } + } +} + +function initMesh() { + cleanup(); + initBatchedMesh(); +} + +function initBatchedMesh() { + const geometryCount = api.count; + const vertexCount = geometries.length * 512; + const indexCount = geometries.length * 1024; + + const euler = new THREE.Euler(); + const matrix = new THREE.Matrix4(); + mesh = new THREE.BatchedMesh(geometryCount, vertexCount, indexCount, createMaterial()); + mesh.userData.rotationSpeeds = []; + + // disable full-object frustum culling since all of the objects can be dynamic. + mesh.frustumCulled = false; + + ids.length = 0; + + const geometryIds = [ + mesh.addGeometry(geometries[0]), + mesh.addGeometry(geometries[1]), + mesh.addGeometry(geometries[2]), + ]; + for (let i = 0; i < api.count; i++) { + const id = mesh.addInstance(geometryIds[i % geometryIds.length]); + mesh.setMatrixAt(id, randomizeMatrix(matrix)); + + const rotationMatrix = new THREE.Matrix4(); + rotationMatrix.makeRotationFromEuler(randomizeRotationSpeed(euler)); + mesh.userData.rotationSpeeds.push(rotationMatrix); + + ids.push(id); + } + + scene.add(mesh); +} + +function init(forceWebGL = false) { + if (renderer) { + renderer.dispose(); + controls.dispose(); + document.body.removeChild(stats.dom); + document.body.removeChild(renderer.domElement); + } + + // camera + + const aspect = window.innerWidth / window.innerHeight; + + camera = new THREE.PerspectiveCamera(70, aspect, 1, 100); + camera.position.z = 50; + + // renderer + + renderer = new THREE.WebGPURenderer({ antialias: true, forceWebGL }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + + renderer.setAnimationLoop(animate); + + // scene + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + + if (forceWebGL) { + scene.background = new THREE.Color(0xf10000); + } else { + scene.background = new THREE.Color(0x0000f1); + } + + document.body.appendChild(renderer.domElement); + + initGeometries(); + initMesh(); + + // controls + + controls = new OrbitControls(camera, renderer.domElement); + controls.autoRotate = true; + controls.autoRotateSpeed = 1.0; + + // stats + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // gui + + gui = new GUI(); + gui.add(api, 'webgpu').onChange(() => { + init(!api.webgpu); + }); + gui.add(api, 'count', 1, MAX_GEOMETRY_COUNT).step(1).onChange(initMesh); + gui.add(api, 'dynamic', 0, MAX_GEOMETRY_COUNT).step(1); + + gui.add(api, 'opacity', 0, 1).onChange(v => { + if (v < 1) { + material.transparent = true; + material.depthWrite = false; + } else { + material.transparent = false; + material.depthWrite = true; + } + + material.opacity = v; + material.needsUpdate = true; + }); + gui.add(api, 'sortObjects'); + gui.add(api, 'perObjectFrustumCulled'); + gui.add(api, 'useCustomSort'); + + // listeners + + window.addEventListener('resize', onWindowResize); + + function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); + } + + async function animate() { + animateMeshes(); + + controls.update(); + + if (mesh.isBatchedMesh) { + mesh.sortObjects = api.sortObjects; + mesh.perObjectFrustumCulled = api.perObjectFrustumCulled; + mesh.setCustomSort(api.useCustomSort ? sortFunction : null); + } + + await renderer.renderAsync(scene, camera); + + stats.update(); + } + + function animateMeshes() { + const loopNum = Math.min(api.count, api.dynamic); + + for (let i = 0; i < loopNum; i++) { + const rotationMatrix = mesh.userData.rotationSpeeds[i]; + const id = ids[i]; + + mesh.getMatrixAt(id, matrix); + matrix.multiply(rotationMatrix); + mesh.setMatrixAt(id, matrix); + } + } +} + +// + +function sortFunction(list, camera) { + // initialize options + this._options = this._options || { + get: el => el.z, + aux: new Array(this.maxInstanceCount), + }; + + const options = this._options; + options.reversed = this.material.transparent; + + // convert depth to unsigned 32 bit range + const factor = (2 ** 32 - 1) / camera.far; // UINT32_MAX / max_depth + for (let i = 0, l = list.length; i < l; i++) { + list[i].z *= factor; + } + + // perform a fast-sort using the hybrid radix sort function + radixSort(list, options); +} diff --git a/examples-testing/examples/webgpu_morphtargets.ts b/examples-testing/examples/webgpu_morphtargets.ts new file mode 100644 index 000000000..9fb7075cb --- /dev/null +++ b/examples-testing/examples/webgpu_morphtargets.ts @@ -0,0 +1,121 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let container, camera, scene, renderer, mesh; + +init(); + +function init() { + container = document.getElementById('container'); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x8fbcd4); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 20); + camera.position.z = 10; + scene.add(camera); + + scene.add(new THREE.AmbientLight(0x8fbcd4, 1.5)); + + const pointLight = new THREE.PointLight(0xffffff, 200); + camera.add(pointLight); + + const geometry = createGeometry(); + + const material = new THREE.MeshPhongMaterial({ + color: 0xff0000, + flatShading: true, + }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + initGUI(); + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(function () { + renderer.render(scene, camera); + }); + container.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.enableZoom = false; + + window.addEventListener('resize', onWindowResize); +} + +function createGeometry() { + const geometry = new THREE.BoxGeometry(2, 2, 2, 32, 32, 32); + + // create an empty array to hold targets for the attribute we want to morph + // morphing positions and normals is supported + geometry.morphAttributes.position = []; + + // the original positions of the cube's vertices + const positionAttribute = geometry.attributes.position; + + // for the first morph target we'll move the cube's vertices onto the surface of a sphere + const spherePositions = []; + + // for the second morph target, we'll twist the cubes vertices + const twistPositions = []; + const direction = new THREE.Vector3(1, 0, 0); + const vertex = new THREE.Vector3(); + + for (let i = 0; i < positionAttribute.count; i++) { + const x = positionAttribute.getX(i); + const y = positionAttribute.getY(i); + const z = positionAttribute.getZ(i); + + spherePositions.push( + x * Math.sqrt(1 - (y * y) / 2 - (z * z) / 2 + (y * y * z * z) / 3), + y * Math.sqrt(1 - (z * z) / 2 - (x * x) / 2 + (z * z * x * x) / 3), + z * Math.sqrt(1 - (x * x) / 2 - (y * y) / 2 + (x * x * y * y) / 3), + ); + + // stretch along the x-axis so we can see the twist better + vertex.set(x * 2, y, z); + + vertex.applyAxisAngle(direction, (Math.PI * x) / 2).toArray(twistPositions, twistPositions.length); + } + + // add the spherical positions as the first morph target + geometry.morphAttributes.position[0] = new THREE.Float32BufferAttribute(spherePositions, 3); + + // add the twisted positions as the second morph target + geometry.morphAttributes.position[1] = new THREE.Float32BufferAttribute(twistPositions, 3); + + return geometry; +} + +function initGUI() { + // Set up dat.GUI to control targets + const params = { + Spherify: 0, + Twist: 0, + }; + const gui = new GUI({ title: 'Morph Targets' }); + + gui.add(params, 'Spherify', 0, 1) + .step(0.01) + .onChange(function (value) { + mesh.morphTargetInfluences[0] = value; + }); + gui.add(params, 'Twist', 0, 1) + .step(0.01) + .onChange(function (value) { + mesh.morphTargetInfluences[1] = value; + }); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} diff --git a/examples-testing/examples/webgpu_morphtargets_face.ts b/examples-testing/examples/webgpu_morphtargets_face.ts new file mode 100644 index 000000000..ea9f86588 --- /dev/null +++ b/examples-testing/examples/webgpu_morphtargets_face.ts @@ -0,0 +1,102 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js'; +import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +init(); + +async function init() { + let mixer; + + const clock = new THREE.Clock(); + + const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 20); + camera.position.set(-1.8, 0.8, 3); + + const scene = new THREE.Scene(); + + const renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + document.body.appendChild(renderer.domElement); + + await renderer.init(); + + const environment = new RoomEnvironment(); + const pmremGenerator = new THREE.PMREMGenerator(renderer); + + scene.background = new THREE.Color(0x666666); + scene.environment = pmremGenerator.fromScene(environment).texture; + + const ktx2Loader = await new KTX2Loader().setTranscoderPath('jsm/libs/basis/').detectSupportAsync(renderer); + + new GLTFLoader() + .setKTX2Loader(ktx2Loader) + .setMeshoptDecoder(MeshoptDecoder) + .load('models/gltf/facecap.glb', gltf => { + const mesh = gltf.scene.children[0]; + + scene.add(mesh); + + mixer = new THREE.AnimationMixer(mesh); + + mixer.clipAction(gltf.animations[0]).play(); + + // GUI + + const head = mesh.getObjectByName('mesh_2'); + const influences = head.morphTargetInfluences; + + const gui = new GUI(); + gui.close(); + + for (const [key, value] of Object.entries(head.morphTargetDictionary)) { + gui.add(influences, value, 0, 1, 0.01).name(key.replace('blendShape1.', '')).listen(); + } + }); + + scene.background = new THREE.Color(0x666666); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.enableDamping = true; + controls.minDistance = 2.5; + controls.maxDistance = 5; + controls.minAzimuthAngle = -Math.PI / 2; + controls.maxAzimuthAngle = Math.PI / 2; + controls.maxPolarAngle = Math.PI / 1.8; + controls.target.set(0, 0.15, -0.2); + + const stats = new Stats(); + document.body.appendChild(stats.dom); + + function animate() { + const delta = clock.getDelta(); + + if (mixer) { + mixer.update(delta); + } + + renderer.render(scene, camera); + + controls.update(); + + stats.update(); + } + + window.addEventListener('resize', () => { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + }); +} diff --git a/examples-testing/examples/webgpu_mrt.ts b/examples-testing/examples/webgpu_mrt.ts new file mode 100644 index 000000000..371c898c7 --- /dev/null +++ b/examples-testing/examples/webgpu_mrt.ts @@ -0,0 +1,119 @@ +import * as THREE from 'three'; +import { + output, + transformedNormalWorld, + pass, + step, + diffuseColor, + emissive, + viewportTopLeft, + mix, + mrt, + Fn, +} from 'three/tsl'; + +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +let camera, scene, renderer; +let postProcessing; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + // scene + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); + camera.position.set(-1.8, 0.6, 2.7); + + scene = new THREE.Scene(); + + new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + scene.environment = texture; + + // model + + const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); + loader.load('DamagedHelmet.gltf', function (gltf) { + scene.add(gltf.scene); + }); + }); + + // renderer + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(render); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + container.appendChild(renderer.domElement); + + // post processing + + const scenePass = pass(scene, camera, { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter }); + scenePass.setMRT( + mrt({ + output: output, + normal: transformedNormalWorld.directionToColor(), + diffuse: diffuseColor, + emissive: emissive, + }), + ); + + // optimize textures + + const normalTexture = scenePass.getTexture('normal'); + const diffuseTexture = scenePass.getTexture('diffuse'); + const emissiveTexture = scenePass.getTexture('emissive'); + + normalTexture.type = diffuseTexture.type = emissiveTexture.type = THREE.UnsignedByteType; + + // post processing - mrt + + postProcessing = new THREE.PostProcessing(renderer); + postProcessing.outputColorTransform = false; + postProcessing.outputNode = Fn(() => { + const output = scenePass.getTextureNode('output'); // output name is optional here + const normal = scenePass.getTextureNode('normal'); + const diffuse = scenePass.getTextureNode('diffuse'); + const emissive = scenePass.getTextureNode('emissive'); + + const out = mix(output.renderOutput(), output, step(0.2, viewportTopLeft.x)); + const nor = mix(out, normal, step(0.4, viewportTopLeft.x)); + const emi = mix(nor, emissive, step(0.6, viewportTopLeft.x)); + const dif = mix(emi, diffuse, step(0.8, viewportTopLeft.x)); + + return dif; + })(); + + // controls + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 2; + controls.maxDistance = 10; + controls.target.set(0, 0, -0.2); + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function render() { + postProcessing.render(); +} diff --git a/examples-testing/examples/webgpu_multiple_rendertargets_readback.ts b/examples-testing/examples/webgpu_multiple_rendertargets_readback.ts new file mode 100644 index 000000000..2405b8ad6 --- /dev/null +++ b/examples-testing/examples/webgpu_multiple_rendertargets_readback.ts @@ -0,0 +1,156 @@ +import * as THREE from 'three'; +import { mix, step, texture, viewportTopLeft, mrt, output, transformedNormalWorld, uv, vec2 } from 'three/tsl'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, torus; +let quadMesh, sceneMRT, renderTarget, readbackTarget, material, readbackMaterial, pixelBuffer, pixelBufferTexture; + +const gui = new GUI(); + +const options = { + selection: 'mrt', +}; + +gui.add(options, 'selection', ['mrt', 'diffuse', 'normal']); + +init(); + +function init() { + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(render); + document.body.appendChild(renderer.domElement); + + // Create a multi render target with Float buffers + + renderTarget = new THREE.RenderTarget( + window.innerWidth * window.devicePixelRatio, + window.innerHeight * window.devicePixelRatio, + { count: 2, minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter }, + ); + + // Name our G-Buffer attachments for debugging + + renderTarget.textures[0].name = 'output'; + renderTarget.textures[1].name = 'normal'; + + // Init readback render target, readback data texture, readback material + // Be careful with the size! 512 is already big. Reading data back from the GPU is computationally intensive + + const size = 512; + + readbackTarget = new THREE.RenderTarget(size, size, { count: 2 }); + + pixelBuffer = new Uint8Array(size ** 2 * 4).fill(0); + pixelBufferTexture = new THREE.DataTexture(pixelBuffer, size, size); + pixelBufferTexture.type = THREE.UnsignedByteType; + pixelBufferTexture.format = THREE.RGBAFormat; + + readbackMaterial = new THREE.MeshBasicNodeMaterial(); + readbackMaterial.colorNode = texture(pixelBufferTexture); + + // MRT + + sceneMRT = mrt({ + output: output, + normal: transformedNormalWorld, + }); + + // Scene + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x222222); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 50); + camera.position.z = 4; + + const loader = new THREE.TextureLoader(); + + const diffuse = loader.load('textures/hardwood2_diffuse.jpg'); + diffuse.colorSpace = THREE.SRGBColorSpace; + diffuse.wrapS = THREE.RepeatWrapping; + diffuse.wrapT = THREE.RepeatWrapping; + + const torusMaterial = new THREE.NodeMaterial(); + torusMaterial.colorNode = texture(diffuse, uv().mul(vec2(10, 4))); + + torus = new THREE.Mesh(new THREE.TorusKnotGeometry(1, 0.3, 128, 32), torusMaterial); + scene.add(torus); + + // Output + + material = new THREE.NodeMaterial(); + material.colorNode = mix( + texture(renderTarget.textures[0]), + texture(renderTarget.textures[1]), + step(0.5, viewportTopLeft.x), + ); + + quadMesh = new THREE.QuadMesh(material); + + // Controls + + new OrbitControls(camera, renderer.domElement); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); + + const dpr = renderer.getPixelRatio(); + renderTarget.setSize(window.innerWidth * dpr, window.innerHeight * dpr); +} + +async function render(time) { + const selection = options.selection; + + torus.rotation.y = (time / 1000) * 0.4; + + const isMRT = selection === 'mrt'; + + // render scene into target + renderer.setMRT(isMRT ? sceneMRT : null); + renderer.setRenderTarget(isMRT ? renderTarget : readbackTarget); + renderer.render(scene, camera); + + // render post FX + renderer.setMRT(null); + renderer.setRenderTarget(null); + + if (isMRT) { + quadMesh.material = material; + } else { + quadMesh.material = readbackMaterial; + + await readback(); + } + + quadMesh.render(renderer); +} + +async function readback() { + const width = readbackTarget.width; + const height = readbackTarget.height; + + const selection = options.selection; + + if (selection === 'diffuse') { + pixelBuffer = await renderer.readRenderTargetPixelsAsync(readbackTarget, 0, 0, width, height, 0); // zero is optional + + pixelBufferTexture.image.data = pixelBuffer; + pixelBufferTexture.needsUpdate = true; + } else if (selection === 'normal') { + pixelBuffer = await renderer.readRenderTargetPixelsAsync(readbackTarget, 0, 0, width, height, 1); + + pixelBufferTexture.image.data = pixelBuffer; + pixelBufferTexture.needsUpdate = true; + } +} diff --git a/examples-testing/examples/webgpu_ocean.ts b/examples-testing/examples/webgpu_ocean.ts new file mode 100644 index 000000000..9eb9922dd --- /dev/null +++ b/examples-testing/examples/webgpu_ocean.ts @@ -0,0 +1,161 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { WaterMesh } from 'three/addons/objects/WaterMesh.js'; +import { SkyMesh } from 'three/addons/objects/SkyMesh.js'; + +let container, stats; +let camera, scene, renderer; +let controls, water, sun, mesh; + +init(); + +function init() { + container = document.getElementById('container'); + + // + + renderer = new THREE.WebGPURenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 0.5; + container.appendChild(renderer.domElement); + + // + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(55, window.innerWidth / window.innerHeight, 1, 20000); + camera.position.set(30, 30, 100); + + // + + sun = new THREE.Vector3(); + + // Water + + const waterGeometry = new THREE.PlaneGeometry(10000, 10000); + const loader = new THREE.TextureLoader(); + const waterNormals = loader.load('textures/waternormals.jpg'); + waterNormals.wrapS = waterNormals.wrapT = THREE.RepeatWrapping; + + water = new WaterMesh(waterGeometry, { + waterNormals: waterNormals, + sunDirection: new THREE.Vector3(), + sunColor: 0xffffff, + waterColor: 0x001e0f, + distortionScale: 3.7, + }); + + water.rotation.x = -Math.PI / 2; + + scene.add(water); + + // Skybox + + const sky = new SkyMesh(); + sky.scale.setScalar(10000); + scene.add(sky); + + sky.turbidity.value = 10; + sky.rayleigh.value = 2; + sky.mieCoefficient.value = 0.005; + sky.mieDirectionalG.value = 0.8; + + const parameters = { + elevation: 2, + azimuth: 180, + }; + + const pmremGenerator = new THREE.PMREMGenerator(renderer); + const sceneEnv = new THREE.Scene(); + + let renderTarget; + + function updateSun() { + const phi = THREE.MathUtils.degToRad(90 - parameters.elevation); + const theta = THREE.MathUtils.degToRad(parameters.azimuth); + + sun.setFromSphericalCoords(1, phi, theta); + + sky.sunPosition.value.copy(sun); + water.sunDirection.value.copy(sun).normalize(); + + if (renderTarget !== undefined) renderTarget.dispose(); + + sceneEnv.add(sky); + renderTarget = pmremGenerator.fromScene(sceneEnv); + scene.add(sky); + + scene.environment = renderTarget.texture; + } + + renderer.init().then(updateSun); + + // + + const geometry = new THREE.BoxGeometry(30, 30, 30); + const material = new THREE.MeshStandardMaterial({ roughness: 0 }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + controls = new OrbitControls(camera, renderer.domElement); + controls.maxPolarAngle = Math.PI * 0.495; + controls.target.set(0, 10, 0); + controls.minDistance = 40.0; + controls.maxDistance = 200.0; + controls.update(); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // GUI + + const gui = new GUI(); + + const folderSky = gui.addFolder('Sky'); + folderSky.add(parameters, 'elevation', 0, 90, 0.1).onChange(updateSun); + folderSky.add(parameters, 'azimuth', -180, 180, 0.1).onChange(updateSun); + folderSky.open(); + + const folderWater = gui.addFolder('Water'); + folderWater.add(water.distortionScale, 'value', 0, 8, 0.1).name('distortionScale'); + folderWater.add(water.size, 'value', 0.1, 10, 0.1).name('size'); + folderWater.open(); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + const time = performance.now() * 0.001; + + mesh.position.y = Math.sin(time) * 20 + 5; + mesh.rotation.x = time * 0.5; + mesh.rotation.z = time * 0.51; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_parallax_uv.ts b/examples-testing/examples/webgpu_parallax_uv.ts new file mode 100644 index 000000000..c11a18ed1 --- /dev/null +++ b/examples-testing/examples/webgpu_parallax_uv.ts @@ -0,0 +1,112 @@ +import * as THREE from 'three'; +import { texture, parallaxUV, uv } from 'three/tsl'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer; + +let controls; + +init(); + +async function init() { + // scene + + scene = new THREE.Scene(); + + // camera + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 50); + camera.position.set(10, 14, 10); + + // environment + + const environmentTexture = await new THREE.CubeTextureLoader() + .setPath('./textures/cube/Park2/') + .loadAsync(['posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg']); + + scene.environment = environmentTexture; + scene.background = environmentTexture; + + // textures + + const loader = new THREE.TextureLoader(); + + const topTexture = await loader.loadAsync('textures/ambientcg/Ice002_1K-JPG_Color.jpg'); + topTexture.colorSpace = THREE.SRGBColorSpace; + + const roughnessTexture = await loader.loadAsync('textures/ambientcg/Ice002_1K-JPG_Roughness.jpg'); + roughnessTexture.colorSpace = THREE.NoColorSpace; + + const normalTexture = await loader.loadAsync('textures/ambientcg/Ice002_1K-JPG_NormalGL.jpg'); + normalTexture.colorSpace = THREE.NoColorSpace; + + const displaceTexture = await loader.loadAsync('textures/ambientcg/Ice002_1K-JPG_Displacement.jpg'); + displaceTexture.colorSpace = THREE.NoColorSpace; + + // + + const bottomTexture = await loader.loadAsync('textures/ambientcg/Ice003_1K-JPG_Color.jpg'); + bottomTexture.colorSpace = THREE.SRGBColorSpace; + bottomTexture.wrapS = THREE.RepeatWrapping; + bottomTexture.wrapT = THREE.RepeatWrapping; + + // paralax effect + + const parallaxScale = 0.3; + const offsetUV = texture(displaceTexture).mul(parallaxScale); + + const parallaxUVOffset = parallaxUV(uv(), offsetUV); + const parallaxResult = texture(bottomTexture, parallaxUVOffset); + + const iceNode = texture(topTexture).overlay(parallaxResult); + + // material + + const material = new THREE.MeshStandardNodeMaterial(); + material.colorNode = iceNode.mul(5); // increase the color intensity to 5 ( contrast ) + material.roughnessNode = texture(roughnessTexture); + material.normalMap = normalTexture; + material.metalness = 0; + + const geometry = new THREE.BoxGeometry(10, 10, 10); + + const ground = new THREE.Mesh(geometry, material); + ground.rotateX(-Math.PI / 2); + scene.add(ground); + + // renderer + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ReinhardToneMapping; + renderer.toneMappingExposure = 6; + document.body.appendChild(renderer.domElement); + + // controls + + controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 0, 0); + controls.maxDistance = 40; + controls.minDistance = 10; + controls.autoRotate = true; + controls.autoRotateSpeed = -1; + controls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + controls.update(); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_postprocessing.ts b/examples-testing/examples/webgpu_postprocessing.ts new file mode 100644 index 000000000..d83642f25 --- /dev/null +++ b/examples-testing/examples/webgpu_postprocessing.ts @@ -0,0 +1,77 @@ +import * as THREE from 'three'; +import { pass } from 'three/tsl'; + +let camera, renderer, postProcessing; +let object; + +init(); + +function init() { + renderer = new THREE.WebGPURenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 400; + + const scene = new THREE.Scene(); + scene.fog = new THREE.Fog(0x000000, 1, 1000); + + object = new THREE.Object3D(); + scene.add(object); + + const geometry = new THREE.SphereGeometry(1, 4, 4); + const material = new THREE.MeshPhongMaterial({ color: 0xffffff, flatShading: true }); + + for (let i = 0; i < 100; i++) { + const mesh = new THREE.Mesh(geometry, material); + mesh.position.set(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5).normalize(); + mesh.position.multiplyScalar(Math.random() * 400); + mesh.rotation.set(Math.random() * 2, Math.random() * 2, Math.random() * 2); + mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50; + object.add(mesh); + } + + scene.add(new THREE.AmbientLight(0xcccccc)); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(1, 1, 1); + scene.add(light); + + // postprocessing + + postProcessing = new THREE.PostProcessing(renderer); + + const scenePass = pass(scene, camera); + const scenePassColor = scenePass.getTextureNode(); + + const dotScreenPass = scenePassColor.dotScreen(); + dotScreenPass.scale.value = 0.3; + + const rgbShiftPass = dotScreenPass.rgbShift(); + rgbShiftPass.amount.value = 0.001; + + postProcessing.outputNode = rgbShiftPass; + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + object.rotation.x += 0.005; + object.rotation.y += 0.01; + + postProcessing.render(); +} diff --git a/examples-testing/examples/webgpu_postprocessing_3dlut.ts b/examples-testing/examples/webgpu_postprocessing_3dlut.ts new file mode 100644 index 000000000..9d4ffa6e9 --- /dev/null +++ b/examples-testing/examples/webgpu_postprocessing_3dlut.ts @@ -0,0 +1,139 @@ +import * as THREE from 'three'; +import { pass, texture3D, uniform, renderOutput } from 'three/tsl'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; +import { LUTCubeLoader } from 'three/addons/loaders/LUTCubeLoader.js'; +import { LUT3dlLoader } from 'three/addons/loaders/LUT3dlLoader.js'; +import { LUTImageLoader } from 'three/addons/loaders/LUTImageLoader.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +const params = { + lut: 'Bourbon 64.CUBE', + intensity: 1, +}; + +const lutMap = { + 'Bourbon 64.CUBE': null, + 'Chemical 168.CUBE': null, + 'Clayton 33.CUBE': null, + 'Cubicle 99.CUBE': null, + 'Remy 24.CUBE': null, + 'Presetpro-Cinematic.3dl': null, + NeutralLUT: null, + 'B&WLUT': null, + NightLUT: null, +}; + +let gui; +let camera, scene, renderer; +let postProcessing, lutPass; + +init(); + +async function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); + camera.position.set(-1.8, 0.6, 2.7); + + scene = new THREE.Scene(); + + new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + scene.environment = texture; + + // model + + const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); + loader.load('DamagedHelmet.gltf', function (gltf) { + scene.add(gltf.scene); + }); + }); + + const lutCubeLoader = new LUTCubeLoader(); + const lutImageLoader = new LUTImageLoader(); + const lut3dlLoader = new LUT3dlLoader(); + + for (const name in lutMap) { + if (/\.CUBE$/i.test(name)) { + lutMap[name] = lutCubeLoader.loadAsync('luts/' + name); + } else if (/\LUT$/i.test(name)) { + lutMap[name] = lutImageLoader.loadAsync(`luts/${name}.png`); + } else { + lutMap[name] = lut3dlLoader.loadAsync('luts/' + name); + } + } + + const pendings = Object.values(lutMap); + await Promise.all(pendings); + + for (const name in lutMap) { + lutMap[name] = await lutMap[name]; + } + + renderer = new THREE.WebGPURenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + container.appendChild(renderer.domElement); + + // post processing + + postProcessing = new THREE.PostProcessing(renderer); + + // ignore default output color transform ( toneMapping and outputColorSpace ) + // use renderOutput() for control the sequence + + postProcessing.outputColorTransform = false; + + // scene pass + + const scenePass = pass(scene, camera); + const outputPass = renderOutput(scenePass); + + const lut = lutMap[params.lut]; + lutPass = outputPass.lut3D(texture3D(lut.texture3D), lut.texture3D.image.width, uniform(1)); + + postProcessing.outputNode = lutPass; + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 2; + controls.maxDistance = 10; + controls.target.set(0, 0, -0.2); + controls.update(); + + gui = new GUI(); + gui.add(params, 'lut', Object.keys(lutMap)); + gui.add(params, 'intensity').min(0).max(1); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + lutPass.intensityNode.value = params.intensity; + + if (lutMap[params.lut]) { + const lut = lutMap[params.lut]; + lutPass.lutNode.value = lut.texture3D; + lutPass.size.value = lut.texture3D.image.width; + } + + postProcessing.render(); +} diff --git a/examples-testing/examples/webgpu_postprocessing_afterimage.ts b/examples-testing/examples/webgpu_postprocessing_afterimage.ts new file mode 100644 index 000000000..4c3a1d66e --- /dev/null +++ b/examples-testing/examples/webgpu_postprocessing_afterimage.ts @@ -0,0 +1,70 @@ +import * as THREE from 'three'; +import { pass } from 'three/tsl'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer; +let mesh, postProcessing, combinedPass; + +const params = { + damp: 0.96, +}; + +init(); +createGUI(); + +function init() { + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 400; + + scene = new THREE.Scene(); + scene.fog = new THREE.Fog(0x000000, 1, 1000); + + const geometry = new THREE.TorusKnotGeometry(100, 30, 100, 16); + const material = new THREE.MeshNormalMaterial(); + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // postprocessing + + postProcessing = new THREE.PostProcessing(renderer); + + const scenePass = pass(scene, camera); + const scenePassColor = scenePass.getTextureNode(); + + combinedPass = scenePassColor; + combinedPass = combinedPass.afterImage(params.damp); + + postProcessing.outputNode = combinedPass; + + window.addEventListener('resize', onWindowResize); +} + +function createGUI() { + const gui = new GUI({ title: 'Damp setting' }); + gui.add(combinedPass.damp, 'value', 0, 1).step(0.001); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function render() { + mesh.rotation.x += 0.0075; + mesh.rotation.y += 0.015; + + postProcessing.render(); +} + +function animate() { + render(); +} diff --git a/examples-testing/examples/webgpu_postprocessing_ao.ts b/examples-testing/examples/webgpu_postprocessing_ao.ts new file mode 100644 index 000000000..432d641a0 --- /dev/null +++ b/examples-testing/examples/webgpu_postprocessing_ao.ts @@ -0,0 +1,204 @@ +import * as THREE from 'three'; +import { pass, mrt, output, transformedNormalView, texture, ao, denoise } from 'three/tsl'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; +import { SimplexNoise } from 'three/addons/math/SimplexNoise.js'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer, postProcessing, controls, clock, stats, mixer; + +let aoPass, denoisePass, blendPassAO, blendPassDenoise, scenePassColor; + +const params = { + distanceExponent: 1, + distanceFallOff: 1, + radius: 0.25, + scale: 1, + thickness: 1, + denoised: true, + enabled: true, + denoiseRadius: 5, + lumaPhi: 5, + depthPhi: 5, + normalPhi: 5, +}; + +init(); + +async function init() { + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 100); + camera.position.set(5, 2, 8); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xbfe3dd); + + clock = new THREE.Clock(); + + const hdrloader = new RGBELoader(); + const envMap = await hdrloader.loadAsync('textures/equirectangular/quarry_01_1k.hdr'); + envMap.mapping = THREE.EquirectangularReflectionMapping; + + scene.environment = envMap; + + renderer = new THREE.WebGPURenderer(); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + controls = new OrbitControls(camera, renderer.domElement); + controls.target.set(0, 0.5, 0); + controls.update(); + controls.enablePan = false; + controls.enableDamping = true; + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // + + postProcessing = new THREE.PostProcessing(renderer); + + const scenePass = pass(scene, camera); + scenePass.setMRT( + mrt({ + output: output, + normal: transformedNormalView, + }), + ); + + scenePassColor = scenePass.getTextureNode('output'); + const scenePassNormal = scenePass.getTextureNode('normal'); + const scenePassDepth = scenePass.getTextureNode('depth'); + + // ao + + aoPass = ao(scenePassDepth, scenePassNormal, camera); + blendPassAO = aoPass.getTextureNode().mul(scenePassColor); + + // denoise (optional) + + const noiseTexture = texture(generateNoise()); + denoisePass = denoise(aoPass.getTextureNode(), scenePassDepth, scenePassNormal, noiseTexture, camera); + blendPassDenoise = denoisePass.mul(scenePassColor); + + postProcessing.outputNode = blendPassDenoise; + + // + + const dracoLoader = new DRACOLoader(); + dracoLoader.setDecoderPath('jsm/libs/draco/'); + dracoLoader.setDecoderConfig({ type: 'js' }); + const loader = new GLTFLoader(); + loader.setDRACOLoader(dracoLoader); + loader.setPath('models/gltf/'); + + const gltf = await loader.loadAsync('LittlestTokyo.glb'); + + const model = gltf.scene; + model.position.set(1, 1, 0); + model.scale.set(0.01, 0.01, 0.01); + scene.add(model); + + mixer = new THREE.AnimationMixer(model); + mixer.clipAction(gltf.animations[0]).play(); + + window.addEventListener('resize', onWindowResize); + + // + + const gui = new GUI(); + gui.title('AO settings'); + gui.add(params, 'distanceExponent').min(1).max(4).onChange(updateParameters); + gui.add(params, 'distanceFallOff').min(0.01).max(1).onChange(updateParameters); + gui.add(params, 'radius').min(0.01).max(1).onChange(updateParameters); + gui.add(params, 'scale').min(0.01).max(2).onChange(updateParameters); + gui.add(params, 'thickness').min(0.01).max(2).onChange(updateParameters); + gui.add(params, 'denoised').onChange(updatePassChain); + gui.add(params, 'enabled').onChange(updatePassChain); + const folder = gui.addFolder('Denoise settings'); + folder.add(params, 'denoiseRadius').min(0.01).max(10).name('radius').onChange(updateParameters); + folder.add(params, 'lumaPhi').min(0.01).max(10).onChange(updateParameters); + folder.add(params, 'depthPhi').min(0.01).max(10).onChange(updateParameters); + folder.add(params, 'normalPhi').min(0.01).max(10).onChange(updateParameters); +} + +function updatePassChain() { + if (params.enabled === true) { + if (params.denoised === true) { + postProcessing.outputNode = blendPassDenoise; + } else { + postProcessing.outputNode = blendPassAO; + } + } else { + postProcessing.outputNode = scenePassColor; + } + + postProcessing.needsUpdate = true; +} + +function updateParameters() { + aoPass.distanceExponent.value = params.distanceExponent; + aoPass.distanceFallOff.value = params.distanceFallOff; + aoPass.radius.value = params.radius; + aoPass.scale.value = params.scale; + aoPass.thickness.value = params.thickness; + + denoisePass.radius.value = params.denoiseRadius; + denoisePass.lumaPhi.value = params.lumaPhi; + denoisePass.depthPhi.value = params.depthPhi; + denoisePass.normalPhi.value = params.normalPhi; +} + +function generateNoise(size = 64) { + const simplex = new SimplexNoise(); + + const arraySize = size * size * 4; + const data = new Uint8Array(arraySize); + + for (let i = 0; i < size; i++) { + for (let j = 0; j < size; j++) { + const x = i; + const y = j; + + data[(i * size + j) * 4] = (simplex.noise(x, y) * 0.5 + 0.5) * 255; + data[(i * size + j) * 4 + 1] = (simplex.noise(x + size, y) * 0.5 + 0.5) * 255; + data[(i * size + j) * 4 + 2] = (simplex.noise(x, y + size) * 0.5 + 0.5) * 255; + data[(i * size + j) * 4 + 3] = (simplex.noise(x + size, y + size) * 0.5 + 0.5) * 255; + } + } + + const noiseTexture = new THREE.DataTexture(data, size, size); + noiseTexture.wrapS = THREE.RepeatWrapping; + noiseTexture.wrapT = THREE.RepeatWrapping; + noiseTexture.needsUpdate = true; + + return noiseTexture; +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); +} + +function animate() { + const delta = clock.getDelta(); + + if (mixer) { + mixer.update(delta); + } + + controls.update(); + + postProcessing.render(); + stats.update(); +} diff --git a/examples-testing/examples/webgpu_postprocessing_bloom.ts b/examples-testing/examples/webgpu_postprocessing_bloom.ts new file mode 100644 index 000000000..d38a7abb1 --- /dev/null +++ b/examples-testing/examples/webgpu_postprocessing_bloom.ts @@ -0,0 +1,127 @@ +import * as THREE from 'three'; +import { pass, bloom } from 'three/tsl'; + +import Stats from 'three/addons/libs/stats.module.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +let camera, stats; +let postProcessing, renderer, mixer, clock; + +const params = { + threshold: 0, + strength: 1, + radius: 0, + exposure: 1, +}; + +init(); + +async function init() { + const container = document.getElementById('container'); + + clock = new THREE.Clock(); + + const scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 100); + camera.position.set(-5, 2.5, -3.5); + scene.add(camera); + + scene.add(new THREE.AmbientLight(0xcccccc)); + + const pointLight = new THREE.PointLight(0xffffff, 100); + camera.add(pointLight); + + const loader = new GLTFLoader(); + const gltf = await loader.loadAsync('models/gltf/PrimaryIonDrive.glb'); + + const model = gltf.scene; + scene.add(model); + + mixer = new THREE.AnimationMixer(model); + const clip = gltf.animations[0]; + mixer.clipAction(clip.optimize()).play(); + + // + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ReinhardToneMapping; + container.appendChild(renderer.domElement); + + // + + postProcessing = new THREE.PostProcessing(renderer); + + const scenePass = pass(scene, camera); + const scenePassColor = scenePass.getTextureNode('output'); + + const bloomPass = bloom(scenePassColor); + + postProcessing.outputNode = scenePassColor.add(bloomPass); + + // + + stats = new Stats(); + container.appendChild(stats.dom); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.maxPolarAngle = Math.PI * 0.5; + controls.minDistance = 3; + controls.maxDistance = 8; + + // + + const gui = new GUI(); + + const bloomFolder = gui.addFolder('bloom'); + + bloomFolder.add(params, 'threshold', 0.0, 1.0).onChange(function (value) { + bloomPass.threshold.value = value; + }); + + bloomFolder.add(params, 'strength', 0.0, 3.0).onChange(function (value) { + bloomPass.strength.value = value; + }); + + gui.add(params, 'radius', 0.0, 1.0) + .step(0.01) + .onChange(function (value) { + bloomPass.radius.value = value; + }); + + const toneMappingFolder = gui.addFolder('tone mapping'); + + toneMappingFolder.add(params, 'exposure', 0.1, 2).onChange(function (value) { + renderer.toneMappingExposure = Math.pow(value, 4.0); + }); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); +} + +function animate() { + const delta = clock.getDelta(); + + mixer.update(delta); + + postProcessing.render(); + + stats.update(); +} diff --git a/examples-testing/examples/webgpu_postprocessing_bloom_emissive.ts b/examples-testing/examples/webgpu_postprocessing_bloom_emissive.ts new file mode 100644 index 000000000..971868360 --- /dev/null +++ b/examples-testing/examples/webgpu_postprocessing_bloom_emissive.ts @@ -0,0 +1,101 @@ +import * as THREE from 'three'; +import { pass, mrt, output, emissive } from 'three/tsl'; + +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer; +let postProcessing; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + // + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); + camera.position.set(-1.8, 0.6, 2.7); + + scene = new THREE.Scene(); + + new RGBELoader().setPath('textures/equirectangular/').load('moonless_golf_1k.hdr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + scene.environment = texture; + + // model + + const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); + loader.load('DamagedHelmet.gltf', function (gltf) { + scene.add(gltf.scene); + }); + }); + + // + + renderer = new THREE.WebGPURenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(render); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + container.appendChild(renderer.domElement); + + // + + const scenePass = pass(scene, camera); + scenePass.setMRT( + mrt({ + output, + emissive, + }), + ); + + const outputPass = scenePass.getTextureNode(); + const emissivePass = scenePass.getTextureNode('emissive'); + + const bloomPass = emissivePass.bloom(2.5, 0.5); + + postProcessing = new THREE.PostProcessing(renderer); + postProcessing.outputColorTransform = false; + postProcessing.outputNode = outputPass.add(bloomPass).renderOutput(); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 2; + controls.maxDistance = 10; + controls.target.set(0, 0, -0.2); + + window.addEventListener('resize', onWindowResize); + + // + + const gui = new GUI(); + + const bloomFolder = gui.addFolder('bloom'); + bloomFolder.add(bloomPass.strength, 'value', 0.0, 5.0).name('strength'); + bloomFolder.add(bloomPass.radius, 'value', 0.0, 1.0).name('radius'); + + const toneMappingFolder = gui.addFolder('tone mapping'); + toneMappingFolder.add(renderer, 'toneMappingExposure', 0.1, 2).name('exposure'); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function render() { + postProcessing.render(); +} diff --git a/examples-testing/examples/webgpu_postprocessing_bloom_selective.ts b/examples-testing/examples/webgpu_postprocessing_bloom_selective.ts new file mode 100644 index 000000000..7140a8ad2 --- /dev/null +++ b/examples-testing/examples/webgpu_postprocessing_bloom_selective.ts @@ -0,0 +1,122 @@ +import * as THREE from 'three'; +import { pass, mrt, output, float, uniform } from 'three/tsl'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +// scene + +const scene = new THREE.Scene(); + +const camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 200); +camera.position.set(0, 0, 20); +camera.lookAt(0, 0, 0); + +const geometry = new THREE.IcosahedronGeometry(1, 15); + +for (let i = 0; i < 50; i++) { + const color = new THREE.Color(); + color.setHSL(Math.random(), 0.7, Math.random() * 0.2 + 0.05); + + const bloomIntensity = Math.random() > 0.5 ? 1 : 0; + + const material = new THREE.MeshBasicNodeMaterial({ color: color }); + material.mrtNode = mrt({ + bloomIntensity: uniform(bloomIntensity), + }); + + const sphere = new THREE.Mesh(geometry, material); + sphere.position.x = Math.random() * 10 - 5; + sphere.position.y = Math.random() * 10 - 5; + sphere.position.z = Math.random() * 10 - 5; + sphere.position.normalize().multiplyScalar(Math.random() * 4.0 + 2.0); + sphere.scale.setScalar(Math.random() * Math.random() + 0.5); + scene.add(sphere); +} + +// renderer + +const renderer = new THREE.WebGPURenderer(); +renderer.setPixelRatio(window.devicePixelRatio); +renderer.setSize(window.innerWidth, window.innerHeight); +renderer.setAnimationLoop(animate); +renderer.toneMapping = THREE.NeutralToneMapping; +document.body.appendChild(renderer.domElement); + +// post processing + +const scenePass = pass(scene, camera); +scenePass.setMRT( + mrt({ + output, + bloomIntensity: float(0), // default bloom intensity + }), +); + +const outputPass = scenePass.getTextureNode(); +const bloomIntensityPass = scenePass.getTextureNode('bloomIntensity'); + +const bloomPass = outputPass.mul(bloomIntensityPass).bloom(); + +const postProcessing = new THREE.PostProcessing(renderer); +postProcessing.outputColorTransform = false; +postProcessing.outputNode = outputPass.add(bloomPass).renderOutput(); + +// controls + +const controls = new OrbitControls(camera, renderer.domElement); +controls.maxPolarAngle = Math.PI * 0.5; +controls.minDistance = 1; +controls.maxDistance = 100; + +// raycaster + +const raycaster = new THREE.Raycaster(); +const mouse = new THREE.Vector2(); + +window.addEventListener('pointerdown', event => { + mouse.x = (event.clientX / window.innerWidth) * 2 - 1; + mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; + + raycaster.setFromCamera(mouse, camera); + + const intersects = raycaster.intersectObjects(scene.children, false); + + if (intersects.length > 0) { + const material = intersects[0].object.material; + + const bloomIntensity = material.mrtNode.getNode('bloomIntensity'); + bloomIntensity.value = bloomIntensity.value === 0 ? 1 : 0; + } +}); + +// gui + +const gui = new GUI(); + +const bloomFolder = gui.addFolder('bloom'); +bloomFolder.add(bloomPass.threshold, 'value', 0.0, 1.0).name('threshold'); +bloomFolder.add(bloomPass.strength, 'value', 0.0, 3).name('strength'); +bloomFolder.add(bloomPass.radius, 'value', 0.0, 1.0).name('radius'); + +const toneMappingFolder = gui.addFolder('tone mapping'); +toneMappingFolder.add(renderer, 'toneMappingExposure', 0.1, 3).name('exposure'); + +// events + +window.onresize = function () { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); +}; + +// animate + +function animate() { + postProcessing.render(); +} diff --git a/examples-testing/examples/webgpu_postprocessing_difference.ts b/examples-testing/examples/webgpu_postprocessing_difference.ts new file mode 100644 index 000000000..49f9084fc --- /dev/null +++ b/examples-testing/examples/webgpu_postprocessing_difference.ts @@ -0,0 +1,92 @@ +import * as THREE from 'three'; +import { pass, luminance } from 'three/tsl'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { Timer } from 'three/addons/misc/Timer.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +const params = { + speed: 0, +}; + +let camera, renderer, postProcessing; +let timer, mesh, controls; + +init(); + +function init() { + renderer = new THREE.WebGPURenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.NeutralToneMapping; + document.body.appendChild(renderer.domElement); + + // + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 100); + camera.position.set(1, 2, 3); + + const scene = new THREE.Scene(); + scene.fog = new THREE.Fog(0x0487e2, 7, 25); + scene.background = new THREE.Color(0x0487e2); + + timer = new Timer(); + + const texture = new THREE.TextureLoader().load('textures/crate.gif'); + texture.colorSpace = THREE.SRGBColorSpace; + + const geometry = new THREE.BoxGeometry(); + const material = new THREE.MeshBasicMaterial({ map: texture }); + + mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // post processing + + postProcessing = new THREE.PostProcessing(renderer); + + const scenePass = pass(scene, camera); + + const currentTexture = scenePass.getTextureNode(); + const previousTexture = scenePass.getPreviousTextureNode(); + + const frameDiff = previousTexture.sub(currentTexture).abs(); + + const saturationAmount = luminance(frameDiff).mul(1000).clamp(0, 3); + + postProcessing.outputNode = currentTexture.saturation(saturationAmount); + + // + + controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 2; + controls.maxDistance = 10; + controls.enableDamping = true; + controls.dampingFactor = 0.01; + + window.addEventListener('resize', onWindowResize); + + // + + const gui = new GUI(); + gui.add(params, 'speed', 0, 2); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + timer.update(); + + controls.update(); + + mesh.rotation.y += timer.getDelta() * 5 * params.speed; + + postProcessing.render(); +} diff --git a/examples-testing/examples/webgpu_postprocessing_dof.ts b/examples-testing/examples/webgpu_postprocessing_dof.ts new file mode 100644 index 000000000..3fb4046be --- /dev/null +++ b/examples-testing/examples/webgpu_postprocessing_dof.ts @@ -0,0 +1,159 @@ +import * as THREE from 'three'; +import { cubeTexture, positionWorld, oscSine, timerGlobal, pass, uniform } from 'three/tsl'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import Stats from 'three/addons/libs/stats.module.js'; + +// + +let camera, scene, renderer, mesh, stats; + +let mouseX = 0, + mouseY = 0; + +let windowHalfX = window.innerWidth / 2; +let windowHalfY = window.innerHeight / 2; + +let width = window.innerWidth; +let height = window.innerHeight; + +let postProcessing; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(70, width / height, 1, 3000); + camera.position.z = 200; + + scene = new THREE.Scene(); + + const path = 'textures/cube/SwedishRoyalCastle/'; + const format = '.jpg'; + const urls = [ + path + 'px' + format, + path + 'nx' + format, + path + 'py' + format, + path + 'ny' + format, + path + 'pz' + format, + path + 'nz' + format, + ]; + + const xgrid = 14, + ygrid = 9, + zgrid = 14; + const count = xgrid * ygrid * zgrid; + + const textureCube = new THREE.CubeTextureLoader().load(urls); + const cubeTextureNode = cubeTexture(textureCube); + const oscPos = oscSine(positionWorld.div(1000 /* scene distance */).add(timerGlobal(0.2 /* speed */))); + + const geometry = new THREE.SphereGeometry(60, 20, 10); + const material = new THREE.MeshBasicNodeMaterial(); + material.colorNode = cubeTextureNode.mul(oscPos); + + mesh = new THREE.InstancedMesh(geometry, material, count); + scene.add(mesh); + + const matrix = new THREE.Matrix4(); + + let index = 0; + + for (let i = 0; i < xgrid; i++) { + for (let j = 0; j < ygrid; j++) { + for (let k = 0; k < zgrid; k++) { + const x = 200 * (i - xgrid / 2); + const y = 200 * (j - ygrid / 2); + const z = 200 * (k - zgrid / 2); + + mesh.setMatrixAt(index, matrix.identity().setPosition(x, y, z)); + index++; + } + } + } + + // renderer + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + const effectController = { + focus: uniform(500.0), + aperture: uniform(5), + maxblur: uniform(0.01), + }; + + // post processing + + postProcessing = new THREE.PostProcessing(renderer); + + const scenePass = pass(scene, camera); + + const scenePassColor = scenePass.getTextureNode(); + const scenePassViewZ = scenePass.getViewZNode(); + + const dofPass = scenePassColor.dof( + scenePassViewZ, + effectController.focus, + effectController.aperture.mul(0.00001), + effectController.maxblur, + ); + + postProcessing.outputNode = dofPass; + + // controls + + renderer.domElement.style.touchAction = 'none'; + renderer.domElement.addEventListener('pointermove', onPointerMove); + + window.addEventListener('resize', onWindowResize); + + // stats + + stats = new Stats(); + document.body.appendChild(stats.dom); + + // gui + + const gui = new GUI(); + gui.add(effectController.focus, 'value', 10.0, 3000.0, 10).name('focus'); + gui.add(effectController.aperture, 'value', 0, 10, 0.1).name('aperture'); + gui.add(effectController.maxblur, 'value', 0.0, 0.01, 0.001).name('maxblur'); +} + +function onPointerMove(event) { + if (event.isPrimary === false) return; + + mouseX = event.clientX - windowHalfX; + mouseY = event.clientY - windowHalfY; +} + +function onWindowResize() { + windowHalfX = window.innerWidth / 2; + windowHalfY = window.innerHeight / 2; + + width = window.innerWidth; + height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); +} + +function animate() { + render(); + stats.update(); +} + +function render() { + camera.position.x += (mouseX - camera.position.x) * 0.036; + camera.position.y += (-mouseY - camera.position.y) * 0.036; + + camera.lookAt(scene.position); + + postProcessing.render(); +} diff --git a/examples-testing/examples/webgpu_postprocessing_fxaa.ts b/examples-testing/examples/webgpu_postprocessing_fxaa.ts new file mode 100644 index 000000000..5e75fa7a9 --- /dev/null +++ b/examples-testing/examples/webgpu_postprocessing_fxaa.ts @@ -0,0 +1,122 @@ +import * as THREE from 'three'; +import { pass, renderOutput } from 'three/tsl'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +const params = { + enabled: true, + animated: false, +}; + +let camera, scene, renderer, clock, group; +let postProcessing; + +init(); + +async function init() { + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200); + camera.position.z = 50; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0xffffff); + + clock = new THREE.Clock(); + + // + + const hemiLight = new THREE.HemisphereLight(0xffffff, 0x8d8d8d); + hemiLight.position.set(0, 1000, 0); + scene.add(hemiLight); + + const dirLight = new THREE.DirectionalLight(0xffffff, 3); + dirLight.position.set(-3000, 1000, -1000); + scene.add(dirLight); + + // + + group = new THREE.Group(); + + const geometry = new THREE.TetrahedronGeometry(); + const material = new THREE.MeshStandardMaterial({ color: 0xf73232, flatShading: true }); + + for (let i = 0; i < 100; i++) { + const mesh = new THREE.Mesh(geometry, material); + + mesh.position.x = Math.random() * 50 - 25; + mesh.position.y = Math.random() * 50 - 25; + mesh.position.z = Math.random() * 50 - 25; + + mesh.scale.setScalar(Math.random() * 2 + 1); + + mesh.rotation.x = Math.random() * Math.PI; + mesh.rotation.y = Math.random() * Math.PI; + mesh.rotation.z = Math.random() * Math.PI; + + group.add(mesh); + } + + scene.add(group); + + renderer = new THREE.WebGPURenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // post processing + + postProcessing = new THREE.PostProcessing(renderer); + + // ignore default output color transform ( toneMapping and outputColorSpace ) + // use renderOutput() for control the sequence + + postProcessing.outputColorTransform = false; + + // scene pass + + const scenePass = pass(scene, camera); + const outputPass = renderOutput(scenePass); + + // FXAA must be computed in sRGB color space (so after tone mapping and color space conversion) + + const fxaaPass = outputPass.fxaa(); + postProcessing.outputNode = fxaaPass; + + // + + window.addEventListener('resize', onWindowResize); + + // + + const gui = new GUI(); + gui.title('FXAA settings'); + gui.add(params, 'enabled').onChange(value => { + if (value === true) { + postProcessing.outputNode = fxaaPass; + } else { + postProcessing.outputNode = outputPass; + } + + postProcessing.needsUpdate = true; + }); + gui.add(params, 'animated'); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + const delta = clock.getDelta(); + + if (params.animated === true) { + group.rotation.y += delta * 0.1; + } + + postProcessing.render(); +} diff --git a/examples-testing/examples/webgpu_postprocessing_masking.ts b/examples-testing/examples/webgpu_postprocessing_masking.ts new file mode 100644 index 000000000..a4f56b170 --- /dev/null +++ b/examples-testing/examples/webgpu_postprocessing_masking.ts @@ -0,0 +1,85 @@ +import * as THREE from 'three'; +import { pass, texture } from 'three/tsl'; + +let camera, postProcessing, renderer; +let box, torus; + +init(); + +function init() { + // scene + + const baseScene = new THREE.Scene(); + baseScene.background = new THREE.Color(0xe0e0e0); + + const maskScene1 = new THREE.Scene(); + box = new THREE.Mesh(new THREE.BoxGeometry(4, 4, 4)); + maskScene1.add(box); + + const maskScene2 = new THREE.Scene(); + torus = new THREE.Mesh(new THREE.TorusGeometry(3, 1, 16, 32)); + maskScene2.add(torus); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); + camera.position.z = 10; + + // textures + + const texture1 = new THREE.TextureLoader().load('textures/758px-Canestra_di_frutta_(Caravaggio).jpg'); + texture1.colorSpace = THREE.SRGBColorSpace; + texture1.minFilter = THREE.LinearFilter; + texture1.flipY = false; + + const texture2 = new THREE.TextureLoader().load('textures/2294472375_24a3b8ef46_o.jpg'); + texture2.colorSpace = THREE.SRGBColorSpace; + texture2.flipY = false; + + // renderer + + renderer = new THREE.WebGPURenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); + + // post processing + + const base = pass(baseScene, camera); + const sceneMask1 = pass(maskScene1, camera).a; + const sceneMask2 = pass(maskScene2, camera).a; + + let compose = base; + compose = sceneMask1.mix(compose, texture(texture1)); + compose = sceneMask2.mix(compose, texture(texture2)); + + postProcessing = new THREE.PostProcessing(renderer); + postProcessing.outputNode = compose; +} + +function onWindowResize() { + const width = window.innerWidth; + const height = window.innerHeight; + + camera.aspect = width / height; + camera.updateProjectionMatrix(); + + renderer.setSize(width, height); +} + +function animate() { + const time = performance.now() * 0.001 + 6000; + + box.position.x = Math.cos(time / 1.5) * 2; + box.position.y = Math.sin(time) * 2; + box.rotation.x = time; + box.rotation.y = time / 2; + + torus.position.x = Math.cos(time) * 2; + torus.position.y = Math.sin(time / 1.5) * 2; + torus.rotation.x = time; + torus.rotation.y = time / 2; + + postProcessing.render(); +} diff --git a/examples-testing/examples/webgpu_postprocessing_pixel.ts b/examples-testing/examples/webgpu_postprocessing_pixel.ts new file mode 100644 index 000000000..d7e51008d --- /dev/null +++ b/examples-testing/examples/webgpu_postprocessing_pixel.ts @@ -0,0 +1,234 @@ +import * as THREE from 'three'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +import { uniform, pixelationPass } from 'three/tsl'; + +let camera, scene, renderer, postProcessing, crystalMesh, clock; +let gui, effectController; + +init(); + +function init() { + const aspectRatio = window.innerWidth / window.innerHeight; + + camera = new THREE.OrthographicCamera(-aspectRatio, aspectRatio, 1, -1, 0.1, 10); + camera.position.y = 2 * Math.tan(Math.PI / 6); + camera.position.z = 2; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x151729); + + clock = new THREE.Clock(); + + // textures + + const loader = new THREE.TextureLoader(); + const texChecker = pixelTexture(loader.load('textures/checker.png')); + const texChecker2 = pixelTexture(loader.load('textures/checker.png')); + texChecker.repeat.set(3, 3); + texChecker2.repeat.set(1.5, 1.5); + + // meshes + + const boxMaterial = new THREE.MeshPhongMaterial({ map: texChecker2 }); + + function addBox(boxSideLength, x, z, rotation) { + const mesh = new THREE.Mesh(new THREE.BoxGeometry(boxSideLength, boxSideLength, boxSideLength), boxMaterial); + mesh.castShadow = true; + mesh.receiveShadow = true; + mesh.rotation.y = rotation; + mesh.position.y = boxSideLength / 2; + mesh.position.set(x, boxSideLength / 2 + 0.0001, z); + scene.add(mesh); + return mesh; + } + + addBox(0.4, 0, 0, Math.PI / 4); + addBox(0.5, -0.5, -0.5, Math.PI / 4); + + const planeSideLength = 2; + const planeMesh = new THREE.Mesh( + new THREE.PlaneGeometry(planeSideLength, planeSideLength), + new THREE.MeshPhongMaterial({ map: texChecker }), + ); + planeMesh.receiveShadow = true; + planeMesh.rotation.x = -Math.PI / 2; + scene.add(planeMesh); + + const radius = 0.2; + const geometry = new THREE.IcosahedronGeometry(radius); + crystalMesh = new THREE.Mesh( + geometry, + new THREE.MeshPhongMaterial({ + color: 0x68b7e9, + emissive: 0x4f7e8b, + shininess: 10, + specular: 0xffffff, + }), + ); + crystalMesh.receiveShadow = true; + crystalMesh.castShadow = true; + scene.add(crystalMesh); + + // lights + + scene.add(new THREE.AmbientLight(0x757f8e, 3)); + + const directionalLight = new THREE.DirectionalLight(0xfffecd, 1.5); + directionalLight.position.set(100, 100, 100); + directionalLight.castShadow = true; + directionalLight.shadow.mapSize.set(2048, 2048); + directionalLight.shadow.bias = -0.0001; + scene.add(directionalLight); + + const spotLight = new THREE.SpotLight(0xffc100, 10, 10, Math.PI / 16, 0.02, 2); + spotLight.position.set(2, 2, 0); + const target = spotLight.target; + scene.add(target); + target.position.set(0, 0, 0); + spotLight.castShadow = true; + spotLight.shadow.bias = -0.001; + scene.add(spotLight); + + renderer = new THREE.WebGPURenderer(); + renderer.shadowMap.enabled = true; + renderer.shadowMap.type = THREE.BasicShadowMap; + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + effectController = { + pixelSize: uniform(6), + normalEdgeStrength: uniform(0.3), + depthEdgeStrength: uniform(0.4), + pixelAlignedPanning: true, + }; + + postProcessing = new THREE.PostProcessing(renderer); + const scenePass = pixelationPass( + scene, + camera, + effectController.pixelSize, + effectController.normalEdgeStrength, + effectController.depthEdgeStrength, + ); + postProcessing.outputNode = scenePass; + + window.addEventListener('resize', onWindowResize); + + const controls = new OrbitControls(camera, renderer.domElement); + controls.maxZoom = 2; + + // gui + + gui = new GUI(); + gui.add(effectController.pixelSize, 'value', 1, 16, 1).name('Pixel Size'); + gui.add(effectController.normalEdgeStrength, 'value', 0, 2, 0.05).name('Normal Edge Strength'); + gui.add(effectController.depthEdgeStrength, 'value', 0, 1, 0.05).name('Depth Edge Strength'); + gui.add(effectController, 'pixelAlignedPanning'); +} + +function onWindowResize() { + const aspectRatio = window.innerWidth / window.innerHeight; + camera.left = -aspectRatio; + camera.right = aspectRatio; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const t = clock.getElapsedTime(); + + crystalMesh.material.emissiveIntensity = Math.sin(t * 3) * 0.5 + 0.5; + crystalMesh.position.y = 0.7 + Math.sin(t * 2) * 0.05; + crystalMesh.rotation.y = stopGoEased(t, 2, 4) * 2 * Math.PI; + + const rendererSize = renderer.getSize(new THREE.Vector2()); + const aspectRatio = rendererSize.x / rendererSize.y; + + if (effectController.pixelAlignedPanning) { + const pixelSize = effectController.pixelSize.value; + + pixelAlignFrustum( + camera, + aspectRatio, + Math.floor(rendererSize.x / pixelSize), + Math.floor(rendererSize.y / pixelSize), + ); + } else if (camera.left != -aspectRatio || camera.top != 1.0) { + // Reset the Camera Frustum if it has been modified + camera.left = -aspectRatio; + camera.right = aspectRatio; + camera.top = 1.0; + camera.bottom = -1.0; + camera.updateProjectionMatrix(); + } + + postProcessing.render(); +} + +// Helper functions + +function pixelTexture(texture) { + texture.minFilter = THREE.NearestFilter; + texture.magFilter = THREE.NearestFilter; + texture.generateMipmaps = false; + texture.wrapS = THREE.RepeatWrapping; + texture.wrapT = THREE.RepeatWrapping; + texture.colorSpace = THREE.SRGBColorSpace; + return texture; +} + +function easeInOutCubic(x) { + return x ** 2 * 3 - x ** 3 * 2; +} + +function linearStep(x, edge0, edge1) { + const w = edge1 - edge0; + const m = 1 / w; + const y0 = -m * edge0; + return THREE.MathUtils.clamp(y0 + m * x, 0, 1); +} + +function stopGoEased(x, downtime, period) { + const cycle = (x / period) | 0; + const tween = x - cycle * period; + const linStep = easeInOutCubic(linearStep(tween, downtime, period)); + return cycle + linStep; +} + +function pixelAlignFrustum(camera, aspectRatio, pixelsPerScreenWidth, pixelsPerScreenHeight) { + // 0. Get Pixel Grid Units + const worldScreenWidth = (camera.right - camera.left) / camera.zoom; + const worldScreenHeight = (camera.top - camera.bottom) / camera.zoom; + const pixelWidth = worldScreenWidth / pixelsPerScreenWidth; + const pixelHeight = worldScreenHeight / pixelsPerScreenHeight; + + // 1. Project the current camera position along its local rotation bases + const camPos = new THREE.Vector3(); + camera.getWorldPosition(camPos); + const camRot = new THREE.Quaternion(); + camera.getWorldQuaternion(camRot); + const camRight = new THREE.Vector3(1.0, 0.0, 0.0).applyQuaternion(camRot); + const camUp = new THREE.Vector3(0.0, 1.0, 0.0).applyQuaternion(camRot); + const camPosRight = camPos.dot(camRight); + const camPosUp = camPos.dot(camUp); + + // 2. Find how far along its position is along these bases in pixel units + const camPosRightPx = camPosRight / pixelWidth; + const camPosUpPx = camPosUp / pixelHeight; + + // 3. Find the fractional pixel units and convert to world units + const fractX = camPosRightPx - Math.round(camPosRightPx); + const fractY = camPosUpPx - Math.round(camPosUpPx); + + // 4. Add fractional world units to the left/right top/bottom to align with the pixel grid + camera.left = -aspectRatio - fractX * pixelWidth; + camera.right = aspectRatio - fractX * pixelWidth; + camera.top = 1.0 - fractY * pixelHeight; + camera.bottom = -1.0 - fractY * pixelHeight; + camera.updateProjectionMatrix(); +} diff --git a/examples-testing/examples/webgpu_postprocessing_sobel.ts b/examples-testing/examples/webgpu_postprocessing_sobel.ts new file mode 100644 index 000000000..cbc5a96ba --- /dev/null +++ b/examples-testing/examples/webgpu_postprocessing_sobel.ts @@ -0,0 +1,89 @@ +import * as THREE from 'three'; +import { pass } from 'three/tsl'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer; +let postProcessing; + +const params = { + enable: true, +}; + +init(); + +function init() { + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(0, 1, 3); + camera.lookAt(scene.position); + + // + + const ambientLight = new THREE.AmbientLight(0xe7e7e7); + scene.add(ambientLight); + + const pointLight = new THREE.PointLight(0xffffff, 20); + camera.add(pointLight); + scene.add(camera); + + // + + const geometry = new THREE.TorusKnotGeometry(1, 0.3, 256, 32); + const material = new THREE.MeshPhongMaterial({ color: 0xffff00 }); + + const mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.outputColorSpace = THREE.LinearSRGBColorSpace; + document.body.appendChild(renderer.domElement); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.enableZoom = false; + + // postprocessing + + postProcessing = new THREE.PostProcessing(renderer); + + const scenePass = pass(scene, camera); + const scenePassColor = scenePass.getTextureNode(); + + postProcessing.outputNode = scenePassColor.sobel(); + + // + + const gui = new GUI(); + + gui.add(params, 'enable'); + gui.open(); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + if (params.enable === true) { + postProcessing.render(); + } else { + renderer.render(scene, camera); + } +} diff --git a/examples-testing/examples/webgpu_postprocessing_transition.ts b/examples-testing/examples/webgpu_postprocessing_transition.ts new file mode 100644 index 000000000..b66bad12c --- /dev/null +++ b/examples-testing/examples/webgpu_postprocessing_transition.ts @@ -0,0 +1,200 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import TWEEN from 'three/addons/libs/tween.module.js'; +import { uniform, transition, pass } from 'three/tsl'; + +let renderer, postProcessing, transitionController, transitionPass; + +const textures = []; +const clock = new THREE.Clock(); + +const effectController = { + animateScene: true, + animateTransition: true, + transition: 0, + _transition: uniform(0), + useTexture: true, + _useTexture: uniform(1), + texture: 5, + cycle: true, + threshold: uniform(0.1), +}; + +function generateInstancedMesh(geometry, material, count) { + const mesh = new THREE.InstancedMesh(geometry, material, count); + + const dummy = new THREE.Object3D(); + const color = new THREE.Color(); + + for (let i = 0; i < count; i++) { + dummy.position.x = Math.random() * 100 - 50; + dummy.position.y = Math.random() * 60 - 30; + dummy.position.z = Math.random() * 80 - 40; + + dummy.rotation.x = Math.random() * 2 * Math.PI; + dummy.rotation.y = Math.random() * 2 * Math.PI; + dummy.rotation.z = Math.random() * 2 * Math.PI; + + dummy.scale.x = Math.random() * 2 + 1; + + if (geometry.type === 'BoxGeometry') { + dummy.scale.y = Math.random() * 2 + 1; + dummy.scale.z = Math.random() * 2 + 1; + } else { + dummy.scale.y = dummy.scale.x; + dummy.scale.z = dummy.scale.x; + } + + dummy.updateMatrix(); + + mesh.setMatrixAt(i, dummy.matrix); + mesh.setColorAt(i, color.setScalar(0.1 + 0.9 * Math.random())); + } + + return mesh; +} + +function FXScene(geometry, rotationSpeed, backgroundColor) { + const camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.z = 20; + + // Setup scene + const scene = new THREE.Scene(); + scene.background = new THREE.Color(backgroundColor); + scene.add(new THREE.AmbientLight(0xaaaaaa, 3)); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0, 1, 4); + scene.add(light); + + this.rotationSpeed = rotationSpeed; + + const color = geometry.type === 'BoxGeometry' ? 0x0000ff : 0xff0000; + const material = new THREE.MeshPhongNodeMaterial({ color: color, flatShading: true }); + const mesh = generateInstancedMesh(geometry, material, 500); + scene.add(mesh); + + this.scene = scene; + this.camera = camera; + this.mesh = mesh; + + this.update = function (delta) { + if (effectController.animateScene) { + mesh.rotation.x += this.rotationSpeed.x * delta; + mesh.rotation.y += this.rotationSpeed.y * delta; + mesh.rotation.z += this.rotationSpeed.z * delta; + } + }; + + this.resize = function () { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + }; +} + +const fxSceneA = new FXScene(new THREE.BoxGeometry(2, 2, 2), new THREE.Vector3(0, -0.4, 0), 0xffffff); +const fxSceneB = new FXScene(new THREE.IcosahedronGeometry(1, 1), new THREE.Vector3(0, 0.2, 0.1), 0x000000); + +function init() { + // Initialize textures + + const loader = new THREE.TextureLoader(); + + for (let i = 0; i < 6; i++) { + textures[i] = loader.load('textures/transition/transition' + (i + 1) + '.png'); + } + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + postProcessing = new THREE.PostProcessing(renderer); + + const scenePassA = pass(fxSceneA.scene, fxSceneA.camera); + const scenePassB = pass(fxSceneB.scene, fxSceneB.camera); + + transitionPass = transition( + scenePassA, + scenePassB, + new THREE.TextureNode(textures[effectController.texture]), + effectController._transition, + effectController.threshold, + effectController._useTexture, + ); + + postProcessing.outputNode = transitionPass; + + const gui = new GUI(); + + gui.add(effectController, 'animateScene').name('Animate Scene'); + gui.add(effectController, 'animateTransition').name('Animate Transition'); + transitionController = gui + .add(effectController, 'transition', 0, 1, 0.01) + .name('transition') + .onChange(() => { + effectController._transition.value = effectController.transition; + }); + gui.add(effectController, 'useTexture').onChange(() => { + const value = effectController.useTexture ? 1 : 0; + effectController._useTexture.value = value; + }); + gui.add(effectController, 'texture', { Perlin: 0, Squares: 1, Cells: 2, Distort: 3, Gradient: 4, Radial: 5 }); + gui.add(effectController, 'cycle'); + gui.add(effectController.threshold, 'value', 0, 1, 0.01).name('threshold'); +} + +window.addEventListener('resize', onWindowResize); + +function onWindowResize() { + fxSceneA.resize(); + fxSceneB.resize(); + renderer.setSize(window.innerWidth, window.innerHeight); +} + +new TWEEN.Tween(effectController) + .to({ transition: 1 }, 1500) + .onUpdate(function () { + transitionController.setValue(effectController.transition); + + // Change the current alpha texture after each transition + if (effectController.cycle) { + if (effectController.transition == 0 || effectController.transition == 1) { + effectController.texture = (effectController.texture + 1) % textures.length; + } + } + }) + .repeat(Infinity) + .delay(2000) + .yoyo(true) + .start(); + +function animate() { + if (effectController.animateTransition) TWEEN.update(); + + if (textures[effectController.texture]) { + const mixTexture = textures[effectController.texture]; + transitionPass.mixTextureNode.value = mixTexture; + } + + const delta = clock.getDelta(); + fxSceneA.update(delta); + fxSceneB.update(delta); + + render(); +} + +function render() { + // Prevent render both scenes when it's not necessary + if (effectController.transition === 0) { + renderer.render(fxSceneB.scene, fxSceneB.camera); + } else if (effectController.transition === 1) { + renderer.render(fxSceneA.scene, fxSceneA.camera); + } else { + postProcessing.render(); + } +} + +init(); diff --git a/examples-testing/examples/webgpu_procedural_texture.ts b/examples-testing/examples/webgpu_procedural_texture.ts new file mode 100644 index 000000000..d4228f908 --- /dev/null +++ b/examples-testing/examples/webgpu_procedural_texture.ts @@ -0,0 +1,74 @@ +import * as THREE from 'three'; +import { checker, uv, uniform } from 'three/tsl'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; + +let camera, scene, renderer; + +init(); +render(); + +function init() { + const aspect = window.innerWidth / window.innerHeight; + camera = new THREE.OrthographicCamera(-aspect, aspect, 1, -1, 0, 2); + camera.position.z = 1; + + scene = new THREE.Scene(); + + // procedural to texture + + const uvScale = uniform(4); + const blurAmount = uniform(0.5); + + const procedural = checker(uv().mul(uvScale)); + const proceduralToTexture = procedural.toTexture(512, 512); // ( width, height ) <- texture size + + const colorNode = proceduralToTexture.gaussianBlur(blurAmount, 10); + + // extra + + //proceduralToTexture.autoUpdate = false; // update just once + //proceduralToTexture.textureNeedsUpdate = true; // manually update + + // scene + + const material = new THREE.MeshBasicNodeMaterial(); + material.colorNode = colorNode; + + const plane = new THREE.Mesh(new THREE.PlaneGeometry(1, 1), material); + scene.add(plane); + + // renderer + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(render); + document.body.appendChild(renderer.domElement); + + window.addEventListener('resize', onWindowResize); + + // gui + + const gui = new GUI(); + gui.add(uvScale, 'value', 1, 10).name('uv scale ( before rtt )'); + gui.add(blurAmount, 'value', 0, 2).name('blur amount ( after rtt )'); + gui.add(proceduralToTexture, 'autoUpdate').name('auto update'); +} + +function onWindowResize() { + renderer.setSize(window.innerWidth, window.innerHeight); + + const aspect = window.innerWidth / window.innerHeight; + + const frustumHeight = camera.top - camera.bottom; + + camera.left = (-frustumHeight * aspect) / 2; + camera.right = (frustumHeight * aspect) / 2; + + camera.updateProjectionMatrix(); +} + +function render() { + renderer.renderAsync(scene, camera); +} diff --git a/examples-testing/examples/webgpu_refraction.ts b/examples-testing/examples/webgpu_refraction.ts new file mode 100644 index 000000000..bf36d9119 --- /dev/null +++ b/examples-testing/examples/webgpu_refraction.ts @@ -0,0 +1,141 @@ +import * as THREE from 'three'; +import { viewportSafeUV, viewportSharedTexture, viewportTopLeft, texture, uv } from 'three/tsl'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer; + +let cameraControls; + +let smallSphere; + +init(); + +function init() { + // scene + scene = new THREE.Scene(); + + // camera + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 500); + camera.position.set(0, 75, 160); + + // + + const geometry = new THREE.IcosahedronGeometry(5, 0); + const material = new THREE.MeshPhongMaterial({ color: 0xffffff, emissive: 0x7b7b7b, flatShading: true }); + smallSphere = new THREE.Mesh(geometry, material); + scene.add(smallSphere); + + // textures + + const loader = new THREE.TextureLoader(); + + const floorNormal = loader.load('textures/floors/FloorsCheckerboard_S_Normal.jpg'); + floorNormal.wrapS = THREE.RepeatWrapping; + floorNormal.wrapT = THREE.RepeatWrapping; + + // refractor + + const verticalNormalScale = 0.1; + const verticalUVOffset = texture(floorNormal, uv().mul(5)).xy.mul(2).sub(1).mul(verticalNormalScale); + + const refractorUV = viewportTopLeft.add(verticalUVOffset); + const verticalRefractor = viewportSharedTexture(viewportSafeUV(refractorUV)); + + const planeGeo = new THREE.PlaneGeometry(100.1, 100.1); + + const planeRefractor = new THREE.Mesh( + planeGeo, + new THREE.MeshBasicNodeMaterial({ + backdropNode: verticalRefractor, + }), + ); + planeRefractor.material.transparent = true; + planeRefractor.position.y = 50; + scene.add(planeRefractor); + + // walls + + const planeTop = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); + planeTop.position.y = 100; + planeTop.rotateX(Math.PI / 2); + scene.add(planeTop); + + const planeBottom = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); + planeBottom.rotateX(-Math.PI / 2); + scene.add(planeBottom); + + const planeBack = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x7f7fff })); + planeBack.position.z = -50; + planeBack.position.y = 50; + scene.add(planeBack); + + const planeRight = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x00ff00 })); + planeRight.position.x = 50; + planeRight.position.y = 50; + planeRight.rotateY(-Math.PI / 2); + scene.add(planeRight); + + const planeLeft = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xff0000 })); + planeLeft.position.x = -50; + planeLeft.position.y = 50; + planeLeft.rotateY(Math.PI / 2); + scene.add(planeLeft); + + // lights + + const mainLight = new THREE.PointLight(0xe7e7e7, 2.5, 250, 0); + mainLight.position.y = 60; + scene.add(mainLight); + + const greenLight = new THREE.PointLight(0x00ff00, 0.5, 1000, 0); + greenLight.position.set(550, 50, 0); + scene.add(greenLight); + + const redLight = new THREE.PointLight(0xff0000, 0.5, 1000, 0); + redLight.position.set(-550, 50, 0); + scene.add(redLight); + + const blueLight = new THREE.PointLight(0xbbbbfe, 0.5, 1000, 0); + blueLight.position.set(0, 50, 550); + scene.add(blueLight); + + // renderer + + renderer = new THREE.WebGPURenderer(/*{ antialias: true }*/); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // controls + + cameraControls = new OrbitControls(camera, renderer.domElement); + cameraControls.target.set(0, 40, 0); + cameraControls.maxDistance = 400; + cameraControls.minDistance = 10; + cameraControls.update(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const timer = Date.now() * 0.01; + + smallSphere.position.set( + Math.cos(timer * 0.1) * 30, + Math.abs(Math.cos(timer * 0.2)) * 20 + 5, + Math.sin(timer * 0.1) * 30, + ); + smallSphere.rotation.y = Math.PI / 2 - timer * 0.1; + smallSphere.rotation.z = timer * 0.8; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_sky.ts b/examples-testing/examples/webgpu_sky.ts new file mode 100644 index 000000000..097d06af6 --- /dev/null +++ b/examples-testing/examples/webgpu_sky.ts @@ -0,0 +1,95 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { SkyMesh } from 'three/addons/objects/SkyMesh.js'; + +let camera, scene, renderer; + +let sky, sun; + +init(); + +function initSky() { + // Add Sky + sky = new SkyMesh(); + sky.scale.setScalar(450000); + scene.add(sky); + + sun = new THREE.Vector3(); + + /// GUI + + const effectController = { + turbidity: 10, + rayleigh: 3, + mieCoefficient: 0.005, + mieDirectionalG: 0.7, + elevation: 2, + azimuth: 180, + exposure: renderer.toneMappingExposure, + }; + + function guiChanged() { + sky.turbidity.value = effectController.turbidity; + sky.rayleigh.value = effectController.rayleigh; + sky.mieCoefficient.value = effectController.mieCoefficient; + sky.mieDirectionalG.value = effectController.mieDirectionalG; + + const phi = THREE.MathUtils.degToRad(90 - effectController.elevation); + const theta = THREE.MathUtils.degToRad(effectController.azimuth); + + sun.setFromSphericalCoords(1, phi, theta); + + sky.sunPosition.value.copy(sun); + + renderer.toneMappingExposure = effectController.exposure; + } + + const gui = new GUI(); + + gui.add(effectController, 'turbidity', 0.0, 20.0, 0.1).onChange(guiChanged); + gui.add(effectController, 'rayleigh', 0.0, 4, 0.001).onChange(guiChanged); + gui.add(effectController, 'mieCoefficient', 0.0, 0.1, 0.001).onChange(guiChanged); + gui.add(effectController, 'mieDirectionalG', 0.0, 1, 0.001).onChange(guiChanged); + gui.add(effectController, 'elevation', 0, 90, 0.1).onChange(guiChanged); + gui.add(effectController, 'azimuth', -180, 180, 0.1).onChange(guiChanged); + gui.add(effectController, 'exposure', 0, 1, 0.0001).onChange(guiChanged); + + guiChanged(); +} + +function init() { + camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 100, 2000000); + camera.position.set(0, 100, 2000); + + scene = new THREE.Scene(); + + renderer = new THREE.WebGPURenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 0.5; + document.body.appendChild(renderer.domElement); + + const controls = new OrbitControls(camera, renderer.domElement); + //controls.maxPolarAngle = Math.PI / 2; + controls.enableZoom = false; + controls.enablePan = false; + + initSky(); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_textures_anisotropy.ts b/examples-testing/examples/webgpu_textures_anisotropy.ts new file mode 100644 index 000000000..21cc11970 --- /dev/null +++ b/examples-testing/examples/webgpu_textures_anisotropy.ts @@ -0,0 +1,155 @@ +import * as THREE from 'three'; + +import Stats from 'three/addons/libs/stats.module.js'; + +let container, stats; + +let camera, scene1, scene2, renderer; + +let mouseX = 0, + mouseY = 0; + +init(); + +function init() { + const SCREEN_WIDTH = window.innerWidth; + const SCREEN_HEIGHT = window.innerHeight; + + container = document.createElement('div'); + document.body.appendChild(container); + + renderer = new THREE.WebGPURenderer({ antialias: true, forceWebGL: false }); + + // RENDERER + + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + renderer.setAnimationLoop(animate); + renderer.autoClear = false; + + renderer.domElement.style.position = 'relative'; + container.appendChild(renderer.domElement); + + // + + camera = new THREE.PerspectiveCamera(35, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 25000); + camera.position.z = 1500; + + scene1 = new THREE.Scene(); + scene1.fog = new THREE.Fog(0xf2f7ff, 1, 25000); + + scene2 = new THREE.Scene(); + scene2.fog = new THREE.Fog(0xf2f7ff, 1, 25000); + + scene1.add(new THREE.AmbientLight(0xeef0ff, 3)); + scene2.add(new THREE.AmbientLight(0xeef0ff, 3)); + + const light1 = new THREE.DirectionalLight(0xffffff, 6); + light1.position.set(1, 1, 1); + scene1.add(light1); + + const light2 = new THREE.DirectionalLight(0xffffff, 6); + light2.position.set(1, 1, 1); + scene2.add(light2); + + // GROUND + + const textureLoader = new THREE.TextureLoader(); + + const maxAnisotropy = renderer.getMaxAnisotropy(); + + const texture1 = textureLoader.load('textures/crate.gif'); + const material1 = new THREE.MeshPhongMaterial({ color: 0xffffff, map: texture1 }); + + texture1.colorSpace = THREE.SRGBColorSpace; + texture1.anisotropy = renderer.getMaxAnisotropy(); + texture1.wrapS = texture1.wrapT = THREE.RepeatWrapping; + texture1.repeat.set(512, 512); + + const texture2 = textureLoader.load('textures/crate.gif'); + const material2 = new THREE.MeshPhongMaterial({ color: 0xffffff, map: texture2 }); + + texture2.colorSpace = THREE.SRGBColorSpace; + texture2.anisotropy = 1; + texture2.wrapS = texture2.wrapT = THREE.RepeatWrapping; + texture2.repeat.set(512, 512); + + if (maxAnisotropy > 0) { + document.getElementById('val_left').innerHTML = texture1.anisotropy; + document.getElementById('val_right').innerHTML = texture2.anisotropy; + } else { + document.getElementById('val_left').innerHTML = 'not supported'; + document.getElementById('val_right').innerHTML = 'not supported'; + } + + // + + const geometry = new THREE.PlaneGeometry(100, 100); + + const mesh1 = new THREE.Mesh(geometry, material1); + mesh1.rotation.x = -Math.PI / 2; + mesh1.scale.set(1000, 1000, 1000); + + const mesh2 = new THREE.Mesh(geometry, material2); + mesh2.rotation.x = -Math.PI / 2; + mesh2.scale.set(1000, 1000, 1000); + + scene1.add(mesh1); + scene2.add(mesh2); + + // STATS1 + + stats = new Stats(); + container.appendChild(stats.dom); + + document.addEventListener('mousemove', onDocumentMouseMove); + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onDocumentMouseMove(event) { + const windowHalfX = window.innerWidth / 2; + const windowHalfY = window.innerHeight / 2; + + mouseX = event.clientX - windowHalfX; + mouseY = event.clientY - windowHalfY; +} + +function animate() { + render(); + stats.update(); +} + +function render() { + const SCREEN_WIDTH = window.innerWidth; + const SCREEN_HEIGHT = window.innerHeight; + + camera.position.x += (mouseX - camera.position.x) * 0.05; + camera.position.y = THREE.MathUtils.clamp( + camera.position.y + (-(mouseY - 200) - camera.position.y) * 0.05, + 50, + 1000, + ); + + camera.lookAt(scene1.position); + renderer.clear(); + + renderer.setScissorTest(true); + + renderer.setScissor(0, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); + renderer.render(scene1, camera); + + renderer.setScissorTest(true); + + renderer.setScissor(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); + renderer.render(scene2, camera); + + // renderer.setScissorTest( false ); +} diff --git a/examples-testing/examples/webgpu_textures_partialupdate.ts b/examples-testing/examples/webgpu_textures_partialupdate.ts new file mode 100644 index 000000000..e8ebe87db --- /dev/null +++ b/examples-testing/examples/webgpu_textures_partialupdate.ts @@ -0,0 +1,103 @@ +import * as THREE from 'three'; + +let camera, scene, renderer, clock, dataTexture, diffuseMap; + +let last = 0; +const position = new THREE.Vector2(); +const color = new THREE.Color(); + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10); + camera.position.z = 2; + + scene = new THREE.Scene(); + + clock = new THREE.Clock(); + + const loader = new THREE.TextureLoader(); + diffuseMap = loader.load('textures/carbon/Carbon.png', animate); + diffuseMap.colorSpace = THREE.SRGBColorSpace; + diffuseMap.minFilter = THREE.LinearFilter; + diffuseMap.generateMipmaps = false; + + const geometry = new THREE.PlaneGeometry(2, 2); + const material = new THREE.MeshBasicMaterial({ map: diffuseMap }); + + const mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + // + + const width = 32; + const height = 32; + + const data = new Uint8Array(width * height * 4); + dataTexture = new THREE.DataTexture(data, width, height); + + // + + renderer = new THREE.WebGPURenderer({ antialias: true, forceWebGL: false }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + + document.body.appendChild(renderer.domElement); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +async function animate() { + requestAnimationFrame(animate); + + const elapsedTime = clock.getElapsedTime(); + + await renderer.renderAsync(scene, camera); + + if (elapsedTime - last > 0.1) { + last = elapsedTime; + + position.x = 32 * THREE.MathUtils.randInt(1, 16) - 32; + position.y = 32 * THREE.MathUtils.randInt(1, 16) - 32; + + // generate new color data + updateDataTexture(dataTexture); + + // perform copy from src to dest texture to a random position + + renderer.copyTextureToTexture(dataTexture, diffuseMap, null, position); + } +} + +function updateDataTexture(texture) { + const size = texture.image.width * texture.image.height; + const data = texture.image.data; + + // generate a random color and update texture data + + color.setHex(Math.random() * 0xffffff); + + const r = Math.floor(color.r * 255); + const g = Math.floor(color.g * 255); + const b = Math.floor(color.b * 255); + + for (let i = 0; i < size; i++) { + const stride = i * 4; + + data[stride] = r; + data[stride + 1] = g; + data[stride + 2] = b; + data[stride + 3] = 1; + } + + texture.needsUpdate = true; +} diff --git a/examples-testing/examples/webgpu_tsl_coffee_smoke.ts b/examples-testing/examples/webgpu_tsl_coffee_smoke.ts new file mode 100644 index 000000000..506f14f19 --- /dev/null +++ b/examples-testing/examples/webgpu_tsl_coffee_smoke.ts @@ -0,0 +1,132 @@ +import * as THREE from 'three'; +import { mix, mul, positionLocal, smoothstep, texture, timerLocal, Fn, uv, vec2, vec3, vec4 } from 'three/tsl'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; + +let camera, scene, renderer, controls; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(25, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(8, 10, 12); + + scene = new THREE.Scene(); + + // Loaders + + const gltfLoader = new GLTFLoader(); + const textureLoader = new THREE.TextureLoader(); + + // baked model + + gltfLoader.load('./models/gltf/coffeeMug.glb', gltf => { + gltf.scene.getObjectByName('baked').material.map.anisotropy = 8; + scene.add(gltf.scene); + }); + + // geometry + + const smokeGeometry = new THREE.PlaneGeometry(1, 1, 16, 64); + smokeGeometry.translate(0, 0.5, 0); + smokeGeometry.scale(1.5, 6, 1.5); + + // texture + + const noiseTexture = textureLoader.load('./textures/noises/perlin/128x128.png'); + noiseTexture.wrapS = THREE.RepeatWrapping; + noiseTexture.wrapT = THREE.RepeatWrapping; + + // material + + const smokeMaterial = new THREE.MeshBasicNodeMaterial({ + transparent: true, + side: THREE.DoubleSide, + depthWrite: false, + }); + const time = timerLocal(); + + // position + + smokeMaterial.positionNode = Fn(() => { + // twist + + const twistNoiseUv = vec2(0.5, uv().y.mul(0.2).sub(time.mul(0.005)).mod(1)); + const twist = texture(noiseTexture, twistNoiseUv).r.mul(10); + positionLocal.xz.assign(positionLocal.xz.rotateUV(twist, vec2(0))); + + // wind + + const windOffset = vec2( + texture(noiseTexture, vec2(0.25, time.mul(0.01)).mod(1)).r.sub(0.5), + texture(noiseTexture, vec2(0.75, time.mul(0.01)).mod(1)).r.sub(0.5), + ).mul(uv().y.pow(2).mul(10)); + positionLocal.addAssign(windOffset); + + return positionLocal; + })(); + + // color + + smokeMaterial.colorNode = Fn(() => { + // alpha + + const alphaNoiseUv = uv() + .mul(vec2(0.5, 0.3)) + .add(vec2(0, time.mul(0.03).negate())); + const alpha = mul( + // pattern + texture(noiseTexture, alphaNoiseUv).r.smoothstep(0.4, 1), + + // edges fade + smoothstep(0, 0.1, uv().x), + smoothstep(1, 0.9, uv().x), + smoothstep(0, 0.1, uv().y), + smoothstep(1, 0.9, uv().y), + ); + + // color + + const finalColor = mix(vec3(0.6, 0.3, 0.2), vec3(1, 1, 1), alpha.pow(3)); + + return vec4(finalColor, alpha); + })(); + + // mesh + + const smoke = new THREE.Mesh(smokeGeometry, smokeMaterial); + smoke.position.y = 1.83; + scene.add(smoke); + + // renderer + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // controls + + controls = new OrbitControls(camera, renderer.domElement); + controls.enableDamping = true; + controls.minDistance = 0.1; + controls.maxDistance = 50; + controls.target.y = 3; + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +async function animate() { + controls.update(); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_tsl_vfx_flames.ts b/examples-testing/examples/webgpu_tsl_vfx_flames.ts new file mode 100644 index 000000000..a9110fa8c --- /dev/null +++ b/examples-testing/examples/webgpu_tsl_vfx_flames.ts @@ -0,0 +1,203 @@ +import * as THREE from 'three'; +import { + PI2, + spherizeUV, + sin, + step, + texture, + timerLocal, + Fn, + uv, + vec2, + vec3, + vec4, + mix, + billboarding, +} from 'three/tsl'; + +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; + +let camera, scene, renderer, controls; + +init(); + +function init() { + camera = new THREE.PerspectiveCamera(25, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(1, 1, 3); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x201919); + + // textures + + const textureLoader = new THREE.TextureLoader(); + + const cellularTexture = textureLoader.load('./textures/noises/voronoi/grayscale-256x256.png'); + const perlinTexture = textureLoader.load('./textures/noises/perlin/rgb-256x256.png'); + + // gradient canvas + + const gradient = {}; + gradient.element = document.createElement('canvas'); + gradient.element.width = 128; + gradient.element.height = 1; + gradient.context = gradient.element.getContext('2d'); + + gradient.colors = ['#090033', '#5f1f93', '#e02e96', '#ffbd80', '#fff0db']; + + gradient.texture = new THREE.CanvasTexture(gradient.element); + gradient.texture.colorSpace = THREE.SRGBColorSpace; + + gradient.update = () => { + const fillGradient = gradient.context.createLinearGradient(0, 0, gradient.element.width, 0); + + for (let i = 0; i < gradient.colors.length; i++) { + const progress = i / (gradient.colors.length - 1); + const color = gradient.colors[i]; + fillGradient.addColorStop(progress, color); + } + + gradient.context.fillStyle = fillGradient; + gradient.context.fillRect(0, 0, gradient.element.width, gradient.element.height); + + gradient.texture.needsUpdate = true; + }; + + gradient.update(); + + // flame 1 material + + const flame1Material = new THREE.SpriteNodeMaterial({ transparent: true, side: THREE.DoubleSide }); + + flame1Material.colorNode = Fn(() => { + const time = timerLocal(); + + // main UV + const mainUv = uv().toVar(); + mainUv.assign(spherizeUV(mainUv, 10).mul(0.6).add(0.2)); // spherize + mainUv.assign(mainUv.pow(vec2(1, 2))); // stretch + mainUv.assign(mainUv.mul(2, 1).sub(vec2(0.5, 0))); // scale + + // gradients + const gradient1 = sin(time.mul(10).sub(mainUv.y.mul(PI2).mul(2))).toVar(); + const gradient2 = mainUv.y.smoothstep(0, 1).toVar(); + mainUv.x.addAssign(gradient1.mul(gradient2).mul(0.2)); + + // cellular noise + const cellularUv = mainUv + .mul(0.5) + .add(vec2(0, time.negate().mul(0.5))) + .mod(1); + const cellularNoise = texture(cellularTexture, cellularUv, 0).r.oneMinus().smoothstep(0, 0.5).oneMinus(); + cellularNoise.mulAssign(gradient2); + + // shape + const shape = mainUv.sub(0.5).mul(vec2(3, 2)).length().oneMinus().toVar(); + shape.assign(shape.sub(cellularNoise)); + + // gradient color + const gradientColor = texture(gradient.texture, vec2(shape.remap(0, 1, 0, 1), 0)); + + // output + const color = mix(gradientColor, vec3(1), shape.step(0.8).oneMinus()); + const alpha = shape.smoothstep(0, 0.3); + return vec4(color.rgb, alpha); + })(); + + // flame 2 material + + const flame2Material = new THREE.SpriteNodeMaterial({ transparent: true, side: THREE.DoubleSide }); + + flame2Material.colorNode = Fn(() => { + const time = timerLocal(); + + // main UV + const mainUv = uv().toVar(); + mainUv.assign(spherizeUV(mainUv, 10).mul(0.6).add(0.2)); // spherize + mainUv.assign(mainUv.pow(vec2(1, 3))); // stretch + mainUv.assign(mainUv.mul(2, 1).sub(vec2(0.5, 0))); // scale + + // perlin noise + const perlinUv = mainUv.add(vec2(0, time.negate().mul(1))).mod(1); + const perlinNoise = texture(perlinTexture, perlinUv, 0).sub(0.5).mul(1); + mainUv.x.addAssign(perlinNoise.x.mul(0.5)); + + // gradients + const gradient1 = sin(time.mul(10).sub(mainUv.y.mul(PI2).mul(2))); + const gradient2 = mainUv.y.smoothstep(0, 1); + const gradient3 = mainUv.y.smoothstep(1, 0.7); + mainUv.x.addAssign(gradient1.mul(gradient2).mul(0.2)); + + // displaced perlin noise + const displacementPerlinUv = mainUv + .mul(0.5) + .add(vec2(0, time.negate().mul(0.25))) + .mod(1); + const displacementPerlinNoise = texture(perlinTexture, displacementPerlinUv, 0).sub(0.5).mul(1); + const displacedPerlinUv = mainUv + .add(vec2(0, time.negate().mul(0.5))) + .add(displacementPerlinNoise) + .mod(1); + const displacedPerlinNoise = texture(perlinTexture, displacedPerlinUv, 0).sub(0.5).mul(1); + mainUv.x.addAssign(displacedPerlinNoise.mul(0.5)); + + // cellular noise + const cellularUv = mainUv.add(vec2(0, time.negate().mul(1.5))).mod(1); + const cellularNoise = texture(cellularTexture, cellularUv, 0).r.oneMinus().smoothstep(0.25, 1); + + // shape + const shape = mainUv.sub(0.5).mul(vec2(6, 1)).length().step(0.5); + shape.assign(shape.mul(cellularNoise)); + shape.mulAssign(gradient3); + shape.assign(step(0.01, shape)); + + // output + return vec4(vec3(1), shape); + })(); + + // billboarding - follow the camera rotation only horizontally + + flame1Material.vertexNode = billboarding(); + flame2Material.vertexNode = billboarding(); + + // meshes + + const flame1 = new THREE.Sprite(flame1Material); + flame1.center.set(0.5, 0); + flame1.scale.x = 0.5; // optional + flame1.position.x = -0.5; + scene.add(flame1); + + const flame2 = new THREE.Sprite(flame2Material); + flame2.center.set(0.5, 0); + flame2.position.x = 0.5; + scene.add(flame2); + + // renderer + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + controls = new OrbitControls(camera, renderer.domElement); + controls.enableDamping = true; + controls.minDistance = 0.1; + controls.maxDistance = 50; + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +async function animate() { + controls.update(); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_video_panorama.ts b/examples-testing/examples/webgpu_video_panorama.ts new file mode 100644 index 000000000..e409b3c07 --- /dev/null +++ b/examples-testing/examples/webgpu_video_panorama.ts @@ -0,0 +1,99 @@ +import * as THREE from 'three'; + +let camera, scene, renderer; + +let isUserInteracting = false, + lon = 0, + lat = 0, + phi = 0, + theta = 0, + onPointerDownPointerX = 0, + onPointerDownPointerY = 0, + onPointerDownLon = 0, + onPointerDownLat = 0; + +const distance = 0.5; + +init(); + +function init() { + const container = document.getElementById('container'); + + camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.25, 10); + + scene = new THREE.Scene(); + + const geometry = new THREE.SphereGeometry(5, 60, 40); + // invert the geometry on the x-axis so that all of the faces point inward + geometry.scale(-1, 1, 1); + + const video = document.getElementById('video'); + video.play(); + + const texture = new THREE.VideoTexture(video); + texture.colorSpace = THREE.SRGBColorSpace; + const material = new THREE.MeshBasicMaterial({ map: texture }); + + const mesh = new THREE.Mesh(geometry, material); + scene.add(mesh); + + renderer = new THREE.WebGPURenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + container.appendChild(renderer.domElement); + + document.addEventListener('pointerdown', onPointerDown); + document.addEventListener('pointermove', onPointerMove); + document.addEventListener('pointerup', onPointerUp); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onPointerDown(event) { + isUserInteracting = true; + + onPointerDownPointerX = event.clientX; + onPointerDownPointerY = event.clientY; + + onPointerDownLon = lon; + onPointerDownLat = lat; +} + +function onPointerMove(event) { + if (isUserInteracting === true) { + lon = (onPointerDownPointerX - event.clientX) * 0.1 + onPointerDownLon; + lat = (onPointerDownPointerY - event.clientY) * 0.1 + onPointerDownLat; + } +} + +function onPointerUp() { + isUserInteracting = false; +} + +function animate() { + update(); +} + +function update() { + lat = Math.max(-85, Math.min(85, lat)); + phi = THREE.MathUtils.degToRad(90 - lat); + theta = THREE.MathUtils.degToRad(lon); + + camera.position.x = distance * Math.sin(phi) * Math.cos(theta); + camera.position.y = distance * Math.cos(phi); + camera.position.z = distance * Math.sin(phi) * Math.sin(theta); + + camera.lookAt(0, 0, 0); + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webgpu_water.ts b/examples-testing/examples/webgpu_water.ts new file mode 100644 index 000000000..76e09f1f8 --- /dev/null +++ b/examples-testing/examples/webgpu_water.ts @@ -0,0 +1,171 @@ +import * as THREE from 'three'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { WaterMesh } from 'three/addons/objects/Water2Mesh.js'; + +let scene, camera, clock, renderer, water; + +let torusKnot; + +const params = { + color: '#ffffff', + scale: 4, + flowX: 1, + flowY: 1, +}; + +init(); + +function init() { + // scene + + scene = new THREE.Scene(); + + // camera + + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200); + camera.position.set(-15, 7, 15); + camera.lookAt(scene.position); + + // clock + + clock = new THREE.Clock(); + + // mesh + + const torusKnotGeometry = new THREE.TorusKnotGeometry(3, 1, 256, 32); + const torusKnotMaterial = new THREE.MeshNormalMaterial(); + + torusKnot = new THREE.Mesh(torusKnotGeometry, torusKnotMaterial); + torusKnot.position.y = 4; + torusKnot.scale.set(0.5, 0.5, 0.5); + scene.add(torusKnot); + + // ground + + const groundGeometry = new THREE.PlaneGeometry(20, 20); + const groundMaterial = new THREE.MeshStandardMaterial({ roughness: 0.8, metalness: 0.4 }); + const ground = new THREE.Mesh(groundGeometry, groundMaterial); + ground.rotation.x = Math.PI * -0.5; + scene.add(ground); + + const textureLoader = new THREE.TextureLoader(); + textureLoader.load('textures/hardwood2_diffuse.jpg', function (map) { + map.wrapS = THREE.RepeatWrapping; + map.wrapT = THREE.RepeatWrapping; + map.anisotropy = 16; + map.repeat.set(4, 4); + map.colorSpace = THREE.SRGBColorSpace; + groundMaterial.map = map; + groundMaterial.needsUpdate = true; + }); + + // + + const normalMap0 = textureLoader.load('textures/water/Water_1_M_Normal.jpg'); + const normalMap1 = textureLoader.load('textures/water/Water_2_M_Normal.jpg'); + + normalMap0.wrapS = normalMap0.wrapT = THREE.RepeatWrapping; + normalMap1.wrapS = normalMap1.wrapT = THREE.RepeatWrapping; + + // water + + const waterGeometry = new THREE.PlaneGeometry(20, 20); + + water = new WaterMesh(waterGeometry, { + color: params.color, + scale: params.scale, + flowDirection: new THREE.Vector2(params.flowX, params.flowY), + normalMap0: normalMap0, + normalMap1: normalMap1, + }); + + water.position.y = 1; + water.rotation.x = Math.PI * -0.5; + scene.add(water); + + // skybox + + const cubeTextureLoader = new THREE.CubeTextureLoader(); + cubeTextureLoader.setPath('textures/cube/Park2/'); + + const cubeTexture = cubeTextureLoader.load([ + 'posx.jpg', + 'negx.jpg', + 'posy.jpg', + 'negy.jpg', + 'posz.jpg', + 'negz.jpg', + ]); + + scene.background = cubeTexture; + + // light + + const ambientLight = new THREE.AmbientLight(0xe7e7e7, 1.2); + scene.add(ambientLight); + + const directionalLight = new THREE.DirectionalLight(0xffffff, 2); + directionalLight.position.set(-1, 1, 1); + scene.add(directionalLight); + + // renderer + + renderer = new THREE.WebGPURenderer(); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setAnimationLoop(animate); + document.body.appendChild(renderer.domElement); + + // gui + + const gui = new GUI(); + const waterNode = water.material.fragmentNode; + + gui.addColor(params, 'color').onChange(function (value) { + waterNode.color.value.set(value); + }); + gui.add(params, 'scale', 1, 10).onChange(function (value) { + waterNode.scale.value = value; + }); + gui.add(params, 'flowX', -1, 1) + .step(0.01) + .onChange(function (value) { + waterNode.flowDirection.value.x = value; + waterNode.flowDirection.value.normalize(); + }); + gui.add(params, 'flowY', -1, 1) + .step(0.01) + .onChange(function (value) { + waterNode.flowDirection.value.y = value; + waterNode.flowDirection.value.normalize(); + }); + + gui.open(); + + // + + const controls = new OrbitControls(camera, renderer.domElement); + controls.minDistance = 5; + controls.maxDistance = 50; + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const delta = clock.getDelta(); + + torusKnot.rotation.x += delta; + torusKnot.rotation.y += delta * 0.5; + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webxr_ar_cones.ts b/examples-testing/examples/webxr_ar_cones.ts new file mode 100644 index 000000000..95eb34393 --- /dev/null +++ b/examples-testing/examples/webxr_ar_cones.ts @@ -0,0 +1,66 @@ +import * as THREE from 'three'; +import { ARButton } from 'three/addons/webxr/ARButton.js'; + +let camera, scene, renderer; +let controller; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 20); + + const light = new THREE.HemisphereLight(0xffffff, 0xbbbbff, 3); + light.position.set(0.5, 1, 0.25); + scene.add(light); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.xr.enabled = true; + container.appendChild(renderer.domElement); + + // + + document.body.appendChild(ARButton.createButton(renderer)); + + // + + const geometry = new THREE.CylinderGeometry(0, 0.05, 0.2, 32).rotateX(Math.PI / 2); + + function onSelect() { + const material = new THREE.MeshPhongMaterial({ color: 0xffffff * Math.random() }); + const mesh = new THREE.Mesh(geometry, material); + mesh.position.set(0, 0, -0.3).applyMatrix4(controller.matrixWorld); + mesh.quaternion.setFromRotationMatrix(controller.matrixWorld); + scene.add(mesh); + } + + controller = renderer.xr.getController(0); + controller.addEventListener('select', onSelect); + scene.add(controller); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webxr_ar_hittest.ts b/examples-testing/examples/webxr_ar_hittest.ts new file mode 100644 index 000000000..1867cc470 --- /dev/null +++ b/examples-testing/examples/webxr_ar_hittest.ts @@ -0,0 +1,115 @@ +import * as THREE from 'three'; +import { ARButton } from 'three/addons/webxr/ARButton.js'; + +let container; +let camera, scene, renderer; +let controller; + +let reticle; + +let hitTestSource = null; +let hitTestSourceRequested = false; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 20); + + const light = new THREE.HemisphereLight(0xffffff, 0xbbbbff, 3); + light.position.set(0.5, 1, 0.25); + scene.add(light); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.xr.enabled = true; + container.appendChild(renderer.domElement); + + // + + document.body.appendChild(ARButton.createButton(renderer, { requiredFeatures: ['hit-test'] })); + + // + + const geometry = new THREE.CylinderGeometry(0.1, 0.1, 0.2, 32).translate(0, 0.1, 0); + + function onSelect() { + if (reticle.visible) { + const material = new THREE.MeshPhongMaterial({ color: 0xffffff * Math.random() }); + const mesh = new THREE.Mesh(geometry, material); + reticle.matrix.decompose(mesh.position, mesh.quaternion, mesh.scale); + mesh.scale.y = Math.random() * 2 + 1; + scene.add(mesh); + } + } + + controller = renderer.xr.getController(0); + controller.addEventListener('select', onSelect); + scene.add(controller); + + reticle = new THREE.Mesh( + new THREE.RingGeometry(0.15, 0.2, 32).rotateX(-Math.PI / 2), + new THREE.MeshBasicMaterial(), + ); + reticle.matrixAutoUpdate = false; + reticle.visible = false; + scene.add(reticle); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate(timestamp, frame) { + if (frame) { + const referenceSpace = renderer.xr.getReferenceSpace(); + const session = renderer.xr.getSession(); + + if (hitTestSourceRequested === false) { + session.requestReferenceSpace('viewer').then(function (referenceSpace) { + session.requestHitTestSource({ space: referenceSpace }).then(function (source) { + hitTestSource = source; + }); + }); + + session.addEventListener('end', function () { + hitTestSourceRequested = false; + hitTestSource = null; + }); + + hitTestSourceRequested = true; + } + + if (hitTestSource) { + const hitTestResults = frame.getHitTestResults(hitTestSource); + + if (hitTestResults.length) { + const hit = hitTestResults[0]; + + reticle.visible = true; + reticle.matrix.fromArray(hit.getPose(referenceSpace).transform.matrix); + } else { + reticle.visible = false; + } + } + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webxr_ar_lighting.ts b/examples-testing/examples/webxr_ar_lighting.ts new file mode 100644 index 000000000..9de23ad94 --- /dev/null +++ b/examples-testing/examples/webxr_ar_lighting.ts @@ -0,0 +1,124 @@ +import * as THREE from 'three'; +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; +import { ARButton } from 'three/addons/webxr/ARButton.js'; +import { XREstimatedLight } from 'three/addons/webxr/XREstimatedLight.js'; + +let camera, scene, renderer; +let controller; +let defaultEnvironment; + +init(); + +function init() { + const container = document.createElement('div'); + document.body.appendChild(container); + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 20); + + const defaultLight = new THREE.HemisphereLight(0xffffff, 0xbbbbff, 1); + defaultLight.position.set(0.5, 1, 0.25); + scene.add(defaultLight); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.xr.enabled = true; + container.appendChild(renderer.domElement); + + // Don't add the XREstimatedLight to the scene initially. + // It doesn't have any estimated lighting values until an AR session starts. + + const xrLight = new XREstimatedLight(renderer); + + xrLight.addEventListener('estimationstart', () => { + // Swap the default light out for the estimated one one we start getting some estimated values. + scene.add(xrLight); + scene.remove(defaultLight); + + // The estimated lighting also provides an environment cubemap, which we can apply here. + if (xrLight.environment) { + scene.environment = xrLight.environment; + } + }); + + xrLight.addEventListener('estimationend', () => { + // Swap the lights back when we stop receiving estimated values. + scene.add(defaultLight); + scene.remove(xrLight); + + // Revert back to the default environment. + scene.environment = defaultEnvironment; + }); + + // + + new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + + defaultEnvironment = texture; + + scene.environment = defaultEnvironment; + }); + + // + + // In order for lighting estimation to work, 'light-estimation' must be included as either an optional or required feature. + document.body.appendChild(ARButton.createButton(renderer, { optionalFeatures: ['light-estimation'] })); + + // + + const ballGeometry = new THREE.SphereGeometry(0.175, 32, 32); + const ballGroup = new THREE.Group(); + ballGroup.position.z = -2; + + const rows = 3; + const cols = 3; + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + const ballMaterial = new THREE.MeshStandardMaterial({ + color: 0xdddddd, + roughness: i / rows, + metalness: j / cols, + }); + const ballMesh = new THREE.Mesh(ballGeometry, ballMaterial); + ballMesh.position.set((i + 0.5 - rows * 0.5) * 0.4, (j + 0.5 - cols * 0.5) * 0.4, 0); + ballGroup.add(ballMesh); + } + } + + scene.add(ballGroup); + + // + + function onSelect() { + ballGroup.position.set(0, 0, -2).applyMatrix4(controller.matrixWorld); + ballGroup.quaternion.setFromRotationMatrix(controller.matrixWorld); + } + + controller = renderer.xr.getController(0); + controller.addEventListener('select', onSelect); + scene.add(controller); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webxr_ar_plane_detection.ts b/examples-testing/examples/webxr_ar_plane_detection.ts new file mode 100644 index 000000000..841b6b04b --- /dev/null +++ b/examples-testing/examples/webxr_ar_plane_detection.ts @@ -0,0 +1,46 @@ +import * as THREE from 'three'; +import { ARButton } from 'three/addons/webxr/ARButton.js'; +import { XRPlanes } from 'three/addons/webxr/XRPlanes.js'; + +// + +const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); +renderer.setPixelRatio(window.devicePixelRatio); +renderer.setSize(window.innerWidth, window.innerHeight); +renderer.setAnimationLoop(animate); +renderer.xr.enabled = true; +document.body.appendChild(renderer.domElement); + +document.body.appendChild( + ARButton.createButton(renderer, { + requiredFeatures: ['plane-detection'], + }), +); + +window.addEventListener('resize', onWindowResize); + +// + +const scene = new THREE.Scene(); + +const planes = new XRPlanes(renderer); +scene.add(planes); + +const camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 20); + +const light = new THREE.HemisphereLight(0xffffff, 0xbbbbff, 3); +light.position.set(0.5, 1, 0.25); +scene.add(light); + +// + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webxr_vr_handinput.ts b/examples-testing/examples/webxr_vr_handinput.ts new file mode 100644 index 000000000..d746e4582 --- /dev/null +++ b/examples-testing/examples/webxr_vr_handinput.ts @@ -0,0 +1,126 @@ +import * as THREE from 'three'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { VRButton } from 'three/addons/webxr/VRButton.js'; +import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js'; +import { XRHandModelFactory } from 'three/addons/webxr/XRHandModelFactory.js'; + +let container; +let camera, scene, renderer; +let hand1, hand2; +let controller1, controller2; +let controllerGrip1, controllerGrip2; + +let controls; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x444444); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 10); + camera.position.set(0, 1.6, 3); + + controls = new OrbitControls(camera, container); + controls.target.set(0, 1.6, 0); + controls.update(); + + const floorGeometry = new THREE.PlaneGeometry(4, 4); + const floorMaterial = new THREE.MeshStandardMaterial({ color: 0x666666 }); + const floor = new THREE.Mesh(floorGeometry, floorMaterial); + floor.rotation.x = -Math.PI / 2; + floor.receiveShadow = true; + scene.add(floor); + + scene.add(new THREE.HemisphereLight(0xbcbcbc, 0xa5a5a5, 3)); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0, 6, 0); + light.castShadow = true; + light.shadow.camera.top = 2; + light.shadow.camera.bottom = -2; + light.shadow.camera.right = 2; + light.shadow.camera.left = -2; + light.shadow.mapSize.set(4096, 4096); + scene.add(light); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + renderer.xr.enabled = true; + + container.appendChild(renderer.domElement); + + const sessionInit = { + requiredFeatures: ['hand-tracking'], + }; + + document.body.appendChild(VRButton.createButton(renderer, sessionInit)); + + // controllers + + controller1 = renderer.xr.getController(0); + scene.add(controller1); + + controller2 = renderer.xr.getController(1); + scene.add(controller2); + + const controllerModelFactory = new XRControllerModelFactory(); + const handModelFactory = new XRHandModelFactory(); + + // Hand 1 + controllerGrip1 = renderer.xr.getControllerGrip(0); + controllerGrip1.add(controllerModelFactory.createControllerModel(controllerGrip1)); + scene.add(controllerGrip1); + + hand1 = renderer.xr.getHand(0); + hand1.add(handModelFactory.createHandModel(hand1)); + + scene.add(hand1); + + // Hand 2 + controllerGrip2 = renderer.xr.getControllerGrip(1); + controllerGrip2.add(controllerModelFactory.createControllerModel(controllerGrip2)); + scene.add(controllerGrip2); + + hand2 = renderer.xr.getHand(1); + hand2.add(handModelFactory.createHandModel(hand2)); + scene.add(hand2); + + // + + const geometry = new THREE.BufferGeometry().setFromPoints([ + new THREE.Vector3(0, 0, 0), + new THREE.Vector3(0, 0, -1), + ]); + + const line = new THREE.Line(geometry); + line.name = 'line'; + line.scale.z = 5; + + controller1.add(line.clone()); + controller2.add(line.clone()); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} +// + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webxr_vr_panorama.ts b/examples-testing/examples/webxr_vr_panorama.ts new file mode 100644 index 000000000..535e1c937 --- /dev/null +++ b/examples-testing/examples/webxr_vr_panorama.ts @@ -0,0 +1,92 @@ +import * as THREE from 'three'; +import { VRButton } from 'three/addons/webxr/VRButton.js'; + +let camera; +let renderer; +let scene; + +init(); + +function init() { + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.xr.enabled = true; + renderer.xr.setReferenceSpaceType('local'); + document.body.appendChild(renderer.domElement); + + document.body.appendChild(VRButton.createButton(renderer)); + + // + + scene = new THREE.Scene(); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); + camera.layers.enable(1); + + const geometry = new THREE.BoxGeometry(100, 100, 100); + geometry.scale(1, 1, -1); + + const textures = getTexturesFromAtlasFile('textures/cube/sun_temple_stripe_stereo.jpg', 12); + + const materials = []; + + for (let i = 0; i < 6; i++) { + materials.push(new THREE.MeshBasicMaterial({ map: textures[i] })); + } + + const skyBox = new THREE.Mesh(geometry, materials); + skyBox.layers.set(1); + scene.add(skyBox); + + const materialsR = []; + + for (let i = 6; i < 12; i++) { + materialsR.push(new THREE.MeshBasicMaterial({ map: textures[i] })); + } + + const skyBoxR = new THREE.Mesh(geometry, materialsR); + skyBoxR.layers.set(2); + scene.add(skyBoxR); + + window.addEventListener('resize', onWindowResize); +} + +function getTexturesFromAtlasFile(atlasImgUrl, tilesNum) { + const textures = []; + + for (let i = 0; i < tilesNum; i++) { + textures[i] = new THREE.Texture(); + } + + const loader = new THREE.ImageLoader(); + loader.load(atlasImgUrl, function (imageObj) { + let canvas, context; + const tileWidth = imageObj.height; + + for (let i = 0; i < textures.length; i++) { + canvas = document.createElement('canvas'); + context = canvas.getContext('2d'); + canvas.height = tileWidth; + canvas.width = tileWidth; + context.drawImage(imageObj, tileWidth * i, 0, tileWidth, tileWidth, 0, 0, tileWidth, tileWidth); + textures[i].colorSpace = THREE.SRGBColorSpace; + textures[i].image = canvas; + textures[i].needsUpdate = true; + } + }); + + return textures; +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webxr_vr_panorama_depth.ts b/examples-testing/examples/webxr_vr_panorama_depth.ts new file mode 100644 index 000000000..66215469d --- /dev/null +++ b/examples-testing/examples/webxr_vr_panorama_depth.ts @@ -0,0 +1,90 @@ +import * as THREE from 'three'; +import { VRButton } from 'three/addons/webxr/VRButton.js'; + +let camera, scene, renderer, sphere, clock; + +init(); + +function init() { + const container = document.getElementById('container'); + + clock = new THREE.Clock(); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x101010); + + const light = new THREE.AmbientLight(0xffffff, 3); + scene.add(light); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 2000); + scene.add(camera); + + // Create the panoramic sphere geometery + const panoSphereGeo = new THREE.SphereGeometry(6, 256, 256); + + // Create the panoramic sphere material + const panoSphereMat = new THREE.MeshStandardMaterial({ + side: THREE.BackSide, + displacementScale: -4.0, + }); + + // Create the panoramic sphere mesh + sphere = new THREE.Mesh(panoSphereGeo, panoSphereMat); + + // Load and assign the texture and depth map + const manager = new THREE.LoadingManager(); + const loader = new THREE.TextureLoader(manager); + + loader.load('./textures/kandao3.jpg', function (texture) { + texture.colorSpace = THREE.SRGBColorSpace; + texture.minFilter = THREE.NearestFilter; + texture.generateMipmaps = false; + sphere.material.map = texture; + }); + + loader.load('./textures/kandao3_depthmap.jpg', function (depth) { + depth.minFilter = THREE.NearestFilter; + depth.generateMipmaps = false; + sphere.material.displacementMap = depth; + }); + + // On load complete add the panoramic sphere to the scene + manager.onLoad = function () { + scene.add(sphere); + }; + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.xr.enabled = true; + renderer.xr.setReferenceSpaceType('local'); + container.appendChild(renderer.domElement); + + document.body.appendChild(VRButton.createButton(renderer)); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + // If we are not presenting move the camera a little so the effect is visible + + if (renderer.xr.isPresenting === false) { + const time = clock.getElapsedTime(); + + sphere.rotation.y += 0.001; + sphere.position.x = Math.sin(time) * 0.2; + sphere.position.z = Math.cos(time) * 0.2; + } + + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webxr_vr_rollercoaster.ts b/examples-testing/examples/webxr_vr_rollercoaster.ts new file mode 100644 index 000000000..b8c35a9e3 --- /dev/null +++ b/examples-testing/examples/webxr_vr_rollercoaster.ts @@ -0,0 +1,211 @@ +import * as THREE from 'three'; + +import { + RollerCoasterGeometry, + RollerCoasterShadowGeometry, + RollerCoasterLiftersGeometry, + TreesGeometry, + SkyGeometry, +} from 'three/addons/misc/RollerCoaster.js'; +import { VRButton } from 'three/addons/webxr/VRButton.js'; + +let mesh, material, geometry; + +const renderer = new THREE.WebGLRenderer({ antialias: true }); +renderer.setPixelRatio(window.devicePixelRatio); +renderer.setSize(window.innerWidth, window.innerHeight); +renderer.setAnimationLoop(animate); +renderer.xr.enabled = true; +renderer.xr.setReferenceSpaceType('local'); +document.body.appendChild(renderer.domElement); + +document.body.appendChild(VRButton.createButton(renderer)); + +// + +const scene = new THREE.Scene(); +scene.background = new THREE.Color(0xf0f0ff); + +const light = new THREE.HemisphereLight(0xfff0f0, 0x60606, 3); +light.position.set(1, 1, 1); +scene.add(light); + +const train = new THREE.Object3D(); +scene.add(train); + +const camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 500); +train.add(camera); + +// environment + +geometry = new THREE.PlaneGeometry(500, 500, 15, 15); +geometry.rotateX(-Math.PI / 2); + +const positions = geometry.attributes.position.array; +const vertex = new THREE.Vector3(); + +for (let i = 0; i < positions.length; i += 3) { + vertex.fromArray(positions, i); + + vertex.x += Math.random() * 10 - 5; + vertex.z += Math.random() * 10 - 5; + + const distance = vertex.distanceTo(scene.position) / 5 - 25; + vertex.y = Math.random() * Math.max(0, distance); + + vertex.toArray(positions, i); +} + +geometry.computeVertexNormals(); + +material = new THREE.MeshLambertMaterial({ + color: 0x407000, +}); + +mesh = new THREE.Mesh(geometry, material); +scene.add(mesh); + +geometry = new TreesGeometry(mesh); +material = new THREE.MeshBasicMaterial({ + side: THREE.DoubleSide, + vertexColors: true, +}); +mesh = new THREE.Mesh(geometry, material); +scene.add(mesh); + +geometry = new SkyGeometry(); +material = new THREE.MeshBasicMaterial({ color: 0xffffff }); +mesh = new THREE.Mesh(geometry, material); +scene.add(mesh); + +// + +const PI2 = Math.PI * 2; + +const curve = (function () { + const vector = new THREE.Vector3(); + const vector2 = new THREE.Vector3(); + + return { + getPointAt: function (t) { + t = t * PI2; + + const x = Math.sin(t * 3) * Math.cos(t * 4) * 50; + const y = Math.sin(t * 10) * 2 + Math.cos(t * 17) * 2 + 5; + const z = Math.sin(t) * Math.sin(t * 4) * 50; + + return vector.set(x, y, z).multiplyScalar(2); + }, + + getTangentAt: function (t) { + const delta = 0.0001; + const t1 = Math.max(0, t - delta); + const t2 = Math.min(1, t + delta); + + return vector2.copy(this.getPointAt(t2)).sub(this.getPointAt(t1)).normalize(); + }, + }; +})(); + +geometry = new RollerCoasterGeometry(curve, 1500); +material = new THREE.MeshPhongMaterial({ + vertexColors: true, +}); +mesh = new THREE.Mesh(geometry, material); +scene.add(mesh); + +geometry = new RollerCoasterLiftersGeometry(curve, 100); +material = new THREE.MeshPhongMaterial(); +mesh = new THREE.Mesh(geometry, material); +mesh.position.y = 0.1; +scene.add(mesh); + +geometry = new RollerCoasterShadowGeometry(curve, 500); +material = new THREE.MeshBasicMaterial({ + color: 0x305000, + depthWrite: false, + transparent: true, +}); +mesh = new THREE.Mesh(geometry, material); +mesh.position.y = 0.1; +scene.add(mesh); + +const funfairs = []; + +// + +geometry = new THREE.CylinderGeometry(10, 10, 5, 15); +material = new THREE.MeshLambertMaterial({ + color: 0xff8080, +}); +mesh = new THREE.Mesh(geometry, material); +mesh.position.set(-80, 10, -70); +mesh.rotation.x = Math.PI / 2; +scene.add(mesh); + +funfairs.push(mesh); + +geometry = new THREE.CylinderGeometry(5, 6, 4, 10); +material = new THREE.MeshLambertMaterial({ + color: 0x8080ff, +}); +mesh = new THREE.Mesh(geometry, material); +mesh.position.set(50, 2, 30); +scene.add(mesh); + +funfairs.push(mesh); + +// + +window.addEventListener('resize', onWindowResize); + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +const position = new THREE.Vector3(); +const tangent = new THREE.Vector3(); + +const lookAt = new THREE.Vector3(); + +let velocity = 0; +let progress = 0; + +let prevTime = performance.now(); + +function animate() { + const time = performance.now(); + const delta = time - prevTime; + + for (let i = 0; i < funfairs.length; i++) { + funfairs[i].rotation.y = time * 0.0004; + } + + // + + progress += velocity; + progress = progress % 1; + + position.copy(curve.getPointAt(progress)); + position.y += 0.3; + + train.position.copy(position); + + tangent.copy(curve.getTangentAt(progress)); + + velocity -= tangent.y * 0.0000001 * delta; + velocity = Math.max(0.00004, Math.min(0.0002, velocity)); + + train.lookAt(lookAt.copy(position).sub(tangent)); + + // + + renderer.render(scene, camera); + + prevTime = time; +} diff --git a/examples-testing/examples/webxr_vr_sandbox.ts b/examples-testing/examples/webxr_vr_sandbox.ts new file mode 100644 index 000000000..9e8e75909 --- /dev/null +++ b/examples-testing/examples/webxr_vr_sandbox.ts @@ -0,0 +1,192 @@ +import * as THREE from 'three'; + +import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; +import { Reflector } from 'three/addons/objects/Reflector.js'; +import { VRButton } from 'three/addons/webxr/VRButton.js'; + +import { HTMLMesh } from 'three/addons/interactive/HTMLMesh.js'; +import { InteractiveGroup } from 'three/addons/interactive/InteractiveGroup.js'; +import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js'; + +import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import Stats from 'three/addons/libs/stats.module.js'; + +let camera, scene, renderer; +let reflector; +let stats, statsMesh; + +const parameters = { + radius: 0.6, + tube: 0.2, + tubularSegments: 150, + radialSegments: 20, + p: 2, + q: 3, + thickness: 0.5, +}; + +init(); + +function init() { + scene = new THREE.Scene(); + + new RGBELoader().setPath('textures/equirectangular/').load('moonless_golf_1k.hdr', function (texture) { + texture.mapping = THREE.EquirectangularReflectionMapping; + + scene.background = texture; + scene.environment = texture; + }); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 10); + camera.position.set(0, 1.6, 1.5); + + // + + const torusGeometry = new THREE.TorusKnotGeometry(...Object.values(parameters)); + const torusMaterial = new THREE.MeshPhysicalMaterial({ + transmission: 1.0, + roughness: 0, + metalness: 0.25, + thickness: 0.5, + side: THREE.DoubleSide, + }); + const torus = new THREE.Mesh(torusGeometry, torusMaterial); + torus.name = 'torus'; + torus.position.y = 1.5; + torus.position.z = -2; + scene.add(torus); + + const cylinderGeometry = new THREE.CylinderGeometry(1, 1, 0.1, 50); + const cylinderMaterial = new THREE.MeshStandardMaterial(); + const cylinder = new THREE.Mesh(cylinderGeometry, cylinderMaterial); + cylinder.position.z = -2; + scene.add(cylinder); + + // + + reflector = new Reflector(new THREE.PlaneGeometry(2, 2), { + textureWidth: window.innerWidth, + textureHeight: window.innerHeight, + }); + reflector.position.x = 1; + reflector.position.y = 1.5; + reflector.position.z = -3; + reflector.rotation.y = -Math.PI / 4; + scene.add(reflector); + + const frameGeometry = new THREE.BoxGeometry(2.1, 2.1, 0.1); + const frameMaterial = new THREE.MeshPhongMaterial(); + const frame = new THREE.Mesh(frameGeometry, frameMaterial); + frame.position.z = -0.07; + reflector.add(frame); + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.autoClear = false; + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.xr.enabled = true; + renderer.toneMapping = THREE.ACESFilmicToneMapping; + renderer.toneMappingExposure = 1; + document.body.appendChild(renderer.domElement); + + document.body.appendChild(VRButton.createButton(renderer)); + + window.addEventListener('resize', onWindowResize); + + // + + const geometry = new THREE.BufferGeometry(); + geometry.setFromPoints([new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 0, -5)]); + + const controller1 = renderer.xr.getController(0); + controller1.add(new THREE.Line(geometry)); + scene.add(controller1); + + const controller2 = renderer.xr.getController(1); + controller2.add(new THREE.Line(geometry)); + scene.add(controller2); + + // + + const controllerModelFactory = new XRControllerModelFactory(); + + const controllerGrip1 = renderer.xr.getControllerGrip(0); + controllerGrip1.add(controllerModelFactory.createControllerModel(controllerGrip1)); + scene.add(controllerGrip1); + + const controllerGrip2 = renderer.xr.getControllerGrip(1); + controllerGrip2.add(controllerModelFactory.createControllerModel(controllerGrip2)); + scene.add(controllerGrip2); + + // GUI + + function onChange() { + torus.geometry.dispose(); + torus.geometry = new THREE.TorusKnotGeometry(...Object.values(parameters)); + } + + function onThicknessChange() { + torus.material.thickness = parameters.thickness; + } + + const gui = new GUI({ width: 300 }); + gui.add(parameters, 'radius', 0.0, 1.0).onChange(onChange); + gui.add(parameters, 'tube', 0.0, 1.0).onChange(onChange); + gui.add(parameters, 'tubularSegments', 10, 150, 1).onChange(onChange); + gui.add(parameters, 'radialSegments', 2, 20, 1).onChange(onChange); + gui.add(parameters, 'p', 1, 10, 1).onChange(onChange); + gui.add(parameters, 'q', 0, 10, 1).onChange(onChange); + gui.add(parameters, 'thickness', 0, 1).onChange(onThicknessChange); + gui.domElement.style.visibility = 'hidden'; + + const group = new InteractiveGroup(); + group.listenToPointerEvents(renderer, camera); + group.listenToXRControllerEvents(controller1); + group.listenToXRControllerEvents(controller2); + scene.add(group); + + const mesh = new HTMLMesh(gui.domElement); + mesh.position.x = -0.75; + mesh.position.y = 1.5; + mesh.position.z = -0.5; + mesh.rotation.y = Math.PI / 4; + mesh.scale.setScalar(2); + group.add(mesh); + + // Add stats.js + stats = new Stats(); + stats.dom.style.width = '80px'; + stats.dom.style.height = '48px'; + document.body.appendChild(stats.dom); + + statsMesh = new HTMLMesh(stats.dom); + statsMesh.position.x = -0.75; + statsMesh.position.y = 2; + statsMesh.position.z = -0.6; + statsMesh.rotation.y = Math.PI / 4; + statsMesh.scale.setScalar(2.5); + group.add(statsMesh); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + const time = performance.now() * 0.0002; + const torus = scene.getObjectByName('torus'); + torus.rotation.x = time * 0.4; + torus.rotation.y = time; + + renderer.render(scene, camera); + stats.update(); + + // Canvas elements doesn't trigger DOM updates, so we have to update the texture + statsMesh.material.map.update(); +} diff --git a/examples-testing/examples/webxr_vr_video.ts b/examples-testing/examples/webxr_vr_video.ts new file mode 100644 index 000000000..50a990412 --- /dev/null +++ b/examples-testing/examples/webxr_vr_video.ts @@ -0,0 +1,92 @@ +import * as THREE from 'three'; +import { VRButton } from 'three/addons/webxr/VRButton.js'; + +let camera, scene, renderer; + +init(); + +function init() { + const container = document.getElementById('container'); + container.addEventListener('click', function () { + video.play(); + }); + + camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 2000); + camera.layers.enable(1); // render left view when no stereo available + + // video + + const video = document.getElementById('video'); + video.play(); + + const texture = new THREE.VideoTexture(video); + texture.colorSpace = THREE.SRGBColorSpace; + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x101010); + + // left + + const geometry1 = new THREE.SphereGeometry(500, 60, 40); + // invert the geometry on the x-axis so that all of the faces point inward + geometry1.scale(-1, 1, 1); + + const uvs1 = geometry1.attributes.uv.array; + + for (let i = 0; i < uvs1.length; i += 2) { + uvs1[i] *= 0.5; + } + + const material1 = new THREE.MeshBasicMaterial({ map: texture }); + + const mesh1 = new THREE.Mesh(geometry1, material1); + mesh1.rotation.y = -Math.PI / 2; + mesh1.layers.set(1); // display in left eye only + scene.add(mesh1); + + // right + + const geometry2 = new THREE.SphereGeometry(500, 60, 40); + geometry2.scale(-1, 1, 1); + + const uvs2 = geometry2.attributes.uv.array; + + for (let i = 0; i < uvs2.length; i += 2) { + uvs2[i] *= 0.5; + uvs2[i] += 0.5; + } + + const material2 = new THREE.MeshBasicMaterial({ map: texture }); + + const mesh2 = new THREE.Mesh(geometry2, material2); + mesh2.rotation.y = -Math.PI / 2; + mesh2.layers.set(2); // display in right eye only + scene.add(mesh2); + + // + + renderer = new THREE.WebGLRenderer(); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.xr.enabled = true; + renderer.xr.setReferenceSpaceType('local'); + container.appendChild(renderer.domElement); + + document.body.appendChild(VRButton.createButton(renderer)); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webxr_xr_controls_transform.ts b/examples-testing/examples/webxr_xr_controls_transform.ts new file mode 100644 index 000000000..f3b4796e6 --- /dev/null +++ b/examples-testing/examples/webxr_xr_controls_transform.ts @@ -0,0 +1,210 @@ +import * as THREE from 'three'; +import { TransformControls } from 'three/addons/controls/TransformControls.js'; +import { XRButton } from 'three/addons/webxr/XRButton.js'; +import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js'; + +let container; +let camera, scene, renderer; +let controller1, controller2, line; +let controllerGrip1, controllerGrip2; + +let raycaster; + +let controls, group; + +init(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x808080); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 10); + camera.position.set(0, 1.6, 0); + + const floorGeometry = new THREE.PlaneGeometry(6, 6); + const floorMaterial = new THREE.ShadowMaterial({ + opacity: 0.25, + blending: THREE.CustomBlending, + transparent: false, + }); + const floor = new THREE.Mesh(floorGeometry, floorMaterial); + floor.rotation.x = -Math.PI / 2; + floor.receiveShadow = true; + scene.add(floor); + + scene.add(new THREE.HemisphereLight(0xbcbcbc, 0xa5a5a5, 3)); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0, 6, 0); + light.castShadow = true; + light.shadow.camera.top = 3; + light.shadow.camera.bottom = -3; + light.shadow.camera.right = 3; + light.shadow.camera.left = -3; + light.shadow.mapSize.set(4096, 4096); + scene.add(light); + + group = new THREE.Group(); + scene.add(group); + + const geometries = [ + new THREE.BoxGeometry(0.2, 0.2, 0.2), + new THREE.ConeGeometry(0.2, 0.4, 64), + new THREE.CylinderGeometry(0.2, 0.2, 0.2, 64), + new THREE.IcosahedronGeometry(0.2, 8), + new THREE.TorusGeometry(0.2, 0.04, 64, 32), + ]; + + for (let i = 0; i < 16; i++) { + const geometry = geometries[Math.floor(Math.random() * geometries.length)]; + const material = new THREE.MeshStandardMaterial({ + color: Math.random() * 0xffffff, + roughness: 0.7, + metalness: 0.0, + }); + + const object = new THREE.Mesh(geometry, material); + + object.position.x = Math.random() - 0.5; + object.position.y = Math.random() * 2 + 0.5; + object.position.z = Math.random() - 2.5; + + object.rotation.x = Math.random() * 2 * Math.PI; + object.rotation.y = Math.random() * 2 * Math.PI; + object.rotation.z = Math.random() * 2 * Math.PI; + + object.scale.setScalar(Math.random() + 0.5); + + object.castShadow = true; + object.receiveShadow = true; + + group.add(object); + } + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.setAnimationLoop(animate); + renderer.shadowMap.enabled = true; + renderer.xr.enabled = true; + container.appendChild(renderer.domElement); + + document.body.appendChild(XRButton.createButton(renderer)); + + // controllers + + controller1 = renderer.xr.getController(0); + controller1.addEventListener('select', onSelect); + controller1.addEventListener('selectstart', onControllerEvent); + controller1.addEventListener('selectend', onControllerEvent); + controller1.addEventListener('move', onControllerEvent); + controller1.userData.active = false; + scene.add(controller1); + + controller2 = renderer.xr.getController(1); + controller2.addEventListener('select', onSelect); + controller2.addEventListener('selectstart', onControllerEvent); + controller2.addEventListener('selectend', onControllerEvent); + controller2.addEventListener('move', onControllerEvent); + controller2.userData.active = true; + scene.add(controller2); + + const controllerModelFactory = new XRControllerModelFactory(); + + controllerGrip1 = renderer.xr.getControllerGrip(0); + controllerGrip1.add(controllerModelFactory.createControllerModel(controllerGrip1)); + scene.add(controllerGrip1); + + controllerGrip2 = renderer.xr.getControllerGrip(1); + controllerGrip2.add(controllerModelFactory.createControllerModel(controllerGrip2)); + scene.add(controllerGrip2); + + // + + const geometry = new THREE.BufferGeometry().setFromPoints([ + new THREE.Vector3(0, 0, 0), + new THREE.Vector3(0, 0, -1), + ]); + + line = new THREE.Line(geometry); + line.name = 'line'; + line.scale.z = 5; + + raycaster = new THREE.Raycaster(); + + // controls + + controls = new TransformControls(camera, renderer.domElement); + controls.attach(group.children[0]); + scene.add(controls); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onSelect(event) { + const controller = event.target; + + controller1.userData.active = false; + controller2.userData.active = false; + + if (controller === controller1) { + controller1.userData.active = true; + controller1.add(line); + } + + if (controller === controller2) { + controller2.userData.active = true; + controller2.add(line); + } + + raycaster.setFromXRController(controller); + + const intersects = raycaster.intersectObjects(group.children); + + if (intersects.length > 0) { + controls.attach(intersects[0].object); + } +} + +function onControllerEvent(event) { + const controller = event.target; + + if (controller.userData.active === false) return; + + controls.getRaycaster().setFromXRController(controller); + + switch (event.type) { + case 'selectstart': + controls.pointerDown(null); + break; + + case 'selectend': + controls.pointerUp(null); + break; + + case 'move': + controls.pointerHover(null); + controls.pointerMove(null); + break; + } +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +// + +function animate() { + renderer.render(scene, camera); +} diff --git a/examples-testing/examples/webxr_xr_dragging_custom_depth.ts b/examples-testing/examples/webxr_xr_dragging_custom_depth.ts new file mode 100644 index 000000000..2cd50ba4c --- /dev/null +++ b/examples-testing/examples/webxr_xr_dragging_custom_depth.ts @@ -0,0 +1,395 @@ +import * as THREE from 'three'; +import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +import { XRButton } from 'three/addons/webxr/XRButton.js'; +import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js'; + +let container; +let camera, scene, renderer; +let controller1, controller2; +let controllerGrip1, controllerGrip2; +let isDepthSupplied = false; + +let raycaster; + +const intersected = []; +const tempMatrix = new THREE.Matrix4(); + +let controls, group; + +init(); +animate(); + +function init() { + container = document.createElement('div'); + document.body.appendChild(container); + + scene = new THREE.Scene(); + scene.background = new THREE.Color(0x808080); + + camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 10); + camera.position.set(0, 1.6, 3); + + controls = new OrbitControls(camera, container); + controls.target.set(0, 1.6, 0); + controls.update(); + + const floorGeometry = new THREE.PlaneGeometry(6, 6); + const floorMaterial = new THREE.ShadowMaterial({ + opacity: 0.25, + blending: THREE.CustomBlending, + transparent: false, + }); + const floor = new THREE.Mesh(floorGeometry, floorMaterial); + floor.rotation.x = -Math.PI / 2; + floor.receiveShadow = true; + scene.add(floor); + + scene.add(new THREE.HemisphereLight(0xbcbcbc, 0xa5a5a5, 3)); + + const light = new THREE.DirectionalLight(0xffffff, 3); + light.position.set(0, 6, 0); + light.castShadow = true; + light.shadow.camera.top = 3; + light.shadow.camera.bottom = -3; + light.shadow.camera.right = 3; + light.shadow.camera.left = -3; + light.shadow.mapSize.set(4096, 4096); + scene.add(light); + + group = new THREE.Group(); + scene.add(group); + + const geometries = [ + new THREE.BoxGeometry(0.2, 0.2, 0.2), + new THREE.ConeGeometry(0.2, 0.2, 64), + new THREE.CylinderGeometry(0.2, 0.2, 0.2, 64), + new THREE.IcosahedronGeometry(0.2, 8), + new THREE.TorusGeometry(0.2, 0.04, 64, 32), + ]; + + for (let i = 0; i < 50; i++) { + const geometry = geometries[Math.floor(Math.random() * geometries.length)]; + const material = new THREE.ShaderMaterial({ + vertexShader: /* glsl */ ` + varying vec3 vNormal; + varying vec2 vUv; + void main() { + vNormal = normalize(normalMatrix * normal); + vUv = uv; + gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); + } + `, + + fragmentShader: /* glsl */ ` + uniform vec3 diffuseColor; + uniform float roughness; + uniform float metalness; + uniform float emissive; + varying vec3 vNormal; + varying vec2 vUv; + uniform sampler2DArray depthColor; + uniform float depthWidth; + uniform float depthHeight; + #define saturate( a ) clamp( a, 0.0, 1.0 ) + float Depth_GetCameraDepthInMeters(const sampler2DArray depthTexture, + const vec2 depthUv, int arrayIndex) { + return texture(depthColor, vec3(depthUv.x, depthUv.y, arrayIndex)).r; + } + float Depth_GetOcclusion(const sampler2DArray depthTexture, const vec2 depthUv, float assetDepthM, int arrayIndex) { + float depthMm = Depth_GetCameraDepthInMeters(depthTexture, depthUv, arrayIndex); + const float kDepthTolerancePerM = 0.001; + return clamp(1.0 - + 0.5 * (depthMm - assetDepthM) / + (kDepthTolerancePerM * assetDepthM) + + 0.5, 0.0, 1.0); + } + float Depth_GetBlurredOcclusionAroundUV(const sampler2DArray depthTexture, const vec2 uv, float assetDepthM, int arrayIndex) { + // Kernel used: + // 0 4 7 4 0 + // 4 16 26 16 4 + // 7 26 41 26 7 + // 4 16 26 16 4 + // 0 4 7 4 0 + const float kKernelTotalWeights = 269.0; + float sum = 0.0; + const float kOcclusionBlurAmount = 0.0005; + vec2 blurriness = + vec2(kOcclusionBlurAmount, kOcclusionBlurAmount /** u_DepthAspectRatio*/); + float current = 0.0; + current += Depth_GetOcclusion( + depthTexture, uv + vec2(-1.0, -2.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(+1.0, -2.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(-1.0, +2.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(+1.0, +2.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(-2.0, +1.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(+2.0, +1.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(-2.0, -1.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(+2.0, -1.0) * blurriness, assetDepthM, arrayIndex); + sum += current * 4.0; + current = 0.0; + current += Depth_GetOcclusion( + depthTexture, uv + vec2(-2.0, -0.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(+2.0, +0.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(+0.0, +2.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(-0.0, -2.0) * blurriness, assetDepthM, arrayIndex); + sum += current * 7.0; + current = 0.0; + current += Depth_GetOcclusion( + depthTexture, uv + vec2(-1.0, -1.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(+1.0, -1.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(-1.0, +1.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(+1.0, +1.0) * blurriness, assetDepthM, arrayIndex); + sum += current * 16.0; + current = 0.0; + current += Depth_GetOcclusion( + depthTexture, uv + vec2(+0.0, +1.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(-0.0, -1.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(-1.0, -0.0) * blurriness, assetDepthM, arrayIndex); + current += Depth_GetOcclusion( + depthTexture, uv + vec2(+1.0, +0.0) * blurriness, assetDepthM, arrayIndex); + sum += current * 26.0; + sum += Depth_GetOcclusion(depthTexture, uv, assetDepthM, arrayIndex) * 41.0; + return sum / kKernelTotalWeights; + } + void main() { + vec3 normal = normalize(vNormal); + vec3 diffuse = diffuseColor; + float specularIntensity = pow(max(dot(normal, normalize(vec3(0, 0, 1))), 0.0), 64.0); + vec3 specular = vec3(specularIntensity) * mix(vec3(0.04), diffuse, metalness); + gl_FragColor = vec4(diffuse * (1.0 - specular) + specular, 1.0) * (1.0 + emissive); + + if (depthWidth > 0.0) { + int arrayIndex = 0; + vec2 depthUv; + if (gl_FragCoord.x>=depthWidth) { + arrayIndex = 1; + depthUv = vec2((gl_FragCoord.x-depthWidth)/depthWidth, gl_FragCoord.y/depthHeight); + } else { + depthUv = vec2(gl_FragCoord.x/depthWidth, gl_FragCoord.y/depthHeight); + } + float assetDepthM = gl_FragCoord.z; + + float occlusion = Depth_GetBlurredOcclusionAroundUV(depthColor, depthUv, assetDepthM, arrayIndex); + float depthMm = Depth_GetCameraDepthInMeters(depthColor, depthUv, arrayIndex); + + float absDistance = abs(assetDepthM - depthMm); + float v = 0.0025; + absDistance = saturate(v - absDistance) / v; + + gl_FragColor.rgb += vec3(absDistance * 2.0, absDistance * 2.0, absDistance * 12.0); + gl_FragColor = mix(gl_FragColor, vec4(0.0, 0.0, 0.0, 0.0), occlusion * 0.7); + } + } + `, + + uniforms: { + diffuseColor: { value: new THREE.Color(Math.random() * 0xffffff) }, + roughness: { value: 0.7 }, + metalness: { value: 0.0 }, + emissive: { value: 0.0 }, + depthWidth: { value: 0.0 }, + depthHeight: { value: 0.0 }, + depthColor: { value: new THREE.Texture() }, + }, + }); + + const object = new THREE.Mesh(geometry, material); + + object.position.x = Math.random() * 4 - 2; + object.position.y = Math.random() * 2; + object.position.z = Math.random() * 4 - 2; + + object.rotation.x = Math.random() * 2 * Math.PI; + object.rotation.y = Math.random() * 2 * Math.PI; + object.rotation.z = Math.random() * 2 * Math.PI; + + object.scale.setScalar(Math.random() + 0.5); + + group.add(object); + } + + // + + renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer.setPixelRatio(window.devicePixelRatio); + renderer.setSize(window.innerWidth, window.innerHeight); + renderer.shadowMap.enabled = true; + renderer.xr.enabled = true; + container.appendChild(renderer.domElement); + + document.body.appendChild( + XRButton.createButton(renderer, { + optionalFeatures: ['depth-sensing'], + depthSensing: { usagePreference: ['gpu-optimized'], dataFormatPreference: [] }, + }), + ); + + // controllers + + controller1 = renderer.xr.getController(0); + controller1.addEventListener('selectstart', onSelectStart); + controller1.addEventListener('selectend', onSelectEnd); + scene.add(controller1); + + controller2 = renderer.xr.getController(1); + controller2.addEventListener('selectstart', onSelectStart); + controller2.addEventListener('selectend', onSelectEnd); + scene.add(controller2); + + const controllerModelFactory = new XRControllerModelFactory(); + + controllerGrip1 = renderer.xr.getControllerGrip(0); + controllerGrip1.add(controllerModelFactory.createControllerModel(controllerGrip1)); + scene.add(controllerGrip1); + + controllerGrip2 = renderer.xr.getControllerGrip(1); + controllerGrip2.add(controllerModelFactory.createControllerModel(controllerGrip2)); + scene.add(controllerGrip2); + + // + + const geometry = new THREE.BufferGeometry().setFromPoints([ + new THREE.Vector3(0, 0, 0), + new THREE.Vector3(0, 0, -1), + ]); + + const line = new THREE.Line(geometry); + line.name = 'line'; + line.scale.z = 5; + + controller1.add(line.clone()); + controller2.add(line.clone()); + + raycaster = new THREE.Raycaster(); + + // + + window.addEventListener('resize', onWindowResize); +} + +function onWindowResize() { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + + renderer.setSize(window.innerWidth, window.innerHeight); +} + +function onSelectStart(event) { + const controller = event.target; + + const intersections = getIntersections(controller); + + if (intersections.length > 0) { + const intersection = intersections[0]; + + const object = intersection.object; + object.material.uniforms.emissive.value = 1; + controller.attach(object); + + controller.userData.selected = object; + } + + controller.userData.targetRayMode = event.data.targetRayMode; +} + +function onSelectEnd(event) { + const controller = event.target; + + if (controller.userData.selected !== undefined) { + const object = controller.userData.selected; + object.material.uniforms.emissive.value = 0; + group.attach(object); + + controller.userData.selected = undefined; + } +} + +function getIntersections(controller) { + controller.updateMatrixWorld(); + + tempMatrix.identity().extractRotation(controller.matrixWorld); + + raycaster.ray.origin.setFromMatrixPosition(controller.matrixWorld); + raycaster.ray.direction.set(0, 0, -1).applyMatrix4(tempMatrix); + + return raycaster.intersectObjects(group.children, false); +} + +function intersectObjects(controller) { + // Do not highlight in mobile-ar + + if (controller.userData.targetRayMode === 'screen') return; + + // Do not highlight when already selected + + if (controller.userData.selected !== undefined) return; + + const line = controller.getObjectByName('line'); + const intersections = getIntersections(controller); + + if (intersections.length > 0) { + const intersection = intersections[0]; + + const object = intersection.object; + object.material.uniforms.emissive.value = 1; + intersected.push(object); + + line.scale.z = intersection.distance; + } else { + line.scale.z = 5; + } +} + +function cleanIntersected() { + while (intersected.length) { + const object = intersected.pop(); + object.material.uniforms.emissive.value = 0; + } +} + +// + +function animate() { + renderer.setAnimationLoop(render); +} + +function render() { + if (renderer.xr.hasDepthSensing() && !isDepthSupplied) { + group.children.forEach(child => { + child.material.uniforms.depthColor.value = renderer.xr.getDepthTexture(); + child.material.uniforms.depthWidth.value = 1680; + child.material.uniforms.depthHeight.value = 1760; + + isDepthSupplied = true; + }); + } else if (!renderer.xr.hasDepthSensing() && isDepthSupplied) { + group.children.forEach(child => { + child.material.uniforms.depthWidth.value = 0; + child.material.uniforms.depthHeight.value = 0; + + isDepthSupplied = false; + }); + } + + cleanIntersected(); + + intersectObjects(controller1); + intersectObjects(controller2); + + renderer.render(scene, camera); +} From 504878d1f906dc257b6544370b521d5a73503755 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Sat, 24 Aug 2024 15:47:08 -0400 Subject: [PATCH 7/7] Update patch and delete examples --- examples-testing/changes.patch | 34 + examples-testing/examples/css2d_label.ts | 186 ---- examples-testing/examples/css3d_molecules.ts | 353 -------- .../examples/css3d_orthographic.ts | 208 ----- .../examples/css3d_periodictable.ts | 793 ------------------ examples-testing/examples/css3d_sandbox.ts | 180 ---- examples-testing/examples/css3d_sprites.ts | 157 ---- examples-testing/examples/css3d_youtube.ts | 79 -- examples-testing/examples/games_fps.ts | 372 -------- .../examples/misc_animation_groups.ts | 125 --- .../examples/misc_animation_keys.ts | 129 --- .../examples/misc_boxselection.ts | 137 --- .../examples/misc_controls_arcball.ts | 210 ----- .../examples/misc_controls_drag.ts | 153 ---- .../examples/misc_controls_fly.ts | 214 ----- .../examples/misc_controls_map.ts | 98 --- .../examples/misc_controls_orbit.ts | 89 -- .../examples/misc_controls_pointerlock.ts | 245 ------ .../examples/misc_controls_trackball.ts | 134 --- .../examples/misc_controls_transform.ts | 181 ---- .../examples/misc_exporter_draco.ts | 117 --- .../examples/misc_exporter_exr.ts | 158 ---- .../examples/misc_exporter_gltf.ts | 507 ----------- .../examples/misc_exporter_obj.ts | 192 ----- .../examples/misc_exporter_ply.ts | 156 ---- .../examples/misc_exporter_stl.ts | 129 --- .../examples/misc_exporter_usdz.ts | 129 --- examples-testing/examples/misc_lookat.ts | 95 --- examples-testing/examples/misc_uv_tests.ts | 44 - .../examples/physics_ammo_instancing.ts | 119 --- .../examples/physics_jolt_instancing.ts | 119 --- .../examples/physics_rapier_instancing.ts | 119 --- examples-testing/examples/svg_lines.ts | 87 -- examples-testing/examples/svg_sandbox.ts | 212 ----- .../examples/webaudio_orientation.ts | 141 ---- examples-testing/examples/webaudio_sandbox.ts | 222 ----- examples-testing/examples/webaudio_timing.ts | 152 ---- .../examples/webaudio_visualizer.ts | 86 -- .../examples/webgl_animation_keyframes.ts | 80 -- .../examples/webgl_animation_multiple.ts | 197 ----- .../webgl_animation_skinning_morph.ts | 187 ----- .../examples/webgl_buffergeometry.ts | 178 ---- ...webgl_buffergeometry_attributes_integer.ts | 113 --- .../webgl_buffergeometry_attributes_none.ts | 56 -- ...fergeometry_custom_attributes_particles.ts | 103 --- .../webgl_buffergeometry_drawrange.ts | 239 ------ .../webgl_buffergeometry_glbufferattribute.ts | 139 --- .../examples/webgl_buffergeometry_indexed.ts | 137 --- .../webgl_buffergeometry_instancing.ts | 138 --- ...gl_buffergeometry_instancing_billboards.ts | 86 -- ...l_buffergeometry_instancing_interleaved.ts | 152 ---- .../examples/webgl_buffergeometry_lines.ts | 118 --- .../webgl_buffergeometry_lines_indexed.ts | 179 ---- .../examples/webgl_buffergeometry_points.ts | 109 --- ...webgl_buffergeometry_points_interleaved.ts | 122 --- .../webgl_buffergeometry_rawshader.ts | 97 --- .../webgl_buffergeometry_selective_draw.ts | 150 ---- .../examples/webgl_buffergeometry_uint.ts | 177 ---- examples-testing/examples/webgl_camera.ts | 218 ----- .../examples/webgl_camera_array.ts | 104 --- .../webgl_camera_logarithmicdepthbuffer.ts | 248 ------ .../examples/webgl_clipculldistance.ts | 110 --- examples-testing/examples/webgl_clipping.ts | 195 ----- .../examples/webgl_clipping_advanced.ts | 355 -------- .../examples/webgl_clipping_intersection.ts | 137 --- .../examples/webgl_clipping_stencil.ts | 260 ------ .../examples/webgl_custom_attributes.ts | 100 --- .../examples/webgl_custom_attributes_lines.ts | 121 --- .../webgl_custom_attributes_points.ts | 117 --- .../webgl_custom_attributes_points2.ts | 193 ----- .../webgl_custom_attributes_points3.ts | 200 ----- examples-testing/examples/webgl_decals.ts | 237 ------ .../examples/webgl_effects_anaglyph.ts | 114 --- .../examples/webgl_effects_ascii.ts | 81 -- .../examples/webgl_effects_parallaxbarrier.ts | 110 --- .../examples/webgl_effects_peppersghost.ts | 85 -- .../examples/webgl_effects_stereo.ts | 98 --- .../examples/webgl_framebuffer_texture.ts | 151 ---- .../examples/webgl_furnace_test.ts | 96 --- examples-testing/examples/webgl_geometries.ts | 137 --- .../examples/webgl_geometries_parametric.ts | 124 --- .../examples/webgl_geometry_colors.ts | 176 ---- .../webgl_geometry_colors_lookuptable.ts | 148 ---- .../examples/webgl_geometry_convex.ts | 117 --- .../examples/webgl_geometry_cube.ts | 46 - .../examples/webgl_geometry_dynamic.ts | 97 --- .../examples/webgl_geometry_extrude_shapes.ts | 149 ---- .../webgl_geometry_extrude_splines.ts | 310 ------- .../examples/webgl_geometry_minecraft.ts | 183 ---- .../examples/webgl_geometry_nurbs.ts | 298 ------- .../examples/webgl_geometry_sdf.ts | 164 ---- .../examples/webgl_geometry_shapes.ts | 363 -------- .../examples/webgl_geometry_teapot.ts | 180 ---- .../examples/webgl_geometry_terrain.ts | 173 ---- .../webgl_geometry_terrain_raycast.ts | 206 ----- .../examples/webgl_geometry_text.ts | 312 ------- .../examples/webgl_geometry_text_shapes.ts | 112 --- .../examples/webgl_geometry_text_stroke.ts | 116 --- .../examples/webgl_gpgpu_birds.ts | 313 ------- .../examples/webgl_gpgpu_birds_gltf.ts | 415 --------- .../examples/webgl_gpgpu_protoplanet.ts | 280 ------- .../examples/webgl_gpgpu_water.ts | 397 --------- examples-testing/examples/webgl_helpers.ts | 117 --- .../examples/webgl_instancing_dynamic.ts | 103 --- .../examples/webgl_instancing_morph.ts | 147 ---- .../examples/webgl_instancing_performance.ts | 262 ------ .../examples/webgl_instancing_raycast.ts | 116 --- .../examples/webgl_instancing_scatter.ts | 257 ------ .../webgl_interactive_buffergeometry.ts | 244 ------ .../examples/webgl_interactive_cubes.ts | 114 --- .../examples/webgl_interactive_cubes_gpu.ts | 229 ----- .../examples/webgl_interactive_cubes_ortho.ts | 129 --- .../examples/webgl_interactive_lines.ts | 160 ---- .../examples/webgl_interactive_points.ts | 143 ---- .../webgl_interactive_raycasting_points.ts | 220 ----- .../webgl_interactive_voxelpainter.ts | 158 ---- examples-testing/examples/webgl_layers.ts | 125 --- examples-testing/examples/webgl_lensflares.ts | 137 --- examples-testing/examples/webgl_lightprobe.ts | 133 --- .../examples/webgl_lightprobe_cubecamera.ts | 83 -- .../examples/webgl_lights_hemisphere.ts | 188 ----- .../examples/webgl_lights_physical.ts | 237 ------ .../examples/webgl_lights_pointlights.ts | 100 --- .../examples/webgl_lights_rectarealight.ts | 79 -- .../examples/webgl_lights_spotlight.ts | 183 ---- .../examples/webgl_lights_spotlights.ts | 133 --- .../examples/webgl_lines_colors.ts | 181 ---- .../examples/webgl_lines_dashed.ts | 186 ---- examples-testing/examples/webgl_lines_fat.ts | 251 ------ .../examples/webgl_lines_fat_raycasting.ts | 294 ------- .../examples/webgl_lines_fat_wireframe.ts | 210 ----- examples-testing/examples/webgl_loader_3dm.ts | 95 --- examples-testing/examples/webgl_loader_3ds.ts | 62 -- examples-testing/examples/webgl_loader_3mf.ts | 105 --- .../examples/webgl_loader_3mf_materials.ts | 106 --- examples-testing/examples/webgl_loader_amf.ts | 62 -- examples-testing/examples/webgl_loader_bvh.ts | 61 -- .../examples/webgl_loader_collada.ts | 83 -- .../examples/webgl_loader_collada_skinning.ts | 97 --- .../examples/webgl_loader_draco.ts | 85 -- examples-testing/examples/webgl_loader_fbx.ts | 162 ---- .../examples/webgl_loader_fbx_nurbs.ts | 61 -- .../examples/webgl_loader_gcode.ts | 49 -- .../examples/webgl_loader_gltf.ts | 74 -- .../examples/webgl_loader_gltf_anisotropy.ts | 68 -- .../examples/webgl_loader_gltf_avif.ts | 61 -- .../examples/webgl_loader_gltf_compressed.ts | 83 -- .../examples/webgl_loader_gltf_dispersion.ts | 66 -- .../examples/webgl_loader_gltf_instancing.ts | 69 -- .../examples/webgl_loader_gltf_iridescence.ts | 66 -- .../examples/webgl_loader_gltf_lights.ts | 83 -- .../examples/webgl_loader_gltf_sheen.ts | 72 -- .../webgl_loader_gltf_transmission.ts | 80 -- .../examples/webgl_loader_imagebitmap.ts | 109 --- examples-testing/examples/webgl_loader_kmz.ts | 59 -- examples-testing/examples/webgl_loader_lwo.ts | 69 -- .../examples/webgl_loader_md2_control.ts | 289 ------- examples-testing/examples/webgl_loader_mdd.ts | 62 -- examples-testing/examples/webgl_loader_obj.ts | 98 --- .../examples/webgl_loader_obj_mtl.ts | 82 -- examples-testing/examples/webgl_loader_pcd.ts | 65 -- examples-testing/examples/webgl_loader_pdb.ts | 208 ----- examples-testing/examples/webgl_loader_ply.ts | 146 ---- examples-testing/examples/webgl_loader_svg.ts | 193 ----- .../examples/webgl_loader_texture_dds.ts | 207 ----- .../examples/webgl_loader_texture_ktx.ts | 137 --- .../examples/webgl_loader_texture_logluv.ts | 75 -- .../examples/webgl_loader_texture_rgbm.ts | 75 -- .../examples/webgl_loader_texture_tga.ts | 90 -- .../examples/webgl_loader_texture_tiff.ts | 87 -- .../examples/webgl_loader_texture_ultrahdr.ts | 101 --- .../examples/webgl_loader_tilt.ts | 54 -- examples-testing/examples/webgl_loader_ttf.ts | 231 ----- .../examples/webgl_loader_usdz.ts | 68 -- examples-testing/examples/webgl_loader_vox.ts | 104 --- .../examples/webgl_loader_vrml.ts | 118 --- examples-testing/examples/webgl_loader_vtk.ts | 123 --- examples-testing/examples/webgl_loader_xyz.ts | 62 -- examples-testing/examples/webgl_lod.ts | 88 -- .../examples/webgl_marchingcubes.ts | 311 ------- .../examples/webgl_materials_alphahash.ts | 178 ---- .../examples/webgl_materials_blending.ts | 147 ---- .../webgl_materials_blending_custom.ts | 214 ----- .../examples/webgl_materials_bumpmap.ts | 140 ---- .../examples/webgl_materials_car.ts | 167 ---- .../examples/webgl_materials_cubemap.ts | 115 --- .../webgl_materials_cubemap_dynamic.ts | 115 --- .../webgl_materials_cubemap_mipmaps.ts | 119 --- .../webgl_materials_cubemap_refraction.ts | 126 --- ...bgl_materials_cubemap_render_to_mipmaps.ts | 183 ---- .../examples/webgl_materials_curvature.ts | 252 ------ .../webgl_materials_displacementmap.ts | 224 ----- .../examples/webgl_materials_envmaps.ts | 131 --- .../examples/webgl_materials_envmaps_exr.ts | 153 ---- ...webgl_materials_envmaps_groundprojected.ts | 150 ---- .../examples/webgl_materials_envmaps_hdr.ts | 176 ---- .../examples/webgl_materials_modified.ts | 115 --- .../webgl_materials_normalmap_object_space.ts | 82 -- .../webgl_materials_physical_clearcoat.ts | 208 ----- .../webgl_materials_physical_transmission.ts | 182 ---- ...l_materials_physical_transmission_alpha.ts | 192 ----- .../webgl_materials_texture_anisotropy.ts | 143 ---- .../webgl_materials_texture_canvas.ts | 92 -- .../webgl_materials_texture_filters.ts | 164 ---- .../webgl_materials_texture_manualmipmap.ts | 175 ---- .../webgl_materials_texture_partialupdate.ts | 100 --- .../webgl_materials_texture_rotation.ts | 113 --- .../examples/webgl_materials_toon.ts | 152 ---- .../examples/webgl_materials_video.ts | 208 ----- .../examples/webgl_materials_video_webcam.ts | 79 -- .../examples/webgl_materials_wireframe.ts | 107 --- examples-testing/examples/webgl_math_obb.ts | 189 ----- .../webgl_math_orientation_transform.ts | 95 --- examples-testing/examples/webgl_mesh_batch.ts | 305 ------- examples-testing/examples/webgl_mirror.ts | 168 ---- .../examples/webgl_modifier_edgesplit.ts | 136 --- .../examples/webgl_modifier_simplifier.ts | 77 -- .../examples/webgl_modifier_tessellation.ts | 142 ---- .../examples/webgl_morphtargets.ts | 120 --- .../examples/webgl_morphtargets_face.ts | 105 --- .../examples/webgl_morphtargets_horse.ts | 100 --- .../examples/webgl_morphtargets_sphere.ts | 105 --- .../examples/webgl_multiple_elements.ts | 139 --- .../examples/webgl_multiple_rendertargets.ts | 133 --- .../webgl_multiple_scenes_comparison.ts | 98 --- .../examples/webgl_multiple_views.ts | 237 ------ .../webgl_multisampled_renderbuffers.ts | 133 --- .../examples/webgl_panorama_cube.ts | 83 -- .../webgl_panorama_equirectangular.ts | 142 ---- examples-testing/examples/webgl_pmrem_test.ts | 141 ---- .../examples/webgl_points_billboards.ts | 120 --- .../examples/webgl_points_sprites.ts | 167 ---- .../examples/webgl_points_waves.ts | 145 ---- examples-testing/examples/webgl_portal.ts | 218 ----- .../examples/webgl_postprocessing.ts | 86 -- .../examples/webgl_postprocessing_advanced.ts | 304 ------- .../webgl_postprocessing_afterimage.ts | 72 -- .../webgl_postprocessing_backgrounds.ts | 214 ----- .../examples/webgl_postprocessing_fxaa.ts | 132 --- .../examples/webgl_postprocessing_glitch.ts | 97 --- .../examples/webgl_postprocessing_godrays.ts | 347 -------- .../examples/webgl_postprocessing_gtao.ts | 215 ----- .../examples/webgl_postprocessing_masking.ts | 100 --- .../webgl_postprocessing_material_ao.ts | 277 ------ .../examples/webgl_postprocessing_outline.ts | 282 ------- .../examples/webgl_postprocessing_pixel.ts | 228 ----- .../webgl_postprocessing_procedural.ts | 77 -- .../webgl_postprocessing_rgb_halftone.ts | 167 ---- .../examples/webgl_postprocessing_sao.ts | 137 --- .../examples/webgl_postprocessing_smaa.ts | 109 --- .../examples/webgl_postprocessing_sobel.ts | 111 --- .../examples/webgl_postprocessing_ssaa.ts | 206 ----- .../examples/webgl_postprocessing_ssao.ts | 118 --- .../examples/webgl_postprocessing_ssr.ts | 261 ------ .../examples/webgl_postprocessing_taa.ts | 139 --- .../webgl_postprocessing_transition.ts | 211 ----- .../webgl_postprocessing_unreal_bloom.ts | 136 --- ...l_postprocessing_unreal_bloom_selective.ts | 195 ----- .../examples/webgl_raycaster_sprite.ts | 103 --- .../examples/webgl_raycaster_texture.ts | 286 ------- .../examples/webgl_raymarching_reflect.ts | 95 --- .../examples/webgl_read_float_buffer.ts | 153 ---- examples-testing/examples/webgl_refraction.ts | 135 --- examples-testing/examples/webgl_rtt.ts | 171 ---- examples-testing/examples/webgl_shader.ts | 50 -- .../examples/webgl_shader_lava.ts | 101 --- .../examples/webgl_shaders_ocean.ts | 169 ---- .../examples/webgl_shaders_sky.ts | 103 --- .../examples/webgl_shadow_contact.ts | 272 ------ examples-testing/examples/webgl_shadowmap.ts | 311 ------- .../examples/webgl_shadowmap_csm.ts | 253 ------ .../examples/webgl_shadowmap_pcss.ts | 161 ---- .../examples/webgl_shadowmap_performance.ts | 281 ------- .../examples/webgl_shadowmap_pointlight.ts | 139 --- .../examples/webgl_shadowmap_progressive.ts | 204 ----- .../examples/webgl_shadowmap_viewer.ts | 178 ---- .../examples/webgl_shadowmap_vsm.ts | 200 ----- examples-testing/examples/webgl_shadowmesh.ts | 250 ------ examples-testing/examples/webgl_simple_gi.ts | 172 ---- examples-testing/examples/webgl_sprites.ts | 187 ----- .../examples/webgl_test_memory.ts | 65 -- .../examples/webgl_test_memory2.ts | 81 -- .../examples/webgl_test_wide_gamut.ts | 118 --- .../webgl_texture2darray_compressed.ts | 88 -- .../webgl_texture2darray_layerupdate.ts | 124 --- examples-testing/examples/webgl_texture3d.ts | 128 --- .../examples/webgl_texture3d_partialupdate.ts | 326 ------- .../examples/webgl_tonemapping.ts | 163 ---- examples-testing/examples/webgl_ubo.ts | 137 --- examples-testing/examples/webgl_ubo_arrays.ts | 171 ---- .../examples/webgl_video_kinect.ts | 113 --- .../webgl_video_panorama_equirectangular.ts | 95 --- .../examples/webgl_volume_cloud.ts | 279 ------ .../examples/webgl_volume_instancing.ts | 192 ----- .../examples/webgl_volume_perlin.ts | 208 ----- examples-testing/examples/webgl_water.ts | 162 ---- .../examples/webgl_water_flowmap.ts | 100 --- .../examples/webgpu_backdrop_area.ts | 162 ---- .../webgpu_camera_logarithmicdepthbuffer.ts | 245 ------ examples-testing/examples/webgpu_clearcoat.ts | 205 ----- examples-testing/examples/webgpu_clipping.ts | 207 ----- .../examples/webgpu_custom_fog_background.ts | 93 -- .../examples/webgpu_instancing_morph.ts | 148 ---- .../examples/webgpu_lights_ies_spotlight.ts | 117 --- .../examples/webgpu_lights_rectarealight.ts | 79 -- .../examples/webgpu_loader_gltf.ts | 71 -- .../examples/webgpu_loader_gltf_anisotropy.ts | 65 -- .../examples/webgpu_loader_gltf_compressed.ts | 67 -- .../examples/webgpu_loader_gltf_dispersion.ts | 63 -- .../webgpu_loader_gltf_iridescence.ts | 70 -- .../examples/webgpu_loader_gltf_sheen.ts | 81 -- .../webgpu_loader_gltf_transmission.ts | 80 -- .../examples/webgpu_materials_basic.ts | 137 --- .../webgpu_materials_displacementmap.ts | 224 ----- .../examples/webgpu_materials_lightmap.ts | 94 --- .../examples/webgpu_materials_toon.ts | 148 ---- .../examples/webgpu_materials_transmission.ts | 168 ---- .../examples/webgpu_materials_video.ts | 184 ---- .../examples/webgpu_mesh_batch.ts | 271 ------ .../examples/webgpu_morphtargets.ts | 121 --- .../examples/webgpu_morphtargets_face.ts | 102 --- examples-testing/examples/webgpu_mrt.ts | 119 --- .../webgpu_multiple_rendertargets_readback.ts | 156 ---- examples-testing/examples/webgpu_ocean.ts | 161 ---- .../examples/webgpu_parallax_uv.ts | 112 --- .../examples/webgpu_postprocessing.ts | 77 -- .../examples/webgpu_postprocessing_3dlut.ts | 139 --- .../webgpu_postprocessing_afterimage.ts | 70 -- .../examples/webgpu_postprocessing_ao.ts | 204 ----- .../examples/webgpu_postprocessing_bloom.ts | 127 --- .../webgpu_postprocessing_bloom_emissive.ts | 101 --- .../webgpu_postprocessing_bloom_selective.ts | 122 --- .../webgpu_postprocessing_difference.ts | 92 -- .../examples/webgpu_postprocessing_dof.ts | 159 ---- .../examples/webgpu_postprocessing_fxaa.ts | 122 --- .../examples/webgpu_postprocessing_masking.ts | 85 -- .../examples/webgpu_postprocessing_pixel.ts | 234 ------ .../examples/webgpu_postprocessing_sobel.ts | 89 -- .../webgpu_postprocessing_transition.ts | 200 ----- .../examples/webgpu_procedural_texture.ts | 74 -- .../examples/webgpu_refraction.ts | 141 ---- examples-testing/examples/webgpu_sky.ts | 95 --- .../examples/webgpu_textures_anisotropy.ts | 155 ---- .../examples/webgpu_textures_partialupdate.ts | 103 --- .../examples/webgpu_tsl_coffee_smoke.ts | 132 --- .../examples/webgpu_tsl_vfx_flames.ts | 203 ----- .../examples/webgpu_video_panorama.ts | 99 --- examples-testing/examples/webgpu_water.ts | 171 ---- examples-testing/examples/webxr_ar_cones.ts | 66 -- examples-testing/examples/webxr_ar_hittest.ts | 115 --- .../examples/webxr_ar_lighting.ts | 124 --- .../examples/webxr_ar_plane_detection.ts | 46 - .../examples/webxr_vr_handinput.ts | 126 --- .../examples/webxr_vr_panorama.ts | 92 -- .../examples/webxr_vr_panorama_depth.ts | 90 -- .../examples/webxr_vr_rollercoaster.ts | 211 ----- examples-testing/examples/webxr_vr_sandbox.ts | 192 ----- examples-testing/examples/webxr_vr_video.ts | 92 -- .../examples/webxr_xr_controls_transform.ts | 210 ----- .../webxr_xr_dragging_custom_depth.ts | 395 --------- .../nodes/display/ColorAdjustmentNode.d.ts | 2 +- 361 files changed, 35 insertions(+), 54959 deletions(-) delete mode 100644 examples-testing/examples/css2d_label.ts delete mode 100644 examples-testing/examples/css3d_molecules.ts delete mode 100644 examples-testing/examples/css3d_orthographic.ts delete mode 100644 examples-testing/examples/css3d_periodictable.ts delete mode 100644 examples-testing/examples/css3d_sandbox.ts delete mode 100644 examples-testing/examples/css3d_sprites.ts delete mode 100644 examples-testing/examples/css3d_youtube.ts delete mode 100644 examples-testing/examples/games_fps.ts delete mode 100644 examples-testing/examples/misc_animation_groups.ts delete mode 100644 examples-testing/examples/misc_animation_keys.ts delete mode 100644 examples-testing/examples/misc_boxselection.ts delete mode 100644 examples-testing/examples/misc_controls_arcball.ts delete mode 100644 examples-testing/examples/misc_controls_drag.ts delete mode 100644 examples-testing/examples/misc_controls_fly.ts delete mode 100644 examples-testing/examples/misc_controls_map.ts delete mode 100644 examples-testing/examples/misc_controls_orbit.ts delete mode 100644 examples-testing/examples/misc_controls_pointerlock.ts delete mode 100644 examples-testing/examples/misc_controls_trackball.ts delete mode 100644 examples-testing/examples/misc_controls_transform.ts delete mode 100644 examples-testing/examples/misc_exporter_draco.ts delete mode 100644 examples-testing/examples/misc_exporter_exr.ts delete mode 100644 examples-testing/examples/misc_exporter_gltf.ts delete mode 100644 examples-testing/examples/misc_exporter_obj.ts delete mode 100644 examples-testing/examples/misc_exporter_ply.ts delete mode 100644 examples-testing/examples/misc_exporter_stl.ts delete mode 100644 examples-testing/examples/misc_exporter_usdz.ts delete mode 100644 examples-testing/examples/misc_lookat.ts delete mode 100644 examples-testing/examples/misc_uv_tests.ts delete mode 100644 examples-testing/examples/physics_ammo_instancing.ts delete mode 100644 examples-testing/examples/physics_jolt_instancing.ts delete mode 100644 examples-testing/examples/physics_rapier_instancing.ts delete mode 100644 examples-testing/examples/svg_lines.ts delete mode 100644 examples-testing/examples/svg_sandbox.ts delete mode 100644 examples-testing/examples/webaudio_orientation.ts delete mode 100644 examples-testing/examples/webaudio_sandbox.ts delete mode 100644 examples-testing/examples/webaudio_timing.ts delete mode 100644 examples-testing/examples/webaudio_visualizer.ts delete mode 100644 examples-testing/examples/webgl_animation_keyframes.ts delete mode 100644 examples-testing/examples/webgl_animation_multiple.ts delete mode 100644 examples-testing/examples/webgl_animation_skinning_morph.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_attributes_integer.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_attributes_none.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_custom_attributes_particles.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_drawrange.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_glbufferattribute.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_indexed.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_instancing.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_instancing_billboards.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_instancing_interleaved.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_lines.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_lines_indexed.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_points.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_points_interleaved.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_rawshader.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_selective_draw.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry_uint.ts delete mode 100644 examples-testing/examples/webgl_camera.ts delete mode 100644 examples-testing/examples/webgl_camera_array.ts delete mode 100644 examples-testing/examples/webgl_camera_logarithmicdepthbuffer.ts delete mode 100644 examples-testing/examples/webgl_clipculldistance.ts delete mode 100644 examples-testing/examples/webgl_clipping.ts delete mode 100644 examples-testing/examples/webgl_clipping_advanced.ts delete mode 100644 examples-testing/examples/webgl_clipping_intersection.ts delete mode 100644 examples-testing/examples/webgl_clipping_stencil.ts delete mode 100644 examples-testing/examples/webgl_custom_attributes.ts delete mode 100644 examples-testing/examples/webgl_custom_attributes_lines.ts delete mode 100644 examples-testing/examples/webgl_custom_attributes_points.ts delete mode 100644 examples-testing/examples/webgl_custom_attributes_points2.ts delete mode 100644 examples-testing/examples/webgl_custom_attributes_points3.ts delete mode 100644 examples-testing/examples/webgl_decals.ts delete mode 100644 examples-testing/examples/webgl_effects_anaglyph.ts delete mode 100644 examples-testing/examples/webgl_effects_ascii.ts delete mode 100644 examples-testing/examples/webgl_effects_parallaxbarrier.ts delete mode 100644 examples-testing/examples/webgl_effects_peppersghost.ts delete mode 100644 examples-testing/examples/webgl_effects_stereo.ts delete mode 100644 examples-testing/examples/webgl_framebuffer_texture.ts delete mode 100644 examples-testing/examples/webgl_furnace_test.ts delete mode 100644 examples-testing/examples/webgl_geometries.ts delete mode 100644 examples-testing/examples/webgl_geometries_parametric.ts delete mode 100644 examples-testing/examples/webgl_geometry_colors.ts delete mode 100644 examples-testing/examples/webgl_geometry_colors_lookuptable.ts delete mode 100644 examples-testing/examples/webgl_geometry_convex.ts delete mode 100644 examples-testing/examples/webgl_geometry_cube.ts delete mode 100644 examples-testing/examples/webgl_geometry_dynamic.ts delete mode 100644 examples-testing/examples/webgl_geometry_extrude_shapes.ts delete mode 100644 examples-testing/examples/webgl_geometry_extrude_splines.ts delete mode 100644 examples-testing/examples/webgl_geometry_minecraft.ts delete mode 100644 examples-testing/examples/webgl_geometry_nurbs.ts delete mode 100644 examples-testing/examples/webgl_geometry_sdf.ts delete mode 100644 examples-testing/examples/webgl_geometry_shapes.ts delete mode 100644 examples-testing/examples/webgl_geometry_teapot.ts delete mode 100644 examples-testing/examples/webgl_geometry_terrain.ts delete mode 100644 examples-testing/examples/webgl_geometry_terrain_raycast.ts delete mode 100644 examples-testing/examples/webgl_geometry_text.ts delete mode 100644 examples-testing/examples/webgl_geometry_text_shapes.ts delete mode 100644 examples-testing/examples/webgl_geometry_text_stroke.ts delete mode 100644 examples-testing/examples/webgl_gpgpu_birds.ts delete mode 100644 examples-testing/examples/webgl_gpgpu_birds_gltf.ts delete mode 100644 examples-testing/examples/webgl_gpgpu_protoplanet.ts delete mode 100644 examples-testing/examples/webgl_gpgpu_water.ts delete mode 100644 examples-testing/examples/webgl_helpers.ts delete mode 100644 examples-testing/examples/webgl_instancing_dynamic.ts delete mode 100644 examples-testing/examples/webgl_instancing_morph.ts delete mode 100644 examples-testing/examples/webgl_instancing_performance.ts delete mode 100644 examples-testing/examples/webgl_instancing_raycast.ts delete mode 100644 examples-testing/examples/webgl_instancing_scatter.ts delete mode 100644 examples-testing/examples/webgl_interactive_buffergeometry.ts delete mode 100644 examples-testing/examples/webgl_interactive_cubes.ts delete mode 100644 examples-testing/examples/webgl_interactive_cubes_gpu.ts delete mode 100644 examples-testing/examples/webgl_interactive_cubes_ortho.ts delete mode 100644 examples-testing/examples/webgl_interactive_lines.ts delete mode 100644 examples-testing/examples/webgl_interactive_points.ts delete mode 100644 examples-testing/examples/webgl_interactive_raycasting_points.ts delete mode 100644 examples-testing/examples/webgl_interactive_voxelpainter.ts delete mode 100644 examples-testing/examples/webgl_layers.ts delete mode 100644 examples-testing/examples/webgl_lensflares.ts delete mode 100644 examples-testing/examples/webgl_lightprobe.ts delete mode 100644 examples-testing/examples/webgl_lightprobe_cubecamera.ts delete mode 100644 examples-testing/examples/webgl_lights_hemisphere.ts delete mode 100644 examples-testing/examples/webgl_lights_physical.ts delete mode 100644 examples-testing/examples/webgl_lights_pointlights.ts delete mode 100644 examples-testing/examples/webgl_lights_rectarealight.ts delete mode 100644 examples-testing/examples/webgl_lights_spotlight.ts delete mode 100644 examples-testing/examples/webgl_lights_spotlights.ts delete mode 100644 examples-testing/examples/webgl_lines_colors.ts delete mode 100644 examples-testing/examples/webgl_lines_dashed.ts delete mode 100644 examples-testing/examples/webgl_lines_fat.ts delete mode 100644 examples-testing/examples/webgl_lines_fat_raycasting.ts delete mode 100644 examples-testing/examples/webgl_lines_fat_wireframe.ts delete mode 100644 examples-testing/examples/webgl_loader_3dm.ts delete mode 100644 examples-testing/examples/webgl_loader_3ds.ts delete mode 100644 examples-testing/examples/webgl_loader_3mf.ts delete mode 100644 examples-testing/examples/webgl_loader_3mf_materials.ts delete mode 100644 examples-testing/examples/webgl_loader_amf.ts delete mode 100644 examples-testing/examples/webgl_loader_bvh.ts delete mode 100644 examples-testing/examples/webgl_loader_collada.ts delete mode 100644 examples-testing/examples/webgl_loader_collada_skinning.ts delete mode 100644 examples-testing/examples/webgl_loader_draco.ts delete mode 100644 examples-testing/examples/webgl_loader_fbx.ts delete mode 100644 examples-testing/examples/webgl_loader_fbx_nurbs.ts delete mode 100644 examples-testing/examples/webgl_loader_gcode.ts delete mode 100644 examples-testing/examples/webgl_loader_gltf.ts delete mode 100644 examples-testing/examples/webgl_loader_gltf_anisotropy.ts delete mode 100644 examples-testing/examples/webgl_loader_gltf_avif.ts delete mode 100644 examples-testing/examples/webgl_loader_gltf_compressed.ts delete mode 100644 examples-testing/examples/webgl_loader_gltf_dispersion.ts delete mode 100644 examples-testing/examples/webgl_loader_gltf_instancing.ts delete mode 100644 examples-testing/examples/webgl_loader_gltf_iridescence.ts delete mode 100644 examples-testing/examples/webgl_loader_gltf_lights.ts delete mode 100644 examples-testing/examples/webgl_loader_gltf_sheen.ts delete mode 100644 examples-testing/examples/webgl_loader_gltf_transmission.ts delete mode 100644 examples-testing/examples/webgl_loader_imagebitmap.ts delete mode 100644 examples-testing/examples/webgl_loader_kmz.ts delete mode 100644 examples-testing/examples/webgl_loader_lwo.ts delete mode 100644 examples-testing/examples/webgl_loader_md2_control.ts delete mode 100644 examples-testing/examples/webgl_loader_mdd.ts delete mode 100644 examples-testing/examples/webgl_loader_obj.ts delete mode 100644 examples-testing/examples/webgl_loader_obj_mtl.ts delete mode 100644 examples-testing/examples/webgl_loader_pcd.ts delete mode 100644 examples-testing/examples/webgl_loader_pdb.ts delete mode 100644 examples-testing/examples/webgl_loader_ply.ts delete mode 100644 examples-testing/examples/webgl_loader_svg.ts delete mode 100644 examples-testing/examples/webgl_loader_texture_dds.ts delete mode 100644 examples-testing/examples/webgl_loader_texture_ktx.ts delete mode 100644 examples-testing/examples/webgl_loader_texture_logluv.ts delete mode 100644 examples-testing/examples/webgl_loader_texture_rgbm.ts delete mode 100644 examples-testing/examples/webgl_loader_texture_tga.ts delete mode 100644 examples-testing/examples/webgl_loader_texture_tiff.ts delete mode 100644 examples-testing/examples/webgl_loader_texture_ultrahdr.ts delete mode 100644 examples-testing/examples/webgl_loader_tilt.ts delete mode 100644 examples-testing/examples/webgl_loader_ttf.ts delete mode 100644 examples-testing/examples/webgl_loader_usdz.ts delete mode 100644 examples-testing/examples/webgl_loader_vox.ts delete mode 100644 examples-testing/examples/webgl_loader_vrml.ts delete mode 100644 examples-testing/examples/webgl_loader_vtk.ts delete mode 100644 examples-testing/examples/webgl_loader_xyz.ts delete mode 100644 examples-testing/examples/webgl_lod.ts delete mode 100644 examples-testing/examples/webgl_marchingcubes.ts delete mode 100644 examples-testing/examples/webgl_materials_alphahash.ts delete mode 100644 examples-testing/examples/webgl_materials_blending.ts delete mode 100644 examples-testing/examples/webgl_materials_blending_custom.ts delete mode 100644 examples-testing/examples/webgl_materials_bumpmap.ts delete mode 100644 examples-testing/examples/webgl_materials_car.ts delete mode 100644 examples-testing/examples/webgl_materials_cubemap.ts delete mode 100644 examples-testing/examples/webgl_materials_cubemap_dynamic.ts delete mode 100644 examples-testing/examples/webgl_materials_cubemap_mipmaps.ts delete mode 100644 examples-testing/examples/webgl_materials_cubemap_refraction.ts delete mode 100644 examples-testing/examples/webgl_materials_cubemap_render_to_mipmaps.ts delete mode 100644 examples-testing/examples/webgl_materials_curvature.ts delete mode 100644 examples-testing/examples/webgl_materials_displacementmap.ts delete mode 100644 examples-testing/examples/webgl_materials_envmaps.ts delete mode 100644 examples-testing/examples/webgl_materials_envmaps_exr.ts delete mode 100644 examples-testing/examples/webgl_materials_envmaps_groundprojected.ts delete mode 100644 examples-testing/examples/webgl_materials_envmaps_hdr.ts delete mode 100644 examples-testing/examples/webgl_materials_modified.ts delete mode 100644 examples-testing/examples/webgl_materials_normalmap_object_space.ts delete mode 100644 examples-testing/examples/webgl_materials_physical_clearcoat.ts delete mode 100644 examples-testing/examples/webgl_materials_physical_transmission.ts delete mode 100644 examples-testing/examples/webgl_materials_physical_transmission_alpha.ts delete mode 100644 examples-testing/examples/webgl_materials_texture_anisotropy.ts delete mode 100644 examples-testing/examples/webgl_materials_texture_canvas.ts delete mode 100644 examples-testing/examples/webgl_materials_texture_filters.ts delete mode 100644 examples-testing/examples/webgl_materials_texture_manualmipmap.ts delete mode 100644 examples-testing/examples/webgl_materials_texture_partialupdate.ts delete mode 100644 examples-testing/examples/webgl_materials_texture_rotation.ts delete mode 100644 examples-testing/examples/webgl_materials_toon.ts delete mode 100644 examples-testing/examples/webgl_materials_video.ts delete mode 100644 examples-testing/examples/webgl_materials_video_webcam.ts delete mode 100644 examples-testing/examples/webgl_materials_wireframe.ts delete mode 100644 examples-testing/examples/webgl_math_obb.ts delete mode 100644 examples-testing/examples/webgl_math_orientation_transform.ts delete mode 100644 examples-testing/examples/webgl_mesh_batch.ts delete mode 100644 examples-testing/examples/webgl_mirror.ts delete mode 100644 examples-testing/examples/webgl_modifier_edgesplit.ts delete mode 100644 examples-testing/examples/webgl_modifier_simplifier.ts delete mode 100644 examples-testing/examples/webgl_modifier_tessellation.ts delete mode 100644 examples-testing/examples/webgl_morphtargets.ts delete mode 100644 examples-testing/examples/webgl_morphtargets_face.ts delete mode 100644 examples-testing/examples/webgl_morphtargets_horse.ts delete mode 100644 examples-testing/examples/webgl_morphtargets_sphere.ts delete mode 100644 examples-testing/examples/webgl_multiple_elements.ts delete mode 100644 examples-testing/examples/webgl_multiple_rendertargets.ts delete mode 100644 examples-testing/examples/webgl_multiple_scenes_comparison.ts delete mode 100644 examples-testing/examples/webgl_multiple_views.ts delete mode 100644 examples-testing/examples/webgl_multisampled_renderbuffers.ts delete mode 100644 examples-testing/examples/webgl_panorama_cube.ts delete mode 100644 examples-testing/examples/webgl_panorama_equirectangular.ts delete mode 100644 examples-testing/examples/webgl_pmrem_test.ts delete mode 100644 examples-testing/examples/webgl_points_billboards.ts delete mode 100644 examples-testing/examples/webgl_points_sprites.ts delete mode 100644 examples-testing/examples/webgl_points_waves.ts delete mode 100644 examples-testing/examples/webgl_portal.ts delete mode 100644 examples-testing/examples/webgl_postprocessing.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_advanced.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_afterimage.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_backgrounds.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_fxaa.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_glitch.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_godrays.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_gtao.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_masking.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_material_ao.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_outline.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_pixel.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_procedural.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_rgb_halftone.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_sao.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_smaa.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_sobel.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_ssaa.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_ssao.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_ssr.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_taa.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_transition.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_unreal_bloom.ts delete mode 100644 examples-testing/examples/webgl_postprocessing_unreal_bloom_selective.ts delete mode 100644 examples-testing/examples/webgl_raycaster_sprite.ts delete mode 100644 examples-testing/examples/webgl_raycaster_texture.ts delete mode 100644 examples-testing/examples/webgl_raymarching_reflect.ts delete mode 100644 examples-testing/examples/webgl_read_float_buffer.ts delete mode 100644 examples-testing/examples/webgl_refraction.ts delete mode 100644 examples-testing/examples/webgl_rtt.ts delete mode 100644 examples-testing/examples/webgl_shader.ts delete mode 100644 examples-testing/examples/webgl_shader_lava.ts delete mode 100644 examples-testing/examples/webgl_shaders_ocean.ts delete mode 100644 examples-testing/examples/webgl_shaders_sky.ts delete mode 100644 examples-testing/examples/webgl_shadow_contact.ts delete mode 100644 examples-testing/examples/webgl_shadowmap.ts delete mode 100644 examples-testing/examples/webgl_shadowmap_csm.ts delete mode 100644 examples-testing/examples/webgl_shadowmap_pcss.ts delete mode 100644 examples-testing/examples/webgl_shadowmap_performance.ts delete mode 100644 examples-testing/examples/webgl_shadowmap_pointlight.ts delete mode 100644 examples-testing/examples/webgl_shadowmap_progressive.ts delete mode 100644 examples-testing/examples/webgl_shadowmap_viewer.ts delete mode 100644 examples-testing/examples/webgl_shadowmap_vsm.ts delete mode 100644 examples-testing/examples/webgl_shadowmesh.ts delete mode 100644 examples-testing/examples/webgl_simple_gi.ts delete mode 100644 examples-testing/examples/webgl_sprites.ts delete mode 100644 examples-testing/examples/webgl_test_memory.ts delete mode 100644 examples-testing/examples/webgl_test_memory2.ts delete mode 100644 examples-testing/examples/webgl_test_wide_gamut.ts delete mode 100644 examples-testing/examples/webgl_texture2darray_compressed.ts delete mode 100644 examples-testing/examples/webgl_texture2darray_layerupdate.ts delete mode 100644 examples-testing/examples/webgl_texture3d.ts delete mode 100644 examples-testing/examples/webgl_texture3d_partialupdate.ts delete mode 100644 examples-testing/examples/webgl_tonemapping.ts delete mode 100644 examples-testing/examples/webgl_ubo.ts delete mode 100644 examples-testing/examples/webgl_ubo_arrays.ts delete mode 100644 examples-testing/examples/webgl_video_kinect.ts delete mode 100644 examples-testing/examples/webgl_video_panorama_equirectangular.ts delete mode 100644 examples-testing/examples/webgl_volume_cloud.ts delete mode 100644 examples-testing/examples/webgl_volume_instancing.ts delete mode 100644 examples-testing/examples/webgl_volume_perlin.ts delete mode 100644 examples-testing/examples/webgl_water.ts delete mode 100644 examples-testing/examples/webgl_water_flowmap.ts delete mode 100644 examples-testing/examples/webgpu_backdrop_area.ts delete mode 100644 examples-testing/examples/webgpu_camera_logarithmicdepthbuffer.ts delete mode 100644 examples-testing/examples/webgpu_clearcoat.ts delete mode 100644 examples-testing/examples/webgpu_clipping.ts delete mode 100644 examples-testing/examples/webgpu_custom_fog_background.ts delete mode 100644 examples-testing/examples/webgpu_instancing_morph.ts delete mode 100644 examples-testing/examples/webgpu_lights_ies_spotlight.ts delete mode 100644 examples-testing/examples/webgpu_lights_rectarealight.ts delete mode 100644 examples-testing/examples/webgpu_loader_gltf.ts delete mode 100644 examples-testing/examples/webgpu_loader_gltf_anisotropy.ts delete mode 100644 examples-testing/examples/webgpu_loader_gltf_compressed.ts delete mode 100644 examples-testing/examples/webgpu_loader_gltf_dispersion.ts delete mode 100644 examples-testing/examples/webgpu_loader_gltf_iridescence.ts delete mode 100644 examples-testing/examples/webgpu_loader_gltf_sheen.ts delete mode 100644 examples-testing/examples/webgpu_loader_gltf_transmission.ts delete mode 100644 examples-testing/examples/webgpu_materials_basic.ts delete mode 100644 examples-testing/examples/webgpu_materials_displacementmap.ts delete mode 100644 examples-testing/examples/webgpu_materials_lightmap.ts delete mode 100644 examples-testing/examples/webgpu_materials_toon.ts delete mode 100644 examples-testing/examples/webgpu_materials_transmission.ts delete mode 100644 examples-testing/examples/webgpu_materials_video.ts delete mode 100644 examples-testing/examples/webgpu_mesh_batch.ts delete mode 100644 examples-testing/examples/webgpu_morphtargets.ts delete mode 100644 examples-testing/examples/webgpu_morphtargets_face.ts delete mode 100644 examples-testing/examples/webgpu_mrt.ts delete mode 100644 examples-testing/examples/webgpu_multiple_rendertargets_readback.ts delete mode 100644 examples-testing/examples/webgpu_ocean.ts delete mode 100644 examples-testing/examples/webgpu_parallax_uv.ts delete mode 100644 examples-testing/examples/webgpu_postprocessing.ts delete mode 100644 examples-testing/examples/webgpu_postprocessing_3dlut.ts delete mode 100644 examples-testing/examples/webgpu_postprocessing_afterimage.ts delete mode 100644 examples-testing/examples/webgpu_postprocessing_ao.ts delete mode 100644 examples-testing/examples/webgpu_postprocessing_bloom.ts delete mode 100644 examples-testing/examples/webgpu_postprocessing_bloom_emissive.ts delete mode 100644 examples-testing/examples/webgpu_postprocessing_bloom_selective.ts delete mode 100644 examples-testing/examples/webgpu_postprocessing_difference.ts delete mode 100644 examples-testing/examples/webgpu_postprocessing_dof.ts delete mode 100644 examples-testing/examples/webgpu_postprocessing_fxaa.ts delete mode 100644 examples-testing/examples/webgpu_postprocessing_masking.ts delete mode 100644 examples-testing/examples/webgpu_postprocessing_pixel.ts delete mode 100644 examples-testing/examples/webgpu_postprocessing_sobel.ts delete mode 100644 examples-testing/examples/webgpu_postprocessing_transition.ts delete mode 100644 examples-testing/examples/webgpu_procedural_texture.ts delete mode 100644 examples-testing/examples/webgpu_refraction.ts delete mode 100644 examples-testing/examples/webgpu_sky.ts delete mode 100644 examples-testing/examples/webgpu_textures_anisotropy.ts delete mode 100644 examples-testing/examples/webgpu_textures_partialupdate.ts delete mode 100644 examples-testing/examples/webgpu_tsl_coffee_smoke.ts delete mode 100644 examples-testing/examples/webgpu_tsl_vfx_flames.ts delete mode 100644 examples-testing/examples/webgpu_video_panorama.ts delete mode 100644 examples-testing/examples/webgpu_water.ts delete mode 100644 examples-testing/examples/webxr_ar_cones.ts delete mode 100644 examples-testing/examples/webxr_ar_hittest.ts delete mode 100644 examples-testing/examples/webxr_ar_lighting.ts delete mode 100644 examples-testing/examples/webxr_ar_plane_detection.ts delete mode 100644 examples-testing/examples/webxr_vr_handinput.ts delete mode 100644 examples-testing/examples/webxr_vr_panorama.ts delete mode 100644 examples-testing/examples/webxr_vr_panorama_depth.ts delete mode 100644 examples-testing/examples/webxr_vr_rollercoaster.ts delete mode 100644 examples-testing/examples/webxr_vr_sandbox.ts delete mode 100644 examples-testing/examples/webxr_vr_video.ts delete mode 100644 examples-testing/examples/webxr_xr_controls_transform.ts delete mode 100644 examples-testing/examples/webxr_xr_dragging_custom_depth.ts diff --git a/examples-testing/changes.patch b/examples-testing/changes.patch index f7cfa0488..b106af37c 100644 --- a/examples-testing/changes.patch +++ b/examples-testing/changes.patch @@ -14773,6 +14773,27 @@ index 7140a8ad..a2dae285 100644 bloomIntensity.value = bloomIntensity.value === 0 ? 1 : 0; } }); +diff --git a/examples-testing/examples/webgpu_postprocessing_difference.ts b/examples-testing/examples/webgpu_postprocessing_difference.ts +index 49f9084f..aafb0475 100644 +--- a/examples-testing/examples/webgpu_postprocessing_difference.ts ++++ b/examples-testing/examples/webgpu_postprocessing_difference.ts +@@ -1,4 +1,4 @@ +-import * as THREE from 'three'; ++import * as THREE from 'three/webgpu'; + import { pass, luminance } from 'three/tsl'; + + import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; +@@ -10,8 +10,8 @@ const params = { + speed: 0, + }; + +-let camera, renderer, postProcessing; +-let timer, mesh, controls; ++let camera: THREE.PerspectiveCamera, renderer: THREE.WebGPURenderer, postProcessing: THREE.PostProcessing; ++let timer: Timer, mesh: THREE.Mesh, controls: OrbitControls; + + init(); + diff --git a/examples-testing/examples/webgpu_postprocessing_dof.ts b/examples-testing/examples/webgpu_postprocessing_dof.ts index 3fb4046b..785a78f8 100644 --- a/examples-testing/examples/webgpu_postprocessing_dof.ts @@ -15822,3 +15843,16 @@ index 2cd50ba4..b97f3eee 100644 isDepthSupplied = false; }); +diff --git a/types/three/src/nodes/display/ColorAdjustmentNode.d.ts b/types/three/src/nodes/display/ColorAdjustmentNode.d.ts +index b49345bb..517407fc 100644 +--- a/types/three/src/nodes/display/ColorAdjustmentNode.d.ts ++++ b/types/three/src/nodes/display/ColorAdjustmentNode.d.ts +@@ -33,7 +33,7 @@ export const hue: ( + adjustmentNode?: NodeRepresentation, + ) => ShaderNodeObject; + +-export const luminance: (a: NodeRepresentation, b: NodeRepresentation) => ShaderNodeObject; ++export const luminance: (a: NodeRepresentation, b?: NodeRepresentation) => ShaderNodeObject; + + export const threshold: (color: NodeRepresentation, thershold: NodeRepresentation) => ShaderNodeObject; + diff --git a/examples-testing/examples/css2d_label.ts b/examples-testing/examples/css2d_label.ts deleted file mode 100644 index 48a2d1f05..000000000 --- a/examples-testing/examples/css2d_label.ts +++ /dev/null @@ -1,186 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { CSS2DRenderer, CSS2DObject } from 'three/addons/renderers/CSS2DRenderer.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let gui; - -let camera, scene, renderer, labelRenderer; - -const layers = { - 'Toggle Name': function () { - camera.layers.toggle(0); - }, - 'Toggle Mass': function () { - camera.layers.toggle(1); - }, - 'Enable All': function () { - camera.layers.enableAll(); - }, - - 'Disable All': function () { - camera.layers.disableAll(); - }, -}; - -const clock = new THREE.Clock(); -const textureLoader = new THREE.TextureLoader(); - -let moon; - -init(); -animate(); - -function init() { - const EARTH_RADIUS = 1; - const MOON_RADIUS = 0.27; - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200); - camera.position.set(10, 5, 20); - camera.layers.enableAll(); - - scene = new THREE.Scene(); - - const dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.position.set(0, 0, 1); - dirLight.layers.enableAll(); - scene.add(dirLight); - - const axesHelper = new THREE.AxesHelper(5); - axesHelper.layers.enableAll(); - scene.add(axesHelper); - - // - - const earthGeometry = new THREE.SphereGeometry(EARTH_RADIUS, 16, 16); - const earthMaterial = new THREE.MeshPhongMaterial({ - specular: 0x333333, - shininess: 5, - map: textureLoader.load('textures/planets/earth_atmos_2048.jpg'), - specularMap: textureLoader.load('textures/planets/earth_specular_2048.jpg'), - normalMap: textureLoader.load('textures/planets/earth_normal_2048.jpg'), - normalScale: new THREE.Vector2(0.85, 0.85), - }); - earthMaterial.map.colorSpace = THREE.SRGBColorSpace; - const earth = new THREE.Mesh(earthGeometry, earthMaterial); - scene.add(earth); - - const moonGeometry = new THREE.SphereGeometry(MOON_RADIUS, 16, 16); - const moonMaterial = new THREE.MeshPhongMaterial({ - shininess: 5, - map: textureLoader.load('textures/planets/moon_1024.jpg'), - }); - moonMaterial.map.colorSpace = THREE.SRGBColorSpace; - moon = new THREE.Mesh(moonGeometry, moonMaterial); - scene.add(moon); - - // - - earth.layers.enableAll(); - moon.layers.enableAll(); - - const earthDiv = document.createElement('div'); - earthDiv.className = 'label'; - earthDiv.textContent = 'Earth'; - earthDiv.style.backgroundColor = 'transparent'; - - const earthLabel = new CSS2DObject(earthDiv); - earthLabel.position.set(1.5 * EARTH_RADIUS, 0, 0); - earthLabel.center.set(0, 1); - earth.add(earthLabel); - earthLabel.layers.set(0); - - const earthMassDiv = document.createElement('div'); - earthMassDiv.className = 'label'; - earthMassDiv.textContent = '5.97237e24 kg'; - earthMassDiv.style.backgroundColor = 'transparent'; - - const earthMassLabel = new CSS2DObject(earthMassDiv); - earthMassLabel.position.set(1.5 * EARTH_RADIUS, 0, 0); - earthMassLabel.center.set(0, 0); - earth.add(earthMassLabel); - earthMassLabel.layers.set(1); - - const moonDiv = document.createElement('div'); - moonDiv.className = 'label'; - moonDiv.textContent = 'Moon'; - moonDiv.style.backgroundColor = 'transparent'; - - const moonLabel = new CSS2DObject(moonDiv); - moonLabel.position.set(1.5 * MOON_RADIUS, 0, 0); - moonLabel.center.set(0, 1); - moon.add(moonLabel); - moonLabel.layers.set(0); - - const moonMassDiv = document.createElement('div'); - moonMassDiv.className = 'label'; - moonMassDiv.textContent = '7.342e22 kg'; - moonMassDiv.style.backgroundColor = 'transparent'; - - const moonMassLabel = new CSS2DObject(moonMassDiv); - moonMassLabel.position.set(1.5 * MOON_RADIUS, 0, 0); - moonMassLabel.center.set(0, 0); - moon.add(moonMassLabel); - moonMassLabel.layers.set(1); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - labelRenderer = new CSS2DRenderer(); - labelRenderer.setSize(window.innerWidth, window.innerHeight); - labelRenderer.domElement.style.position = 'absolute'; - labelRenderer.domElement.style.top = '0px'; - document.body.appendChild(labelRenderer.domElement); - - const controls = new OrbitControls(camera, labelRenderer.domElement); - controls.minDistance = 5; - controls.maxDistance = 100; - - // - - window.addEventListener('resize', onWindowResize); - - initGui(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - labelRenderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - requestAnimationFrame(animate); - - const elapsed = clock.getElapsedTime(); - - moon.position.set(Math.sin(elapsed) * 5, 0, Math.cos(elapsed) * 5); - - renderer.render(scene, camera); - labelRenderer.render(scene, camera); -} - -// - -function initGui() { - gui = new GUI(); - - gui.title('Camera Layers'); - - gui.add(layers, 'Toggle Name'); - gui.add(layers, 'Toggle Mass'); - gui.add(layers, 'Enable All'); - gui.add(layers, 'Disable All'); - - gui.open(); -} diff --git a/examples-testing/examples/css3d_molecules.ts b/examples-testing/examples/css3d_molecules.ts deleted file mode 100644 index 538472607..000000000 --- a/examples-testing/examples/css3d_molecules.ts +++ /dev/null @@ -1,353 +0,0 @@ -import * as THREE from 'three'; - -import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; -import { PDBLoader } from 'three/addons/loaders/PDBLoader.js'; -import { CSS3DRenderer, CSS3DObject, CSS3DSprite } from 'three/addons/renderers/CSS3DRenderer.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer; -let controls; -let root; - -const objects = []; -const tmpVec1 = new THREE.Vector3(); -const tmpVec2 = new THREE.Vector3(); -const tmpVec3 = new THREE.Vector3(); -const tmpVec4 = new THREE.Vector3(); -const offset = new THREE.Vector3(); - -const VIZ_TYPE = { - Atoms: 0, - Bonds: 1, - 'Atoms + Bonds': 2, -}; - -const MOLECULES = { - Ethanol: 'ethanol.pdb', - Aspirin: 'aspirin.pdb', - Caffeine: 'caffeine.pdb', - Nicotine: 'nicotine.pdb', - LSD: 'lsd.pdb', - Cocaine: 'cocaine.pdb', - Cholesterol: 'cholesterol.pdb', - Lycopene: 'lycopene.pdb', - Glucose: 'glucose.pdb', - 'Aluminium oxide': 'Al2O3.pdb', - Cubane: 'cubane.pdb', - Copper: 'cu.pdb', - Fluorite: 'caf2.pdb', - Salt: 'nacl.pdb', - 'YBCO superconductor': 'ybco.pdb', - Buckyball: 'buckyball.pdb', - // 'Diamond': 'diamond.pdb', - Graphite: 'graphite.pdb', -}; - -const params = { - vizType: 2, - molecule: 'caffeine.pdb', -}; - -const loader = new PDBLoader(); -const colorSpriteMap = {}; -const baseSprite = document.createElement('img'); - -init(); -animate(); - -function init() { - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 5000); - camera.position.z = 1000; - - scene = new THREE.Scene(); - - root = new THREE.Object3D(); - scene.add(root); - - // - - renderer = new CSS3DRenderer(); - renderer.setSize(window.innerWidth, window.innerHeight); - document.getElementById('container').appendChild(renderer.domElement); - - // - - controls = new TrackballControls(camera, renderer.domElement); - controls.rotateSpeed = 0.5; - - // - - baseSprite.onload = function () { - loadMolecule(params.molecule); - }; - - baseSprite.src = 'textures/sprites/ball.png'; - - // - - window.addEventListener('resize', onWindowResize); - - // - - const gui = new GUI(); - - gui.add(params, 'vizType', VIZ_TYPE).onChange(changeVizType); - gui.add(params, 'molecule', MOLECULES).onChange(loadMolecule); - gui.open(); -} - -function changeVizType(value) { - if (value === 0) showAtoms(); - else if (value === 1) showBonds(); - else showAtomsBonds(); -} - -// - -function showAtoms() { - for (let i = 0; i < objects.length; i++) { - const object = objects[i]; - - if (object instanceof CSS3DSprite) { - object.element.style.display = ''; - object.visible = true; - } else { - object.element.style.display = 'none'; - object.visible = false; - } - } -} - -function showBonds() { - for (let i = 0; i < objects.length; i++) { - const object = objects[i]; - - if (object instanceof CSS3DSprite) { - object.element.style.display = 'none'; - object.visible = false; - } else { - object.element.style.display = ''; - object.element.style.height = object.userData.bondLengthFull; - object.visible = true; - } - } -} - -function showAtomsBonds() { - for (let i = 0; i < objects.length; i++) { - const object = objects[i]; - - object.element.style.display = ''; - object.visible = true; - - if (!(object instanceof CSS3DSprite)) { - object.element.style.height = object.userData.bondLengthShort; - } - } -} - -// - -function colorify(ctx, width, height, color) { - const r = color.r, - g = color.g, - b = color.b; - - const imageData = ctx.getImageData(0, 0, width, height); - const data = imageData.data; - - for (let i = 0, l = data.length; i < l; i += 4) { - data[i + 0] *= r; - data[i + 1] *= g; - data[i + 2] *= b; - } - - ctx.putImageData(imageData, 0, 0); -} - -function imageToCanvas(image) { - const width = image.width; - const height = image.height; - - const canvas = document.createElement('canvas'); - - canvas.width = width; - canvas.height = height; - - const context = canvas.getContext('2d'); - context.drawImage(image, 0, 0, width, height); - - return canvas; -} - -// - -function loadMolecule(model) { - const url = 'models/pdb/' + model; - - for (let i = 0; i < objects.length; i++) { - const object = objects[i]; - object.parent.remove(object); - } - - objects.length = 0; - - loader.load(url, function (pdb) { - const geometryAtoms = pdb.geometryAtoms; - const geometryBonds = pdb.geometryBonds; - const json = pdb.json; - - geometryAtoms.computeBoundingBox(); - geometryAtoms.boundingBox.getCenter(offset).negate(); - - geometryAtoms.translate(offset.x, offset.y, offset.z); - geometryBonds.translate(offset.x, offset.y, offset.z); - - const positionAtoms = geometryAtoms.getAttribute('position'); - const colorAtoms = geometryAtoms.getAttribute('color'); - - const position = new THREE.Vector3(); - const color = new THREE.Color(); - - for (let i = 0; i < positionAtoms.count; i++) { - position.fromBufferAttribute(positionAtoms, i); - color.fromBufferAttribute(colorAtoms, i); - - const atomJSON = json.atoms[i]; - const element = atomJSON[4]; - - if (!colorSpriteMap[element]) { - const canvas = imageToCanvas(baseSprite); - const context = canvas.getContext('2d'); - - colorify(context, canvas.width, canvas.height, color); - - const dataUrl = canvas.toDataURL(); - - colorSpriteMap[element] = dataUrl; - } - - const colorSprite = colorSpriteMap[element]; - - const atom = document.createElement('img'); - atom.src = colorSprite; - - const object = new CSS3DSprite(atom); - object.position.copy(position); - object.position.multiplyScalar(75); - - object.matrixAutoUpdate = false; - object.updateMatrix(); - - root.add(object); - - objects.push(object); - } - - const positionBonds = geometryBonds.getAttribute('position'); - - const start = new THREE.Vector3(); - const end = new THREE.Vector3(); - - for (let i = 0; i < positionBonds.count; i += 2) { - start.fromBufferAttribute(positionBonds, i); - end.fromBufferAttribute(positionBonds, i + 1); - - start.multiplyScalar(75); - end.multiplyScalar(75); - - tmpVec1.subVectors(end, start); - const bondLength = tmpVec1.length() - 50; - - // - - let bond = document.createElement('div'); - bond.className = 'bond'; - bond.style.height = bondLength + 'px'; - - let object = new CSS3DObject(bond); - object.position.copy(start); - object.position.lerp(end, 0.5); - - object.userData.bondLengthShort = bondLength + 'px'; - object.userData.bondLengthFull = bondLength + 55 + 'px'; - - // - - const axis = tmpVec2.set(0, 1, 0).cross(tmpVec1); - const radians = Math.acos(tmpVec3.set(0, 1, 0).dot(tmpVec4.copy(tmpVec1).normalize())); - - const objMatrix = new THREE.Matrix4().makeRotationAxis(axis.normalize(), radians); - object.matrix.copy(objMatrix); - object.quaternion.setFromRotationMatrix(object.matrix); - - object.matrixAutoUpdate = false; - object.updateMatrix(); - - root.add(object); - - objects.push(object); - - // - - const joint = new THREE.Object3D(); - joint.position.copy(start); - joint.position.lerp(end, 0.5); - - joint.matrix.copy(objMatrix); - joint.quaternion.setFromRotationMatrix(joint.matrix); - - joint.matrixAutoUpdate = false; - joint.updateMatrix(); - - bond = document.createElement('div'); - bond.className = 'bond'; - bond.style.height = bondLength + 'px'; - - object = new CSS3DObject(bond); - object.rotation.y = Math.PI / 2; - - object.matrixAutoUpdate = false; - object.updateMatrix(); - - object.userData.bondLengthShort = bondLength + 'px'; - object.userData.bondLengthFull = bondLength + 55 + 'px'; - - object.userData.joint = joint; - - joint.add(object); - root.add(joint); - - objects.push(object); - } - - //console.log( "CSS3DObjects:", objects.length ); - - changeVizType(params.vizType); - }); -} - -// - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - requestAnimationFrame(animate); - controls.update(); - - const time = Date.now() * 0.0004; - - root.rotation.x = time; - root.rotation.y = time * 0.7; - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/css3d_orthographic.ts b/examples-testing/examples/css3d_orthographic.ts deleted file mode 100644 index 4aabbed08..000000000 --- a/examples-testing/examples/css3d_orthographic.ts +++ /dev/null @@ -1,208 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { CSS3DRenderer, CSS3DObject } from 'three/addons/renderers/CSS3DRenderer.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer; - -let scene2, renderer2; - -const frustumSize = 500; - -init(); -animate(); - -function init() { - const aspect = window.innerWidth / window.innerHeight; - camera = new THREE.OrthographicCamera( - (frustumSize * aspect) / -2, - (frustumSize * aspect) / 2, - frustumSize / 2, - frustumSize / -2, - 1, - 1000, - ); - - camera.position.set(-200, 200, 200); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - scene2 = new THREE.Scene(); - - const material = new THREE.MeshBasicMaterial({ - color: 0x000000, - wireframe: true, - wireframeLinewidth: 1, - side: THREE.DoubleSide, - }); - - // left - createPlane( - 100, - 100, - 'chocolate', - new THREE.Vector3(-50, 0, 0), - new THREE.Euler(0, -90 * THREE.MathUtils.DEG2RAD, 0), - ); - // right - createPlane(100, 100, 'saddlebrown', new THREE.Vector3(0, 0, 50), new THREE.Euler(0, 0, 0)); - // top - createPlane( - 100, - 100, - 'yellowgreen', - new THREE.Vector3(0, 50, 0), - new THREE.Euler(-90 * THREE.MathUtils.DEG2RAD, 0, 0), - ); - // bottom - createPlane( - 300, - 300, - 'seagreen', - new THREE.Vector3(0, -50, 0), - new THREE.Euler(-90 * THREE.MathUtils.DEG2RAD, 0, 0), - ); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - renderer2 = new CSS3DRenderer(); - renderer2.setSize(window.innerWidth, window.innerHeight); - renderer2.domElement.style.position = 'absolute'; - renderer2.domElement.style.top = 0; - document.body.appendChild(renderer2.domElement); - - const controls = new OrbitControls(camera, renderer2.domElement); - controls.minZoom = 0.5; - controls.maxZoom = 2; - - function createPlane(width, height, cssColor, pos, rot) { - const element = document.createElement('div'); - element.style.width = width + 'px'; - element.style.height = height + 'px'; - element.style.opacity = 0.75; - element.style.background = cssColor; - - const object = new CSS3DObject(element); - object.position.copy(pos); - object.rotation.copy(rot); - scene2.add(object); - - const geometry = new THREE.PlaneGeometry(width, height); - const mesh = new THREE.Mesh(geometry, material); - mesh.position.copy(object.position); - mesh.rotation.copy(object.rotation); - scene.add(mesh); - } - - window.addEventListener('resize', onWindowResize); - createPanel(); -} - -function onWindowResize() { - const aspect = window.innerWidth / window.innerHeight; - - camera.left = (-frustumSize * aspect) / 2; - camera.right = (frustumSize * aspect) / 2; - camera.top = frustumSize / 2; - camera.bottom = -frustumSize / 2; - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - renderer2.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - requestAnimationFrame(animate); - - renderer.render(scene, camera); - renderer2.render(scene2, camera); -} - -function createPanel() { - const panel = new GUI(); - const folder1 = panel.addFolder('camera setViewOffset').close(); - - const settings = { - setViewOffset() { - folder1.children[1].enable().setValue(window.innerWidth); - folder1.children[2].enable().setValue(window.innerHeight); - folder1.children[3].enable().setValue(0); - folder1.children[4].enable().setValue(0); - folder1.children[5].enable().setValue(window.innerWidth); - folder1.children[6].enable().setValue(window.innerHeight); - }, - fullWidth: 0, - fullHeight: 0, - offsetX: 0, - offsetY: 0, - width: 0, - height: 0, - clearViewOffset() { - folder1.children[1].setValue(0).disable(); - folder1.children[2].setValue(0).disable(); - folder1.children[3].setValue(0).disable(); - folder1.children[4].setValue(0).disable(); - folder1.children[5].setValue(0).disable(); - folder1.children[6].setValue(0).disable(); - camera.clearViewOffset(); - }, - }; - - folder1.add(settings, 'setViewOffset'); - folder1 - .add(settings, 'fullWidth', window.screen.width / 4, window.screen.width * 2, 1) - .onChange(val => updateCameraViewOffset({ fullWidth: val })) - .disable(); - folder1 - .add(settings, 'fullHeight', window.screen.height / 4, window.screen.height * 2, 1) - .onChange(val => updateCameraViewOffset({ fullHeight: val })) - .disable(); - folder1 - .add(settings, 'offsetX', 0, 256, 1) - .onChange(val => updateCameraViewOffset({ x: val })) - .disable(); - folder1 - .add(settings, 'offsetY', 0, 256, 1) - .onChange(val => updateCameraViewOffset({ y: val })) - .disable(); - folder1 - .add(settings, 'width', window.screen.width / 4, window.screen.width * 2, 1) - .onChange(val => updateCameraViewOffset({ width: val })) - .disable(); - folder1 - .add(settings, 'height', window.screen.height / 4, window.screen.height * 2, 1) - .onChange(val => updateCameraViewOffset({ height: val })) - .disable(); - folder1.add(settings, 'clearViewOffset'); -} - -function updateCameraViewOffset({ fullWidth, fullHeight, x, y, width, height }) { - if (!camera.view) { - camera.setViewOffset( - fullWidth || window.innerWidth, - fullHeight || window.innerHeight, - x || 0, - y || 0, - width || window.innerWidth, - height || window.innerHeight, - ); - } else { - camera.setViewOffset( - fullWidth || camera.view.fullWidth, - fullHeight || camera.view.fullHeight, - x || camera.view.offsetX, - y || camera.view.offsetY, - width || camera.view.width, - height || camera.view.height, - ); - } -} diff --git a/examples-testing/examples/css3d_periodictable.ts b/examples-testing/examples/css3d_periodictable.ts deleted file mode 100644 index e3a33f796..000000000 --- a/examples-testing/examples/css3d_periodictable.ts +++ /dev/null @@ -1,793 +0,0 @@ -import * as THREE from 'three'; - -import TWEEN from 'three/addons/libs/tween.module.js'; -import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; -import { CSS3DRenderer, CSS3DObject } from 'three/addons/renderers/CSS3DRenderer.js'; - -const table = [ - 'H', - 'Hydrogen', - '1.00794', - 1, - 1, - 'He', - 'Helium', - '4.002602', - 18, - 1, - 'Li', - 'Lithium', - '6.941', - 1, - 2, - 'Be', - 'Beryllium', - '9.012182', - 2, - 2, - 'B', - 'Boron', - '10.811', - 13, - 2, - 'C', - 'Carbon', - '12.0107', - 14, - 2, - 'N', - 'Nitrogen', - '14.0067', - 15, - 2, - 'O', - 'Oxygen', - '15.9994', - 16, - 2, - 'F', - 'Fluorine', - '18.9984032', - 17, - 2, - 'Ne', - 'Neon', - '20.1797', - 18, - 2, - 'Na', - 'Sodium', - '22.98976...', - 1, - 3, - 'Mg', - 'Magnesium', - '24.305', - 2, - 3, - 'Al', - 'Aluminium', - '26.9815386', - 13, - 3, - 'Si', - 'Silicon', - '28.0855', - 14, - 3, - 'P', - 'Phosphorus', - '30.973762', - 15, - 3, - 'S', - 'Sulfur', - '32.065', - 16, - 3, - 'Cl', - 'Chlorine', - '35.453', - 17, - 3, - 'Ar', - 'Argon', - '39.948', - 18, - 3, - 'K', - 'Potassium', - '39.948', - 1, - 4, - 'Ca', - 'Calcium', - '40.078', - 2, - 4, - 'Sc', - 'Scandium', - '44.955912', - 3, - 4, - 'Ti', - 'Titanium', - '47.867', - 4, - 4, - 'V', - 'Vanadium', - '50.9415', - 5, - 4, - 'Cr', - 'Chromium', - '51.9961', - 6, - 4, - 'Mn', - 'Manganese', - '54.938045', - 7, - 4, - 'Fe', - 'Iron', - '55.845', - 8, - 4, - 'Co', - 'Cobalt', - '58.933195', - 9, - 4, - 'Ni', - 'Nickel', - '58.6934', - 10, - 4, - 'Cu', - 'Copper', - '63.546', - 11, - 4, - 'Zn', - 'Zinc', - '65.38', - 12, - 4, - 'Ga', - 'Gallium', - '69.723', - 13, - 4, - 'Ge', - 'Germanium', - '72.63', - 14, - 4, - 'As', - 'Arsenic', - '74.9216', - 15, - 4, - 'Se', - 'Selenium', - '78.96', - 16, - 4, - 'Br', - 'Bromine', - '79.904', - 17, - 4, - 'Kr', - 'Krypton', - '83.798', - 18, - 4, - 'Rb', - 'Rubidium', - '85.4678', - 1, - 5, - 'Sr', - 'Strontium', - '87.62', - 2, - 5, - 'Y', - 'Yttrium', - '88.90585', - 3, - 5, - 'Zr', - 'Zirconium', - '91.224', - 4, - 5, - 'Nb', - 'Niobium', - '92.90628', - 5, - 5, - 'Mo', - 'Molybdenum', - '95.96', - 6, - 5, - 'Tc', - 'Technetium', - '(98)', - 7, - 5, - 'Ru', - 'Ruthenium', - '101.07', - 8, - 5, - 'Rh', - 'Rhodium', - '102.9055', - 9, - 5, - 'Pd', - 'Palladium', - '106.42', - 10, - 5, - 'Ag', - 'Silver', - '107.8682', - 11, - 5, - 'Cd', - 'Cadmium', - '112.411', - 12, - 5, - 'In', - 'Indium', - '114.818', - 13, - 5, - 'Sn', - 'Tin', - '118.71', - 14, - 5, - 'Sb', - 'Antimony', - '121.76', - 15, - 5, - 'Te', - 'Tellurium', - '127.6', - 16, - 5, - 'I', - 'Iodine', - '126.90447', - 17, - 5, - 'Xe', - 'Xenon', - '131.293', - 18, - 5, - 'Cs', - 'Caesium', - '132.9054', - 1, - 6, - 'Ba', - 'Barium', - '132.9054', - 2, - 6, - 'La', - 'Lanthanum', - '138.90547', - 4, - 9, - 'Ce', - 'Cerium', - '140.116', - 5, - 9, - 'Pr', - 'Praseodymium', - '140.90765', - 6, - 9, - 'Nd', - 'Neodymium', - '144.242', - 7, - 9, - 'Pm', - 'Promethium', - '(145)', - 8, - 9, - 'Sm', - 'Samarium', - '150.36', - 9, - 9, - 'Eu', - 'Europium', - '151.964', - 10, - 9, - 'Gd', - 'Gadolinium', - '157.25', - 11, - 9, - 'Tb', - 'Terbium', - '158.92535', - 12, - 9, - 'Dy', - 'Dysprosium', - '162.5', - 13, - 9, - 'Ho', - 'Holmium', - '164.93032', - 14, - 9, - 'Er', - 'Erbium', - '167.259', - 15, - 9, - 'Tm', - 'Thulium', - '168.93421', - 16, - 9, - 'Yb', - 'Ytterbium', - '173.054', - 17, - 9, - 'Lu', - 'Lutetium', - '174.9668', - 18, - 9, - 'Hf', - 'Hafnium', - '178.49', - 4, - 6, - 'Ta', - 'Tantalum', - '180.94788', - 5, - 6, - 'W', - 'Tungsten', - '183.84', - 6, - 6, - 'Re', - 'Rhenium', - '186.207', - 7, - 6, - 'Os', - 'Osmium', - '190.23', - 8, - 6, - 'Ir', - 'Iridium', - '192.217', - 9, - 6, - 'Pt', - 'Platinum', - '195.084', - 10, - 6, - 'Au', - 'Gold', - '196.966569', - 11, - 6, - 'Hg', - 'Mercury', - '200.59', - 12, - 6, - 'Tl', - 'Thallium', - '204.3833', - 13, - 6, - 'Pb', - 'Lead', - '207.2', - 14, - 6, - 'Bi', - 'Bismuth', - '208.9804', - 15, - 6, - 'Po', - 'Polonium', - '(209)', - 16, - 6, - 'At', - 'Astatine', - '(210)', - 17, - 6, - 'Rn', - 'Radon', - '(222)', - 18, - 6, - 'Fr', - 'Francium', - '(223)', - 1, - 7, - 'Ra', - 'Radium', - '(226)', - 2, - 7, - 'Ac', - 'Actinium', - '(227)', - 4, - 10, - 'Th', - 'Thorium', - '232.03806', - 5, - 10, - 'Pa', - 'Protactinium', - '231.0588', - 6, - 10, - 'U', - 'Uranium', - '238.02891', - 7, - 10, - 'Np', - 'Neptunium', - '(237)', - 8, - 10, - 'Pu', - 'Plutonium', - '(244)', - 9, - 10, - 'Am', - 'Americium', - '(243)', - 10, - 10, - 'Cm', - 'Curium', - '(247)', - 11, - 10, - 'Bk', - 'Berkelium', - '(247)', - 12, - 10, - 'Cf', - 'Californium', - '(251)', - 13, - 10, - 'Es', - 'Einstenium', - '(252)', - 14, - 10, - 'Fm', - 'Fermium', - '(257)', - 15, - 10, - 'Md', - 'Mendelevium', - '(258)', - 16, - 10, - 'No', - 'Nobelium', - '(259)', - 17, - 10, - 'Lr', - 'Lawrencium', - '(262)', - 18, - 10, - 'Rf', - 'Rutherfordium', - '(267)', - 4, - 7, - 'Db', - 'Dubnium', - '(268)', - 5, - 7, - 'Sg', - 'Seaborgium', - '(271)', - 6, - 7, - 'Bh', - 'Bohrium', - '(272)', - 7, - 7, - 'Hs', - 'Hassium', - '(270)', - 8, - 7, - 'Mt', - 'Meitnerium', - '(276)', - 9, - 7, - 'Ds', - 'Darmstadium', - '(281)', - 10, - 7, - 'Rg', - 'Roentgenium', - '(280)', - 11, - 7, - 'Cn', - 'Copernicium', - '(285)', - 12, - 7, - 'Nh', - 'Nihonium', - '(286)', - 13, - 7, - 'Fl', - 'Flerovium', - '(289)', - 14, - 7, - 'Mc', - 'Moscovium', - '(290)', - 15, - 7, - 'Lv', - 'Livermorium', - '(293)', - 16, - 7, - 'Ts', - 'Tennessine', - '(294)', - 17, - 7, - 'Og', - 'Oganesson', - '(294)', - 18, - 7, -]; - -let camera, scene, renderer; -let controls; - -const objects = []; -const targets = { table: [], sphere: [], helix: [], grid: [] }; - -init(); -animate(); - -function init() { - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 3000; - - scene = new THREE.Scene(); - - // table - - for (let i = 0; i < table.length; i += 5) { - const element = document.createElement('div'); - element.className = 'element'; - element.style.backgroundColor = 'rgba(0,127,127,' + (Math.random() * 0.5 + 0.25) + ')'; - - const number = document.createElement('div'); - number.className = 'number'; - number.textContent = i / 5 + 1; - element.appendChild(number); - - const symbol = document.createElement('div'); - symbol.className = 'symbol'; - symbol.textContent = table[i]; - element.appendChild(symbol); - - const details = document.createElement('div'); - details.className = 'details'; - details.innerHTML = table[i + 1] + '
' + table[i + 2]; - element.appendChild(details); - - const objectCSS = new CSS3DObject(element); - objectCSS.position.x = Math.random() * 4000 - 2000; - objectCSS.position.y = Math.random() * 4000 - 2000; - objectCSS.position.z = Math.random() * 4000 - 2000; - scene.add(objectCSS); - - objects.push(objectCSS); - - // - - const object = new THREE.Object3D(); - object.position.x = table[i + 3] * 140 - 1330; - object.position.y = -(table[i + 4] * 180) + 990; - - targets.table.push(object); - } - - // sphere - - const vector = new THREE.Vector3(); - - for (let i = 0, l = objects.length; i < l; i++) { - const phi = Math.acos(-1 + (2 * i) / l); - const theta = Math.sqrt(l * Math.PI) * phi; - - const object = new THREE.Object3D(); - - object.position.setFromSphericalCoords(800, phi, theta); - - vector.copy(object.position).multiplyScalar(2); - - object.lookAt(vector); - - targets.sphere.push(object); - } - - // helix - - for (let i = 0, l = objects.length; i < l; i++) { - const theta = i * 0.175 + Math.PI; - const y = -(i * 8) + 450; - - const object = new THREE.Object3D(); - - object.position.setFromCylindricalCoords(900, theta, y); - - vector.x = object.position.x * 2; - vector.y = object.position.y; - vector.z = object.position.z * 2; - - object.lookAt(vector); - - targets.helix.push(object); - } - - // grid - - for (let i = 0; i < objects.length; i++) { - const object = new THREE.Object3D(); - - object.position.x = (i % 5) * 400 - 800; - object.position.y = -(Math.floor(i / 5) % 5) * 400 + 800; - object.position.z = Math.floor(i / 25) * 1000 - 2000; - - targets.grid.push(object); - } - - // - - renderer = new CSS3DRenderer(); - renderer.setSize(window.innerWidth, window.innerHeight); - document.getElementById('container').appendChild(renderer.domElement); - - // - - controls = new TrackballControls(camera, renderer.domElement); - controls.minDistance = 500; - controls.maxDistance = 6000; - controls.addEventListener('change', render); - - const buttonTable = document.getElementById('table'); - buttonTable.addEventListener('click', function () { - transform(targets.table, 2000); - }); - - const buttonSphere = document.getElementById('sphere'); - buttonSphere.addEventListener('click', function () { - transform(targets.sphere, 2000); - }); - - const buttonHelix = document.getElementById('helix'); - buttonHelix.addEventListener('click', function () { - transform(targets.helix, 2000); - }); - - const buttonGrid = document.getElementById('grid'); - buttonGrid.addEventListener('click', function () { - transform(targets.grid, 2000); - }); - - transform(targets.table, 2000); - - // - - window.addEventListener('resize', onWindowResize); -} - -function transform(targets, duration) { - TWEEN.removeAll(); - - for (let i = 0; i < objects.length; i++) { - const object = objects[i]; - const target = targets[i]; - - new TWEEN.Tween(object.position) - .to( - { x: target.position.x, y: target.position.y, z: target.position.z }, - Math.random() * duration + duration, - ) - .easing(TWEEN.Easing.Exponential.InOut) - .start(); - - new TWEEN.Tween(object.rotation) - .to( - { x: target.rotation.x, y: target.rotation.y, z: target.rotation.z }, - Math.random() * duration + duration, - ) - .easing(TWEEN.Easing.Exponential.InOut) - .start(); - } - - new TWEEN.Tween(this) - .to({}, duration * 2) - .onUpdate(render) - .start(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function animate() { - requestAnimationFrame(animate); - - TWEEN.update(); - - controls.update(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/css3d_sandbox.ts b/examples-testing/examples/css3d_sandbox.ts deleted file mode 100644 index 1088b84b1..000000000 --- a/examples-testing/examples/css3d_sandbox.ts +++ /dev/null @@ -1,180 +0,0 @@ -import * as THREE from 'three'; - -import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; -import { CSS3DRenderer, CSS3DObject } from 'three/addons/renderers/CSS3DRenderer.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer; - -let scene2, renderer2; - -let controls; - -init(); -animate(); - -function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(200, 200, 200); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - scene2 = new THREE.Scene(); - - const material = new THREE.MeshBasicMaterial({ - color: 0x000000, - wireframe: true, - wireframeLinewidth: 1, - side: THREE.DoubleSide, - }); - - // - - for (let i = 0; i < 10; i++) { - const element = document.createElement('div'); - element.style.width = '100px'; - element.style.height = '100px'; - element.style.opacity = i < 5 ? 0.5 : 1; - element.style.background = new THREE.Color(Math.random() * 0xffffff).getStyle(); - - const object = new CSS3DObject(element); - object.position.x = Math.random() * 200 - 100; - object.position.y = Math.random() * 200 - 100; - object.position.z = Math.random() * 200 - 100; - object.rotation.x = Math.random(); - object.rotation.y = Math.random(); - object.rotation.z = Math.random(); - object.scale.x = Math.random() + 0.5; - object.scale.y = Math.random() + 0.5; - scene2.add(object); - - const geometry = new THREE.PlaneGeometry(100, 100); - const mesh = new THREE.Mesh(geometry, material); - mesh.position.copy(object.position); - mesh.rotation.copy(object.rotation); - mesh.scale.copy(object.scale); - scene.add(mesh); - } - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - renderer2 = new CSS3DRenderer(); - renderer2.setSize(window.innerWidth, window.innerHeight); - renderer2.domElement.style.position = 'absolute'; - renderer2.domElement.style.top = 0; - document.body.appendChild(renderer2.domElement); - - controls = new TrackballControls(camera, renderer2.domElement); - - window.addEventListener('resize', onWindowResize); - createPanel(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - renderer2.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - requestAnimationFrame(animate); - - controls.update(); - - renderer.render(scene, camera); - renderer2.render(scene2, camera); -} - -function createPanel() { - const panel = new GUI(); - const folder1 = panel.addFolder('camera setViewOffset').close(); - - const settings = { - setViewOffset() { - folder1.children[1].enable().setValue(window.innerWidth); - folder1.children[2].enable().setValue(window.innerHeight); - folder1.children[3].enable().setValue(0); - folder1.children[4].enable().setValue(0); - folder1.children[5].enable().setValue(window.innerWidth); - folder1.children[6].enable().setValue(window.innerHeight); - }, - fullWidth: 0, - fullHeight: 0, - offsetX: 0, - offsetY: 0, - width: 0, - height: 0, - clearViewOffset() { - folder1.children[1].setValue(0).disable(); - folder1.children[2].setValue(0).disable(); - folder1.children[3].setValue(0).disable(); - folder1.children[4].setValue(0).disable(); - folder1.children[5].setValue(0).disable(); - folder1.children[6].setValue(0).disable(); - camera.clearViewOffset(); - }, - }; - - folder1.add(settings, 'setViewOffset'); - folder1 - .add(settings, 'fullWidth', window.screen.width / 4, window.screen.width * 2, 1) - .onChange(val => updateCameraViewOffset({ fullWidth: val })) - .disable(); - folder1 - .add(settings, 'fullHeight', window.screen.height / 4, window.screen.height * 2, 1) - .onChange(val => updateCameraViewOffset({ fullHeight: val })) - .disable(); - folder1 - .add(settings, 'offsetX', 0, 256, 1) - .onChange(val => updateCameraViewOffset({ x: val })) - .disable(); - folder1 - .add(settings, 'offsetY', 0, 256, 1) - .onChange(val => updateCameraViewOffset({ y: val })) - .disable(); - folder1 - .add(settings, 'width', window.screen.width / 4, window.screen.width * 2, 1) - .onChange(val => updateCameraViewOffset({ width: val })) - .disable(); - folder1 - .add(settings, 'height', window.screen.height / 4, window.screen.height * 2, 1) - .onChange(val => updateCameraViewOffset({ height: val })) - .disable(); - folder1.add(settings, 'clearViewOffset'); -} - -function updateCameraViewOffset({ fullWidth, fullHeight, x, y, width, height }) { - if (!camera.view) { - camera.setViewOffset( - fullWidth || window.innerWidth, - fullHeight || window.innerHeight, - x || 0, - y || 0, - width || window.innerWidth, - height || window.innerHeight, - ); - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - } else { - camera.setViewOffset( - fullWidth || camera.view.fullWidth, - fullHeight || camera.view.fullHeight, - x || camera.view.offsetX, - y || camera.view.offsetY, - width || camera.view.width, - height || camera.view.height, - ); - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - } -} diff --git a/examples-testing/examples/css3d_sprites.ts b/examples-testing/examples/css3d_sprites.ts deleted file mode 100644 index dfe24e79d..000000000 --- a/examples-testing/examples/css3d_sprites.ts +++ /dev/null @@ -1,157 +0,0 @@ -import * as THREE from 'three'; - -import TWEEN from 'three/addons/libs/tween.module.js'; -import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; -import { CSS3DRenderer, CSS3DSprite } from 'three/addons/renderers/CSS3DRenderer.js'; - -let camera, scene, renderer; -let controls; - -const particlesTotal = 512; -const positions = []; -const objects = []; -let current = 0; - -init(); -animate(); - -function init() { - camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 5000); - camera.position.set(600, 400, 1500); - camera.lookAt(0, 0, 0); - - scene = new THREE.Scene(); - - const image = document.createElement('img'); - image.addEventListener('load', function () { - for (let i = 0; i < particlesTotal; i++) { - const object = new CSS3DSprite(image.cloneNode()); - (object.position.x = Math.random() * 4000 - 2000), - (object.position.y = Math.random() * 4000 - 2000), - (object.position.z = Math.random() * 4000 - 2000); - scene.add(object); - - objects.push(object); - } - - transition(); - }); - image.src = 'textures/sprite.png'; - - // Plane - - const amountX = 16; - const amountZ = 32; - const separationPlane = 150; - const offsetX = ((amountX - 1) * separationPlane) / 2; - const offsetZ = ((amountZ - 1) * separationPlane) / 2; - - for (let i = 0; i < particlesTotal; i++) { - const x = (i % amountX) * separationPlane; - const z = Math.floor(i / amountX) * separationPlane; - const y = (Math.sin(x * 0.5) + Math.sin(z * 0.5)) * 200; - - positions.push(x - offsetX, y, z - offsetZ); - } - - // Cube - - const amount = 8; - const separationCube = 150; - const offset = ((amount - 1) * separationCube) / 2; - - for (let i = 0; i < particlesTotal; i++) { - const x = (i % amount) * separationCube; - const y = Math.floor((i / amount) % amount) * separationCube; - const z = Math.floor(i / (amount * amount)) * separationCube; - - positions.push(x - offset, y - offset, z - offset); - } - - // Random - - for (let i = 0; i < particlesTotal; i++) { - positions.push(Math.random() * 4000 - 2000, Math.random() * 4000 - 2000, Math.random() * 4000 - 2000); - } - - // Sphere - - const radius = 750; - - for (let i = 0; i < particlesTotal; i++) { - const phi = Math.acos(-1 + (2 * i) / particlesTotal); - const theta = Math.sqrt(particlesTotal * Math.PI) * phi; - - positions.push( - radius * Math.cos(theta) * Math.sin(phi), - radius * Math.sin(theta) * Math.sin(phi), - radius * Math.cos(phi), - ); - } - - // - - renderer = new CSS3DRenderer(); - renderer.setSize(window.innerWidth, window.innerHeight); - document.getElementById('container').appendChild(renderer.domElement); - - // - - controls = new TrackballControls(camera, renderer.domElement); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function transition() { - const offset = current * particlesTotal * 3; - const duration = 2000; - - for (let i = 0, j = offset; i < particlesTotal; i++, j += 3) { - const object = objects[i]; - - new TWEEN.Tween(object.position) - .to( - { - x: positions[j], - y: positions[j + 1], - z: positions[j + 2], - }, - Math.random() * duration + duration, - ) - .easing(TWEEN.Easing.Exponential.InOut) - .start(); - } - - new TWEEN.Tween(this) - .to({}, duration * 3) - .onComplete(transition) - .start(); - - current = (current + 1) % 4; -} - -function animate() { - requestAnimationFrame(animate); - - TWEEN.update(); - controls.update(); - - const time = performance.now(); - - for (let i = 0, l = objects.length; i < l; i++) { - const object = objects[i]; - const scale = Math.sin((Math.floor(object.position.x) + time) * 0.002) * 0.3 + 1; - object.scale.set(scale, scale, scale); - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/css3d_youtube.ts b/examples-testing/examples/css3d_youtube.ts deleted file mode 100644 index 62652f87f..000000000 --- a/examples-testing/examples/css3d_youtube.ts +++ /dev/null @@ -1,79 +0,0 @@ -import * as THREE from 'three'; - -import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; -import { CSS3DRenderer, CSS3DObject } from 'three/addons/renderers/CSS3DRenderer.js'; - -let camera, scene, renderer; -let controls; - -function Element(id, x, y, z, ry) { - const div = document.createElement('div'); - div.style.width = '480px'; - div.style.height = '360px'; - div.style.backgroundColor = '#000'; - - const iframe = document.createElement('iframe'); - iframe.style.width = '480px'; - iframe.style.height = '360px'; - iframe.style.border = '0px'; - iframe.src = ['https://www.youtube.com/embed/', id, '?rel=0'].join(''); - div.appendChild(iframe); - - const object = new CSS3DObject(div); - object.position.set(x, y, z); - object.rotation.y = ry; - - return object; -} - -init(); -animate(); - -function init() { - const container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 5000); - camera.position.set(500, 350, 750); - - scene = new THREE.Scene(); - - renderer = new CSS3DRenderer(); - renderer.setSize(window.innerWidth, window.innerHeight); - container.appendChild(renderer.domElement); - - const group = new THREE.Group(); - group.add(new Element('SJOz3qjfQXU', 0, 0, 240, 0)); - group.add(new Element('Y2-xZ-1HE-Q', 240, 0, 0, Math.PI / 2)); - group.add(new Element('IrydklNpcFI', 0, 0, -240, Math.PI)); - group.add(new Element('9ubytEsCaS0', -240, 0, 0, -Math.PI / 2)); - scene.add(group); - - controls = new TrackballControls(camera, renderer.domElement); - controls.rotateSpeed = 4; - - window.addEventListener('resize', onWindowResize); - - // Block iframe events when dragging camera - - const blocker = document.getElementById('blocker'); - blocker.style.display = 'none'; - - controls.addEventListener('start', function () { - blocker.style.display = ''; - }); - controls.addEventListener('end', function () { - blocker.style.display = 'none'; - }); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - requestAnimationFrame(animate); - controls.update(); - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/games_fps.ts b/examples-testing/examples/games_fps.ts deleted file mode 100644 index 4c459f9bc..000000000 --- a/examples-testing/examples/games_fps.ts +++ /dev/null @@ -1,372 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -import { Octree } from 'three/addons/math/Octree.js'; -import { OctreeHelper } from 'three/addons/helpers/OctreeHelper.js'; - -import { Capsule } from 'three/addons/math/Capsule.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -const clock = new THREE.Clock(); - -const scene = new THREE.Scene(); -scene.background = new THREE.Color(0x88ccee); -scene.fog = new THREE.Fog(0x88ccee, 0, 50); - -const camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 1000); -camera.rotation.order = 'YXZ'; - -const fillLight1 = new THREE.HemisphereLight(0x8dc1de, 0x00668d, 1.5); -fillLight1.position.set(2, 1, 1); -scene.add(fillLight1); - -const directionalLight = new THREE.DirectionalLight(0xffffff, 2.5); -directionalLight.position.set(-5, 25, -1); -directionalLight.castShadow = true; -directionalLight.shadow.camera.near = 0.01; -directionalLight.shadow.camera.far = 500; -directionalLight.shadow.camera.right = 30; -directionalLight.shadow.camera.left = -30; -directionalLight.shadow.camera.top = 30; -directionalLight.shadow.camera.bottom = -30; -directionalLight.shadow.mapSize.width = 1024; -directionalLight.shadow.mapSize.height = 1024; -directionalLight.shadow.radius = 4; -directionalLight.shadow.bias = -0.00006; -scene.add(directionalLight); - -const container = document.getElementById('container'); - -const renderer = new THREE.WebGLRenderer({ antialias: true }); -renderer.setPixelRatio(window.devicePixelRatio); -renderer.setSize(window.innerWidth, window.innerHeight); -renderer.setAnimationLoop(animate); -renderer.shadowMap.enabled = true; -renderer.shadowMap.type = THREE.VSMShadowMap; -renderer.toneMapping = THREE.ACESFilmicToneMapping; -container.appendChild(renderer.domElement); - -const stats = new Stats(); -stats.domElement.style.position = 'absolute'; -stats.domElement.style.top = '0px'; -container.appendChild(stats.domElement); - -const GRAVITY = 30; - -const NUM_SPHERES = 100; -const SPHERE_RADIUS = 0.2; - -const STEPS_PER_FRAME = 5; - -const sphereGeometry = new THREE.IcosahedronGeometry(SPHERE_RADIUS, 5); -const sphereMaterial = new THREE.MeshLambertMaterial({ color: 0xdede8d }); - -const spheres = []; -let sphereIdx = 0; - -for (let i = 0; i < NUM_SPHERES; i++) { - const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); - sphere.castShadow = true; - sphere.receiveShadow = true; - - scene.add(sphere); - - spheres.push({ - mesh: sphere, - collider: new THREE.Sphere(new THREE.Vector3(0, -100, 0), SPHERE_RADIUS), - velocity: new THREE.Vector3(), - }); -} - -const worldOctree = new Octree(); - -const playerCollider = new Capsule(new THREE.Vector3(0, 0.35, 0), new THREE.Vector3(0, 1, 0), 0.35); - -const playerVelocity = new THREE.Vector3(); -const playerDirection = new THREE.Vector3(); - -let playerOnFloor = false; -let mouseTime = 0; - -const keyStates = {}; - -const vector1 = new THREE.Vector3(); -const vector2 = new THREE.Vector3(); -const vector3 = new THREE.Vector3(); - -document.addEventListener('keydown', event => { - keyStates[event.code] = true; -}); - -document.addEventListener('keyup', event => { - keyStates[event.code] = false; -}); - -container.addEventListener('mousedown', () => { - document.body.requestPointerLock(); - - mouseTime = performance.now(); -}); - -document.addEventListener('mouseup', () => { - if (document.pointerLockElement !== null) throwBall(); -}); - -document.body.addEventListener('mousemove', event => { - if (document.pointerLockElement === document.body) { - camera.rotation.y -= event.movementX / 500; - camera.rotation.x -= event.movementY / 500; - } -}); - -window.addEventListener('resize', onWindowResize); - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function throwBall() { - const sphere = spheres[sphereIdx]; - - camera.getWorldDirection(playerDirection); - - sphere.collider.center.copy(playerCollider.end).addScaledVector(playerDirection, playerCollider.radius * 1.5); - - // throw the ball with more force if we hold the button longer, and if we move forward - - const impulse = 15 + 30 * (1 - Math.exp((mouseTime - performance.now()) * 0.001)); - - sphere.velocity.copy(playerDirection).multiplyScalar(impulse); - sphere.velocity.addScaledVector(playerVelocity, 2); - - sphereIdx = (sphereIdx + 1) % spheres.length; -} - -function playerCollisions() { - const result = worldOctree.capsuleIntersect(playerCollider); - - playerOnFloor = false; - - if (result) { - playerOnFloor = result.normal.y > 0; - - if (!playerOnFloor) { - playerVelocity.addScaledVector(result.normal, -result.normal.dot(playerVelocity)); - } - - if (result.depth >= 1e-10) { - playerCollider.translate(result.normal.multiplyScalar(result.depth)); - } - } -} - -function updatePlayer(deltaTime) { - let damping = Math.exp(-4 * deltaTime) - 1; - - if (!playerOnFloor) { - playerVelocity.y -= GRAVITY * deltaTime; - - // small air resistance - damping *= 0.1; - } - - playerVelocity.addScaledVector(playerVelocity, damping); - - const deltaPosition = playerVelocity.clone().multiplyScalar(deltaTime); - playerCollider.translate(deltaPosition); - - playerCollisions(); - - camera.position.copy(playerCollider.end); -} - -function playerSphereCollision(sphere) { - const center = vector1.addVectors(playerCollider.start, playerCollider.end).multiplyScalar(0.5); - - const sphere_center = sphere.collider.center; - - const r = playerCollider.radius + sphere.collider.radius; - const r2 = r * r; - - // approximation: player = 3 spheres - - for (const point of [playerCollider.start, playerCollider.end, center]) { - const d2 = point.distanceToSquared(sphere_center); - - if (d2 < r2) { - const normal = vector1.subVectors(point, sphere_center).normalize(); - const v1 = vector2.copy(normal).multiplyScalar(normal.dot(playerVelocity)); - const v2 = vector3.copy(normal).multiplyScalar(normal.dot(sphere.velocity)); - - playerVelocity.add(v2).sub(v1); - sphere.velocity.add(v1).sub(v2); - - const d = (r - Math.sqrt(d2)) / 2; - sphere_center.addScaledVector(normal, -d); - } - } -} - -function spheresCollisions() { - for (let i = 0, length = spheres.length; i < length; i++) { - const s1 = spheres[i]; - - for (let j = i + 1; j < length; j++) { - const s2 = spheres[j]; - - const d2 = s1.collider.center.distanceToSquared(s2.collider.center); - const r = s1.collider.radius + s2.collider.radius; - const r2 = r * r; - - if (d2 < r2) { - const normal = vector1.subVectors(s1.collider.center, s2.collider.center).normalize(); - const v1 = vector2.copy(normal).multiplyScalar(normal.dot(s1.velocity)); - const v2 = vector3.copy(normal).multiplyScalar(normal.dot(s2.velocity)); - - s1.velocity.add(v2).sub(v1); - s2.velocity.add(v1).sub(v2); - - const d = (r - Math.sqrt(d2)) / 2; - - s1.collider.center.addScaledVector(normal, d); - s2.collider.center.addScaledVector(normal, -d); - } - } - } -} - -function updateSpheres(deltaTime) { - spheres.forEach(sphere => { - sphere.collider.center.addScaledVector(sphere.velocity, deltaTime); - - const result = worldOctree.sphereIntersect(sphere.collider); - - if (result) { - sphere.velocity.addScaledVector(result.normal, -result.normal.dot(sphere.velocity) * 1.5); - sphere.collider.center.add(result.normal.multiplyScalar(result.depth)); - } else { - sphere.velocity.y -= GRAVITY * deltaTime; - } - - const damping = Math.exp(-1.5 * deltaTime) - 1; - sphere.velocity.addScaledVector(sphere.velocity, damping); - - playerSphereCollision(sphere); - }); - - spheresCollisions(); - - for (const sphere of spheres) { - sphere.mesh.position.copy(sphere.collider.center); - } -} - -function getForwardVector() { - camera.getWorldDirection(playerDirection); - playerDirection.y = 0; - playerDirection.normalize(); - - return playerDirection; -} - -function getSideVector() { - camera.getWorldDirection(playerDirection); - playerDirection.y = 0; - playerDirection.normalize(); - playerDirection.cross(camera.up); - - return playerDirection; -} - -function controls(deltaTime) { - // gives a bit of air control - const speedDelta = deltaTime * (playerOnFloor ? 25 : 8); - - if (keyStates['KeyW']) { - playerVelocity.add(getForwardVector().multiplyScalar(speedDelta)); - } - - if (keyStates['KeyS']) { - playerVelocity.add(getForwardVector().multiplyScalar(-speedDelta)); - } - - if (keyStates['KeyA']) { - playerVelocity.add(getSideVector().multiplyScalar(-speedDelta)); - } - - if (keyStates['KeyD']) { - playerVelocity.add(getSideVector().multiplyScalar(speedDelta)); - } - - if (playerOnFloor) { - if (keyStates['Space']) { - playerVelocity.y = 15; - } - } -} - -const loader = new GLTFLoader().setPath('./models/gltf/'); - -loader.load('collision-world.glb', gltf => { - scene.add(gltf.scene); - - worldOctree.fromGraphNode(gltf.scene); - - gltf.scene.traverse(child => { - if (child.isMesh) { - child.castShadow = true; - child.receiveShadow = true; - - if (child.material.map) { - child.material.map.anisotropy = 4; - } - } - }); - - const helper = new OctreeHelper(worldOctree); - helper.visible = false; - scene.add(helper); - - const gui = new GUI({ width: 200 }); - gui.add({ debug: false }, 'debug').onChange(function (value) { - helper.visible = value; - }); -}); - -function teleportPlayerIfOob() { - if (camera.position.y <= -25) { - playerCollider.start.set(0, 0.35, 0); - playerCollider.end.set(0, 1, 0); - playerCollider.radius = 0.35; - camera.position.copy(playerCollider.end); - camera.rotation.set(0, 0, 0); - } -} - -function animate() { - const deltaTime = Math.min(0.05, clock.getDelta()) / STEPS_PER_FRAME; - - // we look for collisions in substeps to mitigate the risk of - // an object traversing another too quickly for detection. - - for (let i = 0; i < STEPS_PER_FRAME; i++) { - controls(deltaTime); - - updatePlayer(deltaTime); - - updateSpheres(deltaTime); - - teleportPlayerIfOob(); - } - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/misc_animation_groups.ts b/examples-testing/examples/misc_animation_groups.ts deleted file mode 100644 index 33fc41997..000000000 --- a/examples-testing/examples/misc_animation_groups.ts +++ /dev/null @@ -1,125 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let stats, clock; -let scene, camera, renderer, mixer; - -init(); - -function init() { - scene = new THREE.Scene(); - - // - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(50, 50, 100); - camera.lookAt(scene.position); - - // all objects of this animation group share a common animation state - - const animationGroup = new THREE.AnimationObjectGroup(); - - // - - const geometry = new THREE.BoxGeometry(5, 5, 5); - const material = new THREE.MeshBasicMaterial({ transparent: true }); - - // - - for (let i = 0; i < 5; i++) { - for (let j = 0; j < 5; j++) { - const mesh = new THREE.Mesh(geometry, material); - - mesh.position.x = 32 - 16 * i; - mesh.position.y = 0; - mesh.position.z = 32 - 16 * j; - - scene.add(mesh); - animationGroup.add(mesh); - } - } - - // create some keyframe tracks - - const xAxis = new THREE.Vector3(1, 0, 0); - const qInitial = new THREE.Quaternion().setFromAxisAngle(xAxis, 0); - const qFinal = new THREE.Quaternion().setFromAxisAngle(xAxis, Math.PI); - const quaternionKF = new THREE.QuaternionKeyframeTrack( - '.quaternion', - [0, 1, 2], - [ - qInitial.x, - qInitial.y, - qInitial.z, - qInitial.w, - qFinal.x, - qFinal.y, - qFinal.z, - qFinal.w, - qInitial.x, - qInitial.y, - qInitial.z, - qInitial.w, - ], - ); - - const colorKF = new THREE.ColorKeyframeTrack( - '.material.color', - [0, 1, 2], - [1, 0, 0, 0, 1, 0, 0, 0, 1], - THREE.InterpolateDiscrete, - ); - const opacityKF = new THREE.NumberKeyframeTrack('.material.opacity', [0, 1, 2], [1, 0, 1]); - - // create clip - - const clip = new THREE.AnimationClip('default', 3, [quaternionKF, colorKF, opacityKF]); - - // apply the animation group to the mixer as the root object - - mixer = new THREE.AnimationMixer(animationGroup); - - const clipAction = mixer.clipAction(clip); - clipAction.play(); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - clock = new THREE.Clock(); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const delta = clock.getDelta(); - - if (mixer) { - mixer.update(delta); - } - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/misc_animation_keys.ts b/examples-testing/examples/misc_animation_keys.ts deleted file mode 100644 index e2f141f91..000000000 --- a/examples-testing/examples/misc_animation_keys.ts +++ /dev/null @@ -1,129 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let stats, clock; -let scene, camera, renderer, mixer; - -init(); - -function init() { - scene = new THREE.Scene(); - - // - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(25, 25, 50); - camera.lookAt(scene.position); - - // - - const axesHelper = new THREE.AxesHelper(10); - scene.add(axesHelper); - - // - - const geometry = new THREE.BoxGeometry(5, 5, 5); - const material = new THREE.MeshBasicMaterial({ color: 0xffffff, transparent: true }); - const mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // create a keyframe track (i.e. a timed sequence of keyframes) for each animated property - // Note: the keyframe track type should correspond to the type of the property being animated - - // POSITION - const positionKF = new THREE.VectorKeyframeTrack('.position', [0, 1, 2], [0, 0, 0, 30, 0, 0, 0, 0, 0]); - - // SCALE - const scaleKF = new THREE.VectorKeyframeTrack('.scale', [0, 1, 2], [1, 1, 1, 2, 2, 2, 1, 1, 1]); - - // ROTATION - // Rotation should be performed using quaternions, using a THREE.QuaternionKeyframeTrack - // Interpolating Euler angles (.rotation property) can be problematic and is currently not supported - - // set up rotation about x axis - const xAxis = new THREE.Vector3(1, 0, 0); - - const qInitial = new THREE.Quaternion().setFromAxisAngle(xAxis, 0); - const qFinal = new THREE.Quaternion().setFromAxisAngle(xAxis, Math.PI); - const quaternionKF = new THREE.QuaternionKeyframeTrack( - '.quaternion', - [0, 1, 2], - [ - qInitial.x, - qInitial.y, - qInitial.z, - qInitial.w, - qFinal.x, - qFinal.y, - qFinal.z, - qFinal.w, - qInitial.x, - qInitial.y, - qInitial.z, - qInitial.w, - ], - ); - - // COLOR - const colorKF = new THREE.ColorKeyframeTrack( - '.material.color', - [0, 1, 2], - [1, 0, 0, 0, 1, 0, 0, 0, 1], - THREE.InterpolateDiscrete, - ); - - // OPACITY - const opacityKF = new THREE.NumberKeyframeTrack('.material.opacity', [0, 1, 2], [1, 0, 1]); - - // create an animation sequence with the tracks - // If a negative time value is passed, the duration will be calculated from the times of the passed tracks array - const clip = new THREE.AnimationClip('Action', 3, [scaleKF, positionKF, quaternionKF, colorKF, opacityKF]); - - // setup the THREE.AnimationMixer - mixer = new THREE.AnimationMixer(mesh); - - // create a ClipAction and set it to play - const clipAction = mixer.clipAction(clip); - clipAction.play(); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - clock = new THREE.Clock(); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const delta = clock.getDelta(); - - if (mixer) { - mixer.update(delta); - } - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/misc_boxselection.ts b/examples-testing/examples/misc_boxselection.ts deleted file mode 100644 index e7079c405..000000000 --- a/examples-testing/examples/misc_boxselection.ts +++ /dev/null @@ -1,137 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { SelectionBox } from 'three/addons/interactive/SelectionBox.js'; -import { SelectionHelper } from 'three/addons/interactive/SelectionHelper.js'; - -let container, stats; -let camera, scene, renderer; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 500); - camera.position.z = 50; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - scene.add(new THREE.AmbientLight(0xaaaaaa)); - - const light = new THREE.SpotLight(0xffffff, 10000); - light.position.set(0, 25, 50); - light.angle = Math.PI / 5; - - light.castShadow = true; - light.shadow.camera.near = 10; - light.shadow.camera.far = 100; - light.shadow.mapSize.width = 1024; - light.shadow.mapSize.height = 1024; - - scene.add(light); - - const geometry = new THREE.BoxGeometry(); - - for (let i = 0; i < 200; i++) { - const object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff })); - - object.position.x = Math.random() * 80 - 40; - object.position.y = Math.random() * 45 - 25; - object.position.z = Math.random() * 45 - 25; - - object.rotation.x = Math.random() * 2 * Math.PI; - object.rotation.y = Math.random() * 2 * Math.PI; - object.rotation.z = Math.random() * 2 * Math.PI; - - object.scale.x = Math.random() * 2 + 1; - object.scale.y = Math.random() * 2 + 1; - object.scale.z = Math.random() * 2 + 1; - - object.castShadow = true; - object.receiveShadow = true; - - scene.add(object); - } - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - renderer.shadowMap.type = THREE.PCFShadowMap; - - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - renderer.render(scene, camera); - - stats.update(); -} - -const selectionBox = new SelectionBox(camera, scene); -const helper = new SelectionHelper(renderer, 'selectBox'); - -document.addEventListener('pointerdown', function (event) { - for (const item of selectionBox.collection) { - item.material.emissive.set(0x000000); - } - - selectionBox.startPoint.set( - (event.clientX / window.innerWidth) * 2 - 1, - -(event.clientY / window.innerHeight) * 2 + 1, - 0.5, - ); -}); - -document.addEventListener('pointermove', function (event) { - if (helper.isDown) { - for (let i = 0; i < selectionBox.collection.length; i++) { - selectionBox.collection[i].material.emissive.set(0x000000); - } - - selectionBox.endPoint.set( - (event.clientX / window.innerWidth) * 2 - 1, - -(event.clientY / window.innerHeight) * 2 + 1, - 0.5, - ); - - const allSelected = selectionBox.select(); - - for (let i = 0; i < allSelected.length; i++) { - allSelected[i].material.emissive.set(0xffffff); - } - } -}); - -document.addEventListener('pointerup', function (event) { - selectionBox.endPoint.set( - (event.clientX / window.innerWidth) * 2 - 1, - -(event.clientY / window.innerHeight) * 2 + 1, - 0.5, - ); - - const allSelected = selectionBox.select(); - - for (let i = 0; i < allSelected.length; i++) { - allSelected[i].material.emissive.set(0xffffff); - } -}); diff --git a/examples-testing/examples/misc_controls_arcball.ts b/examples-testing/examples/misc_controls_arcball.ts deleted file mode 100644 index fbef33189..000000000 --- a/examples-testing/examples/misc_controls_arcball.ts +++ /dev/null @@ -1,210 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { ArcballControls } from 'three/addons/controls/ArcballControls.js'; - -import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -const cameras = ['Orthographic', 'Perspective']; -const cameraType = { type: 'Perspective' }; - -const perspectiveDistance = 2.5; -const orthographicDistance = 120; -let camera, controls, scene, renderer, gui; -let folderOptions, folderAnimations; - -const arcballGui = { - gizmoVisible: true, - - setArcballControls: function () { - controls = new ArcballControls(camera, renderer.domElement, scene); - controls.addEventListener('change', render); - - this.gizmoVisible = true; - - this.populateGui(); - }, - - populateGui: function () { - folderOptions.add(controls, 'enabled').name('Enable controls'); - folderOptions.add(controls, 'enableGrid').name('Enable Grid'); - folderOptions.add(controls, 'enableRotate').name('Enable rotate'); - folderOptions.add(controls, 'enablePan').name('Enable pan'); - folderOptions.add(controls, 'enableZoom').name('Enable zoom'); - folderOptions.add(controls, 'cursorZoom').name('Cursor zoom'); - folderOptions.add(controls, 'adjustNearFar').name('adjust near/far'); - folderOptions.add(controls, 'scaleFactor', 1.1, 10, 0.1).name('Scale factor'); - folderOptions.add(controls, 'minDistance', 0, 50, 0.5).name('Min distance'); - folderOptions.add(controls, 'maxDistance', 0, 50, 0.5).name('Max distance'); - folderOptions.add(controls, 'minZoom', 0, 50, 0.5).name('Min zoom'); - folderOptions.add(controls, 'maxZoom', 0, 50, 0.5).name('Max zoom'); - folderOptions - .add(arcballGui, 'gizmoVisible') - .name('Show gizmos') - .onChange(function () { - controls.setGizmosVisible(arcballGui.gizmoVisible); - }); - folderOptions.add(controls, 'copyState').name('Copy state(ctrl+c)'); - folderOptions.add(controls, 'pasteState').name('Paste state(ctrl+v)'); - folderOptions.add(controls, 'reset').name('Reset'); - folderAnimations.add(controls, 'enableAnimations').name('Enable anim.'); - folderAnimations.add(controls, 'dampingFactor', 0, 100, 1).name('Damping'); - folderAnimations.add(controls, 'wMax', 0, 100, 1).name('Angular spd'); - }, -}; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.toneMapping = THREE.ReinhardToneMapping; - renderer.toneMappingExposure = 3; - renderer.domElement.style.background = 'linear-gradient( 180deg, rgba( 0,0,0,1 ) 0%, rgba( 128,128,255,1 ) 100% )'; - container.appendChild(renderer.domElement); - - // - - scene = new THREE.Scene(); - - camera = makePerspectiveCamera(); - camera.position.set(0, 0, perspectiveDistance); - - const material = new THREE.MeshStandardMaterial(); - - new OBJLoader().setPath('models/obj/cerberus/').load('Cerberus.obj', function (group) { - const textureLoader = new THREE.TextureLoader().setPath('models/obj/cerberus/'); - - material.roughness = 1; - material.metalness = 1; - - const diffuseMap = textureLoader.load('Cerberus_A.jpg', render); - diffuseMap.colorSpace = THREE.SRGBColorSpace; - material.map = diffuseMap; - - material.metalnessMap = material.roughnessMap = textureLoader.load('Cerberus_RM.jpg', render); - material.normalMap = textureLoader.load('Cerberus_N.jpg', render); - - material.map.wrapS = THREE.RepeatWrapping; - material.roughnessMap.wrapS = THREE.RepeatWrapping; - material.metalnessMap.wrapS = THREE.RepeatWrapping; - material.normalMap.wrapS = THREE.RepeatWrapping; - - group.traverse(function (child) { - if (child.isMesh) { - child.material = material; - } - }); - - group.rotation.y = Math.PI / 2; - group.position.x += 0.25; - scene.add(group); - render(); - - new RGBELoader().setPath('textures/equirectangular/').load('venice_sunset_1k.hdr', function (hdrEquirect) { - hdrEquirect.mapping = THREE.EquirectangularReflectionMapping; - - scene.environment = hdrEquirect; - - render(); - }); - - window.addEventListener('keydown', onKeyDown); - window.addEventListener('resize', onWindowResize); - - // - - gui = new GUI(); - gui.add(cameraType, 'type', cameras) - .name('Choose Camera') - .onChange(function () { - setCamera(cameraType.type); - }); - - folderOptions = gui.addFolder('Arcball parameters'); - folderAnimations = folderOptions.addFolder('Animations'); - - arcballGui.setArcballControls(); - - render(); - }); -} - -function makeOrthographicCamera() { - const halfFovV = THREE.MathUtils.DEG2RAD * 45 * 0.5; - const halfFovH = Math.atan((window.innerWidth / window.innerHeight) * Math.tan(halfFovV)); - - const halfW = perspectiveDistance * Math.tan(halfFovH); - const halfH = perspectiveDistance * Math.tan(halfFovV); - const near = 0.01; - const far = 2000; - const newCamera = new THREE.OrthographicCamera(-halfW, halfW, halfH, -halfH, near, far); - return newCamera; -} - -function makePerspectiveCamera() { - const fov = 45; - const aspect = window.innerWidth / window.innerHeight; - const near = 0.01; - const far = 2000; - const newCamera = new THREE.PerspectiveCamera(fov, aspect, near, far); - return newCamera; -} - -function onWindowResize() { - if (camera.type == 'OrthographicCamera') { - const halfFovV = THREE.MathUtils.DEG2RAD * 45 * 0.5; - const halfFovH = Math.atan((window.innerWidth / window.innerHeight) * Math.tan(halfFovV)); - - const halfW = perspectiveDistance * Math.tan(halfFovH); - const halfH = perspectiveDistance * Math.tan(halfFovV); - camera.left = -halfW; - camera.right = halfW; - camera.top = halfH; - camera.bottom = -halfH; - } else if (camera.type == 'PerspectiveCamera') { - camera.aspect = window.innerWidth / window.innerHeight; - } - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, camera); -} - -function onKeyDown(event) { - if (event.key === 'c') { - if (event.ctrlKey || event.metaKey) { - controls.copyState(); - } - } else if (event.key === 'v') { - if (event.ctrlKey || event.metaKey) { - controls.pasteState(); - } - } -} - -function setCamera(type) { - if (type == 'Orthographic') { - camera = makeOrthographicCamera(); - camera.position.set(0, 0, orthographicDistance); - } else if (type == 'Perspective') { - camera = makePerspectiveCamera(); - camera.position.set(0, 0, perspectiveDistance); - } - - controls.setCamera(camera); - - render(); -} diff --git a/examples-testing/examples/misc_controls_drag.ts b/examples-testing/examples/misc_controls_drag.ts deleted file mode 100644 index eb7e7ca0f..000000000 --- a/examples-testing/examples/misc_controls_drag.ts +++ /dev/null @@ -1,153 +0,0 @@ -import * as THREE from 'three'; - -import { DragControls } from 'three/addons/controls/DragControls.js'; - -let container; -let camera, scene, renderer; -let controls, group; -let enableSelection = false; - -const objects = []; - -const mouse = new THREE.Vector2(), - raycaster = new THREE.Raycaster(); - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 500); - camera.position.z = 25; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - scene.add(new THREE.AmbientLight(0xaaaaaa)); - - const light = new THREE.SpotLight(0xffffff, 10000); - light.position.set(0, 25, 50); - light.angle = Math.PI / 9; - - light.castShadow = true; - light.shadow.camera.near = 10; - light.shadow.camera.far = 100; - light.shadow.mapSize.width = 1024; - light.shadow.mapSize.height = 1024; - - scene.add(light); - - group = new THREE.Group(); - scene.add(group); - - const geometry = new THREE.BoxGeometry(); - - for (let i = 0; i < 200; i++) { - const object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff })); - - object.position.x = Math.random() * 30 - 15; - object.position.y = Math.random() * 15 - 7.5; - object.position.z = Math.random() * 20 - 10; - - object.rotation.x = Math.random() * 2 * Math.PI; - object.rotation.y = Math.random() * 2 * Math.PI; - object.rotation.z = Math.random() * 2 * Math.PI; - - object.scale.x = Math.random() * 2 + 1; - object.scale.y = Math.random() * 2 + 1; - object.scale.z = Math.random() * 2 + 1; - - object.castShadow = true; - object.receiveShadow = true; - - scene.add(object); - - objects.push(object); - } - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.shadowMap.enabled = true; - renderer.shadowMap.type = THREE.PCFShadowMap; - - container.appendChild(renderer.domElement); - - controls = new DragControls([...objects], camera, renderer.domElement); - controls.rotateSpeed = 2; - controls.addEventListener('drag', render); - - // - - window.addEventListener('resize', onWindowResize); - - document.addEventListener('click', onClick); - window.addEventListener('keydown', onKeyDown); - window.addEventListener('keyup', onKeyUp); - - render(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function onKeyDown(event) { - enableSelection = event.keyCode === 16 ? true : false; - - if (event.keyCode === 77) { - controls.mode = controls.mode === 'translate' ? 'rotate' : 'translate'; - } -} - -function onKeyUp() { - enableSelection = false; -} - -function onClick(event) { - event.preventDefault(); - - if (enableSelection === true) { - const draggableObjects = controls.getObjects(); - draggableObjects.length = 0; - - mouse.x = (event.clientX / window.innerWidth) * 2 - 1; - mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; - - raycaster.setFromCamera(mouse, camera); - - const intersections = raycaster.intersectObjects(objects, true); - - if (intersections.length > 0) { - const object = intersections[0].object; - - if (group.children.includes(object) === true) { - object.material.emissive.set(0x000000); - scene.attach(object); - } else { - object.material.emissive.set(0xaaaaaa); - group.attach(object); - } - - controls.transformGroup = true; - draggableObjects.push(group); - } - - if (group.children.length === 0) { - controls.transformGroup = false; - draggableObjects.push(...objects); - } - } - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/misc_controls_fly.ts b/examples-testing/examples/misc_controls_fly.ts deleted file mode 100644 index 4e58a36ec..000000000 --- a/examples-testing/examples/misc_controls_fly.ts +++ /dev/null @@ -1,214 +0,0 @@ -import * as THREE from 'three'; -import { pass } from 'three/tsl'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { FlyControls } from 'three/addons/controls/FlyControls.js'; - -const radius = 6371; -const tilt = 0.41; -const rotationSpeed = 0.02; - -const cloudsScale = 1.005; -const moonScale = 0.23; - -const MARGIN = 0; -let SCREEN_HEIGHT = window.innerHeight - MARGIN * 2; -let SCREEN_WIDTH = window.innerWidth; - -let camera, controls, scene, renderer, stats; -let geometry, meshPlanet, meshClouds, meshMoon; -let dirLight; - -let postProcessing; - -const textureLoader = new THREE.TextureLoader(); - -let d, dPlanet, dMoon; -const dMoonVec = new THREE.Vector3(); - -const clock = new THREE.Clock(); - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(25, SCREEN_WIDTH / SCREEN_HEIGHT, 50, 1e7); - camera.position.z = radius * 5; - - scene = new THREE.Scene(); - scene.fog = new THREE.FogExp2(0x000000, 0.00000025); - - dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.position.set(-1, 0, 1).normalize(); - scene.add(dirLight); - - const materialNormalMap = new THREE.MeshPhongMaterial({ - specular: 0x7c7c7c, - shininess: 15, - map: textureLoader.load('textures/planets/earth_atmos_2048.jpg'), - specularMap: textureLoader.load('textures/planets/earth_specular_2048.jpg'), - normalMap: textureLoader.load('textures/planets/earth_normal_2048.jpg'), - - // y scale is negated to compensate for normal map handedness. - normalScale: new THREE.Vector2(0.85, -0.85), - }); - materialNormalMap.map.colorSpace = THREE.SRGBColorSpace; - - // planet - - geometry = new THREE.SphereGeometry(radius, 100, 50); - - meshPlanet = new THREE.Mesh(geometry, materialNormalMap); - meshPlanet.rotation.y = 0; - meshPlanet.rotation.z = tilt; - scene.add(meshPlanet); - - // clouds - - const materialClouds = new THREE.MeshLambertMaterial({ - map: textureLoader.load('textures/planets/earth_clouds_1024.png'), - transparent: true, - }); - materialClouds.map.colorSpace = THREE.SRGBColorSpace; - - meshClouds = new THREE.Mesh(geometry, materialClouds); - meshClouds.scale.set(cloudsScale, cloudsScale, cloudsScale); - meshClouds.rotation.z = tilt; - scene.add(meshClouds); - - // moon - - const materialMoon = new THREE.MeshPhongMaterial({ - map: textureLoader.load('textures/planets/moon_1024.jpg'), - }); - materialMoon.map.colorSpace = THREE.SRGBColorSpace; - - meshMoon = new THREE.Mesh(geometry, materialMoon); - meshMoon.position.set(radius * 5, 0, 0); - meshMoon.scale.set(moonScale, moonScale, moonScale); - scene.add(meshMoon); - - // stars - - const r = radius, - starsGeometry = [new THREE.BufferGeometry(), new THREE.BufferGeometry()]; - - const vertices1 = []; - const vertices2 = []; - - const vertex = new THREE.Vector3(); - - for (let i = 0; i < 250; i++) { - vertex.x = Math.random() * 2 - 1; - vertex.y = Math.random() * 2 - 1; - vertex.z = Math.random() * 2 - 1; - vertex.multiplyScalar(r); - - vertices1.push(vertex.x, vertex.y, vertex.z); - } - - for (let i = 0; i < 1500; i++) { - vertex.x = Math.random() * 2 - 1; - vertex.y = Math.random() * 2 - 1; - vertex.z = Math.random() * 2 - 1; - vertex.multiplyScalar(r); - - vertices2.push(vertex.x, vertex.y, vertex.z); - } - - starsGeometry[0].setAttribute('position', new THREE.Float32BufferAttribute(vertices1, 3)); - starsGeometry[1].setAttribute('position', new THREE.Float32BufferAttribute(vertices2, 3)); - - const starsMaterials = [ - new THREE.PointsMaterial({ color: 0x9c9c9c }), - new THREE.PointsMaterial({ color: 0x838383 }), - new THREE.PointsMaterial({ color: 0x5a5a5a }), - ]; - - for (let i = 10; i < 30; i++) { - const stars = new THREE.Points(starsGeometry[i % 2], starsMaterials[i % 3]); - - stars.rotation.x = Math.random() * 6; - stars.rotation.y = Math.random() * 6; - stars.rotation.z = Math.random() * 6; - stars.scale.setScalar(i * 10); - - stars.matrixAutoUpdate = false; - stars.updateMatrix(); - - scene.add(stars); - } - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - controls = new FlyControls(camera, renderer.domElement); - - controls.movementSpeed = 1000; - controls.domElement = renderer.domElement; - controls.rollSpeed = Math.PI / 24; - controls.autoForward = false; - controls.dragToLook = false; - - // - - stats = new Stats(); - document.body.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); - - // postprocessing - - postProcessing = new THREE.PostProcessing(renderer); - - const scenePass = pass(scene, camera); - const scenePassColor = scenePass.getTextureNode(); - - postProcessing.outputNode = scenePassColor.film(); -} - -function onWindowResize() { - SCREEN_HEIGHT = window.innerHeight; - SCREEN_WIDTH = window.innerWidth; - - camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT; - camera.updateProjectionMatrix(); - - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - // rotate the planet and clouds - - const delta = clock.getDelta(); - - meshPlanet.rotation.y += rotationSpeed * delta; - meshClouds.rotation.y += 1.25 * rotationSpeed * delta; - - // slow down as we approach the surface - - dPlanet = camera.position.length(); - - dMoonVec.subVectors(camera.position, meshMoon.position); - dMoon = dMoonVec.length(); - - if (dMoon < dPlanet) { - d = dMoon - radius * moonScale * 1.01; - } else { - d = dPlanet - radius * 1.01; - } - - controls.movementSpeed = 0.33 * d; - controls.update(delta); - - postProcessing.render(); -} diff --git a/examples-testing/examples/misc_controls_map.ts b/examples-testing/examples/misc_controls_map.ts deleted file mode 100644 index 2f52190cf..000000000 --- a/examples-testing/examples/misc_controls_map.ts +++ /dev/null @@ -1,98 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { MapControls } from 'three/addons/controls/MapControls.js'; - -let camera, controls, scene, renderer; - -init(); -//render(); // remove when using animation loop - -function init() { - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xcccccc); - scene.fog = new THREE.FogExp2(0xcccccc, 0.002); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 200, -400); - - // controls - - controls = new MapControls(camera, renderer.domElement); - - //controls.addEventListener( 'change', render ); // call this only in static scenes (i.e., if there is no animation loop) - - controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled - controls.dampingFactor = 0.05; - - controls.screenSpacePanning = false; - - controls.minDistance = 100; - controls.maxDistance = 500; - - controls.maxPolarAngle = Math.PI / 2; - - // world - - const geometry = new THREE.BoxGeometry(); - geometry.translate(0, 0.5, 0); - const material = new THREE.MeshPhongMaterial({ color: 0xeeeeee, flatShading: true }); - - for (let i = 0; i < 500; i++) { - const mesh = new THREE.Mesh(geometry, material); - mesh.position.x = Math.random() * 1600 - 800; - mesh.position.y = 0; - mesh.position.z = Math.random() * 1600 - 800; - mesh.scale.x = 20; - mesh.scale.y = Math.random() * 80 + 10; - mesh.scale.z = 20; - mesh.updateMatrix(); - mesh.matrixAutoUpdate = false; - scene.add(mesh); - } - - // lights - - const dirLight1 = new THREE.DirectionalLight(0xffffff, 3); - dirLight1.position.set(1, 1, 1); - scene.add(dirLight1); - - const dirLight2 = new THREE.DirectionalLight(0x002288, 3); - dirLight2.position.set(-1, -1, -1); - scene.add(dirLight2); - - const ambientLight = new THREE.AmbientLight(0x555555); - scene.add(ambientLight); - - // - - window.addEventListener('resize', onWindowResize); - - const gui = new GUI(); - gui.add(controls, 'zoomToCursor'); - gui.add(controls, 'screenSpacePanning'); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - controls.update(); // only required if controls.enableDamping = true, or if controls.autoRotate = true - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/misc_controls_orbit.ts b/examples-testing/examples/misc_controls_orbit.ts deleted file mode 100644 index 186e216cb..000000000 --- a/examples-testing/examples/misc_controls_orbit.ts +++ /dev/null @@ -1,89 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, controls, scene, renderer; - -init(); -//render(); // remove when using animation loop - -function init() { - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xcccccc); - scene.fog = new THREE.FogExp2(0xcccccc, 0.002); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(400, 200, 0); - - // controls - - controls = new OrbitControls(camera, renderer.domElement); - controls.listenToKeyEvents(window); // optional - - //controls.addEventListener( 'change', render ); // call this only in static scenes (i.e., if there is no animation loop) - - controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled - controls.dampingFactor = 0.05; - - controls.screenSpacePanning = false; - - controls.minDistance = 100; - controls.maxDistance = 500; - - controls.maxPolarAngle = Math.PI / 2; - - // world - - const geometry = new THREE.ConeGeometry(10, 30, 4, 1); - const material = new THREE.MeshPhongMaterial({ color: 0xffffff, flatShading: true }); - - for (let i = 0; i < 500; i++) { - const mesh = new THREE.Mesh(geometry, material); - mesh.position.x = Math.random() * 1600 - 800; - mesh.position.y = 0; - mesh.position.z = Math.random() * 1600 - 800; - mesh.updateMatrix(); - mesh.matrixAutoUpdate = false; - scene.add(mesh); - } - - // lights - - const dirLight1 = new THREE.DirectionalLight(0xffffff, 3); - dirLight1.position.set(1, 1, 1); - scene.add(dirLight1); - - const dirLight2 = new THREE.DirectionalLight(0x002288, 3); - dirLight2.position.set(-1, -1, -1); - scene.add(dirLight2); - - const ambientLight = new THREE.AmbientLight(0x555555); - scene.add(ambientLight); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - controls.update(); // only required if controls.enableDamping = true, or if controls.autoRotate = true - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/misc_controls_pointerlock.ts b/examples-testing/examples/misc_controls_pointerlock.ts deleted file mode 100644 index 66b061671..000000000 --- a/examples-testing/examples/misc_controls_pointerlock.ts +++ /dev/null @@ -1,245 +0,0 @@ -import * as THREE from 'three'; - -import { PointerLockControls } from 'three/addons/controls/PointerLockControls.js'; - -let camera, scene, renderer, controls; - -const objects = []; - -let raycaster; - -let moveForward = false; -let moveBackward = false; -let moveLeft = false; -let moveRight = false; -let canJump = false; - -let prevTime = performance.now(); -const velocity = new THREE.Vector3(); -const direction = new THREE.Vector3(); -const vertex = new THREE.Vector3(); -const color = new THREE.Color(); - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.y = 10; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - scene.fog = new THREE.Fog(0xffffff, 0, 750); - - const light = new THREE.HemisphereLight(0xeeeeff, 0x777788, 2.5); - light.position.set(0.5, 1, 0.75); - scene.add(light); - - controls = new PointerLockControls(camera, document.body); - - const blocker = document.getElementById('blocker'); - const instructions = document.getElementById('instructions'); - - instructions.addEventListener('click', function () { - controls.lock(); - }); - - controls.addEventListener('lock', function () { - instructions.style.display = 'none'; - blocker.style.display = 'none'; - }); - - controls.addEventListener('unlock', function () { - blocker.style.display = 'block'; - instructions.style.display = ''; - }); - - scene.add(controls.getObject()); - - const onKeyDown = function (event) { - switch (event.code) { - case 'ArrowUp': - case 'KeyW': - moveForward = true; - break; - - case 'ArrowLeft': - case 'KeyA': - moveLeft = true; - break; - - case 'ArrowDown': - case 'KeyS': - moveBackward = true; - break; - - case 'ArrowRight': - case 'KeyD': - moveRight = true; - break; - - case 'Space': - if (canJump === true) velocity.y += 350; - canJump = false; - break; - } - }; - - const onKeyUp = function (event) { - switch (event.code) { - case 'ArrowUp': - case 'KeyW': - moveForward = false; - break; - - case 'ArrowLeft': - case 'KeyA': - moveLeft = false; - break; - - case 'ArrowDown': - case 'KeyS': - moveBackward = false; - break; - - case 'ArrowRight': - case 'KeyD': - moveRight = false; - break; - } - }; - - document.addEventListener('keydown', onKeyDown); - document.addEventListener('keyup', onKeyUp); - - raycaster = new THREE.Raycaster(new THREE.Vector3(), new THREE.Vector3(0, -1, 0), 0, 10); - - // floor - - let floorGeometry = new THREE.PlaneGeometry(2000, 2000, 100, 100); - floorGeometry.rotateX(-Math.PI / 2); - - // vertex displacement - - let position = floorGeometry.attributes.position; - - for (let i = 0, l = position.count; i < l; i++) { - vertex.fromBufferAttribute(position, i); - - vertex.x += Math.random() * 20 - 10; - vertex.y += Math.random() * 2; - vertex.z += Math.random() * 20 - 10; - - position.setXYZ(i, vertex.x, vertex.y, vertex.z); - } - - floorGeometry = floorGeometry.toNonIndexed(); // ensure each face has unique vertices - - position = floorGeometry.attributes.position; - const colorsFloor = []; - - for (let i = 0, l = position.count; i < l; i++) { - color.setHSL(Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75, THREE.SRGBColorSpace); - colorsFloor.push(color.r, color.g, color.b); - } - - floorGeometry.setAttribute('color', new THREE.Float32BufferAttribute(colorsFloor, 3)); - - const floorMaterial = new THREE.MeshBasicMaterial({ vertexColors: true }); - - const floor = new THREE.Mesh(floorGeometry, floorMaterial); - scene.add(floor); - - // objects - - const boxGeometry = new THREE.BoxGeometry(20, 20, 20).toNonIndexed(); - - position = boxGeometry.attributes.position; - const colorsBox = []; - - for (let i = 0, l = position.count; i < l; i++) { - color.setHSL(Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75, THREE.SRGBColorSpace); - colorsBox.push(color.r, color.g, color.b); - } - - boxGeometry.setAttribute('color', new THREE.Float32BufferAttribute(colorsBox, 3)); - - for (let i = 0; i < 500; i++) { - const boxMaterial = new THREE.MeshPhongMaterial({ specular: 0xffffff, flatShading: true, vertexColors: true }); - boxMaterial.color.setHSL(Math.random() * 0.2 + 0.5, 0.75, Math.random() * 0.25 + 0.75, THREE.SRGBColorSpace); - - const box = new THREE.Mesh(boxGeometry, boxMaterial); - box.position.x = Math.floor(Math.random() * 20 - 10) * 20; - box.position.y = Math.floor(Math.random() * 20) * 20 + 10; - box.position.z = Math.floor(Math.random() * 20 - 10) * 20; - - scene.add(box); - objects.push(box); - } - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const time = performance.now(); - - if (controls.isLocked === true) { - raycaster.ray.origin.copy(controls.getObject().position); - raycaster.ray.origin.y -= 10; - - const intersections = raycaster.intersectObjects(objects, false); - - const onObject = intersections.length > 0; - - const delta = (time - prevTime) / 1000; - - velocity.x -= velocity.x * 10.0 * delta; - velocity.z -= velocity.z * 10.0 * delta; - - velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass - - direction.z = Number(moveForward) - Number(moveBackward); - direction.x = Number(moveRight) - Number(moveLeft); - direction.normalize(); // this ensures consistent movements in all directions - - if (moveForward || moveBackward) velocity.z -= direction.z * 400.0 * delta; - if (moveLeft || moveRight) velocity.x -= direction.x * 400.0 * delta; - - if (onObject === true) { - velocity.y = Math.max(0, velocity.y); - canJump = true; - } - - controls.moveRight(-velocity.x * delta); - controls.moveForward(-velocity.z * delta); - - controls.getObject().position.y += velocity.y * delta; // new behavior - - if (controls.getObject().position.y < 10) { - velocity.y = 0; - controls.getObject().position.y = 10; - - canJump = true; - } - } - - prevTime = time; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/misc_controls_trackball.ts b/examples-testing/examples/misc_controls_trackball.ts deleted file mode 100644 index b6479e9f6..000000000 --- a/examples-testing/examples/misc_controls_trackball.ts +++ /dev/null @@ -1,134 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; - -let perspectiveCamera, orthographicCamera, controls, scene, renderer, stats; - -const params = { - orthographicCamera: false, -}; - -const frustumSize = 400; - -init(); - -function init() { - const aspect = window.innerWidth / window.innerHeight; - - perspectiveCamera = new THREE.PerspectiveCamera(60, aspect, 1, 1000); - perspectiveCamera.position.z = 500; - - orthographicCamera = new THREE.OrthographicCamera( - (frustumSize * aspect) / -2, - (frustumSize * aspect) / 2, - frustumSize / 2, - frustumSize / -2, - 1, - 1000, - ); - orthographicCamera.position.z = 500; - - // world - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xcccccc); - scene.fog = new THREE.FogExp2(0xcccccc, 0.002); - - const geometry = new THREE.ConeGeometry(10, 30, 4, 1); - const material = new THREE.MeshPhongMaterial({ color: 0xffffff, flatShading: true }); - - for (let i = 0; i < 500; i++) { - const mesh = new THREE.Mesh(geometry, material); - mesh.position.x = (Math.random() - 0.5) * 1000; - mesh.position.y = (Math.random() - 0.5) * 1000; - mesh.position.z = (Math.random() - 0.5) * 1000; - mesh.updateMatrix(); - mesh.matrixAutoUpdate = false; - scene.add(mesh); - } - - // lights - - const dirLight1 = new THREE.DirectionalLight(0xffffff, 3); - dirLight1.position.set(1, 1, 1); - scene.add(dirLight1); - - const dirLight2 = new THREE.DirectionalLight(0x002288, 3); - dirLight2.position.set(-1, -1, -1); - scene.add(dirLight2); - - const ambientLight = new THREE.AmbientLight(0x555555); - scene.add(ambientLight); - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - const gui = new GUI(); - gui.add(params, 'orthographicCamera') - .name('use orthographic') - .onChange(function (value) { - controls.dispose(); - - createControls(value ? orthographicCamera : perspectiveCamera); - }); - - // - - window.addEventListener('resize', onWindowResize); - - createControls(perspectiveCamera); -} - -function createControls(camera) { - controls = new TrackballControls(camera, renderer.domElement); - - controls.rotateSpeed = 1.0; - controls.zoomSpeed = 1.2; - controls.panSpeed = 0.8; - - controls.keys = ['KeyA', 'KeyS', 'KeyD']; -} - -function onWindowResize() { - const aspect = window.innerWidth / window.innerHeight; - - perspectiveCamera.aspect = aspect; - perspectiveCamera.updateProjectionMatrix(); - - orthographicCamera.left = (-frustumSize * aspect) / 2; - orthographicCamera.right = (frustumSize * aspect) / 2; - orthographicCamera.top = frustumSize / 2; - orthographicCamera.bottom = -frustumSize / 2; - orthographicCamera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - controls.handleResize(); -} - -function animate() { - controls.update(); - - render(); - - stats.update(); -} - -function render() { - const camera = params.orthographicCamera ? orthographicCamera : perspectiveCamera; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/misc_controls_transform.ts b/examples-testing/examples/misc_controls_transform.ts deleted file mode 100644 index 9d14bf7ee..000000000 --- a/examples-testing/examples/misc_controls_transform.ts +++ /dev/null @@ -1,181 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { TransformControls } from 'three/addons/controls/TransformControls.js'; - -let cameraPersp, cameraOrtho, currentCamera; -let scene, renderer, control, orbit; - -init(); -render(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - const aspect = window.innerWidth / window.innerHeight; - - const frustumSize = 5; - - cameraPersp = new THREE.PerspectiveCamera(50, aspect, 0.1, 100); - cameraOrtho = new THREE.OrthographicCamera( - -frustumSize * aspect, - frustumSize * aspect, - frustumSize, - -frustumSize, - 0.1, - 100, - ); - currentCamera = cameraPersp; - - currentCamera.position.set(5, 2.5, 5); - - scene = new THREE.Scene(); - scene.add(new THREE.GridHelper(5, 10, 0x888888, 0x444444)); - - const ambientLight = new THREE.AmbientLight(0xffffff); - scene.add(ambientLight); - - const light = new THREE.DirectionalLight(0xffffff, 4); - light.position.set(1, 1, 1); - scene.add(light); - - const texture = new THREE.TextureLoader().load('textures/crate.gif', render); - texture.colorSpace = THREE.SRGBColorSpace; - texture.anisotropy = renderer.capabilities.getMaxAnisotropy(); - - const geometry = new THREE.BoxGeometry(); - const material = new THREE.MeshLambertMaterial({ map: texture }); - - orbit = new OrbitControls(currentCamera, renderer.domElement); - orbit.update(); - orbit.addEventListener('change', render); - - control = new TransformControls(currentCamera, renderer.domElement); - control.addEventListener('change', render); - - control.addEventListener('dragging-changed', function (event) { - orbit.enabled = !event.value; - }); - - const mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - control.attach(mesh); - scene.add(control); - - window.addEventListener('resize', onWindowResize); - - window.addEventListener('keydown', function (event) { - switch (event.key) { - case 'q': - control.setSpace(control.space === 'local' ? 'world' : 'local'); - break; - - case 'Shift': - control.setTranslationSnap(1); - control.setRotationSnap(THREE.MathUtils.degToRad(15)); - control.setScaleSnap(0.25); - break; - - case 'w': - control.setMode('translate'); - break; - - case 'e': - control.setMode('rotate'); - break; - - case 'r': - control.setMode('scale'); - break; - - case 'c': - const position = currentCamera.position.clone(); - - currentCamera = currentCamera.isPerspectiveCamera ? cameraOrtho : cameraPersp; - currentCamera.position.copy(position); - - orbit.object = currentCamera; - control.camera = currentCamera; - - currentCamera.lookAt(orbit.target.x, orbit.target.y, orbit.target.z); - onWindowResize(); - break; - - case 'v': - const randomFoV = Math.random() + 0.1; - const randomZoom = Math.random() + 0.1; - - cameraPersp.fov = randomFoV * 160; - cameraOrtho.bottom = -randomFoV * 500; - cameraOrtho.top = randomFoV * 500; - - cameraPersp.zoom = randomZoom * 5; - cameraOrtho.zoom = randomZoom * 5; - onWindowResize(); - break; - - case '+': - case '=': - control.setSize(control.size + 0.1); - break; - - case '-': - case '_': - control.setSize(Math.max(control.size - 0.1, 0.1)); - break; - - case 'x': - control.showX = !control.showX; - break; - - case 'y': - control.showY = !control.showY; - break; - - case 'z': - control.showZ = !control.showZ; - break; - - case ' ': - control.enabled = !control.enabled; - break; - - case 'Escape': - control.reset(); - break; - } - }); - - window.addEventListener('keyup', function (event) { - switch (event.key) { - case 'Shift': - control.setTranslationSnap(null); - control.setRotationSnap(null); - control.setScaleSnap(null); - break; - } - }); -} - -function onWindowResize() { - const aspect = window.innerWidth / window.innerHeight; - - cameraPersp.aspect = aspect; - cameraPersp.updateProjectionMatrix(); - - cameraOrtho.left = cameraOrtho.bottom * aspect; - cameraOrtho.right = cameraOrtho.top * aspect; - cameraOrtho.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, currentCamera); -} diff --git a/examples-testing/examples/misc_exporter_draco.ts b/examples-testing/examples/misc_exporter_draco.ts deleted file mode 100644 index 40a62fb18..000000000 --- a/examples-testing/examples/misc_exporter_draco.ts +++ /dev/null @@ -1,117 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { DRACOExporter } from 'three/addons/exporters/DRACOExporter.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let scene, camera, renderer, exporter, mesh; - -const params = { - export: exportFile, -}; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(4, 2, 4); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xa0a0a0); - scene.fog = new THREE.Fog(0xa0a0a0, 4, 20); - - exporter = new DRACOExporter(); - - // - - const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 3); - hemiLight.position.set(0, 20, 0); - scene.add(hemiLight); - - const directionalLight = new THREE.DirectionalLight(0xffffff, 3); - directionalLight.position.set(0, 20, 10); - directionalLight.castShadow = true; - directionalLight.shadow.camera.top = 2; - directionalLight.shadow.camera.bottom = -2; - directionalLight.shadow.camera.left = -2; - directionalLight.shadow.camera.right = 2; - scene.add(directionalLight); - - // ground - - const ground = new THREE.Mesh( - new THREE.PlaneGeometry(40, 40), - new THREE.MeshPhongMaterial({ color: 0xbbbbbb, depthWrite: false }), - ); - ground.rotation.x = -Math.PI / 2; - ground.receiveShadow = true; - scene.add(ground); - - const grid = new THREE.GridHelper(40, 20, 0x000000, 0x000000); - grid.material.opacity = 0.2; - grid.material.transparent = true; - scene.add(grid); - - // export mesh - - const geometry = new THREE.TorusKnotGeometry(0.75, 0.2, 200, 30); - const material = new THREE.MeshPhongMaterial({ color: 0x00ff00 }); - mesh = new THREE.Mesh(geometry, material); - mesh.castShadow = true; - mesh.position.y = 1.5; - scene.add(mesh); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - document.body.appendChild(renderer.domElement); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 1.5, 0); - controls.update(); - - // - - window.addEventListener('resize', onWindowResize); - - const gui = new GUI(); - - gui.add(params, 'export').name('Export DRC'); - gui.open(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} - -function exportFile() { - const result = exporter.parse(mesh); - saveArrayBuffer(result, 'file.drc'); -} - -const link = document.createElement('a'); -link.style.display = 'none'; -document.body.appendChild(link); - -function save(blob, filename) { - link.href = URL.createObjectURL(blob); - link.download = filename; - link.click(); -} - -function saveArrayBuffer(buffer, filename) { - save(new Blob([buffer], { type: 'application/octet-stream' }), filename); -} diff --git a/examples-testing/examples/misc_exporter_exr.ts b/examples-testing/examples/misc_exporter_exr.ts deleted file mode 100644 index c239a65fa..000000000 --- a/examples-testing/examples/misc_exporter_exr.ts +++ /dev/null @@ -1,158 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { EXRExporter, ZIP_COMPRESSION, ZIPS_COMPRESSION, NO_COMPRESSION } from 'three/addons/exporters/EXRExporter.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let scene, camera, renderer, exporter, mesh, controls, renderTarget, dataTexture; - -const params = { - target: 'pmrem', - type: 'HalfFloatType', - compression: 'ZIP', - export: exportFile, -}; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(10, 0, 0); - - scene = new THREE.Scene(); - - exporter = new EXRExporter(); - const rgbeloader = new RGBELoader(); - - // - - const pmremGenerator = new THREE.PMREMGenerator(renderer); - pmremGenerator.compileEquirectangularShader(); - - rgbeloader.load('textures/equirectangular/san_giuseppe_bridge_2k.hdr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - - renderTarget = pmremGenerator.fromEquirectangular(texture); - scene.background = renderTarget.texture; - }); - - createDataTexture(); - - // - - controls = new OrbitControls(camera, renderer.domElement); - controls.enableDamping = true; - controls.rotateSpeed = -0.25; // negative, to track mouse pointer - - // - - window.addEventListener('resize', onWindowResize); - - const gui = new GUI(); - - const input = gui.addFolder('Input'); - input.add(params, 'target').options(['pmrem', 'data-texture']).onChange(swapScene); - - const options = gui.addFolder('Output Options'); - options.add(params, 'type').options(['FloatType', 'HalfFloatType']); - options.add(params, 'compression').options(['ZIP', 'ZIPS', 'NONE']); - - gui.add(params, 'export').name('Export EXR'); - gui.open(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - controls.update(); - renderer.render(scene, camera); -} - -function createDataTexture() { - const normal = new THREE.Vector3(); - const coord = new THREE.Vector2(); - const size = 800, - radius = 320, - factor = (Math.PI * 0.5) / radius; - const data = new Float32Array(4 * size * size); - - for (let i = 0; i < size; i++) { - for (let j = 0; j < size; j++) { - const idx = i * size * 4 + j * 4; - coord.set(j, i).subScalar(size / 2); - - if (coord.length() < radius) - normal.set(Math.sin(coord.x * factor), Math.sin(coord.y * factor), Math.cos(coord.x * factor)); - else normal.set(0, 0, 1); - - data[idx + 0] = 0.5 + 0.5 * normal.x; - data[idx + 1] = 0.5 + 0.5 * normal.y; - data[idx + 2] = 0.5 + 0.5 * normal.z; - data[idx + 3] = 1; - } - } - - dataTexture = new THREE.DataTexture(data, size, size, THREE.RGBAFormat, THREE.FloatType); - dataTexture.needsUpdate = true; - - const material = new THREE.MeshBasicMaterial({ map: dataTexture }); - const quad = new THREE.PlaneGeometry(50, 50); - mesh = new THREE.Mesh(quad, material); - mesh.visible = false; - - scene.add(mesh); -} - -function swapScene() { - if (params.target == 'pmrem') { - camera.position.set(10, 0, 0); - controls.enabled = true; - scene.background = renderTarget.texture; - mesh.visible = false; - } else { - camera.position.set(0, 0, 70); - controls.enabled = false; - scene.background = new THREE.Color(0, 0, 0); - mesh.visible = true; - } -} - -function exportFile() { - let result, exportType, exportCompression; - - if (params.type == 'HalfFloatType') exportType = THREE.HalfFloatType; - else exportType = THREE.FloatType; - - if (params.compression == 'ZIP') exportCompression = ZIP_COMPRESSION; - else if (params.compression == 'ZIPS') exportCompression = ZIPS_COMPRESSION; - else exportCompression = NO_COMPRESSION; - - if (params.target == 'pmrem') - result = exporter.parse(renderer, renderTarget, { type: exportType, compression: exportCompression }); - else result = exporter.parse(dataTexture, { type: exportType, compression: exportCompression }); - - saveArrayBuffer(result, params.target + '.exr'); -} - -function saveArrayBuffer(buffer, filename) { - const blob = new Blob([buffer], { type: 'image/x-exr' }); - const link = document.createElement('a'); - - link.href = URL.createObjectURL(blob); - link.download = filename; - link.click(); -} diff --git a/examples-testing/examples/misc_exporter_gltf.ts b/examples-testing/examples/misc_exporter_gltf.ts deleted file mode 100644 index e4172b852..000000000 --- a/examples-testing/examples/misc_exporter_gltf.ts +++ /dev/null @@ -1,507 +0,0 @@ -import * as THREE from 'three'; - -import { GLTFExporter } from 'three/addons/exporters/GLTFExporter.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js'; -import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -function exportGLTF(input) { - const gltfExporter = new GLTFExporter(); - - const options = { - trs: params.trs, - onlyVisible: params.onlyVisible, - binary: params.binary, - maxTextureSize: params.maxTextureSize, - }; - gltfExporter.parse( - input, - function (result) { - if (result instanceof ArrayBuffer) { - saveArrayBuffer(result, 'scene.glb'); - } else { - const output = JSON.stringify(result, null, 2); - console.log(output); - saveString(output, 'scene.gltf'); - } - }, - function (error) { - console.log('An error happened during parsing', error); - }, - options, - ); -} - -const link = document.createElement('a'); -link.style.display = 'none'; -document.body.appendChild(link); // Firefox workaround, see #6594 - -function save(blob, filename) { - link.href = URL.createObjectURL(blob); - link.download = filename; - link.click(); - - // URL.revokeObjectURL( url ); breaks Firefox... -} - -function saveString(text, filename) { - save(new Blob([text], { type: 'text/plain' }), filename); -} - -function saveArrayBuffer(buffer, filename) { - save(new Blob([buffer], { type: 'application/octet-stream' }), filename); -} - -let container; - -let camera, object, object2, material, geometry, scene1, scene2, renderer; -let gridHelper, sphere, model, coffeemat; - -const params = { - trs: false, - onlyVisible: true, - binary: false, - maxTextureSize: 4096, - exportScene1: exportScene1, - exportScenes: exportScenes, - exportSphere: exportSphere, - exportModel: exportModel, - exportObjects: exportObjects, - exportSceneObject: exportSceneObject, - exportCompressedObject: exportCompressedObject, -}; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - // Make linear gradient texture - - const data = new Uint8ClampedArray(100 * 100 * 4); - - for (let y = 0; y < 100; y++) { - for (let x = 0; x < 100; x++) { - const stride = 4 * (100 * y + x); - - data[stride] = Math.round((255 * y) / 99); - data[stride + 1] = Math.round(255 - (255 * y) / 99); - data[stride + 2] = 0; - data[stride + 3] = 255; - } - } - - const gradientTexture = new THREE.DataTexture(data, 100, 100, THREE.RGBAFormat); - gradientTexture.minFilter = THREE.LinearFilter; - gradientTexture.magFilter = THREE.LinearFilter; - gradientTexture.needsUpdate = true; - - scene1 = new THREE.Scene(); - scene1.name = 'Scene1'; - - // --------------------------------------------------------------------- - // Perspective Camera - // --------------------------------------------------------------------- - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000); - camera.position.set(600, 400, 0); - - camera.name = 'PerspectiveCamera'; - scene1.add(camera); - - // --------------------------------------------------------------------- - // Ambient light - // --------------------------------------------------------------------- - const ambientLight = new THREE.AmbientLight(0xcccccc); - ambientLight.name = 'AmbientLight'; - scene1.add(ambientLight); - - // --------------------------------------------------------------------- - // DirectLight - // --------------------------------------------------------------------- - const dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.target.position.set(0, 0, -1); - dirLight.add(dirLight.target); - dirLight.lookAt(-1, -1, 0); - dirLight.name = 'DirectionalLight'; - scene1.add(dirLight); - - // --------------------------------------------------------------------- - // Grid - // --------------------------------------------------------------------- - gridHelper = new THREE.GridHelper(2000, 20, 0xc1c1c1, 0x8d8d8d); - gridHelper.position.y = -50; - gridHelper.name = 'Grid'; - scene1.add(gridHelper); - - // --------------------------------------------------------------------- - // Axes - // --------------------------------------------------------------------- - const axes = new THREE.AxesHelper(500); - axes.name = 'AxesHelper'; - scene1.add(axes); - - // --------------------------------------------------------------------- - // Simple geometry with basic material - // --------------------------------------------------------------------- - // Icosahedron - const mapGrid = new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg'); - mapGrid.wrapS = mapGrid.wrapT = THREE.RepeatWrapping; - mapGrid.colorSpace = THREE.SRGBColorSpace; - material = new THREE.MeshBasicMaterial({ - color: 0xffffff, - map: mapGrid, - }); - - object = new THREE.Mesh(new THREE.IcosahedronGeometry(75, 0), material); - object.position.set(-200, 0, 200); - object.name = 'Icosahedron'; - scene1.add(object); - - // Octahedron - material = new THREE.MeshBasicMaterial({ - color: 0x0000ff, - wireframe: true, - }); - object = new THREE.Mesh(new THREE.OctahedronGeometry(75, 1), material); - object.position.set(0, 0, 200); - object.name = 'Octahedron'; - scene1.add(object); - - // Tetrahedron - material = new THREE.MeshBasicMaterial({ - color: 0xff0000, - transparent: true, - opacity: 0.5, - }); - - object = new THREE.Mesh(new THREE.TetrahedronGeometry(75, 0), material); - object.position.set(200, 0, 200); - object.name = 'Tetrahedron'; - scene1.add(object); - - // --------------------------------------------------------------------- - // Buffered geometry primitives - // --------------------------------------------------------------------- - // Sphere - material = new THREE.MeshStandardMaterial({ - color: 0xffff00, - metalness: 0.5, - roughness: 1.0, - flatShading: true, - }); - material.map = gradientTexture; - material.bumpMap = mapGrid; - sphere = new THREE.Mesh(new THREE.SphereGeometry(70, 10, 10), material); - sphere.position.set(0, 0, 0); - sphere.name = 'Sphere'; - scene1.add(sphere); - - // Cylinder - material = new THREE.MeshStandardMaterial({ - color: 0xff00ff, - flatShading: true, - }); - object = new THREE.Mesh(new THREE.CylinderGeometry(10, 80, 100), material); - object.position.set(200, 0, 0); - object.name = 'Cylinder'; - scene1.add(object); - - // TorusKnot - material = new THREE.MeshStandardMaterial({ - color: 0xff0000, - roughness: 1, - }); - object = new THREE.Mesh(new THREE.TorusKnotGeometry(50, 15, 40, 10), material); - object.position.set(-200, 0, 0); - object.name = 'Cylinder'; - scene1.add(object); - - // --------------------------------------------------------------------- - // Hierarchy - // --------------------------------------------------------------------- - const mapWood = new THREE.TextureLoader().load('textures/hardwood2_diffuse.jpg'); - material = new THREE.MeshStandardMaterial({ map: mapWood, side: THREE.DoubleSide }); - - object = new THREE.Mesh(new THREE.BoxGeometry(40, 100, 100), material); - object.position.set(-200, 0, 400); - object.name = 'Cube'; - scene1.add(object); - - object2 = new THREE.Mesh(new THREE.BoxGeometry(40, 40, 40, 2, 2, 2), material); - object2.position.set(0, 0, 50); - object2.rotation.set(0, 45, 0); - object2.name = 'SubCube'; - object.add(object2); - - // --------------------------------------------------------------------- - // Groups - // --------------------------------------------------------------------- - const group1 = new THREE.Group(); - group1.name = 'Group'; - scene1.add(group1); - - const group2 = new THREE.Group(); - group2.name = 'subGroup'; - group2.position.set(0, 50, 0); - group1.add(group2); - - object2 = new THREE.Mesh(new THREE.BoxGeometry(30, 30, 30), material); - object2.name = 'Cube in group'; - object2.position.set(0, 0, 400); - group2.add(object2); - - // --------------------------------------------------------------------- - // THREE.Line Strip - // --------------------------------------------------------------------- - geometry = new THREE.BufferGeometry(); - let numPoints = 100; - let positions = new Float32Array(numPoints * 3); - - for (let i = 0; i < numPoints; i++) { - positions[i * 3] = i; - positions[i * 3 + 1] = Math.sin(i / 2) * 20; - positions[i * 3 + 2] = 0; - } - - geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); - object = new THREE.Line(geometry, new THREE.LineBasicMaterial({ color: 0xffff00 })); - object.position.set(-50, 0, -200); - scene1.add(object); - - // --------------------------------------------------------------------- - // THREE.Line Loop - // --------------------------------------------------------------------- - geometry = new THREE.BufferGeometry(); - numPoints = 5; - const radius = 70; - positions = new Float32Array(numPoints * 3); - - for (let i = 0; i < numPoints; i++) { - const s = (i * Math.PI * 2) / numPoints; - positions[i * 3] = radius * Math.sin(s); - positions[i * 3 + 1] = radius * Math.cos(s); - positions[i * 3 + 2] = 0; - } - - geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); - object = new THREE.LineLoop(geometry, new THREE.LineBasicMaterial({ color: 0xffff00 })); - object.position.set(0, 0, -200); - - scene1.add(object); - - // --------------------------------------------------------------------- - // THREE.Points - // --------------------------------------------------------------------- - numPoints = 100; - const pointsArray = new Float32Array(numPoints * 3); - for (let i = 0; i < numPoints; i++) { - pointsArray[3 * i] = -50 + Math.random() * 100; - pointsArray[3 * i + 1] = Math.random() * 100; - pointsArray[3 * i + 2] = -50 + Math.random() * 100; - } - - const pointsGeo = new THREE.BufferGeometry(); - pointsGeo.setAttribute('position', new THREE.BufferAttribute(pointsArray, 3)); - - const pointsMaterial = new THREE.PointsMaterial({ color: 0xffff00, size: 5 }); - const pointCloud = new THREE.Points(pointsGeo, pointsMaterial); - pointCloud.name = 'Points'; - pointCloud.position.set(-200, 0, -200); - scene1.add(pointCloud); - - // --------------------------------------------------------------------- - // Ortho camera - // --------------------------------------------------------------------- - const cameraOrtho = new THREE.OrthographicCamera( - window.innerWidth / -2, - window.innerWidth / 2, - window.innerHeight / 2, - window.innerHeight / -2, - 0.1, - 10, - ); - scene1.add(cameraOrtho); - cameraOrtho.name = 'OrthographicCamera'; - - material = new THREE.MeshLambertMaterial({ - color: 0xffff00, - side: THREE.DoubleSide, - }); - - object = new THREE.Mesh(new THREE.CircleGeometry(50, 20, 0, Math.PI * 2), material); - object.position.set(200, 0, -400); - scene1.add(object); - - object = new THREE.Mesh(new THREE.RingGeometry(10, 50, 20, 5, 0, Math.PI * 2), material); - object.position.set(0, 0, -400); - scene1.add(object); - - object = new THREE.Mesh(new THREE.CylinderGeometry(25, 75, 100, 40, 5), material); - object.position.set(-200, 0, -400); - scene1.add(object); - - // - const points = []; - - for (let i = 0; i < 50; i++) { - points.push(new THREE.Vector2(Math.sin(i * 0.2) * Math.sin(i * 0.1) * 15 + 50, (i - 5) * 2)); - } - - object = new THREE.Mesh(new THREE.LatheGeometry(points, 20), material); - object.position.set(200, 0, 400); - scene1.add(object); - - // --------------------------------------------------------------------- - // Big red box hidden just for testing `onlyVisible` option - // --------------------------------------------------------------------- - material = new THREE.MeshBasicMaterial({ - color: 0xff0000, - }); - object = new THREE.Mesh(new THREE.BoxGeometry(200, 200, 200), material); - object.position.set(0, 0, 0); - object.name = 'CubeHidden'; - object.visible = false; - scene1.add(object); - - // --------------------------------------------------------------------- - // Model requiring KHR_mesh_quantization - // --------------------------------------------------------------------- - const loader = new GLTFLoader(); - loader.load('models/gltf/ShaderBall.glb', function (gltf) { - model = gltf.scene; - model.scale.setScalar(50); - model.position.set(200, -40, -200); - scene1.add(model); - }); - - // --------------------------------------------------------------------- - // Model requiring KHR_mesh_quantization - // --------------------------------------------------------------------- - - material = new THREE.MeshBasicMaterial({ - color: 0xffffff, - }); - object = new THREE.InstancedMesh(new THREE.BoxGeometry(10, 10, 10, 2, 2, 2), material, 50); - const matrix = new THREE.Matrix4(); - const color = new THREE.Color(); - for (let i = 0; i < 50; i++) { - matrix.setPosition(Math.random() * 100 - 50, Math.random() * 100 - 50, Math.random() * 100 - 50); - object.setMatrixAt(i, matrix); - object.setColorAt(i, color.setHSL(i / 50, 1, 0.5)); - } - - object.position.set(400, 0, 200); - scene1.add(object); - - // --------------------------------------------------------------------- - // 2nd THREE.Scene - // --------------------------------------------------------------------- - scene2 = new THREE.Scene(); - object = new THREE.Mesh(new THREE.BoxGeometry(100, 100, 100), material); - object.position.set(0, 0, 0); - object.name = 'Cube2ndScene'; - scene2.name = 'Scene2'; - scene2.add(object); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1; - - container.appendChild(renderer.domElement); - - // - - window.addEventListener('resize', onWindowResize); - - // --------------------------------------------------------------------- - // Exporting compressed textures and meshes (KTX2 / Draco / Meshopt) - // --------------------------------------------------------------------- - const ktx2Loader = new KTX2Loader().setTranscoderPath('jsm/libs/basis/').detectSupport(renderer); - - const gltfLoader = new GLTFLoader().setPath('models/gltf/'); - gltfLoader.setKTX2Loader(ktx2Loader); - gltfLoader.setMeshoptDecoder(MeshoptDecoder); - gltfLoader.load('coffeemat.glb', function (gltf) { - gltf.scene.position.x = 400; - gltf.scene.position.z = -200; - - scene1.add(gltf.scene); - - coffeemat = gltf.scene; - }); - - // - - const gui = new GUI(); - - let h = gui.addFolder('Settings'); - h.add(params, 'trs').name('Use TRS'); - h.add(params, 'onlyVisible').name('Only Visible Objects'); - h.add(params, 'binary').name('Binary (GLB)'); - h.add(params, 'maxTextureSize', 2, 8192).name('Max Texture Size').step(1); - - h = gui.addFolder('Export'); - h.add(params, 'exportScene1').name('Export Scene 1'); - h.add(params, 'exportScenes').name('Export Scene 1 and 2'); - h.add(params, 'exportSphere').name('Export Sphere'); - h.add(params, 'exportModel').name('Export Model'); - h.add(params, 'exportObjects').name('Export Sphere With Grid'); - h.add(params, 'exportSceneObject').name('Export Scene 1 and Object'); - h.add(params, 'exportCompressedObject').name('Export Coffeemat (from compressed data)'); - - gui.open(); -} - -function exportScene1() { - exportGLTF(scene1); -} - -function exportScenes() { - exportGLTF([scene1, scene2]); -} - -function exportSphere() { - exportGLTF(sphere); -} - -function exportModel() { - exportGLTF(model); -} - -function exportObjects() { - exportGLTF([sphere, gridHelper]); -} - -function exportSceneObject() { - exportGLTF([scene1, gridHelper]); -} - -function exportCompressedObject() { - exportGLTF([coffeemat]); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const timer = Date.now() * 0.0001; - - camera.position.x = Math.cos(timer) * 800; - camera.position.z = Math.sin(timer) * 800; - - camera.lookAt(scene1.position); - renderer.render(scene1, camera); -} diff --git a/examples-testing/examples/misc_exporter_obj.ts b/examples-testing/examples/misc_exporter_obj.ts deleted file mode 100644 index 025034daf..000000000 --- a/examples-testing/examples/misc_exporter_obj.ts +++ /dev/null @@ -1,192 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { OBJExporter } from 'three/addons/exporters/OBJExporter.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer; - -const params = { - addTriangle: addTriangle, - addCube: addCube, - addCylinder: addCylinder, - addMultiple: addMultiple, - addTransformed: addTransformed, - addPoints: addPoints, - exportToObj: exportToObj, -}; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 0, 400); - - scene = new THREE.Scene(); - - const ambientLight = new THREE.AmbientLight(0xffffff); - scene.add(ambientLight); - - const directionalLight = new THREE.DirectionalLight(0xffffff, 2.5); - directionalLight.position.set(0, 1, 1); - scene.add(directionalLight); - - const gui = new GUI(); - - let h = gui.addFolder('Geometry Selection'); - h.add(params, 'addTriangle').name('Triangle'); - h.add(params, 'addCube').name('Cube'); - h.add(params, 'addCylinder').name('Cylinder'); - h.add(params, 'addMultiple').name('Multiple objects'); - h.add(params, 'addTransformed').name('Transformed objects'); - h.add(params, 'addPoints').name('Point Cloud'); - - h = gui.addFolder('Export'); - h.add(params, 'exportToObj').name('Export OBJ'); - - gui.open(); - - addGeometry(1); - - window.addEventListener('resize', onWindowResize); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.enablePan = false; -} - -function exportToObj() { - const exporter = new OBJExporter(); - const result = exporter.parse(scene); - saveString(result, 'object.obj'); -} - -function addGeometry(type) { - for (let i = 0; i < scene.children.length; i++) { - const child = scene.children[i]; - - if (child.isMesh || child.isPoints) { - child.geometry.dispose(); - scene.remove(child); - i--; - } - } - - if (type === 1) { - const material = new THREE.MeshLambertMaterial({ color: 0x00cc00 }); - const geometry = generateTriangleGeometry(); - - scene.add(new THREE.Mesh(geometry, material)); - } else if (type === 2) { - const material = new THREE.MeshLambertMaterial({ color: 0x00cc00 }); - const geometry = new THREE.BoxGeometry(100, 100, 100); - scene.add(new THREE.Mesh(geometry, material)); - } else if (type === 3) { - const material = new THREE.MeshLambertMaterial({ color: 0x00cc00 }); - const geometry = new THREE.CylinderGeometry(50, 50, 100, 30, 1); - scene.add(new THREE.Mesh(geometry, material)); - } else if (type === 4 || type === 5) { - const material = new THREE.MeshLambertMaterial({ color: 0x00cc00 }); - const geometry = generateTriangleGeometry(); - - const mesh = new THREE.Mesh(geometry, material); - mesh.position.x = -200; - scene.add(mesh); - - const geometry2 = new THREE.BoxGeometry(100, 100, 100); - const mesh2 = new THREE.Mesh(geometry2, material); - scene.add(mesh2); - - const geometry3 = new THREE.CylinderGeometry(50, 50, 100, 30, 1); - const mesh3 = new THREE.Mesh(geometry3, material); - mesh3.position.x = 200; - scene.add(mesh3); - - if (type === 5) { - mesh.rotation.y = Math.PI / 4.0; - mesh2.rotation.y = Math.PI / 4.0; - mesh3.rotation.y = Math.PI / 4.0; - } - } else if (type === 6) { - const points = [0, 0, 0, 100, 0, 0, 100, 100, 0, 0, 100, 0]; - const colors = [0.5, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0, 0.5, 0]; - - const geometry = new THREE.BufferGeometry(); - geometry.setAttribute('position', new THREE.Float32BufferAttribute(points, 3)); - geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); - - const material = new THREE.PointsMaterial({ size: 10, vertexColors: true }); - - const pointCloud = new THREE.Points(geometry, material); - pointCloud.name = 'point cloud'; - scene.add(pointCloud); - } -} - -function addTriangle() { - addGeometry(1); -} - -function addCube() { - addGeometry(2); -} - -function addCylinder() { - addGeometry(3); -} - -function addMultiple() { - addGeometry(4); -} - -function addTransformed() { - addGeometry(5); -} - -function addPoints() { - addGeometry(6); -} - -const link = document.createElement('a'); -link.style.display = 'none'; -document.body.appendChild(link); - -function save(blob, filename) { - link.href = URL.createObjectURL(blob); - link.download = filename; - link.click(); -} - -function saveString(text, filename) { - save(new Blob([text], { type: 'text/plain' }), filename); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} - -function generateTriangleGeometry() { - const geometry = new THREE.BufferGeometry(); - const vertices = []; - - vertices.push(-50, -50, 0); - vertices.push(50, -50, 0); - vertices.push(50, 50, 0); - - geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); - geometry.computeVertexNormals(); - - return geometry; -} diff --git a/examples-testing/examples/misc_exporter_ply.ts b/examples-testing/examples/misc_exporter_ply.ts deleted file mode 100644 index b7e324688..000000000 --- a/examples-testing/examples/misc_exporter_ply.ts +++ /dev/null @@ -1,156 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { PLYExporter } from 'three/addons/exporters/PLYExporter.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let scene, camera, renderer, exporter, mesh; - -const params = { - exportASCII: exportASCII, - exportBinaryBigEndian: exportBinaryBigEndian, - exportBinaryLittleEndian: exportBinaryLittleEndian, -}; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(4, 2, 4); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xa0a0a0); - scene.fog = new THREE.Fog(0xa0a0a0, 4, 20); - - exporter = new PLYExporter(); - - // - - const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 3); - hemiLight.position.set(0, 20, 0); - scene.add(hemiLight); - - const directionalLight = new THREE.DirectionalLight(0xffffff, 3); - directionalLight.position.set(0, 20, 10); - directionalLight.castShadow = true; - directionalLight.shadow.camera.top = 2; - directionalLight.shadow.camera.bottom = -2; - directionalLight.shadow.camera.left = -2; - directionalLight.shadow.camera.right = 2; - scene.add(directionalLight); - - // ground - - const ground = new THREE.Mesh( - new THREE.PlaneGeometry(40, 40), - new THREE.MeshPhongMaterial({ color: 0xcbcbcb, depthWrite: false }), - ); - ground.rotation.x = -Math.PI / 2; - ground.receiveShadow = true; - scene.add(ground); - - const grid = new THREE.GridHelper(40, 20, 0x000000, 0x000000); - grid.material.opacity = 0.2; - grid.material.transparent = true; - scene.add(grid); - - // export mesh - - const geometry = new THREE.BoxGeometry(); - const material = new THREE.MeshPhongMaterial({ vertexColors: true }); - - // color vertices based on vertex positions - const colors = geometry.getAttribute('position').array.slice(); - for (let i = 0, l = colors.length; i < l; i++) { - if (colors[i] > 0) colors[i] = 0.5; - else colors[i] = 0; - } - - geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3, false)); - - mesh = new THREE.Mesh(geometry, material); - mesh.castShadow = true; - mesh.position.y = 0.5; - scene.add(mesh); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - document.body.appendChild(renderer.domElement); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 0.5, 0); - controls.update(); - - // - - window.addEventListener('resize', onWindowResize); - - const gui = new GUI(); - - gui.add(params, 'exportASCII').name('Export PLY (ASCII)'); - gui.add(params, 'exportBinaryBigEndian').name('Export PLY (Binary BE)'); - gui.add(params, 'exportBinaryLittleEndian').name('Export PLY (Binary LE)'); - gui.open(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} - -function exportASCII() { - exporter.parse(mesh, function (result) { - saveString(result, 'box.ply'); - }); -} - -function exportBinaryBigEndian() { - exporter.parse( - mesh, - function (result) { - saveArrayBuffer(result, 'box.ply'); - }, - { binary: true }, - ); -} - -function exportBinaryLittleEndian() { - exporter.parse( - mesh, - function (result) { - saveArrayBuffer(result, 'box.ply'); - }, - { binary: true, littleEndian: true }, - ); -} - -const link = document.createElement('a'); -link.style.display = 'none'; -document.body.appendChild(link); - -function save(blob, filename) { - link.href = URL.createObjectURL(blob); - link.download = filename; - link.click(); -} - -function saveString(text, filename) { - save(new Blob([text], { type: 'text/plain' }), filename); -} - -function saveArrayBuffer(buffer, filename) { - save(new Blob([buffer], { type: 'application/octet-stream' }), filename); -} diff --git a/examples-testing/examples/misc_exporter_stl.ts b/examples-testing/examples/misc_exporter_stl.ts deleted file mode 100644 index ff6d6e2b5..000000000 --- a/examples-testing/examples/misc_exporter_stl.ts +++ /dev/null @@ -1,129 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { STLExporter } from 'three/addons/exporters/STLExporter.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let scene, camera, renderer, exporter, mesh; - -const params = { - exportASCII: exportASCII, - exportBinary: exportBinary, -}; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(4, 2, 4); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xa0a0a0); - scene.fog = new THREE.Fog(0xa0a0a0, 4, 20); - - exporter = new STLExporter(); - - // - - const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 3); - hemiLight.position.set(0, 20, 0); - scene.add(hemiLight); - - const directionalLight = new THREE.DirectionalLight(0xffffff, 3); - directionalLight.position.set(0, 20, 10); - directionalLight.castShadow = true; - directionalLight.shadow.camera.top = 2; - directionalLight.shadow.camera.bottom = -2; - directionalLight.shadow.camera.left = -2; - directionalLight.shadow.camera.right = 2; - scene.add(directionalLight); - - // ground - - const ground = new THREE.Mesh( - new THREE.PlaneGeometry(40, 40), - new THREE.MeshPhongMaterial({ color: 0xbbbbbb, depthWrite: false }), - ); - ground.rotation.x = -Math.PI / 2; - ground.receiveShadow = true; - scene.add(ground); - - const grid = new THREE.GridHelper(40, 20, 0x000000, 0x000000); - grid.material.opacity = 0.2; - grid.material.transparent = true; - scene.add(grid); - - // export mesh - - const geometry = new THREE.BoxGeometry(); - const material = new THREE.MeshPhongMaterial({ color: 0x00ff00 }); - - mesh = new THREE.Mesh(geometry, material); - mesh.castShadow = true; - mesh.position.y = 0.5; - scene.add(mesh); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - document.body.appendChild(renderer.domElement); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 0.5, 0); - controls.update(); - - // - - window.addEventListener('resize', onWindowResize); - - const gui = new GUI(); - - gui.add(params, 'exportASCII').name('Export STL (ASCII)'); - gui.add(params, 'exportBinary').name('Export STL (Binary)'); - gui.open(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} - -function exportASCII() { - const result = exporter.parse(mesh); - saveString(result, 'box.stl'); -} - -function exportBinary() { - const result = exporter.parse(mesh, { binary: true }); - saveArrayBuffer(result, 'box.stl'); -} - -const link = document.createElement('a'); -link.style.display = 'none'; -document.body.appendChild(link); - -function save(blob, filename) { - link.href = URL.createObjectURL(blob); - link.download = filename; - link.click(); -} - -function saveString(text, filename) { - save(new Blob([text], { type: 'text/plain' }), filename); -} - -function saveArrayBuffer(buffer, filename) { - save(new Blob([buffer], { type: 'application/octet-stream' }), filename); -} diff --git a/examples-testing/examples/misc_exporter_usdz.ts b/examples-testing/examples/misc_exporter_usdz.ts deleted file mode 100644 index 9a14919ba..000000000 --- a/examples-testing/examples/misc_exporter_usdz.ts +++ /dev/null @@ -1,129 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { USDZExporter } from 'three/addons/exporters/USDZExporter.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer; - -const params = { - exportUSDZ: exportUSDZ, -}; - -init(); -render(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - document.body.appendChild(renderer.domElement); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); - camera.position.set(-2.5, 0.6, 3.0); - - const pmremGenerator = new THREE.PMREMGenerator(renderer); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - scene.environment = pmremGenerator.fromScene(new RoomEnvironment(), 0.04).texture; - - const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); - loader.load('DamagedHelmet.gltf', async function (gltf) { - scene.add(gltf.scene); - - const shadowMesh = createSpotShadowMesh(); - shadowMesh.position.y = -1.1; - shadowMesh.position.z = -0.25; - shadowMesh.scale.setScalar(2); - scene.add(shadowMesh); - - render(); - - // USDZ - - const exporter = new USDZExporter(); - const arraybuffer = await exporter.parseAsync(gltf.scene); - const blob = new Blob([arraybuffer], { type: 'application/octet-stream' }); - - const link = document.getElementById('link'); - link.href = URL.createObjectURL(blob); - }); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.minDistance = 2; - controls.maxDistance = 10; - controls.target.set(0, -0.15, -0.2); - controls.update(); - - window.addEventListener('resize', onWindowResize); - - const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent); - - if (isIOS === false) { - const gui = new GUI(); - - gui.add(params, 'exportUSDZ').name('Export USDZ'); - gui.open(); - } -} - -function createSpotShadowMesh() { - const canvas = document.createElement('canvas'); - canvas.width = 128; - canvas.height = 128; - - const context = canvas.getContext('2d'); - const gradient = context.createRadialGradient( - canvas.width / 2, - canvas.height / 2, - 0, - canvas.width / 2, - canvas.height / 2, - canvas.width / 2, - ); - gradient.addColorStop(0.1, 'rgba(130,130,130,1)'); - gradient.addColorStop(1, 'rgba(255,255,255,1)'); - - context.fillStyle = gradient; - context.fillRect(0, 0, canvas.width, canvas.height); - - const shadowTexture = new THREE.CanvasTexture(canvas); - - const geometry = new THREE.PlaneGeometry(); - const material = new THREE.MeshBasicMaterial({ - map: shadowTexture, - blending: THREE.MultiplyBlending, - toneMapped: false, - }); - - const mesh = new THREE.Mesh(geometry, material); - mesh.rotation.x = -Math.PI / 2; - - return mesh; -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function exportUSDZ() { - const link = document.getElementById('link'); - link.click(); -} - -// - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/misc_lookat.ts b/examples-testing/examples/misc_lookat.ts deleted file mode 100644 index 280b6e2d8..000000000 --- a/examples-testing/examples/misc_lookat.ts +++ /dev/null @@ -1,95 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let camera, scene, renderer, stats; - -let sphere; - -let mouseX = 0, - mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -document.addEventListener('mousemove', onDocumentMouseMove); - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 15000); - camera.position.z = 3200; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - - sphere = new THREE.Mesh(new THREE.SphereGeometry(100, 20, 20), new THREE.MeshNormalMaterial()); - scene.add(sphere); - - const geometry = new THREE.CylinderGeometry(0, 10, 100, 12); - geometry.rotateX(Math.PI / 2); - - const material = new THREE.MeshNormalMaterial(); - - for (let i = 0; i < 1000; i++) { - const mesh = new THREE.Mesh(geometry, material); - mesh.position.x = Math.random() * 4000 - 2000; - mesh.position.y = Math.random() * 4000 - 2000; - mesh.position.z = Math.random() * 4000 - 2000; - mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 4 + 2; - scene.add(mesh); - } - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onDocumentMouseMove(event) { - mouseX = (event.clientX - windowHalfX) * 10; - mouseY = (event.clientY - windowHalfY) * 10; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - const time = Date.now() * 0.0005; - - sphere.position.x = Math.sin(time * 0.7) * 2000; - sphere.position.y = Math.cos(time * 0.5) * 2000; - sphere.position.z = Math.cos(time * 0.3) * 2000; - - for (let i = 1, l = scene.children.length; i < l; i++) { - scene.children[i].lookAt(sphere.position); - } - - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-mouseY - camera.position.y) * 0.05; - camera.lookAt(scene.position); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/misc_uv_tests.ts b/examples-testing/examples/misc_uv_tests.ts deleted file mode 100644 index 4f782d45f..000000000 --- a/examples-testing/examples/misc_uv_tests.ts +++ /dev/null @@ -1,44 +0,0 @@ -import * as THREE from 'three'; - -import { UVsDebug } from 'three/addons/utils/UVsDebug.js'; - -/* - * This is to help debug UVs problems in geometry, - * as well as allow a new user to visualize what UVs are about. - */ - -function test(name, geometry) { - const d = document.createElement('div'); - - d.innerHTML = '

' + name + '

'; - - d.appendChild(UVsDebug(geometry)); - - document.body.appendChild(d); -} - -const points = []; - -for (let i = 0; i < 10; i++) { - points.push(new THREE.Vector2(Math.sin(i * 0.2) * 15 + 50, (i - 5) * 2)); -} - -// - -test('new THREE.PlaneGeometry( 100, 100, 4, 4 )', new THREE.PlaneGeometry(100, 100, 4, 4)); - -test('new THREE.SphereGeometry( 75, 12, 6 )', new THREE.SphereGeometry(75, 12, 6)); - -test('new THREE.IcosahedronGeometry( 30, 1 )', new THREE.IcosahedronGeometry(30, 1)); - -test('new THREE.OctahedronGeometry( 30, 2 )', new THREE.OctahedronGeometry(30, 2)); - -test('new THREE.CylinderGeometry( 25, 75, 100, 10, 5 )', new THREE.CylinderGeometry(25, 75, 100, 10, 5)); - -test('new THREE.BoxGeometry( 100, 100, 100, 4, 4, 4 )', new THREE.BoxGeometry(100, 100, 100, 4, 4, 4)); - -test('new THREE.LatheGeometry( points, 8 )', new THREE.LatheGeometry(points, 8)); - -test('new THREE.TorusGeometry( 50, 20, 8, 8 )', new THREE.TorusGeometry(50, 20, 8, 8)); - -test('new THREE.TorusKnotGeometry( 50, 10, 12, 6 )', new THREE.TorusKnotGeometry(50, 10, 12, 6)); diff --git a/examples-testing/examples/physics_ammo_instancing.ts b/examples-testing/examples/physics_ammo_instancing.ts deleted file mode 100644 index 265c254c8..000000000 --- a/examples-testing/examples/physics_ammo_instancing.ts +++ /dev/null @@ -1,119 +0,0 @@ -import * as THREE from 'three'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { AmmoPhysics } from 'three/addons/physics/AmmoPhysics.js'; -import Stats from 'three/addons/libs/stats.module.js'; - -let camera, scene, renderer, stats; -let physics, position; - -let boxes, spheres; - -init(); - -async function init() { - physics = await AmmoPhysics(); - position = new THREE.Vector3(); - - // - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(-1, 1.5, 2); - camera.lookAt(0, 0.5, 0); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x666666); - - const hemiLight = new THREE.HemisphereLight(); - scene.add(hemiLight); - - const dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.position.set(5, 5, 5); - dirLight.castShadow = true; - dirLight.shadow.camera.zoom = 2; - scene.add(dirLight); - - const floor = new THREE.Mesh(new THREE.BoxGeometry(10, 5, 10), new THREE.ShadowMaterial({ color: 0x444444 })); - floor.position.y = -2.5; - floor.receiveShadow = true; - floor.userData.physics = { mass: 0 }; - scene.add(floor); - - // - - const material = new THREE.MeshLambertMaterial(); - - const matrix = new THREE.Matrix4(); - const color = new THREE.Color(); - - // Boxes - - const geometryBox = new THREE.BoxGeometry(0.075, 0.075, 0.075); - boxes = new THREE.InstancedMesh(geometryBox, material, 400); - boxes.instanceMatrix.setUsage(THREE.DynamicDrawUsage); // will be updated every frame - boxes.castShadow = true; - boxes.receiveShadow = true; - boxes.userData.physics = { mass: 1 }; - scene.add(boxes); - - for (let i = 0; i < boxes.count; i++) { - matrix.setPosition(Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5); - boxes.setMatrixAt(i, matrix); - boxes.setColorAt(i, color.setHex(0xffffff * Math.random())); - } - - // Spheres - - const geometrySphere = new THREE.IcosahedronGeometry(0.05, 4); - spheres = new THREE.InstancedMesh(geometrySphere, material, 400); - spheres.instanceMatrix.setUsage(THREE.DynamicDrawUsage); // will be updated every frame - spheres.castShadow = true; - spheres.receiveShadow = true; - spheres.userData.physics = { mass: 1 }; - scene.add(spheres); - - for (let i = 0; i < spheres.count; i++) { - matrix.setPosition(Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5); - spheres.setMatrixAt(i, matrix); - spheres.setColorAt(i, color.setHex(0xffffff * Math.random())); - } - - physics.addScene(scene); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.y = 0.5; - controls.update(); - - setInterval(() => { - let index = Math.floor(Math.random() * boxes.count); - - position.set(0, Math.random() + 1, 0); - physics.setMeshPosition(boxes, position, index); - - // - - index = Math.floor(Math.random() * spheres.count); - - position.set(0, Math.random() + 1, 0); - physics.setMeshPosition(spheres, position, index); - }, 1000 / 60); -} - -function animate() { - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/physics_jolt_instancing.ts b/examples-testing/examples/physics_jolt_instancing.ts deleted file mode 100644 index 022263c0d..000000000 --- a/examples-testing/examples/physics_jolt_instancing.ts +++ /dev/null @@ -1,119 +0,0 @@ -import * as THREE from 'three'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { JoltPhysics } from 'three/addons/physics/JoltPhysics.js'; -import Stats from 'three/addons/libs/stats.module.js'; - -let camera, scene, renderer, stats; -let physics, position; - -let boxes, spheres; - -init(); - -async function init() { - physics = await JoltPhysics(); - position = new THREE.Vector3(); - - // - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(-1, 1.5, 2); - camera.lookAt(0, 0.5, 0); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x666666); - - const hemiLight = new THREE.HemisphereLight(); - scene.add(hemiLight); - - const dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.position.set(5, 5, 5); - dirLight.castShadow = true; - dirLight.shadow.camera.zoom = 2; - scene.add(dirLight); - - const floor = new THREE.Mesh(new THREE.BoxGeometry(10, 5, 10), new THREE.ShadowMaterial({ color: 0x444444 })); - floor.position.y = -2.5; - floor.receiveShadow = true; - floor.userData.physics = { mass: 0 }; - scene.add(floor); - - // - - const material = new THREE.MeshLambertMaterial(); - - const matrix = new THREE.Matrix4(); - const color = new THREE.Color(); - - // Boxes - - const geometryBox = new THREE.BoxGeometry(0.075, 0.075, 0.075); - boxes = new THREE.InstancedMesh(geometryBox, material, 400); - boxes.instanceMatrix.setUsage(THREE.DynamicDrawUsage); // will be updated every frame - boxes.castShadow = true; - boxes.receiveShadow = true; - boxes.userData.physics = { mass: 1 }; - scene.add(boxes); - - for (let i = 0; i < boxes.count; i++) { - matrix.setPosition(Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5); - boxes.setMatrixAt(i, matrix); - boxes.setColorAt(i, color.setHex(0xffffff * Math.random())); - } - - // Spheres - - const geometrySphere = new THREE.IcosahedronGeometry(0.05, 4); - spheres = new THREE.InstancedMesh(geometrySphere, material, 400); - spheres.instanceMatrix.setUsage(THREE.DynamicDrawUsage); // will be updated every frame - spheres.castShadow = true; - spheres.receiveShadow = true; - spheres.userData.physics = { mass: 1 }; - scene.add(spheres); - - for (let i = 0; i < spheres.count; i++) { - matrix.setPosition(Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5); - spheres.setMatrixAt(i, matrix); - spheres.setColorAt(i, color.setHex(0xffffff * Math.random())); - } - - physics.addScene(scene); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.y = 0.5; - controls.update(); - - setInterval(() => { - let index = Math.floor(Math.random() * boxes.count); - - position.set(0, Math.random() + 1, 0); - physics.setMeshPosition(boxes, position, index); - - // - - index = Math.floor(Math.random() * spheres.count); - - position.set(0, Math.random() + 1, 0); - physics.setMeshPosition(spheres, position, index); - }, 1000 / 60); -} - -function animate() { - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/physics_rapier_instancing.ts b/examples-testing/examples/physics_rapier_instancing.ts deleted file mode 100644 index f23cf7667..000000000 --- a/examples-testing/examples/physics_rapier_instancing.ts +++ /dev/null @@ -1,119 +0,0 @@ -import * as THREE from 'three'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { RapierPhysics } from 'three/addons/physics/RapierPhysics.js'; -import Stats from 'three/addons/libs/stats.module.js'; - -let camera, scene, renderer, stats; -let physics, position; - -let boxes, spheres; - -init(); - -async function init() { - physics = await RapierPhysics(); - position = new THREE.Vector3(); - - // - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(-1, 1.5, 2); - camera.lookAt(0, 0.5, 0); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x666666); - - const hemiLight = new THREE.HemisphereLight(); - scene.add(hemiLight); - - const dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.position.set(5, 5, 5); - dirLight.castShadow = true; - dirLight.shadow.camera.zoom = 2; - scene.add(dirLight); - - const floor = new THREE.Mesh(new THREE.BoxGeometry(10, 5, 10), new THREE.ShadowMaterial({ color: 0x444444 })); - floor.position.y = -2.5; - floor.receiveShadow = true; - floor.userData.physics = { mass: 0 }; - scene.add(floor); - - // - - const material = new THREE.MeshLambertMaterial(); - - const matrix = new THREE.Matrix4(); - const color = new THREE.Color(); - - // Boxes - - const geometryBox = new THREE.BoxGeometry(0.075, 0.075, 0.075); - boxes = new THREE.InstancedMesh(geometryBox, material, 400); - boxes.instanceMatrix.setUsage(THREE.DynamicDrawUsage); // will be updated every frame - boxes.castShadow = true; - boxes.receiveShadow = true; - boxes.userData.physics = { mass: 1 }; - scene.add(boxes); - - for (let i = 0; i < boxes.count; i++) { - matrix.setPosition(Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5); - boxes.setMatrixAt(i, matrix); - boxes.setColorAt(i, color.setHex(0xffffff * Math.random())); - } - - // Spheres - - const geometrySphere = new THREE.IcosahedronGeometry(0.05, 4); - spheres = new THREE.InstancedMesh(geometrySphere, material, 400); - spheres.instanceMatrix.setUsage(THREE.DynamicDrawUsage); // will be updated every frame - spheres.castShadow = true; - spheres.receiveShadow = true; - spheres.userData.physics = { mass: 1 }; - scene.add(spheres); - - for (let i = 0; i < spheres.count; i++) { - matrix.setPosition(Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5); - spheres.setMatrixAt(i, matrix); - spheres.setColorAt(i, color.setHex(0xffffff * Math.random())); - } - - physics.addScene(scene); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.y = 0.5; - controls.update(); - - setInterval(() => { - let index = Math.floor(Math.random() * boxes.count); - - position.set(0, Math.random() + 1, 0); - physics.setMeshPosition(boxes, position, index); - - // - - index = Math.floor(Math.random() * spheres.count); - - position.set(0, Math.random() + 1, 0); - physics.setMeshPosition(spheres, position, index); - }, 1000 / 60); -} - -function animate() { - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/svg_lines.ts b/examples-testing/examples/svg_lines.ts deleted file mode 100644 index 99b74c405..000000000 --- a/examples-testing/examples/svg_lines.ts +++ /dev/null @@ -1,87 +0,0 @@ -import * as THREE from 'three'; - -import { SVGRenderer } from 'three/addons/renderers/SVGRenderer.js'; - -THREE.ColorManagement.enabled = false; - -let camera, scene, renderer; - -init(); -animate(); - -function init() { - camera = new THREE.PerspectiveCamera(33, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.z = 10; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0, 0, 0); - - renderer = new SVGRenderer(); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - // - - const vertices = []; - const divisions = 50; - - for (let i = 0; i <= divisions; i++) { - const v = (i / divisions) * (Math.PI * 2); - - const x = Math.sin(v); - const z = Math.cos(v); - - vertices.push(x, 0, z); - } - - const geometry = new THREE.BufferGeometry(); - geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); - - // - - for (let i = 1; i <= 3; i++) { - const material = new THREE.LineBasicMaterial({ - color: Math.random() * 0xffffff, - linewidth: 10, - }); - const line = new THREE.Line(geometry, material); - line.scale.setScalar(i / 3); - scene.add(line); - } - - const material = new THREE.LineDashedMaterial({ - color: 'blue', - linewidth: 1, - dashSize: 10, - gapSize: 10, - }); - const line = new THREE.Line(geometry, material); - line.scale.setScalar(2); - scene.add(line); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - let count = 0; - const time = performance.now() / 1000; - - scene.traverse(function (child) { - child.rotation.x = count + time / 3; - child.rotation.z = count + time / 4; - - count++; - }); - - renderer.render(scene, camera); - requestAnimationFrame(animate); -} diff --git a/examples-testing/examples/svg_sandbox.ts b/examples-testing/examples/svg_sandbox.ts deleted file mode 100644 index e6be8386c..000000000 --- a/examples-testing/examples/svg_sandbox.ts +++ /dev/null @@ -1,212 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { SVGRenderer, SVGObject } from 'three/addons/renderers/SVGRenderer.js'; - -THREE.ColorManagement.enabled = false; - -let camera, scene, renderer, stats; - -let group; - -init(); -animate(); - -function init() { - camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 500; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - // QRCODE - - const loader = new THREE.BufferGeometryLoader(); - loader.load('models/json/QRCode_buffergeometry.json', function (geometry) { - mesh = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ vertexColors: true })); - mesh.scale.x = mesh.scale.y = mesh.scale.z = 2; - scene.add(mesh); - }); - - // CUBES - - const boxGeometry = new THREE.BoxGeometry(100, 100, 100); - - let mesh = new THREE.Mesh( - boxGeometry, - new THREE.MeshBasicMaterial({ color: 0x0000ff, opacity: 0.5, transparent: true }), - ); - mesh.position.x = 500; - mesh.rotation.x = Math.random(); - mesh.rotation.y = Math.random(); - mesh.scale.x = mesh.scale.y = mesh.scale.z = 2; - scene.add(mesh); - - mesh = new THREE.Mesh(boxGeometry, new THREE.MeshBasicMaterial({ color: Math.random() * 0xffffff })); - mesh.position.x = 500; - mesh.position.y = 500; - mesh.rotation.x = Math.random(); - mesh.rotation.y = Math.random(); - mesh.scale.x = mesh.scale.y = mesh.scale.z = 2; - scene.add(mesh); - - // PLANE - - mesh = new THREE.Mesh( - new THREE.PlaneGeometry(100, 100), - new THREE.MeshBasicMaterial({ color: Math.random() * 0xffffff, side: THREE.DoubleSide }), - ); - mesh.position.y = -500; - mesh.scale.x = mesh.scale.y = mesh.scale.z = 2; - scene.add(mesh); - - // CYLINDER - - mesh = new THREE.Mesh( - new THREE.CylinderGeometry(20, 100, 200, 10), - new THREE.MeshBasicMaterial({ color: Math.random() * 0xffffff }), - ); - mesh.position.x = -500; - mesh.rotation.x = -Math.PI / 2; - mesh.scale.x = mesh.scale.y = mesh.scale.z = 2; - scene.add(mesh); - - // POLYFIELD - - const geometry = new THREE.BufferGeometry(); - const material = new THREE.MeshBasicMaterial({ vertexColors: true, side: THREE.DoubleSide }); - - const v = new THREE.Vector3(); - const v0 = new THREE.Vector3(); - const v1 = new THREE.Vector3(); - const v2 = new THREE.Vector3(); - const color = new THREE.Color(); - - const vertices = []; - const colors = []; - - for (let i = 0; i < 100; i++) { - v.set(Math.random() * 1000 - 500, Math.random() * 1000 - 500, Math.random() * 1000 - 500); - - v0.set(Math.random() * 100 - 50, Math.random() * 100 - 50, Math.random() * 100 - 50); - - v1.set(Math.random() * 100 - 50, Math.random() * 100 - 50, Math.random() * 100 - 50); - - v2.set(Math.random() * 100 - 50, Math.random() * 100 - 50, Math.random() * 100 - 50); - - v0.add(v); - v1.add(v); - v2.add(v); - - color.setHex(Math.random() * 0xffffff); - - // create a single triangle - - vertices.push(v0.x, v0.y, v0.z); - vertices.push(v1.x, v1.y, v1.z); - vertices.push(v2.x, v2.y, v2.z); - - colors.push(color.r, color.g, color.b); - colors.push(color.r, color.g, color.b); - colors.push(color.r, color.g, color.b); - } - - geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); - geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); - - group = new THREE.Mesh(geometry, material); - group.scale.set(2, 2, 2); - scene.add(group); - - // SPRITES - - for (let i = 0; i < 50; i++) { - const material = new THREE.SpriteMaterial({ color: Math.random() * 0xffffff }); - const sprite = new THREE.Sprite(material); - sprite.position.x = Math.random() * 1000 - 500; - sprite.position.y = Math.random() * 1000 - 500; - sprite.position.z = Math.random() * 1000 - 500; - sprite.scale.set(64, 64, 1); - scene.add(sprite); - } - - // CUSTOM - - const node = document.createElementNS('http://www.w3.org/2000/svg', 'circle'); - node.setAttribute('stroke', 'black'); - node.setAttribute('fill', 'red'); - node.setAttribute('r', '40'); - - for (let i = 0; i < 50; i++) { - const object = new SVGObject(node.cloneNode()); - object.position.x = Math.random() * 1000 - 500; - object.position.y = Math.random() * 1000 - 500; - object.position.z = Math.random() * 1000 - 500; - scene.add(object); - } - - // CUSTOM FROM FILE - - const fileLoader = new THREE.FileLoader(); - fileLoader.load('models/svg/hexagon.svg', function (svg) { - const node = document.createElementNS('http://www.w3.org/2000/svg', 'g'); - const parser = new DOMParser(); - const doc = parser.parseFromString(svg, 'image/svg+xml'); - - node.appendChild(doc.documentElement); - - const object = new SVGObject(node); - object.position.x = 500; - scene.add(object); - }); - - // LIGHTS - - const ambient = new THREE.AmbientLight(0x80ffff); - scene.add(ambient); - - const directional = new THREE.DirectionalLight(0xffff00); - directional.position.set(-1, 0.5, 0); - scene.add(directional); - - renderer = new SVGRenderer(); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setQuality('low'); - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - requestAnimationFrame(animate); - - render(); - stats.update(); -} - -function render() { - const time = Date.now() * 0.0002; - - camera.position.x = Math.sin(time) * 500; - camera.position.z = Math.cos(time) * 500; - camera.lookAt(scene.position); - - group.rotation.x += 0.01; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webaudio_orientation.ts b/examples-testing/examples/webaudio_orientation.ts deleted file mode 100644 index 7baaa88a0..000000000 --- a/examples-testing/examples/webaudio_orientation.ts +++ /dev/null @@ -1,141 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { PositionalAudioHelper } from 'three/addons/helpers/PositionalAudioHelper.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -let scene, camera, renderer; - -const startButton = document.getElementById('startButton'); -startButton.addEventListener('click', init); - -function init() { - const overlay = document.getElementById('overlay'); - overlay.remove(); - - const container = document.getElementById('container'); - - // - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(3, 2, 3); - - const reflectionCube = new THREE.CubeTextureLoader() - .setPath('textures/cube/SwedishRoyalCastle/') - .load(['px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg']); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xa0a0a0); - scene.fog = new THREE.Fog(0xa0a0a0, 2, 20); - - // - - const hemiLight = new THREE.HemisphereLight(0xffffff, 0x8d8d8d, 3); - hemiLight.position.set(0, 20, 0); - scene.add(hemiLight); - - const dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.position.set(5, 5, 0); - dirLight.castShadow = true; - dirLight.shadow.camera.top = 1; - dirLight.shadow.camera.bottom = -1; - dirLight.shadow.camera.left = -1; - dirLight.shadow.camera.right = 1; - dirLight.shadow.camera.near = 0.1; - dirLight.shadow.camera.far = 20; - scene.add(dirLight); - - // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) ); - - // - - const mesh = new THREE.Mesh( - new THREE.PlaneGeometry(50, 50), - new THREE.MeshPhongMaterial({ color: 0xcbcbcb, depthWrite: false }), - ); - mesh.rotation.x = -Math.PI / 2; - mesh.receiveShadow = true; - scene.add(mesh); - - const grid = new THREE.GridHelper(50, 50, 0xc1c1c1, 0xc1c1c1); - scene.add(grid); - - // - - const listener = new THREE.AudioListener(); - camera.add(listener); - - const audioElement = document.getElementById('music'); - audioElement.play(); - - const positionalAudio = new THREE.PositionalAudio(listener); - positionalAudio.setMediaElementSource(audioElement); - positionalAudio.setRefDistance(1); - positionalAudio.setDirectionalCone(180, 230, 0.1); - - const helper = new PositionalAudioHelper(positionalAudio, 0.1); - positionalAudio.add(helper); - - // - - const gltfLoader = new GLTFLoader(); - gltfLoader.load('models/gltf/BoomBox.glb', function (gltf) { - const boomBox = gltf.scene; - boomBox.position.set(0, 0.2, 0); - boomBox.scale.set(20, 20, 20); - - boomBox.traverse(function (object) { - if (object.isMesh) { - object.material.envMap = reflectionCube; - object.geometry.rotateY(-Math.PI); - object.castShadow = true; - } - }); - - boomBox.add(positionalAudio); - scene.add(boomBox); - - renderer.setAnimationLoop(animate); - }); - - // sound is damped behind this wall - - const wallGeometry = new THREE.BoxGeometry(2, 1, 0.1); - const wallMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000, transparent: true, opacity: 0.5 }); - - const wall = new THREE.Mesh(wallGeometry, wallMaterial); - wall.position.set(0, 0.5, -0.5); - scene.add(wall); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.shadowMap.enabled = true; - container.appendChild(renderer.domElement); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 0.1, 0); - controls.update(); - controls.minDistance = 0.5; - controls.maxDistance = 10; - controls.maxPolarAngle = 0.5 * Math.PI; - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webaudio_sandbox.ts b/examples-testing/examples/webaudio_sandbox.ts deleted file mode 100644 index d67d0d552..000000000 --- a/examples-testing/examples/webaudio_sandbox.ts +++ /dev/null @@ -1,222 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js'; - -let camera, controls, scene, renderer, light; - -let material1, material2, material3; - -let analyser1, analyser2, analyser3; - -const clock = new THREE.Clock(); - -const startButton = document.getElementById('startButton'); -startButton.addEventListener('click', init); - -function init() { - const overlay = document.getElementById('overlay'); - overlay.remove(); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.set(0, 25, 0); - - const listener = new THREE.AudioListener(); - camera.add(listener); - - scene = new THREE.Scene(); - scene.fog = new THREE.FogExp2(0x000000, 0.0025); - - light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0, 0.5, 1).normalize(); - scene.add(light); - - const sphere = new THREE.SphereGeometry(20, 32, 16); - - material1 = new THREE.MeshPhongMaterial({ color: 0xffaa00, flatShading: true, shininess: 0 }); - material2 = new THREE.MeshPhongMaterial({ color: 0xff2200, flatShading: true, shininess: 0 }); - material3 = new THREE.MeshPhongMaterial({ color: 0x6622aa, flatShading: true, shininess: 0 }); - - // sound spheres - - const mesh1 = new THREE.Mesh(sphere, material1); - mesh1.position.set(-250, 30, 0); - scene.add(mesh1); - - const sound1 = new THREE.PositionalAudio(listener); - const songElement = document.getElementById('song'); - sound1.setMediaElementSource(songElement); - sound1.setRefDistance(20); - songElement.play(); - mesh1.add(sound1); - - // - - const mesh2 = new THREE.Mesh(sphere, material2); - mesh2.position.set(250, 30, 0); - scene.add(mesh2); - - const sound2 = new THREE.PositionalAudio(listener); - const skullbeatzElement = document.getElementById('skullbeatz'); - sound2.setMediaElementSource(skullbeatzElement); - sound2.setRefDistance(20); - skullbeatzElement.play(); - mesh2.add(sound2); - - // - - const mesh3 = new THREE.Mesh(sphere, material3); - mesh3.position.set(0, 30, -250); - scene.add(mesh3); - - const sound3 = new THREE.PositionalAudio(listener); - const oscillator = listener.context.createOscillator(); - oscillator.type = 'sine'; - oscillator.frequency.setValueAtTime(144, sound3.context.currentTime); - oscillator.start(0); - sound3.setNodeSource(oscillator); - sound3.setRefDistance(20); - sound3.setVolume(0.5); - mesh3.add(sound3); - - // analysers - - analyser1 = new THREE.AudioAnalyser(sound1, 32); - analyser2 = new THREE.AudioAnalyser(sound2, 32); - analyser3 = new THREE.AudioAnalyser(sound3, 32); - - // global ambient audio - - const sound4 = new THREE.Audio(listener); - const utopiaElement = document.getElementById('utopia'); - sound4.setMediaElementSource(utopiaElement); - sound4.setVolume(0.5); - utopiaElement.play(); - - // ground - - const helper = new THREE.GridHelper(1000, 10, 0x444444, 0x444444); - helper.position.y = 0.1; - scene.add(helper); - - // - - const SoundControls = function () { - this.master = listener.getMasterVolume(); - this.firstSphere = sound1.getVolume(); - this.secondSphere = sound2.getVolume(); - this.thirdSphere = sound3.getVolume(); - this.Ambient = sound4.getVolume(); - }; - - const GeneratorControls = function () { - this.frequency = oscillator.frequency.value; - this.wavetype = oscillator.type; - }; - - const gui = new GUI(); - const soundControls = new SoundControls(); - const generatorControls = new GeneratorControls(); - const volumeFolder = gui.addFolder('sound volume'); - const generatorFolder = gui.addFolder('sound generator'); - - volumeFolder - .add(soundControls, 'master') - .min(0.0) - .max(1.0) - .step(0.01) - .onChange(function () { - listener.setMasterVolume(soundControls.master); - }); - volumeFolder - .add(soundControls, 'firstSphere') - .min(0.0) - .max(1.0) - .step(0.01) - .onChange(function () { - sound1.setVolume(soundControls.firstSphere); - }); - volumeFolder - .add(soundControls, 'secondSphere') - .min(0.0) - .max(1.0) - .step(0.01) - .onChange(function () { - sound2.setVolume(soundControls.secondSphere); - }); - - volumeFolder - .add(soundControls, 'thirdSphere') - .min(0.0) - .max(1.0) - .step(0.01) - .onChange(function () { - sound3.setVolume(soundControls.thirdSphere); - }); - volumeFolder - .add(soundControls, 'Ambient') - .min(0.0) - .max(1.0) - .step(0.01) - .onChange(function () { - sound4.setVolume(soundControls.Ambient); - }); - volumeFolder.open(); - generatorFolder - .add(generatorControls, 'frequency') - .min(50.0) - .max(5000.0) - .step(1.0) - .onChange(function () { - oscillator.frequency.setValueAtTime(generatorControls.frequency, listener.context.currentTime); - }); - generatorFolder - .add(generatorControls, 'wavetype', ['sine', 'square', 'sawtooth', 'triangle']) - .onChange(function () { - oscillator.type = generatorControls.wavetype; - }); - - generatorFolder.open(); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - controls = new FirstPersonControls(camera, renderer.domElement); - - controls.movementSpeed = 70; - controls.lookSpeed = 0.05; - controls.lookVertical = false; - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - controls.handleResize(); -} - -function animate() { - const delta = clock.getDelta(); - - controls.update(delta); - - material1.emissive.b = analyser1.getAverageFrequency() / 256; - material2.emissive.b = analyser2.getAverageFrequency() / 256; - material3.emissive.b = analyser3.getAverageFrequency() / 256; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webaudio_timing.ts b/examples-testing/examples/webaudio_timing.ts deleted file mode 100644 index 9e17bcbcd..000000000 --- a/examples-testing/examples/webaudio_timing.ts +++ /dev/null @@ -1,152 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let scene, camera, renderer, clock; - -const objects = []; - -const speed = 2.5; -const height = 3; -const offset = 0.5; - -const startButton = document.getElementById('startButton'); -startButton.addEventListener('click', init); - -function init() { - const overlay = document.getElementById('overlay'); - overlay.remove(); - - const container = document.getElementById('container'); - - scene = new THREE.Scene(); - - clock = new THREE.Clock(); - - // - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(7, 3, 7); - - // lights - - const ambientLight = new THREE.AmbientLight(0xcccccc); - scene.add(ambientLight); - - const directionalLight = new THREE.DirectionalLight(0xffffff, 2.5); - directionalLight.position.set(0, 5, 5); - scene.add(directionalLight); - - const d = 5; - directionalLight.castShadow = true; - directionalLight.shadow.camera.left = -d; - directionalLight.shadow.camera.right = d; - directionalLight.shadow.camera.top = d; - directionalLight.shadow.camera.bottom = -d; - - directionalLight.shadow.camera.near = 1; - directionalLight.shadow.camera.far = 20; - - directionalLight.shadow.mapSize.x = 1024; - directionalLight.shadow.mapSize.y = 1024; - - // audio - - const audioLoader = new THREE.AudioLoader(); - - const listener = new THREE.AudioListener(); - camera.add(listener); - - // floor - - const floorGeometry = new THREE.PlaneGeometry(10, 10); - const floorMaterial = new THREE.MeshLambertMaterial({ color: 0x4676b6 }); - - const floor = new THREE.Mesh(floorGeometry, floorMaterial); - floor.rotation.x = Math.PI * -0.5; - floor.receiveShadow = true; - scene.add(floor); - - // objects - - const count = 5; - const radius = 3; - - const ballGeometry = new THREE.SphereGeometry(0.3, 32, 16); - ballGeometry.translate(0, 0.3, 0); - const ballMaterial = new THREE.MeshLambertMaterial({ color: 0xcccccc }); - - // create objects when audio buffer is loaded - - audioLoader.load('sounds/ping_pong.mp3', function (buffer) { - for (let i = 0; i < count; i++) { - const s = (i / count) * Math.PI * 2; - - const ball = new THREE.Mesh(ballGeometry, ballMaterial); - ball.castShadow = true; - ball.userData.down = false; - - ball.position.x = radius * Math.cos(s); - ball.position.z = radius * Math.sin(s); - - const audio = new THREE.PositionalAudio(listener); - audio.setBuffer(buffer); - ball.add(audio); - - scene.add(ball); - objects.push(ball); - } - - renderer.setAnimationLoop(animate); - }); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.shadowMap.enabled = true; - container.appendChild(renderer.domElement); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 1; - controls.maxDistance = 25; - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const time = clock.getElapsedTime(); - - for (let i = 0; i < objects.length; i++) { - const ball = objects[i]; - - const previousHeight = ball.position.y; - ball.position.y = Math.abs(Math.sin(i * offset + time * speed) * height); - - if (ball.position.y < previousHeight) { - ball.userData.down = true; - } else { - if (ball.userData.down === true) { - // ball changed direction from down to up - - const audio = ball.children[0]; - audio.play(); // play audio with perfect timing when ball hits the surface - ball.userData.down = false; - } - } - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webaudio_visualizer.ts b/examples-testing/examples/webaudio_visualizer.ts deleted file mode 100644 index a3f58cb36..000000000 --- a/examples-testing/examples/webaudio_visualizer.ts +++ /dev/null @@ -1,86 +0,0 @@ -import * as THREE from 'three'; - -let scene, camera, renderer, analyser, uniforms; - -const startButton = document.getElementById('startButton'); -startButton.addEventListener('click', init); - -function init() { - const fftSize = 128; - - // - - const overlay = document.getElementById('overlay'); - overlay.remove(); - - // - - const container = document.getElementById('container'); - - scene = new THREE.Scene(); - - camera = new THREE.Camera(); - - // - - const listener = new THREE.AudioListener(); - - const audio = new THREE.Audio(listener); - const file = './sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3'; - - if (/(iPad|iPhone|iPod)/g.test(navigator.userAgent)) { - const loader = new THREE.AudioLoader(); - loader.load(file, function (buffer) { - audio.setBuffer(buffer); - audio.play(); - }); - } else { - const mediaElement = new Audio(file); - mediaElement.play(); - - audio.setMediaElementSource(mediaElement); - } - - analyser = new THREE.AudioAnalyser(audio, fftSize); - - // - - uniforms = { - tAudioData: { value: new THREE.DataTexture(analyser.data, fftSize / 2, 1, THREE.RedFormat) }, - }; - - const material = new THREE.ShaderMaterial({ - uniforms: uniforms, - vertexShader: document.getElementById('vertexShader').textContent, - fragmentShader: document.getElementById('fragmentShader').textContent, - }); - - const geometry = new THREE.PlaneGeometry(1, 1); - - const mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - analyser.getFrequencyData(); - - uniforms.tAudioData.value.needsUpdate = true; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_animation_keyframes.ts b/examples-testing/examples/webgl_animation_keyframes.ts deleted file mode 100644 index 88048f24c..000000000 --- a/examples-testing/examples/webgl_animation_keyframes.ts +++ /dev/null @@ -1,80 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; - -let mixer; - -const clock = new THREE.Clock(); -const container = document.getElementById('container'); - -const stats = new Stats(); -container.appendChild(stats.dom); - -const renderer = new THREE.WebGLRenderer({ antialias: true }); -renderer.setPixelRatio(window.devicePixelRatio); -renderer.setSize(window.innerWidth, window.innerHeight); -container.appendChild(renderer.domElement); - -const pmremGenerator = new THREE.PMREMGenerator(renderer); - -const scene = new THREE.Scene(); -scene.background = new THREE.Color(0xbfe3dd); -scene.environment = pmremGenerator.fromScene(new RoomEnvironment(), 0.04).texture; - -const camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 100); -camera.position.set(5, 2, 8); - -const controls = new OrbitControls(camera, renderer.domElement); -controls.target.set(0, 0.5, 0); -controls.update(); -controls.enablePan = false; -controls.enableDamping = true; - -const dracoLoader = new DRACOLoader(); -dracoLoader.setDecoderPath('jsm/libs/draco/gltf/'); - -const loader = new GLTFLoader(); -loader.setDRACOLoader(dracoLoader); -loader.load( - 'models/gltf/LittlestTokyo.glb', - function (gltf) { - const model = gltf.scene; - model.position.set(1, 1, 0); - model.scale.set(0.01, 0.01, 0.01); - scene.add(model); - - mixer = new THREE.AnimationMixer(model); - mixer.clipAction(gltf.animations[0]).play(); - - renderer.setAnimationLoop(animate); - }, - undefined, - function (e) { - console.error(e); - }, -); - -window.onresize = function () { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -}; - -function animate() { - const delta = clock.getDelta(); - - mixer.update(delta); - - controls.update(); - - stats.update(); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_animation_multiple.ts b/examples-testing/examples/webgl_animation_multiple.ts deleted file mode 100644 index 152c65067..000000000 --- a/examples-testing/examples/webgl_animation_multiple.ts +++ /dev/null @@ -1,197 +0,0 @@ -import * as THREE from 'three'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import * as SkeletonUtils from 'three/addons/utils/SkeletonUtils.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, clock; -let model, animations; - -const mixers = [], - objects = []; - -const params = { - sharedSkeleton: false, -}; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(2, 3, -6); - camera.lookAt(0, 1, 0); - - clock = new THREE.Clock(); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xa0a0a0); - scene.fog = new THREE.Fog(0xa0a0a0, 10, 50); - - const hemiLight = new THREE.HemisphereLight(0xffffff, 0x8d8d8d, 3); - hemiLight.position.set(0, 20, 0); - scene.add(hemiLight); - - const dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.position.set(-3, 10, -10); - dirLight.castShadow = true; - dirLight.shadow.camera.top = 4; - dirLight.shadow.camera.bottom = -4; - dirLight.shadow.camera.left = -4; - dirLight.shadow.camera.right = 4; - dirLight.shadow.camera.near = 0.1; - dirLight.shadow.camera.far = 40; - scene.add(dirLight); - - // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) ); - - // ground - - const mesh = new THREE.Mesh( - new THREE.PlaneGeometry(200, 200), - new THREE.MeshPhongMaterial({ color: 0xcbcbcb, depthWrite: false }), - ); - mesh.rotation.x = -Math.PI / 2; - mesh.receiveShadow = true; - scene.add(mesh); - - const loader = new GLTFLoader(); - loader.load('models/gltf/Soldier.glb', function (gltf) { - model = gltf.scene; - animations = gltf.animations; - - model.traverse(function (object) { - if (object.isMesh) object.castShadow = true; - }); - - setupDefaultScene(); - }); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - document.body.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); - - const gui = new GUI(); - - gui.add(params, 'sharedSkeleton').onChange(function () { - clearScene(); - - if (params.sharedSkeleton === true) { - setupSharedSkeletonScene(); - } else { - setupDefaultScene(); - } - }); - gui.open(); -} - -function clearScene() { - for (const mixer of mixers) { - mixer.stopAllAction(); - } - - mixers.length = 0; - - // - - for (const object of objects) { - scene.remove(object); - - scene.traverse(function (child) { - if (child.isSkinnedMesh) child.skeleton.dispose(); - }); - } -} - -function setupDefaultScene() { - // three cloned models with independent skeletons. - // each model can have its own animation state - - const model1 = SkeletonUtils.clone(model); - const model2 = SkeletonUtils.clone(model); - const model3 = SkeletonUtils.clone(model); - - model1.position.x = -2; - model2.position.x = 0; - model3.position.x = 2; - - const mixer1 = new THREE.AnimationMixer(model1); - const mixer2 = new THREE.AnimationMixer(model2); - const mixer3 = new THREE.AnimationMixer(model3); - - mixer1.clipAction(animations[0]).play(); // idle - mixer2.clipAction(animations[1]).play(); // run - mixer3.clipAction(animations[3]).play(); // walk - - scene.add(model1, model2, model3); - - objects.push(model1, model2, model3); - mixers.push(mixer1, mixer2, mixer3); -} - -function setupSharedSkeletonScene() { - // three cloned models with a single shared skeleton. - // all models share the same animation state - - const sharedModel = SkeletonUtils.clone(model); - const shareSkinnedMesh = sharedModel.getObjectByName('vanguard_Mesh'); - const sharedSkeleton = shareSkinnedMesh.skeleton; - const sharedParentBone = sharedModel.getObjectByName('mixamorigHips'); - scene.add(sharedParentBone); // the bones need to be in the scene for the animation to work - - const model1 = shareSkinnedMesh.clone(); - const model2 = shareSkinnedMesh.clone(); - const model3 = shareSkinnedMesh.clone(); - - model1.bindMode = THREE.DetachedBindMode; - model2.bindMode = THREE.DetachedBindMode; - model3.bindMode = THREE.DetachedBindMode; - - const identity = new THREE.Matrix4(); - - model1.bind(sharedSkeleton, identity); - model2.bind(sharedSkeleton, identity); - model3.bind(sharedSkeleton, identity); - - model1.position.x = -2; - model2.position.x = 0; - model3.position.x = 2; - - // apply transformation from the glTF asset - - model1.scale.setScalar(0.01); - model1.rotation.x = -Math.PI * 0.5; - model2.scale.setScalar(0.01); - model2.rotation.x = -Math.PI * 0.5; - model3.scale.setScalar(0.01); - model3.rotation.x = -Math.PI * 0.5; - - // - - const mixer = new THREE.AnimationMixer(sharedParentBone); - mixer.clipAction(animations[1]).play(); - - scene.add(sharedParentBone, model1, model2, model3); - - objects.push(sharedParentBone, model1, model2, model3); - mixers.push(mixer); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const delta = clock.getDelta(); - - for (const mixer of mixers) mixer.update(delta); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_animation_skinning_morph.ts b/examples-testing/examples/webgl_animation_skinning_morph.ts deleted file mode 100644 index f05369aa9..000000000 --- a/examples-testing/examples/webgl_animation_skinning_morph.ts +++ /dev/null @@ -1,187 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -let container, stats, clock, gui, mixer, actions, activeAction, previousAction; -let camera, scene, renderer, model, face; - -const api = { state: 'Walking' }; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 100); - camera.position.set(-5, 3, 10); - camera.lookAt(0, 2, 0); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xe0e0e0); - scene.fog = new THREE.Fog(0xe0e0e0, 20, 100); - - clock = new THREE.Clock(); - - // lights - - const hemiLight = new THREE.HemisphereLight(0xffffff, 0x8d8d8d, 3); - hemiLight.position.set(0, 20, 0); - scene.add(hemiLight); - - const dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.position.set(0, 20, 10); - scene.add(dirLight); - - // ground - - const mesh = new THREE.Mesh( - new THREE.PlaneGeometry(2000, 2000), - new THREE.MeshPhongMaterial({ color: 0xcbcbcb, depthWrite: false }), - ); - mesh.rotation.x = -Math.PI / 2; - scene.add(mesh); - - const grid = new THREE.GridHelper(200, 40, 0x000000, 0x000000); - grid.material.opacity = 0.2; - grid.material.transparent = true; - scene.add(grid); - - // model - - const loader = new GLTFLoader(); - loader.load( - 'models/gltf/RobotExpressive/RobotExpressive.glb', - function (gltf) { - model = gltf.scene; - scene.add(model); - - createGUI(model, gltf.animations); - }, - undefined, - function (e) { - console.error(e); - }, - ); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); - - // stats - stats = new Stats(); - container.appendChild(stats.dom); -} - -function createGUI(model, animations) { - const states = ['Idle', 'Walking', 'Running', 'Dance', 'Death', 'Sitting', 'Standing']; - const emotes = ['Jump', 'Yes', 'No', 'Wave', 'Punch', 'ThumbsUp']; - - gui = new GUI(); - - mixer = new THREE.AnimationMixer(model); - - actions = {}; - - for (let i = 0; i < animations.length; i++) { - const clip = animations[i]; - const action = mixer.clipAction(clip); - actions[clip.name] = action; - - if (emotes.indexOf(clip.name) >= 0 || states.indexOf(clip.name) >= 4) { - action.clampWhenFinished = true; - action.loop = THREE.LoopOnce; - } - } - - // states - - const statesFolder = gui.addFolder('States'); - - const clipCtrl = statesFolder.add(api, 'state').options(states); - - clipCtrl.onChange(function () { - fadeToAction(api.state, 0.5); - }); - - statesFolder.open(); - - // emotes - - const emoteFolder = gui.addFolder('Emotes'); - - function createEmoteCallback(name) { - api[name] = function () { - fadeToAction(name, 0.2); - - mixer.addEventListener('finished', restoreState); - }; - - emoteFolder.add(api, name); - } - - function restoreState() { - mixer.removeEventListener('finished', restoreState); - - fadeToAction(api.state, 0.2); - } - - for (let i = 0; i < emotes.length; i++) { - createEmoteCallback(emotes[i]); - } - - emoteFolder.open(); - - // expressions - - face = model.getObjectByName('Head_4'); - - const expressions = Object.keys(face.morphTargetDictionary); - const expressionFolder = gui.addFolder('Expressions'); - - for (let i = 0; i < expressions.length; i++) { - expressionFolder.add(face.morphTargetInfluences, i, 0, 1, 0.01).name(expressions[i]); - } - - activeAction = actions['Walking']; - activeAction.play(); - - expressionFolder.open(); -} - -function fadeToAction(name, duration) { - previousAction = activeAction; - activeAction = actions[name]; - - if (previousAction !== activeAction) { - previousAction.fadeOut(duration); - } - - activeAction.reset().setEffectiveTimeScale(1).setEffectiveWeight(1).fadeIn(duration).play(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const dt = clock.getDelta(); - - if (mixer) mixer.update(dt); - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_buffergeometry.ts b/examples-testing/examples/webgl_buffergeometry.ts deleted file mode 100644 index 28b2c96a4..000000000 --- a/examples-testing/examples/webgl_buffergeometry.ts +++ /dev/null @@ -1,178 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; - -let camera, scene, renderer; - -let mesh; - -init(); -animate(); - -function init() { - container = document.getElementById('container'); - - // - - camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 3500); - camera.position.z = 2750; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x050505); - scene.fog = new THREE.Fog(0x050505, 2000, 3500); - - // - - scene.add(new THREE.AmbientLight(0xcccccc)); - - const light1 = new THREE.DirectionalLight(0xffffff, 1.5); - light1.position.set(1, 1, 1); - scene.add(light1); - - const light2 = new THREE.DirectionalLight(0xffffff, 4.5); - light2.position.set(0, -1, 0); - scene.add(light2); - - // - - const triangles = 160000; - - const geometry = new THREE.BufferGeometry(); - - const positions = []; - const normals = []; - const colors = []; - - const color = new THREE.Color(); - - const n = 800, - n2 = n / 2; // triangles spread in the cube - const d = 12, - d2 = d / 2; // individual triangle size - - const pA = new THREE.Vector3(); - const pB = new THREE.Vector3(); - const pC = new THREE.Vector3(); - - const cb = new THREE.Vector3(); - const ab = new THREE.Vector3(); - - for (let i = 0; i < triangles; i++) { - // positions - - const x = Math.random() * n - n2; - const y = Math.random() * n - n2; - const z = Math.random() * n - n2; - - const ax = x + Math.random() * d - d2; - const ay = y + Math.random() * d - d2; - const az = z + Math.random() * d - d2; - - const bx = x + Math.random() * d - d2; - const by = y + Math.random() * d - d2; - const bz = z + Math.random() * d - d2; - - const cx = x + Math.random() * d - d2; - const cy = y + Math.random() * d - d2; - const cz = z + Math.random() * d - d2; - - positions.push(ax, ay, az); - positions.push(bx, by, bz); - positions.push(cx, cy, cz); - - // flat face normals - - pA.set(ax, ay, az); - pB.set(bx, by, bz); - pC.set(cx, cy, cz); - - cb.subVectors(pC, pB); - ab.subVectors(pA, pB); - cb.cross(ab); - - cb.normalize(); - - const nx = cb.x; - const ny = cb.y; - const nz = cb.z; - - normals.push(nx, ny, nz); - normals.push(nx, ny, nz); - normals.push(nx, ny, nz); - - // colors - - const vx = x / n + 0.5; - const vy = y / n + 0.5; - const vz = z / n + 0.5; - - color.setRGB(vx, vy, vz); - - const alpha = Math.random(); - - colors.push(color.r, color.g, color.b, alpha); - colors.push(color.r, color.g, color.b, alpha); - colors.push(color.r, color.g, color.b, alpha); - } - - function disposeArray() { - this.array = null; - } - - geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3).onUpload(disposeArray)); - geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3).onUpload(disposeArray)); - geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 4).onUpload(disposeArray)); - - geometry.computeBoundingSphere(); - - const material = new THREE.MeshPhongMaterial({ - color: 0xd5d5d5, - specular: 0xffffff, - shininess: 250, - side: THREE.DoubleSide, - vertexColors: true, - transparent: true, - }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const time = Date.now() * 0.001; - - mesh.rotation.x = time * 0.25; - mesh.rotation.y = time * 0.5; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_buffergeometry_attributes_integer.ts b/examples-testing/examples/webgl_buffergeometry_attributes_integer.ts deleted file mode 100644 index 96926c2c3..000000000 --- a/examples-testing/examples/webgl_buffergeometry_attributes_integer.ts +++ /dev/null @@ -1,113 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer, mesh; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 3500); - camera.position.z = 2500; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x050505); - scene.fog = new THREE.Fog(0x050505, 2000, 3500); - - // geometry - - const triangles = 10000; - - const geometry = new THREE.BufferGeometry(); - - const positions = []; - const uvs = []; - const textureIndices = []; - - const n = 800, - n2 = n / 2; // triangles spread in the cube - const d = 50, - d2 = d / 2; // individual triangle size - - for (let i = 0; i < triangles; i++) { - // positions - - const x = Math.random() * n - n2; - const y = Math.random() * n - n2; - const z = Math.random() * n - n2; - - const ax = x + Math.random() * d - d2; - const ay = y + Math.random() * d - d2; - const az = z + Math.random() * d - d2; - - const bx = x + Math.random() * d - d2; - const by = y + Math.random() * d - d2; - const bz = z + Math.random() * d - d2; - - const cx = x + Math.random() * d - d2; - const cy = y + Math.random() * d - d2; - const cz = z + Math.random() * d - d2; - - positions.push(ax, ay, az); - positions.push(bx, by, bz); - positions.push(cx, cy, cz); - - // uvs - - uvs.push(0, 0); - uvs.push(0.5, 1); - uvs.push(1, 0); - - // texture indices - - const t = i % 3; - textureIndices.push(t, t, t); - } - - geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); - geometry.setAttribute('uv', new THREE.Float32BufferAttribute(uvs, 2)); - geometry.setAttribute('textureIndex', new THREE.Int16BufferAttribute(textureIndices, 1)); - geometry.attributes.textureIndex.gpuType = THREE.IntType; - - geometry.computeBoundingSphere(); - - // material - - const loader = new THREE.TextureLoader(); - - const map1 = loader.load('textures/crate.gif'); - const map2 = loader.load('textures/floors/FloorsCheckerboard_S_Diffuse.jpg'); - const map3 = loader.load('textures/terrain/grasslight-big.jpg'); - - const material = new THREE.ShaderMaterial({ - uniforms: { - uTextures: { - value: [map1, map2, map3], - }, - }, - vertexShader: document.getElementById('vertexShader').textContent, - fragmentShader: document.getElementById('fragmentShader').textContent, - side: THREE.DoubleSide, - glslVersion: THREE.GLSL3, - }); - - // mesh - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); -} - -function animate() { - const time = Date.now() * 0.001; - - mesh.rotation.x = time * 0.25; - mesh.rotation.y = time * 0.5; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_buffergeometry_attributes_none.ts b/examples-testing/examples/webgl_buffergeometry_attributes_none.ts deleted file mode 100644 index a1424e871..000000000 --- a/examples-testing/examples/webgl_buffergeometry_attributes_none.ts +++ /dev/null @@ -1,56 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer, mesh; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 3500); - camera.position.z = 4; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x050505); - scene.fog = new THREE.Fog(0x050505, 2000, 3500); - - // geometry - - const triangleCount = 10000; - const vertexCountPerTriangle = 3; - const vertexCount = triangleCount * vertexCountPerTriangle; - - const geometry = new THREE.BufferGeometry(); - geometry.setDrawRange(0, vertexCount); - - // material - - const material = new THREE.RawShaderMaterial({ - uniforms: { - seed: { value: 42 }, - }, - vertexShader: document.getElementById('vertexShader').textContent, - fragmentShader: document.getElementById('fragmentShader').textContent, - side: THREE.DoubleSide, - glslVersion: THREE.GLSL3, - }); - - // mesh - - mesh = new THREE.Mesh(geometry, material); - mesh.frustumCulled = false; - scene.add(mesh); - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); -} - -function animate(time) { - mesh.rotation.x = (time / 1000.0) * 0.25; - mesh.rotation.y = (time / 1000.0) * 0.5; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_buffergeometry_custom_attributes_particles.ts b/examples-testing/examples/webgl_buffergeometry_custom_attributes_particles.ts deleted file mode 100644 index 0dffa65cc..000000000 --- a/examples-testing/examples/webgl_buffergeometry_custom_attributes_particles.ts +++ /dev/null @@ -1,103 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let renderer, scene, camera, stats; - -let particleSystem, uniforms, geometry; - -const particles = 100000; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 300; - - scene = new THREE.Scene(); - - uniforms = { - pointTexture: { value: new THREE.TextureLoader().load('textures/sprites/spark1.png') }, - }; - - const shaderMaterial = new THREE.ShaderMaterial({ - uniforms: uniforms, - vertexShader: document.getElementById('vertexshader').textContent, - fragmentShader: document.getElementById('fragmentshader').textContent, - - blending: THREE.AdditiveBlending, - depthTest: false, - transparent: true, - vertexColors: true, - }); - - const radius = 200; - - geometry = new THREE.BufferGeometry(); - - const positions = []; - const colors = []; - const sizes = []; - - const color = new THREE.Color(); - - for (let i = 0; i < particles; i++) { - positions.push((Math.random() * 2 - 1) * radius); - positions.push((Math.random() * 2 - 1) * radius); - positions.push((Math.random() * 2 - 1) * radius); - - color.setHSL(i / particles, 1.0, 0.5); - - colors.push(color.r, color.g, color.b); - - sizes.push(20); - } - - geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); - geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); - geometry.setAttribute('size', new THREE.Float32BufferAttribute(sizes, 1).setUsage(THREE.DynamicDrawUsage)); - - particleSystem = new THREE.Points(geometry, shaderMaterial); - - scene.add(particleSystem); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - - const container = document.getElementById('container'); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const time = Date.now() * 0.005; - - particleSystem.rotation.z = 0.01 * time; - - const sizes = geometry.attributes.size.array; - - for (let i = 0; i < particles; i++) { - sizes[i] = 10 * (1 + Math.sin(0.1 * i + time)); - } - - geometry.attributes.size.needsUpdate = true; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_buffergeometry_drawrange.ts b/examples-testing/examples/webgl_buffergeometry_drawrange.ts deleted file mode 100644 index 142ff43bf..000000000 --- a/examples-testing/examples/webgl_buffergeometry_drawrange.ts +++ /dev/null @@ -1,239 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let group; -let container, stats; -const particlesData = []; -let camera, scene, renderer; -let positions, colors; -let particles; -let pointCloud; -let particlePositions; -let linesMesh; - -const maxParticleCount = 1000; -let particleCount = 500; -const r = 800; -const rHalf = r / 2; - -const effectController = { - showDots: true, - showLines: true, - minDistance: 150, - limitConnections: false, - maxConnections: 20, - particleCount: 500, -}; - -init(); - -function initGUI() { - const gui = new GUI(); - - gui.add(effectController, 'showDots').onChange(function (value) { - pointCloud.visible = value; - }); - gui.add(effectController, 'showLines').onChange(function (value) { - linesMesh.visible = value; - }); - gui.add(effectController, 'minDistance', 10, 300); - gui.add(effectController, 'limitConnections'); - gui.add(effectController, 'maxConnections', 0, 30, 1); - gui.add(effectController, 'particleCount', 0, maxParticleCount, 1).onChange(function (value) { - particleCount = value; - particles.setDrawRange(0, particleCount); - }); -} - -function init() { - initGUI(); - - container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 4000); - camera.position.z = 1750; - - const controls = new OrbitControls(camera, container); - controls.minDistance = 1000; - controls.maxDistance = 3000; - - scene = new THREE.Scene(); - - group = new THREE.Group(); - scene.add(group); - - const helper = new THREE.BoxHelper(new THREE.Mesh(new THREE.BoxGeometry(r, r, r))); - helper.material.color.setHex(0x474747); - helper.material.blending = THREE.AdditiveBlending; - helper.material.transparent = true; - group.add(helper); - - const segments = maxParticleCount * maxParticleCount; - - positions = new Float32Array(segments * 3); - colors = new Float32Array(segments * 3); - - const pMaterial = new THREE.PointsMaterial({ - color: 0xffffff, - size: 3, - blending: THREE.AdditiveBlending, - transparent: true, - sizeAttenuation: false, - }); - - particles = new THREE.BufferGeometry(); - particlePositions = new Float32Array(maxParticleCount * 3); - - for (let i = 0; i < maxParticleCount; i++) { - const x = Math.random() * r - r / 2; - const y = Math.random() * r - r / 2; - const z = Math.random() * r - r / 2; - - particlePositions[i * 3] = x; - particlePositions[i * 3 + 1] = y; - particlePositions[i * 3 + 2] = z; - - // add it to the geometry - particlesData.push({ - velocity: new THREE.Vector3(-1 + Math.random() * 2, -1 + Math.random() * 2, -1 + Math.random() * 2), - numConnections: 0, - }); - } - - particles.setDrawRange(0, particleCount); - particles.setAttribute( - 'position', - new THREE.BufferAttribute(particlePositions, 3).setUsage(THREE.DynamicDrawUsage), - ); - - // create the particle system - pointCloud = new THREE.Points(particles, pMaterial); - group.add(pointCloud); - - const geometry = new THREE.BufferGeometry(); - - geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3).setUsage(THREE.DynamicDrawUsage)); - geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3).setUsage(THREE.DynamicDrawUsage)); - - geometry.computeBoundingSphere(); - - geometry.setDrawRange(0, 0); - - const material = new THREE.LineBasicMaterial({ - vertexColors: true, - blending: THREE.AdditiveBlending, - transparent: true, - }); - - linesMesh = new THREE.LineSegments(geometry, material); - group.add(linesMesh); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - let vertexpos = 0; - let colorpos = 0; - let numConnected = 0; - - for (let i = 0; i < particleCount; i++) particlesData[i].numConnections = 0; - - for (let i = 0; i < particleCount; i++) { - // get the particle - const particleData = particlesData[i]; - - particlePositions[i * 3] += particleData.velocity.x; - particlePositions[i * 3 + 1] += particleData.velocity.y; - particlePositions[i * 3 + 2] += particleData.velocity.z; - - if (particlePositions[i * 3 + 1] < -rHalf || particlePositions[i * 3 + 1] > rHalf) - particleData.velocity.y = -particleData.velocity.y; - - if (particlePositions[i * 3] < -rHalf || particlePositions[i * 3] > rHalf) - particleData.velocity.x = -particleData.velocity.x; - - if (particlePositions[i * 3 + 2] < -rHalf || particlePositions[i * 3 + 2] > rHalf) - particleData.velocity.z = -particleData.velocity.z; - - if (effectController.limitConnections && particleData.numConnections >= effectController.maxConnections) - continue; - - // Check collision - for (let j = i + 1; j < particleCount; j++) { - const particleDataB = particlesData[j]; - if (effectController.limitConnections && particleDataB.numConnections >= effectController.maxConnections) - continue; - - const dx = particlePositions[i * 3] - particlePositions[j * 3]; - const dy = particlePositions[i * 3 + 1] - particlePositions[j * 3 + 1]; - const dz = particlePositions[i * 3 + 2] - particlePositions[j * 3 + 2]; - const dist = Math.sqrt(dx * dx + dy * dy + dz * dz); - - if (dist < effectController.minDistance) { - particleData.numConnections++; - particleDataB.numConnections++; - - const alpha = 1.0 - dist / effectController.minDistance; - - positions[vertexpos++] = particlePositions[i * 3]; - positions[vertexpos++] = particlePositions[i * 3 + 1]; - positions[vertexpos++] = particlePositions[i * 3 + 2]; - - positions[vertexpos++] = particlePositions[j * 3]; - positions[vertexpos++] = particlePositions[j * 3 + 1]; - positions[vertexpos++] = particlePositions[j * 3 + 2]; - - colors[colorpos++] = alpha; - colors[colorpos++] = alpha; - colors[colorpos++] = alpha; - - colors[colorpos++] = alpha; - colors[colorpos++] = alpha; - colors[colorpos++] = alpha; - - numConnected++; - } - } - } - - linesMesh.geometry.setDrawRange(0, numConnected * 2); - linesMesh.geometry.attributes.position.needsUpdate = true; - linesMesh.geometry.attributes.color.needsUpdate = true; - - pointCloud.geometry.attributes.position.needsUpdate = true; - - render(); - - stats.update(); -} - -function render() { - const time = Date.now() * 0.001; - - group.rotation.y = time * 0.1; - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_buffergeometry_glbufferattribute.ts b/examples-testing/examples/webgl_buffergeometry_glbufferattribute.ts deleted file mode 100644 index aea462cf4..000000000 --- a/examples-testing/examples/webgl_buffergeometry_glbufferattribute.ts +++ /dev/null @@ -1,139 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; - -let camera, scene, renderer; - -let points; - -const particles = 300000; -let drawCount = 10000; - -init(); -animate(); - -function init() { - container = document.getElementById('container'); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - - container.appendChild(renderer.domElement); - - // - - camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 5, 3500); - camera.position.z = 2750; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x050505); - scene.fog = new THREE.Fog(0x050505, 2000, 3500); - - // - - const geometry = new THREE.BufferGeometry(); - - const positions = []; - const positions2 = []; - const colors = []; - - const color = new THREE.Color(); - - const n = 1000, - n2 = n / 2; // particles spread in the cube - - for (let i = 0; i < particles; i++) { - // positions - - const x = Math.random() * n - n2; - const y = Math.random() * n - n2; - const z = Math.random() * n - n2; - - positions.push(x, y, z); - positions2.push(z * 0.3, x * 0.3, y * 0.3); - - // colors - - const vx = x / n + 0.5; - const vy = y / n + 0.5; - const vz = z / n + 0.5; - - color.setRGB(vx, vy, vz, THREE.SRGBColorSpace); - - colors.push(color.r, color.g, color.b); - } - - const gl = renderer.getContext(); - - const pos = gl.createBuffer(); - gl.bindBuffer(gl.ARRAY_BUFFER, pos); - gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW); - - const pos2 = gl.createBuffer(); - gl.bindBuffer(gl.ARRAY_BUFFER, pos2); - gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions2), gl.STATIC_DRAW); - - const rgb = gl.createBuffer(); - gl.bindBuffer(gl.ARRAY_BUFFER, rgb); - gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW); - - const posAttr1 = new THREE.GLBufferAttribute(pos, gl.FLOAT, 3, 4, particles); - const posAttr2 = new THREE.GLBufferAttribute(pos2, gl.FLOAT, 3, 4, particles); - geometry.setAttribute('position', posAttr1); - - setInterval(function () { - const attr = geometry.getAttribute('position'); - - geometry.setAttribute('position', attr === posAttr1 ? posAttr2 : posAttr1); - }, 2000); - - geometry.setAttribute('color', new THREE.GLBufferAttribute(rgb, gl.FLOAT, 3, 4, particles)); - - // - - const material = new THREE.PointsMaterial({ size: 15, vertexColors: true }); - - points = new THREE.Points(geometry, material); - - geometry.boundingSphere = new THREE.Sphere().set(new THREE.Vector3(), 500); - - scene.add(points); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - drawCount = (Math.max(5000, drawCount) + Math.floor(500 * Math.random())) % particles; - points.geometry.setDrawRange(0, drawCount); - - const time = Date.now() * 0.001; - - points.rotation.x = time * 0.1; - points.rotation.y = time * 0.2; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_buffergeometry_indexed.ts b/examples-testing/examples/webgl_buffergeometry_indexed.ts deleted file mode 100644 index a2f9f3795..000000000 --- a/examples-testing/examples/webgl_buffergeometry_indexed.ts +++ /dev/null @@ -1,137 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, stats; - -let mesh; - -init(); - -function init() { - // - - camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 3500); - camera.position.z = 64; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x050505); - - // - - const light = new THREE.HemisphereLight(); - light.intensity = 3; - scene.add(light); - - // - - const geometry = new THREE.BufferGeometry(); - - const indices = []; - - const vertices = []; - const normals = []; - const colors = []; - - const size = 20; - const segments = 10; - - const halfSize = size / 2; - const segmentSize = size / segments; - - const _color = new THREE.Color(); - - // generate vertices, normals and color data for a simple grid geometry - - for (let i = 0; i <= segments; i++) { - const y = i * segmentSize - halfSize; - - for (let j = 0; j <= segments; j++) { - const x = j * segmentSize - halfSize; - - vertices.push(x, -y, 0); - normals.push(0, 0, 1); - - const r = x / size + 0.5; - const g = y / size + 0.5; - - _color.setRGB(r, g, 1, THREE.SRGBColorSpace); - - colors.push(_color.r, _color.g, _color.b); - } - } - - // generate indices (data for element array buffer) - - for (let i = 0; i < segments; i++) { - for (let j = 0; j < segments; j++) { - const a = i * (segments + 1) + (j + 1); - const b = i * (segments + 1) + j; - const c = (i + 1) * (segments + 1) + j; - const d = (i + 1) * (segments + 1) + (j + 1); - - // generate two faces (triangles) per iteration - - indices.push(a, b, d); // face one - indices.push(b, c, d); // face two - } - } - - // - - geometry.setIndex(indices); - geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); - geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3)); - geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); - - const material = new THREE.MeshPhongMaterial({ - side: THREE.DoubleSide, - vertexColors: true, - }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - const gui = new GUI(); - gui.add(material, 'wireframe'); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const time = Date.now() * 0.001; - - mesh.rotation.x = time * 0.25; - mesh.rotation.y = time * 0.5; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_buffergeometry_instancing.ts b/examples-testing/examples/webgl_buffergeometry_instancing.ts deleted file mode 100644 index a5b90ae6e..000000000 --- a/examples-testing/examples/webgl_buffergeometry_instancing.ts +++ /dev/null @@ -1,138 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let container, stats; - -let camera, scene, renderer; - -init(); - -function init() { - container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10); - camera.position.z = 2; - - scene = new THREE.Scene(); - - // geometry - - const vector = new THREE.Vector4(); - - const instances = 50000; - - const positions = []; - const offsets = []; - const colors = []; - const orientationsStart = []; - const orientationsEnd = []; - - positions.push(0.025, -0.025, 0); - positions.push(-0.025, 0.025, 0); - positions.push(0, 0, 0.025); - - // instanced attributes - - for (let i = 0; i < instances; i++) { - // offsets - - offsets.push(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5); - - // colors - - colors.push(Math.random(), Math.random(), Math.random(), Math.random()); - - // orientation start - - vector.set(Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1); - vector.normalize(); - - orientationsStart.push(vector.x, vector.y, vector.z, vector.w); - - // orientation end - - vector.set(Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1); - vector.normalize(); - - orientationsEnd.push(vector.x, vector.y, vector.z, vector.w); - } - - const geometry = new THREE.InstancedBufferGeometry(); - geometry.instanceCount = instances; // set so its initalized for dat.GUI, will be set in first draw otherwise - - geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); - - geometry.setAttribute('offset', new THREE.InstancedBufferAttribute(new Float32Array(offsets), 3)); - geometry.setAttribute('color', new THREE.InstancedBufferAttribute(new Float32Array(colors), 4)); - geometry.setAttribute( - 'orientationStart', - new THREE.InstancedBufferAttribute(new Float32Array(orientationsStart), 4), - ); - geometry.setAttribute('orientationEnd', new THREE.InstancedBufferAttribute(new Float32Array(orientationsEnd), 4)); - - // material - - const material = new THREE.RawShaderMaterial({ - uniforms: { - time: { value: 1.0 }, - sineTime: { value: 1.0 }, - }, - vertexShader: document.getElementById('vertexShader').textContent, - fragmentShader: document.getElementById('fragmentShader').textContent, - side: THREE.DoubleSide, - forceSinglePass: true, - transparent: true, - }); - - // - - const mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - const gui = new GUI({ width: 350 }); - gui.add(geometry, 'instanceCount', 0, instances); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const time = performance.now(); - - const object = scene.children[0]; - - object.rotation.y = time * 0.0005; - object.material.uniforms['time'].value = time * 0.005; - object.material.uniforms['sineTime'].value = Math.sin(object.material.uniforms['time'].value * 0.05); - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_buffergeometry_instancing_billboards.ts b/examples-testing/examples/webgl_buffergeometry_instancing_billboards.ts deleted file mode 100644 index 2158dff39..000000000 --- a/examples-testing/examples/webgl_buffergeometry_instancing_billboards.ts +++ /dev/null @@ -1,86 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; - -let camera, scene, renderer; -let geometry, material, mesh; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 5000); - camera.position.z = 1400; - - scene = new THREE.Scene(); - - const circleGeometry = new THREE.CircleGeometry(1, 6); - - geometry = new THREE.InstancedBufferGeometry(); - geometry.index = circleGeometry.index; - geometry.attributes = circleGeometry.attributes; - - const particleCount = 75000; - - const translateArray = new Float32Array(particleCount * 3); - - for (let i = 0, i3 = 0, l = particleCount; i < l; i++, i3 += 3) { - translateArray[i3 + 0] = Math.random() * 2 - 1; - translateArray[i3 + 1] = Math.random() * 2 - 1; - translateArray[i3 + 2] = Math.random() * 2 - 1; - } - - geometry.setAttribute('translate', new THREE.InstancedBufferAttribute(translateArray, 3)); - - material = new THREE.RawShaderMaterial({ - uniforms: { - map: { value: new THREE.TextureLoader().load('textures/sprites/circle.png') }, - time: { value: 0.0 }, - }, - vertexShader: document.getElementById('vshader').textContent, - fragmentShader: document.getElementById('fshader').textContent, - depthTest: true, - depthWrite: true, - }); - - mesh = new THREE.Mesh(geometry, material); - mesh.scale.set(500, 500, 500); - scene.add(mesh); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); - - return true; -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const time = performance.now() * 0.0005; - - material.uniforms['time'].value = time; - - mesh.rotation.x = time * 0.2; - mesh.rotation.y = time * 0.4; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_buffergeometry_instancing_interleaved.ts b/examples-testing/examples/webgl_buffergeometry_instancing_interleaved.ts deleted file mode 100644 index bef2c264d..000000000 --- a/examples-testing/examples/webgl_buffergeometry_instancing_interleaved.ts +++ /dev/null @@ -1,152 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; -let camera, scene, renderer, mesh; - -const instances = 5000; -let lastTime = 0; - -const moveQ = new THREE.Quaternion(0.5, 0.5, 0.5, 0.0).normalize(); -const tmpQ = new THREE.Quaternion(); -const tmpM = new THREE.Matrix4(); -const currentM = new THREE.Matrix4(); - -init(); - -function init() { - container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x101010); - - // geometry - - const geometry = new THREE.InstancedBufferGeometry(); - - // per mesh data x,y,z,w,u,v,s,t for 4-element alignment - // only use x,y,z and u,v; but x, y, z, nx, ny, nz, u, v would be a good layout - const vertexBuffer = new THREE.InterleavedBuffer( - new Float32Array([ - // Front - -1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, -1, -1, 1, 0, 0, 1, 0, 0, 1, -1, 1, 0, 1, 1, 0, 0, - // Back - 1, 1, -1, 0, 1, 0, 0, 0, -1, 1, -1, 0, 0, 0, 0, 0, 1, -1, -1, 0, 1, 1, 0, 0, -1, -1, -1, 0, 0, 1, 0, 0, - // Left - -1, 1, -1, 0, 1, 1, 0, 0, -1, 1, 1, 0, 1, 0, 0, 0, -1, -1, -1, 0, 0, 1, 0, 0, -1, -1, 1, 0, 0, 0, 0, 0, - // Right - 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, -1, 0, 1, 1, 0, 0, 1, -1, 1, 0, 0, 0, 0, 0, 1, -1, -1, 0, 0, 1, 0, 0, - // Top - -1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, -1, 1, -1, 0, 0, 1, 0, 0, 1, 1, -1, 0, 1, 1, 0, 0, - // Bottom - 1, -1, 1, 0, 1, 0, 0, 0, -1, -1, 1, 0, 0, 0, 0, 0, 1, -1, -1, 0, 1, 1, 0, 0, -1, -1, -1, 0, 0, 1, 0, 0, - ]), - 8, - ); - - // Use vertexBuffer, starting at offset 0, 3 items in position attribute - const positions = new THREE.InterleavedBufferAttribute(vertexBuffer, 3, 0); - geometry.setAttribute('position', positions); - // Use vertexBuffer, starting at offset 4, 2 items in uv attribute - const uvs = new THREE.InterleavedBufferAttribute(vertexBuffer, 2, 4); - geometry.setAttribute('uv', uvs); - - const indices = new Uint16Array([ - 0, 2, 1, 2, 3, 1, 4, 6, 5, 6, 7, 5, 8, 10, 9, 10, 11, 9, 12, 14, 13, 14, 15, 13, 16, 17, 18, 18, 17, 19, 20, 21, - 22, 22, 21, 23, - ]); - - geometry.setIndex(new THREE.BufferAttribute(indices, 1)); - - // material - - const material = new THREE.MeshBasicMaterial(); - material.map = new THREE.TextureLoader().load('textures/crate.gif'); - material.map.colorSpace = THREE.SRGBColorSpace; - material.map.flipY = false; - - // per instance data - - const matrix = new THREE.Matrix4(); - const offset = new THREE.Vector3(); - const orientation = new THREE.Quaternion(); - const scale = new THREE.Vector3(1, 1, 1); - let x, y, z, w; - - mesh = new THREE.InstancedMesh(geometry, material, instances); - - for (let i = 0; i < instances; i++) { - // offsets - - x = Math.random() * 100 - 50; - y = Math.random() * 100 - 50; - z = Math.random() * 100 - 50; - - offset.set(x, y, z).normalize(); - offset.multiplyScalar(5); // move out at least 5 units from center in current direction - offset.set(x + offset.x, y + offset.y, z + offset.z); - - // orientations - - x = Math.random() * 2 - 1; - y = Math.random() * 2 - 1; - z = Math.random() * 2 - 1; - w = Math.random() * 2 - 1; - - orientation.set(x, y, z, w).normalize(); - - matrix.compose(offset, orientation, scale); - - mesh.setMatrixAt(i, matrix); - } - - scene.add(mesh); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const time = performance.now(); - - mesh.rotation.y = time * 0.00005; - - const delta = (time - lastTime) / 5000; - tmpQ.set(moveQ.x * delta, moveQ.y * delta, moveQ.z * delta, 1).normalize(); - tmpM.makeRotationFromQuaternion(tmpQ); - - for (let i = 0, il = instances; i < il; i++) { - mesh.getMatrixAt(i, currentM); - currentM.multiply(tmpM); - mesh.setMatrixAt(i, currentM); - } - - mesh.instanceMatrix.needsUpdate = true; - mesh.computeBoundingSphere(); - - lastTime = time; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_buffergeometry_lines.ts b/examples-testing/examples/webgl_buffergeometry_lines.ts deleted file mode 100644 index 1aaa5ca4a..000000000 --- a/examples-testing/examples/webgl_buffergeometry_lines.ts +++ /dev/null @@ -1,118 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats, clock; - -let camera, scene, renderer; - -let line; - -const segments = 10000; -const r = 800; -let t = 0; - -init(); - -function init() { - container = document.getElementById('container'); - - // - - camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 4000); - camera.position.z = 2750; - - scene = new THREE.Scene(); - - clock = new THREE.Clock(); - - const geometry = new THREE.BufferGeometry(); - const material = new THREE.LineBasicMaterial({ vertexColors: true }); - - const positions = []; - const colors = []; - - for (let i = 0; i < segments; i++) { - const x = Math.random() * r - r / 2; - const y = Math.random() * r - r / 2; - const z = Math.random() * r - r / 2; - - // positions - - positions.push(x, y, z); - - // colors - - colors.push(x / r + 0.5); - colors.push(y / r + 0.5); - colors.push(z / r + 0.5); - } - - geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); - geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); - generateMorphTargets(geometry); - - geometry.computeBoundingSphere(); - - line = new THREE.Line(geometry, material); - scene.add(line); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - - container.appendChild(renderer.domElement); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const delta = clock.getDelta(); - const time = clock.getElapsedTime(); - - line.rotation.x = time * 0.25; - line.rotation.y = time * 0.5; - - t += delta * 0.5; - line.morphTargetInfluences[0] = Math.abs(Math.sin(t)); - - renderer.render(scene, camera); - - stats.update(); -} - -function generateMorphTargets(geometry) { - const data = []; - - for (let i = 0; i < segments; i++) { - const x = Math.random() * r - r / 2; - const y = Math.random() * r - r / 2; - const z = Math.random() * r - r / 2; - - data.push(x, y, z); - } - - const morphTarget = new THREE.Float32BufferAttribute(data, 3); - morphTarget.name = 'target1'; - - geometry.morphAttributes.position = [morphTarget]; -} diff --git a/examples-testing/examples/webgl_buffergeometry_lines_indexed.ts b/examples-testing/examples/webgl_buffergeometry_lines_indexed.ts deleted file mode 100644 index 58296087e..000000000 --- a/examples-testing/examples/webgl_buffergeometry_lines_indexed.ts +++ /dev/null @@ -1,179 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; - -let camera, scene, renderer; - -let parent_node; - -init(); - -function init() { - container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 9000; - - scene = new THREE.Scene(); - - const geometry = new THREE.BufferGeometry(); - const material = new THREE.LineBasicMaterial({ vertexColors: true }); - - const indices = []; - const positions = []; - const colors = []; - - let next_positions_index = 0; - - // - - const iteration_count = 4; - const rangle = (60 * Math.PI) / 180.0; - - function add_vertex(v) { - positions.push(v.x, v.y, v.z); - colors.push(Math.random() * 0.5 + 0.5, Math.random() * 0.5 + 0.5, 1); - - return next_positions_index++; - } - - // simple Koch curve - - function snowflake_iteration(p0, p4, depth) { - if (--depth < 0) { - const i = next_positions_index - 1; // p0 already there - add_vertex(p4); - indices.push(i, i + 1); - - return; - } - - const v = p4.clone().sub(p0); - const v_tier = v.clone().multiplyScalar(1 / 3); - const p1 = p0.clone().add(v_tier); - - const angle = Math.atan2(v.y, v.x) + rangle; - const length = v_tier.length(); - const p2 = p1.clone(); - p2.x += Math.cos(angle) * length; - p2.y += Math.sin(angle) * length; - - const p3 = p0.clone().add(v_tier).add(v_tier); - - snowflake_iteration(p0, p1, depth); - snowflake_iteration(p1, p2, depth); - snowflake_iteration(p2, p3, depth); - snowflake_iteration(p3, p4, depth); - } - - function snowflake(points, loop, x_offset) { - for (let iteration = 0; iteration != iteration_count; iteration++) { - add_vertex(points[0]); - - for (let p_index = 0, p_count = points.length - 1; p_index != p_count; p_index++) { - snowflake_iteration(points[p_index], points[p_index + 1], iteration); - } - - if (loop) snowflake_iteration(points[points.length - 1], points[0], iteration); - - // translate input curve for next iteration - - for (let p_index = 0, p_count = points.length; p_index != p_count; p_index++) { - points[p_index].x += x_offset; - } - } - } - - let y = 0; - - snowflake([new THREE.Vector3(0, y, 0), new THREE.Vector3(500, y, 0)], false, 600); - - y += 600; - snowflake( - [new THREE.Vector3(0, y, 0), new THREE.Vector3(250, y + 400, 0), new THREE.Vector3(500, y, 0)], - true, - 600, - ); - - y += 600; - snowflake( - [ - new THREE.Vector3(0, y, 0), - new THREE.Vector3(500, y, 0), - new THREE.Vector3(500, y + 500, 0), - new THREE.Vector3(0, y + 500, 0), - ], - true, - 600, - ); - - y += 1000; - snowflake( - [ - new THREE.Vector3(250, y, 0), - new THREE.Vector3(500, y, 0), - new THREE.Vector3(250, y, 0), - new THREE.Vector3(250, y + 250, 0), - new THREE.Vector3(250, y, 0), - new THREE.Vector3(0, y, 0), - new THREE.Vector3(250, y, 0), - new THREE.Vector3(250, y - 250, 0), - new THREE.Vector3(250, y, 0), - ], - false, - 600, - ); - - // - - geometry.setIndex(indices); - geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); - geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); - geometry.computeBoundingSphere(); - - const lineSegments = new THREE.LineSegments(geometry, material); - lineSegments.position.x -= 1200; - lineSegments.position.y -= 1200; - - parent_node = new THREE.Object3D(); - parent_node.add(lineSegments); - - scene.add(parent_node); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - - container.appendChild(renderer.domElement); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const time = Date.now() * 0.001; - - parent_node.rotation.z = time * 0.5; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_buffergeometry_points.ts b/examples-testing/examples/webgl_buffergeometry_points.ts deleted file mode 100644 index 4547d9d08..000000000 --- a/examples-testing/examples/webgl_buffergeometry_points.ts +++ /dev/null @@ -1,109 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; - -let camera, scene, renderer; - -let points; - -init(); -animate(); - -function init() { - container = document.getElementById('container'); - - // - - camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 5, 3500); - camera.position.z = 2750; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x050505); - scene.fog = new THREE.Fog(0x050505, 2000, 3500); - - // - - const particles = 500000; - - const geometry = new THREE.BufferGeometry(); - - const positions = []; - const colors = []; - - const color = new THREE.Color(); - - const n = 1000, - n2 = n / 2; // particles spread in the cube - - for (let i = 0; i < particles; i++) { - // positions - - const x = Math.random() * n - n2; - const y = Math.random() * n - n2; - const z = Math.random() * n - n2; - - positions.push(x, y, z); - - // colors - - const vx = x / n + 0.5; - const vy = y / n + 0.5; - const vz = z / n + 0.5; - - color.setRGB(vx, vy, vz, THREE.SRGBColorSpace); - - colors.push(color.r, color.g, color.b); - } - - geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); - geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); - - geometry.computeBoundingSphere(); - - // - - const material = new THREE.PointsMaterial({ size: 15, vertexColors: true }); - - points = new THREE.Points(geometry, material); - scene.add(points); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - - container.appendChild(renderer.domElement); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const time = Date.now() * 0.001; - - points.rotation.x = time * 0.25; - points.rotation.y = time * 0.5; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_buffergeometry_points_interleaved.ts b/examples-testing/examples/webgl_buffergeometry_points_interleaved.ts deleted file mode 100644 index 93eed992e..000000000 --- a/examples-testing/examples/webgl_buffergeometry_points_interleaved.ts +++ /dev/null @@ -1,122 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; - -let camera, scene, renderer; - -let points; - -init(); - -function init() { - container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 5, 3500); - camera.position.z = 2750; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x050505); - scene.fog = new THREE.Fog(0x050505, 2000, 3500); - - // - - const particles = 500000; - - const geometry = new THREE.BufferGeometry(); - - // create a generic buffer of binary data (a single particle has 16 bytes of data) - - const arrayBuffer = new ArrayBuffer(particles * 16); - - // the following typed arrays share the same buffer - - const interleavedFloat32Buffer = new Float32Array(arrayBuffer); - const interleavedUint8Buffer = new Uint8Array(arrayBuffer); - - // - - const color = new THREE.Color(); - - const n = 1000, - n2 = n / 2; // particles spread in the cube - - for (let i = 0; i < interleavedFloat32Buffer.length; i += 4) { - // position (first 12 bytes) - - const x = Math.random() * n - n2; - const y = Math.random() * n - n2; - const z = Math.random() * n - n2; - - interleavedFloat32Buffer[i + 0] = x; - interleavedFloat32Buffer[i + 1] = y; - interleavedFloat32Buffer[i + 2] = z; - - // color (last 4 bytes) - - const vx = x / n + 0.5; - const vy = y / n + 0.5; - const vz = z / n + 0.5; - - color.setRGB(vx, vy, vz, THREE.SRGBColorSpace); - - const j = (i + 3) * 4; - - interleavedUint8Buffer[j + 0] = color.r * 255; - interleavedUint8Buffer[j + 1] = color.g * 255; - interleavedUint8Buffer[j + 2] = color.b * 255; - interleavedUint8Buffer[j + 3] = 0; // not needed - } - - const interleavedBuffer32 = new THREE.InterleavedBuffer(interleavedFloat32Buffer, 4); - const interleavedBuffer8 = new THREE.InterleavedBuffer(interleavedUint8Buffer, 16); - - geometry.setAttribute('position', new THREE.InterleavedBufferAttribute(interleavedBuffer32, 3, 0, false)); - geometry.setAttribute('color', new THREE.InterleavedBufferAttribute(interleavedBuffer8, 3, 12, true)); - - // - - const material = new THREE.PointsMaterial({ size: 15, vertexColors: true }); - - points = new THREE.Points(geometry, material); - scene.add(points); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - - container.appendChild(renderer.domElement); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const time = Date.now() * 0.001; - - points.rotation.x = time * 0.25; - points.rotation.y = time * 0.5; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_buffergeometry_rawshader.ts b/examples-testing/examples/webgl_buffergeometry_rawshader.ts deleted file mode 100644 index 5bc113dc3..000000000 --- a/examples-testing/examples/webgl_buffergeometry_rawshader.ts +++ /dev/null @@ -1,97 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; - -let camera, scene, renderer; - -init(); - -function init() { - container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10); - camera.position.z = 2; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x101010); - - // geometry - // nr of triangles with 3 vertices per triangle - const vertexCount = 200 * 3; - - const geometry = new THREE.BufferGeometry(); - - const positions = []; - const colors = []; - - for (let i = 0; i < vertexCount; i++) { - // adding x,y,z - positions.push(Math.random() - 0.5); - positions.push(Math.random() - 0.5); - positions.push(Math.random() - 0.5); - - // adding r,g,b,a - colors.push(Math.random() * 255); - colors.push(Math.random() * 255); - colors.push(Math.random() * 255); - colors.push(Math.random() * 255); - } - - const positionAttribute = new THREE.Float32BufferAttribute(positions, 3); - const colorAttribute = new THREE.Uint8BufferAttribute(colors, 4); - - colorAttribute.normalized = true; // this will map the buffer values to 0.0f - +1.0f in the shader - - geometry.setAttribute('position', positionAttribute); - geometry.setAttribute('color', colorAttribute); - - // material - - const material = new THREE.RawShaderMaterial({ - uniforms: { - time: { value: 1.0 }, - }, - vertexShader: document.getElementById('vertexShader').textContent, - fragmentShader: document.getElementById('fragmentShader').textContent, - side: THREE.DoubleSide, - transparent: true, - }); - - const mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const time = performance.now(); - - const object = scene.children[0]; - - object.rotation.y = time * 0.0005; - object.material.uniforms.time.value = time * 0.005; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_buffergeometry_selective_draw.ts b/examples-testing/examples/webgl_buffergeometry_selective_draw.ts deleted file mode 100644 index d07176c51..000000000 --- a/examples-testing/examples/webgl_buffergeometry_selective_draw.ts +++ /dev/null @@ -1,150 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let camera, scene, renderer, stats; -let geometry, mesh; -const numLat = 100; -const numLng = 200; -let numLinesCulled = 0; - -init(); - -function init() { - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 10); - camera.position.z = 3.5; - - stats = new Stats(); - document.body.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); - - addLines(1.0); - - const hideLinesButton = document.getElementById('hideLines'); - hideLinesButton.addEventListener('click', hideLines); - - const showAllLinesButton = document.getElementById('showAllLines'); - showAllLinesButton.addEventListener('click', showAllLines); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); -} - -function addLines(radius) { - geometry = new THREE.BufferGeometry(); - const linePositions = new Float32Array(numLat * numLng * 3 * 2); - const lineColors = new Float32Array(numLat * numLng * 3 * 2); - const visible = new Float32Array(numLat * numLng * 2); - - for (let i = 0; i < numLat; ++i) { - for (let j = 0; j < numLng; ++j) { - const lat = (Math.random() * Math.PI) / 50.0 + (i / numLat) * Math.PI; - const lng = (Math.random() * Math.PI) / 50.0 + (j / numLng) * 2 * Math.PI; - - const index = i * numLng + j; - - linePositions[index * 6 + 0] = 0; - linePositions[index * 6 + 1] = 0; - linePositions[index * 6 + 2] = 0; - linePositions[index * 6 + 3] = radius * Math.sin(lat) * Math.cos(lng); - linePositions[index * 6 + 4] = radius * Math.cos(lat); - linePositions[index * 6 + 5] = radius * Math.sin(lat) * Math.sin(lng); - - const color = new THREE.Color(0xffffff); - - color.setHSL(lat / Math.PI, 1.0, 0.2); - lineColors[index * 6 + 0] = color.r; - lineColors[index * 6 + 1] = color.g; - lineColors[index * 6 + 2] = color.b; - - color.setHSL(lat / Math.PI, 1.0, 0.7); - lineColors[index * 6 + 3] = color.r; - lineColors[index * 6 + 4] = color.g; - lineColors[index * 6 + 5] = color.b; - - // non-0 is visible - visible[index * 2 + 0] = 1.0; - visible[index * 2 + 1] = 1.0; - } - } - - geometry.setAttribute('position', new THREE.BufferAttribute(linePositions, 3)); - geometry.setAttribute('vertColor', new THREE.BufferAttribute(lineColors, 3)); - geometry.setAttribute('visible', new THREE.BufferAttribute(visible, 1)); - - geometry.computeBoundingSphere(); - - const shaderMaterial = new THREE.ShaderMaterial({ - vertexShader: document.getElementById('vertexshader').textContent, - fragmentShader: document.getElementById('fragmentshader').textContent, - }); - - mesh = new THREE.LineSegments(geometry, shaderMaterial); - scene.add(mesh); - - updateCount(); -} - -function updateCount() { - const str = - '1 draw call, ' + - numLat * numLng + - ' lines, ' + - numLinesCulled + - ' culled (author)'; - document.getElementById('title').innerHTML = str.replace(/\B(?=(\d{3})+(?!\d))/g, ','); -} - -function hideLines() { - for (let i = 0; i < geometry.attributes.visible.array.length; i += 2) { - if (Math.random() > 0.75) { - if (geometry.attributes.visible.array[i + 0]) { - ++numLinesCulled; - } - - geometry.attributes.visible.array[i + 0] = 0; - geometry.attributes.visible.array[i + 1] = 0; - } - } - - geometry.attributes.visible.needsUpdate = true; - - updateCount(); -} - -function showAllLines() { - numLinesCulled = 0; - - for (let i = 0; i < geometry.attributes.visible.array.length; i += 2) { - geometry.attributes.visible.array[i + 0] = 1; - geometry.attributes.visible.array[i + 1] = 1; - } - - geometry.attributes.visible.needsUpdate = true; - - updateCount(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const time = Date.now() * 0.001; - - mesh.rotation.x = time * 0.25; - mesh.rotation.y = time * 0.5; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_buffergeometry_uint.ts b/examples-testing/examples/webgl_buffergeometry_uint.ts deleted file mode 100644 index 0b8df6ec7..000000000 --- a/examples-testing/examples/webgl_buffergeometry_uint.ts +++ /dev/null @@ -1,177 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; - -let camera, scene, renderer; - -let mesh; - -init(); - -function init() { - container = document.getElementById('container'); - - // - - camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 3500); - camera.position.z = 2750; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x050505); - scene.fog = new THREE.Fog(0x050505, 2000, 3500); - - // - - scene.add(new THREE.AmbientLight(0xcccccc)); - - const light1 = new THREE.DirectionalLight(0xffffff, 1.5); - light1.position.set(1, 1, 1); - scene.add(light1); - - const light2 = new THREE.DirectionalLight(0xffffff, 4.5); - light2.position.set(0, -1, 0); - scene.add(light2); - - // - - const triangles = 500000; - - const geometry = new THREE.BufferGeometry(); - - const positions = []; - const normals = []; - const colors = []; - - const color = new THREE.Color(); - - const n = 800, - n2 = n / 2; // triangles spread in the cube - const d = 12, - d2 = d / 2; // individual triangle size - - const pA = new THREE.Vector3(); - const pB = new THREE.Vector3(); - const pC = new THREE.Vector3(); - - const cb = new THREE.Vector3(); - const ab = new THREE.Vector3(); - - for (let i = 0; i < triangles; i++) { - // positions - - const x = Math.random() * n - n2; - const y = Math.random() * n - n2; - const z = Math.random() * n - n2; - - const ax = x + Math.random() * d - d2; - const ay = y + Math.random() * d - d2; - const az = z + Math.random() * d - d2; - - const bx = x + Math.random() * d - d2; - const by = y + Math.random() * d - d2; - const bz = z + Math.random() * d - d2; - - const cx = x + Math.random() * d - d2; - const cy = y + Math.random() * d - d2; - const cz = z + Math.random() * d - d2; - - positions.push(ax, ay, az); - positions.push(bx, by, bz); - positions.push(cx, cy, cz); - - // flat face normals - - pA.set(ax, ay, az); - pB.set(bx, by, bz); - pC.set(cx, cy, cz); - - cb.subVectors(pC, pB); - ab.subVectors(pA, pB); - cb.cross(ab); - - cb.normalize(); - - const nx = cb.x; - const ny = cb.y; - const nz = cb.z; - - normals.push(nx * 32767, ny * 32767, nz * 32767); - normals.push(nx * 32767, ny * 32767, nz * 32767); - normals.push(nx * 32767, ny * 32767, nz * 32767); - - // colors - - const vx = x / n + 0.5; - const vy = y / n + 0.5; - const vz = z / n + 0.5; - - color.setRGB(vx, vy, vz); - - colors.push(color.r * 255, color.g * 255, color.b * 255); - colors.push(color.r * 255, color.g * 255, color.b * 255); - colors.push(color.r * 255, color.g * 255, color.b * 255); - } - - const positionAttribute = new THREE.Float32BufferAttribute(positions, 3); - const normalAttribute = new THREE.Int16BufferAttribute(normals, 3); - const colorAttribute = new THREE.Uint8BufferAttribute(colors, 3); - - normalAttribute.normalized = true; // this will map the buffer values to 0.0f - +1.0f in the shader - colorAttribute.normalized = true; - - geometry.setAttribute('position', positionAttribute); - geometry.setAttribute('normal', normalAttribute); - geometry.setAttribute('color', colorAttribute); - - geometry.computeBoundingSphere(); - - const material = new THREE.MeshPhongMaterial({ - color: 0xd5d5d5, - specular: 0xffffff, - shininess: 250, - side: THREE.DoubleSide, - vertexColors: true, - }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const time = Date.now() * 0.001; - - mesh.rotation.x = time * 0.25; - mesh.rotation.y = time * 0.5; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_camera.ts b/examples-testing/examples/webgl_camera.ts deleted file mode 100644 index f3d663603..000000000 --- a/examples-testing/examples/webgl_camera.ts +++ /dev/null @@ -1,218 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let SCREEN_WIDTH = window.innerWidth; -let SCREEN_HEIGHT = window.innerHeight; -let aspect = SCREEN_WIDTH / SCREEN_HEIGHT; - -let container, stats; -let camera, scene, renderer, mesh; -let cameraRig, activeCamera, activeHelper; -let cameraPerspective, cameraOrtho; -let cameraPerspectiveHelper, cameraOrthoHelper; -const frustumSize = 600; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - scene = new THREE.Scene(); - - // - - camera = new THREE.PerspectiveCamera(50, 0.5 * aspect, 1, 10000); - camera.position.z = 2500; - - cameraPerspective = new THREE.PerspectiveCamera(50, 0.5 * aspect, 150, 1000); - - cameraPerspectiveHelper = new THREE.CameraHelper(cameraPerspective); - scene.add(cameraPerspectiveHelper); - - // - cameraOrtho = new THREE.OrthographicCamera( - (0.5 * frustumSize * aspect) / -2, - (0.5 * frustumSize * aspect) / 2, - frustumSize / 2, - frustumSize / -2, - 150, - 1000, - ); - - cameraOrthoHelper = new THREE.CameraHelper(cameraOrtho); - scene.add(cameraOrthoHelper); - - // - - activeCamera = cameraPerspective; - activeHelper = cameraPerspectiveHelper; - - // counteract different front orientation of cameras vs rig - - cameraOrtho.rotation.y = Math.PI; - cameraPerspective.rotation.y = Math.PI; - - cameraRig = new THREE.Group(); - - cameraRig.add(cameraPerspective); - cameraRig.add(cameraOrtho); - - scene.add(cameraRig); - - // - - mesh = new THREE.Mesh( - new THREE.SphereGeometry(100, 16, 8), - new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: true }), - ); - scene.add(mesh); - - const mesh2 = new THREE.Mesh( - new THREE.SphereGeometry(50, 16, 8), - new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: true }), - ); - mesh2.position.y = 150; - mesh.add(mesh2); - - const mesh3 = new THREE.Mesh( - new THREE.SphereGeometry(5, 16, 8), - new THREE.MeshBasicMaterial({ color: 0x0000ff, wireframe: true }), - ); - mesh3.position.z = 150; - cameraRig.add(mesh3); - - // - - const geometry = new THREE.BufferGeometry(); - const vertices = []; - - for (let i = 0; i < 10000; i++) { - vertices.push(THREE.MathUtils.randFloatSpread(2000)); // x - vertices.push(THREE.MathUtils.randFloatSpread(2000)); // y - vertices.push(THREE.MathUtils.randFloatSpread(2000)); // z - } - - geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); - - const particles = new THREE.Points(geometry, new THREE.PointsMaterial({ color: 0x888888 })); - scene.add(particles); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - renderer.setScissorTest(true); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); - document.addEventListener('keydown', onKeyDown); -} - -// - -function onKeyDown(event) { - switch (event.keyCode) { - case 79 /*O*/: - activeCamera = cameraOrtho; - activeHelper = cameraOrthoHelper; - - break; - - case 80 /*P*/: - activeCamera = cameraPerspective; - activeHelper = cameraPerspectiveHelper; - - break; - } -} - -// - -function onWindowResize() { - SCREEN_WIDTH = window.innerWidth; - SCREEN_HEIGHT = window.innerHeight; - aspect = SCREEN_WIDTH / SCREEN_HEIGHT; - - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - - camera.aspect = 0.5 * aspect; - camera.updateProjectionMatrix(); - - cameraPerspective.aspect = 0.5 * aspect; - cameraPerspective.updateProjectionMatrix(); - - cameraOrtho.left = (-0.5 * frustumSize * aspect) / 2; - cameraOrtho.right = (0.5 * frustumSize * aspect) / 2; - cameraOrtho.top = frustumSize / 2; - cameraOrtho.bottom = -frustumSize / 2; - cameraOrtho.updateProjectionMatrix(); -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - const r = Date.now() * 0.0005; - - mesh.position.x = 700 * Math.cos(r); - mesh.position.z = 700 * Math.sin(r); - mesh.position.y = 700 * Math.sin(r); - - mesh.children[0].position.x = 70 * Math.cos(2 * r); - mesh.children[0].position.z = 70 * Math.sin(r); - - if (activeCamera === cameraPerspective) { - cameraPerspective.fov = 35 + 30 * Math.sin(0.5 * r); - cameraPerspective.far = mesh.position.length(); - cameraPerspective.updateProjectionMatrix(); - - cameraPerspectiveHelper.update(); - cameraPerspectiveHelper.visible = true; - - cameraOrthoHelper.visible = false; - } else { - cameraOrtho.far = mesh.position.length(); - cameraOrtho.updateProjectionMatrix(); - - cameraOrthoHelper.update(); - cameraOrthoHelper.visible = true; - - cameraPerspectiveHelper.visible = false; - } - - cameraRig.lookAt(mesh.position); - - // - - activeHelper.visible = false; - - renderer.setClearColor(0x000000, 1); - renderer.setScissor(0, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT); - renderer.setViewport(0, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT); - renderer.render(scene, activeCamera); - - // - - activeHelper.visible = true; - - renderer.setClearColor(0x111111, 1); - renderer.setScissor(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT); - renderer.setViewport(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT); - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_camera_array.ts b/examples-testing/examples/webgl_camera_array.ts deleted file mode 100644 index 8b10e27cb..000000000 --- a/examples-testing/examples/webgl_camera_array.ts +++ /dev/null @@ -1,104 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer; -let mesh; -const AMOUNT = 6; - -init(); - -function init() { - const ASPECT_RATIO = window.innerWidth / window.innerHeight; - - const WIDTH = (window.innerWidth / AMOUNT) * window.devicePixelRatio; - const HEIGHT = (window.innerHeight / AMOUNT) * window.devicePixelRatio; - - const cameras = []; - - for (let y = 0; y < AMOUNT; y++) { - for (let x = 0; x < AMOUNT; x++) { - const subcamera = new THREE.PerspectiveCamera(40, ASPECT_RATIO, 0.1, 10); - subcamera.viewport = new THREE.Vector4( - Math.floor(x * WIDTH), - Math.floor(y * HEIGHT), - Math.ceil(WIDTH), - Math.ceil(HEIGHT), - ); - subcamera.position.x = x / AMOUNT - 0.5; - subcamera.position.y = 0.5 - y / AMOUNT; - subcamera.position.z = 1.5; - subcamera.position.multiplyScalar(2); - subcamera.lookAt(0, 0, 0); - subcamera.updateMatrixWorld(); - cameras.push(subcamera); - } - } - - camera = new THREE.ArrayCamera(cameras); - camera.position.z = 3; - - scene = new THREE.Scene(); - - scene.add(new THREE.AmbientLight(0x999999)); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0.5, 0.5, 1); - light.castShadow = true; - light.shadow.camera.zoom = 4; // tighter shadow map - scene.add(light); - - const geometryBackground = new THREE.PlaneGeometry(100, 100); - const materialBackground = new THREE.MeshPhongMaterial({ color: 0x000066 }); - - const background = new THREE.Mesh(geometryBackground, materialBackground); - background.receiveShadow = true; - background.position.set(0, 0, -1); - scene.add(background); - - const geometryCylinder = new THREE.CylinderGeometry(0.5, 0.5, 1, 32); - const materialCylinder = new THREE.MeshPhongMaterial({ color: 0xff0000 }); - - mesh = new THREE.Mesh(geometryCylinder, materialCylinder); - mesh.castShadow = true; - mesh.receiveShadow = true; - scene.add(mesh); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - document.body.appendChild(renderer.domElement); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const ASPECT_RATIO = window.innerWidth / window.innerHeight; - const WIDTH = (window.innerWidth / AMOUNT) * window.devicePixelRatio; - const HEIGHT = (window.innerHeight / AMOUNT) * window.devicePixelRatio; - - camera.aspect = ASPECT_RATIO; - camera.updateProjectionMatrix(); - - for (let y = 0; y < AMOUNT; y++) { - for (let x = 0; x < AMOUNT; x++) { - const subcamera = camera.cameras[AMOUNT * y + x]; - - subcamera.viewport.set(Math.floor(x * WIDTH), Math.floor(y * HEIGHT), Math.ceil(WIDTH), Math.ceil(HEIGHT)); - - subcamera.aspect = ASPECT_RATIO; - subcamera.updateProjectionMatrix(); - } - } - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - mesh.rotation.x += 0.005; - mesh.rotation.z += 0.01; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_camera_logarithmicdepthbuffer.ts b/examples-testing/examples/webgl_camera_logarithmicdepthbuffer.ts deleted file mode 100644 index f1d440004..000000000 --- a/examples-testing/examples/webgl_camera_logarithmicdepthbuffer.ts +++ /dev/null @@ -1,248 +0,0 @@ -import * as THREE from 'three'; - -import { FontLoader } from 'three/addons/loaders/FontLoader.js'; -import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; - -import Stats from 'three/addons/libs/stats.module.js'; - -// 1 micrometer to 100 billion light years in one scene, with 1 unit = 1 meter? preposterous! and yet... -const NEAR = 1e-6, - FAR = 1e27; -let SCREEN_WIDTH = window.innerWidth; -let SCREEN_HEIGHT = window.innerHeight; -let screensplit = 0.25, - screensplit_right = 0; -const mouse = [0.5, 0.5]; -let zoompos = -100, - minzoomspeed = 0.015; -let zoomspeed = minzoomspeed; - -let container, border, stats; -const objects = {}; - -// Generate a number of text labels, from 1µm in size up to 100,000,000 light years -// Try to use some descriptive real-world examples of objects at each scale - -const labeldata = [ - { size: 0.01, scale: 0.0001, label: 'microscopic (1µm)' }, // FIXME - triangulating text fails at this size, so we scale instead - { size: 0.01, scale: 0.1, label: 'minuscule (1mm)' }, - { size: 0.01, scale: 1.0, label: 'tiny (1cm)' }, - { size: 1, scale: 1.0, label: 'child-sized (1m)' }, - { size: 10, scale: 1.0, label: 'tree-sized (10m)' }, - { size: 100, scale: 1.0, label: 'building-sized (100m)' }, - { size: 1000, scale: 1.0, label: 'medium (1km)' }, - { size: 10000, scale: 1.0, label: 'city-sized (10km)' }, - { size: 3400000, scale: 1.0, label: 'moon-sized (3,400 Km)' }, - { size: 12000000, scale: 1.0, label: 'planet-sized (12,000 km)' }, - { size: 1400000000, scale: 1.0, label: 'sun-sized (1,400,000 km)' }, - { size: 7.47e12, scale: 1.0, label: 'solar system-sized (50Au)' }, - { size: 9.4605284e15, scale: 1.0, label: 'gargantuan (1 light year)' }, - { size: 3.08567758e16, scale: 1.0, label: 'ludicrous (1 parsec)' }, - { size: 1e19, scale: 1.0, label: 'mind boggling (1000 light years)' }, -]; - -init(); - -function init() { - container = document.getElementById('container'); - - const loader = new FontLoader(); - loader.load('fonts/helvetiker_regular.typeface.json', function (font) { - const scene = initScene(font); - - // Initialize two copies of the same scene, one with normal z-buffer and one with logarithmic z-buffer - objects.normal = initView(scene, 'normal', false); - objects.logzbuf = initView(scene, 'logzbuf', true); - - animate(); - }); - - stats = new Stats(); - container.appendChild(stats.dom); - - // Resize border allows the user to easily compare effects of logarithmic depth buffer over the whole scene - border = document.getElementById('renderer_border'); - border.addEventListener('pointerdown', onBorderPointerDown); - - window.addEventListener('mousemove', onMouseMove); - window.addEventListener('resize', onWindowResize); - window.addEventListener('wheel', onMouseWheel); -} - -function initView(scene, name, logDepthBuf) { - const framecontainer = document.getElementById('container_' + name); - - const camera = new THREE.PerspectiveCamera(50, (screensplit * SCREEN_WIDTH) / SCREEN_HEIGHT, NEAR, FAR); - scene.add(camera); - - const renderer = new THREE.WebGLRenderer({ antialias: true, logarithmicDepthBuffer: logDepthBuf }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(SCREEN_WIDTH / 2, SCREEN_HEIGHT); - renderer.domElement.style.position = 'relative'; - renderer.domElement.id = 'renderer_' + name; - framecontainer.appendChild(renderer.domElement); - - return { container: framecontainer, renderer: renderer, scene: scene, camera: camera }; -} - -function initScene(font) { - const scene = new THREE.Scene(); - - scene.add(new THREE.AmbientLight(0x777777)); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(100, 100, 100); - scene.add(light); - - const materialargs = { - color: 0xffffff, - specular: 0x050505, - shininess: 50, - emissive: 0x000000, - }; - - const geometry = new THREE.SphereGeometry(0.5, 24, 12); - - for (let i = 0; i < labeldata.length; i++) { - const scale = labeldata[i].scale || 1; - - const labelgeo = new TextGeometry(labeldata[i].label, { - font: font, - size: labeldata[i].size, - depth: labeldata[i].size / 2, - }); - - labelgeo.computeBoundingSphere(); - - // center text - labelgeo.translate(-labelgeo.boundingSphere.radius, 0, 0); - - materialargs.color = new THREE.Color().setHSL(Math.random(), 0.5, 0.5); - - const material = new THREE.MeshPhongMaterial(materialargs); - - const group = new THREE.Group(); - group.position.z = -labeldata[i].size * scale; - scene.add(group); - - const textmesh = new THREE.Mesh(labelgeo, material); - textmesh.scale.set(scale, scale, scale); - textmesh.position.z = -labeldata[i].size * scale; - textmesh.position.y = (labeldata[i].size / 4) * scale; - group.add(textmesh); - - const dotmesh = new THREE.Mesh(geometry, material); - dotmesh.position.y = (-labeldata[i].size / 4) * scale; - dotmesh.scale.multiplyScalar(labeldata[i].size * scale); - group.add(dotmesh); - } - - return scene; -} - -function updateRendererSizes() { - // Recalculate size for both renderers when screen size or split location changes - - SCREEN_WIDTH = window.innerWidth; - SCREEN_HEIGHT = window.innerHeight; - - screensplit_right = 1 - screensplit; - - objects.normal.renderer.setSize(screensplit * SCREEN_WIDTH, SCREEN_HEIGHT); - objects.normal.camera.aspect = (screensplit * SCREEN_WIDTH) / SCREEN_HEIGHT; - objects.normal.camera.updateProjectionMatrix(); - objects.normal.camera.setViewOffset(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, SCREEN_WIDTH * screensplit, SCREEN_HEIGHT); - objects.normal.container.style.width = screensplit * 100 + '%'; - - objects.logzbuf.renderer.setSize(screensplit_right * SCREEN_WIDTH, SCREEN_HEIGHT); - objects.logzbuf.camera.aspect = (screensplit_right * SCREEN_WIDTH) / SCREEN_HEIGHT; - objects.logzbuf.camera.updateProjectionMatrix(); - objects.logzbuf.camera.setViewOffset( - SCREEN_WIDTH, - SCREEN_HEIGHT, - SCREEN_WIDTH * screensplit, - 0, - SCREEN_WIDTH * screensplit_right, - SCREEN_HEIGHT, - ); - objects.logzbuf.container.style.width = screensplit_right * 100 + '%'; - - border.style.left = screensplit * 100 + '%'; -} - -function animate() { - requestAnimationFrame(animate); - render(); -} - -function render() { - // Put some limits on zooming - const minzoom = labeldata[0].size * labeldata[0].scale * 1; - const maxzoom = labeldata[labeldata.length - 1].size * labeldata[labeldata.length - 1].scale * 100; - let damping = Math.abs(zoomspeed) > minzoomspeed ? 0.95 : 1.0; - - // Zoom out faster the further out you go - const zoom = THREE.MathUtils.clamp(Math.pow(Math.E, zoompos), minzoom, maxzoom); - zoompos = Math.log(zoom); - - // Slow down quickly at the zoom limits - if ((zoom == minzoom && zoomspeed < 0) || (zoom == maxzoom && zoomspeed > 0)) { - damping = 0.85; - } - - zoompos += zoomspeed; - zoomspeed *= damping; - - objects.normal.camera.position.x = Math.sin(0.5 * Math.PI * (mouse[0] - 0.5)) * zoom; - objects.normal.camera.position.y = Math.sin(0.25 * Math.PI * (mouse[1] - 0.5)) * zoom; - objects.normal.camera.position.z = Math.cos(0.5 * Math.PI * (mouse[0] - 0.5)) * zoom; - objects.normal.camera.lookAt(objects.normal.scene.position); - - // Clone camera settings across both scenes - objects.logzbuf.camera.position.copy(objects.normal.camera.position); - objects.logzbuf.camera.quaternion.copy(objects.normal.camera.quaternion); - - // Update renderer sizes if the split has changed - if (screensplit_right != 1 - screensplit) { - updateRendererSizes(); - } - - objects.normal.renderer.render(objects.normal.scene, objects.normal.camera); - objects.logzbuf.renderer.render(objects.logzbuf.scene, objects.logzbuf.camera); - - stats.update(); -} - -function onWindowResize() { - updateRendererSizes(); -} - -function onBorderPointerDown() { - // activate draggable window resizing bar - window.addEventListener('pointermove', onBorderPointerMove); - window.addEventListener('pointerup', onBorderPointerUp); -} - -function onBorderPointerMove(ev) { - screensplit = Math.max(0, Math.min(1, ev.clientX / window.innerWidth)); -} - -function onBorderPointerUp() { - window.removeEventListener('pointermove', onBorderPointerMove); - window.removeEventListener('pointerup', onBorderPointerUp); -} - -function onMouseMove(ev) { - mouse[0] = ev.clientX / window.innerWidth; - mouse[1] = ev.clientY / window.innerHeight; -} - -function onMouseWheel(ev) { - const amount = ev.deltaY; - if (amount === 0) return; - const dir = amount / Math.abs(amount); - zoomspeed = dir / 10; - - // Slow down default zoom speed after user starts zooming, to give them more control - minzoomspeed = 0.001; -} diff --git a/examples-testing/examples/webgl_clipculldistance.ts b/examples-testing/examples/webgl_clipculldistance.ts deleted file mode 100644 index a5fb54d47..000000000 --- a/examples-testing/examples/webgl_clipculldistance.ts +++ /dev/null @@ -1,110 +0,0 @@ -import * as THREE from 'three'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import Stats from 'three/addons/libs/stats.module.js'; - -let camera, controls, clock, scene, renderer, stats; - -let material; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10); - camera.position.z = 2; - - scene = new THREE.Scene(); - - clock = new THREE.Clock(); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - if (renderer.extensions.has('WEBGL_clip_cull_distance') === false) { - document.getElementById('notSupported').style.display = ''; - return; - } - - const ext = renderer.getContext().getExtension('WEBGL_clip_cull_distance'); - const gl = renderer.getContext(); - - gl.enable(ext.CLIP_DISTANCE0_WEBGL); - - // geometry - - const vertexCount = 200 * 3; - - const geometry = new THREE.BufferGeometry(); - - const positions = []; - const colors = []; - - for (let i = 0; i < vertexCount; i++) { - // adding x,y,z - positions.push(Math.random() - 0.5); - positions.push(Math.random() - 0.5); - positions.push(Math.random() - 0.5); - - // adding r,g,b,a - colors.push(Math.random() * 255); - colors.push(Math.random() * 255); - colors.push(Math.random() * 255); - colors.push(Math.random() * 255); - } - - const positionAttribute = new THREE.Float32BufferAttribute(positions, 3); - const colorAttribute = new THREE.Uint8BufferAttribute(colors, 4); - colorAttribute.normalized = true; - - geometry.setAttribute('position', positionAttribute); - geometry.setAttribute('color', colorAttribute); - - // material - - material = new THREE.ShaderMaterial({ - uniforms: { - time: { value: 1.0 }, - }, - vertexShader: document.getElementById('vertexShader').textContent, - fragmentShader: document.getElementById('fragmentShader').textContent, - side: THREE.DoubleSide, - transparent: true, - vertexColors: true, - }); - - material.extensions.clipCullDistance = true; - - const mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - controls = new OrbitControls(camera, renderer.domElement); - - // - - stats = new Stats(); - document.body.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - controls.update(); - stats.update(); - - material.uniforms.time.value = clock.getElapsedTime(); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_clipping.ts b/examples-testing/examples/webgl_clipping.ts deleted file mode 100644 index cde10c7d1..000000000 --- a/examples-testing/examples/webgl_clipping.ts +++ /dev/null @@ -1,195 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer, startTime, object, stats; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(36, window.innerWidth / window.innerHeight, 0.25, 16); - - camera.position.set(0, 1.3, 3); - - scene = new THREE.Scene(); - - // Lights - - scene.add(new THREE.AmbientLight(0xcccccc)); - - const spotLight = new THREE.SpotLight(0xffffff, 60); - spotLight.angle = Math.PI / 5; - spotLight.penumbra = 0.2; - spotLight.position.set(2, 3, 3); - spotLight.castShadow = true; - spotLight.shadow.camera.near = 3; - spotLight.shadow.camera.far = 10; - spotLight.shadow.mapSize.width = 1024; - spotLight.shadow.mapSize.height = 1024; - scene.add(spotLight); - - const dirLight = new THREE.DirectionalLight(0x55505a, 3); - dirLight.position.set(0, 3, 0); - dirLight.castShadow = true; - dirLight.shadow.camera.near = 1; - dirLight.shadow.camera.far = 10; - - dirLight.shadow.camera.right = 1; - dirLight.shadow.camera.left = -1; - dirLight.shadow.camera.top = 1; - dirLight.shadow.camera.bottom = -1; - - dirLight.shadow.mapSize.width = 1024; - dirLight.shadow.mapSize.height = 1024; - scene.add(dirLight); - - // ***** Clipping planes: ***** - - const localPlane = new THREE.Plane(new THREE.Vector3(0, -1, 0), 0.8); - const globalPlane = new THREE.Plane(new THREE.Vector3(-1, 0, 0), 0.1); - - // Geometry - - const material = new THREE.MeshPhongMaterial({ - color: 0x80ee10, - shininess: 100, - side: THREE.DoubleSide, - - // ***** Clipping setup (material): ***** - clippingPlanes: [localPlane], - clipShadows: true, - - alphaToCoverage: true, - }); - - const geometry = new THREE.TorusKnotGeometry(0.4, 0.08, 95, 20); - - object = new THREE.Mesh(geometry, material); - object.castShadow = true; - scene.add(object); - - const ground = new THREE.Mesh( - new THREE.PlaneGeometry(9, 9, 1, 1), - new THREE.MeshPhongMaterial({ color: 0xa0adaf, shininess: 150 }), - ); - - ground.rotation.x = -Math.PI / 2; // rotates X/Y to X/Z - ground.receiveShadow = true; - scene.add(ground); - - // Renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - document.body.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); - - // ***** Clipping setup (renderer): ***** - const globalPlanes = [globalPlane], - Empty = Object.freeze([]); - renderer.clippingPlanes = Empty; // GUI sets it to globalPlanes - renderer.localClippingEnabled = true; - - // Stats - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // Controls - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 1, 0); - controls.update(); - - // GUI - - const gui = new GUI(), - props = { - alphaToCoverage: true, - }, - folderLocal = gui.addFolder('Local Clipping'), - propsLocal = { - get Enabled() { - return renderer.localClippingEnabled; - }, - set Enabled(v) { - renderer.localClippingEnabled = v; - }, - - get Shadows() { - return material.clipShadows; - }, - set Shadows(v) { - material.clipShadows = v; - }, - - get Plane() { - return localPlane.constant; - }, - set Plane(v) { - localPlane.constant = v; - }, - }, - folderGlobal = gui.addFolder('Global Clipping'), - propsGlobal = { - get Enabled() { - return renderer.clippingPlanes !== Empty; - }, - set Enabled(v) { - renderer.clippingPlanes = v ? globalPlanes : Empty; - }, - - get Plane() { - return globalPlane.constant; - }, - set Plane(v) { - globalPlane.constant = v; - }, - }; - - gui.add(props, 'alphaToCoverage').onChange(function (value) { - ground.material.alphaToCoverage = value; - ground.material.needsUpdate = true; - - material.alphaToCoverage = value; - material.needsUpdate = true; - }); - folderLocal.add(propsLocal, 'Enabled'); - folderLocal.add(propsLocal, 'Shadows'); - folderLocal.add(propsLocal, 'Plane', 0.3, 1.25); - - folderGlobal.add(propsGlobal, 'Enabled'); - folderGlobal.add(propsGlobal, 'Plane', -0.4, 3); - - // Start - - startTime = Date.now(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const currentTime = Date.now(); - const time = (currentTime - startTime) / 1000; - - object.position.y = 0.8; - object.rotation.x = time * 0.5; - object.rotation.y = time * 0.2; - object.scale.setScalar(Math.cos(time) * 0.125 + 0.875); - - stats.begin(); - renderer.render(scene, camera); - stats.end(); -} diff --git a/examples-testing/examples/webgl_clipping_advanced.ts b/examples-testing/examples/webgl_clipping_advanced.ts deleted file mode 100644 index 614d710da..000000000 --- a/examples-testing/examples/webgl_clipping_advanced.ts +++ /dev/null @@ -1,355 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -function planesFromMesh(vertices, indices) { - // creates a clipping volume from a convex triangular mesh - // specified by the arrays 'vertices' and 'indices' - - const n = indices.length / 3, - result = new Array(n); - - for (let i = 0, j = 0; i < n; ++i, j += 3) { - const a = vertices[indices[j]], - b = vertices[indices[j + 1]], - c = vertices[indices[j + 2]]; - - result[i] = new THREE.Plane().setFromCoplanarPoints(a, b, c); - } - - return result; -} - -function createPlanes(n) { - // creates an array of n uninitialized plane objects - - const result = new Array(n); - - for (let i = 0; i !== n; ++i) result[i] = new THREE.Plane(); - - return result; -} - -function assignTransformedPlanes(planesOut, planesIn, matrix) { - // sets an array of existing planes to transformed 'planesIn' - - for (let i = 0, n = planesIn.length; i !== n; ++i) planesOut[i].copy(planesIn[i]).applyMatrix4(matrix); -} - -function cylindricalPlanes(n, innerRadius) { - const result = createPlanes(n); - - for (let i = 0; i !== n; ++i) { - const plane = result[i], - angle = (i * Math.PI * 2) / n; - - plane.normal.set(Math.cos(angle), 0, Math.sin(angle)); - - plane.constant = innerRadius; - } - - return result; -} - -const planeToMatrix = (function () { - // creates a matrix that aligns X/Y to a given plane - - // temporaries: - const xAxis = new THREE.Vector3(), - yAxis = new THREE.Vector3(), - trans = new THREE.Vector3(); - - return function planeToMatrix(plane) { - const zAxis = plane.normal, - matrix = new THREE.Matrix4(); - - // Hughes & Moeller '99 - // "Building an Orthonormal Basis from a Unit Vector." - - if (Math.abs(zAxis.x) > Math.abs(zAxis.z)) { - yAxis.set(-zAxis.y, zAxis.x, 0); - } else { - yAxis.set(0, -zAxis.z, zAxis.y); - } - - xAxis.crossVectors(yAxis.normalize(), zAxis); - - plane.coplanarPoint(trans); - return matrix.set( - xAxis.x, - yAxis.x, - zAxis.x, - trans.x, - xAxis.y, - yAxis.y, - zAxis.y, - trans.y, - xAxis.z, - yAxis.z, - zAxis.z, - trans.z, - 0, - 0, - 0, - 1, - ); - }; -})(); - -// A regular tetrahedron for the clipping volume: - -const Vertices = [ - new THREE.Vector3(+1, 0, +Math.SQRT1_2), - new THREE.Vector3(-1, 0, +Math.SQRT1_2), - new THREE.Vector3(0, +1, -Math.SQRT1_2), - new THREE.Vector3(0, -1, -Math.SQRT1_2), - ], - Indices = [0, 1, 2, 0, 2, 3, 0, 3, 1, 1, 3, 2], - Planes = planesFromMesh(Vertices, Indices), - PlaneMatrices = Planes.map(planeToMatrix), - GlobalClippingPlanes = cylindricalPlanes(5, 2.5), - Empty = Object.freeze([]); - -let camera, scene, renderer, startTime, stats, object, clipMaterial, volumeVisualization, globalClippingPlanes; - -function init() { - camera = new THREE.PerspectiveCamera(36, window.innerWidth / window.innerHeight, 0.25, 16); - - camera.position.set(0, 1.5, 3); - - scene = new THREE.Scene(); - - // Lights - - scene.add(new THREE.AmbientLight(0xffffff)); - - const spotLight = new THREE.SpotLight(0xffffff, 60); - spotLight.angle = Math.PI / 5; - spotLight.penumbra = 0.2; - spotLight.position.set(2, 3, 3); - spotLight.castShadow = true; - spotLight.shadow.camera.near = 3; - spotLight.shadow.camera.far = 10; - spotLight.shadow.mapSize.width = 1024; - spotLight.shadow.mapSize.height = 1024; - scene.add(spotLight); - - const dirLight = new THREE.DirectionalLight(0xffffff, 1.5); - dirLight.position.set(0, 2, 0); - dirLight.castShadow = true; - dirLight.shadow.camera.near = 1; - dirLight.shadow.camera.far = 10; - - dirLight.shadow.camera.right = 1; - dirLight.shadow.camera.left = -1; - dirLight.shadow.camera.top = 1; - dirLight.shadow.camera.bottom = -1; - - dirLight.shadow.mapSize.width = 1024; - dirLight.shadow.mapSize.height = 1024; - scene.add(dirLight); - - // Geometry - - clipMaterial = new THREE.MeshPhongMaterial({ - color: 0xee0a10, - shininess: 100, - side: THREE.DoubleSide, - // Clipping setup: - clippingPlanes: createPlanes(Planes.length), - clipShadows: true, - }); - - object = new THREE.Group(); - - const geometry = new THREE.BoxGeometry(0.18, 0.18, 0.18); - - for (let z = -2; z <= 2; ++z) - for (let y = -2; y <= 2; ++y) - for (let x = -2; x <= 2; ++x) { - const mesh = new THREE.Mesh(geometry, clipMaterial); - mesh.position.set(x / 5, y / 5, z / 5); - mesh.castShadow = true; - object.add(mesh); - } - - scene.add(object); - - const planeGeometry = new THREE.PlaneGeometry(3, 3, 1, 1), - color = new THREE.Color(); - - volumeVisualization = new THREE.Group(); - volumeVisualization.visible = false; - - for (let i = 0, n = Planes.length; i !== n; ++i) { - const material = new THREE.MeshBasicMaterial({ - color: color.setHSL(i / n, 0.5, 0.5).getHex(), - side: THREE.DoubleSide, - - opacity: 0.2, - transparent: true, - - // clip to the others to show the volume (wildly - // intersecting transparent planes look bad) - clippingPlanes: clipMaterial.clippingPlanes.filter(function (_, j) { - return j !== i; - }), - - // no need to enable shadow clipping - the plane - // visualization does not cast shadows - }); - - const mesh = new THREE.Mesh(planeGeometry, material); - mesh.matrixAutoUpdate = false; - - volumeVisualization.add(mesh); - } - - scene.add(volumeVisualization); - - const ground = new THREE.Mesh( - planeGeometry, - new THREE.MeshPhongMaterial({ - color: 0xa0adaf, - shininess: 10, - }), - ); - ground.rotation.x = -Math.PI / 2; - ground.scale.multiplyScalar(3); - ground.receiveShadow = true; - scene.add(ground); - - // Renderer - - const container = document.body; - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - container.appendChild(renderer.domElement); - // Clipping setup: - globalClippingPlanes = createPlanes(GlobalClippingPlanes.length); - renderer.clippingPlanes = Empty; - renderer.localClippingEnabled = true; - - window.addEventListener('resize', onWindowResize); - - // Stats - - stats = new Stats(); - container.appendChild(stats.dom); - - // Controls - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 1; - controls.maxDistance = 8; - controls.target.set(0, 1, 0); - controls.update(); - - // GUI - - const gui = new GUI(), - folder = gui.addFolder('Local Clipping'), - props = { - get Enabled() { - return renderer.localClippingEnabled; - }, - set Enabled(v) { - renderer.localClippingEnabled = v; - if (!v) volumeVisualization.visible = false; - }, - - get Shadows() { - return clipMaterial.clipShadows; - }, - set Shadows(v) { - clipMaterial.clipShadows = v; - }, - - get Visualize() { - return volumeVisualization.visible; - }, - set Visualize(v) { - if (renderer.localClippingEnabled) volumeVisualization.visible = v; - }, - }; - - folder.add(props, 'Enabled'); - folder.add(props, 'Shadows'); - folder.add(props, 'Visualize').listen(); - - gui.addFolder('Global Clipping').add( - { - get Enabled() { - return renderer.clippingPlanes !== Empty; - }, - set Enabled(v) { - renderer.clippingPlanes = v ? globalClippingPlanes : Empty; - }, - }, - 'Enabled', - ); - - // Start - - startTime = Date.now(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function setObjectWorldMatrix(object, matrix) { - // set the orientation of an object based on a world matrix - - const parent = object.parent; - scene.updateMatrixWorld(); - object.matrix.copy(parent.matrixWorld).invert(); - object.applyMatrix4(matrix); -} - -const transform = new THREE.Matrix4(), - tmpMatrix = new THREE.Matrix4(); - -function animate() { - const currentTime = Date.now(), - time = (currentTime - startTime) / 1000; - - object.position.y = 1; - object.rotation.x = time * 0.5; - object.rotation.y = time * 0.2; - - object.updateMatrix(); - transform.copy(object.matrix); - - const bouncy = Math.cos(time * 0.5) * 0.5 + 0.7; - transform.multiply(tmpMatrix.makeScale(bouncy, bouncy, bouncy)); - - assignTransformedPlanes(clipMaterial.clippingPlanes, Planes, transform); - - const planeMeshes = volumeVisualization.children; - - for (let i = 0, n = planeMeshes.length; i !== n; ++i) { - tmpMatrix.multiplyMatrices(transform, PlaneMatrices[i]); - setObjectWorldMatrix(planeMeshes[i], tmpMatrix); - } - - transform.makeRotationY(time * 0.1); - - assignTransformedPlanes(globalClippingPlanes, GlobalClippingPlanes, transform); - - stats.begin(); - renderer.render(scene, camera); - stats.end(); -} - -init(); diff --git a/examples-testing/examples/webgl_clipping_intersection.ts b/examples-testing/examples/webgl_clipping_intersection.ts deleted file mode 100644 index 5f45e45df..000000000 --- a/examples-testing/examples/webgl_clipping_intersection.ts +++ /dev/null @@ -1,137 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer; - -const params = { - clipIntersection: true, - planeConstant: 0, - showHelpers: false, - alphaToCoverage: true, -}; - -const clipPlanes = [ - new THREE.Plane(new THREE.Vector3(1, 0, 0), 0), - new THREE.Plane(new THREE.Vector3(0, -1, 0), 0), - new THREE.Plane(new THREE.Vector3(0, 0, -1), 0), -]; - -init(); -render(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.localClippingEnabled = true; - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 200); - - camera.position.set(-1.5, 2.5, 3.0); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use only if there is no animation loop - controls.minDistance = 1; - controls.maxDistance = 10; - controls.enablePan = false; - - const light = new THREE.HemisphereLight(0xffffff, 0x080808, 4.5); - light.position.set(-1.25, 1, 1.25); - scene.add(light); - - // - - const group = new THREE.Group(); - - for (let i = 1; i <= 30; i += 2) { - const geometry = new THREE.SphereGeometry(i / 30, 48, 24); - - const material = new THREE.MeshPhongMaterial({ - color: new THREE.Color().setHSL(Math.random(), 0.5, 0.5, THREE.SRGBColorSpace), - side: THREE.DoubleSide, - clippingPlanes: clipPlanes, - clipIntersection: params.clipIntersection, - alphaToCoverage: true, - }); - - group.add(new THREE.Mesh(geometry, material)); - } - - scene.add(group); - - // helpers - - const helpers = new THREE.Group(); - helpers.add(new THREE.PlaneHelper(clipPlanes[0], 2, 0xff0000)); - helpers.add(new THREE.PlaneHelper(clipPlanes[1], 2, 0x00ff00)); - helpers.add(new THREE.PlaneHelper(clipPlanes[2], 2, 0x0000ff)); - helpers.visible = false; - scene.add(helpers); - - // gui - - const gui = new GUI(); - - gui.add(params, 'alphaToCoverage').onChange(function (value) { - group.children.forEach(c => { - c.material.alphaToCoverage = Boolean(value); - c.material.needsUpdate = true; - }); - - render(); - }); - - gui.add(params, 'clipIntersection') - .name('clip intersection') - .onChange(function (value) { - const children = group.children; - - for (let i = 0; i < children.length; i++) { - children[i].material.clipIntersection = value; - } - - render(); - }); - - gui.add(params, 'planeConstant', -1, 1) - .step(0.01) - .name('plane constant') - .onChange(function (value) { - for (let j = 0; j < clipPlanes.length; j++) { - clipPlanes[j].constant = value; - } - - render(); - }); - - gui.add(params, 'showHelpers') - .name('show helpers') - .onChange(function (value) { - helpers.visible = value; - - render(); - }); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_clipping_stencil.ts b/examples-testing/examples/webgl_clipping_stencil.ts deleted file mode 100644 index ecb6b42b8..000000000 --- a/examples-testing/examples/webgl_clipping_stencil.ts +++ /dev/null @@ -1,260 +0,0 @@ -import * as THREE from 'three'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import Stats from 'three/addons/libs/stats.module.js'; - -let camera, scene, renderer, object, stats; -let planes, planeObjects, planeHelpers; -let clock; - -const params = { - animate: true, - planeX: { - constant: 0, - negated: false, - displayHelper: false, - }, - planeY: { - constant: 0, - negated: false, - displayHelper: false, - }, - planeZ: { - constant: 0, - negated: false, - displayHelper: false, - }, -}; - -init(); - -function createPlaneStencilGroup(geometry, plane, renderOrder) { - const group = new THREE.Group(); - const baseMat = new THREE.MeshBasicMaterial(); - baseMat.depthWrite = false; - baseMat.depthTest = false; - baseMat.colorWrite = false; - baseMat.stencilWrite = true; - baseMat.stencilFunc = THREE.AlwaysStencilFunc; - - // back faces - const mat0 = baseMat.clone(); - mat0.side = THREE.BackSide; - mat0.clippingPlanes = [plane]; - mat0.stencilFail = THREE.IncrementWrapStencilOp; - mat0.stencilZFail = THREE.IncrementWrapStencilOp; - mat0.stencilZPass = THREE.IncrementWrapStencilOp; - - const mesh0 = new THREE.Mesh(geometry, mat0); - mesh0.renderOrder = renderOrder; - group.add(mesh0); - - // front faces - const mat1 = baseMat.clone(); - mat1.side = THREE.FrontSide; - mat1.clippingPlanes = [plane]; - mat1.stencilFail = THREE.DecrementWrapStencilOp; - mat1.stencilZFail = THREE.DecrementWrapStencilOp; - mat1.stencilZPass = THREE.DecrementWrapStencilOp; - - const mesh1 = new THREE.Mesh(geometry, mat1); - mesh1.renderOrder = renderOrder; - - group.add(mesh1); - - return group; -} - -function init() { - clock = new THREE.Clock(); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(36, window.innerWidth / window.innerHeight, 1, 100); - camera.position.set(2, 2, 2); - - scene.add(new THREE.AmbientLight(0xffffff, 1.5)); - - const dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.position.set(5, 10, 7.5); - dirLight.castShadow = true; - dirLight.shadow.camera.right = 2; - dirLight.shadow.camera.left = -2; - dirLight.shadow.camera.top = 2; - dirLight.shadow.camera.bottom = -2; - - dirLight.shadow.mapSize.width = 1024; - dirLight.shadow.mapSize.height = 1024; - scene.add(dirLight); - - planes = [ - new THREE.Plane(new THREE.Vector3(-1, 0, 0), 0), - new THREE.Plane(new THREE.Vector3(0, -1, 0), 0), - new THREE.Plane(new THREE.Vector3(0, 0, -1), 0), - ]; - - planeHelpers = planes.map(p => new THREE.PlaneHelper(p, 2, 0xffffff)); - planeHelpers.forEach(ph => { - ph.visible = false; - scene.add(ph); - }); - - const geometry = new THREE.TorusKnotGeometry(0.4, 0.15, 220, 60); - object = new THREE.Group(); - scene.add(object); - - // Set up clip plane rendering - planeObjects = []; - const planeGeom = new THREE.PlaneGeometry(4, 4); - - for (let i = 0; i < 3; i++) { - const poGroup = new THREE.Group(); - const plane = planes[i]; - const stencilGroup = createPlaneStencilGroup(geometry, plane, i + 1); - - // plane is clipped by the other clipping planes - const planeMat = new THREE.MeshStandardMaterial({ - color: 0xe91e63, - metalness: 0.1, - roughness: 0.75, - clippingPlanes: planes.filter(p => p !== plane), - - stencilWrite: true, - stencilRef: 0, - stencilFunc: THREE.NotEqualStencilFunc, - stencilFail: THREE.ReplaceStencilOp, - stencilZFail: THREE.ReplaceStencilOp, - stencilZPass: THREE.ReplaceStencilOp, - }); - const po = new THREE.Mesh(planeGeom, planeMat); - po.onAfterRender = function (renderer) { - renderer.clearStencil(); - }; - - po.renderOrder = i + 1.1; - - object.add(stencilGroup); - poGroup.add(po); - planeObjects.push(po); - scene.add(poGroup); - } - - const material = new THREE.MeshStandardMaterial({ - color: 0xffc107, - metalness: 0.1, - roughness: 0.75, - clippingPlanes: planes, - clipShadows: true, - shadowSide: THREE.DoubleSide, - }); - - // add the color - const clippedColorFront = new THREE.Mesh(geometry, material); - clippedColorFront.castShadow = true; - clippedColorFront.renderOrder = 6; - object.add(clippedColorFront); - - const ground = new THREE.Mesh( - new THREE.PlaneGeometry(9, 9, 1, 1), - new THREE.ShadowMaterial({ color: 0x000000, opacity: 0.25, side: THREE.DoubleSide }), - ); - - ground.rotation.x = -Math.PI / 2; // rotates X/Y to X/Z - ground.position.y = -1; - ground.receiveShadow = true; - scene.add(ground); - - // Renderer - renderer = new THREE.WebGLRenderer({ antialias: true, stencil: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setClearColor(0x263238); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - renderer.localClippingEnabled = true; - document.body.appendChild(renderer.domElement); - - // Stats - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); - - // Controls - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 2; - controls.maxDistance = 20; - controls.update(); - - // GUI - const gui = new GUI(); - gui.add(params, 'animate'); - - const planeX = gui.addFolder('planeX'); - planeX.add(params.planeX, 'displayHelper').onChange(v => (planeHelpers[0].visible = v)); - planeX - .add(params.planeX, 'constant') - .min(-1) - .max(1) - .onChange(d => (planes[0].constant = d)); - planeX.add(params.planeX, 'negated').onChange(() => { - planes[0].negate(); - params.planeX.constant = planes[0].constant; - }); - planeX.open(); - - const planeY = gui.addFolder('planeY'); - planeY.add(params.planeY, 'displayHelper').onChange(v => (planeHelpers[1].visible = v)); - planeY - .add(params.planeY, 'constant') - .min(-1) - .max(1) - .onChange(d => (planes[1].constant = d)); - planeY.add(params.planeY, 'negated').onChange(() => { - planes[1].negate(); - params.planeY.constant = planes[1].constant; - }); - planeY.open(); - - const planeZ = gui.addFolder('planeZ'); - planeZ.add(params.planeZ, 'displayHelper').onChange(v => (planeHelpers[2].visible = v)); - planeZ - .add(params.planeZ, 'constant') - .min(-1) - .max(1) - .onChange(d => (planes[2].constant = d)); - planeZ.add(params.planeZ, 'negated').onChange(() => { - planes[2].negate(); - params.planeZ.constant = planes[2].constant; - }); - planeZ.open(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const delta = clock.getDelta(); - - if (params.animate) { - object.rotation.x += delta * 0.5; - object.rotation.y += delta * 0.2; - } - - for (let i = 0; i < planeObjects.length; i++) { - const plane = planes[i]; - const po = planeObjects[i]; - plane.coplanarPoint(po.position); - po.lookAt(po.position.x - plane.normal.x, po.position.y - plane.normal.y, po.position.z - plane.normal.z); - } - - stats.begin(); - renderer.render(scene, camera); - stats.end(); -} diff --git a/examples-testing/examples/webgl_custom_attributes.ts b/examples-testing/examples/webgl_custom_attributes.ts deleted file mode 100644 index 0dc897748..000000000 --- a/examples-testing/examples/webgl_custom_attributes.ts +++ /dev/null @@ -1,100 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let renderer, scene, camera, stats; - -let sphere, uniforms; - -let displacement, noise; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 300; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x050505); - - uniforms = { - amplitude: { value: 1.0 }, - color: { value: new THREE.Color(0xff2200) }, - colorTexture: { value: new THREE.TextureLoader().load('textures/water.jpg') }, - }; - - uniforms['colorTexture'].value.wrapS = uniforms['colorTexture'].value.wrapT = THREE.RepeatWrapping; - - const shaderMaterial = new THREE.ShaderMaterial({ - uniforms: uniforms, - vertexShader: document.getElementById('vertexshader').textContent, - fragmentShader: document.getElementById('fragmentshader').textContent, - }); - - const radius = 50, - segments = 128, - rings = 64; - - const geometry = new THREE.SphereGeometry(radius, segments, rings); - - displacement = new Float32Array(geometry.attributes.position.count); - noise = new Float32Array(geometry.attributes.position.count); - - for (let i = 0; i < displacement.length; i++) { - noise[i] = Math.random() * 5; - } - - geometry.setAttribute('displacement', new THREE.BufferAttribute(displacement, 1)); - - sphere = new THREE.Mesh(geometry, shaderMaterial); - scene.add(sphere); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - - const container = document.getElementById('container'); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - const time = Date.now() * 0.01; - - sphere.rotation.y = sphere.rotation.z = 0.01 * time; - - uniforms['amplitude'].value = 2.5 * Math.sin(sphere.rotation.y * 0.125); - uniforms['color'].value.offsetHSL(0.0005, 0, 0); - - for (let i = 0; i < displacement.length; i++) { - displacement[i] = Math.sin(0.1 * i + time); - - noise[i] += 0.5 * (0.5 - Math.random()); - noise[i] = THREE.MathUtils.clamp(noise[i], -5, 5); - - displacement[i] += noise[i]; - } - - sphere.geometry.attributes.displacement.needsUpdate = true; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_custom_attributes_lines.ts b/examples-testing/examples/webgl_custom_attributes_lines.ts deleted file mode 100644 index 3e2454e92..000000000 --- a/examples-testing/examples/webgl_custom_attributes_lines.ts +++ /dev/null @@ -1,121 +0,0 @@ -import * as THREE from 'three'; - -import { FontLoader } from 'three/addons/loaders/FontLoader.js'; -import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let renderer, scene, camera, stats; - -let line, uniforms; - -const loader = new FontLoader(); -loader.load('fonts/helvetiker_bold.typeface.json', function (font) { - init(font); -}); - -function init(font) { - camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 400; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x050505); - - uniforms = { - amplitude: { value: 5.0 }, - opacity: { value: 0.3 }, - color: { value: new THREE.Color(0xffffff) }, - }; - - const shaderMaterial = new THREE.ShaderMaterial({ - uniforms: uniforms, - vertexShader: document.getElementById('vertexshader').textContent, - fragmentShader: document.getElementById('fragmentshader').textContent, - blending: THREE.AdditiveBlending, - depthTest: false, - transparent: true, - }); - - const geometry = new TextGeometry('three.js', { - font: font, - - size: 50, - depth: 15, - curveSegments: 10, - - bevelThickness: 5, - bevelSize: 1.5, - bevelEnabled: true, - bevelSegments: 10, - }); - - geometry.center(); - - const count = geometry.attributes.position.count; - - const displacement = new THREE.Float32BufferAttribute(count * 3, 3); - geometry.setAttribute('displacement', displacement); - - const customColor = new THREE.Float32BufferAttribute(count * 3, 3); - geometry.setAttribute('customColor', customColor); - - const color = new THREE.Color(0xffffff); - - for (let i = 0, l = customColor.count; i < l; i++) { - color.setHSL(i / l, 0.5, 0.5); - color.toArray(customColor.array, i * customColor.itemSize); - } - - line = new THREE.Line(geometry, shaderMaterial); - line.rotation.x = 0.2; - scene.add(line); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - - const container = document.getElementById('container'); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - const time = Date.now() * 0.001; - - line.rotation.y = 0.25 * time; - - uniforms.amplitude.value = Math.sin(0.5 * time); - uniforms.color.value.offsetHSL(0.0005, 0, 0); - - const attributes = line.geometry.attributes; - const array = attributes.displacement.array; - - for (let i = 0, l = array.length; i < l; i += 3) { - array[i] += 0.3 * (0.5 - Math.random()); - array[i + 1] += 0.3 * (0.5 - Math.random()); - array[i + 2] += 0.3 * (0.5 - Math.random()); - } - - attributes.displacement.needsUpdate = true; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_custom_attributes_points.ts b/examples-testing/examples/webgl_custom_attributes_points.ts deleted file mode 100644 index ae112980a..000000000 --- a/examples-testing/examples/webgl_custom_attributes_points.ts +++ /dev/null @@ -1,117 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let renderer, scene, camera, stats; - -let sphere; - -const WIDTH = window.innerWidth; -const HEIGHT = window.innerHeight; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(40, WIDTH / HEIGHT, 1, 10000); - camera.position.z = 300; - - scene = new THREE.Scene(); - - const amount = 100000; - const radius = 200; - - const positions = new Float32Array(amount * 3); - const colors = new Float32Array(amount * 3); - const sizes = new Float32Array(amount); - - const vertex = new THREE.Vector3(); - const color = new THREE.Color(0xffffff); - - for (let i = 0; i < amount; i++) { - vertex.x = (Math.random() * 2 - 1) * radius; - vertex.y = (Math.random() * 2 - 1) * radius; - vertex.z = (Math.random() * 2 - 1) * radius; - vertex.toArray(positions, i * 3); - - if (vertex.x < 0) { - color.setHSL(0.5 + 0.1 * (i / amount), 0.7, 0.5); - } else { - color.setHSL(0.0 + 0.1 * (i / amount), 0.9, 0.5); - } - - color.toArray(colors, i * 3); - - sizes[i] = 10; - } - - const geometry = new THREE.BufferGeometry(); - geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); - geometry.setAttribute('customColor', new THREE.BufferAttribute(colors, 3)); - geometry.setAttribute('size', new THREE.BufferAttribute(sizes, 1)); - - // - - const material = new THREE.ShaderMaterial({ - uniforms: { - color: { value: new THREE.Color(0xffffff) }, - pointTexture: { value: new THREE.TextureLoader().load('textures/sprites/spark1.png') }, - }, - vertexShader: document.getElementById('vertexshader').textContent, - fragmentShader: document.getElementById('fragmentshader').textContent, - - blending: THREE.AdditiveBlending, - depthTest: false, - transparent: true, - }); - - // - - sphere = new THREE.Points(geometry, material); - scene.add(sphere); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(WIDTH, HEIGHT); - renderer.setAnimationLoop(animate); - - const container = document.getElementById('container'); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - const time = Date.now() * 0.005; - - sphere.rotation.z = 0.01 * time; - - const geometry = sphere.geometry; - const attributes = geometry.attributes; - - for (let i = 0; i < attributes.size.array.length; i++) { - attributes.size.array[i] = 14 + 13 * Math.sin(0.1 * i + time); - } - - attributes.size.needsUpdate = true; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_custom_attributes_points2.ts b/examples-testing/examples/webgl_custom_attributes_points2.ts deleted file mode 100644 index edd158fa1..000000000 --- a/examples-testing/examples/webgl_custom_attributes_points2.ts +++ /dev/null @@ -1,193 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; - -let renderer, scene, camera, stats; -let sphere, length1; - -const WIDTH = window.innerWidth; -const HEIGHT = window.innerHeight; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(45, WIDTH / HEIGHT, 1, 10000); - camera.position.z = 300; - - scene = new THREE.Scene(); - - const radius = 100, - segments = 68, - rings = 38; - - let sphereGeometry = new THREE.SphereGeometry(radius, segments, rings); - let boxGeometry = new THREE.BoxGeometry(0.8 * radius, 0.8 * radius, 0.8 * radius, 10, 10, 10); - - // if normal and uv attributes are not removed, mergeVertices() can't consolidate identical vertices with different normal/uv data - - sphereGeometry.deleteAttribute('normal'); - sphereGeometry.deleteAttribute('uv'); - - boxGeometry.deleteAttribute('normal'); - boxGeometry.deleteAttribute('uv'); - - sphereGeometry = BufferGeometryUtils.mergeVertices(sphereGeometry); - boxGeometry = BufferGeometryUtils.mergeVertices(boxGeometry); - - const combinedGeometry = BufferGeometryUtils.mergeGeometries([sphereGeometry, boxGeometry]); - const positionAttribute = combinedGeometry.getAttribute('position'); - - const colors = []; - const sizes = []; - - const color = new THREE.Color(); - const vertex = new THREE.Vector3(); - - length1 = sphereGeometry.getAttribute('position').count; - - for (let i = 0, l = positionAttribute.count; i < l; i++) { - vertex.fromBufferAttribute(positionAttribute, i); - - if (i < length1) { - color.setHSL(0.01 + 0.1 * (i / length1), 0.99, (vertex.y + radius) / (4 * radius)); - } else { - color.setHSL(0.6, 0.75, 0.25 + vertex.y / (2 * radius)); - } - - color.toArray(colors, i * 3); - - sizes[i] = i < length1 ? 10 : 40; - } - - const geometry = new THREE.BufferGeometry(); - geometry.setAttribute('position', positionAttribute); - geometry.setAttribute('size', new THREE.Float32BufferAttribute(sizes, 1)); - geometry.setAttribute('ca', new THREE.Float32BufferAttribute(colors, 3)); - - // - - const texture = new THREE.TextureLoader().load('textures/sprites/disc.png'); - texture.wrapS = THREE.RepeatWrapping; - texture.wrapT = THREE.RepeatWrapping; - - const material = new THREE.ShaderMaterial({ - uniforms: { - color: { value: new THREE.Color(0xffffff) }, - pointTexture: { value: texture }, - }, - vertexShader: document.getElementById('vertexshader').textContent, - fragmentShader: document.getElementById('fragmentshader').textContent, - transparent: true, - }); - - // - - sphere = new THREE.Points(geometry, material); - scene.add(sphere); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(WIDTH, HEIGHT); - renderer.setAnimationLoop(animate); - - const container = document.getElementById('container'); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function sortPoints() { - const vector = new THREE.Vector3(); - - // Model View Projection matrix - - const matrix = new THREE.Matrix4(); - matrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse); - matrix.multiply(sphere.matrixWorld); - - // - - const geometry = sphere.geometry; - - let index = geometry.getIndex(); - const positions = geometry.getAttribute('position').array; - const length = positions.length / 3; - - if (index === null) { - const array = new Uint16Array(length); - - for (let i = 0; i < length; i++) { - array[i] = i; - } - - index = new THREE.BufferAttribute(array, 1); - - geometry.setIndex(index); - } - - const sortArray = []; - - for (let i = 0; i < length; i++) { - vector.fromArray(positions, i * 3); - vector.applyMatrix4(matrix); - - sortArray.push([vector.z, i]); - } - - function numericalSort(a, b) { - return b[0] - a[0]; - } - - sortArray.sort(numericalSort); - - const indices = index.array; - - for (let i = 0; i < length; i++) { - indices[i] = sortArray[i][1]; - } - - geometry.index.needsUpdate = true; -} - -function animate() { - render(); - stats.update(); -} - -function render() { - const time = Date.now() * 0.005; - - sphere.rotation.y = 0.02 * time; - sphere.rotation.z = 0.02 * time; - - const geometry = sphere.geometry; - const attributes = geometry.attributes; - - for (let i = 0; i < attributes.size.array.length; i++) { - if (i < length1) { - attributes.size.array[i] = 16 + 12 * Math.sin(0.1 * i + time); - } - } - - attributes.size.needsUpdate = true; - - sortPoints(); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_custom_attributes_points3.ts b/examples-testing/examples/webgl_custom_attributes_points3.ts deleted file mode 100644 index 1b46a805e..000000000 --- a/examples-testing/examples/webgl_custom_attributes_points3.ts +++ /dev/null @@ -1,200 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; - -let renderer, scene, camera, stats; - -let object; - -let vertices1; - -const WIDTH = window.innerWidth; -const HEIGHT = window.innerHeight; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(40, WIDTH / HEIGHT, 1, 1000); - camera.position.z = 500; - - scene = new THREE.Scene(); - - let radius = 100; - const inner = 0.6 * radius; - const vertex = new THREE.Vector3(); - const vertices = []; - - for (let i = 0; i < 100000; i++) { - vertex.x = Math.random() * 2 - 1; - vertex.y = Math.random() * 2 - 1; - vertex.z = Math.random() * 2 - 1; - vertex.multiplyScalar(radius); - - if ( - vertex.x > inner || - vertex.x < -inner || - vertex.y > inner || - vertex.y < -inner || - vertex.z > inner || - vertex.z < -inner - ) - vertices.push(vertex.x, vertex.y, vertex.z); - } - - vertices1 = vertices.length / 3; - - radius = 200; - - let boxGeometry1 = new THREE.BoxGeometry(radius, 0.1 * radius, 0.1 * radius, 50, 5, 5); - - // if normal and uv attributes are not removed, mergeVertices() can't consolidate indentical vertices with different normal/uv data - - boxGeometry1.deleteAttribute('normal'); - boxGeometry1.deleteAttribute('uv'); - - boxGeometry1 = BufferGeometryUtils.mergeVertices(boxGeometry1); - - const matrix = new THREE.Matrix4(); - const position = new THREE.Vector3(); - const rotation = new THREE.Euler(); - const quaternion = new THREE.Quaternion(); - const scale = new THREE.Vector3(1, 1, 1); - - function addGeo(geo, x, y, z, ry) { - position.set(x, y, z); - rotation.set(0, ry, 0); - - matrix.compose(position, quaternion.setFromEuler(rotation), scale); - - const positionAttribute = geo.getAttribute('position'); - - for (let i = 0, l = positionAttribute.count; i < l; i++) { - vertex.fromBufferAttribute(positionAttribute, i); - vertex.applyMatrix4(matrix); - vertices.push(vertex.x, vertex.y, vertex.z); - } - } - - // side 1 - - addGeo(boxGeometry1, 0, 110, 110, 0); - addGeo(boxGeometry1, 0, 110, -110, 0); - addGeo(boxGeometry1, 0, -110, 110, 0); - addGeo(boxGeometry1, 0, -110, -110, 0); - - // side 2 - - addGeo(boxGeometry1, 110, 110, 0, Math.PI / 2); - addGeo(boxGeometry1, 110, -110, 0, Math.PI / 2); - addGeo(boxGeometry1, -110, 110, 0, Math.PI / 2); - addGeo(boxGeometry1, -110, -110, 0, Math.PI / 2); - - // corner edges - - let boxGeometry2 = new THREE.BoxGeometry(0.1 * radius, radius * 1.2, 0.1 * radius, 5, 60, 5); - - boxGeometry2.deleteAttribute('normal'); - boxGeometry2.deleteAttribute('uv'); - - boxGeometry2 = BufferGeometryUtils.mergeVertices(boxGeometry2); - - addGeo(boxGeometry2, 110, 0, 110, 0); - addGeo(boxGeometry2, 110, 0, -110, 0); - addGeo(boxGeometry2, -110, 0, 110, 0); - addGeo(boxGeometry2, -110, 0, -110, 0); - - const positionAttribute = new THREE.Float32BufferAttribute(vertices, 3); - - const colors = []; - const sizes = []; - - const color = new THREE.Color(); - - for (let i = 0; i < positionAttribute.count; i++) { - if (i < vertices1) { - color.setHSL(0.5 + 0.2 * (i / vertices1), 1, 0.5); - } else { - color.setHSL(0.1, 1, 0.5); - } - - color.toArray(colors, i * 3); - - sizes[i] = i < vertices1 ? 10 : 40; - } - - const geometry = new THREE.BufferGeometry(); - geometry.setAttribute('position', positionAttribute); - geometry.setAttribute('ca', new THREE.Float32BufferAttribute(colors, 3)); - geometry.setAttribute('size', new THREE.Float32BufferAttribute(sizes, 1)); - - // - - const texture = new THREE.TextureLoader().load('textures/sprites/ball.png'); - texture.wrapS = THREE.RepeatWrapping; - texture.wrapT = THREE.RepeatWrapping; - - const material = new THREE.ShaderMaterial({ - uniforms: { - amplitude: { value: 1.0 }, - color: { value: new THREE.Color(0xffffff) }, - pointTexture: { value: texture }, - }, - vertexShader: document.getElementById('vertexshader').textContent, - fragmentShader: document.getElementById('fragmentshader').textContent, - }); - - // - - object = new THREE.Points(geometry, material); - scene.add(object); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(WIDTH, HEIGHT); - renderer.setAnimationLoop(animate); - - const container = document.getElementById('container'); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - const time = Date.now() * 0.01; - - object.rotation.y = object.rotation.z = 0.02 * time; - - const geometry = object.geometry; - const attributes = geometry.attributes; - - for (let i = 0; i < attributes.size.array.length; i++) { - if (i < vertices1) { - attributes.size.array[i] = Math.max(0, 26 + 32 * Math.sin(0.1 * i + 0.6 * time)); - } - } - - attributes.size.needsUpdate = true; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_decals.ts b/examples-testing/examples/webgl_decals.ts deleted file mode 100644 index 23cdb4da8..000000000 --- a/examples-testing/examples/webgl_decals.ts +++ /dev/null @@ -1,237 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { DecalGeometry } from 'three/addons/geometries/DecalGeometry.js'; - -const container = document.getElementById('container'); - -let renderer, scene, camera, stats; -let mesh; -let raycaster; -let line; - -const intersection = { - intersects: false, - point: new THREE.Vector3(), - normal: new THREE.Vector3(), -}; -const mouse = new THREE.Vector2(); -const intersects = []; - -const textureLoader = new THREE.TextureLoader(); -const decalDiffuse = textureLoader.load('textures/decal/decal-diffuse.png'); -decalDiffuse.colorSpace = THREE.SRGBColorSpace; -const decalNormal = textureLoader.load('textures/decal/decal-normal.jpg'); - -const decalMaterial = new THREE.MeshPhongMaterial({ - specular: 0x444444, - map: decalDiffuse, - normalMap: decalNormal, - normalScale: new THREE.Vector2(1, 1), - shininess: 30, - transparent: true, - depthTest: true, - depthWrite: false, - polygonOffset: true, - polygonOffsetFactor: -4, - wireframe: false, -}); - -const decals = []; -let mouseHelper; -const position = new THREE.Vector3(); -const orientation = new THREE.Euler(); -const size = new THREE.Vector3(10, 10, 10); - -const params = { - minScale: 10, - maxScale: 20, - rotate: true, - clear: function () { - removeDecals(); - }, -}; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 120; - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 50; - controls.maxDistance = 200; - - scene.add(new THREE.AmbientLight(0x666666)); - - const dirLight1 = new THREE.DirectionalLight(0xffddcc, 3); - dirLight1.position.set(1, 0.75, 0.5); - scene.add(dirLight1); - - const dirLight2 = new THREE.DirectionalLight(0xccccff, 3); - dirLight2.position.set(-1, 0.75, -0.5); - scene.add(dirLight2); - - const geometry = new THREE.BufferGeometry(); - geometry.setFromPoints([new THREE.Vector3(), new THREE.Vector3()]); - - line = new THREE.Line(geometry, new THREE.LineBasicMaterial()); - scene.add(line); - - loadLeePerrySmith(); - - raycaster = new THREE.Raycaster(); - - mouseHelper = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 10), new THREE.MeshNormalMaterial()); - mouseHelper.visible = false; - scene.add(mouseHelper); - - window.addEventListener('resize', onWindowResize); - - let moved = false; - - controls.addEventListener('change', function () { - moved = true; - }); - - window.addEventListener('pointerdown', function () { - moved = false; - }); - - window.addEventListener('pointerup', function (event) { - if (moved === false) { - checkIntersection(event.clientX, event.clientY); - - if (intersection.intersects) shoot(); - } - }); - - window.addEventListener('pointermove', onPointerMove); - - function onPointerMove(event) { - if (event.isPrimary) { - checkIntersection(event.clientX, event.clientY); - } - } - - function checkIntersection(x, y) { - if (mesh === undefined) return; - - mouse.x = (x / window.innerWidth) * 2 - 1; - mouse.y = -(y / window.innerHeight) * 2 + 1; - - raycaster.setFromCamera(mouse, camera); - raycaster.intersectObject(mesh, false, intersects); - - if (intersects.length > 0) { - const p = intersects[0].point; - mouseHelper.position.copy(p); - intersection.point.copy(p); - - const n = intersects[0].face.normal.clone(); - n.transformDirection(mesh.matrixWorld); - n.multiplyScalar(10); - n.add(intersects[0].point); - - intersection.normal.copy(intersects[0].face.normal); - mouseHelper.lookAt(n); - - const positions = line.geometry.attributes.position; - positions.setXYZ(0, p.x, p.y, p.z); - positions.setXYZ(1, n.x, n.y, n.z); - positions.needsUpdate = true; - - intersection.intersects = true; - - intersects.length = 0; - } else { - intersection.intersects = false; - } - } - - const gui = new GUI(); - - gui.add(params, 'minScale', 1, 30); - gui.add(params, 'maxScale', 1, 30); - gui.add(params, 'rotate'); - gui.add(params, 'clear'); - gui.open(); -} - -function loadLeePerrySmith() { - const map = textureLoader.load('models/gltf/LeePerrySmith/Map-COL.jpg'); - map.colorSpace = THREE.SRGBColorSpace; - const specularMap = textureLoader.load('models/gltf/LeePerrySmith/Map-SPEC.jpg'); - const normalMap = textureLoader.load('models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg'); - - const loader = new GLTFLoader(); - - loader.load('models/gltf/LeePerrySmith/LeePerrySmith.glb', function (gltf) { - mesh = gltf.scene.children[0]; - mesh.material = new THREE.MeshPhongMaterial({ - specular: 0x111111, - map: map, - specularMap: specularMap, - normalMap: normalMap, - shininess: 25, - }); - - scene.add(mesh); - mesh.scale.set(10, 10, 10); - }); -} - -function shoot() { - position.copy(intersection.point); - orientation.copy(mouseHelper.rotation); - - if (params.rotate) orientation.z = Math.random() * 2 * Math.PI; - - const scale = params.minScale + Math.random() * (params.maxScale - params.minScale); - size.set(scale, scale, scale); - - const material = decalMaterial.clone(); - material.color.setHex(Math.random() * 0xffffff); - - const m = new THREE.Mesh(new DecalGeometry(mesh, position, orientation, size), material); - m.renderOrder = decals.length; // give decals a fixed render order - - decals.push(m); - scene.add(m); -} - -function removeDecals() { - decals.forEach(function (d) { - scene.remove(d); - }); - - decals.length = 0; -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_effects_anaglyph.ts b/examples-testing/examples/webgl_effects_anaglyph.ts deleted file mode 100644 index 8415973df..000000000 --- a/examples-testing/examples/webgl_effects_anaglyph.ts +++ /dev/null @@ -1,114 +0,0 @@ -import * as THREE from 'three'; - -import { AnaglyphEffect } from 'three/addons/effects/AnaglyphEffect.js'; - -let container, camera, scene, renderer, effect; - -const spheres = []; - -let mouseX = 0; -let mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -document.addEventListener('mousemove', onDocumentMouseMove); - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 100); - camera.position.z = 3; - - const path = 'textures/cube/pisa/'; - const format = '.png'; - const urls = [ - path + 'px' + format, - path + 'nx' + format, - path + 'py' + format, - path + 'ny' + format, - path + 'pz' + format, - path + 'nz' + format, - ]; - - const textureCube = new THREE.CubeTextureLoader().load(urls); - - scene = new THREE.Scene(); - scene.background = textureCube; - - const geometry = new THREE.SphereGeometry(0.1, 32, 16); - const material = new THREE.MeshBasicMaterial({ color: 0xffffff, envMap: textureCube }); - - for (let i = 0; i < 500; i++) { - const mesh = new THREE.Mesh(geometry, material); - - mesh.position.x = Math.random() * 10 - 5; - mesh.position.y = Math.random() * 10 - 5; - mesh.position.z = Math.random() * 10 - 5; - - mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1; - - scene.add(mesh); - - spheres.push(mesh); - } - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - const width = window.innerWidth || 2; - const height = window.innerHeight || 2; - - effect = new AnaglyphEffect(renderer); - effect.setSize(width, height); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - effect.setSize(window.innerWidth, window.innerHeight); -} - -function onDocumentMouseMove(event) { - mouseX = (event.clientX - windowHalfX) / 100; - mouseY = (event.clientY - windowHalfY) / 100; -} - -// - -function animate() { - render(); -} - -function render() { - const timer = 0.0001 * Date.now(); - - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-mouseY - camera.position.y) * 0.05; - - camera.lookAt(scene.position); - - for (let i = 0, il = spheres.length; i < il; i++) { - const sphere = spheres[i]; - - sphere.position.x = 5 * Math.cos(timer + i); - sphere.position.y = 5 * Math.sin(timer + i * 1.1); - } - - effect.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_effects_ascii.ts b/examples-testing/examples/webgl_effects_ascii.ts deleted file mode 100644 index a412bb79e..000000000 --- a/examples-testing/examples/webgl_effects_ascii.ts +++ /dev/null @@ -1,81 +0,0 @@ -import * as THREE from 'three'; - -import { AsciiEffect } from 'three/addons/effects/AsciiEffect.js'; -import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; - -let camera, controls, scene, renderer, effect; - -let sphere, plane; - -const start = Date.now(); - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.y = 150; - camera.position.z = 500; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0, 0, 0); - - const pointLight1 = new THREE.PointLight(0xffffff, 3, 0, 0); - pointLight1.position.set(500, 500, 500); - scene.add(pointLight1); - - const pointLight2 = new THREE.PointLight(0xffffff, 1, 0, 0); - pointLight2.position.set(-500, -500, -500); - scene.add(pointLight2); - - sphere = new THREE.Mesh(new THREE.SphereGeometry(200, 20, 10), new THREE.MeshPhongMaterial({ flatShading: true })); - scene.add(sphere); - - // Plane - - plane = new THREE.Mesh(new THREE.PlaneGeometry(400, 400), new THREE.MeshBasicMaterial({ color: 0xe0e0e0 })); - plane.position.y = -200; - plane.rotation.x = -Math.PI / 2; - scene.add(plane); - - renderer = new THREE.WebGLRenderer(); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - - effect = new AsciiEffect(renderer, ' .:-+*=%@#', { invert: true }); - effect.setSize(window.innerWidth, window.innerHeight); - effect.domElement.style.color = 'white'; - effect.domElement.style.backgroundColor = 'black'; - - // Special case: append effect.domElement, instead of renderer.domElement. - // AsciiEffect creates a custom domElement (a div container) where the ASCII elements are placed. - - document.body.appendChild(effect.domElement); - - controls = new TrackballControls(camera, effect.domElement); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - effect.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const timer = Date.now() - start; - - sphere.position.y = Math.abs(Math.sin(timer * 0.002)) * 150; - sphere.rotation.x = timer * 0.0003; - sphere.rotation.z = timer * 0.0002; - - controls.update(); - - effect.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_effects_parallaxbarrier.ts b/examples-testing/examples/webgl_effects_parallaxbarrier.ts deleted file mode 100644 index 90c867973..000000000 --- a/examples-testing/examples/webgl_effects_parallaxbarrier.ts +++ /dev/null @@ -1,110 +0,0 @@ -import * as THREE from 'three'; - -import { ParallaxBarrierEffect } from 'three/addons/effects/ParallaxBarrierEffect.js'; - -let container, camera, scene, renderer, effect; - -const spheres = []; - -let mouseX = 0; -let mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -document.addEventListener('mousemove', onDocumentMouseMove); - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 100); - camera.position.z = 3; - - const path = 'textures/cube/pisa/'; - const format = '.png'; - const urls = [ - path + 'px' + format, - path + 'nx' + format, - path + 'py' + format, - path + 'ny' + format, - path + 'pz' + format, - path + 'nz' + format, - ]; - - const textureCube = new THREE.CubeTextureLoader().load(urls); - - scene = new THREE.Scene(); - scene.background = textureCube; - - const geometry = new THREE.SphereGeometry(0.1, 32, 16); - const material = new THREE.MeshBasicMaterial({ color: 0xffffff, envMap: textureCube }); - - for (let i = 0; i < 500; i++) { - const mesh = new THREE.Mesh(geometry, material); - - mesh.position.x = Math.random() * 10 - 5; - mesh.position.y = Math.random() * 10 - 5; - mesh.position.z = Math.random() * 10 - 5; - - mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1; - - scene.add(mesh); - - spheres.push(mesh); - } - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - const width = window.innerWidth || 2; - const height = window.innerHeight || 2; - - effect = new ParallaxBarrierEffect(renderer); - effect.setSize(width, height); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - effect.setSize(window.innerWidth, window.innerHeight); -} - -function onDocumentMouseMove(event) { - mouseX = (event.clientX - windowHalfX) / 100; - mouseY = (event.clientY - windowHalfY) / 100; -} - -// - -function animate() { - const timer = 0.0001 * Date.now(); - - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-mouseY - camera.position.y) * 0.05; - - camera.lookAt(scene.position); - - for (let i = 0, il = spheres.length; i < il; i++) { - const sphere = spheres[i]; - - sphere.position.x = 5 * Math.cos(timer + i); - sphere.position.y = 5 * Math.sin(timer + i * 1.1); - } - - effect.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_effects_peppersghost.ts b/examples-testing/examples/webgl_effects_peppersghost.ts deleted file mode 100644 index 41dfb4b65..000000000 --- a/examples-testing/examples/webgl_effects_peppersghost.ts +++ /dev/null @@ -1,85 +0,0 @@ -import * as THREE from 'three'; - -import { PeppersGhostEffect } from 'three/addons/effects/PeppersGhostEffect.js'; - -let container; - -let camera, scene, renderer, effect; -let group; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 100000); - - scene = new THREE.Scene(); - - group = new THREE.Group(); - scene.add(group); - - // Cube - - const geometry = new THREE.BoxGeometry().toNonIndexed(); // ensure unique vertices for each triangle - - const position = geometry.attributes.position; - const colors = []; - const color = new THREE.Color(); - - // generate for each side of the cube a different color - - for (let i = 0; i < position.count; i += 6) { - color.setHex(Math.random() * 0xffffff); - - // first face - - colors.push(color.r, color.g, color.b); - colors.push(color.r, color.g, color.b); - colors.push(color.r, color.g, color.b); - - // second face - - colors.push(color.r, color.g, color.b); - colors.push(color.r, color.g, color.b); - colors.push(color.r, color.g, color.b); - } - - geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); - - const material = new THREE.MeshBasicMaterial({ vertexColors: true }); - - for (let i = 0; i < 10; i++) { - const cube = new THREE.Mesh(geometry, material); - cube.position.x = Math.random() * 2 - 1; - cube.position.y = Math.random() * 2 - 1; - cube.position.z = Math.random() * 2 - 1; - cube.scale.multiplyScalar(Math.random() + 0.5); - group.add(cube); - } - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - effect = new PeppersGhostEffect(renderer); - effect.setSize(window.innerWidth, window.innerHeight); - effect.cameraDistance = 5; - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - effect.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - group.rotation.y += 0.01; - - effect.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_effects_stereo.ts b/examples-testing/examples/webgl_effects_stereo.ts deleted file mode 100644 index dd2f61f91..000000000 --- a/examples-testing/examples/webgl_effects_stereo.ts +++ /dev/null @@ -1,98 +0,0 @@ -import * as THREE from 'three'; - -import { StereoEffect } from 'three/addons/effects/StereoEffect.js'; - -let container, camera, scene, renderer, effect; - -const spheres = []; - -let mouseX = 0, - mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -document.addEventListener('mousemove', onDocumentMouseMove); - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 100000); - camera.position.z = 3200; - - scene = new THREE.Scene(); - scene.background = new THREE.CubeTextureLoader() - .setPath('textures/cube/Park3Med/') - .load(['px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg']); - - const geometry = new THREE.SphereGeometry(100, 32, 16); - - const textureCube = new THREE.CubeTextureLoader() - .setPath('textures/cube/Park3Med/') - .load(['px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg']); - textureCube.mapping = THREE.CubeRefractionMapping; - - const material = new THREE.MeshBasicMaterial({ color: 0xffffff, envMap: textureCube, refractionRatio: 0.95 }); - - for (let i = 0; i < 500; i++) { - const mesh = new THREE.Mesh(geometry, material); - mesh.position.x = Math.random() * 10000 - 5000; - mesh.position.y = Math.random() * 10000 - 5000; - mesh.position.z = Math.random() * 10000 - 5000; - mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1; - scene.add(mesh); - - spheres.push(mesh); - } - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - effect = new StereoEffect(renderer); - effect.setSize(window.innerWidth, window.innerHeight); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - effect.setSize(window.innerWidth, window.innerHeight); -} - -function onDocumentMouseMove(event) { - mouseX = (event.clientX - windowHalfX) * 10; - mouseY = (event.clientY - windowHalfY) * 10; -} - -// - -function animate() { - const timer = 0.0001 * Date.now(); - - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-mouseY - camera.position.y) * 0.05; - camera.lookAt(scene.position); - - for (let i = 0, il = spheres.length; i < il; i++) { - const sphere = spheres[i]; - - sphere.position.x = 5000 * Math.cos(timer + i); - sphere.position.y = 5000 * Math.sin(timer + i * 1.1); - } - - effect.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_framebuffer_texture.ts b/examples-testing/examples/webgl_framebuffer_texture.ts deleted file mode 100644 index df4acc9d6..000000000 --- a/examples-testing/examples/webgl_framebuffer_texture.ts +++ /dev/null @@ -1,151 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js'; - -let camera, scene, renderer; -let line, sprite, texture; - -let cameraOrtho, sceneOrtho; - -let offset = 0; - -const dpr = window.devicePixelRatio; - -const textureSize = 128 * dpr; -const vector = new THREE.Vector2(); -const color = new THREE.Color(); - -init(); - -function init() { - // - - const width = window.innerWidth; - const height = window.innerHeight; - - camera = new THREE.PerspectiveCamera(70, width / height, 1, 1000); - camera.position.z = 20; - - cameraOrtho = new THREE.OrthographicCamera(-width / 2, width / 2, height / 2, -height / 2, 1, 10); - cameraOrtho.position.z = 10; - - scene = new THREE.Scene(); - sceneOrtho = new THREE.Scene(); - - // - - const points = GeometryUtils.gosper(8); - - const geometry = new THREE.BufferGeometry(); - const positionAttribute = new THREE.Float32BufferAttribute(points, 3); - geometry.setAttribute('position', positionAttribute); - geometry.center(); - - const colorAttribute = new THREE.BufferAttribute(new Float32Array(positionAttribute.array.length), 3); - colorAttribute.setUsage(THREE.DynamicDrawUsage); - geometry.setAttribute('color', colorAttribute); - - const material = new THREE.LineBasicMaterial({ vertexColors: true }); - - line = new THREE.Line(geometry, material); - line.scale.setScalar(0.05); - scene.add(line); - - // - - texture = new THREE.FramebufferTexture(textureSize, textureSize); - - // - - const spriteMaterial = new THREE.SpriteMaterial({ map: texture }); - sprite = new THREE.Sprite(spriteMaterial); - sprite.scale.set(textureSize, textureSize, 1); - sceneOrtho.add(sprite); - - updateSpritePosition(); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.autoClear = false; - document.body.appendChild(renderer.domElement); - - // - - const selection = document.getElementById('selection'); - const controls = new OrbitControls(camera, selection); - controls.enablePan = false; - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - cameraOrtho.left = -width / 2; - cameraOrtho.right = width / 2; - cameraOrtho.top = height / 2; - cameraOrtho.bottom = -height / 2; - cameraOrtho.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - updateSpritePosition(); -} - -function updateSpritePosition() { - const halfWidth = window.innerWidth / 2; - const halfHeight = window.innerHeight / 2; - - const halfImageWidth = textureSize / 2; - const halfImageHeight = textureSize / 2; - - sprite.position.set(-halfWidth + halfImageWidth, halfHeight - halfImageHeight, 1); -} - -function animate() { - const colorAttribute = line.geometry.getAttribute('color'); - updateColors(colorAttribute); - - // scene rendering - - renderer.clear(); - renderer.render(scene, camera); - - // calculate start position for copying data - - vector.x = (window.innerWidth * dpr) / 2 - textureSize / 2; - vector.y = (window.innerHeight * dpr) / 2 - textureSize / 2; - - renderer.copyFramebufferToTexture(texture, vector); - - renderer.clearDepth(); - renderer.render(sceneOrtho, cameraOrtho); -} - -function updateColors(colorAttribute) { - const l = colorAttribute.count; - - for (let i = 0; i < l; i++) { - const h = ((offset + i) % l) / l; - - color.setHSL(h, 1, 0.5); - colorAttribute.setX(i, color.r); - colorAttribute.setY(i, color.g); - colorAttribute.setZ(i, color.b); - } - - colorAttribute.needsUpdate = true; - - offset -= 25; -} diff --git a/examples-testing/examples/webgl_furnace_test.ts b/examples-testing/examples/webgl_furnace_test.ts deleted file mode 100644 index a81954176..000000000 --- a/examples-testing/examples/webgl_furnace_test.ts +++ /dev/null @@ -1,96 +0,0 @@ -import * as THREE from 'three'; - -let scene, camera, renderer, radianceMap; - -const COLOR = 0xcccccc; - -function init() { - const width = window.innerWidth; - const height = window.innerHeight; - const aspect = width / height; - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setSize(width, height); - renderer.setPixelRatio(window.devicePixelRatio); - document.body.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); - - document.body.addEventListener('mouseover', function () { - scene.traverse(function (child) { - if (child.isMesh) child.material.color.setHex(0xffffff); - }); - - render(); - }); - - document.body.addEventListener('mouseout', function () { - scene.traverse(function (child) { - if (child.isMesh) child.material.color.setHex(0xccccff); // tinted for visibility - }); - - render(); - }); - - // scene - - scene = new THREE.Scene(); - - // camera - camera = new THREE.PerspectiveCamera(40, aspect, 1, 30); - camera.position.set(0, 0, 18); -} - -function createObjects() { - const geometry = new THREE.SphereGeometry(0.4, 32, 16); - - for (let x = 0; x <= 10; x++) { - for (let y = 0; y <= 10; y++) { - const material = new THREE.MeshPhysicalMaterial({ - roughness: x / 10, - metalness: y / 10, - color: 0xffffff, - envMap: radianceMap, - envMapIntensity: 1, - transmission: 0, - ior: 1.5, - }); - - const mesh = new THREE.Mesh(geometry, material); - mesh.position.x = x - 5; - mesh.position.y = 5 - y; - scene.add(mesh); - } - } -} - -function createEnvironment() { - const envScene = new THREE.Scene(); - envScene.background = new THREE.Color(COLOR); - - const pmremGenerator = new THREE.PMREMGenerator(renderer); - radianceMap = pmremGenerator.fromScene(envScene).texture; - pmremGenerator.dispose(); - - scene.background = envScene.background; -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); - - render(); -} - -function render() { - renderer.render(scene, camera); -} - -Promise.resolve().then(init).then(createEnvironment).then(createObjects).then(render); diff --git a/examples-testing/examples/webgl_geometries.ts b/examples-testing/examples/webgl_geometries.ts deleted file mode 100644 index 2b2d02613..000000000 --- a/examples-testing/examples/webgl_geometries.ts +++ /dev/null @@ -1,137 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let camera, scene, renderer, stats; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000); - camera.position.y = 400; - - scene = new THREE.Scene(); - - let object; - - const ambientLight = new THREE.AmbientLight(0xcccccc, 1.5); - scene.add(ambientLight); - - const pointLight = new THREE.PointLight(0xffffff, 2.5, 0, 0); - camera.add(pointLight); - scene.add(camera); - - const map = new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg'); - map.wrapS = map.wrapT = THREE.RepeatWrapping; - map.anisotropy = 16; - map.colorSpace = THREE.SRGBColorSpace; - - const material = new THREE.MeshPhongMaterial({ map: map, side: THREE.DoubleSide }); - - // - - object = new THREE.Mesh(new THREE.SphereGeometry(75, 20, 10), material); - object.position.set(-300, 0, 200); - scene.add(object); - - object = new THREE.Mesh(new THREE.IcosahedronGeometry(75, 1), material); - object.position.set(-100, 0, 200); - scene.add(object); - - object = new THREE.Mesh(new THREE.OctahedronGeometry(75, 2), material); - object.position.set(100, 0, 200); - scene.add(object); - - object = new THREE.Mesh(new THREE.TetrahedronGeometry(75, 0), material); - object.position.set(300, 0, 200); - scene.add(object); - - // - - object = new THREE.Mesh(new THREE.PlaneGeometry(100, 100, 4, 4), material); - object.position.set(-300, 0, 0); - scene.add(object); - - object = new THREE.Mesh(new THREE.BoxGeometry(100, 100, 100, 4, 4, 4), material); - object.position.set(-100, 0, 0); - scene.add(object); - - object = new THREE.Mesh(new THREE.CircleGeometry(50, 20, 0, Math.PI * 2), material); - object.position.set(100, 0, 0); - scene.add(object); - - object = new THREE.Mesh(new THREE.RingGeometry(10, 50, 20, 5, 0, Math.PI * 2), material); - object.position.set(300, 0, 0); - scene.add(object); - - // - - object = new THREE.Mesh(new THREE.CylinderGeometry(25, 75, 100, 40, 5), material); - object.position.set(-300, 0, -200); - scene.add(object); - - const points = []; - - for (let i = 0; i < 50; i++) { - points.push(new THREE.Vector2(Math.sin(i * 0.2) * Math.sin(i * 0.1) * 15 + 50, (i - 5) * 2)); - } - - object = new THREE.Mesh(new THREE.LatheGeometry(points, 20), material); - object.position.set(-100, 0, -200); - scene.add(object); - - object = new THREE.Mesh(new THREE.TorusGeometry(50, 20, 20, 20), material); - object.position.set(100, 0, -200); - scene.add(object); - - object = new THREE.Mesh(new THREE.TorusKnotGeometry(50, 10, 50, 20), material); - object.position.set(300, 0, -200); - scene.add(object); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - const timer = Date.now() * 0.0001; - - camera.position.x = Math.cos(timer) * 800; - camera.position.z = Math.sin(timer) * 800; - - camera.lookAt(scene.position); - - scene.traverse(function (object) { - if (object.isMesh === true) { - object.rotation.x = timer * 5; - object.rotation.y = timer * 2.5; - } - }); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_geometries_parametric.ts b/examples-testing/examples/webgl_geometries_parametric.ts deleted file mode 100644 index 29bf7ae26..000000000 --- a/examples-testing/examples/webgl_geometries_parametric.ts +++ /dev/null @@ -1,124 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import * as Curves from 'three/addons/curves/CurveExtras.js'; -import { ParametricGeometry } from 'three/addons/geometries/ParametricGeometry.js'; -import { ParametricGeometries } from 'three/addons/geometries/ParametricGeometries.js'; - -let camera, scene, renderer, stats; - -init(); - -function init() { - const container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000); - camera.position.y = 400; - - scene = new THREE.Scene(); - - // - - const ambientLight = new THREE.AmbientLight(0xcccccc, 1.5); - scene.add(ambientLight); - - const pointLight = new THREE.PointLight(0xffffff, 2.5, 0, 0); - camera.add(pointLight); - scene.add(camera); - - // - - const map = new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg'); - map.wrapS = map.wrapT = THREE.RepeatWrapping; - map.anisotropy = 16; - map.colorSpace = THREE.SRGBColorSpace; - - const material = new THREE.MeshPhongMaterial({ map: map, side: THREE.DoubleSide }); - - // - - let geometry, object; - - geometry = new ParametricGeometry(ParametricGeometries.plane(100, 100), 10, 10); - geometry.center(); - object = new THREE.Mesh(geometry, material); - object.position.set(-200, 0, 200); - scene.add(object); - - geometry = new ParametricGeometry(ParametricGeometries.klein, 20, 20); - object = new THREE.Mesh(geometry, material); - object.position.set(0, 0, 200); - object.scale.multiplyScalar(5); - scene.add(object); - - geometry = new ParametricGeometry(ParametricGeometries.mobius, 20, 20); - object = new THREE.Mesh(geometry, material); - object.position.set(200, 0, 200); - object.scale.multiplyScalar(30); - scene.add(object); - - // - - const GrannyKnot = new Curves.GrannyKnot(); - - const torus = new ParametricGeometries.TorusKnotGeometry(50, 10, 50, 20, 2, 3); - const sphere = new ParametricGeometries.SphereGeometry(50, 20, 10); - const tube = new ParametricGeometries.TubeGeometry(GrannyKnot, 100, 3, 8, true); - - object = new THREE.Mesh(torus, material); - object.position.set(-200, 0, -200); - scene.add(object); - - object = new THREE.Mesh(sphere, material); - object.position.set(0, 0, -200); - scene.add(object); - - object = new THREE.Mesh(tube, material); - object.position.set(200, 0, -200); - object.scale.multiplyScalar(2); - scene.add(object); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - const timer = Date.now() * 0.0001; - - camera.position.x = Math.cos(timer) * 800; - camera.position.z = Math.sin(timer) * 800; - - camera.lookAt(scene.position); - - scene.traverse(function (object) { - if (object.isMesh === true) { - object.rotation.x = timer * 5; - object.rotation.y = timer * 2.5; - } - }); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_geometry_colors.ts b/examples-testing/examples/webgl_geometry_colors.ts deleted file mode 100644 index bc0bf5174..000000000 --- a/examples-testing/examples/webgl_geometry_colors.ts +++ /dev/null @@ -1,176 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; - -let camera, scene, renderer; - -let mouseX = 0, - mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -init(); - -function init() { - container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(20, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 1800; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0, 0, 1); - scene.add(light); - - // shadow - - const canvas = document.createElement('canvas'); - canvas.width = 128; - canvas.height = 128; - - const context = canvas.getContext('2d'); - const gradient = context.createRadialGradient( - canvas.width / 2, - canvas.height / 2, - 0, - canvas.width / 2, - canvas.height / 2, - canvas.width / 2, - ); - gradient.addColorStop(0.1, 'rgba(210,210,210,1)'); - gradient.addColorStop(1, 'rgba(255,255,255,1)'); - - context.fillStyle = gradient; - context.fillRect(0, 0, canvas.width, canvas.height); - - const shadowTexture = new THREE.CanvasTexture(canvas); - - const shadowMaterial = new THREE.MeshBasicMaterial({ map: shadowTexture }); - const shadowGeo = new THREE.PlaneGeometry(300, 300, 1, 1); - - let shadowMesh; - - shadowMesh = new THREE.Mesh(shadowGeo, shadowMaterial); - shadowMesh.position.y = -250; - shadowMesh.rotation.x = -Math.PI / 2; - scene.add(shadowMesh); - - shadowMesh = new THREE.Mesh(shadowGeo, shadowMaterial); - shadowMesh.position.y = -250; - shadowMesh.position.x = -400; - shadowMesh.rotation.x = -Math.PI / 2; - scene.add(shadowMesh); - - shadowMesh = new THREE.Mesh(shadowGeo, shadowMaterial); - shadowMesh.position.y = -250; - shadowMesh.position.x = 400; - shadowMesh.rotation.x = -Math.PI / 2; - scene.add(shadowMesh); - - const radius = 200; - - const geometry1 = new THREE.IcosahedronGeometry(radius, 1); - - const count = geometry1.attributes.position.count; - geometry1.setAttribute('color', new THREE.BufferAttribute(new Float32Array(count * 3), 3)); - - const geometry2 = geometry1.clone(); - const geometry3 = geometry1.clone(); - - const color = new THREE.Color(); - const positions1 = geometry1.attributes.position; - const positions2 = geometry2.attributes.position; - const positions3 = geometry3.attributes.position; - const colors1 = geometry1.attributes.color; - const colors2 = geometry2.attributes.color; - const colors3 = geometry3.attributes.color; - - for (let i = 0; i < count; i++) { - color.setHSL((positions1.getY(i) / radius + 1) / 2, 1.0, 0.5, THREE.SRGBColorSpace); - colors1.setXYZ(i, color.r, color.g, color.b); - - color.setHSL(0, (positions2.getY(i) / radius + 1) / 2, 0.5, THREE.SRGBColorSpace); - colors2.setXYZ(i, color.r, color.g, color.b); - - color.setRGB(1, 0.8 - (positions3.getY(i) / radius + 1) / 2, 0, THREE.SRGBColorSpace); - colors3.setXYZ(i, color.r, color.g, color.b); - } - - const material = new THREE.MeshPhongMaterial({ - color: 0xffffff, - flatShading: true, - vertexColors: true, - shininess: 0, - }); - - const wireframeMaterial = new THREE.MeshBasicMaterial({ color: 0x000000, wireframe: true, transparent: true }); - - let mesh = new THREE.Mesh(geometry1, material); - let wireframe = new THREE.Mesh(geometry1, wireframeMaterial); - mesh.add(wireframe); - mesh.position.x = -400; - mesh.rotation.x = -1.87; - scene.add(mesh); - - mesh = new THREE.Mesh(geometry2, material); - wireframe = new THREE.Mesh(geometry2, wireframeMaterial); - mesh.add(wireframe); - mesh.position.x = 400; - scene.add(mesh); - - mesh = new THREE.Mesh(geometry3, material); - wireframe = new THREE.Mesh(geometry3, wireframeMaterial); - mesh.add(wireframe); - scene.add(mesh); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - document.addEventListener('mousemove', onDocumentMouseMove); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onDocumentMouseMove(event) { - mouseX = event.clientX - windowHalfX; - mouseY = event.clientY - windowHalfY; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-mouseY - camera.position.y) * 0.05; - - camera.lookAt(scene.position); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_geometry_colors_lookuptable.ts b/examples-testing/examples/webgl_geometry_colors_lookuptable.ts deleted file mode 100644 index 6b0138529..000000000 --- a/examples-testing/examples/webgl_geometry_colors_lookuptable.ts +++ /dev/null @@ -1,148 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { Lut } from 'three/addons/math/Lut.js'; - -let container; - -let perpCamera, orthoCamera, renderer, lut; - -let mesh, sprite; -let scene, uiScene; - -let params; - -init(); - -function init() { - container = document.getElementById('container'); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - - uiScene = new THREE.Scene(); - - lut = new Lut(); - - const width = window.innerWidth; - const height = window.innerHeight; - - perpCamera = new THREE.PerspectiveCamera(60, width / height, 1, 100); - perpCamera.position.set(0, 0, 10); - scene.add(perpCamera); - - orthoCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 1, 2); - orthoCamera.position.set(0.5, 0, 1); - - sprite = new THREE.Sprite( - new THREE.SpriteMaterial({ - map: new THREE.CanvasTexture(lut.createCanvas()), - }), - ); - sprite.material.map.colorSpace = THREE.SRGBColorSpace; - sprite.scale.x = 0.125; - uiScene.add(sprite); - - mesh = new THREE.Mesh( - undefined, - new THREE.MeshLambertMaterial({ - side: THREE.DoubleSide, - color: 0xf5f5f5, - vertexColors: true, - }), - ); - scene.add(mesh); - - params = { - colorMap: 'rainbow', - }; - loadModel(); - - const pointLight = new THREE.PointLight(0xffffff, 3, 0, 0); - perpCamera.add(pointLight); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.autoClear = false; - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(width, height); - container.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); - - const controls = new OrbitControls(perpCamera, renderer.domElement); - controls.addEventListener('change', render); - - const gui = new GUI(); - - gui.add(params, 'colorMap', ['rainbow', 'cooltowarm', 'blackbody', 'grayscale']).onChange(function () { - updateColors(); - render(); - }); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - perpCamera.aspect = width / height; - perpCamera.updateProjectionMatrix(); - - renderer.setSize(width, height); - render(); -} - -function render() { - renderer.clear(); - renderer.render(scene, perpCamera); - renderer.render(uiScene, orthoCamera); -} - -function loadModel() { - const loader = new THREE.BufferGeometryLoader(); - loader.load('models/json/pressure.json', function (geometry) { - geometry.center(); - geometry.computeVertexNormals(); - - // default color attribute - const colors = []; - - for (let i = 0, n = geometry.attributes.position.count; i < n; ++i) { - colors.push(1, 1, 1); - } - - geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); - - mesh.geometry = geometry; - updateColors(); - - render(); - }); -} - -function updateColors() { - lut.setColorMap(params.colorMap); - - lut.setMax(2000); - lut.setMin(0); - - const geometry = mesh.geometry; - const pressures = geometry.attributes.pressure; - const colors = geometry.attributes.color; - const color = new THREE.Color(); - - for (let i = 0; i < pressures.array.length; i++) { - const colorValue = pressures.array[i]; - - color.copy(lut.getColor(colorValue)).convertSRGBToLinear(); - - colors.setXYZ(i, color.r, color.g, color.b); - } - - colors.needsUpdate = true; - - const map = sprite.material.map; - lut.updateCanvas(map.image); - map.needsUpdate = true; -} diff --git a/examples-testing/examples/webgl_geometry_convex.ts b/examples-testing/examples/webgl_geometry_convex.ts deleted file mode 100644 index ade9cb801..000000000 --- a/examples-testing/examples/webgl_geometry_convex.ts +++ /dev/null @@ -1,117 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { ConvexGeometry } from 'three/addons/geometries/ConvexGeometry.js'; -import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; - -let group, camera, scene, renderer; - -init(); - -function init() { - scene = new THREE.Scene(); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // camera - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(15, 20, 30); - scene.add(camera); - - // controls - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 20; - controls.maxDistance = 50; - controls.maxPolarAngle = Math.PI / 2; - - // ambient light - - scene.add(new THREE.AmbientLight(0x666666)); - - // point light - - const light = new THREE.PointLight(0xffffff, 3, 0, 0); - camera.add(light); - - // helper - - scene.add(new THREE.AxesHelper(20)); - - // textures - - const loader = new THREE.TextureLoader(); - const texture = loader.load('textures/sprites/disc.png'); - texture.colorSpace = THREE.SRGBColorSpace; - - group = new THREE.Group(); - scene.add(group); - - // points - - let dodecahedronGeometry = new THREE.DodecahedronGeometry(10); - - // if normal and uv attributes are not removed, mergeVertices() can't consolidate indentical vertices with different normal/uv data - - dodecahedronGeometry.deleteAttribute('normal'); - dodecahedronGeometry.deleteAttribute('uv'); - - dodecahedronGeometry = BufferGeometryUtils.mergeVertices(dodecahedronGeometry); - - const vertices = []; - const positionAttribute = dodecahedronGeometry.getAttribute('position'); - - for (let i = 0; i < positionAttribute.count; i++) { - const vertex = new THREE.Vector3(); - vertex.fromBufferAttribute(positionAttribute, i); - vertices.push(vertex); - } - - const pointsMaterial = new THREE.PointsMaterial({ - color: 0x0080ff, - map: texture, - size: 1, - alphaTest: 0.5, - }); - - const pointsGeometry = new THREE.BufferGeometry().setFromPoints(vertices); - - const points = new THREE.Points(pointsGeometry, pointsMaterial); - group.add(points); - - // convex hull - - const meshMaterial = new THREE.MeshLambertMaterial({ - color: 0xffffff, - opacity: 0.5, - side: THREE.DoubleSide, - transparent: true, - }); - - const meshGeometry = new ConvexGeometry(vertices); - - const mesh = new THREE.Mesh(meshGeometry, meshMaterial); - group.add(mesh); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - group.rotation.y += 0.005; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_geometry_cube.ts b/examples-testing/examples/webgl_geometry_cube.ts deleted file mode 100644 index 572601acb..000000000 --- a/examples-testing/examples/webgl_geometry_cube.ts +++ /dev/null @@ -1,46 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer; -let mesh; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.z = 2; - - scene = new THREE.Scene(); - - const texture = new THREE.TextureLoader().load('textures/crate.gif'); - texture.colorSpace = THREE.SRGBColorSpace; - - const geometry = new THREE.BoxGeometry(); - const material = new THREE.MeshBasicMaterial({ map: texture }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - mesh.rotation.x += 0.005; - mesh.rotation.y += 0.01; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_geometry_dynamic.ts b/examples-testing/examples/webgl_geometry_dynamic.ts deleted file mode 100644 index 06e858f54..000000000 --- a/examples-testing/examples/webgl_geometry_dynamic.ts +++ /dev/null @@ -1,97 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js'; - -let camera, controls, scene, renderer, stats; - -let mesh, geometry, material, clock; - -const worldWidth = 128, - worldDepth = 128; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 20000); - camera.position.y = 200; - - clock = new THREE.Clock(); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xaaccff); - scene.fog = new THREE.FogExp2(0xaaccff, 0.0007); - - geometry = new THREE.PlaneGeometry(20000, 20000, worldWidth - 1, worldDepth - 1); - geometry.rotateX(-Math.PI / 2); - - const position = geometry.attributes.position; - position.usage = THREE.DynamicDrawUsage; - - for (let i = 0; i < position.count; i++) { - const y = 35 * Math.sin(i / 2); - position.setY(i, y); - } - - const texture = new THREE.TextureLoader().load('textures/water.jpg'); - texture.wrapS = texture.wrapT = THREE.RepeatWrapping; - texture.repeat.set(5, 5); - texture.colorSpace = THREE.SRGBColorSpace; - - material = new THREE.MeshBasicMaterial({ color: 0x0044ff, map: texture }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - controls = new FirstPersonControls(camera, renderer.domElement); - - controls.movementSpeed = 500; - controls.lookSpeed = 0.1; - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - controls.handleResize(); -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - const delta = clock.getDelta(); - const time = clock.getElapsedTime() * 10; - - const position = geometry.attributes.position; - - for (let i = 0; i < position.count; i++) { - const y = 35 * Math.sin(i / 5 + (time + i) / 7); - position.setY(i, y); - } - - position.needsUpdate = true; - - controls.update(delta); - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_geometry_extrude_shapes.ts b/examples-testing/examples/webgl_geometry_extrude_shapes.ts deleted file mode 100644 index 7428aee31..000000000 --- a/examples-testing/examples/webgl_geometry_extrude_shapes.ts +++ /dev/null @@ -1,149 +0,0 @@ -import * as THREE from 'three'; - -import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; - -let camera, scene, renderer, controls; - -init(); - -function init() { - const info = document.createElement('div'); - info.style.position = 'absolute'; - info.style.top = '10px'; - info.style.width = '100%'; - info.style.textAlign = 'center'; - info.style.color = '#fff'; - info.style.link = '#f80'; - info.innerHTML = - 'three.js webgl - geometry extrude shapes'; - document.body.appendChild(info); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x222222); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 0, 500); - - controls = new TrackballControls(camera, renderer.domElement); - controls.minDistance = 200; - controls.maxDistance = 500; - - scene.add(new THREE.AmbientLight(0x666666)); - - const light = new THREE.PointLight(0xffffff, 3, 0, 0); - light.position.copy(camera.position); - scene.add(light); - - // - - const closedSpline = new THREE.CatmullRomCurve3([ - new THREE.Vector3(-60, -100, 60), - new THREE.Vector3(-60, 20, 60), - new THREE.Vector3(-60, 120, 60), - new THREE.Vector3(60, 20, -60), - new THREE.Vector3(60, -100, -60), - ]); - - closedSpline.curveType = 'catmullrom'; - closedSpline.closed = true; - - const extrudeSettings1 = { - steps: 100, - bevelEnabled: false, - extrudePath: closedSpline, - }; - - const pts1 = [], - count = 3; - - for (let i = 0; i < count; i++) { - const l = 20; - - const a = ((2 * i) / count) * Math.PI; - - pts1.push(new THREE.Vector2(Math.cos(a) * l, Math.sin(a) * l)); - } - - const shape1 = new THREE.Shape(pts1); - - const geometry1 = new THREE.ExtrudeGeometry(shape1, extrudeSettings1); - - const material1 = new THREE.MeshLambertMaterial({ color: 0xb00000, wireframe: false }); - - const mesh1 = new THREE.Mesh(geometry1, material1); - - scene.add(mesh1); - - // - - const randomPoints = []; - - for (let i = 0; i < 10; i++) { - randomPoints.push( - new THREE.Vector3((i - 4.5) * 50, THREE.MathUtils.randFloat(-50, 50), THREE.MathUtils.randFloat(-50, 50)), - ); - } - - const randomSpline = new THREE.CatmullRomCurve3(randomPoints); - - // - - const extrudeSettings2 = { - steps: 200, - bevelEnabled: false, - extrudePath: randomSpline, - }; - - const pts2 = [], - numPts = 5; - - for (let i = 0; i < numPts * 2; i++) { - const l = i % 2 == 1 ? 10 : 20; - - const a = (i / numPts) * Math.PI; - - pts2.push(new THREE.Vector2(Math.cos(a) * l, Math.sin(a) * l)); - } - - const shape2 = new THREE.Shape(pts2); - - const geometry2 = new THREE.ExtrudeGeometry(shape2, extrudeSettings2); - - const material2 = new THREE.MeshLambertMaterial({ color: 0xff8000, wireframe: false }); - - const mesh2 = new THREE.Mesh(geometry2, material2); - - scene.add(mesh2); - - // - - const materials = [material1, material2]; - - const extrudeSettings3 = { - depth: 20, - steps: 1, - bevelEnabled: true, - bevelThickness: 2, - bevelSize: 4, - bevelSegments: 1, - }; - - const geometry3 = new THREE.ExtrudeGeometry(shape2, extrudeSettings3); - - const mesh3 = new THREE.Mesh(geometry3, materials); - - mesh3.position.set(50, 100, 50); - - scene.add(mesh3); -} - -function animate() { - controls.update(); - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_geometry_extrude_splines.ts b/examples-testing/examples/webgl_geometry_extrude_splines.ts deleted file mode 100644 index 0741083da..000000000 --- a/examples-testing/examples/webgl_geometry_extrude_splines.ts +++ /dev/null @@ -1,310 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import * as Curves from 'three/addons/curves/CurveExtras.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let container, stats; - -let camera, scene, renderer, splineCamera, cameraHelper, cameraEye; - -const direction = new THREE.Vector3(); -const binormal = new THREE.Vector3(); -const normal = new THREE.Vector3(); -const position = new THREE.Vector3(); -const lookAt = new THREE.Vector3(); - -const pipeSpline = new THREE.CatmullRomCurve3([ - new THREE.Vector3(0, 10, -10), - new THREE.Vector3(10, 0, -10), - new THREE.Vector3(20, 0, 0), - new THREE.Vector3(30, 0, 10), - new THREE.Vector3(30, 0, 20), - new THREE.Vector3(20, 0, 30), - new THREE.Vector3(10, 0, 30), - new THREE.Vector3(0, 0, 30), - new THREE.Vector3(-10, 10, 30), - new THREE.Vector3(-10, 20, 30), - new THREE.Vector3(0, 30, 30), - new THREE.Vector3(10, 30, 30), - new THREE.Vector3(20, 30, 15), - new THREE.Vector3(10, 30, 10), - new THREE.Vector3(0, 30, 10), - new THREE.Vector3(-10, 20, 10), - new THREE.Vector3(-10, 10, 10), - new THREE.Vector3(0, 0, 10), - new THREE.Vector3(10, -10, 10), - new THREE.Vector3(20, -15, 10), - new THREE.Vector3(30, -15, 10), - new THREE.Vector3(40, -15, 10), - new THREE.Vector3(50, -15, 10), - new THREE.Vector3(60, 0, 10), - new THREE.Vector3(70, 0, 0), - new THREE.Vector3(80, 0, 0), - new THREE.Vector3(90, 0, 0), - new THREE.Vector3(100, 0, 0), -]); - -const sampleClosedSpline = new THREE.CatmullRomCurve3([ - new THREE.Vector3(0, -40, -40), - new THREE.Vector3(0, 40, -40), - new THREE.Vector3(0, 140, -40), - new THREE.Vector3(0, 40, 40), - new THREE.Vector3(0, -40, 40), -]); - -sampleClosedSpline.curveType = 'catmullrom'; -sampleClosedSpline.closed = true; - -// Keep a dictionary of Curve instances -const splines = { - GrannyKnot: new Curves.GrannyKnot(), - HeartCurve: new Curves.HeartCurve(3.5), - VivianiCurve: new Curves.VivianiCurve(70), - KnotCurve: new Curves.KnotCurve(), - HelixCurve: new Curves.HelixCurve(), - TrefoilKnot: new Curves.TrefoilKnot(), - TorusKnot: new Curves.TorusKnot(20), - CinquefoilKnot: new Curves.CinquefoilKnot(20), - TrefoilPolynomialKnot: new Curves.TrefoilPolynomialKnot(14), - FigureEightPolynomialKnot: new Curves.FigureEightPolynomialKnot(), - DecoratedTorusKnot4a: new Curves.DecoratedTorusKnot4a(), - DecoratedTorusKnot4b: new Curves.DecoratedTorusKnot4b(), - DecoratedTorusKnot5a: new Curves.DecoratedTorusKnot5a(), - DecoratedTorusKnot5c: new Curves.DecoratedTorusKnot5c(), - PipeSpline: pipeSpline, - SampleClosedSpline: sampleClosedSpline, -}; - -let parent, tubeGeometry, mesh; - -const params = { - spline: 'GrannyKnot', - scale: 4, - extrusionSegments: 100, - radiusSegments: 3, - closed: true, - animationView: false, - lookAhead: false, - cameraHelper: false, -}; - -const material = new THREE.MeshLambertMaterial({ color: 0xff00ff }); - -const wireframeMaterial = new THREE.MeshBasicMaterial({ - color: 0x000000, - opacity: 0.3, - wireframe: true, - transparent: true, -}); - -function addTube() { - if (mesh !== undefined) { - parent.remove(mesh); - mesh.geometry.dispose(); - } - - const extrudePath = splines[params.spline]; - - tubeGeometry = new THREE.TubeGeometry( - extrudePath, - params.extrusionSegments, - 2, - params.radiusSegments, - params.closed, - ); - - addGeometry(tubeGeometry); - - setScale(); -} - -function setScale() { - mesh.scale.set(params.scale, params.scale, params.scale); -} - -function addGeometry(geometry) { - // 3D shape - - mesh = new THREE.Mesh(geometry, material); - const wireframe = new THREE.Mesh(geometry, wireframeMaterial); - mesh.add(wireframe); - - parent.add(mesh); -} - -function animateCamera() { - cameraHelper.visible = params.cameraHelper; - cameraEye.visible = params.cameraHelper; -} - -init(); - -function init() { - container = document.getElementById('container'); - - // camera - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.01, 10000); - camera.position.set(0, 50, 500); - - // scene - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - // light - - scene.add(new THREE.AmbientLight(0xffffff)); - - const light = new THREE.DirectionalLight(0xffffff, 1.5); - light.position.set(0, 0, 1); - scene.add(light); - - // tube - - parent = new THREE.Object3D(); - scene.add(parent); - - splineCamera = new THREE.PerspectiveCamera(84, window.innerWidth / window.innerHeight, 0.01, 1000); - parent.add(splineCamera); - - cameraHelper = new THREE.CameraHelper(splineCamera); - scene.add(cameraHelper); - - addTube(); - - // debug camera - - cameraEye = new THREE.Mesh(new THREE.SphereGeometry(5), new THREE.MeshBasicMaterial({ color: 0xdddddd })); - parent.add(cameraEye); - - cameraHelper.visible = params.cameraHelper; - cameraEye.visible = params.cameraHelper; - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // stats - - stats = new Stats(); - container.appendChild(stats.dom); - - // dat.GUI - - const gui = new GUI({ width: 285 }); - - const folderGeometry = gui.addFolder('Geometry'); - folderGeometry.add(params, 'spline', Object.keys(splines)).onChange(function () { - addTube(); - }); - folderGeometry - .add(params, 'scale', 2, 10) - .step(2) - .onChange(function () { - setScale(); - }); - folderGeometry - .add(params, 'extrusionSegments', 50, 500) - .step(50) - .onChange(function () { - addTube(); - }); - folderGeometry - .add(params, 'radiusSegments', 2, 12) - .step(1) - .onChange(function () { - addTube(); - }); - folderGeometry.add(params, 'closed').onChange(function () { - addTube(); - }); - folderGeometry.open(); - - const folderCamera = gui.addFolder('Camera'); - folderCamera.add(params, 'animationView').onChange(function () { - animateCamera(); - }); - folderCamera.add(params, 'lookAhead').onChange(function () { - animateCamera(); - }); - folderCamera.add(params, 'cameraHelper').onChange(function () { - animateCamera(); - }); - folderCamera.open(); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 100; - controls.maxDistance = 2000; - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - // animate camera along spline - - const time = Date.now(); - const looptime = 20 * 1000; - const t = (time % looptime) / looptime; - - tubeGeometry.parameters.path.getPointAt(t, position); - position.multiplyScalar(params.scale); - - // interpolation - - const segments = tubeGeometry.tangents.length; - const pickt = t * segments; - const pick = Math.floor(pickt); - const pickNext = (pick + 1) % segments; - - binormal.subVectors(tubeGeometry.binormals[pickNext], tubeGeometry.binormals[pick]); - binormal.multiplyScalar(pickt - pick).add(tubeGeometry.binormals[pick]); - - tubeGeometry.parameters.path.getTangentAt(t, direction); - const offset = 15; - - normal.copy(binormal).cross(direction); - - // we move on a offset on its binormal - - position.add(normal.clone().multiplyScalar(offset)); - - splineCamera.position.copy(position); - cameraEye.position.copy(position); - - // using arclength for stablization in look ahead - - tubeGeometry.parameters.path.getPointAt((t + 30 / tubeGeometry.parameters.path.getLength()) % 1, lookAt); - lookAt.multiplyScalar(params.scale); - - // camera orientation 2 - up orientation via normal - - if (!params.lookAhead) lookAt.copy(position).add(direction); - splineCamera.matrix.lookAt(splineCamera.position, lookAt, normal); - splineCamera.quaternion.setFromRotationMatrix(splineCamera.matrix); - - cameraHelper.update(); - - renderer.render(scene, params.animationView === true ? splineCamera : camera); -} diff --git a/examples-testing/examples/webgl_geometry_minecraft.ts b/examples-testing/examples/webgl_geometry_minecraft.ts deleted file mode 100644 index 765aa1e49..000000000 --- a/examples-testing/examples/webgl_geometry_minecraft.ts +++ /dev/null @@ -1,183 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js'; -import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js'; -import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; - -let container, stats; - -let camera, controls, scene, renderer; - -const worldWidth = 128, - worldDepth = 128; -const worldHalfWidth = worldWidth / 2; -const worldHalfDepth = worldDepth / 2; -const data = generateHeight(worldWidth, worldDepth); - -const clock = new THREE.Clock(); - -init(); - -function init() { - container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 20000); - camera.position.y = getY(worldHalfWidth, worldHalfDepth) * 100 + 100; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xbfd1e5); - - // sides - - const matrix = new THREE.Matrix4(); - - const pxGeometry = new THREE.PlaneGeometry(100, 100); - pxGeometry.attributes.uv.array[1] = 0.5; - pxGeometry.attributes.uv.array[3] = 0.5; - pxGeometry.rotateY(Math.PI / 2); - pxGeometry.translate(50, 0, 0); - - const nxGeometry = new THREE.PlaneGeometry(100, 100); - nxGeometry.attributes.uv.array[1] = 0.5; - nxGeometry.attributes.uv.array[3] = 0.5; - nxGeometry.rotateY(-Math.PI / 2); - nxGeometry.translate(-50, 0, 0); - - const pyGeometry = new THREE.PlaneGeometry(100, 100); - pyGeometry.attributes.uv.array[5] = 0.5; - pyGeometry.attributes.uv.array[7] = 0.5; - pyGeometry.rotateX(-Math.PI / 2); - pyGeometry.translate(0, 50, 0); - - const pzGeometry = new THREE.PlaneGeometry(100, 100); - pzGeometry.attributes.uv.array[1] = 0.5; - pzGeometry.attributes.uv.array[3] = 0.5; - pzGeometry.translate(0, 0, 50); - - const nzGeometry = new THREE.PlaneGeometry(100, 100); - nzGeometry.attributes.uv.array[1] = 0.5; - nzGeometry.attributes.uv.array[3] = 0.5; - nzGeometry.rotateY(Math.PI); - nzGeometry.translate(0, 0, -50); - - // - - const geometries = []; - - for (let z = 0; z < worldDepth; z++) { - for (let x = 0; x < worldWidth; x++) { - const h = getY(x, z); - - matrix.makeTranslation(x * 100 - worldHalfWidth * 100, h * 100, z * 100 - worldHalfDepth * 100); - - const px = getY(x + 1, z); - const nx = getY(x - 1, z); - const pz = getY(x, z + 1); - const nz = getY(x, z - 1); - - geometries.push(pyGeometry.clone().applyMatrix4(matrix)); - - if ((px !== h && px !== h + 1) || x === 0) { - geometries.push(pxGeometry.clone().applyMatrix4(matrix)); - } - - if ((nx !== h && nx !== h + 1) || x === worldWidth - 1) { - geometries.push(nxGeometry.clone().applyMatrix4(matrix)); - } - - if ((pz !== h && pz !== h + 1) || z === worldDepth - 1) { - geometries.push(pzGeometry.clone().applyMatrix4(matrix)); - } - - if ((nz !== h && nz !== h + 1) || z === 0) { - geometries.push(nzGeometry.clone().applyMatrix4(matrix)); - } - } - } - - const geometry = BufferGeometryUtils.mergeGeometries(geometries); - geometry.computeBoundingSphere(); - - const texture = new THREE.TextureLoader().load('textures/minecraft/atlas.png'); - texture.colorSpace = THREE.SRGBColorSpace; - texture.magFilter = THREE.NearestFilter; - - const mesh = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ map: texture, side: THREE.DoubleSide })); - scene.add(mesh); - - const ambientLight = new THREE.AmbientLight(0xeeeeee, 3); - scene.add(ambientLight); - - const directionalLight = new THREE.DirectionalLight(0xffffff, 12); - directionalLight.position.set(1, 1, 0.5).normalize(); - scene.add(directionalLight); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - controls = new FirstPersonControls(camera, renderer.domElement); - - controls.movementSpeed = 1000; - controls.lookSpeed = 0.125; - controls.lookVertical = true; - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - controls.handleResize(); -} - -function generateHeight(width, height) { - const data = [], - perlin = new ImprovedNoise(), - size = width * height, - z = Math.random() * 100; - - let quality = 2; - - for (let j = 0; j < 4; j++) { - if (j === 0) for (let i = 0; i < size; i++) data[i] = 0; - - for (let i = 0; i < size; i++) { - const x = i % width, - y = (i / width) | 0; - data[i] += perlin.noise(x / quality, y / quality, z) * quality; - } - - quality *= 4; - } - - return data; -} - -function getY(x, z) { - return (data[x + z * worldWidth] * 0.15) | 0; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - controls.update(clock.getDelta()); - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_geometry_nurbs.ts b/examples-testing/examples/webgl_geometry_nurbs.ts deleted file mode 100644 index a603710bd..000000000 --- a/examples-testing/examples/webgl_geometry_nurbs.ts +++ /dev/null @@ -1,298 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { NURBSCurve } from 'three/addons/curves/NURBSCurve.js'; -import { NURBSSurface } from 'three/addons/curves/NURBSSurface.js'; -import { NURBSVolume } from 'three/addons/curves/NURBSVolume.js'; -import { ParametricGeometry } from 'three/addons/geometries/ParametricGeometry.js'; - -let container, stats; - -let camera, scene, renderer; -let group; - -let targetRotation = 0; -let targetRotationOnPointerDown = 0; - -let pointerX = 0; -let pointerXOnPointerDown = 0; - -let windowHalfX = window.innerWidth / 2; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 2000); - camera.position.set(0, 150, 750); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - scene.add(new THREE.AmbientLight(0xffffff)); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(1, 1, 1); - scene.add(light); - - group = new THREE.Group(); - group.position.y = 50; - scene.add(group); - - // NURBS curve - - const nurbsControlPoints = []; - const nurbsKnots = []; - const nurbsDegree = 3; - - for (let i = 0; i <= nurbsDegree; i++) { - nurbsKnots.push(0); - } - - for (let i = 0, j = 20; i < j; i++) { - nurbsControlPoints.push( - new THREE.Vector4( - Math.random() * 400 - 200, - Math.random() * 400, - Math.random() * 400 - 200, - 1, // weight of control point: higher means stronger attraction - ), - ); - - const knot = (i + 1) / (j - nurbsDegree); - nurbsKnots.push(THREE.MathUtils.clamp(knot, 0, 1)); - } - - const nurbsCurve = new NURBSCurve(nurbsDegree, nurbsKnots, nurbsControlPoints); - - const nurbsGeometry = new THREE.BufferGeometry(); - nurbsGeometry.setFromPoints(nurbsCurve.getPoints(200)); - - const nurbsMaterial = new THREE.LineBasicMaterial({ color: 0x333333 }); - - const nurbsLine = new THREE.Line(nurbsGeometry, nurbsMaterial); - nurbsLine.position.set(0, -100, 0); - group.add(nurbsLine); - - const nurbsControlPointsGeometry = new THREE.BufferGeometry(); - nurbsControlPointsGeometry.setFromPoints(nurbsCurve.controlPoints); - - const nurbsControlPointsMaterial = new THREE.LineBasicMaterial({ - color: 0x333333, - opacity: 0.25, - transparent: true, - }); - - const nurbsControlPointsLine = new THREE.Line(nurbsControlPointsGeometry, nurbsControlPointsMaterial); - nurbsControlPointsLine.position.copy(nurbsLine.position); - group.add(nurbsControlPointsLine); - - // NURBS surface - { - const nsControlPoints = [ - [ - new THREE.Vector4(-200, -200, 100, 1), - new THREE.Vector4(-200, -100, -200, 1), - new THREE.Vector4(-200, 100, 250, 1), - new THREE.Vector4(-200, 200, -100, 1), - ], - [ - new THREE.Vector4(0, -200, 0, 1), - new THREE.Vector4(0, -100, -100, 5), - new THREE.Vector4(0, 100, 150, 5), - new THREE.Vector4(0, 200, 0, 1), - ], - [ - new THREE.Vector4(200, -200, -100, 1), - new THREE.Vector4(200, -100, 200, 1), - new THREE.Vector4(200, 100, -250, 1), - new THREE.Vector4(200, 200, 100, 1), - ], - ]; - const degree1 = 2; - const degree2 = 3; - const knots1 = [0, 0, 0, 1, 1, 1]; - const knots2 = [0, 0, 0, 0, 1, 1, 1, 1]; - const nurbsSurface = new NURBSSurface(degree1, degree2, knots1, knots2, nsControlPoints); - - const map = new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg'); - map.wrapS = map.wrapT = THREE.RepeatWrapping; - map.anisotropy = 16; - map.colorSpace = THREE.SRGBColorSpace; - - function getSurfacePoint(u, v, target) { - return nurbsSurface.getPoint(u, v, target); - } - - const geometry = new ParametricGeometry(getSurfacePoint, 20, 20); - const material = new THREE.MeshLambertMaterial({ map: map, side: THREE.DoubleSide }); - const object = new THREE.Mesh(geometry, material); - object.position.set(-400, 100, 0); - object.scale.multiplyScalar(1); - group.add(object); - } - - // NURBS volume - { - const nsControlPoints = [ - [ - [new THREE.Vector4(-200, -200, -200, 1), new THREE.Vector4(-200, -200, 200, 1)], - [new THREE.Vector4(-200, -100, -200, 1), new THREE.Vector4(-200, -100, 200, 1)], - [new THREE.Vector4(-200, 100, -200, 1), new THREE.Vector4(-200, 100, 200, 1)], - [new THREE.Vector4(-200, 200, -200, 1), new THREE.Vector4(-200, 200, 200, 1)], - ], - [ - [new THREE.Vector4(0, -200, -200, 1), new THREE.Vector4(0, -200, 200, 1)], - [new THREE.Vector4(0, -100, -200, 1), new THREE.Vector4(0, -100, 200, 1)], - [new THREE.Vector4(0, 100, -200, 1), new THREE.Vector4(0, 100, 200, 1)], - [new THREE.Vector4(0, 200, -200, 1), new THREE.Vector4(0, 200, 200, 1)], - ], - [ - [new THREE.Vector4(200, -200, -200, 1), new THREE.Vector4(200, -200, 200, 1)], - [new THREE.Vector4(200, -100, 0, 1), new THREE.Vector4(200, -100, 100, 1)], - [new THREE.Vector4(200, 100, 0, 1), new THREE.Vector4(200, 100, 100, 1)], - [new THREE.Vector4(200, 200, 0, 1), new THREE.Vector4(200, 200, 100, 1)], - ], - ]; - const degree1 = 2; - const degree2 = 3; - const degree3 = 1; - const knots1 = [0, 0, 0, 1, 1, 1]; - const knots2 = [0, 0, 0, 0, 1, 1, 1, 1]; - const knots3 = [0, 0, 1, 1]; - const nurbsVolume = new NURBSVolume(degree1, degree2, degree3, knots1, knots2, knots3, nsControlPoints); - - const map = new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg'); - map.wrapS = map.wrapT = THREE.RepeatWrapping; - map.anisotropy = 16; - map.colorSpace = THREE.SRGBColorSpace; - - // Since ParametricGeometry() only support bi-variate parametric geometries - // we create evaluation functions for different surfaces with one of the three - // parameter values (u, v, w) kept constant and create multiple THREE.Mesh - // objects one for each surface - function getSurfacePointFront(u, v, target) { - return nurbsVolume.getPoint(u, v, 0, target); - } - - function getSurfacePointMiddle(u, v, target) { - return nurbsVolume.getPoint(u, v, 0.5, target); - } - - function getSurfacePointBack(u, v, target) { - return nurbsVolume.getPoint(u, v, 1, target); - } - - function getSurfacePointTop(u, w, target) { - return nurbsVolume.getPoint(u, 1, w, target); - } - - function getSurfacePointSide(v, w, target) { - return nurbsVolume.getPoint(0, v, w, target); - } - - const geometryFront = new ParametricGeometry(getSurfacePointFront, 20, 20); - const materialFront = new THREE.MeshLambertMaterial({ map: map, side: THREE.DoubleSide }); - const objectFront = new THREE.Mesh(geometryFront, materialFront); - objectFront.position.set(400, 100, 0); - objectFront.scale.multiplyScalar(0.5); - group.add(objectFront); - - const geometryMiddle = new ParametricGeometry(getSurfacePointMiddle, 20, 20); - const materialMiddle = new THREE.MeshLambertMaterial({ map: map, side: THREE.DoubleSide }); - const objectMiddle = new THREE.Mesh(geometryMiddle, materialMiddle); - objectMiddle.position.set(400, 100, 0); - objectMiddle.scale.multiplyScalar(0.5); - group.add(objectMiddle); - - const geometryBack = new ParametricGeometry(getSurfacePointBack, 20, 20); - const materialBack = new THREE.MeshLambertMaterial({ map: map, side: THREE.DoubleSide }); - const objectBack = new THREE.Mesh(geometryBack, materialBack); - objectBack.position.set(400, 100, 0); - objectBack.scale.multiplyScalar(0.5); - group.add(objectBack); - - const geometryTop = new ParametricGeometry(getSurfacePointTop, 20, 20); - const materialTop = new THREE.MeshLambertMaterial({ map: map, side: THREE.DoubleSide }); - const objectTop = new THREE.Mesh(geometryTop, materialTop); - objectTop.position.set(400, 100, 0); - objectTop.scale.multiplyScalar(0.5); - group.add(objectTop); - - const geometrySide = new ParametricGeometry(getSurfacePointSide, 20, 20); - const materialSide = new THREE.MeshLambertMaterial({ map: map, side: THREE.DoubleSide }); - const objectSide = new THREE.Mesh(geometrySide, materialSide); - objectSide.position.set(400, 100, 0); - objectSide.scale.multiplyScalar(0.5); - group.add(objectSide); - } - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - container.style.touchAction = 'none'; - container.addEventListener('pointerdown', onPointerDown); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function onPointerDown(event) { - if (event.isPrimary === false) return; - - pointerXOnPointerDown = event.clientX - windowHalfX; - targetRotationOnPointerDown = targetRotation; - - document.addEventListener('pointermove', onPointerMove); - document.addEventListener('pointerup', onPointerUp); -} - -function onPointerMove(event) { - if (event.isPrimary === false) return; - - pointerX = event.clientX - windowHalfX; - - targetRotation = targetRotationOnPointerDown + (pointerX - pointerXOnPointerDown) * 0.02; -} - -function onPointerUp() { - if (event.isPrimary === false) return; - - document.removeEventListener('pointermove', onPointerMove); - document.removeEventListener('pointerup', onPointerUp); -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - group.rotation.y += (targetRotation - group.rotation.y) * 0.05; - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_geometry_sdf.ts b/examples-testing/examples/webgl_geometry_sdf.ts deleted file mode 100644 index fa5dba102..000000000 --- a/examples-testing/examples/webgl_geometry_sdf.ts +++ /dev/null @@ -1,164 +0,0 @@ -import * as THREE from 'three'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { SDFGeometryGenerator } from 'three/addons/geometries/SDFGeometryGenerator.js'; -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let renderer, stats, meshFromSDF, scene, camera, clock, controls; - -const settings = { - res: 4, - bounds: 1, - autoRotate: true, - wireframe: true, - material: 'depth', - vertexCount: '0', -}; - -// Example SDF from https://www.shadertoy.com/view/MdXSWn --> - -const shader = /* glsl */ ` - float dist(vec3 p) { - p.xyz = p.xzy; - p *= 1.2; - vec3 z = p; - vec3 dz=vec3(0.0); - float power = 8.0; - float r, theta, phi; - float dr = 1.0; - - float t0 = 1.0; - for(int i = 0; i < 7; ++i) { - r = length(z); - if(r > 2.0) continue; - theta = atan(z.y / z.x); - #ifdef phase_shift_on - phi = asin(z.z / r) ; - #else - phi = asin(z.z / r); - #endif - - dr = pow(r, power - 1.0) * dr * power + 1.0; - - r = pow(r, power); - theta = theta * power; - phi = phi * power; - - z = r * vec3(cos(theta)*cos(phi), sin(theta)*cos(phi), sin(phi)) + p; - - t0 = min(t0, r); - } - - return 0.5 * log(r) * r / dr; - } - `; - -init(); - -function init() { - const w = window.innerWidth; - const h = window.innerHeight; - - camera = new THREE.OrthographicCamera(w / -2, w / 2, h / 2, h / -2, 0.01, 1600); - camera.position.z = 1100; - - scene = new THREE.Scene(); - - clock = new THREE.Clock(); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - controls = new OrbitControls(camera, renderer.domElement); - controls.enableDamping = true; - - window.addEventListener('resize', onWindowResize); - - // - - const panel = new GUI(); - - panel.add(settings, 'res', 1, 6, 1).name('Res').onFinishChange(compile); - panel.add(settings, 'bounds', 1, 10, 1).name('Bounds').onFinishChange(compile); - panel.add(settings, 'material', ['depth', 'normal']).name('Material').onChange(setMaterial); - panel.add(settings, 'wireframe').name('Wireframe').onChange(setMaterial); - panel.add(settings, 'autoRotate').name('Auto Rotate'); - panel.add(settings, 'vertexCount').name('Vertex count').listen().disable(); - - // - - compile(); -} - -function compile() { - const generator = new SDFGeometryGenerator(renderer); - const geometry = generator.generate(Math.pow(2, settings.res + 2), shader, settings.bounds); - geometry.computeVertexNormals(); - - if (meshFromSDF) { - // updates mesh - - meshFromSDF.geometry.dispose(); - meshFromSDF.geometry = geometry; - } else { - // inits meshFromSDF : THREE.Mesh - - meshFromSDF = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial()); - scene.add(meshFromSDF); - - const scale = (Math.min(window.innerWidth, window.innerHeight) / 2) * 0.66; - meshFromSDF.scale.set(scale, scale, scale); - - setMaterial(); - } - - settings.vertexCount = geometry.attributes.position.count; -} - -function setMaterial() { - meshFromSDF.material.dispose(); - - if (settings.material == 'depth') { - meshFromSDF.material = new THREE.MeshDepthMaterial(); - } else if (settings.material == 'normal') { - meshFromSDF.material = new THREE.MeshNormalMaterial(); - } - - meshFromSDF.material.wireframe = settings.wireframe; -} - -function onWindowResize() { - const w = window.innerWidth; - const h = window.innerHeight; - - renderer.setSize(w, h); - - camera.left = w / -2; - camera.right = w / 2; - camera.top = h / 2; - camera.bottom = h / -2; - - camera.updateProjectionMatrix(); -} - -function render() { - renderer.render(scene, camera); -} - -function animate() { - controls.update(); - - if (settings.autoRotate) { - meshFromSDF.rotation.y += Math.PI * 0.05 * clock.getDelta(); - } - - render(); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_geometry_shapes.ts b/examples-testing/examples/webgl_geometry_shapes.ts deleted file mode 100644 index f1d00f011..000000000 --- a/examples-testing/examples/webgl_geometry_shapes.ts +++ /dev/null @@ -1,363 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; - -let camera, scene, renderer; - -let group; - -let targetRotation = 0; -let targetRotationOnPointerDown = 0; - -let pointerX = 0; -let pointerXOnPointerDown = 0; - -let windowHalfX = window.innerWidth / 2; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 150, 500); - scene.add(camera); - - const light = new THREE.PointLight(0xffffff, 2.5, 0, 0); - camera.add(light); - - group = new THREE.Group(); - group.position.y = 50; - scene.add(group); - - const loader = new THREE.TextureLoader(); - const texture = loader.load('textures/uv_grid_opengl.jpg'); - texture.colorSpace = THREE.SRGBColorSpace; - - // it's necessary to apply these settings in order to correctly display the texture on a shape geometry - - texture.wrapS = texture.wrapT = THREE.RepeatWrapping; - texture.repeat.set(0.008, 0.008); - - function addShape(shape, extrudeSettings, color, x, y, z, rx, ry, rz, s) { - // flat shape with texture - // note: default UVs generated by THREE.ShapeGeometry are simply the x- and y-coordinates of the vertices - - let geometry = new THREE.ShapeGeometry(shape); - - let mesh = new THREE.Mesh(geometry, new THREE.MeshPhongMaterial({ side: THREE.DoubleSide, map: texture })); - mesh.position.set(x, y, z - 175); - mesh.rotation.set(rx, ry, rz); - mesh.scale.set(s, s, s); - group.add(mesh); - - // flat shape - - geometry = new THREE.ShapeGeometry(shape); - - mesh = new THREE.Mesh(geometry, new THREE.MeshPhongMaterial({ color: color, side: THREE.DoubleSide })); - mesh.position.set(x, y, z - 125); - mesh.rotation.set(rx, ry, rz); - mesh.scale.set(s, s, s); - group.add(mesh); - - // extruded shape - - geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings); - - mesh = new THREE.Mesh(geometry, new THREE.MeshPhongMaterial({ color: color })); - mesh.position.set(x, y, z - 75); - mesh.rotation.set(rx, ry, rz); - mesh.scale.set(s, s, s); - group.add(mesh); - - addLineShape(shape, color, x, y, z, rx, ry, rz, s); - } - - function addLineShape(shape, color, x, y, z, rx, ry, rz, s) { - // lines - - shape.autoClose = true; - - const points = shape.getPoints(); - const spacedPoints = shape.getSpacedPoints(50); - - const geometryPoints = new THREE.BufferGeometry().setFromPoints(points); - const geometrySpacedPoints = new THREE.BufferGeometry().setFromPoints(spacedPoints); - - // solid line - - let line = new THREE.Line(geometryPoints, new THREE.LineBasicMaterial({ color: color })); - line.position.set(x, y, z - 25); - line.rotation.set(rx, ry, rz); - line.scale.set(s, s, s); - group.add(line); - - // line from equidistance sampled points - - line = new THREE.Line(geometrySpacedPoints, new THREE.LineBasicMaterial({ color: color })); - line.position.set(x, y, z + 25); - line.rotation.set(rx, ry, rz); - line.scale.set(s, s, s); - group.add(line); - - // vertices from real points - - let particles = new THREE.Points(geometryPoints, new THREE.PointsMaterial({ color: color, size: 4 })); - particles.position.set(x, y, z + 75); - particles.rotation.set(rx, ry, rz); - particles.scale.set(s, s, s); - group.add(particles); - - // equidistance sampled points - - particles = new THREE.Points(geometrySpacedPoints, new THREE.PointsMaterial({ color: color, size: 4 })); - particles.position.set(x, y, z + 125); - particles.rotation.set(rx, ry, rz); - particles.scale.set(s, s, s); - group.add(particles); - } - - // California - - const californiaPts = []; - - californiaPts.push(new THREE.Vector2(610, 320)); - californiaPts.push(new THREE.Vector2(450, 300)); - californiaPts.push(new THREE.Vector2(392, 392)); - californiaPts.push(new THREE.Vector2(266, 438)); - californiaPts.push(new THREE.Vector2(190, 570)); - californiaPts.push(new THREE.Vector2(190, 600)); - californiaPts.push(new THREE.Vector2(160, 620)); - californiaPts.push(new THREE.Vector2(160, 650)); - californiaPts.push(new THREE.Vector2(180, 640)); - californiaPts.push(new THREE.Vector2(165, 680)); - californiaPts.push(new THREE.Vector2(150, 670)); - californiaPts.push(new THREE.Vector2(90, 737)); - californiaPts.push(new THREE.Vector2(80, 795)); - californiaPts.push(new THREE.Vector2(50, 835)); - californiaPts.push(new THREE.Vector2(64, 870)); - californiaPts.push(new THREE.Vector2(60, 945)); - californiaPts.push(new THREE.Vector2(300, 945)); - californiaPts.push(new THREE.Vector2(300, 743)); - californiaPts.push(new THREE.Vector2(600, 473)); - californiaPts.push(new THREE.Vector2(626, 425)); - californiaPts.push(new THREE.Vector2(600, 370)); - californiaPts.push(new THREE.Vector2(610, 320)); - - for (let i = 0; i < californiaPts.length; i++) californiaPts[i].multiplyScalar(0.25); - - const californiaShape = new THREE.Shape(californiaPts); - - // Triangle - - const triangleShape = new THREE.Shape().moveTo(80, 20).lineTo(40, 80).lineTo(120, 80).lineTo(80, 20); // close path - - // Heart - - const x = 0, - y = 0; - - const heartShape = new THREE.Shape() - .moveTo(x + 25, y + 25) - .bezierCurveTo(x + 25, y + 25, x + 20, y, x, y) - .bezierCurveTo(x - 30, y, x - 30, y + 35, x - 30, y + 35) - .bezierCurveTo(x - 30, y + 55, x - 10, y + 77, x + 25, y + 95) - .bezierCurveTo(x + 60, y + 77, x + 80, y + 55, x + 80, y + 35) - .bezierCurveTo(x + 80, y + 35, x + 80, y, x + 50, y) - .bezierCurveTo(x + 35, y, x + 25, y + 25, x + 25, y + 25); - - // Square - - const sqLength = 80; - - const squareShape = new THREE.Shape() - .moveTo(0, 0) - .lineTo(0, sqLength) - .lineTo(sqLength, sqLength) - .lineTo(sqLength, 0) - .lineTo(0, 0); - - // Rounded rectangle - - const roundedRectShape = new THREE.Shape(); - - (function roundedRect(ctx, x, y, width, height, radius) { - ctx.moveTo(x, y + radius); - ctx.lineTo(x, y + height - radius); - ctx.quadraticCurveTo(x, y + height, x + radius, y + height); - ctx.lineTo(x + width - radius, y + height); - ctx.quadraticCurveTo(x + width, y + height, x + width, y + height - radius); - ctx.lineTo(x + width, y + radius); - ctx.quadraticCurveTo(x + width, y, x + width - radius, y); - ctx.lineTo(x + radius, y); - ctx.quadraticCurveTo(x, y, x, y + radius); - })(roundedRectShape, 0, 0, 50, 50, 20); - - // Track - - const trackShape = new THREE.Shape() - .moveTo(40, 40) - .lineTo(40, 160) - .absarc(60, 160, 20, Math.PI, 0, true) - .lineTo(80, 40) - .absarc(60, 40, 20, 2 * Math.PI, Math.PI, true); - - // Circle - - const circleRadius = 40; - const circleShape = new THREE.Shape() - .moveTo(0, circleRadius) - .quadraticCurveTo(circleRadius, circleRadius, circleRadius, 0) - .quadraticCurveTo(circleRadius, -circleRadius, 0, -circleRadius) - .quadraticCurveTo(-circleRadius, -circleRadius, -circleRadius, 0) - .quadraticCurveTo(-circleRadius, circleRadius, 0, circleRadius); - - // Fish - - const fishShape = new THREE.Shape() - .moveTo(x, y) - .quadraticCurveTo(x + 50, y - 80, x + 90, y - 10) - .quadraticCurveTo(x + 100, y - 10, x + 115, y - 40) - .quadraticCurveTo(x + 115, y, x + 115, y + 40) - .quadraticCurveTo(x + 100, y + 10, x + 90, y + 10) - .quadraticCurveTo(x + 50, y + 80, x, y); - - // Arc circle - - const arcShape = new THREE.Shape().moveTo(50, 10).absarc(10, 10, 40, 0, Math.PI * 2, false); - - const holePath = new THREE.Path().moveTo(20, 10).absarc(10, 10, 10, 0, Math.PI * 2, true); - - arcShape.holes.push(holePath); - - // Smiley - - const smileyShape = new THREE.Shape().moveTo(80, 40).absarc(40, 40, 40, 0, Math.PI * 2, false); - - const smileyEye1Path = new THREE.Path().moveTo(35, 20).absellipse(25, 20, 10, 10, 0, Math.PI * 2, true); - - const smileyEye2Path = new THREE.Path().moveTo(65, 20).absarc(55, 20, 10, 0, Math.PI * 2, true); - - const smileyMouthPath = new THREE.Path() - .moveTo(20, 40) - .quadraticCurveTo(40, 60, 60, 40) - .bezierCurveTo(70, 45, 70, 50, 60, 60) - .quadraticCurveTo(40, 80, 20, 60) - .quadraticCurveTo(5, 50, 20, 40); - - smileyShape.holes.push(smileyEye1Path); - smileyShape.holes.push(smileyEye2Path); - smileyShape.holes.push(smileyMouthPath); - - // Spline shape - - const splinepts = []; - splinepts.push(new THREE.Vector2(70, 20)); - splinepts.push(new THREE.Vector2(80, 90)); - splinepts.push(new THREE.Vector2(-30, 70)); - splinepts.push(new THREE.Vector2(0, 0)); - - const splineShape = new THREE.Shape().moveTo(0, 0).splineThru(splinepts); - - const extrudeSettings = { - depth: 8, - bevelEnabled: true, - bevelSegments: 2, - steps: 2, - bevelSize: 1, - bevelThickness: 1, - }; - - // addShape( shape, color, x, y, z, rx, ry,rz, s ); - - addShape(californiaShape, extrudeSettings, 0xf08000, -300, -100, 0, 0, 0, 0, 1); - addShape(triangleShape, extrudeSettings, 0x8080f0, -180, 0, 0, 0, 0, 0, 1); - addShape(roundedRectShape, extrudeSettings, 0x008000, -150, 150, 0, 0, 0, 0, 1); - addShape(trackShape, extrudeSettings, 0x008080, 200, -100, 0, 0, 0, 0, 1); - addShape(squareShape, extrudeSettings, 0x0040f0, 150, 100, 0, 0, 0, 0, 1); - addShape(heartShape, extrudeSettings, 0xf00000, 60, 100, 0, 0, 0, Math.PI, 1); - addShape(circleShape, extrudeSettings, 0x00f000, 120, 250, 0, 0, 0, 0, 1); - addShape(fishShape, extrudeSettings, 0x404040, -60, 200, 0, 0, 0, 0, 1); - addShape(smileyShape, extrudeSettings, 0xf000f0, -200, 250, 0, 0, 0, Math.PI, 1); - addShape(arcShape, extrudeSettings, 0x804000, 150, 0, 0, 0, 0, 0, 1); - addShape(splineShape, extrudeSettings, 0x808080, -50, -100, 0, 0, 0, 0, 1); - - addLineShape(arcShape.holes[0], 0x804000, 150, 0, 0, 0, 0, 0, 1); - - for (let i = 0; i < smileyShape.holes.length; i += 1) { - addLineShape(smileyShape.holes[i], 0xf000f0, -200, 250, 0, 0, 0, Math.PI, 1); - } - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - container.style.touchAction = 'none'; - container.addEventListener('pointerdown', onPointerDown); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function onPointerDown(event) { - if (event.isPrimary === false) return; - - pointerXOnPointerDown = event.clientX - windowHalfX; - targetRotationOnPointerDown = targetRotation; - - document.addEventListener('pointermove', onPointerMove); - document.addEventListener('pointerup', onPointerUp); -} - -function onPointerMove(event) { - if (event.isPrimary === false) return; - - pointerX = event.clientX - windowHalfX; - - targetRotation = targetRotationOnPointerDown + (pointerX - pointerXOnPointerDown) * 0.02; -} - -function onPointerUp() { - if (event.isPrimary === false) return; - - document.removeEventListener('pointermove', onPointerMove); - document.removeEventListener('pointerup', onPointerUp); -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - group.rotation.y += (targetRotation - group.rotation.y) * 0.05; - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_geometry_teapot.ts b/examples-testing/examples/webgl_geometry_teapot.ts deleted file mode 100644 index 4c884a559..000000000 --- a/examples-testing/examples/webgl_geometry_teapot.ts +++ /dev/null @@ -1,180 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js'; - -let camera, scene, renderer; -let cameraControls; -let effectController; -const teapotSize = 300; -let ambientLight, light; - -let tess = -1; // force initialization -let bBottom; -let bLid; -let bBody; -let bFitLid; -let bNonBlinn; -let shading; - -let teapot, textureCube; -const materials = {}; - -init(); -render(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - const canvasWidth = window.innerWidth; - const canvasHeight = window.innerHeight; - - // CAMERA - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 80000); - camera.position.set(-600, 550, 1300); - - // LIGHTS - ambientLight = new THREE.AmbientLight(0x7c7c7c, 3.0); - - light = new THREE.DirectionalLight(0xffffff, 3.0); - light.position.set(0.32, 0.39, 0.7); - - // RENDERER - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(canvasWidth, canvasHeight); - container.appendChild(renderer.domElement); - - // EVENTS - window.addEventListener('resize', onWindowResize); - - // CONTROLS - cameraControls = new OrbitControls(camera, renderer.domElement); - cameraControls.addEventListener('change', render); - - // TEXTURE MAP - const textureMap = new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg'); - textureMap.wrapS = textureMap.wrapT = THREE.RepeatWrapping; - textureMap.anisotropy = 16; - textureMap.colorSpace = THREE.SRGBColorSpace; - - // REFLECTION MAP - const path = 'textures/cube/pisa/'; - const urls = ['px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png']; - - textureCube = new THREE.CubeTextureLoader().setPath(path).load(urls); - - materials['wireframe'] = new THREE.MeshBasicMaterial({ wireframe: true }); - materials['flat'] = new THREE.MeshPhongMaterial({ specular: 0x000000, flatShading: true, side: THREE.DoubleSide }); - materials['smooth'] = new THREE.MeshLambertMaterial({ side: THREE.DoubleSide }); - materials['glossy'] = new THREE.MeshPhongMaterial({ side: THREE.DoubleSide }); - materials['textured'] = new THREE.MeshPhongMaterial({ map: textureMap, side: THREE.DoubleSide }); - materials['reflective'] = new THREE.MeshPhongMaterial({ envMap: textureCube, side: THREE.DoubleSide }); - - // scene itself - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xaaaaaa); - - scene.add(ambientLight); - scene.add(light); - - // GUI - setupGui(); -} - -// EVENT HANDLERS - -function onWindowResize() { - const canvasWidth = window.innerWidth; - const canvasHeight = window.innerHeight; - - renderer.setSize(canvasWidth, canvasHeight); - - camera.aspect = canvasWidth / canvasHeight; - camera.updateProjectionMatrix(); - - render(); -} - -function setupGui() { - effectController = { - newTess: 15, - bottom: true, - lid: true, - body: true, - fitLid: false, - nonblinn: false, - newShading: 'glossy', - }; - - const gui = new GUI(); - gui.add(effectController, 'newTess', [2, 3, 4, 5, 6, 8, 10, 15, 20, 30, 40, 50]) - .name('Tessellation Level') - .onChange(render); - gui.add(effectController, 'lid').name('display lid').onChange(render); - gui.add(effectController, 'body').name('display body').onChange(render); - gui.add(effectController, 'bottom').name('display bottom').onChange(render); - gui.add(effectController, 'fitLid').name('snug lid').onChange(render); - gui.add(effectController, 'nonblinn').name('original scale').onChange(render); - gui.add(effectController, 'newShading', ['wireframe', 'flat', 'smooth', 'glossy', 'textured', 'reflective']) - .name('Shading') - .onChange(render); -} - -// - -function render() { - if ( - effectController.newTess !== tess || - effectController.bottom !== bBottom || - effectController.lid !== bLid || - effectController.body !== bBody || - effectController.fitLid !== bFitLid || - effectController.nonblinn !== bNonBlinn || - effectController.newShading !== shading - ) { - tess = effectController.newTess; - bBottom = effectController.bottom; - bLid = effectController.lid; - bBody = effectController.body; - bFitLid = effectController.fitLid; - bNonBlinn = effectController.nonblinn; - shading = effectController.newShading; - - createNewTeapot(); - } - - // skybox is rendered separately, so that it is always behind the teapot. - if (shading === 'reflective') { - scene.background = textureCube; - } else { - scene.background = null; - } - - renderer.render(scene, camera); -} - -// Whenever the teapot changes, the scene is rebuilt from scratch (not much to it). -function createNewTeapot() { - if (teapot !== undefined) { - teapot.geometry.dispose(); - scene.remove(teapot); - } - - const geometry = new TeapotGeometry( - teapotSize, - tess, - effectController.bottom, - effectController.lid, - effectController.body, - effectController.fitLid, - !effectController.nonblinn, - ); - - teapot = new THREE.Mesh(geometry, materials[shading]); - - scene.add(teapot); -} diff --git a/examples-testing/examples/webgl_geometry_terrain.ts b/examples-testing/examples/webgl_geometry_terrain.ts deleted file mode 100644 index 8b6ed84ea..000000000 --- a/examples-testing/examples/webgl_geometry_terrain.ts +++ /dev/null @@ -1,173 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js'; -import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js'; - -let container, stats; -let camera, controls, scene, renderer; -let mesh, texture; - -const worldWidth = 256, - worldDepth = 256; -const clock = new THREE.Clock(); - -init(); - -function init() { - container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 10000); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xefd1b5); - scene.fog = new THREE.FogExp2(0xefd1b5, 0.0025); - - const data = generateHeight(worldWidth, worldDepth); - - camera.position.set(100, 800, -800); - camera.lookAt(-100, 810, -800); - - const geometry = new THREE.PlaneGeometry(7500, 7500, worldWidth - 1, worldDepth - 1); - geometry.rotateX(-Math.PI / 2); - - const vertices = geometry.attributes.position.array; - - for (let i = 0, j = 0, l = vertices.length; i < l; i++, j += 3) { - vertices[j + 1] = data[i] * 10; - } - - texture = new THREE.CanvasTexture(generateTexture(data, worldWidth, worldDepth)); - texture.wrapS = THREE.ClampToEdgeWrapping; - texture.wrapT = THREE.ClampToEdgeWrapping; - texture.colorSpace = THREE.SRGBColorSpace; - - mesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ map: texture })); - scene.add(mesh); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - controls = new FirstPersonControls(camera, renderer.domElement); - controls.movementSpeed = 150; - controls.lookSpeed = 0.1; - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - controls.handleResize(); -} - -function generateHeight(width, height) { - let seed = Math.PI / 4; - window.Math.random = function () { - const x = Math.sin(seed++) * 10000; - return x - Math.floor(x); - }; - - const size = width * height, - data = new Uint8Array(size); - const perlin = new ImprovedNoise(), - z = Math.random() * 100; - - let quality = 1; - - for (let j = 0; j < 4; j++) { - for (let i = 0; i < size; i++) { - const x = i % width, - y = ~~(i / width); - data[i] += Math.abs(perlin.noise(x / quality, y / quality, z) * quality * 1.75); - } - - quality *= 5; - } - - return data; -} - -function generateTexture(data, width, height) { - let context, image, imageData, shade; - - const vector3 = new THREE.Vector3(0, 0, 0); - - const sun = new THREE.Vector3(1, 1, 1); - sun.normalize(); - - const canvas = document.createElement('canvas'); - canvas.width = width; - canvas.height = height; - - context = canvas.getContext('2d'); - context.fillStyle = '#000'; - context.fillRect(0, 0, width, height); - - image = context.getImageData(0, 0, canvas.width, canvas.height); - imageData = image.data; - - for (let i = 0, j = 0, l = imageData.length; i < l; i += 4, j++) { - vector3.x = data[j - 2] - data[j + 2]; - vector3.y = 2; - vector3.z = data[j - width * 2] - data[j + width * 2]; - vector3.normalize(); - - shade = vector3.dot(sun); - - imageData[i] = (96 + shade * 128) * (0.5 + data[j] * 0.007); - imageData[i + 1] = (32 + shade * 96) * (0.5 + data[j] * 0.007); - imageData[i + 2] = shade * 96 * (0.5 + data[j] * 0.007); - } - - context.putImageData(image, 0, 0); - - // Scaled 4x - - const canvasScaled = document.createElement('canvas'); - canvasScaled.width = width * 4; - canvasScaled.height = height * 4; - - context = canvasScaled.getContext('2d'); - context.scale(4, 4); - context.drawImage(canvas, 0, 0); - - image = context.getImageData(0, 0, canvasScaled.width, canvasScaled.height); - imageData = image.data; - - for (let i = 0, l = imageData.length; i < l; i += 4) { - const v = ~~(Math.random() * 5); - - imageData[i] += v; - imageData[i + 1] += v; - imageData[i + 2] += v; - } - - context.putImageData(image, 0, 0); - - return canvasScaled; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - controls.update(clock.getDelta()); - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_geometry_terrain_raycast.ts b/examples-testing/examples/webgl_geometry_terrain_raycast.ts deleted file mode 100644 index f1383c138..000000000 --- a/examples-testing/examples/webgl_geometry_terrain_raycast.ts +++ /dev/null @@ -1,206 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js'; - -let container, stats; - -let camera, controls, scene, renderer; - -let mesh, texture; - -const worldWidth = 256, - worldDepth = 256, - worldHalfWidth = worldWidth / 2, - worldHalfDepth = worldDepth / 2; - -let helper; - -const raycaster = new THREE.Raycaster(); -const pointer = new THREE.Vector2(); - -init(); - -function init() { - container = document.getElementById('container'); - container.innerHTML = ''; - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xbfd1e5); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 10, 20000); - - controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 1000; - controls.maxDistance = 10000; - controls.maxPolarAngle = Math.PI / 2; - - // - - const data = generateHeight(worldWidth, worldDepth); - - controls.target.y = data[worldHalfWidth + worldHalfDepth * worldWidth] + 500; - camera.position.y = controls.target.y + 2000; - camera.position.x = 2000; - controls.update(); - - const geometry = new THREE.PlaneGeometry(7500, 7500, worldWidth - 1, worldDepth - 1); - geometry.rotateX(-Math.PI / 2); - - const vertices = geometry.attributes.position.array; - - for (let i = 0, j = 0, l = vertices.length; i < l; i++, j += 3) { - vertices[j + 1] = data[i] * 10; - } - - // - - texture = new THREE.CanvasTexture(generateTexture(data, worldWidth, worldDepth)); - texture.wrapS = THREE.ClampToEdgeWrapping; - texture.wrapT = THREE.ClampToEdgeWrapping; - texture.colorSpace = THREE.SRGBColorSpace; - - mesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ map: texture })); - scene.add(mesh); - - const geometryHelper = new THREE.ConeGeometry(20, 100, 3); - geometryHelper.translate(0, 50, 0); - geometryHelper.rotateX(Math.PI / 2); - helper = new THREE.Mesh(geometryHelper, new THREE.MeshNormalMaterial()); - scene.add(helper); - - container.addEventListener('pointermove', onPointerMove); - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function generateHeight(width, height) { - const size = width * height, - data = new Uint8Array(size), - perlin = new ImprovedNoise(), - z = Math.random() * 100; - - let quality = 1; - - for (let j = 0; j < 4; j++) { - for (let i = 0; i < size; i++) { - const x = i % width, - y = ~~(i / width); - data[i] += Math.abs(perlin.noise(x / quality, y / quality, z) * quality * 1.75); - } - - quality *= 5; - } - - return data; -} - -function generateTexture(data, width, height) { - // bake lighting into texture - - let context, image, imageData, shade; - - const vector3 = new THREE.Vector3(0, 0, 0); - - const sun = new THREE.Vector3(1, 1, 1); - sun.normalize(); - - const canvas = document.createElement('canvas'); - canvas.width = width; - canvas.height = height; - - context = canvas.getContext('2d'); - context.fillStyle = '#000'; - context.fillRect(0, 0, width, height); - - image = context.getImageData(0, 0, canvas.width, canvas.height); - imageData = image.data; - - for (let i = 0, j = 0, l = imageData.length; i < l; i += 4, j++) { - vector3.x = data[j - 2] - data[j + 2]; - vector3.y = 2; - vector3.z = data[j - width * 2] - data[j + width * 2]; - vector3.normalize(); - - shade = vector3.dot(sun); - - imageData[i] = (96 + shade * 128) * (0.5 + data[j] * 0.007); - imageData[i + 1] = (32 + shade * 96) * (0.5 + data[j] * 0.007); - imageData[i + 2] = shade * 96 * (0.5 + data[j] * 0.007); - } - - context.putImageData(image, 0, 0); - - // Scaled 4x - - const canvasScaled = document.createElement('canvas'); - canvasScaled.width = width * 4; - canvasScaled.height = height * 4; - - context = canvasScaled.getContext('2d'); - context.scale(4, 4); - context.drawImage(canvas, 0, 0); - - image = context.getImageData(0, 0, canvasScaled.width, canvasScaled.height); - imageData = image.data; - - for (let i = 0, l = imageData.length; i < l; i += 4) { - const v = ~~(Math.random() * 5); - - imageData[i] += v; - imageData[i + 1] += v; - imageData[i + 2] += v; - } - - context.putImageData(image, 0, 0); - - return canvasScaled; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - renderer.render(scene, camera); -} - -function onPointerMove(event) { - pointer.x = (event.clientX / renderer.domElement.clientWidth) * 2 - 1; - pointer.y = -(event.clientY / renderer.domElement.clientHeight) * 2 + 1; - raycaster.setFromCamera(pointer, camera); - - // See if the ray from the camera into the world hits one of our meshes - const intersects = raycaster.intersectObject(mesh); - - // Toggle rotation bool for meshes that we clicked - if (intersects.length > 0) { - helper.position.set(0, 0, 0); - helper.lookAt(intersects[0].face.normal); - - helper.position.copy(intersects[0].point); - } -} diff --git a/examples-testing/examples/webgl_geometry_text.ts b/examples-testing/examples/webgl_geometry_text.ts deleted file mode 100644 index 831ebcd6b..000000000 --- a/examples-testing/examples/webgl_geometry_text.ts +++ /dev/null @@ -1,312 +0,0 @@ -import * as THREE from 'three'; - -import { FontLoader } from 'three/addons/loaders/FontLoader.js'; -import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -THREE.Cache.enabled = true; - -let container; - -let camera, cameraTarget, scene, renderer; - -let group, textMesh1, textMesh2, textGeo, materials; - -let firstLetter = true; - -let text = 'three.js', - bevelEnabled = true, - font = undefined, - fontName = 'optimer', // helvetiker, optimer, gentilis, droid sans, droid serif - fontWeight = 'bold'; // normal bold - -const depth = 20, - size = 70, - hover = 30, - curveSegments = 4, - bevelThickness = 2, - bevelSize = 1.5; - -const mirror = true; - -const fontMap = { - helvetiker: 0, - optimer: 1, - gentilis: 2, - 'droid/droid_sans': 3, - 'droid/droid_serif': 4, -}; - -const weightMap = { - regular: 0, - bold: 1, -}; - -const reverseFontMap = []; -const reverseWeightMap = []; - -for (const i in fontMap) reverseFontMap[fontMap[i]] = i; -for (const i in weightMap) reverseWeightMap[weightMap[i]] = i; - -let targetRotation = 0; -let targetRotationOnPointerDown = 0; - -let pointerX = 0; -let pointerXOnPointerDown = 0; - -let windowHalfX = window.innerWidth / 2; - -let fontIndex = 1; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - // CAMERA - - camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 1500); - camera.position.set(0, 400, 700); - - cameraTarget = new THREE.Vector3(0, 150, 0); - - // SCENE - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x000000); - scene.fog = new THREE.Fog(0x000000, 250, 1400); - - // LIGHTS - - const dirLight = new THREE.DirectionalLight(0xffffff, 0.4); - dirLight.position.set(0, 0, 1).normalize(); - scene.add(dirLight); - - const pointLight = new THREE.PointLight(0xffffff, 4.5, 0, 0); - pointLight.color.setHSL(Math.random(), 1, 0.5); - pointLight.position.set(0, 100, 90); - scene.add(pointLight); - - materials = [ - new THREE.MeshPhongMaterial({ color: 0xffffff, flatShading: true }), // front - new THREE.MeshPhongMaterial({ color: 0xffffff }), // side - ]; - - group = new THREE.Group(); - group.position.y = 100; - - scene.add(group); - - loadFont(); - - const plane = new THREE.Mesh( - new THREE.PlaneGeometry(10000, 10000), - new THREE.MeshBasicMaterial({ color: 0xffffff, opacity: 0.5, transparent: true }), - ); - plane.position.y = 100; - plane.rotation.x = -Math.PI / 2; - scene.add(plane); - - // RENDERER - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // EVENTS - - container.style.touchAction = 'none'; - container.addEventListener('pointerdown', onPointerDown); - - document.addEventListener('keypress', onDocumentKeyPress); - document.addEventListener('keydown', onDocumentKeyDown); - - // - - const params = { - changeColor: function () { - pointLight.color.setHSL(Math.random(), 1, 0.5); - }, - changeFont: function () { - fontIndex++; - - fontName = reverseFontMap[fontIndex % reverseFontMap.length]; - - loadFont(); - }, - changeWeight: function () { - if (fontWeight === 'bold') { - fontWeight = 'regular'; - } else { - fontWeight = 'bold'; - } - - loadFont(); - }, - changeBevel: function () { - bevelEnabled = !bevelEnabled; - - refreshText(); - }, - }; - - // - - const gui = new GUI(); - - gui.add(params, 'changeColor').name('change color'); - gui.add(params, 'changeFont').name('change font'); - gui.add(params, 'changeWeight').name('change weight'); - gui.add(params, 'changeBevel').name('change bevel'); - gui.open(); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function onDocumentKeyDown(event) { - if (firstLetter) { - firstLetter = false; - text = ''; - } - - const keyCode = event.keyCode; - - // backspace - - if (keyCode == 8) { - event.preventDefault(); - - text = text.substring(0, text.length - 1); - refreshText(); - - return false; - } -} - -function onDocumentKeyPress(event) { - const keyCode = event.which; - - // backspace - - if (keyCode == 8) { - event.preventDefault(); - } else { - const ch = String.fromCharCode(keyCode); - text += ch; - - refreshText(); - } -} - -function loadFont() { - const loader = new FontLoader(); - loader.load('fonts/' + fontName + '_' + fontWeight + '.typeface.json', function (response) { - font = response; - - refreshText(); - }); -} - -function createText() { - textGeo = new TextGeometry(text, { - font: font, - - size: size, - depth: depth, - curveSegments: curveSegments, - - bevelThickness: bevelThickness, - bevelSize: bevelSize, - bevelEnabled: bevelEnabled, - }); - - textGeo.computeBoundingBox(); - - const centerOffset = -0.5 * (textGeo.boundingBox.max.x - textGeo.boundingBox.min.x); - - textMesh1 = new THREE.Mesh(textGeo, materials); - - textMesh1.position.x = centerOffset; - textMesh1.position.y = hover; - textMesh1.position.z = 0; - - textMesh1.rotation.x = 0; - textMesh1.rotation.y = Math.PI * 2; - - group.add(textMesh1); - - if (mirror) { - textMesh2 = new THREE.Mesh(textGeo, materials); - - textMesh2.position.x = centerOffset; - textMesh2.position.y = -hover; - textMesh2.position.z = depth; - - textMesh2.rotation.x = Math.PI; - textMesh2.rotation.y = Math.PI * 2; - - group.add(textMesh2); - } -} - -function refreshText() { - group.remove(textMesh1); - if (mirror) group.remove(textMesh2); - - if (!text) return; - - createText(); -} - -function onPointerDown(event) { - if (event.isPrimary === false) return; - - pointerXOnPointerDown = event.clientX - windowHalfX; - targetRotationOnPointerDown = targetRotation; - - document.addEventListener('pointermove', onPointerMove); - document.addEventListener('pointerup', onPointerUp); -} - -function onPointerMove(event) { - if (event.isPrimary === false) return; - - pointerX = event.clientX - windowHalfX; - - targetRotation = targetRotationOnPointerDown + (pointerX - pointerXOnPointerDown) * 0.02; -} - -function onPointerUp() { - if (event.isPrimary === false) return; - - document.removeEventListener('pointermove', onPointerMove); - document.removeEventListener('pointerup', onPointerUp); -} - -// - -function animate() { - group.rotation.y += (targetRotation - group.rotation.y) * 0.05; - - camera.lookAt(cameraTarget); - - renderer.clear(); - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_geometry_text_shapes.ts b/examples-testing/examples/webgl_geometry_text_shapes.ts deleted file mode 100644 index adfb6008d..000000000 --- a/examples-testing/examples/webgl_geometry_text_shapes.ts +++ /dev/null @@ -1,112 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { FontLoader } from 'three/addons/loaders/FontLoader.js'; - -let camera, scene, renderer; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.set(0, -400, 600); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - const loader = new FontLoader(); - loader.load('fonts/helvetiker_regular.typeface.json', function (font) { - const color = 0x006699; - - const matDark = new THREE.LineBasicMaterial({ - color: color, - side: THREE.DoubleSide, - }); - - const matLite = new THREE.MeshBasicMaterial({ - color: color, - transparent: true, - opacity: 0.4, - side: THREE.DoubleSide, - }); - - const message = ' Three.js\nSimple text.'; - - const shapes = font.generateShapes(message, 100); - - const geometry = new THREE.ShapeGeometry(shapes); - - geometry.computeBoundingBox(); - - const xMid = -0.5 * (geometry.boundingBox.max.x - geometry.boundingBox.min.x); - - geometry.translate(xMid, 0, 0); - - // make shape ( N.B. edge view not visible ) - - const text = new THREE.Mesh(geometry, matLite); - text.position.z = -150; - scene.add(text); - - // make line shape ( N.B. edge view remains visible ) - - const holeShapes = []; - - for (let i = 0; i < shapes.length; i++) { - const shape = shapes[i]; - - if (shape.holes && shape.holes.length > 0) { - for (let j = 0; j < shape.holes.length; j++) { - const hole = shape.holes[j]; - holeShapes.push(hole); - } - } - } - - shapes.push.apply(shapes, holeShapes); - - const lineText = new THREE.Object3D(); - - for (let i = 0; i < shapes.length; i++) { - const shape = shapes[i]; - - const points = shape.getPoints(); - const geometry = new THREE.BufferGeometry().setFromPoints(points); - - geometry.translate(xMid, 0, 0); - - const lineMesh = new THREE.Line(geometry, matDark); - lineText.add(lineMesh); - } - - scene.add(lineText); - - render(); - }); //end load function - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 0, 0); - controls.update(); - - controls.addEventListener('change', render); - - window.addEventListener('resize', onWindowResize); -} // end init - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_geometry_text_stroke.ts b/examples-testing/examples/webgl_geometry_text_stroke.ts deleted file mode 100644 index 9a1983253..000000000 --- a/examples-testing/examples/webgl_geometry_text_stroke.ts +++ /dev/null @@ -1,116 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { SVGLoader } from 'three/addons/loaders/SVGLoader.js'; -import { FontLoader } from 'three/addons/loaders/FontLoader.js'; - -let camera, scene, renderer; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.set(0, -400, 600); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - const loader = new FontLoader(); - loader.load('fonts/helvetiker_regular.typeface.json', function (font) { - const color = new THREE.Color(0x006699); - - const matDark = new THREE.MeshBasicMaterial({ - color: color, - side: THREE.DoubleSide, - }); - - const matLite = new THREE.MeshBasicMaterial({ - color: color, - transparent: true, - opacity: 0.4, - side: THREE.DoubleSide, - }); - - const message = ' Three.js\nStroke text.'; - - const shapes = font.generateShapes(message, 100); - - const geometry = new THREE.ShapeGeometry(shapes); - - geometry.computeBoundingBox(); - - const xMid = -0.5 * (geometry.boundingBox.max.x - geometry.boundingBox.min.x); - - geometry.translate(xMid, 0, 0); - - // make shape ( N.B. edge view not visible ) - - const text = new THREE.Mesh(geometry, matLite); - text.position.z = -150; - scene.add(text); - - // make line shape ( N.B. edge view remains visible ) - - const holeShapes = []; - - for (let i = 0; i < shapes.length; i++) { - const shape = shapes[i]; - - if (shape.holes && shape.holes.length > 0) { - for (let j = 0; j < shape.holes.length; j++) { - const hole = shape.holes[j]; - holeShapes.push(hole); - } - } - } - - shapes.push.apply(shapes, holeShapes); - - const style = SVGLoader.getStrokeStyle(5, color.getStyle()); - - const strokeText = new THREE.Group(); - - for (let i = 0; i < shapes.length; i++) { - const shape = shapes[i]; - - const points = shape.getPoints(); - - const geometry = SVGLoader.pointsToStroke(points, style); - - geometry.translate(xMid, 0, 0); - - const strokeMesh = new THREE.Mesh(geometry, matDark); - strokeText.add(strokeMesh); - } - - scene.add(strokeText); - - render(); - }); //end load function - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 0, 0); - controls.update(); - - controls.addEventListener('change', render); - - window.addEventListener('resize', onWindowResize); -} // end init - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_gpgpu_birds.ts b/examples-testing/examples/webgl_gpgpu_birds.ts deleted file mode 100644 index 20a5e0d97..000000000 --- a/examples-testing/examples/webgl_gpgpu_birds.ts +++ /dev/null @@ -1,313 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { GPUComputationRenderer } from 'three/addons/misc/GPUComputationRenderer.js'; - -/* TEXTURE WIDTH FOR SIMULATION */ -const WIDTH = 32; - -const BIRDS = WIDTH * WIDTH; - -// Custom Geometry - using 3 triangles each. No UVs, no normals currently. -class BirdGeometry extends THREE.BufferGeometry { - constructor() { - super(); - - const trianglesPerBird = 3; - const triangles = BIRDS * trianglesPerBird; - const points = triangles * 3; - - const vertices = new THREE.BufferAttribute(new Float32Array(points * 3), 3); - const birdColors = new THREE.BufferAttribute(new Float32Array(points * 3), 3); - const references = new THREE.BufferAttribute(new Float32Array(points * 2), 2); - const birdVertex = new THREE.BufferAttribute(new Float32Array(points), 1); - - this.setAttribute('position', vertices); - this.setAttribute('birdColor', birdColors); - this.setAttribute('reference', references); - this.setAttribute('birdVertex', birdVertex); - - // this.setAttribute( 'normal', new Float32Array( points * 3 ), 3 ); - - let v = 0; - - function verts_push() { - for (let i = 0; i < arguments.length; i++) { - vertices.array[v++] = arguments[i]; - } - } - - const wingsSpan = 20; - - for (let f = 0; f < BIRDS; f++) { - // Body - - verts_push(0, -0, -20, 0, 4, -20, 0, 0, 30); - - // Wings - - verts_push(0, 0, -15, -wingsSpan, 0, 0, 0, 0, 15); - - verts_push(0, 0, 15, wingsSpan, 0, 0, 0, 0, -15); - } - - for (let v = 0; v < triangles * 3; v++) { - const triangleIndex = ~~(v / 3); - const birdIndex = ~~(triangleIndex / trianglesPerBird); - const x = (birdIndex % WIDTH) / WIDTH; - const y = ~~(birdIndex / WIDTH) / WIDTH; - - const c = new THREE.Color(0x666666 + (~~(v / 9) / BIRDS) * 0x666666); - - birdColors.array[v * 3 + 0] = c.r; - birdColors.array[v * 3 + 1] = c.g; - birdColors.array[v * 3 + 2] = c.b; - - references.array[v * 2] = x; - references.array[v * 2 + 1] = y; - - birdVertex.array[v] = v % 9; - } - - this.scale(0.2, 0.2, 0.2); - } -} - -// - -let container, stats; -let camera, scene, renderer; -let mouseX = 0, - mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -const BOUNDS = 800, - BOUNDS_HALF = BOUNDS / 2; - -let last = performance.now(); - -let gpuCompute; -let velocityVariable; -let positionVariable; -let positionUniforms; -let velocityUniforms; -let birdUniforms; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 3000); - camera.position.z = 350; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - scene.fog = new THREE.Fog(0xffffff, 100, 1000); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - initComputeRenderer(); - - stats = new Stats(); - container.appendChild(stats.dom); - - container.style.touchAction = 'none'; - container.addEventListener('pointermove', onPointerMove); - - // - - window.addEventListener('resize', onWindowResize); - - const gui = new GUI(); - - const effectController = { - separation: 20.0, - alignment: 20.0, - cohesion: 20.0, - freedom: 0.75, - }; - - const valuesChanger = function () { - velocityUniforms['separationDistance'].value = effectController.separation; - velocityUniforms['alignmentDistance'].value = effectController.alignment; - velocityUniforms['cohesionDistance'].value = effectController.cohesion; - velocityUniforms['freedomFactor'].value = effectController.freedom; - }; - - valuesChanger(); - - gui.add(effectController, 'separation', 0.0, 100.0, 1.0).onChange(valuesChanger); - gui.add(effectController, 'alignment', 0.0, 100, 0.001).onChange(valuesChanger); - gui.add(effectController, 'cohesion', 0.0, 100, 0.025).onChange(valuesChanger); - gui.close(); - - initBirds(); -} - -function initComputeRenderer() { - gpuCompute = new GPUComputationRenderer(WIDTH, WIDTH, renderer); - - const dtPosition = gpuCompute.createTexture(); - const dtVelocity = gpuCompute.createTexture(); - fillPositionTexture(dtPosition); - fillVelocityTexture(dtVelocity); - - velocityVariable = gpuCompute.addVariable( - 'textureVelocity', - document.getElementById('fragmentShaderVelocity').textContent, - dtVelocity, - ); - positionVariable = gpuCompute.addVariable( - 'texturePosition', - document.getElementById('fragmentShaderPosition').textContent, - dtPosition, - ); - - gpuCompute.setVariableDependencies(velocityVariable, [positionVariable, velocityVariable]); - gpuCompute.setVariableDependencies(positionVariable, [positionVariable, velocityVariable]); - - positionUniforms = positionVariable.material.uniforms; - velocityUniforms = velocityVariable.material.uniforms; - - positionUniforms['time'] = { value: 0.0 }; - positionUniforms['delta'] = { value: 0.0 }; - velocityUniforms['time'] = { value: 1.0 }; - velocityUniforms['delta'] = { value: 0.0 }; - velocityUniforms['testing'] = { value: 1.0 }; - velocityUniforms['separationDistance'] = { value: 1.0 }; - velocityUniforms['alignmentDistance'] = { value: 1.0 }; - velocityUniforms['cohesionDistance'] = { value: 1.0 }; - velocityUniforms['freedomFactor'] = { value: 1.0 }; - velocityUniforms['predator'] = { value: new THREE.Vector3() }; - velocityVariable.material.defines.BOUNDS = BOUNDS.toFixed(2); - - velocityVariable.wrapS = THREE.RepeatWrapping; - velocityVariable.wrapT = THREE.RepeatWrapping; - positionVariable.wrapS = THREE.RepeatWrapping; - positionVariable.wrapT = THREE.RepeatWrapping; - - const error = gpuCompute.init(); - - if (error !== null) { - console.error(error); - } -} - -function initBirds() { - const geometry = new BirdGeometry(); - - // For Vertex and Fragment - birdUniforms = { - color: { value: new THREE.Color(0xff2200) }, - texturePosition: { value: null }, - textureVelocity: { value: null }, - time: { value: 1.0 }, - delta: { value: 0.0 }, - }; - - // THREE.ShaderMaterial - const material = new THREE.ShaderMaterial({ - uniforms: birdUniforms, - vertexShader: document.getElementById('birdVS').textContent, - fragmentShader: document.getElementById('birdFS').textContent, - side: THREE.DoubleSide, - }); - - const birdMesh = new THREE.Mesh(geometry, material); - birdMesh.rotation.y = Math.PI / 2; - birdMesh.matrixAutoUpdate = false; - birdMesh.updateMatrix(); - - scene.add(birdMesh); -} - -function fillPositionTexture(texture) { - const theArray = texture.image.data; - - for (let k = 0, kl = theArray.length; k < kl; k += 4) { - const x = Math.random() * BOUNDS - BOUNDS_HALF; - const y = Math.random() * BOUNDS - BOUNDS_HALF; - const z = Math.random() * BOUNDS - BOUNDS_HALF; - - theArray[k + 0] = x; - theArray[k + 1] = y; - theArray[k + 2] = z; - theArray[k + 3] = 1; - } -} - -function fillVelocityTexture(texture) { - const theArray = texture.image.data; - - for (let k = 0, kl = theArray.length; k < kl; k += 4) { - const x = Math.random() - 0.5; - const y = Math.random() - 0.5; - const z = Math.random() - 0.5; - - theArray[k + 0] = x * 10; - theArray[k + 1] = y * 10; - theArray[k + 2] = z * 10; - theArray[k + 3] = 1; - } -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onPointerMove(event) { - if (event.isPrimary === false) return; - - mouseX = event.clientX - windowHalfX; - mouseY = event.clientY - windowHalfY; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - const now = performance.now(); - let delta = (now - last) / 1000; - - if (delta > 1) delta = 1; // safety cap on large deltas - last = now; - - positionUniforms['time'].value = now; - positionUniforms['delta'].value = delta; - velocityUniforms['time'].value = now; - velocityUniforms['delta'].value = delta; - birdUniforms['time'].value = now; - birdUniforms['delta'].value = delta; - - velocityUniforms['predator'].value.set((0.5 * mouseX) / windowHalfX, (-0.5 * mouseY) / windowHalfY, 0); - - mouseX = 10000; - mouseY = 10000; - - gpuCompute.compute(); - - birdUniforms['texturePosition'].value = gpuCompute.getCurrentRenderTarget(positionVariable).texture; - birdUniforms['textureVelocity'].value = gpuCompute.getCurrentRenderTarget(velocityVariable).texture; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_gpgpu_birds_gltf.ts b/examples-testing/examples/webgl_gpgpu_birds_gltf.ts deleted file mode 100644 index 3176b95a9..000000000 --- a/examples-testing/examples/webgl_gpgpu_birds_gltf.ts +++ /dev/null @@ -1,415 +0,0 @@ -import * as THREE from 'three'; -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { GPUComputationRenderer } from 'three/addons/misc/GPUComputationRenderer.js'; - -/* TEXTURE WIDTH FOR SIMULATION */ -const WIDTH = 64; -const BIRDS = WIDTH * WIDTH; - -/* BAKE ANIMATION INTO TEXTURE and CREATE GEOMETRY FROM BASE MODEL */ -const BirdGeometry = new THREE.BufferGeometry(); -let textureAnimation, durationAnimation, birdMesh, materialShader, indicesPerBird; - -function nextPowerOf2(n) { - return Math.pow(2, Math.ceil(Math.log(n) / Math.log(2))); -} - -Math.lerp = function (value1, value2, amount) { - amount = Math.max(Math.min(amount, 1), 0); - return value1 + (value2 - value1) * amount; -}; - -const gltfs = ['models/gltf/Parrot.glb', 'models/gltf/Flamingo.glb']; -const colors = [0xccffff, 0xffdeff]; -const sizes = [0.2, 0.1]; -const selectModel = Math.floor(Math.random() * gltfs.length); -new GLTFLoader().load(gltfs[selectModel], function (gltf) { - const animations = gltf.animations; - durationAnimation = Math.round(animations[0].duration * 60); - const birdGeo = gltf.scene.children[0].geometry; - const morphAttributes = birdGeo.morphAttributes.position; - const tHeight = nextPowerOf2(durationAnimation); - const tWidth = nextPowerOf2(birdGeo.getAttribute('position').count); - indicesPerBird = birdGeo.index.count; - const tData = new Float32Array(4 * tWidth * tHeight); - - for (let i = 0; i < tWidth; i++) { - for (let j = 0; j < tHeight; j++) { - const offset = j * tWidth * 4; - - const curMorph = Math.floor((j / durationAnimation) * morphAttributes.length); - const nextMorph = - (Math.floor((j / durationAnimation) * morphAttributes.length) + 1) % morphAttributes.length; - const lerpAmount = ((j / durationAnimation) * morphAttributes.length) % 1; - - if (j < durationAnimation) { - let d0, d1; - - d0 = morphAttributes[curMorph].array[i * 3]; - d1 = morphAttributes[nextMorph].array[i * 3]; - - if (d0 !== undefined && d1 !== undefined) tData[offset + i * 4] = Math.lerp(d0, d1, lerpAmount); - - d0 = morphAttributes[curMorph].array[i * 3 + 1]; - d1 = morphAttributes[nextMorph].array[i * 3 + 1]; - - if (d0 !== undefined && d1 !== undefined) tData[offset + i * 4 + 1] = Math.lerp(d0, d1, lerpAmount); - - d0 = morphAttributes[curMorph].array[i * 3 + 2]; - d1 = morphAttributes[nextMorph].array[i * 3 + 2]; - - if (d0 !== undefined && d1 !== undefined) tData[offset + i * 4 + 2] = Math.lerp(d0, d1, lerpAmount); - - tData[offset + i * 4 + 3] = 1; - } - } - } - - textureAnimation = new THREE.DataTexture(tData, tWidth, tHeight, THREE.RGBAFormat, THREE.FloatType); - textureAnimation.needsUpdate = true; - - const vertices = [], - color = [], - reference = [], - seeds = [], - indices = []; - const totalVertices = birdGeo.getAttribute('position').count * 3 * BIRDS; - for (let i = 0; i < totalVertices; i++) { - const bIndex = i % (birdGeo.getAttribute('position').count * 3); - vertices.push(birdGeo.getAttribute('position').array[bIndex]); - color.push(birdGeo.getAttribute('color').array[bIndex]); - } - - let r = Math.random(); - for (let i = 0; i < birdGeo.getAttribute('position').count * BIRDS; i++) { - const bIndex = i % birdGeo.getAttribute('position').count; - const bird = Math.floor(i / birdGeo.getAttribute('position').count); - if (bIndex == 0) r = Math.random(); - const j = ~~bird; - const x = (j % WIDTH) / WIDTH; - const y = ~~(j / WIDTH) / WIDTH; - reference.push(x, y, bIndex / tWidth, durationAnimation / tHeight); - seeds.push(bird, r, Math.random(), Math.random()); - } - - for (let i = 0; i < birdGeo.index.array.length * BIRDS; i++) { - const offset = Math.floor(i / birdGeo.index.array.length) * birdGeo.getAttribute('position').count; - indices.push(birdGeo.index.array[i % birdGeo.index.array.length] + offset); - } - - BirdGeometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(vertices), 3)); - BirdGeometry.setAttribute('birdColor', new THREE.BufferAttribute(new Float32Array(color), 3)); - BirdGeometry.setAttribute('color', new THREE.BufferAttribute(new Float32Array(color), 3)); - BirdGeometry.setAttribute('reference', new THREE.BufferAttribute(new Float32Array(reference), 4)); - BirdGeometry.setAttribute('seeds', new THREE.BufferAttribute(new Float32Array(seeds), 4)); - - BirdGeometry.setIndex(indices); - - init(); -}); - -let container, stats; -let camera, scene, renderer; -let mouseX = 0, - mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -const BOUNDS = 800, - BOUNDS_HALF = BOUNDS / 2; - -let last = performance.now(); - -let gpuCompute; -let velocityVariable; -let positionVariable; -let positionUniforms; -let velocityUniforms; - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 3000); - camera.position.z = 350; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(colors[selectModel]); - scene.fog = new THREE.Fog(colors[selectModel], 100, 1000); - - // LIGHTS - - const hemiLight = new THREE.HemisphereLight(colors[selectModel], 0xffffff, 4.5); - hemiLight.color.setHSL(0.6, 1, 0.6, THREE.SRGBColorSpace); - hemiLight.groundColor.setHSL(0.095, 1, 0.75, THREE.SRGBColorSpace); - hemiLight.position.set(0, 50, 0); - scene.add(hemiLight); - - const dirLight = new THREE.DirectionalLight(0x00ced1, 2.0); - dirLight.color.setHSL(0.1, 1, 0.95, THREE.SRGBColorSpace); - dirLight.position.set(-1, 1.75, 1); - dirLight.position.multiplyScalar(30); - scene.add(dirLight); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - initComputeRenderer(); - - stats = new Stats(); - container.appendChild(stats.dom); - - container.style.touchAction = 'none'; - container.addEventListener('pointermove', onPointerMove); - - window.addEventListener('resize', onWindowResize); - - const gui = new GUI(); - - const effectController = { - separation: 20.0, - alignment: 20.0, - cohesion: 20.0, - freedom: 0.75, - size: sizes[selectModel], - count: Math.floor(BIRDS / 4), - }; - - const valuesChanger = function () { - velocityUniforms['separationDistance'].value = effectController.separation; - velocityUniforms['alignmentDistance'].value = effectController.alignment; - velocityUniforms['cohesionDistance'].value = effectController.cohesion; - velocityUniforms['freedomFactor'].value = effectController.freedom; - if (materialShader) materialShader.uniforms['size'].value = effectController.size; - BirdGeometry.setDrawRange(0, indicesPerBird * effectController.count); - }; - - valuesChanger(); - - gui.add(effectController, 'separation', 0.0, 100.0, 1.0).onChange(valuesChanger); - gui.add(effectController, 'alignment', 0.0, 100, 0.001).onChange(valuesChanger); - gui.add(effectController, 'cohesion', 0.0, 100, 0.025).onChange(valuesChanger); - gui.add(effectController, 'size', 0, 1, 0.01).onChange(valuesChanger); - gui.add(effectController, 'count', 0, BIRDS, 1).onChange(valuesChanger); - gui.close(); - - initBirds(effectController); -} - -function initComputeRenderer() { - gpuCompute = new GPUComputationRenderer(WIDTH, WIDTH, renderer); - - const dtPosition = gpuCompute.createTexture(); - const dtVelocity = gpuCompute.createTexture(); - fillPositionTexture(dtPosition); - fillVelocityTexture(dtVelocity); - - velocityVariable = gpuCompute.addVariable( - 'textureVelocity', - document.getElementById('fragmentShaderVelocity').textContent, - dtVelocity, - ); - positionVariable = gpuCompute.addVariable( - 'texturePosition', - document.getElementById('fragmentShaderPosition').textContent, - dtPosition, - ); - - gpuCompute.setVariableDependencies(velocityVariable, [positionVariable, velocityVariable]); - gpuCompute.setVariableDependencies(positionVariable, [positionVariable, velocityVariable]); - - positionUniforms = positionVariable.material.uniforms; - velocityUniforms = velocityVariable.material.uniforms; - - positionUniforms['time'] = { value: 0.0 }; - positionUniforms['delta'] = { value: 0.0 }; - velocityUniforms['time'] = { value: 1.0 }; - velocityUniforms['delta'] = { value: 0.0 }; - velocityUniforms['testing'] = { value: 1.0 }; - velocityUniforms['separationDistance'] = { value: 1.0 }; - velocityUniforms['alignmentDistance'] = { value: 1.0 }; - velocityUniforms['cohesionDistance'] = { value: 1.0 }; - velocityUniforms['freedomFactor'] = { value: 1.0 }; - velocityUniforms['predator'] = { value: new THREE.Vector3() }; - velocityVariable.material.defines.BOUNDS = BOUNDS.toFixed(2); - - velocityVariable.wrapS = THREE.RepeatWrapping; - velocityVariable.wrapT = THREE.RepeatWrapping; - positionVariable.wrapS = THREE.RepeatWrapping; - positionVariable.wrapT = THREE.RepeatWrapping; - - const error = gpuCompute.init(); - - if (error !== null) { - console.error(error); - } -} - -function initBirds(effectController) { - const geometry = BirdGeometry; - - const m = new THREE.MeshStandardMaterial({ - vertexColors: true, - flatShading: true, - roughness: 1, - metalness: 0, - }); - - m.onBeforeCompile = shader => { - shader.uniforms.texturePosition = { value: null }; - shader.uniforms.textureVelocity = { value: null }; - shader.uniforms.textureAnimation = { value: textureAnimation }; - shader.uniforms.time = { value: 1.0 }; - shader.uniforms.size = { value: effectController.size }; - shader.uniforms.delta = { value: 0.0 }; - - let token = '#define STANDARD'; - - let insert = /* glsl */ ` - attribute vec4 reference; - attribute vec4 seeds; - attribute vec3 birdColor; - uniform sampler2D texturePosition; - uniform sampler2D textureVelocity; - uniform sampler2D textureAnimation; - uniform float size; - uniform float time; - `; - - shader.vertexShader = shader.vertexShader.replace(token, token + insert); - - token = '#include '; - - insert = /* glsl */ ` - vec4 tmpPos = texture2D( texturePosition, reference.xy ); - - vec3 pos = tmpPos.xyz; - vec3 velocity = normalize(texture2D( textureVelocity, reference.xy ).xyz); - vec3 aniPos = texture2D( textureAnimation, vec2( reference.z, mod( time + ( seeds.x ) * ( ( 0.0004 + seeds.y / 10000.0) + normalize( velocity ) / 20000.0 ), reference.w ) ) ).xyz; - vec3 newPosition = position; - - newPosition = mat3( modelMatrix ) * ( newPosition + aniPos ); - newPosition *= size + seeds.y * size * 0.2; - - velocity.z *= -1.; - float xz = length( velocity.xz ); - float xyz = 1.; - float x = sqrt( 1. - velocity.y * velocity.y ); - - float cosry = velocity.x / xz; - float sinry = velocity.z / xz; - - float cosrz = x / xyz; - float sinrz = velocity.y / xyz; - - mat3 maty = mat3( cosry, 0, -sinry, 0 , 1, 0 , sinry, 0, cosry ); - mat3 matz = mat3( cosrz , sinrz, 0, -sinrz, cosrz, 0, 0 , 0 , 1 ); - - newPosition = maty * matz * newPosition; - newPosition += pos; - - vec3 transformed = vec3( newPosition ); - `; - - shader.vertexShader = shader.vertexShader.replace(token, insert); - - materialShader = shader; - }; - - birdMesh = new THREE.Mesh(geometry, m); - birdMesh.rotation.y = Math.PI / 2; - - birdMesh.castShadow = true; - birdMesh.receiveShadow = true; - - scene.add(birdMesh); -} - -function fillPositionTexture(texture) { - const theArray = texture.image.data; - - for (let k = 0, kl = theArray.length; k < kl; k += 4) { - const x = Math.random() * BOUNDS - BOUNDS_HALF; - const y = Math.random() * BOUNDS - BOUNDS_HALF; - const z = Math.random() * BOUNDS - BOUNDS_HALF; - - theArray[k + 0] = x; - theArray[k + 1] = y; - theArray[k + 2] = z; - theArray[k + 3] = 1; - } -} - -function fillVelocityTexture(texture) { - const theArray = texture.image.data; - - for (let k = 0, kl = theArray.length; k < kl; k += 4) { - const x = Math.random() - 0.5; - const y = Math.random() - 0.5; - const z = Math.random() - 0.5; - - theArray[k + 0] = x * 10; - theArray[k + 1] = y * 10; - theArray[k + 2] = z * 10; - theArray[k + 3] = 1; - } -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onPointerMove(event) { - if (event.isPrimary === false) return; - - mouseX = event.clientX - windowHalfX; - mouseY = event.clientY - windowHalfY; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - const now = performance.now(); - let delta = (now - last) / 1000; - - if (delta > 1) delta = 1; // safety cap on large deltas - last = now; - - positionUniforms['time'].value = now; - positionUniforms['delta'].value = delta; - velocityUniforms['time'].value = now; - velocityUniforms['delta'].value = delta; - if (materialShader) materialShader.uniforms['time'].value = now / 1000; - if (materialShader) materialShader.uniforms['delta'].value = delta; - - velocityUniforms['predator'].value.set((0.5 * mouseX) / windowHalfX, (-0.5 * mouseY) / windowHalfY, 0); - - mouseX = 10000; - mouseY = 10000; - - gpuCompute.compute(); - - if (materialShader) - materialShader.uniforms['texturePosition'].value = gpuCompute.getCurrentRenderTarget(positionVariable).texture; - if (materialShader) - materialShader.uniforms['textureVelocity'].value = gpuCompute.getCurrentRenderTarget(velocityVariable).texture; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_gpgpu_protoplanet.ts b/examples-testing/examples/webgl_gpgpu_protoplanet.ts deleted file mode 100644 index 30444ddba..000000000 --- a/examples-testing/examples/webgl_gpgpu_protoplanet.ts +++ /dev/null @@ -1,280 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GPUComputationRenderer } from 'three/addons/misc/GPUComputationRenderer.js'; - -// Texture width for simulation (each texel is a debris particle) -const WIDTH = 64; - -let container, stats; -let camera, scene, renderer, geometry; - -const PARTICLES = WIDTH * WIDTH; - -let gpuCompute; -let velocityVariable; -let positionVariable; -let velocityUniforms; -let particleUniforms; -let effectController; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 5, 15000); - camera.position.y = 120; - camera.position.z = 400; - - scene = new THREE.Scene(); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 100; - controls.maxDistance = 1000; - - effectController = { - // Can be changed dynamically - gravityConstant: 100.0, - density: 0.45, - - // Must restart simulation - radius: 300, - height: 8, - exponent: 0.4, - maxMass: 15.0, - velocity: 70, - velocityExponent: 0.2, - randVelocity: 0.001, - }; - - initComputeRenderer(); - - stats = new Stats(); - container.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); - - initGUI(); - - initProtoplanets(); - - dynamicValuesChanger(); -} - -function initComputeRenderer() { - gpuCompute = new GPUComputationRenderer(WIDTH, WIDTH, renderer); - - const dtPosition = gpuCompute.createTexture(); - const dtVelocity = gpuCompute.createTexture(); - - fillTextures(dtPosition, dtVelocity); - - velocityVariable = gpuCompute.addVariable( - 'textureVelocity', - document.getElementById('computeShaderVelocity').textContent, - dtVelocity, - ); - positionVariable = gpuCompute.addVariable( - 'texturePosition', - document.getElementById('computeShaderPosition').textContent, - dtPosition, - ); - - gpuCompute.setVariableDependencies(velocityVariable, [positionVariable, velocityVariable]); - gpuCompute.setVariableDependencies(positionVariable, [positionVariable, velocityVariable]); - - velocityUniforms = velocityVariable.material.uniforms; - - velocityUniforms['gravityConstant'] = { value: 0.0 }; - velocityUniforms['density'] = { value: 0.0 }; - - const error = gpuCompute.init(); - - if (error !== null) { - console.error(error); - } -} - -function restartSimulation() { - const dtPosition = gpuCompute.createTexture(); - const dtVelocity = gpuCompute.createTexture(); - - fillTextures(dtPosition, dtVelocity); - - gpuCompute.renderTexture(dtPosition, positionVariable.renderTargets[0]); - gpuCompute.renderTexture(dtPosition, positionVariable.renderTargets[1]); - gpuCompute.renderTexture(dtVelocity, velocityVariable.renderTargets[0]); - gpuCompute.renderTexture(dtVelocity, velocityVariable.renderTargets[1]); -} - -function initProtoplanets() { - geometry = new THREE.BufferGeometry(); - - const positions = new Float32Array(PARTICLES * 3); - let p = 0; - - for (let i = 0; i < PARTICLES; i++) { - positions[p++] = (Math.random() * 2 - 1) * effectController.radius; - positions[p++] = 0; //( Math.random() * 2 - 1 ) * effectController.radius; - positions[p++] = (Math.random() * 2 - 1) * effectController.radius; - } - - const uvs = new Float32Array(PARTICLES * 2); - p = 0; - - for (let j = 0; j < WIDTH; j++) { - for (let i = 0; i < WIDTH; i++) { - uvs[p++] = i / (WIDTH - 1); - uvs[p++] = j / (WIDTH - 1); - } - } - - geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); - geometry.setAttribute('uv', new THREE.BufferAttribute(uvs, 2)); - - particleUniforms = { - texturePosition: { value: null }, - textureVelocity: { value: null }, - cameraConstant: { value: getCameraConstant(camera) }, - density: { value: 0.0 }, - }; - - // THREE.ShaderMaterial - const material = new THREE.ShaderMaterial({ - uniforms: particleUniforms, - vertexShader: document.getElementById('particleVertexShader').textContent, - fragmentShader: document.getElementById('particleFragmentShader').textContent, - }); - - const particles = new THREE.Points(geometry, material); - particles.matrixAutoUpdate = false; - particles.updateMatrix(); - - scene.add(particles); -} - -function fillTextures(texturePosition, textureVelocity) { - const posArray = texturePosition.image.data; - const velArray = textureVelocity.image.data; - - const radius = effectController.radius; - const height = effectController.height; - const exponent = effectController.exponent; - const maxMass = (effectController.maxMass * 1024) / PARTICLES; - const maxVel = effectController.velocity; - const velExponent = effectController.velocityExponent; - const randVel = effectController.randVelocity; - - for (let k = 0, kl = posArray.length; k < kl; k += 4) { - // Position - let x, z, rr; - - do { - x = Math.random() * 2 - 1; - z = Math.random() * 2 - 1; - rr = x * x + z * z; - } while (rr > 1); - - rr = Math.sqrt(rr); - - const rExp = radius * Math.pow(rr, exponent); - - // Velocity - const vel = maxVel * Math.pow(rr, velExponent); - - const vx = vel * z + (Math.random() * 2 - 1) * randVel; - const vy = (Math.random() * 2 - 1) * randVel * 0.05; - const vz = -vel * x + (Math.random() * 2 - 1) * randVel; - - x *= rExp; - z *= rExp; - const y = (Math.random() * 2 - 1) * height; - - const mass = Math.random() * maxMass + 1; - - // Fill in texture values - posArray[k + 0] = x; - posArray[k + 1] = y; - posArray[k + 2] = z; - posArray[k + 3] = 1; - - velArray[k + 0] = vx; - velArray[k + 1] = vy; - velArray[k + 2] = vz; - velArray[k + 3] = mass; - } -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - particleUniforms['cameraConstant'].value = getCameraConstant(camera); -} - -function dynamicValuesChanger() { - velocityUniforms['gravityConstant'].value = effectController.gravityConstant; - velocityUniforms['density'].value = effectController.density; - particleUniforms['density'].value = effectController.density; -} - -function initGUI() { - const gui = new GUI({ width: 280 }); - - const folder1 = gui.addFolder('Dynamic parameters'); - - folder1.add(effectController, 'gravityConstant', 0.0, 1000.0, 0.05).onChange(dynamicValuesChanger); - folder1.add(effectController, 'density', 0.0, 10.0, 0.001).onChange(dynamicValuesChanger); - - const folder2 = gui.addFolder('Static parameters'); - - folder2.add(effectController, 'radius', 10.0, 1000.0, 1.0); - folder2.add(effectController, 'height', 0.0, 50.0, 0.01); - folder2.add(effectController, 'exponent', 0.0, 2.0, 0.001); - folder2.add(effectController, 'maxMass', 1.0, 50.0, 0.1); - folder2.add(effectController, 'velocity', 0.0, 150.0, 0.1); - folder2.add(effectController, 'velocityExponent', 0.0, 1.0, 0.01); - folder2.add(effectController, 'randVelocity', 0.0, 50.0, 0.1); - - const buttonRestart = { - restartSimulation: function () { - restartSimulation(); - }, - }; - - folder2.add(buttonRestart, 'restartSimulation'); - - folder1.open(); - folder2.open(); -} - -function getCameraConstant(camera) { - return window.innerHeight / (Math.tan(THREE.MathUtils.DEG2RAD * 0.5 * camera.fov) / camera.zoom); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - gpuCompute.compute(); - - particleUniforms['texturePosition'].value = gpuCompute.getCurrentRenderTarget(positionVariable).texture; - particleUniforms['textureVelocity'].value = gpuCompute.getCurrentRenderTarget(velocityVariable).texture; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_gpgpu_water.ts b/examples-testing/examples/webgl_gpgpu_water.ts deleted file mode 100644 index 00c32f229..000000000 --- a/examples-testing/examples/webgl_gpgpu_water.ts +++ /dev/null @@ -1,397 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { GPUComputationRenderer } from 'three/addons/misc/GPUComputationRenderer.js'; -import { SimplexNoise } from 'three/addons/math/SimplexNoise.js'; - -// Texture width for simulation -const WIDTH = 128; - -// Water size in system units -const BOUNDS = 512; -const BOUNDS_HALF = BOUNDS * 0.5; - -let container, stats; -let camera, scene, renderer; -let mouseMoved = false; -const mouseCoords = new THREE.Vector2(); -const raycaster = new THREE.Raycaster(); - -let waterMesh; -let meshRay; -let gpuCompute; -let heightmapVariable; -let waterUniforms; -let smoothShader; -let readWaterLevelShader; -let readWaterLevelRenderTarget; -let readWaterLevelImage; -const waterNormal = new THREE.Vector3(); - -const NUM_SPHERES = 5; -const spheres = []; -let spheresEnabled = true; - -const simplex = new SimplexNoise(); - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 3000); - camera.position.set(0, 200, 350); - camera.lookAt(0, 0, 0); - - scene = new THREE.Scene(); - - const sun = new THREE.DirectionalLight(0xffffff, 3.0); - sun.position.set(300, 400, 175); - scene.add(sun); - - const sun2 = new THREE.DirectionalLight(0x40a040, 2.0); - sun2.position.set(-100, 350, -200); - scene.add(sun2); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - container.style.touchAction = 'none'; - container.addEventListener('pointermove', onPointerMove); - - document.addEventListener('keydown', function (event) { - // W Pressed: Toggle wireframe - if (event.keyCode === 87) { - waterMesh.material.wireframe = !waterMesh.material.wireframe; - waterMesh.material.needsUpdate = true; - } - }); - - window.addEventListener('resize', onWindowResize); - - const gui = new GUI(); - - const effectController = { - mouseSize: 20.0, - viscosity: 0.98, - spheresEnabled: spheresEnabled, - }; - - const valuesChanger = function () { - heightmapVariable.material.uniforms['mouseSize'].value = effectController.mouseSize; - heightmapVariable.material.uniforms['viscosityConstant'].value = effectController.viscosity; - spheresEnabled = effectController.spheresEnabled; - for (let i = 0; i < NUM_SPHERES; i++) { - if (spheres[i]) { - spheres[i].visible = spheresEnabled; - } - } - }; - - gui.add(effectController, 'mouseSize', 1.0, 100.0, 1.0).onChange(valuesChanger); - gui.add(effectController, 'viscosity', 0.9, 0.999, 0.001).onChange(valuesChanger); - gui.add(effectController, 'spheresEnabled').onChange(valuesChanger); - const buttonSmooth = { - smoothWater: function () { - smoothWater(); - }, - }; - gui.add(buttonSmooth, 'smoothWater'); - - initWater(); - - createSpheres(); - - valuesChanger(); -} - -function initWater() { - const materialColor = 0x0040c0; - - const geometry = new THREE.PlaneGeometry(BOUNDS, BOUNDS, WIDTH - 1, WIDTH - 1); - - // material: make a THREE.ShaderMaterial clone of THREE.MeshPhongMaterial, with customized vertex shader - const material = new THREE.ShaderMaterial({ - uniforms: THREE.UniformsUtils.merge([ - THREE.ShaderLib['phong'].uniforms, - { - heightmap: { value: null }, - }, - ]), - vertexShader: document.getElementById('waterVertexShader').textContent, - fragmentShader: THREE.ShaderChunk['meshphong_frag'], - }); - - material.lights = true; - - // Material attributes from THREE.MeshPhongMaterial - // Sets the uniforms with the material values - material.uniforms['diffuse'].value = new THREE.Color(materialColor); - material.uniforms['specular'].value = new THREE.Color(0x111111); - material.uniforms['shininess'].value = Math.max(50, 1e-4); - material.uniforms['opacity'].value = material.opacity; - - // Defines - material.defines.WIDTH = WIDTH.toFixed(1); - material.defines.BOUNDS = BOUNDS.toFixed(1); - - waterUniforms = material.uniforms; - - waterMesh = new THREE.Mesh(geometry, material); - waterMesh.rotation.x = -Math.PI / 2; - waterMesh.matrixAutoUpdate = false; - waterMesh.updateMatrix(); - - scene.add(waterMesh); - - // THREE.Mesh just for mouse raycasting - const geometryRay = new THREE.PlaneGeometry(BOUNDS, BOUNDS, 1, 1); - meshRay = new THREE.Mesh(geometryRay, new THREE.MeshBasicMaterial({ color: 0xffffff, visible: false })); - meshRay.rotation.x = -Math.PI / 2; - meshRay.matrixAutoUpdate = false; - meshRay.updateMatrix(); - scene.add(meshRay); - - // Creates the gpu computation class and sets it up - - gpuCompute = new GPUComputationRenderer(WIDTH, WIDTH, renderer); - - const heightmap0 = gpuCompute.createTexture(); - - fillTexture(heightmap0); - - heightmapVariable = gpuCompute.addVariable( - 'heightmap', - document.getElementById('heightmapFragmentShader').textContent, - heightmap0, - ); - - gpuCompute.setVariableDependencies(heightmapVariable, [heightmapVariable]); - - heightmapVariable.material.uniforms['mousePos'] = { value: new THREE.Vector2(10000, 10000) }; - heightmapVariable.material.uniforms['mouseSize'] = { value: 20.0 }; - heightmapVariable.material.uniforms['viscosityConstant'] = { value: 0.98 }; - heightmapVariable.material.uniforms['heightCompensation'] = { value: 0 }; - heightmapVariable.material.defines.BOUNDS = BOUNDS.toFixed(1); - - const error = gpuCompute.init(); - if (error !== null) { - console.error(error); - } - - // Create compute shader to smooth the water surface and velocity - smoothShader = gpuCompute.createShaderMaterial(document.getElementById('smoothFragmentShader').textContent, { - smoothTexture: { value: null }, - }); - - // Create compute shader to read water level - readWaterLevelShader = gpuCompute.createShaderMaterial( - document.getElementById('readWaterLevelFragmentShader').textContent, - { - point1: { value: new THREE.Vector2() }, - levelTexture: { value: null }, - }, - ); - readWaterLevelShader.defines.WIDTH = WIDTH.toFixed(1); - readWaterLevelShader.defines.BOUNDS = BOUNDS.toFixed(1); - - // Create a 4x1 pixel image and a render target (Uint8, 4 channels, 1 byte per channel) to read water height and orientation - readWaterLevelImage = new Uint8Array(4 * 1 * 4); - - readWaterLevelRenderTarget = new THREE.WebGLRenderTarget(4, 1, { - wrapS: THREE.ClampToEdgeWrapping, - wrapT: THREE.ClampToEdgeWrapping, - minFilter: THREE.NearestFilter, - magFilter: THREE.NearestFilter, - format: THREE.RGBAFormat, - type: THREE.UnsignedByteType, - depthBuffer: false, - }); -} - -function fillTexture(texture) { - const waterMaxHeight = 10; - - function noise(x, y) { - let multR = waterMaxHeight; - let mult = 0.025; - let r = 0; - for (let i = 0; i < 15; i++) { - r += multR * simplex.noise(x * mult, y * mult); - multR *= 0.53 + 0.025 * i; - mult *= 1.25; - } - - return r; - } - - const pixels = texture.image.data; - - let p = 0; - for (let j = 0; j < WIDTH; j++) { - for (let i = 0; i < WIDTH; i++) { - const x = (i * 128) / WIDTH; - const y = (j * 128) / WIDTH; - - pixels[p + 0] = noise(x, y); - pixels[p + 1] = pixels[p + 0]; - pixels[p + 2] = 0; - pixels[p + 3] = 1; - - p += 4; - } - } -} - -function smoothWater() { - const currentRenderTarget = gpuCompute.getCurrentRenderTarget(heightmapVariable); - const alternateRenderTarget = gpuCompute.getAlternateRenderTarget(heightmapVariable); - - for (let i = 0; i < 10; i++) { - smoothShader.uniforms['smoothTexture'].value = currentRenderTarget.texture; - gpuCompute.doRenderTarget(smoothShader, alternateRenderTarget); - - smoothShader.uniforms['smoothTexture'].value = alternateRenderTarget.texture; - gpuCompute.doRenderTarget(smoothShader, currentRenderTarget); - } -} - -function createSpheres() { - const sphereTemplate = new THREE.Mesh( - new THREE.SphereGeometry(4, 24, 12), - new THREE.MeshPhongMaterial({ color: 0xffff00 }), - ); - - for (let i = 0; i < NUM_SPHERES; i++) { - let sphere = sphereTemplate; - if (i < NUM_SPHERES - 1) { - sphere = sphereTemplate.clone(); - } - - sphere.position.x = (Math.random() - 0.5) * BOUNDS * 0.7; - sphere.position.z = (Math.random() - 0.5) * BOUNDS * 0.7; - - sphere.userData.velocity = new THREE.Vector3(); - - scene.add(sphere); - - spheres[i] = sphere; - } -} - -function sphereDynamics() { - const currentRenderTarget = gpuCompute.getCurrentRenderTarget(heightmapVariable); - - readWaterLevelShader.uniforms['levelTexture'].value = currentRenderTarget.texture; - - for (let i = 0; i < NUM_SPHERES; i++) { - const sphere = spheres[i]; - - if (sphere) { - // Read water level and orientation - const u = (0.5 * sphere.position.x) / BOUNDS_HALF + 0.5; - const v = 1 - ((0.5 * sphere.position.z) / BOUNDS_HALF + 0.5); - readWaterLevelShader.uniforms['point1'].value.set(u, v); - gpuCompute.doRenderTarget(readWaterLevelShader, readWaterLevelRenderTarget); - - renderer.readRenderTargetPixels(readWaterLevelRenderTarget, 0, 0, 4, 1, readWaterLevelImage); - const pixels = new Float32Array(readWaterLevelImage.buffer); - - // Get orientation - waterNormal.set(pixels[1], 0, -pixels[2]); - - const pos = sphere.position; - - // Set height - pos.y = pixels[0]; - - // Move sphere - waterNormal.multiplyScalar(0.1); - sphere.userData.velocity.add(waterNormal); - sphere.userData.velocity.multiplyScalar(0.998); - pos.add(sphere.userData.velocity); - - if (pos.x < -BOUNDS_HALF) { - pos.x = -BOUNDS_HALF + 0.001; - sphere.userData.velocity.x *= -0.3; - } else if (pos.x > BOUNDS_HALF) { - pos.x = BOUNDS_HALF - 0.001; - sphere.userData.velocity.x *= -0.3; - } - - if (pos.z < -BOUNDS_HALF) { - pos.z = -BOUNDS_HALF + 0.001; - sphere.userData.velocity.z *= -0.3; - } else if (pos.z > BOUNDS_HALF) { - pos.z = BOUNDS_HALF - 0.001; - sphere.userData.velocity.z *= -0.3; - } - } - } -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function setMouseCoords(x, y) { - mouseCoords.set((x / renderer.domElement.clientWidth) * 2 - 1, -(y / renderer.domElement.clientHeight) * 2 + 1); - mouseMoved = true; -} - -function onPointerMove(event) { - if (event.isPrimary === false) return; - - setMouseCoords(event.clientX, event.clientY); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - // Set uniforms: mouse interaction - const uniforms = heightmapVariable.material.uniforms; - if (mouseMoved) { - raycaster.setFromCamera(mouseCoords, camera); - - const intersects = raycaster.intersectObject(meshRay); - - if (intersects.length > 0) { - const point = intersects[0].point; - uniforms['mousePos'].value.set(point.x, point.z); - } else { - uniforms['mousePos'].value.set(10000, 10000); - } - - mouseMoved = false; - } else { - uniforms['mousePos'].value.set(10000, 10000); - } - - // Do the gpu computation - gpuCompute.compute(); - - if (spheresEnabled) { - sphereDynamics(); - } - - // Get compute output in custom uniform - waterUniforms['heightmap'].value = gpuCompute.getCurrentRenderTarget(heightmapVariable).texture; - - // Render - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_helpers.ts b/examples-testing/examples/webgl_helpers.ts deleted file mode 100644 index a8c3b9773..000000000 --- a/examples-testing/examples/webgl_helpers.ts +++ /dev/null @@ -1,117 +0,0 @@ -import * as THREE from 'three'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -import { VertexNormalsHelper } from 'three/addons/helpers/VertexNormalsHelper.js'; -import { VertexTangentsHelper } from 'three/addons/helpers/VertexTangentsHelper.js'; - -let scene, renderer; -let camera, light; -let vnh; -let vth; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 400; - - scene = new THREE.Scene(); - - light = new THREE.PointLight(); - light.position.set(200, 100, 150); - scene.add(light); - - scene.add(new THREE.PointLightHelper(light, 15)); - - const gridHelper = new THREE.GridHelper(400, 40, 0x0000ff, 0x808080); - gridHelper.position.y = -150; - gridHelper.position.x = -150; - scene.add(gridHelper); - - const polarGridHelper = new THREE.PolarGridHelper(200, 16, 8, 64, 0x0000ff, 0x808080); - polarGridHelper.position.y = -150; - polarGridHelper.position.x = 200; - scene.add(polarGridHelper); - - const loader = new GLTFLoader(); - loader.load('models/gltf/LeePerrySmith/LeePerrySmith.glb', function (gltf) { - const mesh = gltf.scene.children[0]; - - mesh.geometry.computeTangents(); // generates bad data due to degenerate UVs - - const group = new THREE.Group(); - group.scale.multiplyScalar(50); - scene.add(group); - - // To make sure that the matrixWorld is up to date for the boxhelpers - group.updateMatrixWorld(true); - - group.add(mesh); - - vnh = new VertexNormalsHelper(mesh, 5); - scene.add(vnh); - - vth = new VertexTangentsHelper(mesh, 5); - scene.add(vth); - - scene.add(new THREE.BoxHelper(mesh)); - - const wireframe = new THREE.WireframeGeometry(mesh.geometry); - let line = new THREE.LineSegments(wireframe); - line.material.depthTest = false; - line.material.opacity = 0.25; - line.material.transparent = true; - line.position.x = 4; - group.add(line); - scene.add(new THREE.BoxHelper(line)); - - const edges = new THREE.EdgesGeometry(mesh.geometry); - line = new THREE.LineSegments(edges); - line.material.depthTest = false; - line.material.opacity = 0.25; - line.material.transparent = true; - line.position.x = -4; - group.add(line); - scene.add(new THREE.BoxHelper(line)); - - scene.add(new THREE.BoxHelper(group)); - scene.add(new THREE.BoxHelper(scene)); - }); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const time = -performance.now() * 0.0003; - - camera.position.x = 400 * Math.cos(time); - camera.position.z = 400 * Math.sin(time); - camera.lookAt(scene.position); - - light.position.x = Math.sin(time * 1.7) * 300; - light.position.y = Math.cos(time * 1.5) * 400; - light.position.z = Math.cos(time * 1.3) * 300; - - if (vnh) vnh.update(); - if (vth) vth.update(); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_instancing_dynamic.ts b/examples-testing/examples/webgl_instancing_dynamic.ts deleted file mode 100644 index 88562fc5a..000000000 --- a/examples-testing/examples/webgl_instancing_dynamic.ts +++ /dev/null @@ -1,103 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, stats; - -let mesh; -const amount = parseInt(window.location.search.slice(1)) || 10; -const count = Math.pow(amount, 3); -const dummy = new THREE.Object3D(); - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(amount * 0.9, amount * 0.9, amount * 0.9); - camera.lookAt(0, 0, 0); - - scene = new THREE.Scene(); - - const loader = new THREE.BufferGeometryLoader(); - loader.load('models/json/suzanne_buffergeometry.json', function (geometry) { - geometry.computeVertexNormals(); - geometry.scale(0.5, 0.5, 0.5); - - const material = new THREE.MeshNormalMaterial(); - // check overdraw - // let material = new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.1, transparent: true } ); - - mesh = new THREE.InstancedMesh(geometry, material, count); - mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage); // will be updated every frame - scene.add(mesh); - - // - - const gui = new GUI(); - gui.add(mesh, 'count', 0, count); - }); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - render(); - - stats.update(); -} - -function render() { - if (mesh) { - const time = Date.now() * 0.001; - - mesh.rotation.x = Math.sin(time / 4); - mesh.rotation.y = Math.sin(time / 2); - - let i = 0; - const offset = (amount - 1) / 2; - - for (let x = 0; x < amount; x++) { - for (let y = 0; y < amount; y++) { - for (let z = 0; z < amount; z++) { - dummy.position.set(offset - x, offset - y, offset - z); - dummy.rotation.y = Math.sin(x / 4 + time) + Math.sin(y / 4 + time) + Math.sin(z / 4 + time); - dummy.rotation.z = dummy.rotation.y * 2; - - dummy.updateMatrix(); - - mesh.setMatrixAt(i++, dummy.matrix); - } - } - } - - mesh.instanceMatrix.needsUpdate = true; - mesh.computeBoundingSphere(); - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_instancing_morph.ts b/examples-testing/examples/webgl_instancing_morph.ts deleted file mode 100644 index 8686a75b9..000000000 --- a/examples-testing/examples/webgl_instancing_morph.ts +++ /dev/null @@ -1,147 +0,0 @@ -import * as THREE from 'three'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let camera, scene, renderer, stats, mesh, mixer, dummy; - -const offset = 5000; - -const timeOffsets = new Float32Array(1024); - -for (let i = 0; i < 1024; i++) { - timeOffsets[i] = Math.random() * 3; -} - -const clock = new THREE.Clock(true); - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 100, 10000); - - scene = new THREE.Scene(); - - scene.background = new THREE.Color(0x99ddff); - - scene.fog = new THREE.Fog(0x99ddff, 5000, 10000); - - const light = new THREE.DirectionalLight(0xffffff, 1); - - light.position.set(200, 1000, 50); - - light.castShadow = true; - - light.shadow.camera.left = -5000; - light.shadow.camera.right = 5000; - light.shadow.camera.top = 5000; - light.shadow.camera.bottom = -5000; - light.shadow.camera.far = 2000; - - light.shadow.bias = -0.01; - - light.shadow.camera.updateProjectionMatrix(); - - scene.add(light); - - const hemi = new THREE.HemisphereLight(0x99ddff, 0x669933, 1 / 3); - - scene.add(hemi); - - const ground = new THREE.Mesh( - new THREE.PlaneGeometry(1000000, 1000000), - new THREE.MeshStandardMaterial({ color: 0x669933, depthWrite: true }), - ); - - ground.rotation.x = -Math.PI / 2; - - ground.receiveShadow = true; - - scene.add(ground); - - const loader = new GLTFLoader(); - - loader.load('models/gltf/Horse.glb', function (glb) { - dummy = glb.scene.children[0]; - - mesh = new THREE.InstancedMesh(dummy.geometry, dummy.material, 1024); - - mesh.castShadow = true; - - for (let x = 0, i = 0; x < 32; x++) { - for (let y = 0; y < 32; y++) { - dummy.position.set(offset - 300 * x + 200 * Math.random(), 0, offset - 300 * y); - - dummy.updateMatrix(); - - mesh.setMatrixAt(i, dummy.matrix); - - mesh.setColorAt(i, new THREE.Color(`hsl(${Math.random() * 360}, 50%, 66%)`)); - - i++; - } - } - - scene.add(mesh); - - mixer = new THREE.AnimationMixer(glb.scene); - - const action = mixer.clipAction(glb.animations[0]); - - action.play(); - }); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - renderer.shadowMap.enabled = true; - renderer.shadowMap.type = THREE.VSMShadowMap; - // - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - render(); - - stats.update(); -} - -function render() { - const time = clock.getElapsedTime(); - - const r = 3000; - camera.position.set(Math.sin(time / 10) * r, 1500 + 1000 * Math.cos(time / 5), Math.cos(time / 10) * r); - camera.lookAt(0, 0, 0); - - if (mesh) { - for (let i = 0; i < 1024; i++) { - mixer.setTime(time + timeOffsets[i]); - - mesh.setMorphAt(i, dummy); - } - - mesh.morphTexture.needsUpdate = true; - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_instancing_performance.ts b/examples-testing/examples/webgl_instancing_performance.ts deleted file mode 100644 index bf1deabad..000000000 --- a/examples-testing/examples/webgl_instancing_performance.ts +++ /dev/null @@ -1,262 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; - -let container, stats, gui, guiStatsEl; -let camera, controls, scene, renderer, material; - -// gui - -const Method = { - INSTANCED: 'INSTANCED', - MERGED: 'MERGED', - NAIVE: 'NAIVE', -}; - -const api = { - method: Method.INSTANCED, - count: 1000, -}; - -// - -init(); -initMesh(); - -// - -function clean() { - const meshes = []; - - scene.traverse(function (object) { - if (object.isMesh) meshes.push(object); - }); - - for (let i = 0; i < meshes.length; i++) { - const mesh = meshes[i]; - mesh.material.dispose(); - mesh.geometry.dispose(); - - scene.remove(mesh); - } -} - -const randomizeMatrix = (function () { - const position = new THREE.Vector3(); - const quaternion = new THREE.Quaternion(); - const scale = new THREE.Vector3(); - - return function (matrix) { - position.x = Math.random() * 40 - 20; - position.y = Math.random() * 40 - 20; - position.z = Math.random() * 40 - 20; - - quaternion.random(); - - scale.x = scale.y = scale.z = Math.random() * 1; - - matrix.compose(position, quaternion, scale); - }; -})(); - -function initMesh() { - clean(); - - // make instances - new THREE.BufferGeometryLoader().setPath('models/json/').load('suzanne_buffergeometry.json', function (geometry) { - material = new THREE.MeshNormalMaterial(); - - geometry.computeVertexNormals(); - - console.time(api.method + ' (build)'); - - switch (api.method) { - case Method.INSTANCED: - makeInstanced(geometry); - break; - - case Method.MERGED: - makeMerged(geometry); - break; - - case Method.NAIVE: - makeNaive(geometry); - break; - } - - console.timeEnd(api.method + ' (build)'); - }); -} - -function makeInstanced(geometry) { - const matrix = new THREE.Matrix4(); - const mesh = new THREE.InstancedMesh(geometry, material, api.count); - - for (let i = 0; i < api.count; i++) { - randomizeMatrix(matrix); - mesh.setMatrixAt(i, matrix); - } - - scene.add(mesh); - - // - - const geometryByteLength = getGeometryByteLength(geometry); - - guiStatsEl.innerHTML = [ - 'GPU draw calls: 1', - 'GPU memory: ' + formatBytes(api.count * 16 + geometryByteLength, 2), - ].join('
'); -} - -function makeMerged(geometry) { - const geometries = []; - const matrix = new THREE.Matrix4(); - - for (let i = 0; i < api.count; i++) { - randomizeMatrix(matrix); - - const instanceGeometry = geometry.clone(); - instanceGeometry.applyMatrix4(matrix); - - geometries.push(instanceGeometry); - } - - const mergedGeometry = BufferGeometryUtils.mergeGeometries(geometries); - - scene.add(new THREE.Mesh(mergedGeometry, material)); - - // - - guiStatsEl.innerHTML = [ - 'GPU draw calls: 1', - 'GPU memory: ' + formatBytes(getGeometryByteLength(mergedGeometry), 2), - ].join('
'); -} - -function makeNaive(geometry) { - const matrix = new THREE.Matrix4(); - - for (let i = 0; i < api.count; i++) { - randomizeMatrix(matrix); - - const mesh = new THREE.Mesh(geometry, material); - mesh.applyMatrix4(matrix); - - scene.add(mesh); - } - - // - - const geometryByteLength = getGeometryByteLength(geometry); - - guiStatsEl.innerHTML = [ - 'GPU draw calls: ' + api.count, - 'GPU memory: ' + formatBytes(api.count * 16 + geometryByteLength, 2), - ].join('
'); -} - -function init() { - const width = window.innerWidth; - const height = window.innerHeight; - - // camera - - camera = new THREE.PerspectiveCamera(70, width / height, 1, 100); - camera.position.z = 30; - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(width, height); - renderer.setAnimationLoop(animate); - container = document.getElementById('container'); - container.appendChild(renderer.domElement); - - // scene - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - - // controls - - controls = new OrbitControls(camera, renderer.domElement); - controls.autoRotate = true; - - // stats - - stats = new Stats(); - container.appendChild(stats.dom); - - // gui - - gui = new GUI(); - gui.add(api, 'method', Method).onChange(initMesh); - gui.add(api, 'count', 1, 10000).step(1).onChange(initMesh); - - const perfFolder = gui.addFolder('Performance'); - - guiStatsEl = document.createElement('div'); - guiStatsEl.classList.add('gui-stats'); - - perfFolder.$children.appendChild(guiStatsEl); - perfFolder.open(); - - // listeners - - window.addEventListener('resize', onWindowResize); - - Object.assign(window, { scene }); -} - -// - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); -} - -function animate() { - controls.update(); - - renderer.render(scene, camera); - - stats.update(); -} - -// - -function getGeometryByteLength(geometry) { - let total = 0; - - if (geometry.index) total += geometry.index.array.byteLength; - - for (const name in geometry.attributes) { - total += geometry.attributes[name].array.byteLength; - } - - return total; -} - -// Source: https://stackoverflow.com/a/18650828/1314762 -function formatBytes(bytes, decimals) { - if (bytes === 0) return '0 bytes'; - - const k = 1024; - const dm = decimals < 0 ? 0 : decimals; - const sizes = ['bytes', 'KB', 'MB']; - - const i = Math.floor(Math.log(bytes) / Math.log(k)); - - return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; -} diff --git a/examples-testing/examples/webgl_instancing_raycast.ts b/examples-testing/examples/webgl_instancing_raycast.ts deleted file mode 100644 index 371ea070b..000000000 --- a/examples-testing/examples/webgl_instancing_raycast.ts +++ /dev/null @@ -1,116 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer, controls, stats; - -let mesh; -const amount = parseInt(window.location.search.slice(1)) || 10; -const count = Math.pow(amount, 3); - -const raycaster = new THREE.Raycaster(); -const mouse = new THREE.Vector2(1, 1); - -const color = new THREE.Color(); -const white = new THREE.Color().setHex(0xffffff); - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(amount, amount, amount); - camera.lookAt(0, 0, 0); - - scene = new THREE.Scene(); - - const light = new THREE.HemisphereLight(0xffffff, 0x888888, 3); - light.position.set(0, 1, 0); - scene.add(light); - - const geometry = new THREE.IcosahedronGeometry(0.5, 3); - const material = new THREE.MeshPhongMaterial({ color: 0xffffff }); - - mesh = new THREE.InstancedMesh(geometry, material, count); - - let i = 0; - const offset = (amount - 1) / 2; - - const matrix = new THREE.Matrix4(); - - for (let x = 0; x < amount; x++) { - for (let y = 0; y < amount; y++) { - for (let z = 0; z < amount; z++) { - matrix.setPosition(offset - x, offset - y, offset - z); - - mesh.setMatrixAt(i, matrix); - mesh.setColorAt(i, color); - - i++; - } - } - } - - scene.add(mesh); - - // - - const gui = new GUI(); - gui.add(mesh, 'count', 0, count); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - controls = new OrbitControls(camera, renderer.domElement); - controls.enableDamping = true; - controls.enableZoom = false; - controls.enablePan = false; - - stats = new Stats(); - document.body.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); - document.addEventListener('mousemove', onMouseMove); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onMouseMove(event) { - event.preventDefault(); - - mouse.x = (event.clientX / window.innerWidth) * 2 - 1; - mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; -} - -function animate() { - controls.update(); - - raycaster.setFromCamera(mouse, camera); - - const intersection = raycaster.intersectObject(mesh); - - if (intersection.length > 0) { - const instanceId = intersection[0].instanceId; - - mesh.getColorAt(instanceId, color); - - if (color.equals(white)) { - mesh.setColorAt(instanceId, color.setHex(Math.random() * 0xffffff)); - - mesh.instanceColor.needsUpdate = true; - } - } - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_instancing_scatter.ts b/examples-testing/examples/webgl_instancing_scatter.ts deleted file mode 100644 index fc3b9cc9f..000000000 --- a/examples-testing/examples/webgl_instancing_scatter.ts +++ /dev/null @@ -1,257 +0,0 @@ -import * as THREE from 'three'; - -import { MeshSurfaceSampler } from 'three/addons/math/MeshSurfaceSampler.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, stats; - -const api = { - count: 2000, - distribution: 'random', - resample: resample, - surfaceColor: 0xfff784, - backgroundColor: 0xe39469, -}; - -let stemMesh, blossomMesh; -let stemGeometry, blossomGeometry; -let stemMaterial, blossomMaterial; - -let sampler; -const count = api.count; -const ages = new Float32Array(count); -const scales = new Float32Array(count); -const dummy = new THREE.Object3D(); - -const _position = new THREE.Vector3(); -const _normal = new THREE.Vector3(); -const _scale = new THREE.Vector3(); - -// let surfaceGeometry = new THREE.BoxGeometry( 10, 10, 10 ).toNonIndexed(); -const surfaceGeometry = new THREE.TorusKnotGeometry(10, 3, 100, 16).toNonIndexed(); -const surfaceMaterial = new THREE.MeshLambertMaterial({ color: api.surfaceColor, wireframe: false }); -const surface = new THREE.Mesh(surfaceGeometry, surfaceMaterial); - -// Source: https://gist.github.com/gre/1650294 -const easeOutCubic = function (t) { - return --t * t * t + 1; -}; - -// Scaling curve causes particles to grow quickly, ease gradually into full scale, then -// disappear quickly. More of the particle's lifetime is spent around full scale. -const scaleCurve = function (t) { - return Math.abs(easeOutCubic((t > 0.5 ? 1 - t : t) * 2)); -}; - -const loader = new GLTFLoader(); - -loader.load('./models/gltf/Flower/Flower.glb', function (gltf) { - const _stemMesh = gltf.scene.getObjectByName('Stem'); - const _blossomMesh = gltf.scene.getObjectByName('Blossom'); - - stemGeometry = _stemMesh.geometry.clone(); - blossomGeometry = _blossomMesh.geometry.clone(); - - const defaultTransform = new THREE.Matrix4() - .makeRotationX(Math.PI) - .multiply(new THREE.Matrix4().makeScale(7, 7, 7)); - - stemGeometry.applyMatrix4(defaultTransform); - blossomGeometry.applyMatrix4(defaultTransform); - - stemMaterial = _stemMesh.material; - blossomMaterial = _blossomMesh.material; - - stemMesh = new THREE.InstancedMesh(stemGeometry, stemMaterial, count); - blossomMesh = new THREE.InstancedMesh(blossomGeometry, blossomMaterial, count); - - // Assign random colors to the blossoms. - const color = new THREE.Color(); - const blossomPalette = [0xf20587, 0xf2d479, 0xf2c879, 0xf2b077, 0xf24405]; - - for (let i = 0; i < count; i++) { - color.setHex(blossomPalette[Math.floor(Math.random() * blossomPalette.length)]); - blossomMesh.setColorAt(i, color); - } - - // Instance matrices will be updated every frame. - stemMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage); - blossomMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage); - - resample(); - - init(); -}); - -function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(25, 25, 25); - camera.lookAt(0, 0, 0); - - // - - scene = new THREE.Scene(); - scene.background = new THREE.Color(api.backgroundColor); - - const pointLight = new THREE.PointLight(0xaa8899, 2.5, 0, 0); - pointLight.position.set(50, -25, 75); - scene.add(pointLight); - - scene.add(new THREE.AmbientLight(0xffffff, 3)); - - // - - scene.add(stemMesh); - scene.add(blossomMesh); - - scene.add(surface); - - // - - const gui = new GUI(); - gui.add(api, 'count', 0, count).onChange(function () { - stemMesh.count = api.count; - blossomMesh.count = api.count; - }); - - // gui.addColor( api, 'backgroundColor' ).onChange( function () { - - // scene.background.setHex( api.backgroundColor ); - - // } ); - - // gui.addColor( api, 'surfaceColor' ).onChange( function () { - - // surfaceMaterial.color.setHex( api.surfaceColor ); - - // } ); - - gui.add(api, 'distribution').options(['random', 'weighted']).onChange(resample); - gui.add(api, 'resample'); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function resample() { - const vertexCount = surface.geometry.getAttribute('position').count; - - console.info('Sampling ' + count + ' points from a surface with ' + vertexCount + ' vertices...'); - - // - - console.time('.build()'); - - sampler = new MeshSurfaceSampler(surface).setWeightAttribute(api.distribution === 'weighted' ? 'uv' : null).build(); - - console.timeEnd('.build()'); - - // - - console.time('.sample()'); - - for (let i = 0; i < count; i++) { - ages[i] = Math.random(); - scales[i] = scaleCurve(ages[i]); - - resampleParticle(i); - } - - console.timeEnd('.sample()'); - - stemMesh.instanceMatrix.needsUpdate = true; - blossomMesh.instanceMatrix.needsUpdate = true; -} - -function resampleParticle(i) { - sampler.sample(_position, _normal); - _normal.add(_position); - - dummy.position.copy(_position); - dummy.scale.set(scales[i], scales[i], scales[i]); - dummy.lookAt(_normal); - dummy.updateMatrix(); - - stemMesh.setMatrixAt(i, dummy.matrix); - blossomMesh.setMatrixAt(i, dummy.matrix); -} - -function updateParticle(i) { - // Update lifecycle. - - ages[i] += 0.005; - - if (ages[i] >= 1) { - ages[i] = 0.001; - scales[i] = scaleCurve(ages[i]); - - resampleParticle(i); - - return; - } - - // Update scale. - - const prevScale = scales[i]; - scales[i] = scaleCurve(ages[i]); - _scale.set(scales[i] / prevScale, scales[i] / prevScale, scales[i] / prevScale); - - // Update transform. - - stemMesh.getMatrixAt(i, dummy.matrix); - dummy.matrix.scale(_scale); - stemMesh.setMatrixAt(i, dummy.matrix); - blossomMesh.setMatrixAt(i, dummy.matrix); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - render(); - - stats.update(); -} - -function render() { - if (stemMesh && blossomMesh) { - const time = Date.now() * 0.001; - - scene.rotation.x = Math.sin(time / 4); - scene.rotation.y = Math.sin(time / 2); - - for (let i = 0; i < api.count; i++) { - updateParticle(i); - } - - stemMesh.instanceMatrix.needsUpdate = true; - blossomMesh.instanceMatrix.needsUpdate = true; - - stemMesh.computeBoundingSphere(); - blossomMesh.computeBoundingSphere(); - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_interactive_buffergeometry.ts b/examples-testing/examples/webgl_interactive_buffergeometry.ts deleted file mode 100644 index 1d6608b13..000000000 --- a/examples-testing/examples/webgl_interactive_buffergeometry.ts +++ /dev/null @@ -1,244 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; - -let camera, scene, renderer; - -let raycaster, pointer; - -let mesh, line; - -init(); - -function init() { - container = document.getElementById('container'); - - // - - camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 3500); - camera.position.z = 2750; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x050505); - scene.fog = new THREE.Fog(0x050505, 2000, 3500); - - // - - scene.add(new THREE.AmbientLight(0x444444, 3)); - - const light1 = new THREE.DirectionalLight(0xffffff, 1.5); - light1.position.set(1, 1, 1); - scene.add(light1); - - const light2 = new THREE.DirectionalLight(0xffffff, 4.5); - light2.position.set(0, -1, 0); - scene.add(light2); - - // - - const triangles = 5000; - - let geometry = new THREE.BufferGeometry(); - - const positions = new Float32Array(triangles * 3 * 3); - const normals = new Float32Array(triangles * 3 * 3); - const colors = new Float32Array(triangles * 3 * 3); - - const color = new THREE.Color(); - - const n = 800, - n2 = n / 2; // triangles spread in the cube - const d = 120, - d2 = d / 2; // individual triangle size - - const pA = new THREE.Vector3(); - const pB = new THREE.Vector3(); - const pC = new THREE.Vector3(); - - const cb = new THREE.Vector3(); - const ab = new THREE.Vector3(); - - for (let i = 0; i < positions.length; i += 9) { - // positions - - const x = Math.random() * n - n2; - const y = Math.random() * n - n2; - const z = Math.random() * n - n2; - - const ax = x + Math.random() * d - d2; - const ay = y + Math.random() * d - d2; - const az = z + Math.random() * d - d2; - - const bx = x + Math.random() * d - d2; - const by = y + Math.random() * d - d2; - const bz = z + Math.random() * d - d2; - - const cx = x + Math.random() * d - d2; - const cy = y + Math.random() * d - d2; - const cz = z + Math.random() * d - d2; - - positions[i] = ax; - positions[i + 1] = ay; - positions[i + 2] = az; - - positions[i + 3] = bx; - positions[i + 4] = by; - positions[i + 5] = bz; - - positions[i + 6] = cx; - positions[i + 7] = cy; - positions[i + 8] = cz; - - // flat face normals - - pA.set(ax, ay, az); - pB.set(bx, by, bz); - pC.set(cx, cy, cz); - - cb.subVectors(pC, pB); - ab.subVectors(pA, pB); - cb.cross(ab); - - cb.normalize(); - - const nx = cb.x; - const ny = cb.y; - const nz = cb.z; - - normals[i] = nx; - normals[i + 1] = ny; - normals[i + 2] = nz; - - normals[i + 3] = nx; - normals[i + 4] = ny; - normals[i + 5] = nz; - - normals[i + 6] = nx; - normals[i + 7] = ny; - normals[i + 8] = nz; - - // colors - - const vx = x / n + 0.5; - const vy = y / n + 0.5; - const vz = z / n + 0.5; - - color.setRGB(vx, vy, vz); - - colors[i] = color.r; - colors[i + 1] = color.g; - colors[i + 2] = color.b; - - colors[i + 3] = color.r; - colors[i + 4] = color.g; - colors[i + 5] = color.b; - - colors[i + 6] = color.r; - colors[i + 7] = color.g; - colors[i + 8] = color.b; - } - - geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); - geometry.setAttribute('normal', new THREE.BufferAttribute(normals, 3)); - geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3)); - - geometry.computeBoundingSphere(); - - let material = new THREE.MeshPhongMaterial({ - color: 0xaaaaaa, - specular: 0xffffff, - shininess: 250, - side: THREE.DoubleSide, - vertexColors: true, - }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - raycaster = new THREE.Raycaster(); - - pointer = new THREE.Vector2(); - - geometry = new THREE.BufferGeometry(); - geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(4 * 3), 3)); - - material = new THREE.LineBasicMaterial({ color: 0xffffff, transparent: true }); - - line = new THREE.Line(geometry, material); - scene.add(line); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); - document.addEventListener('pointermove', onPointerMove); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onPointerMove(event) { - pointer.x = (event.clientX / window.innerWidth) * 2 - 1; - pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - const time = Date.now() * 0.001; - - mesh.rotation.x = time * 0.15; - mesh.rotation.y = time * 0.25; - - raycaster.setFromCamera(pointer, camera); - - const intersects = raycaster.intersectObject(mesh); - - if (intersects.length > 0) { - const intersect = intersects[0]; - const face = intersect.face; - - const linePosition = line.geometry.attributes.position; - const meshPosition = mesh.geometry.attributes.position; - - linePosition.copyAt(0, meshPosition, face.a); - linePosition.copyAt(1, meshPosition, face.b); - linePosition.copyAt(2, meshPosition, face.c); - linePosition.copyAt(3, meshPosition, face.a); - - mesh.updateMatrix(); - - line.geometry.applyMatrix4(mesh.matrix); - - line.visible = true; - } else { - line.visible = false; - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_interactive_cubes.ts b/examples-testing/examples/webgl_interactive_cubes.ts deleted file mode 100644 index adfcfddf8..000000000 --- a/examples-testing/examples/webgl_interactive_cubes.ts +++ /dev/null @@ -1,114 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let stats; -let camera, scene, raycaster, renderer; - -let INTERSECTED; -let theta = 0; - -const pointer = new THREE.Vector2(); -const radius = 5; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(1, 1, 1).normalize(); - scene.add(light); - - const geometry = new THREE.BoxGeometry(); - - for (let i = 0; i < 2000; i++) { - const object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff })); - - object.position.x = Math.random() * 40 - 20; - object.position.y = Math.random() * 40 - 20; - object.position.z = Math.random() * 40 - 20; - - object.rotation.x = Math.random() * 2 * Math.PI; - object.rotation.y = Math.random() * 2 * Math.PI; - object.rotation.z = Math.random() * 2 * Math.PI; - - object.scale.x = Math.random() + 0.5; - object.scale.y = Math.random() + 0.5; - object.scale.z = Math.random() + 0.5; - - scene.add(object); - } - - raycaster = new THREE.Raycaster(); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - document.addEventListener('mousemove', onPointerMove); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onPointerMove(event) { - pointer.x = (event.clientX / window.innerWidth) * 2 - 1; - pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - theta += 0.1; - - camera.position.x = radius * Math.sin(THREE.MathUtils.degToRad(theta)); - camera.position.y = radius * Math.sin(THREE.MathUtils.degToRad(theta)); - camera.position.z = radius * Math.cos(THREE.MathUtils.degToRad(theta)); - camera.lookAt(scene.position); - - camera.updateMatrixWorld(); - - // find intersections - - raycaster.setFromCamera(pointer, camera); - - const intersects = raycaster.intersectObjects(scene.children, false); - - if (intersects.length > 0) { - if (INTERSECTED != intersects[0].object) { - if (INTERSECTED) INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex); - - INTERSECTED = intersects[0].object; - INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex(); - INTERSECTED.material.emissive.setHex(0xff0000); - } - } else { - if (INTERSECTED) INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex); - - INTERSECTED = null; - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_interactive_cubes_gpu.ts b/examples-testing/examples/webgl_interactive_cubes_gpu.ts deleted file mode 100644 index 2644469c3..000000000 --- a/examples-testing/examples/webgl_interactive_cubes_gpu.ts +++ /dev/null @@ -1,229 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; -import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; - -let container, stats; -let camera, controls, scene, renderer; -let pickingTexture, pickingScene; -let highlightBox; - -const pickingData = []; - -const pointer = new THREE.Vector2(); -const offset = new THREE.Vector3(10, 10, 10); -const clearColor = new THREE.Color(); - -init(); - -function init() { - container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 1000; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - - scene.add(new THREE.AmbientLight(0xcccccc)); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0, 500, 2000); - scene.add(light); - - const defaultMaterial = new THREE.MeshPhongMaterial({ - color: 0xffffff, - flatShading: true, - vertexColors: true, - shininess: 0, - }); - - // set up the picking texture to use a 32 bit integer so we can write and read integer ids from it - pickingScene = new THREE.Scene(); - pickingTexture = new THREE.WebGLRenderTarget(1, 1, { - type: THREE.IntType, - format: THREE.RGBAIntegerFormat, - internalFormat: 'RGBA32I', - }); - const pickingMaterial = new THREE.ShaderMaterial({ - glslVersion: THREE.GLSL3, - - vertexShader: /* glsl */ ` - attribute int id; - flat varying int vid; - void main() { - - vid = id; - gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); - - } - `, - - fragmentShader: /* glsl */ ` - layout(location = 0) out int out_id; - flat varying int vid; - - void main() { - - out_id = vid; - - } - `, - }); - - function applyId(geometry, id) { - const position = geometry.attributes.position; - const array = new Int16Array(position.count); - array.fill(id); - - const bufferAttribute = new THREE.Int16BufferAttribute(array, 1, false); - bufferAttribute.gpuType = THREE.IntType; - geometry.setAttribute('id', bufferAttribute); - } - - function applyVertexColors(geometry, color) { - const position = geometry.attributes.position; - const colors = []; - - for (let i = 0; i < position.count; i++) { - colors.push(color.r, color.g, color.b); - } - - geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); - } - - const geometries = []; - const matrix = new THREE.Matrix4(); - const quaternion = new THREE.Quaternion(); - const color = new THREE.Color(); - - for (let i = 0; i < 5000; i++) { - const geometry = new THREE.BoxGeometry(); - - const position = new THREE.Vector3(); - position.x = Math.random() * 10000 - 5000; - position.y = Math.random() * 6000 - 3000; - position.z = Math.random() * 8000 - 4000; - - const rotation = new THREE.Euler(); - rotation.x = Math.random() * 2 * Math.PI; - rotation.y = Math.random() * 2 * Math.PI; - rotation.z = Math.random() * 2 * Math.PI; - - const scale = new THREE.Vector3(); - scale.x = Math.random() * 200 + 100; - scale.y = Math.random() * 200 + 100; - scale.z = Math.random() * 200 + 100; - - quaternion.setFromEuler(rotation); - matrix.compose(position, quaternion, scale); - - geometry.applyMatrix4(matrix); - - // give the geometry's vertices a random color to be displayed and an integer - // identifier as a vertex attribute so boxes can be identified after being merged. - applyVertexColors(geometry, color.setHex(Math.random() * 0xffffff)); - applyId(geometry, i); - - geometries.push(geometry); - - pickingData[i] = { - position: position, - rotation: rotation, - scale: scale, - }; - } - - const mergedGeometry = BufferGeometryUtils.mergeGeometries(geometries); - scene.add(new THREE.Mesh(mergedGeometry, defaultMaterial)); - pickingScene.add(new THREE.Mesh(mergedGeometry, pickingMaterial)); - - highlightBox = new THREE.Mesh(new THREE.BoxGeometry(), new THREE.MeshLambertMaterial({ color: 0xffff00 })); - scene.add(highlightBox); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - controls = new TrackballControls(camera, renderer.domElement); - controls.rotateSpeed = 1.0; - controls.zoomSpeed = 1.2; - controls.panSpeed = 0.8; - controls.noZoom = false; - controls.noPan = false; - controls.staticMoving = true; - controls.dynamicDampingFactor = 0.3; - - stats = new Stats(); - container.appendChild(stats.dom); - - renderer.domElement.addEventListener('pointermove', onPointerMove); -} - -// - -function onPointerMove(e) { - pointer.x = e.clientX; - pointer.y = e.clientY; -} - -function animate() { - render(); - stats.update(); -} - -function pick() { - // render the picking scene off-screen - // set the view offset to represent just a single pixel under the mouse - const dpr = window.devicePixelRatio; - camera.setViewOffset( - renderer.domElement.width, - renderer.domElement.height, - Math.floor(pointer.x * dpr), - Math.floor(pointer.y * dpr), - 1, - 1, - ); - - // render the scene - renderer.setRenderTarget(pickingTexture); - - // clear the background to - 1 meaning no item was hit - clearColor.setRGB(-1, -1, -1); - renderer.setClearColor(clearColor); - renderer.render(pickingScene, camera); - - // clear the view offset so rendering returns to normal - camera.clearViewOffset(); - - // create buffer for reading single pixel - const pixelBuffer = new Int32Array(4); - - // read the pixel - renderer.readRenderTargetPixelsAsync(pickingTexture, 0, 0, 1, 1, pixelBuffer).then(() => { - const id = pixelBuffer[0]; - if (id !== -1) { - // move our highlightBox so that it surrounds the picked object - const data = pickingData[id]; - highlightBox.position.copy(data.position); - highlightBox.rotation.copy(data.rotation); - highlightBox.scale.copy(data.scale).add(offset); - highlightBox.visible = true; - } else { - highlightBox.visible = false; - } - }); -} - -function render() { - controls.update(); - - pick(); - - renderer.setRenderTarget(null); - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_interactive_cubes_ortho.ts b/examples-testing/examples/webgl_interactive_cubes_ortho.ts deleted file mode 100644 index 520674b5f..000000000 --- a/examples-testing/examples/webgl_interactive_cubes_ortho.ts +++ /dev/null @@ -1,129 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let stats; -let camera, scene, raycaster, renderer; - -let theta = 0; -let INTERSECTED; - -const pointer = new THREE.Vector2(); -const radius = 25; -const frustumSize = 50; - -init(); - -function init() { - const aspect = window.innerWidth / window.innerHeight; - camera = new THREE.OrthographicCamera( - (frustumSize * aspect) / -2, - (frustumSize * aspect) / 2, - frustumSize / 2, - frustumSize / -2, - 0.1, - 100, - ); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(1, 1, 1).normalize(); - scene.add(light); - - const geometry = new THREE.BoxGeometry(); - - for (let i = 0; i < 2000; i++) { - const object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff })); - - object.position.x = Math.random() * 40 - 20; - object.position.y = Math.random() * 40 - 20; - object.position.z = Math.random() * 40 - 20; - - object.rotation.x = Math.random() * 2 * Math.PI; - object.rotation.y = Math.random() * 2 * Math.PI; - object.rotation.z = Math.random() * 2 * Math.PI; - - object.scale.x = Math.random() + 0.5; - object.scale.y = Math.random() + 0.5; - object.scale.z = Math.random() + 0.5; - - scene.add(object); - } - - raycaster = new THREE.Raycaster(); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - document.addEventListener('pointermove', onPointerMove); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const aspect = window.innerWidth / window.innerHeight; - - camera.left = (-frustumSize * aspect) / 2; - camera.right = (frustumSize * aspect) / 2; - camera.top = frustumSize / 2; - camera.bottom = -frustumSize / 2; - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onPointerMove(event) { - pointer.x = (event.clientX / window.innerWidth) * 2 - 1; - pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - theta += 0.1; - - camera.position.x = radius * Math.sin(THREE.MathUtils.degToRad(theta)); - camera.position.y = radius * Math.sin(THREE.MathUtils.degToRad(theta)); - camera.position.z = radius * Math.cos(THREE.MathUtils.degToRad(theta)); - camera.lookAt(scene.position); - - camera.updateMatrixWorld(); - - // find intersections - - raycaster.setFromCamera(pointer, camera); - - const intersects = raycaster.intersectObjects(scene.children, false); - - if (intersects.length > 0) { - if (INTERSECTED != intersects[0].object) { - if (INTERSECTED) INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex); - - INTERSECTED = intersects[0].object; - INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex(); - INTERSECTED.material.emissive.setHex(0xff0000); - } - } else { - if (INTERSECTED) INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex); - - INTERSECTED = null; - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_interactive_lines.ts b/examples-testing/examples/webgl_interactive_lines.ts deleted file mode 100644 index b137c5501..000000000 --- a/examples-testing/examples/webgl_interactive_lines.ts +++ /dev/null @@ -1,160 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; -let camera, scene, raycaster, renderer, parentTransform, sphereInter; - -const pointer = new THREE.Vector2(); -const radius = 100; -let theta = 0; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - const info = document.createElement('div'); - info.style.position = 'absolute'; - info.style.top = '10px'; - info.style.width = '100%'; - info.style.textAlign = 'center'; - info.innerHTML = - 'three.js webgl - interactive lines'; - container.appendChild(info); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 10000); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - const geometry = new THREE.SphereGeometry(5); - const material = new THREE.MeshBasicMaterial({ color: 0xff0000 }); - - sphereInter = new THREE.Mesh(geometry, material); - sphereInter.visible = false; - scene.add(sphereInter); - - const lineGeometry = new THREE.BufferGeometry(); - const points = []; - - const point = new THREE.Vector3(); - const direction = new THREE.Vector3(); - - for (let i = 0; i < 50; i++) { - direction.x += Math.random() - 0.5; - direction.y += Math.random() - 0.5; - direction.z += Math.random() - 0.5; - direction.normalize().multiplyScalar(10); - - point.add(direction); - points.push(point.x, point.y, point.z); - } - - lineGeometry.setAttribute('position', new THREE.Float32BufferAttribute(points, 3)); - - parentTransform = new THREE.Object3D(); - parentTransform.position.x = Math.random() * 40 - 20; - parentTransform.position.y = Math.random() * 40 - 20; - parentTransform.position.z = Math.random() * 40 - 20; - - parentTransform.rotation.x = Math.random() * 2 * Math.PI; - parentTransform.rotation.y = Math.random() * 2 * Math.PI; - parentTransform.rotation.z = Math.random() * 2 * Math.PI; - - parentTransform.scale.x = Math.random() + 0.5; - parentTransform.scale.y = Math.random() + 0.5; - parentTransform.scale.z = Math.random() + 0.5; - - for (let i = 0; i < 50; i++) { - let object; - - const lineMaterial = new THREE.LineBasicMaterial({ color: Math.random() * 0xffffff }); - - if (Math.random() > 0.5) { - object = new THREE.Line(lineGeometry, lineMaterial); - } else { - object = new THREE.LineSegments(lineGeometry, lineMaterial); - } - - object.position.x = Math.random() * 400 - 200; - object.position.y = Math.random() * 400 - 200; - object.position.z = Math.random() * 400 - 200; - - object.rotation.x = Math.random() * 2 * Math.PI; - object.rotation.y = Math.random() * 2 * Math.PI; - object.rotation.z = Math.random() * 2 * Math.PI; - - object.scale.x = Math.random() + 0.5; - object.scale.y = Math.random() + 0.5; - object.scale.z = Math.random() + 0.5; - - parentTransform.add(object); - } - - scene.add(parentTransform); - - raycaster = new THREE.Raycaster(); - raycaster.params.Line.threshold = 3; - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - document.addEventListener('pointermove', onPointerMove); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onPointerMove(event) { - pointer.x = (event.clientX / window.innerWidth) * 2 - 1; - pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - theta += 0.1; - - camera.position.x = radius * Math.sin(THREE.MathUtils.degToRad(theta)); - camera.position.y = radius * Math.sin(THREE.MathUtils.degToRad(theta)); - camera.position.z = radius * Math.cos(THREE.MathUtils.degToRad(theta)); - camera.lookAt(scene.position); - - camera.updateMatrixWorld(); - - // find intersections - - raycaster.setFromCamera(pointer, camera); - - const intersects = raycaster.intersectObjects(parentTransform.children, true); - - if (intersects.length > 0) { - sphereInter.visible = true; - sphereInter.position.copy(intersects[0].point); - } else { - sphereInter.visible = false; - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_interactive_points.ts b/examples-testing/examples/webgl_interactive_points.ts deleted file mode 100644 index 93113b867..000000000 --- a/examples-testing/examples/webgl_interactive_points.ts +++ /dev/null @@ -1,143 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; - -let renderer, scene, camera, stats; - -let particles; - -const PARTICLE_SIZE = 20; - -let raycaster, intersects; -let pointer, INTERSECTED; - -init(); - -function init() { - const container = document.getElementById('container'); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 250; - - // - - let boxGeometry = new THREE.BoxGeometry(200, 200, 200, 16, 16, 16); - - // if normal and uv attributes are not removed, mergeVertices() can't consolidate indentical vertices with different normal/uv data - - boxGeometry.deleteAttribute('normal'); - boxGeometry.deleteAttribute('uv'); - - boxGeometry = BufferGeometryUtils.mergeVertices(boxGeometry); - - // - - const positionAttribute = boxGeometry.getAttribute('position'); - - const colors = []; - const sizes = []; - - const color = new THREE.Color(); - - for (let i = 0, l = positionAttribute.count; i < l; i++) { - color.setHSL(0.01 + 0.1 * (i / l), 1.0, 0.5); - color.toArray(colors, i * 3); - - sizes[i] = PARTICLE_SIZE * 0.5; - } - - const geometry = new THREE.BufferGeometry(); - geometry.setAttribute('position', positionAttribute); - geometry.setAttribute('customColor', new THREE.Float32BufferAttribute(colors, 3)); - geometry.setAttribute('size', new THREE.Float32BufferAttribute(sizes, 1)); - - // - - const material = new THREE.ShaderMaterial({ - uniforms: { - color: { value: new THREE.Color(0xffffff) }, - pointTexture: { value: new THREE.TextureLoader().load('textures/sprites/disc.png') }, - alphaTest: { value: 0.9 }, - }, - vertexShader: document.getElementById('vertexshader').textContent, - fragmentShader: document.getElementById('fragmentshader').textContent, - }); - - // - - particles = new THREE.Points(geometry, material); - scene.add(particles); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - raycaster = new THREE.Raycaster(); - pointer = new THREE.Vector2(); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); - document.addEventListener('pointermove', onPointerMove); -} - -function onPointerMove(event) { - pointer.x = (event.clientX / window.innerWidth) * 2 - 1; - pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - particles.rotation.x += 0.0005; - particles.rotation.y += 0.001; - - const geometry = particles.geometry; - const attributes = geometry.attributes; - - raycaster.setFromCamera(pointer, camera); - - intersects = raycaster.intersectObject(particles); - - if (intersects.length > 0) { - if (INTERSECTED != intersects[0].index) { - attributes.size.array[INTERSECTED] = PARTICLE_SIZE; - - INTERSECTED = intersects[0].index; - - attributes.size.array[INTERSECTED] = PARTICLE_SIZE * 1.25; - attributes.size.needsUpdate = true; - } - } else if (INTERSECTED !== null) { - attributes.size.array[INTERSECTED] = PARTICLE_SIZE; - attributes.size.needsUpdate = true; - INTERSECTED = null; - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_interactive_raycasting_points.ts b/examples-testing/examples/webgl_interactive_raycasting_points.ts deleted file mode 100644 index 41c158a43..000000000 --- a/examples-testing/examples/webgl_interactive_raycasting_points.ts +++ /dev/null @@ -1,220 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let renderer, scene, camera, stats; -let pointclouds; -let raycaster; -let intersection = null; -let spheresIndex = 0; -let clock; -let toggle = 0; - -const pointer = new THREE.Vector2(); -const spheres = []; - -const threshold = 0.1; -const pointSize = 0.05; -const width = 80; -const length = 160; -const rotateY = new THREE.Matrix4().makeRotationY(0.005); - -init(); - -function generatePointCloudGeometry(color, width, length) { - const geometry = new THREE.BufferGeometry(); - const numPoints = width * length; - - const positions = new Float32Array(numPoints * 3); - const colors = new Float32Array(numPoints * 3); - - let k = 0; - - for (let i = 0; i < width; i++) { - for (let j = 0; j < length; j++) { - const u = i / width; - const v = j / length; - const x = u - 0.5; - const y = (Math.cos(u * Math.PI * 4) + Math.sin(v * Math.PI * 8)) / 20; - const z = v - 0.5; - - positions[3 * k] = x; - positions[3 * k + 1] = y; - positions[3 * k + 2] = z; - - const intensity = (y + 0.1) * 5; - colors[3 * k] = color.r * intensity; - colors[3 * k + 1] = color.g * intensity; - colors[3 * k + 2] = color.b * intensity; - - k++; - } - } - - geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); - geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3)); - geometry.computeBoundingBox(); - - return geometry; -} - -function generatePointcloud(color, width, length) { - const geometry = generatePointCloudGeometry(color, width, length); - const material = new THREE.PointsMaterial({ size: pointSize, vertexColors: true }); - - return new THREE.Points(geometry, material); -} - -function generateIndexedPointcloud(color, width, length) { - const geometry = generatePointCloudGeometry(color, width, length); - const numPoints = width * length; - const indices = new Uint16Array(numPoints); - - let k = 0; - - for (let i = 0; i < width; i++) { - for (let j = 0; j < length; j++) { - indices[k] = k; - k++; - } - } - - geometry.setIndex(new THREE.BufferAttribute(indices, 1)); - - const material = new THREE.PointsMaterial({ size: pointSize, vertexColors: true }); - - return new THREE.Points(geometry, material); -} - -function generateIndexedWithOffsetPointcloud(color, width, length) { - const geometry = generatePointCloudGeometry(color, width, length); - const numPoints = width * length; - const indices = new Uint16Array(numPoints); - - let k = 0; - - for (let i = 0; i < width; i++) { - for (let j = 0; j < length; j++) { - indices[k] = k; - k++; - } - } - - geometry.setIndex(new THREE.BufferAttribute(indices, 1)); - geometry.addGroup(0, indices.length); - - const material = new THREE.PointsMaterial({ size: pointSize, vertexColors: true }); - - return new THREE.Points(geometry, material); -} - -function init() { - const container = document.getElementById('container'); - - scene = new THREE.Scene(); - - clock = new THREE.Clock(); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.set(10, 10, 10); - camera.lookAt(scene.position); - camera.updateMatrix(); - - // - - const pcBuffer = generatePointcloud(new THREE.Color(1, 0, 0), width, length); - pcBuffer.scale.set(5, 10, 10); - pcBuffer.position.set(-5, 0, 0); - scene.add(pcBuffer); - - const pcIndexed = generateIndexedPointcloud(new THREE.Color(0, 1, 0), width, length); - pcIndexed.scale.set(5, 10, 10); - pcIndexed.position.set(0, 0, 0); - scene.add(pcIndexed); - - const pcIndexedOffset = generateIndexedWithOffsetPointcloud(new THREE.Color(0, 1, 1), width, length); - pcIndexedOffset.scale.set(5, 10, 10); - pcIndexedOffset.position.set(5, 0, 0); - scene.add(pcIndexedOffset); - - pointclouds = [pcBuffer, pcIndexed, pcIndexedOffset]; - - // - - const sphereGeometry = new THREE.SphereGeometry(0.1, 32, 32); - const sphereMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 }); - - for (let i = 0; i < 40; i++) { - const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); - scene.add(sphere); - spheres.push(sphere); - } - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - raycaster = new THREE.Raycaster(); - raycaster.params.Points.threshold = threshold; - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); - document.addEventListener('pointermove', onPointerMove); -} - -function onPointerMove(event) { - pointer.x = (event.clientX / window.innerWidth) * 2 - 1; - pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - camera.applyMatrix4(rotateY); - camera.updateMatrixWorld(); - - raycaster.setFromCamera(pointer, camera); - - const intersections = raycaster.intersectObjects(pointclouds, false); - intersection = intersections.length > 0 ? intersections[0] : null; - - if (toggle > 0.02 && intersection !== null) { - spheres[spheresIndex].position.copy(intersection.point); - spheres[spheresIndex].scale.set(1, 1, 1); - spheresIndex = (spheresIndex + 1) % spheres.length; - - toggle = 0; - } - - for (let i = 0; i < spheres.length; i++) { - const sphere = spheres[i]; - sphere.scale.multiplyScalar(0.98); - sphere.scale.clampScalar(0.01, 1); - } - - toggle += clock.getDelta(); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_interactive_voxelpainter.ts b/examples-testing/examples/webgl_interactive_voxelpainter.ts deleted file mode 100644 index 48b16f3b7..000000000 --- a/examples-testing/examples/webgl_interactive_voxelpainter.ts +++ /dev/null @@ -1,158 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer; -let plane; -let pointer, - raycaster, - isShiftDown = false; - -let rollOverMesh, rollOverMaterial; -let cubeGeo, cubeMaterial; - -const objects = []; - -init(); -render(); - -function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.set(500, 800, 1300); - camera.lookAt(0, 0, 0); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - // roll-over helpers - - const rollOverGeo = new THREE.BoxGeometry(50, 50, 50); - rollOverMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000, opacity: 0.5, transparent: true }); - rollOverMesh = new THREE.Mesh(rollOverGeo, rollOverMaterial); - scene.add(rollOverMesh); - - // cubes - - const map = new THREE.TextureLoader().load('textures/square-outline-textured.png'); - map.colorSpace = THREE.SRGBColorSpace; - cubeGeo = new THREE.BoxGeometry(50, 50, 50); - cubeMaterial = new THREE.MeshLambertMaterial({ color: 0xfeb74c, map: map }); - - // grid - - const gridHelper = new THREE.GridHelper(1000, 20); - scene.add(gridHelper); - - // - - raycaster = new THREE.Raycaster(); - pointer = new THREE.Vector2(); - - const geometry = new THREE.PlaneGeometry(1000, 1000); - geometry.rotateX(-Math.PI / 2); - - plane = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ visible: false })); - scene.add(plane); - - objects.push(plane); - - // lights - - const ambientLight = new THREE.AmbientLight(0x606060, 3); - scene.add(ambientLight); - - const directionalLight = new THREE.DirectionalLight(0xffffff, 3); - directionalLight.position.set(1, 0.75, 0.5).normalize(); - scene.add(directionalLight); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - document.addEventListener('pointermove', onPointerMove); - document.addEventListener('pointerdown', onPointerDown); - document.addEventListener('keydown', onDocumentKeyDown); - document.addEventListener('keyup', onDocumentKeyUp); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function onPointerMove(event) { - pointer.set((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1); - - raycaster.setFromCamera(pointer, camera); - - const intersects = raycaster.intersectObjects(objects, false); - - if (intersects.length > 0) { - const intersect = intersects[0]; - - rollOverMesh.position.copy(intersect.point).add(intersect.face.normal); - rollOverMesh.position.divideScalar(50).floor().multiplyScalar(50).addScalar(25); - - render(); - } -} - -function onPointerDown(event) { - pointer.set((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1); - - raycaster.setFromCamera(pointer, camera); - - const intersects = raycaster.intersectObjects(objects, false); - - if (intersects.length > 0) { - const intersect = intersects[0]; - - // delete cube - - if (isShiftDown) { - if (intersect.object !== plane) { - scene.remove(intersect.object); - - objects.splice(objects.indexOf(intersect.object), 1); - } - - // create cube - } else { - const voxel = new THREE.Mesh(cubeGeo, cubeMaterial); - voxel.position.copy(intersect.point).add(intersect.face.normal); - voxel.position.divideScalar(50).floor().multiplyScalar(50).addScalar(25); - scene.add(voxel); - - objects.push(voxel); - } - - render(); - } -} - -function onDocumentKeyDown(event) { - switch (event.keyCode) { - case 16: - isShiftDown = true; - break; - } -} - -function onDocumentKeyUp(event) { - switch (event.keyCode) { - case 16: - isShiftDown = false; - break; - } -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_layers.ts b/examples-testing/examples/webgl_layers.ts deleted file mode 100644 index 8bdcda7f9..000000000 --- a/examples-testing/examples/webgl_layers.ts +++ /dev/null @@ -1,125 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let container, stats; -let camera, scene, renderer; - -let theta = 0; -const radius = 5; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100); - camera.layers.enable(0); // enabled by default - camera.layers.enable(1); - camera.layers.enable(2); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - const light = new THREE.PointLight(0xffffff, 3, 0, 0); - light.layers.enable(0); - light.layers.enable(1); - light.layers.enable(2); - - scene.add(camera); - camera.add(light); - - const colors = [0xff0000, 0x00ff00, 0x0000ff]; - const geometry = new THREE.BoxGeometry(); - - for (let i = 0; i < 300; i++) { - const layer = i % 3; - - const object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: colors[layer] })); - - object.position.x = Math.random() * 40 - 20; - object.position.y = Math.random() * 40 - 20; - object.position.z = Math.random() * 40 - 20; - - object.rotation.x = Math.random() * 2 * Math.PI; - object.rotation.y = Math.random() * 2 * Math.PI; - object.rotation.z = Math.random() * 2 * Math.PI; - - object.scale.x = Math.random() + 0.5; - object.scale.y = Math.random() + 0.5; - object.scale.z = Math.random() + 0.5; - - object.layers.set(layer); - - scene.add(object); - } - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - const layers = { - 'toggle red': function () { - camera.layers.toggle(0); - }, - - 'toggle green': function () { - camera.layers.toggle(1); - }, - - 'toggle blue': function () { - camera.layers.toggle(2); - }, - - 'enable all': function () { - camera.layers.enableAll(); - }, - - 'disable all': function () { - camera.layers.disableAll(); - }, - }; - - // - // Init gui - const gui = new GUI(); - gui.add(layers, 'toggle red'); - gui.add(layers, 'toggle green'); - gui.add(layers, 'toggle blue'); - gui.add(layers, 'enable all'); - gui.add(layers, 'disable all'); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - theta += 0.1; - - camera.position.x = radius * Math.sin(THREE.MathUtils.degToRad(theta)); - camera.position.y = radius * Math.sin(THREE.MathUtils.degToRad(theta)); - camera.position.z = radius * Math.cos(THREE.MathUtils.degToRad(theta)); - camera.lookAt(scene.position); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_lensflares.ts b/examples-testing/examples/webgl_lensflares.ts deleted file mode 100644 index 230cebfa0..000000000 --- a/examples-testing/examples/webgl_lensflares.ts +++ /dev/null @@ -1,137 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { FlyControls } from 'three/addons/controls/FlyControls.js'; -import { Lensflare, LensflareElement } from 'three/addons/objects/Lensflare.js'; - -let container, stats; - -let camera, scene, renderer; -let controls; - -const clock = new THREE.Clock(); - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - // camera - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 15000); - camera.position.z = 250; - - // scene - - scene = new THREE.Scene(); - scene.background = new THREE.Color().setHSL(0.51, 0.4, 0.01, THREE.SRGBColorSpace); - scene.fog = new THREE.Fog(scene.background, 3500, 15000); - - // world - - const s = 250; - - const geometry = new THREE.BoxGeometry(s, s, s); - const material = new THREE.MeshPhongMaterial({ color: 0xffffff, specular: 0xffffff, shininess: 50 }); - - for (let i = 0; i < 3000; i++) { - const mesh = new THREE.Mesh(geometry, material); - - mesh.position.x = 8000 * (2.0 * Math.random() - 1.0); - mesh.position.y = 8000 * (2.0 * Math.random() - 1.0); - mesh.position.z = 8000 * (2.0 * Math.random() - 1.0); - - mesh.rotation.x = Math.random() * Math.PI; - mesh.rotation.y = Math.random() * Math.PI; - mesh.rotation.z = Math.random() * Math.PI; - - mesh.matrixAutoUpdate = false; - mesh.updateMatrix(); - - scene.add(mesh); - } - - // lights - - const dirLight = new THREE.DirectionalLight(0xffffff, 0.15); - dirLight.position.set(0, -1, 0).normalize(); - dirLight.color.setHSL(0.1, 0.7, 0.5); - scene.add(dirLight); - - // lensflares - const textureLoader = new THREE.TextureLoader(); - - const textureFlare0 = textureLoader.load('textures/lensflare/lensflare0.png'); - const textureFlare3 = textureLoader.load('textures/lensflare/lensflare3.png'); - - addLight(0.55, 0.9, 0.5, 5000, 0, -1000); - addLight(0.08, 0.8, 0.5, 0, 0, -1000); - addLight(0.995, 0.5, 0.9, 5000, 5000, -1000); - - function addLight(h, s, l, x, y, z) { - const light = new THREE.PointLight(0xffffff, 1.5, 2000, 0); - light.color.setHSL(h, s, l); - light.position.set(x, y, z); - scene.add(light); - - const lensflare = new Lensflare(); - lensflare.addElement(new LensflareElement(textureFlare0, 700, 0, light.color)); - lensflare.addElement(new LensflareElement(textureFlare3, 60, 0.6)); - lensflare.addElement(new LensflareElement(textureFlare3, 70, 0.7)); - lensflare.addElement(new LensflareElement(textureFlare3, 120, 0.9)); - lensflare.addElement(new LensflareElement(textureFlare3, 70, 1)); - light.add(lensflare); - } - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - controls = new FlyControls(camera, renderer.domElement); - - controls.movementSpeed = 2500; - controls.domElement = container; - controls.rollSpeed = Math.PI / 6; - controls.autoForward = false; - controls.dragToLook = false; - - // stats - - stats = new Stats(); - container.appendChild(stats.dom); - - // events - - window.addEventListener('resize', onWindowResize); -} - -// - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - const delta = clock.getDelta(); - - controls.update(delta); - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_lightprobe.ts b/examples-testing/examples/webgl_lightprobe.ts deleted file mode 100644 index 2efcad52a..000000000 --- a/examples-testing/examples/webgl_lightprobe.ts +++ /dev/null @@ -1,133 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { LightProbeGenerator } from 'three/addons/lights/LightProbeGenerator.js'; - -let mesh, renderer, scene, camera; - -let gui; - -let lightProbe; -let directionalLight; - -// linear color space -const API = { - lightProbeIntensity: 1.0, - directionalLightIntensity: 0.6, - envMapIntensity: 1, -}; - -init(); - -function init() { - // renderer - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - // tone mapping - renderer.toneMapping = THREE.NoToneMapping; - - // scene - scene = new THREE.Scene(); - - // camera - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 0, 30); - - // controls - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); - controls.minDistance = 10; - controls.maxDistance = 50; - controls.enablePan = false; - - // probe - lightProbe = new THREE.LightProbe(); - scene.add(lightProbe); - - // light - directionalLight = new THREE.DirectionalLight(0xffffff, API.directionalLightIntensity); - directionalLight.position.set(10, 10, 10); - scene.add(directionalLight); - - // envmap - const genCubeUrls = function (prefix, postfix) { - return [ - prefix + 'px' + postfix, - prefix + 'nx' + postfix, - prefix + 'py' + postfix, - prefix + 'ny' + postfix, - prefix + 'pz' + postfix, - prefix + 'nz' + postfix, - ]; - }; - - const urls = genCubeUrls('textures/cube/pisa/', '.png'); - - new THREE.CubeTextureLoader().load(urls, function (cubeTexture) { - scene.background = cubeTexture; - - lightProbe.copy(LightProbeGenerator.fromCubeTexture(cubeTexture)); - - const geometry = new THREE.SphereGeometry(5, 64, 32); - //const geometry = new THREE.TorusKnotGeometry( 4, 1.5, 256, 32, 2, 3 ); - - const material = new THREE.MeshStandardMaterial({ - color: 0xffffff, - metalness: 0, - roughness: 0, - envMap: cubeTexture, - envMapIntensity: API.envMapIntensity, - }); - - // mesh - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - render(); - }); - - // gui - gui = new GUI({ title: 'Intensity' }); - - gui.add(API, 'lightProbeIntensity', 0, 1, 0.02) - .name('light probe') - .onChange(function () { - lightProbe.intensity = API.lightProbeIntensity; - render(); - }); - - gui.add(API, 'directionalLightIntensity', 0, 1, 0.02) - .name('directional light') - .onChange(function () { - directionalLight.intensity = API.directionalLightIntensity; - render(); - }); - - gui.add(API, 'envMapIntensity', 0, 1, 0.02) - .name('envMap') - .onChange(function () { - mesh.material.envMapIntensity = API.envMapIntensity; - render(); - }); - - // listener - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_lightprobe_cubecamera.ts b/examples-testing/examples/webgl_lightprobe_cubecamera.ts deleted file mode 100644 index c714d2978..000000000 --- a/examples-testing/examples/webgl_lightprobe_cubecamera.ts +++ /dev/null @@ -1,83 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { LightProbeHelper } from 'three/addons/helpers/LightProbeHelper.js'; -import { LightProbeGenerator } from 'three/addons/lights/LightProbeGenerator.js'; - -let renderer, scene, camera, cubeCamera; - -let lightProbe; - -init(); - -function init() { - // renderer - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - // scene - scene = new THREE.Scene(); - - // camera - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 0, 30); - - const cubeRenderTarget = new THREE.WebGLCubeRenderTarget(256); - - cubeCamera = new THREE.CubeCamera(1, 1000, cubeRenderTarget); - - // controls - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); - controls.minDistance = 10; - controls.maxDistance = 50; - controls.enablePan = false; - - // probe - lightProbe = new THREE.LightProbe(); - scene.add(lightProbe); - - // envmap - const genCubeUrls = function (prefix, postfix) { - return [ - prefix + 'px' + postfix, - prefix + 'nx' + postfix, - prefix + 'py' + postfix, - prefix + 'ny' + postfix, - prefix + 'pz' + postfix, - prefix + 'nz' + postfix, - ]; - }; - - const urls = genCubeUrls('textures/cube/pisa/', '.png'); - - new THREE.CubeTextureLoader().load(urls, function (cubeTexture) { - scene.background = cubeTexture; - - cubeCamera.update(renderer, scene); - - lightProbe.copy(LightProbeGenerator.fromCubeRenderTarget(renderer, cubeRenderTarget)); - - scene.add(new LightProbeHelper(lightProbe, 5)); - - render(); - }); - - // listener - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_lights_hemisphere.ts b/examples-testing/examples/webgl_lights_hemisphere.ts deleted file mode 100644 index 15bc76099..000000000 --- a/examples-testing/examples/webgl_lights_hemisphere.ts +++ /dev/null @@ -1,188 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -let camera, scene, renderer; -const mixers = []; -let stats; - -const clock = new THREE.Clock(); - -init(); - -function init() { - const container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 5000); - camera.position.set(0, 0, 250); - - scene = new THREE.Scene(); - scene.background = new THREE.Color().setHSL(0.6, 0, 1); - scene.fog = new THREE.Fog(scene.background, 1, 5000); - - // LIGHTS - - const hemiLight = new THREE.HemisphereLight(0xffffff, 0xffffff, 2); - hemiLight.color.setHSL(0.6, 1, 0.6); - hemiLight.groundColor.setHSL(0.095, 1, 0.75); - hemiLight.position.set(0, 50, 0); - scene.add(hemiLight); - - const hemiLightHelper = new THREE.HemisphereLightHelper(hemiLight, 10); - scene.add(hemiLightHelper); - - // - - const dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.color.setHSL(0.1, 1, 0.95); - dirLight.position.set(-1, 1.75, 1); - dirLight.position.multiplyScalar(30); - scene.add(dirLight); - - dirLight.castShadow = true; - - dirLight.shadow.mapSize.width = 2048; - dirLight.shadow.mapSize.height = 2048; - - const d = 50; - - dirLight.shadow.camera.left = -d; - dirLight.shadow.camera.right = d; - dirLight.shadow.camera.top = d; - dirLight.shadow.camera.bottom = -d; - - dirLight.shadow.camera.far = 3500; - dirLight.shadow.bias = -0.0001; - - const dirLightHelper = new THREE.DirectionalLightHelper(dirLight, 10); - scene.add(dirLightHelper); - - // GROUND - - const groundGeo = new THREE.PlaneGeometry(10000, 10000); - const groundMat = new THREE.MeshLambertMaterial({ color: 0xffffff }); - groundMat.color.setHSL(0.095, 1, 0.75); - - const ground = new THREE.Mesh(groundGeo, groundMat); - ground.position.y = -33; - ground.rotation.x = -Math.PI / 2; - ground.receiveShadow = true; - scene.add(ground); - - // SKYDOME - - const vertexShader = document.getElementById('vertexShader').textContent; - const fragmentShader = document.getElementById('fragmentShader').textContent; - const uniforms = { - topColor: { value: new THREE.Color(0x0077ff) }, - bottomColor: { value: new THREE.Color(0xffffff) }, - offset: { value: 33 }, - exponent: { value: 0.6 }, - }; - uniforms['topColor'].value.copy(hemiLight.color); - - scene.fog.color.copy(uniforms['bottomColor'].value); - - const skyGeo = new THREE.SphereGeometry(4000, 32, 15); - const skyMat = new THREE.ShaderMaterial({ - uniforms: uniforms, - vertexShader: vertexShader, - fragmentShader: fragmentShader, - side: THREE.BackSide, - }); - - const sky = new THREE.Mesh(skyGeo, skyMat); - scene.add(sky); - - // MODEL - - const loader = new GLTFLoader(); - - loader.load('models/gltf/Flamingo.glb', function (gltf) { - const mesh = gltf.scene.children[0]; - - const s = 0.35; - mesh.scale.set(s, s, s); - mesh.position.y = 15; - mesh.rotation.y = -1; - - mesh.castShadow = true; - mesh.receiveShadow = true; - - scene.add(mesh); - - const mixer = new THREE.AnimationMixer(mesh); - mixer.clipAction(gltf.animations[0]).setDuration(1).play(); - mixers.push(mixer); - }); - - // RENDERER - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - renderer.shadowMap.enabled = true; - - // STATS - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - const params = { - toggleHemisphereLight: function () { - hemiLight.visible = !hemiLight.visible; - hemiLightHelper.visible = !hemiLightHelper.visible; - }, - toggleDirectionalLight: function () { - dirLight.visible = !dirLight.visible; - dirLightHelper.visible = !dirLightHelper.visible; - }, - shadowIntensity: 1, - }; - - const gui = new GUI(); - - gui.add(params, 'toggleHemisphereLight').name('toggle hemisphere light'); - gui.add(params, 'toggleDirectionalLight').name('toggle directional light'); - gui.add(params, 'shadowIntensity', 0, 1) - .name('shadow intensity') - .onChange(value => { - dirLight.shadow.intensity = value; - }); - gui.open(); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - const delta = clock.getDelta(); - - for (let i = 0; i < mixers.length; i++) { - mixers[i].update(delta); - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_lights_physical.ts b/examples-testing/examples/webgl_lights_physical.ts deleted file mode 100644 index 707ef200e..000000000 --- a/examples-testing/examples/webgl_lights_physical.ts +++ /dev/null @@ -1,237 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer, bulbLight, bulbMat, hemiLight, stats; -let ballMat, cubeMat, floorMat; - -let previousShadowMap = false; - -// ref for lumens: http://www.power-sure.com/lumens.htm -const bulbLuminousPowers = { - '110000 lm (1000W)': 110000, - '3500 lm (300W)': 3500, - '1700 lm (100W)': 1700, - '800 lm (60W)': 800, - '400 lm (40W)': 400, - '180 lm (25W)': 180, - '20 lm (4W)': 20, - Off: 0, -}; - -// ref for solar irradiances: https://en.wikipedia.org/wiki/Lux -const hemiLuminousIrradiances = { - '0.0001 lx (Moonless Night)': 0.0001, - '0.002 lx (Night Airglow)': 0.002, - '0.5 lx (Full Moon)': 0.5, - '3.4 lx (City Twilight)': 3.4, - '50 lx (Living Room)': 50, - '100 lx (Very Overcast)': 100, - '350 lx (Office Room)': 350, - '400 lx (Sunrise/Sunset)': 400, - '1000 lx (Overcast)': 1000, - '18000 lx (Daylight)': 18000, - '50000 lx (Direct Sun)': 50000, -}; - -const params = { - shadows: true, - exposure: 0.68, - bulbPower: Object.keys(bulbLuminousPowers)[4], - hemiIrradiance: Object.keys(hemiLuminousIrradiances)[0], -}; - -init(); - -function init() { - const container = document.getElementById('container'); - - stats = new Stats(); - container.appendChild(stats.dom); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.x = -4; - camera.position.z = 4; - camera.position.y = 2; - - scene = new THREE.Scene(); - - const bulbGeometry = new THREE.SphereGeometry(0.02, 16, 8); - bulbLight = new THREE.PointLight(0xffee88, 1, 100, 2); - - bulbMat = new THREE.MeshStandardMaterial({ - emissive: 0xffffee, - emissiveIntensity: 1, - color: 0x000000, - }); - bulbLight.add(new THREE.Mesh(bulbGeometry, bulbMat)); - bulbLight.position.set(0, 2, 0); - bulbLight.castShadow = true; - scene.add(bulbLight); - - hemiLight = new THREE.HemisphereLight(0xddeeff, 0x0f0e0d, 0.02); - scene.add(hemiLight); - - floorMat = new THREE.MeshStandardMaterial({ - roughness: 0.8, - color: 0xffffff, - metalness: 0.2, - bumpScale: 1, - }); - const textureLoader = new THREE.TextureLoader(); - textureLoader.load('textures/hardwood2_diffuse.jpg', function (map) { - map.wrapS = THREE.RepeatWrapping; - map.wrapT = THREE.RepeatWrapping; - map.anisotropy = 4; - map.repeat.set(10, 24); - map.colorSpace = THREE.SRGBColorSpace; - floorMat.map = map; - floorMat.needsUpdate = true; - }); - textureLoader.load('textures/hardwood2_bump.jpg', function (map) { - map.wrapS = THREE.RepeatWrapping; - map.wrapT = THREE.RepeatWrapping; - map.anisotropy = 4; - map.repeat.set(10, 24); - floorMat.bumpMap = map; - floorMat.needsUpdate = true; - }); - textureLoader.load('textures/hardwood2_roughness.jpg', function (map) { - map.wrapS = THREE.RepeatWrapping; - map.wrapT = THREE.RepeatWrapping; - map.anisotropy = 4; - map.repeat.set(10, 24); - floorMat.roughnessMap = map; - floorMat.needsUpdate = true; - }); - - cubeMat = new THREE.MeshStandardMaterial({ - roughness: 0.7, - color: 0xffffff, - bumpScale: 1, - metalness: 0.2, - }); - textureLoader.load('textures/brick_diffuse.jpg', function (map) { - map.wrapS = THREE.RepeatWrapping; - map.wrapT = THREE.RepeatWrapping; - map.anisotropy = 4; - map.repeat.set(1, 1); - map.colorSpace = THREE.SRGBColorSpace; - cubeMat.map = map; - cubeMat.needsUpdate = true; - }); - textureLoader.load('textures/brick_bump.jpg', function (map) { - map.wrapS = THREE.RepeatWrapping; - map.wrapT = THREE.RepeatWrapping; - map.anisotropy = 4; - map.repeat.set(1, 1); - cubeMat.bumpMap = map; - cubeMat.needsUpdate = true; - }); - - ballMat = new THREE.MeshStandardMaterial({ - color: 0xffffff, - roughness: 0.5, - metalness: 1.0, - }); - textureLoader.load('textures/planets/earth_atmos_2048.jpg', function (map) { - map.anisotropy = 4; - map.colorSpace = THREE.SRGBColorSpace; - ballMat.map = map; - ballMat.needsUpdate = true; - }); - textureLoader.load('textures/planets/earth_specular_2048.jpg', function (map) { - map.anisotropy = 4; - map.colorSpace = THREE.SRGBColorSpace; - ballMat.metalnessMap = map; - ballMat.needsUpdate = true; - }); - - const floorGeometry = new THREE.PlaneGeometry(20, 20); - const floorMesh = new THREE.Mesh(floorGeometry, floorMat); - floorMesh.receiveShadow = true; - floorMesh.rotation.x = -Math.PI / 2.0; - scene.add(floorMesh); - - const ballGeometry = new THREE.SphereGeometry(0.25, 32, 32); - const ballMesh = new THREE.Mesh(ballGeometry, ballMat); - ballMesh.position.set(1, 0.25, 1); - ballMesh.rotation.y = Math.PI; - ballMesh.castShadow = true; - scene.add(ballMesh); - - const boxGeometry = new THREE.BoxGeometry(0.5, 0.5, 0.5); - const boxMesh = new THREE.Mesh(boxGeometry, cubeMat); - boxMesh.position.set(-0.5, 0.25, -1); - boxMesh.castShadow = true; - scene.add(boxMesh); - - const boxMesh2 = new THREE.Mesh(boxGeometry, cubeMat); - boxMesh2.position.set(0, 0.25, -5); - boxMesh2.castShadow = true; - scene.add(boxMesh2); - - const boxMesh3 = new THREE.Mesh(boxGeometry, cubeMat); - boxMesh3.position.set(7, 0.25, 0); - boxMesh3.castShadow = true; - scene.add(boxMesh3); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - renderer.toneMapping = THREE.ReinhardToneMapping; - container.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 1; - controls.maxDistance = 20; - - window.addEventListener('resize', onWindowResize); - - const gui = new GUI(); - - gui.add(params, 'hemiIrradiance', Object.keys(hemiLuminousIrradiances)); - gui.add(params, 'bulbPower', Object.keys(bulbLuminousPowers)); - gui.add(params, 'exposure', 0, 1); - gui.add(params, 'shadows'); - gui.open(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - renderer.toneMappingExposure = Math.pow(params.exposure, 5.0); // to allow for very bright scenes. - renderer.shadowMap.enabled = params.shadows; - bulbLight.castShadow = params.shadows; - - if (params.shadows !== previousShadowMap) { - ballMat.needsUpdate = true; - cubeMat.needsUpdate = true; - floorMat.needsUpdate = true; - previousShadowMap = params.shadows; - } - - bulbLight.power = bulbLuminousPowers[params.bulbPower]; - bulbMat.emissiveIntensity = bulbLight.intensity / Math.pow(0.02, 2.0); // convert from intensity to irradiance at bulb surface - - hemiLight.intensity = hemiLuminousIrradiances[params.hemiIrradiance]; - const time = Date.now() * 0.0005; - - bulbLight.position.y = Math.cos(time) * 0.75 + 1.25; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_lights_pointlights.ts b/examples-testing/examples/webgl_lights_pointlights.ts deleted file mode 100644 index ea95070ce..000000000 --- a/examples-testing/examples/webgl_lights_pointlights.ts +++ /dev/null @@ -1,100 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; - -let camera, scene, renderer, light1, light2, light3, light4, object, stats; - -const clock = new THREE.Clock(); - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 100; - - scene = new THREE.Scene(); - - //model - - const loader = new OBJLoader(); - loader.load('models/obj/walt/WaltHead.obj', function (obj) { - object = obj; - object.scale.multiplyScalar(0.8); - object.position.y = -30; - scene.add(object); - }); - - const sphere = new THREE.SphereGeometry(0.5, 16, 8); - - //lights - - light1 = new THREE.PointLight(0xff0040, 400); - light1.add(new THREE.Mesh(sphere, new THREE.MeshBasicMaterial({ color: 0xff0040 }))); - scene.add(light1); - - light2 = new THREE.PointLight(0x0040ff, 400); - light2.add(new THREE.Mesh(sphere, new THREE.MeshBasicMaterial({ color: 0x0040ff }))); - scene.add(light2); - - light3 = new THREE.PointLight(0x80ff80, 400); - light3.add(new THREE.Mesh(sphere, new THREE.MeshBasicMaterial({ color: 0x80ff80 }))); - scene.add(light3); - - light4 = new THREE.PointLight(0xffaa00, 400); - light4.add(new THREE.Mesh(sphere, new THREE.MeshBasicMaterial({ color: 0xffaa00 }))); - scene.add(light4); - - //renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - //stats - - stats = new Stats(); - document.body.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - const time = Date.now() * 0.0005; - const delta = clock.getDelta(); - - if (object) object.rotation.y -= 0.5 * delta; - - light1.position.x = Math.sin(time * 0.7) * 30; - light1.position.y = Math.cos(time * 0.5) * 40; - light1.position.z = Math.cos(time * 0.3) * 30; - - light2.position.x = Math.cos(time * 0.3) * 30; - light2.position.y = Math.sin(time * 0.5) * 40; - light2.position.z = Math.sin(time * 0.7) * 30; - - light3.position.x = Math.sin(time * 0.7) * 30; - light3.position.y = Math.cos(time * 0.3) * 40; - light3.position.z = Math.sin(time * 0.5) * 30; - - light4.position.x = Math.sin(time * 0.3) * 30; - light4.position.y = Math.cos(time * 0.7) * 40; - light4.position.z = Math.sin(time * 0.5) * 30; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_lights_rectarealight.ts b/examples-testing/examples/webgl_lights_rectarealight.ts deleted file mode 100644 index b841fa6b5..000000000 --- a/examples-testing/examples/webgl_lights_rectarealight.ts +++ /dev/null @@ -1,79 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { RectAreaLightHelper } from 'three/addons/helpers/RectAreaLightHelper.js'; -import { RectAreaLightUniformsLib } from 'three/addons/lights/RectAreaLightUniformsLib.js'; - -let renderer, scene, camera; -let stats, meshKnot; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animation); - document.body.appendChild(renderer.domElement); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 5, -15); - - scene = new THREE.Scene(); - - RectAreaLightUniformsLib.init(); - - const rectLight1 = new THREE.RectAreaLight(0xff0000, 5, 4, 10); - rectLight1.position.set(-5, 5, 5); - scene.add(rectLight1); - - const rectLight2 = new THREE.RectAreaLight(0x00ff00, 5, 4, 10); - rectLight2.position.set(0, 5, 5); - scene.add(rectLight2); - - const rectLight3 = new THREE.RectAreaLight(0x0000ff, 5, 4, 10); - rectLight3.position.set(5, 5, 5); - scene.add(rectLight3); - - scene.add(new RectAreaLightHelper(rectLight1)); - scene.add(new RectAreaLightHelper(rectLight2)); - scene.add(new RectAreaLightHelper(rectLight3)); - - const geoFloor = new THREE.BoxGeometry(2000, 0.1, 2000); - const matStdFloor = new THREE.MeshStandardMaterial({ color: 0xbcbcbc, roughness: 0.1, metalness: 0 }); - const mshStdFloor = new THREE.Mesh(geoFloor, matStdFloor); - scene.add(mshStdFloor); - - const geoKnot = new THREE.TorusKnotGeometry(1.5, 0.5, 200, 16); - const matKnot = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0, metalness: 0 }); - meshKnot = new THREE.Mesh(geoKnot, matKnot); - meshKnot.position.set(0, 5, 0); - scene.add(meshKnot); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.copy(meshKnot.position); - controls.update(); - - // - - window.addEventListener('resize', onWindowResize); - - stats = new Stats(); - document.body.appendChild(stats.dom); -} - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); -} - -function animation(time) { - meshKnot.rotation.y = time / 1000; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_lights_spotlight.ts b/examples-testing/examples/webgl_lights_spotlight.ts deleted file mode 100644 index 894abaf6f..000000000 --- a/examples-testing/examples/webgl_lights_spotlight.ts +++ /dev/null @@ -1,183 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { PLYLoader } from 'three/addons/loaders/PLYLoader.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let renderer, scene, camera; - -let spotLight, lightHelper; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - renderer.shadowMap.enabled = true; - renderer.shadowMap.type = THREE.PCFSoftShadowMap; - - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1; - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(7, 4, 1); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 2; - controls.maxDistance = 10; - controls.maxPolarAngle = Math.PI / 2; - controls.target.set(0, 1, 0); - controls.update(); - - const ambient = new THREE.HemisphereLight(0xffffff, 0x8d8d8d, 0.15); - scene.add(ambient); - - const loader = new THREE.TextureLoader().setPath('textures/'); - const filenames = ['disturb.jpg', 'colors.png', 'uv_grid_opengl.jpg']; - - const textures = { none: null }; - - for (let i = 0; i < filenames.length; i++) { - const filename = filenames[i]; - - const texture = loader.load(filename); - texture.minFilter = THREE.LinearFilter; - texture.magFilter = THREE.LinearFilter; - texture.colorSpace = THREE.SRGBColorSpace; - - textures[filename] = texture; - } - - spotLight = new THREE.SpotLight(0xffffff, 100); - spotLight.position.set(2.5, 5, 2.5); - spotLight.angle = Math.PI / 6; - spotLight.penumbra = 1; - spotLight.decay = 2; - spotLight.distance = 0; - spotLight.map = textures['disturb.jpg']; - - spotLight.castShadow = true; - spotLight.shadow.mapSize.width = 1024; - spotLight.shadow.mapSize.height = 1024; - spotLight.shadow.camera.near = 1; - spotLight.shadow.camera.far = 10; - spotLight.shadow.focus = 1; - scene.add(spotLight); - - lightHelper = new THREE.SpotLightHelper(spotLight); - scene.add(lightHelper); - - // - - const geometry = new THREE.PlaneGeometry(200, 200); - const material = new THREE.MeshLambertMaterial({ color: 0xbcbcbc }); - - const mesh = new THREE.Mesh(geometry, material); - mesh.position.set(0, -1, 0); - mesh.rotation.x = -Math.PI / 2; - mesh.receiveShadow = true; - scene.add(mesh); - - // - - new PLYLoader().load('models/ply/binary/Lucy100k.ply', function (geometry) { - geometry.scale(0.0024, 0.0024, 0.0024); - geometry.computeVertexNormals(); - - const material = new THREE.MeshLambertMaterial(); - - const mesh = new THREE.Mesh(geometry, material); - mesh.rotation.y = -Math.PI / 2; - mesh.position.y = 0.8; - mesh.castShadow = true; - mesh.receiveShadow = true; - scene.add(mesh); - }); - - window.addEventListener('resize', onWindowResize); - - // GUI - - const gui = new GUI(); - - const params = { - map: textures['disturb.jpg'], - color: spotLight.color.getHex(), - intensity: spotLight.intensity, - distance: spotLight.distance, - angle: spotLight.angle, - penumbra: spotLight.penumbra, - decay: spotLight.decay, - focus: spotLight.shadow.focus, - shadows: true, - }; - - gui.add(params, 'map', textures).onChange(function (val) { - spotLight.map = val; - }); - - gui.addColor(params, 'color').onChange(function (val) { - spotLight.color.setHex(val); - }); - - gui.add(params, 'intensity', 0, 500).onChange(function (val) { - spotLight.intensity = val; - }); - - gui.add(params, 'distance', 0, 20).onChange(function (val) { - spotLight.distance = val; - }); - - gui.add(params, 'angle', 0, Math.PI / 3).onChange(function (val) { - spotLight.angle = val; - }); - - gui.add(params, 'penumbra', 0, 1).onChange(function (val) { - spotLight.penumbra = val; - }); - - gui.add(params, 'decay', 1, 2).onChange(function (val) { - spotLight.decay = val; - }); - - gui.add(params, 'focus', 0, 1).onChange(function (val) { - spotLight.shadow.focus = val; - }); - - gui.add(params, 'shadows').onChange(function (val) { - renderer.shadowMap.enabled = val; - - scene.traverse(function (child) { - if (child.material) { - child.material.needsUpdate = true; - } - }); - }); - - gui.open(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const time = performance.now() / 3000; - - spotLight.position.x = Math.cos(time) * 2.5; - spotLight.position.z = Math.sin(time) * 2.5; - - lightHelper.update(); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_lights_spotlights.ts b/examples-testing/examples/webgl_lights_spotlights.ts deleted file mode 100644 index 70caf5a58..000000000 --- a/examples-testing/examples/webgl_lights_spotlights.ts +++ /dev/null @@ -1,133 +0,0 @@ -import * as THREE from 'three'; -import TWEEN from 'three/addons/libs/tween.module.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -const renderer = new THREE.WebGLRenderer({ antialias: true }); -renderer.setPixelRatio(window.devicePixelRatio); -renderer.setSize(window.innerWidth, window.innerHeight); -renderer.setAnimationLoop(animate); - -const camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 100); - -const controls = new OrbitControls(camera, renderer.domElement); - -const scene = new THREE.Scene(); - -const matFloor = new THREE.MeshPhongMaterial({ color: 0x808080 }); -const matBox = new THREE.MeshPhongMaterial({ color: 0xaaaaaa }); - -const geoFloor = new THREE.PlaneGeometry(100, 100); -const geoBox = new THREE.BoxGeometry(0.3, 0.1, 0.2); - -const mshFloor = new THREE.Mesh(geoFloor, matFloor); -mshFloor.rotation.x = -Math.PI * 0.5; -const mshBox = new THREE.Mesh(geoBox, matBox); - -const ambient = new THREE.AmbientLight(0x444444); - -const spotLight1 = createSpotlight(0xff7f00); -const spotLight2 = createSpotlight(0x00ff7f); -const spotLight3 = createSpotlight(0x7f00ff); - -let lightHelper1, lightHelper2, lightHelper3; - -function init() { - renderer.shadowMap.enabled = true; - renderer.shadowMap.type = THREE.PCFSoftShadowMap; - - camera.position.set(4.6, 2.2, -2.1); - - spotLight1.position.set(1.5, 4, 4.5); - spotLight2.position.set(0, 4, 3.5); - spotLight3.position.set(-1.5, 4, 4.5); - - lightHelper1 = new THREE.SpotLightHelper(spotLight1); - lightHelper2 = new THREE.SpotLightHelper(spotLight2); - lightHelper3 = new THREE.SpotLightHelper(spotLight3); - - mshFloor.receiveShadow = true; - mshFloor.position.set(0, -0.05, 0); - - mshBox.castShadow = true; - mshBox.receiveShadow = true; - mshBox.position.set(0, 0.5, 0); - - scene.add(mshFloor); - scene.add(mshBox); - scene.add(ambient); - scene.add(spotLight1, spotLight2, spotLight3); - scene.add(lightHelper1, lightHelper2, lightHelper3); - - document.body.appendChild(renderer.domElement); - window.addEventListener('resize', onWindowResize); - - controls.target.set(0, 0.5, 0); - controls.maxPolarAngle = Math.PI / 2; - controls.minDistance = 1; - controls.maxDistance = 10; - controls.update(); -} - -function createSpotlight(color) { - const newObj = new THREE.SpotLight(color, 10); - - newObj.castShadow = true; - newObj.angle = 0.3; - newObj.penumbra = 0.2; - newObj.decay = 2; - newObj.distance = 50; - - return newObj; -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function tween(light) { - new TWEEN.Tween(light) - .to( - { - angle: Math.random() * 0.7 + 0.1, - penumbra: Math.random() + 1, - }, - Math.random() * 3000 + 2000, - ) - .easing(TWEEN.Easing.Quadratic.Out) - .start(); - - new TWEEN.Tween(light.position) - .to( - { - x: Math.random() * 3 - 1.5, - y: Math.random() * 1 + 1.5, - z: Math.random() * 3 - 1.5, - }, - Math.random() * 3000 + 2000, - ) - .easing(TWEEN.Easing.Quadratic.Out) - .start(); -} - -function updateTweens() { - tween(spotLight1); - tween(spotLight2); - tween(spotLight3); - - setTimeout(updateTweens, 5000); -} - -function animate() { - TWEEN.update(); - - if (lightHelper1) lightHelper1.update(); - if (lightHelper2) lightHelper2.update(); - if (lightHelper3) lightHelper3.update(); - - renderer.render(scene, camera); -} - -init(); -updateTweens(); diff --git a/examples-testing/examples/webgl_lines_colors.ts b/examples-testing/examples/webgl_lines_colors.ts deleted file mode 100644 index 9da19ee2e..000000000 --- a/examples-testing/examples/webgl_lines_colors.ts +++ /dev/null @@ -1,181 +0,0 @@ -import * as THREE from 'three'; - -import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js'; - -let mouseX = 0, - mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -let camera, scene, renderer; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(33, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 1000; - - scene = new THREE.Scene(); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - const hilbertPoints = GeometryUtils.hilbert3D(new THREE.Vector3(0, 0, 0), 200.0, 1, 0, 1, 2, 3, 4, 5, 6, 7); - - const geometry1 = new THREE.BufferGeometry(); - const geometry2 = new THREE.BufferGeometry(); - const geometry3 = new THREE.BufferGeometry(); - - const subdivisions = 6; - - let vertices = []; - let colors1 = []; - let colors2 = []; - let colors3 = []; - - const point = new THREE.Vector3(); - const color = new THREE.Color(); - - const spline = new THREE.CatmullRomCurve3(hilbertPoints); - - for (let i = 0; i < hilbertPoints.length * subdivisions; i++) { - const t = i / (hilbertPoints.length * subdivisions); - spline.getPoint(t, point); - - vertices.push(point.x, point.y, point.z); - - color.setHSL(0.6, 1.0, Math.max(0, -point.x / 200) + 0.5, THREE.SRGBColorSpace); - colors1.push(color.r, color.g, color.b); - - color.setHSL(0.9, 1.0, Math.max(0, -point.y / 200) + 0.5, THREE.SRGBColorSpace); - colors2.push(color.r, color.g, color.b); - - color.setHSL(i / (hilbertPoints.length * subdivisions), 1.0, 0.5, THREE.SRGBColorSpace); - colors3.push(color.r, color.g, color.b); - } - - geometry1.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); - geometry2.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); - geometry3.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); - - geometry1.setAttribute('color', new THREE.Float32BufferAttribute(colors1, 3)); - geometry2.setAttribute('color', new THREE.Float32BufferAttribute(colors2, 3)); - geometry3.setAttribute('color', new THREE.Float32BufferAttribute(colors3, 3)); - - // - - const geometry4 = new THREE.BufferGeometry(); - const geometry5 = new THREE.BufferGeometry(); - const geometry6 = new THREE.BufferGeometry(); - - vertices = []; - colors1 = []; - colors2 = []; - colors3 = []; - - for (let i = 0; i < hilbertPoints.length; i++) { - const point = hilbertPoints[i]; - - vertices.push(point.x, point.y, point.z); - - color.setHSL(0.6, 1.0, Math.max(0, (200 - hilbertPoints[i].x) / 400) * 0.5 + 0.5, THREE.SRGBColorSpace); - colors1.push(color.r, color.g, color.b); - - color.setHSL(0.3, 1.0, Math.max(0, (200 + hilbertPoints[i].x) / 400) * 0.5, THREE.SRGBColorSpace); - colors2.push(color.r, color.g, color.b); - - color.setHSL(i / hilbertPoints.length, 1.0, 0.5, THREE.SRGBColorSpace); - colors3.push(color.r, color.g, color.b); - } - - geometry4.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); - geometry5.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); - geometry6.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); - - geometry4.setAttribute('color', new THREE.Float32BufferAttribute(colors1, 3)); - geometry5.setAttribute('color', new THREE.Float32BufferAttribute(colors2, 3)); - geometry6.setAttribute('color', new THREE.Float32BufferAttribute(colors3, 3)); - - // Create lines and add to scene - - const material = new THREE.LineBasicMaterial({ color: 0xffffff, vertexColors: true }); - - let line, p; - const scale = 0.3, - d = 225; - - const parameters = [ - [material, scale * 1.5, [-d, -d / 2, 0], geometry1], - [material, scale * 1.5, [0, -d / 2, 0], geometry2], - [material, scale * 1.5, [d, -d / 2, 0], geometry3], - - [material, scale * 1.5, [-d, d / 2, 0], geometry4], - [material, scale * 1.5, [0, d / 2, 0], geometry5], - [material, scale * 1.5, [d, d / 2, 0], geometry6], - ]; - - for (let i = 0; i < parameters.length; i++) { - p = parameters[i]; - line = new THREE.Line(p[3], p[0]); - line.scale.x = line.scale.y = line.scale.z = p[1]; - line.position.x = p[2][0]; - line.position.y = p[2][1]; - line.position.z = p[2][2]; - scene.add(line); - } - - // - - document.body.style.touchAction = 'none'; - document.body.addEventListener('pointermove', onPointerMove); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function onPointerMove(event) { - if (event.isPrimary === false) return; - - mouseX = event.clientX - windowHalfX; - mouseY = event.clientY - windowHalfY; -} - -// - -function animate() { - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-mouseY + 200 - camera.position.y) * 0.05; - - camera.lookAt(scene.position); - - const time = Date.now() * 0.0005; - - for (let i = 0; i < scene.children.length; i++) { - const object = scene.children[i]; - - if (object.isLine) { - object.rotation.y = time * (i % 2 ? 1 : -1); - } - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_lines_dashed.ts b/examples-testing/examples/webgl_lines_dashed.ts deleted file mode 100644 index 4849e7c3a..000000000 --- a/examples-testing/examples/webgl_lines_dashed.ts +++ /dev/null @@ -1,186 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js'; - -let renderer, scene, camera, stats; -const objects = []; - -const WIDTH = window.innerWidth, - HEIGHT = window.innerHeight; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(60, WIDTH / HEIGHT, 1, 200); - camera.position.z = 150; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x111111); - scene.fog = new THREE.Fog(0x111111, 150, 200); - - const subdivisions = 6; - const recursion = 1; - - const points = GeometryUtils.hilbert3D(new THREE.Vector3(0, 0, 0), 25.0, recursion, 0, 1, 2, 3, 4, 5, 6, 7); - const spline = new THREE.CatmullRomCurve3(points); - - const samples = spline.getPoints(points.length * subdivisions); - const geometrySpline = new THREE.BufferGeometry().setFromPoints(samples); - - const line = new THREE.Line( - geometrySpline, - new THREE.LineDashedMaterial({ color: 0xffffff, dashSize: 1, gapSize: 0.5 }), - ); - line.computeLineDistances(); - - objects.push(line); - scene.add(line); - - const geometryBox = box(50, 50, 50); - - const lineSegments = new THREE.LineSegments( - geometryBox, - new THREE.LineDashedMaterial({ color: 0xffaa00, dashSize: 3, gapSize: 1 }), - ); - lineSegments.computeLineDistances(); - - objects.push(lineSegments); - scene.add(lineSegments); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(WIDTH, HEIGHT); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function box(width, height, depth) { - (width = width * 0.5), (height = height * 0.5), (depth = depth * 0.5); - - const geometry = new THREE.BufferGeometry(); - const position = []; - - position.push( - -width, - -height, - -depth, - -width, - height, - -depth, - - -width, - height, - -depth, - width, - height, - -depth, - - width, - height, - -depth, - width, - -height, - -depth, - - width, - -height, - -depth, - -width, - -height, - -depth, - - -width, - -height, - depth, - -width, - height, - depth, - - -width, - height, - depth, - width, - height, - depth, - - width, - height, - depth, - width, - -height, - depth, - - width, - -height, - depth, - -width, - -height, - depth, - - -width, - -height, - -depth, - -width, - -height, - depth, - - -width, - height, - -depth, - -width, - height, - depth, - - width, - height, - -depth, - width, - height, - depth, - - width, - -height, - -depth, - width, - -height, - depth, - ); - - geometry.setAttribute('position', new THREE.Float32BufferAttribute(position, 3)); - - return geometry; -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - const time = Date.now() * 0.001; - - scene.traverse(function (object) { - if (object.isLine) { - object.rotation.x = 0.25 * time; - object.rotation.y = 0.25 * time; - } - }); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_lines_fat.ts b/examples-testing/examples/webgl_lines_fat.ts deleted file mode 100644 index 34c2e2d5e..000000000 --- a/examples-testing/examples/webgl_lines_fat.ts +++ /dev/null @@ -1,251 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GPUStatsPanel } from 'three/addons/utils/GPUStatsPanel.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { Line2 } from 'three/addons/lines/Line2.js'; -import { LineMaterial } from 'three/addons/lines/LineMaterial.js'; -import { LineGeometry } from 'three/addons/lines/LineGeometry.js'; -import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js'; - -let line, renderer, scene, camera, camera2, controls; -let line1; -let matLine, matLineBasic, matLineDashed; -let stats, gpuPanel; -let gui; - -// viewport -let insetWidth; -let insetHeight; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setClearColor(0x000000, 0.0); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(-40, 0, 60); - - camera2 = new THREE.PerspectiveCamera(40, 1, 1, 1000); - camera2.position.copy(camera.position); - - controls = new OrbitControls(camera, renderer.domElement); - controls.enableDamping = true; - controls.minDistance = 10; - controls.maxDistance = 500; - - // Position and THREE.Color Data - - const positions = []; - const colors = []; - - const points = GeometryUtils.hilbert3D(new THREE.Vector3(0, 0, 0), 20.0, 1, 0, 1, 2, 3, 4, 5, 6, 7); - - const spline = new THREE.CatmullRomCurve3(points); - const divisions = Math.round(12 * points.length); - const point = new THREE.Vector3(); - const color = new THREE.Color(); - - for (let i = 0, l = divisions; i < l; i++) { - const t = i / l; - - spline.getPoint(t, point); - positions.push(point.x, point.y, point.z); - - color.setHSL(t, 1.0, 0.5, THREE.SRGBColorSpace); - colors.push(color.r, color.g, color.b); - } - - // Line2 ( LineGeometry, LineMaterial ) - - const geometry = new LineGeometry(); - geometry.setPositions(positions); - geometry.setColors(colors); - - matLine = new LineMaterial({ - color: 0xffffff, - linewidth: 5, // in world units with size attenuation, pixels otherwise - vertexColors: true, - - dashed: false, - alphaToCoverage: true, - }); - - line = new Line2(geometry, matLine); - line.computeLineDistances(); - line.scale.set(1, 1, 1); - scene.add(line); - - // THREE.Line ( THREE.BufferGeometry, THREE.LineBasicMaterial ) - rendered with gl.LINE_STRIP - - const geo = new THREE.BufferGeometry(); - geo.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); - geo.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); - - matLineBasic = new THREE.LineBasicMaterial({ vertexColors: true }); - matLineDashed = new THREE.LineDashedMaterial({ vertexColors: true, scale: 2, dashSize: 1, gapSize: 1 }); - - line1 = new THREE.Line(geo, matLineBasic); - line1.computeLineDistances(); - line1.visible = false; - scene.add(line1); - - // - - window.addEventListener('resize', onWindowResize); - onWindowResize(); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - gpuPanel = new GPUStatsPanel(renderer.getContext()); - stats.addPanel(gpuPanel); - stats.showPanel(0); - - initGui(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - insetWidth = window.innerHeight / 4; // square - insetHeight = window.innerHeight / 4; - - camera2.aspect = insetWidth / insetHeight; - camera2.updateProjectionMatrix(); -} - -function animate() { - // main scene - - renderer.setClearColor(0x000000, 0); - - renderer.setViewport(0, 0, window.innerWidth, window.innerHeight); - - controls.update(); - - gpuPanel.startQuery(); - renderer.render(scene, camera); - gpuPanel.endQuery(); - - // inset scene - - renderer.setClearColor(0x222222, 1); - - renderer.clearDepth(); // important! - - renderer.setScissorTest(true); - - renderer.setScissor(20, 20, insetWidth, insetHeight); - - renderer.setViewport(20, 20, insetWidth, insetHeight); - - camera2.position.copy(camera.position); - camera2.quaternion.copy(camera.quaternion); - - renderer.render(scene, camera2); - - renderer.setScissorTest(false); - - stats.update(); -} - -// - -function initGui() { - gui = new GUI(); - - const param = { - 'line type': 0, - 'world units': false, - width: 5, - alphaToCoverage: true, - dashed: false, - 'dash scale': 1, - 'dash / gap': 1, - }; - - gui.add(param, 'line type', { LineGeometry: 0, 'gl.LINE': 1 }).onChange(function (val) { - switch (val) { - case 0: - line.visible = true; - - line1.visible = false; - - break; - - case 1: - line.visible = false; - - line1.visible = true; - - break; - } - }); - - gui.add(param, 'world units').onChange(function (val) { - matLine.worldUnits = val; - matLine.needsUpdate = true; - }); - - gui.add(param, 'width', 1, 10).onChange(function (val) { - matLine.linewidth = val; - }); - - gui.add(param, 'alphaToCoverage').onChange(function (val) { - matLine.alphaToCoverage = val; - }); - - gui.add(param, 'dashed').onChange(function (val) { - matLine.dashed = val; - line1.material = val ? matLineDashed : matLineBasic; - }); - - gui.add(param, 'dash scale', 0.5, 2, 0.1).onChange(function (val) { - matLine.dashScale = val; - matLineDashed.scale = val; - }); - - gui.add(param, 'dash / gap', { '2 : 1': 0, '1 : 1': 1, '1 : 2': 2 }).onChange(function (val) { - switch (val) { - case 0: - matLine.dashSize = 2; - matLine.gapSize = 1; - - matLineDashed.dashSize = 2; - matLineDashed.gapSize = 1; - - break; - - case 1: - matLine.dashSize = 1; - matLine.gapSize = 1; - - matLineDashed.dashSize = 1; - matLineDashed.gapSize = 1; - - break; - - case 2: - matLine.dashSize = 1; - matLine.gapSize = 2; - - matLineDashed.dashSize = 1; - matLineDashed.gapSize = 2; - - break; - } - }); -} diff --git a/examples-testing/examples/webgl_lines_fat_raycasting.ts b/examples-testing/examples/webgl_lines_fat_raycasting.ts deleted file mode 100644 index 75cef6204..000000000 --- a/examples-testing/examples/webgl_lines_fat_raycasting.ts +++ /dev/null @@ -1,294 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GPUStatsPanel } from 'three/addons/utils/GPUStatsPanel.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { LineMaterial } from 'three/addons/lines/LineMaterial.js'; -import { LineSegments2 } from 'three/addons/lines/LineSegments2.js'; -import { LineSegmentsGeometry } from 'three/addons/lines/LineSegmentsGeometry.js'; -import { Line2 } from 'three/addons/lines/Line2.js'; -import { LineGeometry } from 'three/addons/lines/LineGeometry.js'; - -let line, thresholdLine, segments, thresholdSegments; -let renderer, scene, camera, controls; -let sphereInter, sphereOnLine; -let stats, gpuPanel; -let gui; -let clock; - -const color = new THREE.Color(); - -const pointer = new THREE.Vector2(Infinity, Infinity); - -const raycaster = new THREE.Raycaster(); - -raycaster.params.Line2 = {}; -raycaster.params.Line2.threshold = 0; - -const matLine = new LineMaterial({ - color: 0xffffff, - linewidth: 1, // in world units with size attenuation, pixels otherwise - worldUnits: true, - vertexColors: true, - - alphaToCoverage: true, -}); - -const matThresholdLine = new LineMaterial({ - color: 0xffffff, - linewidth: matLine.linewidth, // in world units with size attenuation, pixels otherwise - worldUnits: true, - // vertexColors: true, - transparent: true, - opacity: 0.2, - depthTest: false, - visible: false, -}); - -const params = { - 'line type': 0, - 'world units': matLine.worldUnits, - 'visualize threshold': matThresholdLine.visible, - width: matLine.linewidth, - alphaToCoverage: matLine.alphaToCoverage, - threshold: raycaster.params.Line2.threshold, - translation: raycaster.params.Line2.threshold, - animate: true, -}; - -init(); - -function init() { - clock = new THREE.Clock(); - - renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setClearColor(0x000000, 0.0); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(-40, 0, 60); - - controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 10; - controls.maxDistance = 500; - - const sphereGeometry = new THREE.SphereGeometry(0.25, 8, 4); - const sphereInterMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000, depthTest: false }); - const sphereOnLineMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00, depthTest: false }); - - sphereInter = new THREE.Mesh(sphereGeometry, sphereInterMaterial); - sphereOnLine = new THREE.Mesh(sphereGeometry, sphereOnLineMaterial); - sphereInter.visible = false; - sphereOnLine.visible = false; - sphereInter.renderOrder = 10; - sphereOnLine.renderOrder = 10; - scene.add(sphereInter); - scene.add(sphereOnLine); - - // Position and THREE.Color Data - - const positions = []; - const colors = []; - const points = []; - for (let i = -50; i < 50; i++) { - const t = i / 3; - points.push(new THREE.Vector3(t * Math.sin(2 * t), t, t * Math.cos(2 * t))); - } - - const spline = new THREE.CatmullRomCurve3(points); - const divisions = Math.round(3 * points.length); - const point = new THREE.Vector3(); - const color = new THREE.Color(); - - for (let i = 0, l = divisions; i < l; i++) { - const t = i / l; - - spline.getPoint(t, point); - positions.push(point.x, point.y, point.z); - - color.setHSL(t, 1.0, 0.5, THREE.SRGBColorSpace); - colors.push(color.r, color.g, color.b); - } - - const lineGeometry = new LineGeometry(); - lineGeometry.setPositions(positions); - lineGeometry.setColors(colors); - - const segmentsGeometry = new LineSegmentsGeometry(); - segmentsGeometry.setPositions(positions); - segmentsGeometry.setColors(colors); - - segments = new LineSegments2(segmentsGeometry, matLine); - segments.computeLineDistances(); - segments.scale.set(1, 1, 1); - scene.add(segments); - segments.visible = false; - - thresholdSegments = new LineSegments2(segmentsGeometry, matThresholdLine); - thresholdSegments.computeLineDistances(); - thresholdSegments.scale.set(1, 1, 1); - scene.add(thresholdSegments); - thresholdSegments.visible = false; - - line = new Line2(lineGeometry, matLine); - line.computeLineDistances(); - line.scale.set(1, 1, 1); - scene.add(line); - - thresholdLine = new Line2(lineGeometry, matThresholdLine); - thresholdLine.computeLineDistances(); - thresholdLine.scale.set(1, 1, 1); - scene.add(thresholdLine); - - const geo = new THREE.BufferGeometry(); - geo.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); - geo.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); - - // - - document.addEventListener('pointermove', onPointerMove); - window.addEventListener('resize', onWindowResize); - onWindowResize(); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - gpuPanel = new GPUStatsPanel(renderer.getContext()); - stats.addPanel(gpuPanel); - stats.showPanel(0); - initGui(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onPointerMove(event) { - pointer.x = (event.clientX / window.innerWidth) * 2 - 1; - pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; -} - -function animate() { - const delta = clock.getDelta(); - - const obj = line.visible ? line : segments; - thresholdLine.position.copy(line.position); - thresholdLine.quaternion.copy(line.quaternion); - thresholdSegments.position.copy(segments.position); - thresholdSegments.quaternion.copy(segments.quaternion); - - if (params.animate) { - line.rotation.y += delta * 0.1; - - segments.rotation.y = line.rotation.y; - } - - raycaster.setFromCamera(pointer, camera); - - const intersects = raycaster.intersectObject(obj); - - if (intersects.length > 0) { - sphereInter.visible = true; - sphereOnLine.visible = true; - - sphereInter.position.copy(intersects[0].point); - sphereOnLine.position.copy(intersects[0].pointOnLine); - - const index = intersects[0].faceIndex; - const colors = obj.geometry.getAttribute('instanceColorStart'); - - color.fromBufferAttribute(colors, index); - - sphereInter.material.color.copy(color).offsetHSL(0.3, 0, 0); - sphereOnLine.material.color.copy(color).offsetHSL(0.7, 0, 0); - - renderer.domElement.style.cursor = 'crosshair'; - } else { - sphereInter.visible = false; - sphereOnLine.visible = false; - renderer.domElement.style.cursor = ''; - } - - gpuPanel.startQuery(); - renderer.render(scene, camera); - gpuPanel.endQuery(); - - stats.update(); -} - -// - -function switchLine(val) { - switch (val) { - case 0: - line.visible = true; - thresholdLine.visible = true; - - segments.visible = false; - thresholdSegments.visible = false; - - break; - - case 1: - line.visible = false; - thresholdLine.visible = false; - - segments.visible = true; - thresholdSegments.visible = true; - - break; - } -} - -function initGui() { - gui = new GUI(); - - gui.add(params, 'line type', { LineGeometry: 0, LineSegmentsGeometry: 1 }) - .onChange(function (val) { - switchLine(val); - }) - .setValue(1); - - gui.add(params, 'world units').onChange(function (val) { - matLine.worldUnits = val; - matLine.needsUpdate = true; - - matThresholdLine.worldUnits = val; - matThresholdLine.needsUpdate = true; - }); - - gui.add(params, 'visualize threshold').onChange(function (val) { - matThresholdLine.visible = val; - }); - - gui.add(params, 'width', 1, 10).onChange(function (val) { - matLine.linewidth = val; - matThresholdLine.linewidth = matLine.linewidth + raycaster.params.Line2.threshold; - }); - - gui.add(params, 'alphaToCoverage').onChange(function (val) { - matLine.alphaToCoverage = val; - }); - - gui.add(params, 'threshold', 0, 10).onChange(function (val) { - raycaster.params.Line2.threshold = val; - matThresholdLine.linewidth = matLine.linewidth + raycaster.params.Line2.threshold; - }); - - gui.add(params, 'translation', 0, 10).onChange(function (val) { - line.position.x = val; - segments.position.x = val; - }); - - gui.add(params, 'animate'); -} diff --git a/examples-testing/examples/webgl_lines_fat_wireframe.ts b/examples-testing/examples/webgl_lines_fat_wireframe.ts deleted file mode 100644 index 59660ad7e..000000000 --- a/examples-testing/examples/webgl_lines_fat_wireframe.ts +++ /dev/null @@ -1,210 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { LineMaterial } from 'three/addons/lines/LineMaterial.js'; -import { Wireframe } from 'three/addons/lines/Wireframe.js'; -import { WireframeGeometry2 } from 'three/addons/lines/WireframeGeometry2.js'; - -let wireframe, renderer, scene, camera, camera2, controls; -let wireframe1; -let matLine, matLineBasic, matLineDashed; -let stats; -let gui; - -// viewport -let insetWidth; -let insetHeight; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setClearColor(0x000000, 0.0); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(-50, 0, 50); - - camera2 = new THREE.PerspectiveCamera(40, 1, 1, 1000); - camera2.position.copy(camera.position); - - controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 10; - controls.maxDistance = 500; - - // Wireframe ( WireframeGeometry2, LineMaterial ) - - let geo = new THREE.IcosahedronGeometry(20, 1); - - const geometry = new WireframeGeometry2(geo); - - matLine = new LineMaterial({ - color: 0x4080ff, - linewidth: 5, // in pixels - dashed: false, - }); - - wireframe = new Wireframe(geometry, matLine); - wireframe.computeLineDistances(); - wireframe.scale.set(1, 1, 1); - scene.add(wireframe); - - // Line ( THREE.WireframeGeometry, THREE.LineBasicMaterial ) - rendered with gl.LINE - - geo = new THREE.WireframeGeometry(geo); - - matLineBasic = new THREE.LineBasicMaterial({ color: 0x4080ff }); - matLineDashed = new THREE.LineDashedMaterial({ scale: 2, dashSize: 1, gapSize: 1 }); - - wireframe1 = new THREE.LineSegments(geo, matLineBasic); - wireframe1.computeLineDistances(); - wireframe1.visible = false; - scene.add(wireframe1); - - // - - window.addEventListener('resize', onWindowResize); - onWindowResize(); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - initGui(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - insetWidth = window.innerHeight / 4; // square - insetHeight = window.innerHeight / 4; - - camera2.aspect = insetWidth / insetHeight; - camera2.updateProjectionMatrix(); -} - -function animate() { - // main scene - - renderer.setClearColor(0x000000, 0); - - renderer.setViewport(0, 0, window.innerWidth, window.innerHeight); - - renderer.render(scene, camera); - - // inset scene - - renderer.setClearColor(0x222222, 1); - - renderer.clearDepth(); // important! - - renderer.setScissorTest(true); - - renderer.setScissor(20, 20, insetWidth, insetHeight); - - renderer.setViewport(20, 20, insetWidth, insetHeight); - - camera2.position.copy(camera.position); - camera2.quaternion.copy(camera.quaternion); - - renderer.render(scene, camera2); - - renderer.setScissorTest(false); - - stats.update(); -} - -// - -function initGui() { - gui = new GUI(); - - const param = { - 'line type': 0, - 'width (px)': 5, - dashed: false, - 'dash scale': 1, - 'dash / gap': 1, - }; - - gui.add(param, 'line type', { LineGeometry: 0, 'gl.LINE': 1 }).onChange(function (val) { - switch (val) { - case 0: - wireframe.visible = true; - - wireframe1.visible = false; - - break; - - case 1: - wireframe.visible = false; - - wireframe1.visible = true; - - break; - } - }); - - gui.add(param, 'width (px)', 1, 10).onChange(function (val) { - matLine.linewidth = val; - }); - - gui.add(param, 'dashed').onChange(function (val) { - matLine.dashed = val; - - // dashed is implemented as a defines -- not as a uniform. this could be changed. - // ... or THREE.LineDashedMaterial could be implemented as a separate material - // temporary hack - renderer should do this eventually - if (val) matLine.defines.USE_DASH = ''; - else delete matLine.defines.USE_DASH; - matLine.needsUpdate = true; - - wireframe1.material = val ? matLineDashed : matLineBasic; - }); - - gui.add(param, 'dash scale', 0.5, 1, 0.1).onChange(function (val) { - matLine.dashScale = val; - matLineDashed.scale = val; - }); - - gui.add(param, 'dash / gap', { '2 : 1': 0, '1 : 1': 1, '1 : 2': 2 }).onChange(function (val) { - switch (val) { - case 0: - matLine.dashSize = 2; - matLine.gapSize = 1; - - matLineDashed.dashSize = 2; - matLineDashed.gapSize = 1; - - break; - - case 1: - matLine.dashSize = 1; - matLine.gapSize = 1; - - matLineDashed.dashSize = 1; - matLineDashed.gapSize = 1; - - break; - - case 2: - matLine.dashSize = 1; - matLine.gapSize = 2; - - matLineDashed.dashSize = 1; - matLineDashed.gapSize = 2; - - break; - } - }); -} diff --git a/examples-testing/examples/webgl_loader_3dm.ts b/examples-testing/examples/webgl_loader_3dm.ts deleted file mode 100644 index 7570306fd..000000000 --- a/examples-testing/examples/webgl_loader_3dm.ts +++ /dev/null @@ -1,95 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { Rhino3dmLoader } from 'three/addons/loaders/3DMLoader.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer; -let controls, gui; - -init(); - -function init() { - THREE.Object3D.DEFAULT_UP.set(0, 0, 1); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(26, -40, 5); - - scene = new THREE.Scene(); - - const directionalLight = new THREE.DirectionalLight(0xffffff, 6); - directionalLight.position.set(0, 0, 2); - scene.add(directionalLight); - - const loader = new Rhino3dmLoader(); - //generally, use this for the Library Path: https://cdn.jsdelivr.net/npm/rhino3dm@8.0.1 - loader.setLibraryPath('jsm/libs/rhino3dm/'); - loader.load( - 'models/3dm/Rhino_Logo.3dm', - function (object) { - scene.add(object); - initGUI(object.userData.layers); - - // hide spinner - document.getElementById('loader').style.display = 'none'; - }, - function (progress) { - console.log((progress.loaded / progress.total) * 100 + '%'); - }, - function (error) { - console.log(error); - }, - ); - - controls = new OrbitControls(camera, renderer.domElement); - - window.addEventListener('resize', resize); -} - -function resize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); -} - -function animate() { - controls.update(); - renderer.render(scene, camera); -} - -function initGUI(layers) { - gui = new GUI({ title: 'layers' }); - - for (let i = 0; i < layers.length; i++) { - const layer = layers[i]; - gui.add(layer, 'visible') - .name(layer.name) - .onChange(function (val) { - const name = this.object.name; - - scene.traverse(function (child) { - if (child.userData.hasOwnProperty('attributes')) { - if ('layerIndex' in child.userData.attributes) { - const layerName = layers[child.userData.attributes.layerIndex].name; - - if (layerName === name) { - child.visible = val; - layer.visible = val; - } - } - } - }); - }); - } -} diff --git a/examples-testing/examples/webgl_loader_3ds.ts b/examples-testing/examples/webgl_loader_3ds.ts deleted file mode 100644 index 10ce34076..000000000 --- a/examples-testing/examples/webgl_loader_3ds.ts +++ /dev/null @@ -1,62 +0,0 @@ -import * as THREE from 'three'; - -import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; -import { TDSLoader } from 'three/addons/loaders/TDSLoader.js'; - -let container, controls; -let camera, scene, renderer; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 10); - camera.position.z = 2; - - scene = new THREE.Scene(); - scene.add(new THREE.AmbientLight(0xffffff, 3)); - - const directionalLight = new THREE.DirectionalLight(0xffeedd, 3); - directionalLight.position.set(0, 0, 2); - scene.add(directionalLight); - - //3ds files dont store normal maps - const normal = new THREE.TextureLoader().load('models/3ds/portalgun/textures/normal.jpg'); - - const loader = new TDSLoader(); - loader.setResourcePath('models/3ds/portalgun/textures/'); - loader.load('models/3ds/portalgun/portalgun.3ds', function (object) { - object.traverse(function (child) { - if (child.isMesh) { - child.material.specular.setScalar(0.1); - child.material.normalMap = normal; - } - }); - - scene.add(object); - }); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - controls = new TrackballControls(camera, renderer.domElement); - - window.addEventListener('resize', resize); -} - -function resize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - controls.update(); - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_3mf.ts b/examples-testing/examples/webgl_loader_3mf.ts deleted file mode 100644 index c31e32196..000000000 --- a/examples-testing/examples/webgl_loader_3mf.ts +++ /dev/null @@ -1,105 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { ThreeMFLoader } from 'three/addons/loaders/3MFLoader.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, object, loader, controls; - -const params = { - asset: 'cube_gears', -}; - -const assets = ['cube_gears', 'facecolors', 'multipletextures', 'vertexcolors']; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x333333); - - camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 500); - - // Z is up for objects intended to be 3D printed. - - camera.up.set(0, 0, 1); - camera.position.set(-100, -250, 100); - scene.add(camera); - - controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); - controls.minDistance = 50; - controls.maxDistance = 400; - controls.enablePan = false; - controls.update(); - - scene.add(new THREE.AmbientLight(0xffffff, 0.6)); - - const light = new THREE.DirectionalLight(0xffffff, 2); - light.position.set(-1, -2.5, 1); - scene.add(light); - - const manager = new THREE.LoadingManager(); - - manager.onLoad = function () { - const aabb = new THREE.Box3().setFromObject(object); - const center = aabb.getCenter(new THREE.Vector3()); - - object.position.x += object.position.x - center.x; - object.position.y += object.position.y - center.y; - object.position.z += object.position.z - center.z; - - controls.reset(); - - scene.add(object); - render(); - }; - - loader = new ThreeMFLoader(manager); - loadAsset(params.asset); - - window.addEventListener('resize', onWindowResize); - - // - - const gui = new GUI(); - gui.add(params, 'asset', assets).onChange(function (value) { - loadAsset(value); - }); -} - -function loadAsset(asset) { - loader.load('models/3mf/' + asset + '.3mf', function (group) { - if (object) { - object.traverse(function (child) { - if (child.material) child.material.dispose(); - if (child.material && child.material.map) child.material.map.dispose(); - if (child.geometry) child.geometry.dispose(); - }); - - scene.remove(object); - } - - // - - object = group; - }); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_3mf_materials.ts b/examples-testing/examples/webgl_loader_3mf_materials.ts deleted file mode 100644 index fcdd7308e..000000000 --- a/examples-testing/examples/webgl_loader_3mf_materials.ts +++ /dev/null @@ -1,106 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { ThreeMFLoader } from 'three/addons/loaders/3MFLoader.js'; - -let camera, scene, renderer; - -init(); - -function init() { - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xa0a0a0); - scene.fog = new THREE.Fog(0xa0a0a0, 10, 500); - - camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 500); - camera.position.set(-50, 40, 50); - scene.add(camera); - - // - - const hemiLight = new THREE.HemisphereLight(0xffffff, 0x8d8d8d, 3); - hemiLight.position.set(0, 100, 0); - scene.add(hemiLight); - - const dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.position.set(-0, 40, 50); - dirLight.castShadow = true; - dirLight.shadow.camera.top = 50; - dirLight.shadow.camera.bottom = -25; - dirLight.shadow.camera.left = -25; - dirLight.shadow.camera.right = 25; - dirLight.shadow.camera.near = 0.1; - dirLight.shadow.camera.far = 200; - dirLight.shadow.mapSize.set(1024, 1024); - scene.add(dirLight); - - // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) ); - - // - - const manager = new THREE.LoadingManager(); - - const loader = new ThreeMFLoader(manager); - loader.load('./models/3mf/truck.3mf', function (object) { - object.rotation.set(-Math.PI / 2, 0, 0); // z-up conversion - - object.traverse(function (child) { - child.castShadow = true; - }); - - scene.add(object); - }); - - // - - manager.onLoad = function () { - render(); - }; - - // - - const ground = new THREE.Mesh( - new THREE.PlaneGeometry(1000, 1000), - new THREE.MeshPhongMaterial({ color: 0xcbcbcb, depthWrite: false }), - ); - ground.rotation.x = -Math.PI / 2; - ground.position.y = 11; - ground.receiveShadow = true; - scene.add(ground); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.shadowMap.enabled = true; - renderer.shadowMap.type = THREE.PCFSoftShadowMap; - document.body.appendChild(renderer.domElement); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); - controls.minDistance = 50; - controls.maxDistance = 200; - controls.enablePan = false; - controls.target.set(0, 20, 0); - controls.update(); - - window.addEventListener('resize', onWindowResize); - - render(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_amf.ts b/examples-testing/examples/webgl_loader_amf.ts deleted file mode 100644 index ee576e04f..000000000 --- a/examples-testing/examples/webgl_loader_amf.ts +++ /dev/null @@ -1,62 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { AMFLoader } from 'three/addons/loaders/AMFLoader.js'; - -let camera, scene, renderer; - -init(); - -function init() { - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x999999); - - scene.add(new THREE.AmbientLight(0x999999)); - - camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 500); - - // Z is up for objects intended to be 3D printed. - - camera.up.set(0, 0, 1); - camera.position.set(0, -9, 6); - - camera.add(new THREE.PointLight(0xffffff, 250)); - - scene.add(camera); - - const grid = new THREE.GridHelper(50, 50, 0xffffff, 0x555555); - grid.rotateOnAxis(new THREE.Vector3(1, 0, 0), 90 * (Math.PI / 180)); - scene.add(grid); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - const loader = new AMFLoader(); - loader.load('./models/amf/rook.amf', function (amfobject) { - scene.add(amfobject); - render(); - }); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); - controls.target.set(0, 0, 2); - controls.enableZoom = false; - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_bvh.ts b/examples-testing/examples/webgl_loader_bvh.ts deleted file mode 100644 index 0be3add4d..000000000 --- a/examples-testing/examples/webgl_loader_bvh.ts +++ /dev/null @@ -1,61 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { BVHLoader } from 'three/addons/loaders/BVHLoader.js'; - -const clock = new THREE.Clock(); - -let camera, controls, scene, renderer; -let mixer; - -init(); - -const loader = new BVHLoader(); -loader.load('models/bvh/pirouette.bvh', function (result) { - const skeletonHelper = new THREE.SkeletonHelper(result.skeleton.bones[0]); - - scene.add(result.skeleton.bones[0]); - scene.add(skeletonHelper); - - // play animation - mixer = new THREE.AnimationMixer(result.skeleton.bones[0]); - mixer.clipAction(result.clip).play(); -}); - -function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 200, 300); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xeeeeee); - - scene.add(new THREE.GridHelper(400, 10)); - - // renderer - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 300; - controls.maxDistance = 700; - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const delta = clock.getDelta(); - - if (mixer) mixer.update(delta); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_collada.ts b/examples-testing/examples/webgl_loader_collada.ts deleted file mode 100644 index 62588b698..000000000 --- a/examples-testing/examples/webgl_loader_collada.ts +++ /dev/null @@ -1,83 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { ColladaLoader } from 'three/addons/loaders/ColladaLoader.js'; - -let container, stats, clock; -let camera, scene, renderer, elf; - -init(); - -function init() { - container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 2000); - camera.position.set(8, 10, 8); - camera.lookAt(0, 3, 0); - - scene = new THREE.Scene(); - - clock = new THREE.Clock(); - - // loading manager - - const loadingManager = new THREE.LoadingManager(function () { - scene.add(elf); - }); - - // collada - - const loader = new ColladaLoader(loadingManager); - loader.load('./models/collada/elf/elf.dae', function (collada) { - elf = collada.scene; - }); - - // - - const ambientLight = new THREE.AmbientLight(0xffffff); - scene.add(ambientLight); - - const directionalLight = new THREE.DirectionalLight(0xffffff, 2.5); - directionalLight.position.set(1, 1, 0).normalize(); - scene.add(directionalLight); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - const delta = clock.getDelta(); - - if (elf !== undefined) { - elf.rotation.z += delta * 0.5; - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_collada_skinning.ts b/examples-testing/examples/webgl_loader_collada_skinning.ts deleted file mode 100644 index 5cb808b17..000000000 --- a/examples-testing/examples/webgl_loader_collada_skinning.ts +++ /dev/null @@ -1,97 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { ColladaLoader } from 'three/addons/loaders/ColladaLoader.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let container, stats, clock, controls; -let camera, scene, renderer, mixer; - -init(); - -function init() { - container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(25, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(15, 10, -15); - - scene = new THREE.Scene(); - - clock = new THREE.Clock(); - - // collada - - const loader = new ColladaLoader(); - loader.load('./models/collada/stormtrooper/stormtrooper.dae', function (collada) { - const avatar = collada.scene; - const animations = avatar.animations; - - mixer = new THREE.AnimationMixer(avatar); - mixer.clipAction(animations[0]).play(); - - scene.add(avatar); - }); - - // - - const gridHelper = new THREE.GridHelper(10, 20, 0xc1c1c1, 0x8d8d8d); - scene.add(gridHelper); - - // - - const ambientLight = new THREE.AmbientLight(0xffffff, 0.6); - scene.add(ambientLight); - - const directionalLight = new THREE.DirectionalLight(0xffffff, 3); - directionalLight.position.set(1.5, 1, -1.5); - scene.add(directionalLight); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - controls = new OrbitControls(camera, renderer.domElement); - controls.screenSpacePanning = true; - controls.minDistance = 5; - controls.maxDistance = 40; - controls.target.set(0, 2, 0); - controls.update(); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - const delta = clock.getDelta(); - - if (mixer !== undefined) { - mixer.update(delta); - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_draco.ts b/examples-testing/examples/webgl_loader_draco.ts deleted file mode 100644 index c9947c693..000000000 --- a/examples-testing/examples/webgl_loader_draco.ts +++ /dev/null @@ -1,85 +0,0 @@ -import * as THREE from 'three'; - -import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; - -let camera, scene, renderer; - -const container = document.querySelector('#container'); - -// Configure and create Draco decoder. -const dracoLoader = new DRACOLoader(); -dracoLoader.setDecoderPath('jsm/libs/draco/'); -dracoLoader.setDecoderConfig({ type: 'js' }); - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 15); - camera.position.set(3, 0.25, 3); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x443333); - scene.fog = new THREE.Fog(0x443333, 1, 4); - - // Ground - const plane = new THREE.Mesh( - new THREE.PlaneGeometry(8, 8), - new THREE.MeshPhongMaterial({ color: 0xcbcbcb, specular: 0x101010 }), - ); - plane.rotation.x = -Math.PI / 2; - plane.position.y = 0.03; - plane.receiveShadow = true; - scene.add(plane); - - // Lights - const hemiLight = new THREE.HemisphereLight(0x8d7c7c, 0x494966, 3); - scene.add(hemiLight); - - const spotLight = new THREE.SpotLight(); - spotLight.intensity = 7; - spotLight.angle = Math.PI / 16; - spotLight.penumbra = 0.5; - spotLight.castShadow = true; - spotLight.position.set(-1, 1, 1); - scene.add(spotLight); - - dracoLoader.load('models/draco/bunny.drc', function (geometry) { - geometry.computeVertexNormals(); - - const material = new THREE.MeshStandardMaterial({ color: 0xa5a5a5 }); - const mesh = new THREE.Mesh(geometry, material); - mesh.castShadow = true; - mesh.receiveShadow = true; - scene.add(mesh); - - // Release decoder resources. - dracoLoader.dispose(); - }); - - // renderer - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - container.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const timer = Date.now() * 0.0003; - - camera.position.x = Math.sin(timer) * 0.5; - camera.position.z = Math.cos(timer) * 0.5; - camera.lookAt(0, 0.1, 0); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_fbx.ts b/examples-testing/examples/webgl_loader_fbx.ts deleted file mode 100644 index 3b157a222..000000000 --- a/examples-testing/examples/webgl_loader_fbx.ts +++ /dev/null @@ -1,162 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { FBXLoader } from 'three/addons/loaders/FBXLoader.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -const manager = new THREE.LoadingManager(); - -let camera, scene, renderer, stats, object, loader, guiMorphsFolder; -let mixer; - -const clock = new THREE.Clock(); - -const params = { - asset: 'Samba Dancing', -}; - -const assets = ['Samba Dancing', 'morph_test']; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000); - camera.position.set(100, 200, 300); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xa0a0a0); - scene.fog = new THREE.Fog(0xa0a0a0, 200, 1000); - - const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 5); - hemiLight.position.set(0, 200, 0); - scene.add(hemiLight); - - const dirLight = new THREE.DirectionalLight(0xffffff, 5); - dirLight.position.set(0, 200, 100); - dirLight.castShadow = true; - dirLight.shadow.camera.top = 180; - dirLight.shadow.camera.bottom = -100; - dirLight.shadow.camera.left = -120; - dirLight.shadow.camera.right = 120; - scene.add(dirLight); - - // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) ); - - // ground - const mesh = new THREE.Mesh( - new THREE.PlaneGeometry(2000, 2000), - new THREE.MeshPhongMaterial({ color: 0x999999, depthWrite: false }), - ); - mesh.rotation.x = -Math.PI / 2; - mesh.receiveShadow = true; - scene.add(mesh); - - const grid = new THREE.GridHelper(2000, 20, 0x000000, 0x000000); - grid.material.opacity = 0.2; - grid.material.transparent = true; - scene.add(grid); - - loader = new FBXLoader(manager); - loadAsset(params.asset); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - container.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 100, 0); - controls.update(); - - window.addEventListener('resize', onWindowResize); - - // stats - stats = new Stats(); - container.appendChild(stats.dom); - - const gui = new GUI(); - gui.add(params, 'asset', assets).onChange(function (value) { - loadAsset(value); - }); - - guiMorphsFolder = gui.addFolder('Morphs').hide(); -} - -function loadAsset(asset) { - loader.load('models/fbx/' + asset + '.fbx', function (group) { - if (object) { - object.traverse(function (child) { - if (child.material) { - const materials = Array.isArray(child.material) ? child.material : [child.material]; - materials.forEach(material => { - if (material.map) material.map.dispose(); - material.dispose(); - }); - } - - if (child.geometry) child.geometry.dispose(); - }); - - scene.remove(object); - } - - // - - object = group; - - if (object.animations && object.animations.length) { - mixer = new THREE.AnimationMixer(object); - - const action = mixer.clipAction(object.animations[0]); - action.play(); - } else { - mixer = null; - } - - guiMorphsFolder.children.forEach(child => child.destroy()); - guiMorphsFolder.hide(); - - object.traverse(function (child) { - if (child.isMesh) { - child.castShadow = true; - child.receiveShadow = true; - - if (child.morphTargetDictionary) { - guiMorphsFolder.show(); - const meshFolder = guiMorphsFolder.addFolder(child.name || child.uuid); - Object.keys(child.morphTargetDictionary).forEach(key => { - meshFolder.add(child.morphTargetInfluences, child.morphTargetDictionary[key], 0, 1, 0.01); - }); - } - } - }); - - scene.add(object); - }); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const delta = clock.getDelta(); - - if (mixer) mixer.update(delta); - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_loader_fbx_nurbs.ts b/examples-testing/examples/webgl_loader_fbx_nurbs.ts deleted file mode 100644 index f2e45bcb5..000000000 --- a/examples-testing/examples/webgl_loader_fbx_nurbs.ts +++ /dev/null @@ -1,61 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { FBXLoader } from 'three/addons/loaders/FBXLoader.js'; - -let camera, scene, renderer, stats; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000); - camera.position.set(2, 18, 28); - - scene = new THREE.Scene(); - - // grid - const gridHelper = new THREE.GridHelper(28, 28, 0x303030, 0x303030); - scene.add(gridHelper); - - // stats - stats = new Stats(); - container.appendChild(stats.dom); - - // model - const loader = new FBXLoader(); - loader.load('models/fbx/nurbs.fbx', function (object) { - scene.add(object); - }); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 12, 0); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_loader_gcode.ts b/examples-testing/examples/webgl_loader_gcode.ts deleted file mode 100644 index 6fd3e149d..000000000 --- a/examples-testing/examples/webgl_loader_gcode.ts +++ /dev/null @@ -1,49 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GCodeLoader } from 'three/addons/loaders/GCodeLoader.js'; - -let camera, scene, renderer; - -init(); -render(); - -function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 0, 70); - - scene = new THREE.Scene(); - - const loader = new GCodeLoader(); - loader.load('models/gcode/benchy.gcode', function (object) { - object.position.set(-100, -20, 100); - scene.add(object); - - render(); - }); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.minDistance = 10; - controls.maxDistance = 100; - - window.addEventListener('resize', resize); -} - -function resize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_gltf.ts b/examples-testing/examples/webgl_loader_gltf.ts deleted file mode 100644 index e1b0adc51..000000000 --- a/examples-testing/examples/webgl_loader_gltf.ts +++ /dev/null @@ -1,74 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -let camera, scene, renderer; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); - camera.position.set(-1.8, 0.6, 2.7); - - scene = new THREE.Scene(); - - new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - scene.environment = texture; - - render(); - - // model - - const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); - loader.load('DamagedHelmet.gltf', async function (gltf) { - const model = gltf.scene; - - // wait until the model can be added to the scene without blocking due to shader compilation - - await renderer.compileAsync(model, camera, scene); - - scene.add(model); - - render(); - }); - }); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1; - container.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.minDistance = 2; - controls.maxDistance = 10; - controls.target.set(0, 0, -0.2); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -// - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_gltf_anisotropy.ts b/examples-testing/examples/webgl_loader_gltf_anisotropy.ts deleted file mode 100644 index 6e240a272..000000000 --- a/examples-testing/examples/webgl_loader_gltf_anisotropy.ts +++ /dev/null @@ -1,68 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -let renderer, scene, camera, controls; - -init(); - -async function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1.35; - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.01, 10); - camera.position.set(-0.35, -0.2, 0.35); - - controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, -0.08, 0.11); - controls.minDistance = 0.1; - controls.maxDistance = 2; - controls.addEventListener('change', render); - controls.update(); - - const rgbeLoader = new RGBELoader().setPath('textures/equirectangular/'); - const gltfLoader = new GLTFLoader().setPath('models/gltf/'); - - const [texture, gltf] = await Promise.all([ - rgbeLoader.loadAsync('royal_esplanade_1k.hdr'), - gltfLoader.loadAsync('AnisotropyBarnLamp.glb'), - ]); - - // environment - - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - scene.backgroundBlurriness = 0.5; - scene.environment = texture; - - // model - - scene.add(gltf.scene); - - render(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_gltf_avif.ts b/examples-testing/examples/webgl_loader_gltf_avif.ts deleted file mode 100644 index 37d63859e..000000000 --- a/examples-testing/examples/webgl_loader_gltf_avif.ts +++ /dev/null @@ -1,61 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; - -let camera, scene, renderer; - -init(); -render(); - -function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(1.5, 4, 9); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf6eedc); - - // - - const dracoLoader = new DRACOLoader(); - dracoLoader.setDecoderPath('jsm/libs/draco/gltf/'); - - const loader = new GLTFLoader(); - loader.setDRACOLoader(dracoLoader); - loader.setPath('models/gltf/AVIFTest/'); - loader.load('forest_house.glb', function (gltf) { - scene.add(gltf.scene); - - render(); - }); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); - controls.target.set(0, 2, 0); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -// - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_gltf_compressed.ts b/examples-testing/examples/webgl_loader_gltf_compressed.ts deleted file mode 100644 index 3bdcea8ec..000000000 --- a/examples-testing/examples/webgl_loader_gltf_compressed.ts +++ /dev/null @@ -1,83 +0,0 @@ -import * as THREE from 'three'; - -import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js'; -import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js'; - -let camera, scene, renderer; - -init(); -render(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1; - container.appendChild(renderer.domElement); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000); - camera.position.set(0, 100, 0); - - const environment = new RoomEnvironment(); - const pmremGenerator = new THREE.PMREMGenerator(renderer); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xbbbbbb); - scene.environment = pmremGenerator.fromScene(environment).texture; - environment.dispose(); - - const grid = new THREE.GridHelper(500, 10, 0xffffff, 0xffffff); - grid.material.opacity = 0.5; - grid.material.depthWrite = false; - grid.material.transparent = true; - scene.add(grid); - - const ktx2Loader = new KTX2Loader().setTranscoderPath('jsm/libs/basis/').detectSupport(renderer); - - const loader = new GLTFLoader().setPath('models/gltf/'); - loader.setKTX2Loader(ktx2Loader); - loader.setMeshoptDecoder(MeshoptDecoder); - loader.load('coffeemat.glb', function (gltf) { - // coffeemat.glb was produced from the source scene using gltfpack: - // gltfpack -i coffeemat/scene.gltf -o coffeemat.glb -cc -tc - // The resulting model uses EXT_meshopt_compression (for geometry) and KHR_texture_basisu (for texture compression using ETC1S/BasisLZ) - - gltf.scene.position.y = 8; - - scene.add(gltf.scene); - - render(); - }); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.minDistance = 400; - controls.maxDistance = 1000; - controls.target.set(10, 90, -16); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -// - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_gltf_dispersion.ts b/examples-testing/examples/webgl_loader_gltf_dispersion.ts deleted file mode 100644 index 100badcab..000000000 --- a/examples-testing/examples/webgl_loader_gltf_dispersion.ts +++ /dev/null @@ -1,66 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; - -let camera, scene, renderer; - -init().then(render); - -async function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 5); - camera.position.set(0.1, 0.05, 0.15); - - scene = new THREE.Scene(); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.toneMapping = THREE.NeutralToneMapping; - renderer.toneMappingExposure = 1; - container.appendChild(renderer.domElement); - - const environment = new RoomEnvironment(); - const pmremGenerator = new THREE.PMREMGenerator(renderer); - - scene = new THREE.Scene(); - scene.backgroundBlurriness = 0.5; - - const env = pmremGenerator.fromScene(environment).texture; - scene.background = env; - scene.environment = env; - environment.dispose(); - - const loader = new GLTFLoader(); - const gltf = await loader.loadAsync('models/gltf/DispersionTest.glb'); - - scene.add(gltf.scene); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.minDistance = 0.1; - controls.maxDistance = 10; - controls.target.set(0, 0, 0); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -// - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_gltf_instancing.ts b/examples-testing/examples/webgl_loader_gltf_instancing.ts deleted file mode 100644 index 5d23e7750..000000000 --- a/examples-testing/examples/webgl_loader_gltf_instancing.ts +++ /dev/null @@ -1,69 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -let camera, scene, renderer; - -init(); -render(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); - camera.position.set(-0.9, 0.41, -0.89); - - scene = new THREE.Scene(); - - new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - scene.environment = texture; - - render(); - - // model - - const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF-instancing/'); - loader.load('DamagedHelmetGpuInstancing.gltf', function (gltf) { - scene.add(gltf.scene); - - render(); - }); - }); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1; - container.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.minDistance = 0.2; - controls.maxDistance = 10; - controls.target.set(0, 0.25, 0); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -// - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_gltf_iridescence.ts b/examples-testing/examples/webgl_loader_gltf_iridescence.ts deleted file mode 100644 index eb0f8d914..000000000 --- a/examples-testing/examples/webgl_loader_gltf_iridescence.ts +++ /dev/null @@ -1,66 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -let renderer, scene, camera, controls; - -init().catch(function (err) { - console.error(err); -}); - -async function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setAnimationLoop(animate); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.05, 20); - camera.position.set(0.35, 0.05, 0.35); - - controls = new OrbitControls(camera, renderer.domElement); - controls.autoRotate = true; - controls.autoRotateSpeed = -0.5; - controls.target.set(0, 0.2, 0); - controls.update(); - - const rgbeLoader = new RGBELoader().setPath('textures/equirectangular/'); - - const gltfLoader = new GLTFLoader().setPath('models/gltf/'); - - const [texture, gltf] = await Promise.all([ - rgbeLoader.loadAsync('venice_sunset_1k.hdr'), - gltfLoader.loadAsync('IridescenceLamp.glb'), - ]); - - // environment - - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - scene.environment = texture; - - // model - - scene.add(gltf.scene); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - controls.update(); - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_gltf_lights.ts b/examples-testing/examples/webgl_loader_gltf_lights.ts deleted file mode 100644 index f2bd5b104..000000000 --- a/examples-testing/examples/webgl_loader_gltf_lights.ts +++ /dev/null @@ -1,83 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer; - -const params = { - punctualLightsEnabled: true, -}; - -init().then(render); - -async function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); - camera.position.set(-2, 1.5, 3); - - scene = new THREE.Scene(); - - const rgbeLoader = new RGBELoader(); - const envMap = await rgbeLoader.loadAsync('textures/equirectangular/moonless_golf_1k.hdr '); - envMap.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = envMap; - scene.environment = envMap; - - const loader = new GLTFLoader(); - const gltf = await loader.loadAsync('models/gltf/LightsPunctualLamp.glb'); - - scene.add(gltf.scene); - - const gui = new GUI(); - - gui.add(params, 'punctualLightsEnabled').onChange(toggleLights); - gui.open(); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1; - container.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.minDistance = 2; - controls.maxDistance = 10; - controls.target.set(0, 1, 0); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function toggleLights(visible) { - scene.traverse(function (object) { - if (object.isLight) { - object.visible = visible; - } - }); - - render(); -} - -// - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_gltf_sheen.ts b/examples-testing/examples/webgl_loader_gltf_sheen.ts deleted file mode 100644 index bd258d5c1..000000000 --- a/examples-testing/examples/webgl_loader_gltf_sheen.ts +++ /dev/null @@ -1,72 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, controls; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 20); - camera.position.set(-0.75, 0.7, 1.25); - - scene = new THREE.Scene(); - - // model - - new GLTFLoader().setPath('models/gltf/').load('SheenChair.glb', function (gltf) { - scene.add(gltf.scene); - - const object = gltf.scene.getObjectByName('SheenChair_fabric'); - - const gui = new GUI(); - - gui.add(object.material, 'sheen', 0, 1); - gui.open(); - }); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1; - container.appendChild(renderer.domElement); - - const environment = new RoomEnvironment(); - const pmremGenerator = new THREE.PMREMGenerator(renderer); - - scene.background = new THREE.Color(0xbbbbbb); - scene.environment = pmremGenerator.fromScene(environment).texture; - - controls = new OrbitControls(camera, renderer.domElement); - controls.enableDamping = true; - controls.minDistance = 1; - controls.maxDistance = 10; - controls.target.set(0, 0.35, 0); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - controls.update(); // required if damping enabled - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_gltf_transmission.ts b/examples-testing/examples/webgl_loader_gltf_transmission.ts deleted file mode 100644 index 87a47d2c0..000000000 --- a/examples-testing/examples/webgl_loader_gltf_transmission.ts +++ /dev/null @@ -1,80 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; - -let camera, scene, renderer, controls, clock, mixer; - -init(); - -function init() { - clock = new THREE.Clock(); - - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); - camera.position.set(0, 0.4, 0.7); - - scene = new THREE.Scene(); - - new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - scene.backgroundBlurriness = 0.35; - - scene.environment = texture; - - // model - - new GLTFLoader() - .setPath('models/gltf/') - .setDRACOLoader(new DRACOLoader().setDecoderPath('jsm/libs/draco/gltf/')) - .load('IridescentDishWithOlives.glb', function (gltf) { - mixer = new THREE.AnimationMixer(gltf.scene); - mixer.clipAction(gltf.animations[0]).play(); - - scene.add(gltf.scene); - }); - }); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1; - container.appendChild(renderer.domElement); - - controls = new OrbitControls(camera, renderer.domElement); - controls.autoRotate = true; - controls.autoRotateSpeed = -0.75; - controls.enableDamping = true; - controls.minDistance = 0.5; - controls.maxDistance = 1; - controls.target.set(0, 0.1, 0); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - if (mixer) mixer.update(clock.getDelta()); - - controls.update(); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_imagebitmap.ts b/examples-testing/examples/webgl_loader_imagebitmap.ts deleted file mode 100644 index 1049e9857..000000000 --- a/examples-testing/examples/webgl_loader_imagebitmap.ts +++ /dev/null @@ -1,109 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer; -let group, cubes; - -init(); - -function addImageBitmap() { - new THREE.ImageBitmapLoader().load( - 'textures/planets/earth_atmos_2048.jpg?' + performance.now(), - function (imageBitmap) { - const texture = new THREE.CanvasTexture(imageBitmap); - texture.colorSpace = THREE.SRGBColorSpace; - const material = new THREE.MeshBasicMaterial({ map: texture }); - - /* ImageBitmap should be disposed when done with it - Can't be done until it's actually uploaded to WebGLTexture */ - - // imageBitmap.close(); - - addCube(material); - }, - function (p) { - console.log(p); - }, - function (e) { - console.log(e); - }, - ); -} - -function addImage() { - new THREE.ImageLoader() - .setCrossOrigin('*') - .load('textures/planets/earth_atmos_2048.jpg?' + performance.now(), function (image) { - const texture = new THREE.CanvasTexture(image); - texture.colorSpace = THREE.SRGBColorSpace; - const material = new THREE.MeshBasicMaterial({ color: 0xff8888, map: texture }); - addCube(material); - }); -} - -const geometry = new THREE.BoxGeometry(); - -function addCube(material) { - const cube = new THREE.Mesh(geometry, material); - cube.position.set(Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1); - cube.rotation.set(Math.random() * 2 * Math.PI, Math.random() * 2 * Math.PI, Math.random() * 2 * Math.PI); - cubes.add(cube); -} - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - // CAMERA - - camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 1500); - camera.position.set(0, 4, 7); - camera.lookAt(0, 0, 0); - - // SCENE - - scene = new THREE.Scene(); - - // - - group = new THREE.Group(); - scene.add(group); - - group.add(new THREE.GridHelper(4, 12, 0x888888, 0x444444)); - - cubes = new THREE.Group(); - group.add(cubes); - - // RENDERER - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // TESTS - - setTimeout(addImage, 300); - setTimeout(addImage, 600); - setTimeout(addImage, 900); - setTimeout(addImageBitmap, 1300); - setTimeout(addImageBitmap, 1600); - setTimeout(addImageBitmap, 1900); - - // EVENTS - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - group.rotation.y = performance.now() / 3000; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_kmz.ts b/examples-testing/examples/webgl_loader_kmz.ts deleted file mode 100644 index f93555e41..000000000 --- a/examples-testing/examples/webgl_loader_kmz.ts +++ /dev/null @@ -1,59 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { KMZLoader } from 'three/addons/loaders/KMZLoader.js'; - -let camera, scene, renderer; - -init(); - -function init() { - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x999999); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0.5, 1.0, 0.5).normalize(); - - scene.add(light); - - camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 500); - - camera.position.y = 5; - camera.position.z = 10; - - scene.add(camera); - - const grid = new THREE.GridHelper(50, 50, 0xffffff, 0x7b7b7b); - scene.add(grid); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - const loader = new KMZLoader(); - loader.load('./models/kmz/Box.kmz', function (kmz) { - kmz.scene.position.y = 0.5; - scene.add(kmz.scene); - render(); - }); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_lwo.ts b/examples-testing/examples/webgl_loader_lwo.ts deleted file mode 100644 index fb10c8340..000000000 --- a/examples-testing/examples/webgl_loader_lwo.ts +++ /dev/null @@ -1,69 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { LWOLoader } from 'three/addons/loaders/LWOLoader.js'; - -let camera, scene, renderer; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 200); - camera.position.set(0.7, 14.6, -43.2); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xa0a0a0); - - const ambientLight = new THREE.AmbientLight(0xbbbbbb); - scene.add(ambientLight); - - const light1 = new THREE.DirectionalLight(0xc1c1c1, 3); - light1.position.set(0, 200, -100); - scene.add(light1); - - const grid = new THREE.GridHelper(200, 20, 0x000000, 0x000000); - grid.material.opacity = 0.3; - grid.material.transparent = true; - scene.add(grid); - - const loader = new LWOLoader(); - loader.load('models/lwo/Objects/LWO3/Demo.lwo', function (object) { - const phong = object.meshes[0]; - phong.position.set(2, 12, 0); - - const standard = object.meshes[1]; - standard.position.set(-2, 12, 0); - - const rocket = object.meshes[2]; - rocket.position.set(0, 10.5, 1); - - scene.add(phong, standard, rocket); - }); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - container.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(-1.33, 10, 6.7); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_md2_control.ts b/examples-testing/examples/webgl_loader_md2_control.ts deleted file mode 100644 index 683e4c2ad..000000000 --- a/examples-testing/examples/webgl_loader_md2_control.ts +++ /dev/null @@ -1,289 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { MD2CharacterComplex } from 'three/addons/misc/MD2CharacterComplex.js'; -import { Gyroscope } from 'three/addons/misc/Gyroscope.js'; - -let SCREEN_WIDTH = window.innerWidth; -let SCREEN_HEIGHT = window.innerHeight; - -let container, stats; -let camera, scene, renderer; - -const characters = []; -let nCharacters = 0; - -let cameraControls; - -const controls = { - moveForward: false, - moveBackward: false, - moveLeft: false, - moveRight: false, -}; - -const clock = new THREE.Clock(); - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - // CAMERA - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 4000); - camera.position.set(0, 150, 1300); - - // SCENE - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - scene.fog = new THREE.Fog(0xffffff, 1000, 4000); - - scene.add(camera); - - // LIGHTS - - scene.add(new THREE.AmbientLight(0x666666, 3)); - - const light = new THREE.DirectionalLight(0xffffff, 7); - light.position.set(200, 450, 500); - - light.castShadow = true; - - light.shadow.mapSize.width = 1024; - light.shadow.mapSize.height = 512; - - light.shadow.camera.near = 100; - light.shadow.camera.far = 1200; - - light.shadow.camera.left = -1000; - light.shadow.camera.right = 1000; - light.shadow.camera.top = 350; - light.shadow.camera.bottom = -350; - - scene.add(light); - // scene.add( new THREE.CameraHelper( light.shadow.camera ) ); - - // GROUND - - const gt = new THREE.TextureLoader().load('textures/terrain/grasslight-big.jpg'); - const gg = new THREE.PlaneGeometry(16000, 16000); - const gm = new THREE.MeshPhongMaterial({ color: 0xffffff, map: gt }); - - const ground = new THREE.Mesh(gg, gm); - ground.rotation.x = -Math.PI / 2; - ground.material.map.repeat.set(64, 64); - ground.material.map.wrapS = THREE.RepeatWrapping; - ground.material.map.wrapT = THREE.RepeatWrapping; - ground.material.map.colorSpace = THREE.SRGBColorSpace; - // note that because the ground does not cast a shadow, .castShadow is left false - ground.receiveShadow = true; - - scene.add(ground); - - // RENDERER - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - renderer.shadowMap.enabled = true; - renderer.shadowMap.type = THREE.PCFSoftShadowMap; - - // STATS - - stats = new Stats(); - container.appendChild(stats.dom); - - // EVENTS - - window.addEventListener('resize', onWindowResize); - document.addEventListener('keydown', onKeyDown); - document.addEventListener('keyup', onKeyUp); - - // CONTROLS - - cameraControls = new OrbitControls(camera, renderer.domElement); - cameraControls.target.set(0, 50, 0); - cameraControls.update(); - - // CHARACTER - - const configOgro = { - baseUrl: 'models/md2/ogro/', - - body: 'ogro.md2', - skins: [ - 'grok.jpg', - 'ogrobase.png', - 'arboshak.png', - 'ctf_r.png', - 'ctf_b.png', - 'darkam.png', - 'freedom.png', - 'gib.png', - 'gordogh.png', - 'igdosh.png', - 'khorne.png', - 'nabogro.png', - 'sharokh.png', - ], - weapons: [['weapon.md2', 'weapon.jpg']], - animations: { - move: 'run', - idle: 'stand', - jump: 'jump', - attack: 'attack', - crouchMove: 'cwalk', - crouchIdle: 'cstand', - crouchAttach: 'crattack', - }, - - walkSpeed: 350, - crouchSpeed: 175, - }; - - const nRows = 1; - const nSkins = configOgro.skins.length; - - nCharacters = nSkins * nRows; - - for (let i = 0; i < nCharacters; i++) { - const character = new MD2CharacterComplex(); - character.scale = 3; - character.controls = controls; - characters.push(character); - } - - const baseCharacter = new MD2CharacterComplex(); - baseCharacter.scale = 3; - - baseCharacter.onLoadComplete = function () { - let k = 0; - - for (let j = 0; j < nRows; j++) { - for (let i = 0; i < nSkins; i++) { - const cloneCharacter = characters[k]; - - cloneCharacter.shareParts(baseCharacter); - - // cast and receive shadows - cloneCharacter.enableShadows(true); - - cloneCharacter.setWeapon(0); - cloneCharacter.setSkin(i); - - cloneCharacter.root.position.x = (i - nSkins / 2) * 150; - cloneCharacter.root.position.z = j * 250; - - scene.add(cloneCharacter.root); - - k++; - } - } - - const gyro = new Gyroscope(); - gyro.add(camera); - gyro.add(light, light.target); - - characters[Math.floor(nSkins / 2)].root.add(gyro); - }; - - baseCharacter.loadParts(configOgro); -} - -// EVENT HANDLERS - -function onWindowResize() { - SCREEN_WIDTH = window.innerWidth; - SCREEN_HEIGHT = window.innerHeight; - - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - - camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT; - camera.updateProjectionMatrix(); -} - -function onKeyDown(event) { - switch (event.code) { - case 'ArrowUp': - case 'KeyW': - controls.moveForward = true; - break; - - case 'ArrowDown': - case 'KeyS': - controls.moveBackward = true; - break; - - case 'ArrowLeft': - case 'KeyA': - controls.moveLeft = true; - break; - - case 'ArrowRight': - case 'KeyD': - controls.moveRight = true; - break; - - // case 'KeyC': controls.crouch = true; break; - // case 'Space': controls.jump = true; break; - // case 'ControlLeft': - // case 'ControlRight': controls.attack = true; break; - } -} - -function onKeyUp(event) { - switch (event.code) { - case 'ArrowUp': - case 'KeyW': - controls.moveForward = false; - break; - - case 'ArrowDown': - case 'KeyS': - controls.moveBackward = false; - break; - - case 'ArrowLeft': - case 'KeyA': - controls.moveLeft = false; - break; - - case 'ArrowRight': - case 'KeyD': - controls.moveRight = false; - break; - - // case 'KeyC': controls.crouch = false; break; - // case 'Space': controls.jump = false; break; - // case 'ControlLeft': - // case 'ControlRight': controls.attack = false; break; - } -} - -// - -function animate() { - render(); - - stats.update(); -} - -function render() { - const delta = clock.getDelta(); - - for (let i = 0; i < nCharacters; i++) { - characters[i].update(delta); - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_mdd.ts b/examples-testing/examples/webgl_loader_mdd.ts deleted file mode 100644 index 5b13e8f4b..000000000 --- a/examples-testing/examples/webgl_loader_mdd.ts +++ /dev/null @@ -1,62 +0,0 @@ -import * as THREE from 'three'; - -import { MDDLoader } from 'three/addons/loaders/MDDLoader.js'; - -let camera, scene, renderer, mixer, clock; - -init(); - -function init() { - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(8, 8, 8); - camera.lookAt(scene.position); - - clock = new THREE.Clock(); - - // - - const loader = new MDDLoader(); - loader.load('models/mdd/cube.mdd', function (result) { - const morphTargets = result.morphTargets; - const clip = result.clip; - // clip.optimize(); // optional - - const geometry = new THREE.BoxGeometry(); - geometry.morphAttributes.position = morphTargets; // apply morph targets - - const material = new THREE.MeshNormalMaterial(); - - const mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - mixer = new THREE.AnimationMixer(mesh); - mixer.clipAction(clip).play(); // use clip - }); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const delta = clock.getDelta(); - - if (mixer) mixer.update(delta); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_obj.ts b/examples-testing/examples/webgl_loader_obj.ts deleted file mode 100644 index f61eeb758..000000000 --- a/examples-testing/examples/webgl_loader_obj.ts +++ /dev/null @@ -1,98 +0,0 @@ -import * as THREE from 'three'; - -import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer; - -let object; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 20); - camera.position.z = 2.5; - - // scene - - scene = new THREE.Scene(); - - const ambientLight = new THREE.AmbientLight(0xffffff); - scene.add(ambientLight); - - const pointLight = new THREE.PointLight(0xffffff, 15); - camera.add(pointLight); - scene.add(camera); - - // manager - - function loadModel() { - object.traverse(function (child) { - if (child.isMesh) child.material.map = texture; - }); - - object.position.y = -0.95; - object.scale.setScalar(0.01); - scene.add(object); - - render(); - } - - const manager = new THREE.LoadingManager(loadModel); - - // texture - - const textureLoader = new THREE.TextureLoader(manager); - const texture = textureLoader.load('textures/uv_grid_opengl.jpg', render); - texture.colorSpace = THREE.SRGBColorSpace; - - // model - - function onProgress(xhr) { - if (xhr.lengthComputable) { - const percentComplete = (xhr.loaded / xhr.total) * 100; - console.log('model ' + percentComplete.toFixed(2) + '% downloaded'); - } - } - - function onError() {} - - const loader = new OBJLoader(manager); - loader.load( - 'models/obj/male02/male02.obj', - function (obj) { - object = obj; - }, - onProgress, - onError, - ); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 2; - controls.maxDistance = 5; - controls.addEventListener('change', render); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_obj_mtl.ts b/examples-testing/examples/webgl_loader_obj_mtl.ts deleted file mode 100644 index 4308aee7b..000000000 --- a/examples-testing/examples/webgl_loader_obj_mtl.ts +++ /dev/null @@ -1,82 +0,0 @@ -import * as THREE from 'three'; - -import { MTLLoader } from 'three/addons/loaders/MTLLoader.js'; -import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 20); - camera.position.z = 2.5; - - // scene - - scene = new THREE.Scene(); - - const ambientLight = new THREE.AmbientLight(0xffffff); - scene.add(ambientLight); - - const pointLight = new THREE.PointLight(0xffffff, 15); - camera.add(pointLight); - scene.add(camera); - - // model - - const onProgress = function (xhr) { - if (xhr.lengthComputable) { - const percentComplete = (xhr.loaded / xhr.total) * 100; - console.log(percentComplete.toFixed(2) + '% downloaded'); - } - }; - - new MTLLoader().setPath('models/obj/male02/').load('male02.mtl', function (materials) { - materials.preload(); - - new OBJLoader() - .setMaterials(materials) - .setPath('models/obj/male02/') - .load( - 'male02.obj', - function (object) { - object.position.y = -0.95; - object.scale.setScalar(0.01); - scene.add(object); - }, - onProgress, - ); - }); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 2; - controls.maxDistance = 5; - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_pcd.ts b/examples-testing/examples/webgl_loader_pcd.ts deleted file mode 100644 index d69e3fa2a..000000000 --- a/examples-testing/examples/webgl_loader_pcd.ts +++ /dev/null @@ -1,65 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { PCDLoader } from 'three/addons/loaders/PCDLoader.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer; - -init(); -render(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 0.01, 40); - camera.position.set(0, 0, 1); - scene.add(camera); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.minDistance = 0.5; - controls.maxDistance = 10; - - //scene.add( new THREE.AxesHelper( 1 ) ); - - const loader = new PCDLoader(); - loader.load('./models/pcd/binary/Zaghetto.pcd', function (points) { - points.geometry.center(); - points.geometry.rotateX(Math.PI); - points.name = 'Zaghetto.pcd'; - scene.add(points); - - // - - const gui = new GUI(); - - gui.add(points.material, 'size', 0.001, 0.01).onChange(render); - gui.addColor(points.material, 'color').onChange(render); - gui.open(); - - // - - render(); - }); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_pdb.ts b/examples-testing/examples/webgl_loader_pdb.ts deleted file mode 100644 index b560efa73..000000000 --- a/examples-testing/examples/webgl_loader_pdb.ts +++ /dev/null @@ -1,208 +0,0 @@ -import * as THREE from 'three'; - -import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; -import { PDBLoader } from 'three/addons/loaders/PDBLoader.js'; -import { CSS2DRenderer, CSS2DObject } from 'three/addons/renderers/CSS2DRenderer.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, labelRenderer; -let controls; - -let root; - -const MOLECULES = { - Ethanol: 'ethanol.pdb', - Aspirin: 'aspirin.pdb', - Caffeine: 'caffeine.pdb', - Nicotine: 'nicotine.pdb', - LSD: 'lsd.pdb', - Cocaine: 'cocaine.pdb', - Cholesterol: 'cholesterol.pdb', - Lycopene: 'lycopene.pdb', - Glucose: 'glucose.pdb', - 'Aluminium oxide': 'Al2O3.pdb', - Cubane: 'cubane.pdb', - Copper: 'cu.pdb', - Fluorite: 'caf2.pdb', - Salt: 'nacl.pdb', - 'YBCO superconductor': 'ybco.pdb', - Buckyball: 'buckyball.pdb', - Graphite: 'graphite.pdb', -}; - -const params = { - molecule: 'caffeine.pdb', -}; - -const loader = new PDBLoader(); -const offset = new THREE.Vector3(); - -init(); - -function init() { - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x050505); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 5000); - camera.position.z = 1000; - scene.add(camera); - - const light1 = new THREE.DirectionalLight(0xffffff, 2.5); - light1.position.set(1, 1, 1); - scene.add(light1); - - const light2 = new THREE.DirectionalLight(0xffffff, 1.5); - light2.position.set(-1, -1, 1); - scene.add(light2); - - root = new THREE.Group(); - scene.add(root); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.getElementById('container').appendChild(renderer.domElement); - - labelRenderer = new CSS2DRenderer(); - labelRenderer.setSize(window.innerWidth, window.innerHeight); - labelRenderer.domElement.style.position = 'absolute'; - labelRenderer.domElement.style.top = '0px'; - labelRenderer.domElement.style.pointerEvents = 'none'; - document.getElementById('container').appendChild(labelRenderer.domElement); - - // - - controls = new TrackballControls(camera, renderer.domElement); - controls.minDistance = 500; - controls.maxDistance = 2000; - - // - - loadMolecule(params.molecule); - - // - - window.addEventListener('resize', onWindowResize); - - // - - const gui = new GUI(); - - gui.add(params, 'molecule', MOLECULES).onChange(loadMolecule); - gui.open(); -} - -// - -function loadMolecule(model) { - const url = 'models/pdb/' + model; - - while (root.children.length > 0) { - const object = root.children[0]; - object.parent.remove(object); - } - - loader.load(url, function (pdb) { - const geometryAtoms = pdb.geometryAtoms; - const geometryBonds = pdb.geometryBonds; - const json = pdb.json; - - const boxGeometry = new THREE.BoxGeometry(1, 1, 1); - const sphereGeometry = new THREE.IcosahedronGeometry(1, 3); - - geometryAtoms.computeBoundingBox(); - geometryAtoms.boundingBox.getCenter(offset).negate(); - - geometryAtoms.translate(offset.x, offset.y, offset.z); - geometryBonds.translate(offset.x, offset.y, offset.z); - - let positions = geometryAtoms.getAttribute('position'); - const colors = geometryAtoms.getAttribute('color'); - - const position = new THREE.Vector3(); - const color = new THREE.Color(); - - for (let i = 0; i < positions.count; i++) { - position.x = positions.getX(i); - position.y = positions.getY(i); - position.z = positions.getZ(i); - - color.r = colors.getX(i); - color.g = colors.getY(i); - color.b = colors.getZ(i); - - const material = new THREE.MeshPhongMaterial({ color: color }); - - const object = new THREE.Mesh(sphereGeometry, material); - object.position.copy(position); - object.position.multiplyScalar(75); - object.scale.multiplyScalar(25); - root.add(object); - - const atom = json.atoms[i]; - - const text = document.createElement('div'); - text.className = 'label'; - text.style.color = 'rgb(' + atom[3][0] + ',' + atom[3][1] + ',' + atom[3][2] + ')'; - text.textContent = atom[4]; - - const label = new CSS2DObject(text); - label.position.copy(object.position); - root.add(label); - } - - positions = geometryBonds.getAttribute('position'); - - const start = new THREE.Vector3(); - const end = new THREE.Vector3(); - - for (let i = 0; i < positions.count; i += 2) { - start.x = positions.getX(i); - start.y = positions.getY(i); - start.z = positions.getZ(i); - - end.x = positions.getX(i + 1); - end.y = positions.getY(i + 1); - end.z = positions.getZ(i + 1); - - start.multiplyScalar(75); - end.multiplyScalar(75); - - const object = new THREE.Mesh(boxGeometry, new THREE.MeshPhongMaterial({ color: 0xffffff })); - object.position.copy(start); - object.position.lerp(end, 0.5); - object.scale.set(5, 5, start.distanceTo(end)); - object.lookAt(end); - root.add(object); - } - }); -} - -// - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - labelRenderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - controls.update(); - - const time = Date.now() * 0.0004; - - root.rotation.x = time; - root.rotation.y = time * 0.7; - - render(); -} - -function render() { - renderer.render(scene, camera); - labelRenderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_ply.ts b/examples-testing/examples/webgl_loader_ply.ts deleted file mode 100644 index 0f4042b7d..000000000 --- a/examples-testing/examples/webgl_loader_ply.ts +++ /dev/null @@ -1,146 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { PLYLoader } from 'three/addons/loaders/PLYLoader.js'; - -let container, stats; - -let camera, cameraTarget, scene, renderer; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 15); - camera.position.set(3, 0.15, 3); - - cameraTarget = new THREE.Vector3(0, -0.1, 0); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x72645b); - scene.fog = new THREE.Fog(0x72645b, 2, 15); - - // Ground - - const plane = new THREE.Mesh( - new THREE.PlaneGeometry(40, 40), - new THREE.MeshPhongMaterial({ color: 0xcbcbcb, specular: 0x474747 }), - ); - plane.rotation.x = -Math.PI / 2; - plane.position.y = -0.5; - scene.add(plane); - - plane.receiveShadow = true; - - // PLY file - - const loader = new PLYLoader(); - loader.load('./models/ply/ascii/dolphins.ply', function (geometry) { - geometry.computeVertexNormals(); - - const material = new THREE.MeshStandardMaterial({ color: 0x009cff, flatShading: true }); - const mesh = new THREE.Mesh(geometry, material); - - mesh.position.y = -0.2; - mesh.position.z = 0.3; - mesh.rotation.x = -Math.PI / 2; - mesh.scale.multiplyScalar(0.001); - - mesh.castShadow = true; - mesh.receiveShadow = true; - - scene.add(mesh); - }); - - loader.load('./models/ply/binary/Lucy100k.ply', function (geometry) { - geometry.computeVertexNormals(); - - const material = new THREE.MeshStandardMaterial({ color: 0x009cff, flatShading: true }); - const mesh = new THREE.Mesh(geometry, material); - - mesh.position.x = -0.2; - mesh.position.y = -0.02; - mesh.position.z = -0.2; - mesh.scale.multiplyScalar(0.0006); - - mesh.castShadow = true; - mesh.receiveShadow = true; - - scene.add(mesh); - }); - - // Lights - - scene.add(new THREE.HemisphereLight(0x8d7c7c, 0x494966, 3)); - - addShadowedLight(1, 1, 1, 0xffffff, 3.5); - addShadowedLight(0.5, 1, -1, 0xffd500, 3); - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - - renderer.shadowMap.enabled = true; - - container.appendChild(renderer.domElement); - - // stats - - stats = new Stats(); - container.appendChild(stats.dom); - - // resize - - window.addEventListener('resize', onWindowResize); -} - -function addShadowedLight(x, y, z, color, intensity) { - const directionalLight = new THREE.DirectionalLight(color, intensity); - directionalLight.position.set(x, y, z); - scene.add(directionalLight); - - directionalLight.castShadow = true; - - const d = 1; - directionalLight.shadow.camera.left = -d; - directionalLight.shadow.camera.right = d; - directionalLight.shadow.camera.top = d; - directionalLight.shadow.camera.bottom = -d; - - directionalLight.shadow.camera.near = 1; - directionalLight.shadow.camera.far = 4; - - directionalLight.shadow.mapSize.width = 1024; - directionalLight.shadow.mapSize.height = 1024; - - directionalLight.shadow.bias = -0.001; -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - const timer = Date.now() * 0.0005; - - camera.position.x = Math.sin(timer) * 2.5; - camera.position.z = Math.cos(timer) * 2.5; - - camera.lookAt(cameraTarget); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_svg.ts b/examples-testing/examples/webgl_loader_svg.ts deleted file mode 100644 index 45361b92f..000000000 --- a/examples-testing/examples/webgl_loader_svg.ts +++ /dev/null @@ -1,193 +0,0 @@ -import * as THREE from 'three'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { SVGLoader } from 'three/addons/loaders/SVGLoader.js'; - -let renderer, scene, camera, gui, guiData; - -init(); - -// - -function init() { - const container = document.getElementById('container'); - - // - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 0, 200); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - container.appendChild(renderer.domElement); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); - controls.screenSpacePanning = true; - - // - - window.addEventListener('resize', onWindowResize); - - guiData = { - currentURL: 'models/svg/tiger.svg', - drawFillShapes: true, - drawStrokes: true, - fillShapesWireframe: false, - strokesWireframe: false, - }; - - loadSVG(guiData.currentURL); - - createGUI(); -} - -function createGUI() { - if (gui) gui.destroy(); - - gui = new GUI(); - - gui.add(guiData, 'currentURL', { - Tiger: 'models/svg/tiger.svg', - 'Joins and caps': 'models/svg/lineJoinsAndCaps.svg', - Hexagon: 'models/svg/hexagon.svg', - Energy: 'models/svg/energy.svg', - 'Test 1': 'models/svg/tests/1.svg', - 'Test 2': 'models/svg/tests/2.svg', - 'Test 3': 'models/svg/tests/3.svg', - 'Test 4': 'models/svg/tests/4.svg', - 'Test 5': 'models/svg/tests/5.svg', - 'Test 6': 'models/svg/tests/6.svg', - 'Test 7': 'models/svg/tests/7.svg', - 'Test 8': 'models/svg/tests/8.svg', - 'Test 9': 'models/svg/tests/9.svg', - Units: 'models/svg/tests/units.svg', - Ordering: 'models/svg/tests/ordering.svg', - Defs: 'models/svg/tests/testDefs/Svg-defs.svg', - Defs2: 'models/svg/tests/testDefs/Svg-defs2.svg', - Defs3: 'models/svg/tests/testDefs/Wave-defs.svg', - Defs4: 'models/svg/tests/testDefs/defs4.svg', - Defs5: 'models/svg/tests/testDefs/defs5.svg', - 'Style CSS inside defs': 'models/svg/style-css-inside-defs.svg', - 'Multiple CSS classes': 'models/svg/multiple-css-classes.svg', - 'Zero Radius': 'models/svg/zero-radius.svg', - 'Styles in svg tag': 'models/svg/tests/styles.svg', - 'Round join': 'models/svg/tests/roundJoinPrecisionIssue.svg', - 'Ellipse Transformations': 'models/svg/tests/ellipseTransform.svg', - singlePointTest: 'models/svg/singlePointTest.svg', - singlePointTest2: 'models/svg/singlePointTest2.svg', - singlePointTest3: 'models/svg/singlePointTest3.svg', - emptyPath: 'models/svg/emptyPath.svg', - }) - .name('SVG File') - .onChange(update); - - gui.add(guiData, 'drawStrokes').name('Draw strokes').onChange(update); - - gui.add(guiData, 'drawFillShapes').name('Draw fill shapes').onChange(update); - - gui.add(guiData, 'strokesWireframe').name('Wireframe strokes').onChange(update); - - gui.add(guiData, 'fillShapesWireframe').name('Wireframe fill shapes').onChange(update); - - function update() { - loadSVG(guiData.currentURL); - } -} - -function loadSVG(url) { - // - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xb0b0b0); - - // - - const helper = new THREE.GridHelper(160, 10, 0x8d8d8d, 0xc1c1c1); - helper.rotation.x = Math.PI / 2; - scene.add(helper); - - // - - const loader = new SVGLoader(); - - loader.load(url, function (data) { - const group = new THREE.Group(); - group.scale.multiplyScalar(0.25); - group.position.x = -70; - group.position.y = 70; - group.scale.y *= -1; - - let renderOrder = 0; - - for (const path of data.paths) { - const fillColor = path.userData.style.fill; - - if (guiData.drawFillShapes && fillColor !== undefined && fillColor !== 'none') { - const material = new THREE.MeshBasicMaterial({ - color: new THREE.Color().setStyle(fillColor), - opacity: path.userData.style.fillOpacity, - transparent: true, - side: THREE.DoubleSide, - depthWrite: false, - wireframe: guiData.fillShapesWireframe, - }); - - const shapes = SVGLoader.createShapes(path); - - for (const shape of shapes) { - const geometry = new THREE.ShapeGeometry(shape); - const mesh = new THREE.Mesh(geometry, material); - mesh.renderOrder = renderOrder++; - - group.add(mesh); - } - } - - const strokeColor = path.userData.style.stroke; - - if (guiData.drawStrokes && strokeColor !== undefined && strokeColor !== 'none') { - const material = new THREE.MeshBasicMaterial({ - color: new THREE.Color().setStyle(strokeColor), - opacity: path.userData.style.strokeOpacity, - transparent: true, - side: THREE.DoubleSide, - depthWrite: false, - wireframe: guiData.strokesWireframe, - }); - - for (const subPath of path.subPaths) { - const geometry = SVGLoader.pointsToStroke(subPath.getPoints(), path.userData.style); - - if (geometry) { - const mesh = new THREE.Mesh(geometry, material); - mesh.renderOrder = renderOrder++; - - group.add(mesh); - } - } - } - } - - scene.add(group); - - render(); - }); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_texture_dds.ts b/examples-testing/examples/webgl_loader_texture_dds.ts deleted file mode 100644 index bc4bd0572..000000000 --- a/examples-testing/examples/webgl_loader_texture_dds.ts +++ /dev/null @@ -1,207 +0,0 @@ -import * as THREE from 'three'; - -import { DDSLoader } from 'three/addons/loaders/DDSLoader.js'; - -let camera, scene, renderer; -const meshes = []; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.z = 15; - - scene = new THREE.Scene(); - - const geometry = new THREE.BoxGeometry(2, 2, 2); - - /* - This is how compressed textures are supposed to be used: - - DXT1 - RGB - opaque textures - DXT3 - RGBA - transparent textures with sharp alpha transitions - DXT5 - RGBA - transparent textures with full alpha range - */ - - const loader = new DDSLoader(); - - const map1 = loader.load('textures/compressed/disturb_dxt1_nomip.dds'); - map1.minFilter = map1.magFilter = THREE.LinearFilter; - map1.anisotropy = 4; - map1.colorSpace = THREE.SRGBColorSpace; - - const map2 = loader.load('textures/compressed/disturb_dxt1_mip.dds'); - map2.anisotropy = 4; - map2.colorSpace = THREE.SRGBColorSpace; - - const map3 = loader.load('textures/compressed/hepatica_dxt3_mip.dds'); - map3.anisotropy = 4; - map3.colorSpace = THREE.SRGBColorSpace; - - const map4 = loader.load('textures/compressed/explosion_dxt5_mip.dds'); - map4.anisotropy = 4; - map4.colorSpace = THREE.SRGBColorSpace; - - const map5 = loader.load('textures/compressed/disturb_argb_nomip.dds'); - map5.minFilter = map5.magFilter = THREE.LinearFilter; - map5.anisotropy = 4; - map5.colorSpace = THREE.SRGBColorSpace; - - const map6 = loader.load('textures/compressed/disturb_argb_mip.dds'); - map6.anisotropy = 4; - map6.colorSpace = THREE.SRGBColorSpace; - - const map7 = loader.load('textures/compressed/disturb_dx10_bc6h_signed_nomip.dds'); - map7.anisotropy = 4; - - const map8 = loader.load('textures/compressed/disturb_dx10_bc6h_signed_mip.dds'); - map8.anisotropy = 4; - - const map9 = loader.load('textures/compressed/disturb_dx10_bc6h_unsigned_nomip.dds'); - map9.anisotropy = 4; - - const map10 = loader.load('textures/compressed/disturb_dx10_bc6h_unsigned_mip.dds'); - map10.anisotropy = 4; - - const cubemap1 = loader.load('textures/compressed/Mountains.dds', function (texture) { - texture.magFilter = THREE.LinearFilter; - texture.minFilter = THREE.LinearFilter; - texture.mapping = THREE.CubeReflectionMapping; - texture.colorSpace = THREE.SRGBColorSpace; - material1.needsUpdate = true; - }); - - const cubemap2 = loader.load('textures/compressed/Mountains_argb_mip.dds', function (texture) { - texture.magFilter = THREE.LinearFilter; - texture.minFilter = THREE.LinearFilter; - texture.mapping = THREE.CubeReflectionMapping; - texture.colorSpace = THREE.SRGBColorSpace; - material5.needsUpdate = true; - }); - - const cubemap3 = loader.load('textures/compressed/Mountains_argb_nomip.dds', function (texture) { - texture.magFilter = THREE.LinearFilter; - texture.minFilter = THREE.LinearFilter; - texture.mapping = THREE.CubeReflectionMapping; - texture.colorSpace = THREE.SRGBColorSpace; - material6.needsUpdate = true; - }); - - const material1 = new THREE.MeshBasicMaterial({ map: map1, envMap: cubemap1 }); - const material2 = new THREE.MeshBasicMaterial({ map: map2 }); - const material3 = new THREE.MeshBasicMaterial({ map: map3, alphaTest: 0.5, side: THREE.DoubleSide }); - const material4 = new THREE.MeshBasicMaterial({ - map: map4, - side: THREE.DoubleSide, - blending: THREE.AdditiveBlending, - depthTest: false, - transparent: true, - }); - const material5 = new THREE.MeshBasicMaterial({ envMap: cubemap2 }); - const material6 = new THREE.MeshBasicMaterial({ envMap: cubemap3 }); - const material7 = new THREE.MeshBasicMaterial({ map: map5 }); - const material8 = new THREE.MeshBasicMaterial({ map: map6 }); - const material9 = new THREE.MeshBasicMaterial({ map: map7 }); - const material10 = new THREE.MeshBasicMaterial({ map: map8 }); - const material11 = new THREE.MeshBasicMaterial({ map: map9 }); - const material12 = new THREE.MeshBasicMaterial({ map: map10 }); - - let mesh = new THREE.Mesh(new THREE.TorusGeometry(), material1); - mesh.position.x = -10; - mesh.position.y = -2; - scene.add(mesh); - meshes.push(mesh); - - mesh = new THREE.Mesh(geometry, material2); - mesh.position.x = -6; - mesh.position.y = -2; - scene.add(mesh); - meshes.push(mesh); - - mesh = new THREE.Mesh(geometry, material3); - mesh.position.x = -6; - mesh.position.y = 2; - scene.add(mesh); - meshes.push(mesh); - - mesh = new THREE.Mesh(geometry, material4); - mesh.position.x = -10; - mesh.position.y = 2; - scene.add(mesh); - meshes.push(mesh); - - mesh = new THREE.Mesh(geometry, material5); - mesh.position.x = -2; - mesh.position.y = 2; - scene.add(mesh); - meshes.push(mesh); - - mesh = new THREE.Mesh(geometry, material6); - mesh.position.x = -2; - mesh.position.y = -2; - scene.add(mesh); - meshes.push(mesh); - - mesh = new THREE.Mesh(geometry, material7); - mesh.position.x = 2; - mesh.position.y = -2; - scene.add(mesh); - meshes.push(mesh); - - mesh = new THREE.Mesh(geometry, material8); - mesh.position.x = 2; - mesh.position.y = 2; - scene.add(mesh); - meshes.push(mesh); - - mesh = new THREE.Mesh(geometry, material9); - mesh.position.x = 6; - mesh.position.y = -2; - scene.add(mesh); - meshes.push(mesh); - - mesh = new THREE.Mesh(geometry, material10); - mesh.position.x = 6; - mesh.position.y = 2; - scene.add(mesh); - meshes.push(mesh); - - mesh = new THREE.Mesh(geometry, material11); - mesh.position.x = 10; - mesh.position.y = -2; - scene.add(mesh); - meshes.push(mesh); - - mesh = new THREE.Mesh(geometry, material12); - mesh.position.x = 10; - mesh.position.y = 2; - scene.add(mesh); - meshes.push(mesh); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const time = Date.now() * 0.001; - - for (let i = 0; i < meshes.length; i++) { - const mesh = meshes[i]; - mesh.rotation.x = time; - mesh.rotation.y = time; - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_texture_ktx.ts b/examples-testing/examples/webgl_loader_texture_ktx.ts deleted file mode 100644 index af66eb810..000000000 --- a/examples-testing/examples/webgl_loader_texture_ktx.ts +++ /dev/null @@ -1,137 +0,0 @@ -import * as THREE from 'three'; - -import { KTXLoader } from 'three/addons/loaders/KTXLoader.js'; - -/* - This is how compressed textures are supposed to be used: - - best for desktop: - BC1(DXT1) - opaque textures - BC3(DXT5) - transparent textures with full alpha range - - best for iOS: - PVR2, PVR4 - opaque textures or alpha - - best for Android: - ETC1 - opaque textures - ASTC_4x4, ASTC8x8 - transparent textures with full alpha range - */ - -let camera, scene, renderer; -const meshes = []; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - const formats = { - astc: renderer.extensions.has('WEBGL_compressed_texture_astc'), - etc1: renderer.extensions.has('WEBGL_compressed_texture_etc1'), - s3tc: renderer.extensions.has('WEBGL_compressed_texture_s3tc'), - pvrtc: renderer.extensions.has('WEBGL_compressed_texture_pvrtc'), - }; - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 2000); - camera.position.z = 1000; - - scene = new THREE.Scene(); - - const geometry = new THREE.BoxGeometry(200, 200, 200); - let material1, material2; - - // TODO: add cubemap support - const loader = new KTXLoader(); - - if (formats.pvrtc) { - material1 = new THREE.MeshBasicMaterial({ - map: loader.load('textures/compressed/disturb_PVR2bpp.ktx'), - }); - material1.map.colorSpace = THREE.SRGBColorSpace; - material2 = new THREE.MeshBasicMaterial({ - map: loader.load('textures/compressed/lensflare_PVR4bpp.ktx'), - depthTest: false, - transparent: true, - side: THREE.DoubleSide, - }); - material2.map.colorSpace = THREE.SRGBColorSpace; - - meshes.push(new THREE.Mesh(geometry, material1)); - meshes.push(new THREE.Mesh(geometry, material2)); - } - - if (formats.s3tc) { - material1 = new THREE.MeshBasicMaterial({ - map: loader.load('textures/compressed/disturb_BC1.ktx'), - }); - material1.map.colorSpace = THREE.SRGBColorSpace; - material2 = new THREE.MeshBasicMaterial({ - map: loader.load('textures/compressed/lensflare_BC3.ktx'), - depthTest: false, - transparent: true, - side: THREE.DoubleSide, - }); - material2.map.colorSpace = THREE.SRGBColorSpace; - - meshes.push(new THREE.Mesh(geometry, material1)); - meshes.push(new THREE.Mesh(geometry, material2)); - } - - if (formats.etc1) { - material1 = new THREE.MeshBasicMaterial({ - map: loader.load('textures/compressed/disturb_ETC1.ktx'), - }); - - meshes.push(new THREE.Mesh(geometry, material1)); - } - - if (formats.astc) { - material1 = new THREE.MeshBasicMaterial({ - map: loader.load('textures/compressed/disturb_ASTC4x4.ktx'), - }); - material1.map.colorSpace = THREE.SRGBColorSpace; - material2 = new THREE.MeshBasicMaterial({ - map: loader.load('textures/compressed/lensflare_ASTC8x8.ktx'), - depthTest: false, - transparent: true, - side: THREE.DoubleSide, - }); - material2.map.colorSpace = THREE.SRGBColorSpace; - - meshes.push(new THREE.Mesh(geometry, material1)); - meshes.push(new THREE.Mesh(geometry, material2)); - } - - let x = (-meshes.length / 2) * 150; - for (let i = 0; i < meshes.length; ++i, x += 300) { - const mesh = meshes[i]; - mesh.position.x = x; - mesh.position.y = 0; - scene.add(mesh); - } - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const time = Date.now() * 0.001; - - for (let i = 0; i < meshes.length; i++) { - const mesh = meshes[i]; - mesh.rotation.x = time; - mesh.rotation.y = time; - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_texture_logluv.ts b/examples-testing/examples/webgl_loader_texture_logluv.ts deleted file mode 100644 index 7f3fbd4a0..000000000 --- a/examples-testing/examples/webgl_loader_texture_logluv.ts +++ /dev/null @@ -1,75 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { LogLuvLoader } from 'three/addons/loaders/LogLuvLoader.js'; - -const params = { - exposure: 2.0, -}; - -let renderer, scene, camera; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - renderer.toneMapping = THREE.ReinhardToneMapping; - renderer.toneMappingExposure = params.exposure; - - scene = new THREE.Scene(); - - const aspect = window.innerWidth / window.innerHeight; - - camera = new THREE.OrthographicCamera(-aspect, aspect, 1, -1, 0, 1); - - new LogLuvLoader().load('textures/memorial.tif', function (texture) { - const material = new THREE.MeshBasicMaterial({ map: texture }); - - const quad = new THREE.PlaneGeometry(1, 1.5); - - const mesh = new THREE.Mesh(quad, material); - - scene.add(mesh); - - render(); - }); - - // - - const gui = new GUI(); - - gui.add(params, 'exposure', 0, 4, 0.01).onChange(render); - gui.open(); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const aspect = window.innerWidth / window.innerHeight; - - const frustumHeight = camera.top - camera.bottom; - - camera.left = (-frustumHeight * aspect) / 2; - camera.right = (frustumHeight * aspect) / 2; - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -// - -function render() { - renderer.toneMappingExposure = params.exposure; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_texture_rgbm.ts b/examples-testing/examples/webgl_loader_texture_rgbm.ts deleted file mode 100644 index a882cdbc5..000000000 --- a/examples-testing/examples/webgl_loader_texture_rgbm.ts +++ /dev/null @@ -1,75 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { RGBMLoader } from 'three/addons/loaders/RGBMLoader.js'; - -const params = { - exposure: 2.0, -}; - -let renderer, scene, camera; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - renderer.toneMapping = THREE.ReinhardToneMapping; - renderer.toneMappingExposure = params.exposure; - - scene = new THREE.Scene(); - - const aspect = window.innerWidth / window.innerHeight; - - camera = new THREE.OrthographicCamera(-aspect, aspect, 1, -1, 0, 1); - - new RGBMLoader().setMaxRange(16).load('textures/memorial.png', function (texture) { - const material = new THREE.MeshBasicMaterial({ map: texture }); - - const quad = new THREE.PlaneGeometry(1, 1.5); - - const mesh = new THREE.Mesh(quad, material); - - scene.add(mesh); - - render(); - }); - - // - - const gui = new GUI(); - - gui.add(params, 'exposure', 0, 4, 0.01).onChange(render); - gui.open(); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const aspect = window.innerWidth / window.innerHeight; - - const frustumHeight = camera.top - camera.bottom; - - camera.left = (-frustumHeight * aspect) / 2; - camera.right = (frustumHeight * aspect) / 2; - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -// - -function render() { - renderer.toneMappingExposure = params.exposure; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_texture_tga.ts b/examples-testing/examples/webgl_loader_texture_tga.ts deleted file mode 100644 index c4f65b79a..000000000 --- a/examples-testing/examples/webgl_loader_texture_tga.ts +++ /dev/null @@ -1,90 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { TGALoader } from 'three/addons/loaders/TGALoader.js'; - -let camera, scene, renderer, stats; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(0, 1, 5); - - scene = new THREE.Scene(); - - // - - const loader = new TGALoader(); - const geometry = new THREE.BoxGeometry(); - - // add box 1 - grey8 texture - - const texture1 = loader.load('textures/crate_grey8.tga'); - texture1.colorSpace = THREE.SRGBColorSpace; - const material1 = new THREE.MeshPhongMaterial({ color: 0xffffff, map: texture1 }); - - const mesh1 = new THREE.Mesh(geometry, material1); - mesh1.position.x = -1; - - scene.add(mesh1); - - // add box 2 - tga texture - - const texture2 = loader.load('textures/crate_color8.tga'); - texture2.colorSpace = THREE.SRGBColorSpace; - const material2 = new THREE.MeshPhongMaterial({ color: 0xffffff, map: texture2 }); - - const mesh2 = new THREE.Mesh(geometry, material2); - mesh2.position.x = 1; - - scene.add(mesh2); - - // - - const ambientLight = new THREE.AmbientLight(0xffffff, 1.5); - scene.add(ambientLight); - - const light = new THREE.DirectionalLight(0xffffff, 2.5); - light.position.set(1, 1, 1); - scene.add(light); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.enableZoom = false; - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); - stats.update(); -} diff --git a/examples-testing/examples/webgl_loader_texture_tiff.ts b/examples-testing/examples/webgl_loader_texture_tiff.ts deleted file mode 100644 index f097774aa..000000000 --- a/examples-testing/examples/webgl_loader_texture_tiff.ts +++ /dev/null @@ -1,87 +0,0 @@ -import * as THREE from 'three'; - -import { TIFFLoader } from 'three/addons/loaders/TIFFLoader.js'; - -let renderer, scene, camera; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 10); - camera.position.set(0, 0, 4); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - const loader = new TIFFLoader(); - - const geometry = new THREE.PlaneGeometry(); - - // uncompressed - - loader.load('textures/tiff/crate_uncompressed.tif', function (texture) { - texture.colorSpace = THREE.SRGBColorSpace; - - const material = new THREE.MeshBasicMaterial({ map: texture }); - - const mesh = new THREE.Mesh(geometry, material); - mesh.position.set(-1.5, 0, 0); - - scene.add(mesh); - - render(); - }); - - // LZW - - loader.load('textures/tiff/crate_lzw.tif', function (texture) { - texture.colorSpace = THREE.SRGBColorSpace; - - const material = new THREE.MeshBasicMaterial({ map: texture }); - - const mesh = new THREE.Mesh(geometry, material); - mesh.position.set(0, 0, 0); - - scene.add(mesh); - - render(); - }); - - // JPEG - - loader.load('textures/tiff/crate_jpeg.tif', function (texture) { - texture.colorSpace = THREE.SRGBColorSpace; - - const material = new THREE.MeshBasicMaterial({ map: texture }); - - const mesh = new THREE.Mesh(geometry, material); - mesh.position.set(1.5, 0, 0); - - scene.add(mesh); - - render(); - }); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -// - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_texture_ultrahdr.ts b/examples-testing/examples/webgl_loader_texture_ultrahdr.ts deleted file mode 100644 index c8bce4bf9..000000000 --- a/examples-testing/examples/webgl_loader_texture_ultrahdr.ts +++ /dev/null @@ -1,101 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -const params = { - autoRotate: true, - metalness: 1.0, - roughness: 0.0, - exposure: 1.0, - resolution: '2k', - type: 'HalfFloatType', -}; - -let renderer, scene, camera, controls, torusMesh, loader; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = params.exposure; - - renderer.setAnimationLoop(render); - - scene = new THREE.Scene(); - - torusMesh = new THREE.Mesh( - new THREE.TorusKnotGeometry(1, 0.4, 128, 128, 1, 3), - new THREE.MeshStandardMaterial({ roughness: params.roughness, metalness: params.metalness }), - ); - scene.add(torusMesh); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 500); - camera.position.set(0.0, 0.0, -6.0); - - controls = new OrbitControls(camera, renderer.domElement); - - loader = new UltraHDRLoader(); - loader.setDataType(THREE.FloatType); - - const loadEnvironment = function (resolution = '2k', type = 'HalfFloatType') { - loader.setDataType(THREE[type]); - - loader.load(`textures/equirectangular/spruit_sunrise_${resolution}.hdr.jpg`, function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - texture.needsUpdate = true; - - scene.background = texture; - scene.environment = texture; - }); - }; - - loadEnvironment(params.resolution, params.type); - - const gui = new GUI(); - - gui.add(params, 'autoRotate'); - gui.add(params, 'metalness', 0, 1, 0.01); - gui.add(params, 'roughness', 0, 1, 0.01); - gui.add(params, 'exposure', 0, 4, 0.01); - gui.add(params, 'resolution', ['2k', '4k']).onChange(value => { - loadEnvironment(value, params.type); - }); - gui.add(params, 'type', ['HalfFloatType', 'FloatType']).onChange(value => { - loadEnvironment(params.resolution, value); - }); - - gui.open(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function render() { - torusMesh.material.roughness = params.roughness; - torusMesh.material.metalness = params.metalness; - - if (params.autoRotate) { - torusMesh.rotation.y += 0.005; - } - - renderer.toneMappingExposure = params.exposure; - - controls.update(); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_tilt.ts b/examples-testing/examples/webgl_loader_tilt.ts deleted file mode 100644 index 2a583c2b0..000000000 --- a/examples-testing/examples/webgl_loader_tilt.ts +++ /dev/null @@ -1,54 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { TiltLoader } from 'three/addons/loaders/TiltLoader.js'; - -let camera, scene, renderer; - -init(); - -function init() { - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 500); - - camera.position.y = 43; - camera.position.z = 100; - - scene.add(camera); - - const grid = new THREE.GridHelper(50, 50, 0xffffff, 0x555555); - scene.add(grid); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - const loader = new TiltLoader(); - loader.load('./models/tilt/BRUSH_DOME.tilt', function (object) { - // console.log( object.children.length ); - scene.add(object); - render(); - }); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); - controls.target.y = camera.position.y; - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_ttf.ts b/examples-testing/examples/webgl_loader_ttf.ts deleted file mode 100644 index 168371a14..000000000 --- a/examples-testing/examples/webgl_loader_ttf.ts +++ /dev/null @@ -1,231 +0,0 @@ -import * as THREE from 'three'; - -import { TTFLoader } from 'three/addons/loaders/TTFLoader.js'; -import { Font } from 'three/addons/loaders/FontLoader.js'; -import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; - -let container; -let camera, cameraTarget, scene, renderer; -let group, textMesh1, textMesh2, textGeo, material; -let firstLetter = true; - -let text = 'three.js'; -const depth = 20, - size = 70, - hover = 30, - curveSegments = 4, - bevelThickness = 2, - bevelSize = 1.5; - -let font = null; -const mirror = true; - -let targetRotation = 0; -let targetRotationOnPointerDown = 0; - -let pointerX = 0; -let pointerXOnPointerDown = 0; - -let windowHalfX = window.innerWidth / 2; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - // CAMERA - - camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 1500); - camera.position.set(0, 400, 700); - - cameraTarget = new THREE.Vector3(0, 150, 0); - - // SCENE - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x000000); - scene.fog = new THREE.Fog(0x000000, 250, 1400); - - // LIGHTS - - const dirLight1 = new THREE.DirectionalLight(0xffffff, 0.4); - dirLight1.position.set(0, 0, 1).normalize(); - scene.add(dirLight1); - - const dirLight2 = new THREE.DirectionalLight(0xffffff, 2); - dirLight2.position.set(0, hover, 10).normalize(); - dirLight2.color.setHSL(Math.random(), 1, 0.5, THREE.SRGBColorSpace); - scene.add(dirLight2); - - material = new THREE.MeshPhongMaterial({ color: 0xffffff, flatShading: true }); - - group = new THREE.Group(); - group.position.y = 100; - - scene.add(group); - - const loader = new TTFLoader(); - - loader.load('fonts/ttf/kenpixel.ttf', function (json) { - font = new Font(json); - createText(); - }); - - const plane = new THREE.Mesh( - new THREE.PlaneGeometry(10000, 10000), - new THREE.MeshBasicMaterial({ color: 0xffffff, opacity: 0.5, transparent: true }), - ); - plane.position.y = 100; - plane.rotation.x = -Math.PI / 2; - scene.add(plane); - - // RENDERER - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // EVENTS - - container.style.touchAction = 'none'; - container.addEventListener('pointerdown', onPointerDown); - - document.addEventListener('keypress', onDocumentKeyPress); - document.addEventListener('keydown', onDocumentKeyDown); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onDocumentKeyDown(event) { - if (firstLetter) { - firstLetter = false; - text = ''; - } - - const keyCode = event.keyCode; - - // backspace - - if (keyCode === 8) { - event.preventDefault(); - - text = text.substring(0, text.length - 1); - refreshText(); - - return false; - } -} - -function onDocumentKeyPress(event) { - const keyCode = event.which; - - // backspace - - if (keyCode === 8) { - event.preventDefault(); - } else { - const ch = String.fromCharCode(keyCode); - text += ch; - - refreshText(); - } -} - -function createText() { - textGeo = new TextGeometry(text, { - font: font, - - size: size, - depth: depth, - curveSegments: curveSegments, - - bevelThickness: bevelThickness, - bevelSize: bevelSize, - bevelEnabled: true, - }); - - textGeo.computeBoundingBox(); - textGeo.computeVertexNormals(); - - const centerOffset = -0.5 * (textGeo.boundingBox.max.x - textGeo.boundingBox.min.x); - - textMesh1 = new THREE.Mesh(textGeo, material); - - textMesh1.position.x = centerOffset; - textMesh1.position.y = hover; - textMesh1.position.z = 0; - - textMesh1.rotation.x = 0; - textMesh1.rotation.y = Math.PI * 2; - - group.add(textMesh1); - - if (mirror) { - textMesh2 = new THREE.Mesh(textGeo, material); - - textMesh2.position.x = centerOffset; - textMesh2.position.y = -hover; - textMesh2.position.z = depth; - - textMesh2.rotation.x = Math.PI; - textMesh2.rotation.y = Math.PI * 2; - - group.add(textMesh2); - } -} - -function refreshText() { - group.remove(textMesh1); - if (mirror) group.remove(textMesh2); - - if (!text) return; - - createText(); -} - -function onPointerDown(event) { - if (event.isPrimary === false) return; - - pointerXOnPointerDown = event.clientX - windowHalfX; - targetRotationOnPointerDown = targetRotation; - - document.addEventListener('pointermove', onPointerMove); - document.addEventListener('pointerup', onPointerUp); -} - -function onPointerMove(event) { - if (event.isPrimary === false) return; - - pointerX = event.clientX - windowHalfX; - - targetRotation = targetRotationOnPointerDown + (pointerX - pointerXOnPointerDown) * 0.02; -} - -function onPointerUp() { - if (event.isPrimary === false) return; - - document.removeEventListener('pointermove', onPointerMove); - document.removeEventListener('pointerup', onPointerUp); -} - -// - -function animate() { - group.rotation.y += (targetRotation - group.rotation.y) * 0.05; - - camera.lookAt(cameraTarget); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_usdz.ts b/examples-testing/examples/webgl_loader_usdz.ts deleted file mode 100644 index d75823d88..000000000 --- a/examples-testing/examples/webgl_loader_usdz.ts +++ /dev/null @@ -1,68 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; -import { USDZLoader } from 'three/addons/loaders/USDZLoader.js'; - -let camera, scene, renderer; - -init(); - -async function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(0, 0.75, -1.5); - - scene = new THREE.Scene(); - - const rgbeLoader = new RGBELoader().setPath('textures/equirectangular/'); - - const usdzLoader = new USDZLoader().setPath('models/usdz/'); - - const [texture, model] = await Promise.all([ - rgbeLoader.loadAsync('venice_sunset_1k.hdr'), - usdzLoader.loadAsync('saeukkang.usdz'), - ]); - - // environment - - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - scene.backgroundBlurriness = 0.5; - scene.environment = texture; - - // model - - model.position.y = 0.25; - model.position.z = -0.25; - scene.add(model); - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 2.0; - document.body.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 1; - controls.maxDistance = 8; - // controls.target.y = 15; - // controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_vox.ts b/examples-testing/examples/webgl_loader_vox.ts deleted file mode 100644 index 061848012..000000000 --- a/examples-testing/examples/webgl_loader_vox.ts +++ /dev/null @@ -1,104 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { VOXLoader, VOXMesh } from 'three/addons/loaders/VOXLoader.js'; - -let camera, controls, scene, renderer; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.01, 10); - camera.position.set(0.175, 0.075, 0.175); - - scene = new THREE.Scene(); - scene.add(camera); - - // light - - const hemiLight = new THREE.HemisphereLight(0xcccccc, 0x444444, 3); - scene.add(hemiLight); - - const dirLight = new THREE.DirectionalLight(0xffffff, 2.5); - dirLight.position.set(1.5, 3, 2.5); - scene.add(dirLight); - - const dirLight2 = new THREE.DirectionalLight(0xffffff, 1.5); - dirLight2.position.set(-1.5, -3, -2.5); - scene.add(dirLight2); - - const loader = new VOXLoader(); - loader.load('models/vox/monu10.vox', function (chunks) { - for (let i = 0; i < chunks.length; i++) { - const chunk = chunks[i]; - - // displayPalette( chunk.palette ); - - const mesh = new VOXMesh(chunk); - mesh.scale.setScalar(0.0015); - scene.add(mesh); - } - }); - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // controls - - controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 0.1; - controls.maxDistance = 0.5; - - // - - window.addEventListener('resize', onWindowResize); -} - -/* - function displayPalette( palette ) { - - const canvas = document.createElement( 'canvas' ); - canvas.width = 8; - canvas.height = 32; - canvas.style.position = 'absolute'; - canvas.style.top = '0'; - canvas.style.width = '100px'; - canvas.style.imageRendering = 'pixelated'; - document.body.appendChild( canvas ); - - const context = canvas.getContext( '2d' ); - - for ( let c = 0; c < 256; c ++ ) { - - const x = c % 8; - const y = Math.floor( c / 8 ); - - const hex = palette[ c + 1 ]; - const r = hex >> 0 & 0xff; - const g = hex >> 8 & 0xff; - const b = hex >> 16 & 0xff; - context.fillStyle = `rgba(${r},${g},${b},1)`; - context.fillRect( x, 31 - y, 1, 1 ); - - } - - } - */ - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - controls.update(); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_loader_vrml.ts b/examples-testing/examples/webgl_loader_vrml.ts deleted file mode 100644 index fecf4bb45..000000000 --- a/examples-testing/examples/webgl_loader_vrml.ts +++ /dev/null @@ -1,118 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { VRMLLoader } from 'three/addons/loaders/VRMLLoader.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, stats, controls, loader; - -const params = { - asset: 'house', -}; - -const assets = [ - 'creaseAngle', - 'crystal', - 'house', - 'elevationGrid1', - 'elevationGrid2', - 'extrusion1', - 'extrusion2', - 'extrusion3', - 'lines', - 'linesTransparent', - 'meshWithLines', - 'meshWithTexture', - 'pixelTexture', - 'points', -]; - -let vrmlScene; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1e10); - camera.position.set(-10, 5, 10); - - scene = new THREE.Scene(); - scene.add(camera); - - // light - - const ambientLight = new THREE.AmbientLight(0xffffff, 1.2); - scene.add(ambientLight); - - const dirLight = new THREE.DirectionalLight(0xffffff, 2.0); - dirLight.position.set(200, 200, 200); - scene.add(dirLight); - - loader = new VRMLLoader(); - loadAsset(params.asset); - - // renderer - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // controls - - controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 1; - controls.maxDistance = 200; - controls.enableDamping = true; - - // - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); - - // - - const gui = new GUI(); - gui.add(params, 'asset', assets).onChange(function (value) { - if (vrmlScene) { - vrmlScene.traverse(function (object) { - if (object.material) object.material.dispose(); - if (object.material && object.material.map) object.material.map.dispose(); - if (object.geometry) object.geometry.dispose(); - }); - - scene.remove(vrmlScene); - } - - loadAsset(value); - }); -} - -function loadAsset(asset) { - loader.load('models/vrml/' + asset + '.wrl', function (object) { - vrmlScene = object; - scene.add(object); - controls.reset(); - }); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - controls.update(); // to support damping - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_loader_vtk.ts b/examples-testing/examples/webgl_loader_vtk.ts deleted file mode 100644 index dfc798657..000000000 --- a/examples-testing/examples/webgl_loader_vtk.ts +++ /dev/null @@ -1,123 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; -import { VTKLoader } from 'three/addons/loaders/VTKLoader.js'; - -let stats; - -let camera, controls, scene, renderer; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 100); - camera.position.z = 0.2; - - scene = new THREE.Scene(); - - scene.add(camera); - - // light - - const hemiLight = new THREE.HemisphereLight(0xffffff, 0x000000, 3); - scene.add(hemiLight); - - const dirLight = new THREE.DirectionalLight(0xffffff, 1.5); - dirLight.position.set(2, 2, 2); - scene.add(dirLight); - - const loader = new VTKLoader(); - loader.load('models/vtk/bunny.vtk', function (geometry) { - geometry.center(); - geometry.computeVertexNormals(); - - const material = new THREE.MeshLambertMaterial({ color: 0xffffff }); - const mesh = new THREE.Mesh(geometry, material); - mesh.position.set(-0.075, 0.005, 0); - mesh.scale.multiplyScalar(0.2); - scene.add(mesh); - }); - - const loader1 = new VTKLoader(); - loader1.load('models/vtk/cube_ascii.vtp', function (geometry) { - geometry.computeVertexNormals(); - geometry.center(); - - const material = new THREE.MeshLambertMaterial({ color: 0x00ff00 }); - const mesh = new THREE.Mesh(geometry, material); - - mesh.position.set(-0.025, 0, 0); - mesh.scale.multiplyScalar(0.01); - - scene.add(mesh); - }); - - const loader2 = new VTKLoader(); - loader2.load('models/vtk/cube_binary.vtp', function (geometry) { - geometry.computeVertexNormals(); - geometry.center(); - - const material = new THREE.MeshLambertMaterial({ color: 0x0000ff }); - const mesh = new THREE.Mesh(geometry, material); - - mesh.position.set(0.025, 0, 0); - mesh.scale.multiplyScalar(0.01); - - scene.add(mesh); - }); - - const loader3 = new VTKLoader(); - loader3.load('models/vtk/cube_no_compression.vtp', function (geometry) { - geometry.computeVertexNormals(); - geometry.center(); - - const material = new THREE.MeshLambertMaterial({ color: 0xff0000 }); - const mesh = new THREE.Mesh(geometry, material); - - mesh.position.set(0.075, 0, 0); - mesh.scale.multiplyScalar(0.01); - - scene.add(mesh); - }); - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // controls - - controls = new TrackballControls(camera, renderer.domElement); - controls.minDistance = 0.1; - controls.maxDistance = 0.5; - controls.rotateSpeed = 5.0; - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - controls.handleResize(); -} - -function animate() { - controls.update(); - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_loader_xyz.ts b/examples-testing/examples/webgl_loader_xyz.ts deleted file mode 100644 index 90e009840..000000000 --- a/examples-testing/examples/webgl_loader_xyz.ts +++ /dev/null @@ -1,62 +0,0 @@ -import * as THREE from 'three'; - -import { XYZLoader } from 'three/addons/loaders/XYZLoader.js'; - -let camera, scene, renderer, clock; - -let points; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(10, 7, 10); - - scene = new THREE.Scene(); - scene.add(camera); - camera.lookAt(scene.position); - - clock = new THREE.Clock(); - - const loader = new XYZLoader(); - loader.load('models/xyz/helix_201.xyz', function (geometry) { - geometry.center(); - - const vertexColors = geometry.hasAttribute('color') === true; - - const material = new THREE.PointsMaterial({ size: 0.1, vertexColors: vertexColors }); - - points = new THREE.Points(geometry, material); - scene.add(points); - }); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const delta = clock.getDelta(); - - if (points) { - points.rotation.x += delta * 0.2; - points.rotation.y += delta * 0.5; - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_lod.ts b/examples-testing/examples/webgl_lod.ts deleted file mode 100644 index 0bb9e7be0..000000000 --- a/examples-testing/examples/webgl_lod.ts +++ /dev/null @@ -1,88 +0,0 @@ -import * as THREE from 'three'; - -import { FlyControls } from 'three/addons/controls/FlyControls.js'; - -let container; - -let camera, scene, renderer, controls; - -const clock = new THREE.Clock(); - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 15000); - camera.position.z = 1000; - - scene = new THREE.Scene(); - scene.fog = new THREE.Fog(0x000000, 1, 15000); - - const pointLight = new THREE.PointLight(0xff2200, 3, 0, 0); - pointLight.position.set(0, 0, 0); - scene.add(pointLight); - - const dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.position.set(0, 0, 1).normalize(); - scene.add(dirLight); - - const geometry = [ - [new THREE.IcosahedronGeometry(100, 16), 50], - [new THREE.IcosahedronGeometry(100, 8), 300], - [new THREE.IcosahedronGeometry(100, 4), 1000], - [new THREE.IcosahedronGeometry(100, 2), 2000], - [new THREE.IcosahedronGeometry(100, 1), 8000], - ]; - - const material = new THREE.MeshLambertMaterial({ color: 0xffffff, wireframe: true }); - - for (let j = 0; j < 1000; j++) { - const lod = new THREE.LOD(); - - for (let i = 0; i < geometry.length; i++) { - const mesh = new THREE.Mesh(geometry[i][0], material); - mesh.scale.set(1.5, 1.5, 1.5); - mesh.updateMatrix(); - mesh.matrixAutoUpdate = false; - lod.addLevel(mesh, geometry[i][1]); - } - - lod.position.x = 10000 * (0.5 - Math.random()); - lod.position.y = 7500 * (0.5 - Math.random()); - lod.position.z = 10000 * (0.5 - Math.random()); - lod.updateMatrix(); - lod.matrixAutoUpdate = false; - scene.add(lod); - } - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - controls = new FlyControls(camera, renderer.domElement); - controls.movementSpeed = 1000; - controls.rollSpeed = Math.PI / 10; - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - controls.update(clock.getDelta()); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_marchingcubes.ts b/examples-testing/examples/webgl_marchingcubes.ts deleted file mode 100644 index d11df56a4..000000000 --- a/examples-testing/examples/webgl_marchingcubes.ts +++ /dev/null @@ -1,311 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { MarchingCubes } from 'three/addons/objects/MarchingCubes.js'; -import { ToonShader1, ToonShader2, ToonShaderHatching, ToonShaderDotted } from 'three/addons/shaders/ToonShader.js'; - -let container, stats; - -let camera, scene, renderer; - -let materials, current_material; - -let light, pointLight, ambientLight; - -let effect, resolution; - -let effectController; - -let time = 0; - -const clock = new THREE.Clock(); - -init(); - -function init() { - container = document.getElementById('container'); - - // CAMERA - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.set(-500, 500, 1500); - - // SCENE - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x050505); - - // LIGHTS - - light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0.5, 0.5, 1); - scene.add(light); - - pointLight = new THREE.PointLight(0xff7c00, 3, 0, 0); - pointLight.position.set(0, 0, 100); - scene.add(pointLight); - - ambientLight = new THREE.AmbientLight(0x323232, 3); - scene.add(ambientLight); - - // MATERIALS - - materials = generateMaterials(); - current_material = 'shiny'; - - // MARCHING CUBES - - resolution = 28; - - effect = new MarchingCubes(resolution, materials[current_material], true, true, 100000); - effect.position.set(0, 0, 0); - effect.scale.set(700, 700, 700); - - effect.enableUvs = false; - effect.enableColors = false; - - scene.add(effect); - - // RENDERER - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // CONTROLS - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 500; - controls.maxDistance = 5000; - - // STATS - - stats = new Stats(); - container.appendChild(stats.dom); - - // GUI - - setupGui(); - - // EVENTS - - window.addEventListener('resize', onWindowResize); -} - -// - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function generateMaterials() { - // environment map - - const path = 'textures/cube/SwedishRoyalCastle/'; - const format = '.jpg'; - const urls = [ - path + 'px' + format, - path + 'nx' + format, - path + 'py' + format, - path + 'ny' + format, - path + 'pz' + format, - path + 'nz' + format, - ]; - - const cubeTextureLoader = new THREE.CubeTextureLoader(); - - const reflectionCube = cubeTextureLoader.load(urls); - const refractionCube = cubeTextureLoader.load(urls); - refractionCube.mapping = THREE.CubeRefractionMapping; - - // toons - - const toonMaterial1 = createShaderMaterial(ToonShader1, light, ambientLight); - const toonMaterial2 = createShaderMaterial(ToonShader2, light, ambientLight); - const hatchingMaterial = createShaderMaterial(ToonShaderHatching, light, ambientLight); - const dottedMaterial = createShaderMaterial(ToonShaderDotted, light, ambientLight); - - const texture = new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg'); - texture.wrapS = THREE.RepeatWrapping; - texture.wrapT = THREE.RepeatWrapping; - texture.colorSpace = THREE.SRGBColorSpace; - - const materials = { - shiny: new THREE.MeshStandardMaterial({ - color: 0x9c0000, - envMap: reflectionCube, - roughness: 0.1, - metalness: 1.0, - }), - chrome: new THREE.MeshLambertMaterial({ color: 0xffffff, envMap: reflectionCube }), - liquid: new THREE.MeshLambertMaterial({ color: 0xffffff, envMap: refractionCube, refractionRatio: 0.85 }), - matte: new THREE.MeshPhongMaterial({ specular: 0x494949, shininess: 1 }), - flat: new THREE.MeshLambertMaterial({ - /*TODO flatShading: true */ - }), - textured: new THREE.MeshPhongMaterial({ color: 0xffffff, specular: 0x111111, shininess: 1, map: texture }), - colors: new THREE.MeshPhongMaterial({ color: 0xffffff, specular: 0xffffff, shininess: 2, vertexColors: true }), - multiColors: new THREE.MeshPhongMaterial({ shininess: 2, vertexColors: true }), - plastic: new THREE.MeshPhongMaterial({ specular: 0xc1c1c1, shininess: 250 }), - toon1: toonMaterial1, - toon2: toonMaterial2, - hatching: hatchingMaterial, - dotted: dottedMaterial, - }; - - return materials; -} - -function createShaderMaterial(shader, light, ambientLight) { - const u = THREE.UniformsUtils.clone(shader.uniforms); - - const vs = shader.vertexShader; - const fs = shader.fragmentShader; - - const material = new THREE.ShaderMaterial({ uniforms: u, vertexShader: vs, fragmentShader: fs }); - - material.uniforms['uDirLightPos'].value = light.position; - material.uniforms['uDirLightColor'].value = light.color; - - material.uniforms['uAmbientLightColor'].value = ambientLight.color; - - return material; -} - -// - -function setupGui() { - const createHandler = function (id) { - return function () { - current_material = id; - - effect.material = materials[id]; - effect.enableUvs = current_material === 'textured' ? true : false; - effect.enableColors = current_material === 'colors' || current_material === 'multiColors' ? true : false; - }; - }; - - effectController = { - material: 'shiny', - - speed: 1.0, - numBlobs: 10, - resolution: 28, - isolation: 80, - - floor: true, - wallx: false, - wallz: false, - - dummy: function () {}, - }; - - let h; - - const gui = new GUI(); - - // material (type) - - h = gui.addFolder('Materials'); - - for (const m in materials) { - effectController[m] = createHandler(m); - h.add(effectController, m).name(m); - } - - // simulation - - h = gui.addFolder('Simulation'); - - h.add(effectController, 'speed', 0.1, 8.0, 0.05); - h.add(effectController, 'numBlobs', 1, 50, 1); - h.add(effectController, 'resolution', 14, 100, 1); - h.add(effectController, 'isolation', 10, 300, 1); - - h.add(effectController, 'floor'); - h.add(effectController, 'wallx'); - h.add(effectController, 'wallz'); -} - -// this controls content of marching cubes voxel field - -function updateCubes(object, time, numblobs, floor, wallx, wallz) { - object.reset(); - - // fill the field with some metaballs - - const rainbow = [ - new THREE.Color(0xff0000), - new THREE.Color(0xffbb00), - new THREE.Color(0xffff00), - new THREE.Color(0x00ff00), - new THREE.Color(0x0000ff), - new THREE.Color(0x9400bd), - new THREE.Color(0xc800eb), - ]; - const subtract = 12; - const strength = 1.2 / ((Math.sqrt(numblobs) - 1) / 4 + 1); - - for (let i = 0; i < numblobs; i++) { - const ballx = Math.sin(i + 1.26 * time * (1.03 + 0.5 * Math.cos(0.21 * i))) * 0.27 + 0.5; - const bally = Math.abs(Math.cos(i + 1.12 * time * Math.cos(1.22 + 0.1424 * i))) * 0.77; // dip into the floor - const ballz = Math.cos(i + 1.32 * time * 0.1 * Math.sin(0.92 + 0.53 * i)) * 0.27 + 0.5; - - if (current_material === 'multiColors') { - object.addBall(ballx, bally, ballz, strength, subtract, rainbow[i % 7]); - } else { - object.addBall(ballx, bally, ballz, strength, subtract); - } - } - - if (floor) object.addPlaneY(2, 12); - if (wallz) object.addPlaneZ(2, 12); - if (wallx) object.addPlaneX(2, 12); - - object.update(); -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - const delta = clock.getDelta(); - - time += delta * effectController.speed * 0.5; - - // marching cubes - - if (effectController.resolution !== resolution) { - resolution = effectController.resolution; - effect.init(Math.floor(resolution)); - } - - if (effectController.isolation !== effect.isolation) { - effect.isolation = effectController.isolation; - } - - updateCubes( - effect, - time, - effectController.numBlobs, - effectController.floor, - effectController.wallx, - effectController.wallz, - ); - - // render - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_alphahash.ts b/examples-testing/examples/webgl_materials_alphahash.ts deleted file mode 100644 index 1ecf95f26..000000000 --- a/examples-testing/examples/webgl_materials_alphahash.ts +++ /dev/null @@ -1,178 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { TAARenderPass } from 'three/addons/postprocessing/TAARenderPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -let camera, scene, renderer, controls, stats, mesh, material; - -let composer, renderPass, taaRenderPass, outputPass; - -let needsUpdate = false; - -const amount = parseInt(window.location.search.slice(1)) || 3; -const count = Math.pow(amount, 3); - -const color = new THREE.Color(); - -const params = { - alpha: 0.5, - alphaHash: true, - taa: true, - sampleLevel: 2, -}; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(amount, amount, amount); - camera.lookAt(0, 0, 0); - - scene = new THREE.Scene(); - - const geometry = new THREE.IcosahedronGeometry(0.5, 3); - - material = new THREE.MeshStandardMaterial({ - color: 0xffffff, - alphaHash: params.alphaHash, - opacity: params.alpha, - }); - - mesh = new THREE.InstancedMesh(geometry, material, count); - - let i = 0; - const offset = (amount - 1) / 2; - - const matrix = new THREE.Matrix4(); - - for (let x = 0; x < amount; x++) { - for (let y = 0; y < amount; y++) { - for (let z = 0; z < amount; z++) { - matrix.setPosition(offset - x, offset - y, offset - z); - - mesh.setMatrixAt(i, matrix); - mesh.setColorAt(i, color.setHex(Math.random() * 0xffffff)); - - i++; - } - } - } - - scene.add(mesh); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - const environment = new RoomEnvironment(); - const pmremGenerator = new THREE.PMREMGenerator(renderer); - - scene.environment = pmremGenerator.fromScene(environment).texture; - environment.dispose(); - - // - - composer = new EffectComposer(renderer); - - renderPass = new RenderPass(scene, camera); - renderPass.enabled = false; - - taaRenderPass = new TAARenderPass(scene, camera); - - outputPass = new OutputPass(); - - composer.addPass(renderPass); - composer.addPass(taaRenderPass); - composer.addPass(outputPass); - - // - - controls = new OrbitControls(camera, renderer.domElement); - controls.enableZoom = false; - controls.enablePan = false; - - controls.addEventListener('change', () => (needsUpdate = true)); - - // - - const gui = new GUI(); - - gui.add(params, 'alpha', 0, 1).onChange(onMaterialUpdate); - gui.add(params, 'alphaHash').onChange(onMaterialUpdate); - - const taaFolder = gui.addFolder('Temporal Anti-Aliasing'); - - taaFolder - .add(params, 'taa') - .name('enabled') - .onChange(() => { - renderPass.enabled = !params.taa; - taaRenderPass.enabled = params.taa; - - sampleLevelCtrl.enable(params.taa); - - needsUpdate = true; - }); - - const sampleLevelCtrl = taaFolder.add(params, 'sampleLevel', 0, 6, 1).onChange(() => (needsUpdate = true)); - - // - - stats = new Stats(); - document.body.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - composer.setSize(window.innerWidth, window.innerHeight); - - needsUpdate = true; -} - -function onMaterialUpdate() { - material.opacity = params.alpha; - material.alphaHash = params.alphaHash; - material.transparent = !params.alphaHash; - material.depthWrite = params.alphaHash; - - material.needsUpdate = true; - needsUpdate = true; -} - -function animate() { - render(); - - stats.update(); -} - -function render() { - if (needsUpdate) { - taaRenderPass.accumulate = false; - taaRenderPass.sampleLevel = 0; - - needsUpdate = false; - } else { - taaRenderPass.accumulate = true; - taaRenderPass.sampleLevel = params.sampleLevel; - } - - composer.render(); -} diff --git a/examples-testing/examples/webgl_materials_blending.ts b/examples-testing/examples/webgl_materials_blending.ts deleted file mode 100644 index 11cc009bc..000000000 --- a/examples-testing/examples/webgl_materials_blending.ts +++ /dev/null @@ -1,147 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer; -let mapBg; - -const textureLoader = new THREE.TextureLoader(); - -init(); - -function init() { - // CAMERA - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 600; - - // SCENE - - scene = new THREE.Scene(); - - // BACKGROUND - - const canvas = document.createElement('canvas'); - const ctx = canvas.getContext('2d'); - canvas.width = canvas.height = 128; - ctx.fillStyle = '#ddd'; - ctx.fillRect(0, 0, 128, 128); - ctx.fillStyle = '#555'; - ctx.fillRect(0, 0, 64, 64); - ctx.fillStyle = '#999'; - ctx.fillRect(32, 32, 32, 32); - ctx.fillStyle = '#555'; - ctx.fillRect(64, 64, 64, 64); - ctx.fillStyle = '#777'; - ctx.fillRect(96, 96, 32, 32); - - mapBg = new THREE.CanvasTexture(canvas); - mapBg.colorSpace = THREE.SRGBColorSpace; - mapBg.wrapS = mapBg.wrapT = THREE.RepeatWrapping; - mapBg.repeat.set(64, 32); - - scene.background = mapBg; - - // OBJECTS - - const blendings = [ - { name: 'No', constant: THREE.NoBlending }, - { name: 'Normal', constant: THREE.NormalBlending }, - { name: 'Additive', constant: THREE.AdditiveBlending }, - { name: 'Subtractive', constant: THREE.SubtractiveBlending }, - { name: 'Multiply', constant: THREE.MultiplyBlending }, - ]; - - const assignSRGB = texture => { - texture.colorSpace = THREE.SRGBColorSpace; - }; - - const map0 = textureLoader.load('textures/uv_grid_opengl.jpg', assignSRGB); - const map1 = textureLoader.load('textures/sprite0.jpg', assignSRGB); - const map2 = textureLoader.load('textures/sprite0.png', assignSRGB); - const map3 = textureLoader.load('textures/lensflare/lensflare0.png', assignSRGB); - const map4 = textureLoader.load('textures/lensflare/lensflare0_alpha.png', assignSRGB); - - const geo1 = new THREE.PlaneGeometry(100, 100); - const geo2 = new THREE.PlaneGeometry(100, 25); - - addImageRow(map0, 300); - addImageRow(map1, 150); - addImageRow(map2, 0); - addImageRow(map3, -150); - addImageRow(map4, -300); - - function addImageRow(map, y) { - for (let i = 0; i < blendings.length; i++) { - const blending = blendings[i]; - - const material = new THREE.MeshBasicMaterial({ map: map }); - material.transparent = true; - material.blending = blending.constant; - - const x = (i - blendings.length / 2) * 110; - const z = 0; - - let mesh = new THREE.Mesh(geo1, material); - mesh.position.set(x, y, z); - scene.add(mesh); - - mesh = new THREE.Mesh(geo2, generateLabelMaterial(blending.name)); - mesh.position.set(x, y - 75, z); - scene.add(mesh); - } - } - - // RENDERER - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // EVENTS - - window.addEventListener('resize', onWindowResize); -} - -// - -function onWindowResize() { - const SCREEN_WIDTH = window.innerWidth; - const SCREEN_HEIGHT = window.innerHeight; - - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - - camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT; - camera.updateProjectionMatrix(); -} - -function generateLabelMaterial(text) { - const canvas = document.createElement('canvas'); - const ctx = canvas.getContext('2d'); - canvas.width = 128; - canvas.height = 32; - - ctx.fillStyle = 'rgba( 0, 0, 0, 0.95 )'; - ctx.fillRect(0, 0, 128, 32); - - ctx.fillStyle = 'white'; - ctx.font = 'bold 12pt arial'; - ctx.fillText(text, 10, 22); - - const map = new THREE.CanvasTexture(canvas); - map.colorSpace = THREE.SRGBColorSpace; - - const material = new THREE.MeshBasicMaterial({ map: map, transparent: true }); - - return material; -} - -function animate() { - const time = Date.now() * 0.00025; - const ox = (time * -0.01 * mapBg.repeat.x) % 1; - const oy = (time * -0.01 * mapBg.repeat.y) % 1; - - mapBg.offset.set(ox, oy); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_blending_custom.ts b/examples-testing/examples/webgl_materials_blending_custom.ts deleted file mode 100644 index 072447426..000000000 --- a/examples-testing/examples/webgl_materials_blending_custom.ts +++ /dev/null @@ -1,214 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer; - -let mapBg; -const materials = []; - -const params = { - blendEquation: THREE.AddEquation, -}; - -const equations = { - Add: THREE.AddEquation, - Subtract: THREE.SubtractEquation, - ReverseSubtract: THREE.ReverseSubtractEquation, - Min: THREE.MinEquation, - Max: THREE.MaxEquation, -}; - -init(); - -function init() { - // CAMERA - - camera = new THREE.PerspectiveCamera(80, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 700; - - // SCENE - - scene = new THREE.Scene(); - - // BACKGROUND - - const canvas = document.createElement('canvas'); - const ctx = canvas.getContext('2d'); - canvas.width = canvas.height = 128; - ctx.fillStyle = '#ddd'; - ctx.fillRect(0, 0, 128, 128); - ctx.fillStyle = '#555'; - ctx.fillRect(0, 0, 64, 64); - ctx.fillStyle = '#999'; - ctx.fillRect(32, 32, 32, 32); - ctx.fillStyle = '#555'; - ctx.fillRect(64, 64, 64, 64); - ctx.fillStyle = '#777'; - ctx.fillRect(96, 96, 32, 32); - - mapBg = new THREE.CanvasTexture(canvas); - mapBg.colorSpace = THREE.SRGBColorSpace; - mapBg.wrapS = mapBg.wrapT = THREE.RepeatWrapping; - mapBg.repeat.set(64, 32); - - scene.background = mapBg; - - // FOREGROUND OBJECTS - - const src = [ - { name: 'Zero', constant: THREE.ZeroFactor }, - { name: 'One', constant: THREE.OneFactor }, - { name: 'SrcColor', constant: THREE.SrcColorFactor }, - { name: 'OneMinusSrcColor', constant: THREE.OneMinusSrcColorFactor }, - { name: 'SrcAlpha', constant: THREE.SrcAlphaFactor }, - { name: 'OneMinusSrcAlpha', constant: THREE.OneMinusSrcAlphaFactor }, - { name: 'DstAlpha', constant: THREE.DstAlphaFactor }, - { name: 'OneMinusDstAlpha', constant: THREE.OneMinusDstAlphaFactor }, - { name: 'DstColor', constant: THREE.DstColorFactor }, - { name: 'OneMinusDstColor', constant: THREE.OneMinusDstColorFactor }, - { name: 'SrcAlphaSaturate', constant: THREE.SrcAlphaSaturateFactor }, - ]; - - const dst = [ - { name: 'Zero', constant: THREE.ZeroFactor }, - { name: 'One', constant: THREE.OneFactor }, - { name: 'SrcColor', constant: THREE.SrcColorFactor }, - { name: 'OneMinusSrcColor', constant: THREE.OneMinusSrcColorFactor }, - { name: 'SrcAlpha', constant: THREE.SrcAlphaFactor }, - { name: 'OneMinusSrcAlpha', constant: THREE.OneMinusSrcAlphaFactor }, - { name: 'DstAlpha', constant: THREE.DstAlphaFactor }, - { name: 'OneMinusDstAlpha', constant: THREE.OneMinusDstAlphaFactor }, - { name: 'DstColor', constant: THREE.DstColorFactor }, - { name: 'OneMinusDstColor', constant: THREE.OneMinusDstColorFactor }, - ]; - - const geo1 = new THREE.PlaneGeometry(100, 100); - const geo2 = new THREE.PlaneGeometry(100, 25); - - const texture = new THREE.TextureLoader().load('textures/lensflare/lensflare0_alpha.png'); - texture.colorSpace = THREE.SRGBColorSpace; - - for (let i = 0; i < dst.length; i++) { - const blendDst = dst[i]; - - for (let j = 0; j < src.length; j++) { - const blendSrc = src[j]; - - const material = new THREE.MeshBasicMaterial({ map: texture }); - material.transparent = true; - - material.blending = THREE.CustomBlending; - material.blendSrc = blendSrc.constant; - material.blendDst = blendDst.constant; - material.blendEquation = THREE.AddEquation; - - const x = (j - src.length / 2) * 110; - const z = 0; - const y = (i - dst.length / 2) * 110 + 50; - - const mesh = new THREE.Mesh(geo1, material); - mesh.position.set(x, -y, z); - mesh.matrixAutoUpdate = false; - mesh.updateMatrix(); - scene.add(mesh); - - materials.push(material); - } - } - - for (let j = 0; j < src.length; j++) { - const blendSrc = src[j]; - - const x = (j - src.length / 2) * 110; - const z = 0; - const y = (0 - dst.length / 2) * 110 + 50; - - const mesh = new THREE.Mesh(geo2, generateLabelMaterial(blendSrc.name, 'rgba( 0, 150, 0, 1 )')); - mesh.position.set(x, -(y - 70), z); - mesh.matrixAutoUpdate = false; - mesh.updateMatrix(); - scene.add(mesh); - } - - for (let i = 0; i < dst.length; i++) { - const blendDst = dst[i]; - - const x = (0 - src.length / 2) * 110 - 125; - const z = 0; - const y = (i - dst.length / 2) * 110 + 165; - - const mesh = new THREE.Mesh(geo2, generateLabelMaterial(blendDst.name, 'rgba( 150, 0, 0, 1 )')); - mesh.position.set(x, -(y - 120), z); - mesh.matrixAutoUpdate = false; - mesh.updateMatrix(); - scene.add(mesh); - } - - // RENDERER - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // EVENTS - - window.addEventListener('resize', onWindowResize); - - // GUI - - // - const gui = new GUI({ width: 300 }); - - gui.add(params, 'blendEquation', equations).onChange(updateBlendEquation); - gui.open(); -} - -// - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); -} - -// - -function generateLabelMaterial(text, bg) { - const canvas = document.createElement('canvas'); - const ctx = canvas.getContext('2d'); - canvas.width = 128; - canvas.height = 32; - - ctx.fillStyle = bg; - ctx.fillRect(0, 0, 128, 32); - - ctx.fillStyle = 'white'; - ctx.font = 'bold 11pt arial'; - ctx.fillText(text, 8, 22); - - const map = new THREE.CanvasTexture(canvas); - map.colorSpace = THREE.SRGBColorSpace; - - const material = new THREE.MeshBasicMaterial({ map: map, transparent: true }); - return material; -} - -function updateBlendEquation(value) { - for (const material of materials) { - material.blendEquation = value; - } -} - -function animate() { - const time = Date.now() * 0.00025; - const ox = (time * -0.01 * mapBg.repeat.x) % 1; - const oy = (time * -0.01 * mapBg.repeat.y) % 1; - - mapBg.offset.set(ox, oy); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_bumpmap.ts b/examples-testing/examples/webgl_materials_bumpmap.ts deleted file mode 100644 index d954fab7e..000000000 --- a/examples-testing/examples/webgl_materials_bumpmap.ts +++ /dev/null @@ -1,140 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -let container, stats, loader; - -let camera, scene, renderer; - -let mesh; - -let spotLight; - -let mouseX = 0; -let mouseY = 0; - -let targetX = 0; -let targetY = 0; - -const windowHalfX = window.innerWidth / 2; -const windowHalfY = window.innerHeight / 2; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - // - - camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.z = 12; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x060708); - - // LIGHTS - - scene.add(new THREE.HemisphereLight(0x8d7c7c, 0x494966, 3)); - - spotLight = new THREE.SpotLight(0xffffde, 200); - spotLight.position.set(3.5, 0, 7); - scene.add(spotLight); - - spotLight.castShadow = true; - - spotLight.shadow.mapSize.width = 2048; - spotLight.shadow.mapSize.height = 2048; - - spotLight.shadow.camera.near = 2; - spotLight.shadow.camera.far = 15; - - spotLight.shadow.camera.fov = 40; - - spotLight.shadow.bias = -0.005; - - // - - const mapHeight = new THREE.TextureLoader().load( - 'models/gltf/LeePerrySmith/Infinite-Level_02_Disp_NoSmoothUV-4096.jpg', - ); - - const material = new THREE.MeshPhongMaterial({ - color: 0x9c6e49, - specular: 0x666666, - shininess: 25, - bumpMap: mapHeight, - bumpScale: 10, - }); - - loader = new GLTFLoader(); - loader.load('models/gltf/LeePerrySmith/LeePerrySmith.glb', function (gltf) { - createScene(gltf.scene.children[0].geometry, 1, material); - }); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - renderer.shadowMap.enabled = true; - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // EVENTS - - document.addEventListener('mousemove', onDocumentMouseMove); - window.addEventListener('resize', onWindowResize); -} - -function createScene(geometry, scale, material) { - mesh = new THREE.Mesh(geometry, material); - - mesh.position.y = -0.5; - mesh.scale.set(scale, scale, scale); - - mesh.castShadow = true; - mesh.receiveShadow = true; - - scene.add(mesh); -} - -// - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); -} - -function onDocumentMouseMove(event) { - mouseX = event.clientX - windowHalfX; - mouseY = event.clientY - windowHalfY; -} - -// - -function animate() { - render(); - - stats.update(); -} - -function render() { - targetX = mouseX * 0.001; - targetY = mouseY * 0.001; - - if (mesh) { - mesh.rotation.y += 0.05 * (targetX - mesh.rotation.y); - mesh.rotation.x += 0.05 * (targetY - mesh.rotation.x); - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_car.ts b/examples-testing/examples/webgl_materials_car.ts deleted file mode 100644 index e810f7b7d..000000000 --- a/examples-testing/examples/webgl_materials_car.ts +++ /dev/null @@ -1,167 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -let camera, scene, renderer; -let stats; - -let grid; -let controls; - -const wheels = []; - -function init() { - const container = document.getElementById('container'); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 0.85; - container.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(4.25, 1.4, -4.5); - - controls = new OrbitControls(camera, container); - controls.maxDistance = 9; - controls.maxPolarAngle = THREE.MathUtils.degToRad(90); - controls.target.set(0, 0.5, 0); - controls.update(); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x333333); - scene.environment = new RGBELoader().load('textures/equirectangular/venice_sunset_1k.hdr'); - scene.environment.mapping = THREE.EquirectangularReflectionMapping; - scene.fog = new THREE.Fog(0x333333, 10, 15); - - grid = new THREE.GridHelper(20, 40, 0xffffff, 0xffffff); - grid.material.opacity = 0.2; - grid.material.depthWrite = false; - grid.material.transparent = true; - scene.add(grid); - - // materials - - const bodyMaterial = new THREE.MeshPhysicalMaterial({ - color: 0xff0000, - metalness: 1.0, - roughness: 0.5, - clearcoat: 1.0, - clearcoatRoughness: 0.03, - }); - - const detailsMaterial = new THREE.MeshStandardMaterial({ - color: 0xffffff, - metalness: 1.0, - roughness: 0.5, - }); - - const glassMaterial = new THREE.MeshPhysicalMaterial({ - color: 0xffffff, - metalness: 0.25, - roughness: 0, - transmission: 1.0, - }); - - const bodyColorInput = document.getElementById('body-color'); - bodyColorInput.addEventListener('input', function () { - bodyMaterial.color.set(this.value); - }); - - const detailsColorInput = document.getElementById('details-color'); - detailsColorInput.addEventListener('input', function () { - detailsMaterial.color.set(this.value); - }); - - const glassColorInput = document.getElementById('glass-color'); - glassColorInput.addEventListener('input', function () { - glassMaterial.color.set(this.value); - }); - - // Car - - const shadow = new THREE.TextureLoader().load('models/gltf/ferrari_ao.png'); - - const dracoLoader = new DRACOLoader(); - dracoLoader.setDecoderPath('jsm/libs/draco/gltf/'); - - const loader = new GLTFLoader(); - loader.setDRACOLoader(dracoLoader); - - loader.load('models/gltf/ferrari.glb', function (gltf) { - const carModel = gltf.scene.children[0]; - - carModel.getObjectByName('body').material = bodyMaterial; - - carModel.getObjectByName('rim_fl').material = detailsMaterial; - carModel.getObjectByName('rim_fr').material = detailsMaterial; - carModel.getObjectByName('rim_rr').material = detailsMaterial; - carModel.getObjectByName('rim_rl').material = detailsMaterial; - carModel.getObjectByName('trim').material = detailsMaterial; - - carModel.getObjectByName('glass').material = glassMaterial; - - wheels.push( - carModel.getObjectByName('wheel_fl'), - carModel.getObjectByName('wheel_fr'), - carModel.getObjectByName('wheel_rl'), - carModel.getObjectByName('wheel_rr'), - ); - - // shadow - const mesh = new THREE.Mesh( - new THREE.PlaneGeometry(0.655 * 4, 1.3 * 4), - new THREE.MeshBasicMaterial({ - map: shadow, - blending: THREE.MultiplyBlending, - toneMapped: false, - transparent: true, - }), - ); - mesh.rotation.x = -Math.PI / 2; - mesh.renderOrder = 2; - carModel.add(mesh); - - scene.add(carModel); - }); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - controls.update(); - - const time = -performance.now() / 1000; - - for (let i = 0; i < wheels.length; i++) { - wheels[i].rotation.x = time * Math.PI * 2; - } - - grid.position.z = -time % 1; - - renderer.render(scene, camera); - - stats.update(); -} - -init(); diff --git a/examples-testing/examples/webgl_materials_cubemap.ts b/examples-testing/examples/webgl_materials_cubemap.ts deleted file mode 100644 index 5f2692751..000000000 --- a/examples-testing/examples/webgl_materials_cubemap.ts +++ /dev/null @@ -1,115 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; - -let container, stats; - -let camera, scene, renderer; - -let pointLight; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.z = 13; - - //cubemap - const path = 'textures/cube/SwedishRoyalCastle/'; - const format = '.jpg'; - const urls = [ - path + 'px' + format, - path + 'nx' + format, - path + 'py' + format, - path + 'ny' + format, - path + 'pz' + format, - path + 'nz' + format, - ]; - - const reflectionCube = new THREE.CubeTextureLoader().load(urls); - const refractionCube = new THREE.CubeTextureLoader().load(urls); - refractionCube.mapping = THREE.CubeRefractionMapping; - - scene = new THREE.Scene(); - scene.background = reflectionCube; - - //lights - const ambient = new THREE.AmbientLight(0xffffff, 3); - scene.add(ambient); - - pointLight = new THREE.PointLight(0xffffff, 200); - scene.add(pointLight); - - //materials - const cubeMaterial3 = new THREE.MeshLambertMaterial({ - color: 0xffaa00, - envMap: reflectionCube, - combine: THREE.MixOperation, - reflectivity: 0.3, - }); - const cubeMaterial2 = new THREE.MeshLambertMaterial({ - color: 0xfff700, - envMap: refractionCube, - refractionRatio: 0.95, - }); - const cubeMaterial1 = new THREE.MeshLambertMaterial({ color: 0xffffff, envMap: reflectionCube }); - - //models - const objLoader = new OBJLoader(); - - objLoader.setPath('models/obj/walt/'); - objLoader.load('WaltHead.obj', function (object) { - const head = object.children[0]; - head.scale.setScalar(0.1); - head.position.y = -3; - head.material = cubeMaterial1; - - const head2 = head.clone(); - head2.position.x = -6; - head2.material = cubeMaterial2; - - const head3 = head.clone(); - head3.position.x = 6; - head3.material = cubeMaterial3; - - scene.add(head, head2, head3); - }); - - //renderer - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - //controls - const controls = new OrbitControls(camera, renderer.domElement); - controls.enableZoom = false; - controls.enablePan = false; - controls.minPolarAngle = Math.PI / 4; - controls.maxPolarAngle = Math.PI / 1.5; - - //stats - stats = new Stats(); - container.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); - stats.update(); -} diff --git a/examples-testing/examples/webgl_materials_cubemap_dynamic.ts b/examples-testing/examples/webgl_materials_cubemap_dynamic.ts deleted file mode 100644 index 13a268901..000000000 --- a/examples-testing/examples/webgl_materials_cubemap_dynamic.ts +++ /dev/null @@ -1,115 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import Stats from 'three/addons/libs/stats.module.js'; - -let camera, scene, renderer, stats; -let cube, sphere, torus, material; - -let cubeCamera, cubeRenderTarget; - -let controls; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - document.body.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResized); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 75; - - scene = new THREE.Scene(); - scene.rotation.y = 0.5; // avoid flying objects occluding the sun - - new RGBELoader().setPath('textures/equirectangular/').load('quarry_01_1k.hdr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - scene.environment = texture; - }); - - // - - cubeRenderTarget = new THREE.WebGLCubeRenderTarget(256); - cubeRenderTarget.texture.type = THREE.HalfFloatType; - - cubeCamera = new THREE.CubeCamera(1, 1000, cubeRenderTarget); - - // - - material = new THREE.MeshStandardMaterial({ - envMap: cubeRenderTarget.texture, - roughness: 0.05, - metalness: 1, - }); - - const gui = new GUI(); - gui.add(material, 'roughness', 0, 1); - gui.add(material, 'metalness', 0, 1); - gui.add(renderer, 'toneMappingExposure', 0, 2).name('exposure'); - - sphere = new THREE.Mesh(new THREE.IcosahedronGeometry(15, 8), material); - scene.add(sphere); - - const material2 = new THREE.MeshStandardMaterial({ - roughness: 0.1, - metalness: 0, - }); - - cube = new THREE.Mesh(new THREE.BoxGeometry(15, 15, 15), material2); - scene.add(cube); - - torus = new THREE.Mesh(new THREE.TorusKnotGeometry(8, 3, 128, 16), material2); - scene.add(torus); - - // - - controls = new OrbitControls(camera, renderer.domElement); - controls.autoRotate = true; -} - -function onWindowResized() { - renderer.setSize(window.innerWidth, window.innerHeight); - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); -} - -function animate(msTime) { - const time = msTime / 1000; - - cube.position.x = Math.cos(time) * 30; - cube.position.y = Math.sin(time) * 30; - cube.position.z = Math.sin(time) * 30; - - cube.rotation.x += 0.02; - cube.rotation.y += 0.03; - - torus.position.x = Math.cos(time + 10) * 30; - torus.position.y = Math.sin(time + 10) * 30; - torus.position.z = Math.sin(time + 10) * 30; - - torus.rotation.x += 0.02; - torus.rotation.y += 0.03; - - cubeCamera.update(renderer, scene); - - controls.update(); - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_materials_cubemap_mipmaps.ts b/examples-testing/examples/webgl_materials_cubemap_mipmaps.ts deleted file mode 100644 index 944f4c18e..000000000 --- a/examples-testing/examples/webgl_materials_cubemap_mipmaps.ts +++ /dev/null @@ -1,119 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let container; - -let camera, scene, renderer; - -init(); - -//load customized cube texture -async function loadCubeTextureWithMipmaps() { - const path = 'textures/cube/angus/'; - const format = '.jpg'; - const mipmaps = []; - const maxLevel = 8; - - async function loadCubeTexture(urls) { - return new Promise(function (resolve) { - new THREE.CubeTextureLoader().load(urls, function (cubeTexture) { - resolve(cubeTexture); - }); - }); - } - - // load mipmaps - const pendings = []; - - for (let level = 0; level <= maxLevel; ++level) { - const urls = []; - - for (let face = 0; face < 6; ++face) { - urls.push(path + 'cube_m0' + level + '_c0' + face + format); - } - - const mipmapLevel = level; - - pendings.push( - loadCubeTexture(urls).then(function (cubeTexture) { - mipmaps[mipmapLevel] = cubeTexture; - }), - ); - } - - await Promise.all(pendings); - - const customizedCubeTexture = mipmaps.shift(); - customizedCubeTexture.mipmaps = mipmaps; - customizedCubeTexture.colorSpace = THREE.SRGBColorSpace; - customizedCubeTexture.minFilter = THREE.LinearMipMapLinearFilter; - customizedCubeTexture.magFilter = THREE.LinearFilter; - customizedCubeTexture.generateMipmaps = false; - customizedCubeTexture.needsUpdate = true; - - return customizedCubeTexture; -} - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 500; - - scene = new THREE.Scene(); - - loadCubeTextureWithMipmaps().then(function (cubeTexture) { - //model - const sphere = new THREE.SphereGeometry(100, 128, 128); - - //manual mipmaps - let material = new THREE.MeshBasicMaterial({ color: 0xffffff, envMap: cubeTexture }); - material.name = 'manual mipmaps'; - - let mesh = new THREE.Mesh(sphere, material); - mesh.position.set(100, 0, 0); - scene.add(mesh); - - //webgl mipmaps - material = material.clone(); - material.name = 'auto mipmaps'; - - const autoCubeTexture = cubeTexture.clone(); - autoCubeTexture.mipmaps = []; - autoCubeTexture.generateMipmaps = true; - autoCubeTexture.needsUpdate = true; - - material.envMap = autoCubeTexture; - - mesh = new THREE.Mesh(sphere, material); - mesh.position.set(-100, 0, 0); - scene.add(mesh); - }); - - //renderer - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - //controls - const controls = new OrbitControls(camera, renderer.domElement); - controls.minPolarAngle = Math.PI / 4; - controls.maxPolarAngle = Math.PI / 1.5; - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_cubemap_refraction.ts b/examples-testing/examples/webgl_materials_cubemap_refraction.ts deleted file mode 100644 index 8c025071f..000000000 --- a/examples-testing/examples/webgl_materials_cubemap_refraction.ts +++ /dev/null @@ -1,126 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { PLYLoader } from 'three/addons/loaders/PLYLoader.js'; - -let container, stats; - -let camera, scene, renderer; - -let mouseX = 0, - mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 100000); - camera.position.z = -4000; - - // - - const r = 'textures/cube/Park3Med/'; - - const urls = [r + 'px.jpg', r + 'nx.jpg', r + 'py.jpg', r + 'ny.jpg', r + 'pz.jpg', r + 'nz.jpg']; - - const textureCube = new THREE.CubeTextureLoader().load(urls); - textureCube.mapping = THREE.CubeRefractionMapping; - - scene = new THREE.Scene(); - scene.background = textureCube; - - // LIGHTS - - const ambient = new THREE.AmbientLight(0xffffff, 3.5); - scene.add(ambient); - - // material samples - - const cubeMaterial3 = new THREE.MeshPhongMaterial({ - color: 0xccddff, - envMap: textureCube, - refractionRatio: 0.98, - reflectivity: 0.9, - }); - const cubeMaterial2 = new THREE.MeshPhongMaterial({ color: 0xccfffd, envMap: textureCube, refractionRatio: 0.985 }); - const cubeMaterial1 = new THREE.MeshPhongMaterial({ color: 0xffffff, envMap: textureCube, refractionRatio: 0.98 }); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - const loader = new PLYLoader(); - loader.load('models/ply/binary/Lucy100k.ply', function (geometry) { - createScene(geometry, cubeMaterial1, cubeMaterial2, cubeMaterial3); - }); - - document.addEventListener('mousemove', onDocumentMouseMove); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function createScene(geometry, m1, m2, m3) { - geometry.computeVertexNormals(); - - const s = 1.5; - - let mesh = new THREE.Mesh(geometry, m1); - mesh.scale.x = mesh.scale.y = mesh.scale.z = s; - scene.add(mesh); - - mesh = new THREE.Mesh(geometry, m2); - mesh.position.x = -1500; - mesh.scale.x = mesh.scale.y = mesh.scale.z = s; - scene.add(mesh); - - mesh = new THREE.Mesh(geometry, m3); - mesh.position.x = 1500; - mesh.scale.x = mesh.scale.y = mesh.scale.z = s; - scene.add(mesh); -} - -function onDocumentMouseMove(event) { - mouseX = (event.clientX - windowHalfX) * 4; - mouseY = (event.clientY - windowHalfY) * 4; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-mouseY - camera.position.y) * 0.05; - - camera.lookAt(scene.position); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_cubemap_render_to_mipmaps.ts b/examples-testing/examples/webgl_materials_cubemap_render_to_mipmaps.ts deleted file mode 100644 index 599a1369b..000000000 --- a/examples-testing/examples/webgl_materials_cubemap_render_to_mipmaps.ts +++ /dev/null @@ -1,183 +0,0 @@ -import * as THREE from 'three'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let container; -let camera, scene, renderer; - -const CubemapFilterShader = { - name: 'CubemapFilterShader', - - uniforms: { - cubeTexture: { value: null }, - mipIndex: { value: 0 }, - }, - - vertexShader: /* glsl */ ` - - varying vec3 vWorldDirection; - - #include - - void main() { - vWorldDirection = transformDirection(position, modelMatrix); - #include - #include - gl_Position.z = gl_Position.w; // set z to camera.far - } - - `, - - fragmentShader: /* glsl */ ` - - uniform samplerCube cubeTexture; - varying vec3 vWorldDirection; - - uniform float mipIndex; - - #include - - void main() { - vec3 cubeCoordinates = normalize(vWorldDirection); - - // Colorize mip levels - vec4 color = vec4(1.0, 0.0, 0.0, 1.0); - if (mipIndex == 0.0) color.rgb = vec3(1.0, 1.0, 1.0); - else if (mipIndex == 1.0) color.rgb = vec3(0.0, 0.0, 1.0); - else if (mipIndex == 2.0) color.rgb = vec3(0.0, 1.0, 1.0); - else if (mipIndex == 3.0) color.rgb = vec3(0.0, 1.0, 0.0); - else if (mipIndex == 4.0) color.rgb = vec3(1.0, 1.0, 0.0); - - gl_FragColor = textureCube(cubeTexture, cubeCoordinates, 0.0) * color; - } - - `, -}; - -init(); - -async function loadCubeTexture(urls) { - return new Promise(function (resolve) { - new THREE.CubeTextureLoader().load(urls, function (cubeTexture) { - resolve(cubeTexture); - }); - }); -} - -function allocateCubemapRenderTarget(cubeMapSize) { - const params = { - magFilter: THREE.LinearFilter, - minFilter: THREE.LinearMipMapLinearFilter, - generateMipmaps: false, - type: THREE.HalfFloatType, - format: THREE.RGBAFormat, - colorSpace: THREE.LinearSRGBColorSpace, - depthBuffer: false, - }; - - const rt = new THREE.WebGLCubeRenderTarget(cubeMapSize, params); - - const mipLevels = Math.log(cubeMapSize) * Math.LOG2E + 1.0; - for (let i = 0; i < mipLevels; i++) rt.texture.mipmaps.push({}); - - rt.texture.mapping = THREE.CubeReflectionMapping; - return rt; -} - -function renderToCubeTexture(cubeMapRenderTarget, sourceCubeTexture) { - const geometry = new THREE.BoxGeometry(5, 5, 5); - - const material = new THREE.ShaderMaterial({ - name: CubemapFilterShader.name, - uniforms: THREE.UniformsUtils.clone(CubemapFilterShader.uniforms), - vertexShader: CubemapFilterShader.vertexShader, - fragmentShader: CubemapFilterShader.fragmentShader, - side: THREE.BackSide, - blending: THREE.NoBlending, - }); - - material.uniforms.cubeTexture.value = sourceCubeTexture; - - const mesh = new THREE.Mesh(geometry, material); - const cubeCamera = new THREE.CubeCamera(1, 10, cubeMapRenderTarget); - const mipmapCount = Math.floor(Math.log2(Math.max(cubeMapRenderTarget.width, cubeMapRenderTarget.height))); - - for (let mipmap = 0; mipmap < mipmapCount; mipmap++) { - material.uniforms.mipIndex.value = mipmap; - material.needsUpdate = true; - - cubeMapRenderTarget.viewport.set( - 0, - 0, - cubeMapRenderTarget.width >> mipmap, - cubeMapRenderTarget.height >> mipmap, - ); - - cubeCamera.activeMipmapLevel = mipmap; - cubeCamera.update(renderer, mesh); - } - - mesh.geometry.dispose(); - mesh.material.dispose(); -} - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - // Create renderer - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 500; - - // Create controls - const controls = new OrbitControls(camera, renderer.domElement); - controls.minPolarAngle = Math.PI / 4; - controls.maxPolarAngle = Math.PI / 1.5; - - window.addEventListener('resize', onWindowResize); - - // Load a cube texture - const r = 'textures/cube/Park3Med/'; - const urls = [r + 'px.jpg', r + 'nx.jpg', r + 'py.jpg', r + 'ny.jpg', r + 'pz.jpg', r + 'nz.jpg']; - - loadCubeTexture(urls).then(cubeTexture => { - // Allocate a cube map render target - const cubeMapRenderTarget = allocateCubemapRenderTarget(512); - - // Render to all the mip levels of cubeMapRenderTarget - renderToCubeTexture(cubeMapRenderTarget, cubeTexture); - - // Create geometry - const sphere = new THREE.SphereGeometry(100, 128, 128); - let material = new THREE.MeshBasicMaterial({ color: 0xffffff, envMap: cubeTexture }); - - let mesh = new THREE.Mesh(sphere, material); - mesh.position.set(-100, 0, 0); - scene.add(mesh); - - material = material.clone(); - material.envMap = cubeMapRenderTarget.texture; - - mesh = new THREE.Mesh(sphere, material); - mesh.position.set(100, 0, 0); - scene.add(mesh); - }); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_curvature.ts b/examples-testing/examples/webgl_materials_curvature.ts deleted file mode 100644 index 60992dbc6..000000000 --- a/examples-testing/examples/webgl_materials_curvature.ts +++ /dev/null @@ -1,252 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; - -let camera, scene, renderer; - -let ninjaMeshRaw, curvatureAttribute, bufferGeo; - -init(); - -//returns average of elements in a dictionary -function average(dict) { - let sum = 0; - let length = 0; - - Object.keys(dict).forEach(function (key) { - sum += dict[key]; - length++; - }); - - return sum / length; -} - -//clamp a number between min and max -function clamp(number, min, max) { - return Math.max(min, Math.min(number, max)); -} - -//filter the curvature array to only show concave values -function filterConcave(curvature) { - for (let i = 0; i < curvature.length; i++) { - curvature[i] = Math.abs(clamp(curvature[i], -1, 0)); - } -} - -//filter the curvature array to only show convex values -function filterConvex(curvature) { - for (let i = 0; i < curvature.length; i++) { - curvature[i] = clamp(curvature[i], 0, 1); - } -} - -//filter the curvature array to show both the concave and convex values -function filterBoth(curvature) { - for (let i = 0; i < curvature.length; i++) { - curvature[i] = Math.abs(curvature[i]); - } -} - -//initialize the scene -function init() { - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); - - camera.position.x = -23; - camera.position.y = 2; - camera.position.z = 24; - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.autoClear = false; - document.body.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 20; - controls.maxDistance = 100; - - const loader = new OBJLoader(); - //load the obj - loader.load('models/obj/ninja/ninjaHead_Low.obj', function (object) { - object.traverse(function (child) { - if (child.isMesh) { - bufferGeo = child.geometry; - bufferGeo.center(); - const dict = {}; - - for (let i = 0; i < bufferGeo.attributes.position.count; i += 3) { - //create a dictionary of every position, and its neighboring positions - const array = bufferGeo.attributes.position.array; - const normArray = bufferGeo.attributes.normal.array; - - const posA = new THREE.Vector3(array[3 * i], array[3 * i + 1], array[3 * i + 2]); - const posB = new THREE.Vector3(array[3 * (i + 1)], array[3 * (i + 1) + 1], array[3 * (i + 1) + 2]); - const posC = new THREE.Vector3(array[3 * (i + 2)], array[3 * (i + 2) + 1], array[3 * (i + 2) + 2]); - - const normA = new THREE.Vector3( - normArray[3 * i], - normArray[3 * i + 1], - normArray[3 * i + 2], - ).normalize(); - const normB = new THREE.Vector3( - normArray[3 * (i + 1)], - normArray[3 * (i + 1) + 1], - normArray[3 * (i + 1) + 2], - ).normalize(); - const normC = new THREE.Vector3( - normArray[3 * (i + 2)], - normArray[3 * (i + 2) + 1], - normArray[3 * (i + 2) + 2], - ).normalize(); - - const strA = posA.toArray().toString(); - const strB = posB.toArray().toString(); - const strC = posC.toArray().toString(); - - const posB_A = new THREE.Vector3().subVectors(posB, posA); - const posB_C = new THREE.Vector3().subVectors(posB, posC); - const posC_A = new THREE.Vector3().subVectors(posC, posA); - - const b2a = normB.dot(posB_A.normalize()); - const b2c = normB.dot(posB_C.normalize()); - const c2a = normC.dot(posC_A.normalize()); - - const a2b = -normA.dot(posB_A.normalize()); - const c2b = -normC.dot(posB_C.normalize()); - const a2c = -normA.dot(posC_A.normalize()); - - if (dict[strA] === undefined) { - dict[strA] = {}; - } - - if (dict[strB] === undefined) { - dict[strB] = {}; - } - - if (dict[strC] === undefined) { - dict[strC] = {}; - } - - dict[strA][strB] = a2b; - dict[strA][strC] = a2c; - dict[strB][strA] = b2a; - dict[strB][strC] = b2c; - dict[strC][strA] = c2a; - dict[strC][strB] = c2b; - } - - let curvatureDict = {}; - let min = 10, - max = 0; - - Object.keys(dict).forEach(function (key) { - curvatureDict[key] = average(dict[key]); - }); - - //smoothing - const smoothCurvatureDict = Object.create(curvatureDict); - - Object.keys(dict).forEach(function (key) { - let count = 0; - let sum = 0; - Object.keys(dict[key]).forEach(function (key2) { - sum += smoothCurvatureDict[key2]; - count++; - }); - smoothCurvatureDict[key] = sum / count; - }); - - curvatureDict = smoothCurvatureDict; - - // fit values to 0 and 1 - Object.keys(curvatureDict).forEach(function (key) { - const val = Math.abs(curvatureDict[key]); - if (val < min) min = val; - if (val > max) max = val; - }); - - const range = max - min; - - Object.keys(curvatureDict).forEach(function (key) { - const val = Math.abs(curvatureDict[key]); - if (curvatureDict[key] < 0) { - curvatureDict[key] = (min - val) / range; - } else { - curvatureDict[key] = (val - min) / range; - } - }); - - curvatureAttribute = new Float32Array(bufferGeo.attributes.position.count); - - for (let i = 0; i < bufferGeo.attributes.position.count; i++) { - const array = bufferGeo.attributes.position.array; - const pos = new THREE.Vector3(array[3 * i], array[3 * i + 1], array[3 * i + 2]); - const str = pos.toArray().toString(); - curvatureAttribute[i] = curvatureDict[str]; - } - - bufferGeo.setAttribute('curvature', new THREE.BufferAttribute(curvatureAttribute, 1)); - - //starting filter is to show both concave and convex - const curvatureFiltered = new Float32Array(curvatureAttribute); - filterBoth(curvatureFiltered); - - const materialRaw = new THREE.ShaderMaterial({ - vertexShader: document.getElementById('vertexShaderRaw').textContent, - fragmentShader: document.getElementById('fragmentShaderRaw').textContent, - }); - - ninjaMeshRaw = new THREE.Mesh(bufferGeo, materialRaw); - } - }); - - scene.add(ninjaMeshRaw); - }); - - //init GUI - const params = { - filterConvex: function () { - const curvatureFiltered = new Float32Array(curvatureAttribute); - filterConvex(curvatureFiltered); - bufferGeo.attributes.curvature.array = curvatureFiltered; - bufferGeo.attributes.curvature.needsUpdate = true; - }, - filterConcave: function () { - const curvatureFiltered = new Float32Array(curvatureAttribute); - filterConcave(curvatureFiltered); - bufferGeo.attributes.curvature.array = curvatureFiltered; - bufferGeo.attributes.curvature.needsUpdate = true; - }, - filterBoth: function () { - const curvatureFiltered = new Float32Array(curvatureAttribute); - filterBoth(curvatureFiltered); - bufferGeo.attributes.curvature.array = curvatureFiltered; - bufferGeo.attributes.curvature.needsUpdate = true; - }, - }; - - const gui = new GUI({ title: 'Topology' }); - - gui.add(params, 'filterConvex'); - gui.add(params, 'filterConcave'); - gui.add(params, 'filterBoth'); - - onWindowResize(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); -} - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_displacementmap.ts b/examples-testing/examples/webgl_materials_displacementmap.ts deleted file mode 100644 index fd0be9a5e..000000000 --- a/examples-testing/examples/webgl_materials_displacementmap.ts +++ /dev/null @@ -1,224 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; - -let stats; -let camera, scene, renderer, controls; - -const settings = { - metalness: 1.0, - roughness: 0.4, - ambientIntensity: 0.2, - aoMapIntensity: 1.0, - envMapIntensity: 1.0, - displacementScale: 2.436143, // from original model - normalScale: 1.0, -}; - -let mesh, material; - -let pointLight, ambientLight; - -const height = 500; // of camera frustum - -let r = 0.0; - -init(); -initGui(); - -// Init gui -function initGui() { - const gui = new GUI(); - //let gui = gui.addFolder( "Material" ); - gui.add(settings, 'metalness') - .min(0) - .max(1) - .onChange(function (value) { - material.metalness = value; - }); - - gui.add(settings, 'roughness') - .min(0) - .max(1) - .onChange(function (value) { - material.roughness = value; - }); - - gui.add(settings, 'aoMapIntensity') - .min(0) - .max(1) - .onChange(function (value) { - material.aoMapIntensity = value; - }); - - gui.add(settings, 'ambientIntensity') - .min(0) - .max(1) - .onChange(function (value) { - ambientLight.intensity = value; - }); - - gui.add(settings, 'envMapIntensity') - .min(0) - .max(3) - .onChange(function (value) { - material.envMapIntensity = value; - }); - - gui.add(settings, 'displacementScale') - .min(0) - .max(3.0) - .onChange(function (value) { - material.displacementScale = value; - }); - - gui.add(settings, 'normalScale') - .min(-1) - .max(1) - .onChange(function (value) { - material.normalScale.set(1, -1).multiplyScalar(value); - }); -} - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - scene = new THREE.Scene(); - - const aspect = window.innerWidth / window.innerHeight; - camera = new THREE.OrthographicCamera(-height * aspect, height * aspect, height, -height, 1, 10000); - camera.position.z = 1500; - scene.add(camera); - - controls = new OrbitControls(camera, renderer.domElement); - controls.enableZoom = false; - controls.enableDamping = true; - - // lights - - ambientLight = new THREE.AmbientLight(0xffffff, settings.ambientIntensity); - scene.add(ambientLight); - - pointLight = new THREE.PointLight(0xff0000, 1.5, 0, 0); - pointLight.position.z = 2500; - scene.add(pointLight); - - const pointLight2 = new THREE.PointLight(0xff6666, 3, 0, 0); - camera.add(pointLight2); - - const pointLight3 = new THREE.PointLight(0x0000ff, 1.5, 0, 0); - pointLight3.position.x = -1000; - pointLight3.position.z = 1000; - scene.add(pointLight3); - - // env map - - const path = 'textures/cube/SwedishRoyalCastle/'; - const format = '.jpg'; - const urls = [ - path + 'px' + format, - path + 'nx' + format, - path + 'py' + format, - path + 'ny' + format, - path + 'pz' + format, - path + 'nz' + format, - ]; - - const reflectionCube = new THREE.CubeTextureLoader().load(urls); - - // textures - - const textureLoader = new THREE.TextureLoader(); - const normalMap = textureLoader.load('models/obj/ninja/normal.png'); - const aoMap = textureLoader.load('models/obj/ninja/ao.jpg'); - const displacementMap = textureLoader.load('models/obj/ninja/displacement.jpg'); - - // material - - material = new THREE.MeshStandardMaterial({ - color: 0xc1c1c1, - roughness: settings.roughness, - metalness: settings.metalness, - - normalMap: normalMap, - normalScale: new THREE.Vector2(1, -1), // why does the normal map require negation in this case? - - aoMap: aoMap, - aoMapIntensity: 1, - - displacementMap: displacementMap, - displacementScale: settings.displacementScale, - displacementBias: -0.428408, // from original model - - envMap: reflectionCube, - envMapIntensity: settings.envMapIntensity, - - side: THREE.DoubleSide, - }); - - // - - const loader = new OBJLoader(); - loader.load('models/obj/ninja/ninjaHead_Low.obj', function (group) { - const geometry = group.children[0].geometry; - geometry.center(); - - mesh = new THREE.Mesh(geometry, material); - mesh.scale.multiplyScalar(25); - scene.add(mesh); - }); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const aspect = window.innerWidth / window.innerHeight; - - camera.left = -height * aspect; - camera.right = height * aspect; - camera.top = height; - camera.bottom = -height; - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - controls.update(); - - stats.begin(); - render(); - stats.end(); -} - -function render() { - pointLight.position.x = 2500 * Math.cos(r); - pointLight.position.z = 2500 * Math.sin(r); - - r += 0.01; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_envmaps.ts b/examples-testing/examples/webgl_materials_envmaps.ts deleted file mode 100644 index 18a5542ed..000000000 --- a/examples-testing/examples/webgl_materials_envmaps.ts +++ /dev/null @@ -1,131 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let controls, camera, scene, renderer; -let textureEquirec, textureCube; -let sphereMesh, sphereMaterial, params; - -init(); - -function init() { - // CAMERAS - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(0, 0, 2.5); - - // SCENE - - scene = new THREE.Scene(); - - // Textures - - const loader = new THREE.CubeTextureLoader(); - loader.setPath('textures/cube/Bridge2/'); - - textureCube = loader.load(['posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg']); - - const textureLoader = new THREE.TextureLoader(); - - textureEquirec = textureLoader.load('textures/2294472375_24a3b8ef46_o.jpg'); - textureEquirec.mapping = THREE.EquirectangularReflectionMapping; - textureEquirec.colorSpace = THREE.SRGBColorSpace; - - scene.background = textureCube; - - // - - const geometry = new THREE.IcosahedronGeometry(1, 15); - sphereMaterial = new THREE.MeshBasicMaterial({ envMap: textureCube }); - sphereMesh = new THREE.Mesh(geometry, sphereMaterial); - scene.add(sphereMesh); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 1.5; - controls.maxDistance = 6; - - // - - params = { - Cube: function () { - scene.background = textureCube; - - sphereMaterial.envMap = textureCube; - sphereMaterial.needsUpdate = true; - }, - Equirectangular: function () { - scene.background = textureEquirec; - - sphereMaterial.envMap = textureEquirec; - sphereMaterial.needsUpdate = true; - }, - Refraction: false, - backgroundRotationX: false, - backgroundRotationY: false, - backgroundRotationZ: false, - syncMaterial: false, - }; - - const gui = new GUI({ width: 300 }); - gui.add(params, 'Cube'); - gui.add(params, 'Equirectangular'); - gui.add(params, 'Refraction').onChange(function (value) { - if (value) { - textureEquirec.mapping = THREE.EquirectangularRefractionMapping; - textureCube.mapping = THREE.CubeRefractionMapping; - } else { - textureEquirec.mapping = THREE.EquirectangularReflectionMapping; - textureCube.mapping = THREE.CubeReflectionMapping; - } - - sphereMaterial.needsUpdate = true; - }); - gui.add(params, 'backgroundRotationX'); - gui.add(params, 'backgroundRotationY'); - gui.add(params, 'backgroundRotationZ'); - gui.add(params, 'syncMaterial'); - gui.open(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - if (params.backgroundRotationX) { - scene.backgroundRotation.x += 0.001; - } - - if (params.backgroundRotationY) { - scene.backgroundRotation.y += 0.001; - } - - if (params.backgroundRotationZ) { - scene.backgroundRotation.z += 0.001; - } - - if (params.syncMaterial) { - sphereMesh.material.envMapRotation.copy(scene.backgroundRotation); - } - - camera.lookAt(scene.position); - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_envmaps_exr.ts b/examples-testing/examples/webgl_materials_envmaps_exr.ts deleted file mode 100644 index c3f3f4f7d..000000000 --- a/examples-testing/examples/webgl_materials_envmaps_exr.ts +++ /dev/null @@ -1,153 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { EXRLoader } from 'three/addons/loaders/EXRLoader.js'; - -const params = { - envMap: 'EXR', - roughness: 0.0, - metalness: 0.0, - exposure: 1.0, - debug: false, -}; - -let container, stats; -let camera, scene, renderer, controls; -let torusMesh, planeMesh; -let pngCubeRenderTarget, exrCubeRenderTarget; -let pngBackground, exrBackground; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 0, 120); - - scene = new THREE.Scene(); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - - container.appendChild(renderer.domElement); - - renderer.toneMapping = THREE.ACESFilmicToneMapping; - - // - - let geometry = new THREE.TorusKnotGeometry(18, 8, 150, 20); - let material = new THREE.MeshStandardMaterial({ - metalness: params.metalness, - roughness: params.roughness, - envMapIntensity: 1.0, - }); - - torusMesh = new THREE.Mesh(geometry, material); - scene.add(torusMesh); - - geometry = new THREE.PlaneGeometry(200, 200); - material = new THREE.MeshBasicMaterial(); - - planeMesh = new THREE.Mesh(geometry, material); - planeMesh.position.y = -50; - planeMesh.rotation.x = -Math.PI * 0.5; - scene.add(planeMesh); - - THREE.DefaultLoadingManager.onLoad = function () { - pmremGenerator.dispose(); - }; - - new EXRLoader().load('textures/piz_compressed.exr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - - exrCubeRenderTarget = pmremGenerator.fromEquirectangular(texture); - exrBackground = texture; - }); - - new THREE.TextureLoader().load('textures/equirectangular.png', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - texture.colorSpace = THREE.SRGBColorSpace; - - pngCubeRenderTarget = pmremGenerator.fromEquirectangular(texture); - pngBackground = texture; - }); - - const pmremGenerator = new THREE.PMREMGenerator(renderer); - pmremGenerator.compileEquirectangularShader(); - - stats = new Stats(); - container.appendChild(stats.dom); - - controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 50; - controls.maxDistance = 300; - - window.addEventListener('resize', onWindowResize); - - const gui = new GUI(); - - gui.add(params, 'envMap', ['EXR', 'PNG']); - gui.add(params, 'roughness', 0, 1, 0.01); - gui.add(params, 'metalness', 0, 1, 0.01); - gui.add(params, 'exposure', 0, 2, 0.01); - gui.add(params, 'debug'); - gui.open(); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); -} - -function animate() { - stats.begin(); - render(); - stats.end(); -} - -function render() { - torusMesh.material.roughness = params.roughness; - torusMesh.material.metalness = params.metalness; - - let newEnvMap = torusMesh.material.envMap; - let background = scene.background; - - switch (params.envMap) { - case 'EXR': - newEnvMap = exrCubeRenderTarget ? exrCubeRenderTarget.texture : null; - background = exrBackground; - break; - case 'PNG': - newEnvMap = pngCubeRenderTarget ? pngCubeRenderTarget.texture : null; - background = pngBackground; - break; - } - - if (newEnvMap !== torusMesh.material.envMap) { - torusMesh.material.envMap = newEnvMap; - torusMesh.material.needsUpdate = true; - - planeMesh.material.map = newEnvMap; - planeMesh.material.needsUpdate = true; - } - - torusMesh.rotation.y += 0.005; - planeMesh.visible = params.debug; - - scene.background = background; - renderer.toneMappingExposure = params.exposure; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_envmaps_groundprojected.ts b/examples-testing/examples/webgl_materials_envmaps_groundprojected.ts deleted file mode 100644 index 48e0077f4..000000000 --- a/examples-testing/examples/webgl_materials_envmaps_groundprojected.ts +++ /dev/null @@ -1,150 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GroundedSkybox } from 'three/addons/objects/GroundedSkybox.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -const params = { - height: 15, - radius: 100, - enabled: true, -}; - -let camera, scene, renderer, skybox; - -init().then(render); - -async function init() { - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(-20, 7, 20); - camera.lookAt(0, 4, 0); - - scene = new THREE.Scene(); - - const hdrLoader = new RGBELoader(); - const envMap = await hdrLoader.loadAsync('textures/equirectangular/blouberg_sunrise_2_1k.hdr'); - envMap.mapping = THREE.EquirectangularReflectionMapping; - - skybox = new GroundedSkybox(envMap, params.height, params.radius); - skybox.position.y = params.height - 0.01; - scene.add(skybox); - - scene.environment = envMap; - - const dracoLoader = new DRACOLoader(); - dracoLoader.setDecoderPath('jsm/libs/draco/gltf/'); - - const loader = new GLTFLoader(); - loader.setDRACOLoader(dracoLoader); - - const shadow = new THREE.TextureLoader().load('models/gltf/ferrari_ao.png'); - - loader.load('models/gltf/ferrari.glb', function (gltf) { - const bodyMaterial = new THREE.MeshPhysicalMaterial({ - color: 0x000000, - metalness: 1.0, - roughness: 0.8, - clearcoat: 1.0, - clearcoatRoughness: 0.2, - }); - - const detailsMaterial = new THREE.MeshStandardMaterial({ - color: 0xffffff, - metalness: 1.0, - roughness: 0.5, - }); - - const glassMaterial = new THREE.MeshPhysicalMaterial({ - color: 0xffffff, - metalness: 0.25, - roughness: 0, - transmission: 1.0, - }); - - const carModel = gltf.scene.children[0]; - carModel.scale.multiplyScalar(4); - carModel.rotation.y = Math.PI; - - carModel.getObjectByName('body').material = bodyMaterial; - - carModel.getObjectByName('rim_fl').material = detailsMaterial; - carModel.getObjectByName('rim_fr').material = detailsMaterial; - carModel.getObjectByName('rim_rr').material = detailsMaterial; - carModel.getObjectByName('rim_rl').material = detailsMaterial; - carModel.getObjectByName('trim').material = detailsMaterial; - - carModel.getObjectByName('glass').material = glassMaterial; - - // shadow - const mesh = new THREE.Mesh( - new THREE.PlaneGeometry(0.655 * 4, 1.3 * 4), - new THREE.MeshBasicMaterial({ - map: shadow, - blending: THREE.MultiplyBlending, - toneMapped: false, - transparent: true, - }), - ); - mesh.rotation.x = -Math.PI / 2; - carModel.add(mesh); - - scene.add(carModel); - - render(); - }); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); - controls.target.set(0, 2, 0); - controls.maxPolarAngle = THREE.MathUtils.degToRad(90); - controls.maxDistance = 80; - controls.minDistance = 20; - controls.enablePan = false; - controls.update(); - - document.body.appendChild(renderer.domElement); - window.addEventListener('resize', onWindowResize); - - const gui = new GUI(); - - gui.add(params, 'enabled') - .name('Grounded') - .onChange(function (value) { - if (value) { - scene.add(skybox); - scene.background = null; - } else { - scene.remove(skybox); - scene.background = scene.environment; - } - - render(); - }); - - gui.open(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_envmaps_hdr.ts b/examples-testing/examples/webgl_materials_envmaps_hdr.ts deleted file mode 100644 index b4c6f64ef..000000000 --- a/examples-testing/examples/webgl_materials_envmaps_hdr.ts +++ /dev/null @@ -1,176 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { HDRCubeTextureLoader } from 'three/addons/loaders/HDRCubeTextureLoader.js'; -import { RGBMLoader } from 'three/addons/loaders/RGBMLoader.js'; -import { DebugEnvironment } from 'three/addons/environments/DebugEnvironment.js'; - -const params = { - envMap: 'HDR', - roughness: 0.0, - metalness: 0.0, - exposure: 1.0, - debug: false, -}; - -let container, stats; -let camera, scene, renderer, controls; -let torusMesh, planeMesh; -let generatedCubeRenderTarget, ldrCubeRenderTarget, hdrCubeRenderTarget, rgbmCubeRenderTarget; -let ldrCubeMap, hdrCubeMap, rgbmCubeMap; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 0, 120); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x000000); - - renderer = new THREE.WebGLRenderer(); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - - // - - let geometry = new THREE.TorusKnotGeometry(18, 8, 150, 20); - // let geometry = new THREE.SphereGeometry( 26, 64, 32 ); - let material = new THREE.MeshStandardMaterial({ - color: 0xffffff, - metalness: params.metalness, - roughness: params.roughness, - }); - - torusMesh = new THREE.Mesh(geometry, material); - scene.add(torusMesh); - - geometry = new THREE.PlaneGeometry(200, 200); - material = new THREE.MeshBasicMaterial(); - - planeMesh = new THREE.Mesh(geometry, material); - planeMesh.position.y = -50; - planeMesh.rotation.x = -Math.PI * 0.5; - scene.add(planeMesh); - - THREE.DefaultLoadingManager.onLoad = function () { - pmremGenerator.dispose(); - }; - - const hdrUrls = ['px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr']; - hdrCubeMap = new HDRCubeTextureLoader().setPath('./textures/cube/pisaHDR/').load(hdrUrls, function () { - hdrCubeRenderTarget = pmremGenerator.fromCubemap(hdrCubeMap); - - hdrCubeMap.magFilter = THREE.LinearFilter; - hdrCubeMap.needsUpdate = true; - }); - - const ldrUrls = ['px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png']; - ldrCubeMap = new THREE.CubeTextureLoader().setPath('./textures/cube/pisa/').load(ldrUrls, function () { - ldrCubeRenderTarget = pmremGenerator.fromCubemap(ldrCubeMap); - }); - - const rgbmUrls = ['px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png']; - rgbmCubeMap = new RGBMLoader() - .setMaxRange(16) - .setPath('./textures/cube/pisaRGBM16/') - .loadCubemap(rgbmUrls, function () { - rgbmCubeRenderTarget = pmremGenerator.fromCubemap(rgbmCubeMap); - }); - - const pmremGenerator = new THREE.PMREMGenerator(renderer); - pmremGenerator.compileCubemapShader(); - - const envScene = new DebugEnvironment(); - generatedCubeRenderTarget = pmremGenerator.fromScene(envScene); - - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - //renderer.toneMapping = ReinhardToneMapping; - - stats = new Stats(); - container.appendChild(stats.dom); - - controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 50; - controls.maxDistance = 300; - - window.addEventListener('resize', onWindowResize); - - const gui = new GUI(); - - gui.add(params, 'envMap', ['Generated', 'LDR', 'HDR', 'RGBM16']); - gui.add(params, 'roughness', 0, 1, 0.01); - gui.add(params, 'metalness', 0, 1, 0.01); - gui.add(params, 'exposure', 0, 2, 0.01); - gui.add(params, 'debug'); - gui.open(); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); -} - -function animate() { - stats.begin(); - render(); - stats.end(); -} - -function render() { - torusMesh.material.roughness = params.roughness; - torusMesh.material.metalness = params.metalness; - - let renderTarget, cubeMap; - - switch (params.envMap) { - case 'Generated': - renderTarget = generatedCubeRenderTarget; - cubeMap = generatedCubeRenderTarget.texture; - break; - case 'LDR': - renderTarget = ldrCubeRenderTarget; - cubeMap = ldrCubeMap; - break; - case 'HDR': - renderTarget = hdrCubeRenderTarget; - cubeMap = hdrCubeMap; - break; - case 'RGBM16': - renderTarget = rgbmCubeRenderTarget; - cubeMap = rgbmCubeMap; - break; - } - - const newEnvMap = renderTarget ? renderTarget.texture : null; - - if (newEnvMap && newEnvMap !== torusMesh.material.envMap) { - torusMesh.material.envMap = newEnvMap; - torusMesh.material.needsUpdate = true; - - planeMesh.material.map = newEnvMap; - planeMesh.material.needsUpdate = true; - } - - torusMesh.rotation.y += 0.005; - planeMesh.visible = params.debug; - - scene.background = cubeMap; - renderer.toneMappingExposure = params.exposure; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_modified.ts b/examples-testing/examples/webgl_materials_modified.ts deleted file mode 100644 index c6ee5af3c..000000000 --- a/examples-testing/examples/webgl_materials_modified.ts +++ /dev/null @@ -1,115 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -let camera, scene, renderer, stats; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.z = 20; - - scene = new THREE.Scene(); - - const loader = new GLTFLoader(); - loader.load('models/gltf/LeePerrySmith/LeePerrySmith.glb', function (gltf) { - const geometry = gltf.scene.children[0].geometry; - - let mesh = new THREE.Mesh(geometry, buildTwistMaterial(2.0)); - mesh.position.x = -3.5; - mesh.position.y = -0.5; - scene.add(mesh); - - mesh = new THREE.Mesh(geometry, buildTwistMaterial(-2.0)); - mesh.position.x = 3.5; - mesh.position.y = -0.5; - scene.add(mesh); - }); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 10; - controls.maxDistance = 50; - - // - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // EVENTS - - window.addEventListener('resize', onWindowResize); -} - -function buildTwistMaterial(amount) { - const material = new THREE.MeshNormalMaterial(); - material.onBeforeCompile = function (shader) { - shader.uniforms.time = { value: 0 }; - - shader.vertexShader = 'uniform float time;\n' + shader.vertexShader; - shader.vertexShader = shader.vertexShader.replace( - '#include ', - [ - `float theta = sin( time + position.y ) / ${amount.toFixed(1)};`, - 'float c = cos( theta );', - 'float s = sin( theta );', - 'mat3 m = mat3( c, 0, s, 0, 1, 0, -s, 0, c );', - 'vec3 transformed = vec3( position ) * m;', - 'vNormal = vNormal * m;', - ].join('\n'), - ); - - material.userData.shader = shader; - }; - - // Make sure WebGLRenderer doesnt reuse a single program - - material.customProgramCacheKey = function () { - return amount.toFixed(1); - }; - - return material; -} - -// - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); -} - -// - -function animate() { - render(); - - stats.update(); -} - -function render() { - scene.traverse(function (child) { - if (child.isMesh) { - const shader = child.material.userData.shader; - - if (shader) { - shader.uniforms.time.value = performance.now() / 1000; - } - } - }); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_normalmap_object_space.ts b/examples-testing/examples/webgl_materials_normalmap_object_space.ts deleted file mode 100644 index 1fc6f8066..000000000 --- a/examples-testing/examples/webgl_materials_normalmap_object_space.ts +++ /dev/null @@ -1,82 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -let renderer, scene, camera; - -init(); - -function init() { - // renderer - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - // scene - scene = new THREE.Scene(); - - // camera - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(-10, 0, 23); - scene.add(camera); - - // controls - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); - controls.minDistance = 10; - controls.maxDistance = 50; - controls.enablePan = false; - - // ambient - scene.add(new THREE.AmbientLight(0xffffff, 0.6)); - - // light - const light = new THREE.PointLight(0xffffff, 4.5, 0, 0); - camera.add(light); - - // model - new GLTFLoader().load('models/gltf/Nefertiti/Nefertiti.glb', function (gltf) { - gltf.scene.traverse(function (child) { - if (child.isMesh) { - // glTF currently supports only tangent-space normal maps. - // this model has been modified to demonstrate the use of an object-space normal map. - - child.material.normalMapType = THREE.ObjectSpaceNormalMap; - - // attribute normals are not required with an object-space normal map. remove them. - - child.geometry.deleteAttribute('normal'); - - // - - child.material.side = THREE.DoubleSide; - - child.scale.multiplyScalar(0.5); - - // recenter - - new THREE.Box3().setFromObject(child).getCenter(child.position).multiplyScalar(-1); - - scene.add(child); - } - }); - - render(); - }); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_physical_clearcoat.ts b/examples-testing/examples/webgl_materials_physical_clearcoat.ts deleted file mode 100644 index 408fd9921..000000000 --- a/examples-testing/examples/webgl_materials_physical_clearcoat.ts +++ /dev/null @@ -1,208 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { HDRCubeTextureLoader } from 'three/addons/loaders/HDRCubeTextureLoader.js'; - -import { FlakesTexture } from 'three/addons/textures/FlakesTexture.js'; - -let container, stats; - -let camera, scene, renderer; - -let particleLight; -let group; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 0.25, 50); - camera.position.z = 10; - - scene = new THREE.Scene(); - - group = new THREE.Group(); - scene.add(group); - - new HDRCubeTextureLoader() - .setPath('textures/cube/pisaHDR/') - .load(['px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr'], function (texture) { - const geometry = new THREE.SphereGeometry(0.8, 64, 32); - - const textureLoader = new THREE.TextureLoader(); - - const diffuse = textureLoader.load('textures/carbon/Carbon.png'); - diffuse.colorSpace = THREE.SRGBColorSpace; - diffuse.wrapS = THREE.RepeatWrapping; - diffuse.wrapT = THREE.RepeatWrapping; - diffuse.repeat.x = 10; - diffuse.repeat.y = 10; - - const normalMap = textureLoader.load('textures/carbon/Carbon_Normal.png'); - normalMap.wrapS = THREE.RepeatWrapping; - normalMap.wrapT = THREE.RepeatWrapping; - normalMap.repeat.x = 10; - normalMap.repeat.y = 10; - - const normalMap2 = textureLoader.load('textures/water/Water_1_M_Normal.jpg'); - - const normalMap3 = new THREE.CanvasTexture(new FlakesTexture()); - normalMap3.wrapS = THREE.RepeatWrapping; - normalMap3.wrapT = THREE.RepeatWrapping; - normalMap3.repeat.x = 10; - normalMap3.repeat.y = 6; - normalMap3.anisotropy = 16; - - const normalMap4 = textureLoader.load('textures/golfball.jpg'); - - const clearcoatNormalMap = textureLoader.load( - 'textures/pbr/Scratched_gold/Scratched_gold_01_1K_Normal.png', - ); - - // car paint - - let material = new THREE.MeshPhysicalMaterial({ - clearcoat: 1.0, - clearcoatRoughness: 0.1, - metalness: 0.9, - roughness: 0.5, - color: 0x0000ff, - normalMap: normalMap3, - normalScale: new THREE.Vector2(0.15, 0.15), - }); - - let mesh = new THREE.Mesh(geometry, material); - mesh.position.x = -1; - mesh.position.y = 1; - group.add(mesh); - - // fibers - - material = new THREE.MeshPhysicalMaterial({ - roughness: 0.5, - clearcoat: 1.0, - clearcoatRoughness: 0.1, - map: diffuse, - normalMap: normalMap, - }); - mesh = new THREE.Mesh(geometry, material); - mesh.position.x = 1; - mesh.position.y = 1; - group.add(mesh); - - // golf - - material = new THREE.MeshPhysicalMaterial({ - metalness: 0.0, - roughness: 0.1, - clearcoat: 1.0, - normalMap: normalMap4, - clearcoatNormalMap: clearcoatNormalMap, - - // y scale is negated to compensate for normal map handedness. - clearcoatNormalScale: new THREE.Vector2(2.0, -2.0), - }); - mesh = new THREE.Mesh(geometry, material); - mesh.position.x = -1; - mesh.position.y = -1; - group.add(mesh); - - // clearcoat + normalmap - - material = new THREE.MeshPhysicalMaterial({ - clearcoat: 1.0, - metalness: 1.0, - color: 0xff0000, - normalMap: normalMap2, - normalScale: new THREE.Vector2(0.15, 0.15), - clearcoatNormalMap: clearcoatNormalMap, - - // y scale is negated to compensate for normal map handedness. - clearcoatNormalScale: new THREE.Vector2(2.0, -2.0), - }); - mesh = new THREE.Mesh(geometry, material); - mesh.position.x = 1; - mesh.position.y = -1; - group.add(mesh); - - // - - scene.background = texture; - scene.environment = texture; - }); - - // LIGHTS - - particleLight = new THREE.Mesh( - new THREE.SphereGeometry(0.05, 8, 8), - new THREE.MeshBasicMaterial({ color: 0xffffff }), - ); - scene.add(particleLight); - - particleLight.add(new THREE.PointLight(0xffffff, 30)); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1.25; - - // - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // EVENTS - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 3; - controls.maxDistance = 30; - - window.addEventListener('resize', onWindowResize); -} - -// - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); -} - -// - -function animate() { - render(); - - stats.update(); -} - -function render() { - const timer = Date.now() * 0.00025; - - particleLight.position.x = Math.sin(timer * 7) * 3; - particleLight.position.y = Math.cos(timer * 5) * 4; - particleLight.position.z = Math.cos(timer * 3) * 3; - - for (let i = 0; i < group.children.length; i++) { - const child = group.children[i]; - child.rotation.y += 0.005; - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_physical_transmission.ts b/examples-testing/examples/webgl_materials_physical_transmission.ts deleted file mode 100644 index d45967971..000000000 --- a/examples-testing/examples/webgl_materials_physical_transmission.ts +++ /dev/null @@ -1,182 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -const params = { - color: 0xffffff, - transmission: 1, - opacity: 1, - metalness: 0, - roughness: 0, - ior: 1.5, - thickness: 0.01, - specularIntensity: 1, - specularColor: 0xffffff, - envMapIntensity: 1, - lightIntensity: 1, - exposure: 1, -}; - -let camera, scene, renderer; - -let mesh; - -const hdrEquirect = new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function () { - hdrEquirect.mapping = THREE.EquirectangularReflectionMapping; - - init(); - render(); -}); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.shadowMap.enabled = true; - document.body.appendChild(renderer.domElement); - - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = params.exposure; - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 2000); - camera.position.set(0, 0, 120); - - // - - scene.background = hdrEquirect; - - // - - const geometry = new THREE.SphereGeometry(20, 64, 32); - - const texture = new THREE.CanvasTexture(generateTexture()); - texture.magFilter = THREE.NearestFilter; - texture.wrapT = THREE.RepeatWrapping; - texture.wrapS = THREE.RepeatWrapping; - texture.repeat.set(1, 3.5); - - const material = new THREE.MeshPhysicalMaterial({ - color: params.color, - metalness: params.metalness, - roughness: params.roughness, - ior: params.ior, - alphaMap: texture, - envMap: hdrEquirect, - envMapIntensity: params.envMapIntensity, - transmission: params.transmission, // use material.transmission for glass materials - specularIntensity: params.specularIntensity, - specularColor: params.specularColor, - opacity: params.opacity, - side: THREE.DoubleSide, - transparent: true, - }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.minDistance = 10; - controls.maxDistance = 150; - - window.addEventListener('resize', onWindowResize); - - // - - const gui = new GUI(); - - gui.addColor(params, 'color').onChange(function () { - material.color.set(params.color); - render(); - }); - - gui.add(params, 'transmission', 0, 1, 0.01).onChange(function () { - material.transmission = params.transmission; - render(); - }); - - gui.add(params, 'opacity', 0, 1, 0.01).onChange(function () { - material.opacity = params.opacity; - render(); - }); - - gui.add(params, 'metalness', 0, 1, 0.01).onChange(function () { - material.metalness = params.metalness; - render(); - }); - - gui.add(params, 'roughness', 0, 1, 0.01).onChange(function () { - material.roughness = params.roughness; - render(); - }); - - gui.add(params, 'ior', 1, 2, 0.01).onChange(function () { - material.ior = params.ior; - render(); - }); - - gui.add(params, 'thickness', 0, 5, 0.01).onChange(function () { - material.thickness = params.thickness; - render(); - }); - - gui.add(params, 'specularIntensity', 0, 1, 0.01).onChange(function () { - material.specularIntensity = params.specularIntensity; - render(); - }); - - gui.addColor(params, 'specularColor').onChange(function () { - material.specularColor.set(params.specularColor); - render(); - }); - - gui.add(params, 'envMapIntensity', 0, 1, 0.01) - .name('envMap intensity') - .onChange(function () { - material.envMapIntensity = params.envMapIntensity; - render(); - }); - - gui.add(params, 'exposure', 0, 1, 0.01).onChange(function () { - renderer.toneMappingExposure = params.exposure; - render(); - }); - - gui.open(); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); - - render(); -} - -// - -function generateTexture() { - const canvas = document.createElement('canvas'); - canvas.width = 2; - canvas.height = 2; - - const context = canvas.getContext('2d'); - context.fillStyle = 'white'; - context.fillRect(0, 1, 2, 1); - - return canvas; -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_physical_transmission_alpha.ts b/examples-testing/examples/webgl_materials_physical_transmission_alpha.ts deleted file mode 100644 index d81f59c37..000000000 --- a/examples-testing/examples/webgl_materials_physical_transmission_alpha.ts +++ /dev/null @@ -1,192 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -const params = { - color: 0xffffff, - transmission: 1, - opacity: 1, - metalness: 0, - roughness: 0, - ior: 1.5, - thickness: 0.01, - attenuationColor: 0xffffff, - attenuationDistance: 1, - specularIntensity: 1, - specularColor: 0xffffff, - envMapIntensity: 1, - lightIntensity: 1, - exposure: 1, -}; - -let camera, scene, renderer; - -let mesh, material; - -const hdrEquirect = new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function () { - hdrEquirect.mapping = THREE.EquirectangularReflectionMapping; - - new GLTFLoader().setPath('models/gltf/').load('DragonAttenuation.glb', function (gltf) { - gltf.scene.traverse(function (child) { - if (child.isMesh && child.material.isMeshPhysicalMaterial) { - mesh = child; - material = mesh.material; - - const color = new THREE.Color(); - - params.color = color.copy(mesh.material.color).getHex(); - params.roughness = mesh.material.roughness; - params.metalness = mesh.material.metalness; - - params.ior = mesh.material.ior; - params.specularIntensity = mesh.material.specularIntensity; - - params.transmission = mesh.material.transmission; - params.thickness = mesh.material.thickness; - params.attenuationColor = color.copy(mesh.material.attenuationColor).getHex(); - params.attenuationDistance = mesh.material.attenuationDistance; - } - }); - - init(); - - scene.add(gltf.scene); - - scene.environment = hdrEquirect; - //scene.background = hdrEquirect; - - render(); - }); -}); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.shadowMap.enabled = true; - document.body.appendChild(renderer.domElement); - - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = params.exposure; - - // accommodate CSS table - renderer.domElement.style.position = 'absolute'; - renderer.domElement.style.top = 0; - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 2000); - camera.position.set(-5, 0.5, 0); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.minDistance = 5; - controls.maxDistance = 20; - controls.target.y = 0.5; - controls.update(); - - window.addEventListener('resize', onWindowResize); - - // - - const gui = new GUI(); - - gui.addColor(params, 'color').onChange(function () { - material.color.set(params.color); - render(); - }); - - gui.add(params, 'transmission', 0, 1, 0.01).onChange(function () { - material.transmission = params.transmission; - render(); - }); - - gui.add(params, 'opacity', 0, 1, 0.01).onChange(function () { - material.opacity = params.opacity; - const transparent = params.opacity < 1; - - if (transparent !== material.transparent) { - material.transparent = transparent; - material.needsUpdate = true; - } - - render(); - }); - - gui.add(params, 'metalness', 0, 1, 0.01).onChange(function () { - material.metalness = params.metalness; - render(); - }); - - gui.add(params, 'roughness', 0, 1, 0.01).onChange(function () { - material.roughness = params.roughness; - render(); - }); - - gui.add(params, 'ior', 1, 2, 0.01).onChange(function () { - material.ior = params.ior; - render(); - }); - - gui.add(params, 'thickness', 0, 5, 0.01).onChange(function () { - material.thickness = params.thickness; - render(); - }); - - gui.addColor(params, 'attenuationColor') - .name('attenuation color') - .onChange(function () { - material.attenuationColor.set(params.attenuationColor); - render(); - }); - - gui.add(params, 'attenuationDistance', 0, 1, 0.01).onChange(function () { - material.attenuationDistance = params.attenuationDistance; - render(); - }); - - gui.add(params, 'specularIntensity', 0, 1, 0.01).onChange(function () { - material.specularIntensity = params.specularIntensity; - render(); - }); - - gui.addColor(params, 'specularColor').onChange(function () { - material.specularColor.set(params.specularColor); - render(); - }); - - gui.add(params, 'envMapIntensity', 0, 1, 0.01) - .name('envMap intensity') - .onChange(function () { - material.envMapIntensity = params.envMapIntensity; - render(); - }); - - gui.add(params, 'exposure', 0, 1, 0.01).onChange(function () { - renderer.toneMappingExposure = params.exposure; - render(); - }); - - gui.open(); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); - - render(); -} - -// - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_texture_anisotropy.ts b/examples-testing/examples/webgl_materials_texture_anisotropy.ts deleted file mode 100644 index 1e030d64d..000000000 --- a/examples-testing/examples/webgl_materials_texture_anisotropy.ts +++ /dev/null @@ -1,143 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -const SCREEN_WIDTH = window.innerWidth; -const SCREEN_HEIGHT = window.innerHeight; - -let container, stats; - -let camera, scene1, scene2, renderer; - -let mouseX = 0, - mouseY = 0; - -const windowHalfX = window.innerWidth / 2; -const windowHalfY = window.innerHeight / 2; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - - // - - camera = new THREE.PerspectiveCamera(35, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 25000); - camera.position.z = 1500; - - scene1 = new THREE.Scene(); - scene1.background = new THREE.Color(0xf2f7ff); - scene1.fog = new THREE.Fog(0xf2f7ff, 1, 25000); - - scene2 = new THREE.Scene(); - scene2.background = new THREE.Color(0xf2f7ff); - scene2.fog = new THREE.Fog(0xf2f7ff, 1, 25000); - - scene1.add(new THREE.AmbientLight(0xeef0ff, 3)); - scene2.add(new THREE.AmbientLight(0xeef0ff, 3)); - - const light1 = new THREE.DirectionalLight(0xffffff, 6); - light1.position.set(1, 1, 1); - scene1.add(light1); - - const light2 = new THREE.DirectionalLight(0xffffff, 6); - light2.position.set(1, 1, 1); - scene2.add(light2); - - // GROUND - - const textureLoader = new THREE.TextureLoader(); - - const maxAnisotropy = renderer.capabilities.getMaxAnisotropy(); - - const texture1 = textureLoader.load('textures/crate.gif'); - const material1 = new THREE.MeshPhongMaterial({ color: 0xffffff, map: texture1 }); - - texture1.colorSpace = THREE.SRGBColorSpace; - texture1.anisotropy = maxAnisotropy; - texture1.wrapS = texture1.wrapT = THREE.RepeatWrapping; - texture1.repeat.set(512, 512); - - const texture2 = textureLoader.load('textures/crate.gif'); - const material2 = new THREE.MeshPhongMaterial({ color: 0xffffff, map: texture2 }); - - texture2.colorSpace = THREE.SRGBColorSpace; - texture2.anisotropy = 1; - texture2.wrapS = texture2.wrapT = THREE.RepeatWrapping; - texture2.repeat.set(512, 512); - - if (maxAnisotropy > 0) { - document.getElementById('val_left').innerHTML = texture1.anisotropy; - document.getElementById('val_right').innerHTML = texture2.anisotropy; - } else { - document.getElementById('val_left').innerHTML = 'not supported'; - document.getElementById('val_right').innerHTML = 'not supported'; - } - - // - - const geometry = new THREE.PlaneGeometry(100, 100); - - const mesh1 = new THREE.Mesh(geometry, material1); - mesh1.rotation.x = -Math.PI / 2; - mesh1.scale.set(1000, 1000, 1000); - - const mesh2 = new THREE.Mesh(geometry, material2); - mesh2.rotation.x = -Math.PI / 2; - mesh2.scale.set(1000, 1000, 1000); - - scene1.add(mesh1); - scene2.add(mesh2); - - // RENDERER - - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - renderer.setAnimationLoop(animate); - renderer.autoClear = false; - - renderer.domElement.style.position = 'relative'; - container.appendChild(renderer.domElement); - - // STATS1 - - stats = new Stats(); - container.appendChild(stats.dom); - - document.addEventListener('mousemove', onDocumentMouseMove); -} - -function onDocumentMouseMove(event) { - mouseX = event.clientX - windowHalfX; - mouseY = event.clientY - windowHalfY; -} - -function animate() { - render(); - stats.update(); -} - -function render() { - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y = THREE.MathUtils.clamp( - camera.position.y + (-(mouseY - 200) - camera.position.y) * 0.05, - 50, - 1000, - ); - - camera.lookAt(scene1.position); - - renderer.clear(); - renderer.setScissorTest(true); - - renderer.setScissor(0, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); - renderer.render(scene1, camera); - - renderer.setScissor(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); - renderer.render(scene2, camera); - - renderer.setScissorTest(false); -} diff --git a/examples-testing/examples/webgl_materials_texture_canvas.ts b/examples-testing/examples/webgl_materials_texture_canvas.ts deleted file mode 100644 index d23c68436..000000000 --- a/examples-testing/examples/webgl_materials_texture_canvas.ts +++ /dev/null @@ -1,92 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer, mesh, material; -const drawStartPos = new THREE.Vector2(); - -init(); -setupCanvasDrawing(); - -function init() { - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 2000); - camera.position.z = 500; - - scene = new THREE.Scene(); - - material = new THREE.MeshBasicMaterial(); - - mesh = new THREE.Mesh(new THREE.BoxGeometry(200, 200, 200), material); - scene.add(mesh); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); -} - -// Sets up the drawing canvas and adds it as the material map - -function setupCanvasDrawing() { - // get canvas and context - - const drawingCanvas = document.getElementById('drawing-canvas'); - const drawingContext = drawingCanvas.getContext('2d'); - - // draw white background - - drawingContext.fillStyle = '#FFFFFF'; - drawingContext.fillRect(0, 0, 128, 128); - - // set canvas as material.map (this could be done to any map, bump, displacement etc.) - - material.map = new THREE.CanvasTexture(drawingCanvas); - - // set the variable to keep track of when to draw - - let paint = false; - - // add canvas event listeners - drawingCanvas.addEventListener('pointerdown', function (e) { - paint = true; - drawStartPos.set(e.offsetX, e.offsetY); - }); - - drawingCanvas.addEventListener('pointermove', function (e) { - if (paint) draw(drawingContext, e.offsetX, e.offsetY); - }); - - drawingCanvas.addEventListener('pointerup', function () { - paint = false; - }); - - drawingCanvas.addEventListener('pointerleave', function () { - paint = false; - }); -} - -function draw(drawContext, x, y) { - drawContext.moveTo(drawStartPos.x, drawStartPos.y); - drawContext.strokeStyle = '#000000'; - drawContext.lineTo(x, y); - drawContext.stroke(); - // reset drawing start position to current position. - drawStartPos.set(x, y); - // need to flag the map as needing updating. - material.map.needsUpdate = true; -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - mesh.rotation.x += 0.01; - mesh.rotation.y += 0.01; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_texture_filters.ts b/examples-testing/examples/webgl_materials_texture_filters.ts deleted file mode 100644 index 178c2ce49..000000000 --- a/examples-testing/examples/webgl_materials_texture_filters.ts +++ /dev/null @@ -1,164 +0,0 @@ -import * as THREE from 'three'; - -const SCREEN_WIDTH = window.innerWidth; -const SCREEN_HEIGHT = window.innerHeight; - -let container; - -let camera, scene, scene2, renderer; - -let mouseX = 0, - mouseY = 0; - -const windowHalfX = window.innerWidth / 2; -const windowHalfY = window.innerHeight / 2; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(35, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 5000); - camera.position.z = 1500; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x000000); - scene.fog = new THREE.Fog(0x000000, 1500, 4000); - - scene2 = new THREE.Scene(); - scene2.background = new THREE.Color(0x000000); - scene2.fog = new THREE.Fog(0x000000, 1500, 4000); - - // GROUND - - const imageCanvas = document.createElement('canvas'); - const context = imageCanvas.getContext('2d'); - - imageCanvas.width = imageCanvas.height = 128; - - context.fillStyle = '#444'; - context.fillRect(0, 0, 128, 128); - - context.fillStyle = '#fff'; - context.fillRect(0, 0, 64, 64); - context.fillRect(64, 64, 64, 64); - - const textureCanvas = new THREE.CanvasTexture(imageCanvas); - textureCanvas.colorSpace = THREE.SRGBColorSpace; - textureCanvas.repeat.set(1000, 1000); - textureCanvas.wrapS = THREE.RepeatWrapping; - textureCanvas.wrapT = THREE.RepeatWrapping; - - const textureCanvas2 = textureCanvas.clone(); - textureCanvas2.magFilter = THREE.NearestFilter; - textureCanvas2.minFilter = THREE.NearestFilter; - - const materialCanvas = new THREE.MeshBasicMaterial({ map: textureCanvas }); - const materialCanvas2 = new THREE.MeshBasicMaterial({ color: 0xffccaa, map: textureCanvas2 }); - - const geometry = new THREE.PlaneGeometry(100, 100); - - const meshCanvas = new THREE.Mesh(geometry, materialCanvas); - meshCanvas.rotation.x = -Math.PI / 2; - meshCanvas.scale.set(1000, 1000, 1000); - - const meshCanvas2 = new THREE.Mesh(geometry, materialCanvas2); - meshCanvas2.rotation.x = -Math.PI / 2; - meshCanvas2.scale.set(1000, 1000, 1000); - - // PAINTING - - const callbackPainting = function () { - const image = texturePainting.image; - - texturePainting2.image = image; - texturePainting2.needsUpdate = true; - - scene.add(meshCanvas); - scene2.add(meshCanvas2); - - const geometry = new THREE.PlaneGeometry(100, 100); - const mesh = new THREE.Mesh(geometry, materialPainting); - const mesh2 = new THREE.Mesh(geometry, materialPainting2); - - addPainting(scene, mesh); - addPainting(scene2, mesh2); - - function addPainting(zscene, zmesh) { - zmesh.scale.x = image.width / 100; - zmesh.scale.y = image.height / 100; - - zscene.add(zmesh); - - const meshFrame = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ color: 0x000000 })); - meshFrame.position.z = -10.0; - meshFrame.scale.x = (1.1 * image.width) / 100; - meshFrame.scale.y = (1.1 * image.height) / 100; - zscene.add(meshFrame); - - const meshShadow = new THREE.Mesh( - geometry, - new THREE.MeshBasicMaterial({ color: 0x000000, opacity: 0.75, transparent: true }), - ); - meshShadow.position.y = (-1.1 * image.height) / 2; - meshShadow.position.z = (-1.1 * image.height) / 2; - meshShadow.rotation.x = -Math.PI / 2; - meshShadow.scale.x = (1.1 * image.width) / 100; - meshShadow.scale.y = (1.1 * image.height) / 100; - zscene.add(meshShadow); - - const floorHeight = (-1.117 * image.height) / 2; - meshCanvas.position.y = meshCanvas2.position.y = floorHeight; - } - }; - - const texturePainting = new THREE.TextureLoader().load( - 'textures/758px-Canestra_di_frutta_(Caravaggio).jpg', - callbackPainting, - ); - const texturePainting2 = new THREE.Texture(); - - const materialPainting = new THREE.MeshBasicMaterial({ color: 0xffffff, map: texturePainting }); - const materialPainting2 = new THREE.MeshBasicMaterial({ color: 0xffccaa, map: texturePainting2 }); - - texturePainting.colorSpace = THREE.SRGBColorSpace; - texturePainting2.colorSpace = THREE.SRGBColorSpace; - texturePainting2.minFilter = texturePainting2.magFilter = THREE.NearestFilter; - texturePainting.minFilter = texturePainting.magFilter = THREE.LinearFilter; - texturePainting.mapping = THREE.UVMapping; - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - renderer.setAnimationLoop(animate); - renderer.autoClear = false; - - renderer.domElement.style.position = 'relative'; - container.appendChild(renderer.domElement); - - document.addEventListener('mousemove', onDocumentMouseMove); -} - -function onDocumentMouseMove(event) { - mouseX = event.clientX - windowHalfX; - mouseY = event.clientY - windowHalfY; -} - -function animate() { - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-(mouseY - 200) - camera.position.y) * 0.05; - - camera.lookAt(scene.position); - - renderer.clear(); - renderer.setScissorTest(true); - - renderer.setScissor(0, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); - renderer.render(scene, camera); - - renderer.setScissor(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); - renderer.render(scene2, camera); - - renderer.setScissorTest(false); -} diff --git a/examples-testing/examples/webgl_materials_texture_manualmipmap.ts b/examples-testing/examples/webgl_materials_texture_manualmipmap.ts deleted file mode 100644 index 24bd4eb9f..000000000 --- a/examples-testing/examples/webgl_materials_texture_manualmipmap.ts +++ /dev/null @@ -1,175 +0,0 @@ -import * as THREE from 'three'; - -const SCREEN_WIDTH = window.innerWidth; -const SCREEN_HEIGHT = window.innerHeight; - -let container; - -let camera, scene1, scene2, renderer; - -let mouseX = 0, - mouseY = 0; - -const windowHalfX = window.innerWidth / 2; -const windowHalfY = window.innerHeight / 2; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(35, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 5000); - camera.position.z = 1500; - - scene1 = new THREE.Scene(); - scene1.background = new THREE.Color(0x000000); - scene1.fog = new THREE.Fog(0x000000, 1500, 4000); - - scene2 = new THREE.Scene(); - scene2.background = new THREE.Color(0x000000); - scene2.fog = new THREE.Fog(0x000000, 1500, 4000); - - // GROUND - - function mipmap(size, color) { - const imageCanvas = document.createElement('canvas'); - const context = imageCanvas.getContext('2d'); - - imageCanvas.width = imageCanvas.height = size; - - context.fillStyle = '#444'; - context.fillRect(0, 0, size, size); - - context.fillStyle = color; - context.fillRect(0, 0, size / 2, size / 2); - context.fillRect(size / 2, size / 2, size / 2, size / 2); - return imageCanvas; - } - - const canvas = mipmap(128, '#f00'); - const textureCanvas1 = new THREE.CanvasTexture(canvas); - textureCanvas1.mipmaps[0] = canvas; - textureCanvas1.mipmaps[1] = mipmap(64, '#0f0'); - textureCanvas1.mipmaps[2] = mipmap(32, '#00f'); - textureCanvas1.mipmaps[3] = mipmap(16, '#400'); - textureCanvas1.mipmaps[4] = mipmap(8, '#040'); - textureCanvas1.mipmaps[5] = mipmap(4, '#004'); - textureCanvas1.mipmaps[6] = mipmap(2, '#044'); - textureCanvas1.mipmaps[7] = mipmap(1, '#404'); - textureCanvas1.colorSpace = THREE.SRGBColorSpace; - textureCanvas1.repeat.set(1000, 1000); - textureCanvas1.wrapS = THREE.RepeatWrapping; - textureCanvas1.wrapT = THREE.RepeatWrapping; - - const textureCanvas2 = textureCanvas1.clone(); - textureCanvas2.magFilter = THREE.NearestFilter; - textureCanvas2.minFilter = THREE.NearestMipmapNearestFilter; - - const materialCanvas1 = new THREE.MeshBasicMaterial({ map: textureCanvas1 }); - const materialCanvas2 = new THREE.MeshBasicMaterial({ color: 0xffccaa, map: textureCanvas2 }); - - const geometry = new THREE.PlaneGeometry(100, 100); - - const meshCanvas1 = new THREE.Mesh(geometry, materialCanvas1); - meshCanvas1.rotation.x = -Math.PI / 2; - meshCanvas1.scale.set(1000, 1000, 1000); - - const meshCanvas2 = new THREE.Mesh(geometry, materialCanvas2); - meshCanvas2.rotation.x = -Math.PI / 2; - meshCanvas2.scale.set(1000, 1000, 1000); - - // PAINTING - - const callbackPainting = function () { - const image = texturePainting1.image; - - texturePainting2.image = image; - texturePainting2.needsUpdate = true; - - scene1.add(meshCanvas1); - scene2.add(meshCanvas2); - - const geometry = new THREE.PlaneGeometry(100, 100); - const mesh1 = new THREE.Mesh(geometry, materialPainting1); - const mesh2 = new THREE.Mesh(geometry, materialPainting2); - - addPainting(scene1, mesh1); - addPainting(scene2, mesh2); - - function addPainting(zscene, zmesh) { - zmesh.scale.x = image.width / 100; - zmesh.scale.y = image.height / 100; - - zscene.add(zmesh); - - const meshFrame = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ color: 0x000000 })); - meshFrame.position.z = -10.0; - meshFrame.scale.x = (1.1 * image.width) / 100; - meshFrame.scale.y = (1.1 * image.height) / 100; - zscene.add(meshFrame); - - const meshShadow = new THREE.Mesh( - geometry, - new THREE.MeshBasicMaterial({ color: 0x000000, opacity: 0.75, transparent: true }), - ); - meshShadow.position.y = (-1.1 * image.height) / 2; - meshShadow.position.z = (-1.1 * image.height) / 2; - meshShadow.rotation.x = -Math.PI / 2; - meshShadow.scale.x = (1.1 * image.width) / 100; - meshShadow.scale.y = (1.1 * image.height) / 100; - zscene.add(meshShadow); - - const floorHeight = (-1.117 * image.height) / 2; - meshCanvas1.position.y = meshCanvas2.position.y = floorHeight; - } - }; - - const texturePainting1 = new THREE.TextureLoader().load( - 'textures/758px-Canestra_di_frutta_(Caravaggio).jpg', - callbackPainting, - ); - const texturePainting2 = new THREE.Texture(); - const materialPainting1 = new THREE.MeshBasicMaterial({ color: 0xffffff, map: texturePainting1 }); - const materialPainting2 = new THREE.MeshBasicMaterial({ color: 0xffccaa, map: texturePainting2 }); - - texturePainting1.colorSpace = THREE.SRGBColorSpace; - texturePainting2.colorSpace = THREE.SRGBColorSpace; - texturePainting2.minFilter = texturePainting2.magFilter = THREE.NearestFilter; - texturePainting1.minFilter = texturePainting1.magFilter = THREE.LinearFilter; - texturePainting1.mapping = THREE.UVMapping; - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - renderer.setAnimationLoop(animate); - renderer.autoClear = false; - - renderer.domElement.style.position = 'relative'; - container.appendChild(renderer.domElement); - - document.addEventListener('mousemove', onDocumentMouseMove); -} - -function onDocumentMouseMove(event) { - mouseX = event.clientX - windowHalfX; - mouseY = event.clientY - windowHalfY; -} - -function animate() { - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-(mouseY - 200) - camera.position.y) * 0.05; - - camera.lookAt(scene1.position); - - renderer.clear(); - renderer.setScissorTest(true); - - renderer.setScissor(0, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); - renderer.render(scene1, camera); - - renderer.setScissor(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); - renderer.render(scene2, camera); - - renderer.setScissorTest(false); -} diff --git a/examples-testing/examples/webgl_materials_texture_partialupdate.ts b/examples-testing/examples/webgl_materials_texture_partialupdate.ts deleted file mode 100644 index 5adfc8e69..000000000 --- a/examples-testing/examples/webgl_materials_texture_partialupdate.ts +++ /dev/null @@ -1,100 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer, clock, dataTexture, diffuseMap; - -let last = 0; -const position = new THREE.Vector2(); -const color = new THREE.Color(); - -init(); - -async function init() { - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10); - camera.position.z = 2; - - scene = new THREE.Scene(); - - clock = new THREE.Clock(); - - const loader = new THREE.TextureLoader(); - diffuseMap = await loader.loadAsync('textures/floors/FloorsCheckerboard_S_Diffuse.jpg'); - diffuseMap.colorSpace = THREE.SRGBColorSpace; - diffuseMap.minFilter = THREE.LinearFilter; - diffuseMap.generateMipmaps = false; - - const geometry = new THREE.PlaneGeometry(2, 2); - const material = new THREE.MeshBasicMaterial({ map: diffuseMap }); - - const mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - const width = 32; - const height = 32; - - const data = new Uint8Array(width * height * 4); - dataTexture = new THREE.DataTexture(data, width, height); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const elapsedTime = clock.getElapsedTime(); - - if (elapsedTime - last > 0.1) { - last = elapsedTime; - - position.x = 32 * THREE.MathUtils.randInt(1, 16) - 32; - position.y = 32 * THREE.MathUtils.randInt(1, 16) - 32; - - // generate new color data - - updateDataTexture(dataTexture); - - // perform copy from src to dest texture to a random position - - renderer.copyTextureToTexture(dataTexture, diffuseMap, null, position); - } - - renderer.render(scene, camera); -} - -function updateDataTexture(texture) { - const size = texture.image.width * texture.image.height; - const data = texture.image.data; - - // generate a random color and update texture data - - color.setHex(Math.random() * 0xffffff); - - const r = Math.floor(color.r * 255); - const g = Math.floor(color.g * 255); - const b = Math.floor(color.b * 255); - - for (let i = 0; i < size; i++) { - const stride = i * 4; - - data[stride] = r; - data[stride + 1] = g; - data[stride + 2] = b; - data[stride + 3] = 1; - } -} diff --git a/examples-testing/examples/webgl_materials_texture_rotation.ts b/examples-testing/examples/webgl_materials_texture_rotation.ts deleted file mode 100644 index 2666d09d6..000000000 --- a/examples-testing/examples/webgl_materials_texture_rotation.ts +++ /dev/null @@ -1,113 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let mesh, renderer, scene, camera; - -let gui; - -const API = { - offsetX: 0, - offsetY: 0, - repeatX: 0.25, - repeatY: 0.25, - rotation: Math.PI / 4, // positive is counterclockwise - centerX: 0.5, - centerY: 0.5, -}; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(10, 15, 25); - scene.add(camera); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); - controls.minDistance = 20; - controls.maxDistance = 50; - controls.maxPolarAngle = Math.PI / 2; - - const geometry = new THREE.BoxGeometry(10, 10, 10); - - new THREE.TextureLoader().load('textures/uv_grid_opengl.jpg', function (texture) { - texture.wrapS = texture.wrapT = THREE.RepeatWrapping; - texture.anisotropy = renderer.capabilities.getMaxAnisotropy(); - texture.colorSpace = THREE.SRGBColorSpace; - - //texture.matrixAutoUpdate = false; // default true; set to false to update texture.matrix manually - - const material = new THREE.MeshBasicMaterial({ map: texture }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - updateUvTransform(); - - initGui(); - - render(); - }); - - window.addEventListener('resize', onWindowResize); -} - -function render() { - renderer.render(scene, camera); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function updateUvTransform() { - const texture = mesh.material.map; - - if (texture.matrixAutoUpdate === true) { - texture.offset.set(API.offsetX, API.offsetY); - texture.repeat.set(API.repeatX, API.repeatY); - texture.center.set(API.centerX, API.centerY); - texture.rotation = API.rotation; // rotation is around center - } else { - // setting the matrix uv transform directly - //texture.matrix.setUvTransform( API.offsetX, API.offsetY, API.repeatX, API.repeatY, API.rotation, API.centerX, API.centerY ); - - // another way... - texture.matrix - .identity() - .translate(-API.centerX, -API.centerY) - .rotate(API.rotation) // I don't understand how rotation can preceed scale, but it seems to be required... - .scale(API.repeatX, API.repeatY) - .translate(API.centerX, API.centerY) - .translate(API.offsetX, API.offsetY); - } - - render(); -} - -function initGui() { - gui = new GUI(); - - gui.add(API, 'offsetX', 0.0, 1.0).name('offset.x').onChange(updateUvTransform); - gui.add(API, 'offsetY', 0.0, 1.0).name('offset.y').onChange(updateUvTransform); - gui.add(API, 'repeatX', 0.25, 2.0).name('repeat.x').onChange(updateUvTransform); - gui.add(API, 'repeatY', 0.25, 2.0).name('repeat.y').onChange(updateUvTransform); - gui.add(API, 'rotation', -2.0, 2.0).name('rotation').onChange(updateUvTransform); - gui.add(API, 'centerX', 0.0, 1.0).name('center.x').onChange(updateUvTransform); - gui.add(API, 'centerY', 0.0, 1.0).name('center.y').onChange(updateUvTransform); -} diff --git a/examples-testing/examples/webgl_materials_toon.ts b/examples-testing/examples/webgl_materials_toon.ts deleted file mode 100644 index 03db286ad..000000000 --- a/examples-testing/examples/webgl_materials_toon.ts +++ /dev/null @@ -1,152 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { OutlineEffect } from 'three/addons/effects/OutlineEffect.js'; -import { FontLoader } from 'three/addons/loaders/FontLoader.js'; -import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; - -let container, stats; - -let camera, scene, renderer, effect; -let particleLight; - -const loader = new FontLoader(); -loader.load('fonts/gentilis_regular.typeface.json', function (font) { - init(font); -}); - -function init(font) { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 2500); - camera.position.set(0.0, 400, 400 * 3.5); - - // - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x444488); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // Materials - - const cubeWidth = 400; - const numberOfSphersPerSide = 5; - const sphereRadius = (cubeWidth / numberOfSphersPerSide) * 0.8 * 0.5; - const stepSize = 1.0 / numberOfSphersPerSide; - - const geometry = new THREE.SphereGeometry(sphereRadius, 32, 16); - - for (let alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex++) { - const colors = new Uint8Array(alphaIndex + 2); - - for (let c = 0; c <= colors.length; c++) { - colors[c] = (c / colors.length) * 256; - } - - const gradientMap = new THREE.DataTexture(colors, colors.length, 1, THREE.RedFormat); - gradientMap.needsUpdate = true; - - for (let beta = 0; beta <= 1.0; beta += stepSize) { - for (let gamma = 0; gamma <= 1.0; gamma += stepSize) { - // basic monochromatic energy preservation - const diffuseColor = new THREE.Color() - .setHSL(alpha, 0.5, gamma * 0.5 + 0.1) - .multiplyScalar(1 - beta * 0.2); - - const material = new THREE.MeshToonMaterial({ - color: diffuseColor, - gradientMap: gradientMap, - }); - - const mesh = new THREE.Mesh(geometry, material); - - mesh.position.x = alpha * 400 - 200; - mesh.position.y = beta * 400 - 200; - mesh.position.z = gamma * 400 - 200; - - scene.add(mesh); - } - } - } - - function addLabel(name, location) { - const textGeo = new TextGeometry(name, { - font: font, - - size: 20, - depth: 1, - curveSegments: 1, - }); - - const textMaterial = new THREE.MeshBasicMaterial(); - const textMesh = new THREE.Mesh(textGeo, textMaterial); - textMesh.position.copy(location); - scene.add(textMesh); - } - - addLabel('-gradientMap', new THREE.Vector3(-350, 0, 0)); - addLabel('+gradientMap', new THREE.Vector3(350, 0, 0)); - - addLabel('-diffuse', new THREE.Vector3(0, 0, -300)); - addLabel('+diffuse', new THREE.Vector3(0, 0, 300)); - - particleLight = new THREE.Mesh(new THREE.SphereGeometry(4, 8, 8), new THREE.MeshBasicMaterial({ color: 0xffffff })); - scene.add(particleLight); - - // Lights - - scene.add(new THREE.AmbientLight(0xc1c1c1, 3)); - - const pointLight = new THREE.PointLight(0xffffff, 2, 800, 0); - particleLight.add(pointLight); - - // - - effect = new OutlineEffect(renderer); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 200; - controls.maxDistance = 2000; - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - stats.begin(); - render(); - stats.end(); -} - -function render() { - const timer = Date.now() * 0.00025; - - particleLight.position.x = Math.sin(timer * 7) * 300; - particleLight.position.y = Math.cos(timer * 5) * 400; - particleLight.position.z = Math.cos(timer * 3) * 300; - - effect.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_video.ts b/examples-testing/examples/webgl_materials_video.ts deleted file mode 100644 index 4f0d26a18..000000000 --- a/examples-testing/examples/webgl_materials_video.ts +++ /dev/null @@ -1,208 +0,0 @@ -import * as THREE from 'three'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { BloomPass } from 'three/addons/postprocessing/BloomPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -let container; - -let camera, scene, renderer; - -let video, texture, material, mesh; - -let composer; - -let mouseX = 0; -let mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -let cube_count; - -const meshes = [], - materials = [], - xgrid = 20, - ygrid = 10; - -const startButton = document.getElementById('startButton'); -startButton.addEventListener('click', function () { - init(); -}); - -function init() { - const overlay = document.getElementById('overlay'); - overlay.remove(); - - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 500; - - scene = new THREE.Scene(); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0.5, 1, 1).normalize(); - scene.add(light); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - video = document.getElementById('video'); - video.play(); - video.addEventListener('play', function () { - this.currentTime = 3; - }); - - texture = new THREE.VideoTexture(video); - texture.colorSpace = THREE.SRGBColorSpace; - - // - - let i, j, ox, oy, geometry; - - const ux = 1 / xgrid; - const uy = 1 / ygrid; - - const xsize = 480 / xgrid; - const ysize = 204 / ygrid; - - const parameters = { color: 0xffffff, map: texture }; - - cube_count = 0; - - for (i = 0; i < xgrid; i++) { - for (j = 0; j < ygrid; j++) { - ox = i; - oy = j; - - geometry = new THREE.BoxGeometry(xsize, ysize, xsize); - - change_uvs(geometry, ux, uy, ox, oy); - - materials[cube_count] = new THREE.MeshLambertMaterial(parameters); - - material = materials[cube_count]; - - material.hue = i / xgrid; - material.saturation = 1 - j / ygrid; - - material.color.setHSL(material.hue, material.saturation, 0.5); - - mesh = new THREE.Mesh(geometry, material); - - mesh.position.x = (i - xgrid / 2) * xsize; - mesh.position.y = (j - ygrid / 2) * ysize; - mesh.position.z = 0; - - mesh.scale.x = mesh.scale.y = mesh.scale.z = 1; - - scene.add(mesh); - - mesh.dx = 0.001 * (0.5 - Math.random()); - mesh.dy = 0.001 * (0.5 - Math.random()); - - meshes[cube_count] = mesh; - - cube_count += 1; - } - } - - renderer.autoClear = false; - - document.addEventListener('mousemove', onDocumentMouseMove); - - // postprocessing - - const renderPass = new RenderPass(scene, camera); - const bloomPass = new BloomPass(1.3); - const outputPass = new OutputPass(); - - composer = new EffectComposer(renderer); - - composer.addPass(renderPass); - composer.addPass(bloomPass); - composer.addPass(outputPass); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - composer.setSize(window.innerWidth, window.innerHeight); -} - -function change_uvs(geometry, unitx, unity, offsetx, offsety) { - const uvs = geometry.attributes.uv.array; - - for (let i = 0; i < uvs.length; i += 2) { - uvs[i] = (uvs[i] + offsetx) * unitx; - uvs[i + 1] = (uvs[i + 1] + offsety) * unity; - } -} - -function onDocumentMouseMove(event) { - mouseX = event.clientX - windowHalfX; - mouseY = (event.clientY - windowHalfY) * 0.3; -} - -// - -let h, - counter = 1; - -function animate() { - const time = Date.now() * 0.00005; - - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-mouseY - camera.position.y) * 0.05; - - camera.lookAt(scene.position); - - for (let i = 0; i < cube_count; i++) { - material = materials[i]; - - h = ((360 * (material.hue + time)) % 360) / 360; - material.color.setHSL(h, material.saturation, 0.5); - } - - if (counter % 1000 > 200) { - for (let i = 0; i < cube_count; i++) { - mesh = meshes[i]; - - mesh.rotation.x += 10 * mesh.dx; - mesh.rotation.y += 10 * mesh.dy; - - mesh.position.x -= 150 * mesh.dx; - mesh.position.y += 150 * mesh.dy; - mesh.position.z += 300 * mesh.dx; - } - } - - if (counter % 1000 === 0) { - for (let i = 0; i < cube_count; i++) { - mesh = meshes[i]; - - mesh.dx *= -1; - mesh.dy *= -1; - } - } - - counter++; - - renderer.clear(); - composer.render(); -} diff --git a/examples-testing/examples/webgl_materials_video_webcam.ts b/examples-testing/examples/webgl_materials_video_webcam.ts deleted file mode 100644 index cf6f8d50c..000000000 --- a/examples-testing/examples/webgl_materials_video_webcam.ts +++ /dev/null @@ -1,79 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer, video; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.z = 0.01; - - scene = new THREE.Scene(); - - video = document.getElementById('video'); - - const texture = new THREE.VideoTexture(video); - texture.colorSpace = THREE.SRGBColorSpace; - - const geometry = new THREE.PlaneGeometry(16, 9); - geometry.scale(0.5, 0.5, 0.5); - const material = new THREE.MeshBasicMaterial({ map: texture }); - - const count = 128; - const radius = 32; - - for (let i = 1, l = count; i <= l; i++) { - const phi = Math.acos(-1 + (2 * i) / l); - const theta = Math.sqrt(l * Math.PI) * phi; - - const mesh = new THREE.Mesh(geometry, material); - mesh.position.setFromSphericalCoords(radius, phi, theta); - mesh.lookAt(camera.position); - scene.add(mesh); - } - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.enableZoom = false; - controls.enablePan = false; - - window.addEventListener('resize', onWindowResize); - - // - - if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { - const constraints = { video: { width: 1280, height: 720, facingMode: 'user' } }; - - navigator.mediaDevices - .getUserMedia(constraints) - .then(function (stream) { - // apply the stream to the video element used in the texture - - video.srcObject = stream; - video.play(); - }) - .catch(function (error) { - console.error('Unable to access the camera/webcam.', error); - }); - } else { - console.error('MediaDevices interface not available.'); - } -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_materials_wireframe.ts b/examples-testing/examples/webgl_materials_wireframe.ts deleted file mode 100644 index 8adbd71d6..000000000 --- a/examples-testing/examples/webgl_materials_wireframe.ts +++ /dev/null @@ -1,107 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -const API = { - thickness: 1, -}; - -let renderer, scene, camera, mesh2; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 500); - camera.position.z = 200; - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.enablePan = false; - controls.enableZoom = false; - - new THREE.BufferGeometryLoader().load('models/json/WaltHeadLo_buffergeometry.json', function (geometry) { - geometry.deleteAttribute('normal'); - geometry.deleteAttribute('uv'); - - setupAttributes(geometry); - - // left - - const material1 = new THREE.MeshBasicMaterial({ - color: 0xe0e0ff, - wireframe: true, - }); - - const mesh1 = new THREE.Mesh(geometry, material1); - mesh1.position.set(-40, 0, 0); - - scene.add(mesh1); - - // right - - const material2 = new THREE.ShaderMaterial({ - uniforms: { thickness: { value: API.thickness } }, - vertexShader: document.getElementById('vertexShader').textContent, - fragmentShader: document.getElementById('fragmentShader').textContent, - side: THREE.DoubleSide, - alphaToCoverage: true, // only works when WebGLRenderer's "antialias" is set to "true" - }); - - mesh2 = new THREE.Mesh(geometry, material2); - mesh2.position.set(40, 0, 0); - - scene.add(mesh2); - - // - - render(); - }); - - // - - const gui = new GUI(); - - gui.add(API, 'thickness', 0, 4).onChange(function () { - mesh2.material.uniforms.thickness.value = API.thickness; - render(); - }); - - gui.open(); - - // - - window.addEventListener('resize', onWindowResize); -} - -function setupAttributes(geometry) { - const vectors = [new THREE.Vector3(1, 0, 0), new THREE.Vector3(0, 1, 0), new THREE.Vector3(0, 0, 1)]; - - const position = geometry.attributes.position; - const centers = new Float32Array(position.count * 3); - - for (let i = 0, l = position.count; i < l; i++) { - vectors[i % 3].toArray(centers, i * 3); - } - - geometry.setAttribute('center', new THREE.BufferAttribute(centers, 3)); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_math_obb.ts b/examples-testing/examples/webgl_math_obb.ts deleted file mode 100644 index 48480d10b..000000000 --- a/examples-testing/examples/webgl_math_obb.ts +++ /dev/null @@ -1,189 +0,0 @@ -import * as THREE from 'three'; - -import { OBB } from 'three/addons/math/OBB.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let camera, scene, renderer, clock, controls, stats, raycaster, hitbox; - -const objects = [], - mouse = new THREE.Vector2(); - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 0, 75); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - - clock = new THREE.Clock(); - - raycaster = new THREE.Raycaster(); - - const hemiLight = new THREE.HemisphereLight(0xffffff, 0x222222, 4); - hemiLight.position.set(1, 1, 1); - scene.add(hemiLight); - - const size = new THREE.Vector3(10, 5, 6); - const geometry = new THREE.BoxGeometry(size.x, size.y, size.z); - - // setup OBB on geometry level (doing this manually for now) - - geometry.userData.obb = new OBB(); - geometry.userData.obb.halfSize.copy(size).multiplyScalar(0.5); - - for (let i = 0; i < 100; i++) { - const object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: 0x00ff00 })); - object.matrixAutoUpdate = false; - - object.position.x = Math.random() * 80 - 40; - object.position.y = Math.random() * 80 - 40; - object.position.z = Math.random() * 80 - 40; - - object.rotation.x = Math.random() * 2 * Math.PI; - object.rotation.y = Math.random() * 2 * Math.PI; - object.rotation.z = Math.random() * 2 * Math.PI; - - object.scale.x = Math.random() + 0.5; - object.scale.y = Math.random() + 0.5; - object.scale.z = Math.random() + 0.5; - - scene.add(object); - - // bounding volume on object level (this will reflect the current world transform) - - object.userData.obb = new OBB(); - - objects.push(object); - } - - // - - hitbox = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ color: 0x000000, wireframe: true })); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - controls = new OrbitControls(camera, renderer.domElement); - controls.enableDamping = true; - - // - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); - - document.addEventListener('click', onClick); -} - -function onClick(event) { - event.preventDefault(); - - mouse.x = (event.clientX / window.innerWidth) * 2 - 1; - mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; - - raycaster.setFromCamera(mouse, camera); - - const intersectionPoint = new THREE.Vector3(); - const intersections = []; - - for (let i = 0, il = objects.length; i < il; i++) { - const object = objects[i]; - const obb = object.userData.obb; - - const ray = raycaster.ray; - - if (obb.intersectRay(ray, intersectionPoint) !== null) { - const distance = ray.origin.distanceTo(intersectionPoint); - intersections.push({ distance: distance, object: object }); - } - } - - if (intersections.length > 0) { - // determine closest intersection and highlight the respective 3D object - - intersections.sort(sortIntersections); - - intersections[0].object.add(hitbox); - } else { - const parent = hitbox.parent; - - if (parent) parent.remove(hitbox); - } -} - -function sortIntersections(a, b) { - return a.distance - b.distance; -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - controls.update(); - - // transform cubes - - const delta = clock.getDelta(); - - for (let i = 0, il = objects.length; i < il; i++) { - const object = objects[i]; - - object.rotation.x += delta * Math.PI * 0.2; - object.rotation.y += delta * Math.PI * 0.1; - - object.updateMatrix(); - object.updateMatrixWorld(); - - // update OBB - - object.userData.obb.copy(object.geometry.userData.obb); - object.userData.obb.applyMatrix4(object.matrixWorld); - - // reset - - object.material.color.setHex(0x00ff00); - } - - // collision detection - - for (let i = 0, il = objects.length; i < il; i++) { - const object = objects[i]; - const obb = object.userData.obb; - - for (let j = i + 1, jl = objects.length; j < jl; j++) { - const objectToTest = objects[j]; - const obbToTest = objectToTest.userData.obb; - - // now perform intersection test - - if (obb.intersectsOBB(obbToTest) === true) { - object.material.color.setHex(0xff0000); - objectToTest.material.color.setHex(0xff0000); - } - } - } - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_math_orientation_transform.ts b/examples-testing/examples/webgl_math_orientation_transform.ts deleted file mode 100644 index 99be247d8..000000000 --- a/examples-testing/examples/webgl_math_orientation_transform.ts +++ /dev/null @@ -1,95 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer, mesh, target; - -const spherical = new THREE.Spherical(); -const rotationMatrix = new THREE.Matrix4(); -const targetQuaternion = new THREE.Quaternion(); -const clock = new THREE.Clock(); -const speed = 2; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10); - camera.position.z = 5; - - scene = new THREE.Scene(); - - const geometry = new THREE.ConeGeometry(0.1, 0.5, 8); - geometry.rotateX(Math.PI * 0.5); - const material = new THREE.MeshNormalMaterial(); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - const targetGeometry = new THREE.SphereGeometry(0.05); - const targetMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 }); - target = new THREE.Mesh(targetGeometry, targetMaterial); - scene.add(target); - - // - - const sphereGeometry = new THREE.SphereGeometry(2, 32, 32); - const sphereMaterial = new THREE.MeshBasicMaterial({ - color: 0xcccccc, - wireframe: true, - transparent: true, - opacity: 0.3, - }); - const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); - scene.add(sphere); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - window.addEventListener('resize', onWindowResize); - - // - - generateTarget(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const delta = clock.getDelta(); - - if (!mesh.quaternion.equals(targetQuaternion)) { - const step = speed * delta; - mesh.quaternion.rotateTowards(targetQuaternion, step); - } - - renderer.render(scene, camera); -} - -function generateTarget() { - // generate a random point on a sphere - - spherical.theta = Math.random() * Math.PI * 2; - spherical.phi = Math.acos(2 * Math.random() - 1); - spherical.radius = 2; - - target.position.setFromSpherical(spherical); - - // compute target rotation - - rotationMatrix.lookAt(target.position, mesh.position, mesh.up); - targetQuaternion.setFromRotationMatrix(rotationMatrix); - - setTimeout(generateTarget, 2000); -} diff --git a/examples-testing/examples/webgl_mesh_batch.ts b/examples-testing/examples/webgl_mesh_batch.ts deleted file mode 100644 index f93e5fb85..000000000 --- a/examples-testing/examples/webgl_mesh_batch.ts +++ /dev/null @@ -1,305 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { radixSort } from 'three/addons/utils/SortUtils.js'; - -let stats, gui, guiStatsEl; -let camera, controls, scene, renderer; -let geometries, mesh, material; -const ids = []; -const matrix = new THREE.Matrix4(); - -// - -const position = new THREE.Vector3(); -const rotation = new THREE.Euler(); -const quaternion = new THREE.Quaternion(); -const scale = new THREE.Vector3(); - -// - -const MAX_GEOMETRY_COUNT = 20000; - -const Method = { - BATCHED: 'BATCHED', - NAIVE: 'NAIVE', -}; - -const api = { - method: Method.BATCHED, - count: 256, - dynamic: 16, - - sortObjects: true, - perObjectFrustumCulled: true, - opacity: 1, - useCustomSort: true, -}; - -init(); -initGeometries(); -initMesh(); - -// - -function randomizeMatrix(matrix) { - position.x = Math.random() * 40 - 20; - position.y = Math.random() * 40 - 20; - position.z = Math.random() * 40 - 20; - - rotation.x = Math.random() * 2 * Math.PI; - rotation.y = Math.random() * 2 * Math.PI; - rotation.z = Math.random() * 2 * Math.PI; - - quaternion.setFromEuler(rotation); - - scale.x = scale.y = scale.z = 0.5 + Math.random() * 0.5; - - return matrix.compose(position, quaternion, scale); -} - -function randomizeRotationSpeed(rotation) { - rotation.x = Math.random() * 0.01; - rotation.y = Math.random() * 0.01; - rotation.z = Math.random() * 0.01; - return rotation; -} - -function initGeometries() { - geometries = [ - new THREE.ConeGeometry(1.0, 2.0), - new THREE.BoxGeometry(2.0, 2.0, 2.0), - new THREE.SphereGeometry(1.0, 16, 8), - ]; -} - -function createMaterial() { - if (!material) { - material = new THREE.MeshNormalMaterial(); - } - - return material; -} - -function cleanup() { - if (mesh) { - mesh.parent.remove(mesh); - - if (mesh.dispose) { - mesh.dispose(); - } - } -} - -function initMesh() { - cleanup(); - - if (api.method === Method.BATCHED) { - initBatchedMesh(); - } else { - initRegularMesh(); - } -} - -function initRegularMesh() { - mesh = new THREE.Group(); - const material = createMaterial(); - - for (let i = 0; i < api.count; i++) { - const child = new THREE.Mesh(geometries[i % geometries.length], material); - randomizeMatrix(child.matrix); - child.matrix.decompose(child.position, child.quaternion, child.scale); - child.userData.rotationSpeed = randomizeRotationSpeed(new THREE.Euler()); - mesh.add(child); - } - - scene.add(mesh); -} - -function initBatchedMesh() { - const geometryCount = api.count; - const vertexCount = geometries.length * 512; - const indexCount = geometries.length * 1024; - - const euler = new THREE.Euler(); - const matrix = new THREE.Matrix4(); - mesh = new THREE.BatchedMesh(geometryCount, vertexCount, indexCount, createMaterial()); - mesh.userData.rotationSpeeds = []; - - // disable full-object frustum culling since all of the objects can be dynamic. - mesh.frustumCulled = false; - - ids.length = 0; - - const geometryIds = [ - mesh.addGeometry(geometries[0]), - mesh.addGeometry(geometries[1]), - mesh.addGeometry(geometries[2]), - ]; - - for (let i = 0; i < api.count; i++) { - const id = mesh.addInstance(geometryIds[i % geometryIds.length]); - mesh.setMatrixAt(id, randomizeMatrix(matrix)); - - const rotationMatrix = new THREE.Matrix4(); - rotationMatrix.makeRotationFromEuler(randomizeRotationSpeed(euler)); - mesh.userData.rotationSpeeds.push(rotationMatrix); - - ids.push(id); - } - - scene.add(mesh); -} - -function init() { - const width = window.innerWidth; - const height = window.innerHeight; - - // camera - - camera = new THREE.PerspectiveCamera(70, width / height, 1, 100); - camera.position.z = 30; - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(width, height); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // scene - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - - // controls - - controls = new OrbitControls(camera, renderer.domElement); - controls.autoRotate = true; - controls.autoRotateSpeed = 1.0; - - // stats - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // gui - - gui = new GUI(); - gui.add(api, 'count', 1, MAX_GEOMETRY_COUNT).step(1).onChange(initMesh); - gui.add(api, 'dynamic', 0, MAX_GEOMETRY_COUNT).step(1); - gui.add(api, 'method', Method).onChange(initMesh); - gui.add(api, 'opacity', 0, 1).onChange(v => { - if (v < 1) { - material.transparent = true; - material.depthWrite = false; - } else { - material.transparent = false; - material.depthWrite = true; - } - - material.opacity = v; - material.needsUpdate = true; - }); - gui.add(api, 'sortObjects'); - gui.add(api, 'perObjectFrustumCulled'); - gui.add(api, 'useCustomSort'); - - guiStatsEl = document.createElement('li'); - guiStatsEl.classList.add('gui-stats'); - - // listeners - - window.addEventListener('resize', onWindowResize); -} - -// - -function sortFunction(list) { - // initialize options - this._options = this._options || { - get: el => el.z, - aux: new Array(this.maxInstanceCount), - }; - - const options = this._options; - options.reversed = this.material.transparent; - - let minZ = Infinity; - let maxZ = -Infinity; - for (let i = 0, l = list.length; i < l; i++) { - const z = list[i].z; - if (z > maxZ) maxZ = z; - if (z < minZ) minZ = z; - } - - // convert depth to unsigned 32 bit range - const depthDelta = maxZ - minZ; - const factor = (2 ** 32 - 1) / depthDelta; // UINT32_MAX / z range - for (let i = 0, l = list.length; i < l; i++) { - list[i].z -= minZ; - list[i].z *= factor; - } - - // perform a fast-sort using the hybrid radix sort function - radixSort(list, options); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); -} - -function animate() { - animateMeshes(); - - controls.update(); - stats.update(); - - render(); -} - -function animateMeshes() { - const loopNum = Math.min(api.count, api.dynamic); - - if (api.method === Method.BATCHED) { - for (let i = 0; i < loopNum; i++) { - const rotationMatrix = mesh.userData.rotationSpeeds[i]; - const id = ids[i]; - - mesh.getMatrixAt(id, matrix); - matrix.multiply(rotationMatrix); - mesh.setMatrixAt(id, matrix); - } - } else { - for (let i = 0; i < loopNum; i++) { - const child = mesh.children[i]; - const rotationSpeed = child.userData.rotationSpeed; - - child.rotation.set( - child.rotation.x + rotationSpeed.x, - child.rotation.y + rotationSpeed.y, - child.rotation.z + rotationSpeed.z, - ); - } - } -} - -function render() { - if (mesh.isBatchedMesh) { - mesh.sortObjects = api.sortObjects; - mesh.perObjectFrustumCulled = api.perObjectFrustumCulled; - mesh.setCustomSort(api.useCustomSort ? sortFunction : null); - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_mirror.ts b/examples-testing/examples/webgl_mirror.ts deleted file mode 100644 index 8b27363a8..000000000 --- a/examples-testing/examples/webgl_mirror.ts +++ /dev/null @@ -1,168 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { Reflector } from 'three/addons/objects/Reflector.js'; - -let camera, scene, renderer; - -let cameraControls; - -let sphereGroup, smallSphere; - -let groundMirror, verticalMirror; - -init(); - -function init() { - const container = document.getElementById('container'); - - // renderer - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // scene - scene = new THREE.Scene(); - - // camera - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 500); - camera.position.set(0, 75, 160); - - cameraControls = new OrbitControls(camera, renderer.domElement); - cameraControls.target.set(0, 40, 0); - cameraControls.maxDistance = 400; - cameraControls.minDistance = 10; - cameraControls.update(); - - // - - const planeGeo = new THREE.PlaneGeometry(100.1, 100.1); - - // reflectors/mirrors - - let geometry, material; - - geometry = new THREE.CircleGeometry(40, 64); - groundMirror = new Reflector(geometry, { - clipBias: 0.003, - textureWidth: window.innerWidth * window.devicePixelRatio, - textureHeight: window.innerHeight * window.devicePixelRatio, - color: 0xb5b5b5, - }); - groundMirror.position.y = 0.5; - groundMirror.rotateX(-Math.PI / 2); - scene.add(groundMirror); - - geometry = new THREE.PlaneGeometry(100, 100); - verticalMirror = new Reflector(geometry, { - clipBias: 0.003, - textureWidth: window.innerWidth * window.devicePixelRatio, - textureHeight: window.innerHeight * window.devicePixelRatio, - color: 0xc1cbcb, - }); - verticalMirror.position.y = 50; - verticalMirror.position.z = -50; - scene.add(verticalMirror); - - sphereGroup = new THREE.Object3D(); - scene.add(sphereGroup); - - geometry = new THREE.CylinderGeometry(0.1, 15 * Math.cos((Math.PI / 180) * 30), 0.1, 24, 1); - material = new THREE.MeshPhongMaterial({ color: 0xffffff, emissive: 0x8d8d8d }); - const sphereCap = new THREE.Mesh(geometry, material); - sphereCap.position.y = -15 * Math.sin((Math.PI / 180) * 30) - 0.05; - sphereCap.rotateX(-Math.PI); - - geometry = new THREE.SphereGeometry(15, 24, 24, Math.PI / 2, Math.PI * 2, 0, (Math.PI / 180) * 120); - const halfSphere = new THREE.Mesh(geometry, material); - halfSphere.add(sphereCap); - halfSphere.rotateX((-Math.PI / 180) * 135); - halfSphere.rotateZ((-Math.PI / 180) * 20); - halfSphere.position.y = 7.5 + 15 * Math.sin((Math.PI / 180) * 30); - - sphereGroup.add(halfSphere); - - geometry = new THREE.IcosahedronGeometry(5, 0); - material = new THREE.MeshPhongMaterial({ color: 0xffffff, emissive: 0x7b7b7b, flatShading: true }); - smallSphere = new THREE.Mesh(geometry, material); - scene.add(smallSphere); - - // walls - const planeTop = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); - planeTop.position.y = 100; - planeTop.rotateX(Math.PI / 2); - scene.add(planeTop); - - const planeBottom = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); - planeBottom.rotateX(-Math.PI / 2); - scene.add(planeBottom); - - const planeFront = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x7f7fff })); - planeFront.position.z = 50; - planeFront.position.y = 50; - planeFront.rotateY(Math.PI); - scene.add(planeFront); - - const planeRight = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x00ff00 })); - planeRight.position.x = 50; - planeRight.position.y = 50; - planeRight.rotateY(-Math.PI / 2); - scene.add(planeRight); - - const planeLeft = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xff0000 })); - planeLeft.position.x = -50; - planeLeft.position.y = 50; - planeLeft.rotateY(Math.PI / 2); - scene.add(planeLeft); - - // lights - const mainLight = new THREE.PointLight(0xe7e7e7, 2.5, 250, 0); - mainLight.position.y = 60; - scene.add(mainLight); - - const greenLight = new THREE.PointLight(0x00ff00, 0.5, 1000, 0); - greenLight.position.set(550, 50, 0); - scene.add(greenLight); - - const redLight = new THREE.PointLight(0xff0000, 0.5, 1000, 0); - redLight.position.set(-550, 50, 0); - scene.add(redLight); - - const blueLight = new THREE.PointLight(0xbbbbfe, 0.5, 1000, 0); - blueLight.position.set(0, 50, 550); - scene.add(blueLight); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - groundMirror - .getRenderTarget() - .setSize(window.innerWidth * window.devicePixelRatio, window.innerHeight * window.devicePixelRatio); - verticalMirror - .getRenderTarget() - .setSize(window.innerWidth * window.devicePixelRatio, window.innerHeight * window.devicePixelRatio); -} - -function animate() { - const timer = Date.now() * 0.01; - - sphereGroup.rotation.y -= 0.002; - - smallSphere.position.set( - Math.cos(timer * 0.1) * 30, - Math.abs(Math.cos(timer * 0.2)) * 20 + 5, - Math.sin(timer * 0.1) * 30, - ); - smallSphere.rotation.y = Math.PI / 2 - timer * 0.1; - smallSphere.rotation.z = timer * 0.8; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_modifier_edgesplit.ts b/examples-testing/examples/webgl_modifier_edgesplit.ts deleted file mode 100644 index 4725eff62..000000000 --- a/examples-testing/examples/webgl_modifier_edgesplit.ts +++ /dev/null @@ -1,136 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; -import { EdgeSplitModifier } from 'three/addons/modifiers/EdgeSplitModifier.js'; -import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let renderer, scene, camera; -let modifier, mesh, baseGeometry; -let map; - -const params = { - smoothShading: true, - edgeSplit: true, - cutOffAngle: 20, - showMap: false, - tryKeepNormals: true, -}; - -init(); - -function init() { - const info = document.createElement('div'); - info.style.position = 'absolute'; - info.style.top = '10px'; - info.style.width = '100%'; - info.style.textAlign = 'center'; - info.innerHTML = 'three.js - Edge Split modifier'; - document.body.appendChild(info); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.enableDamping = true; - controls.dampingFactor = 0.25; - controls.rotateSpeed = 0.35; - controls.minZoom = 1; - camera.position.set(0, 0, 4); - - scene.add(new THREE.HemisphereLight(0xffffff, 0x444444, 3)); - - new OBJLoader().load('./models/obj/cerberus/Cerberus.obj', function (group) { - const cerberus = group.children[0]; - const modelGeometry = cerberus.geometry; - - modifier = new EdgeSplitModifier(); - baseGeometry = BufferGeometryUtils.mergeVertices(modelGeometry); - - mesh = new THREE.Mesh(getGeometry(), new THREE.MeshStandardMaterial()); - mesh.material.flatShading = !params.smoothShading; - mesh.rotateY(-Math.PI / 2); - mesh.scale.set(3.5, 3.5, 3.5); - mesh.translateZ(1.5); - scene.add(mesh); - - if (map !== undefined && params.showMap) { - mesh.material.map = map; - mesh.material.needsUpdate = true; - } - - render(); - }); - - window.addEventListener('resize', onWindowResize); - - new THREE.TextureLoader().load('./models/obj/cerberus/Cerberus_A.jpg', function (texture) { - map = texture; - map.colorSpace = THREE.SRGBColorSpace; - - if (mesh !== undefined && params.showMap) { - mesh.material.map = map; - mesh.material.needsUpdate = true; - } - }); - - const gui = new GUI({ title: 'Edge split modifier parameters' }); - - gui.add(params, 'showMap').onFinishChange(updateMesh); - gui.add(params, 'smoothShading').onFinishChange(updateMesh); - gui.add(params, 'edgeSplit').onFinishChange(updateMesh); - gui.add(params, 'cutOffAngle').min(0).max(180).onFinishChange(updateMesh); - gui.add(params, 'tryKeepNormals').onFinishChange(updateMesh); -} - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - render(); -} - -function getGeometry() { - let geometry; - - if (params.edgeSplit) { - geometry = modifier.modify(baseGeometry, (params.cutOffAngle * Math.PI) / 180, params.tryKeepNormals); - } else { - geometry = baseGeometry; - } - - return geometry; -} - -function updateMesh() { - if (mesh !== undefined) { - mesh.geometry = getGeometry(); - - let needsUpdate = mesh.material.flatShading === params.smoothShading; - mesh.material.flatShading = params.smoothShading === false; - - if (map !== undefined) { - needsUpdate = needsUpdate || mesh.material.map !== (params.showMap ? map : null); - mesh.material.map = params.showMap ? map : null; - } - - mesh.material.needsUpdate = needsUpdate; - - render(); - } -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_modifier_simplifier.ts b/examples-testing/examples/webgl_modifier_simplifier.ts deleted file mode 100644 index e6ea453b3..000000000 --- a/examples-testing/examples/webgl_modifier_simplifier.ts +++ /dev/null @@ -1,77 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { SimplifyModifier } from 'three/addons/modifiers/SimplifyModifier.js'; - -let renderer, scene, camera; - -init(); - -function init() { - const info = document.createElement('div'); - info.style.position = 'absolute'; - info.style.top = '10px'; - info.style.width = '100%'; - info.style.textAlign = 'center'; - info.innerHTML = - 'three.js - Vertex Reduction using SimplifyModifier'; - document.body.appendChild(info); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 15; - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.enablePan = false; - controls.enableZoom = false; - - scene.add(new THREE.AmbientLight(0xffffff, 0.6)); - - const light = new THREE.PointLight(0xffffff, 400); - camera.add(light); - scene.add(camera); - - new GLTFLoader().load('models/gltf/LeePerrySmith/LeePerrySmith.glb', function (gltf) { - const mesh = gltf.scene.children[0]; - mesh.position.x = -3; - mesh.rotation.y = Math.PI / 2; - scene.add(mesh); - - const modifier = new SimplifyModifier(); - - const simplified = mesh.clone(); - simplified.material = simplified.material.clone(); - simplified.material.flatShading = true; - const count = Math.floor(simplified.geometry.attributes.position.count * 0.875); // number of vertices to remove - simplified.geometry = modifier.modify(simplified.geometry, count); - - simplified.position.x = 3; - simplified.rotation.y = -Math.PI / 2; - scene.add(simplified); - - render(); - }); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_modifier_tessellation.ts b/examples-testing/examples/webgl_modifier_tessellation.ts deleted file mode 100644 index 4600fc6cb..000000000 --- a/examples-testing/examples/webgl_modifier_tessellation.ts +++ /dev/null @@ -1,142 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { TrackballControls } from 'three/addons/controls/TrackballControls.js'; -import { TessellateModifier } from 'three/addons/modifiers/TessellateModifier.js'; -import { FontLoader } from 'three/addons/loaders/FontLoader.js'; -import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; - -let renderer, scene, camera, stats; - -let controls; - -let mesh, uniforms; - -const WIDTH = window.innerWidth; -const HEIGHT = window.innerHeight; - -const loader = new FontLoader(); -loader.load('fonts/helvetiker_bold.typeface.json', function (font) { - init(font); -}); - -function init(font) { - camera = new THREE.PerspectiveCamera(40, WIDTH / HEIGHT, 1, 10000); - camera.position.set(-100, 100, 200); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x050505); - - // - - let geometry = new TextGeometry('THREE.JS', { - font: font, - - size: 40, - depth: 5, - curveSegments: 3, - - bevelThickness: 2, - bevelSize: 1, - bevelEnabled: true, - }); - - geometry.center(); - - const tessellateModifier = new TessellateModifier(8, 6); - - geometry = tessellateModifier.modify(geometry); - - // - - const numFaces = geometry.attributes.position.count / 3; - - const colors = new Float32Array(numFaces * 3 * 3); - const displacement = new Float32Array(numFaces * 3 * 3); - - const color = new THREE.Color(); - - for (let f = 0; f < numFaces; f++) { - const index = 9 * f; - - const h = 0.2 * Math.random(); - const s = 0.5 + 0.5 * Math.random(); - const l = 0.5 + 0.5 * Math.random(); - - color.setHSL(h, s, l); - - const d = 10 * (0.5 - Math.random()); - - for (let i = 0; i < 3; i++) { - colors[index + 3 * i] = color.r; - colors[index + 3 * i + 1] = color.g; - colors[index + 3 * i + 2] = color.b; - - displacement[index + 3 * i] = d; - displacement[index + 3 * i + 1] = d; - displacement[index + 3 * i + 2] = d; - } - } - - geometry.setAttribute('customColor', new THREE.BufferAttribute(colors, 3)); - geometry.setAttribute('displacement', new THREE.BufferAttribute(displacement, 3)); - - // - - uniforms = { - amplitude: { value: 0.0 }, - }; - - const shaderMaterial = new THREE.ShaderMaterial({ - uniforms: uniforms, - vertexShader: document.getElementById('vertexshader').textContent, - fragmentShader: document.getElementById('fragmentshader').textContent, - }); - - // - - mesh = new THREE.Mesh(geometry, shaderMaterial); - - scene.add(mesh); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(WIDTH, HEIGHT); - renderer.setAnimationLoop(animate); - - const container = document.getElementById('container'); - container.appendChild(renderer.domElement); - - controls = new TrackballControls(camera, renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - render(); - - stats.update(); -} - -function render() { - const time = Date.now() * 0.001; - - uniforms.amplitude.value = 1.0 + Math.sin(time * 0.5); - - controls.update(); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_morphtargets.ts b/examples-testing/examples/webgl_morphtargets.ts deleted file mode 100644 index 40d605f8d..000000000 --- a/examples-testing/examples/webgl_morphtargets.ts +++ /dev/null @@ -1,120 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let container, camera, scene, renderer, mesh; - -init(); - -function init() { - container = document.getElementById('container'); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x8fbcd4); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 20); - camera.position.z = 10; - scene.add(camera); - - scene.add(new THREE.AmbientLight(0x8fbcd4, 1.5)); - - const pointLight = new THREE.PointLight(0xffffff, 200); - camera.add(pointLight); - - const geometry = createGeometry(); - - const material = new THREE.MeshPhongMaterial({ - color: 0xff0000, - flatShading: true, - }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - initGUI(); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(function () { - renderer.render(scene, camera); - }); - container.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.enableZoom = false; - - window.addEventListener('resize', onWindowResize); -} - -function createGeometry() { - const geometry = new THREE.BoxGeometry(2, 2, 2, 32, 32, 32); - - // create an empty array to hold targets for the attribute we want to morph - // morphing positions and normals is supported - geometry.morphAttributes.position = []; - - // the original positions of the cube's vertices - const positionAttribute = geometry.attributes.position; - - // for the first morph target we'll move the cube's vertices onto the surface of a sphere - const spherePositions = []; - - // for the second morph target, we'll twist the cubes vertices - const twistPositions = []; - const direction = new THREE.Vector3(1, 0, 0); - const vertex = new THREE.Vector3(); - - for (let i = 0; i < positionAttribute.count; i++) { - const x = positionAttribute.getX(i); - const y = positionAttribute.getY(i); - const z = positionAttribute.getZ(i); - - spherePositions.push( - x * Math.sqrt(1 - (y * y) / 2 - (z * z) / 2 + (y * y * z * z) / 3), - y * Math.sqrt(1 - (z * z) / 2 - (x * x) / 2 + (z * z * x * x) / 3), - z * Math.sqrt(1 - (x * x) / 2 - (y * y) / 2 + (x * x * y * y) / 3), - ); - - // stretch along the x-axis so we can see the twist better - vertex.set(x * 2, y, z); - - vertex.applyAxisAngle(direction, (Math.PI * x) / 2).toArray(twistPositions, twistPositions.length); - } - - // add the spherical positions as the first morph target - geometry.morphAttributes.position[0] = new THREE.Float32BufferAttribute(spherePositions, 3); - - // add the twisted positions as the second morph target - geometry.morphAttributes.position[1] = new THREE.Float32BufferAttribute(twistPositions, 3); - - return geometry; -} - -function initGUI() { - // Set up dat.GUI to control targets - const params = { - Spherify: 0, - Twist: 0, - }; - const gui = new GUI({ title: 'Morph Targets' }); - - gui.add(params, 'Spherify', 0, 1) - .step(0.01) - .onChange(function (value) { - mesh.morphTargetInfluences[0] = value; - }); - gui.add(params, 'Twist', 0, 1) - .step(0.01) - .onChange(function (value) { - mesh.morphTargetInfluences[1] = value; - }); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} diff --git a/examples-testing/examples/webgl_morphtargets_face.ts b/examples-testing/examples/webgl_morphtargets_face.ts deleted file mode 100644 index 76179d902..000000000 --- a/examples-testing/examples/webgl_morphtargets_face.ts +++ /dev/null @@ -1,105 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js'; -import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js'; - -import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, stats, mixer, clock, controls; - -init(); - -function init() { - clock = new THREE.Clock(); - - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 20); - camera.position.set(-1.8, 0.8, 3); - - scene = new THREE.Scene(); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - - container.appendChild(renderer.domElement); - - const ktx2Loader = new KTX2Loader().setTranscoderPath('jsm/libs/basis/').detectSupport(renderer); - - new GLTFLoader() - .setKTX2Loader(ktx2Loader) - .setMeshoptDecoder(MeshoptDecoder) - .load('models/gltf/facecap.glb', gltf => { - const mesh = gltf.scene.children[0]; - - scene.add(mesh); - - mixer = new THREE.AnimationMixer(mesh); - - mixer.clipAction(gltf.animations[0]).play(); - - // GUI - - const head = mesh.getObjectByName('mesh_2'); - const influences = head.morphTargetInfluences; - - const gui = new GUI(); - gui.close(); - - for (const [key, value] of Object.entries(head.morphTargetDictionary)) { - gui.add(influences, value, 0, 1, 0.01).name(key.replace('blendShape1.', '')).listen(); - } - }); - - const environment = new RoomEnvironment(); - const pmremGenerator = new THREE.PMREMGenerator(renderer); - - scene.background = new THREE.Color(0x666666); - scene.environment = pmremGenerator.fromScene(environment).texture; - - controls = new OrbitControls(camera, renderer.domElement); - controls.enableDamping = true; - controls.minDistance = 2.5; - controls.maxDistance = 5; - controls.minAzimuthAngle = -Math.PI / 2; - controls.maxAzimuthAngle = Math.PI / 2; - controls.maxPolarAngle = Math.PI / 1.8; - controls.target.set(0, 0.15, -0.2); - - stats = new Stats(); - container.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const delta = clock.getDelta(); - - if (mixer) { - mixer.update(delta); - } - - renderer.render(scene, camera); - - controls.update(); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_morphtargets_horse.ts b/examples-testing/examples/webgl_morphtargets_horse.ts deleted file mode 100644 index 2c29e9c0e..000000000 --- a/examples-testing/examples/webgl_morphtargets_horse.ts +++ /dev/null @@ -1,100 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -let container, stats; -let camera, scene, renderer; -let mesh, mixer; - -const radius = 600; -let theta = 0; -let prevTime = Date.now(); - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - // - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.y = 300; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xf0f0f0); - - // - - const light1 = new THREE.DirectionalLight(0xefefff, 5); - light1.position.set(1, 1, 1).normalize(); - scene.add(light1); - - const light2 = new THREE.DirectionalLight(0xffefef, 5); - light2.position.set(-1, -1, -1).normalize(); - scene.add(light2); - - const loader = new GLTFLoader(); - loader.load('models/gltf/Horse.glb', function (gltf) { - mesh = gltf.scene.children[0]; - mesh.scale.set(1.5, 1.5, 1.5); - scene.add(mesh); - - mixer = new THREE.AnimationMixer(mesh); - - mixer.clipAction(gltf.animations[0]).setDuration(1).play(); - }); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - - container.appendChild(renderer.domElement); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - theta += 0.1; - - camera.position.x = radius * Math.sin(THREE.MathUtils.degToRad(theta)); - camera.position.z = radius * Math.cos(THREE.MathUtils.degToRad(theta)); - - camera.lookAt(0, 150, 0); - - if (mixer) { - const time = Date.now(); - - mixer.update((time - prevTime) * 0.001); - - prevTime = time; - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_morphtargets_sphere.ts b/examples-testing/examples/webgl_morphtargets_sphere.ts deleted file mode 100644 index 2b8899111..000000000 --- a/examples-testing/examples/webgl_morphtargets_sphere.ts +++ /dev/null @@ -1,105 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { Timer } from 'three/addons/misc/Timer.js'; - -let camera, scene, renderer, timer; - -let mesh; - -let sign = 1; -const speed = 0.5; - -init(); - -function init() { - const container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.2, 100); - camera.position.set(0, 5, 5); - - scene = new THREE.Scene(); - - timer = new Timer(); - - const light1 = new THREE.PointLight(0xff2200, 50000); - light1.position.set(100, 100, 100); - scene.add(light1); - - const light2 = new THREE.PointLight(0x22ff00, 10000); - light2.position.set(-100, -100, -100); - scene.add(light2); - - scene.add(new THREE.AmbientLight(0x111111)); - - const loader = new GLTFLoader(); - loader.load('models/gltf/AnimatedMorphSphere/glTF/AnimatedMorphSphere.gltf', function (gltf) { - mesh = gltf.scene.getObjectByName('AnimatedMorphSphere'); - mesh.rotation.z = Math.PI / 2; - scene.add(mesh); - - // - - const pointsMaterial = new THREE.PointsMaterial({ - size: 10, - sizeAttenuation: false, - map: new THREE.TextureLoader().load('textures/sprites/disc.png'), - alphaTest: 0.5, - }); - - const points = new THREE.Points(mesh.geometry, pointsMaterial); - points.morphTargetInfluences = mesh.morphTargetInfluences; - points.morphTargetDictionary = mesh.morphTargetDictionary; - mesh.add(points); - }); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - - container.appendChild(renderer.domElement); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 1; - controls.maxDistance = 20; - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - timer.update(); - render(); -} - -function render() { - const delta = timer.getDelta(); - - if (mesh !== undefined) { - const step = delta * speed; - - mesh.rotation.y += step; - - mesh.morphTargetInfluences[1] = mesh.morphTargetInfluences[1] + step * sign; - - if (mesh.morphTargetInfluences[1] <= 0 || mesh.morphTargetInfluences[1] >= 1) { - sign *= -1; - } - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_multiple_elements.ts b/examples-testing/examples/webgl_multiple_elements.ts deleted file mode 100644 index 64f8a9c5f..000000000 --- a/examples-testing/examples/webgl_multiple_elements.ts +++ /dev/null @@ -1,139 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let canvas, renderer; - -const scenes = []; - -init(); - -function init() { - canvas = document.getElementById('c'); - - const geometries = [ - new THREE.BoxGeometry(1, 1, 1), - new THREE.SphereGeometry(0.5, 12, 8), - new THREE.DodecahedronGeometry(0.5), - new THREE.CylinderGeometry(0.5, 0.5, 1, 12), - ]; - - const content = document.getElementById('content'); - - for (let i = 0; i < 40; i++) { - const scene = new THREE.Scene(); - - // make a list item - const element = document.createElement('div'); - element.className = 'list-item'; - - const sceneElement = document.createElement('div'); - element.appendChild(sceneElement); - - const descriptionElement = document.createElement('div'); - descriptionElement.innerText = 'Scene ' + (i + 1); - element.appendChild(descriptionElement); - - // the element that represents the area we want to render the scene - scene.userData.element = sceneElement; - content.appendChild(element); - - const camera = new THREE.PerspectiveCamera(50, 1, 1, 10); - camera.position.z = 2; - scene.userData.camera = camera; - - const controls = new OrbitControls(scene.userData.camera, scene.userData.element); - controls.minDistance = 2; - controls.maxDistance = 5; - controls.enablePan = false; - controls.enableZoom = false; - scene.userData.controls = controls; - - // add one random mesh to each scene - const geometry = geometries[(geometries.length * Math.random()) | 0]; - - const material = new THREE.MeshStandardMaterial({ - color: new THREE.Color().setHSL(Math.random(), 1, 0.75, THREE.SRGBColorSpace), - roughness: 0.5, - metalness: 0, - flatShading: true, - }); - - scene.add(new THREE.Mesh(geometry, material)); - - scene.add(new THREE.HemisphereLight(0xaaaaaa, 0x444444, 3)); - - const light = new THREE.DirectionalLight(0xffffff, 1.5); - light.position.set(1, 1, 1); - scene.add(light); - - scenes.push(scene); - } - - renderer = new THREE.WebGLRenderer({ canvas: canvas, antialias: true }); - renderer.setClearColor(0xffffff, 1); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setAnimationLoop(animate); -} - -function updateSize() { - const width = canvas.clientWidth; - const height = canvas.clientHeight; - - if (canvas.width !== width || canvas.height !== height) { - renderer.setSize(width, height, false); - } -} - -function animate() { - updateSize(); - - canvas.style.transform = `translateY(${window.scrollY}px)`; - - renderer.setClearColor(0xffffff); - renderer.setScissorTest(false); - renderer.clear(); - - renderer.setClearColor(0xe0e0e0); - renderer.setScissorTest(true); - - scenes.forEach(function (scene) { - // so something moves - scene.children[0].rotation.y = Date.now() * 0.001; - - // get the element that is a place holder for where we want to - // draw the scene - const element = scene.userData.element; - - // get its position relative to the page's viewport - const rect = element.getBoundingClientRect(); - - // check if it's offscreen. If so skip it - if ( - rect.bottom < 0 || - rect.top > renderer.domElement.clientHeight || - rect.right < 0 || - rect.left > renderer.domElement.clientWidth - ) { - return; // it's off screen - } - - // set the viewport - const width = rect.right - rect.left; - const height = rect.bottom - rect.top; - const left = rect.left; - const bottom = renderer.domElement.clientHeight - rect.bottom; - - renderer.setViewport(left, bottom, width, height); - renderer.setScissor(left, bottom, width, height); - - const camera = scene.userData.camera; - - //camera.aspect = width / height; // not changing in this example - //camera.updateProjectionMatrix(); - - //scene.userData.controls.update(); - - renderer.render(scene, camera); - }); -} diff --git a/examples-testing/examples/webgl_multiple_rendertargets.ts b/examples-testing/examples/webgl_multiple_rendertargets.ts deleted file mode 100644 index 86708082b..000000000 --- a/examples-testing/examples/webgl_multiple_rendertargets.ts +++ /dev/null @@ -1,133 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, controls; -let renderTarget; -let postScene, postCamera; - -const parameters = { - samples: 4, - wireframe: false, -}; - -const gui = new GUI(); -gui.add(parameters, 'samples', 0, 4).step(1); -gui.add(parameters, 'wireframe'); -gui.onChange(render); - -init(); - -function init() { - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - // Create a multi render target with Float buffers - - renderTarget = new THREE.WebGLRenderTarget( - window.innerWidth * window.devicePixelRatio, - window.innerHeight * window.devicePixelRatio, - { - count: 2, - minFilter: THREE.NearestFilter, - magFilter: THREE.NearestFilter, - }, - ); - - // Name our G-Buffer attachments for debugging - - renderTarget.textures[0].name = 'diffuse'; - renderTarget.textures[1].name = 'normal'; - - // Scene setup - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x222222); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 50); - camera.position.z = 4; - - const loader = new THREE.TextureLoader(); - - const diffuse = loader.load('textures/hardwood2_diffuse.jpg', render); - diffuse.wrapS = THREE.RepeatWrapping; - diffuse.wrapT = THREE.RepeatWrapping; - diffuse.colorSpace = THREE.SRGBColorSpace; - - scene.add( - new THREE.Mesh( - new THREE.TorusKnotGeometry(1, 0.3, 128, 32), - new THREE.RawShaderMaterial({ - name: 'G-Buffer Shader', - vertexShader: document.querySelector('#gbuffer-vert').textContent.trim(), - fragmentShader: document.querySelector('#gbuffer-frag').textContent.trim(), - uniforms: { - tDiffuse: { value: diffuse }, - repeat: { value: new THREE.Vector2(5, 0.5) }, - }, - glslVersion: THREE.GLSL3, - }), - ), - ); - - // PostProcessing setup - - postScene = new THREE.Scene(); - postCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1); - - postScene.add( - new THREE.Mesh( - new THREE.PlaneGeometry(2, 2), - new THREE.RawShaderMaterial({ - name: 'Post-FX Shader', - vertexShader: document.querySelector('#render-vert').textContent.trim(), - fragmentShader: document.querySelector('#render-frag').textContent.trim(), - uniforms: { - tDiffuse: { value: renderTarget.textures[0] }, - tNormal: { value: renderTarget.textures[1] }, - }, - glslVersion: THREE.GLSL3, - }), - ), - ); - - // Controls - - controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - const dpr = renderer.getPixelRatio(); - renderTarget.setSize(window.innerWidth * dpr, window.innerHeight * dpr); - - render(); -} - -function render() { - renderTarget.samples = parameters.samples; - - scene.traverse(function (child) { - if (child.material !== undefined) { - child.material.wireframe = parameters.wireframe; - } - }); - - // render scene into target - renderer.setRenderTarget(renderTarget); - renderer.render(scene, camera); - - // render post FX - renderer.setRenderTarget(null); - renderer.render(postScene, postCamera); -} diff --git a/examples-testing/examples/webgl_multiple_scenes_comparison.ts b/examples-testing/examples/webgl_multiple_scenes_comparison.ts deleted file mode 100644 index 41a5130d4..000000000 --- a/examples-testing/examples/webgl_multiple_scenes_comparison.ts +++ /dev/null @@ -1,98 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let container, camera, renderer, controls; -let sceneL, sceneR; - -let sliderPos = window.innerWidth / 2; - -init(); - -function init() { - container = document.querySelector('.container'); - - sceneL = new THREE.Scene(); - sceneL.background = new THREE.Color(0xbcd48f); - - sceneR = new THREE.Scene(); - sceneR.background = new THREE.Color(0x8fbcd4); - - camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.z = 6; - - controls = new OrbitControls(camera, container); - - const light = new THREE.HemisphereLight(0xffffff, 0x444444, 3); - light.position.set(-2, 2, 2); - sceneL.add(light.clone()); - sceneR.add(light.clone()); - - initMeshes(); - initSlider(); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setScissorTest(true); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); -} - -function initMeshes() { - const geometry = new THREE.IcosahedronGeometry(1, 3); - - const meshL = new THREE.Mesh(geometry, new THREE.MeshStandardMaterial()); - sceneL.add(meshL); - - const meshR = new THREE.Mesh(geometry, new THREE.MeshStandardMaterial({ wireframe: true })); - sceneR.add(meshR); -} - -function initSlider() { - const slider = document.querySelector('.slider'); - - function onPointerDown() { - if (event.isPrimary === false) return; - - controls.enabled = false; - - window.addEventListener('pointermove', onPointerMove); - window.addEventListener('pointerup', onPointerUp); - } - - function onPointerUp() { - controls.enabled = true; - - window.removeEventListener('pointermove', onPointerMove); - window.removeEventListener('pointerup', onPointerUp); - } - - function onPointerMove(e) { - if (event.isPrimary === false) return; - - sliderPos = Math.max(0, Math.min(window.innerWidth, e.pageX)); - - slider.style.left = sliderPos - slider.offsetWidth / 2 + 'px'; - } - - slider.style.touchAction = 'none'; // disable touch scroll - slider.addEventListener('pointerdown', onPointerDown); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.setScissor(0, 0, sliderPos, window.innerHeight); - renderer.render(sceneL, camera); - - renderer.setScissor(sliderPos, 0, window.innerWidth, window.innerHeight); - renderer.render(sceneR, camera); -} diff --git a/examples-testing/examples/webgl_multiple_views.ts b/examples-testing/examples/webgl_multiple_views.ts deleted file mode 100644 index 29126b013..000000000 --- a/examples-testing/examples/webgl_multiple_views.ts +++ /dev/null @@ -1,237 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let stats; - -let scene, renderer; - -let mouseX = 0, - mouseY = 0; - -let windowWidth, windowHeight; - -const views = [ - { - left: 0, - bottom: 0, - width: 0.5, - height: 1.0, - background: new THREE.Color().setRGB(0.5, 0.5, 0.7, THREE.SRGBColorSpace), - eye: [0, 300, 1800], - up: [0, 1, 0], - fov: 30, - updateCamera: function (camera, scene, mouseX) { - camera.position.x += mouseX * 0.05; - camera.position.x = Math.max(Math.min(camera.position.x, 2000), -2000); - camera.lookAt(scene.position); - }, - }, - { - left: 0.5, - bottom: 0, - width: 0.5, - height: 0.5, - background: new THREE.Color().setRGB(0.7, 0.5, 0.5, THREE.SRGBColorSpace), - eye: [0, 1800, 0], - up: [0, 0, 1], - fov: 45, - updateCamera: function (camera, scene, mouseX) { - camera.position.x -= mouseX * 0.05; - camera.position.x = Math.max(Math.min(camera.position.x, 2000), -2000); - camera.lookAt(camera.position.clone().setY(0)); - }, - }, - { - left: 0.5, - bottom: 0.5, - width: 0.5, - height: 0.5, - background: new THREE.Color().setRGB(0.5, 0.7, 0.7, THREE.SRGBColorSpace), - eye: [1400, 800, 1400], - up: [0, 1, 0], - fov: 60, - updateCamera: function (camera, scene, mouseX) { - camera.position.y -= mouseX * 0.05; - camera.position.y = Math.max(Math.min(camera.position.y, 1600), -1600); - camera.lookAt(scene.position); - }, - }, -]; - -init(); - -function init() { - const container = document.getElementById('container'); - - for (let ii = 0; ii < views.length; ++ii) { - const view = views[ii]; - const camera = new THREE.PerspectiveCamera(view.fov, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.fromArray(view.eye); - camera.up.fromArray(view.up); - view.camera = camera; - } - - scene = new THREE.Scene(); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0, 0, 1); - scene.add(light); - - // shadow - - const canvas = document.createElement('canvas'); - canvas.width = 128; - canvas.height = 128; - - const context = canvas.getContext('2d'); - const gradient = context.createRadialGradient( - canvas.width / 2, - canvas.height / 2, - 0, - canvas.width / 2, - canvas.height / 2, - canvas.width / 2, - ); - gradient.addColorStop(0.1, 'rgba(0,0,0,0.15)'); - gradient.addColorStop(1, 'rgba(0,0,0,0)'); - - context.fillStyle = gradient; - context.fillRect(0, 0, canvas.width, canvas.height); - - const shadowTexture = new THREE.CanvasTexture(canvas); - - const shadowMaterial = new THREE.MeshBasicMaterial({ map: shadowTexture, transparent: true }); - const shadowGeo = new THREE.PlaneGeometry(300, 300, 1, 1); - - let shadowMesh; - - shadowMesh = new THREE.Mesh(shadowGeo, shadowMaterial); - shadowMesh.position.y = -250; - shadowMesh.rotation.x = -Math.PI / 2; - scene.add(shadowMesh); - - shadowMesh = new THREE.Mesh(shadowGeo, shadowMaterial); - shadowMesh.position.x = -400; - shadowMesh.position.y = -250; - shadowMesh.rotation.x = -Math.PI / 2; - scene.add(shadowMesh); - - shadowMesh = new THREE.Mesh(shadowGeo, shadowMaterial); - shadowMesh.position.x = 400; - shadowMesh.position.y = -250; - shadowMesh.rotation.x = -Math.PI / 2; - scene.add(shadowMesh); - - const radius = 200; - - const geometry1 = new THREE.IcosahedronGeometry(radius, 1); - - const count = geometry1.attributes.position.count; - geometry1.setAttribute('color', new THREE.BufferAttribute(new Float32Array(count * 3), 3)); - - const geometry2 = geometry1.clone(); - const geometry3 = geometry1.clone(); - - const color = new THREE.Color(); - const positions1 = geometry1.attributes.position; - const positions2 = geometry2.attributes.position; - const positions3 = geometry3.attributes.position; - const colors1 = geometry1.attributes.color; - const colors2 = geometry2.attributes.color; - const colors3 = geometry3.attributes.color; - - for (let i = 0; i < count; i++) { - color.setHSL((positions1.getY(i) / radius + 1) / 2, 1.0, 0.5, THREE.SRGBColorSpace); - colors1.setXYZ(i, color.r, color.g, color.b); - - color.setHSL(0, (positions2.getY(i) / radius + 1) / 2, 0.5, THREE.SRGBColorSpace); - colors2.setXYZ(i, color.r, color.g, color.b); - - color.setRGB(1, 0.8 - (positions3.getY(i) / radius + 1) / 2, 0, THREE.SRGBColorSpace); - colors3.setXYZ(i, color.r, color.g, color.b); - } - - const material = new THREE.MeshPhongMaterial({ - color: 0xffffff, - flatShading: true, - vertexColors: true, - shininess: 0, - }); - - const wireframeMaterial = new THREE.MeshBasicMaterial({ color: 0x000000, wireframe: true, transparent: true }); - - let mesh = new THREE.Mesh(geometry1, material); - let wireframe = new THREE.Mesh(geometry1, wireframeMaterial); - mesh.add(wireframe); - mesh.position.x = -400; - mesh.rotation.x = -1.87; - scene.add(mesh); - - mesh = new THREE.Mesh(geometry2, material); - wireframe = new THREE.Mesh(geometry2, wireframeMaterial); - mesh.add(wireframe); - mesh.position.x = 400; - scene.add(mesh); - - mesh = new THREE.Mesh(geometry3, material); - wireframe = new THREE.Mesh(geometry3, wireframeMaterial); - mesh.add(wireframe); - scene.add(mesh); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - document.addEventListener('mousemove', onDocumentMouseMove); -} - -function onDocumentMouseMove(event) { - mouseX = event.clientX - windowWidth / 2; - mouseY = event.clientY - windowHeight / 2; -} - -function updateSize() { - if (windowWidth != window.innerWidth || windowHeight != window.innerHeight) { - windowWidth = window.innerWidth; - windowHeight = window.innerHeight; - - renderer.setSize(windowWidth, windowHeight); - } -} - -function animate() { - render(); - stats.update(); -} - -function render() { - updateSize(); - - for (let ii = 0; ii < views.length; ++ii) { - const view = views[ii]; - const camera = view.camera; - - view.updateCamera(camera, scene, mouseX, mouseY); - - const left = Math.floor(windowWidth * view.left); - const bottom = Math.floor(windowHeight * view.bottom); - const width = Math.floor(windowWidth * view.width); - const height = Math.floor(windowHeight * view.height); - - renderer.setViewport(left, bottom, width, height); - renderer.setScissor(left, bottom, width, height); - renderer.setScissorTest(true); - renderer.setClearColor(view.background); - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.render(scene, camera); - } -} diff --git a/examples-testing/examples/webgl_multisampled_renderbuffers.ts b/examples-testing/examples/webgl_multisampled_renderbuffers.ts deleted file mode 100644 index df84fb144..000000000 --- a/examples-testing/examples/webgl_multisampled_renderbuffers.ts +++ /dev/null @@ -1,133 +0,0 @@ -import * as THREE from 'three'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, renderer, group, container; - -let composer1, composer2; - -const params = { - animate: true, -}; - -init(); - -function init() { - container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(45, container.offsetWidth / container.offsetHeight, 10, 2000); - camera.position.z = 500; - - const scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - scene.fog = new THREE.Fog(0xcccccc, 100, 1500); - - // - - const hemiLight = new THREE.HemisphereLight(0xffffff, 0x222222, 5); - hemiLight.position.set(1, 1, 1); - scene.add(hemiLight); - - // - - group = new THREE.Group(); - - const geometry = new THREE.SphereGeometry(10, 64, 40); - const material = new THREE.MeshLambertMaterial({ - color: 0xee0808, - polygonOffset: true, - polygonOffsetFactor: 1, // positive value pushes polygon further away - polygonOffsetUnits: 1, - }); - const material2 = new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: true }); - - for (let i = 0; i < 50; i++) { - const mesh = new THREE.Mesh(geometry, material); - mesh.position.x = Math.random() * 600 - 300; - mesh.position.y = Math.random() * 600 - 300; - mesh.position.z = Math.random() * 600 - 300; - mesh.rotation.x = Math.random(); - mesh.rotation.z = Math.random(); - mesh.scale.setScalar(Math.random() * 5 + 5); - group.add(mesh); - - const mesh2 = new THREE.Mesh(geometry, material2); - mesh2.position.copy(mesh.position); - mesh2.rotation.copy(mesh.rotation); - mesh2.scale.copy(mesh.scale); - group.add(mesh2); - } - - scene.add(group); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(container.offsetWidth, container.offsetHeight); - renderer.setAnimationLoop(animate); - renderer.autoClear = false; - container.appendChild(renderer.domElement); - - // - - const size = renderer.getDrawingBufferSize(new THREE.Vector2()); - const renderTarget = new THREE.WebGLRenderTarget(size.width, size.height, { - samples: 4, - type: THREE.HalfFloatType, - }); - - const renderPass = new RenderPass(scene, camera); - const outputPass = new OutputPass(); - - // - - composer1 = new EffectComposer(renderer); - composer1.addPass(renderPass); - composer1.addPass(outputPass); - - // - - composer2 = new EffectComposer(renderer, renderTarget); - composer2.addPass(renderPass); - composer2.addPass(outputPass); - - // - - const gui = new GUI(); - gui.add(params, 'animate'); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = container.offsetWidth / container.offsetHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(container.offsetWidth, container.offsetHeight); - composer1.setSize(container.offsetWidth, container.offsetHeight); - composer2.setSize(container.offsetWidth, container.offsetHeight); -} - -function animate() { - const halfWidth = container.offsetWidth / 2; - - if (params.animate) { - group.rotation.y += 0.002; - } - - renderer.setScissorTest(true); - - renderer.setScissor(0, 0, halfWidth - 1, container.offsetHeight); - composer1.render(); - - renderer.setScissor(halfWidth, 0, halfWidth, container.offsetHeight); - composer2.render(); - - renderer.setScissorTest(false); -} diff --git a/examples-testing/examples/webgl_panorama_cube.ts b/examples-testing/examples/webgl_panorama_cube.ts deleted file mode 100644 index efd09cfc5..000000000 --- a/examples-testing/examples/webgl_panorama_cube.ts +++ /dev/null @@ -1,83 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, controls; -let renderer; -let scene; - -init(); - -function init() { - const container = document.getElementById('container'); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(90, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.z = 0.01; - - controls = new OrbitControls(camera, renderer.domElement); - controls.enableZoom = false; - controls.enablePan = false; - controls.enableDamping = true; - controls.rotateSpeed = -0.25; - - const textures = getTexturesFromAtlasFile('textures/cube/sun_temple_stripe.jpg', 6); - - const materials = []; - - for (let i = 0; i < 6; i++) { - materials.push(new THREE.MeshBasicMaterial({ map: textures[i] })); - } - - const skyBox = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), materials); - skyBox.geometry.scale(1, 1, -1); - scene.add(skyBox); - - window.addEventListener('resize', onWindowResize); -} - -function getTexturesFromAtlasFile(atlasImgUrl, tilesNum) { - const textures = []; - - for (let i = 0; i < tilesNum; i++) { - textures[i] = new THREE.Texture(); - } - - new THREE.ImageLoader().load(atlasImgUrl, image => { - let canvas, context; - const tileWidth = image.height; - - for (let i = 0; i < textures.length; i++) { - canvas = document.createElement('canvas'); - context = canvas.getContext('2d'); - canvas.height = tileWidth; - canvas.width = tileWidth; - context.drawImage(image, tileWidth * i, 0, tileWidth, tileWidth, 0, 0, tileWidth, tileWidth); - textures[i].colorSpace = THREE.SRGBColorSpace; - textures[i].image = canvas; - textures[i].needsUpdate = true; - } - }); - - return textures; -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - controls.update(); // required when damping is enabled - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_panorama_equirectangular.ts b/examples-testing/examples/webgl_panorama_equirectangular.ts deleted file mode 100644 index 552745222..000000000 --- a/examples-testing/examples/webgl_panorama_equirectangular.ts +++ /dev/null @@ -1,142 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer; - -let isUserInteracting = false, - onPointerDownMouseX = 0, - onPointerDownMouseY = 0, - lon = 0, - onPointerDownLon = 0, - lat = 0, - onPointerDownLat = 0, - phi = 0, - theta = 0; - -init(); - -function init() { - const container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1100); - - scene = new THREE.Scene(); - - const geometry = new THREE.SphereGeometry(500, 60, 40); - // invert the geometry on the x-axis so that all of the faces point inward - geometry.scale(-1, 1, 1); - - const texture = new THREE.TextureLoader().load('textures/2294472375_24a3b8ef46_o.jpg'); - texture.colorSpace = THREE.SRGBColorSpace; - const material = new THREE.MeshBasicMaterial({ map: texture }); - - const mesh = new THREE.Mesh(geometry, material); - - scene.add(mesh); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - container.style.touchAction = 'none'; - container.addEventListener('pointerdown', onPointerDown); - - document.addEventListener('wheel', onDocumentMouseWheel); - - // - - document.addEventListener('dragover', function (event) { - event.preventDefault(); - event.dataTransfer.dropEffect = 'copy'; - }); - - document.addEventListener('dragenter', function () { - document.body.style.opacity = 0.5; - }); - - document.addEventListener('dragleave', function () { - document.body.style.opacity = 1; - }); - - document.addEventListener('drop', function (event) { - event.preventDefault(); - - const reader = new FileReader(); - reader.addEventListener('load', function (event) { - material.map.image.src = event.target.result; - material.map.needsUpdate = true; - }); - reader.readAsDataURL(event.dataTransfer.files[0]); - - document.body.style.opacity = 1; - }); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onPointerDown(event) { - if (event.isPrimary === false) return; - - isUserInteracting = true; - - onPointerDownMouseX = event.clientX; - onPointerDownMouseY = event.clientY; - - onPointerDownLon = lon; - onPointerDownLat = lat; - - document.addEventListener('pointermove', onPointerMove); - document.addEventListener('pointerup', onPointerUp); -} - -function onPointerMove(event) { - if (event.isPrimary === false) return; - - lon = (onPointerDownMouseX - event.clientX) * 0.1 + onPointerDownLon; - lat = (event.clientY - onPointerDownMouseY) * 0.1 + onPointerDownLat; -} - -function onPointerUp() { - if (event.isPrimary === false) return; - - isUserInteracting = false; - - document.removeEventListener('pointermove', onPointerMove); - document.removeEventListener('pointerup', onPointerUp); -} - -function onDocumentMouseWheel(event) { - const fov = camera.fov + event.deltaY * 0.05; - - camera.fov = THREE.MathUtils.clamp(fov, 10, 75); - - camera.updateProjectionMatrix(); -} - -function animate() { - if (isUserInteracting === false) { - lon += 0.1; - } - - lat = Math.max(-85, Math.min(85, lat)); - phi = THREE.MathUtils.degToRad(90 - lat); - theta = THREE.MathUtils.degToRad(lon); - - const x = 500 * Math.sin(phi) * Math.cos(theta); - const y = 500 * Math.cos(phi); - const z = 500 * Math.sin(phi) * Math.sin(theta); - - camera.lookAt(x, y, z); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_pmrem_test.ts b/examples-testing/examples/webgl_pmrem_test.ts deleted file mode 100644 index b33e4e2f1..000000000 --- a/examples-testing/examples/webgl_pmrem_test.ts +++ /dev/null @@ -1,141 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let scene, camera, controls, renderer; - -function init() { - const width = window.innerWidth; - const height = window.innerHeight; - const aspect = width / height; - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(width, height); - - // tonemapping - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1; - - document.body.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); - - // scene - - scene = new THREE.Scene(); - - // camera - - camera = new THREE.PerspectiveCamera(40, aspect, 1, 30); - updateCamera(); - camera.position.set(0, 0, 16); - - // controls - - controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.minDistance = 4; - controls.maxDistance = 20; - - // light - - const directionalLight = new THREE.DirectionalLight(0xffffff, 0); // set intensity to 0 to start - const x = 597; - const y = 213; - const theta = ((x + 0.5) * Math.PI) / 512; - const phi = ((y + 0.5) * Math.PI) / 512; - - directionalLight.position.setFromSphericalCoords(100, -phi, Math.PI / 2 - theta); - - scene.add(directionalLight); - // scene.add( new THREE.DirectionalLightHelper( directionalLight ) ); - - // The spot1Lux HDR environment map is expressed in nits (lux / sr). The directional light has units of lux, - // so to match a 1 lux light, we set a single pixel with a value equal to 1 divided by the solid - // angle of the pixel in steradians. This image is 1024 x 512, - // so the value is 1 / ( sin( phi ) * ( pi / 512 ) ^ 2 ) = 27,490 nits. - - const gui = new GUI(); - gui.add({ enabled: true }, 'enabled') - .name('PMREM') - .onChange(value => { - directionalLight.intensity = value ? 0 : 1; - - scene.traverse(function (child) { - if (child.isMesh) { - child.material.envMapIntensity = 1 - directionalLight.intensity; - } - }); - - render(); - }); -} - -function createObjects() { - let radianceMap = null; - new RGBELoader() - // .setDataType( THREE.FloatType ) - .setPath('textures/equirectangular/') - .load('spot1Lux.hdr', function (texture) { - radianceMap = pmremGenerator.fromEquirectangular(texture).texture; - pmremGenerator.dispose(); - - scene.background = radianceMap; - - const geometry = new THREE.SphereGeometry(0.4, 32, 32); - - for (let x = 0; x <= 10; x++) { - for (let y = 0; y <= 2; y++) { - const material = new THREE.MeshPhysicalMaterial({ - roughness: x / 10, - metalness: y < 1 ? 1 : 0, - color: y < 2 ? 0xffffff : 0x000000, - envMap: radianceMap, - envMapIntensity: 1, - }); - - const mesh = new THREE.Mesh(geometry, material); - mesh.position.x = x - 5; - mesh.position.y = 1 - y; - scene.add(mesh); - } - } - - render(); - }); - - const pmremGenerator = new THREE.PMREMGenerator(renderer); - pmremGenerator.compileEquirectangularShader(); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - updateCamera(); - - renderer.setSize(width, height); - - render(); -} - -function updateCamera() { - const horizontalFoV = 40; - const verticalFoV = - (2 * Math.atan(Math.tan(((horizontalFoV / 2) * Math.PI) / 180) / camera.aspect) * 180) / Math.PI; - camera.fov = verticalFoV; - camera.updateProjectionMatrix(); -} - -function render() { - renderer.render(scene, camera); -} - -Promise.resolve().then(init).then(createObjects).then(render); diff --git a/examples-testing/examples/webgl_points_billboards.ts b/examples-testing/examples/webgl_points_billboards.ts deleted file mode 100644 index 24d4de1a9..000000000 --- a/examples-testing/examples/webgl_points_billboards.ts +++ /dev/null @@ -1,120 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, stats, material; -let mouseX = 0, - mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(55, window.innerWidth / window.innerHeight, 2, 2000); - camera.position.z = 1000; - - scene = new THREE.Scene(); - scene.fog = new THREE.FogExp2(0x000000, 0.001); - - const geometry = new THREE.BufferGeometry(); - const vertices = []; - - const sprite = new THREE.TextureLoader().load('textures/sprites/disc.png'); - sprite.colorSpace = THREE.SRGBColorSpace; - - for (let i = 0; i < 10000; i++) { - const x = 2000 * Math.random() - 1000; - const y = 2000 * Math.random() - 1000; - const z = 2000 * Math.random() - 1000; - - vertices.push(x, y, z); - } - - geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); - - material = new THREE.PointsMaterial({ - size: 35, - sizeAttenuation: true, - map: sprite, - alphaTest: 0.5, - transparent: true, - }); - material.color.setHSL(1.0, 0.3, 0.7, THREE.SRGBColorSpace); - - const particles = new THREE.Points(geometry, material); - scene.add(particles); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - const gui = new GUI(); - - gui.add(material, 'sizeAttenuation').onChange(function () { - material.needsUpdate = true; - }); - - gui.open(); - - // - - document.body.style.touchAction = 'none'; - document.body.addEventListener('pointermove', onPointerMove); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onPointerMove(event) { - if (event.isPrimary === false) return; - - mouseX = event.clientX - windowHalfX; - mouseY = event.clientY - windowHalfY; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - const time = Date.now() * 0.00005; - - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-mouseY - camera.position.y) * 0.05; - - camera.lookAt(scene.position); - - const h = ((360 * (1.0 + time)) % 360) / 360; - material.color.setHSL(h, 0.5, 0.5); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_points_sprites.ts b/examples-testing/examples/webgl_points_sprites.ts deleted file mode 100644 index 31b9e2ce1..000000000 --- a/examples-testing/examples/webgl_points_sprites.ts +++ /dev/null @@ -1,167 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, stats, parameters; -let mouseX = 0, - mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -const materials = []; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 2000); - camera.position.z = 1000; - - scene = new THREE.Scene(); - scene.fog = new THREE.FogExp2(0x000000, 0.0008); - - const geometry = new THREE.BufferGeometry(); - const vertices = []; - - const textureLoader = new THREE.TextureLoader(); - - const assignSRGB = texture => { - texture.colorSpace = THREE.SRGBColorSpace; - }; - - const sprite1 = textureLoader.load('textures/sprites/snowflake1.png', assignSRGB); - const sprite2 = textureLoader.load('textures/sprites/snowflake2.png', assignSRGB); - const sprite3 = textureLoader.load('textures/sprites/snowflake3.png', assignSRGB); - const sprite4 = textureLoader.load('textures/sprites/snowflake4.png', assignSRGB); - const sprite5 = textureLoader.load('textures/sprites/snowflake5.png', assignSRGB); - - for (let i = 0; i < 10000; i++) { - const x = Math.random() * 2000 - 1000; - const y = Math.random() * 2000 - 1000; - const z = Math.random() * 2000 - 1000; - - vertices.push(x, y, z); - } - - geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); - - parameters = [ - [[1.0, 0.2, 0.5], sprite2, 20], - [[0.95, 0.1, 0.5], sprite3, 15], - [[0.9, 0.05, 0.5], sprite1, 10], - [[0.85, 0, 0.5], sprite5, 8], - [[0.8, 0, 0.5], sprite4, 5], - ]; - - for (let i = 0; i < parameters.length; i++) { - const color = parameters[i][0]; - const sprite = parameters[i][1]; - const size = parameters[i][2]; - - materials[i] = new THREE.PointsMaterial({ - size: size, - map: sprite, - blending: THREE.AdditiveBlending, - depthTest: false, - transparent: true, - }); - materials[i].color.setHSL(color[0], color[1], color[2], THREE.SRGBColorSpace); - - const particles = new THREE.Points(geometry, materials[i]); - - particles.rotation.x = Math.random() * 6; - particles.rotation.y = Math.random() * 6; - particles.rotation.z = Math.random() * 6; - - scene.add(particles); - } - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - const gui = new GUI(); - - const params = { - texture: true, - }; - - gui.add(params, 'texture').onChange(function (value) { - for (let i = 0; i < materials.length; i++) { - materials[i].map = value === true ? parameters[i][1] : null; - materials[i].needsUpdate = true; - } - }); - - gui.open(); - - document.body.style.touchAction = 'none'; - document.body.addEventListener('pointermove', onPointerMove); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onPointerMove(event) { - if (event.isPrimary === false) return; - - mouseX = event.clientX - windowHalfX; - mouseY = event.clientY - windowHalfY; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - const time = Date.now() * 0.00005; - - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-mouseY - camera.position.y) * 0.05; - - camera.lookAt(scene.position); - - for (let i = 0; i < scene.children.length; i++) { - const object = scene.children[i]; - - if (object instanceof THREE.Points) { - object.rotation.y = time * (i < 4 ? i + 1 : -(i + 1)); - } - } - - for (let i = 0; i < materials.length; i++) { - const color = parameters[i][0]; - - const h = ((360 * (color[0] + time)) % 360) / 360; - materials[i].color.setHSL(h, color[1], color[2], THREE.SRGBColorSpace); - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_points_waves.ts b/examples-testing/examples/webgl_points_waves.ts deleted file mode 100644 index 91986e9e9..000000000 --- a/examples-testing/examples/webgl_points_waves.ts +++ /dev/null @@ -1,145 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -const SEPARATION = 100, - AMOUNTX = 50, - AMOUNTY = 50; - -let container, stats; -let camera, scene, renderer; - -let particles, - count = 0; - -let mouseX = 0, - mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 1000; - - scene = new THREE.Scene(); - - // - - const numParticles = AMOUNTX * AMOUNTY; - - const positions = new Float32Array(numParticles * 3); - const scales = new Float32Array(numParticles); - - let i = 0, - j = 0; - - for (let ix = 0; ix < AMOUNTX; ix++) { - for (let iy = 0; iy < AMOUNTY; iy++) { - positions[i] = ix * SEPARATION - (AMOUNTX * SEPARATION) / 2; // x - positions[i + 1] = 0; // y - positions[i + 2] = iy * SEPARATION - (AMOUNTY * SEPARATION) / 2; // z - - scales[j] = 1; - - i += 3; - j++; - } - } - - const geometry = new THREE.BufferGeometry(); - geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); - geometry.setAttribute('scale', new THREE.BufferAttribute(scales, 1)); - - const material = new THREE.ShaderMaterial({ - uniforms: { - color: { value: new THREE.Color(0xffffff) }, - }, - vertexShader: document.getElementById('vertexshader').textContent, - fragmentShader: document.getElementById('fragmentshader').textContent, - }); - - // - - particles = new THREE.Points(geometry, material); - scene.add(particles); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - container.style.touchAction = 'none'; - container.addEventListener('pointermove', onPointerMove); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function onPointerMove(event) { - if (event.isPrimary === false) return; - - mouseX = event.clientX - windowHalfX; - mouseY = event.clientY - windowHalfY; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-mouseY - camera.position.y) * 0.05; - camera.lookAt(scene.position); - - const positions = particles.geometry.attributes.position.array; - const scales = particles.geometry.attributes.scale.array; - - let i = 0, - j = 0; - - for (let ix = 0; ix < AMOUNTX; ix++) { - for (let iy = 0; iy < AMOUNTY; iy++) { - positions[i + 1] = Math.sin((ix + count) * 0.3) * 50 + Math.sin((iy + count) * 0.5) * 50; - - scales[j] = (Math.sin((ix + count) * 0.3) + 1) * 20 + (Math.sin((iy + count) * 0.5) + 1) * 20; - - i += 3; - j++; - } - } - - particles.geometry.attributes.position.needsUpdate = true; - particles.geometry.attributes.scale.needsUpdate = true; - - renderer.render(scene, camera); - - count += 0.1; -} diff --git a/examples-testing/examples/webgl_portal.ts b/examples-testing/examples/webgl_portal.ts deleted file mode 100644 index 4bc59593f..000000000 --- a/examples-testing/examples/webgl_portal.ts +++ /dev/null @@ -1,218 +0,0 @@ -import * as THREE from 'three'; - -import * as CameraUtils from 'three/addons/utils/CameraUtils.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer; - -let cameraControls; - -let smallSphereOne, smallSphereTwo; - -let portalCamera, - leftPortal, - rightPortal, - leftPortalTexture, - reflectedPosition, - rightPortalTexture, - bottomLeftCorner, - bottomRightCorner, - topLeftCorner; - -init(); - -function init() { - const container = document.getElementById('container'); - - // renderer - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.localClippingEnabled = true; - renderer.toneMapping = THREE.ACESFilmicToneMapping; - container.appendChild(renderer.domElement); - - // scene - scene = new THREE.Scene(); - - // camera - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 5000); - camera.position.set(0, 75, 160); - - cameraControls = new OrbitControls(camera, renderer.domElement); - cameraControls.target.set(0, 40, 0); - cameraControls.maxDistance = 400; - cameraControls.minDistance = 10; - cameraControls.update(); - - // - - const planeGeo = new THREE.PlaneGeometry(100.1, 100.1); - - // bouncing icosphere - const portalPlane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0.0); - const geometry = new THREE.IcosahedronGeometry(5, 0); - const material = new THREE.MeshPhongMaterial({ - color: 0xffffff, - emissive: 0x333333, - flatShading: true, - clippingPlanes: [portalPlane], - clipShadows: true, - }); - smallSphereOne = new THREE.Mesh(geometry, material); - scene.add(smallSphereOne); - smallSphereTwo = new THREE.Mesh(geometry, material); - scene.add(smallSphereTwo); - - // portals - portalCamera = new THREE.PerspectiveCamera(45, 1.0, 0.1, 500.0); - scene.add(portalCamera); - //frustumHelper = new THREE.CameraHelper( portalCamera ); - //scene.add( frustumHelper ); - bottomLeftCorner = new THREE.Vector3(); - bottomRightCorner = new THREE.Vector3(); - topLeftCorner = new THREE.Vector3(); - reflectedPosition = new THREE.Vector3(); - - leftPortalTexture = new THREE.WebGLRenderTarget(256, 256); - leftPortal = new THREE.Mesh(planeGeo, new THREE.MeshBasicMaterial({ map: leftPortalTexture.texture })); - leftPortal.position.x = -30; - leftPortal.position.y = 20; - leftPortal.scale.set(0.35, 0.35, 0.35); - scene.add(leftPortal); - - rightPortalTexture = new THREE.WebGLRenderTarget(256, 256); - rightPortal = new THREE.Mesh(planeGeo, new THREE.MeshBasicMaterial({ map: rightPortalTexture.texture })); - rightPortal.position.x = 30; - rightPortal.position.y = 20; - rightPortal.scale.set(0.35, 0.35, 0.35); - scene.add(rightPortal); - - // walls - const planeTop = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); - planeTop.position.y = 100; - planeTop.rotateX(Math.PI / 2); - scene.add(planeTop); - - const planeBottom = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); - planeBottom.rotateX(-Math.PI / 2); - scene.add(planeBottom); - - const planeFront = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x7f7fff })); - planeFront.position.z = 50; - planeFront.position.y = 50; - planeFront.rotateY(Math.PI); - scene.add(planeFront); - - const planeBack = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xff7fff })); - planeBack.position.z = -50; - planeBack.position.y = 50; - //planeBack.rotateY( Math.PI ); - scene.add(planeBack); - - const planeRight = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x00ff00 })); - planeRight.position.x = 50; - planeRight.position.y = 50; - planeRight.rotateY(-Math.PI / 2); - scene.add(planeRight); - - const planeLeft = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xff0000 })); - planeLeft.position.x = -50; - planeLeft.position.y = 50; - planeLeft.rotateY(Math.PI / 2); - scene.add(planeLeft); - - // lights - const mainLight = new THREE.PointLight(0xe7e7e7, 2.5, 250, 0); - mainLight.position.y = 60; - scene.add(mainLight); - - const greenLight = new THREE.PointLight(0x00ff00, 0.5, 1000, 0); - greenLight.position.set(550, 50, 0); - scene.add(greenLight); - - const redLight = new THREE.PointLight(0xff0000, 0.5, 1000, 0); - redLight.position.set(-550, 50, 0); - scene.add(redLight); - - const blueLight = new THREE.PointLight(0xbbbbfe, 0.5, 1000, 0); - blueLight.position.set(0, 50, 550); - scene.add(blueLight); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function renderPortal(thisPortalMesh, otherPortalMesh, thisPortalTexture) { - // set the portal camera position to be reflected about the portal plane - thisPortalMesh.worldToLocal(reflectedPosition.copy(camera.position)); - reflectedPosition.x *= -1.0; - reflectedPosition.z *= -1.0; - otherPortalMesh.localToWorld(reflectedPosition); - portalCamera.position.copy(reflectedPosition); - - // grab the corners of the other portal - // - note: the portal is viewed backwards; flip the left/right coordinates - otherPortalMesh.localToWorld(bottomLeftCorner.set(50.05, -50.05, 0.0)); - otherPortalMesh.localToWorld(bottomRightCorner.set(-50.05, -50.05, 0.0)); - otherPortalMesh.localToWorld(topLeftCorner.set(50.05, 50.05, 0.0)); - // set the projection matrix to encompass the portal's frame - CameraUtils.frameCorners(portalCamera, bottomLeftCorner, bottomRightCorner, topLeftCorner, false); - - // render the portal - thisPortalTexture.texture.colorSpace = renderer.outputColorSpace; - renderer.setRenderTarget(thisPortalTexture); - renderer.state.buffers.depth.setMask(true); // make sure the depth buffer is writable so it can be properly cleared, see #18897 - if (renderer.autoClear === false) renderer.clear(); - thisPortalMesh.visible = false; // hide this portal from its own rendering - renderer.render(scene, portalCamera); - thisPortalMesh.visible = true; // re-enable this portal's visibility for general rendering -} - -function animate() { - // move the bouncing sphere(s) - const timerOne = Date.now() * 0.01; - const timerTwo = timerOne + Math.PI * 10.0; - - smallSphereOne.position.set( - Math.cos(timerOne * 0.1) * 30, - Math.abs(Math.cos(timerOne * 0.2)) * 20 + 5, - Math.sin(timerOne * 0.1) * 30, - ); - smallSphereOne.rotation.y = Math.PI / 2 - timerOne * 0.1; - smallSphereOne.rotation.z = timerOne * 0.8; - - smallSphereTwo.position.set( - Math.cos(timerTwo * 0.1) * 30, - Math.abs(Math.cos(timerTwo * 0.2)) * 20 + 5, - Math.sin(timerTwo * 0.1) * 30, - ); - smallSphereTwo.rotation.y = Math.PI / 2 - timerTwo * 0.1; - smallSphereTwo.rotation.z = timerTwo * 0.8; - - // save the original camera properties - const currentRenderTarget = renderer.getRenderTarget(); - const currentXrEnabled = renderer.xr.enabled; - const currentShadowAutoUpdate = renderer.shadowMap.autoUpdate; - renderer.xr.enabled = false; // Avoid camera modification - renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows - - // render the portal effect - renderPortal(leftPortal, rightPortal, leftPortalTexture); - renderPortal(rightPortal, leftPortal, rightPortalTexture); - - // restore the original rendering properties - renderer.xr.enabled = currentXrEnabled; - renderer.shadowMap.autoUpdate = currentShadowAutoUpdate; - renderer.setRenderTarget(currentRenderTarget); - - // render the main scene - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_postprocessing.ts b/examples-testing/examples/webgl_postprocessing.ts deleted file mode 100644 index ecc9b28ee..000000000 --- a/examples-testing/examples/webgl_postprocessing.ts +++ /dev/null @@ -1,86 +0,0 @@ -import * as THREE from 'three'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js'; - -import { RGBShiftShader } from 'three/addons/shaders/RGBShiftShader.js'; -import { DotScreenShader } from 'three/addons/shaders/DotScreenShader.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -let camera, renderer, composer; -let object; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 400; - - const scene = new THREE.Scene(); - scene.fog = new THREE.Fog(0x000000, 1, 1000); - - object = new THREE.Object3D(); - scene.add(object); - - const geometry = new THREE.SphereGeometry(1, 4, 4); - const material = new THREE.MeshPhongMaterial({ color: 0xffffff, flatShading: true }); - - for (let i = 0; i < 100; i++) { - const mesh = new THREE.Mesh(geometry, material); - mesh.position.set(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5).normalize(); - mesh.position.multiplyScalar(Math.random() * 400); - mesh.rotation.set(Math.random() * 2, Math.random() * 2, Math.random() * 2); - mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50; - object.add(mesh); - } - - scene.add(new THREE.AmbientLight(0xcccccc)); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(1, 1, 1); - scene.add(light); - - // postprocessing - - composer = new EffectComposer(renderer); - composer.addPass(new RenderPass(scene, camera)); - - const effect1 = new ShaderPass(DotScreenShader); - effect1.uniforms['scale'].value = 4; - composer.addPass(effect1); - - const effect2 = new ShaderPass(RGBShiftShader); - effect2.uniforms['amount'].value = 0.0015; - composer.addPass(effect2); - - const effect3 = new OutputPass(); - composer.addPass(effect3); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - composer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - object.rotation.x += 0.005; - object.rotation.y += 0.01; - - composer.render(); -} diff --git a/examples-testing/examples/webgl_postprocessing_advanced.ts b/examples-testing/examples/webgl_postprocessing_advanced.ts deleted file mode 100644 index adaef6208..000000000 --- a/examples-testing/examples/webgl_postprocessing_advanced.ts +++ /dev/null @@ -1,304 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js'; -import { BloomPass } from 'three/addons/postprocessing/BloomPass.js'; -import { FilmPass } from 'three/addons/postprocessing/FilmPass.js'; -import { DotScreenPass } from 'three/addons/postprocessing/DotScreenPass.js'; -import { MaskPass, ClearMaskPass } from 'three/addons/postprocessing/MaskPass.js'; -import { TexturePass } from 'three/addons/postprocessing/TexturePass.js'; - -import { BleachBypassShader } from 'three/addons/shaders/BleachBypassShader.js'; -import { ColorifyShader } from 'three/addons/shaders/ColorifyShader.js'; -import { HorizontalBlurShader } from 'three/addons/shaders/HorizontalBlurShader.js'; -import { VerticalBlurShader } from 'three/addons/shaders/VerticalBlurShader.js'; -import { SepiaShader } from 'three/addons/shaders/SepiaShader.js'; -import { VignetteShader } from 'three/addons/shaders/VignetteShader.js'; -import { GammaCorrectionShader } from 'three/addons/shaders/GammaCorrectionShader.js'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -let container, stats; - -let composerScene, composer1, composer2, composer3, composer4; - -let cameraOrtho, cameraPerspective, sceneModel, sceneBG, renderer, mesh, directionalLight; - -const width = window.innerWidth || 2; -const height = window.innerHeight || 2; - -let halfWidth = width / 2; -let halfHeight = height / 2; - -let quadBG, quadMask, renderScene; - -const delta = 0.01; - -init(); - -function init() { - container = document.getElementById('container'); - - // - - cameraOrtho = new THREE.OrthographicCamera(-halfWidth, halfWidth, halfHeight, -halfHeight, -10000, 10000); - cameraOrtho.position.z = 100; - - cameraPerspective = new THREE.PerspectiveCamera(50, width / height, 1, 10000); - cameraPerspective.position.z = 900; - - // - - sceneModel = new THREE.Scene(); - sceneBG = new THREE.Scene(); - - // - - directionalLight = new THREE.DirectionalLight(0xffffff, 3); - directionalLight.position.set(0, -0.1, 1).normalize(); - sceneModel.add(directionalLight); - - const loader = new GLTFLoader(); - loader.load('models/gltf/LeePerrySmith/LeePerrySmith.glb', function (gltf) { - createMesh(gltf.scene.children[0].geometry, sceneModel, 100); - }); - - // - - const diffuseMap = new THREE.TextureLoader().load('textures/cube/SwedishRoyalCastle/pz.jpg'); - diffuseMap.colorSpace = THREE.SRGBColorSpace; - - const materialColor = new THREE.MeshBasicMaterial({ - map: diffuseMap, - depthTest: false, - }); - - quadBG = new THREE.Mesh(new THREE.PlaneGeometry(1, 1), materialColor); - quadBG.position.z = -500; - quadBG.scale.set(width, height, 1); - sceneBG.add(quadBG); - - // - - const sceneMask = new THREE.Scene(); - - quadMask = new THREE.Mesh(new THREE.PlaneGeometry(1, 1), new THREE.MeshBasicMaterial({ color: 0xffaa00 })); - quadMask.position.z = -300; - quadMask.scale.set(width / 2, height / 2, 1); - sceneMask.add(quadMask); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(width, height); - renderer.setAnimationLoop(animate); - renderer.autoClear = false; - - // - - container.appendChild(renderer.domElement); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - const shaderBleach = BleachBypassShader; - const shaderSepia = SepiaShader; - const shaderVignette = VignetteShader; - - const effectBleach = new ShaderPass(shaderBleach); - const effectSepia = new ShaderPass(shaderSepia); - const effectVignette = new ShaderPass(shaderVignette); - const gammaCorrection = new ShaderPass(GammaCorrectionShader); - - effectBleach.uniforms['opacity'].value = 0.95; - - effectSepia.uniforms['amount'].value = 0.9; - - effectVignette.uniforms['offset'].value = 0.95; - effectVignette.uniforms['darkness'].value = 1.6; - - const effectBloom = new BloomPass(0.5); - const effectFilm = new FilmPass(0.35); - const effectFilmBW = new FilmPass(0.35, true); - const effectDotScreen = new DotScreenPass(new THREE.Vector2(0, 0), 0.5, 0.8); - - const effectHBlur = new ShaderPass(HorizontalBlurShader); - const effectVBlur = new ShaderPass(VerticalBlurShader); - effectHBlur.uniforms['h'].value = 2 / (width / 2); - effectVBlur.uniforms['v'].value = 2 / (height / 2); - - const effectColorify1 = new ShaderPass(ColorifyShader); - const effectColorify2 = new ShaderPass(ColorifyShader); - effectColorify1.uniforms['color'] = new THREE.Uniform(new THREE.Color(1, 0.8, 0.8)); - effectColorify2.uniforms['color'] = new THREE.Uniform(new THREE.Color(1, 0.75, 0.5)); - - const clearMask = new ClearMaskPass(); - const renderMask = new MaskPass(sceneModel, cameraPerspective); - const renderMaskInverse = new MaskPass(sceneModel, cameraPerspective); - - renderMaskInverse.inverse = true; - - // - - const rtParameters = { - stencilBuffer: true, - }; - - const rtWidth = width / 2; - const rtHeight = height / 2; - - // - - const renderBackground = new RenderPass(sceneBG, cameraOrtho); - const renderModel = new RenderPass(sceneModel, cameraPerspective); - - renderModel.clear = false; - - composerScene = new EffectComposer(renderer, new THREE.WebGLRenderTarget(rtWidth * 2, rtHeight * 2, rtParameters)); - - composerScene.addPass(renderBackground); - composerScene.addPass(renderModel); - composerScene.addPass(renderMaskInverse); - composerScene.addPass(effectHBlur); - composerScene.addPass(effectVBlur); - composerScene.addPass(clearMask); - - // - - renderScene = new TexturePass(composerScene.renderTarget2.texture); - - // - - composer1 = new EffectComposer(renderer, new THREE.WebGLRenderTarget(rtWidth, rtHeight, rtParameters)); - - composer1.addPass(renderScene); - composer1.addPass(gammaCorrection); - composer1.addPass(effectFilmBW); - composer1.addPass(effectVignette); - - // - - composer2 = new EffectComposer(renderer, new THREE.WebGLRenderTarget(rtWidth, rtHeight, rtParameters)); - - composer2.addPass(renderScene); - composer2.addPass(gammaCorrection); - composer2.addPass(effectDotScreen); - composer2.addPass(renderMask); - composer2.addPass(effectColorify1); - composer2.addPass(clearMask); - composer2.addPass(renderMaskInverse); - composer2.addPass(effectColorify2); - composer2.addPass(clearMask); - composer2.addPass(effectVignette); - - // - - composer3 = new EffectComposer(renderer, new THREE.WebGLRenderTarget(rtWidth, rtHeight, rtParameters)); - - composer3.addPass(renderScene); - composer3.addPass(gammaCorrection); - composer3.addPass(effectSepia); - composer3.addPass(effectFilm); - composer3.addPass(effectVignette); - - // - - composer4 = new EffectComposer(renderer, new THREE.WebGLRenderTarget(rtWidth, rtHeight, rtParameters)); - - composer4.addPass(renderScene); - composer4.addPass(gammaCorrection); - composer4.addPass(effectBloom); - composer4.addPass(effectFilm); - composer4.addPass(effectBleach); - composer4.addPass(effectVignette); - - renderScene.uniforms['tDiffuse'].value = composerScene.renderTarget2.texture; - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - halfWidth = window.innerWidth / 2; - halfHeight = window.innerHeight / 2; - - cameraPerspective.aspect = window.innerWidth / window.innerHeight; - cameraPerspective.updateProjectionMatrix(); - - cameraOrtho.left = -halfWidth; - cameraOrtho.right = halfWidth; - cameraOrtho.top = halfHeight; - cameraOrtho.bottom = -halfHeight; - - cameraOrtho.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - composerScene.setSize(halfWidth * 2, halfHeight * 2); - - composer1.setSize(halfWidth, halfHeight); - composer2.setSize(halfWidth, halfHeight); - composer3.setSize(halfWidth, halfHeight); - composer4.setSize(halfWidth, halfHeight); - - renderScene.uniforms['tDiffuse'].value = composerScene.renderTarget2.texture; - - quadBG.scale.set(window.innerWidth, window.innerHeight, 1); - quadMask.scale.set(window.innerWidth / 2, window.innerHeight / 2, 1); -} - -function createMesh(geometry, scene, scale) { - const diffuseMap = new THREE.TextureLoader().load('models/gltf/LeePerrySmith/Map-COL.jpg'); - diffuseMap.colorSpace = THREE.SRGBColorSpace; - - const mat2 = new THREE.MeshPhongMaterial({ - color: 0xcbcbcb, - specular: 0x080808, - shininess: 20, - map: diffuseMap, - normalMap: new THREE.TextureLoader().load('models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg'), - normalScale: new THREE.Vector2(0.75, 0.75), - }); - - mesh = new THREE.Mesh(geometry, mat2); - mesh.position.set(0, -50, 0); - mesh.scale.set(scale, scale, scale); - - scene.add(mesh); -} - -// - -function animate() { - stats.begin(); - render(); - stats.end(); -} - -function render() { - const time = Date.now() * 0.0004; - - if (mesh) mesh.rotation.y = -time; - - renderer.setViewport(0, 0, halfWidth, halfHeight); - composerScene.render(delta); - - renderer.setViewport(0, 0, halfWidth, halfHeight); - composer1.render(delta); - - renderer.setViewport(halfWidth, 0, halfWidth, halfHeight); - composer2.render(delta); - - renderer.setViewport(0, halfHeight, halfWidth, halfHeight); - composer3.render(delta); - - renderer.setViewport(halfWidth, halfHeight, halfWidth, halfHeight); - composer4.render(delta); -} diff --git a/examples-testing/examples/webgl_postprocessing_afterimage.ts b/examples-testing/examples/webgl_postprocessing_afterimage.ts deleted file mode 100644 index 508f90b89..000000000 --- a/examples-testing/examples/webgl_postprocessing_afterimage.ts +++ /dev/null @@ -1,72 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { AfterimagePass } from 'three/addons/postprocessing/AfterimagePass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -let camera, scene, renderer, composer; -let mesh; - -let afterimagePass; - -const params = { - enable: true, -}; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 400; - - scene = new THREE.Scene(); - scene.fog = new THREE.Fog(0x000000, 1, 1000); - - const geometry = new THREE.BoxGeometry(150, 150, 150, 2, 2, 2); - const material = new THREE.MeshNormalMaterial(); - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // postprocessing - - composer = new EffectComposer(renderer); - composer.addPass(new RenderPass(scene, camera)); - - afterimagePass = new AfterimagePass(); - composer.addPass(afterimagePass); - - const outputPass = new OutputPass(); - composer.addPass(outputPass); - - window.addEventListener('resize', onWindowResize); - - const gui = new GUI({ title: 'Damp setting' }); - gui.add(afterimagePass.uniforms['damp'], 'value', 0, 1).step(0.001); - gui.add(params, 'enable'); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - composer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - mesh.rotation.x += 0.005; - mesh.rotation.y += 0.01; - - afterimagePass.enabled = params.enable; - - composer.render(); -} diff --git a/examples-testing/examples/webgl_postprocessing_backgrounds.ts b/examples-testing/examples/webgl_postprocessing_backgrounds.ts deleted file mode 100644 index 57a6a2dbd..000000000 --- a/examples-testing/examples/webgl_postprocessing_backgrounds.ts +++ /dev/null @@ -1,214 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { TexturePass } from 'three/addons/postprocessing/TexturePass.js'; -import { CubeTexturePass } from 'three/addons/postprocessing/CubeTexturePass.js'; -import { ClearPass } from 'three/addons/postprocessing/ClearPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let scene, renderer, composer; -let clearPass, texturePass, renderPass; -let cameraP, cubeTexturePassP; -let gui, stats; - -const params = { - clearPass: true, - clearColor: 'white', - clearAlpha: 1.0, - - texturePass: true, - texturePassOpacity: 1.0, - - cubeTexturePass: true, - cubeTexturePassOpacity: 1.0, - - renderPass: true, -}; - -init(); - -clearGui(); - -function clearGui() { - if (gui) gui.destroy(); - - gui = new GUI(); - - gui.add(params, 'clearPass'); - gui.add(params, 'clearColor', ['black', 'white', 'blue', 'green', 'red']); - gui.add(params, 'clearAlpha', 0, 1); - - gui.add(params, 'texturePass'); - gui.add(params, 'texturePassOpacity', 0, 1); - - gui.add(params, 'cubeTexturePass'); - gui.add(params, 'cubeTexturePassOpacity', 0, 1); - - gui.add(params, 'renderPass'); - - gui.open(); -} - -function init() { - const container = document.getElementById('container'); - - const width = window.innerWidth || 1; - const height = window.innerHeight || 1; - const aspect = width / height; - const devicePixelRatio = window.devicePixelRatio || 1; - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(devicePixelRatio); - renderer.setSize(width, height); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - cameraP = new THREE.PerspectiveCamera(65, aspect, 1, 10); - cameraP.position.z = 7; - - scene = new THREE.Scene(); - - const group = new THREE.Group(); - scene.add(group); - - const light = new THREE.PointLight(0xefffef, 500); - light.position.z = 10; - light.position.y = -10; - light.position.x = -10; - scene.add(light); - - const light2 = new THREE.PointLight(0xffefef, 500); - light2.position.z = 10; - light2.position.x = -10; - light2.position.y = 10; - scene.add(light2); - - const light3 = new THREE.PointLight(0xefefff, 500); - light3.position.z = 10; - light3.position.x = 10; - light3.position.y = -10; - scene.add(light3); - - const geometry = new THREE.SphereGeometry(1, 48, 24); - - const material = new THREE.MeshStandardMaterial(); - material.roughness = 0.5 * Math.random() + 0.25; - material.metalness = 0; - material.color.setHSL(Math.random(), 1.0, 0.3); - - const mesh = new THREE.Mesh(geometry, material); - group.add(mesh); - - // postprocessing - - const genCubeUrls = function (prefix, postfix) { - return [ - prefix + 'px' + postfix, - prefix + 'nx' + postfix, - prefix + 'py' + postfix, - prefix + 'ny' + postfix, - prefix + 'pz' + postfix, - prefix + 'nz' + postfix, - ]; - }; - - composer = new EffectComposer(renderer); - - clearPass = new ClearPass(params.clearColor, params.clearAlpha); - composer.addPass(clearPass); - - texturePass = new TexturePass(); - composer.addPass(texturePass); - - const textureLoader = new THREE.TextureLoader(); - textureLoader.load('textures/hardwood2_diffuse.jpg', function (map) { - map.colorSpace = THREE.SRGBColorSpace; - texturePass.map = map; - }); - - cubeTexturePassP = null; - - const ldrUrls = genCubeUrls('textures/cube/pisa/', '.png'); - new THREE.CubeTextureLoader().load(ldrUrls, function (ldrCubeMap) { - cubeTexturePassP = new CubeTexturePass(cameraP, ldrCubeMap); - composer.insertPass(cubeTexturePassP, 2); - }); - - renderPass = new RenderPass(scene, cameraP); - renderPass.clear = false; - composer.addPass(renderPass); - - const outputPass = new OutputPass(); - composer.addPass(outputPass); - - const controls = new OrbitControls(cameraP, renderer.domElement); - controls.enableZoom = false; - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - const aspect = width / height; - - cameraP.aspect = aspect; - cameraP.updateProjectionMatrix(); - - renderer.setSize(width, height); - composer.setSize(width, height); -} - -function animate() { - stats.begin(); - - cameraP.updateMatrixWorld(true); - - let newColor = clearPass.clearColor; - - switch (params.clearColor) { - case 'blue': - newColor = 0x0000ff; - break; - case 'red': - newColor = 0xff0000; - break; - case 'green': - newColor = 0x00ff00; - break; - case 'white': - newColor = 0xffffff; - break; - case 'black': - newColor = 0x000000; - break; - } - - clearPass.enabled = params.clearPass; - clearPass.clearColor = newColor; - clearPass.clearAlpha = params.clearAlpha; - - texturePass.enabled = params.texturePass; - texturePass.opacity = params.texturePassOpacity; - - if (cubeTexturePassP !== null) { - cubeTexturePassP.enabled = params.cubeTexturePass; - cubeTexturePassP.opacity = params.cubeTexturePassOpacity; - } - - renderPass.enabled = params.renderPass; - - composer.render(); - - stats.end(); -} diff --git a/examples-testing/examples/webgl_postprocessing_fxaa.ts b/examples-testing/examples/webgl_postprocessing_fxaa.ts deleted file mode 100644 index 55745f88e..000000000 --- a/examples-testing/examples/webgl_postprocessing_fxaa.ts +++ /dev/null @@ -1,132 +0,0 @@ -import * as THREE from 'three'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; -import { FXAAShader } from 'three/addons/shaders/FXAAShader.js'; - -let camera, scene, renderer, clock, group, container; - -let composer1, composer2, fxaaPass; - -init(); - -function init() { - container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(45, container.offsetWidth / container.offsetHeight, 1, 2000); - camera.position.z = 500; - - scene = new THREE.Scene(); - - clock = new THREE.Clock(); - - // - - const hemiLight = new THREE.HemisphereLight(0xffffff, 0x8d8d8d); - hemiLight.position.set(0, 1000, 0); - scene.add(hemiLight); - - const dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.position.set(-3000, 1000, -1000); - scene.add(dirLight); - - // - - group = new THREE.Group(); - - const geometry = new THREE.TetrahedronGeometry(10); - const material = new THREE.MeshStandardMaterial({ color: 0xf73232, flatShading: true }); - - for (let i = 0; i < 100; i++) { - const mesh = new THREE.Mesh(geometry, material); - - mesh.position.x = Math.random() * 500 - 250; - mesh.position.y = Math.random() * 500 - 250; - mesh.position.z = Math.random() * 500 - 250; - - mesh.scale.setScalar(Math.random() * 2 + 1); - - mesh.rotation.x = Math.random() * Math.PI; - mesh.rotation.y = Math.random() * Math.PI; - mesh.rotation.z = Math.random() * Math.PI; - - group.add(mesh); - } - - scene.add(group); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(container.offsetWidth, container.offsetHeight); - renderer.setAnimationLoop(animate); - renderer.autoClear = false; - container.appendChild(renderer.domElement); - - // - - const renderPass = new RenderPass(scene, camera); - renderPass.clearAlpha = 0; - - // - - fxaaPass = new ShaderPass(FXAAShader); - - const outputPass = new OutputPass(); - - composer1 = new EffectComposer(renderer); - composer1.addPass(renderPass); - composer1.addPass(outputPass); - - // - - const pixelRatio = renderer.getPixelRatio(); - - fxaaPass.material.uniforms['resolution'].value.x = 1 / (container.offsetWidth * pixelRatio); - fxaaPass.material.uniforms['resolution'].value.y = 1 / (container.offsetHeight * pixelRatio); - - composer2 = new EffectComposer(renderer); - composer2.addPass(renderPass); - composer2.addPass(outputPass); - - // FXAA is engineered to be applied towards the end of engine post processing after conversion to low dynamic range and conversion to the sRGB color space for display. - - composer2.addPass(fxaaPass); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = container.offsetWidth / container.offsetHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(container.offsetWidth, container.offsetHeight); - composer1.setSize(container.offsetWidth, container.offsetHeight); - composer2.setSize(container.offsetWidth, container.offsetHeight); - - const pixelRatio = renderer.getPixelRatio(); - - fxaaPass.material.uniforms['resolution'].value.x = 1 / (container.offsetWidth * pixelRatio); - fxaaPass.material.uniforms['resolution'].value.y = 1 / (container.offsetHeight * pixelRatio); -} - -function animate() { - const halfWidth = container.offsetWidth / 2; - - group.rotation.y += clock.getDelta() * 0.1; - - renderer.setScissorTest(true); - - renderer.setScissor(0, 0, halfWidth - 1, container.offsetHeight); - composer1.render(); - - renderer.setScissor(halfWidth, 0, halfWidth, container.offsetHeight); - composer2.render(); - - renderer.setScissorTest(false); -} diff --git a/examples-testing/examples/webgl_postprocessing_glitch.ts b/examples-testing/examples/webgl_postprocessing_glitch.ts deleted file mode 100644 index f846c0ce6..000000000 --- a/examples-testing/examples/webgl_postprocessing_glitch.ts +++ /dev/null @@ -1,97 +0,0 @@ -import * as THREE from 'three'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { GlitchPass } from 'three/addons/postprocessing/GlitchPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -let camera, scene, renderer, composer; -let object, light; - -let glitchPass; - -const button = document.querySelector('#startButton'); -button.addEventListener('click', function () { - const overlay = document.getElementById('overlay'); - overlay.remove(); - - init(); -}); - -function updateOptions() { - const wildGlitch = document.getElementById('wildGlitch'); - glitchPass.goWild = wildGlitch.checked; -} - -function init() { - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 400; - - scene = new THREE.Scene(); - scene.fog = new THREE.Fog(0x000000, 1, 1000); - - object = new THREE.Object3D(); - scene.add(object); - - const geometry = new THREE.SphereGeometry(1, 4, 4); - - for (let i = 0; i < 100; i++) { - const material = new THREE.MeshPhongMaterial({ color: 0xffffff * Math.random(), flatShading: true }); - - const mesh = new THREE.Mesh(geometry, material); - mesh.position.set(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5).normalize(); - mesh.position.multiplyScalar(Math.random() * 400); - mesh.rotation.set(Math.random() * 2, Math.random() * 2, Math.random() * 2); - mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50; - object.add(mesh); - } - - scene.add(new THREE.AmbientLight(0xcccccc)); - - light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(1, 1, 1); - scene.add(light); - - // postprocessing - - composer = new EffectComposer(renderer); - composer.addPass(new RenderPass(scene, camera)); - - glitchPass = new GlitchPass(); - composer.addPass(glitchPass); - - const outputPass = new OutputPass(); - composer.addPass(outputPass); - - // - - window.addEventListener('resize', onWindowResize); - - const wildGlitchOption = document.getElementById('wildGlitch'); - wildGlitchOption.addEventListener('change', updateOptions); - - updateOptions(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - composer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - object.rotation.x += 0.005; - object.rotation.y += 0.01; - - composer.render(); -} diff --git a/examples-testing/examples/webgl_postprocessing_godrays.ts b/examples-testing/examples/webgl_postprocessing_godrays.ts deleted file mode 100644 index fb7604411..000000000 --- a/examples-testing/examples/webgl_postprocessing_godrays.ts +++ /dev/null @@ -1,347 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { - GodRaysFakeSunShader, - GodRaysDepthMaskShader, - GodRaysCombineShader, - GodRaysGenerateShader, -} from 'three/addons/shaders/GodRaysShader.js'; - -let container, stats; -let camera, scene, renderer, materialDepth; - -let sphereMesh; - -const sunPosition = new THREE.Vector3(0, 1000, -1000); -const clipPosition = new THREE.Vector4(); -const screenSpacePosition = new THREE.Vector3(); - -const postprocessing = { enabled: true }; - -const orbitRadius = 200; - -const bgColor = 0x000511; -const sunColor = 0xffee00; - -// Use a smaller size for some of the god-ray render targets for better performance. -const godrayRenderTargetResolutionMultiplier = 1.0 / 4.0; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - // - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 3000); - camera.position.z = 200; - - scene = new THREE.Scene(); - - // - - materialDepth = new THREE.MeshDepthMaterial(); - - // tree - - const loader = new OBJLoader(); - loader.load('models/obj/tree.obj', function (object) { - object.position.set(0, -150, -150); - object.scale.multiplyScalar(400); - scene.add(object); - }); - - // sphere - - const geo = new THREE.SphereGeometry(1, 20, 10); - sphereMesh = new THREE.Mesh(geo, new THREE.MeshBasicMaterial({ color: 0x000000 })); - sphereMesh.scale.multiplyScalar(20); - scene.add(sphereMesh); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setClearColor(0xffffff); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - renderer.autoClear = false; - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 50; - controls.maxDistance = 500; - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); - - // - - initPostprocessing(window.innerWidth, window.innerHeight); -} - -// - -function onWindowResize() { - const renderTargetWidth = window.innerWidth; - const renderTargetHeight = window.innerHeight; - - camera.aspect = renderTargetWidth / renderTargetHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(renderTargetWidth, renderTargetHeight); - postprocessing.rtTextureColors.setSize(renderTargetWidth, renderTargetHeight); - postprocessing.rtTextureDepth.setSize(renderTargetWidth, renderTargetHeight); - postprocessing.rtTextureDepthMask.setSize(renderTargetWidth, renderTargetHeight); - - const adjustedWidth = renderTargetWidth * godrayRenderTargetResolutionMultiplier; - const adjustedHeight = renderTargetHeight * godrayRenderTargetResolutionMultiplier; - postprocessing.rtTextureGodRays1.setSize(adjustedWidth, adjustedHeight); - postprocessing.rtTextureGodRays2.setSize(adjustedWidth, adjustedHeight); -} - -function initPostprocessing(renderTargetWidth, renderTargetHeight) { - postprocessing.scene = new THREE.Scene(); - - postprocessing.camera = new THREE.OrthographicCamera(-0.5, 0.5, 0.5, -0.5, -10000, 10000); - postprocessing.camera.position.z = 100; - - postprocessing.scene.add(postprocessing.camera); - - postprocessing.rtTextureColors = new THREE.WebGLRenderTarget(renderTargetWidth, renderTargetHeight, { - type: THREE.HalfFloatType, - }); - - // Switching the depth formats to luminance from rgb doesn't seem to work. I didn't - // investigate further for now. - // pars.format = LuminanceFormat; - - // I would have this quarter size and use it as one of the ping-pong render - // targets but the aliasing causes some temporal flickering - - postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget(renderTargetWidth, renderTargetHeight, { - type: THREE.HalfFloatType, - }); - postprocessing.rtTextureDepthMask = new THREE.WebGLRenderTarget(renderTargetWidth, renderTargetHeight, { - type: THREE.HalfFloatType, - }); - - // The ping-pong render targets can use an adjusted resolution to minimize cost - - const adjustedWidth = renderTargetWidth * godrayRenderTargetResolutionMultiplier; - const adjustedHeight = renderTargetHeight * godrayRenderTargetResolutionMultiplier; - postprocessing.rtTextureGodRays1 = new THREE.WebGLRenderTarget(adjustedWidth, adjustedHeight, { - type: THREE.HalfFloatType, - }); - postprocessing.rtTextureGodRays2 = new THREE.WebGLRenderTarget(adjustedWidth, adjustedHeight, { - type: THREE.HalfFloatType, - }); - - // god-ray shaders - - const godraysMaskShader = GodRaysDepthMaskShader; - postprocessing.godrayMaskUniforms = THREE.UniformsUtils.clone(godraysMaskShader.uniforms); - postprocessing.materialGodraysDepthMask = new THREE.ShaderMaterial({ - uniforms: postprocessing.godrayMaskUniforms, - vertexShader: godraysMaskShader.vertexShader, - fragmentShader: godraysMaskShader.fragmentShader, - }); - - const godraysGenShader = GodRaysGenerateShader; - postprocessing.godrayGenUniforms = THREE.UniformsUtils.clone(godraysGenShader.uniforms); - postprocessing.materialGodraysGenerate = new THREE.ShaderMaterial({ - uniforms: postprocessing.godrayGenUniforms, - vertexShader: godraysGenShader.vertexShader, - fragmentShader: godraysGenShader.fragmentShader, - }); - - const godraysCombineShader = GodRaysCombineShader; - postprocessing.godrayCombineUniforms = THREE.UniformsUtils.clone(godraysCombineShader.uniforms); - postprocessing.materialGodraysCombine = new THREE.ShaderMaterial({ - uniforms: postprocessing.godrayCombineUniforms, - vertexShader: godraysCombineShader.vertexShader, - fragmentShader: godraysCombineShader.fragmentShader, - }); - - const godraysFakeSunShader = GodRaysFakeSunShader; - postprocessing.godraysFakeSunUniforms = THREE.UniformsUtils.clone(godraysFakeSunShader.uniforms); - postprocessing.materialGodraysFakeSun = new THREE.ShaderMaterial({ - uniforms: postprocessing.godraysFakeSunUniforms, - vertexShader: godraysFakeSunShader.vertexShader, - fragmentShader: godraysFakeSunShader.fragmentShader, - }); - - postprocessing.godraysFakeSunUniforms.bgColor.value.setHex(bgColor); - postprocessing.godraysFakeSunUniforms.sunColor.value.setHex(sunColor); - - postprocessing.godrayCombineUniforms.fGodRayIntensity.value = 0.75; - - postprocessing.quad = new THREE.Mesh(new THREE.PlaneGeometry(1.0, 1.0), postprocessing.materialGodraysGenerate); - postprocessing.quad.position.z = -9900; - postprocessing.scene.add(postprocessing.quad); -} - -function animate() { - stats.begin(); - render(); - stats.end(); -} - -function getStepSize(filterLen, tapsPerPass, pass) { - return filterLen * Math.pow(tapsPerPass, -pass); -} - -function filterGodRays(inputTex, renderTarget, stepSize) { - postprocessing.scene.overrideMaterial = postprocessing.materialGodraysGenerate; - - postprocessing.godrayGenUniforms['fStepSize'].value = stepSize; - postprocessing.godrayGenUniforms['tInput'].value = inputTex; - - renderer.setRenderTarget(renderTarget); - renderer.render(postprocessing.scene, postprocessing.camera); - postprocessing.scene.overrideMaterial = null; -} - -function render() { - const time = Date.now() / 4000; - - sphereMesh.position.x = orbitRadius * Math.cos(time); - sphereMesh.position.z = orbitRadius * Math.sin(time) - 100; - - if (postprocessing.enabled) { - clipPosition.x = sunPosition.x; - clipPosition.y = sunPosition.y; - clipPosition.z = sunPosition.z; - clipPosition.w = 1; - - clipPosition.applyMatrix4(camera.matrixWorldInverse).applyMatrix4(camera.projectionMatrix); - - // perspective divide (produce NDC space) - - clipPosition.x /= clipPosition.w; - clipPosition.y /= clipPosition.w; - - screenSpacePosition.x = (clipPosition.x + 1) / 2; // transform from [-1,1] to [0,1] - screenSpacePosition.y = (clipPosition.y + 1) / 2; // transform from [-1,1] to [0,1] - screenSpacePosition.z = clipPosition.z; // needs to stay in clip space for visibilty checks - - // Give it to the god-ray and sun shaders - - postprocessing.godrayGenUniforms['vSunPositionScreenSpace'].value.copy(screenSpacePosition); - postprocessing.godraysFakeSunUniforms['vSunPositionScreenSpace'].value.copy(screenSpacePosition); - - // -- Draw sky and sun -- - - // Clear colors and depths, will clear to sky color - - renderer.setRenderTarget(postprocessing.rtTextureColors); - renderer.clear(true, true, false); - - // Sun render. Runs a shader that gives a brightness based on the screen - // space distance to the sun. Not very efficient, so i make a scissor - // rectangle around the suns position to avoid rendering surrounding pixels. - - const sunsqH = 0.74 * window.innerHeight; // 0.74 depends on extent of sun from shader - const sunsqW = 0.74 * window.innerHeight; // both depend on height because sun is aspect-corrected - - screenSpacePosition.x *= window.innerWidth; - screenSpacePosition.y *= window.innerHeight; - - renderer.setScissor(screenSpacePosition.x - sunsqW / 2, screenSpacePosition.y - sunsqH / 2, sunsqW, sunsqH); - renderer.setScissorTest(true); - - postprocessing.godraysFakeSunUniforms['fAspect'].value = window.innerWidth / window.innerHeight; - - postprocessing.scene.overrideMaterial = postprocessing.materialGodraysFakeSun; - renderer.setRenderTarget(postprocessing.rtTextureColors); - renderer.render(postprocessing.scene, postprocessing.camera); - - renderer.setScissorTest(false); - - // -- Draw scene objects -- - - // Colors - - scene.overrideMaterial = null; - renderer.setRenderTarget(postprocessing.rtTextureColors); - renderer.render(scene, camera); - - // Depth - - scene.overrideMaterial = materialDepth; - renderer.setRenderTarget(postprocessing.rtTextureDepth); - renderer.clear(); - renderer.render(scene, camera); - - // - - postprocessing.godrayMaskUniforms['tInput'].value = postprocessing.rtTextureDepth.texture; - - postprocessing.scene.overrideMaterial = postprocessing.materialGodraysDepthMask; - renderer.setRenderTarget(postprocessing.rtTextureDepthMask); - renderer.render(postprocessing.scene, postprocessing.camera); - - // -- Render god-rays -- - - // Maximum length of god-rays (in texture space [0,1]X[0,1]) - - const filterLen = 1.0; - - // Samples taken by filter - - const TAPS_PER_PASS = 6.0; - - // Pass order could equivalently be 3,2,1 (instead of 1,2,3), which - // would start with a small filter support and grow to large. however - // the large-to-small order produces less objectionable aliasing artifacts that - // appear as a glimmer along the length of the beams - - // pass 1 - render into first ping-pong target - filterGodRays( - postprocessing.rtTextureDepthMask.texture, - postprocessing.rtTextureGodRays2, - getStepSize(filterLen, TAPS_PER_PASS, 1.0), - ); - - // pass 2 - render into second ping-pong target - filterGodRays( - postprocessing.rtTextureGodRays2.texture, - postprocessing.rtTextureGodRays1, - getStepSize(filterLen, TAPS_PER_PASS, 2.0), - ); - - // pass 3 - 1st RT - filterGodRays( - postprocessing.rtTextureGodRays1.texture, - postprocessing.rtTextureGodRays2, - getStepSize(filterLen, TAPS_PER_PASS, 3.0), - ); - - // final pass - composite god-rays onto colors - - postprocessing.godrayCombineUniforms['tColors'].value = postprocessing.rtTextureColors.texture; - postprocessing.godrayCombineUniforms['tGodRays'].value = postprocessing.rtTextureGodRays2.texture; - - postprocessing.scene.overrideMaterial = postprocessing.materialGodraysCombine; - - renderer.setRenderTarget(null); - renderer.render(postprocessing.scene, postprocessing.camera); - postprocessing.scene.overrideMaterial = null; - } else { - renderer.setRenderTarget(null); - renderer.clear(); - renderer.render(scene, camera); - } -} diff --git a/examples-testing/examples/webgl_postprocessing_gtao.ts b/examples-testing/examples/webgl_postprocessing_gtao.ts deleted file mode 100644 index 4f16d1554..000000000 --- a/examples-testing/examples/webgl_postprocessing_gtao.ts +++ /dev/null @@ -1,215 +0,0 @@ -import * as THREE from 'three'; -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { GTAOPass } from 'three/addons/postprocessing/GTAOPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -let camera, scene, renderer, composer, controls, clock, stats, mixer; - -init(); - -function init() { - const dracoLoader = new DRACOLoader(); - dracoLoader.setDecoderPath('jsm/libs/draco/'); - dracoLoader.setDecoderConfig({ type: 'js' }); - const loader = new GLTFLoader(); - loader.setDRACOLoader(dracoLoader); - loader.setPath('models/gltf/'); - - clock = new THREE.Clock(); - const container = document.createElement('div'); - document.body.appendChild(container); - - stats = new Stats(); - container.appendChild(stats.dom); - - renderer = new THREE.WebGLRenderer(); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - const pmremGenerator = new THREE.PMREMGenerator(renderer); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xbfe3dd); - scene.environment = pmremGenerator.fromScene(new RoomEnvironment(), 0.04).texture; - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 100); - camera.position.set(5, 2, 8); - - controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 0.5, 0); - controls.update(); - controls.enablePan = false; - controls.enableDamping = true; - - const width = window.innerWidth; - const height = window.innerHeight; - - composer = new EffectComposer(renderer); - - const renderPass = new RenderPass(scene, camera); - composer.addPass(renderPass); - - const gtaoPass = new GTAOPass(scene, camera, width, height); - gtaoPass.output = GTAOPass.OUTPUT.Denoise; - composer.addPass(gtaoPass); - - const outputPass = new OutputPass(); - composer.addPass(outputPass); - - // - - loader.load( - 'LittlestTokyo.glb', - gltf => { - const model = gltf.scene; - model.position.set(1, 1, 0); - model.scale.set(0.01, 0.01, 0.01); - scene.add(model); - - mixer = new THREE.AnimationMixer(model); - mixer.clipAction(gltf.animations[0]).play(); - - const box = new THREE.Box3().setFromObject(scene); - gtaoPass.setSceneClipBox(box); - }, - undefined, - e => console.error(e), - ); - - // Init gui - const gui = new GUI(); - - gui.add(gtaoPass, 'output', { - Default: GTAOPass.OUTPUT.Default, - Diffuse: GTAOPass.OUTPUT.Diffuse, - 'AO Only': GTAOPass.OUTPUT.AO, - 'AO Only + Denoise': GTAOPass.OUTPUT.Denoise, - Depth: GTAOPass.OUTPUT.Depth, - Normal: GTAOPass.OUTPUT.Normal, - }).onChange(function (value) { - gtaoPass.output = value; - }); - - const aoParameters = { - radius: 0.25, - distanceExponent: 1, - thickness: 1, - scale: 1, - samples: 16, - distanceFallOff: 1, - screenSpaceRadius: false, - }; - const pdParameters = { - lumaPhi: 10, - depthPhi: 2, - normalPhi: 3, - radius: 4, - radiusExponent: 1, - rings: 2, - samples: 16, - }; - gtaoPass.updateGtaoMaterial(aoParameters); - gtaoPass.updatePdMaterial(pdParameters); - gui.add(gtaoPass, 'blendIntensity').min(0).max(1).step(0.01); - gui.add(aoParameters, 'radius') - .min(0.01) - .max(1) - .step(0.01) - .onChange(() => gtaoPass.updateGtaoMaterial(aoParameters)); - gui.add(aoParameters, 'distanceExponent') - .min(1) - .max(4) - .step(0.01) - .onChange(() => gtaoPass.updateGtaoMaterial(aoParameters)); - gui.add(aoParameters, 'thickness') - .min(0.01) - .max(10) - .step(0.01) - .onChange(() => gtaoPass.updateGtaoMaterial(aoParameters)); - gui.add(aoParameters, 'distanceFallOff') - .min(0) - .max(1) - .step(0.01) - .onChange(() => gtaoPass.updateGtaoMaterial(aoParameters)); - gui.add(aoParameters, 'scale') - .min(0.01) - .max(2.0) - .step(0.01) - .onChange(() => gtaoPass.updateGtaoMaterial(aoParameters)); - gui.add(aoParameters, 'samples') - .min(2) - .max(32) - .step(1) - .onChange(() => gtaoPass.updateGtaoMaterial(aoParameters)); - gui.add(aoParameters, 'screenSpaceRadius').onChange(() => gtaoPass.updateGtaoMaterial(aoParameters)); - gui.add(pdParameters, 'lumaPhi') - .min(0) - .max(20) - .step(0.01) - .onChange(() => gtaoPass.updatePdMaterial(pdParameters)); - gui.add(pdParameters, 'depthPhi') - .min(0.01) - .max(20) - .step(0.01) - .onChange(() => gtaoPass.updatePdMaterial(pdParameters)); - gui.add(pdParameters, 'normalPhi') - .min(0.01) - .max(20) - .step(0.01) - .onChange(() => gtaoPass.updatePdMaterial(pdParameters)); - gui.add(pdParameters, 'radius') - .min(0) - .max(32) - .step(1) - .onChange(() => gtaoPass.updatePdMaterial(pdParameters)); - gui.add(pdParameters, 'radiusExponent') - .min(0.1) - .max(4) - .step(0.1) - .onChange(() => gtaoPass.updatePdMaterial(pdParameters)); - gui.add(pdParameters, 'rings') - .min(1) - .max(16) - .step(0.125) - .onChange(() => gtaoPass.updatePdMaterial(pdParameters)); - gui.add(pdParameters, 'samples') - .min(2) - .max(32) - .step(1) - .onChange(() => gtaoPass.updatePdMaterial(pdParameters)); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); - composer.setSize(width, height); -} - -function animate() { - const delta = clock.getDelta(); - - if (mixer) { - mixer.update(delta); - } - - controls.update(); - - stats.begin(); - composer.render(); - stats.end(); -} diff --git a/examples-testing/examples/webgl_postprocessing_masking.ts b/examples-testing/examples/webgl_postprocessing_masking.ts deleted file mode 100644 index f6e7310bf..000000000 --- a/examples-testing/examples/webgl_postprocessing_masking.ts +++ /dev/null @@ -1,100 +0,0 @@ -import * as THREE from 'three'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { TexturePass } from 'three/addons/postprocessing/TexturePass.js'; -import { ClearPass } from 'three/addons/postprocessing/ClearPass.js'; -import { MaskPass, ClearMaskPass } from 'three/addons/postprocessing/MaskPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -let camera, composer, renderer; -let box, torus; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 10; - - const scene1 = new THREE.Scene(); - const scene2 = new THREE.Scene(); - - box = new THREE.Mesh(new THREE.BoxGeometry(4, 4, 4)); - scene1.add(box); - - torus = new THREE.Mesh(new THREE.TorusGeometry(3, 1, 16, 32)); - scene2.add(torus); - - renderer = new THREE.WebGLRenderer(); - renderer.setClearColor(0xe0e0e0); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.autoClear = false; - document.body.appendChild(renderer.domElement); - - // - - const clearPass = new ClearPass(); - - const clearMaskPass = new ClearMaskPass(); - - const maskPass1 = new MaskPass(scene1, camera); - const maskPass2 = new MaskPass(scene2, camera); - - const texture1 = new THREE.TextureLoader().load('textures/758px-Canestra_di_frutta_(Caravaggio).jpg'); - texture1.colorSpace = THREE.SRGBColorSpace; - texture1.minFilter = THREE.LinearFilter; - const texture2 = new THREE.TextureLoader().load('textures/2294472375_24a3b8ef46_o.jpg'); - texture2.colorSpace = THREE.SRGBColorSpace; - - const texturePass1 = new TexturePass(texture1); - const texturePass2 = new TexturePass(texture2); - - const outputPass = new OutputPass(); - - const parameters = { - stencilBuffer: true, - }; - - const renderTarget = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight, parameters); - - composer = new EffectComposer(renderer, renderTarget); - composer.addPass(clearPass); - composer.addPass(maskPass1); - composer.addPass(texturePass1); - composer.addPass(clearMaskPass); - composer.addPass(maskPass2); - composer.addPass(texturePass2); - composer.addPass(clearMaskPass); - composer.addPass(outputPass); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); - composer.setSize(width, height); -} - -function animate() { - const time = performance.now() * 0.001 + 6000; - - box.position.x = Math.cos(time / 1.5) * 2; - box.position.y = Math.sin(time) * 2; - box.rotation.x = time; - box.rotation.y = time / 2; - - torus.position.x = Math.cos(time) * 2; - torus.position.y = Math.sin(time / 1.5) * 2; - torus.rotation.x = time; - torus.rotation.y = time / 2; - - renderer.clear(); - composer.render(time); -} diff --git a/examples-testing/examples/webgl_postprocessing_material_ao.ts b/examples-testing/examples/webgl_postprocessing_material_ao.ts deleted file mode 100644 index 2f17a5304..000000000 --- a/examples-testing/examples/webgl_postprocessing_material_ao.ts +++ /dev/null @@ -1,277 +0,0 @@ -import * as THREE from 'three'; -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; -import { PLYLoader } from 'three/addons/loaders/PLYLoader.js'; -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { GTAOPass } from 'three/addons/postprocessing/GTAOPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; -import { MeshPostProcessingMaterial } from 'three/addons/materials/MeshPostProcessingMaterial.js'; - -let renderer, camera, scene, composer, controls, stats; -const sceneParameters = { - output: 0, - envMapIntensity: 1.0, - ambientLightIntensity: 0.0, - lightIntensity: 50, - shadow: true, -}; -const aoParameters = { - radius: 0.5, - distanceExponent: 2, - thickness: 10, - scale: 1, - samples: 16, - distanceFallOff: 1, -}; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - stats = new Stats(); - container.appendChild(stats.dom); - - renderer = new THREE.WebGLRenderer(); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - renderer.shadowMap.enabled = sceneParameters.shadow; - - const plyLoader = new PLYLoader(); - const rgbeloader = new RGBELoader(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 50); - camera.position.set(0, 3, 5); - controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 1, 0); - controls.update(); - controls.enablePan = false; - controls.enableDamping = true; - - const width = window.innerWidth; - const height = window.innerHeight; - - scene = new THREE.Scene(); - composer = new EffectComposer(renderer); - - const gtaoPass = new GTAOPass(scene, camera, width, height); - gtaoPass.output = GTAOPass.OUTPUT.Off; - const renderPasse = new RenderPass(scene, camera); - const outputPass = new OutputPass(); - - composer.addPass(gtaoPass); - composer.addPass(renderPasse); - composer.addPass(outputPass); - - rgbeloader.load('textures/equirectangular/royal_esplanade_1k.hdr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - scene.environment = texture; - }); - - const groundMaterial = new MeshPostProcessingMaterial({ - color: 0x7f7f7f, - envMapIntensity: sceneParameters.envMapIntensity, - aoPassMap: gtaoPass.gtaoMap, - }); - const objectMaterial = new MeshPostProcessingMaterial({ - color: 0xffffff, - roughness: 0.5, - metalness: 0.5, - envMapIntensity: sceneParameters.envMapIntensity, - aoPassMap: gtaoPass.gtaoMap, - }); - const emissiveMaterial = new MeshPostProcessingMaterial({ - color: 0, - emissive: 0xffffff, - aoPassMap: gtaoPass.gtaoMap, - }); - plyLoader.load('models/ply/binary/Lucy100k.ply', geometry => { - geometry.computeVertexNormals(); - const lucy = new THREE.Mesh(geometry, objectMaterial); - lucy.receiveShadow = true; - lucy.castShadow = true; - lucy.scale.setScalar(0.001); - lucy.rotation.set(0, Math.PI, 0); - lucy.position.set(0.04, 1.8, 0.02); - scene.add(lucy); - }); - const ambientLight = new THREE.AmbientLight(0xffffff, sceneParameters.ambientLightIntensity); - const lightGroup = new THREE.Group(); - const planeGeometry = new THREE.PlaneGeometry(6, 6); - const cylinderGeometry = new THREE.CylinderGeometry(0.5, 0.5, 1, 64); - const sphereGeometry = new THREE.SphereGeometry(0.5, 32, 32); - const lightSphereGeometry = new THREE.SphereGeometry(0.1, 32, 32); - scene.background = new THREE.Color(0xbfe3dd); - scene.add(ambientLight); - scene.add(lightGroup); - const targetObject = new THREE.Object3D(); - targetObject.position.set(0, 1, 0); - scene.add(targetObject); - const lightColors = [0xff4040, 0x40ff40, 0x4040ff]; - for (let j = 0; j < 3; ++j) { - const light = new THREE.SpotLight(lightColors[j], sceneParameters.lightIntensity, 0, Math.PI / 9); - light.castShadow = true; - light.shadow.camera.far = 15; - light.position.set(5 * Math.cos((Math.PI * j * 2) / 3), 2.5, 5 * Math.sin((Math.PI * j * 2) / 3)); - light.target = targetObject; - lightGroup.add(light); - } - - const groundPlane = new THREE.Mesh(planeGeometry, groundMaterial); - groundPlane.rotation.x = -Math.PI / 2; - groundPlane.position.set(0, 0, 0); - groundPlane.receiveShadow = true; - scene.add(groundPlane); - const pedestal = new THREE.Mesh(cylinderGeometry, groundMaterial); - pedestal.position.set(0, 0.5, 0); - pedestal.receiveShadow = true; - pedestal.castShadow = true; - scene.add(pedestal); - const sphereMesh = new THREE.InstancedMesh(sphereGeometry, objectMaterial, 6); - sphereMesh.receiveShadow = true; - sphereMesh.castShadow = true; - scene.add(sphereMesh); - [...Array(6).keys()].forEach(i => - sphereMesh.setMatrixAt( - i, - new THREE.Matrix4().makeTranslation(Math.cos((Math.PI * i) / 3), 0.5, Math.sin((Math.PI * i) / 3)), - ), - ); - const lightSphereMesh = new THREE.InstancedMesh(lightSphereGeometry, emissiveMaterial, 4); - scene.add(lightSphereMesh); - [...Array(4).keys()].forEach(i => - lightSphereMesh.setMatrixAt( - i, - new THREE.Matrix4().makeTranslation( - 0.4 * Math.cos((Math.PI * (i + 0.5)) / 2), - 1.1, - 0.45 * Math.sin((Math.PI * (i + 0.5)) / 2), - ), - ), - ); - - const updateGtaoMaterial = () => gtaoPass.updateGtaoMaterial(aoParameters); - const updateOutput = () => { - composer.removePass(gtaoPass); - composer.insertPass(gtaoPass, sceneParameters.output == 1 ? 1 : 0); - - switch (sceneParameters.output) { - default: - case 0: - gtaoPass.output = GTAOPass.OUTPUT.Off; - gtaoPass.enabled = true; - renderPasse.enabled = true; - break; - case 1: - gtaoPass.output = GTAOPass.OUTPUT.Default; - gtaoPass.enabled = true; - renderPasse.enabled = true; - break; - case 2: - gtaoPass.output = GTAOPass.OUTPUT.Diffuse; - gtaoPass.enabled = false; - renderPasse.enabled = true; - break; - case 3: - gtaoPass.output = GTAOPass.OUTPUT.Denoise; - gtaoPass.enabled = true; - renderPasse.enabled = false; - break; - } - - groundMaterial.aoPassMap = sceneParameters.output === 0 ? gtaoPass.gtaoMap : null; - objectMaterial.aoPassMap = sceneParameters.output === 0 ? gtaoPass.gtaoMap : null; - }; - - updateOutput(); - updateGtaoMaterial(); - - const gui = new GUI(); - gui.add(sceneParameters, 'output', { - 'material AO': 0, - 'post blended AO': 1, - 'only diffuse': 2, - 'only AO': 3, - }).onChange(() => updateOutput()); - gui.add(sceneParameters, 'envMapIntensity') - .min(0) - .max(1) - .step(0.01) - .onChange(() => { - groundMaterial.envMapIntensity = sceneParameters.envMapIntensity; - objectMaterial.envMapIntensity = sceneParameters.envMapIntensity; - }); - gui.add(sceneParameters, 'ambientLightIntensity') - .min(0.0) - .max(1.0) - .step(0.01) - .onChange(() => { - ambientLight.intensity = sceneParameters.ambientLightIntensity; - }); - gui.add(sceneParameters, 'lightIntensity') - .min(0) - .max(100) - .step(1) - .onChange(() => { - lightGroup.children.forEach(light => (light.intensity = sceneParameters.lightIntensity)); - }); - gui.add(sceneParameters, 'shadow').onChange(value => { - renderer.shadowMap.enabled = value; - lightGroup.children.forEach(light => (light.castShadow = value)); - }); - gui.add(aoParameters, 'radius') - .min(0.01) - .max(2) - .step(0.01) - .onChange(() => updateGtaoMaterial()); - gui.add(aoParameters, 'distanceExponent') - .min(1) - .max(4) - .step(0.01) - .onChange(() => updateGtaoMaterial()); - gui.add(aoParameters, 'thickness') - .min(0.01) - .max(10) - .step(0.01) - .onChange(() => updateGtaoMaterial()); - gui.add(aoParameters, 'distanceFallOff') - .min(0) - .max(1) - .step(0.01) - .onChange(() => updateGtaoMaterial()); - gui.add(aoParameters, 'scale') - .min(0.01) - .max(2.0) - .step(0.01) - .onChange(() => updateGtaoMaterial()); - gui.add(aoParameters, 'samples') - .min(2) - .max(32) - .step(1) - .onChange(() => updateGtaoMaterial()); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); - composer.setSize(width, height); -} - -function animate() { - controls.update(); - stats.begin(); - composer.render(); - stats.end(); -} diff --git a/examples-testing/examples/webgl_postprocessing_outline.ts b/examples-testing/examples/webgl_postprocessing_outline.ts deleted file mode 100644 index 356575460..000000000 --- a/examples-testing/examples/webgl_postprocessing_outline.ts +++ /dev/null @@ -1,282 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js'; -import { OutlinePass } from 'three/addons/postprocessing/OutlinePass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; -import { FXAAShader } from 'three/addons/shaders/FXAAShader.js'; - -let container, stats; -let camera, scene, renderer, controls; -let composer, effectFXAA, outlinePass; - -let selectedObjects = []; - -const raycaster = new THREE.Raycaster(); -const mouse = new THREE.Vector2(); - -const obj3d = new THREE.Object3D(); -const group = new THREE.Group(); - -const params = { - edgeStrength: 3.0, - edgeGlow: 0.0, - edgeThickness: 1.0, - pulsePeriod: 0, - rotate: false, - usePatternTexture: false, -}; - -// Init gui - -const gui = new GUI({ width: 280 }); - -gui.add(params, 'edgeStrength', 0.01, 10).onChange(function (value) { - outlinePass.edgeStrength = Number(value); -}); - -gui.add(params, 'edgeGlow', 0.0, 1).onChange(function (value) { - outlinePass.edgeGlow = Number(value); -}); - -gui.add(params, 'edgeThickness', 1, 4).onChange(function (value) { - outlinePass.edgeThickness = Number(value); -}); - -gui.add(params, 'pulsePeriod', 0.0, 5).onChange(function (value) { - outlinePass.pulsePeriod = Number(value); -}); - -gui.add(params, 'rotate'); - -gui.add(params, 'usePatternTexture').onChange(function (value) { - outlinePass.usePatternTexture = value; -}); - -function Configuration() { - this.visibleEdgeColor = '#ffffff'; - this.hiddenEdgeColor = '#190a05'; -} - -const conf = new Configuration(); - -gui.addColor(conf, 'visibleEdgeColor').onChange(function (value) { - outlinePass.visibleEdgeColor.set(value); -}); - -gui.addColor(conf, 'hiddenEdgeColor').onChange(function (value) { - outlinePass.hiddenEdgeColor.set(value); -}); - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - const width = window.innerWidth; - const height = window.innerHeight; - - renderer = new THREE.WebGLRenderer(); - renderer.shadowMap.enabled = true; - // todo - support pixelRatio in this demo - renderer.setSize(width, height); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100); - camera.position.set(0, 0, 8); - - controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 5; - controls.maxDistance = 20; - controls.enablePan = false; - controls.enableDamping = true; - controls.dampingFactor = 0.05; - - // - - scene.add(new THREE.AmbientLight(0xaaaaaa, 0.6)); - - const light = new THREE.DirectionalLight(0xddffdd, 2); - light.position.set(1, 1, 1); - light.castShadow = true; - light.shadow.mapSize.width = 1024; - light.shadow.mapSize.height = 1024; - - const d = 10; - - light.shadow.camera.left = -d; - light.shadow.camera.right = d; - light.shadow.camera.top = d; - light.shadow.camera.bottom = -d; - light.shadow.camera.far = 1000; - - scene.add(light); - - // model - - const loader = new OBJLoader(); - loader.load('models/obj/tree.obj', function (object) { - let scale = 1.0; - - object.traverse(function (child) { - if (child instanceof THREE.Mesh) { - child.geometry.center(); - child.geometry.computeBoundingSphere(); - scale = 0.2 * child.geometry.boundingSphere.radius; - - const phongMaterial = new THREE.MeshPhongMaterial({ - color: 0xffffff, - specular: 0x111111, - shininess: 5, - }); - child.material = phongMaterial; - child.receiveShadow = true; - child.castShadow = true; - } - }); - - object.position.y = 1; - object.scale.divideScalar(scale); - obj3d.add(object); - }); - - scene.add(group); - - group.add(obj3d); - - // - - const geometry = new THREE.SphereGeometry(3, 48, 24); - - for (let i = 0; i < 20; i++) { - const material = new THREE.MeshLambertMaterial(); - material.color.setHSL(Math.random(), 1.0, 0.3); - - const mesh = new THREE.Mesh(geometry, material); - mesh.position.x = Math.random() * 4 - 2; - mesh.position.y = Math.random() * 4 - 2; - mesh.position.z = Math.random() * 4 - 2; - mesh.receiveShadow = true; - mesh.castShadow = true; - mesh.scale.multiplyScalar(Math.random() * 0.3 + 0.1); - group.add(mesh); - } - - const floorMaterial = new THREE.MeshLambertMaterial({ side: THREE.DoubleSide }); - - const floorGeometry = new THREE.PlaneGeometry(12, 12); - const floorMesh = new THREE.Mesh(floorGeometry, floorMaterial); - floorMesh.rotation.x -= Math.PI * 0.5; - floorMesh.position.y -= 1.5; - group.add(floorMesh); - floorMesh.receiveShadow = true; - - const torusGeometry = new THREE.TorusGeometry(1, 0.3, 16, 100); - const torusMaterial = new THREE.MeshPhongMaterial({ color: 0xffaaff }); - const torus = new THREE.Mesh(torusGeometry, torusMaterial); - torus.position.z = -4; - group.add(torus); - torus.receiveShadow = true; - torus.castShadow = true; - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // postprocessing - - composer = new EffectComposer(renderer); - - const renderPass = new RenderPass(scene, camera); - composer.addPass(renderPass); - - outlinePass = new OutlinePass(new THREE.Vector2(window.innerWidth, window.innerHeight), scene, camera); - composer.addPass(outlinePass); - - const textureLoader = new THREE.TextureLoader(); - textureLoader.load('textures/tri_pattern.jpg', function (texture) { - outlinePass.patternTexture = texture; - texture.wrapS = THREE.RepeatWrapping; - texture.wrapT = THREE.RepeatWrapping; - }); - - const outputPass = new OutputPass(); - composer.addPass(outputPass); - - effectFXAA = new ShaderPass(FXAAShader); - effectFXAA.uniforms['resolution'].value.set(1 / window.innerWidth, 1 / window.innerHeight); - composer.addPass(effectFXAA); - - window.addEventListener('resize', onWindowResize); - - renderer.domElement.style.touchAction = 'none'; - renderer.domElement.addEventListener('pointermove', onPointerMove); - - function onPointerMove(event) { - if (event.isPrimary === false) return; - - mouse.x = (event.clientX / window.innerWidth) * 2 - 1; - mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; - - checkIntersection(); - } - - function addSelectedObject(object) { - selectedObjects = []; - selectedObjects.push(object); - } - - function checkIntersection() { - raycaster.setFromCamera(mouse, camera); - - const intersects = raycaster.intersectObject(scene, true); - - if (intersects.length > 0) { - const selectedObject = intersects[0].object; - addSelectedObject(selectedObject); - outlinePass.selectedObjects = selectedObjects; - } else { - // outlinePass.selectedObjects = []; - } - } -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); - composer.setSize(width, height); - - effectFXAA.uniforms['resolution'].value.set(1 / window.innerWidth, 1 / window.innerHeight); -} - -function animate() { - stats.begin(); - - const timer = performance.now(); - - if (params.rotate) { - group.rotation.y = timer * 0.0001; - } - - controls.update(); - - composer.render(); - - stats.end(); -} diff --git a/examples-testing/examples/webgl_postprocessing_pixel.ts b/examples-testing/examples/webgl_postprocessing_pixel.ts deleted file mode 100644 index 15b54d072..000000000 --- a/examples-testing/examples/webgl_postprocessing_pixel.ts +++ /dev/null @@ -1,228 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPixelatedPass } from 'three/addons/postprocessing/RenderPixelatedPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, composer, crystalMesh, clock; -let gui, params; - -init(); - -function init() { - const aspectRatio = window.innerWidth / window.innerHeight; - - camera = new THREE.OrthographicCamera(-aspectRatio, aspectRatio, 1, -1, 0.1, 10); - camera.position.y = 2 * Math.tan(Math.PI / 6); - camera.position.z = 2; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x151729); - - clock = new THREE.Clock(); - - renderer = new THREE.WebGLRenderer(); - renderer.shadowMap.enabled = true; - //renderer.setPixelRatio( window.devicePixelRatio ); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - composer = new EffectComposer(renderer); - const renderPixelatedPass = new RenderPixelatedPass(6, scene, camera); - composer.addPass(renderPixelatedPass); - - const outputPass = new OutputPass(); - composer.addPass(outputPass); - - window.addEventListener('resize', onWindowResize); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.maxZoom = 2; - - // gui - - gui = new GUI(); - params = { pixelSize: 6, normalEdgeStrength: 0.3, depthEdgeStrength: 0.4, pixelAlignedPanning: true }; - gui.add(params, 'pixelSize') - .min(1) - .max(16) - .step(1) - .onChange(() => { - renderPixelatedPass.setPixelSize(params.pixelSize); - }); - gui.add(renderPixelatedPass, 'normalEdgeStrength').min(0).max(2).step(0.05); - gui.add(renderPixelatedPass, 'depthEdgeStrength').min(0).max(1).step(0.05); - gui.add(params, 'pixelAlignedPanning'); - - // textures - - const loader = new THREE.TextureLoader(); - const texChecker = pixelTexture(loader.load('textures/checker.png')); - const texChecker2 = pixelTexture(loader.load('textures/checker.png')); - texChecker.repeat.set(3, 3); - texChecker2.repeat.set(1.5, 1.5); - - // meshes - - const boxMaterial = new THREE.MeshPhongMaterial({ map: texChecker2 }); - - function addBox(boxSideLength, x, z, rotation) { - const mesh = new THREE.Mesh(new THREE.BoxGeometry(boxSideLength, boxSideLength, boxSideLength), boxMaterial); - mesh.castShadow = true; - mesh.receiveShadow = true; - mesh.rotation.y = rotation; - mesh.position.y = boxSideLength / 2; - mesh.position.set(x, boxSideLength / 2 + 0.0001, z); - scene.add(mesh); - return mesh; - } - - addBox(0.4, 0, 0, Math.PI / 4); - addBox(0.5, -0.5, -0.5, Math.PI / 4); - - const planeSideLength = 2; - const planeMesh = new THREE.Mesh( - new THREE.PlaneGeometry(planeSideLength, planeSideLength), - new THREE.MeshPhongMaterial({ map: texChecker }), - ); - planeMesh.receiveShadow = true; - planeMesh.rotation.x = -Math.PI / 2; - scene.add(planeMesh); - - const radius = 0.2; - const geometry = new THREE.IcosahedronGeometry(radius); - crystalMesh = new THREE.Mesh( - geometry, - new THREE.MeshPhongMaterial({ - color: 0x68b7e9, - emissive: 0x4f7e8b, - shininess: 10, - specular: 0xffffff, - }), - ); - crystalMesh.receiveShadow = true; - crystalMesh.castShadow = true; - scene.add(crystalMesh); - - // lights - - scene.add(new THREE.AmbientLight(0x757f8e, 3)); - - const directionalLight = new THREE.DirectionalLight(0xfffecd, 1.5); - directionalLight.position.set(100, 100, 100); - directionalLight.castShadow = true; - directionalLight.shadow.mapSize.set(2048, 2048); - scene.add(directionalLight); - - const spotLight = new THREE.SpotLight(0xffc100, 10, 10, Math.PI / 16, 0.02, 2); - spotLight.position.set(2, 2, 0); - const target = spotLight.target; - scene.add(target); - target.position.set(0, 0, 0); - spotLight.castShadow = true; - scene.add(spotLight); -} - -function onWindowResize() { - const aspectRatio = window.innerWidth / window.innerHeight; - camera.left = -aspectRatio; - camera.right = aspectRatio; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - composer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const t = clock.getElapsedTime(); - - crystalMesh.material.emissiveIntensity = Math.sin(t * 3) * 0.5 + 0.5; - crystalMesh.position.y = 0.7 + Math.sin(t * 2) * 0.05; - crystalMesh.rotation.y = stopGoEased(t, 2, 4) * 2 * Math.PI; - - const rendererSize = renderer.getSize(new THREE.Vector2()); - const aspectRatio = rendererSize.x / rendererSize.y; - if (params['pixelAlignedPanning']) { - pixelAlignFrustum( - camera, - aspectRatio, - Math.floor(rendererSize.x / params['pixelSize']), - Math.floor(rendererSize.y / params['pixelSize']), - ); - } else if (camera.left != -aspectRatio || camera.top != 1.0) { - // Reset the Camera Frustum if it has been modified - camera.left = -aspectRatio; - camera.right = aspectRatio; - camera.top = 1.0; - camera.bottom = -1.0; - camera.updateProjectionMatrix(); - } - - composer.render(); -} - -// Helper functions - -function pixelTexture(texture) { - texture.minFilter = THREE.NearestFilter; - texture.magFilter = THREE.NearestFilter; - texture.generateMipmaps = false; - texture.wrapS = THREE.RepeatWrapping; - texture.wrapT = THREE.RepeatWrapping; - texture.colorSpace = THREE.SRGBColorSpace; - return texture; -} - -function easeInOutCubic(x) { - return x ** 2 * 3 - x ** 3 * 2; -} - -function linearStep(x, edge0, edge1) { - const w = edge1 - edge0; - const m = 1 / w; - const y0 = -m * edge0; - return THREE.MathUtils.clamp(y0 + m * x, 0, 1); -} - -function stopGoEased(x, downtime, period) { - const cycle = (x / period) | 0; - const tween = x - cycle * period; - const linStep = easeInOutCubic(linearStep(tween, downtime, period)); - return cycle + linStep; -} - -function pixelAlignFrustum(camera, aspectRatio, pixelsPerScreenWidth, pixelsPerScreenHeight) { - // 0. Get Pixel Grid Units - const worldScreenWidth = (camera.right - camera.left) / camera.zoom; - const worldScreenHeight = (camera.top - camera.bottom) / camera.zoom; - const pixelWidth = worldScreenWidth / pixelsPerScreenWidth; - const pixelHeight = worldScreenHeight / pixelsPerScreenHeight; - - // 1. Project the current camera position along its local rotation bases - const camPos = new THREE.Vector3(); - camera.getWorldPosition(camPos); - const camRot = new THREE.Quaternion(); - camera.getWorldQuaternion(camRot); - const camRight = new THREE.Vector3(1.0, 0.0, 0.0).applyQuaternion(camRot); - const camUp = new THREE.Vector3(0.0, 1.0, 0.0).applyQuaternion(camRot); - const camPosRight = camPos.dot(camRight); - const camPosUp = camPos.dot(camUp); - - // 2. Find how far along its position is along these bases in pixel units - const camPosRightPx = camPosRight / pixelWidth; - const camPosUpPx = camPosUp / pixelHeight; - - // 3. Find the fractional pixel units and convert to world units - const fractX = camPosRightPx - Math.round(camPosRightPx); - const fractY = camPosUpPx - Math.round(camPosUpPx); - - // 4. Add fractional world units to the left/right top/bottom to align with the pixel grid - camera.left = -aspectRatio - fractX * pixelWidth; - camera.right = aspectRatio - fractX * pixelWidth; - camera.top = 1.0 - fractY * pixelHeight; - camera.bottom = -1.0 - fractY * pixelHeight; - camera.updateProjectionMatrix(); -} diff --git a/examples-testing/examples/webgl_postprocessing_procedural.ts b/examples-testing/examples/webgl_postprocessing_procedural.ts deleted file mode 100644 index 869824270..000000000 --- a/examples-testing/examples/webgl_postprocessing_procedural.ts +++ /dev/null @@ -1,77 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let postCamera, postScene, renderer; -let postMaterial, noiseRandom1DMaterial, noiseRandom2DMaterial, noiseRandom3DMaterial, postQuad; -let stats; - -const params = { procedure: 'noiseRandom3D' }; - -init(); - -function init() { - const container = document.getElementById('container'); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - // Setup post processing stage - postCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1); - noiseRandom1DMaterial = new THREE.ShaderMaterial({ - vertexShader: document.querySelector('#procedural-vert').textContent.trim(), - fragmentShader: document.querySelector('#noiseRandom1D-frag').textContent.trim(), - }); - noiseRandom2DMaterial = new THREE.ShaderMaterial({ - vertexShader: document.querySelector('#procedural-vert').textContent.trim(), - fragmentShader: document.querySelector('#noiseRandom2D-frag').textContent.trim(), - }); - noiseRandom3DMaterial = new THREE.ShaderMaterial({ - vertexShader: document.querySelector('#procedural-vert').textContent.trim(), - fragmentShader: document.querySelector('#noiseRandom3D-frag').textContent.trim(), - }); - postMaterial = noiseRandom3DMaterial; - const postPlane = new THREE.PlaneGeometry(2, 2); - postQuad = new THREE.Mesh(postPlane, postMaterial); - postScene = new THREE.Scene(); - postScene.add(postQuad); - - window.addEventListener('resize', onWindowResize); - - // - - const gui = new GUI(); - gui.add(params, 'procedure', ['noiseRandom1D', 'noiseRandom2D', 'noiseRandom3D']); -} - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - switch (params.procedure) { - case 'noiseRandom1D': - postMaterial = noiseRandom1DMaterial; - break; - case 'noiseRandom2D': - postMaterial = noiseRandom2DMaterial; - break; - case 'noiseRandom3D': - postMaterial = noiseRandom3DMaterial; - break; - } - - postQuad.material = postMaterial; - - // render post FX - renderer.render(postScene, postCamera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_postprocessing_rgb_halftone.ts b/examples-testing/examples/webgl_postprocessing_rgb_halftone.ts deleted file mode 100644 index fa46d4c8d..000000000 --- a/examples-testing/examples/webgl_postprocessing_rgb_halftone.ts +++ /dev/null @@ -1,167 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { HalftonePass } from 'three/addons/postprocessing/HalftonePass.js'; - -let renderer, clock, camera, stats; - -const rotationSpeed = Math.PI / 64; - -let composer, group; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - - clock = new THREE.Clock(); - - camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 12; - - stats = new Stats(); - - document.body.appendChild(renderer.domElement); - document.body.appendChild(stats.dom); - - // camera controls - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 0, 0); - controls.update(); - - // scene - - const scene = new THREE.Scene(); - scene.background = new THREE.Color(0x444444); - - group = new THREE.Group(); - const floor = new THREE.Mesh(new THREE.BoxGeometry(100, 1, 100), new THREE.MeshPhongMaterial({})); - floor.position.y = -10; - const light = new THREE.PointLight(0xffffff, 250); - light.position.y = 2; - group.add(floor, light); - scene.add(group); - - const mat = new THREE.ShaderMaterial({ - uniforms: {}, - - vertexShader: [ - 'varying vec2 vUV;', - 'varying vec3 vNormal;', - - 'void main() {', - - 'vUV = uv;', - 'vNormal = vec3( normal );', - 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );', - - '}', - ].join('\n'), - - fragmentShader: [ - 'varying vec2 vUV;', - 'varying vec3 vNormal;', - - 'void main() {', - - 'vec4 c = vec4( abs( vNormal ) + vec3( vUV, 0.0 ), 0.0 );', - 'gl_FragColor = c;', - - '}', - ].join('\n'), - }); - - for (let i = 0; i < 50; ++i) { - // fill scene with coloured cubes - const mesh = new THREE.Mesh(new THREE.BoxGeometry(2, 2, 2), mat); - mesh.position.set(Math.random() * 16 - 8, Math.random() * 16 - 8, Math.random() * 16 - 8); - mesh.rotation.set(Math.random() * Math.PI * 2, Math.random() * Math.PI * 2, Math.random() * Math.PI * 2); - group.add(mesh); - } - - // post-processing - - composer = new EffectComposer(renderer); - const renderPass = new RenderPass(scene, camera); - const params = { - shape: 1, - radius: 4, - rotateR: Math.PI / 12, - rotateB: (Math.PI / 12) * 2, - rotateG: (Math.PI / 12) * 3, - scatter: 0, - blending: 1, - blendingMode: 1, - greyscale: false, - disable: false, - }; - const halftonePass = new HalftonePass(window.innerWidth, window.innerHeight, params); - composer.addPass(renderPass); - composer.addPass(halftonePass); - - window.onresize = function () { - // resize composer - renderer.setSize(window.innerWidth, window.innerHeight); - composer.setSize(window.innerWidth, window.innerHeight); - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - }; - - // GUI - - const controller = { - radius: halftonePass.uniforms['radius'].value, - rotateR: halftonePass.uniforms['rotateR'].value / (Math.PI / 180), - rotateG: halftonePass.uniforms['rotateG'].value / (Math.PI / 180), - rotateB: halftonePass.uniforms['rotateB'].value / (Math.PI / 180), - scatter: halftonePass.uniforms['scatter'].value, - shape: halftonePass.uniforms['shape'].value, - greyscale: halftonePass.uniforms['greyscale'].value, - blending: halftonePass.uniforms['blending'].value, - blendingMode: halftonePass.uniforms['blendingMode'].value, - disable: halftonePass.uniforms['disable'].value, - }; - - function onGUIChange() { - // update uniforms - halftonePass.uniforms['radius'].value = controller.radius; - halftonePass.uniforms['rotateR'].value = controller.rotateR * (Math.PI / 180); - halftonePass.uniforms['rotateG'].value = controller.rotateG * (Math.PI / 180); - halftonePass.uniforms['rotateB'].value = controller.rotateB * (Math.PI / 180); - halftonePass.uniforms['scatter'].value = controller.scatter; - halftonePass.uniforms['shape'].value = controller.shape; - halftonePass.uniforms['greyscale'].value = controller.greyscale; - halftonePass.uniforms['blending'].value = controller.blending; - halftonePass.uniforms['blendingMode'].value = controller.blendingMode; - halftonePass.uniforms['disable'].value = controller.disable; - } - - const gui = new GUI(); - gui.add(controller, 'shape', { Dot: 1, Ellipse: 2, Line: 3, Square: 4 }).onChange(onGUIChange); - gui.add(controller, 'radius', 1, 25).onChange(onGUIChange); - gui.add(controller, 'rotateR', 0, 90).onChange(onGUIChange); - gui.add(controller, 'rotateG', 0, 90).onChange(onGUIChange); - gui.add(controller, 'rotateB', 0, 90).onChange(onGUIChange); - gui.add(controller, 'scatter', 0, 1, 0.01).onChange(onGUIChange); - gui.add(controller, 'greyscale').onChange(onGUIChange); - gui.add(controller, 'blending', 0, 1, 0.01).onChange(onGUIChange); - gui.add(controller, 'blendingMode', { Linear: 1, Multiply: 2, Add: 3, Lighter: 4, Darker: 5 }).onChange( - onGUIChange, - ); - gui.add(controller, 'disable').onChange(onGUIChange); -} - -function animate() { - const delta = clock.getDelta(); - stats.update(); - group.rotation.y += delta * rotationSpeed; - composer.render(delta); -} diff --git a/examples-testing/examples/webgl_postprocessing_sao.ts b/examples-testing/examples/webgl_postprocessing_sao.ts deleted file mode 100644 index bf40d026b..000000000 --- a/examples-testing/examples/webgl_postprocessing_sao.ts +++ /dev/null @@ -1,137 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { SAOPass } from 'three/addons/postprocessing/SAOPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -let container, stats; -let camera, scene, renderer; -let composer, renderPass, saoPass; -let group; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - const width = window.innerWidth; - const height = window.innerHeight; - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(width, height); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - camera = new THREE.PerspectiveCamera(65, width / height, 3, 10); - camera.position.z = 7; - - scene = new THREE.Scene(); - - group = new THREE.Object3D(); - scene.add(group); - - const light = new THREE.PointLight(0xefffef, 500); - light.position.z = 10; - light.position.y = -10; - light.position.x = -10; - scene.add(light); - - const light2 = new THREE.PointLight(0xffefef, 500); - light2.position.z = 10; - light2.position.x = -10; - light2.position.y = 10; - scene.add(light2); - - const light3 = new THREE.PointLight(0xefefff, 500); - light3.position.z = 10; - light3.position.x = 10; - light3.position.y = -10; - scene.add(light3); - - const light4 = new THREE.AmbientLight(0xffffff, 0.2); - scene.add(light4); - - const geometry = new THREE.SphereGeometry(3, 48, 24); - - for (let i = 0; i < 120; i++) { - const material = new THREE.MeshStandardMaterial(); - material.roughness = 0.5 * Math.random() + 0.25; - material.metalness = 0; - material.color.setHSL(Math.random(), 1.0, 0.3); - - const mesh = new THREE.Mesh(geometry, material); - mesh.position.x = Math.random() * 4 - 2; - mesh.position.y = Math.random() * 4 - 2; - mesh.position.z = Math.random() * 4 - 2; - mesh.rotation.x = Math.random(); - mesh.rotation.y = Math.random(); - mesh.rotation.z = Math.random(); - - mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 0.2 + 0.05; - group.add(mesh); - } - - stats = new Stats(); - container.appendChild(stats.dom); - - composer = new EffectComposer(renderer); - renderPass = new RenderPass(scene, camera); - composer.addPass(renderPass); - saoPass = new SAOPass(scene, camera); - composer.addPass(saoPass); - const outputPass = new OutputPass(); - composer.addPass(outputPass); - - // Init gui - const gui = new GUI(); - gui.add(saoPass.params, 'output', { - Default: SAOPass.OUTPUT.Default, - 'SAO Only': SAOPass.OUTPUT.SAO, - Normal: SAOPass.OUTPUT.Normal, - }).onChange(function (value) { - saoPass.params.output = value; - }); - gui.add(saoPass.params, 'saoBias', -1, 1); - gui.add(saoPass.params, 'saoIntensity', 0, 1); - gui.add(saoPass.params, 'saoScale', 0, 10); - gui.add(saoPass.params, 'saoKernelRadius', 1, 100); - gui.add(saoPass.params, 'saoMinResolution', 0, 1); - gui.add(saoPass.params, 'saoBlur'); - gui.add(saoPass.params, 'saoBlurRadius', 0, 200); - gui.add(saoPass.params, 'saoBlurStdDev', 0.5, 150); - gui.add(saoPass.params, 'saoBlurDepthCutoff', 0.0, 0.1); - gui.add(saoPass, 'enabled'); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const width = window.innerWidth || 1; - const height = window.innerHeight || 1; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - renderer.setSize(width, height); - - composer.setSize(width, height); -} - -function animate() { - stats.begin(); - render(); - stats.end(); -} - -function render() { - const timer = performance.now(); - group.rotation.x = timer * 0.0002; - group.rotation.y = timer * 0.0001; - - composer.render(); -} diff --git a/examples-testing/examples/webgl_postprocessing_smaa.ts b/examples-testing/examples/webgl_postprocessing_smaa.ts deleted file mode 100644 index 6f71f6478..000000000 --- a/examples-testing/examples/webgl_postprocessing_smaa.ts +++ /dev/null @@ -1,109 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { SMAAPass } from 'three/addons/postprocessing/SMAAPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -let camera, scene, renderer, composer, stats, smaaPass; - -const params = { - enabled: true, - autoRotate: true, -}; - -init(); - -function init() { - const container = document.getElementById('container'); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 300; - - scene = new THREE.Scene(); - - const geometry = new THREE.BoxGeometry(120, 120, 120); - const material1 = new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: true }); - - const mesh1 = new THREE.Mesh(geometry, material1); - mesh1.position.x = -100; - scene.add(mesh1); - - const texture = new THREE.TextureLoader().load('textures/brick_diffuse.jpg'); - texture.anisotropy = renderer.capabilities.getMaxAnisotropy(); - texture.colorSpace = THREE.SRGBColorSpace; - - const material2 = new THREE.MeshBasicMaterial({ map: texture }); - - const mesh2 = new THREE.Mesh(geometry, material2); - mesh2.position.x = 100; - scene.add(mesh2); - - // postprocessing - - composer = new EffectComposer(renderer); - composer.addPass(new RenderPass(scene, camera)); - - smaaPass = new SMAAPass( - window.innerWidth * renderer.getPixelRatio(), - window.innerHeight * renderer.getPixelRatio(), - ); - composer.addPass(smaaPass); - - const outputPass = new OutputPass(); - composer.addPass(outputPass); - - window.addEventListener('resize', onWindowResize); - - const gui = new GUI(); - - const smaaFolder = gui.addFolder('SMAA'); - smaaFolder.add(params, 'enabled'); - - const sceneFolder = gui.addFolder('Scene'); - sceneFolder.add(params, 'autoRotate'); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); - composer.setSize(width, height); -} - -function animate() { - stats.begin(); - - if (params.autoRotate === true) { - for (let i = 0; i < scene.children.length; i++) { - const child = scene.children[i]; - - child.rotation.x += 0.005; - child.rotation.y += 0.01; - } - } - - smaaPass.enabled = params.enabled; - - composer.render(); - - stats.end(); -} diff --git a/examples-testing/examples/webgl_postprocessing_sobel.ts b/examples-testing/examples/webgl_postprocessing_sobel.ts deleted file mode 100644 index 55d88dc02..000000000 --- a/examples-testing/examples/webgl_postprocessing_sobel.ts +++ /dev/null @@ -1,111 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js'; - -import { LuminosityShader } from 'three/addons/shaders/LuminosityShader.js'; -import { SobelOperatorShader } from 'three/addons/shaders/SobelOperatorShader.js'; - -let camera, scene, renderer, composer; - -let effectSobel; - -const params = { - enable: true, -}; - -init(); - -function init() { - // - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(0, 1, 3); - camera.lookAt(scene.position); - - // - - const geometry = new THREE.TorusKnotGeometry(1, 0.3, 256, 32); - const material = new THREE.MeshPhongMaterial({ color: 0xffff00 }); - - const mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - const ambientLight = new THREE.AmbientLight(0xe7e7e7); - scene.add(ambientLight); - - const pointLight = new THREE.PointLight(0xffffff, 20); - camera.add(pointLight); - scene.add(camera); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // postprocessing - - composer = new EffectComposer(renderer); - const renderPass = new RenderPass(scene, camera); - composer.addPass(renderPass); - - // color to grayscale conversion - - const effectGrayScale = new ShaderPass(LuminosityShader); - composer.addPass(effectGrayScale); - - // you might want to use a gaussian blur filter before - // the next pass to improve the result of the Sobel operator - - // Sobel operator - - effectSobel = new ShaderPass(SobelOperatorShader); - effectSobel.uniforms['resolution'].value.x = window.innerWidth * window.devicePixelRatio; - effectSobel.uniforms['resolution'].value.y = window.innerHeight * window.devicePixelRatio; - composer.addPass(effectSobel); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.enableZoom = false; - - // - - const gui = new GUI(); - - gui.add(params, 'enable'); - gui.open(); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - composer.setSize(window.innerWidth, window.innerHeight); - - effectSobel.uniforms['resolution'].value.x = window.innerWidth * window.devicePixelRatio; - effectSobel.uniforms['resolution'].value.y = window.innerHeight * window.devicePixelRatio; -} - -function animate() { - if (params.enable === true) { - composer.render(); - } else { - renderer.render(scene, camera); - } -} diff --git a/examples-testing/examples/webgl_postprocessing_ssaa.ts b/examples-testing/examples/webgl_postprocessing_ssaa.ts deleted file mode 100644 index 429e02dee..000000000 --- a/examples-testing/examples/webgl_postprocessing_ssaa.ts +++ /dev/null @@ -1,206 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { SSAARenderPass } from 'three/addons/postprocessing/SSAARenderPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -let scene, renderer, composer; -let cameraP, ssaaRenderPassP; -let cameraO, ssaaRenderPassO; -let gui, stats; - -const params = { - sampleLevel: 4, - unbiased: true, - camera: 'perspective', - clearColor: 'black', - clearAlpha: 1.0, - viewOffsetX: 0, - autoRotate: true, -}; - -init(); - -clearGui(); - -function clearGui() { - if (gui) gui.destroy(); - - gui = new GUI(); - - gui.add(params, 'unbiased'); - gui.add(params, 'sampleLevel', { - 'Level 0: 1 Sample': 0, - 'Level 1: 2 Samples': 1, - 'Level 2: 4 Samples': 2, - 'Level 3: 8 Samples': 3, - 'Level 4: 16 Samples': 4, - 'Level 5: 32 Samples': 5, - }); - gui.add(params, 'camera', ['perspective', 'orthographic']); - gui.add(params, 'clearColor', ['black', 'white', 'blue', 'green', 'red']); - gui.add(params, 'clearAlpha', 0, 1); - gui.add(params, 'viewOffsetX', -100, 100); - gui.add(params, 'autoRotate'); - - gui.open(); -} - -function init() { - const container = document.getElementById('container'); - - const width = window.innerWidth || 1; - const height = window.innerHeight || 1; - const aspect = width / height; - const devicePixelRatio = window.devicePixelRatio || 1; - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(devicePixelRatio); - renderer.setSize(width, height); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - cameraP = new THREE.PerspectiveCamera(65, aspect, 3, 10); - cameraP.position.z = 7; - cameraP.setViewOffset(width, height, params.viewOffsetX, 0, width, height); - - cameraO = new THREE.OrthographicCamera(width / -2, width / 2, height / 2, height / -2, 3, 10); - cameraO.position.z = 7; - - const fov = THREE.MathUtils.degToRad(cameraP.fov); - const hyperfocus = (cameraP.near + cameraP.far) / 2; - const _height = 2 * Math.tan(fov / 2) * hyperfocus; - cameraO.zoom = height / _height; - - scene = new THREE.Scene(); - - const group = new THREE.Group(); - scene.add(group); - - const light = new THREE.PointLight(0xefffef, 500); - light.position.z = 10; - light.position.y = -10; - light.position.x = -10; - scene.add(light); - - const light2 = new THREE.PointLight(0xffefef, 500); - light2.position.z = 10; - light2.position.x = -10; - light2.position.y = 10; - scene.add(light2); - - const light3 = new THREE.PointLight(0xefefff, 500); - light3.position.z = 10; - light3.position.x = 10; - light3.position.y = -10; - scene.add(light3); - - const light4 = new THREE.AmbientLight(0xffffff, 0.2); - scene.add(light4); - - const geometry = new THREE.SphereGeometry(3, 48, 24); - - for (let i = 0; i < 120; i++) { - const material = new THREE.MeshStandardMaterial(); - material.roughness = 0.5 * Math.random() + 0.25; - material.metalness = 0; - material.color.setHSL(Math.random(), 1.0, 0.3); - - const mesh = new THREE.Mesh(geometry, material); - mesh.position.x = Math.random() * 4 - 2; - mesh.position.y = Math.random() * 4 - 2; - mesh.position.z = Math.random() * 4 - 2; - mesh.rotation.x = Math.random(); - mesh.rotation.y = Math.random(); - mesh.rotation.z = Math.random(); - - mesh.scale.setScalar(Math.random() * 0.2 + 0.05); - group.add(mesh); - } - - // postprocessing - - composer = new EffectComposer(renderer); - composer.setPixelRatio(1); // ensure pixel ratio is always 1 for performance reasons - ssaaRenderPassP = new SSAARenderPass(scene, cameraP); - composer.addPass(ssaaRenderPassP); - ssaaRenderPassO = new SSAARenderPass(scene, cameraO); - composer.addPass(ssaaRenderPassO); - const outputPass = new OutputPass(); - composer.addPass(outputPass); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - const aspect = width / height; - - cameraP.aspect = aspect; - cameraP.setViewOffset(width, height, params.viewOffsetX, 0, width, height); - cameraO.updateProjectionMatrix(); - - cameraO.left = -height * aspect; - cameraO.right = height * aspect; - cameraO.top = height; - cameraO.bottom = -height; - cameraO.updateProjectionMatrix(); - - renderer.setSize(width, height); - composer.setSize(width, height); -} - -function animate() { - stats.begin(); - - if (params.autoRotate) { - for (let i = 0; i < scene.children.length; i++) { - const child = scene.children[i]; - - child.rotation.x += 0.005; - child.rotation.y += 0.01; - } - } - - let newColor = ssaaRenderPassP.clearColor; - - switch (params.clearColor) { - case 'blue': - newColor = 0x0000ff; - break; - case 'red': - newColor = 0xff0000; - break; - case 'green': - newColor = 0x00ff00; - break; - case 'white': - newColor = 0xffffff; - break; - case 'black': - newColor = 0x000000; - break; - } - - ssaaRenderPassP.clearColor = ssaaRenderPassO.clearColor = newColor; - ssaaRenderPassP.clearAlpha = ssaaRenderPassO.clearAlpha = params.clearAlpha; - - ssaaRenderPassP.sampleLevel = ssaaRenderPassO.sampleLevel = params.sampleLevel; - ssaaRenderPassP.unbiased = ssaaRenderPassO.unbiased = params.unbiased; - - ssaaRenderPassP.enabled = params.camera === 'perspective'; - ssaaRenderPassO.enabled = params.camera === 'orthographic'; - - cameraP.view.offsetX = params.viewOffsetX; - - composer.render(); - - stats.end(); -} diff --git a/examples-testing/examples/webgl_postprocessing_ssao.ts b/examples-testing/examples/webgl_postprocessing_ssao.ts deleted file mode 100644 index e55ab0446..000000000 --- a/examples-testing/examples/webgl_postprocessing_ssao.ts +++ /dev/null @@ -1,118 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { SSAOPass } from 'three/addons/postprocessing/SSAOPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -let container, stats; -let camera, scene, renderer; -let composer; -let group; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - renderer = new THREE.WebGLRenderer(); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - camera = new THREE.PerspectiveCamera(65, window.innerWidth / window.innerHeight, 100, 700); - camera.position.z = 500; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xaaaaaa); - - scene.add(new THREE.DirectionalLight(0xffffff, 4)); - scene.add(new THREE.AmbientLight(0xffffff)); - - group = new THREE.Group(); - scene.add(group); - - const geometry = new THREE.BoxGeometry(10, 10, 10); - - for (let i = 0; i < 100; i++) { - const material = new THREE.MeshLambertMaterial({ - color: Math.random() * 0xffffff, - }); - - const mesh = new THREE.Mesh(geometry, material); - mesh.position.x = Math.random() * 400 - 200; - mesh.position.y = Math.random() * 400 - 200; - mesh.position.z = Math.random() * 400 - 200; - mesh.rotation.x = Math.random(); - mesh.rotation.y = Math.random(); - mesh.rotation.z = Math.random(); - - mesh.scale.setScalar(Math.random() * 10 + 2); - group.add(mesh); - } - - stats = new Stats(); - container.appendChild(stats.dom); - - const width = window.innerWidth; - const height = window.innerHeight; - - composer = new EffectComposer(renderer); - - const renderPass = new RenderPass(scene, camera); - composer.addPass(renderPass); - - const ssaoPass = new SSAOPass(scene, camera, width, height); - composer.addPass(ssaoPass); - - const outputPass = new OutputPass(); - composer.addPass(outputPass); - - // Init gui - const gui = new GUI(); - - gui.add(ssaoPass, 'output', { - Default: SSAOPass.OUTPUT.Default, - 'SSAO Only': SSAOPass.OUTPUT.SSAO, - 'SSAO Only + Blur': SSAOPass.OUTPUT.Blur, - Depth: SSAOPass.OUTPUT.Depth, - Normal: SSAOPass.OUTPUT.Normal, - }).onChange(function (value) { - ssaoPass.output = value; - }); - gui.add(ssaoPass, 'kernelRadius').min(0).max(32); - gui.add(ssaoPass, 'minDistance').min(0.001).max(0.02); - gui.add(ssaoPass, 'maxDistance').min(0.01).max(0.3); - gui.add(ssaoPass, 'enabled'); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); - composer.setSize(width, height); -} - -function animate() { - stats.begin(); - render(); - stats.end(); -} - -function render() { - const timer = performance.now(); - group.rotation.x = timer * 0.0002; - group.rotation.y = timer * 0.0001; - - composer.render(); -} diff --git a/examples-testing/examples/webgl_postprocessing_ssr.ts b/examples-testing/examples/webgl_postprocessing_ssr.ts deleted file mode 100644 index 307cfd1de..000000000 --- a/examples-testing/examples/webgl_postprocessing_ssr.ts +++ /dev/null @@ -1,261 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { SSRPass } from 'three/addons/postprocessing/SSRPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; -import { ReflectorForSSRPass } from 'three/addons/objects/ReflectorForSSRPass.js'; - -import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; - -const params = { - enableSSR: true, - autoRotate: true, - otherMeshes: true, - groundReflector: true, -}; -let composer; -let ssrPass; -let gui; -let stats; -let controls; -let camera, scene, renderer; -const otherMeshes = []; -let groundReflector; -const selects = []; - -const container = document.querySelector('#container'); - -// Configure and create Draco decoder. -const dracoLoader = new DRACOLoader(); -dracoLoader.setDecoderPath('jsm/libs/draco/'); -dracoLoader.setDecoderConfig({ type: 'js' }); - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 15); - camera.position.set(0.13271600513224902, 0.3489546826045913, 0.43921296427927076); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x443333); - scene.fog = new THREE.Fog(0x443333, 1, 4); - - // Ground - const plane = new THREE.Mesh(new THREE.PlaneGeometry(8, 8), new THREE.MeshPhongMaterial({ color: 0xcbcbcb })); - plane.rotation.x = -Math.PI / 2; - plane.position.y = -0.0001; - // plane.receiveShadow = true; - scene.add(plane); - - // Lights - const hemiLight = new THREE.HemisphereLight(0x8d7c7c, 0x494966, 3); - scene.add(hemiLight); - - const spotLight = new THREE.SpotLight(); - spotLight.intensity = 8; - spotLight.angle = Math.PI / 16; - spotLight.penumbra = 0.5; - // spotLight.castShadow = true; - spotLight.position.set(-1, 1, 1); - scene.add(spotLight); - - dracoLoader.load('models/draco/bunny.drc', function (geometry) { - geometry.computeVertexNormals(); - - const material = new THREE.MeshStandardMaterial({ color: 0xa5a5a5 }); - const mesh = new THREE.Mesh(geometry, material); - mesh.position.y = -0.0365; - scene.add(mesh); - selects.push(mesh); - - // Release decoder resources. - dracoLoader.dispose(); - }); - - let geometry, material, mesh; - - geometry = new THREE.BoxGeometry(0.05, 0.05, 0.05); - material = new THREE.MeshStandardMaterial({ color: 'green' }); - mesh = new THREE.Mesh(geometry, material); - mesh.position.set(-0.12, 0.025, 0.015); - scene.add(mesh); - otherMeshes.push(mesh); - selects.push(mesh); - - geometry = new THREE.IcosahedronGeometry(0.025, 4); - material = new THREE.MeshStandardMaterial({ color: 'cyan' }); - mesh = new THREE.Mesh(geometry, material); - mesh.position.set(-0.05, 0.025, 0.08); - scene.add(mesh); - otherMeshes.push(mesh); - selects.push(mesh); - - geometry = new THREE.ConeGeometry(0.025, 0.05, 64); - material = new THREE.MeshStandardMaterial({ color: 'yellow' }); - mesh = new THREE.Mesh(geometry, material); - mesh.position.set(-0.05, 0.025, -0.055); - scene.add(mesh); - otherMeshes.push(mesh); - selects.push(mesh); - - geometry = new THREE.PlaneGeometry(1, 1); - groundReflector = new ReflectorForSSRPass(geometry, { - clipBias: 0.0003, - textureWidth: window.innerWidth, - textureHeight: window.innerHeight, - color: 0x888888, - useDepthTexture: true, - }); - groundReflector.material.depthWrite = false; - groundReflector.rotation.x = -Math.PI / 2; - groundReflector.visible = false; - scene.add(groundReflector); - - // renderer - renderer = new THREE.WebGLRenderer({ antialias: false }); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - controls = new OrbitControls(camera, renderer.domElement); - controls.enableDamping = true; - controls.target.set(0, 0.0635, 0); - controls.update(); - controls.enabled = !params.autoRotate; - - // STATS - - stats = new Stats(); - container.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); - - // composer - - composer = new EffectComposer(renderer); - ssrPass = new SSRPass({ - renderer, - scene, - camera, - width: innerWidth, - height: innerHeight, - groundReflector: params.groundReflector ? groundReflector : null, - selects: params.groundReflector ? selects : null, - }); - - composer.addPass(ssrPass); - composer.addPass(new OutputPass()); - - // GUI - - gui = new GUI({ width: 260 }); - gui.add(params, 'enableSSR').name('Enable SSR'); - gui.add(params, 'groundReflector').onChange(() => { - if (params.groundReflector) { - (ssrPass.groundReflector = groundReflector), (ssrPass.selects = selects); - } else { - (ssrPass.groundReflector = null), (ssrPass.selects = null); - } - }); - ssrPass.thickness = 0.018; - gui.add(ssrPass, 'thickness').min(0).max(0.1).step(0.0001); - ssrPass.infiniteThick = false; - gui.add(ssrPass, 'infiniteThick'); - gui.add(params, 'autoRotate').onChange(() => { - controls.enabled = !params.autoRotate; - }); - - const folder = gui.addFolder('more settings'); - folder.add(ssrPass, 'fresnel').onChange(() => { - groundReflector.fresnel = ssrPass.fresnel; - }); - folder.add(ssrPass, 'distanceAttenuation').onChange(() => { - groundReflector.distanceAttenuation = ssrPass.distanceAttenuation; - }); - ssrPass.maxDistance = 0.1; - groundReflector.maxDistance = ssrPass.maxDistance; - folder - .add(ssrPass, 'maxDistance') - .min(0) - .max(0.5) - .step(0.001) - .onChange(() => { - groundReflector.maxDistance = ssrPass.maxDistance; - }); - folder.add(params, 'otherMeshes').onChange(() => { - if (params.otherMeshes) { - otherMeshes.forEach(mesh => (mesh.visible = true)); - } else { - otherMeshes.forEach(mesh => (mesh.visible = false)); - } - }); - folder.add(ssrPass, 'bouncing'); - folder - .add(ssrPass, 'output', { - Default: SSRPass.OUTPUT.Default, - 'SSR Only': SSRPass.OUTPUT.SSR, - Beauty: SSRPass.OUTPUT.Beauty, - Depth: SSRPass.OUTPUT.Depth, - Normal: SSRPass.OUTPUT.Normal, - Metalness: SSRPass.OUTPUT.Metalness, - }) - .onChange(function (value) { - ssrPass.output = value; - }); - ssrPass.opacity = 1; - groundReflector.opacity = ssrPass.opacity; - folder - .add(ssrPass, 'opacity') - .min(0) - .max(1) - .onChange(() => { - groundReflector.opacity = ssrPass.opacity; - }); - folder.add(ssrPass, 'blur'); - // folder.open() - // gui.close() -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - composer.setSize(window.innerWidth, window.innerHeight); - groundReflector.getRenderTarget().setSize(window.innerWidth, window.innerHeight); - groundReflector.resolution.set(window.innerWidth, window.innerHeight); -} - -function animate() { - stats.begin(); - render(); - stats.end(); -} - -function render() { - if (params.autoRotate) { - const timer = Date.now() * 0.0003; - - camera.position.x = Math.sin(timer) * 0.5; - camera.position.y = 0.2135; - camera.position.z = Math.cos(timer) * 0.5; - camera.lookAt(0, 0.0635, 0); - } else { - controls.update(); - } - - if (params.enableSSR) { - // TODO: groundReflector has full ground info, need use it to solve reflection gaps problem on objects when camera near ground. - // TODO: the normal and depth info where groundReflector reflected need to be changed. - composer.render(); - } else { - renderer.render(scene, camera); - } -} diff --git a/examples-testing/examples/webgl_postprocessing_taa.ts b/examples-testing/examples/webgl_postprocessing_taa.ts deleted file mode 100644 index 11a986741..000000000 --- a/examples-testing/examples/webgl_postprocessing_taa.ts +++ /dev/null @@ -1,139 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { TAARenderPass } from 'three/addons/postprocessing/TAARenderPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -let camera, scene, renderer, composer, taaRenderPass, renderPass; -let gui, stats; -let index = 0; - -const param = { TAAEnabled: '1', TAASampleLevel: 0 }; - -init(); - -clearGui(); - -function clearGui() { - if (gui) gui.destroy(); - - gui = new GUI(); - - gui.add(param, 'TAAEnabled', { - Disabled: '0', - Enabled: '1', - }).onFinishChange(function () { - if (taaRenderPass) { - taaRenderPass.enabled = param.TAAEnabled === '1'; - renderPass.enabled = param.TAAEnabled !== '1'; - } - }); - - gui.add(param, 'TAASampleLevel', { - 'Level 0: 1 Sample': 0, - 'Level 1: 2 Samples': 1, - 'Level 2: 4 Samples': 2, - 'Level 3: 8 Samples': 3, - 'Level 4: 16 Samples': 4, - 'Level 5: 32 Samples': 5, - }).onFinishChange(function () { - if (taaRenderPass) { - taaRenderPass.sampleLevel = param.TAASampleLevel; - } - }); - - gui.open(); -} - -function init() { - const container = document.getElementById('container'); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 300; - - scene = new THREE.Scene(); - - const geometry = new THREE.BoxGeometry(120, 120, 120); - const material1 = new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: true }); - - const mesh1 = new THREE.Mesh(geometry, material1); - mesh1.position.x = -100; - scene.add(mesh1); - - const texture = new THREE.TextureLoader().load('textures/brick_diffuse.jpg'); - texture.minFilter = THREE.NearestFilter; - texture.magFilter = THREE.NearestFilter; - texture.anisotropy = 1; - texture.generateMipmaps = false; - texture.colorSpace = THREE.SRGBColorSpace; - - const material2 = new THREE.MeshBasicMaterial({ map: texture }); - - const mesh2 = new THREE.Mesh(geometry, material2); - mesh2.position.x = 100; - scene.add(mesh2); - - // postprocessing - - composer = new EffectComposer(renderer); - - taaRenderPass = new TAARenderPass(scene, camera); - taaRenderPass.unbiased = false; - composer.addPass(taaRenderPass); - - renderPass = new RenderPass(scene, camera); - renderPass.enabled = false; - composer.addPass(renderPass); - - const outputPass = new OutputPass(); - composer.addPass(outputPass); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); - composer.setSize(width, height); -} - -function animate() { - index++; - - if (Math.round(index / 200) % 2 === 0) { - for (let i = 0; i < scene.children.length; i++) { - const child = scene.children[i]; - - child.rotation.x += 0.005; - child.rotation.y += 0.01; - } - - if (taaRenderPass) taaRenderPass.accumulate = false; - } else { - if (taaRenderPass) taaRenderPass.accumulate = true; - } - - composer.render(); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_postprocessing_transition.ts b/examples-testing/examples/webgl_postprocessing_transition.ts deleted file mode 100644 index d05466131..000000000 --- a/examples-testing/examples/webgl_postprocessing_transition.ts +++ /dev/null @@ -1,211 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import TWEEN from 'three/addons/libs/tween.module.js'; -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderTransitionPass } from 'three/addons/postprocessing/RenderTransitionPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -let stats; -let renderer, composer, renderTransitionPass; - -const textures = []; -const clock = new THREE.Clock(); - -const params = { - sceneAnimate: true, - transitionAnimate: true, - transition: 0, - useTexture: true, - texture: 5, - cycle: true, - threshold: 0.1, -}; - -const fxSceneA = new FXScene(new THREE.BoxGeometry(2, 2, 2), new THREE.Vector3(0, -0.4, 0), 0xffffff); -const fxSceneB = new FXScene(new THREE.IcosahedronGeometry(1, 1), new THREE.Vector3(0, 0.2, 0.1), 0x000000); - -init(); - -function init() { - initGUI(); - initTextures(); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - composer = new EffectComposer(renderer); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - renderTransitionPass = new RenderTransitionPass(fxSceneA.scene, fxSceneA.camera, fxSceneB.scene, fxSceneB.camera); - renderTransitionPass.setTexture(textures[0]); - composer.addPass(renderTransitionPass); - - const outputPass = new OutputPass(); - composer.addPass(outputPass); -} - -window.addEventListener('resize', onWindowResize); - -function onWindowResize() { - fxSceneA.resize(); - fxSceneB.resize(); - renderer.setSize(window.innerWidth, window.innerHeight); - composer.setSize(window.innerWidth, window.innerHeight); -} - -new TWEEN.Tween(params) - .to({ transition: 1 }, 1500) - .onUpdate(function () { - renderTransitionPass.setTransition(params.transition); - - // Change the current alpha texture after each transition - if (params.cycle) { - if (params.transition == 0 || params.transition == 1) { - params.texture = (params.texture + 1) % textures.length; - renderTransitionPass.setTexture(textures[params.texture]); - } - } - }) - .repeat(Infinity) - .delay(2000) - .yoyo(true) - .start(); - -function animate() { - // Transition animation - if (params.transitionAnimate) TWEEN.update(); - - const delta = clock.getDelta(); - fxSceneA.update(delta); - fxSceneB.update(delta); - - render(); - stats.update(); -} - -function initTextures() { - const loader = new THREE.TextureLoader(); - - for (let i = 0; i < 6; i++) { - textures[i] = loader.load('textures/transition/transition' + (i + 1) + '.png'); - } -} - -function initGUI() { - const gui = new GUI(); - - gui.add(params, 'sceneAnimate').name('Animate scene'); - gui.add(params, 'transitionAnimate').name('Animate transition'); - gui.add(params, 'transition', 0, 1, 0.01) - .onChange(function (value) { - renderTransitionPass.setTransition(value); - }) - .listen(); - - gui.add(params, 'useTexture').onChange(function (value) { - renderTransitionPass.useTexture(value); - }); - - gui.add(params, 'texture', { Perlin: 0, Squares: 1, Cells: 2, Distort: 3, Gradient: 4, Radial: 5 }) - .onChange(function (value) { - renderTransitionPass.setTexture(textures[value]); - }) - .listen(); - - gui.add(params, 'cycle'); - - gui.add(params, 'threshold', 0, 1, 0.01).onChange(function (value) { - renderTransitionPass.setTextureThreshold(value); - }); -} - -function render() { - // Prevent render both scenes when it's not necessary - if (params.transition === 0) { - renderer.render(fxSceneB.scene, fxSceneB.camera); - } else if (params.transition === 1) { - renderer.render(fxSceneA.scene, fxSceneA.camera); - } else { - // When 0 < transition < 1 render transition between two scenes - composer.render(); - } -} - -function FXScene(geometry, rotationSpeed, backgroundColor) { - const camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.z = 20; - - // Setup scene - const scene = new THREE.Scene(); - scene.background = new THREE.Color(backgroundColor); - scene.add(new THREE.AmbientLight(0xaaaaaa, 3)); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0, 1, 4); - scene.add(light); - - this.rotationSpeed = rotationSpeed; - - const color = geometry.type === 'BoxGeometry' ? 0x0000ff : 0xff0000; - const material = new THREE.MeshPhongMaterial({ color: color, flatShading: true }); - const mesh = generateInstancedMesh(geometry, material, 500); - scene.add(mesh); - - this.scene = scene; - this.camera = camera; - this.mesh = mesh; - - this.update = function (delta) { - if (params.sceneAnimate) { - mesh.rotation.x += this.rotationSpeed.x * delta; - mesh.rotation.y += this.rotationSpeed.y * delta; - mesh.rotation.z += this.rotationSpeed.z * delta; - } - }; - - this.resize = function () { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - }; -} - -function generateInstancedMesh(geometry, material, count) { - const mesh = new THREE.InstancedMesh(geometry, material, count); - - const dummy = new THREE.Object3D(); - const color = new THREE.Color(); - - for (let i = 0; i < count; i++) { - dummy.position.x = Math.random() * 100 - 50; - dummy.position.y = Math.random() * 60 - 30; - dummy.position.z = Math.random() * 80 - 40; - - dummy.rotation.x = Math.random() * 2 * Math.PI; - dummy.rotation.y = Math.random() * 2 * Math.PI; - dummy.rotation.z = Math.random() * 2 * Math.PI; - - dummy.scale.x = Math.random() * 2 + 1; - - if (geometry.type === 'BoxGeometry') { - dummy.scale.y = Math.random() * 2 + 1; - dummy.scale.z = Math.random() * 2 + 1; - } else { - dummy.scale.y = dummy.scale.x; - dummy.scale.z = dummy.scale.x; - } - - dummy.updateMatrix(); - - mesh.setMatrixAt(i, dummy.matrix); - mesh.setColorAt(i, color.setScalar(0.1 + 0.9 * Math.random())); - } - - return mesh; -} diff --git a/examples-testing/examples/webgl_postprocessing_unreal_bloom.ts b/examples-testing/examples/webgl_postprocessing_unreal_bloom.ts deleted file mode 100644 index 53ec2fe2f..000000000 --- a/examples-testing/examples/webgl_postprocessing_unreal_bloom.ts +++ /dev/null @@ -1,136 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -let camera, stats; -let composer, renderer, mixer, clock; - -const params = { - threshold: 0, - strength: 1, - radius: 0, - exposure: 1, -}; - -init(); - -async function init() { - const container = document.getElementById('container'); - - clock = new THREE.Clock(); - - const scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 100); - camera.position.set(-5, 2.5, -3.5); - scene.add(camera); - - scene.add(new THREE.AmbientLight(0xcccccc)); - - const pointLight = new THREE.PointLight(0xffffff, 100); - camera.add(pointLight); - - const loader = new GLTFLoader(); - const gltf = await loader.loadAsync('models/gltf/PrimaryIonDrive.glb'); - - const model = gltf.scene; - scene.add(model); - - mixer = new THREE.AnimationMixer(model); - const clip = gltf.animations[0]; - mixer.clipAction(clip.optimize()).play(); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ReinhardToneMapping; - container.appendChild(renderer.domElement); - - // - - const renderScene = new RenderPass(scene, camera); - - const bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight), 1.5, 0.4, 0.85); - bloomPass.threshold = params.threshold; - bloomPass.strength = params.strength; - bloomPass.radius = params.radius; - - const outputPass = new OutputPass(); - - composer = new EffectComposer(renderer); - composer.addPass(renderScene); - composer.addPass(bloomPass); - composer.addPass(outputPass); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.maxPolarAngle = Math.PI * 0.5; - controls.minDistance = 3; - controls.maxDistance = 8; - - // - - const gui = new GUI(); - - const bloomFolder = gui.addFolder('bloom'); - - bloomFolder.add(params, 'threshold', 0.0, 1.0).onChange(function (value) { - bloomPass.threshold = Number(value); - }); - - bloomFolder.add(params, 'strength', 0.0, 3.0).onChange(function (value) { - bloomPass.strength = Number(value); - }); - - gui.add(params, 'radius', 0.0, 1.0) - .step(0.01) - .onChange(function (value) { - bloomPass.radius = Number(value); - }); - - const toneMappingFolder = gui.addFolder('tone mapping'); - - toneMappingFolder.add(params, 'exposure', 0.1, 2).onChange(function (value) { - renderer.toneMappingExposure = Math.pow(value, 4.0); - }); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); - composer.setSize(width, height); -} - -function animate() { - const delta = clock.getDelta(); - - mixer.update(delta); - - stats.update(); - - composer.render(); -} diff --git a/examples-testing/examples/webgl_postprocessing_unreal_bloom_selective.ts b/examples-testing/examples/webgl_postprocessing_unreal_bloom_selective.ts deleted file mode 100644 index d633806ee..000000000 --- a/examples-testing/examples/webgl_postprocessing_unreal_bloom_selective.ts +++ /dev/null @@ -1,195 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js'; -import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -const BLOOM_SCENE = 1; - -const bloomLayer = new THREE.Layers(); -bloomLayer.set(BLOOM_SCENE); - -const params = { - threshold: 0, - strength: 1, - radius: 0.5, - exposure: 1, -}; - -const darkMaterial = new THREE.MeshBasicMaterial({ color: 'black' }); -const materials = {}; - -const renderer = new THREE.WebGLRenderer({ antialias: true }); -renderer.setPixelRatio(window.devicePixelRatio); -renderer.setSize(window.innerWidth, window.innerHeight); -renderer.toneMapping = THREE.ReinhardToneMapping; -document.body.appendChild(renderer.domElement); - -const scene = new THREE.Scene(); - -const camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 200); -camera.position.set(0, 0, 20); -camera.lookAt(0, 0, 0); - -const controls = new OrbitControls(camera, renderer.domElement); -controls.maxPolarAngle = Math.PI * 0.5; -controls.minDistance = 1; -controls.maxDistance = 100; -controls.addEventListener('change', render); - -const renderScene = new RenderPass(scene, camera); - -const bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight), 1.5, 0.4, 0.85); -bloomPass.threshold = params.threshold; -bloomPass.strength = params.strength; -bloomPass.radius = params.radius; - -const bloomComposer = new EffectComposer(renderer); -bloomComposer.renderToScreen = false; -bloomComposer.addPass(renderScene); -bloomComposer.addPass(bloomPass); - -const mixPass = new ShaderPass( - new THREE.ShaderMaterial({ - uniforms: { - baseTexture: { value: null }, - bloomTexture: { value: bloomComposer.renderTarget2.texture }, - }, - vertexShader: document.getElementById('vertexshader').textContent, - fragmentShader: document.getElementById('fragmentshader').textContent, - defines: {}, - }), - 'baseTexture', -); -mixPass.needsSwap = true; - -const outputPass = new OutputPass(); - -const finalComposer = new EffectComposer(renderer); -finalComposer.addPass(renderScene); -finalComposer.addPass(mixPass); -finalComposer.addPass(outputPass); - -const raycaster = new THREE.Raycaster(); - -const mouse = new THREE.Vector2(); - -window.addEventListener('pointerdown', onPointerDown); - -const gui = new GUI(); - -const bloomFolder = gui.addFolder('bloom'); - -bloomFolder.add(params, 'threshold', 0.0, 1.0).onChange(function (value) { - bloomPass.threshold = Number(value); - render(); -}); - -bloomFolder.add(params, 'strength', 0.0, 3).onChange(function (value) { - bloomPass.strength = Number(value); - render(); -}); - -bloomFolder - .add(params, 'radius', 0.0, 1.0) - .step(0.01) - .onChange(function (value) { - bloomPass.radius = Number(value); - render(); - }); - -const toneMappingFolder = gui.addFolder('tone mapping'); - -toneMappingFolder.add(params, 'exposure', 0.1, 2).onChange(function (value) { - renderer.toneMappingExposure = Math.pow(value, 4.0); - render(); -}); - -setupScene(); - -function onPointerDown(event) { - mouse.x = (event.clientX / window.innerWidth) * 2 - 1; - mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; - - raycaster.setFromCamera(mouse, camera); - const intersects = raycaster.intersectObjects(scene.children, false); - if (intersects.length > 0) { - const object = intersects[0].object; - object.layers.toggle(BLOOM_SCENE); - render(); - } -} - -window.onresize = function () { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); - - bloomComposer.setSize(width, height); - finalComposer.setSize(width, height); - - render(); -}; - -function setupScene() { - scene.traverse(disposeMaterial); - scene.children.length = 0; - - const geometry = new THREE.IcosahedronGeometry(1, 15); - - for (let i = 0; i < 50; i++) { - const color = new THREE.Color(); - color.setHSL(Math.random(), 0.7, Math.random() * 0.2 + 0.05); - - const material = new THREE.MeshBasicMaterial({ color: color }); - const sphere = new THREE.Mesh(geometry, material); - sphere.position.x = Math.random() * 10 - 5; - sphere.position.y = Math.random() * 10 - 5; - sphere.position.z = Math.random() * 10 - 5; - sphere.position.normalize().multiplyScalar(Math.random() * 4.0 + 2.0); - sphere.scale.setScalar(Math.random() * Math.random() + 0.5); - scene.add(sphere); - - if (Math.random() < 0.25) sphere.layers.enable(BLOOM_SCENE); - } - - render(); -} - -function disposeMaterial(obj) { - if (obj.material) { - obj.material.dispose(); - } -} - -function render() { - scene.traverse(darkenNonBloomed); - bloomComposer.render(); - scene.traverse(restoreMaterial); - - // render the entire scene, then render bloom scene on top - finalComposer.render(); -} - -function darkenNonBloomed(obj) { - if (obj.isMesh && bloomLayer.test(obj.layers) === false) { - materials[obj.uuid] = obj.material; - obj.material = darkMaterial; - } -} - -function restoreMaterial(obj) { - if (materials[obj.uuid]) { - obj.material = materials[obj.uuid]; - delete materials[obj.uuid]; - } -} diff --git a/examples-testing/examples/webgl_raycaster_sprite.ts b/examples-testing/examples/webgl_raycaster_sprite.ts deleted file mode 100644 index f35d5de17..000000000 --- a/examples-testing/examples/webgl_raycaster_sprite.ts +++ /dev/null @@ -1,103 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let renderer, scene, camera; -let group; - -let selectedObject = null; -const raycaster = new THREE.Raycaster(); -const pointer = new THREE.Vector2(); - -init(); - -function init() { - // init renderer - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // init scene - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - - group = new THREE.Group(); - scene.add(group); - - // init camera - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(15, 15, 15); - camera.lookAt(scene.position); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 15; - controls.maxDistance = 250; - - // add sprites - - const sprite1 = new THREE.Sprite(new THREE.SpriteMaterial({ color: '#69f' })); - sprite1.position.set(6, 5, 5); - sprite1.scale.set(2, 5, 1); - group.add(sprite1); - - const sprite2 = new THREE.Sprite(new THREE.SpriteMaterial({ color: '#69f', sizeAttenuation: false })); - sprite2.material.rotation = (Math.PI / 3) * 4; - sprite2.position.set(8, -2, 2); - sprite2.center.set(0.5, 0); - sprite2.scale.set(0.1, 0.5, 0.1); - group.add(sprite2); - - const group2 = new THREE.Object3D(); - group2.scale.set(1, 2, 1); - group2.position.set(-5, 0, 0); - group2.rotation.set(Math.PI / 2, 0, 0); - group.add(group2); - - const sprite3 = new THREE.Sprite(new THREE.SpriteMaterial({ color: '#69f' })); - sprite3.position.set(0, 2, 5); - sprite3.scale.set(10, 2, 3); - sprite3.center.set(-0.1, 0); - sprite3.material.rotation = Math.PI / 3; - group2.add(sprite3); - - window.addEventListener('resize', onWindowResize); - document.addEventListener('pointermove', onPointerMove); -} - -function animate() { - renderer.render(scene, camera); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onPointerMove(event) { - if (selectedObject) { - selectedObject.material.color.set('#69f'); - selectedObject = null; - } - - pointer.x = (event.clientX / window.innerWidth) * 2 - 1; - pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; - - raycaster.setFromCamera(pointer, camera); - - const intersects = raycaster.intersectObject(group, true); - - if (intersects.length > 0) { - const res = intersects.filter(function (res) { - return res && res.object; - })[0]; - - if (res && res.object) { - selectedObject = res.object; - selectedObject.material.color.set('#f00'); - } - } -} diff --git a/examples-testing/examples/webgl_raycaster_texture.ts b/examples-testing/examples/webgl_raycaster_texture.ts deleted file mode 100644 index 72c7054dc..000000000 --- a/examples-testing/examples/webgl_raycaster_texture.ts +++ /dev/null @@ -1,286 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -const WRAPPING = { - RepeatWrapping: THREE.RepeatWrapping, - ClampToEdgeWrapping: THREE.ClampToEdgeWrapping, - MirroredRepeatWrapping: THREE.MirroredRepeatWrapping, -}; - -const params = { - wrapS: THREE.RepeatWrapping, - wrapT: THREE.RepeatWrapping, - offsetX: 0, - offsetY: 0, - repeatX: 1, - repeatY: 1, - rotation: 0, -}; - -function CanvasTexture(parentTexture) { - this._canvas = document.createElement('canvas'); - this._canvas.width = this._canvas.height = 1024; - this._context2D = this._canvas.getContext('2d'); - - if (parentTexture) { - this._parentTexture.push(parentTexture); - parentTexture.image = this._canvas; - } - - const that = this; - this._background = document.createElement('img'); - this._background.addEventListener('load', function () { - that._canvas.width = that._background.naturalWidth; - that._canvas.height = that._background.naturalHeight; - - that._crossRadius = Math.ceil(Math.min(that._canvas.width, that._canvas.height / 30)); - that._crossMax = Math.ceil(0.70710678 * that._crossRadius); - that._crossMin = Math.ceil(that._crossMax / 10); - that._crossThickness = Math.ceil(that._crossMax / 10); - - that._draw(); - }); - this._background.crossOrigin = ''; - this._background.src = 'textures/uv_grid_opengl.jpg'; - - this._draw(); -} - -CanvasTexture.prototype = { - constructor: CanvasTexture, - - _canvas: null, - _context2D: null, - _xCross: 0, - _yCross: 0, - - _crossRadius: 57, - _crossMax: 40, - _crossMin: 4, - _crossThickness: 4, - - _parentTexture: [], - - addParent: function (parentTexture) { - if (this._parentTexture.indexOf(parentTexture) === -1) { - this._parentTexture.push(parentTexture); - parentTexture.image = this._canvas; - } - }, - - setCrossPosition: function (x, y) { - this._xCross = x * this._canvas.width; - this._yCross = y * this._canvas.height; - - this._draw(); - }, - - _draw: function () { - if (!this._context2D) return; - - this._context2D.clearRect(0, 0, this._canvas.width, this._canvas.height); - - // Background. - this._context2D.drawImage(this._background, 0, 0); - - // Yellow cross. - this._context2D.lineWidth = this._crossThickness * 3; - this._context2D.strokeStyle = '#FFFF00'; - - this._context2D.beginPath(); - this._context2D.moveTo(this._xCross - this._crossMax - 2, this._yCross - this._crossMax - 2); - this._context2D.lineTo(this._xCross - this._crossMin, this._yCross - this._crossMin); - - this._context2D.moveTo(this._xCross + this._crossMin, this._yCross + this._crossMin); - this._context2D.lineTo(this._xCross + this._crossMax + 2, this._yCross + this._crossMax + 2); - - this._context2D.moveTo(this._xCross - this._crossMax - 2, this._yCross + this._crossMax + 2); - this._context2D.lineTo(this._xCross - this._crossMin, this._yCross + this._crossMin); - - this._context2D.moveTo(this._xCross + this._crossMin, this._yCross - this._crossMin); - this._context2D.lineTo(this._xCross + this._crossMax + 2, this._yCross - this._crossMax - 2); - - this._context2D.stroke(); - - for (let i = 0; i < this._parentTexture.length; i++) { - this._parentTexture[i].needsUpdate = true; - } - }, -}; - -const width = window.innerWidth; -const height = window.innerHeight; - -let canvas; -let planeTexture, cubeTexture, circleTexture; - -let container; - -let camera, scene, renderer; - -const raycaster = new THREE.Raycaster(); -const mouse = new THREE.Vector2(); -const onClickPosition = new THREE.Vector2(); - -init(); - -function init() { - container = document.getElementById('container'); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xeeeeee); - - camera = new THREE.PerspectiveCamera(45, width / height, 1, 1000); - camera.position.x = -30; - camera.position.y = 40; - camera.position.z = 50; - camera.lookAt(scene.position); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(width, height); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // A cube, in the middle. - cubeTexture = new THREE.Texture(undefined, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping); - cubeTexture.colorSpace = THREE.SRGBColorSpace; - canvas = new CanvasTexture(cubeTexture); - const cubeMaterial = new THREE.MeshBasicMaterial({ map: cubeTexture }); - const cubeGeometry = new THREE.BoxGeometry(20, 20, 20); - let uvs = cubeGeometry.attributes.uv.array; - // Set a specific texture mapping. - for (let i = 0; i < uvs.length; i++) { - uvs[i] *= 2; - } - - const cube = new THREE.Mesh(cubeGeometry, cubeMaterial); - cube.position.x = 4; - cube.position.y = -5; - cube.position.z = 0; - scene.add(cube); - - // A plane on the left - - planeTexture = new THREE.Texture( - undefined, - THREE.UVMapping, - THREE.MirroredRepeatWrapping, - THREE.MirroredRepeatWrapping, - ); - planeTexture.colorSpace = THREE.SRGBColorSpace; - canvas.addParent(planeTexture); - const planeMaterial = new THREE.MeshBasicMaterial({ map: planeTexture }); - const planeGeometry = new THREE.PlaneGeometry(25, 25, 1, 1); - uvs = planeGeometry.attributes.uv.array; - - // Set a specific texture mapping. - - for (let i = 0; i < uvs.length; i++) { - uvs[i] *= 2; - } - - const plane = new THREE.Mesh(planeGeometry, planeMaterial); - plane.position.x = -16; - plane.position.y = -5; - plane.position.z = 0; - scene.add(plane); - - // A circle on the right. - - circleTexture = new THREE.Texture(undefined, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping); - circleTexture.colorSpace = THREE.SRGBColorSpace; - canvas.addParent(circleTexture); - const circleMaterial = new THREE.MeshBasicMaterial({ map: circleTexture }); - const circleGeometry = new THREE.CircleGeometry(25, 40, 0, Math.PI * 2); - uvs = circleGeometry.attributes.uv.array; - - // Set a specific texture mapping. - - for (let i = 0; i < uvs.length; i++) { - uvs[i] = (uvs[i] - 0.25) * 2; - } - - const circle = new THREE.Mesh(circleGeometry, circleMaterial); - circle.position.x = 24; - circle.position.y = -5; - circle.position.z = 0; - scene.add(circle); - - window.addEventListener('resize', onWindowResize); - container.addEventListener('mousemove', onMouseMove); - - // - - const gui = new GUI(); - gui.title('Circle Texture Settings'); - - gui.add(params, 'wrapS', WRAPPING).onChange(setwrapS); - gui.add(params, 'wrapT', WRAPPING).onChange(setwrapT); - gui.add(params, 'offsetX', 0, 5); - gui.add(params, 'offsetY', 0, 5); - gui.add(params, 'repeatX', 0, 5); - gui.add(params, 'repeatY', 0, 5); - gui.add(params, 'rotation', 0, 2 * Math.PI); - gui.open(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onMouseMove(evt) { - evt.preventDefault(); - - const array = getMousePosition(container, evt.clientX, evt.clientY); - onClickPosition.fromArray(array); - - const intersects = getIntersects(onClickPosition, scene.children); - - if (intersects.length > 0 && intersects[0].uv) { - const uv = intersects[0].uv; - intersects[0].object.material.map.transformUv(uv); - canvas.setCrossPosition(uv.x, uv.y); - } -} - -function getMousePosition(dom, x, y) { - const rect = dom.getBoundingClientRect(); - return [(x - rect.left) / rect.width, (y - rect.top) / rect.height]; -} - -function getIntersects(point, objects) { - mouse.set(point.x * 2 - 1, -(point.y * 2) + 1); - - raycaster.setFromCamera(mouse, camera); - - return raycaster.intersectObjects(objects, false); -} - -function animate() { - // update texture parameters - - circleTexture.offset.x = params.offsetX; - circleTexture.offset.y = params.offsetY; - circleTexture.repeat.x = params.repeatX; - circleTexture.repeat.y = params.repeatY; - circleTexture.rotation = params.rotation; - - // - - renderer.render(scene, camera); -} - -function setwrapS(value) { - circleTexture.wrapS = value; - circleTexture.needsUpdate = true; -} - -function setwrapT(value) { - circleTexture.wrapT = value; - circleTexture.needsUpdate = true; -} diff --git a/examples-testing/examples/webgl_raymarching_reflect.ts b/examples-testing/examples/webgl_raymarching_reflect.ts deleted file mode 100644 index e5448ebbd..000000000 --- a/examples-testing/examples/webgl_raymarching_reflect.ts +++ /dev/null @@ -1,95 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let dolly, camera, scene, renderer; -let geometry, material, mesh; -let stats, clock; - -const canvas = document.querySelector('#canvas'); - -const config = { - saveImage: function () { - renderer.render(scene, camera); - window.open(canvas.toDataURL()); - }, - resolution: '512', -}; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer({ canvas: canvas }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(parseInt(config.resolution), parseInt(config.resolution)); - renderer.setAnimationLoop(animate); - - window.addEventListener('resize', onWindowResize); - - // THREE.Scene - scene = new THREE.Scene(); - - dolly = new THREE.Group(); - scene.add(dolly); - - clock = new THREE.Clock(); - - camera = new THREE.PerspectiveCamera(60, canvas.width / canvas.height, 1, 2000); - camera.position.z = 4; - dolly.add(camera); - - geometry = new THREE.PlaneGeometry(2.0, 2.0); - material = new THREE.RawShaderMaterial({ - uniforms: { - resolution: { value: new THREE.Vector2(canvas.width, canvas.height) }, - cameraWorldMatrix: { value: camera.matrixWorld }, - cameraProjectionMatrixInverse: { value: camera.projectionMatrixInverse.clone() }, - }, - vertexShader: document.getElementById('vertex_shader').textContent, - fragmentShader: document.getElementById('fragment_shader').textContent, - }); - mesh = new THREE.Mesh(geometry, material); - mesh.frustumCulled = false; - scene.add(mesh); - - // Controls - const controls = new OrbitControls(camera, canvas); - controls.enableZoom = false; - - // GUI - const gui = new GUI(); - gui.add(config, 'saveImage').name('Save Image'); - gui.add(config, 'resolution', ['256', '512', '800', 'full']).name('Resolution').onChange(onWindowResize); - - stats = new Stats(); - document.body.appendChild(stats.dom); -} - -function onWindowResize() { - if (config.resolution === 'full') { - renderer.setSize(window.innerWidth, window.innerHeight); - } else { - renderer.setSize(parseInt(config.resolution), parseInt(config.resolution)); - } - - camera.aspect = canvas.width / canvas.height; - camera.updateProjectionMatrix(); - - material.uniforms.resolution.value.set(canvas.width, canvas.height); - material.uniforms.cameraProjectionMatrixInverse.value.copy(camera.projectionMatrixInverse); -} - -function animate() { - stats.begin(); - - const elapsedTime = clock.getElapsedTime(); - - dolly.position.z = -elapsedTime; - - renderer.render(scene, camera); - - stats.end(); -} diff --git a/examples-testing/examples/webgl_read_float_buffer.ts b/examples-testing/examples/webgl_read_float_buffer.ts deleted file mode 100644 index 68452a12a..000000000 --- a/examples-testing/examples/webgl_read_float_buffer.ts +++ /dev/null @@ -1,153 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; - -let cameraRTT, sceneRTT, sceneScreen, renderer, zmesh1, zmesh2; - -let mouseX = 0, - mouseY = 0; - -const windowHalfX = window.innerWidth / 2; -const windowHalfY = window.innerHeight / 2; - -let rtTexture, material, quad; - -let delta = 0.01; -let valueNode; - -init(); - -function init() { - container = document.getElementById('container'); - - cameraRTT = new THREE.OrthographicCamera( - window.innerWidth / -2, - window.innerWidth / 2, - window.innerHeight / 2, - window.innerHeight / -2, - -10000, - 10000, - ); - cameraRTT.position.z = 100; - - // - - sceneRTT = new THREE.Scene(); - sceneScreen = new THREE.Scene(); - - let light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0, 0, 1).normalize(); - sceneRTT.add(light); - - light = new THREE.DirectionalLight(0xffd5d5, 4.5); - light.position.set(0, 0, -1).normalize(); - sceneRTT.add(light); - - rtTexture = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight, { - minFilter: THREE.LinearFilter, - magFilter: THREE.NearestFilter, - format: THREE.RGBAFormat, - type: THREE.FloatType, - }); - - material = new THREE.ShaderMaterial({ - uniforms: { time: { value: 0.0 } }, - vertexShader: document.getElementById('vertexShader').textContent, - fragmentShader: document.getElementById('fragment_shader_pass_1').textContent, - }); - - const materialScreen = new THREE.ShaderMaterial({ - uniforms: { tDiffuse: { value: rtTexture.texture } }, - vertexShader: document.getElementById('vertexShader').textContent, - fragmentShader: document.getElementById('fragment_shader_screen').textContent, - - depthWrite: false, - }); - - const plane = new THREE.PlaneGeometry(window.innerWidth, window.innerHeight); - - quad = new THREE.Mesh(plane, material); - quad.position.z = -100; - sceneRTT.add(quad); - - const geometry = new THREE.TorusGeometry(100, 25, 15, 30); - - const mat1 = new THREE.MeshPhongMaterial({ color: 0x9c9c9c, specular: 0xffaa00, shininess: 5 }); - const mat2 = new THREE.MeshPhongMaterial({ color: 0x9c0000, specular: 0xff2200, shininess: 5 }); - - zmesh1 = new THREE.Mesh(geometry, mat1); - zmesh1.position.set(0, 0, 100); - zmesh1.scale.set(1.5, 1.5, 1.5); - sceneRTT.add(zmesh1); - - zmesh2 = new THREE.Mesh(geometry, mat2); - zmesh2.position.set(0, 150, 100); - zmesh2.scale.set(0.75, 0.75, 0.75); - sceneRTT.add(zmesh2); - - quad = new THREE.Mesh(plane, materialScreen); - quad.position.z = -100; - sceneScreen.add(quad); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.autoClear = false; - - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - valueNode = document.getElementById('values'); - - document.addEventListener('mousemove', onDocumentMouseMove); -} - -function onDocumentMouseMove(event) { - mouseX = event.clientX - windowHalfX; - mouseY = event.clientY - windowHalfY; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - const time = Date.now() * 0.0015; - - if (zmesh1 && zmesh2) { - zmesh1.rotation.y = -time; - zmesh2.rotation.y = -time + Math.PI / 2; - } - - if (material.uniforms['time'].value > 1 || material.uniforms['time'].value < 0) { - delta *= -1; - } - - material.uniforms['time'].value += delta; - - renderer.clear(); - - // Render first scene into texture - - renderer.setRenderTarget(rtTexture); - renderer.clear(); - renderer.render(sceneRTT, cameraRTT); - - // Render full screen quad with generated texture - - renderer.setRenderTarget(null); - renderer.render(sceneScreen, cameraRTT); - - const read = new Float32Array(4); - renderer.readRenderTargetPixels(rtTexture, windowHalfX + mouseX, windowHalfY - mouseY, 1, 1, read); - - valueNode.innerHTML = 'r:' + read[0] + '
g:' + read[1] + '
b:' + read[2]; -} diff --git a/examples-testing/examples/webgl_refraction.ts b/examples-testing/examples/webgl_refraction.ts deleted file mode 100644 index 572575afa..000000000 --- a/examples-testing/examples/webgl_refraction.ts +++ /dev/null @@ -1,135 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { Refractor } from 'three/addons/objects/Refractor.js'; -import { WaterRefractionShader } from 'three/addons/shaders/WaterRefractionShader.js'; - -let camera, scene, renderer, clock; - -let refractor, smallSphere; - -init(); - -async function init() { - const container = document.getElementById('container'); - - clock = new THREE.Clock(); - - // scene - scene = new THREE.Scene(); - - // camera - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 500); - camera.position.set(0, 75, 160); - - // refractor - - const refractorGeometry = new THREE.PlaneGeometry(90, 90); - - refractor = new Refractor(refractorGeometry, { - color: 0xcbcbcb, - textureWidth: 1024, - textureHeight: 1024, - shader: WaterRefractionShader, - }); - - refractor.position.set(0, 50, 0); - - scene.add(refractor); - - // load dudv map for distortion effect - - const loader = new THREE.TextureLoader(); - const dudvMap = await loader.loadAsync('textures/waterdudv.jpg'); - - dudvMap.wrapS = dudvMap.wrapT = THREE.RepeatWrapping; - refractor.material.uniforms.tDudv.value = dudvMap; - - // - - const geometry = new THREE.IcosahedronGeometry(5, 0); - const material = new THREE.MeshPhongMaterial({ color: 0xffffff, emissive: 0x333333, flatShading: true }); - smallSphere = new THREE.Mesh(geometry, material); - scene.add(smallSphere); - - // walls - const planeGeo = new THREE.PlaneGeometry(100.1, 100.1); - - const planeTop = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); - planeTop.position.y = 100; - planeTop.rotateX(Math.PI / 2); - scene.add(planeTop); - - const planeBottom = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); - planeBottom.rotateX(-Math.PI / 2); - scene.add(planeBottom); - - const planeBack = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x7f7fff })); - planeBack.position.z = -50; - planeBack.position.y = 50; - scene.add(planeBack); - - const planeRight = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x00ff00 })); - planeRight.position.x = 50; - planeRight.position.y = 50; - planeRight.rotateY(-Math.PI / 2); - scene.add(planeRight); - - const planeLeft = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xff0000 })); - planeLeft.position.x = -50; - planeLeft.position.y = 50; - planeLeft.rotateY(Math.PI / 2); - scene.add(planeLeft); - - // lights - const mainLight = new THREE.PointLight(0xe7e7e7, 2.5, 250, 0); - mainLight.position.y = 60; - scene.add(mainLight); - - const greenLight = new THREE.PointLight(0x00ff00, 0.5, 1000, 0); - greenLight.position.set(550, 50, 0); - scene.add(greenLight); - - const redLight = new THREE.PointLight(0xff0000, 0.5, 1000, 0); - redLight.position.set(-550, 50, 0); - scene.add(redLight); - - const blueLight = new THREE.PointLight(0xbbbbfe, 0.5, 1000, 0); - blueLight.position.set(0, 50, 550); - scene.add(blueLight); - - // renderer - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // controls - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 40, 0); - controls.maxDistance = 400; - controls.minDistance = 10; - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const time = clock.getElapsedTime(); - - refractor.material.uniforms.time.value = time; - - smallSphere.position.set(Math.cos(time) * 30, Math.abs(Math.cos(time * 2)) * 20 + 5, Math.sin(time) * 30); - smallSphere.rotation.y = Math.PI / 2 - time; - smallSphere.rotation.z = time * 8; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_rtt.ts b/examples-testing/examples/webgl_rtt.ts deleted file mode 100644 index 9f16fdab8..000000000 --- a/examples-testing/examples/webgl_rtt.ts +++ /dev/null @@ -1,171 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; - -let cameraRTT, camera, sceneRTT, sceneScreen, scene, renderer, zmesh1, zmesh2; - -let mouseX = 0, - mouseY = 0; - -const windowHalfX = window.innerWidth / 2; -const windowHalfY = window.innerHeight / 2; - -let rtTexture, material, quad; - -let delta = 0.01; - -init(); - -function init() { - container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 100; - - cameraRTT = new THREE.OrthographicCamera( - window.innerWidth / -2, - window.innerWidth / 2, - window.innerHeight / 2, - window.innerHeight / -2, - -10000, - 10000, - ); - cameraRTT.position.z = 100; - - // - - scene = new THREE.Scene(); - sceneRTT = new THREE.Scene(); - sceneScreen = new THREE.Scene(); - - let light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0, 0, 1).normalize(); - sceneRTT.add(light); - - light = new THREE.DirectionalLight(0xffd5d5, 4.5); - light.position.set(0, 0, -1).normalize(); - sceneRTT.add(light); - - rtTexture = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight); - - material = new THREE.ShaderMaterial({ - uniforms: { time: { value: 0.0 } }, - vertexShader: document.getElementById('vertexShader').textContent, - fragmentShader: document.getElementById('fragment_shader_pass_1').textContent, - }); - - const materialScreen = new THREE.ShaderMaterial({ - uniforms: { tDiffuse: { value: rtTexture.texture } }, - vertexShader: document.getElementById('vertexShader').textContent, - fragmentShader: document.getElementById('fragment_shader_screen').textContent, - - depthWrite: false, - }); - - const plane = new THREE.PlaneGeometry(window.innerWidth, window.innerHeight); - - quad = new THREE.Mesh(plane, material); - quad.position.z = -100; - sceneRTT.add(quad); - - const torusGeometry = new THREE.TorusGeometry(100, 25, 15, 30); - - const mat1 = new THREE.MeshPhongMaterial({ color: 0x9c9c9c, specular: 0xffaa00, shininess: 5 }); - const mat2 = new THREE.MeshPhongMaterial({ color: 0x9c0000, specular: 0xff2200, shininess: 5 }); - - zmesh1 = new THREE.Mesh(torusGeometry, mat1); - zmesh1.position.set(0, 0, 100); - zmesh1.scale.set(1.5, 1.5, 1.5); - sceneRTT.add(zmesh1); - - zmesh2 = new THREE.Mesh(torusGeometry, mat2); - zmesh2.position.set(0, 150, 100); - zmesh2.scale.set(0.75, 0.75, 0.75); - sceneRTT.add(zmesh2); - - quad = new THREE.Mesh(plane, materialScreen); - quad.position.z = -100; - sceneScreen.add(quad); - - const n = 5, - geometry = new THREE.SphereGeometry(10, 64, 32), - material2 = new THREE.MeshBasicMaterial({ color: 0xffffff, map: rtTexture.texture }); - - for (let j = 0; j < n; j++) { - for (let i = 0; i < n; i++) { - const mesh = new THREE.Mesh(geometry, material2); - - mesh.position.x = (i - (n - 1) / 2) * 20; - mesh.position.y = (j - (n - 1) / 2) * 20; - mesh.position.z = 0; - - mesh.rotation.y = -Math.PI / 2; - - scene.add(mesh); - } - } - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.autoClear = false; - - container.appendChild(renderer.domElement); - - stats = new Stats(); - container.appendChild(stats.dom); - - document.addEventListener('mousemove', onDocumentMouseMove); -} - -function onDocumentMouseMove(event) { - mouseX = event.clientX - windowHalfX; - mouseY = event.clientY - windowHalfY; -} - -// - -function animate() { - render(); - stats.update(); -} - -function render() { - const time = Date.now() * 0.0015; - - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-mouseY - camera.position.y) * 0.05; - - camera.lookAt(scene.position); - - if (zmesh1 && zmesh2) { - zmesh1.rotation.y = -time; - zmesh2.rotation.y = -time + Math.PI / 2; - } - - if (material.uniforms['time'].value > 1 || material.uniforms['time'].value < 0) { - delta *= -1; - } - - material.uniforms['time'].value += delta; - - // Render first scene into texture - - renderer.setRenderTarget(rtTexture); - renderer.clear(); - renderer.render(sceneRTT, cameraRTT); - - // Render full screen quad with generated texture - - renderer.setRenderTarget(null); - renderer.clear(); - renderer.render(sceneScreen, cameraRTT); - - // Render second scene to screen - // (using first scene as regular texture) - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_shader.ts b/examples-testing/examples/webgl_shader.ts deleted file mode 100644 index 47a6c7ece..000000000 --- a/examples-testing/examples/webgl_shader.ts +++ /dev/null @@ -1,50 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer; - -let uniforms; - -init(); - -function init() { - const container = document.getElementById('container'); - - camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1); - - scene = new THREE.Scene(); - - const geometry = new THREE.PlaneGeometry(2, 2); - - uniforms = { - time: { value: 1.0 }, - }; - - const material = new THREE.ShaderMaterial({ - uniforms: uniforms, - vertexShader: document.getElementById('vertexShader').textContent, - fragmentShader: document.getElementById('fragmentShader').textContent, - }); - - const mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - uniforms['time'].value = performance.now() / 1000; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_shader_lava.ts b/examples-testing/examples/webgl_shader_lava.ts deleted file mode 100644 index 973a580eb..000000000 --- a/examples-testing/examples/webgl_shader_lava.ts +++ /dev/null @@ -1,101 +0,0 @@ -import * as THREE from 'three'; - -import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; -import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; -import { BloomPass } from 'three/addons/postprocessing/BloomPass.js'; -import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; - -let camera, renderer, composer, clock; - -let uniforms, mesh; - -init(); - -function init() { - const container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 3000); - camera.position.z = 4; - - const scene = new THREE.Scene(); - - clock = new THREE.Clock(); - - const textureLoader = new THREE.TextureLoader(); - - const cloudTexture = textureLoader.load('textures/lava/cloud.png'); - const lavaTexture = textureLoader.load('textures/lava/lavatile.jpg'); - - lavaTexture.colorSpace = THREE.SRGBColorSpace; - - cloudTexture.wrapS = cloudTexture.wrapT = THREE.RepeatWrapping; - lavaTexture.wrapS = lavaTexture.wrapT = THREE.RepeatWrapping; - - uniforms = { - fogDensity: { value: 0.45 }, - fogColor: { value: new THREE.Vector3(0, 0, 0) }, - time: { value: 1.0 }, - uvScale: { value: new THREE.Vector2(3.0, 1.0) }, - texture1: { value: cloudTexture }, - texture2: { value: lavaTexture }, - }; - - const size = 0.65; - - const material = new THREE.ShaderMaterial({ - uniforms: uniforms, - vertexShader: document.getElementById('vertexShader').textContent, - fragmentShader: document.getElementById('fragmentShader').textContent, - }); - - mesh = new THREE.Mesh(new THREE.TorusGeometry(size, 0.3, 30, 30), material); - mesh.rotation.x = 0.3; - scene.add(mesh); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.autoClear = false; - container.appendChild(renderer.domElement); - - // - - const renderModel = new RenderPass(scene, camera); - const effectBloom = new BloomPass(1.25); - const outputPass = new OutputPass(); - - composer = new EffectComposer(renderer); - - composer.addPass(renderModel); - composer.addPass(effectBloom); - composer.addPass(outputPass); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - composer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const delta = 5 * clock.getDelta(); - - uniforms['time'].value += 0.2 * delta; - - mesh.rotation.y += 0.0125 * delta; - mesh.rotation.x += 0.05 * delta; - - renderer.clear(); - composer.render(0.01); -} diff --git a/examples-testing/examples/webgl_shaders_ocean.ts b/examples-testing/examples/webgl_shaders_ocean.ts deleted file mode 100644 index 8b0f9a738..000000000 --- a/examples-testing/examples/webgl_shaders_ocean.ts +++ /dev/null @@ -1,169 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { Water } from 'three/addons/objects/Water.js'; -import { Sky } from 'three/addons/objects/Sky.js'; - -let container, stats; -let camera, scene, renderer; -let controls, water, sun, mesh; - -init(); - -function init() { - container = document.getElementById('container'); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 0.5; - container.appendChild(renderer.domElement); - - // - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(55, window.innerWidth / window.innerHeight, 1, 20000); - camera.position.set(30, 30, 100); - - // - - sun = new THREE.Vector3(); - - // Water - - const waterGeometry = new THREE.PlaneGeometry(10000, 10000); - - water = new Water(waterGeometry, { - textureWidth: 512, - textureHeight: 512, - waterNormals: new THREE.TextureLoader().load('textures/waternormals.jpg', function (texture) { - texture.wrapS = texture.wrapT = THREE.RepeatWrapping; - }), - sunDirection: new THREE.Vector3(), - sunColor: 0xffffff, - waterColor: 0x001e0f, - distortionScale: 3.7, - fog: scene.fog !== undefined, - }); - - water.rotation.x = -Math.PI / 2; - - scene.add(water); - - // Skybox - - const sky = new Sky(); - sky.scale.setScalar(10000); - scene.add(sky); - - const skyUniforms = sky.material.uniforms; - - skyUniforms['turbidity'].value = 10; - skyUniforms['rayleigh'].value = 2; - skyUniforms['mieCoefficient'].value = 0.005; - skyUniforms['mieDirectionalG'].value = 0.8; - - const parameters = { - elevation: 2, - azimuth: 180, - }; - - const pmremGenerator = new THREE.PMREMGenerator(renderer); - const sceneEnv = new THREE.Scene(); - - let renderTarget; - - function updateSun() { - const phi = THREE.MathUtils.degToRad(90 - parameters.elevation); - const theta = THREE.MathUtils.degToRad(parameters.azimuth); - - sun.setFromSphericalCoords(1, phi, theta); - - sky.material.uniforms['sunPosition'].value.copy(sun); - water.material.uniforms['sunDirection'].value.copy(sun).normalize(); - - if (renderTarget !== undefined) renderTarget.dispose(); - - sceneEnv.add(sky); - renderTarget = pmremGenerator.fromScene(sceneEnv); - scene.add(sky); - - scene.environment = renderTarget.texture; - } - - updateSun(); - - // - - const geometry = new THREE.BoxGeometry(30, 30, 30); - const material = new THREE.MeshStandardMaterial({ roughness: 0 }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - controls = new OrbitControls(camera, renderer.domElement); - controls.maxPolarAngle = Math.PI * 0.495; - controls.target.set(0, 10, 0); - controls.minDistance = 40.0; - controls.maxDistance = 200.0; - controls.update(); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // GUI - - const gui = new GUI(); - - const folderSky = gui.addFolder('Sky'); - folderSky.add(parameters, 'elevation', 0, 90, 0.1).onChange(updateSun); - folderSky.add(parameters, 'azimuth', -180, 180, 0.1).onChange(updateSun); - folderSky.open(); - - const waterUniforms = water.material.uniforms; - - const folderWater = gui.addFolder('Water'); - folderWater.add(waterUniforms.distortionScale, 'value', 0, 8, 0.1).name('distortionScale'); - folderWater.add(waterUniforms.size, 'value', 0.1, 10, 0.1).name('size'); - folderWater.open(); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - const time = performance.now() * 0.001; - - mesh.position.y = Math.sin(time) * 20 + 5; - mesh.rotation.x = time * 0.5; - mesh.rotation.z = time * 0.51; - - water.material.uniforms['time'].value += 1.0 / 60.0; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_shaders_sky.ts b/examples-testing/examples/webgl_shaders_sky.ts deleted file mode 100644 index 18020f78f..000000000 --- a/examples-testing/examples/webgl_shaders_sky.ts +++ /dev/null @@ -1,103 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { Sky } from 'three/addons/objects/Sky.js'; - -let camera, scene, renderer; - -let sky, sun; - -init(); -render(); - -function initSky() { - // Add Sky - sky = new Sky(); - sky.scale.setScalar(450000); - scene.add(sky); - - sun = new THREE.Vector3(); - - /// GUI - - const effectController = { - turbidity: 10, - rayleigh: 3, - mieCoefficient: 0.005, - mieDirectionalG: 0.7, - elevation: 2, - azimuth: 180, - exposure: renderer.toneMappingExposure, - }; - - function guiChanged() { - const uniforms = sky.material.uniforms; - uniforms['turbidity'].value = effectController.turbidity; - uniforms['rayleigh'].value = effectController.rayleigh; - uniforms['mieCoefficient'].value = effectController.mieCoefficient; - uniforms['mieDirectionalG'].value = effectController.mieDirectionalG; - - const phi = THREE.MathUtils.degToRad(90 - effectController.elevation); - const theta = THREE.MathUtils.degToRad(effectController.azimuth); - - sun.setFromSphericalCoords(1, phi, theta); - - uniforms['sunPosition'].value.copy(sun); - - renderer.toneMappingExposure = effectController.exposure; - renderer.render(scene, camera); - } - - const gui = new GUI(); - - gui.add(effectController, 'turbidity', 0.0, 20.0, 0.1).onChange(guiChanged); - gui.add(effectController, 'rayleigh', 0.0, 4, 0.001).onChange(guiChanged); - gui.add(effectController, 'mieCoefficient', 0.0, 0.1, 0.001).onChange(guiChanged); - gui.add(effectController, 'mieDirectionalG', 0.0, 1, 0.001).onChange(guiChanged); - gui.add(effectController, 'elevation', 0, 90, 0.1).onChange(guiChanged); - gui.add(effectController, 'azimuth', -180, 180, 0.1).onChange(guiChanged); - gui.add(effectController, 'exposure', 0, 1, 0.0001).onChange(guiChanged); - - guiChanged(); -} - -function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 100, 2000000); - camera.position.set(0, 100, 2000); - - scene = new THREE.Scene(); - - const helper = new THREE.GridHelper(10000, 2, 0xffffff, 0xffffff); - scene.add(helper); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 0.5; - document.body.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); - //controls.maxPolarAngle = Math.PI / 2; - controls.enableZoom = false; - controls.enablePan = false; - - initSky(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_shadow_contact.ts b/examples-testing/examples/webgl_shadow_contact.ts deleted file mode 100644 index 9eda35b83..000000000 --- a/examples-testing/examples/webgl_shadow_contact.ts +++ /dev/null @@ -1,272 +0,0 @@ -import * as THREE from 'three'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { HorizontalBlurShader } from 'three/addons/shaders/HorizontalBlurShader.js'; -import { VerticalBlurShader } from 'three/addons/shaders/VerticalBlurShader.js'; - -let camera, scene, renderer, stats, gui; - -const meshes = []; - -const PLANE_WIDTH = 2.5; -const PLANE_HEIGHT = 2.5; -const CAMERA_HEIGHT = 0.3; - -const state = { - shadow: { - blur: 3.5, - darkness: 1, - opacity: 1, - }, - plane: { - color: '#ffffff', - opacity: 1, - }, - showWireframe: false, -}; - -let shadowGroup, - renderTarget, - renderTargetBlur, - shadowCamera, - cameraHelper, - depthMaterial, - horizontalBlurMaterial, - verticalBlurMaterial; - -let plane, blurPlane, fillPlane; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(0.5, 1, 2); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); - - // add the example meshes - - const geometries = [ - new THREE.BoxGeometry(0.4, 0.4, 0.4), - new THREE.IcosahedronGeometry(0.3), - new THREE.TorusKnotGeometry(0.4, 0.05, 256, 24, 1, 3), - ]; - - const material = new THREE.MeshNormalMaterial(); - - for (let i = 0, l = geometries.length; i < l; i++) { - const angle = (i / l) * Math.PI * 2; - - const geometry = geometries[i]; - const mesh = new THREE.Mesh(geometry, material); - mesh.position.y = 0.1; - mesh.position.x = Math.cos(angle) / 2.0; - mesh.position.z = Math.sin(angle) / 2.0; - scene.add(mesh); - meshes.push(mesh); - } - - // the container, if you need to move the plane just move this - shadowGroup = new THREE.Group(); - shadowGroup.position.y = -0.3; - scene.add(shadowGroup); - - // the render target that will show the shadows in the plane texture - renderTarget = new THREE.WebGLRenderTarget(512, 512); - renderTarget.texture.generateMipmaps = false; - - // the render target that we will use to blur the first render target - renderTargetBlur = new THREE.WebGLRenderTarget(512, 512); - renderTargetBlur.texture.generateMipmaps = false; - - // make a plane and make it face up - const planeGeometry = new THREE.PlaneGeometry(PLANE_WIDTH, PLANE_HEIGHT).rotateX(Math.PI / 2); - const planeMaterial = new THREE.MeshBasicMaterial({ - map: renderTarget.texture, - opacity: state.shadow.opacity, - transparent: true, - depthWrite: false, - }); - plane = new THREE.Mesh(planeGeometry, planeMaterial); - // make sure it's rendered after the fillPlane - plane.renderOrder = 1; - shadowGroup.add(plane); - - // the y from the texture is flipped! - plane.scale.y = -1; - - // the plane onto which to blur the texture - blurPlane = new THREE.Mesh(planeGeometry); - blurPlane.visible = false; - shadowGroup.add(blurPlane); - - // the plane with the color of the ground - const fillPlaneMaterial = new THREE.MeshBasicMaterial({ - color: state.plane.color, - opacity: state.plane.opacity, - transparent: true, - depthWrite: false, - }); - fillPlane = new THREE.Mesh(planeGeometry, fillPlaneMaterial); - fillPlane.rotateX(Math.PI); - shadowGroup.add(fillPlane); - - // the camera to render the depth material from - shadowCamera = new THREE.OrthographicCamera( - -PLANE_WIDTH / 2, - PLANE_WIDTH / 2, - PLANE_HEIGHT / 2, - -PLANE_HEIGHT / 2, - 0, - CAMERA_HEIGHT, - ); - shadowCamera.rotation.x = Math.PI / 2; // get the camera to look up - shadowGroup.add(shadowCamera); - - cameraHelper = new THREE.CameraHelper(shadowCamera); - - // like MeshDepthMaterial, but goes from black to transparent - depthMaterial = new THREE.MeshDepthMaterial(); - depthMaterial.userData.darkness = { value: state.shadow.darkness }; - depthMaterial.onBeforeCompile = function (shader) { - shader.uniforms.darkness = depthMaterial.userData.darkness; - shader.fragmentShader = /* glsl */ ` - uniform float darkness; - ${shader.fragmentShader.replace( - 'gl_FragColor = vec4( vec3( 1.0 - fragCoordZ ), opacity );', - 'gl_FragColor = vec4( vec3( 0.0 ), ( 1.0 - fragCoordZ ) * darkness );', - )} - `; - }; - - depthMaterial.depthTest = false; - depthMaterial.depthWrite = false; - - horizontalBlurMaterial = new THREE.ShaderMaterial(HorizontalBlurShader); - horizontalBlurMaterial.depthTest = false; - - verticalBlurMaterial = new THREE.ShaderMaterial(VerticalBlurShader); - verticalBlurMaterial.depthTest = false; - - // - - gui = new GUI(); - const shadowFolder = gui.addFolder('shadow'); - shadowFolder.open(); - const planeFolder = gui.addFolder('plane'); - planeFolder.open(); - - shadowFolder.add(state.shadow, 'blur', 0, 15, 0.1); - shadowFolder.add(state.shadow, 'darkness', 1, 5, 0.1).onChange(function () { - depthMaterial.userData.darkness.value = state.shadow.darkness; - }); - shadowFolder.add(state.shadow, 'opacity', 0, 1, 0.01).onChange(function () { - plane.material.opacity = state.shadow.opacity; - }); - planeFolder.addColor(state.plane, 'color').onChange(function () { - fillPlane.material.color = new THREE.Color(state.plane.color); - }); - planeFolder.add(state.plane, 'opacity', 0, 1, 0.01).onChange(function () { - fillPlane.material.opacity = state.plane.opacity; - }); - - gui.add(state, 'showWireframe').onChange(function () { - if (state.showWireframe) { - scene.add(cameraHelper); - } else { - scene.remove(cameraHelper); - } - }); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - new OrbitControls(camera, renderer.domElement); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// renderTarget --> blurPlane (horizontalBlur) --> renderTargetBlur --> blurPlane (verticalBlur) --> renderTarget -function blurShadow(amount) { - blurPlane.visible = true; - - // blur horizontally and draw in the renderTargetBlur - blurPlane.material = horizontalBlurMaterial; - blurPlane.material.uniforms.tDiffuse.value = renderTarget.texture; - horizontalBlurMaterial.uniforms.h.value = (amount * 1) / 256; - - renderer.setRenderTarget(renderTargetBlur); - renderer.render(blurPlane, shadowCamera); - - // blur vertically and draw in the main renderTarget - blurPlane.material = verticalBlurMaterial; - blurPlane.material.uniforms.tDiffuse.value = renderTargetBlur.texture; - verticalBlurMaterial.uniforms.v.value = (amount * 1) / 256; - - renderer.setRenderTarget(renderTarget); - renderer.render(blurPlane, shadowCamera); - - blurPlane.visible = false; -} - -function animate() { - meshes.forEach(mesh => { - mesh.rotation.x += 0.01; - mesh.rotation.y += 0.02; - }); - - // - - // remove the background - const initialBackground = scene.background; - scene.background = null; - - // force the depthMaterial to everything - cameraHelper.visible = false; - scene.overrideMaterial = depthMaterial; - - // set renderer clear alpha - const initialClearAlpha = renderer.getClearAlpha(); - renderer.setClearAlpha(0); - - // render to the render target to get the depths - renderer.setRenderTarget(renderTarget); - renderer.render(scene, shadowCamera); - - // and reset the override material - scene.overrideMaterial = null; - cameraHelper.visible = true; - - blurShadow(state.shadow.blur); - - // a second pass to reduce the artifacts - // (0.4 is the minimum blur amout so that the artifacts are gone) - blurShadow(state.shadow.blur * 0.4); - - // reset and render the normal scene - renderer.setRenderTarget(null); - renderer.setClearAlpha(initialClearAlpha); - scene.background = initialBackground; - - renderer.render(scene, camera); - stats.update(); -} diff --git a/examples-testing/examples/webgl_shadowmap.ts b/examples-testing/examples/webgl_shadowmap.ts deleted file mode 100644 index 6d0ac3adb..000000000 --- a/examples-testing/examples/webgl_shadowmap.ts +++ /dev/null @@ -1,311 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { FontLoader } from 'three/addons/loaders/FontLoader.js'; -import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; -import { ShadowMapViewer } from 'three/addons/utils/ShadowMapViewer.js'; - -const SHADOW_MAP_WIDTH = 2048, - SHADOW_MAP_HEIGHT = 1024; - -let SCREEN_WIDTH = window.innerWidth; -let SCREEN_HEIGHT = window.innerHeight; -const FLOOR = -250; - -let camera, controls, scene, renderer; -let container, stats; - -const NEAR = 10, - FAR = 3000; - -let mixer; - -const morphs = []; - -let light; -let lightShadowMapViewer; - -const clock = new THREE.Clock(); - -let showHUD = false; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - // CAMERA - - camera = new THREE.PerspectiveCamera(23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR); - camera.position.set(700, 50, 1900); - - // SCENE - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x59472b); - scene.fog = new THREE.Fog(0x59472b, 1000, FAR); - - // LIGHTS - - const ambient = new THREE.AmbientLight(0xffffff); - scene.add(ambient); - - light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0, 1500, 1000); - light.castShadow = true; - light.shadow.camera.top = 2000; - light.shadow.camera.bottom = -2000; - light.shadow.camera.left = -2000; - light.shadow.camera.right = 2000; - light.shadow.camera.near = 1200; - light.shadow.camera.far = 2500; - light.shadow.bias = 0.0001; - - light.shadow.mapSize.width = SHADOW_MAP_WIDTH; - light.shadow.mapSize.height = SHADOW_MAP_HEIGHT; - - scene.add(light); - - createHUD(); - createScene(); - - // RENDERER - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - renderer.autoClear = false; - - // - - renderer.shadowMap.enabled = true; - renderer.shadowMap.type = THREE.PCFShadowMap; - - // CONTROLS - - controls = new FirstPersonControls(camera, renderer.domElement); - - controls.lookSpeed = 0.0125; - controls.movementSpeed = 500; - controls.lookVertical = true; - - controls.lookAt(scene.position); - - // STATS - - stats = new Stats(); - //container.appendChild( stats.dom ); - - // - - window.addEventListener('resize', onWindowResize); - window.addEventListener('keydown', onKeyDown); -} - -function onWindowResize() { - SCREEN_WIDTH = window.innerWidth; - SCREEN_HEIGHT = window.innerHeight; - - camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT; - camera.updateProjectionMatrix(); - - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - - controls.handleResize(); -} - -function onKeyDown(event) { - switch (event.keyCode) { - case 84 /*t*/: - showHUD = !showHUD; - break; - } -} - -function createHUD() { - lightShadowMapViewer = new ShadowMapViewer(light); - lightShadowMapViewer.position.x = 10; - lightShadowMapViewer.position.y = SCREEN_HEIGHT - SHADOW_MAP_HEIGHT / 4 - 10; - lightShadowMapViewer.size.width = SHADOW_MAP_WIDTH / 4; - lightShadowMapViewer.size.height = SHADOW_MAP_HEIGHT / 4; - lightShadowMapViewer.update(); -} - -function createScene() { - // GROUND - - const geometry = new THREE.PlaneGeometry(100, 100); - const planeMaterial = new THREE.MeshPhongMaterial({ color: 0xffdd99 }); - - const ground = new THREE.Mesh(geometry, planeMaterial); - - ground.position.set(0, FLOOR, 0); - ground.rotation.x = -Math.PI / 2; - ground.scale.set(100, 100, 100); - - ground.castShadow = false; - ground.receiveShadow = true; - - scene.add(ground); - - // TEXT - - const loader = new FontLoader(); - loader.load('fonts/helvetiker_bold.typeface.json', function (font) { - const textGeo = new TextGeometry('THREE.JS', { - font: font, - - size: 200, - depth: 50, - curveSegments: 12, - - bevelThickness: 2, - bevelSize: 5, - bevelEnabled: true, - }); - - textGeo.computeBoundingBox(); - const centerOffset = -0.5 * (textGeo.boundingBox.max.x - textGeo.boundingBox.min.x); - - const textMaterial = new THREE.MeshPhongMaterial({ color: 0xff0000, specular: 0xffffff }); - - const mesh = new THREE.Mesh(textGeo, textMaterial); - mesh.position.x = centerOffset; - mesh.position.y = FLOOR + 67; - - mesh.castShadow = true; - mesh.receiveShadow = true; - - scene.add(mesh); - }); - - // CUBES - - const cubes1 = new THREE.Mesh(new THREE.BoxGeometry(1500, 220, 150), planeMaterial); - - cubes1.position.y = FLOOR - 50; - cubes1.position.z = 20; - - cubes1.castShadow = true; - cubes1.receiveShadow = true; - - scene.add(cubes1); - - const cubes2 = new THREE.Mesh(new THREE.BoxGeometry(1600, 170, 250), planeMaterial); - - cubes2.position.y = FLOOR - 50; - cubes2.position.z = 20; - - cubes2.castShadow = true; - cubes2.receiveShadow = true; - - scene.add(cubes2); - - // MORPHS - - mixer = new THREE.AnimationMixer(scene); - - function addMorph(mesh, clip, speed, duration, x, y, z, fudgeColor) { - mesh = mesh.clone(); - mesh.material = mesh.material.clone(); - - if (fudgeColor) { - mesh.material.color.offsetHSL(0, Math.random() * 0.5 - 0.25, Math.random() * 0.5 - 0.25); - } - - mesh.speed = speed; - - mixer - .clipAction(clip, mesh) - .setDuration(duration) - // to shift the playback out of phase: - .startAt(-duration * Math.random()) - .play(); - - mesh.position.set(x, y, z); - mesh.rotation.y = Math.PI / 2; - - mesh.castShadow = true; - mesh.receiveShadow = true; - - scene.add(mesh); - - morphs.push(mesh); - } - - const gltfloader = new GLTFLoader(); - - gltfloader.load('models/gltf/Horse.glb', function (gltf) { - const mesh = gltf.scene.children[0]; - - const clip = gltf.animations[0]; - - addMorph(mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, 300, true); - addMorph(mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, 450, true); - addMorph(mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, 600, true); - - addMorph(mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, -300, true); - addMorph(mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, -450, true); - addMorph(mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, -600, true); - }); - - gltfloader.load('models/gltf/Flamingo.glb', function (gltf) { - const mesh = gltf.scene.children[0]; - const clip = gltf.animations[0]; - - addMorph(mesh, clip, 500, 1, 500 - Math.random() * 500, FLOOR + 350, 40); - }); - - gltfloader.load('models/gltf/Stork.glb', function (gltf) { - const mesh = gltf.scene.children[0]; - const clip = gltf.animations[0]; - - addMorph(mesh, clip, 350, 1, 500 - Math.random() * 500, FLOOR + 350, 340); - }); - - gltfloader.load('models/gltf/Parrot.glb', function (gltf) { - const mesh = gltf.scene.children[0]; - const clip = gltf.animations[0]; - - addMorph(mesh, clip, 450, 0.5, 500 - Math.random() * 500, FLOOR + 300, 700); - }); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - const delta = clock.getDelta(); - - mixer.update(delta); - - for (let i = 0; i < morphs.length; i++) { - const morph = morphs[i]; - - morph.position.x += morph.speed * delta; - - if (morph.position.x > 2000) { - morph.position.x = -1000 - Math.random() * 500; - } - } - - controls.update(delta); - - renderer.clear(); - renderer.render(scene, camera); - - // Render debug HUD with shadow map - - if (showHUD) { - lightShadowMapViewer.render(renderer); - } -} diff --git a/examples-testing/examples/webgl_shadowmap_csm.ts b/examples-testing/examples/webgl_shadowmap_csm.ts deleted file mode 100644 index c89bc02df..000000000 --- a/examples-testing/examples/webgl_shadowmap_csm.ts +++ /dev/null @@ -1,253 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { CSM } from 'three/addons/csm/CSM.js'; -import { CSMHelper } from 'three/addons/csm/CSMHelper.js'; - -let renderer, scene, camera, orthoCamera, controls, csm, csmHelper; - -const params = { - orthographic: false, - fade: false, - shadows: true, - far: 1000, - mode: 'practical', - lightX: -1, - lightY: -1, - lightZ: -1, - margin: 100, - lightFar: 5000, - lightNear: 1, - autoUpdateHelper: true, - updateHelper: function () { - csmHelper.update(); - }, -}; - -init(); - -function updateOrthoCamera() { - const size = controls.target.distanceTo(camera.position); - const aspect = camera.aspect; - - orthoCamera.left = (size * aspect) / -2; - orthoCamera.right = (size * aspect) / 2; - - orthoCamera.top = size / 2; - orthoCamera.bottom = size / -2; - orthoCamera.position.copy(camera.position); - orthoCamera.rotation.copy(camera.rotation); - orthoCamera.updateProjectionMatrix(); -} - -function init() { - scene = new THREE.Scene(); - scene.background = new THREE.Color('#454e61'); - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 5000); - orthoCamera = new THREE.OrthographicCamera(); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - renderer.shadowMap.enabled = params.shadows; - renderer.shadowMap.type = THREE.PCFSoftShadowMap; - - controls = new OrbitControls(camera, renderer.domElement); - controls.maxPolarAngle = Math.PI / 2; - camera.position.set(60, 60, 0); - controls.target = new THREE.Vector3(-100, 10, 0); - controls.update(); - - const ambientLight = new THREE.AmbientLight(0xffffff, 1.5); - scene.add(ambientLight); - - const additionalDirectionalLight = new THREE.DirectionalLight(0x000020, 1.5); - additionalDirectionalLight.position - .set(params.lightX, params.lightY, params.lightZ) - .normalize() - .multiplyScalar(-200); - scene.add(additionalDirectionalLight); - - csm = new CSM({ - maxFar: params.far, - cascades: 4, - mode: params.mode, - parent: scene, - shadowMapSize: 1024, - lightDirection: new THREE.Vector3(params.lightX, params.lightY, params.lightZ).normalize(), - camera: camera, - }); - - csmHelper = new CSMHelper(csm); - csmHelper.visible = false; - scene.add(csmHelper); - - const floorMaterial = new THREE.MeshPhongMaterial({ color: '#252a34' }); - csm.setupMaterial(floorMaterial); - - const floor = new THREE.Mesh(new THREE.PlaneGeometry(10000, 10000, 8, 8), floorMaterial); - floor.rotation.x = -Math.PI / 2; - floor.castShadow = true; - floor.receiveShadow = true; - scene.add(floor); - - const material1 = new THREE.MeshPhongMaterial({ color: '#08d9d6' }); - csm.setupMaterial(material1); - - const material2 = new THREE.MeshPhongMaterial({ color: '#ff2e63' }); - csm.setupMaterial(material2); - - const geometry = new THREE.BoxGeometry(10, 10, 10); - - for (let i = 0; i < 40; i++) { - const cube1 = new THREE.Mesh(geometry, i % 2 === 0 ? material1 : material2); - cube1.castShadow = true; - cube1.receiveShadow = true; - scene.add(cube1); - cube1.position.set(-i * 25, 20, 30); - cube1.scale.y = Math.random() * 2 + 6; - - const cube2 = new THREE.Mesh(geometry, i % 2 === 0 ? material2 : material1); - cube2.castShadow = true; - cube2.receiveShadow = true; - scene.add(cube2); - cube2.position.set(-i * 25, 20, -30); - cube2.scale.y = Math.random() * 2 + 6; - } - - const gui = new GUI(); - - gui.add(params, 'orthographic').onChange(function (value) { - csm.camera = value ? orthoCamera : camera; - csm.updateFrustums(); - }); - - gui.add(params, 'fade').onChange(function (value) { - csm.fade = value; - csm.updateFrustums(); - }); - - gui.add(params, 'shadows').onChange(function (value) { - renderer.shadowMap.enabled = value; - - scene.traverse(function (child) { - if (child.material) { - child.material.needsUpdate = true; - } - }); - }); - - gui.add(params, 'far', 1, 5000) - .step(1) - .name('shadow far') - .onChange(function (value) { - csm.maxFar = value; - csm.updateFrustums(); - }); - - gui.add(params, 'mode', ['uniform', 'logarithmic', 'practical']) - .name('frustum split mode') - .onChange(function (value) { - csm.mode = value; - csm.updateFrustums(); - }); - - gui.add(params, 'lightX', -1, 1) - .name('light direction x') - .onChange(function (value) { - csm.lightDirection.x = value; - }); - - gui.add(params, 'lightY', -1, 1) - .name('light direction y') - .onChange(function (value) { - csm.lightDirection.y = value; - }); - - gui.add(params, 'lightZ', -1, 1) - .name('light direction z') - .onChange(function (value) { - csm.lightDirection.z = value; - }); - - gui.add(params, 'margin', 0, 200) - .name('light margin') - .onChange(function (value) { - csm.lightMargin = value; - }); - - gui.add(params, 'lightNear', 1, 10000) - .name('light near') - .onChange(function (value) { - for (let i = 0; i < csm.lights.length; i++) { - csm.lights[i].shadow.camera.near = value; - csm.lights[i].shadow.camera.updateProjectionMatrix(); - } - }); - - gui.add(params, 'lightFar', 1, 10000) - .name('light far') - .onChange(function (value) { - for (let i = 0; i < csm.lights.length; i++) { - csm.lights[i].shadow.camera.far = value; - csm.lights[i].shadow.camera.updateProjectionMatrix(); - } - }); - - const helperFolder = gui.addFolder('helper'); - - helperFolder.add(csmHelper, 'visible'); - - helperFolder.add(csmHelper, 'displayFrustum').onChange(function () { - csmHelper.updateVisibility(); - }); - - helperFolder.add(csmHelper, 'displayPlanes').onChange(function () { - csmHelper.updateVisibility(); - }); - - helperFolder.add(csmHelper, 'displayShadowBounds').onChange(function () { - csmHelper.updateVisibility(); - }); - - helperFolder.add(params, 'autoUpdateHelper').name('auto update'); - - helperFolder.add(params, 'updateHelper').name('update'); - - helperFolder.open(); - - window.addEventListener('resize', function () { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - updateOrthoCamera(); - csm.updateFrustums(); - - renderer.setSize(window.innerWidth, window.innerHeight); - }); -} - -function animate() { - camera.updateMatrixWorld(); - csm.update(); - controls.update(); - - if (params.orthographic) { - updateOrthoCamera(); - csm.updateFrustums(); - - if (params.autoUpdateHelper) { - csmHelper.update(); - } - - renderer.render(scene, orthoCamera); - } else { - if (params.autoUpdateHelper) { - csmHelper.update(); - } - - renderer.render(scene, camera); - } -} diff --git a/examples-testing/examples/webgl_shadowmap_pcss.ts b/examples-testing/examples/webgl_shadowmap_pcss.ts deleted file mode 100644 index a47a011ff..000000000 --- a/examples-testing/examples/webgl_shadowmap_pcss.ts +++ /dev/null @@ -1,161 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let stats; -let camera, scene, renderer; - -let group; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - // scene - - scene = new THREE.Scene(); - scene.fog = new THREE.Fog(0xcce0ff, 5, 100); - - // camera - - camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 10000); - - // We use this particular camera position in order to expose a bug that can sometimes happen presumably - // due to lack of precision when interpolating values over really large triangles. - // It reproduced on at least NVIDIA GTX 1080 and GTX 1050 Ti GPUs when the ground plane was not - // subdivided into segments. - camera.position.x = 7; - camera.position.y = 13; - camera.position.z = 7; - - scene.add(camera); - - // lights - - scene.add(new THREE.AmbientLight(0xaaaaaa, 3)); - - const light = new THREE.DirectionalLight(0xf0f6ff, 4.5); - light.position.set(2, 8, 4); - - light.castShadow = true; - light.shadow.mapSize.width = 1024; - light.shadow.mapSize.height = 1024; - light.shadow.camera.far = 20; - - scene.add(light); - - // scene.add( new DirectionalLightHelper( light ) ); - scene.add(new THREE.CameraHelper(light.shadow.camera)); - - // group - - group = new THREE.Group(); - scene.add(group); - - const geometry = new THREE.SphereGeometry(0.3, 20, 20); - - for (let i = 0; i < 20; i++) { - const material = new THREE.MeshPhongMaterial({ color: Math.random() * 0xffffff }); - - const sphere = new THREE.Mesh(geometry, material); - sphere.position.x = Math.random() - 0.5; - sphere.position.z = Math.random() - 0.5; - sphere.position.normalize(); - sphere.position.multiplyScalar(Math.random() * 2 + 1); - sphere.castShadow = true; - sphere.receiveShadow = true; - sphere.userData.phase = Math.random() * Math.PI; - group.add(sphere); - } - - // ground - - const groundMaterial = new THREE.MeshPhongMaterial({ color: 0x898989 }); - - const ground = new THREE.Mesh(new THREE.PlaneGeometry(20000, 20000, 8, 8), groundMaterial); - ground.rotation.x = -Math.PI / 2; - ground.receiveShadow = true; - scene.add(ground); - - // column - - const column = new THREE.Mesh(new THREE.BoxGeometry(1, 4, 1), groundMaterial); - column.position.y = 2; - column.castShadow = true; - column.receiveShadow = true; - scene.add(column); - - // overwrite shadowmap code - - let shader = THREE.ShaderChunk.shadowmap_pars_fragment; - - shader = shader.replace( - '#ifdef USE_SHADOWMAP', - '#ifdef USE_SHADOWMAP' + document.getElementById('PCSS').textContent, - ); - - shader = shader.replace( - '#if defined( SHADOWMAP_TYPE_PCF )', - document.getElementById('PCSSGetShadow').textContent + '#if defined( SHADOWMAP_TYPE_PCF )', - ); - - THREE.ShaderChunk.shadowmap_pars_fragment = shader; - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.setClearColor(scene.fog.color); - - container.appendChild(renderer.domElement); - - renderer.shadowMap.enabled = true; - - // controls - const controls = new OrbitControls(camera, renderer.domElement); - controls.maxPolarAngle = Math.PI * 0.5; - controls.minDistance = 10; - controls.maxDistance = 75; - controls.target.set(0, 2.5, 0); - controls.update(); - - // performance monitor - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -// - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const time = performance.now() / 1000; - - group.traverse(function (child) { - if ('phase' in child.userData) { - child.position.y = Math.abs(Math.sin(time + child.userData.phase)) * 4 + 0.3; - } - }); - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_shadowmap_performance.ts b/examples-testing/examples/webgl_shadowmap_performance.ts deleted file mode 100644 index 0e45b63f9..000000000 --- a/examples-testing/examples/webgl_shadowmap_performance.ts +++ /dev/null @@ -1,281 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { FontLoader } from 'three/addons/loaders/FontLoader.js'; -import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; - -const SHADOW_MAP_WIDTH = 2048, - SHADOW_MAP_HEIGHT = 1024; - -let SCREEN_WIDTH = window.innerWidth; -let SCREEN_HEIGHT = window.innerHeight; -const FLOOR = -250; - -const ANIMATION_GROUPS = 25; - -let camera, controls, scene, renderer; -let stats; - -const NEAR = 5, - FAR = 3000; - -let morph, mixer; - -const morphs = [], - animGroups = []; - -const clock = new THREE.Clock(); - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - // CAMERA - - camera = new THREE.PerspectiveCamera(23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR); - camera.position.set(700, 50, 1900); - - // SCENE - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x59472b); - scene.fog = new THREE.Fog(0x59472b, 1000, FAR); - - // LIGHTS - - const ambient = new THREE.AmbientLight(0xffffff); - scene.add(ambient); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0, 1500, 1000); - light.castShadow = true; - light.shadow.camera.top = 2000; - light.shadow.camera.bottom = -2000; - light.shadow.camera.left = -2000; - light.shadow.camera.right = 2000; - light.shadow.camera.near = 1200; - light.shadow.camera.far = 2500; - light.shadow.bias = 0.0001; - - light.shadow.mapSize.width = SHADOW_MAP_WIDTH; - light.shadow.mapSize.height = SHADOW_MAP_HEIGHT; - - scene.add(light); - - createScene(); - - // RENDERER - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - renderer.autoClear = false; - - // - - renderer.shadowMap.enabled = true; - renderer.shadowMap.type = THREE.PCFSoftShadowMap; - - // CONTROLS - - controls = new FirstPersonControls(camera, renderer.domElement); - - controls.lookSpeed = 0.0125; - controls.movementSpeed = 500; - controls.lookVertical = true; - - controls.lookAt(scene.position); - - // STATS - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - SCREEN_WIDTH = window.innerWidth; - SCREEN_HEIGHT = window.innerHeight; - - camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT; - camera.updateProjectionMatrix(); - - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - - controls.handleResize(); -} - -function createScene() { - // GROUND - - const geometry = new THREE.PlaneGeometry(100, 100); - const planeMaterial = new THREE.MeshPhongMaterial({ color: 0xffdd99 }); - - const ground = new THREE.Mesh(geometry, planeMaterial); - - ground.position.set(0, FLOOR, 0); - ground.rotation.x = -Math.PI / 2; - ground.scale.set(100, 100, 100); - - ground.castShadow = false; - ground.receiveShadow = true; - - scene.add(ground); - - // TEXT - - const loader = new FontLoader(); - loader.load('fonts/helvetiker_bold.typeface.json', function (font) { - const textGeo = new TextGeometry('THREE.JS', { - font: font, - - size: 200, - depth: 50, - curveSegments: 12, - - bevelThickness: 2, - bevelSize: 5, - bevelEnabled: true, - }); - - textGeo.computeBoundingBox(); - const centerOffset = -0.5 * (textGeo.boundingBox.max.x - textGeo.boundingBox.min.x); - - const textMaterial = new THREE.MeshPhongMaterial({ color: 0xff0000, specular: 0xffffff }); - - const mesh = new THREE.Mesh(textGeo, textMaterial); - mesh.position.x = centerOffset; - mesh.position.y = FLOOR + 67; - - mesh.castShadow = true; - mesh.receiveShadow = true; - - scene.add(mesh); - }); - - // CUBES - - const cubes1 = new THREE.Mesh(new THREE.BoxGeometry(1500, 220, 150), planeMaterial); - - cubes1.position.y = FLOOR - 50; - cubes1.position.z = 20; - - cubes1.castShadow = true; - cubes1.receiveShadow = true; - - scene.add(cubes1); - - const cubes2 = new THREE.Mesh(new THREE.BoxGeometry(1600, 170, 250), planeMaterial); - - cubes2.position.y = FLOOR - 50; - cubes2.position.z = 20; - - cubes2.castShadow = true; - cubes2.receiveShadow = true; - - scene.add(cubes2); - - mixer = new THREE.AnimationMixer(scene); - - for (let i = 0; i !== ANIMATION_GROUPS; ++i) { - const group = new THREE.AnimationObjectGroup(); - animGroups.push(group); - } - - // MORPHS - - function addMorph(mesh, clip, speed, duration, x, y, z, fudgeColor, massOptimization) { - mesh = mesh.clone(); - mesh.material = mesh.material.clone(); - - if (fudgeColor) { - mesh.material.color.offsetHSL(0, Math.random() * 0.5 - 0.25, Math.random() * 0.5 - 0.25); - } - - mesh.speed = speed; - - if (massOptimization) { - const index = Math.floor(Math.random() * ANIMATION_GROUPS), - animGroup = animGroups[index]; - - animGroup.add(mesh); - - if (!mixer.existingAction(clip, animGroup)) { - const randomness = 0.6 * Math.random() - 0.3; - const phase = (index + randomness) / ANIMATION_GROUPS; - - mixer - .clipAction(clip, animGroup) - .setDuration(duration) - .startAt(-duration * phase) - .play(); - } - } else { - mixer - .clipAction(clip, mesh) - .setDuration(duration) - .startAt(-duration * Math.random()) - .play(); - } - - mesh.position.set(x, y, z); - mesh.rotation.y = Math.PI / 2; - - mesh.castShadow = true; - mesh.receiveShadow = true; - - scene.add(mesh); - - morphs.push(mesh); - } - - const gltfLoader = new GLTFLoader(); - gltfLoader.load('models/gltf/Horse.glb', function (gltf) { - const mesh = gltf.scene.children[0]; - const clip = gltf.animations[0]; - - for (let i = -600; i < 601; i += 2) { - addMorph(mesh, clip, 550, 1, 100 - Math.random() * 3000, FLOOR, i, true, true); - } - }); -} - -// - -function animate() { - stats.begin(); - render(); - stats.end(); -} - -function render() { - const delta = clock.getDelta(); - - if (mixer) mixer.update(delta); - - for (let i = 0; i < morphs.length; i++) { - morph = morphs[i]; - - morph.position.x += morph.speed * delta; - - if (morph.position.x > 2000) { - morph.position.x = -1000 - Math.random() * 500; - } - } - - controls.update(delta); - - renderer.clear(); - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_shadowmap_pointlight.ts b/examples-testing/examples/webgl_shadowmap_pointlight.ts deleted file mode 100644 index c68d69749..000000000 --- a/examples-testing/examples/webgl_shadowmap_pointlight.ts +++ /dev/null @@ -1,139 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer, stats; -let pointLight, pointLight2; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 10, 40); - - scene = new THREE.Scene(); - scene.add(new THREE.AmbientLight(0x111122, 3)); - - // lights - - function createLight(color) { - const intensity = 200; - - const light = new THREE.PointLight(color, intensity, 20); - light.castShadow = true; - light.shadow.bias = -0.005; // reduces self-shadowing on double-sided objects - - let geometry = new THREE.SphereGeometry(0.3, 12, 6); - let material = new THREE.MeshBasicMaterial({ color: color }); - material.color.multiplyScalar(intensity); - let sphere = new THREE.Mesh(geometry, material); - light.add(sphere); - - const texture = new THREE.CanvasTexture(generateTexture()); - texture.magFilter = THREE.NearestFilter; - texture.wrapT = THREE.RepeatWrapping; - texture.wrapS = THREE.RepeatWrapping; - texture.repeat.set(1, 4.5); - - geometry = new THREE.SphereGeometry(2, 32, 8); - material = new THREE.MeshPhongMaterial({ - side: THREE.DoubleSide, - alphaMap: texture, - alphaTest: 0.5, - }); - - sphere = new THREE.Mesh(geometry, material); - sphere.castShadow = true; - sphere.receiveShadow = true; - light.add(sphere); - - return light; - } - - pointLight = createLight(0x0088ff); - scene.add(pointLight); - - pointLight2 = createLight(0xff8888); - scene.add(pointLight2); - // - - const geometry = new THREE.BoxGeometry(30, 30, 30); - - const material = new THREE.MeshPhongMaterial({ - color: 0xa0adaf, - shininess: 10, - specular: 0x111111, - side: THREE.BackSide, - }); - - const mesh = new THREE.Mesh(geometry, material); - mesh.position.y = 10; - mesh.receiveShadow = true; - scene.add(mesh); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - renderer.shadowMap.type = THREE.BasicShadowMap; - document.body.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 10, 0); - controls.update(); - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function generateTexture() { - const canvas = document.createElement('canvas'); - canvas.width = 2; - canvas.height = 2; - - const context = canvas.getContext('2d'); - context.fillStyle = 'white'; - context.fillRect(0, 1, 2, 1); - - return canvas; -} - -function animate() { - let time = performance.now() * 0.001; - - pointLight.position.x = Math.sin(time * 0.6) * 9; - pointLight.position.y = Math.sin(time * 0.7) * 9 + 6; - pointLight.position.z = Math.sin(time * 0.8) * 9; - - pointLight.rotation.x = time; - pointLight.rotation.z = time; - - time += 10000; - - pointLight2.position.x = Math.sin(time * 0.6) * 9; - pointLight2.position.y = Math.sin(time * 0.7) * 9 + 6; - pointLight2.position.z = Math.sin(time * 0.8) * 9; - - pointLight2.rotation.x = time; - pointLight2.rotation.z = time; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_shadowmap_progressive.ts b/examples-testing/examples/webgl_shadowmap_progressive.ts deleted file mode 100644 index 86ec68172..000000000 --- a/examples-testing/examples/webgl_shadowmap_progressive.ts +++ /dev/null @@ -1,204 +0,0 @@ -import * as THREE from 'three'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { TransformControls } from 'three/addons/controls/TransformControls.js'; -import { ProgressiveLightMap } from 'three/addons/misc/ProgressiveLightMap.js'; - -// ShadowMap + LightMap Res and Number of Directional Lights -const shadowMapRes = 512, - lightMapRes = 1024, - lightCount = 8; -let camera, - scene, - renderer, - controls, - control, - control2, - object = new THREE.Mesh(), - lightOrigin = null, - progressiveSurfacemap; -const dirLights = [], - lightmapObjects = []; -const params = { - Enable: true, - 'Blur Edges': true, - 'Blend Window': 200, - 'Light Radius': 50, - 'Ambient Weight': 0.5, - 'Debug Lightmap': false, -}; -init(); -createGUI(); - -function init() { - // renderer - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - document.body.appendChild(renderer.domElement); - - // camera - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 100, 200); - camera.name = 'Camera'; - - // scene - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x949494); - scene.fog = new THREE.Fog(0x949494, 1000, 3000); - - // progressive lightmap - progressiveSurfacemap = new ProgressiveLightMap(renderer, lightMapRes); - - // directional lighting "origin" - lightOrigin = new THREE.Group(); - lightOrigin.position.set(60, 150, 100); - scene.add(lightOrigin); - - // transform gizmo - control = new TransformControls(camera, renderer.domElement); - control.addEventListener('dragging-changed', event => { - controls.enabled = !event.value; - }); - control.attach(lightOrigin); - scene.add(control); - - // create 8 directional lights to speed up the convergence - for (let l = 0; l < lightCount; l++) { - const dirLight = new THREE.DirectionalLight(0xffffff, 1.0 / lightCount); - dirLight.name = 'Dir. Light ' + l; - dirLight.position.set(200, 200, 200); - dirLight.castShadow = true; - dirLight.shadow.camera.near = 100; - dirLight.shadow.camera.far = 5000; - dirLight.shadow.camera.right = 150; - dirLight.shadow.camera.left = -150; - dirLight.shadow.camera.top = 150; - dirLight.shadow.camera.bottom = -150; - dirLight.shadow.mapSize.width = shadowMapRes; - dirLight.shadow.mapSize.height = shadowMapRes; - lightmapObjects.push(dirLight); - dirLights.push(dirLight); - } - - // ground - const groundMesh = new THREE.Mesh( - new THREE.PlaneGeometry(600, 600), - new THREE.MeshPhongMaterial({ color: 0xffffff, depthWrite: true }), - ); - groundMesh.position.y = -0.1; - groundMesh.rotation.x = -Math.PI / 2; - groundMesh.name = 'Ground Mesh'; - lightmapObjects.push(groundMesh); - scene.add(groundMesh); - - // model - function loadModel() { - object.traverse(function (child) { - if (child.isMesh) { - child.name = 'Loaded Mesh'; - child.castShadow = true; - child.receiveShadow = true; - child.material = new THREE.MeshPhongMaterial(); - - // This adds the model to the lightmap - lightmapObjects.push(child); - progressiveSurfacemap.addObjectsToLightMap(lightmapObjects); - } else { - child.layers.disableAll(); // Disable Rendering for this - } - }); - scene.add(object); - object.scale.set(2, 2, 2); - object.position.set(0, -16, 0); - control2 = new TransformControls(camera, renderer.domElement); - control2.addEventListener('dragging-changed', event => { - controls.enabled = !event.value; - }); - control2.attach(object); - scene.add(control2); - const lightTarget = new THREE.Group(); - lightTarget.position.set(0, 20, 0); - for (let l = 0; l < dirLights.length; l++) { - dirLights[l].target = lightTarget; - } - - object.add(lightTarget); - } - - const manager = new THREE.LoadingManager(loadModel); - const loader = new GLTFLoader(manager); - loader.load('models/gltf/ShadowmappableMesh.glb', function (obj) { - object = obj.scene.children[0]; - }); - - // controls - controls = new OrbitControls(camera, renderer.domElement); - controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled - controls.dampingFactor = 0.05; - controls.screenSpacePanning = true; - controls.minDistance = 100; - controls.maxDistance = 500; - controls.maxPolarAngle = Math.PI / 1.5; - controls.target.set(0, 100, 0); - - window.addEventListener('resize', onWindowResize); -} - -function createGUI() { - const gui = new GUI({ title: 'Accumulation Settings' }); - gui.add(params, 'Enable'); - gui.add(params, 'Blur Edges'); - gui.add(params, 'Blend Window', 1, 500).step(1); - gui.add(params, 'Light Radius', 0, 200).step(10); - gui.add(params, 'Ambient Weight', 0, 1).step(0.1); - gui.add(params, 'Debug Lightmap'); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - // Update the inertia on the orbit controls - controls.update(); - - // Accumulate Surface Maps - if (params['Enable']) { - progressiveSurfacemap.update(camera, params['Blend Window'], params['Blur Edges']); - - if (!progressiveSurfacemap.firstUpdate) { - progressiveSurfacemap.showDebugLightmap(params['Debug Lightmap']); - } - } - - // Manually Update the Directional Lights - for (let l = 0; l < dirLights.length; l++) { - // Sometimes they will be sampled from the target direction - // Sometimes they will be uniformly sampled from the upper hemisphere - if (Math.random() > params['Ambient Weight']) { - dirLights[l].position.set( - lightOrigin.position.x + Math.random() * params['Light Radius'], - lightOrigin.position.y + Math.random() * params['Light Radius'], - lightOrigin.position.z + Math.random() * params['Light Radius'], - ); - } else { - // Uniform Hemispherical Surface Distribution for Ambient Occlusion - const lambda = Math.acos(2 * Math.random() - 1) - 3.14159 / 2.0; - const phi = 2 * 3.14159 * Math.random(); - dirLights[l].position.set( - Math.cos(lambda) * Math.cos(phi) * 300 + object.position.x, - Math.abs(Math.cos(lambda) * Math.sin(phi) * 300) + object.position.y + 20, - Math.sin(lambda) * 300 + object.position.z, - ); - } - } - - // Render Scene - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_shadowmap_viewer.ts b/examples-testing/examples/webgl_shadowmap_viewer.ts deleted file mode 100644 index f974ef038..000000000 --- a/examples-testing/examples/webgl_shadowmap_viewer.ts +++ /dev/null @@ -1,178 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { ShadowMapViewer } from 'three/addons/utils/ShadowMapViewer.js'; - -let camera, scene, renderer, clock, stats; -let dirLight, spotLight; -let torusKnot, cube; -let dirLightShadowMapViewer, spotLightShadowMapViewer; - -init(); - -function init() { - initScene(); - initShadowMapViewers(); - initMisc(); - - document.body.appendChild(renderer.domElement); - window.addEventListener('resize', onWindowResize); -} - -function initScene() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 15, 35); - - scene = new THREE.Scene(); - - // Lights - - scene.add(new THREE.AmbientLight(0x404040, 3)); - - spotLight = new THREE.SpotLight(0xffffff, 500); - spotLight.name = 'Spot Light'; - spotLight.angle = Math.PI / 5; - spotLight.penumbra = 0.3; - spotLight.position.set(10, 10, 5); - spotLight.castShadow = true; - spotLight.shadow.camera.near = 8; - spotLight.shadow.camera.far = 30; - spotLight.shadow.mapSize.width = 1024; - spotLight.shadow.mapSize.height = 1024; - scene.add(spotLight); - - scene.add(new THREE.CameraHelper(spotLight.shadow.camera)); - - dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.name = 'Dir. Light'; - dirLight.position.set(0, 10, 0); - dirLight.castShadow = true; - dirLight.shadow.camera.near = 1; - dirLight.shadow.camera.far = 10; - dirLight.shadow.camera.right = 15; - dirLight.shadow.camera.left = -15; - dirLight.shadow.camera.top = 15; - dirLight.shadow.camera.bottom = -15; - dirLight.shadow.mapSize.width = 1024; - dirLight.shadow.mapSize.height = 1024; - scene.add(dirLight); - - scene.add(new THREE.CameraHelper(dirLight.shadow.camera)); - - // Geometry - let geometry = new THREE.TorusKnotGeometry(25, 8, 75, 20); - let material = new THREE.MeshPhongMaterial({ - color: 0xff0000, - shininess: 150, - specular: 0x222222, - }); - - torusKnot = new THREE.Mesh(geometry, material); - torusKnot.scale.multiplyScalar(1 / 18); - torusKnot.position.y = 3; - torusKnot.castShadow = true; - torusKnot.receiveShadow = true; - scene.add(torusKnot); - - geometry = new THREE.BoxGeometry(3, 3, 3); - cube = new THREE.Mesh(geometry, material); - cube.position.set(8, 3, 8); - cube.castShadow = true; - cube.receiveShadow = true; - scene.add(cube); - - geometry = new THREE.BoxGeometry(10, 0.15, 10); - material = new THREE.MeshPhongMaterial({ - color: 0xa0adaf, - shininess: 150, - specular: 0x111111, - }); - - const ground = new THREE.Mesh(geometry, material); - ground.scale.multiplyScalar(3); - ground.castShadow = false; - ground.receiveShadow = true; - scene.add(ground); -} - -function initShadowMapViewers() { - dirLightShadowMapViewer = new ShadowMapViewer(dirLight); - spotLightShadowMapViewer = new ShadowMapViewer(spotLight); - resizeShadowMapViewers(); -} - -function initMisc() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - renderer.shadowMap.type = THREE.BasicShadowMap; - - // Mouse control - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 2, 0); - controls.update(); - - clock = new THREE.Clock(); - - stats = new Stats(); - document.body.appendChild(stats.dom); -} - -function resizeShadowMapViewers() { - const size = window.innerWidth * 0.15; - - dirLightShadowMapViewer.position.x = 10; - dirLightShadowMapViewer.position.y = 10; - dirLightShadowMapViewer.size.width = size; - dirLightShadowMapViewer.size.height = size; - dirLightShadowMapViewer.update(); //Required when setting position or size directly - - spotLightShadowMapViewer.size.set(size, size); - spotLightShadowMapViewer.position.set(size + 20, 10); - // spotLightShadowMapViewer.update(); //NOT required because .set updates automatically -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - resizeShadowMapViewers(); - dirLightShadowMapViewer.updateForWindowResize(); - spotLightShadowMapViewer.updateForWindowResize(); -} - -function animate() { - render(); - - stats.update(); -} - -function renderScene() { - renderer.render(scene, camera); -} - -function renderShadowMapViewers() { - dirLightShadowMapViewer.render(renderer); - spotLightShadowMapViewer.render(renderer); -} - -function render() { - const delta = clock.getDelta(); - - renderScene(); - renderShadowMapViewers(); - - torusKnot.rotation.x += 0.25 * delta; - torusKnot.rotation.y += 2 * delta; - torusKnot.rotation.z += 1 * delta; - - cube.rotation.x += 0.25 * delta; - cube.rotation.y += 2 * delta; - cube.rotation.z += 1 * delta; -} diff --git a/examples-testing/examples/webgl_shadowmap_vsm.ts b/examples-testing/examples/webgl_shadowmap_vsm.ts deleted file mode 100644 index 4867c7315..000000000 --- a/examples-testing/examples/webgl_shadowmap_vsm.ts +++ /dev/null @@ -1,200 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer, clock, stats; -let dirLight, spotLight; -let torusKnot, dirGroup; - -init(); - -function init() { - initScene(); - initMisc(); - - // Init gui - const gui = new GUI(); - - const config = { - spotlightRadius: 4, - spotlightSamples: 8, - dirlightRadius: 4, - dirlightSamples: 8, - }; - - const spotlightFolder = gui.addFolder('Spotlight'); - spotlightFolder - .add(config, 'spotlightRadius') - .name('radius') - .min(0) - .max(25) - .onChange(function (value) { - spotLight.shadow.radius = value; - }); - - spotlightFolder - .add(config, 'spotlightSamples', 1, 25, 1) - .name('samples') - .onChange(function (value) { - spotLight.shadow.blurSamples = value; - }); - spotlightFolder.open(); - - const dirlightFolder = gui.addFolder('Directional Light'); - dirlightFolder - .add(config, 'dirlightRadius') - .name('radius') - .min(0) - .max(25) - .onChange(function (value) { - dirLight.shadow.radius = value; - }); - - dirlightFolder - .add(config, 'dirlightSamples', 1, 25, 1) - .name('samples') - .onChange(function (value) { - dirLight.shadow.blurSamples = value; - }); - dirlightFolder.open(); - - document.body.appendChild(renderer.domElement); - window.addEventListener('resize', onWindowResize); -} - -function initScene() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 10, 30); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x222244); - scene.fog = new THREE.Fog(0x222244, 50, 100); - - // Lights - - scene.add(new THREE.AmbientLight(0x444444)); - - spotLight = new THREE.SpotLight(0xff8888, 400); - spotLight.angle = Math.PI / 5; - spotLight.penumbra = 0.3; - spotLight.position.set(8, 10, 5); - spotLight.castShadow = true; - spotLight.shadow.camera.near = 8; - spotLight.shadow.camera.far = 200; - spotLight.shadow.mapSize.width = 256; - spotLight.shadow.mapSize.height = 256; - spotLight.shadow.bias = -0.002; - spotLight.shadow.radius = 4; - scene.add(spotLight); - - dirLight = new THREE.DirectionalLight(0x8888ff, 3); - dirLight.position.set(3, 12, 17); - dirLight.castShadow = true; - dirLight.shadow.camera.near = 0.1; - dirLight.shadow.camera.far = 500; - dirLight.shadow.camera.right = 17; - dirLight.shadow.camera.left = -17; - dirLight.shadow.camera.top = 17; - dirLight.shadow.camera.bottom = -17; - dirLight.shadow.mapSize.width = 512; - dirLight.shadow.mapSize.height = 512; - dirLight.shadow.radius = 4; - dirLight.shadow.bias = -0.0005; - - dirGroup = new THREE.Group(); - dirGroup.add(dirLight); - scene.add(dirGroup); - - // Geometry - - const geometry = new THREE.TorusKnotGeometry(25, 8, 75, 20); - const material = new THREE.MeshPhongMaterial({ - color: 0x999999, - shininess: 0, - specular: 0x222222, - }); - - torusKnot = new THREE.Mesh(geometry, material); - torusKnot.scale.multiplyScalar(1 / 18); - torusKnot.position.y = 3; - torusKnot.castShadow = true; - torusKnot.receiveShadow = true; - scene.add(torusKnot); - - const cylinderGeometry = new THREE.CylinderGeometry(0.75, 0.75, 7, 32); - - const pillar1 = new THREE.Mesh(cylinderGeometry, material); - pillar1.position.set(8, 3.5, 8); - pillar1.castShadow = true; - pillar1.receiveShadow = true; - - const pillar2 = pillar1.clone(); - pillar2.position.set(8, 3.5, -8); - const pillar3 = pillar1.clone(); - pillar3.position.set(-8, 3.5, 8); - const pillar4 = pillar1.clone(); - pillar4.position.set(-8, 3.5, -8); - - scene.add(pillar1); - scene.add(pillar2); - scene.add(pillar3); - scene.add(pillar4); - - const planeGeometry = new THREE.PlaneGeometry(200, 200); - const planeMaterial = new THREE.MeshPhongMaterial({ - color: 0x999999, - shininess: 0, - specular: 0x111111, - }); - - const ground = new THREE.Mesh(planeGeometry, planeMaterial); - ground.rotation.x = -Math.PI / 2; - ground.scale.multiplyScalar(3); - ground.castShadow = true; - ground.receiveShadow = true; - scene.add(ground); -} - -function initMisc() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - renderer.shadowMap.type = THREE.VSMShadowMap; - - // Mouse control - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 2, 0); - controls.update(); - - clock = new THREE.Clock(); - - stats = new Stats(); - document.body.appendChild(stats.dom); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate(time) { - const delta = clock.getDelta(); - - torusKnot.rotation.x += 0.25 * delta; - torusKnot.rotation.y += 0.5 * delta; - torusKnot.rotation.z += 1 * delta; - - dirGroup.rotation.y += 0.7 * delta; - dirLight.position.z = 17 + Math.sin(time * 0.001) * 5; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_shadowmesh.ts b/examples-testing/examples/webgl_shadowmesh.ts deleted file mode 100644 index 412fc0283..000000000 --- a/examples-testing/examples/webgl_shadowmesh.ts +++ /dev/null @@ -1,250 +0,0 @@ -import * as THREE from 'three'; - -import { ShadowMesh } from 'three/addons/objects/ShadowMesh.js'; - -let SCREEN_WIDTH = window.innerWidth; -let SCREEN_HEIGHT = window.innerHeight; - -const scene = new THREE.Scene(); -const camera = new THREE.PerspectiveCamera(55, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 3000); -const clock = new THREE.Clock(); -const renderer = new THREE.WebGLRenderer({ stencil: true }); - -const sunLight = new THREE.DirectionalLight('rgb(255,255,255)', 3); -let useDirectionalLight = true; -let arrowHelper1, arrowHelper2, arrowHelper3; -const arrowDirection = new THREE.Vector3(); -const arrowPosition1 = new THREE.Vector3(); -const arrowPosition2 = new THREE.Vector3(); -const arrowPosition3 = new THREE.Vector3(); -let groundMesh; -let lightSphere, lightHolder; -let pyramid, pyramidShadow; -let sphere, sphereShadow; -let cube, cubeShadow; -let cylinder, cylinderShadow; -let torus, torusShadow; -const normalVector = new THREE.Vector3(0, 1, 0); -const planeConstant = 0.01; // this value must be slightly higher than the groundMesh's y position of 0.0 -const groundPlane = new THREE.Plane(normalVector, planeConstant); -const lightPosition4D = new THREE.Vector4(); -let verticalAngle = 0; -let horizontalAngle = 0; -let frameTime = 0; -const TWO_PI = Math.PI * 2; - -init(); - -function init() { - scene.background = new THREE.Color(0x0096ff); - - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - renderer.setAnimationLoop(animate); - document.getElementById('container').appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); - - camera.position.set(0, 2.5, 10); - scene.add(camera); - onWindowResize(); - - sunLight.position.set(5, 7, -1); - sunLight.lookAt(scene.position); - scene.add(sunLight); - - lightPosition4D.x = sunLight.position.x; - lightPosition4D.y = sunLight.position.y; - lightPosition4D.z = sunLight.position.z; - // amount of light-ray divergence. Ranging from: - // 0.001 = sunlight(min divergence) to 1.0 = pointlight(max divergence) - lightPosition4D.w = 0.001; // must be slightly greater than 0, due to 0 causing matrixInverse errors - - // YELLOW ARROW HELPERS - arrowDirection.subVectors(scene.position, sunLight.position).normalize(); - - arrowPosition1.copy(sunLight.position); - arrowHelper1 = new THREE.ArrowHelper(arrowDirection, arrowPosition1, 0.9, 0xffff00, 0.25, 0.08); - scene.add(arrowHelper1); - - arrowPosition2.copy(sunLight.position).add(new THREE.Vector3(0, 0.2, 0)); - arrowHelper2 = new THREE.ArrowHelper(arrowDirection, arrowPosition2, 0.9, 0xffff00, 0.25, 0.08); - scene.add(arrowHelper2); - - arrowPosition3.copy(sunLight.position).add(new THREE.Vector3(0, -0.2, 0)); - arrowHelper3 = new THREE.ArrowHelper(arrowDirection, arrowPosition3, 0.9, 0xffff00, 0.25, 0.08); - scene.add(arrowHelper3); - - // LIGHTBULB - const lightSphereGeometry = new THREE.SphereGeometry(0.09); - const lightSphereMaterial = new THREE.MeshBasicMaterial({ color: 'rgb(255,255,255)' }); - lightSphere = new THREE.Mesh(lightSphereGeometry, lightSphereMaterial); - scene.add(lightSphere); - lightSphere.visible = false; - - const lightHolderGeometry = new THREE.CylinderGeometry(0.05, 0.05, 0.13); - const lightHolderMaterial = new THREE.MeshBasicMaterial({ color: 'rgb(75,75,75)' }); - lightHolder = new THREE.Mesh(lightHolderGeometry, lightHolderMaterial); - scene.add(lightHolder); - lightHolder.visible = false; - - // GROUND - const groundGeometry = new THREE.BoxGeometry(30, 0.01, 40); - const groundMaterial = new THREE.MeshLambertMaterial({ color: 'rgb(0,130,0)' }); - groundMesh = new THREE.Mesh(groundGeometry, groundMaterial); - groundMesh.position.y = 0.0; //this value must be slightly lower than the planeConstant (0.01) parameter above - scene.add(groundMesh); - - // RED CUBE and CUBE's SHADOW - const cubeGeometry = new THREE.BoxGeometry(1, 1, 1); - const cubeMaterial = new THREE.MeshLambertMaterial({ color: 'rgb(255,0,0)', emissive: 0x200000 }); - cube = new THREE.Mesh(cubeGeometry, cubeMaterial); - cube.position.z = -1; - scene.add(cube); - - cubeShadow = new ShadowMesh(cube); - scene.add(cubeShadow); - - // BLUE CYLINDER and CYLINDER's SHADOW - const cylinderGeometry = new THREE.CylinderGeometry(0.3, 0.3, 2); - const cylinderMaterial = new THREE.MeshPhongMaterial({ color: 'rgb(0,0,255)', emissive: 0x000020 }); - cylinder = new THREE.Mesh(cylinderGeometry, cylinderMaterial); - cylinder.position.z = -2.5; - scene.add(cylinder); - - cylinderShadow = new ShadowMesh(cylinder); - scene.add(cylinderShadow); - - // MAGENTA TORUS and TORUS' SHADOW - const torusGeometry = new THREE.TorusGeometry(1, 0.2, 10, 16, TWO_PI); - const torusMaterial = new THREE.MeshPhongMaterial({ color: 'rgb(255,0,255)', emissive: 0x200020 }); - torus = new THREE.Mesh(torusGeometry, torusMaterial); - torus.position.z = -6; - scene.add(torus); - - torusShadow = new ShadowMesh(torus); - scene.add(torusShadow); - - // WHITE SPHERE and SPHERE'S SHADOW - const sphereGeometry = new THREE.SphereGeometry(0.5, 20, 10); - const sphereMaterial = new THREE.MeshPhongMaterial({ color: 'rgb(255,255,255)', emissive: 0x222222 }); - sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); - sphere.position.set(4, 0.5, 2); - scene.add(sphere); - - sphereShadow = new ShadowMesh(sphere); - scene.add(sphereShadow); - - // YELLOW PYRAMID and PYRAMID'S SHADOW - const pyramidGeometry = new THREE.CylinderGeometry(0, 0.5, 2, 4); - const pyramidMaterial = new THREE.MeshPhongMaterial({ - color: 'rgb(255,255,0)', - emissive: 0x440000, - flatShading: true, - shininess: 0, - }); - pyramid = new THREE.Mesh(pyramidGeometry, pyramidMaterial); - pyramid.position.set(-4, 1, 2); - scene.add(pyramid); - - pyramidShadow = new ShadowMesh(pyramid); - scene.add(pyramidShadow); - - document.getElementById('lightButton').addEventListener('click', lightButtonHandler); -} - -function animate() { - frameTime = clock.getDelta(); - - cube.rotation.x += 1.0 * frameTime; - cube.rotation.y += 1.0 * frameTime; - - cylinder.rotation.y += 1.0 * frameTime; - cylinder.rotation.z -= 1.0 * frameTime; - - torus.rotation.x -= 1.0 * frameTime; - torus.rotation.y -= 1.0 * frameTime; - - pyramid.rotation.y += 0.5 * frameTime; - - horizontalAngle += 0.5 * frameTime; - if (horizontalAngle > TWO_PI) horizontalAngle -= TWO_PI; - cube.position.x = Math.sin(horizontalAngle) * 4; - cylinder.position.x = Math.sin(horizontalAngle) * -4; - torus.position.x = Math.cos(horizontalAngle) * 4; - - verticalAngle += 1.5 * frameTime; - if (verticalAngle > TWO_PI) verticalAngle -= TWO_PI; - cube.position.y = Math.sin(verticalAngle) * 2 + 2.9; - cylinder.position.y = Math.sin(verticalAngle) * 2 + 3.1; - torus.position.y = Math.cos(verticalAngle) * 2 + 3.3; - - // update the ShadowMeshes to follow their shadow-casting objects - cubeShadow.update(groundPlane, lightPosition4D); - cylinderShadow.update(groundPlane, lightPosition4D); - torusShadow.update(groundPlane, lightPosition4D); - sphereShadow.update(groundPlane, lightPosition4D); - pyramidShadow.update(groundPlane, lightPosition4D); - - renderer.render(scene, camera); -} - -function onWindowResize() { - SCREEN_WIDTH = window.innerWidth; - SCREEN_HEIGHT = window.innerHeight; - - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - - camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT; - camera.updateProjectionMatrix(); -} - -function lightButtonHandler() { - useDirectionalLight = !useDirectionalLight; - - if (useDirectionalLight) { - scene.background.setHex(0x0096ff); - - groundMesh.material.color.setHex(0x008200); - sunLight.position.set(5, 7, -1); - sunLight.lookAt(scene.position); - - lightPosition4D.x = sunLight.position.x; - lightPosition4D.y = sunLight.position.y; - lightPosition4D.z = sunLight.position.z; - lightPosition4D.w = 0.001; // more of a directional Light value - - arrowHelper1.visible = true; - arrowHelper2.visible = true; - arrowHelper3.visible = true; - - lightSphere.visible = false; - lightHolder.visible = false; - - document.getElementById('lightButton').value = 'Switch to PointLight'; - } else { - scene.background.setHex(0x000000); - - groundMesh.material.color.setHex(0x969696); - - sunLight.position.set(0, 6, -2); - sunLight.lookAt(scene.position); - lightSphere.position.copy(sunLight.position); - lightHolder.position.copy(lightSphere.position); - lightHolder.position.y += 0.12; - - lightPosition4D.x = sunLight.position.x; - lightPosition4D.y = sunLight.position.y; - lightPosition4D.z = sunLight.position.z; - lightPosition4D.w = 0.9; // more of a point Light value - - arrowHelper1.visible = false; - arrowHelper2.visible = false; - arrowHelper3.visible = false; - - lightSphere.visible = true; - lightHolder.visible = true; - - document.getElementById('lightButton').value = 'Switch to THREE.DirectionalLight'; - } -} diff --git a/examples-testing/examples/webgl_simple_gi.ts b/examples-testing/examples/webgl_simple_gi.ts deleted file mode 100644 index 4ab6dc895..000000000 --- a/examples-testing/examples/webgl_simple_gi.ts +++ /dev/null @@ -1,172 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -class GIMesh extends THREE.Mesh { - copy(source) { - super.copy(source); - - this.geometry = source.geometry.clone(); - - return this; - } -} - -// - -const SimpleGI = function (renderer, scene) { - const SIZE = 32, - SIZE2 = SIZE * SIZE; - - const camera = new THREE.PerspectiveCamera(90, 1, 0.01, 100); - - scene.updateMatrixWorld(true); - - let clone = scene.clone(); - clone.matrixWorldAutoUpdate = false; - - const rt = new THREE.WebGLRenderTarget(SIZE, SIZE); - - const normalMatrix = new THREE.Matrix3(); - - const position = new THREE.Vector3(); - const normal = new THREE.Vector3(); - - let bounces = 0; - let currentVertex = 0; - - const color = new Float32Array(3); - const buffer = new Uint8Array(SIZE2 * 4); - - function compute() { - if (bounces === 3) return; - - const object = scene.children[0]; // torusKnot - const geometry = object.geometry; - - const attributes = geometry.attributes; - const positions = attributes.position.array; - const normals = attributes.normal.array; - - if (attributes.color === undefined) { - const colors = new Float32Array(positions.length); - geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3).setUsage(THREE.DynamicDrawUsage)); - } - - const colors = attributes.color.array; - - const startVertex = currentVertex; - const totalVertex = positions.length / 3; - - for (let i = 0; i < 32; i++) { - if (currentVertex >= totalVertex) break; - - position.fromArray(positions, currentVertex * 3); - position.applyMatrix4(object.matrixWorld); - - normal.fromArray(normals, currentVertex * 3); - normal.applyMatrix3(normalMatrix.getNormalMatrix(object.matrixWorld)).normalize(); - - camera.position.copy(position); - camera.lookAt(position.add(normal)); - - renderer.setRenderTarget(rt); - renderer.render(clone, camera); - - renderer.readRenderTargetPixels(rt, 0, 0, SIZE, SIZE, buffer); - - color[0] = 0; - color[1] = 0; - color[2] = 0; - - for (let k = 0, kl = buffer.length; k < kl; k += 4) { - color[0] += buffer[k + 0]; - color[1] += buffer[k + 1]; - color[2] += buffer[k + 2]; - } - - colors[currentVertex * 3 + 0] = color[0] / (SIZE2 * 255); - colors[currentVertex * 3 + 1] = color[1] / (SIZE2 * 255); - colors[currentVertex * 3 + 2] = color[2] / (SIZE2 * 255); - - currentVertex++; - } - - attributes.color.addUpdateRange(startVertex * 3, (currentVertex - startVertex) * 3); - attributes.color.needsUpdate = true; - - if (currentVertex >= totalVertex) { - clone = scene.clone(); - clone.matrixWorldAutoUpdate = false; - - bounces++; - currentVertex = 0; - } - - requestAnimationFrame(compute); - } - - requestAnimationFrame(compute); -}; - -// - -let camera, scene, renderer; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.z = 4; - - scene = new THREE.Scene(); - - // torus knot - - const torusGeometry = new THREE.TorusKnotGeometry(0.75, 0.3, 128, 32, 1); - const material = new THREE.MeshBasicMaterial({ vertexColors: true }); - - const torusKnot = new GIMesh(torusGeometry, material); - scene.add(torusKnot); - - // room - - const materials = []; - - for (let i = 0; i < 8; i++) { - materials.push(new THREE.MeshBasicMaterial({ color: Math.random() * 0xffffff, side: THREE.BackSide })); - } - - const boxGeometry = new THREE.BoxGeometry(3, 3, 3); - - const box = new THREE.Mesh(boxGeometry, materials); - scene.add(box); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - new SimpleGI(renderer, scene); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 1; - controls.maxDistance = 10; - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.setRenderTarget(null); - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_sprites.ts b/examples-testing/examples/webgl_sprites.ts deleted file mode 100644 index 2e4189347..000000000 --- a/examples-testing/examples/webgl_sprites.ts +++ /dev/null @@ -1,187 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer; -let cameraOrtho, sceneOrtho; - -let spriteTL, spriteTR, spriteBL, spriteBR, spriteC; - -let mapC; - -let group; - -init(); - -function init() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera = new THREE.PerspectiveCamera(60, width / height, 1, 2100); - camera.position.z = 1500; - - cameraOrtho = new THREE.OrthographicCamera(-width / 2, width / 2, height / 2, -height / 2, 1, 10); - cameraOrtho.position.z = 10; - - scene = new THREE.Scene(); - scene.fog = new THREE.Fog(0x000000, 1500, 2100); - - sceneOrtho = new THREE.Scene(); - - // create sprites - - const amount = 200; - const radius = 500; - - const textureLoader = new THREE.TextureLoader(); - - textureLoader.load('textures/sprite0.png', createHUDSprites); - const mapB = textureLoader.load('textures/sprite1.png'); - mapC = textureLoader.load('textures/sprite2.png'); - - mapB.colorSpace = THREE.SRGBColorSpace; - mapC.colorSpace = THREE.SRGBColorSpace; - - group = new THREE.Group(); - - const materialC = new THREE.SpriteMaterial({ map: mapC, color: 0xffffff, fog: true }); - const materialB = new THREE.SpriteMaterial({ map: mapB, color: 0xffffff, fog: true }); - - for (let a = 0; a < amount; a++) { - const x = Math.random() - 0.5; - const y = Math.random() - 0.5; - const z = Math.random() - 0.5; - - let material; - - if (z < 0) { - material = materialB.clone(); - } else { - material = materialC.clone(); - material.color.setHSL(0.5 * Math.random(), 0.75, 0.5); - material.map.offset.set(-0.5, -0.5); - material.map.repeat.set(2, 2); - } - - const sprite = new THREE.Sprite(material); - - sprite.position.set(x, y, z); - sprite.position.normalize(); - sprite.position.multiplyScalar(radius); - - group.add(sprite); - } - - scene.add(group); - - // renderer - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.autoClear = false; // To allow render overlay on top of sprited sphere - - document.body.appendChild(renderer.domElement); - - // - - window.addEventListener('resize', onWindowResize); -} - -function createHUDSprites(texture) { - texture.colorSpace = THREE.SRGBColorSpace; - - const material = new THREE.SpriteMaterial({ map: texture }); - - const width = material.map.image.width; - const height = material.map.image.height; - - spriteTL = new THREE.Sprite(material); - spriteTL.center.set(0.0, 1.0); - spriteTL.scale.set(width, height, 1); - sceneOrtho.add(spriteTL); - - spriteTR = new THREE.Sprite(material); - spriteTR.center.set(1.0, 1.0); - spriteTR.scale.set(width, height, 1); - sceneOrtho.add(spriteTR); - - spriteBL = new THREE.Sprite(material); - spriteBL.center.set(0.0, 0.0); - spriteBL.scale.set(width, height, 1); - sceneOrtho.add(spriteBL); - - spriteBR = new THREE.Sprite(material); - spriteBR.center.set(1.0, 0.0); - spriteBR.scale.set(width, height, 1); - sceneOrtho.add(spriteBR); - - spriteC = new THREE.Sprite(material); - spriteC.center.set(0.5, 0.5); - spriteC.scale.set(width, height, 1); - sceneOrtho.add(spriteC); - - updateHUDSprites(); -} - -function updateHUDSprites() { - const width = window.innerWidth / 2; - const height = window.innerHeight / 2; - - spriteTL.position.set(-width, height, 1); // top left - spriteTR.position.set(width, height, 1); // top right - spriteBL.position.set(-width, -height, 1); // bottom left - spriteBR.position.set(width, -height, 1); // bottom right - spriteC.position.set(0, 0, 1); // center -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - cameraOrtho.left = -width / 2; - cameraOrtho.right = width / 2; - cameraOrtho.top = height / 2; - cameraOrtho.bottom = -height / 2; - cameraOrtho.updateProjectionMatrix(); - - updateHUDSprites(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const time = Date.now() / 1000; - - for (let i = 0, l = group.children.length; i < l; i++) { - const sprite = group.children[i]; - const material = sprite.material; - const scale = Math.sin(time + sprite.position.x * 0.01) * 0.3 + 1.0; - - let imageWidth = 1; - let imageHeight = 1; - - if (material.map && material.map.image && material.map.image.width) { - imageWidth = material.map.image.width; - imageHeight = material.map.image.height; - } - - sprite.material.rotation += 0.1 * (i / l); - sprite.scale.set(scale * imageWidth, scale * imageHeight, 1.0); - - if (material.map !== mapC) { - material.opacity = Math.sin(time + sprite.position.x * 0.01) * 0.4 + 0.6; - } - } - - group.rotation.x = time * 0.5; - group.rotation.y = time * 0.75; - group.rotation.z = time * 1.0; - - renderer.clear(); - renderer.render(scene, camera); - renderer.clearDepth(); - renderer.render(sceneOrtho, cameraOrtho); -} diff --git a/examples-testing/examples/webgl_test_memory.ts b/examples-testing/examples/webgl_test_memory.ts deleted file mode 100644 index f5d0e112d..000000000 --- a/examples-testing/examples/webgl_test_memory.ts +++ /dev/null @@ -1,65 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 200; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); -} - -function createImage() { - const canvas = document.createElement('canvas'); - canvas.width = 256; - canvas.height = 256; - - const context = canvas.getContext('2d'); - context.fillStyle = - 'rgb(' + - Math.floor(Math.random() * 256) + - ',' + - Math.floor(Math.random() * 256) + - ',' + - Math.floor(Math.random() * 256) + - ')'; - context.fillRect(0, 0, 256, 256); - - return canvas; -} - -// - -function animate() { - const geometry = new THREE.SphereGeometry(50, Math.random() * 64, Math.random() * 32); - - const texture = new THREE.CanvasTexture(createImage()); - - const material = new THREE.MeshBasicMaterial({ map: texture, wireframe: true }); - - const mesh = new THREE.Mesh(geometry, material); - - scene.add(mesh); - - renderer.render(scene, camera); - - scene.remove(mesh); - - // clean up - - geometry.dispose(); - material.dispose(); - texture.dispose(); -} diff --git a/examples-testing/examples/webgl_test_memory2.ts b/examples-testing/examples/webgl_test_memory2.ts deleted file mode 100644 index 366a27914..000000000 --- a/examples-testing/examples/webgl_test_memory2.ts +++ /dev/null @@ -1,81 +0,0 @@ -import * as THREE from 'three'; - -const N = 100; - -let container; - -let camera, scene, renderer; - -let geometry; - -const meshes = []; - -let fragmentShader, vertexShader; - -init(); -setInterval(render, 1000 / 60); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - vertexShader = document.getElementById('vertexShader').textContent; - fragmentShader = document.getElementById('fragmentShader').textContent; - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 2000; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - - geometry = new THREE.SphereGeometry(15, 64, 32); - - for (let i = 0; i < N; i++) { - const material = new THREE.ShaderMaterial({ - vertexShader: vertexShader, - fragmentShader: generateFragmentShader(), - }); - - const mesh = new THREE.Mesh(geometry, material); - - mesh.position.x = (0.5 - Math.random()) * 1000; - mesh.position.y = (0.5 - Math.random()) * 1000; - mesh.position.z = (0.5 - Math.random()) * 1000; - - scene.add(mesh); - - meshes.push(mesh); - } - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - container.appendChild(renderer.domElement); -} - -// - -function generateFragmentShader() { - return fragmentShader.replace('XXX', Math.random() + ',' + Math.random() + ',' + Math.random()); -} - -function render() { - for (let i = 0; i < N; i++) { - const mesh = meshes[i]; - mesh.material = new THREE.ShaderMaterial({ - vertexShader: vertexShader, - fragmentShader: generateFragmentShader(), - }); - } - - renderer.render(scene, camera); - - console.log('before', renderer.info.programs.length); - - for (let i = 0; i < N; i++) { - const mesh = meshes[i]; - mesh.material.dispose(); - } - - console.log('after', renderer.info.programs.length); -} diff --git a/examples-testing/examples/webgl_test_wide_gamut.ts b/examples-testing/examples/webgl_test_wide_gamut.ts deleted file mode 100644 index 693dd4593..000000000 --- a/examples-testing/examples/webgl_test_wide_gamut.ts +++ /dev/null @@ -1,118 +0,0 @@ -import * as THREE from 'three'; - -import WebGL from 'three/addons/capabilities/WebGL.js'; - -let container, camera, renderer, loader; -let sceneL, sceneR, textureL, textureR; - -let sliderPos = window.innerWidth / 2; - -const slider = document.querySelector('.slider'); - -const isP3Context = WebGL.isColorSpaceAvailable(THREE.DisplayP3ColorSpace); - -if (isP3Context) { - THREE.ColorManagement.workingColorSpace = THREE.LinearDisplayP3ColorSpace; -} - -init(); - -function init() { - container = document.querySelector('.container'); - - sceneL = new THREE.Scene(); - sceneR = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.z = 6; - - loader = new THREE.TextureLoader(); - - initTextures(); - initSlider(); - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.setScissorTest(true); - container.appendChild(renderer.domElement); - - if (isP3Context && window.matchMedia('( color-gamut: p3 )').matches) { - renderer.outputColorSpace = THREE.DisplayP3ColorSpace; - } - - window.addEventListener('resize', onWindowResize); - window.matchMedia('( color-gamut: p3 )').addEventListener('change', onGamutChange); -} - -async function initTextures() { - const path = 'textures/wide_gamut/logo_{colorSpace}.png'; - - textureL = await loader.loadAsync(path.replace('{colorSpace}', 'srgb')); - textureR = await loader.loadAsync(path.replace('{colorSpace}', 'p3')); - - textureL.colorSpace = THREE.SRGBColorSpace; - textureR.colorSpace = THREE.DisplayP3ColorSpace; - - sceneL.background = THREE.TextureUtils.contain(textureL, window.innerWidth / window.innerHeight); - sceneR.background = THREE.TextureUtils.contain(textureR, window.innerWidth / window.innerHeight); -} - -function initSlider() { - function onPointerDown() { - if (event.isPrimary === false) return; - - window.addEventListener('pointermove', onPointerMove); - window.addEventListener('pointerup', onPointerUp); - } - - function onPointerUp() { - window.removeEventListener('pointermove', onPointerMove); - window.removeEventListener('pointerup', onPointerUp); - } - - function onPointerMove(e) { - if (event.isPrimary === false) return; - - updateSlider(e.pageX); - } - - updateSlider(sliderPos); - - slider.style.touchAction = 'none'; // disable touch scroll - slider.addEventListener('pointerdown', onPointerDown); -} - -function updateSlider(offset) { - sliderPos = Math.max(10, Math.min(window.innerWidth - 10, offset)); - - slider.style.left = sliderPos - slider.offsetWidth / 2 + 'px'; -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - THREE.TextureUtils.contain(sceneL.background, window.innerWidth / window.innerHeight); - THREE.TextureUtils.contain(sceneR.background, window.innerWidth / window.innerHeight); - - updateSlider(sliderPos); -} - -function onGamutChange({ matches }) { - renderer.outputColorSpace = isP3Context && matches ? THREE.DisplayP3ColorSpace : THREE.SRGBColorSpace; - - textureL.needsUpdate = true; - textureR.needsUpdate = true; -} - -function animate() { - renderer.setScissor(0, 0, sliderPos, window.innerHeight); - renderer.render(sceneL, camera); - - renderer.setScissor(sliderPos, 0, window.innerWidth, window.innerHeight); - renderer.render(sceneR, camera); -} diff --git a/examples-testing/examples/webgl_texture2darray_compressed.ts b/examples-testing/examples/webgl_texture2darray_compressed.ts deleted file mode 100644 index f263be706..000000000 --- a/examples-testing/examples/webgl_texture2darray_compressed.ts +++ /dev/null @@ -1,88 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js'; - -let camera, scene, mesh, renderer, stats, clock; - -const planeWidth = 50; -const planeHeight = 25; - -let depthStep = 1; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 2000); - camera.position.z = 70; - - scene = new THREE.Scene(); - - // - clock = new THREE.Clock(); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - const ktx2Loader = new KTX2Loader(); - ktx2Loader.setTranscoderPath('jsm/libs/basis/'); - ktx2Loader.detectSupport(renderer); - - ktx2Loader.load('textures/spiritedaway.ktx2', function (texturearray) { - const material = new THREE.ShaderMaterial({ - uniforms: { - diffuse: { value: texturearray }, - depth: { value: 55 }, - size: { value: new THREE.Vector2(planeWidth, planeHeight) }, - }, - vertexShader: document.getElementById('vs').textContent.trim(), - fragmentShader: document.getElementById('fs').textContent.trim(), - glslVersion: THREE.GLSL3, - }); - - const geometry = new THREE.PlaneGeometry(planeWidth, planeHeight); - - mesh = new THREE.Mesh(geometry, material); - - scene.add(mesh); - }); - - stats = new Stats(); - container.appendChild(stats.dom); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - if (mesh) { - const delta = clock.getDelta() * 10; - - depthStep += delta; - - const value = depthStep % 5; - - mesh.material.uniforms['depth'].value = value; - } - - render(); - stats.update(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_texture2darray_layerupdate.ts b/examples-testing/examples/webgl_texture2darray_layerupdate.ts deleted file mode 100644 index 599fd8579..000000000 --- a/examples-testing/examples/webgl_texture2darray_layerupdate.ts +++ /dev/null @@ -1,124 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js'; - -let camera, scene, mesh, renderer; - -const planeWidth = 20; -const planeHeight = 10; - -init(); - -async function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 2000); - camera.position.z = 70; - - scene = new THREE.Scene(); - - // Configure the renderer. - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - container.appendChild(renderer.domElement); - - // Configure the KTX2 loader. - - const ktx2Loader = new KTX2Loader(); - ktx2Loader.setTranscoderPath('jsm/libs/basis/'); - ktx2Loader.detectSupport(renderer); - - // Load several KTX2 textures which will later be used to modify - // specific texture array layers. - - const spiritedaway = await ktx2Loader.loadAsync('textures/spiritedaway.ktx2'); - - // Create a texture array for rendering. - - const layerByteLength = THREE.TextureUtils.getByteLength( - spiritedaway.image.width, - spiritedaway.image.height, - spiritedaway.format, - spiritedaway.type, - ); - - const textureArray = new THREE.CompressedArrayTexture( - [ - { - data: new Uint8Array(layerByteLength * 3), - width: spiritedaway.image.width, - height: spiritedaway.image.height, - }, - ], - spiritedaway.image.width, - spiritedaway.image.height, - 3, - spiritedaway.format, - spiritedaway.type, - ); - - // Setup the GUI - - const formData = { - srcLayer: 0, - destLayer: 0, - transfer() { - const layerElementLength = layerByteLength / spiritedaway.mipmaps[0].data.BYTES_PER_ELEMENT; - textureArray.mipmaps[0].data.set( - spiritedaway.mipmaps[0].data.subarray( - layerElementLength * (formData.srcLayer % spiritedaway.image.depth), - layerElementLength * ((formData.srcLayer % spiritedaway.image.depth) + 1), - ), - layerByteLength * formData.destLayer, - ); - textureArray.addLayerUpdate(formData.destLayer); - textureArray.needsUpdate = true; - - renderer.render(scene, camera); - }, - }; - - const gui = new GUI(); - gui.add(formData, 'srcLayer', 0, spiritedaway.image.depth - 1, 1); - gui.add(formData, 'destLayer', 0, textureArray.image.depth - 1, 1); - gui.add(formData, 'transfer'); - - /// Setup the scene. - - const material = new THREE.ShaderMaterial({ - uniforms: { - diffuse: { value: textureArray }, - size: { value: new THREE.Vector2(planeWidth, planeHeight) }, - }, - vertexShader: document.getElementById('vs').textContent.trim(), - fragmentShader: document.getElementById('fs').textContent.trim(), - glslVersion: THREE.GLSL3, - }); - - const geometry = new THREE.InstancedBufferGeometry(); - geometry.copy(new THREE.PlaneGeometry(planeWidth, planeHeight)); - geometry.instanceCount = 3; - - const instancedIndexAttribute = new THREE.InstancedBufferAttribute(new Uint16Array([0, 1, 2]), 1, false, 1); - instancedIndexAttribute.gpuType = THREE.IntType; - geometry.setAttribute('instancedIndex', instancedIndexAttribute); - - mesh = new THREE.InstancedMesh(geometry, material, 3); - - scene.add(mesh); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_texture3d.ts b/examples-testing/examples/webgl_texture3d.ts deleted file mode 100644 index 977dbadb7..000000000 --- a/examples-testing/examples/webgl_texture3d.ts +++ /dev/null @@ -1,128 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { NRRDLoader } from 'three/addons/loaders/NRRDLoader.js'; -import { VolumeRenderShader1 } from 'three/addons/shaders/VolumeShader.js'; - -let renderer, scene, camera, controls, material, volconfig, cmtextures; - -init(); - -function init() { - scene = new THREE.Scene(); - - // Create renderer - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - // Create camera (The volume renderer does not work very well with perspective yet) - const h = 512; // frustum height - const aspect = window.innerWidth / window.innerHeight; - camera = new THREE.OrthographicCamera((-h * aspect) / 2, (h * aspect) / 2, h / 2, -h / 2, 1, 1000); - camera.position.set(-64, -64, 128); - camera.up.set(0, 0, 1); // In our data, z is up - - // Create controls - controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); - controls.target.set(64, 64, 128); - controls.minZoom = 0.5; - controls.maxZoom = 4; - controls.enablePan = false; - controls.update(); - - // scene.add( new AxesHelper( 128 ) ); - - // Lighting is baked into the shader a.t.m. - // let dirLight = new DirectionalLight( 0xffffff ); - - // The gui for interaction - volconfig = { clim1: 0, clim2: 1, renderstyle: 'iso', isothreshold: 0.15, colormap: 'viridis' }; - const gui = new GUI(); - gui.add(volconfig, 'clim1', 0, 1, 0.01).onChange(updateUniforms); - gui.add(volconfig, 'clim2', 0, 1, 0.01).onChange(updateUniforms); - gui.add(volconfig, 'colormap', { gray: 'gray', viridis: 'viridis' }).onChange(updateUniforms); - gui.add(volconfig, 'renderstyle', { mip: 'mip', iso: 'iso' }).onChange(updateUniforms); - gui.add(volconfig, 'isothreshold', 0, 1, 0.01).onChange(updateUniforms); - - // Load the data ... - new NRRDLoader().load('models/nrrd/stent.nrrd', function (volume) { - // Texture to hold the volume. We have scalars, so we put our data in the red channel. - // THREEJS will select R32F (33326) based on the THREE.RedFormat and THREE.FloatType. - // Also see https://www.khronos.org/registry/webgl/specs/latest/2.0/#TEXTURE_TYPES_FORMATS_FROM_DOM_ELEMENTS_TABLE - // TODO: look the dtype up in the volume metadata - const texture = new THREE.Data3DTexture(volume.data, volume.xLength, volume.yLength, volume.zLength); - texture.format = THREE.RedFormat; - texture.type = THREE.FloatType; - texture.minFilter = texture.magFilter = THREE.LinearFilter; - texture.unpackAlignment = 1; - texture.needsUpdate = true; - - // Colormap textures - cmtextures = { - viridis: new THREE.TextureLoader().load('textures/cm_viridis.png', render), - gray: new THREE.TextureLoader().load('textures/cm_gray.png', render), - }; - - // Material - const shader = VolumeRenderShader1; - - const uniforms = THREE.UniformsUtils.clone(shader.uniforms); - - uniforms['u_data'].value = texture; - uniforms['u_size'].value.set(volume.xLength, volume.yLength, volume.zLength); - uniforms['u_clim'].value.set(volconfig.clim1, volconfig.clim2); - uniforms['u_renderstyle'].value = volconfig.renderstyle == 'mip' ? 0 : 1; // 0: MIP, 1: ISO - uniforms['u_renderthreshold'].value = volconfig.isothreshold; // For ISO renderstyle - uniforms['u_cmdata'].value = cmtextures[volconfig.colormap]; - - material = new THREE.ShaderMaterial({ - uniforms: uniforms, - vertexShader: shader.vertexShader, - fragmentShader: shader.fragmentShader, - side: THREE.BackSide, // The volume shader uses the backface as its "reference point" - }); - - // THREE.Mesh - const geometry = new THREE.BoxGeometry(volume.xLength, volume.yLength, volume.zLength); - geometry.translate(volume.xLength / 2 - 0.5, volume.yLength / 2 - 0.5, volume.zLength / 2 - 0.5); - - const mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - render(); - }); - - window.addEventListener('resize', onWindowResize); -} - -function updateUniforms() { - material.uniforms['u_clim'].value.set(volconfig.clim1, volconfig.clim2); - material.uniforms['u_renderstyle'].value = volconfig.renderstyle == 'mip' ? 0 : 1; // 0: MIP, 1: ISO - material.uniforms['u_renderthreshold'].value = volconfig.isothreshold; // For ISO renderstyle - material.uniforms['u_cmdata'].value = cmtextures[volconfig.colormap]; - - render(); -} - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); - - const aspect = window.innerWidth / window.innerHeight; - - const frustumHeight = camera.top - camera.bottom; - - camera.left = (-frustumHeight * aspect) / 2; - camera.right = (frustumHeight * aspect) / 2; - - camera.updateProjectionMatrix(); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_texture3d_partialupdate.ts b/examples-testing/examples/webgl_texture3d_partialupdate.ts deleted file mode 100644 index 1ad6d2646..000000000 --- a/examples-testing/examples/webgl_texture3d_partialupdate.ts +++ /dev/null @@ -1,326 +0,0 @@ -import * as THREE from 'three'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -const INITIAL_CLOUD_SIZE = 128; - -let renderer, scene, camera; -let mesh; -let prevTime = performance.now(); -let cloudTexture = null; - -init(); - -function generateCloudTexture(size, scaleFactor = 1.0) { - const data = new Uint8Array(size * size * size); - const scale = (scaleFactor * 10.0) / size; - - let i = 0; - const perlin = new ImprovedNoise(); - const vector = new THREE.Vector3(); - - for (let z = 0; z < size; z++) { - for (let y = 0; y < size; y++) { - for (let x = 0; x < size; x++) { - const dist = vector - .set(x, y, z) - .subScalar(size / 2) - .divideScalar(size) - .length(); - const fadingFactor = (1.0 - dist) * (1.0 - dist); - data[i] = (128 + 128 * perlin.noise((x * scale) / 1.5, y * scale, (z * scale) / 1.5)) * fadingFactor; - - i++; - } - } - } - - return new THREE.Data3DTexture(data, size, size, size); -} - -function init() { - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(0, 0, 1.5); - - new OrbitControls(camera, renderer.domElement); - - // Sky - - const canvas = document.createElement('canvas'); - canvas.width = 1; - canvas.height = 32; - - const context = canvas.getContext('2d'); - const gradient = context.createLinearGradient(0, 0, 0, 32); - gradient.addColorStop(0.0, '#014a84'); - gradient.addColorStop(0.5, '#0561a0'); - gradient.addColorStop(1.0, '#437ab6'); - context.fillStyle = gradient; - context.fillRect(0, 0, 1, 32); - - const skyMap = new THREE.CanvasTexture(canvas); - skyMap.colorSpace = THREE.SRGBColorSpace; - - const sky = new THREE.Mesh( - new THREE.SphereGeometry(10), - new THREE.MeshBasicMaterial({ map: skyMap, side: THREE.BackSide }), - ); - scene.add(sky); - - // Texture - - const texture = new THREE.Data3DTexture( - new Uint8Array(INITIAL_CLOUD_SIZE * INITIAL_CLOUD_SIZE * INITIAL_CLOUD_SIZE).fill(0), - INITIAL_CLOUD_SIZE, - INITIAL_CLOUD_SIZE, - INITIAL_CLOUD_SIZE, - ); - texture.format = THREE.RedFormat; - texture.minFilter = THREE.LinearFilter; - texture.magFilter = THREE.LinearFilter; - texture.unpackAlignment = 1; - texture.needsUpdate = true; - - cloudTexture = texture; - - // Material - - const vertexShader = /* glsl */ ` - in vec3 position; - - uniform mat4 modelMatrix; - uniform mat4 modelViewMatrix; - uniform mat4 projectionMatrix; - uniform vec3 cameraPos; - - out vec3 vOrigin; - out vec3 vDirection; - - void main() { - vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); - - vOrigin = vec3( inverse( modelMatrix ) * vec4( cameraPos, 1.0 ) ).xyz; - vDirection = position - vOrigin; - - gl_Position = projectionMatrix * mvPosition; - } - `; - - const fragmentShader = /* glsl */ ` - precision highp float; - precision highp sampler3D; - - uniform mat4 modelViewMatrix; - uniform mat4 projectionMatrix; - - in vec3 vOrigin; - in vec3 vDirection; - - out vec4 color; - - uniform vec3 base; - uniform sampler3D map; - - uniform float threshold; - uniform float range; - uniform float opacity; - uniform float steps; - uniform float frame; - - uint wang_hash(uint seed) - { - seed = (seed ^ 61u) ^ (seed >> 16u); - seed *= 9u; - seed = seed ^ (seed >> 4u); - seed *= 0x27d4eb2du; - seed = seed ^ (seed >> 15u); - return seed; - } - - float randomFloat(inout uint seed) - { - return float(wang_hash(seed)) / 4294967296.; - } - - vec2 hitBox( vec3 orig, vec3 dir ) { - const vec3 box_min = vec3( - 0.5 ); - const vec3 box_max = vec3( 0.5 ); - vec3 inv_dir = 1.0 / dir; - vec3 tmin_tmp = ( box_min - orig ) * inv_dir; - vec3 tmax_tmp = ( box_max - orig ) * inv_dir; - vec3 tmin = min( tmin_tmp, tmax_tmp ); - vec3 tmax = max( tmin_tmp, tmax_tmp ); - float t0 = max( tmin.x, max( tmin.y, tmin.z ) ); - float t1 = min( tmax.x, min( tmax.y, tmax.z ) ); - return vec2( t0, t1 ); - } - - float sample1( vec3 p ) { - return texture( map, p ).r; - } - - float shading( vec3 coord ) { - float step = 0.01; - return sample1( coord + vec3( - step ) ) - sample1( coord + vec3( step ) ); - } - - vec4 linearToSRGB( in vec4 value ) { - return vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) * 1.055 - vec3( 0.055 ), value.rgb * 12.92, vec3( lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ) ), value.a ); - } - - void main(){ - vec3 rayDir = normalize( vDirection ); - vec2 bounds = hitBox( vOrigin, rayDir ); - - if ( bounds.x > bounds.y ) discard; - - bounds.x = max( bounds.x, 0.0 ); - - vec3 p = vOrigin + bounds.x * rayDir; - vec3 inc = 1.0 / abs( rayDir ); - float delta = min( inc.x, min( inc.y, inc.z ) ); - delta /= steps; - - // Jitter - - // Nice little seed from - // https://blog.demofox.org/2020/05/25/casual-shadertoy-path-tracing-1-basic-camera-diffuse-emissive/ - uint seed = uint( gl_FragCoord.x ) * uint( 1973 ) + uint( gl_FragCoord.y ) * uint( 9277 ) + uint( frame ) * uint( 26699 ); - vec3 size = vec3( textureSize( map, 0 ) ); - float randNum = randomFloat( seed ) * 2.0 - 1.0; - p += rayDir * randNum * ( 1.0 / size ); - - // - - vec4 ac = vec4( base, 0.0 ); - - for ( float t = bounds.x; t < bounds.y; t += delta ) { - - float d = sample1( p + 0.5 ); - - d = smoothstep( threshold - range, threshold + range, d ) * opacity; - - float col = shading( p + 0.5 ) * 3.0 + ( ( p.x + p.y ) * 0.25 ) + 0.2; - - ac.rgb += ( 1.0 - ac.a ) * d * col; - - ac.a += ( 1.0 - ac.a ) * d; - - if ( ac.a >= 0.95 ) break; - - p += rayDir * delta; - - } - - color = linearToSRGB( ac ); - - if ( color.a == 0.0 ) discard; - - } - `; - - const geometry = new THREE.BoxGeometry(1, 1, 1); - const material = new THREE.RawShaderMaterial({ - glslVersion: THREE.GLSL3, - uniforms: { - base: { value: new THREE.Color(0x798aa0) }, - map: { value: texture }, - cameraPos: { value: new THREE.Vector3() }, - threshold: { value: 0.25 }, - opacity: { value: 0.25 }, - range: { value: 0.1 }, - steps: { value: 100 }, - frame: { value: 0 }, - }, - vertexShader, - fragmentShader, - side: THREE.BackSide, - transparent: true, - }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - const parameters = { - threshold: 0.25, - opacity: 0.25, - range: 0.1, - steps: 100, - }; - - function update() { - material.uniforms.threshold.value = parameters.threshold; - material.uniforms.opacity.value = parameters.opacity; - material.uniforms.range.value = parameters.range; - material.uniforms.steps.value = parameters.steps; - } - - const gui = new GUI(); - gui.add(parameters, 'threshold', 0, 1, 0.01).onChange(update); - gui.add(parameters, 'opacity', 0, 1, 0.01).onChange(update); - gui.add(parameters, 'range', 0, 1, 0.01).onChange(update); - gui.add(parameters, 'steps', 0, 200, 1).onChange(update); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -let curr = 0; -const countPerRow = 4; -const countPerSlice = countPerRow * countPerRow; -const sliceCount = 4; -const totalCount = sliceCount * countPerSlice; -const margins = 8; - -const perElementPaddedSize = (INITIAL_CLOUD_SIZE - margins) / countPerRow; -const perElementSize = Math.floor((INITIAL_CLOUD_SIZE - 1) / countPerRow); - -function animate() { - const time = performance.now(); - if (time - prevTime > 1500.0 && curr < totalCount) { - const position = new THREE.Vector3( - Math.floor(curr % countPerRow) * perElementSize + margins * 0.5, - Math.floor((curr % countPerSlice) / countPerRow) * perElementSize + margins * 0.5, - Math.floor(curr / countPerSlice) * perElementSize + margins * 0.5, - ).floor(); - - const maxDimension = perElementPaddedSize - 1; - const box = new THREE.Box3( - new THREE.Vector3(0, 0, 0), - new THREE.Vector3(maxDimension, maxDimension, maxDimension), - ); - const scaleFactor = (Math.random() + 0.5) * 0.5; - const source = generateCloudTexture(perElementPaddedSize, scaleFactor); - - renderer.copyTextureToTexture3D(source, cloudTexture, box, position); - - prevTime = time; - - curr++; - } - - mesh.material.uniforms.cameraPos.value.copy(camera.position); - // mesh.rotation.y = - performance.now() / 7500; - - mesh.material.uniforms.frame.value++; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_tonemapping.ts b/examples-testing/examples/webgl_tonemapping.ts deleted file mode 100644 index 08115cf3e..000000000 --- a/examples-testing/examples/webgl_tonemapping.ts +++ /dev/null @@ -1,163 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -let mesh, renderer, scene, camera, controls; -let gui, - guiExposure = null; - -const params = { - exposure: 1.0, - toneMapping: 'AgX', - blurriness: 0.3, - intensity: 1.0, -}; - -const toneMappingOptions = { - None: THREE.NoToneMapping, - Linear: THREE.LinearToneMapping, - Reinhard: THREE.ReinhardToneMapping, - Cineon: THREE.CineonToneMapping, - ACESFilmic: THREE.ACESFilmicToneMapping, - AgX: THREE.AgXToneMapping, - Neutral: THREE.NeutralToneMapping, - Custom: THREE.CustomToneMapping, -}; - -init().catch(function (err) { - console.error(err); -}); - -async function init() { - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - renderer.toneMapping = toneMappingOptions[params.toneMapping]; - renderer.toneMappingExposure = params.exposure; - - // Set CustomToneMapping to Uncharted2 - // source: http://filmicworlds.com/blog/filmic-tonemapping-operators/ - - THREE.ShaderChunk.tonemapping_pars_fragment = THREE.ShaderChunk.tonemapping_pars_fragment.replace( - 'vec3 CustomToneMapping( vec3 color ) { return color; }', - - `#define Uncharted2Helper( x ) max( ( ( x * ( 0.15 * x + 0.10 * 0.50 ) + 0.20 * 0.02 ) / ( x * ( 0.15 * x + 0.50 ) + 0.20 * 0.30 ) ) - 0.02 / 0.30, vec3( 0.0 ) ) - - float toneMappingWhitePoint = 1.0; - - vec3 CustomToneMapping( vec3 color ) { - color *= toneMappingExposure; - return saturate( Uncharted2Helper( color ) / Uncharted2Helper( vec3( toneMappingWhitePoint ) ) ); - - }`, - ); - - scene = new THREE.Scene(); - scene.backgroundBlurriness = params.blurriness; - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); - camera.position.set(-1.8, 0.6, 2.7); - - controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.enableZoom = false; - controls.enablePan = false; - controls.target.set(0, 0, -0.2); - controls.update(); - - const rgbeLoader = new RGBELoader().setPath('textures/equirectangular/'); - - const gltfLoader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); - - const [texture, gltf] = await Promise.all([ - rgbeLoader.loadAsync('venice_sunset_1k.hdr'), - gltfLoader.loadAsync('DamagedHelmet.gltf'), - ]); - - // environment - - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - scene.environment = texture; - - // model - - mesh = gltf.scene.getObjectByName('node_damagedHelmet_-6514'); - scene.add(mesh); - - render(); - - window.addEventListener('resize', onWindowResize); - - gui = new GUI(); - const toneMappingFolder = gui.addFolder('tone mapping'); - - toneMappingFolder - .add(params, 'toneMapping', Object.keys(toneMappingOptions)) - - .onChange(function () { - updateGUI(toneMappingFolder); - - renderer.toneMapping = toneMappingOptions[params.toneMapping]; - render(); - }); - - const backgroundFolder = gui.addFolder('background'); - - backgroundFolder - .add(params, 'blurriness', 0, 1) - - .onChange(function (value) { - scene.backgroundBlurriness = value; - render(); - }); - - backgroundFolder - .add(params, 'intensity', 0, 1) - - .onChange(function (value) { - scene.backgroundIntensity = value; - render(); - }); - - updateGUI(toneMappingFolder); - - gui.open(); -} - -function updateGUI(folder) { - if (guiExposure !== null) { - guiExposure.destroy(); - guiExposure = null; - } - - if (params.toneMapping !== 'None') { - guiExposure = folder - .add(params, 'exposure', 0, 2) - - .onChange(function () { - renderer.toneMappingExposure = params.exposure; - render(); - }); - } -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_ubo.ts b/examples-testing/examples/webgl_ubo.ts deleted file mode 100644 index 01064f115..000000000 --- a/examples-testing/examples/webgl_ubo.ts +++ /dev/null @@ -1,137 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer, clock; - -init(); - -function init() { - const container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(0, 0, 25); - - scene = new THREE.Scene(); - camera.lookAt(scene.position); - - clock = new THREE.Clock(); - - // geometry - - const geometry1 = new THREE.TetrahedronGeometry(); - const geometry2 = new THREE.BoxGeometry(); - - // texture - - const texture = new THREE.TextureLoader().load('textures/crate.gif'); - texture.colorSpace = THREE.SRGBColorSpace; - - // uniforms groups - - // Camera and lighting related data are perfect examples of using UBOs since you have to store these - // data just once. They can be shared across all shader programs. - - const cameraUniformsGroup = new THREE.UniformsGroup(); - cameraUniformsGroup.setName('ViewData'); - cameraUniformsGroup.add(new THREE.Uniform(camera.projectionMatrix)); // projection matrix - cameraUniformsGroup.add(new THREE.Uniform(camera.matrixWorldInverse)); // view matrix - - const lightingUniformsGroup = new THREE.UniformsGroup(); - lightingUniformsGroup.setName('LightingData'); - lightingUniformsGroup.add(new THREE.Uniform(new THREE.Vector3(0, 0, 10))); // light position - lightingUniformsGroup.add(new THREE.Uniform(new THREE.Color(0x7c7c7c))); // ambient color - lightingUniformsGroup.add(new THREE.Uniform(new THREE.Color(0xd5d5d5))); // diffuse color - lightingUniformsGroup.add(new THREE.Uniform(new THREE.Color(0xe7e7e7))); // specular color - lightingUniformsGroup.add(new THREE.Uniform(64)); // shininess - - // materials - - const material1 = new THREE.RawShaderMaterial({ - uniforms: { - modelMatrix: { value: null }, - normalMatrix: { value: null }, - color: { value: null }, - }, - vertexShader: document.getElementById('vertexShader1').textContent, - fragmentShader: document.getElementById('fragmentShader1').textContent, - glslVersion: THREE.GLSL3, - }); - - const material2 = new THREE.RawShaderMaterial({ - uniforms: { - modelMatrix: { value: null }, - diffuseMap: { value: null }, - }, - vertexShader: document.getElementById('vertexShader2').textContent, - fragmentShader: document.getElementById('fragmentShader2').textContent, - glslVersion: THREE.GLSL3, - }); - - // meshes - - for (let i = 0; i < 200; i++) { - let mesh; - - if (i % 2 === 0) { - mesh = new THREE.Mesh(geometry1, material1.clone()); - - mesh.material.uniformsGroups = [cameraUniformsGroup, lightingUniformsGroup]; - mesh.material.uniforms.modelMatrix.value = mesh.matrixWorld; - mesh.material.uniforms.normalMatrix.value = mesh.normalMatrix; - mesh.material.uniforms.color.value = new THREE.Color(0xffffff * Math.random()); - } else { - mesh = new THREE.Mesh(geometry2, material2.clone()); - - mesh.material.uniformsGroups = [cameraUniformsGroup, lightingUniformsGroup]; - mesh.material.uniforms.modelMatrix.value = mesh.matrixWorld; - mesh.material.uniforms.diffuseMap.value = texture; - } - - scene.add(mesh); - - const s = 1 + Math.random() * 0.5; - - mesh.scale.x = s; - mesh.scale.y = s; - mesh.scale.z = s; - - mesh.rotation.x = Math.random() * Math.PI; - mesh.rotation.y = Math.random() * Math.PI; - mesh.rotation.z = Math.random() * Math.PI; - - mesh.position.x = Math.random() * 40 - 20; - mesh.position.y = Math.random() * 40 - 20; - mesh.position.z = Math.random() * 20 - 10; - } - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize, false); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const delta = clock.getDelta(); - - scene.traverse(function (child) { - if (child.isMesh) { - child.rotation.x += delta * 0.5; - child.rotation.y += delta * 0.3; - } - }); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_ubo_arrays.ts b/examples-testing/examples/webgl_ubo_arrays.ts deleted file mode 100644 index d846e1443..000000000 --- a/examples-testing/examples/webgl_ubo_arrays.ts +++ /dev/null @@ -1,171 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, clock, stats; - -let lightingUniformsGroup, lightCenters; - -const container = document.getElementById('container'); - -const pointLightsMax = 300; - -const api = { - count: 200, -}; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(0, 50, 50); - - scene = new THREE.Scene(); - camera.lookAt(scene.position); - - clock = new THREE.Clock(); - - // geometry - - const geometry = new THREE.SphereGeometry(); - - // uniforms groups - - lightingUniformsGroup = new THREE.UniformsGroup(); - lightingUniformsGroup.setName('LightingData'); - - const data = []; - const dataColors = []; - lightCenters = []; - - for (let i = 0; i < pointLightsMax; i++) { - const col = new THREE.Color(0xffffff * Math.random()).toArray(); - const x = Math.random() * 50 - 25; - const z = Math.random() * 50 - 25; - - data.push(new THREE.Uniform(new THREE.Vector4(x, 1, z, 0))); // light position - dataColors.push(new THREE.Uniform(new THREE.Vector4(col[0], col[1], col[2], 0))); // light color - - // Store the center positions - lightCenters.push({ x, z }); - } - - lightingUniformsGroup.add(data); // light position - lightingUniformsGroup.add(dataColors); // light position - lightingUniformsGroup.add(new THREE.Uniform(pointLightsMax)); // light position - - const cameraUniformsGroup = new THREE.UniformsGroup(); - cameraUniformsGroup.setName('ViewData'); - cameraUniformsGroup.add(new THREE.Uniform(camera.projectionMatrix)); // projection matrix - cameraUniformsGroup.add(new THREE.Uniform(camera.matrixWorldInverse)); // view matrix - - const material = new THREE.RawShaderMaterial({ - uniforms: { - modelMatrix: { value: null }, - normalMatrix: { value: null }, - }, - // uniformsGroups: [ cameraUniformsGroup, lightingUniformsGroup ], - name: 'Box', - defines: { - POINTLIGHTS_MAX: pointLightsMax, - }, - vertexShader: document.getElementById('vertexShader').textContent, - fragmentShader: document.getElementById('fragmentShader').textContent, - glslVersion: THREE.GLSL3, - }); - - const plane = new THREE.Mesh(new THREE.PlaneGeometry(100, 100), material.clone()); - plane.material.uniformsGroups = [cameraUniformsGroup, lightingUniformsGroup]; - plane.material.uniforms.modelMatrix.value = plane.matrixWorld; - plane.material.uniforms.normalMatrix.value = plane.normalMatrix; - plane.rotation.x = -Math.PI / 2; - plane.position.y = -1; - scene.add(plane); - - // meshes - const gridSize = { x: 10, y: 1, z: 10 }; - const spacing = 6; - - for (let i = 0; i < gridSize.x; i++) { - for (let j = 0; j < gridSize.y; j++) { - for (let k = 0; k < gridSize.z; k++) { - const mesh = new THREE.Mesh(geometry, material.clone()); - mesh.name = 'Sphere'; - mesh.material.uniformsGroups = [cameraUniformsGroup, lightingUniformsGroup]; - mesh.material.uniforms.modelMatrix.value = mesh.matrixWorld; - mesh.material.uniforms.normalMatrix.value = mesh.normalMatrix; - scene.add(mesh); - - mesh.position.x = i * spacing - (gridSize.x * spacing) / 2; - mesh.position.y = 0; - mesh.position.z = k * spacing - (gridSize.z * spacing) / 2; - } - } - } - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize, false); - - // controls - - const controls = new OrbitControls(camera, renderer.domElement); - controls.enablePan = false; - - // stats - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // gui - const gui = new GUI(); - gui.add(api, 'count', 1, pointLightsMax) - .step(1) - .onChange(function () { - lightingUniformsGroup.uniforms[2].value = api.count; - }); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const elapsedTime = clock.getElapsedTime(); - - const lights = lightingUniformsGroup.uniforms[0]; - - // Parameters for circular movement - const radius = 5; // Smaller radius for individual circular movements - const speed = 0.5; // Speed of rotation - - // Update each light's position - for (let i = 0; i < lights.length; i++) { - const light = lights[i]; - const center = lightCenters[i]; - - // Calculate circular movement around the light's center - const angle = speed * elapsedTime + i * 0.5; // Phase difference for each light - const x = center.x + Math.sin(angle) * radius; - const z = center.z + Math.cos(angle) * radius; - - // Update the light's position - light.value.set(x, 1, z, 0); - } - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgl_video_kinect.ts b/examples-testing/examples/webgl_video_kinect.ts deleted file mode 100644 index 4f0e2f113..000000000 --- a/examples-testing/examples/webgl_video_kinect.ts +++ /dev/null @@ -1,113 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let scene, camera, renderer; -let geometry, mesh, material; -let mouse, center; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - const info = document.createElement('div'); - info.id = 'info'; - info.innerHTML = 'three.js - kinect'; - document.body.appendChild(info); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.set(0, 0, 500); - - scene = new THREE.Scene(); - center = new THREE.Vector3(); - center.z = -1000; - - const video = document.getElementById('video'); - - const texture = new THREE.VideoTexture(video); - texture.minFilter = THREE.NearestFilter; - - const width = 640, - height = 480; - const nearClipping = 850, - farClipping = 4000; - - geometry = new THREE.BufferGeometry(); - - const vertices = new Float32Array(width * height * 3); - - for (let i = 0, j = 0, l = vertices.length; i < l; i += 3, j++) { - vertices[i] = j % width; - vertices[i + 1] = Math.floor(j / width); - } - - geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); - - material = new THREE.ShaderMaterial({ - uniforms: { - map: { value: texture }, - width: { value: width }, - height: { value: height }, - nearClipping: { value: nearClipping }, - farClipping: { value: farClipping }, - - pointSize: { value: 2 }, - zOffset: { value: 1000 }, - }, - vertexShader: document.getElementById('vs').textContent, - fragmentShader: document.getElementById('fs').textContent, - blending: THREE.AdditiveBlending, - depthTest: false, - depthWrite: false, - transparent: true, - }); - - mesh = new THREE.Points(geometry, material); - scene.add(mesh); - - const gui = new GUI(); - gui.add(material.uniforms.nearClipping, 'value', 1, 10000, 1.0).name('nearClipping'); - gui.add(material.uniforms.farClipping, 'value', 1, 10000, 1.0).name('farClipping'); - gui.add(material.uniforms.pointSize, 'value', 1, 10, 1.0).name('pointSize'); - gui.add(material.uniforms.zOffset, 'value', 0, 4000, 1.0).name('zOffset'); - - video.play(); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - mouse = new THREE.Vector3(0, 0, 1); - - document.addEventListener('mousemove', onDocumentMouseMove); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onDocumentMouseMove(event) { - mouse.x = (event.clientX - window.innerWidth / 2) * 8; - mouse.y = (event.clientY - window.innerHeight / 2) * 8; -} - -function animate() { - camera.position.x += (mouse.x - camera.position.x) * 0.05; - camera.position.y += (-mouse.y - camera.position.y) * 0.05; - camera.lookAt(center); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_video_panorama_equirectangular.ts b/examples-testing/examples/webgl_video_panorama_equirectangular.ts deleted file mode 100644 index 866eca16a..000000000 --- a/examples-testing/examples/webgl_video_panorama_equirectangular.ts +++ /dev/null @@ -1,95 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer; - -let isUserInteracting = false, - lon = 0, - lat = 0, - phi = 0, - theta = 0, - onPointerDownPointerX = 0, - onPointerDownPointerY = 0, - onPointerDownLon = 0, - onPointerDownLat = 0; - -const distance = 0.5; - -init(); - -function init() { - const container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.25, 10); - - scene = new THREE.Scene(); - - const geometry = new THREE.SphereGeometry(5, 60, 40); - // invert the geometry on the x-axis so that all of the faces point inward - geometry.scale(-1, 1, 1); - - const video = document.getElementById('video'); - video.play(); - - const texture = new THREE.VideoTexture(video); - texture.colorSpace = THREE.SRGBColorSpace; - const material = new THREE.MeshBasicMaterial({ map: texture }); - - const mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - document.addEventListener('pointerdown', onPointerDown); - document.addEventListener('pointermove', onPointerMove); - document.addEventListener('pointerup', onPointerUp); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onPointerDown(event) { - isUserInteracting = true; - - onPointerDownPointerX = event.clientX; - onPointerDownPointerY = event.clientY; - - onPointerDownLon = lon; - onPointerDownLat = lat; -} - -function onPointerMove(event) { - if (isUserInteracting === true) { - lon = (onPointerDownPointerX - event.clientX) * 0.1 + onPointerDownLon; - lat = (onPointerDownPointerY - event.clientY) * 0.1 + onPointerDownLat; - } -} - -function onPointerUp() { - isUserInteracting = false; -} - -function animate() { - lat = Math.max(-85, Math.min(85, lat)); - phi = THREE.MathUtils.degToRad(90 - lat); - theta = THREE.MathUtils.degToRad(lon); - - camera.position.x = distance * Math.sin(phi) * Math.cos(theta); - camera.position.y = distance * Math.cos(phi); - camera.position.z = distance * Math.sin(phi) * Math.sin(theta); - - camera.lookAt(0, 0, 0); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_volume_cloud.ts b/examples-testing/examples/webgl_volume_cloud.ts deleted file mode 100644 index 77dd8de43..000000000 --- a/examples-testing/examples/webgl_volume_cloud.ts +++ /dev/null @@ -1,279 +0,0 @@ -import * as THREE from 'three'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let renderer, scene, camera; -let mesh; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(0, 0, 1.5); - - new OrbitControls(camera, renderer.domElement); - - // Sky - - const canvas = document.createElement('canvas'); - canvas.width = 1; - canvas.height = 32; - - const context = canvas.getContext('2d'); - const gradient = context.createLinearGradient(0, 0, 0, 32); - gradient.addColorStop(0.0, '#014a84'); - gradient.addColorStop(0.5, '#0561a0'); - gradient.addColorStop(1.0, '#437ab6'); - context.fillStyle = gradient; - context.fillRect(0, 0, 1, 32); - - const skyMap = new THREE.CanvasTexture(canvas); - skyMap.colorSpace = THREE.SRGBColorSpace; - - const sky = new THREE.Mesh( - new THREE.SphereGeometry(10), - new THREE.MeshBasicMaterial({ map: skyMap, side: THREE.BackSide }), - ); - scene.add(sky); - - // Texture - - const size = 128; - const data = new Uint8Array(size * size * size); - - let i = 0; - const scale = 0.05; - const perlin = new ImprovedNoise(); - const vector = new THREE.Vector3(); - - for (let z = 0; z < size; z++) { - for (let y = 0; y < size; y++) { - for (let x = 0; x < size; x++) { - const d = - 1.0 - - vector - .set(x, y, z) - .subScalar(size / 2) - .divideScalar(size) - .length(); - data[i] = (128 + 128 * perlin.noise((x * scale) / 1.5, y * scale, (z * scale) / 1.5)) * d * d; - i++; - } - } - } - - const texture = new THREE.Data3DTexture(data, size, size, size); - texture.format = THREE.RedFormat; - texture.minFilter = THREE.LinearFilter; - texture.magFilter = THREE.LinearFilter; - texture.unpackAlignment = 1; - texture.needsUpdate = true; - - // Material - - const vertexShader = /* glsl */ ` - in vec3 position; - - uniform mat4 modelMatrix; - uniform mat4 modelViewMatrix; - uniform mat4 projectionMatrix; - uniform vec3 cameraPos; - - out vec3 vOrigin; - out vec3 vDirection; - - void main() { - vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); - - vOrigin = vec3( inverse( modelMatrix ) * vec4( cameraPos, 1.0 ) ).xyz; - vDirection = position - vOrigin; - - gl_Position = projectionMatrix * mvPosition; - } - `; - - const fragmentShader = /* glsl */ ` - precision highp float; - precision highp sampler3D; - - uniform mat4 modelViewMatrix; - uniform mat4 projectionMatrix; - - in vec3 vOrigin; - in vec3 vDirection; - - out vec4 color; - - uniform vec3 base; - uniform sampler3D map; - - uniform float threshold; - uniform float range; - uniform float opacity; - uniform float steps; - uniform float frame; - - uint wang_hash(uint seed) - { - seed = (seed ^ 61u) ^ (seed >> 16u); - seed *= 9u; - seed = seed ^ (seed >> 4u); - seed *= 0x27d4eb2du; - seed = seed ^ (seed >> 15u); - return seed; - } - - float randomFloat(inout uint seed) - { - return float(wang_hash(seed)) / 4294967296.; - } - - vec2 hitBox( vec3 orig, vec3 dir ) { - const vec3 box_min = vec3( - 0.5 ); - const vec3 box_max = vec3( 0.5 ); - vec3 inv_dir = 1.0 / dir; - vec3 tmin_tmp = ( box_min - orig ) * inv_dir; - vec3 tmax_tmp = ( box_max - orig ) * inv_dir; - vec3 tmin = min( tmin_tmp, tmax_tmp ); - vec3 tmax = max( tmin_tmp, tmax_tmp ); - float t0 = max( tmin.x, max( tmin.y, tmin.z ) ); - float t1 = min( tmax.x, min( tmax.y, tmax.z ) ); - return vec2( t0, t1 ); - } - - float sample1( vec3 p ) { - return texture( map, p ).r; - } - - float shading( vec3 coord ) { - float step = 0.01; - return sample1( coord + vec3( - step ) ) - sample1( coord + vec3( step ) ); - } - - vec4 linearToSRGB( in vec4 value ) { - return vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) * 1.055 - vec3( 0.055 ), value.rgb * 12.92, vec3( lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ) ), value.a ); - } - - void main(){ - vec3 rayDir = normalize( vDirection ); - vec2 bounds = hitBox( vOrigin, rayDir ); - - if ( bounds.x > bounds.y ) discard; - - bounds.x = max( bounds.x, 0.0 ); - - vec3 p = vOrigin + bounds.x * rayDir; - vec3 inc = 1.0 / abs( rayDir ); - float delta = min( inc.x, min( inc.y, inc.z ) ); - delta /= steps; - - // Jitter - - // Nice little seed from - // https://blog.demofox.org/2020/05/25/casual-shadertoy-path-tracing-1-basic-camera-diffuse-emissive/ - uint seed = uint( gl_FragCoord.x ) * uint( 1973 ) + uint( gl_FragCoord.y ) * uint( 9277 ) + uint( frame ) * uint( 26699 ); - vec3 size = vec3( textureSize( map, 0 ) ); - float randNum = randomFloat( seed ) * 2.0 - 1.0; - p += rayDir * randNum * ( 1.0 / size ); - - // - - vec4 ac = vec4( base, 0.0 ); - - for ( float t = bounds.x; t < bounds.y; t += delta ) { - - float d = sample1( p + 0.5 ); - - d = smoothstep( threshold - range, threshold + range, d ) * opacity; - - float col = shading( p + 0.5 ) * 3.0 + ( ( p.x + p.y ) * 0.25 ) + 0.2; - - ac.rgb += ( 1.0 - ac.a ) * d * col; - - ac.a += ( 1.0 - ac.a ) * d; - - if ( ac.a >= 0.95 ) break; - - p += rayDir * delta; - - } - - color = linearToSRGB( ac ); - - if ( color.a == 0.0 ) discard; - - } - `; - - const geometry = new THREE.BoxGeometry(1, 1, 1); - const material = new THREE.RawShaderMaterial({ - glslVersion: THREE.GLSL3, - uniforms: { - base: { value: new THREE.Color(0x798aa0) }, - map: { value: texture }, - cameraPos: { value: new THREE.Vector3() }, - threshold: { value: 0.25 }, - opacity: { value: 0.25 }, - range: { value: 0.1 }, - steps: { value: 100 }, - frame: { value: 0 }, - }, - vertexShader, - fragmentShader, - side: THREE.BackSide, - transparent: true, - }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - const parameters = { - threshold: 0.25, - opacity: 0.25, - range: 0.1, - steps: 100, - }; - - function update() { - material.uniforms.threshold.value = parameters.threshold; - material.uniforms.opacity.value = parameters.opacity; - material.uniforms.range.value = parameters.range; - material.uniforms.steps.value = parameters.steps; - } - - const gui = new GUI(); - gui.add(parameters, 'threshold', 0, 1, 0.01).onChange(update); - gui.add(parameters, 'opacity', 0, 1, 0.01).onChange(update); - gui.add(parameters, 'range', 0, 1, 0.01).onChange(update); - gui.add(parameters, 'steps', 0, 200, 1).onChange(update); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - mesh.material.uniforms.cameraPos.value.copy(camera.position); - mesh.rotation.y = -performance.now() / 7500; - - mesh.material.uniforms.frame.value++; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_volume_instancing.ts b/examples-testing/examples/webgl_volume_instancing.ts deleted file mode 100644 index bf90eeea9..000000000 --- a/examples-testing/examples/webgl_volume_instancing.ts +++ /dev/null @@ -1,192 +0,0 @@ -import * as THREE from 'three'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { VOXLoader, VOXData3DTexture } from 'three/addons/loaders/VOXLoader.js'; - -let renderer, scene, camera, controls, clock; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000); - camera.position.set(0, 0, 4); - - controls = new OrbitControls(camera, renderer.domElement); - controls.autoRotate = true; - controls.autoRotateSpeed = -1.0; - controls.enableDamping = true; - - clock = new THREE.Clock(); - - // Material - - const vertexShader = /* glsl */ ` - in vec3 position; - in mat4 instanceMatrix; - - uniform mat4 modelMatrix; - uniform mat4 modelViewMatrix; - uniform mat4 projectionMatrix; - uniform vec3 cameraPos; - - out vec3 vOrigin; - out vec3 vDirection; - - void main() { - vec4 mvPosition = modelViewMatrix * instanceMatrix * vec4( position, 1.0 ); - - vOrigin = vec3( inverse( instanceMatrix * modelMatrix ) * vec4( cameraPos, 1.0 ) ).xyz; - vDirection = position - vOrigin; - - gl_Position = projectionMatrix * mvPosition; - } - `; - - const fragmentShader = /* glsl */ ` - precision highp float; - precision highp sampler3D; - - uniform mat4 modelViewMatrix; - uniform mat4 projectionMatrix; - - in vec3 vOrigin; - in vec3 vDirection; - - out vec4 color; - - uniform sampler3D map; - - uniform float threshold; - uniform float steps; - - vec2 hitBox( vec3 orig, vec3 dir ) { - const vec3 box_min = vec3( - 0.5 ); - const vec3 box_max = vec3( 0.5 ); - vec3 inv_dir = 1.0 / dir; - vec3 tmin_tmp = ( box_min - orig ) * inv_dir; - vec3 tmax_tmp = ( box_max - orig ) * inv_dir; - vec3 tmin = min( tmin_tmp, tmax_tmp ); - vec3 tmax = max( tmin_tmp, tmax_tmp ); - float t0 = max( tmin.x, max( tmin.y, tmin.z ) ); - float t1 = min( tmax.x, min( tmax.y, tmax.z ) ); - return vec2( t0, t1 ); - } - - float sample1( vec3 p ) { - return texture( map, p ).r; - } - - #define epsilon .0001 - - vec3 normal( vec3 coord ) { - if ( coord.x < epsilon ) return vec3( 1.0, 0.0, 0.0 ); - if ( coord.y < epsilon ) return vec3( 0.0, 1.0, 0.0 ); - if ( coord.z < epsilon ) return vec3( 0.0, 0.0, 1.0 ); - if ( coord.x > 1.0 - epsilon ) return vec3( - 1.0, 0.0, 0.0 ); - if ( coord.y > 1.0 - epsilon ) return vec3( 0.0, - 1.0, 0.0 ); - if ( coord.z > 1.0 - epsilon ) return vec3( 0.0, 0.0, - 1.0 ); - - float step = 0.01; - float x = sample1( coord + vec3( - step, 0.0, 0.0 ) ) - sample1( coord + vec3( step, 0.0, 0.0 ) ); - float y = sample1( coord + vec3( 0.0, - step, 0.0 ) ) - sample1( coord + vec3( 0.0, step, 0.0 ) ); - float z = sample1( coord + vec3( 0.0, 0.0, - step ) ) - sample1( coord + vec3( 0.0, 0.0, step ) ); - - return normalize( vec3( x, y, z ) ); - } - - void main(){ - - vec3 rayDir = normalize( vDirection ); - vec2 bounds = hitBox( vOrigin, rayDir ); - - if ( bounds.x > bounds.y ) discard; - - bounds.x = max( bounds.x, 0.0 ); - - vec3 p = vOrigin + bounds.x * rayDir; - vec3 inc = 1.0 / abs( rayDir ); - float delta = min( inc.x, min( inc.y, inc.z ) ); - delta /= 50.0; - - for ( float t = bounds.x; t < bounds.y; t += delta ) { - - float d = sample1( p + 0.5 ); - - if ( d > 0.5 ) { - - color.rgb = p * 2.0; // normal( p + 0.5 ); // * 0.5 + ( p * 1.5 + 0.25 ); - color.a = 1.; - break; - - } - - p += rayDir * delta; - - } - - if ( color.a == 0.0 ) discard; - - } - `; - - const loader = new VOXLoader(); - loader.load('models/vox/menger.vox', function (chunks) { - for (let i = 0; i < chunks.length; i++) { - const chunk = chunks[i]; - - const geometry = new THREE.BoxGeometry(1, 1, 1); - const material = new THREE.RawShaderMaterial({ - glslVersion: THREE.GLSL3, - uniforms: { - map: { value: new VOXData3DTexture(chunk) }, - cameraPos: { value: new THREE.Vector3() }, - }, - vertexShader, - fragmentShader, - side: THREE.BackSide, - }); - - const mesh = new THREE.InstancedMesh(geometry, material, 50000); - mesh.onBeforeRender = function () { - this.material.uniforms.cameraPos.value.copy(camera.position); - }; - - const transform = new THREE.Object3D(); - - for (let i = 0; i < mesh.count; i++) { - transform.position.random().subScalar(0.5).multiplyScalar(150); - transform.rotation.x = Math.random() * Math.PI; - transform.rotation.y = Math.random() * Math.PI; - transform.rotation.z = Math.random() * Math.PI; - transform.updateMatrix(); - - mesh.setMatrixAt(i, transform.matrix); - } - - scene.add(mesh); - } - }); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const delta = clock.getDelta(); - controls.update(delta); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_volume_perlin.ts b/examples-testing/examples/webgl_volume_perlin.ts deleted file mode 100644 index a98f9a682..000000000 --- a/examples-testing/examples/webgl_volume_perlin.ts +++ /dev/null @@ -1,208 +0,0 @@ -import * as THREE from 'three'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let renderer, scene, camera; -let mesh; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(0, 0, 2); - - new OrbitControls(camera, renderer.domElement); - - // Texture - - const size = 128; - const data = new Uint8Array(size * size * size); - - let i = 0; - const perlin = new ImprovedNoise(); - const vector = new THREE.Vector3(); - - for (let z = 0; z < size; z++) { - for (let y = 0; y < size; y++) { - for (let x = 0; x < size; x++) { - vector.set(x, y, z).divideScalar(size); - - const d = perlin.noise(vector.x * 6.5, vector.y * 6.5, vector.z * 6.5); - - data[i++] = d * 128 + 128; - } - } - } - - const texture = new THREE.Data3DTexture(data, size, size, size); - texture.format = THREE.RedFormat; - texture.minFilter = THREE.LinearFilter; - texture.magFilter = THREE.LinearFilter; - texture.unpackAlignment = 1; - texture.needsUpdate = true; - - // Material - - const vertexShader = /* glsl */ ` - in vec3 position; - - uniform mat4 modelMatrix; - uniform mat4 modelViewMatrix; - uniform mat4 projectionMatrix; - uniform vec3 cameraPos; - - out vec3 vOrigin; - out vec3 vDirection; - - void main() { - vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); - - vOrigin = vec3( inverse( modelMatrix ) * vec4( cameraPos, 1.0 ) ).xyz; - vDirection = position - vOrigin; - - gl_Position = projectionMatrix * mvPosition; - } - `; - - const fragmentShader = /* glsl */ ` - precision highp float; - precision highp sampler3D; - - uniform mat4 modelViewMatrix; - uniform mat4 projectionMatrix; - - in vec3 vOrigin; - in vec3 vDirection; - - out vec4 color; - - uniform sampler3D map; - - uniform float threshold; - uniform float steps; - - vec2 hitBox( vec3 orig, vec3 dir ) { - const vec3 box_min = vec3( - 0.5 ); - const vec3 box_max = vec3( 0.5 ); - vec3 inv_dir = 1.0 / dir; - vec3 tmin_tmp = ( box_min - orig ) * inv_dir; - vec3 tmax_tmp = ( box_max - orig ) * inv_dir; - vec3 tmin = min( tmin_tmp, tmax_tmp ); - vec3 tmax = max( tmin_tmp, tmax_tmp ); - float t0 = max( tmin.x, max( tmin.y, tmin.z ) ); - float t1 = min( tmax.x, min( tmax.y, tmax.z ) ); - return vec2( t0, t1 ); - } - - float sample1( vec3 p ) { - return texture( map, p ).r; - } - - #define epsilon .0001 - - vec3 normal( vec3 coord ) { - if ( coord.x < epsilon ) return vec3( 1.0, 0.0, 0.0 ); - if ( coord.y < epsilon ) return vec3( 0.0, 1.0, 0.0 ); - if ( coord.z < epsilon ) return vec3( 0.0, 0.0, 1.0 ); - if ( coord.x > 1.0 - epsilon ) return vec3( - 1.0, 0.0, 0.0 ); - if ( coord.y > 1.0 - epsilon ) return vec3( 0.0, - 1.0, 0.0 ); - if ( coord.z > 1.0 - epsilon ) return vec3( 0.0, 0.0, - 1.0 ); - - float step = 0.01; - float x = sample1( coord + vec3( - step, 0.0, 0.0 ) ) - sample1( coord + vec3( step, 0.0, 0.0 ) ); - float y = sample1( coord + vec3( 0.0, - step, 0.0 ) ) - sample1( coord + vec3( 0.0, step, 0.0 ) ); - float z = sample1( coord + vec3( 0.0, 0.0, - step ) ) - sample1( coord + vec3( 0.0, 0.0, step ) ); - - return normalize( vec3( x, y, z ) ); - } - - void main(){ - - vec3 rayDir = normalize( vDirection ); - vec2 bounds = hitBox( vOrigin, rayDir ); - - if ( bounds.x > bounds.y ) discard; - - bounds.x = max( bounds.x, 0.0 ); - - vec3 p = vOrigin + bounds.x * rayDir; - vec3 inc = 1.0 / abs( rayDir ); - float delta = min( inc.x, min( inc.y, inc.z ) ); - delta /= steps; - - for ( float t = bounds.x; t < bounds.y; t += delta ) { - - float d = sample1( p + 0.5 ); - - if ( d > threshold ) { - - color.rgb = normal( p + 0.5 ) * 0.5 + ( p * 1.5 + 0.25 ); - color.a = 1.; - break; - - } - - p += rayDir * delta; - - } - - if ( color.a == 0.0 ) discard; - - } - `; - - const geometry = new THREE.BoxGeometry(1, 1, 1); - const material = new THREE.RawShaderMaterial({ - glslVersion: THREE.GLSL3, - uniforms: { - map: { value: texture }, - cameraPos: { value: new THREE.Vector3() }, - threshold: { value: 0.6 }, - steps: { value: 200 }, - }, - vertexShader, - fragmentShader, - side: THREE.BackSide, - }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - const parameters = { threshold: 0.6, steps: 200 }; - - function update() { - material.uniforms.threshold.value = parameters.threshold; - material.uniforms.steps.value = parameters.steps; - } - - const gui = new GUI(); - gui.add(parameters, 'threshold', 0, 1, 0.01).onChange(update); - gui.add(parameters, 'steps', 0, 300, 1).onChange(update); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - mesh.material.uniforms.cameraPos.value.copy(camera.position); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_water.ts b/examples-testing/examples/webgl_water.ts deleted file mode 100644 index 496a5f855..000000000 --- a/examples-testing/examples/webgl_water.ts +++ /dev/null @@ -1,162 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { Water } from 'three/addons/objects/Water2.js'; - -let scene, camera, clock, renderer, water; - -let torusKnot; - -const params = { - color: '#ffffff', - scale: 4, - flowX: 1, - flowY: 1, -}; - -init(); - -function init() { - // scene - - scene = new THREE.Scene(); - - // camera - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200); - camera.position.set(-15, 7, 15); - camera.lookAt(scene.position); - - // clock - - clock = new THREE.Clock(); - - // mesh - - const torusKnotGeometry = new THREE.TorusKnotGeometry(3, 1, 256, 32); - const torusKnotMaterial = new THREE.MeshNormalMaterial(); - - torusKnot = new THREE.Mesh(torusKnotGeometry, torusKnotMaterial); - torusKnot.position.y = 4; - torusKnot.scale.set(0.5, 0.5, 0.5); - scene.add(torusKnot); - - // ground - - const groundGeometry = new THREE.PlaneGeometry(20, 20); - const groundMaterial = new THREE.MeshStandardMaterial({ roughness: 0.8, metalness: 0.4 }); - const ground = new THREE.Mesh(groundGeometry, groundMaterial); - ground.rotation.x = Math.PI * -0.5; - scene.add(ground); - - const textureLoader = new THREE.TextureLoader(); - textureLoader.load('textures/hardwood2_diffuse.jpg', function (map) { - map.wrapS = THREE.RepeatWrapping; - map.wrapT = THREE.RepeatWrapping; - map.anisotropy = 16; - map.repeat.set(4, 4); - map.colorSpace = THREE.SRGBColorSpace; - groundMaterial.map = map; - groundMaterial.needsUpdate = true; - }); - - // water - - const waterGeometry = new THREE.PlaneGeometry(20, 20); - - water = new Water(waterGeometry, { - color: params.color, - scale: params.scale, - flowDirection: new THREE.Vector2(params.flowX, params.flowY), - textureWidth: 1024, - textureHeight: 1024, - }); - - water.position.y = 1; - water.rotation.x = Math.PI * -0.5; - scene.add(water); - - // skybox - - const cubeTextureLoader = new THREE.CubeTextureLoader(); - cubeTextureLoader.setPath('textures/cube/Park2/'); - - const cubeTexture = cubeTextureLoader.load([ - 'posx.jpg', - 'negx.jpg', - 'posy.jpg', - 'negy.jpg', - 'posz.jpg', - 'negz.jpg', - ]); - - scene.background = cubeTexture; - - // light - - const ambientLight = new THREE.AmbientLight(0xe7e7e7, 1.2); - scene.add(ambientLight); - - const directionalLight = new THREE.DirectionalLight(0xffffff, 2); - directionalLight.position.set(-1, 1, 1); - scene.add(directionalLight); - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // gui - - const gui = new GUI(); - - gui.addColor(params, 'color').onChange(function (value) { - water.material.uniforms['color'].value.set(value); - }); - gui.add(params, 'scale', 1, 10).onChange(function (value) { - water.material.uniforms['config'].value.w = value; - }); - gui.add(params, 'flowX', -1, 1) - .step(0.01) - .onChange(function (value) { - water.material.uniforms['flowDirection'].value.x = value; - water.material.uniforms['flowDirection'].value.normalize(); - }); - gui.add(params, 'flowY', -1, 1) - .step(0.01) - .onChange(function (value) { - water.material.uniforms['flowDirection'].value.y = value; - water.material.uniforms['flowDirection'].value.normalize(); - }); - - gui.open(); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 5; - controls.maxDistance = 50; - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const delta = clock.getDelta(); - - torusKnot.rotation.x += delta; - torusKnot.rotation.y += delta * 0.5; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgl_water_flowmap.ts b/examples-testing/examples/webgl_water_flowmap.ts deleted file mode 100644 index d0255e431..000000000 --- a/examples-testing/examples/webgl_water_flowmap.ts +++ /dev/null @@ -1,100 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { Water } from 'three/addons/objects/Water2.js'; - -let scene, camera, renderer, water; - -init(); - -function init() { - // scene - - scene = new THREE.Scene(); - - // camera - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200); - camera.position.set(0, 25, 0); - camera.lookAt(scene.position); - - // ground - - const groundGeometry = new THREE.PlaneGeometry(20, 20, 10, 10); - const groundMaterial = new THREE.MeshBasicMaterial({ color: 0xe7e7e7 }); - const ground = new THREE.Mesh(groundGeometry, groundMaterial); - ground.rotation.x = Math.PI * -0.5; - scene.add(ground); - - const textureLoader = new THREE.TextureLoader(); - textureLoader.load('textures/floors/FloorsCheckerboard_S_Diffuse.jpg', function (map) { - map.wrapS = THREE.RepeatWrapping; - map.wrapT = THREE.RepeatWrapping; - map.anisotropy = 16; - map.repeat.set(4, 4); - map.colorSpace = THREE.SRGBColorSpace; - groundMaterial.map = map; - groundMaterial.needsUpdate = true; - }); - - // water - - const waterGeometry = new THREE.PlaneGeometry(20, 20); - const flowMap = textureLoader.load('textures/water/Water_1_M_Flow.jpg'); - - water = new Water(waterGeometry, { - scale: 2, - textureWidth: 1024, - textureHeight: 1024, - flowMap: flowMap, - }); - - water.position.y = 1; - water.rotation.x = Math.PI * -0.5; - scene.add(water); - - // flow map helper - - const helperGeometry = new THREE.PlaneGeometry(20, 20); - const helperMaterial = new THREE.MeshBasicMaterial({ map: flowMap }); - const helper = new THREE.Mesh(helperGeometry, helperMaterial); - helper.position.y = 1.01; - helper.rotation.x = Math.PI * -0.5; - helper.visible = false; - scene.add(helper); - - // renderer - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - const gui = new GUI(); - gui.add(helper, 'visible').name('Show Flow Map'); - gui.open(); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 5; - controls.maxDistance = 50; - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_backdrop_area.ts b/examples-testing/examples/webgpu_backdrop_area.ts deleted file mode 100644 index 208bb15ee..000000000 --- a/examples-testing/examples/webgpu_backdrop_area.ts +++ /dev/null @@ -1,162 +0,0 @@ -import * as THREE from 'three'; -import { - color, - linearDepth, - viewportLinearDepth, - viewportSharedTexture, - viewportMipTexture, - viewportTopLeft, - checker, - uv, - modelScale, -} from 'three/tsl'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer; -let mixer, clock; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.25, 25); - camera.position.set(3, 2, 3); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x777777); - camera.lookAt(0, 1, 0); - - clock = new THREE.Clock(); - - const light = new THREE.PointLight(0xffffff, 50); - camera.add(light); - scene.add(camera); - - const ambient = new THREE.AmbientLight(0x4466ff, 1); - scene.add(ambient); - - // model - - const loader = new GLTFLoader(); - loader.load('models/gltf/Michelle.glb', function (gltf) { - const object = gltf.scene; - mixer = new THREE.AnimationMixer(object); - - const action = mixer.clipAction(gltf.animations[0]); - action.play(); - - scene.add(object); - }); - - // volume - - // compare depth from viewportLinearDepth with linearDepth() to create a distance field - // viewportLinearDepth return the linear depth of the scene - // linearDepth() returns the linear depth of the mesh - const depthDistance = viewportLinearDepth.distance(linearDepth()); - - const depthAlphaNode = depthDistance.oneMinus().smoothstep(0.9, 2).mul(10).saturate(); - const depthBlurred = viewportMipTexture().bicubic( - depthDistance - .smoothstep(0, 0.6) - .mul(40 * 5) - .clamp(0, 5), - ); - - const blurredBlur = new THREE.MeshBasicNodeMaterial(); - blurredBlur.backdropNode = depthBlurred.add(depthAlphaNode.mix(color(0x0066ff), 0)); - blurredBlur.transparent = true; - blurredBlur.side = THREE.DoubleSide; - - const volumeMaterial = new THREE.MeshBasicNodeMaterial(); - volumeMaterial.colorNode = color(0x0066ff); - volumeMaterial.backdropNode = viewportSharedTexture(); - volumeMaterial.backdropAlphaNode = depthAlphaNode; - volumeMaterial.transparent = true; - volumeMaterial.side = THREE.DoubleSide; - - const depthMaterial = new THREE.MeshBasicNodeMaterial(); - depthMaterial.backdropNode = depthAlphaNode; - depthMaterial.transparent = true; - depthMaterial.side = THREE.DoubleSide; - - const bicubicMaterial = new THREE.MeshBasicNodeMaterial(); - bicubicMaterial.backdropNode = viewportMipTexture().bicubic(5); // @TODO: Move to alpha value [ 0, 1 ] - bicubicMaterial.backdropAlphaNode = checker(uv().mul(3).mul(modelScale.xy)); - bicubicMaterial.opacityNode = bicubicMaterial.backdropAlphaNode; - bicubicMaterial.transparent = true; - bicubicMaterial.side = THREE.DoubleSide; - - const pixelMaterial = new THREE.MeshBasicNodeMaterial(); - pixelMaterial.backdropNode = viewportSharedTexture(viewportTopLeft.mul(100).floor().div(100)); - pixelMaterial.transparent = true; - - // box / floor - - const box = new THREE.Mesh(new THREE.BoxGeometry(2, 2, 2), volumeMaterial); - box.position.set(0, 1, 0); - scene.add(box); - - const floor = new THREE.Mesh( - new THREE.BoxGeometry(1.99, 0.01, 1.99), - new THREE.MeshBasicNodeMaterial({ color: 0x333333 }), - ); - floor.position.set(0, 0, 0); - scene.add(floor); - - // renderer - - renderer = new THREE.WebGPURenderer(/*{ antialias: true }*/); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.LinearToneMapping; - renderer.toneMappingExposure = 0.2; - document.body.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 1, 0); - controls.update(); - - window.addEventListener('resize', onWindowResize); - - // gui - - const materials = { - blurred: blurredBlur, - volume: volumeMaterial, - depth: depthMaterial, - bicubic: bicubicMaterial, - pixel: pixelMaterial, - }; - - const gui = new GUI(); - const options = { material: 'blurred' }; - - box.material = materials[options.material]; - - gui.add(box.scale, 'x', 0.1, 2, 0.01); - gui.add(box.scale, 'z', 0.1, 2, 0.01); - gui.add(options, 'material', Object.keys(materials)).onChange(name => { - box.material = materials[name]; - }); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const delta = clock.getDelta(); - - if (mixer) mixer.update(delta); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_camera_logarithmicdepthbuffer.ts b/examples-testing/examples/webgpu_camera_logarithmicdepthbuffer.ts deleted file mode 100644 index 155276322..000000000 --- a/examples-testing/examples/webgpu_camera_logarithmicdepthbuffer.ts +++ /dev/null @@ -1,245 +0,0 @@ -import * as THREE from 'three'; - -import { FontLoader } from 'three/addons/loaders/FontLoader.js'; -import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; - -import Stats from 'three/addons/libs/stats.module.js'; - -// 1 micrometer to 100 billion light years in one scene, with 1 unit = 1 meter? preposterous! and yet... -const NEAR = 1e-6, - FAR = 1e27; -let SCREEN_WIDTH = window.innerWidth; -let SCREEN_HEIGHT = window.innerHeight; -let screensplit = 0.25, - screensplit_right = 0; -const mouse = [0.5, 0.5]; -let zoompos = -100, - minzoomspeed = 0.015; -let zoomspeed = minzoomspeed; - -let container, border, stats; -const objects = {}; - -// Generate a number of text labels, from 1µm in size up to 100,000,000 light years -// Try to use some descriptive real-world examples of objects at each scale - -const labeldata = [ - { size: 0.01, scale: 0.0001, label: 'microscopic (1µm)' }, // FIXME - triangulating text fails at this size, so we scale instead - { size: 0.01, scale: 0.1, label: 'minuscule (1mm)' }, - { size: 0.01, scale: 1.0, label: 'tiny (1cm)' }, - { size: 1, scale: 1.0, label: 'child-sized (1m)' }, - { size: 10, scale: 1.0, label: 'tree-sized (10m)' }, - { size: 100, scale: 1.0, label: 'building-sized (100m)' }, - { size: 1000, scale: 1.0, label: 'medium (1km)' }, - { size: 10000, scale: 1.0, label: 'city-sized (10km)' }, - { size: 3400000, scale: 1.0, label: 'moon-sized (3,400 Km)' }, - { size: 12000000, scale: 1.0, label: 'planet-sized (12,000 km)' }, - { size: 1400000000, scale: 1.0, label: 'sun-sized (1,400,000 km)' }, - { size: 7.47e12, scale: 1.0, label: 'solar system-sized (50Au)' }, - { size: 9.4605284e15, scale: 1.0, label: 'gargantuan (1 light year)' }, - { size: 3.08567758e16, scale: 1.0, label: 'ludicrous (1 parsec)' }, - { size: 1e19, scale: 1.0, label: 'mind boggling (1000 light years)' }, -]; - -init().then(animate); - -async function init() { - container = document.getElementById('container'); - - const loader = new FontLoader(); - const font = await loader.loadAsync('fonts/helvetiker_regular.typeface.json'); - - const scene = initScene(font); - - // Initialize two copies of the same scene, one with normal z-buffer and one with logarithmic z-buffer - objects.normal = await initView(scene, 'normal', false); - objects.logzbuf = await initView(scene, 'logzbuf', true); - - stats = new Stats(); - container.appendChild(stats.dom); - - // Resize border allows the user to easily compare effects of logarithmic depth buffer over the whole scene - border = document.getElementById('renderer_border'); - border.addEventListener('pointerdown', onBorderPointerDown); - - window.addEventListener('mousemove', onMouseMove); - window.addEventListener('resize', onWindowResize); - window.addEventListener('wheel', onMouseWheel); -} - -async function initView(scene, name, logDepthBuf) { - const framecontainer = document.getElementById('container_' + name); - - const camera = new THREE.PerspectiveCamera(50, (screensplit * SCREEN_WIDTH) / SCREEN_HEIGHT, NEAR, FAR); - scene.add(camera); - - const renderer = new THREE.WebGPURenderer({ antialias: true, logarithmicDepthBuffer: logDepthBuf }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(SCREEN_WIDTH / 2, SCREEN_HEIGHT); - renderer.domElement.style.position = 'relative'; - renderer.domElement.id = 'renderer_' + name; - framecontainer.appendChild(renderer.domElement); - - await renderer.init(); - - return { container: framecontainer, renderer: renderer, scene: scene, camera: camera }; -} - -function initScene(font) { - const scene = new THREE.Scene(); - - scene.add(new THREE.AmbientLight(0x777777)); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(100, 100, 100); - scene.add(light); - - const materialargs = { - color: 0xffffff, - specular: 0x050505, - shininess: 50, - emissive: 0x000000, - }; - - const geometry = new THREE.SphereGeometry(0.5, 24, 12); - - for (let i = 0; i < labeldata.length; i++) { - const scale = labeldata[i].scale || 1; - - const labelgeo = new TextGeometry(labeldata[i].label, { - font: font, - size: labeldata[i].size, - depth: labeldata[i].size / 2, - }); - - labelgeo.computeBoundingSphere(); - - // center text - labelgeo.translate(-labelgeo.boundingSphere.radius, 0, 0); - - materialargs.color = new THREE.Color().setHSL(Math.random(), 0.5, 0.5); - - const material = new THREE.MeshPhongMaterial(materialargs); - - const group = new THREE.Group(); - group.position.z = -labeldata[i].size * scale; - scene.add(group); - - const textmesh = new THREE.Mesh(labelgeo, material); - textmesh.scale.set(scale, scale, scale); - textmesh.position.z = -labeldata[i].size * scale; - textmesh.position.y = (labeldata[i].size / 4) * scale; - group.add(textmesh); - - const dotmesh = new THREE.Mesh(geometry, material); - dotmesh.position.y = (-labeldata[i].size / 4) * scale; - dotmesh.scale.multiplyScalar(labeldata[i].size * scale); - group.add(dotmesh); - } - - return scene; -} - -function updateRendererSizes() { - // Recalculate size for both renderers when screen size or split location changes - - SCREEN_WIDTH = window.innerWidth; - SCREEN_HEIGHT = window.innerHeight; - - screensplit_right = 1 - screensplit; - - objects.normal.renderer.setSize(screensplit * SCREEN_WIDTH, SCREEN_HEIGHT); - objects.normal.camera.aspect = (screensplit * SCREEN_WIDTH) / SCREEN_HEIGHT; - objects.normal.camera.updateProjectionMatrix(); - objects.normal.camera.setViewOffset(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, SCREEN_WIDTH * screensplit, SCREEN_HEIGHT); - objects.normal.container.style.width = screensplit * 100 + '%'; - - objects.logzbuf.renderer.setSize(screensplit_right * SCREEN_WIDTH, SCREEN_HEIGHT); - objects.logzbuf.camera.aspect = (screensplit_right * SCREEN_WIDTH) / SCREEN_HEIGHT; - objects.logzbuf.camera.updateProjectionMatrix(); - objects.logzbuf.camera.setViewOffset( - SCREEN_WIDTH, - SCREEN_HEIGHT, - SCREEN_WIDTH * screensplit, - 0, - SCREEN_WIDTH * screensplit_right, - SCREEN_HEIGHT, - ); - objects.logzbuf.container.style.width = screensplit_right * 100 + '%'; - - border.style.left = screensplit * 100 + '%'; -} - -function animate() { - requestAnimationFrame(animate); - - // Put some limits on zooming - const minzoom = labeldata[0].size * labeldata[0].scale * 1; - const maxzoom = labeldata[labeldata.length - 1].size * labeldata[labeldata.length - 1].scale * 100; - let damping = Math.abs(zoomspeed) > minzoomspeed ? 0.95 : 1.0; - - // Zoom out faster the further out you go - const zoom = THREE.MathUtils.clamp(Math.pow(Math.E, zoompos), minzoom, maxzoom); - zoompos = Math.log(zoom); - - // Slow down quickly at the zoom limits - if ((zoom == minzoom && zoomspeed < 0) || (zoom == maxzoom && zoomspeed > 0)) { - damping = 0.85; - } - - zoompos += zoomspeed; - zoomspeed *= damping; - - objects.normal.camera.position.x = Math.sin(0.5 * Math.PI * (mouse[0] - 0.5)) * zoom; - objects.normal.camera.position.y = Math.sin(0.25 * Math.PI * (mouse[1] - 0.5)) * zoom; - objects.normal.camera.position.z = Math.cos(0.5 * Math.PI * (mouse[0] - 0.5)) * zoom; - objects.normal.camera.lookAt(objects.normal.scene.position); - - // Clone camera settings across both scenes - objects.logzbuf.camera.position.copy(objects.normal.camera.position); - objects.logzbuf.camera.quaternion.copy(objects.normal.camera.quaternion); - - // Update renderer sizes if the split has changed - if (screensplit_right != 1 - screensplit) { - updateRendererSizes(); - } - - objects.normal.renderer.render(objects.normal.scene, objects.normal.camera); - objects.logzbuf.renderer.render(objects.logzbuf.scene, objects.logzbuf.camera); - - stats.update(); -} - -function onWindowResize() { - updateRendererSizes(); -} - -function onBorderPointerDown() { - // activate draggable window resizing bar - window.addEventListener('pointermove', onBorderPointerMove); - window.addEventListener('pointerup', onBorderPointerUp); -} - -function onBorderPointerMove(ev) { - screensplit = Math.max(0, Math.min(1, ev.clientX / window.innerWidth)); -} - -function onBorderPointerUp() { - window.removeEventListener('pointermove', onBorderPointerMove); - window.removeEventListener('pointerup', onBorderPointerUp); -} - -function onMouseMove(ev) { - mouse[0] = ev.clientX / window.innerWidth; - mouse[1] = ev.clientY / window.innerHeight; -} - -function onMouseWheel(ev) { - const amount = ev.deltaY; - if (amount === 0) return; - const dir = amount / Math.abs(amount); - zoomspeed = dir / 10; - - // Slow down default zoom speed after user starts zooming, to give them more control - minzoomspeed = 0.001; -} diff --git a/examples-testing/examples/webgpu_clearcoat.ts b/examples-testing/examples/webgpu_clearcoat.ts deleted file mode 100644 index 0d5b70a2f..000000000 --- a/examples-testing/examples/webgpu_clearcoat.ts +++ /dev/null @@ -1,205 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { HDRCubeTextureLoader } from 'three/addons/loaders/HDRCubeTextureLoader.js'; - -import { FlakesTexture } from 'three/addons/textures/FlakesTexture.js'; - -let container, stats; - -let camera, scene, renderer; - -let particleLight; -let group; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 0.25, 50); - camera.position.z = 10; - - scene = new THREE.Scene(); - - group = new THREE.Group(); - scene.add(group); - - new HDRCubeTextureLoader() - .setPath('textures/cube/pisaHDR/') - .load(['px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr'], function (texture) { - const geometry = new THREE.SphereGeometry(0.8, 64, 32); - - const textureLoader = new THREE.TextureLoader(); - - const diffuse = textureLoader.load('textures/carbon/Carbon.png'); - diffuse.colorSpace = THREE.SRGBColorSpace; - diffuse.wrapS = THREE.RepeatWrapping; - diffuse.wrapT = THREE.RepeatWrapping; - diffuse.repeat.x = 10; - diffuse.repeat.y = 10; - - const normalMap = textureLoader.load('textures/carbon/Carbon_Normal.png'); - normalMap.wrapS = THREE.RepeatWrapping; - normalMap.wrapT = THREE.RepeatWrapping; - normalMap.repeat.x = 10; - normalMap.repeat.y = 10; - - const normalMap2 = textureLoader.load('textures/water/Water_1_M_Normal.jpg'); - - const normalMap3 = new THREE.CanvasTexture(new FlakesTexture()); - normalMap3.wrapS = THREE.RepeatWrapping; - normalMap3.wrapT = THREE.RepeatWrapping; - normalMap3.repeat.x = 10; - normalMap3.repeat.y = 6; - normalMap3.anisotropy = 16; - - const normalMap4 = textureLoader.load('textures/golfball.jpg'); - - const clearcoatNormalMap = textureLoader.load( - 'textures/pbr/Scratched_gold/Scratched_gold_01_1K_Normal.png', - ); - - // car paint - - let material = new THREE.MeshPhysicalMaterial({ - clearcoat: 1.0, - clearcoatRoughness: 0.1, - metalness: 0.9, - roughness: 0.5, - color: 0x0000ff, - normalMap: normalMap3, - normalScale: new THREE.Vector2(0.15, 0.15), - }); - let mesh = new THREE.Mesh(geometry, material); - mesh.position.x = -1; - mesh.position.y = 1; - group.add(mesh); - - // fibers - - material = new THREE.MeshPhysicalMaterial({ - roughness: 0.5, - clearcoat: 1.0, - clearcoatRoughness: 0.1, - map: diffuse, - normalMap: normalMap, - }); - mesh = new THREE.Mesh(geometry, material); - mesh.position.x = 1; - mesh.position.y = 1; - group.add(mesh); - - // golf - - material = new THREE.MeshPhysicalMaterial({ - metalness: 0.0, - roughness: 0.1, - clearcoat: 1.0, - normalMap: normalMap4, - clearcoatNormalMap: clearcoatNormalMap, - - // y scale is negated to compensate for normal map handedness. - clearcoatNormalScale: new THREE.Vector2(2.0, -2.0), - }); - mesh = new THREE.Mesh(geometry, material); - mesh.position.x = -1; - mesh.position.y = -1; - group.add(mesh); - - // clearcoat + normalmap - - material = new THREE.MeshPhysicalMaterial({ - clearcoat: 1.0, - metalness: 1.0, - color: 0xff0000, - normalMap: normalMap2, - normalScale: new THREE.Vector2(0.15, 0.15), - clearcoatNormalMap: clearcoatNormalMap, - - // y scale is negated to compensate for normal map handedness. - clearcoatNormalScale: new THREE.Vector2(2.0, -2.0), - }); - mesh = new THREE.Mesh(geometry, material); - mesh.position.x = 1; - mesh.position.y = -1; - group.add(mesh); - - // - - scene.background = texture; - scene.environment = texture; - }); - - // LIGHTS - - particleLight = new THREE.Mesh( - new THREE.SphereGeometry(0.05, 8, 8), - new THREE.MeshBasicMaterial({ color: 0xffffff }), - ); - scene.add(particleLight); - - particleLight.add(new THREE.PointLight(0xffffff, 30)); - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - // - - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1.25; - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // EVENTS - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 3; - controls.maxDistance = 30; - - window.addEventListener('resize', onWindowResize); -} - -// - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); -} - -// - -function animate() { - render(); - - stats.update(); -} - -function render() { - const timer = Date.now() * 0.00025; - - particleLight.position.x = Math.sin(timer * 7) * 3; - particleLight.position.y = Math.cos(timer * 5) * 4; - particleLight.position.z = Math.cos(timer * 3) * 3; - - for (let i = 0; i < group.children.length; i++) { - const child = group.children[i]; - child.rotation.y += 0.005; - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_clipping.ts b/examples-testing/examples/webgpu_clipping.ts deleted file mode 100644 index e57a7e96c..000000000 --- a/examples-testing/examples/webgpu_clipping.ts +++ /dev/null @@ -1,207 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer, startTime, object, stats; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(36, window.innerWidth / window.innerHeight, 0.25, 16); - - camera.position.set(0, 1.3, 3); - - scene = new THREE.Scene(); - - // Lights - - scene.add(new THREE.AmbientLight(0xcccccc)); - - const spotLight = new THREE.SpotLight(0xffffff, 60); - spotLight.angle = Math.PI / 5; - spotLight.penumbra = 0.2; - spotLight.position.set(2, 3, 3); - spotLight.castShadow = true; - spotLight.shadow.camera.near = 3; - spotLight.shadow.camera.far = 10; - spotLight.shadow.mapSize.width = 2048; - spotLight.shadow.mapSize.height = 2048; - spotLight.shadow.bias = -0.002; - spotLight.shadow.radius = 4; - scene.add(spotLight); - - const dirLight = new THREE.DirectionalLight(0x55505a, 3); - dirLight.position.set(0, 3, 0); - dirLight.castShadow = true; - dirLight.shadow.camera.near = 1; - dirLight.shadow.camera.far = 10; - - dirLight.shadow.camera.right = 1; - dirLight.shadow.camera.left = -1; - dirLight.shadow.camera.top = 1; - dirLight.shadow.camera.bottom = -1; - - dirLight.shadow.mapSize.width = 1024; - dirLight.shadow.mapSize.height = 1024; - scene.add(dirLight); - - // ***** Clipping planes: ***** - - const localPlane = new THREE.Plane(new THREE.Vector3(0, -1, 0), 0.8); - const localPlane2 = new THREE.Plane(new THREE.Vector3(0, 0, -1), 0.1); - const globalPlane = new THREE.Plane(new THREE.Vector3(-1, 0, 0), 0.1); - - // Geometry - - const material = new THREE.MeshPhongNodeMaterial({ - color: 0x80ee10, - shininess: 0, - side: THREE.DoubleSide, - - // ***** Clipping setup (material): ***** - clippingPlanes: [localPlane, localPlane2], - clipShadows: true, - alphaToCoverage: true, - clipIntersection: true, - }); - - const geometry = new THREE.TorusKnotGeometry(0.4, 0.08, 95, 20); - - object = new THREE.Mesh(geometry, material); - object.castShadow = true; - scene.add(object); - - const ground = new THREE.Mesh( - new THREE.PlaneGeometry(9, 9, 1, 1), - new THREE.MeshPhongNodeMaterial({ color: 0xa0adaf, shininess: 150 }), - ); - - ground.rotation.x = -Math.PI / 2; // rotates X/Y to X/Z - ground.receiveShadow = true; - scene.add(ground); - - // Stats - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // Renderer - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.shadowMap.enabled = true; - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - window.addEventListener('resize', onWindowResize); - document.body.appendChild(renderer.domElement); - - // ***** Clipping setup (renderer): ***** - const globalPlanes = [globalPlane]; - const Empty = Object.freeze([]); - - renderer.clippingPlanes = Empty; // GUI sets it to globalPlanes - renderer.localClippingEnabled = true; - - // Controls - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 1, 0); - controls.update(); - - // GUI - - const gui = new GUI(), - props = { - alphaToCoverage: true, - }, - folderLocal = gui.addFolder('Local Clipping'), - propsLocal = { - get Enabled() { - return renderer.localClippingEnabled; - }, - set Enabled(v) { - renderer.localClippingEnabled = v; - }, - - get Shadows() { - return material.clipShadows; - }, - set Shadows(v) { - material.clipShadows = v; - }, - - get Intersection() { - return material.clipIntersection; - }, - - set Intersection(v) { - material.clipIntersection = v; - }, - - get Plane() { - return localPlane.constant; - }, - set Plane(v) { - localPlane.constant = v; - }, - }, - folderGlobal = gui.addFolder('Global Clipping'), - propsGlobal = { - get Enabled() { - return renderer.clippingPlanes !== Empty; - }, - set Enabled(v) { - renderer.clippingPlanes = v ? globalPlanes : Empty; - }, - - get Plane() { - return globalPlane.constant; - }, - set Plane(v) { - globalPlane.constant = v; - }, - }; - - gui.add(props, 'alphaToCoverage').onChange(function (value) { - ground.material.alphaToCoverage = value; - ground.material.needsUpdate = true; - - material.alphaToCoverage = value; - material.needsUpdate = true; - }); - - folderLocal.add(propsLocal, 'Enabled'); - folderLocal.add(propsLocal, 'Shadows'); - folderLocal.add(propsLocal, 'Intersection'); - folderLocal.add(propsLocal, 'Plane', 0.3, 1.25); - - folderGlobal.add(propsGlobal, 'Enabled'); - folderGlobal.add(propsGlobal, 'Plane', -0.4, 3); - - // Start - - startTime = Date.now(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate(currentTime) { - const time = (currentTime - startTime) / 1000; - - object.position.y = 0.8; - object.rotation.x = time * 0.5; - object.rotation.y = time * 0.2; - object.scale.setScalar(Math.cos(time) * 0.125 + 0.875); - - stats.begin(); - renderer.render(scene, camera); - stats.end(); -} diff --git a/examples-testing/examples/webgpu_custom_fog_background.ts b/examples-testing/examples/webgpu_custom_fog_background.ts deleted file mode 100644 index 4a2e6c800..000000000 --- a/examples-testing/examples/webgpu_custom_fog_background.ts +++ /dev/null @@ -1,93 +0,0 @@ -import * as THREE from 'three'; -import { pass, color, rangeFog } from 'three/tsl'; - -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -let camera, scene, renderer; -let postProcessing; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); - camera.position.set(-1.8, 0.6, 2.7); - - scene = new THREE.Scene(); - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - //renderer.toneMapping = THREE.ACESFilmicToneMapping; // apply tone mapping in post processing - container.appendChild(renderer.domElement); - - // post processing - - // render scene pass ( the same of css ) - const scenePass = pass(scene, camera); - const scenePassViewZ = scenePass.getViewZNode(); - - // background color - const backgroundColor = color(0x0066ff); - - // get fog factor from scene pass context - // equivalent to: scene.fog = new THREE.Fog( 0x0066ff, 2.7, 4 ); - const fogFactor = rangeFog(null, 2.7, 4).context({ getViewZ: () => scenePassViewZ }); - - // tone mapping scene pass - const scenePassTM = scenePass.toneMapping(THREE.ACESFilmicToneMapping); - - // mix fog from fog factor and background color - const compose = fogFactor.mix(scenePassTM, backgroundColor); - - postProcessing = new THREE.PostProcessing(renderer); - postProcessing.outputNode = compose; - - // - - new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.environment = texture; - - // model - - const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); - loader.load('DamagedHelmet.gltf', function (gltf) { - scene.add(gltf.scene); - - render(); - }); - }); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 2; - controls.maxDistance = 5; - controls.target.set(0, -0.1, -0.2); - controls.update(); - controls.addEventListener('change', render); // use if there is no animation loop - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -// - -function render() { - postProcessing.renderAsync(); -} diff --git a/examples-testing/examples/webgpu_instancing_morph.ts b/examples-testing/examples/webgpu_instancing_morph.ts deleted file mode 100644 index cfd721721..000000000 --- a/examples-testing/examples/webgpu_instancing_morph.ts +++ /dev/null @@ -1,148 +0,0 @@ -import * as THREE from 'three'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let camera, scene, renderer, stats, mesh, mixer, dummy; - -const offset = 5000; - -const timeOffsets = new Float32Array(1024); - -for (let i = 0; i < 1024; i++) { - timeOffsets[i] = Math.random() * 3; -} - -const clock = new THREE.Clock(true); - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 100, 10000); - - scene = new THREE.Scene(); - - scene.background = new THREE.Color(0x99ddff); - - scene.fog = new THREE.Fog(0x99ddff, 5000, 10000); - - // - - stats = new Stats(); - document.body.appendChild(stats.dom); - - const light = new THREE.DirectionalLight(0xffffff, 1); - - light.position.set(200, 1000, 50); - - light.shadow.mapSize.width = 2048; - light.shadow.mapSize.height = 2048; - light.castShadow = true; - - light.shadow.camera.left = -5000; - light.shadow.camera.right = 5000; - light.shadow.camera.top = 5000; - light.shadow.camera.bottom = -5000; - light.shadow.camera.far = 2000; - - light.shadow.bias = -0.01; - - light.shadow.camera.updateProjectionMatrix(); - - scene.add(light); - - const hemi = new THREE.HemisphereLight(0x99ddff, 0x669933, 1 / 3); - - scene.add(hemi); - - const ground = new THREE.Mesh( - new THREE.PlaneGeometry(1000000, 1000000), - new THREE.MeshStandardMaterial({ color: 0x669933 }), - ); - - ground.rotation.x = -Math.PI / 2; - - ground.receiveShadow = true; - - scene.add(ground); - - const loader = new GLTFLoader(); - - loader.load('models/gltf/Horse.glb', function (glb) { - dummy = glb.scene.children[0]; - - mesh = new THREE.InstancedMesh( - dummy.geometry, - new THREE.MeshStandardNodeMaterial({ - flatShading: true, - }), - 1024, - ); - - mesh.castShadow = true; - - for (let x = 0, i = 0; x < 32; x++) { - for (let y = 0; y < 32; y++) { - dummy.position.set(offset - 300 * x + 200 * Math.random(), 0, offset - 300 * y); - - dummy.updateMatrix(); - - mesh.setMatrixAt(i, dummy.matrix); - - mesh.setColorAt(i, new THREE.Color(`hsl(${Math.random() * 360}, 50%, 66%)`)); - - i++; - } - } - - scene.add(mesh); - - mixer = new THREE.AnimationMixer(glb.scene); - - const action = mixer.clipAction(glb.animations[0]); - - action.play(); - }); - - // renderer - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setAnimationLoop(animate); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - document.body.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const time = clock.getElapsedTime(); - - const r = 3000; - camera.position.set(Math.sin(time / 10) * r, 1500 + 1000 * Math.cos(time / 5), Math.cos(time / 10) * r); - camera.lookAt(0, 0, 0); - - if (mesh) { - for (let i = 0; i < 1024; i++) { - mixer.setTime(time + timeOffsets[i]); - - mesh.setMorphAt(i, dummy); - } - - mesh.morphTexture.needsUpdate = true; - } - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgpu_lights_ies_spotlight.ts b/examples-testing/examples/webgpu_lights_ies_spotlight.ts deleted file mode 100644 index 41b56de88..000000000 --- a/examples-testing/examples/webgpu_lights_ies_spotlight.ts +++ /dev/null @@ -1,117 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from './jsm/controls/OrbitControls.js'; - -import { IESLoader } from 'three/addons/loaders/IESLoader.js'; - -let renderer, scene, camera; -let lights; - -async function init() { - const iesLoader = new IESLoader().setPath('./ies/'); - //iesLoader.type = THREE.UnsignedByteType; // LDR - - const [iesTexture1, iesTexture2, iesTexture3, iesTexture4] = await Promise.all([ - iesLoader.loadAsync('007cfb11e343e2f42e3b476be4ab684e.ies'), - iesLoader.loadAsync('06b4cfdc8805709e767b5e2e904be8ad.ies'), - iesLoader.loadAsync('02a7562c650498ebb301153dbbf59207.ies'), - iesLoader.loadAsync('1a936937a49c63374e6d4fbed9252b29.ies'), - ]); - - // - - scene = new THREE.Scene(); - - // - - const spotLight = new THREE.IESSpotLight(0xff0000, 500); - spotLight.position.set(6.5, 1.5, 6.5); - spotLight.angle = Math.PI / 8; - spotLight.penumbra = 0.7; - spotLight.distance = 20; - spotLight.iesMap = iesTexture1; - scene.add(spotLight); - - // - - const spotLight2 = new THREE.IESSpotLight(0x00ff00, 500); - spotLight2.position.set(6.5, 1.5, -6.5); - spotLight2.angle = Math.PI / 8; - spotLight2.penumbra = 0.7; - spotLight2.distance = 20; - spotLight2.iesMap = iesTexture2; - scene.add(spotLight2); - - // - - const spotLight3 = new THREE.IESSpotLight(0x0000ff, 500); - spotLight3.position.set(-6.5, 1.5, -6.5); - spotLight3.angle = Math.PI / 8; - spotLight3.penumbra = 0.7; - spotLight3.distance = 20; - spotLight3.iesMap = iesTexture3; - scene.add(spotLight3); - - // - - const spotLight4 = new THREE.IESSpotLight(0xffffff, 500); - spotLight4.position.set(-6.5, 1.5, 6.5); - spotLight4.angle = Math.PI / 8; - spotLight4.penumbra = 0.7; - spotLight4.distance = 20; - spotLight4.iesMap = iesTexture4; - scene.add(spotLight4); - - // - - lights = [spotLight, spotLight2, spotLight3, spotLight4]; - - // - - const material = new THREE.MeshPhongMaterial({ color: 0x808080 /*, dithering: true*/ }); - - const geometry = new THREE.PlaneGeometry(200, 200); - - const mesh = new THREE.Mesh(geometry, material); - mesh.rotation.x = -Math.PI * 0.5; - scene.add(mesh); - - // - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(render); - document.body.appendChild(renderer.domElement); - - camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(16, 4, 1); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 2; - controls.maxDistance = 50; - controls.enablePan = false; - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function render(time) { - time = (time / 1000) * 2.0; - - for (let i = 0; i < lights.length; i++) { - lights[i].position.y = Math.sin(time + i) + 0.97; - } - - renderer.render(scene, camera); -} - -init(); diff --git a/examples-testing/examples/webgpu_lights_rectarealight.ts b/examples-testing/examples/webgpu_lights_rectarealight.ts deleted file mode 100644 index 5638c9029..000000000 --- a/examples-testing/examples/webgpu_lights_rectarealight.ts +++ /dev/null @@ -1,79 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { RectAreaLightHelper } from 'three/addons/helpers/RectAreaLightHelper.js'; -import { RectAreaLightTexturesLib } from 'three/addons/lights/RectAreaLightTexturesLib.js'; - -let renderer, scene, camera; -let stats, meshKnot; - -init(); - -function init() { - THREE.RectAreaLightNode.setLTC(RectAreaLightTexturesLib.init()); - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animation); - document.body.appendChild(renderer.domElement); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.set(0, 5, -15); - - scene = new THREE.Scene(); - - const rectLight1 = new THREE.RectAreaLight(0xff0000, 5, 4, 10); - rectLight1.position.set(-5, 5, 5); - scene.add(rectLight1); - - const rectLight2 = new THREE.RectAreaLight(0x00ff00, 5, 4, 10); - rectLight2.position.set(0, 5, 5); - scene.add(rectLight2); - - const rectLight3 = new THREE.RectAreaLight(0x0000ff, 5, 4, 10); - rectLight3.position.set(5, 5, 5); - scene.add(rectLight3); - - scene.add(new RectAreaLightHelper(rectLight1)); - scene.add(new RectAreaLightHelper(rectLight2)); - scene.add(new RectAreaLightHelper(rectLight3)); - - const geoFloor = new THREE.BoxGeometry(2000, 0.1, 2000); - const matStdFloor = new THREE.MeshStandardMaterial({ color: 0xbcbcbc, roughness: 0.1, metalness: 0 }); - const mshStdFloor = new THREE.Mesh(geoFloor, matStdFloor); - scene.add(mshStdFloor); - - const geoKnot = new THREE.TorusKnotGeometry(1.5, 0.5, 200, 16); - const matKnot = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0, metalness: 0 }); - meshKnot = new THREE.Mesh(geoKnot, matKnot); - meshKnot.position.set(0, 5, 0); - scene.add(meshKnot); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.target.copy(meshKnot.position); - controls.update(); - - // - - window.addEventListener('resize', onWindowResize); - - stats = new Stats(); - document.body.appendChild(stats.dom); -} - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); -} - -function animation(time) { - meshKnot.rotation.y = time / 1000; - - renderer.render(scene, camera); - - stats.update(); -} diff --git a/examples-testing/examples/webgpu_loader_gltf.ts b/examples-testing/examples/webgpu_loader_gltf.ts deleted file mode 100644 index 64d1fda4b..000000000 --- a/examples-testing/examples/webgpu_loader_gltf.ts +++ /dev/null @@ -1,71 +0,0 @@ -import * as THREE from 'three'; - -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -let camera, scene, renderer; - -init(); -render(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); - camera.position.set(-1.8, 0.6, 2.7); - - scene = new THREE.Scene(); - - new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - //texture.minFilter = THREE.LinearMipmapLinearFilter; - //texture.generateMipmaps = true; - - scene.background = texture; - scene.environment = texture; - - render(); - - // model - - const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); - loader.load('DamagedHelmet.gltf', function (gltf) { - scene.add(gltf.scene); - - render(); - }); - }); - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - container.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.addEventListener('change', render); // use if there is no animation loop - controls.minDistance = 2; - controls.maxDistance = 10; - controls.target.set(0, 0, -0.2); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -// - -function render() { - renderer.renderAsync(scene, camera); -} diff --git a/examples-testing/examples/webgpu_loader_gltf_anisotropy.ts b/examples-testing/examples/webgpu_loader_gltf_anisotropy.ts deleted file mode 100644 index d100e8c81..000000000 --- a/examples-testing/examples/webgpu_loader_gltf_anisotropy.ts +++ /dev/null @@ -1,65 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -let renderer, scene, camera, controls; - -init(); - -async function init() { - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(render); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1.35; - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.01, 10); - camera.position.set(-0.35, -0.2, 0.35); - - controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, -0.08, 0.11); - controls.minDistance = 0.1; - controls.maxDistance = 2; - controls.addEventListener('change', render); - controls.update(); - - const rgbeLoader = new RGBELoader().setPath('textures/equirectangular/'); - const gltfLoader = new GLTFLoader().setPath('models/gltf/'); - - const [texture, gltf] = await Promise.all([ - rgbeLoader.loadAsync('royal_esplanade_1k.hdr'), - gltfLoader.loadAsync('AnisotropyBarnLamp.glb'), - ]); - - // environment - - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - scene.backgroundBlurriness = 0.5; - scene.environment = texture; - - // model - - scene.add(gltf.scene); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function render() { - renderer.renderAsync(scene, camera); -} diff --git a/examples-testing/examples/webgpu_loader_gltf_compressed.ts b/examples-testing/examples/webgpu_loader_gltf_compressed.ts deleted file mode 100644 index 9405b64ae..000000000 --- a/examples-testing/examples/webgpu_loader_gltf_compressed.ts +++ /dev/null @@ -1,67 +0,0 @@ -import * as THREE from 'three'; - -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js'; -import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer; - -init(); - -async function init() { - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 20); - camera.position.set(2, 2, 2); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xeeeeee); - - //lights - - const light = new THREE.PointLight(0xffffff); - light.power = 1300; - camera.add(light); - scene.add(camera); - - //renderer - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ReinhardToneMapping; - renderer.toneMappingExposure = 1; - document.body.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 3; - controls.maxDistance = 6; - controls.update(); - - const ktx2Loader = await new KTX2Loader().setTranscoderPath('jsm/libs/basis/').detectSupportAsync(renderer); - - const loader = new GLTFLoader(); - loader.setKTX2Loader(ktx2Loader); - loader.setMeshoptDecoder(MeshoptDecoder); - loader.load('models/gltf/coffeemat.glb', function (gltf) { - const gltfScene = gltf.scene; - gltfScene.position.y = -0.8; - gltfScene.scale.setScalar(0.01); - - scene.add(gltfScene); - }); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_loader_gltf_dispersion.ts b/examples-testing/examples/webgpu_loader_gltf_dispersion.ts deleted file mode 100644 index c1f1ecc8f..000000000 --- a/examples-testing/examples/webgpu_loader_gltf_dispersion.ts +++ /dev/null @@ -1,63 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -let camera, scene, renderer; - -init().then(render); - -async function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 5); - camera.position.set(0.1, 0.05, 0.15); - - scene = new THREE.Scene(); - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(render); - renderer.toneMapping = THREE.ReinhardToneMapping; // TODO: Add THREE.NeutralToneMapping; - renderer.toneMappingExposure = 1; - container.appendChild(renderer.domElement); - - const rgbeLoader = await new RGBELoader() - .setPath('textures/equirectangular/') - .loadAsync('pedestrian_overpass_1k.hdr'); - rgbeLoader.mapping = THREE.EquirectangularReflectionMapping; - - scene = new THREE.Scene(); - scene.backgroundBlurriness = 0.5; - scene.environment = rgbeLoader; - scene.background = rgbeLoader; - - const loader = new GLTFLoader(); - const gltf = await loader.loadAsync('models/gltf/DispersionTest.glb'); - - scene.add(gltf.scene); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 0.1; - controls.maxDistance = 10; - controls.target.set(0, 0, 0); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_loader_gltf_iridescence.ts b/examples-testing/examples/webgpu_loader_gltf_iridescence.ts deleted file mode 100644 index f163ea770..000000000 --- a/examples-testing/examples/webgpu_loader_gltf_iridescence.ts +++ /dev/null @@ -1,70 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -let renderer, scene, camera, controls; - -init().catch(function (err) { - console.error(err); -}); - -async function init() { - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setAnimationLoop(render); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - document.body.appendChild(renderer.domElement); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.05, 20); - camera.position.set(0.35, 0.05, 0.35); - - controls = new OrbitControls(camera, renderer.domElement); - controls.autoRotate = true; - controls.autoRotateSpeed = -0.5; - controls.target.set(0, 0.2, 0); - controls.update(); - - const rgbeLoader = new RGBELoader().setPath('textures/equirectangular/'); - - const gltfLoader = new GLTFLoader().setPath('models/gltf/'); - - const [texture, gltf] = await Promise.all([ - rgbeLoader.loadAsync('venice_sunset_1k.hdr'), - gltfLoader.loadAsync('IridescenceLamp.glb'), - ]); - - // environment - - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - scene.environment = texture; - - // model - - scene.add(gltf.scene); - - render(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - render(); -} - -function render() { - controls.update(); - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_loader_gltf_sheen.ts b/examples-testing/examples/webgpu_loader_gltf_sheen.ts deleted file mode 100644 index 788ef2a89..000000000 --- a/examples-testing/examples/webgpu_loader_gltf_sheen.ts +++ /dev/null @@ -1,81 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, controls; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 20); - camera.position.set(-0.75, 0.7, 1.25); - - scene = new THREE.Scene(); - //scene.add( new THREE.DirectionalLight( 0xffffff, 2 ) ); - - // model - - new GLTFLoader().setPath('models/gltf/').load('SheenChair.glb', function (gltf) { - scene.add(gltf.scene); - - const object = gltf.scene.getObjectByName('SheenChair_fabric'); - - const gui = new GUI(); - - gui.add(object.material, 'sheen', 0, 1); - gui.open(); - }); - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1; - container.appendChild(renderer.domElement); - - scene.background = new THREE.Color(0xaaaaaa); - - new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - //scene.backgroundBlurriness = 1; // @TODO: Needs PMREM - scene.environment = texture; - }); - - controls = new OrbitControls(camera, renderer.domElement); - controls.enableDamping = true; - controls.minDistance = 1; - controls.maxDistance = 10; - controls.target.set(0, 0.35, 0); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - controls.update(); // required if damping enabled - - render(); -} - -function render() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_loader_gltf_transmission.ts b/examples-testing/examples/webgpu_loader_gltf_transmission.ts deleted file mode 100644 index 040233262..000000000 --- a/examples-testing/examples/webgpu_loader_gltf_transmission.ts +++ /dev/null @@ -1,80 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; - -let camera, scene, renderer, controls, clock, mixer; - -init(); - -function init() { - clock = new THREE.Clock(); - - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); - camera.position.set(0, 0.4, 0.7); - - scene = new THREE.Scene(); - - new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - scene.backgroundBlurriness = 0.35; - - scene.environment = texture; - - // model - - new GLTFLoader() - .setPath('models/gltf/') - .setDRACOLoader(new DRACOLoader().setDecoderPath('jsm/libs/draco/gltf/')) - .load('IridescentDishWithOlives.glb', function (gltf) { - mixer = new THREE.AnimationMixer(gltf.scene); - mixer.clipAction(gltf.animations[0]).play(); - - scene.add(gltf.scene); - }); - }); - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setAnimationLoop(render); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1; - container.appendChild(renderer.domElement); - - controls = new OrbitControls(camera, renderer.domElement); - controls.autoRotate = true; - controls.autoRotateSpeed = -0.75; - controls.enableDamping = true; - controls.minDistance = 0.5; - controls.maxDistance = 1; - controls.target.set(0, 0.1, 0); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function render() { - if (mixer) mixer.update(clock.getDelta()); - - controls.update(); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_materials_basic.ts b/examples-testing/examples/webgpu_materials_basic.ts deleted file mode 100644 index 0161a9c7b..000000000 --- a/examples-testing/examples/webgpu_materials_basic.ts +++ /dev/null @@ -1,137 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer; - -const spheres = []; - -let mouseX = 0; -let mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -const params = { - color: '#ffffff', - mapping: THREE.CubeReflectionMapping, - refractionRatio: 0.98, - transparent: false, - opacity: 1, -}; - -const mappings = { ReflectionMapping: THREE.CubeReflectionMapping, RefractionMapping: THREE.CubeRefractionMapping }; - -document.addEventListener('mousemove', onDocumentMouseMove); - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 100); - camera.position.z = 3; - - const path = './textures/cube/pisa/'; - const format = '.png'; - const urls = [ - path + 'px' + format, - path + 'nx' + format, - path + 'py' + format, - path + 'ny' + format, - path + 'pz' + format, - path + 'nz' + format, - ]; - - const textureCube = new THREE.CubeTextureLoader().load(urls); - - scene = new THREE.Scene(); - scene.background = textureCube; - - const geometry = new THREE.SphereGeometry(0.1, 32, 16); - const material = new THREE.MeshBasicMaterial({ color: 0xffffff, envMap: textureCube }); - - for (let i = 0; i < 500; i++) { - const mesh = new THREE.Mesh(geometry, material); - - mesh.position.x = Math.random() * 10 - 5; - mesh.position.y = Math.random() * 10 - 5; - mesh.position.z = Math.random() * 10 - 5; - - mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1; - - scene.add(mesh); - - spheres.push(mesh); - } - - // - - renderer = new THREE.WebGPURenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - const gui = new GUI({ width: 300 }); - - gui.addColor(params, 'color').onChange(value => material.color.set(value)); - gui.add(params, 'mapping', mappings).onChange(value => { - textureCube.mapping = value; - material.needsUpdate = true; - }); - gui.add(params, 'refractionRatio') - .min(0.0) - .max(1.0) - .step(0.01) - .onChange(value => (material.refractionRatio = value)); - gui.add(params, 'transparent').onChange(value => { - material.transparent = value; - material.needsUpdate = true; - }); - gui.add(params, 'opacity') - .min(0.0) - .max(1.0) - .step(0.01) - .onChange(value => (material.opacity = value)); - gui.open(); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onDocumentMouseMove(event) { - mouseX = (event.clientX - windowHalfX) / 100; - mouseY = (event.clientY - windowHalfY) / 100; -} - -// - -function animate() { - const timer = 0.0001 * Date.now(); - - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-mouseY - camera.position.y) * 0.05; - - camera.lookAt(scene.position); - - for (let i = 0, il = spheres.length; i < il; i++) { - const sphere = spheres[i]; - - sphere.position.x = 5 * Math.cos(timer + i); - sphere.position.y = 5 * Math.sin(timer + i * 1.1); - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_materials_displacementmap.ts b/examples-testing/examples/webgpu_materials_displacementmap.ts deleted file mode 100644 index 54d26d65e..000000000 --- a/examples-testing/examples/webgpu_materials_displacementmap.ts +++ /dev/null @@ -1,224 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; - -let stats; -let camera, scene, renderer, controls; - -const settings = { - metalness: 1.0, - roughness: 0.4, - ambientIntensity: 0.2, - aoMapIntensity: 1.0, - envMapIntensity: 1.0, - displacementScale: 2.436143, // from original model - normalScale: 1.0, -}; - -let mesh, material; - -let pointLight, ambientLight; - -const height = 500; // of camera frustum - -let r = 0.0; - -init(); -initGui(); - -// Init gui -function initGui() { - const gui = new GUI(); - - gui.add(settings, 'metalness') - .min(0) - .max(1) - .onChange(function (value) { - material.metalness = value; - }); - - gui.add(settings, 'roughness') - .min(0) - .max(1) - .onChange(function (value) { - material.roughness = value; - }); - - gui.add(settings, 'aoMapIntensity') - .min(0) - .max(1) - .onChange(function (value) { - material.aoMapIntensity = value; - }); - - gui.add(settings, 'ambientIntensity') - .min(0) - .max(1) - .onChange(function (value) { - ambientLight.intensity = value; - }); - - gui.add(settings, 'envMapIntensity') - .min(0) - .max(3) - .onChange(function (value) { - material.envMapIntensity = value; - }); - - gui.add(settings, 'displacementScale') - .min(0) - .max(3.0) - .onChange(function (value) { - material.displacementScale = value; - }); - - gui.add(settings, 'normalScale') - .min(-1) - .max(1) - .onChange(function (value) { - material.normalScale.set(1, -1).multiplyScalar(value); - }); -} - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - renderer = new THREE.WebGPURenderer(); - renderer.setAnimationLoop(animate); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - container.appendChild(renderer.domElement); - - // - - scene = new THREE.Scene(); - - const aspect = window.innerWidth / window.innerHeight; - camera = new THREE.OrthographicCamera(-height * aspect, height * aspect, height, -height, 1, 10000); - camera.position.z = 1500; - scene.add(camera); - - controls = new OrbitControls(camera, renderer.domElement); - controls.enableZoom = false; - controls.enableDamping = true; - - // lights - - ambientLight = new THREE.AmbientLight(0xffffff, settings.ambientIntensity); - scene.add(ambientLight); - - pointLight = new THREE.PointLight(0xff0000, 1.5, 0, 0); - pointLight.position.z = 2500; - scene.add(pointLight); - - const pointLight2 = new THREE.PointLight(0xff6666, 3, 0, 0); - camera.add(pointLight2); - - const pointLight3 = new THREE.PointLight(0x0000ff, 1.5, 0, 0); - pointLight3.position.x = -1000; - pointLight3.position.z = 1000; - scene.add(pointLight3); - - // env map - - const path = 'textures/cube/SwedishRoyalCastle/'; - const format = '.jpg'; - const urls = [ - path + 'px' + format, - path + 'nx' + format, - path + 'py' + format, - path + 'ny' + format, - path + 'pz' + format, - path + 'nz' + format, - ]; - - const reflectionCube = new THREE.CubeTextureLoader().load(urls); - - // textures - - const textureLoader = new THREE.TextureLoader(); - const normalMap = textureLoader.load('models/obj/ninja/normal.png'); - const aoMap = textureLoader.load('models/obj/ninja/ao.jpg'); - const displacementMap = textureLoader.load('models/obj/ninja/displacement.jpg'); - - // material - - material = new THREE.MeshStandardNodeMaterial({ - color: 0xc1c1c1, - roughness: settings.roughness, - metalness: settings.metalness, - - normalMap: normalMap, - normalScale: new THREE.Vector2(1, -1), // why does the normal map require negation in this case? - - aoMap: aoMap, - aoMapIntensity: 1, - - displacementMap: displacementMap, - displacementScale: settings.displacementScale, - displacementBias: -0.428408, // from original model - - envMap: reflectionCube, - envMapIntensity: settings.envMapIntensity, - - side: THREE.DoubleSide, - }); - - // - - const loader = new OBJLoader(); - loader.load('models/obj/ninja/ninjaHead_Low.obj', function (group) { - const geometry = group.children[0].geometry; - geometry.center(); - - mesh = new THREE.Mesh(geometry, material); - mesh.scale.multiplyScalar(25); - scene.add(mesh); - }); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const aspect = window.innerWidth / window.innerHeight; - - camera.left = -height * aspect; - camera.right = height * aspect; - camera.top = height; - camera.bottom = -height; - - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - controls.update(); - - stats.begin(); - render(); - stats.end(); -} - -function render() { - pointLight.position.x = 2500 * Math.cos(r); - pointLight.position.z = 2500 * Math.sin(r); - - r += 0.01; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_materials_lightmap.ts b/examples-testing/examples/webgpu_materials_lightmap.ts deleted file mode 100644 index 616645aab..000000000 --- a/examples-testing/examples/webgpu_materials_lightmap.ts +++ /dev/null @@ -1,94 +0,0 @@ -import * as THREE from 'three'; -import { vec4, color, positionLocal, mix } from 'three/tsl'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let container, stats; -let camera, scene, renderer; - -init(); - -async function init() { - const { innerWidth, innerHeight } = window; - - container = document.createElement('div'); - document.body.appendChild(container); - - // CAMERA - - camera = new THREE.PerspectiveCamera(40, innerWidth / innerHeight, 1, 10000); - camera.position.set(700, 200, -500); - - // SCENE - - scene = new THREE.Scene(); - - // LIGHTS - - const light = new THREE.DirectionalLight(0xd5deff); - light.position.x = 300; - light.position.y = 250; - light.position.z = -500; - scene.add(light); - - // SKYDOME - - const topColor = new THREE.Color().copy(light.color); - const bottomColor = new THREE.Color(0xffffff); - const offset = 400; - const exponent = 0.6; - - const h = positionLocal.add(offset).normalize().y; - - const skyMat = new THREE.MeshBasicNodeMaterial(); - skyMat.colorNode = vec4(mix(color(bottomColor), color(topColor), h.max(0.0).pow(exponent)), 1.0); - skyMat.side = THREE.BackSide; - - const sky = new THREE.Mesh(new THREE.SphereGeometry(4000, 32, 15), skyMat); - scene.add(sky); - - // MODEL - - const loader = new THREE.ObjectLoader(); - const object = await loader.loadAsync('models/json/lightmap/lightmap.json'); - scene.add(object); - - // RENDERER - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setAnimationLoop(animate); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(innerWidth, innerHeight); - container.appendChild(renderer.domElement); - - // CONTROLS - - const controls = new OrbitControls(camera, renderer.domElement); - controls.maxPolarAngle = (0.9 * Math.PI) / 2; - controls.enableZoom = false; - - // STATS - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - renderer.render(scene, camera); - stats.update(); -} diff --git a/examples-testing/examples/webgpu_materials_toon.ts b/examples-testing/examples/webgpu_materials_toon.ts deleted file mode 100644 index 217460596..000000000 --- a/examples-testing/examples/webgpu_materials_toon.ts +++ /dev/null @@ -1,148 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { FontLoader } from 'three/addons/loaders/FontLoader.js'; -import { TextGeometry } from 'three/addons/geometries/TextGeometry.js'; - -let container, stats; - -let camera, scene, renderer; -let particleLight; - -const loader = new FontLoader(); -loader.load('fonts/gentilis_regular.typeface.json', function (font) { - init(font); -}); - -function init(font) { - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 2500); - camera.position.set(0.0, 400, 400 * 3.5); - - // - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x444488); - - // - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(render); - container.appendChild(renderer.domElement); - - // Materials - - const cubeWidth = 400; - const numberOfSphersPerSide = 5; - const sphereRadius = (cubeWidth / numberOfSphersPerSide) * 0.8 * 0.5; - const stepSize = 1.0 / numberOfSphersPerSide; - - const geometry = new THREE.SphereGeometry(sphereRadius, 32, 16); - - for (let alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex++) { - const colors = new Uint8Array(alphaIndex + 2); - - for (let c = 0; c <= colors.length; c++) { - colors[c] = (c / colors.length) * 256; - } - - const gradientMap = new THREE.DataTexture(colors, colors.length, 1, THREE.RedFormat); - gradientMap.needsUpdate = true; - - for (let beta = 0; beta <= 1.0; beta += stepSize) { - for (let gamma = 0; gamma <= 1.0; gamma += stepSize) { - // basic monochromatic energy preservation - const diffuseColor = new THREE.Color() - .setHSL(alpha, 0.5, gamma * 0.5 + 0.1) - .multiplyScalar(1 - beta * 0.2); - - const material = new THREE.MeshToonNodeMaterial({ - color: diffuseColor, - gradientMap: gradientMap, - }); - - const mesh = new THREE.Mesh(geometry, material); - - mesh.position.x = alpha * 400 - 200; - mesh.position.y = beta * 400 - 200; - mesh.position.z = gamma * 400 - 200; - - scene.add(mesh); - } - } - } - - function addLabel(name, location) { - const textGeo = new TextGeometry(name, { - font: font, - - size: 20, - depth: 1, - curveSegments: 1, - }); - - const textMaterial = new THREE.MeshBasicNodeMaterial(); - const textMesh = new THREE.Mesh(textGeo, textMaterial); - textMesh.position.copy(location); - scene.add(textMesh); - } - - addLabel('-gradientMap', new THREE.Vector3(-350, 0, 0)); - addLabel('+gradientMap', new THREE.Vector3(350, 0, 0)); - - addLabel('-diffuse', new THREE.Vector3(0, 0, -300)); - addLabel('+diffuse', new THREE.Vector3(0, 0, 300)); - - particleLight = new THREE.Mesh( - new THREE.SphereGeometry(4, 8, 8), - new THREE.MeshBasicNodeMaterial({ color: 0xffffff }), - ); - scene.add(particleLight); - - // Lights - - scene.add(new THREE.AmbientLight(0xc1c1c1, 3)); - - const pointLight = new THREE.PointLight(0xffffff, 2, 800, 0); - particleLight.add(pointLight); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 200; - controls.maxDistance = 2000; - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function render() { - const timer = Date.now() * 0.00025; - - particleLight.position.x = Math.sin(timer * 7) * 300; - particleLight.position.y = Math.cos(timer * 5) * 400; - particleLight.position.z = Math.cos(timer * 3) * 300; - - stats.begin(); - - renderer.render(scene, camera); - - stats.end(); -} diff --git a/examples-testing/examples/webgpu_materials_transmission.ts b/examples-testing/examples/webgpu_materials_transmission.ts deleted file mode 100644 index 0e04ddad9..000000000 --- a/examples-testing/examples/webgpu_materials_transmission.ts +++ /dev/null @@ -1,168 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -const params = { - color: 0xffffff, - transmission: 1, - opacity: 1, - metalness: 0, - roughness: 0, - ior: 1.5, - thickness: 0.01, - specularIntensity: 1, - specularColor: 0xffffff, - envMapIntensity: 1, - lightIntensity: 1, - exposure: 1, -}; - -let camera, scene, renderer; - -let mesh; - -const hdrEquirect = new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function () { - hdrEquirect.mapping = THREE.EquirectangularReflectionMapping; - - init(); - render(); -}); - -function init() { - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(render); - document.body.appendChild(renderer.domElement); - - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = params.exposure; - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 2000); - camera.position.set(0, 0, 120); - - // - - scene.background = hdrEquirect; - - // - - const geometry = new THREE.SphereGeometry(20, 64, 32); - - const texture = new THREE.CanvasTexture(generateTexture()); - texture.magFilter = THREE.NearestFilter; - texture.wrapT = THREE.RepeatWrapping; - texture.wrapS = THREE.RepeatWrapping; - texture.repeat.set(1, 3.5); - - const material = new THREE.MeshPhysicalMaterial({ - color: params.color, - metalness: params.metalness, - roughness: params.roughness, - ior: params.ior, - alphaMap: texture, - envMap: hdrEquirect, - envMapIntensity: params.envMapIntensity, - transmission: params.transmission, // use material.transmission for glass materials - specularIntensity: params.specularIntensity, - specularColor: params.specularColor, - opacity: params.opacity, - side: THREE.DoubleSide, - transparent: true, - }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 10; - controls.maxDistance = 150; - - window.addEventListener('resize', onWindowResize); - - // - - const gui = new GUI(); - - gui.addColor(params, 'color').onChange(function () { - material.color.set(params.color); - }); - - gui.add(params, 'transmission', 0, 1, 0.01).onChange(function () { - material.transmission = params.transmission; - }); - - gui.add(params, 'opacity', 0, 1, 0.01).onChange(function () { - material.opacity = params.opacity; - }); - - gui.add(params, 'metalness', 0, 1, 0.01).onChange(function () { - material.metalness = params.metalness; - }); - - gui.add(params, 'roughness', 0, 1, 0.01).onChange(function () { - material.roughness = params.roughness; - }); - - gui.add(params, 'ior', 1, 2, 0.01).onChange(function () { - material.ior = params.ior; - }); - - gui.add(params, 'thickness', 0, 5, 0.01).onChange(function () { - material.thickness = params.thickness; - }); - - gui.add(params, 'specularIntensity', 0, 1, 0.01).onChange(function () { - material.specularIntensity = params.specularIntensity; - }); - - gui.addColor(params, 'specularColor').onChange(function () { - material.specularColor.set(params.specularColor); - }); - - gui.add(params, 'envMapIntensity', 0, 1, 0.01) - .name('envMap intensity') - .onChange(function () { - material.envMapIntensity = params.envMapIntensity; - }); - - gui.add(params, 'exposure', 0, 1, 0.01).onChange(function () { - renderer.toneMappingExposure = params.exposure; - }); - - gui.open(); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); -} - -// - -function generateTexture() { - const canvas = document.createElement('canvas'); - canvas.width = 2; - canvas.height = 2; - - const context = canvas.getContext('2d'); - context.fillStyle = 'white'; - context.fillRect(0, 1, 2, 1); - - return canvas; -} - -function render() { - renderer.renderAsync(scene, camera); -} diff --git a/examples-testing/examples/webgpu_materials_video.ts b/examples-testing/examples/webgpu_materials_video.ts deleted file mode 100644 index bd84aba0f..000000000 --- a/examples-testing/examples/webgpu_materials_video.ts +++ /dev/null @@ -1,184 +0,0 @@ -import * as THREE from 'three'; - -let container; - -let camera, scene, renderer; - -let video, texture, material, mesh; - -let mouseX = 0; -let mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -let cube_count; - -const meshes = [], - materials = [], - xgrid = 20, - ygrid = 10; - -const startButton = document.getElementById('startButton'); -startButton.addEventListener('click', function () { - init(); -}); - -function init() { - const overlay = document.getElementById('overlay'); - overlay.remove(); - - container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10000); - camera.position.z = 500; - - scene = new THREE.Scene(); - - const light = new THREE.DirectionalLight(0xffffff, 7); - light.position.set(0.5, 1, 1).normalize(); - scene.add(light); - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(render); - container.appendChild(renderer.domElement); - - video = document.getElementById('video'); - video.play(); - video.addEventListener('play', function () { - this.currentTime = 3; - }); - - texture = new THREE.VideoTexture(video); - - // - - let i, j, ox, oy, geometry; - - const ux = 1 / xgrid; - const uy = 1 / ygrid; - - const xsize = 480 / xgrid; - const ysize = 204 / ygrid; - - const parameters = { color: 0xffffff, map: texture }; - - cube_count = 0; - - for (i = 0; i < xgrid; i++) { - for (j = 0; j < ygrid; j++) { - ox = i; - oy = j; - - geometry = new THREE.BoxGeometry(xsize, ysize, xsize); - - change_uvs(geometry, ux, uy, ox, oy); - - materials[cube_count] = new THREE.MeshPhongMaterial(parameters); - - material = materials[cube_count]; - - material.hue = i / xgrid; - material.saturation = 1 - j / ygrid; - - material.color.setHSL(material.hue, material.saturation, 0.5); - - mesh = new THREE.Mesh(geometry, material); - - mesh.position.x = (i - xgrid / 2) * xsize; - mesh.position.y = (j - ygrid / 2) * ysize; - mesh.position.z = 0; - - mesh.scale.x = mesh.scale.y = mesh.scale.z = 1; - - scene.add(mesh); - - mesh.dx = 0.001 * (0.5 - Math.random()); - mesh.dy = 0.001 * (0.5 - Math.random()); - - meshes[cube_count] = mesh; - - cube_count += 1; - } - } - - document.addEventListener('mousemove', onDocumentMouseMove); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function change_uvs(geometry, unitx, unity, offsetx, offsety) { - const uvs = geometry.attributes.uv.array; - - for (let i = 0; i < uvs.length; i += 2) { - uvs[i] = (uvs[i] + offsetx) * unitx; - uvs[i + 1] = (uvs[i + 1] + offsety) * unity; - } -} - -function onDocumentMouseMove(event) { - mouseX = event.clientX - windowHalfX; - mouseY = (event.clientY - windowHalfY) * 0.3; -} - -// - -let h, - counter = 1; - -function render() { - const time = Date.now() * 0.00005; - - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y += (-mouseY - camera.position.y) * 0.05; - - camera.lookAt(scene.position); - - for (let i = 0; i < cube_count; i++) { - material = materials[i]; - - h = ((360 * (material.hue + time)) % 360) / 360; - material.color.setHSL(h, material.saturation, 0.5); - } - - if (counter % 1000 > 200) { - for (let i = 0; i < cube_count; i++) { - mesh = meshes[i]; - - mesh.rotation.x += 10 * mesh.dx; - mesh.rotation.y += 10 * mesh.dy; - - mesh.position.x -= 150 * mesh.dx; - mesh.position.y += 150 * mesh.dy; - mesh.position.z += 300 * mesh.dx; - } - } - - if (counter % 1000 === 0) { - for (let i = 0; i < cube_count; i++) { - mesh = meshes[i]; - - mesh.dx *= -1; - mesh.dy *= -1; - } - } - - counter++; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_mesh_batch.ts b/examples-testing/examples/webgpu_mesh_batch.ts deleted file mode 100644 index a619f430b..000000000 --- a/examples-testing/examples/webgpu_mesh_batch.ts +++ /dev/null @@ -1,271 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { radixSort } from 'three/addons/utils/SortUtils.js'; - -let camera, scene, renderer; -let controls, stats; -let gui; -let geometries, mesh, material; -const ids = []; - -const matrix = new THREE.Matrix4(); - -// - -const position = new THREE.Vector3(); -const rotation = new THREE.Euler(); -const quaternion = new THREE.Quaternion(); -const scale = new THREE.Vector3(); - -// - -const MAX_GEOMETRY_COUNT = 20000; - -const api = { - webgpu: true, - count: 512, - dynamic: 16, - - sortObjects: true, - perObjectFrustumCulled: true, - opacity: 1, - useCustomSort: true, -}; - -init(); - -// - -function randomizeMatrix(matrix) { - position.x = Math.random() * 40 - 20; - position.y = Math.random() * 40 - 20; - position.z = Math.random() * 40 - 20; - - rotation.x = Math.random() * 2 * Math.PI; - rotation.y = Math.random() * 2 * Math.PI; - rotation.z = Math.random() * 2 * Math.PI; - - quaternion.setFromEuler(rotation); - - scale.x = scale.y = scale.z = 0.5 + Math.random() * 0.5; - - return matrix.compose(position, quaternion, scale); -} - -function randomizeRotationSpeed(rotation) { - rotation.x = Math.random() * 0.01; - rotation.y = Math.random() * 0.01; - rotation.z = Math.random() * 0.01; - return rotation; -} - -function initGeometries() { - geometries = [ - new THREE.ConeGeometry(1.0, 2.0), - new THREE.BoxGeometry(2.0, 2.0, 2.0), - new THREE.SphereGeometry(1.0, 16, 8), - ]; -} - -function createMaterial() { - if (!material) { - material = new THREE.MeshNormalNodeMaterial(); - } - - return material; -} - -function cleanup() { - if (mesh) { - mesh.parent.remove(mesh); - - if (mesh.dispose) { - mesh.dispose(); - } - } -} - -function initMesh() { - cleanup(); - initBatchedMesh(); -} - -function initBatchedMesh() { - const geometryCount = api.count; - const vertexCount = geometries.length * 512; - const indexCount = geometries.length * 1024; - - const euler = new THREE.Euler(); - const matrix = new THREE.Matrix4(); - mesh = new THREE.BatchedMesh(geometryCount, vertexCount, indexCount, createMaterial()); - mesh.userData.rotationSpeeds = []; - - // disable full-object frustum culling since all of the objects can be dynamic. - mesh.frustumCulled = false; - - ids.length = 0; - - const geometryIds = [ - mesh.addGeometry(geometries[0]), - mesh.addGeometry(geometries[1]), - mesh.addGeometry(geometries[2]), - ]; - for (let i = 0; i < api.count; i++) { - const id = mesh.addInstance(geometryIds[i % geometryIds.length]); - mesh.setMatrixAt(id, randomizeMatrix(matrix)); - - const rotationMatrix = new THREE.Matrix4(); - rotationMatrix.makeRotationFromEuler(randomizeRotationSpeed(euler)); - mesh.userData.rotationSpeeds.push(rotationMatrix); - - ids.push(id); - } - - scene.add(mesh); -} - -function init(forceWebGL = false) { - if (renderer) { - renderer.dispose(); - controls.dispose(); - document.body.removeChild(stats.dom); - document.body.removeChild(renderer.domElement); - } - - // camera - - const aspect = window.innerWidth / window.innerHeight; - - camera = new THREE.PerspectiveCamera(70, aspect, 1, 100); - camera.position.z = 50; - - // renderer - - renderer = new THREE.WebGPURenderer({ antialias: true, forceWebGL }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - - renderer.setAnimationLoop(animate); - - // scene - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - - if (forceWebGL) { - scene.background = new THREE.Color(0xf10000); - } else { - scene.background = new THREE.Color(0x0000f1); - } - - document.body.appendChild(renderer.domElement); - - initGeometries(); - initMesh(); - - // controls - - controls = new OrbitControls(camera, renderer.domElement); - controls.autoRotate = true; - controls.autoRotateSpeed = 1.0; - - // stats - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // gui - - gui = new GUI(); - gui.add(api, 'webgpu').onChange(() => { - init(!api.webgpu); - }); - gui.add(api, 'count', 1, MAX_GEOMETRY_COUNT).step(1).onChange(initMesh); - gui.add(api, 'dynamic', 0, MAX_GEOMETRY_COUNT).step(1); - - gui.add(api, 'opacity', 0, 1).onChange(v => { - if (v < 1) { - material.transparent = true; - material.depthWrite = false; - } else { - material.transparent = false; - material.depthWrite = true; - } - - material.opacity = v; - material.needsUpdate = true; - }); - gui.add(api, 'sortObjects'); - gui.add(api, 'perObjectFrustumCulled'); - gui.add(api, 'useCustomSort'); - - // listeners - - window.addEventListener('resize', onWindowResize); - - function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); - } - - async function animate() { - animateMeshes(); - - controls.update(); - - if (mesh.isBatchedMesh) { - mesh.sortObjects = api.sortObjects; - mesh.perObjectFrustumCulled = api.perObjectFrustumCulled; - mesh.setCustomSort(api.useCustomSort ? sortFunction : null); - } - - await renderer.renderAsync(scene, camera); - - stats.update(); - } - - function animateMeshes() { - const loopNum = Math.min(api.count, api.dynamic); - - for (let i = 0; i < loopNum; i++) { - const rotationMatrix = mesh.userData.rotationSpeeds[i]; - const id = ids[i]; - - mesh.getMatrixAt(id, matrix); - matrix.multiply(rotationMatrix); - mesh.setMatrixAt(id, matrix); - } - } -} - -// - -function sortFunction(list, camera) { - // initialize options - this._options = this._options || { - get: el => el.z, - aux: new Array(this.maxInstanceCount), - }; - - const options = this._options; - options.reversed = this.material.transparent; - - // convert depth to unsigned 32 bit range - const factor = (2 ** 32 - 1) / camera.far; // UINT32_MAX / max_depth - for (let i = 0, l = list.length; i < l; i++) { - list[i].z *= factor; - } - - // perform a fast-sort using the hybrid radix sort function - radixSort(list, options); -} diff --git a/examples-testing/examples/webgpu_morphtargets.ts b/examples-testing/examples/webgpu_morphtargets.ts deleted file mode 100644 index 9fb7075cb..000000000 --- a/examples-testing/examples/webgpu_morphtargets.ts +++ /dev/null @@ -1,121 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let container, camera, scene, renderer, mesh; - -init(); - -function init() { - container = document.getElementById('container'); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x8fbcd4); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 20); - camera.position.z = 10; - scene.add(camera); - - scene.add(new THREE.AmbientLight(0x8fbcd4, 1.5)); - - const pointLight = new THREE.PointLight(0xffffff, 200); - camera.add(pointLight); - - const geometry = createGeometry(); - - const material = new THREE.MeshPhongMaterial({ - color: 0xff0000, - flatShading: true, - }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - initGUI(); - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(function () { - renderer.render(scene, camera); - }); - container.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.enableZoom = false; - - window.addEventListener('resize', onWindowResize); -} - -function createGeometry() { - const geometry = new THREE.BoxGeometry(2, 2, 2, 32, 32, 32); - - // create an empty array to hold targets for the attribute we want to morph - // morphing positions and normals is supported - geometry.morphAttributes.position = []; - - // the original positions of the cube's vertices - const positionAttribute = geometry.attributes.position; - - // for the first morph target we'll move the cube's vertices onto the surface of a sphere - const spherePositions = []; - - // for the second morph target, we'll twist the cubes vertices - const twistPositions = []; - const direction = new THREE.Vector3(1, 0, 0); - const vertex = new THREE.Vector3(); - - for (let i = 0; i < positionAttribute.count; i++) { - const x = positionAttribute.getX(i); - const y = positionAttribute.getY(i); - const z = positionAttribute.getZ(i); - - spherePositions.push( - x * Math.sqrt(1 - (y * y) / 2 - (z * z) / 2 + (y * y * z * z) / 3), - y * Math.sqrt(1 - (z * z) / 2 - (x * x) / 2 + (z * z * x * x) / 3), - z * Math.sqrt(1 - (x * x) / 2 - (y * y) / 2 + (x * x * y * y) / 3), - ); - - // stretch along the x-axis so we can see the twist better - vertex.set(x * 2, y, z); - - vertex.applyAxisAngle(direction, (Math.PI * x) / 2).toArray(twistPositions, twistPositions.length); - } - - // add the spherical positions as the first morph target - geometry.morphAttributes.position[0] = new THREE.Float32BufferAttribute(spherePositions, 3); - - // add the twisted positions as the second morph target - geometry.morphAttributes.position[1] = new THREE.Float32BufferAttribute(twistPositions, 3); - - return geometry; -} - -function initGUI() { - // Set up dat.GUI to control targets - const params = { - Spherify: 0, - Twist: 0, - }; - const gui = new GUI({ title: 'Morph Targets' }); - - gui.add(params, 'Spherify', 0, 1) - .step(0.01) - .onChange(function (value) { - mesh.morphTargetInfluences[0] = value; - }); - gui.add(params, 'Twist', 0, 1) - .step(0.01) - .onChange(function (value) { - mesh.morphTargetInfluences[1] = value; - }); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} diff --git a/examples-testing/examples/webgpu_morphtargets_face.ts b/examples-testing/examples/webgpu_morphtargets_face.ts deleted file mode 100644 index ea9f86588..000000000 --- a/examples-testing/examples/webgpu_morphtargets_face.ts +++ /dev/null @@ -1,102 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js'; -import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -init(); - -async function init() { - let mixer; - - const clock = new THREE.Clock(); - - const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 20); - camera.position.set(-1.8, 0.8, 3); - - const scene = new THREE.Scene(); - - const renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - document.body.appendChild(renderer.domElement); - - await renderer.init(); - - const environment = new RoomEnvironment(); - const pmremGenerator = new THREE.PMREMGenerator(renderer); - - scene.background = new THREE.Color(0x666666); - scene.environment = pmremGenerator.fromScene(environment).texture; - - const ktx2Loader = await new KTX2Loader().setTranscoderPath('jsm/libs/basis/').detectSupportAsync(renderer); - - new GLTFLoader() - .setKTX2Loader(ktx2Loader) - .setMeshoptDecoder(MeshoptDecoder) - .load('models/gltf/facecap.glb', gltf => { - const mesh = gltf.scene.children[0]; - - scene.add(mesh); - - mixer = new THREE.AnimationMixer(mesh); - - mixer.clipAction(gltf.animations[0]).play(); - - // GUI - - const head = mesh.getObjectByName('mesh_2'); - const influences = head.morphTargetInfluences; - - const gui = new GUI(); - gui.close(); - - for (const [key, value] of Object.entries(head.morphTargetDictionary)) { - gui.add(influences, value, 0, 1, 0.01).name(key.replace('blendShape1.', '')).listen(); - } - }); - - scene.background = new THREE.Color(0x666666); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.enableDamping = true; - controls.minDistance = 2.5; - controls.maxDistance = 5; - controls.minAzimuthAngle = -Math.PI / 2; - controls.maxAzimuthAngle = Math.PI / 2; - controls.maxPolarAngle = Math.PI / 1.8; - controls.target.set(0, 0.15, -0.2); - - const stats = new Stats(); - document.body.appendChild(stats.dom); - - function animate() { - const delta = clock.getDelta(); - - if (mixer) { - mixer.update(delta); - } - - renderer.render(scene, camera); - - controls.update(); - - stats.update(); - } - - window.addEventListener('resize', () => { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - }); -} diff --git a/examples-testing/examples/webgpu_mrt.ts b/examples-testing/examples/webgpu_mrt.ts deleted file mode 100644 index 371c898c7..000000000 --- a/examples-testing/examples/webgpu_mrt.ts +++ /dev/null @@ -1,119 +0,0 @@ -import * as THREE from 'three'; -import { - output, - transformedNormalWorld, - pass, - step, - diffuseColor, - emissive, - viewportTopLeft, - mix, - mrt, - Fn, -} from 'three/tsl'; - -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -let camera, scene, renderer; -let postProcessing; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - // scene - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); - camera.position.set(-1.8, 0.6, 2.7); - - scene = new THREE.Scene(); - - new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - scene.environment = texture; - - // model - - const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); - loader.load('DamagedHelmet.gltf', function (gltf) { - scene.add(gltf.scene); - }); - }); - - // renderer - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(render); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - container.appendChild(renderer.domElement); - - // post processing - - const scenePass = pass(scene, camera, { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter }); - scenePass.setMRT( - mrt({ - output: output, - normal: transformedNormalWorld.directionToColor(), - diffuse: diffuseColor, - emissive: emissive, - }), - ); - - // optimize textures - - const normalTexture = scenePass.getTexture('normal'); - const diffuseTexture = scenePass.getTexture('diffuse'); - const emissiveTexture = scenePass.getTexture('emissive'); - - normalTexture.type = diffuseTexture.type = emissiveTexture.type = THREE.UnsignedByteType; - - // post processing - mrt - - postProcessing = new THREE.PostProcessing(renderer); - postProcessing.outputColorTransform = false; - postProcessing.outputNode = Fn(() => { - const output = scenePass.getTextureNode('output'); // output name is optional here - const normal = scenePass.getTextureNode('normal'); - const diffuse = scenePass.getTextureNode('diffuse'); - const emissive = scenePass.getTextureNode('emissive'); - - const out = mix(output.renderOutput(), output, step(0.2, viewportTopLeft.x)); - const nor = mix(out, normal, step(0.4, viewportTopLeft.x)); - const emi = mix(nor, emissive, step(0.6, viewportTopLeft.x)); - const dif = mix(emi, diffuse, step(0.8, viewportTopLeft.x)); - - return dif; - })(); - - // controls - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 2; - controls.maxDistance = 10; - controls.target.set(0, 0, -0.2); - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function render() { - postProcessing.render(); -} diff --git a/examples-testing/examples/webgpu_multiple_rendertargets_readback.ts b/examples-testing/examples/webgpu_multiple_rendertargets_readback.ts deleted file mode 100644 index 2405b8ad6..000000000 --- a/examples-testing/examples/webgpu_multiple_rendertargets_readback.ts +++ /dev/null @@ -1,156 +0,0 @@ -import * as THREE from 'three'; -import { mix, step, texture, viewportTopLeft, mrt, output, transformedNormalWorld, uv, vec2 } from 'three/tsl'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, torus; -let quadMesh, sceneMRT, renderTarget, readbackTarget, material, readbackMaterial, pixelBuffer, pixelBufferTexture; - -const gui = new GUI(); - -const options = { - selection: 'mrt', -}; - -gui.add(options, 'selection', ['mrt', 'diffuse', 'normal']); - -init(); - -function init() { - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(render); - document.body.appendChild(renderer.domElement); - - // Create a multi render target with Float buffers - - renderTarget = new THREE.RenderTarget( - window.innerWidth * window.devicePixelRatio, - window.innerHeight * window.devicePixelRatio, - { count: 2, minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter }, - ); - - // Name our G-Buffer attachments for debugging - - renderTarget.textures[0].name = 'output'; - renderTarget.textures[1].name = 'normal'; - - // Init readback render target, readback data texture, readback material - // Be careful with the size! 512 is already big. Reading data back from the GPU is computationally intensive - - const size = 512; - - readbackTarget = new THREE.RenderTarget(size, size, { count: 2 }); - - pixelBuffer = new Uint8Array(size ** 2 * 4).fill(0); - pixelBufferTexture = new THREE.DataTexture(pixelBuffer, size, size); - pixelBufferTexture.type = THREE.UnsignedByteType; - pixelBufferTexture.format = THREE.RGBAFormat; - - readbackMaterial = new THREE.MeshBasicNodeMaterial(); - readbackMaterial.colorNode = texture(pixelBufferTexture); - - // MRT - - sceneMRT = mrt({ - output: output, - normal: transformedNormalWorld, - }); - - // Scene - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x222222); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 50); - camera.position.z = 4; - - const loader = new THREE.TextureLoader(); - - const diffuse = loader.load('textures/hardwood2_diffuse.jpg'); - diffuse.colorSpace = THREE.SRGBColorSpace; - diffuse.wrapS = THREE.RepeatWrapping; - diffuse.wrapT = THREE.RepeatWrapping; - - const torusMaterial = new THREE.NodeMaterial(); - torusMaterial.colorNode = texture(diffuse, uv().mul(vec2(10, 4))); - - torus = new THREE.Mesh(new THREE.TorusKnotGeometry(1, 0.3, 128, 32), torusMaterial); - scene.add(torus); - - // Output - - material = new THREE.NodeMaterial(); - material.colorNode = mix( - texture(renderTarget.textures[0]), - texture(renderTarget.textures[1]), - step(0.5, viewportTopLeft.x), - ); - - quadMesh = new THREE.QuadMesh(material); - - // Controls - - new OrbitControls(camera, renderer.domElement); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); - - const dpr = renderer.getPixelRatio(); - renderTarget.setSize(window.innerWidth * dpr, window.innerHeight * dpr); -} - -async function render(time) { - const selection = options.selection; - - torus.rotation.y = (time / 1000) * 0.4; - - const isMRT = selection === 'mrt'; - - // render scene into target - renderer.setMRT(isMRT ? sceneMRT : null); - renderer.setRenderTarget(isMRT ? renderTarget : readbackTarget); - renderer.render(scene, camera); - - // render post FX - renderer.setMRT(null); - renderer.setRenderTarget(null); - - if (isMRT) { - quadMesh.material = material; - } else { - quadMesh.material = readbackMaterial; - - await readback(); - } - - quadMesh.render(renderer); -} - -async function readback() { - const width = readbackTarget.width; - const height = readbackTarget.height; - - const selection = options.selection; - - if (selection === 'diffuse') { - pixelBuffer = await renderer.readRenderTargetPixelsAsync(readbackTarget, 0, 0, width, height, 0); // zero is optional - - pixelBufferTexture.image.data = pixelBuffer; - pixelBufferTexture.needsUpdate = true; - } else if (selection === 'normal') { - pixelBuffer = await renderer.readRenderTargetPixelsAsync(readbackTarget, 0, 0, width, height, 1); - - pixelBufferTexture.image.data = pixelBuffer; - pixelBufferTexture.needsUpdate = true; - } -} diff --git a/examples-testing/examples/webgpu_ocean.ts b/examples-testing/examples/webgpu_ocean.ts deleted file mode 100644 index 9eb9922dd..000000000 --- a/examples-testing/examples/webgpu_ocean.ts +++ /dev/null @@ -1,161 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { WaterMesh } from 'three/addons/objects/WaterMesh.js'; -import { SkyMesh } from 'three/addons/objects/SkyMesh.js'; - -let container, stats; -let camera, scene, renderer; -let controls, water, sun, mesh; - -init(); - -function init() { - container = document.getElementById('container'); - - // - - renderer = new THREE.WebGPURenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 0.5; - container.appendChild(renderer.domElement); - - // - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(55, window.innerWidth / window.innerHeight, 1, 20000); - camera.position.set(30, 30, 100); - - // - - sun = new THREE.Vector3(); - - // Water - - const waterGeometry = new THREE.PlaneGeometry(10000, 10000); - const loader = new THREE.TextureLoader(); - const waterNormals = loader.load('textures/waternormals.jpg'); - waterNormals.wrapS = waterNormals.wrapT = THREE.RepeatWrapping; - - water = new WaterMesh(waterGeometry, { - waterNormals: waterNormals, - sunDirection: new THREE.Vector3(), - sunColor: 0xffffff, - waterColor: 0x001e0f, - distortionScale: 3.7, - }); - - water.rotation.x = -Math.PI / 2; - - scene.add(water); - - // Skybox - - const sky = new SkyMesh(); - sky.scale.setScalar(10000); - scene.add(sky); - - sky.turbidity.value = 10; - sky.rayleigh.value = 2; - sky.mieCoefficient.value = 0.005; - sky.mieDirectionalG.value = 0.8; - - const parameters = { - elevation: 2, - azimuth: 180, - }; - - const pmremGenerator = new THREE.PMREMGenerator(renderer); - const sceneEnv = new THREE.Scene(); - - let renderTarget; - - function updateSun() { - const phi = THREE.MathUtils.degToRad(90 - parameters.elevation); - const theta = THREE.MathUtils.degToRad(parameters.azimuth); - - sun.setFromSphericalCoords(1, phi, theta); - - sky.sunPosition.value.copy(sun); - water.sunDirection.value.copy(sun).normalize(); - - if (renderTarget !== undefined) renderTarget.dispose(); - - sceneEnv.add(sky); - renderTarget = pmremGenerator.fromScene(sceneEnv); - scene.add(sky); - - scene.environment = renderTarget.texture; - } - - renderer.init().then(updateSun); - - // - - const geometry = new THREE.BoxGeometry(30, 30, 30); - const material = new THREE.MeshStandardMaterial({ roughness: 0 }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - controls = new OrbitControls(camera, renderer.domElement); - controls.maxPolarAngle = Math.PI * 0.495; - controls.target.set(0, 10, 0); - controls.minDistance = 40.0; - controls.maxDistance = 200.0; - controls.update(); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // GUI - - const gui = new GUI(); - - const folderSky = gui.addFolder('Sky'); - folderSky.add(parameters, 'elevation', 0, 90, 0.1).onChange(updateSun); - folderSky.add(parameters, 'azimuth', -180, 180, 0.1).onChange(updateSun); - folderSky.open(); - - const folderWater = gui.addFolder('Water'); - folderWater.add(water.distortionScale, 'value', 0, 8, 0.1).name('distortionScale'); - folderWater.add(water.size, 'value', 0.1, 10, 0.1).name('size'); - folderWater.open(); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - const time = performance.now() * 0.001; - - mesh.position.y = Math.sin(time) * 20 + 5; - mesh.rotation.x = time * 0.5; - mesh.rotation.z = time * 0.51; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_parallax_uv.ts b/examples-testing/examples/webgpu_parallax_uv.ts deleted file mode 100644 index c11a18ed1..000000000 --- a/examples-testing/examples/webgpu_parallax_uv.ts +++ /dev/null @@ -1,112 +0,0 @@ -import * as THREE from 'three'; -import { texture, parallaxUV, uv } from 'three/tsl'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer; - -let controls; - -init(); - -async function init() { - // scene - - scene = new THREE.Scene(); - - // camera - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 50); - camera.position.set(10, 14, 10); - - // environment - - const environmentTexture = await new THREE.CubeTextureLoader() - .setPath('./textures/cube/Park2/') - .loadAsync(['posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg']); - - scene.environment = environmentTexture; - scene.background = environmentTexture; - - // textures - - const loader = new THREE.TextureLoader(); - - const topTexture = await loader.loadAsync('textures/ambientcg/Ice002_1K-JPG_Color.jpg'); - topTexture.colorSpace = THREE.SRGBColorSpace; - - const roughnessTexture = await loader.loadAsync('textures/ambientcg/Ice002_1K-JPG_Roughness.jpg'); - roughnessTexture.colorSpace = THREE.NoColorSpace; - - const normalTexture = await loader.loadAsync('textures/ambientcg/Ice002_1K-JPG_NormalGL.jpg'); - normalTexture.colorSpace = THREE.NoColorSpace; - - const displaceTexture = await loader.loadAsync('textures/ambientcg/Ice002_1K-JPG_Displacement.jpg'); - displaceTexture.colorSpace = THREE.NoColorSpace; - - // - - const bottomTexture = await loader.loadAsync('textures/ambientcg/Ice003_1K-JPG_Color.jpg'); - bottomTexture.colorSpace = THREE.SRGBColorSpace; - bottomTexture.wrapS = THREE.RepeatWrapping; - bottomTexture.wrapT = THREE.RepeatWrapping; - - // paralax effect - - const parallaxScale = 0.3; - const offsetUV = texture(displaceTexture).mul(parallaxScale); - - const parallaxUVOffset = parallaxUV(uv(), offsetUV); - const parallaxResult = texture(bottomTexture, parallaxUVOffset); - - const iceNode = texture(topTexture).overlay(parallaxResult); - - // material - - const material = new THREE.MeshStandardNodeMaterial(); - material.colorNode = iceNode.mul(5); // increase the color intensity to 5 ( contrast ) - material.roughnessNode = texture(roughnessTexture); - material.normalMap = normalTexture; - material.metalness = 0; - - const geometry = new THREE.BoxGeometry(10, 10, 10); - - const ground = new THREE.Mesh(geometry, material); - ground.rotateX(-Math.PI / 2); - scene.add(ground); - - // renderer - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ReinhardToneMapping; - renderer.toneMappingExposure = 6; - document.body.appendChild(renderer.domElement); - - // controls - - controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 0, 0); - controls.maxDistance = 40; - controls.minDistance = 10; - controls.autoRotate = true; - controls.autoRotateSpeed = -1; - controls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - controls.update(); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_postprocessing.ts b/examples-testing/examples/webgpu_postprocessing.ts deleted file mode 100644 index d83642f25..000000000 --- a/examples-testing/examples/webgpu_postprocessing.ts +++ /dev/null @@ -1,77 +0,0 @@ -import * as THREE from 'three'; -import { pass } from 'three/tsl'; - -let camera, renderer, postProcessing; -let object; - -init(); - -function init() { - renderer = new THREE.WebGPURenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 400; - - const scene = new THREE.Scene(); - scene.fog = new THREE.Fog(0x000000, 1, 1000); - - object = new THREE.Object3D(); - scene.add(object); - - const geometry = new THREE.SphereGeometry(1, 4, 4); - const material = new THREE.MeshPhongMaterial({ color: 0xffffff, flatShading: true }); - - for (let i = 0; i < 100; i++) { - const mesh = new THREE.Mesh(geometry, material); - mesh.position.set(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5).normalize(); - mesh.position.multiplyScalar(Math.random() * 400); - mesh.rotation.set(Math.random() * 2, Math.random() * 2, Math.random() * 2); - mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50; - object.add(mesh); - } - - scene.add(new THREE.AmbientLight(0xcccccc)); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(1, 1, 1); - scene.add(light); - - // postprocessing - - postProcessing = new THREE.PostProcessing(renderer); - - const scenePass = pass(scene, camera); - const scenePassColor = scenePass.getTextureNode(); - - const dotScreenPass = scenePassColor.dotScreen(); - dotScreenPass.scale.value = 0.3; - - const rgbShiftPass = dotScreenPass.rgbShift(); - rgbShiftPass.amount.value = 0.001; - - postProcessing.outputNode = rgbShiftPass; - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - object.rotation.x += 0.005; - object.rotation.y += 0.01; - - postProcessing.render(); -} diff --git a/examples-testing/examples/webgpu_postprocessing_3dlut.ts b/examples-testing/examples/webgpu_postprocessing_3dlut.ts deleted file mode 100644 index 9d4ffa6e9..000000000 --- a/examples-testing/examples/webgpu_postprocessing_3dlut.ts +++ /dev/null @@ -1,139 +0,0 @@ -import * as THREE from 'three'; -import { pass, texture3D, uniform, renderOutput } from 'three/tsl'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; -import { LUTCubeLoader } from 'three/addons/loaders/LUTCubeLoader.js'; -import { LUT3dlLoader } from 'three/addons/loaders/LUT3dlLoader.js'; -import { LUTImageLoader } from 'three/addons/loaders/LUTImageLoader.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -const params = { - lut: 'Bourbon 64.CUBE', - intensity: 1, -}; - -const lutMap = { - 'Bourbon 64.CUBE': null, - 'Chemical 168.CUBE': null, - 'Clayton 33.CUBE': null, - 'Cubicle 99.CUBE': null, - 'Remy 24.CUBE': null, - 'Presetpro-Cinematic.3dl': null, - NeutralLUT: null, - 'B&WLUT': null, - NightLUT: null, -}; - -let gui; -let camera, scene, renderer; -let postProcessing, lutPass; - -init(); - -async function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); - camera.position.set(-1.8, 0.6, 2.7); - - scene = new THREE.Scene(); - - new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - scene.environment = texture; - - // model - - const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); - loader.load('DamagedHelmet.gltf', function (gltf) { - scene.add(gltf.scene); - }); - }); - - const lutCubeLoader = new LUTCubeLoader(); - const lutImageLoader = new LUTImageLoader(); - const lut3dlLoader = new LUT3dlLoader(); - - for (const name in lutMap) { - if (/\.CUBE$/i.test(name)) { - lutMap[name] = lutCubeLoader.loadAsync('luts/' + name); - } else if (/\LUT$/i.test(name)) { - lutMap[name] = lutImageLoader.loadAsync(`luts/${name}.png`); - } else { - lutMap[name] = lut3dlLoader.loadAsync('luts/' + name); - } - } - - const pendings = Object.values(lutMap); - await Promise.all(pendings); - - for (const name in lutMap) { - lutMap[name] = await lutMap[name]; - } - - renderer = new THREE.WebGPURenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - container.appendChild(renderer.domElement); - - // post processing - - postProcessing = new THREE.PostProcessing(renderer); - - // ignore default output color transform ( toneMapping and outputColorSpace ) - // use renderOutput() for control the sequence - - postProcessing.outputColorTransform = false; - - // scene pass - - const scenePass = pass(scene, camera); - const outputPass = renderOutput(scenePass); - - const lut = lutMap[params.lut]; - lutPass = outputPass.lut3D(texture3D(lut.texture3D), lut.texture3D.image.width, uniform(1)); - - postProcessing.outputNode = lutPass; - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 2; - controls.maxDistance = 10; - controls.target.set(0, 0, -0.2); - controls.update(); - - gui = new GUI(); - gui.add(params, 'lut', Object.keys(lutMap)); - gui.add(params, 'intensity').min(0).max(1); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - lutPass.intensityNode.value = params.intensity; - - if (lutMap[params.lut]) { - const lut = lutMap[params.lut]; - lutPass.lutNode.value = lut.texture3D; - lutPass.size.value = lut.texture3D.image.width; - } - - postProcessing.render(); -} diff --git a/examples-testing/examples/webgpu_postprocessing_afterimage.ts b/examples-testing/examples/webgpu_postprocessing_afterimage.ts deleted file mode 100644 index 4c3a1d66e..000000000 --- a/examples-testing/examples/webgpu_postprocessing_afterimage.ts +++ /dev/null @@ -1,70 +0,0 @@ -import * as THREE from 'three'; -import { pass } from 'three/tsl'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer; -let mesh, postProcessing, combinedPass; - -const params = { - damp: 0.96, -}; - -init(); -createGUI(); - -function init() { - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 400; - - scene = new THREE.Scene(); - scene.fog = new THREE.Fog(0x000000, 1, 1000); - - const geometry = new THREE.TorusKnotGeometry(100, 30, 100, 16); - const material = new THREE.MeshNormalMaterial(); - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // postprocessing - - postProcessing = new THREE.PostProcessing(renderer); - - const scenePass = pass(scene, camera); - const scenePassColor = scenePass.getTextureNode(); - - combinedPass = scenePassColor; - combinedPass = combinedPass.afterImage(params.damp); - - postProcessing.outputNode = combinedPass; - - window.addEventListener('resize', onWindowResize); -} - -function createGUI() { - const gui = new GUI({ title: 'Damp setting' }); - gui.add(combinedPass.damp, 'value', 0, 1).step(0.001); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function render() { - mesh.rotation.x += 0.0075; - mesh.rotation.y += 0.015; - - postProcessing.render(); -} - -function animate() { - render(); -} diff --git a/examples-testing/examples/webgpu_postprocessing_ao.ts b/examples-testing/examples/webgpu_postprocessing_ao.ts deleted file mode 100644 index 432d641a0..000000000 --- a/examples-testing/examples/webgpu_postprocessing_ao.ts +++ /dev/null @@ -1,204 +0,0 @@ -import * as THREE from 'three'; -import { pass, mrt, output, transformedNormalView, texture, ao, denoise } from 'three/tsl'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; -import { SimplexNoise } from 'three/addons/math/SimplexNoise.js'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer, postProcessing, controls, clock, stats, mixer; - -let aoPass, denoisePass, blendPassAO, blendPassDenoise, scenePassColor; - -const params = { - distanceExponent: 1, - distanceFallOff: 1, - radius: 0.25, - scale: 1, - thickness: 1, - denoised: true, - enabled: true, - denoiseRadius: 5, - lumaPhi: 5, - depthPhi: 5, - normalPhi: 5, -}; - -init(); - -async function init() { - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 100); - camera.position.set(5, 2, 8); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xbfe3dd); - - clock = new THREE.Clock(); - - const hdrloader = new RGBELoader(); - const envMap = await hdrloader.loadAsync('textures/equirectangular/quarry_01_1k.hdr'); - envMap.mapping = THREE.EquirectangularReflectionMapping; - - scene.environment = envMap; - - renderer = new THREE.WebGPURenderer(); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - controls = new OrbitControls(camera, renderer.domElement); - controls.target.set(0, 0.5, 0); - controls.update(); - controls.enablePan = false; - controls.enableDamping = true; - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // - - postProcessing = new THREE.PostProcessing(renderer); - - const scenePass = pass(scene, camera); - scenePass.setMRT( - mrt({ - output: output, - normal: transformedNormalView, - }), - ); - - scenePassColor = scenePass.getTextureNode('output'); - const scenePassNormal = scenePass.getTextureNode('normal'); - const scenePassDepth = scenePass.getTextureNode('depth'); - - // ao - - aoPass = ao(scenePassDepth, scenePassNormal, camera); - blendPassAO = aoPass.getTextureNode().mul(scenePassColor); - - // denoise (optional) - - const noiseTexture = texture(generateNoise()); - denoisePass = denoise(aoPass.getTextureNode(), scenePassDepth, scenePassNormal, noiseTexture, camera); - blendPassDenoise = denoisePass.mul(scenePassColor); - - postProcessing.outputNode = blendPassDenoise; - - // - - const dracoLoader = new DRACOLoader(); - dracoLoader.setDecoderPath('jsm/libs/draco/'); - dracoLoader.setDecoderConfig({ type: 'js' }); - const loader = new GLTFLoader(); - loader.setDRACOLoader(dracoLoader); - loader.setPath('models/gltf/'); - - const gltf = await loader.loadAsync('LittlestTokyo.glb'); - - const model = gltf.scene; - model.position.set(1, 1, 0); - model.scale.set(0.01, 0.01, 0.01); - scene.add(model); - - mixer = new THREE.AnimationMixer(model); - mixer.clipAction(gltf.animations[0]).play(); - - window.addEventListener('resize', onWindowResize); - - // - - const gui = new GUI(); - gui.title('AO settings'); - gui.add(params, 'distanceExponent').min(1).max(4).onChange(updateParameters); - gui.add(params, 'distanceFallOff').min(0.01).max(1).onChange(updateParameters); - gui.add(params, 'radius').min(0.01).max(1).onChange(updateParameters); - gui.add(params, 'scale').min(0.01).max(2).onChange(updateParameters); - gui.add(params, 'thickness').min(0.01).max(2).onChange(updateParameters); - gui.add(params, 'denoised').onChange(updatePassChain); - gui.add(params, 'enabled').onChange(updatePassChain); - const folder = gui.addFolder('Denoise settings'); - folder.add(params, 'denoiseRadius').min(0.01).max(10).name('radius').onChange(updateParameters); - folder.add(params, 'lumaPhi').min(0.01).max(10).onChange(updateParameters); - folder.add(params, 'depthPhi').min(0.01).max(10).onChange(updateParameters); - folder.add(params, 'normalPhi').min(0.01).max(10).onChange(updateParameters); -} - -function updatePassChain() { - if (params.enabled === true) { - if (params.denoised === true) { - postProcessing.outputNode = blendPassDenoise; - } else { - postProcessing.outputNode = blendPassAO; - } - } else { - postProcessing.outputNode = scenePassColor; - } - - postProcessing.needsUpdate = true; -} - -function updateParameters() { - aoPass.distanceExponent.value = params.distanceExponent; - aoPass.distanceFallOff.value = params.distanceFallOff; - aoPass.radius.value = params.radius; - aoPass.scale.value = params.scale; - aoPass.thickness.value = params.thickness; - - denoisePass.radius.value = params.denoiseRadius; - denoisePass.lumaPhi.value = params.lumaPhi; - denoisePass.depthPhi.value = params.depthPhi; - denoisePass.normalPhi.value = params.normalPhi; -} - -function generateNoise(size = 64) { - const simplex = new SimplexNoise(); - - const arraySize = size * size * 4; - const data = new Uint8Array(arraySize); - - for (let i = 0; i < size; i++) { - for (let j = 0; j < size; j++) { - const x = i; - const y = j; - - data[(i * size + j) * 4] = (simplex.noise(x, y) * 0.5 + 0.5) * 255; - data[(i * size + j) * 4 + 1] = (simplex.noise(x + size, y) * 0.5 + 0.5) * 255; - data[(i * size + j) * 4 + 2] = (simplex.noise(x, y + size) * 0.5 + 0.5) * 255; - data[(i * size + j) * 4 + 3] = (simplex.noise(x + size, y + size) * 0.5 + 0.5) * 255; - } - } - - const noiseTexture = new THREE.DataTexture(data, size, size); - noiseTexture.wrapS = THREE.RepeatWrapping; - noiseTexture.wrapT = THREE.RepeatWrapping; - noiseTexture.needsUpdate = true; - - return noiseTexture; -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); -} - -function animate() { - const delta = clock.getDelta(); - - if (mixer) { - mixer.update(delta); - } - - controls.update(); - - postProcessing.render(); - stats.update(); -} diff --git a/examples-testing/examples/webgpu_postprocessing_bloom.ts b/examples-testing/examples/webgpu_postprocessing_bloom.ts deleted file mode 100644 index d38a7abb1..000000000 --- a/examples-testing/examples/webgpu_postprocessing_bloom.ts +++ /dev/null @@ -1,127 +0,0 @@ -import * as THREE from 'three'; -import { pass, bloom } from 'three/tsl'; - -import Stats from 'three/addons/libs/stats.module.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -let camera, stats; -let postProcessing, renderer, mixer, clock; - -const params = { - threshold: 0, - strength: 1, - radius: 0, - exposure: 1, -}; - -init(); - -async function init() { - const container = document.getElementById('container'); - - clock = new THREE.Clock(); - - const scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 100); - camera.position.set(-5, 2.5, -3.5); - scene.add(camera); - - scene.add(new THREE.AmbientLight(0xcccccc)); - - const pointLight = new THREE.PointLight(0xffffff, 100); - camera.add(pointLight); - - const loader = new GLTFLoader(); - const gltf = await loader.loadAsync('models/gltf/PrimaryIonDrive.glb'); - - const model = gltf.scene; - scene.add(model); - - mixer = new THREE.AnimationMixer(model); - const clip = gltf.animations[0]; - mixer.clipAction(clip.optimize()).play(); - - // - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ReinhardToneMapping; - container.appendChild(renderer.domElement); - - // - - postProcessing = new THREE.PostProcessing(renderer); - - const scenePass = pass(scene, camera); - const scenePassColor = scenePass.getTextureNode('output'); - - const bloomPass = bloom(scenePassColor); - - postProcessing.outputNode = scenePassColor.add(bloomPass); - - // - - stats = new Stats(); - container.appendChild(stats.dom); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.maxPolarAngle = Math.PI * 0.5; - controls.minDistance = 3; - controls.maxDistance = 8; - - // - - const gui = new GUI(); - - const bloomFolder = gui.addFolder('bloom'); - - bloomFolder.add(params, 'threshold', 0.0, 1.0).onChange(function (value) { - bloomPass.threshold.value = value; - }); - - bloomFolder.add(params, 'strength', 0.0, 3.0).onChange(function (value) { - bloomPass.strength.value = value; - }); - - gui.add(params, 'radius', 0.0, 1.0) - .step(0.01) - .onChange(function (value) { - bloomPass.radius.value = value; - }); - - const toneMappingFolder = gui.addFolder('tone mapping'); - - toneMappingFolder.add(params, 'exposure', 0.1, 2).onChange(function (value) { - renderer.toneMappingExposure = Math.pow(value, 4.0); - }); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); -} - -function animate() { - const delta = clock.getDelta(); - - mixer.update(delta); - - postProcessing.render(); - - stats.update(); -} diff --git a/examples-testing/examples/webgpu_postprocessing_bloom_emissive.ts b/examples-testing/examples/webgpu_postprocessing_bloom_emissive.ts deleted file mode 100644 index 971868360..000000000 --- a/examples-testing/examples/webgpu_postprocessing_bloom_emissive.ts +++ /dev/null @@ -1,101 +0,0 @@ -import * as THREE from 'three'; -import { pass, mrt, output, emissive } from 'three/tsl'; - -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer; -let postProcessing; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - // - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 20); - camera.position.set(-1.8, 0.6, 2.7); - - scene = new THREE.Scene(); - - new RGBELoader().setPath('textures/equirectangular/').load('moonless_golf_1k.hdr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - scene.environment = texture; - - // model - - const loader = new GLTFLoader().setPath('models/gltf/DamagedHelmet/glTF/'); - loader.load('DamagedHelmet.gltf', function (gltf) { - scene.add(gltf.scene); - }); - }); - - // - - renderer = new THREE.WebGPURenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(render); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - container.appendChild(renderer.domElement); - - // - - const scenePass = pass(scene, camera); - scenePass.setMRT( - mrt({ - output, - emissive, - }), - ); - - const outputPass = scenePass.getTextureNode(); - const emissivePass = scenePass.getTextureNode('emissive'); - - const bloomPass = emissivePass.bloom(2.5, 0.5); - - postProcessing = new THREE.PostProcessing(renderer); - postProcessing.outputColorTransform = false; - postProcessing.outputNode = outputPass.add(bloomPass).renderOutput(); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 2; - controls.maxDistance = 10; - controls.target.set(0, 0, -0.2); - - window.addEventListener('resize', onWindowResize); - - // - - const gui = new GUI(); - - const bloomFolder = gui.addFolder('bloom'); - bloomFolder.add(bloomPass.strength, 'value', 0.0, 5.0).name('strength'); - bloomFolder.add(bloomPass.radius, 'value', 0.0, 1.0).name('radius'); - - const toneMappingFolder = gui.addFolder('tone mapping'); - toneMappingFolder.add(renderer, 'toneMappingExposure', 0.1, 2).name('exposure'); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function render() { - postProcessing.render(); -} diff --git a/examples-testing/examples/webgpu_postprocessing_bloom_selective.ts b/examples-testing/examples/webgpu_postprocessing_bloom_selective.ts deleted file mode 100644 index 7140a8ad2..000000000 --- a/examples-testing/examples/webgpu_postprocessing_bloom_selective.ts +++ /dev/null @@ -1,122 +0,0 @@ -import * as THREE from 'three'; -import { pass, mrt, output, float, uniform } from 'three/tsl'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -// scene - -const scene = new THREE.Scene(); - -const camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 200); -camera.position.set(0, 0, 20); -camera.lookAt(0, 0, 0); - -const geometry = new THREE.IcosahedronGeometry(1, 15); - -for (let i = 0; i < 50; i++) { - const color = new THREE.Color(); - color.setHSL(Math.random(), 0.7, Math.random() * 0.2 + 0.05); - - const bloomIntensity = Math.random() > 0.5 ? 1 : 0; - - const material = new THREE.MeshBasicNodeMaterial({ color: color }); - material.mrtNode = mrt({ - bloomIntensity: uniform(bloomIntensity), - }); - - const sphere = new THREE.Mesh(geometry, material); - sphere.position.x = Math.random() * 10 - 5; - sphere.position.y = Math.random() * 10 - 5; - sphere.position.z = Math.random() * 10 - 5; - sphere.position.normalize().multiplyScalar(Math.random() * 4.0 + 2.0); - sphere.scale.setScalar(Math.random() * Math.random() + 0.5); - scene.add(sphere); -} - -// renderer - -const renderer = new THREE.WebGPURenderer(); -renderer.setPixelRatio(window.devicePixelRatio); -renderer.setSize(window.innerWidth, window.innerHeight); -renderer.setAnimationLoop(animate); -renderer.toneMapping = THREE.NeutralToneMapping; -document.body.appendChild(renderer.domElement); - -// post processing - -const scenePass = pass(scene, camera); -scenePass.setMRT( - mrt({ - output, - bloomIntensity: float(0), // default bloom intensity - }), -); - -const outputPass = scenePass.getTextureNode(); -const bloomIntensityPass = scenePass.getTextureNode('bloomIntensity'); - -const bloomPass = outputPass.mul(bloomIntensityPass).bloom(); - -const postProcessing = new THREE.PostProcessing(renderer); -postProcessing.outputColorTransform = false; -postProcessing.outputNode = outputPass.add(bloomPass).renderOutput(); - -// controls - -const controls = new OrbitControls(camera, renderer.domElement); -controls.maxPolarAngle = Math.PI * 0.5; -controls.minDistance = 1; -controls.maxDistance = 100; - -// raycaster - -const raycaster = new THREE.Raycaster(); -const mouse = new THREE.Vector2(); - -window.addEventListener('pointerdown', event => { - mouse.x = (event.clientX / window.innerWidth) * 2 - 1; - mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; - - raycaster.setFromCamera(mouse, camera); - - const intersects = raycaster.intersectObjects(scene.children, false); - - if (intersects.length > 0) { - const material = intersects[0].object.material; - - const bloomIntensity = material.mrtNode.getNode('bloomIntensity'); - bloomIntensity.value = bloomIntensity.value === 0 ? 1 : 0; - } -}); - -// gui - -const gui = new GUI(); - -const bloomFolder = gui.addFolder('bloom'); -bloomFolder.add(bloomPass.threshold, 'value', 0.0, 1.0).name('threshold'); -bloomFolder.add(bloomPass.strength, 'value', 0.0, 3).name('strength'); -bloomFolder.add(bloomPass.radius, 'value', 0.0, 1.0).name('radius'); - -const toneMappingFolder = gui.addFolder('tone mapping'); -toneMappingFolder.add(renderer, 'toneMappingExposure', 0.1, 3).name('exposure'); - -// events - -window.onresize = function () { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); -}; - -// animate - -function animate() { - postProcessing.render(); -} diff --git a/examples-testing/examples/webgpu_postprocessing_difference.ts b/examples-testing/examples/webgpu_postprocessing_difference.ts deleted file mode 100644 index 49f9084fc..000000000 --- a/examples-testing/examples/webgpu_postprocessing_difference.ts +++ /dev/null @@ -1,92 +0,0 @@ -import * as THREE from 'three'; -import { pass, luminance } from 'three/tsl'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { Timer } from 'three/addons/misc/Timer.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -const params = { - speed: 0, -}; - -let camera, renderer, postProcessing; -let timer, mesh, controls; - -init(); - -function init() { - renderer = new THREE.WebGPURenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.NeutralToneMapping; - document.body.appendChild(renderer.domElement); - - // - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 100); - camera.position.set(1, 2, 3); - - const scene = new THREE.Scene(); - scene.fog = new THREE.Fog(0x0487e2, 7, 25); - scene.background = new THREE.Color(0x0487e2); - - timer = new Timer(); - - const texture = new THREE.TextureLoader().load('textures/crate.gif'); - texture.colorSpace = THREE.SRGBColorSpace; - - const geometry = new THREE.BoxGeometry(); - const material = new THREE.MeshBasicMaterial({ map: texture }); - - mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // post processing - - postProcessing = new THREE.PostProcessing(renderer); - - const scenePass = pass(scene, camera); - - const currentTexture = scenePass.getTextureNode(); - const previousTexture = scenePass.getPreviousTextureNode(); - - const frameDiff = previousTexture.sub(currentTexture).abs(); - - const saturationAmount = luminance(frameDiff).mul(1000).clamp(0, 3); - - postProcessing.outputNode = currentTexture.saturation(saturationAmount); - - // - - controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 2; - controls.maxDistance = 10; - controls.enableDamping = true; - controls.dampingFactor = 0.01; - - window.addEventListener('resize', onWindowResize); - - // - - const gui = new GUI(); - gui.add(params, 'speed', 0, 2); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - timer.update(); - - controls.update(); - - mesh.rotation.y += timer.getDelta() * 5 * params.speed; - - postProcessing.render(); -} diff --git a/examples-testing/examples/webgpu_postprocessing_dof.ts b/examples-testing/examples/webgpu_postprocessing_dof.ts deleted file mode 100644 index 3fb4046be..000000000 --- a/examples-testing/examples/webgpu_postprocessing_dof.ts +++ /dev/null @@ -1,159 +0,0 @@ -import * as THREE from 'three'; -import { cubeTexture, positionWorld, oscSine, timerGlobal, pass, uniform } from 'three/tsl'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import Stats from 'three/addons/libs/stats.module.js'; - -// - -let camera, scene, renderer, mesh, stats; - -let mouseX = 0, - mouseY = 0; - -let windowHalfX = window.innerWidth / 2; -let windowHalfY = window.innerHeight / 2; - -let width = window.innerWidth; -let height = window.innerHeight; - -let postProcessing; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(70, width / height, 1, 3000); - camera.position.z = 200; - - scene = new THREE.Scene(); - - const path = 'textures/cube/SwedishRoyalCastle/'; - const format = '.jpg'; - const urls = [ - path + 'px' + format, - path + 'nx' + format, - path + 'py' + format, - path + 'ny' + format, - path + 'pz' + format, - path + 'nz' + format, - ]; - - const xgrid = 14, - ygrid = 9, - zgrid = 14; - const count = xgrid * ygrid * zgrid; - - const textureCube = new THREE.CubeTextureLoader().load(urls); - const cubeTextureNode = cubeTexture(textureCube); - const oscPos = oscSine(positionWorld.div(1000 /* scene distance */).add(timerGlobal(0.2 /* speed */))); - - const geometry = new THREE.SphereGeometry(60, 20, 10); - const material = new THREE.MeshBasicNodeMaterial(); - material.colorNode = cubeTextureNode.mul(oscPos); - - mesh = new THREE.InstancedMesh(geometry, material, count); - scene.add(mesh); - - const matrix = new THREE.Matrix4(); - - let index = 0; - - for (let i = 0; i < xgrid; i++) { - for (let j = 0; j < ygrid; j++) { - for (let k = 0; k < zgrid; k++) { - const x = 200 * (i - xgrid / 2); - const y = 200 * (j - ygrid / 2); - const z = 200 * (k - zgrid / 2); - - mesh.setMatrixAt(index, matrix.identity().setPosition(x, y, z)); - index++; - } - } - } - - // renderer - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - const effectController = { - focus: uniform(500.0), - aperture: uniform(5), - maxblur: uniform(0.01), - }; - - // post processing - - postProcessing = new THREE.PostProcessing(renderer); - - const scenePass = pass(scene, camera); - - const scenePassColor = scenePass.getTextureNode(); - const scenePassViewZ = scenePass.getViewZNode(); - - const dofPass = scenePassColor.dof( - scenePassViewZ, - effectController.focus, - effectController.aperture.mul(0.00001), - effectController.maxblur, - ); - - postProcessing.outputNode = dofPass; - - // controls - - renderer.domElement.style.touchAction = 'none'; - renderer.domElement.addEventListener('pointermove', onPointerMove); - - window.addEventListener('resize', onWindowResize); - - // stats - - stats = new Stats(); - document.body.appendChild(stats.dom); - - // gui - - const gui = new GUI(); - gui.add(effectController.focus, 'value', 10.0, 3000.0, 10).name('focus'); - gui.add(effectController.aperture, 'value', 0, 10, 0.1).name('aperture'); - gui.add(effectController.maxblur, 'value', 0.0, 0.01, 0.001).name('maxblur'); -} - -function onPointerMove(event) { - if (event.isPrimary === false) return; - - mouseX = event.clientX - windowHalfX; - mouseY = event.clientY - windowHalfY; -} - -function onWindowResize() { - windowHalfX = window.innerWidth / 2; - windowHalfY = window.innerHeight / 2; - - width = window.innerWidth; - height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); -} - -function animate() { - render(); - stats.update(); -} - -function render() { - camera.position.x += (mouseX - camera.position.x) * 0.036; - camera.position.y += (-mouseY - camera.position.y) * 0.036; - - camera.lookAt(scene.position); - - postProcessing.render(); -} diff --git a/examples-testing/examples/webgpu_postprocessing_fxaa.ts b/examples-testing/examples/webgpu_postprocessing_fxaa.ts deleted file mode 100644 index 5e75fa7a9..000000000 --- a/examples-testing/examples/webgpu_postprocessing_fxaa.ts +++ /dev/null @@ -1,122 +0,0 @@ -import * as THREE from 'three'; -import { pass, renderOutput } from 'three/tsl'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -const params = { - enabled: true, - animated: false, -}; - -let camera, scene, renderer, clock, group; -let postProcessing; - -init(); - -async function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200); - camera.position.z = 50; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0xffffff); - - clock = new THREE.Clock(); - - // - - const hemiLight = new THREE.HemisphereLight(0xffffff, 0x8d8d8d); - hemiLight.position.set(0, 1000, 0); - scene.add(hemiLight); - - const dirLight = new THREE.DirectionalLight(0xffffff, 3); - dirLight.position.set(-3000, 1000, -1000); - scene.add(dirLight); - - // - - group = new THREE.Group(); - - const geometry = new THREE.TetrahedronGeometry(); - const material = new THREE.MeshStandardMaterial({ color: 0xf73232, flatShading: true }); - - for (let i = 0; i < 100; i++) { - const mesh = new THREE.Mesh(geometry, material); - - mesh.position.x = Math.random() * 50 - 25; - mesh.position.y = Math.random() * 50 - 25; - mesh.position.z = Math.random() * 50 - 25; - - mesh.scale.setScalar(Math.random() * 2 + 1); - - mesh.rotation.x = Math.random() * Math.PI; - mesh.rotation.y = Math.random() * Math.PI; - mesh.rotation.z = Math.random() * Math.PI; - - group.add(mesh); - } - - scene.add(group); - - renderer = new THREE.WebGPURenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // post processing - - postProcessing = new THREE.PostProcessing(renderer); - - // ignore default output color transform ( toneMapping and outputColorSpace ) - // use renderOutput() for control the sequence - - postProcessing.outputColorTransform = false; - - // scene pass - - const scenePass = pass(scene, camera); - const outputPass = renderOutput(scenePass); - - // FXAA must be computed in sRGB color space (so after tone mapping and color space conversion) - - const fxaaPass = outputPass.fxaa(); - postProcessing.outputNode = fxaaPass; - - // - - window.addEventListener('resize', onWindowResize); - - // - - const gui = new GUI(); - gui.title('FXAA settings'); - gui.add(params, 'enabled').onChange(value => { - if (value === true) { - postProcessing.outputNode = fxaaPass; - } else { - postProcessing.outputNode = outputPass; - } - - postProcessing.needsUpdate = true; - }); - gui.add(params, 'animated'); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - const delta = clock.getDelta(); - - if (params.animated === true) { - group.rotation.y += delta * 0.1; - } - - postProcessing.render(); -} diff --git a/examples-testing/examples/webgpu_postprocessing_masking.ts b/examples-testing/examples/webgpu_postprocessing_masking.ts deleted file mode 100644 index a4f56b170..000000000 --- a/examples-testing/examples/webgpu_postprocessing_masking.ts +++ /dev/null @@ -1,85 +0,0 @@ -import * as THREE from 'three'; -import { pass, texture } from 'three/tsl'; - -let camera, postProcessing, renderer; -let box, torus; - -init(); - -function init() { - // scene - - const baseScene = new THREE.Scene(); - baseScene.background = new THREE.Color(0xe0e0e0); - - const maskScene1 = new THREE.Scene(); - box = new THREE.Mesh(new THREE.BoxGeometry(4, 4, 4)); - maskScene1.add(box); - - const maskScene2 = new THREE.Scene(); - torus = new THREE.Mesh(new THREE.TorusGeometry(3, 1, 16, 32)); - maskScene2.add(torus); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); - camera.position.z = 10; - - // textures - - const texture1 = new THREE.TextureLoader().load('textures/758px-Canestra_di_frutta_(Caravaggio).jpg'); - texture1.colorSpace = THREE.SRGBColorSpace; - texture1.minFilter = THREE.LinearFilter; - texture1.flipY = false; - - const texture2 = new THREE.TextureLoader().load('textures/2294472375_24a3b8ef46_o.jpg'); - texture2.colorSpace = THREE.SRGBColorSpace; - texture2.flipY = false; - - // renderer - - renderer = new THREE.WebGPURenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); - - // post processing - - const base = pass(baseScene, camera); - const sceneMask1 = pass(maskScene1, camera).a; - const sceneMask2 = pass(maskScene2, camera).a; - - let compose = base; - compose = sceneMask1.mix(compose, texture(texture1)); - compose = sceneMask2.mix(compose, texture(texture2)); - - postProcessing = new THREE.PostProcessing(renderer); - postProcessing.outputNode = compose; -} - -function onWindowResize() { - const width = window.innerWidth; - const height = window.innerHeight; - - camera.aspect = width / height; - camera.updateProjectionMatrix(); - - renderer.setSize(width, height); -} - -function animate() { - const time = performance.now() * 0.001 + 6000; - - box.position.x = Math.cos(time / 1.5) * 2; - box.position.y = Math.sin(time) * 2; - box.rotation.x = time; - box.rotation.y = time / 2; - - torus.position.x = Math.cos(time) * 2; - torus.position.y = Math.sin(time / 1.5) * 2; - torus.rotation.x = time; - torus.rotation.y = time / 2; - - postProcessing.render(); -} diff --git a/examples-testing/examples/webgpu_postprocessing_pixel.ts b/examples-testing/examples/webgpu_postprocessing_pixel.ts deleted file mode 100644 index d7e51008d..000000000 --- a/examples-testing/examples/webgpu_postprocessing_pixel.ts +++ /dev/null @@ -1,234 +0,0 @@ -import * as THREE from 'three'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -import { uniform, pixelationPass } from 'three/tsl'; - -let camera, scene, renderer, postProcessing, crystalMesh, clock; -let gui, effectController; - -init(); - -function init() { - const aspectRatio = window.innerWidth / window.innerHeight; - - camera = new THREE.OrthographicCamera(-aspectRatio, aspectRatio, 1, -1, 0.1, 10); - camera.position.y = 2 * Math.tan(Math.PI / 6); - camera.position.z = 2; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x151729); - - clock = new THREE.Clock(); - - // textures - - const loader = new THREE.TextureLoader(); - const texChecker = pixelTexture(loader.load('textures/checker.png')); - const texChecker2 = pixelTexture(loader.load('textures/checker.png')); - texChecker.repeat.set(3, 3); - texChecker2.repeat.set(1.5, 1.5); - - // meshes - - const boxMaterial = new THREE.MeshPhongMaterial({ map: texChecker2 }); - - function addBox(boxSideLength, x, z, rotation) { - const mesh = new THREE.Mesh(new THREE.BoxGeometry(boxSideLength, boxSideLength, boxSideLength), boxMaterial); - mesh.castShadow = true; - mesh.receiveShadow = true; - mesh.rotation.y = rotation; - mesh.position.y = boxSideLength / 2; - mesh.position.set(x, boxSideLength / 2 + 0.0001, z); - scene.add(mesh); - return mesh; - } - - addBox(0.4, 0, 0, Math.PI / 4); - addBox(0.5, -0.5, -0.5, Math.PI / 4); - - const planeSideLength = 2; - const planeMesh = new THREE.Mesh( - new THREE.PlaneGeometry(planeSideLength, planeSideLength), - new THREE.MeshPhongMaterial({ map: texChecker }), - ); - planeMesh.receiveShadow = true; - planeMesh.rotation.x = -Math.PI / 2; - scene.add(planeMesh); - - const radius = 0.2; - const geometry = new THREE.IcosahedronGeometry(radius); - crystalMesh = new THREE.Mesh( - geometry, - new THREE.MeshPhongMaterial({ - color: 0x68b7e9, - emissive: 0x4f7e8b, - shininess: 10, - specular: 0xffffff, - }), - ); - crystalMesh.receiveShadow = true; - crystalMesh.castShadow = true; - scene.add(crystalMesh); - - // lights - - scene.add(new THREE.AmbientLight(0x757f8e, 3)); - - const directionalLight = new THREE.DirectionalLight(0xfffecd, 1.5); - directionalLight.position.set(100, 100, 100); - directionalLight.castShadow = true; - directionalLight.shadow.mapSize.set(2048, 2048); - directionalLight.shadow.bias = -0.0001; - scene.add(directionalLight); - - const spotLight = new THREE.SpotLight(0xffc100, 10, 10, Math.PI / 16, 0.02, 2); - spotLight.position.set(2, 2, 0); - const target = spotLight.target; - scene.add(target); - target.position.set(0, 0, 0); - spotLight.castShadow = true; - spotLight.shadow.bias = -0.001; - scene.add(spotLight); - - renderer = new THREE.WebGPURenderer(); - renderer.shadowMap.enabled = true; - renderer.shadowMap.type = THREE.BasicShadowMap; - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - effectController = { - pixelSize: uniform(6), - normalEdgeStrength: uniform(0.3), - depthEdgeStrength: uniform(0.4), - pixelAlignedPanning: true, - }; - - postProcessing = new THREE.PostProcessing(renderer); - const scenePass = pixelationPass( - scene, - camera, - effectController.pixelSize, - effectController.normalEdgeStrength, - effectController.depthEdgeStrength, - ); - postProcessing.outputNode = scenePass; - - window.addEventListener('resize', onWindowResize); - - const controls = new OrbitControls(camera, renderer.domElement); - controls.maxZoom = 2; - - // gui - - gui = new GUI(); - gui.add(effectController.pixelSize, 'value', 1, 16, 1).name('Pixel Size'); - gui.add(effectController.normalEdgeStrength, 'value', 0, 2, 0.05).name('Normal Edge Strength'); - gui.add(effectController.depthEdgeStrength, 'value', 0, 1, 0.05).name('Depth Edge Strength'); - gui.add(effectController, 'pixelAlignedPanning'); -} - -function onWindowResize() { - const aspectRatio = window.innerWidth / window.innerHeight; - camera.left = -aspectRatio; - camera.right = aspectRatio; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const t = clock.getElapsedTime(); - - crystalMesh.material.emissiveIntensity = Math.sin(t * 3) * 0.5 + 0.5; - crystalMesh.position.y = 0.7 + Math.sin(t * 2) * 0.05; - crystalMesh.rotation.y = stopGoEased(t, 2, 4) * 2 * Math.PI; - - const rendererSize = renderer.getSize(new THREE.Vector2()); - const aspectRatio = rendererSize.x / rendererSize.y; - - if (effectController.pixelAlignedPanning) { - const pixelSize = effectController.pixelSize.value; - - pixelAlignFrustum( - camera, - aspectRatio, - Math.floor(rendererSize.x / pixelSize), - Math.floor(rendererSize.y / pixelSize), - ); - } else if (camera.left != -aspectRatio || camera.top != 1.0) { - // Reset the Camera Frustum if it has been modified - camera.left = -aspectRatio; - camera.right = aspectRatio; - camera.top = 1.0; - camera.bottom = -1.0; - camera.updateProjectionMatrix(); - } - - postProcessing.render(); -} - -// Helper functions - -function pixelTexture(texture) { - texture.minFilter = THREE.NearestFilter; - texture.magFilter = THREE.NearestFilter; - texture.generateMipmaps = false; - texture.wrapS = THREE.RepeatWrapping; - texture.wrapT = THREE.RepeatWrapping; - texture.colorSpace = THREE.SRGBColorSpace; - return texture; -} - -function easeInOutCubic(x) { - return x ** 2 * 3 - x ** 3 * 2; -} - -function linearStep(x, edge0, edge1) { - const w = edge1 - edge0; - const m = 1 / w; - const y0 = -m * edge0; - return THREE.MathUtils.clamp(y0 + m * x, 0, 1); -} - -function stopGoEased(x, downtime, period) { - const cycle = (x / period) | 0; - const tween = x - cycle * period; - const linStep = easeInOutCubic(linearStep(tween, downtime, period)); - return cycle + linStep; -} - -function pixelAlignFrustum(camera, aspectRatio, pixelsPerScreenWidth, pixelsPerScreenHeight) { - // 0. Get Pixel Grid Units - const worldScreenWidth = (camera.right - camera.left) / camera.zoom; - const worldScreenHeight = (camera.top - camera.bottom) / camera.zoom; - const pixelWidth = worldScreenWidth / pixelsPerScreenWidth; - const pixelHeight = worldScreenHeight / pixelsPerScreenHeight; - - // 1. Project the current camera position along its local rotation bases - const camPos = new THREE.Vector3(); - camera.getWorldPosition(camPos); - const camRot = new THREE.Quaternion(); - camera.getWorldQuaternion(camRot); - const camRight = new THREE.Vector3(1.0, 0.0, 0.0).applyQuaternion(camRot); - const camUp = new THREE.Vector3(0.0, 1.0, 0.0).applyQuaternion(camRot); - const camPosRight = camPos.dot(camRight); - const camPosUp = camPos.dot(camUp); - - // 2. Find how far along its position is along these bases in pixel units - const camPosRightPx = camPosRight / pixelWidth; - const camPosUpPx = camPosUp / pixelHeight; - - // 3. Find the fractional pixel units and convert to world units - const fractX = camPosRightPx - Math.round(camPosRightPx); - const fractY = camPosUpPx - Math.round(camPosUpPx); - - // 4. Add fractional world units to the left/right top/bottom to align with the pixel grid - camera.left = -aspectRatio - fractX * pixelWidth; - camera.right = aspectRatio - fractX * pixelWidth; - camera.top = 1.0 - fractY * pixelHeight; - camera.bottom = -1.0 - fractY * pixelHeight; - camera.updateProjectionMatrix(); -} diff --git a/examples-testing/examples/webgpu_postprocessing_sobel.ts b/examples-testing/examples/webgpu_postprocessing_sobel.ts deleted file mode 100644 index cbc5a96ba..000000000 --- a/examples-testing/examples/webgpu_postprocessing_sobel.ts +++ /dev/null @@ -1,89 +0,0 @@ -import * as THREE from 'three'; -import { pass } from 'three/tsl'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer; -let postProcessing; - -const params = { - enable: true, -}; - -init(); - -function init() { - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(0, 1, 3); - camera.lookAt(scene.position); - - // - - const ambientLight = new THREE.AmbientLight(0xe7e7e7); - scene.add(ambientLight); - - const pointLight = new THREE.PointLight(0xffffff, 20); - camera.add(pointLight); - scene.add(camera); - - // - - const geometry = new THREE.TorusKnotGeometry(1, 0.3, 256, 32); - const material = new THREE.MeshPhongMaterial({ color: 0xffff00 }); - - const mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.outputColorSpace = THREE.LinearSRGBColorSpace; - document.body.appendChild(renderer.domElement); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.enableZoom = false; - - // postprocessing - - postProcessing = new THREE.PostProcessing(renderer); - - const scenePass = pass(scene, camera); - const scenePassColor = scenePass.getTextureNode(); - - postProcessing.outputNode = scenePassColor.sobel(); - - // - - const gui = new GUI(); - - gui.add(params, 'enable'); - gui.open(); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - if (params.enable === true) { - postProcessing.render(); - } else { - renderer.render(scene, camera); - } -} diff --git a/examples-testing/examples/webgpu_postprocessing_transition.ts b/examples-testing/examples/webgpu_postprocessing_transition.ts deleted file mode 100644 index b66bad12c..000000000 --- a/examples-testing/examples/webgpu_postprocessing_transition.ts +++ /dev/null @@ -1,200 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import TWEEN from 'three/addons/libs/tween.module.js'; -import { uniform, transition, pass } from 'three/tsl'; - -let renderer, postProcessing, transitionController, transitionPass; - -const textures = []; -const clock = new THREE.Clock(); - -const effectController = { - animateScene: true, - animateTransition: true, - transition: 0, - _transition: uniform(0), - useTexture: true, - _useTexture: uniform(1), - texture: 5, - cycle: true, - threshold: uniform(0.1), -}; - -function generateInstancedMesh(geometry, material, count) { - const mesh = new THREE.InstancedMesh(geometry, material, count); - - const dummy = new THREE.Object3D(); - const color = new THREE.Color(); - - for (let i = 0; i < count; i++) { - dummy.position.x = Math.random() * 100 - 50; - dummy.position.y = Math.random() * 60 - 30; - dummy.position.z = Math.random() * 80 - 40; - - dummy.rotation.x = Math.random() * 2 * Math.PI; - dummy.rotation.y = Math.random() * 2 * Math.PI; - dummy.rotation.z = Math.random() * 2 * Math.PI; - - dummy.scale.x = Math.random() * 2 + 1; - - if (geometry.type === 'BoxGeometry') { - dummy.scale.y = Math.random() * 2 + 1; - dummy.scale.z = Math.random() * 2 + 1; - } else { - dummy.scale.y = dummy.scale.x; - dummy.scale.z = dummy.scale.x; - } - - dummy.updateMatrix(); - - mesh.setMatrixAt(i, dummy.matrix); - mesh.setColorAt(i, color.setScalar(0.1 + 0.9 * Math.random())); - } - - return mesh; -} - -function FXScene(geometry, rotationSpeed, backgroundColor) { - const camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.z = 20; - - // Setup scene - const scene = new THREE.Scene(); - scene.background = new THREE.Color(backgroundColor); - scene.add(new THREE.AmbientLight(0xaaaaaa, 3)); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0, 1, 4); - scene.add(light); - - this.rotationSpeed = rotationSpeed; - - const color = geometry.type === 'BoxGeometry' ? 0x0000ff : 0xff0000; - const material = new THREE.MeshPhongNodeMaterial({ color: color, flatShading: true }); - const mesh = generateInstancedMesh(geometry, material, 500); - scene.add(mesh); - - this.scene = scene; - this.camera = camera; - this.mesh = mesh; - - this.update = function (delta) { - if (effectController.animateScene) { - mesh.rotation.x += this.rotationSpeed.x * delta; - mesh.rotation.y += this.rotationSpeed.y * delta; - mesh.rotation.z += this.rotationSpeed.z * delta; - } - }; - - this.resize = function () { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - }; -} - -const fxSceneA = new FXScene(new THREE.BoxGeometry(2, 2, 2), new THREE.Vector3(0, -0.4, 0), 0xffffff); -const fxSceneB = new FXScene(new THREE.IcosahedronGeometry(1, 1), new THREE.Vector3(0, 0.2, 0.1), 0x000000); - -function init() { - // Initialize textures - - const loader = new THREE.TextureLoader(); - - for (let i = 0; i < 6; i++) { - textures[i] = loader.load('textures/transition/transition' + (i + 1) + '.png'); - } - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - postProcessing = new THREE.PostProcessing(renderer); - - const scenePassA = pass(fxSceneA.scene, fxSceneA.camera); - const scenePassB = pass(fxSceneB.scene, fxSceneB.camera); - - transitionPass = transition( - scenePassA, - scenePassB, - new THREE.TextureNode(textures[effectController.texture]), - effectController._transition, - effectController.threshold, - effectController._useTexture, - ); - - postProcessing.outputNode = transitionPass; - - const gui = new GUI(); - - gui.add(effectController, 'animateScene').name('Animate Scene'); - gui.add(effectController, 'animateTransition').name('Animate Transition'); - transitionController = gui - .add(effectController, 'transition', 0, 1, 0.01) - .name('transition') - .onChange(() => { - effectController._transition.value = effectController.transition; - }); - gui.add(effectController, 'useTexture').onChange(() => { - const value = effectController.useTexture ? 1 : 0; - effectController._useTexture.value = value; - }); - gui.add(effectController, 'texture', { Perlin: 0, Squares: 1, Cells: 2, Distort: 3, Gradient: 4, Radial: 5 }); - gui.add(effectController, 'cycle'); - gui.add(effectController.threshold, 'value', 0, 1, 0.01).name('threshold'); -} - -window.addEventListener('resize', onWindowResize); - -function onWindowResize() { - fxSceneA.resize(); - fxSceneB.resize(); - renderer.setSize(window.innerWidth, window.innerHeight); -} - -new TWEEN.Tween(effectController) - .to({ transition: 1 }, 1500) - .onUpdate(function () { - transitionController.setValue(effectController.transition); - - // Change the current alpha texture after each transition - if (effectController.cycle) { - if (effectController.transition == 0 || effectController.transition == 1) { - effectController.texture = (effectController.texture + 1) % textures.length; - } - } - }) - .repeat(Infinity) - .delay(2000) - .yoyo(true) - .start(); - -function animate() { - if (effectController.animateTransition) TWEEN.update(); - - if (textures[effectController.texture]) { - const mixTexture = textures[effectController.texture]; - transitionPass.mixTextureNode.value = mixTexture; - } - - const delta = clock.getDelta(); - fxSceneA.update(delta); - fxSceneB.update(delta); - - render(); -} - -function render() { - // Prevent render both scenes when it's not necessary - if (effectController.transition === 0) { - renderer.render(fxSceneB.scene, fxSceneB.camera); - } else if (effectController.transition === 1) { - renderer.render(fxSceneA.scene, fxSceneA.camera); - } else { - postProcessing.render(); - } -} - -init(); diff --git a/examples-testing/examples/webgpu_procedural_texture.ts b/examples-testing/examples/webgpu_procedural_texture.ts deleted file mode 100644 index d4228f908..000000000 --- a/examples-testing/examples/webgpu_procedural_texture.ts +++ /dev/null @@ -1,74 +0,0 @@ -import * as THREE from 'three'; -import { checker, uv, uniform } from 'three/tsl'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; - -let camera, scene, renderer; - -init(); -render(); - -function init() { - const aspect = window.innerWidth / window.innerHeight; - camera = new THREE.OrthographicCamera(-aspect, aspect, 1, -1, 0, 2); - camera.position.z = 1; - - scene = new THREE.Scene(); - - // procedural to texture - - const uvScale = uniform(4); - const blurAmount = uniform(0.5); - - const procedural = checker(uv().mul(uvScale)); - const proceduralToTexture = procedural.toTexture(512, 512); // ( width, height ) <- texture size - - const colorNode = proceduralToTexture.gaussianBlur(blurAmount, 10); - - // extra - - //proceduralToTexture.autoUpdate = false; // update just once - //proceduralToTexture.textureNeedsUpdate = true; // manually update - - // scene - - const material = new THREE.MeshBasicNodeMaterial(); - material.colorNode = colorNode; - - const plane = new THREE.Mesh(new THREE.PlaneGeometry(1, 1), material); - scene.add(plane); - - // renderer - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(render); - document.body.appendChild(renderer.domElement); - - window.addEventListener('resize', onWindowResize); - - // gui - - const gui = new GUI(); - gui.add(uvScale, 'value', 1, 10).name('uv scale ( before rtt )'); - gui.add(blurAmount, 'value', 0, 2).name('blur amount ( after rtt )'); - gui.add(proceduralToTexture, 'autoUpdate').name('auto update'); -} - -function onWindowResize() { - renderer.setSize(window.innerWidth, window.innerHeight); - - const aspect = window.innerWidth / window.innerHeight; - - const frustumHeight = camera.top - camera.bottom; - - camera.left = (-frustumHeight * aspect) / 2; - camera.right = (frustumHeight * aspect) / 2; - - camera.updateProjectionMatrix(); -} - -function render() { - renderer.renderAsync(scene, camera); -} diff --git a/examples-testing/examples/webgpu_refraction.ts b/examples-testing/examples/webgpu_refraction.ts deleted file mode 100644 index bf36d9119..000000000 --- a/examples-testing/examples/webgpu_refraction.ts +++ /dev/null @@ -1,141 +0,0 @@ -import * as THREE from 'three'; -import { viewportSafeUV, viewportSharedTexture, viewportTopLeft, texture, uv } from 'three/tsl'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer; - -let cameraControls; - -let smallSphere; - -init(); - -function init() { - // scene - scene = new THREE.Scene(); - - // camera - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 500); - camera.position.set(0, 75, 160); - - // - - const geometry = new THREE.IcosahedronGeometry(5, 0); - const material = new THREE.MeshPhongMaterial({ color: 0xffffff, emissive: 0x7b7b7b, flatShading: true }); - smallSphere = new THREE.Mesh(geometry, material); - scene.add(smallSphere); - - // textures - - const loader = new THREE.TextureLoader(); - - const floorNormal = loader.load('textures/floors/FloorsCheckerboard_S_Normal.jpg'); - floorNormal.wrapS = THREE.RepeatWrapping; - floorNormal.wrapT = THREE.RepeatWrapping; - - // refractor - - const verticalNormalScale = 0.1; - const verticalUVOffset = texture(floorNormal, uv().mul(5)).xy.mul(2).sub(1).mul(verticalNormalScale); - - const refractorUV = viewportTopLeft.add(verticalUVOffset); - const verticalRefractor = viewportSharedTexture(viewportSafeUV(refractorUV)); - - const planeGeo = new THREE.PlaneGeometry(100.1, 100.1); - - const planeRefractor = new THREE.Mesh( - planeGeo, - new THREE.MeshBasicNodeMaterial({ - backdropNode: verticalRefractor, - }), - ); - planeRefractor.material.transparent = true; - planeRefractor.position.y = 50; - scene.add(planeRefractor); - - // walls - - const planeTop = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); - planeTop.position.y = 100; - planeTop.rotateX(Math.PI / 2); - scene.add(planeTop); - - const planeBottom = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xffffff })); - planeBottom.rotateX(-Math.PI / 2); - scene.add(planeBottom); - - const planeBack = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x7f7fff })); - planeBack.position.z = -50; - planeBack.position.y = 50; - scene.add(planeBack); - - const planeRight = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0x00ff00 })); - planeRight.position.x = 50; - planeRight.position.y = 50; - planeRight.rotateY(-Math.PI / 2); - scene.add(planeRight); - - const planeLeft = new THREE.Mesh(planeGeo, new THREE.MeshPhongMaterial({ color: 0xff0000 })); - planeLeft.position.x = -50; - planeLeft.position.y = 50; - planeLeft.rotateY(Math.PI / 2); - scene.add(planeLeft); - - // lights - - const mainLight = new THREE.PointLight(0xe7e7e7, 2.5, 250, 0); - mainLight.position.y = 60; - scene.add(mainLight); - - const greenLight = new THREE.PointLight(0x00ff00, 0.5, 1000, 0); - greenLight.position.set(550, 50, 0); - scene.add(greenLight); - - const redLight = new THREE.PointLight(0xff0000, 0.5, 1000, 0); - redLight.position.set(-550, 50, 0); - scene.add(redLight); - - const blueLight = new THREE.PointLight(0xbbbbfe, 0.5, 1000, 0); - blueLight.position.set(0, 50, 550); - scene.add(blueLight); - - // renderer - - renderer = new THREE.WebGPURenderer(/*{ antialias: true }*/); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // controls - - cameraControls = new OrbitControls(camera, renderer.domElement); - cameraControls.target.set(0, 40, 0); - cameraControls.maxDistance = 400; - cameraControls.minDistance = 10; - cameraControls.update(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const timer = Date.now() * 0.01; - - smallSphere.position.set( - Math.cos(timer * 0.1) * 30, - Math.abs(Math.cos(timer * 0.2)) * 20 + 5, - Math.sin(timer * 0.1) * 30, - ); - smallSphere.rotation.y = Math.PI / 2 - timer * 0.1; - smallSphere.rotation.z = timer * 0.8; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_sky.ts b/examples-testing/examples/webgpu_sky.ts deleted file mode 100644 index 097d06af6..000000000 --- a/examples-testing/examples/webgpu_sky.ts +++ /dev/null @@ -1,95 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { SkyMesh } from 'three/addons/objects/SkyMesh.js'; - -let camera, scene, renderer; - -let sky, sun; - -init(); - -function initSky() { - // Add Sky - sky = new SkyMesh(); - sky.scale.setScalar(450000); - scene.add(sky); - - sun = new THREE.Vector3(); - - /// GUI - - const effectController = { - turbidity: 10, - rayleigh: 3, - mieCoefficient: 0.005, - mieDirectionalG: 0.7, - elevation: 2, - azimuth: 180, - exposure: renderer.toneMappingExposure, - }; - - function guiChanged() { - sky.turbidity.value = effectController.turbidity; - sky.rayleigh.value = effectController.rayleigh; - sky.mieCoefficient.value = effectController.mieCoefficient; - sky.mieDirectionalG.value = effectController.mieDirectionalG; - - const phi = THREE.MathUtils.degToRad(90 - effectController.elevation); - const theta = THREE.MathUtils.degToRad(effectController.azimuth); - - sun.setFromSphericalCoords(1, phi, theta); - - sky.sunPosition.value.copy(sun); - - renderer.toneMappingExposure = effectController.exposure; - } - - const gui = new GUI(); - - gui.add(effectController, 'turbidity', 0.0, 20.0, 0.1).onChange(guiChanged); - gui.add(effectController, 'rayleigh', 0.0, 4, 0.001).onChange(guiChanged); - gui.add(effectController, 'mieCoefficient', 0.0, 0.1, 0.001).onChange(guiChanged); - gui.add(effectController, 'mieDirectionalG', 0.0, 1, 0.001).onChange(guiChanged); - gui.add(effectController, 'elevation', 0, 90, 0.1).onChange(guiChanged); - gui.add(effectController, 'azimuth', -180, 180, 0.1).onChange(guiChanged); - gui.add(effectController, 'exposure', 0, 1, 0.0001).onChange(guiChanged); - - guiChanged(); -} - -function init() { - camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 100, 2000000); - camera.position.set(0, 100, 2000); - - scene = new THREE.Scene(); - - renderer = new THREE.WebGPURenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 0.5; - document.body.appendChild(renderer.domElement); - - const controls = new OrbitControls(camera, renderer.domElement); - //controls.maxPolarAngle = Math.PI / 2; - controls.enableZoom = false; - controls.enablePan = false; - - initSky(); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_textures_anisotropy.ts b/examples-testing/examples/webgpu_textures_anisotropy.ts deleted file mode 100644 index 21cc11970..000000000 --- a/examples-testing/examples/webgpu_textures_anisotropy.ts +++ /dev/null @@ -1,155 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container, stats; - -let camera, scene1, scene2, renderer; - -let mouseX = 0, - mouseY = 0; - -init(); - -function init() { - const SCREEN_WIDTH = window.innerWidth; - const SCREEN_HEIGHT = window.innerHeight; - - container = document.createElement('div'); - document.body.appendChild(container); - - renderer = new THREE.WebGPURenderer({ antialias: true, forceWebGL: false }); - - // RENDERER - - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - renderer.setAnimationLoop(animate); - renderer.autoClear = false; - - renderer.domElement.style.position = 'relative'; - container.appendChild(renderer.domElement); - - // - - camera = new THREE.PerspectiveCamera(35, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 25000); - camera.position.z = 1500; - - scene1 = new THREE.Scene(); - scene1.fog = new THREE.Fog(0xf2f7ff, 1, 25000); - - scene2 = new THREE.Scene(); - scene2.fog = new THREE.Fog(0xf2f7ff, 1, 25000); - - scene1.add(new THREE.AmbientLight(0xeef0ff, 3)); - scene2.add(new THREE.AmbientLight(0xeef0ff, 3)); - - const light1 = new THREE.DirectionalLight(0xffffff, 6); - light1.position.set(1, 1, 1); - scene1.add(light1); - - const light2 = new THREE.DirectionalLight(0xffffff, 6); - light2.position.set(1, 1, 1); - scene2.add(light2); - - // GROUND - - const textureLoader = new THREE.TextureLoader(); - - const maxAnisotropy = renderer.getMaxAnisotropy(); - - const texture1 = textureLoader.load('textures/crate.gif'); - const material1 = new THREE.MeshPhongMaterial({ color: 0xffffff, map: texture1 }); - - texture1.colorSpace = THREE.SRGBColorSpace; - texture1.anisotropy = renderer.getMaxAnisotropy(); - texture1.wrapS = texture1.wrapT = THREE.RepeatWrapping; - texture1.repeat.set(512, 512); - - const texture2 = textureLoader.load('textures/crate.gif'); - const material2 = new THREE.MeshPhongMaterial({ color: 0xffffff, map: texture2 }); - - texture2.colorSpace = THREE.SRGBColorSpace; - texture2.anisotropy = 1; - texture2.wrapS = texture2.wrapT = THREE.RepeatWrapping; - texture2.repeat.set(512, 512); - - if (maxAnisotropy > 0) { - document.getElementById('val_left').innerHTML = texture1.anisotropy; - document.getElementById('val_right').innerHTML = texture2.anisotropy; - } else { - document.getElementById('val_left').innerHTML = 'not supported'; - document.getElementById('val_right').innerHTML = 'not supported'; - } - - // - - const geometry = new THREE.PlaneGeometry(100, 100); - - const mesh1 = new THREE.Mesh(geometry, material1); - mesh1.rotation.x = -Math.PI / 2; - mesh1.scale.set(1000, 1000, 1000); - - const mesh2 = new THREE.Mesh(geometry, material2); - mesh2.rotation.x = -Math.PI / 2; - mesh2.scale.set(1000, 1000, 1000); - - scene1.add(mesh1); - scene2.add(mesh2); - - // STATS1 - - stats = new Stats(); - container.appendChild(stats.dom); - - document.addEventListener('mousemove', onDocumentMouseMove); - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onDocumentMouseMove(event) { - const windowHalfX = window.innerWidth / 2; - const windowHalfY = window.innerHeight / 2; - - mouseX = event.clientX - windowHalfX; - mouseY = event.clientY - windowHalfY; -} - -function animate() { - render(); - stats.update(); -} - -function render() { - const SCREEN_WIDTH = window.innerWidth; - const SCREEN_HEIGHT = window.innerHeight; - - camera.position.x += (mouseX - camera.position.x) * 0.05; - camera.position.y = THREE.MathUtils.clamp( - camera.position.y + (-(mouseY - 200) - camera.position.y) * 0.05, - 50, - 1000, - ); - - camera.lookAt(scene1.position); - renderer.clear(); - - renderer.setScissorTest(true); - - renderer.setScissor(0, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); - renderer.render(scene1, camera); - - renderer.setScissorTest(true); - - renderer.setScissor(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT); - renderer.render(scene2, camera); - - // renderer.setScissorTest( false ); -} diff --git a/examples-testing/examples/webgpu_textures_partialupdate.ts b/examples-testing/examples/webgpu_textures_partialupdate.ts deleted file mode 100644 index e8ebe87db..000000000 --- a/examples-testing/examples/webgpu_textures_partialupdate.ts +++ /dev/null @@ -1,103 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer, clock, dataTexture, diffuseMap; - -let last = 0; -const position = new THREE.Vector2(); -const color = new THREE.Color(); - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10); - camera.position.z = 2; - - scene = new THREE.Scene(); - - clock = new THREE.Clock(); - - const loader = new THREE.TextureLoader(); - diffuseMap = loader.load('textures/carbon/Carbon.png', animate); - diffuseMap.colorSpace = THREE.SRGBColorSpace; - diffuseMap.minFilter = THREE.LinearFilter; - diffuseMap.generateMipmaps = false; - - const geometry = new THREE.PlaneGeometry(2, 2); - const material = new THREE.MeshBasicMaterial({ map: diffuseMap }); - - const mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - // - - const width = 32; - const height = 32; - - const data = new Uint8Array(width * height * 4); - dataTexture = new THREE.DataTexture(data, width, height); - - // - - renderer = new THREE.WebGPURenderer({ antialias: true, forceWebGL: false }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - - document.body.appendChild(renderer.domElement); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -async function animate() { - requestAnimationFrame(animate); - - const elapsedTime = clock.getElapsedTime(); - - await renderer.renderAsync(scene, camera); - - if (elapsedTime - last > 0.1) { - last = elapsedTime; - - position.x = 32 * THREE.MathUtils.randInt(1, 16) - 32; - position.y = 32 * THREE.MathUtils.randInt(1, 16) - 32; - - // generate new color data - updateDataTexture(dataTexture); - - // perform copy from src to dest texture to a random position - - renderer.copyTextureToTexture(dataTexture, diffuseMap, null, position); - } -} - -function updateDataTexture(texture) { - const size = texture.image.width * texture.image.height; - const data = texture.image.data; - - // generate a random color and update texture data - - color.setHex(Math.random() * 0xffffff); - - const r = Math.floor(color.r * 255); - const g = Math.floor(color.g * 255); - const b = Math.floor(color.b * 255); - - for (let i = 0; i < size; i++) { - const stride = i * 4; - - data[stride] = r; - data[stride + 1] = g; - data[stride + 2] = b; - data[stride + 3] = 1; - } - - texture.needsUpdate = true; -} diff --git a/examples-testing/examples/webgpu_tsl_coffee_smoke.ts b/examples-testing/examples/webgpu_tsl_coffee_smoke.ts deleted file mode 100644 index 506f14f19..000000000 --- a/examples-testing/examples/webgpu_tsl_coffee_smoke.ts +++ /dev/null @@ -1,132 +0,0 @@ -import * as THREE from 'three'; -import { mix, mul, positionLocal, smoothstep, texture, timerLocal, Fn, uv, vec2, vec3, vec4 } from 'three/tsl'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; - -let camera, scene, renderer, controls; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(25, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(8, 10, 12); - - scene = new THREE.Scene(); - - // Loaders - - const gltfLoader = new GLTFLoader(); - const textureLoader = new THREE.TextureLoader(); - - // baked model - - gltfLoader.load('./models/gltf/coffeeMug.glb', gltf => { - gltf.scene.getObjectByName('baked').material.map.anisotropy = 8; - scene.add(gltf.scene); - }); - - // geometry - - const smokeGeometry = new THREE.PlaneGeometry(1, 1, 16, 64); - smokeGeometry.translate(0, 0.5, 0); - smokeGeometry.scale(1.5, 6, 1.5); - - // texture - - const noiseTexture = textureLoader.load('./textures/noises/perlin/128x128.png'); - noiseTexture.wrapS = THREE.RepeatWrapping; - noiseTexture.wrapT = THREE.RepeatWrapping; - - // material - - const smokeMaterial = new THREE.MeshBasicNodeMaterial({ - transparent: true, - side: THREE.DoubleSide, - depthWrite: false, - }); - const time = timerLocal(); - - // position - - smokeMaterial.positionNode = Fn(() => { - // twist - - const twistNoiseUv = vec2(0.5, uv().y.mul(0.2).sub(time.mul(0.005)).mod(1)); - const twist = texture(noiseTexture, twistNoiseUv).r.mul(10); - positionLocal.xz.assign(positionLocal.xz.rotateUV(twist, vec2(0))); - - // wind - - const windOffset = vec2( - texture(noiseTexture, vec2(0.25, time.mul(0.01)).mod(1)).r.sub(0.5), - texture(noiseTexture, vec2(0.75, time.mul(0.01)).mod(1)).r.sub(0.5), - ).mul(uv().y.pow(2).mul(10)); - positionLocal.addAssign(windOffset); - - return positionLocal; - })(); - - // color - - smokeMaterial.colorNode = Fn(() => { - // alpha - - const alphaNoiseUv = uv() - .mul(vec2(0.5, 0.3)) - .add(vec2(0, time.mul(0.03).negate())); - const alpha = mul( - // pattern - texture(noiseTexture, alphaNoiseUv).r.smoothstep(0.4, 1), - - // edges fade - smoothstep(0, 0.1, uv().x), - smoothstep(1, 0.9, uv().x), - smoothstep(0, 0.1, uv().y), - smoothstep(1, 0.9, uv().y), - ); - - // color - - const finalColor = mix(vec3(0.6, 0.3, 0.2), vec3(1, 1, 1), alpha.pow(3)); - - return vec4(finalColor, alpha); - })(); - - // mesh - - const smoke = new THREE.Mesh(smokeGeometry, smokeMaterial); - smoke.position.y = 1.83; - scene.add(smoke); - - // renderer - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // controls - - controls = new OrbitControls(camera, renderer.domElement); - controls.enableDamping = true; - controls.minDistance = 0.1; - controls.maxDistance = 50; - controls.target.y = 3; - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -async function animate() { - controls.update(); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_tsl_vfx_flames.ts b/examples-testing/examples/webgpu_tsl_vfx_flames.ts deleted file mode 100644 index a9110fa8c..000000000 --- a/examples-testing/examples/webgpu_tsl_vfx_flames.ts +++ /dev/null @@ -1,203 +0,0 @@ -import * as THREE from 'three'; -import { - PI2, - spherizeUV, - sin, - step, - texture, - timerLocal, - Fn, - uv, - vec2, - vec3, - vec4, - mix, - billboarding, -} from 'three/tsl'; - -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera, scene, renderer, controls; - -init(); - -function init() { - camera = new THREE.PerspectiveCamera(25, window.innerWidth / window.innerHeight, 0.1, 100); - camera.position.set(1, 1, 3); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x201919); - - // textures - - const textureLoader = new THREE.TextureLoader(); - - const cellularTexture = textureLoader.load('./textures/noises/voronoi/grayscale-256x256.png'); - const perlinTexture = textureLoader.load('./textures/noises/perlin/rgb-256x256.png'); - - // gradient canvas - - const gradient = {}; - gradient.element = document.createElement('canvas'); - gradient.element.width = 128; - gradient.element.height = 1; - gradient.context = gradient.element.getContext('2d'); - - gradient.colors = ['#090033', '#5f1f93', '#e02e96', '#ffbd80', '#fff0db']; - - gradient.texture = new THREE.CanvasTexture(gradient.element); - gradient.texture.colorSpace = THREE.SRGBColorSpace; - - gradient.update = () => { - const fillGradient = gradient.context.createLinearGradient(0, 0, gradient.element.width, 0); - - for (let i = 0; i < gradient.colors.length; i++) { - const progress = i / (gradient.colors.length - 1); - const color = gradient.colors[i]; - fillGradient.addColorStop(progress, color); - } - - gradient.context.fillStyle = fillGradient; - gradient.context.fillRect(0, 0, gradient.element.width, gradient.element.height); - - gradient.texture.needsUpdate = true; - }; - - gradient.update(); - - // flame 1 material - - const flame1Material = new THREE.SpriteNodeMaterial({ transparent: true, side: THREE.DoubleSide }); - - flame1Material.colorNode = Fn(() => { - const time = timerLocal(); - - // main UV - const mainUv = uv().toVar(); - mainUv.assign(spherizeUV(mainUv, 10).mul(0.6).add(0.2)); // spherize - mainUv.assign(mainUv.pow(vec2(1, 2))); // stretch - mainUv.assign(mainUv.mul(2, 1).sub(vec2(0.5, 0))); // scale - - // gradients - const gradient1 = sin(time.mul(10).sub(mainUv.y.mul(PI2).mul(2))).toVar(); - const gradient2 = mainUv.y.smoothstep(0, 1).toVar(); - mainUv.x.addAssign(gradient1.mul(gradient2).mul(0.2)); - - // cellular noise - const cellularUv = mainUv - .mul(0.5) - .add(vec2(0, time.negate().mul(0.5))) - .mod(1); - const cellularNoise = texture(cellularTexture, cellularUv, 0).r.oneMinus().smoothstep(0, 0.5).oneMinus(); - cellularNoise.mulAssign(gradient2); - - // shape - const shape = mainUv.sub(0.5).mul(vec2(3, 2)).length().oneMinus().toVar(); - shape.assign(shape.sub(cellularNoise)); - - // gradient color - const gradientColor = texture(gradient.texture, vec2(shape.remap(0, 1, 0, 1), 0)); - - // output - const color = mix(gradientColor, vec3(1), shape.step(0.8).oneMinus()); - const alpha = shape.smoothstep(0, 0.3); - return vec4(color.rgb, alpha); - })(); - - // flame 2 material - - const flame2Material = new THREE.SpriteNodeMaterial({ transparent: true, side: THREE.DoubleSide }); - - flame2Material.colorNode = Fn(() => { - const time = timerLocal(); - - // main UV - const mainUv = uv().toVar(); - mainUv.assign(spherizeUV(mainUv, 10).mul(0.6).add(0.2)); // spherize - mainUv.assign(mainUv.pow(vec2(1, 3))); // stretch - mainUv.assign(mainUv.mul(2, 1).sub(vec2(0.5, 0))); // scale - - // perlin noise - const perlinUv = mainUv.add(vec2(0, time.negate().mul(1))).mod(1); - const perlinNoise = texture(perlinTexture, perlinUv, 0).sub(0.5).mul(1); - mainUv.x.addAssign(perlinNoise.x.mul(0.5)); - - // gradients - const gradient1 = sin(time.mul(10).sub(mainUv.y.mul(PI2).mul(2))); - const gradient2 = mainUv.y.smoothstep(0, 1); - const gradient3 = mainUv.y.smoothstep(1, 0.7); - mainUv.x.addAssign(gradient1.mul(gradient2).mul(0.2)); - - // displaced perlin noise - const displacementPerlinUv = mainUv - .mul(0.5) - .add(vec2(0, time.negate().mul(0.25))) - .mod(1); - const displacementPerlinNoise = texture(perlinTexture, displacementPerlinUv, 0).sub(0.5).mul(1); - const displacedPerlinUv = mainUv - .add(vec2(0, time.negate().mul(0.5))) - .add(displacementPerlinNoise) - .mod(1); - const displacedPerlinNoise = texture(perlinTexture, displacedPerlinUv, 0).sub(0.5).mul(1); - mainUv.x.addAssign(displacedPerlinNoise.mul(0.5)); - - // cellular noise - const cellularUv = mainUv.add(vec2(0, time.negate().mul(1.5))).mod(1); - const cellularNoise = texture(cellularTexture, cellularUv, 0).r.oneMinus().smoothstep(0.25, 1); - - // shape - const shape = mainUv.sub(0.5).mul(vec2(6, 1)).length().step(0.5); - shape.assign(shape.mul(cellularNoise)); - shape.mulAssign(gradient3); - shape.assign(step(0.01, shape)); - - // output - return vec4(vec3(1), shape); - })(); - - // billboarding - follow the camera rotation only horizontally - - flame1Material.vertexNode = billboarding(); - flame2Material.vertexNode = billboarding(); - - // meshes - - const flame1 = new THREE.Sprite(flame1Material); - flame1.center.set(0.5, 0); - flame1.scale.x = 0.5; // optional - flame1.position.x = -0.5; - scene.add(flame1); - - const flame2 = new THREE.Sprite(flame2Material); - flame2.center.set(0.5, 0); - flame2.position.x = 0.5; - scene.add(flame2); - - // renderer - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - controls = new OrbitControls(camera, renderer.domElement); - controls.enableDamping = true; - controls.minDistance = 0.1; - controls.maxDistance = 50; - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -async function animate() { - controls.update(); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_video_panorama.ts b/examples-testing/examples/webgpu_video_panorama.ts deleted file mode 100644 index e409b3c07..000000000 --- a/examples-testing/examples/webgpu_video_panorama.ts +++ /dev/null @@ -1,99 +0,0 @@ -import * as THREE from 'three'; - -let camera, scene, renderer; - -let isUserInteracting = false, - lon = 0, - lat = 0, - phi = 0, - theta = 0, - onPointerDownPointerX = 0, - onPointerDownPointerY = 0, - onPointerDownLon = 0, - onPointerDownLat = 0; - -const distance = 0.5; - -init(); - -function init() { - const container = document.getElementById('container'); - - camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.25, 10); - - scene = new THREE.Scene(); - - const geometry = new THREE.SphereGeometry(5, 60, 40); - // invert the geometry on the x-axis so that all of the faces point inward - geometry.scale(-1, 1, 1); - - const video = document.getElementById('video'); - video.play(); - - const texture = new THREE.VideoTexture(video); - texture.colorSpace = THREE.SRGBColorSpace; - const material = new THREE.MeshBasicMaterial({ map: texture }); - - const mesh = new THREE.Mesh(geometry, material); - scene.add(mesh); - - renderer = new THREE.WebGPURenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - container.appendChild(renderer.domElement); - - document.addEventListener('pointerdown', onPointerDown); - document.addEventListener('pointermove', onPointerMove); - document.addEventListener('pointerup', onPointerUp); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onPointerDown(event) { - isUserInteracting = true; - - onPointerDownPointerX = event.clientX; - onPointerDownPointerY = event.clientY; - - onPointerDownLon = lon; - onPointerDownLat = lat; -} - -function onPointerMove(event) { - if (isUserInteracting === true) { - lon = (onPointerDownPointerX - event.clientX) * 0.1 + onPointerDownLon; - lat = (onPointerDownPointerY - event.clientY) * 0.1 + onPointerDownLat; - } -} - -function onPointerUp() { - isUserInteracting = false; -} - -function animate() { - update(); -} - -function update() { - lat = Math.max(-85, Math.min(85, lat)); - phi = THREE.MathUtils.degToRad(90 - lat); - theta = THREE.MathUtils.degToRad(lon); - - camera.position.x = distance * Math.sin(phi) * Math.cos(theta); - camera.position.y = distance * Math.cos(phi); - camera.position.z = distance * Math.sin(phi) * Math.sin(theta); - - camera.lookAt(0, 0, 0); - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webgpu_water.ts b/examples-testing/examples/webgpu_water.ts deleted file mode 100644 index 76e09f1f8..000000000 --- a/examples-testing/examples/webgpu_water.ts +++ /dev/null @@ -1,171 +0,0 @@ -import * as THREE from 'three'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { WaterMesh } from 'three/addons/objects/Water2Mesh.js'; - -let scene, camera, clock, renderer, water; - -let torusKnot; - -const params = { - color: '#ffffff', - scale: 4, - flowX: 1, - flowY: 1, -}; - -init(); - -function init() { - // scene - - scene = new THREE.Scene(); - - // camera - - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200); - camera.position.set(-15, 7, 15); - camera.lookAt(scene.position); - - // clock - - clock = new THREE.Clock(); - - // mesh - - const torusKnotGeometry = new THREE.TorusKnotGeometry(3, 1, 256, 32); - const torusKnotMaterial = new THREE.MeshNormalMaterial(); - - torusKnot = new THREE.Mesh(torusKnotGeometry, torusKnotMaterial); - torusKnot.position.y = 4; - torusKnot.scale.set(0.5, 0.5, 0.5); - scene.add(torusKnot); - - // ground - - const groundGeometry = new THREE.PlaneGeometry(20, 20); - const groundMaterial = new THREE.MeshStandardMaterial({ roughness: 0.8, metalness: 0.4 }); - const ground = new THREE.Mesh(groundGeometry, groundMaterial); - ground.rotation.x = Math.PI * -0.5; - scene.add(ground); - - const textureLoader = new THREE.TextureLoader(); - textureLoader.load('textures/hardwood2_diffuse.jpg', function (map) { - map.wrapS = THREE.RepeatWrapping; - map.wrapT = THREE.RepeatWrapping; - map.anisotropy = 16; - map.repeat.set(4, 4); - map.colorSpace = THREE.SRGBColorSpace; - groundMaterial.map = map; - groundMaterial.needsUpdate = true; - }); - - // - - const normalMap0 = textureLoader.load('textures/water/Water_1_M_Normal.jpg'); - const normalMap1 = textureLoader.load('textures/water/Water_2_M_Normal.jpg'); - - normalMap0.wrapS = normalMap0.wrapT = THREE.RepeatWrapping; - normalMap1.wrapS = normalMap1.wrapT = THREE.RepeatWrapping; - - // water - - const waterGeometry = new THREE.PlaneGeometry(20, 20); - - water = new WaterMesh(waterGeometry, { - color: params.color, - scale: params.scale, - flowDirection: new THREE.Vector2(params.flowX, params.flowY), - normalMap0: normalMap0, - normalMap1: normalMap1, - }); - - water.position.y = 1; - water.rotation.x = Math.PI * -0.5; - scene.add(water); - - // skybox - - const cubeTextureLoader = new THREE.CubeTextureLoader(); - cubeTextureLoader.setPath('textures/cube/Park2/'); - - const cubeTexture = cubeTextureLoader.load([ - 'posx.jpg', - 'negx.jpg', - 'posy.jpg', - 'negy.jpg', - 'posz.jpg', - 'negz.jpg', - ]); - - scene.background = cubeTexture; - - // light - - const ambientLight = new THREE.AmbientLight(0xe7e7e7, 1.2); - scene.add(ambientLight); - - const directionalLight = new THREE.DirectionalLight(0xffffff, 2); - directionalLight.position.set(-1, 1, 1); - scene.add(directionalLight); - - // renderer - - renderer = new THREE.WebGPURenderer(); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setAnimationLoop(animate); - document.body.appendChild(renderer.domElement); - - // gui - - const gui = new GUI(); - const waterNode = water.material.fragmentNode; - - gui.addColor(params, 'color').onChange(function (value) { - waterNode.color.value.set(value); - }); - gui.add(params, 'scale', 1, 10).onChange(function (value) { - waterNode.scale.value = value; - }); - gui.add(params, 'flowX', -1, 1) - .step(0.01) - .onChange(function (value) { - waterNode.flowDirection.value.x = value; - waterNode.flowDirection.value.normalize(); - }); - gui.add(params, 'flowY', -1, 1) - .step(0.01) - .onChange(function (value) { - waterNode.flowDirection.value.y = value; - waterNode.flowDirection.value.normalize(); - }); - - gui.open(); - - // - - const controls = new OrbitControls(camera, renderer.domElement); - controls.minDistance = 5; - controls.maxDistance = 50; - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const delta = clock.getDelta(); - - torusKnot.rotation.x += delta; - torusKnot.rotation.y += delta * 0.5; - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webxr_ar_cones.ts b/examples-testing/examples/webxr_ar_cones.ts deleted file mode 100644 index 95eb34393..000000000 --- a/examples-testing/examples/webxr_ar_cones.ts +++ /dev/null @@ -1,66 +0,0 @@ -import * as THREE from 'three'; -import { ARButton } from 'three/addons/webxr/ARButton.js'; - -let camera, scene, renderer; -let controller; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 20); - - const light = new THREE.HemisphereLight(0xffffff, 0xbbbbff, 3); - light.position.set(0.5, 1, 0.25); - scene.add(light); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.xr.enabled = true; - container.appendChild(renderer.domElement); - - // - - document.body.appendChild(ARButton.createButton(renderer)); - - // - - const geometry = new THREE.CylinderGeometry(0, 0.05, 0.2, 32).rotateX(Math.PI / 2); - - function onSelect() { - const material = new THREE.MeshPhongMaterial({ color: 0xffffff * Math.random() }); - const mesh = new THREE.Mesh(geometry, material); - mesh.position.set(0, 0, -0.3).applyMatrix4(controller.matrixWorld); - mesh.quaternion.setFromRotationMatrix(controller.matrixWorld); - scene.add(mesh); - } - - controller = renderer.xr.getController(0); - controller.addEventListener('select', onSelect); - scene.add(controller); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webxr_ar_hittest.ts b/examples-testing/examples/webxr_ar_hittest.ts deleted file mode 100644 index 1867cc470..000000000 --- a/examples-testing/examples/webxr_ar_hittest.ts +++ /dev/null @@ -1,115 +0,0 @@ -import * as THREE from 'three'; -import { ARButton } from 'three/addons/webxr/ARButton.js'; - -let container; -let camera, scene, renderer; -let controller; - -let reticle; - -let hitTestSource = null; -let hitTestSourceRequested = false; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 20); - - const light = new THREE.HemisphereLight(0xffffff, 0xbbbbff, 3); - light.position.set(0.5, 1, 0.25); - scene.add(light); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.xr.enabled = true; - container.appendChild(renderer.domElement); - - // - - document.body.appendChild(ARButton.createButton(renderer, { requiredFeatures: ['hit-test'] })); - - // - - const geometry = new THREE.CylinderGeometry(0.1, 0.1, 0.2, 32).translate(0, 0.1, 0); - - function onSelect() { - if (reticle.visible) { - const material = new THREE.MeshPhongMaterial({ color: 0xffffff * Math.random() }); - const mesh = new THREE.Mesh(geometry, material); - reticle.matrix.decompose(mesh.position, mesh.quaternion, mesh.scale); - mesh.scale.y = Math.random() * 2 + 1; - scene.add(mesh); - } - } - - controller = renderer.xr.getController(0); - controller.addEventListener('select', onSelect); - scene.add(controller); - - reticle = new THREE.Mesh( - new THREE.RingGeometry(0.15, 0.2, 32).rotateX(-Math.PI / 2), - new THREE.MeshBasicMaterial(), - ); - reticle.matrixAutoUpdate = false; - reticle.visible = false; - scene.add(reticle); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate(timestamp, frame) { - if (frame) { - const referenceSpace = renderer.xr.getReferenceSpace(); - const session = renderer.xr.getSession(); - - if (hitTestSourceRequested === false) { - session.requestReferenceSpace('viewer').then(function (referenceSpace) { - session.requestHitTestSource({ space: referenceSpace }).then(function (source) { - hitTestSource = source; - }); - }); - - session.addEventListener('end', function () { - hitTestSourceRequested = false; - hitTestSource = null; - }); - - hitTestSourceRequested = true; - } - - if (hitTestSource) { - const hitTestResults = frame.getHitTestResults(hitTestSource); - - if (hitTestResults.length) { - const hit = hitTestResults[0]; - - reticle.visible = true; - reticle.matrix.fromArray(hit.getPose(referenceSpace).transform.matrix); - } else { - reticle.visible = false; - } - } - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webxr_ar_lighting.ts b/examples-testing/examples/webxr_ar_lighting.ts deleted file mode 100644 index 9de23ad94..000000000 --- a/examples-testing/examples/webxr_ar_lighting.ts +++ /dev/null @@ -1,124 +0,0 @@ -import * as THREE from 'three'; -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; -import { ARButton } from 'three/addons/webxr/ARButton.js'; -import { XREstimatedLight } from 'three/addons/webxr/XREstimatedLight.js'; - -let camera, scene, renderer; -let controller; -let defaultEnvironment; - -init(); - -function init() { - const container = document.createElement('div'); - document.body.appendChild(container); - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 20); - - const defaultLight = new THREE.HemisphereLight(0xffffff, 0xbbbbff, 1); - defaultLight.position.set(0.5, 1, 0.25); - scene.add(defaultLight); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.xr.enabled = true; - container.appendChild(renderer.domElement); - - // Don't add the XREstimatedLight to the scene initially. - // It doesn't have any estimated lighting values until an AR session starts. - - const xrLight = new XREstimatedLight(renderer); - - xrLight.addEventListener('estimationstart', () => { - // Swap the default light out for the estimated one one we start getting some estimated values. - scene.add(xrLight); - scene.remove(defaultLight); - - // The estimated lighting also provides an environment cubemap, which we can apply here. - if (xrLight.environment) { - scene.environment = xrLight.environment; - } - }); - - xrLight.addEventListener('estimationend', () => { - // Swap the lights back when we stop receiving estimated values. - scene.add(defaultLight); - scene.remove(xrLight); - - // Revert back to the default environment. - scene.environment = defaultEnvironment; - }); - - // - - new RGBELoader().setPath('textures/equirectangular/').load('royal_esplanade_1k.hdr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - - defaultEnvironment = texture; - - scene.environment = defaultEnvironment; - }); - - // - - // In order for lighting estimation to work, 'light-estimation' must be included as either an optional or required feature. - document.body.appendChild(ARButton.createButton(renderer, { optionalFeatures: ['light-estimation'] })); - - // - - const ballGeometry = new THREE.SphereGeometry(0.175, 32, 32); - const ballGroup = new THREE.Group(); - ballGroup.position.z = -2; - - const rows = 3; - const cols = 3; - - for (let i = 0; i < rows; i++) { - for (let j = 0; j < cols; j++) { - const ballMaterial = new THREE.MeshStandardMaterial({ - color: 0xdddddd, - roughness: i / rows, - metalness: j / cols, - }); - const ballMesh = new THREE.Mesh(ballGeometry, ballMaterial); - ballMesh.position.set((i + 0.5 - rows * 0.5) * 0.4, (j + 0.5 - cols * 0.5) * 0.4, 0); - ballGroup.add(ballMesh); - } - } - - scene.add(ballGroup); - - // - - function onSelect() { - ballGroup.position.set(0, 0, -2).applyMatrix4(controller.matrixWorld); - ballGroup.quaternion.setFromRotationMatrix(controller.matrixWorld); - } - - controller = renderer.xr.getController(0); - controller.addEventListener('select', onSelect); - scene.add(controller); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webxr_ar_plane_detection.ts b/examples-testing/examples/webxr_ar_plane_detection.ts deleted file mode 100644 index 841b6b04b..000000000 --- a/examples-testing/examples/webxr_ar_plane_detection.ts +++ /dev/null @@ -1,46 +0,0 @@ -import * as THREE from 'three'; -import { ARButton } from 'three/addons/webxr/ARButton.js'; -import { XRPlanes } from 'three/addons/webxr/XRPlanes.js'; - -// - -const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); -renderer.setPixelRatio(window.devicePixelRatio); -renderer.setSize(window.innerWidth, window.innerHeight); -renderer.setAnimationLoop(animate); -renderer.xr.enabled = true; -document.body.appendChild(renderer.domElement); - -document.body.appendChild( - ARButton.createButton(renderer, { - requiredFeatures: ['plane-detection'], - }), -); - -window.addEventListener('resize', onWindowResize); - -// - -const scene = new THREE.Scene(); - -const planes = new XRPlanes(renderer); -scene.add(planes); - -const camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 20); - -const light = new THREE.HemisphereLight(0xffffff, 0xbbbbff, 3); -light.position.set(0.5, 1, 0.25); -scene.add(light); - -// - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webxr_vr_handinput.ts b/examples-testing/examples/webxr_vr_handinput.ts deleted file mode 100644 index d746e4582..000000000 --- a/examples-testing/examples/webxr_vr_handinput.ts +++ /dev/null @@ -1,126 +0,0 @@ -import * as THREE from 'three'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { VRButton } from 'three/addons/webxr/VRButton.js'; -import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js'; -import { XRHandModelFactory } from 'three/addons/webxr/XRHandModelFactory.js'; - -let container; -let camera, scene, renderer; -let hand1, hand2; -let controller1, controller2; -let controllerGrip1, controllerGrip2; - -let controls; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x444444); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 10); - camera.position.set(0, 1.6, 3); - - controls = new OrbitControls(camera, container); - controls.target.set(0, 1.6, 0); - controls.update(); - - const floorGeometry = new THREE.PlaneGeometry(4, 4); - const floorMaterial = new THREE.MeshStandardMaterial({ color: 0x666666 }); - const floor = new THREE.Mesh(floorGeometry, floorMaterial); - floor.rotation.x = -Math.PI / 2; - floor.receiveShadow = true; - scene.add(floor); - - scene.add(new THREE.HemisphereLight(0xbcbcbc, 0xa5a5a5, 3)); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0, 6, 0); - light.castShadow = true; - light.shadow.camera.top = 2; - light.shadow.camera.bottom = -2; - light.shadow.camera.right = 2; - light.shadow.camera.left = -2; - light.shadow.mapSize.set(4096, 4096); - scene.add(light); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - renderer.xr.enabled = true; - - container.appendChild(renderer.domElement); - - const sessionInit = { - requiredFeatures: ['hand-tracking'], - }; - - document.body.appendChild(VRButton.createButton(renderer, sessionInit)); - - // controllers - - controller1 = renderer.xr.getController(0); - scene.add(controller1); - - controller2 = renderer.xr.getController(1); - scene.add(controller2); - - const controllerModelFactory = new XRControllerModelFactory(); - const handModelFactory = new XRHandModelFactory(); - - // Hand 1 - controllerGrip1 = renderer.xr.getControllerGrip(0); - controllerGrip1.add(controllerModelFactory.createControllerModel(controllerGrip1)); - scene.add(controllerGrip1); - - hand1 = renderer.xr.getHand(0); - hand1.add(handModelFactory.createHandModel(hand1)); - - scene.add(hand1); - - // Hand 2 - controllerGrip2 = renderer.xr.getControllerGrip(1); - controllerGrip2.add(controllerModelFactory.createControllerModel(controllerGrip2)); - scene.add(controllerGrip2); - - hand2 = renderer.xr.getHand(1); - hand2.add(handModelFactory.createHandModel(hand2)); - scene.add(hand2); - - // - - const geometry = new THREE.BufferGeometry().setFromPoints([ - new THREE.Vector3(0, 0, 0), - new THREE.Vector3(0, 0, -1), - ]); - - const line = new THREE.Line(geometry); - line.name = 'line'; - line.scale.z = 5; - - controller1.add(line.clone()); - controller2.add(line.clone()); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} -// - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webxr_vr_panorama.ts b/examples-testing/examples/webxr_vr_panorama.ts deleted file mode 100644 index 535e1c937..000000000 --- a/examples-testing/examples/webxr_vr_panorama.ts +++ /dev/null @@ -1,92 +0,0 @@ -import * as THREE from 'three'; -import { VRButton } from 'three/addons/webxr/VRButton.js'; - -let camera; -let renderer; -let scene; - -init(); - -function init() { - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.xr.enabled = true; - renderer.xr.setReferenceSpaceType('local'); - document.body.appendChild(renderer.domElement); - - document.body.appendChild(VRButton.createButton(renderer)); - - // - - scene = new THREE.Scene(); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); - camera.layers.enable(1); - - const geometry = new THREE.BoxGeometry(100, 100, 100); - geometry.scale(1, 1, -1); - - const textures = getTexturesFromAtlasFile('textures/cube/sun_temple_stripe_stereo.jpg', 12); - - const materials = []; - - for (let i = 0; i < 6; i++) { - materials.push(new THREE.MeshBasicMaterial({ map: textures[i] })); - } - - const skyBox = new THREE.Mesh(geometry, materials); - skyBox.layers.set(1); - scene.add(skyBox); - - const materialsR = []; - - for (let i = 6; i < 12; i++) { - materialsR.push(new THREE.MeshBasicMaterial({ map: textures[i] })); - } - - const skyBoxR = new THREE.Mesh(geometry, materialsR); - skyBoxR.layers.set(2); - scene.add(skyBoxR); - - window.addEventListener('resize', onWindowResize); -} - -function getTexturesFromAtlasFile(atlasImgUrl, tilesNum) { - const textures = []; - - for (let i = 0; i < tilesNum; i++) { - textures[i] = new THREE.Texture(); - } - - const loader = new THREE.ImageLoader(); - loader.load(atlasImgUrl, function (imageObj) { - let canvas, context; - const tileWidth = imageObj.height; - - for (let i = 0; i < textures.length; i++) { - canvas = document.createElement('canvas'); - context = canvas.getContext('2d'); - canvas.height = tileWidth; - canvas.width = tileWidth; - context.drawImage(imageObj, tileWidth * i, 0, tileWidth, tileWidth, 0, 0, tileWidth, tileWidth); - textures[i].colorSpace = THREE.SRGBColorSpace; - textures[i].image = canvas; - textures[i].needsUpdate = true; - } - }); - - return textures; -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webxr_vr_panorama_depth.ts b/examples-testing/examples/webxr_vr_panorama_depth.ts deleted file mode 100644 index 66215469d..000000000 --- a/examples-testing/examples/webxr_vr_panorama_depth.ts +++ /dev/null @@ -1,90 +0,0 @@ -import * as THREE from 'three'; -import { VRButton } from 'three/addons/webxr/VRButton.js'; - -let camera, scene, renderer, sphere, clock; - -init(); - -function init() { - const container = document.getElementById('container'); - - clock = new THREE.Clock(); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x101010); - - const light = new THREE.AmbientLight(0xffffff, 3); - scene.add(light); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 2000); - scene.add(camera); - - // Create the panoramic sphere geometery - const panoSphereGeo = new THREE.SphereGeometry(6, 256, 256); - - // Create the panoramic sphere material - const panoSphereMat = new THREE.MeshStandardMaterial({ - side: THREE.BackSide, - displacementScale: -4.0, - }); - - // Create the panoramic sphere mesh - sphere = new THREE.Mesh(panoSphereGeo, panoSphereMat); - - // Load and assign the texture and depth map - const manager = new THREE.LoadingManager(); - const loader = new THREE.TextureLoader(manager); - - loader.load('./textures/kandao3.jpg', function (texture) { - texture.colorSpace = THREE.SRGBColorSpace; - texture.minFilter = THREE.NearestFilter; - texture.generateMipmaps = false; - sphere.material.map = texture; - }); - - loader.load('./textures/kandao3_depthmap.jpg', function (depth) { - depth.minFilter = THREE.NearestFilter; - depth.generateMipmaps = false; - sphere.material.displacementMap = depth; - }); - - // On load complete add the panoramic sphere to the scene - manager.onLoad = function () { - scene.add(sphere); - }; - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.xr.enabled = true; - renderer.xr.setReferenceSpaceType('local'); - container.appendChild(renderer.domElement); - - document.body.appendChild(VRButton.createButton(renderer)); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - // If we are not presenting move the camera a little so the effect is visible - - if (renderer.xr.isPresenting === false) { - const time = clock.getElapsedTime(); - - sphere.rotation.y += 0.001; - sphere.position.x = Math.sin(time) * 0.2; - sphere.position.z = Math.cos(time) * 0.2; - } - - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webxr_vr_rollercoaster.ts b/examples-testing/examples/webxr_vr_rollercoaster.ts deleted file mode 100644 index b8c35a9e3..000000000 --- a/examples-testing/examples/webxr_vr_rollercoaster.ts +++ /dev/null @@ -1,211 +0,0 @@ -import * as THREE from 'three'; - -import { - RollerCoasterGeometry, - RollerCoasterShadowGeometry, - RollerCoasterLiftersGeometry, - TreesGeometry, - SkyGeometry, -} from 'three/addons/misc/RollerCoaster.js'; -import { VRButton } from 'three/addons/webxr/VRButton.js'; - -let mesh, material, geometry; - -const renderer = new THREE.WebGLRenderer({ antialias: true }); -renderer.setPixelRatio(window.devicePixelRatio); -renderer.setSize(window.innerWidth, window.innerHeight); -renderer.setAnimationLoop(animate); -renderer.xr.enabled = true; -renderer.xr.setReferenceSpaceType('local'); -document.body.appendChild(renderer.domElement); - -document.body.appendChild(VRButton.createButton(renderer)); - -// - -const scene = new THREE.Scene(); -scene.background = new THREE.Color(0xf0f0ff); - -const light = new THREE.HemisphereLight(0xfff0f0, 0x60606, 3); -light.position.set(1, 1, 1); -scene.add(light); - -const train = new THREE.Object3D(); -scene.add(train); - -const camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 500); -train.add(camera); - -// environment - -geometry = new THREE.PlaneGeometry(500, 500, 15, 15); -geometry.rotateX(-Math.PI / 2); - -const positions = geometry.attributes.position.array; -const vertex = new THREE.Vector3(); - -for (let i = 0; i < positions.length; i += 3) { - vertex.fromArray(positions, i); - - vertex.x += Math.random() * 10 - 5; - vertex.z += Math.random() * 10 - 5; - - const distance = vertex.distanceTo(scene.position) / 5 - 25; - vertex.y = Math.random() * Math.max(0, distance); - - vertex.toArray(positions, i); -} - -geometry.computeVertexNormals(); - -material = new THREE.MeshLambertMaterial({ - color: 0x407000, -}); - -mesh = new THREE.Mesh(geometry, material); -scene.add(mesh); - -geometry = new TreesGeometry(mesh); -material = new THREE.MeshBasicMaterial({ - side: THREE.DoubleSide, - vertexColors: true, -}); -mesh = new THREE.Mesh(geometry, material); -scene.add(mesh); - -geometry = new SkyGeometry(); -material = new THREE.MeshBasicMaterial({ color: 0xffffff }); -mesh = new THREE.Mesh(geometry, material); -scene.add(mesh); - -// - -const PI2 = Math.PI * 2; - -const curve = (function () { - const vector = new THREE.Vector3(); - const vector2 = new THREE.Vector3(); - - return { - getPointAt: function (t) { - t = t * PI2; - - const x = Math.sin(t * 3) * Math.cos(t * 4) * 50; - const y = Math.sin(t * 10) * 2 + Math.cos(t * 17) * 2 + 5; - const z = Math.sin(t) * Math.sin(t * 4) * 50; - - return vector.set(x, y, z).multiplyScalar(2); - }, - - getTangentAt: function (t) { - const delta = 0.0001; - const t1 = Math.max(0, t - delta); - const t2 = Math.min(1, t + delta); - - return vector2.copy(this.getPointAt(t2)).sub(this.getPointAt(t1)).normalize(); - }, - }; -})(); - -geometry = new RollerCoasterGeometry(curve, 1500); -material = new THREE.MeshPhongMaterial({ - vertexColors: true, -}); -mesh = new THREE.Mesh(geometry, material); -scene.add(mesh); - -geometry = new RollerCoasterLiftersGeometry(curve, 100); -material = new THREE.MeshPhongMaterial(); -mesh = new THREE.Mesh(geometry, material); -mesh.position.y = 0.1; -scene.add(mesh); - -geometry = new RollerCoasterShadowGeometry(curve, 500); -material = new THREE.MeshBasicMaterial({ - color: 0x305000, - depthWrite: false, - transparent: true, -}); -mesh = new THREE.Mesh(geometry, material); -mesh.position.y = 0.1; -scene.add(mesh); - -const funfairs = []; - -// - -geometry = new THREE.CylinderGeometry(10, 10, 5, 15); -material = new THREE.MeshLambertMaterial({ - color: 0xff8080, -}); -mesh = new THREE.Mesh(geometry, material); -mesh.position.set(-80, 10, -70); -mesh.rotation.x = Math.PI / 2; -scene.add(mesh); - -funfairs.push(mesh); - -geometry = new THREE.CylinderGeometry(5, 6, 4, 10); -material = new THREE.MeshLambertMaterial({ - color: 0x8080ff, -}); -mesh = new THREE.Mesh(geometry, material); -mesh.position.set(50, 2, 30); -scene.add(mesh); - -funfairs.push(mesh); - -// - -window.addEventListener('resize', onWindowResize); - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -const position = new THREE.Vector3(); -const tangent = new THREE.Vector3(); - -const lookAt = new THREE.Vector3(); - -let velocity = 0; -let progress = 0; - -let prevTime = performance.now(); - -function animate() { - const time = performance.now(); - const delta = time - prevTime; - - for (let i = 0; i < funfairs.length; i++) { - funfairs[i].rotation.y = time * 0.0004; - } - - // - - progress += velocity; - progress = progress % 1; - - position.copy(curve.getPointAt(progress)); - position.y += 0.3; - - train.position.copy(position); - - tangent.copy(curve.getTangentAt(progress)); - - velocity -= tangent.y * 0.0000001 * delta; - velocity = Math.max(0.00004, Math.min(0.0002, velocity)); - - train.lookAt(lookAt.copy(position).sub(tangent)); - - // - - renderer.render(scene, camera); - - prevTime = time; -} diff --git a/examples-testing/examples/webxr_vr_sandbox.ts b/examples-testing/examples/webxr_vr_sandbox.ts deleted file mode 100644 index 9e8e75909..000000000 --- a/examples-testing/examples/webxr_vr_sandbox.ts +++ /dev/null @@ -1,192 +0,0 @@ -import * as THREE from 'three'; - -import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'; -import { Reflector } from 'three/addons/objects/Reflector.js'; -import { VRButton } from 'three/addons/webxr/VRButton.js'; - -import { HTMLMesh } from 'three/addons/interactive/HTMLMesh.js'; -import { InteractiveGroup } from 'three/addons/interactive/InteractiveGroup.js'; -import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js'; - -import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; -import Stats from 'three/addons/libs/stats.module.js'; - -let camera, scene, renderer; -let reflector; -let stats, statsMesh; - -const parameters = { - radius: 0.6, - tube: 0.2, - tubularSegments: 150, - radialSegments: 20, - p: 2, - q: 3, - thickness: 0.5, -}; - -init(); - -function init() { - scene = new THREE.Scene(); - - new RGBELoader().setPath('textures/equirectangular/').load('moonless_golf_1k.hdr', function (texture) { - texture.mapping = THREE.EquirectangularReflectionMapping; - - scene.background = texture; - scene.environment = texture; - }); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 10); - camera.position.set(0, 1.6, 1.5); - - // - - const torusGeometry = new THREE.TorusKnotGeometry(...Object.values(parameters)); - const torusMaterial = new THREE.MeshPhysicalMaterial({ - transmission: 1.0, - roughness: 0, - metalness: 0.25, - thickness: 0.5, - side: THREE.DoubleSide, - }); - const torus = new THREE.Mesh(torusGeometry, torusMaterial); - torus.name = 'torus'; - torus.position.y = 1.5; - torus.position.z = -2; - scene.add(torus); - - const cylinderGeometry = new THREE.CylinderGeometry(1, 1, 0.1, 50); - const cylinderMaterial = new THREE.MeshStandardMaterial(); - const cylinder = new THREE.Mesh(cylinderGeometry, cylinderMaterial); - cylinder.position.z = -2; - scene.add(cylinder); - - // - - reflector = new Reflector(new THREE.PlaneGeometry(2, 2), { - textureWidth: window.innerWidth, - textureHeight: window.innerHeight, - }); - reflector.position.x = 1; - reflector.position.y = 1.5; - reflector.position.z = -3; - reflector.rotation.y = -Math.PI / 4; - scene.add(reflector); - - const frameGeometry = new THREE.BoxGeometry(2.1, 2.1, 0.1); - const frameMaterial = new THREE.MeshPhongMaterial(); - const frame = new THREE.Mesh(frameGeometry, frameMaterial); - frame.position.z = -0.07; - reflector.add(frame); - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.autoClear = false; - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.xr.enabled = true; - renderer.toneMapping = THREE.ACESFilmicToneMapping; - renderer.toneMappingExposure = 1; - document.body.appendChild(renderer.domElement); - - document.body.appendChild(VRButton.createButton(renderer)); - - window.addEventListener('resize', onWindowResize); - - // - - const geometry = new THREE.BufferGeometry(); - geometry.setFromPoints([new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 0, -5)]); - - const controller1 = renderer.xr.getController(0); - controller1.add(new THREE.Line(geometry)); - scene.add(controller1); - - const controller2 = renderer.xr.getController(1); - controller2.add(new THREE.Line(geometry)); - scene.add(controller2); - - // - - const controllerModelFactory = new XRControllerModelFactory(); - - const controllerGrip1 = renderer.xr.getControllerGrip(0); - controllerGrip1.add(controllerModelFactory.createControllerModel(controllerGrip1)); - scene.add(controllerGrip1); - - const controllerGrip2 = renderer.xr.getControllerGrip(1); - controllerGrip2.add(controllerModelFactory.createControllerModel(controllerGrip2)); - scene.add(controllerGrip2); - - // GUI - - function onChange() { - torus.geometry.dispose(); - torus.geometry = new THREE.TorusKnotGeometry(...Object.values(parameters)); - } - - function onThicknessChange() { - torus.material.thickness = parameters.thickness; - } - - const gui = new GUI({ width: 300 }); - gui.add(parameters, 'radius', 0.0, 1.0).onChange(onChange); - gui.add(parameters, 'tube', 0.0, 1.0).onChange(onChange); - gui.add(parameters, 'tubularSegments', 10, 150, 1).onChange(onChange); - gui.add(parameters, 'radialSegments', 2, 20, 1).onChange(onChange); - gui.add(parameters, 'p', 1, 10, 1).onChange(onChange); - gui.add(parameters, 'q', 0, 10, 1).onChange(onChange); - gui.add(parameters, 'thickness', 0, 1).onChange(onThicknessChange); - gui.domElement.style.visibility = 'hidden'; - - const group = new InteractiveGroup(); - group.listenToPointerEvents(renderer, camera); - group.listenToXRControllerEvents(controller1); - group.listenToXRControllerEvents(controller2); - scene.add(group); - - const mesh = new HTMLMesh(gui.domElement); - mesh.position.x = -0.75; - mesh.position.y = 1.5; - mesh.position.z = -0.5; - mesh.rotation.y = Math.PI / 4; - mesh.scale.setScalar(2); - group.add(mesh); - - // Add stats.js - stats = new Stats(); - stats.dom.style.width = '80px'; - stats.dom.style.height = '48px'; - document.body.appendChild(stats.dom); - - statsMesh = new HTMLMesh(stats.dom); - statsMesh.position.x = -0.75; - statsMesh.position.y = 2; - statsMesh.position.z = -0.6; - statsMesh.rotation.y = Math.PI / 4; - statsMesh.scale.setScalar(2.5); - group.add(statsMesh); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - const time = performance.now() * 0.0002; - const torus = scene.getObjectByName('torus'); - torus.rotation.x = time * 0.4; - torus.rotation.y = time; - - renderer.render(scene, camera); - stats.update(); - - // Canvas elements doesn't trigger DOM updates, so we have to update the texture - statsMesh.material.map.update(); -} diff --git a/examples-testing/examples/webxr_vr_video.ts b/examples-testing/examples/webxr_vr_video.ts deleted file mode 100644 index 50a990412..000000000 --- a/examples-testing/examples/webxr_vr_video.ts +++ /dev/null @@ -1,92 +0,0 @@ -import * as THREE from 'three'; -import { VRButton } from 'three/addons/webxr/VRButton.js'; - -let camera, scene, renderer; - -init(); - -function init() { - const container = document.getElementById('container'); - container.addEventListener('click', function () { - video.play(); - }); - - camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 2000); - camera.layers.enable(1); // render left view when no stereo available - - // video - - const video = document.getElementById('video'); - video.play(); - - const texture = new THREE.VideoTexture(video); - texture.colorSpace = THREE.SRGBColorSpace; - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x101010); - - // left - - const geometry1 = new THREE.SphereGeometry(500, 60, 40); - // invert the geometry on the x-axis so that all of the faces point inward - geometry1.scale(-1, 1, 1); - - const uvs1 = geometry1.attributes.uv.array; - - for (let i = 0; i < uvs1.length; i += 2) { - uvs1[i] *= 0.5; - } - - const material1 = new THREE.MeshBasicMaterial({ map: texture }); - - const mesh1 = new THREE.Mesh(geometry1, material1); - mesh1.rotation.y = -Math.PI / 2; - mesh1.layers.set(1); // display in left eye only - scene.add(mesh1); - - // right - - const geometry2 = new THREE.SphereGeometry(500, 60, 40); - geometry2.scale(-1, 1, 1); - - const uvs2 = geometry2.attributes.uv.array; - - for (let i = 0; i < uvs2.length; i += 2) { - uvs2[i] *= 0.5; - uvs2[i] += 0.5; - } - - const material2 = new THREE.MeshBasicMaterial({ map: texture }); - - const mesh2 = new THREE.Mesh(geometry2, material2); - mesh2.rotation.y = -Math.PI / 2; - mesh2.layers.set(2); // display in right eye only - scene.add(mesh2); - - // - - renderer = new THREE.WebGLRenderer(); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.xr.enabled = true; - renderer.xr.setReferenceSpaceType('local'); - container.appendChild(renderer.domElement); - - document.body.appendChild(VRButton.createButton(renderer)); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webxr_xr_controls_transform.ts b/examples-testing/examples/webxr_xr_controls_transform.ts deleted file mode 100644 index f3b4796e6..000000000 --- a/examples-testing/examples/webxr_xr_controls_transform.ts +++ /dev/null @@ -1,210 +0,0 @@ -import * as THREE from 'three'; -import { TransformControls } from 'three/addons/controls/TransformControls.js'; -import { XRButton } from 'three/addons/webxr/XRButton.js'; -import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js'; - -let container; -let camera, scene, renderer; -let controller1, controller2, line; -let controllerGrip1, controllerGrip2; - -let raycaster; - -let controls, group; - -init(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x808080); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 10); - camera.position.set(0, 1.6, 0); - - const floorGeometry = new THREE.PlaneGeometry(6, 6); - const floorMaterial = new THREE.ShadowMaterial({ - opacity: 0.25, - blending: THREE.CustomBlending, - transparent: false, - }); - const floor = new THREE.Mesh(floorGeometry, floorMaterial); - floor.rotation.x = -Math.PI / 2; - floor.receiveShadow = true; - scene.add(floor); - - scene.add(new THREE.HemisphereLight(0xbcbcbc, 0xa5a5a5, 3)); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0, 6, 0); - light.castShadow = true; - light.shadow.camera.top = 3; - light.shadow.camera.bottom = -3; - light.shadow.camera.right = 3; - light.shadow.camera.left = -3; - light.shadow.mapSize.set(4096, 4096); - scene.add(light); - - group = new THREE.Group(); - scene.add(group); - - const geometries = [ - new THREE.BoxGeometry(0.2, 0.2, 0.2), - new THREE.ConeGeometry(0.2, 0.4, 64), - new THREE.CylinderGeometry(0.2, 0.2, 0.2, 64), - new THREE.IcosahedronGeometry(0.2, 8), - new THREE.TorusGeometry(0.2, 0.04, 64, 32), - ]; - - for (let i = 0; i < 16; i++) { - const geometry = geometries[Math.floor(Math.random() * geometries.length)]; - const material = new THREE.MeshStandardMaterial({ - color: Math.random() * 0xffffff, - roughness: 0.7, - metalness: 0.0, - }); - - const object = new THREE.Mesh(geometry, material); - - object.position.x = Math.random() - 0.5; - object.position.y = Math.random() * 2 + 0.5; - object.position.z = Math.random() - 2.5; - - object.rotation.x = Math.random() * 2 * Math.PI; - object.rotation.y = Math.random() * 2 * Math.PI; - object.rotation.z = Math.random() * 2 * Math.PI; - - object.scale.setScalar(Math.random() + 0.5); - - object.castShadow = true; - object.receiveShadow = true; - - group.add(object); - } - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.setAnimationLoop(animate); - renderer.shadowMap.enabled = true; - renderer.xr.enabled = true; - container.appendChild(renderer.domElement); - - document.body.appendChild(XRButton.createButton(renderer)); - - // controllers - - controller1 = renderer.xr.getController(0); - controller1.addEventListener('select', onSelect); - controller1.addEventListener('selectstart', onControllerEvent); - controller1.addEventListener('selectend', onControllerEvent); - controller1.addEventListener('move', onControllerEvent); - controller1.userData.active = false; - scene.add(controller1); - - controller2 = renderer.xr.getController(1); - controller2.addEventListener('select', onSelect); - controller2.addEventListener('selectstart', onControllerEvent); - controller2.addEventListener('selectend', onControllerEvent); - controller2.addEventListener('move', onControllerEvent); - controller2.userData.active = true; - scene.add(controller2); - - const controllerModelFactory = new XRControllerModelFactory(); - - controllerGrip1 = renderer.xr.getControllerGrip(0); - controllerGrip1.add(controllerModelFactory.createControllerModel(controllerGrip1)); - scene.add(controllerGrip1); - - controllerGrip2 = renderer.xr.getControllerGrip(1); - controllerGrip2.add(controllerModelFactory.createControllerModel(controllerGrip2)); - scene.add(controllerGrip2); - - // - - const geometry = new THREE.BufferGeometry().setFromPoints([ - new THREE.Vector3(0, 0, 0), - new THREE.Vector3(0, 0, -1), - ]); - - line = new THREE.Line(geometry); - line.name = 'line'; - line.scale.z = 5; - - raycaster = new THREE.Raycaster(); - - // controls - - controls = new TransformControls(camera, renderer.domElement); - controls.attach(group.children[0]); - scene.add(controls); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onSelect(event) { - const controller = event.target; - - controller1.userData.active = false; - controller2.userData.active = false; - - if (controller === controller1) { - controller1.userData.active = true; - controller1.add(line); - } - - if (controller === controller2) { - controller2.userData.active = true; - controller2.add(line); - } - - raycaster.setFromXRController(controller); - - const intersects = raycaster.intersectObjects(group.children); - - if (intersects.length > 0) { - controls.attach(intersects[0].object); - } -} - -function onControllerEvent(event) { - const controller = event.target; - - if (controller.userData.active === false) return; - - controls.getRaycaster().setFromXRController(controller); - - switch (event.type) { - case 'selectstart': - controls.pointerDown(null); - break; - - case 'selectend': - controls.pointerUp(null); - break; - - case 'move': - controls.pointerHover(null); - controls.pointerMove(null); - break; - } -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -// - -function animate() { - renderer.render(scene, camera); -} diff --git a/examples-testing/examples/webxr_xr_dragging_custom_depth.ts b/examples-testing/examples/webxr_xr_dragging_custom_depth.ts deleted file mode 100644 index 2cd50ba4c..000000000 --- a/examples-testing/examples/webxr_xr_dragging_custom_depth.ts +++ /dev/null @@ -1,395 +0,0 @@ -import * as THREE from 'three'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; -import { XRButton } from 'three/addons/webxr/XRButton.js'; -import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js'; - -let container; -let camera, scene, renderer; -let controller1, controller2; -let controllerGrip1, controllerGrip2; -let isDepthSupplied = false; - -let raycaster; - -const intersected = []; -const tempMatrix = new THREE.Matrix4(); - -let controls, group; - -init(); -animate(); - -function init() { - container = document.createElement('div'); - document.body.appendChild(container); - - scene = new THREE.Scene(); - scene.background = new THREE.Color(0x808080); - - camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 10); - camera.position.set(0, 1.6, 3); - - controls = new OrbitControls(camera, container); - controls.target.set(0, 1.6, 0); - controls.update(); - - const floorGeometry = new THREE.PlaneGeometry(6, 6); - const floorMaterial = new THREE.ShadowMaterial({ - opacity: 0.25, - blending: THREE.CustomBlending, - transparent: false, - }); - const floor = new THREE.Mesh(floorGeometry, floorMaterial); - floor.rotation.x = -Math.PI / 2; - floor.receiveShadow = true; - scene.add(floor); - - scene.add(new THREE.HemisphereLight(0xbcbcbc, 0xa5a5a5, 3)); - - const light = new THREE.DirectionalLight(0xffffff, 3); - light.position.set(0, 6, 0); - light.castShadow = true; - light.shadow.camera.top = 3; - light.shadow.camera.bottom = -3; - light.shadow.camera.right = 3; - light.shadow.camera.left = -3; - light.shadow.mapSize.set(4096, 4096); - scene.add(light); - - group = new THREE.Group(); - scene.add(group); - - const geometries = [ - new THREE.BoxGeometry(0.2, 0.2, 0.2), - new THREE.ConeGeometry(0.2, 0.2, 64), - new THREE.CylinderGeometry(0.2, 0.2, 0.2, 64), - new THREE.IcosahedronGeometry(0.2, 8), - new THREE.TorusGeometry(0.2, 0.04, 64, 32), - ]; - - for (let i = 0; i < 50; i++) { - const geometry = geometries[Math.floor(Math.random() * geometries.length)]; - const material = new THREE.ShaderMaterial({ - vertexShader: /* glsl */ ` - varying vec3 vNormal; - varying vec2 vUv; - void main() { - vNormal = normalize(normalMatrix * normal); - vUv = uv; - gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); - } - `, - - fragmentShader: /* glsl */ ` - uniform vec3 diffuseColor; - uniform float roughness; - uniform float metalness; - uniform float emissive; - varying vec3 vNormal; - varying vec2 vUv; - uniform sampler2DArray depthColor; - uniform float depthWidth; - uniform float depthHeight; - #define saturate( a ) clamp( a, 0.0, 1.0 ) - float Depth_GetCameraDepthInMeters(const sampler2DArray depthTexture, - const vec2 depthUv, int arrayIndex) { - return texture(depthColor, vec3(depthUv.x, depthUv.y, arrayIndex)).r; - } - float Depth_GetOcclusion(const sampler2DArray depthTexture, const vec2 depthUv, float assetDepthM, int arrayIndex) { - float depthMm = Depth_GetCameraDepthInMeters(depthTexture, depthUv, arrayIndex); - const float kDepthTolerancePerM = 0.001; - return clamp(1.0 - - 0.5 * (depthMm - assetDepthM) / - (kDepthTolerancePerM * assetDepthM) + - 0.5, 0.0, 1.0); - } - float Depth_GetBlurredOcclusionAroundUV(const sampler2DArray depthTexture, const vec2 uv, float assetDepthM, int arrayIndex) { - // Kernel used: - // 0 4 7 4 0 - // 4 16 26 16 4 - // 7 26 41 26 7 - // 4 16 26 16 4 - // 0 4 7 4 0 - const float kKernelTotalWeights = 269.0; - float sum = 0.0; - const float kOcclusionBlurAmount = 0.0005; - vec2 blurriness = - vec2(kOcclusionBlurAmount, kOcclusionBlurAmount /** u_DepthAspectRatio*/); - float current = 0.0; - current += Depth_GetOcclusion( - depthTexture, uv + vec2(-1.0, -2.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(+1.0, -2.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(-1.0, +2.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(+1.0, +2.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(-2.0, +1.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(+2.0, +1.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(-2.0, -1.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(+2.0, -1.0) * blurriness, assetDepthM, arrayIndex); - sum += current * 4.0; - current = 0.0; - current += Depth_GetOcclusion( - depthTexture, uv + vec2(-2.0, -0.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(+2.0, +0.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(+0.0, +2.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(-0.0, -2.0) * blurriness, assetDepthM, arrayIndex); - sum += current * 7.0; - current = 0.0; - current += Depth_GetOcclusion( - depthTexture, uv + vec2(-1.0, -1.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(+1.0, -1.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(-1.0, +1.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(+1.0, +1.0) * blurriness, assetDepthM, arrayIndex); - sum += current * 16.0; - current = 0.0; - current += Depth_GetOcclusion( - depthTexture, uv + vec2(+0.0, +1.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(-0.0, -1.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(-1.0, -0.0) * blurriness, assetDepthM, arrayIndex); - current += Depth_GetOcclusion( - depthTexture, uv + vec2(+1.0, +0.0) * blurriness, assetDepthM, arrayIndex); - sum += current * 26.0; - sum += Depth_GetOcclusion(depthTexture, uv, assetDepthM, arrayIndex) * 41.0; - return sum / kKernelTotalWeights; - } - void main() { - vec3 normal = normalize(vNormal); - vec3 diffuse = diffuseColor; - float specularIntensity = pow(max(dot(normal, normalize(vec3(0, 0, 1))), 0.0), 64.0); - vec3 specular = vec3(specularIntensity) * mix(vec3(0.04), diffuse, metalness); - gl_FragColor = vec4(diffuse * (1.0 - specular) + specular, 1.0) * (1.0 + emissive); - - if (depthWidth > 0.0) { - int arrayIndex = 0; - vec2 depthUv; - if (gl_FragCoord.x>=depthWidth) { - arrayIndex = 1; - depthUv = vec2((gl_FragCoord.x-depthWidth)/depthWidth, gl_FragCoord.y/depthHeight); - } else { - depthUv = vec2(gl_FragCoord.x/depthWidth, gl_FragCoord.y/depthHeight); - } - float assetDepthM = gl_FragCoord.z; - - float occlusion = Depth_GetBlurredOcclusionAroundUV(depthColor, depthUv, assetDepthM, arrayIndex); - float depthMm = Depth_GetCameraDepthInMeters(depthColor, depthUv, arrayIndex); - - float absDistance = abs(assetDepthM - depthMm); - float v = 0.0025; - absDistance = saturate(v - absDistance) / v; - - gl_FragColor.rgb += vec3(absDistance * 2.0, absDistance * 2.0, absDistance * 12.0); - gl_FragColor = mix(gl_FragColor, vec4(0.0, 0.0, 0.0, 0.0), occlusion * 0.7); - } - } - `, - - uniforms: { - diffuseColor: { value: new THREE.Color(Math.random() * 0xffffff) }, - roughness: { value: 0.7 }, - metalness: { value: 0.0 }, - emissive: { value: 0.0 }, - depthWidth: { value: 0.0 }, - depthHeight: { value: 0.0 }, - depthColor: { value: new THREE.Texture() }, - }, - }); - - const object = new THREE.Mesh(geometry, material); - - object.position.x = Math.random() * 4 - 2; - object.position.y = Math.random() * 2; - object.position.z = Math.random() * 4 - 2; - - object.rotation.x = Math.random() * 2 * Math.PI; - object.rotation.y = Math.random() * 2 * Math.PI; - object.rotation.z = Math.random() * 2 * Math.PI; - - object.scale.setScalar(Math.random() + 0.5); - - group.add(object); - } - - // - - renderer = new THREE.WebGLRenderer({ antialias: true }); - renderer.setPixelRatio(window.devicePixelRatio); - renderer.setSize(window.innerWidth, window.innerHeight); - renderer.shadowMap.enabled = true; - renderer.xr.enabled = true; - container.appendChild(renderer.domElement); - - document.body.appendChild( - XRButton.createButton(renderer, { - optionalFeatures: ['depth-sensing'], - depthSensing: { usagePreference: ['gpu-optimized'], dataFormatPreference: [] }, - }), - ); - - // controllers - - controller1 = renderer.xr.getController(0); - controller1.addEventListener('selectstart', onSelectStart); - controller1.addEventListener('selectend', onSelectEnd); - scene.add(controller1); - - controller2 = renderer.xr.getController(1); - controller2.addEventListener('selectstart', onSelectStart); - controller2.addEventListener('selectend', onSelectEnd); - scene.add(controller2); - - const controllerModelFactory = new XRControllerModelFactory(); - - controllerGrip1 = renderer.xr.getControllerGrip(0); - controllerGrip1.add(controllerModelFactory.createControllerModel(controllerGrip1)); - scene.add(controllerGrip1); - - controllerGrip2 = renderer.xr.getControllerGrip(1); - controllerGrip2.add(controllerModelFactory.createControllerModel(controllerGrip2)); - scene.add(controllerGrip2); - - // - - const geometry = new THREE.BufferGeometry().setFromPoints([ - new THREE.Vector3(0, 0, 0), - new THREE.Vector3(0, 0, -1), - ]); - - const line = new THREE.Line(geometry); - line.name = 'line'; - line.scale.z = 5; - - controller1.add(line.clone()); - controller2.add(line.clone()); - - raycaster = new THREE.Raycaster(); - - // - - window.addEventListener('resize', onWindowResize); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onSelectStart(event) { - const controller = event.target; - - const intersections = getIntersections(controller); - - if (intersections.length > 0) { - const intersection = intersections[0]; - - const object = intersection.object; - object.material.uniforms.emissive.value = 1; - controller.attach(object); - - controller.userData.selected = object; - } - - controller.userData.targetRayMode = event.data.targetRayMode; -} - -function onSelectEnd(event) { - const controller = event.target; - - if (controller.userData.selected !== undefined) { - const object = controller.userData.selected; - object.material.uniforms.emissive.value = 0; - group.attach(object); - - controller.userData.selected = undefined; - } -} - -function getIntersections(controller) { - controller.updateMatrixWorld(); - - tempMatrix.identity().extractRotation(controller.matrixWorld); - - raycaster.ray.origin.setFromMatrixPosition(controller.matrixWorld); - raycaster.ray.direction.set(0, 0, -1).applyMatrix4(tempMatrix); - - return raycaster.intersectObjects(group.children, false); -} - -function intersectObjects(controller) { - // Do not highlight in mobile-ar - - if (controller.userData.targetRayMode === 'screen') return; - - // Do not highlight when already selected - - if (controller.userData.selected !== undefined) return; - - const line = controller.getObjectByName('line'); - const intersections = getIntersections(controller); - - if (intersections.length > 0) { - const intersection = intersections[0]; - - const object = intersection.object; - object.material.uniforms.emissive.value = 1; - intersected.push(object); - - line.scale.z = intersection.distance; - } else { - line.scale.z = 5; - } -} - -function cleanIntersected() { - while (intersected.length) { - const object = intersected.pop(); - object.material.uniforms.emissive.value = 0; - } -} - -// - -function animate() { - renderer.setAnimationLoop(render); -} - -function render() { - if (renderer.xr.hasDepthSensing() && !isDepthSupplied) { - group.children.forEach(child => { - child.material.uniforms.depthColor.value = renderer.xr.getDepthTexture(); - child.material.uniforms.depthWidth.value = 1680; - child.material.uniforms.depthHeight.value = 1760; - - isDepthSupplied = true; - }); - } else if (!renderer.xr.hasDepthSensing() && isDepthSupplied) { - group.children.forEach(child => { - child.material.uniforms.depthWidth.value = 0; - child.material.uniforms.depthHeight.value = 0; - - isDepthSupplied = false; - }); - } - - cleanIntersected(); - - intersectObjects(controller1); - intersectObjects(controller2); - - renderer.render(scene, camera); -} diff --git a/types/three/src/nodes/display/ColorAdjustmentNode.d.ts b/types/three/src/nodes/display/ColorAdjustmentNode.d.ts index b49345bbb..517407fce 100644 --- a/types/three/src/nodes/display/ColorAdjustmentNode.d.ts +++ b/types/three/src/nodes/display/ColorAdjustmentNode.d.ts @@ -33,7 +33,7 @@ export const hue: ( adjustmentNode?: NodeRepresentation, ) => ShaderNodeObject; -export const luminance: (a: NodeRepresentation, b: NodeRepresentation) => ShaderNodeObject; +export const luminance: (a: NodeRepresentation, b?: NodeRepresentation) => ShaderNodeObject; export const threshold: (color: NodeRepresentation, thershold: NodeRepresentation) => ShaderNodeObject;